From patchwork Wed Jan 13 09:49:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Archie Pusaka X-Patchwork-Id: 362260 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3DDDC433E0 for ; Wed, 13 Jan 2021 09:50:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6B7A5233A2 for ; Wed, 13 Jan 2021 09:50:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726989AbhAMJuU (ORCPT ); Wed, 13 Jan 2021 04:50:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726840AbhAMJuU (ORCPT ); Wed, 13 Jan 2021 04:50:20 -0500 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9516C061794 for ; Wed, 13 Jan 2021 01:49:39 -0800 (PST) Received: by mail-qv1-xf4a.google.com with SMTP id k16so1003534qve.19 for ; Wed, 13 Jan 2021 01:49:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=/VNAtsVw6enMHnDDbVjZRt46WOBcUozQBtnp1MuA0cc=; b=WlqIydCS5Psxv3ojlyqGV1Ne+qW/U5PhdVpHfSpB3TV4jQoy/wFZCtt/usRtNlRGKT d5knZhgor8bk44BCUyZguT+Nt5LoIIiZw3UaFcuRIvUoYKJJjqX1KMaRYIlElO9+aYUs kYGbfHA5OPd385B7QAwL9EyaQ4j2Hdw1PV/g8I27G1i9fnawNSuDsHYJjA85y94Py72B 2IZchXFKNK3A3zeQcQzzSYiz89DWUFtutKBwd3Zrk1f4qnaOpchMZ2nYvPFudWSQZPvd EETY/iHwV1zmacBA6BFWgj9tPzXRQxAVEL36P/U7lWoH1nIijahGfNFaK116d4o5cGvj gO6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=/VNAtsVw6enMHnDDbVjZRt46WOBcUozQBtnp1MuA0cc=; b=mLQw7odFlUYbR4uQcAQX73YEHBnb1+hJcL8TMc5OvHpVC0HmBTnhti+SgFCv7Q4naz X48fN6iTvb74fMrP3v/qAAbMNzFIkUAFpvMAGjFprqFWQMpAF08u7YM/gTuYknFiRvX7 xGQQojZZ/mCXD9IiQtYbyFHKhrBd6UhvZ1A8/govIn8KdqSOMzwYJ3TX2ImePBwHBoo6 pXdGKnifjO3hpVRNuc/ZHMQ+4Zw6JyzoNBZpMNxYrjnuVVvF5pGJrAxOxg894+Q632dV rX990DDwGkNXXsry9I6rRs/EOGHIliudCFSdca2mOnZGBkxjN7Wsl+lF31irGsjy+6wK e6Vw== X-Gm-Message-State: AOAM532UI2MdklvXZM5Sy4ac0AxduYn+aDGl74qNNs+68uPlAfoJ/moT dS0qrf/IXL+LkJEnmDfoxcY5eAJfgpO39zRiBD2R9GBojWEE2WYgdczsjK+PyY5U6fddXKVvVrZ hbI+Ak14ZlGXQ7KmzER4UYS1pA6FyvqqBofPYbSLqpC1qagtEXL24jt01PjXjZxzLOtBYY96RiX sR X-Google-Smtp-Source: ABdhPJxO1aVfYqUSjL1JVTbiiCOBl0oew4DJqnVwdGMnCyauPeXjJM15PDKCJu0GD4PIrGa80ydUp99ztBw1 Sender: "apusaka via sendgmr" X-Received: from apusaka-p920.tpe.corp.google.com ([2401:fa00:1:b:f693:9fff:fef4:2347]) (user=apusaka job=sendgmr) by 2002:ad4:4e31:: with SMTP id dm17mr1531187qvb.27.1610531379016; Wed, 13 Jan 2021 01:49:39 -0800 (PST) Date: Wed, 13 Jan 2021 17:49:02 +0800 In-Reply-To: <20210113094905.2787919-1-apusaka@google.com> Message-Id: <20210113174829.Bluez.v2.2.I9a37334cdf4a6eaadc1850f5a777e724e9a7081c@changeid> Mime-Version: 1.0 References: <20210113094905.2787919-1-apusaka@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [Bluez PATCH v2 2/5] src/adv_monitor: add monitor with rssi support for mgmt From: Archie Pusaka To: linux-bluetooth , Luiz Augusto von Dentz Cc: CrosBT Upstreaming , Archie Pusaka , Yun-Hao Chung Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Archie Pusaka Using the new opcode MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI to monitor advertisement according to some RSSI criteria. Reviewed-by: Yun-Hao Chung --- (no changes since v1) src/adv_monitor.c | 143 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 110 insertions(+), 33 deletions(-) diff --git a/src/adv_monitor.c b/src/adv_monitor.c index 5a0498ec2e..bd53e0a0d9 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -44,6 +44,8 @@ #define ADV_MONITOR_UNSET_TIMER 0 /* second */ #define ADV_MONITOR_MIN_TIMER 1 /* second */ #define ADV_MONITOR_MAX_TIMER 300 /* second */ +#define ADV_MONITOR_UNSET_SAMPLING_PERIOD 256 /* 100 ms */ +#define ADV_MONITOR_MAX_SAMPLING_PERIOD 255 /* 100 ms */ struct btd_adv_monitor_manager { struct btd_adapter *adapter; @@ -95,6 +97,10 @@ struct adv_monitor { uint16_t high_rssi_timeout; /* High RSSI threshold timeout */ int8_t low_rssi; /* Low RSSI threshold */ uint16_t low_rssi_timeout; /* Low RSSI threshold timeout */ + uint16_t sampling_period; /* Merge packets in the same timeslot. + * Currenly unimplemented in user space. + * Used only to pass data to kernel. + */ struct queue *devices; /* List of adv_monitor_device objects */ enum monitor_type type; /* MONITOR_TYPE_* */ @@ -363,6 +369,7 @@ static struct adv_monitor *monitor_new(struct adv_monitor_app *app, monitor->high_rssi_timeout = ADV_MONITOR_UNSET_TIMER; monitor->low_rssi = ADV_MONITOR_UNSET_RSSI; monitor->low_rssi_timeout = ADV_MONITOR_UNSET_TIMER; + monitor->sampling_period = ADV_MONITOR_UNSET_SAMPLING_PERIOD; monitor->devices = queue_new(); monitor->type = MONITOR_TYPE_NONE; @@ -432,6 +439,7 @@ static bool parse_rssi_and_timeout(struct adv_monitor *monitor, DBusMessageIter prop_struct, iter; int16_t h_rssi, l_rssi; uint16_t h_rssi_timer, l_rssi_timer; + int16_t sampling_period; uint16_t adapter_id = monitor->app->manager->adapter_id; /* Property RSSIThresholdsAndTimers is optional */ @@ -473,6 +481,13 @@ static bool parse_rssi_and_timeout(struct adv_monitor *monitor, if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT16) goto failed; dbus_message_iter_get_basic(&iter, &l_rssi_timer); + if (!dbus_message_iter_next(&iter)) + goto failed; + + /* Extract SamplingPeriod */ + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT16) + goto failed; + dbus_message_iter_get_basic(&iter, &sampling_period); /* Verify the values of RSSIs and their timers. For simplicity, we * enforce the all-or-none rule to these fields. In other words, either @@ -481,7 +496,8 @@ static bool parse_rssi_and_timeout(struct adv_monitor *monitor, if (h_rssi == ADV_MONITOR_UNSET_RSSI && l_rssi == ADV_MONITOR_UNSET_RSSI && h_rssi_timer == ADV_MONITOR_UNSET_TIMER && - l_rssi_timer == ADV_MONITOR_UNSET_TIMER) { + l_rssi_timer == ADV_MONITOR_UNSET_TIMER && + sampling_period == ADV_MONITOR_UNSET_SAMPLING_PERIOD) { goto done; } @@ -498,17 +514,22 @@ static bool parse_rssi_and_timeout(struct adv_monitor *monitor, goto failed; } + if (sampling_period > ADV_MONITOR_MAX_SAMPLING_PERIOD) + goto failed; + monitor->high_rssi = h_rssi; monitor->low_rssi = l_rssi; monitor->high_rssi_timeout = h_rssi_timer; monitor->low_rssi_timeout = l_rssi_timer; + monitor->sampling_period = sampling_period; done: DBG("Adv Monitor at %s initiated with high RSSI threshold %d, high " "RSSI threshold timeout %d, low RSSI threshold %d, low RSSI " - "threshold timeout %d", path, monitor->high_rssi, - monitor->high_rssi_timeout, monitor->low_rssi, - monitor->low_rssi_timeout); + "threshold timeout %d, sampling period %d", path, + monitor->high_rssi, monitor->high_rssi_timeout, + monitor->low_rssi, monitor->low_rssi_timeout, + monitor->sampling_period); return true; @@ -517,6 +538,7 @@ failed: monitor->low_rssi = ADV_MONITOR_UNSET_RSSI; monitor->high_rssi_timeout = ADV_MONITOR_UNSET_TIMER; monitor->low_rssi_timeout = ADV_MONITOR_UNSET_TIMER; + monitor->sampling_period = ADV_MONITOR_UNSET_SAMPLING_PERIOD; btd_error(adapter_id, "Invalid argument of property RSSIThresholdsAndTimers " @@ -673,16 +695,88 @@ static void add_adv_patterns_monitor_cb(uint8_t status, uint16_t length, DBG("Adv monitor with handle:0x%04x added", monitor->monitor_handle); } -static void monitor_copy_patterns(void *data, void *user_data) +static bool monitor_rssi_is_unset(struct adv_monitor *monitor) { - struct bt_ad_pattern *pattern = data; - struct mgmt_cp_add_adv_monitor *cp = user_data; + return monitor->high_rssi == ADV_MONITOR_UNSET_RSSI && + monitor->low_rssi == ADV_MONITOR_UNSET_RSSI && + monitor->high_rssi_timeout == ADV_MONITOR_UNSET_TIMER && + monitor->low_rssi_timeout == ADV_MONITOR_UNSET_TIMER && + monitor->low_rssi_timeout == ADV_MONITOR_UNSET_TIMER; +} - if (!pattern) - return; +/* sends MGMT_OP_ADD_ADV_PATTERNS_MONITOR */ +static bool monitor_send_add_pattern(struct adv_monitor *monitor) +{ + struct mgmt_cp_add_adv_monitor *cp = NULL; + uint8_t pattern_count, cp_len; + const struct queue_entry *e; + bool success = true; + + pattern_count = queue_length(monitor->patterns); + cp_len = sizeof(*cp) + pattern_count * sizeof(struct mgmt_adv_pattern); + + cp = malloc0(cp_len); + if (!cp) + return false; - memcpy(cp->patterns + cp->pattern_count, pattern, sizeof(*pattern)); - cp->pattern_count++; + for (e = queue_get_entries(monitor->patterns); e; e = e->next) { + struct bt_ad_pattern *pattern = e->data; + + memcpy(&cp->patterns[cp->pattern_count++], pattern, + sizeof(*pattern)); + } + + if (!mgmt_send(monitor->app->manager->mgmt, + MGMT_OP_ADD_ADV_PATTERNS_MONITOR, + monitor->app->manager->adapter_id, cp_len, cp, + add_adv_patterns_monitor_cb, monitor, NULL)) { + error("Unable to send Add Adv Patterns Monitor command"); + success = false; + } + + free(cp); + return success; +} + +/* sends MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI */ +static bool monitor_send_add_pattern_rssi(struct adv_monitor *monitor) +{ + struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = NULL; + uint8_t pattern_count, cp_len; + const struct queue_entry *e; + bool success = true; + + pattern_count = queue_length(monitor->patterns); + cp_len = sizeof(*cp) + pattern_count * sizeof(struct mgmt_adv_pattern); + + cp = malloc0(cp_len); + if (!cp) + return false; + + cp->rssi.high_threshold = monitor->high_rssi; + /* High threshold timeout is unsupported in kernel. Value must be 0. */ + cp->rssi.high_threshold_timeout = 0; + cp->rssi.low_threshold = monitor->low_rssi; + cp->rssi.low_threshold_timeout = htobs(monitor->low_rssi_timeout); + cp->rssi.sampling_period = monitor->sampling_period; + + for (e = queue_get_entries(monitor->patterns); e; e = e->next) { + struct bt_ad_pattern *pattern = e->data; + + memcpy(&cp->patterns[cp->pattern_count++], pattern, + sizeof(*pattern)); + } + + if (!mgmt_send(monitor->app->manager->mgmt, + MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, + monitor->app->manager->adapter_id, cp_len, cp, + add_adv_patterns_monitor_cb, monitor, NULL)) { + error("Unable to send Add Adv Patterns Monitor RSSI command"); + success = false; + } + + free(cp); + return success; } /* Handles an Adv Monitor D-Bus proxy added event */ @@ -690,8 +784,6 @@ static void monitor_proxy_added_cb(GDBusProxy *proxy, void *user_data) { struct adv_monitor *monitor; struct adv_monitor_app *app = user_data; - struct mgmt_cp_add_adv_monitor *cp = NULL; - uint8_t pattern_count, cp_len; uint16_t adapter_id = app->manager->adapter_id; const char *path = g_dbus_proxy_get_path(proxy); const char *iface = g_dbus_proxy_get_interface(proxy); @@ -725,24 +817,12 @@ static void monitor_proxy_added_cb(GDBusProxy *proxy, void *user_data) queue_push_tail(app->monitors, monitor); - pattern_count = queue_length(monitor->patterns); - cp_len = sizeof(struct mgmt_cp_add_adv_monitor) + - pattern_count * sizeof(struct mgmt_adv_pattern); - - cp = malloc0(cp_len); - queue_foreach(monitor->patterns, monitor_copy_patterns, cp); - - if (!mgmt_send(app->manager->mgmt, MGMT_OP_ADD_ADV_PATTERNS_MONITOR, - adapter_id, cp_len, cp, add_adv_patterns_monitor_cb, - monitor, NULL)) { - error("Unable to send Add Adv Patterns Monitor command"); - goto done; - } + if (monitor_rssi_is_unset(monitor)) + monitor_send_add_pattern(monitor); + else + monitor_send_add_pattern_rssi(monitor); DBG("Adv Monitor allocated for the object at path %s", path); - -done: - free(cp); } /* Handles the removal of an Adv Monitor D-Bus proxy */ @@ -1428,10 +1508,7 @@ static void adv_monitor_filter_rssi(struct adv_monitor *monitor, * DeviceFound() event without tracking for the RSSI as the Adv has * already matched the pattern filter. */ - if (monitor->high_rssi == ADV_MONITOR_UNSET_RSSI && - monitor->low_rssi == ADV_MONITOR_UNSET_RSSI && - monitor->high_rssi_timeout == ADV_MONITOR_UNSET_TIMER && - monitor->low_rssi_timeout == ADV_MONITOR_UNSET_TIMER) { + if (monitor_rssi_is_unset(monitor)) { DBG("Calling DeviceFound() on Adv Monitor of owner %s " "at path %s", monitor->app->owner, monitor->path); From patchwork Wed Jan 13 09:49:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Archie Pusaka X-Patchwork-Id: 362259 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3CE9BC433DB for ; Wed, 13 Jan 2021 09:50:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E488E233A2 for ; Wed, 13 Jan 2021 09:50:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727433AbhAMJu2 (ORCPT ); Wed, 13 Jan 2021 04:50:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727009AbhAMJu1 (ORCPT ); Wed, 13 Jan 2021 04:50:27 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74C1EC06179F for ; Wed, 13 Jan 2021 01:49:47 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id d84so1003977pfd.21 for ; Wed, 13 Jan 2021 01:49:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=O7sBjXQyZolLQj0CozKoeHqkp2oHZqJXh9vy848KggY=; b=iO7sLAR5X6JDTZHLqkknSbvvTZg9HzRbBu4l7QglBvV13CSpaeq1ENISwimWRX01D7 HvjAfYZPfnUebhZIOyjY6DQF9zdMuL1NXY5F3HZB2+jIbUbLjbQx8D23jvbK8mioa2ZK IW6g7IFlWML77s2H65lWWm8a1J2hmPvVvumkieHi4kNKlI6fOgfCFo3VkzGtuWBo80uV YTTHJ3kjOFZSzjU5brwvZvnHSe3QpOSVWroj5WMPO/VSA+yCZmbTb9FmZ/kO8dzNsMrw d0RzEwj9hZHOe8QYPaaVNQWAKot4yTO4Y7YmYIkoq0x4U80GFM+jnvXrmYXPmT/lBUFB pBlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=O7sBjXQyZolLQj0CozKoeHqkp2oHZqJXh9vy848KggY=; b=gjCQja94zHbV+L2/zuS4jQmd6Q+mwOAPz34W/1kRKM3DlcLOpffB1pLM6TadZ9CWCT wG52twgxW4Qzm2fT8EaumpZ8XL7Lxb6kd86EtPsPXjFDu0MA4zjJv4T3FmAKVrDzyjP4 of58ho49yPCW8M3RX2nKuIf2SW48PJZASE0mVsky0YACjINADRlL3e3NRls7Afg0WZcq iz8WkzOuB6TBSbPHdUooGvcyqili47/7QohCrydD+LADII0QVLlPDqC17W95kaBQilik au+pvU0pBgdH33jr901tG+L261bfhJkDvI1Thgk5CQbeW7k6bHnwMCEb4md6zOKdwIsM 5USw== X-Gm-Message-State: AOAM532eQdxuQOIuRqt98jb09KtIQD+bnzziZB+eH1RUkBuPXimUEW9m bS8038V/Bnyfuqnw8aPmgu0gKposcWHOucE0U0fLhggo+nejeEV3voexhEhP0FxpTXiSya4Zikb znW9LTID8Vs9XVOUEqxoojti9X0rsry4pqdqT0dOa6/3MAbpfXDt9SCxe24BbJ3rlNfVygh+/Az ei X-Google-Smtp-Source: ABdhPJzVJdaYIAUfZlk5jQhPLiJ+DaAIOt8zCN1GFobGx5PVSk28HFoiRqhJHR0OEThb0VmZuiKW2tjMFVRR Sender: "apusaka via sendgmr" X-Received: from apusaka-p920.tpe.corp.google.com ([2401:fa00:1:b:f693:9fff:fef4:2347]) (user=apusaka job=sendgmr) by 2002:a17:90a:8b94:: with SMTP id z20mr1085375pjn.1.1610531386632; Wed, 13 Jan 2021 01:49:46 -0800 (PST) Date: Wed, 13 Jan 2021 17:49:04 +0800 In-Reply-To: <20210113094905.2787919-1-apusaka@google.com> Message-Id: <20210113174829.Bluez.v2.4.I20391efb1b5a40cd2b0cb6069d88b7fb9f7ed66b@changeid> Mime-Version: 1.0 References: <20210113094905.2787919-1-apusaka@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [Bluez PATCH v2 4/5] bluetoothctl: advmon rssi support for mgmt From: Archie Pusaka To: linux-bluetooth , Luiz Augusto von Dentz Cc: CrosBT Upstreaming , Archie Pusaka , Yun-Hao Chung Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Archie Pusaka Using the new opcode MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI to monitor advertisement according to some RSSI criteria. Reviewed-by: Yun-Hao Chung --- (no changes since v1) client/adv_monitor.c | 90 ++++++++++++++++++++++---------------------- client/adv_monitor.h | 1 + client/main.c | 29 +++++++------- 3 files changed, 59 insertions(+), 61 deletions(-) diff --git a/client/adv_monitor.c b/client/adv_monitor.c index f62e9f4442..74f7581776 100644 --- a/client/adv_monitor.c +++ b/client/adv_monitor.c @@ -33,6 +33,7 @@ struct rssi_setting { uint16_t high_timer; int16_t low_threshold; uint16_t low_timer; + uint16_t sampling_period; }; struct pattern { @@ -148,6 +149,8 @@ static gboolean get_rssi(const GDBusPropertyTable *property, &rssi->low_threshold); dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_UINT16, &rssi->low_timer); + dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_UINT16, + &rssi->sampling_period); dbus_message_iter_close_container(iter, &data_iter); return TRUE; } @@ -212,7 +215,7 @@ static gboolean pattern_exists(const GDBusPropertyTable *property, void *data) static const GDBusPropertyTable adv_monitor_props[] = { { "Type", "s", get_type }, - { "RSSIThresholdsAndTimers", "(nqnq)", get_rssi, NULL, rssi_exists }, + { "RSSIThresholdsAndTimers", "(nqnqq)", get_rssi, NULL, rssi_exists }, { "Patterns", "a(yyay)", get_patterns, NULL, pattern_exists }, { } }; @@ -376,56 +379,51 @@ static uint8_t str2bytearray(char *str, uint8_t *arr) return arr_len; } -static void parse_rssi_value_pair(char *value_pair, int *low, int *high) -{ - char *val1, *val2; - bool flag = value_pair[0] == ','; - - val1 = strtok(value_pair, ","); - - if (!val1) - return; - - val2 = strtok(NULL, ","); - - if (!val2) { - if (!flag) - *low = atoi(val1); - else - *high = atoi(val1); - } else { - *low = atoi(val1); - *high = atoi(val2); - } -} - -static struct rssi_setting *parse_rssi(char *range, char *timeout) +static struct rssi_setting *parse_rssi(char *params) { struct rssi_setting *rssi; - int high_threshold, low_threshold, high_timer, low_timer; - - high_threshold = RSSI_DEFAULT_HIGH_THRESHOLD; - low_threshold = RSSI_DEFAULT_LOW_THRESHOLD; - high_timer = RSSI_DEFAULT_HIGH_TIMEOUT; - low_timer = RSSI_DEFAULT_LOW_TIMEOUT; + char *split, *endptr; + int i; + int values[5] = {RSSI_DEFAULT_LOW_THRESHOLD, + RSSI_DEFAULT_HIGH_THRESHOLD, + RSSI_DEFAULT_LOW_TIMEOUT, + RSSI_DEFAULT_HIGH_TIMEOUT, + RSSI_DEFAULT_SAMPLING_PERIOD}; + + for (i = 0; i < 5; i++) { + if (!params) /* Params too short */ + goto bad_format; + + split = strsep(¶ms, ","); + if (*split != '\0') { + values[i] = strtol(split, &endptr, 0); + if (*endptr != '\0') /* Conversion failed */ + goto bad_format; + } /* Otherwise no parsing needed - use default */ + } - parse_rssi_value_pair(range, &low_threshold, &high_threshold); - parse_rssi_value_pair(timeout, &low_timer, &high_timer); + if (params) /* There are trailing unused params */ + goto bad_format; rssi = g_malloc0(sizeof(struct rssi_setting)); - if (!rssi) { - bt_shell_printf("Failed to allocate rssi_setting"); + bt_shell_printf("Failed to allocate rssi_setting\n"); bt_shell_noninteractive_quit(EXIT_FAILURE); return NULL; } - rssi->high_threshold = high_threshold; - rssi->high_timer = high_timer; - rssi->low_threshold = low_threshold; - rssi->low_timer = low_timer; + rssi->low_threshold = values[0]; + rssi->high_threshold = values[1]; + rssi->low_timer = values[2]; + rssi->high_timer = values[3]; + rssi->sampling_period = values[4]; return rssi; + +bad_format: + bt_shell_printf("Failed to parse RSSI\n"); + bt_shell_noninteractive_quit(EXIT_FAILURE); + return NULL; } static struct pattern *parse_pattern(char *parameter_list[]) @@ -435,7 +433,7 @@ static struct pattern *parse_pattern(char *parameter_list[]) pat = g_malloc0(sizeof(struct pattern)); if (!pat) { - bt_shell_printf("Failed to allocate pattern"); + bt_shell_printf("Failed to allocate pattern\n"); bt_shell_noninteractive_quit(EXIT_FAILURE); return NULL; } @@ -537,6 +535,8 @@ static void print_adv_monitor(struct adv_monitor *adv_monitor) adv_monitor->rssi->low_threshold); bt_shell_printf("\t\tlow threshold timer: %hu\n", adv_monitor->rssi->low_timer); + bt_shell_printf("\t\tsampling period: %hu\n", + adv_monitor->rssi->sampling_period); } if (adv_monitor->patterns) { @@ -572,15 +572,15 @@ void adv_monitor_add_monitor(DBusConnection *conn, char *type, while (find_adv_monitor_with_idx(adv_mon_idx)) adv_mon_idx += 1; - if (rssi_enabled == FALSE) + if (rssi_enabled == FALSE) { rssi = NULL; - else { - rssi = parse_rssi(argv[1], argv[2]); + } else { + rssi = parse_rssi(argv[1]); if (rssi == NULL) return; - argv += 2; - argc -= 2; + argv += 1; + argc -= 1; } patterns = parse_patterns(argv+1, argc-1); diff --git a/client/adv_monitor.h b/client/adv_monitor.h index dd6f615799..2bdc447265 100644 --- a/client/adv_monitor.h +++ b/client/adv_monitor.h @@ -12,6 +12,7 @@ #define RSSI_DEFAULT_LOW_THRESHOLD -70 #define RSSI_DEFAULT_HIGH_TIMEOUT 10 #define RSSI_DEFAULT_LOW_TIMEOUT 5 +#define RSSI_DEFAULT_SAMPLING_PERIOD 0 void adv_monitor_add_manager(DBusConnection *conn, GDBusProxy *proxy); void adv_monitor_remove_manager(DBusConnection *conn); diff --git a/client/main.c b/client/main.c index 9403f1af6e..5d84e7cd54 100644 --- a/client/main.c +++ b/client/main.c @@ -2709,26 +2709,23 @@ static void cmd_ad_clear(int argc, char *argv[]) static void print_add_or_pattern_with_rssi_usage(void) { - bt_shell_printf("rssi-range format:\n" - "\t,\n" - "\tBoth parameters can be skipped, in that case the\n" - "\tparamter will be set to its pre-defined value\n"); - bt_shell_printf("\tPre-defined low-rssi,high-rssi: %d,%d\n", - RSSI_DEFAULT_LOW_THRESHOLD, - RSSI_DEFAULT_HIGH_THRESHOLD); - bt_shell_printf("timeout format:\n" - "\t,\n" - "\tBoth parameters can be skipped, in that case the\n" - "\tparamter will be set to its pre-defined value\n"); - bt_shell_printf("\tPre-defined low-timeout,high-timeout: %d,%d\n", - RSSI_DEFAULT_LOW_TIMEOUT, + bt_shell_printf("rssi format:\n" + "\t,,," + ",\n" + "\tAll parameters can be skipped, in that case they\n" + "\twill be set to pre-defined values, which are:\n"); + bt_shell_printf("\t\tlow-rssi: %d\n", RSSI_DEFAULT_LOW_THRESHOLD); + bt_shell_printf("\t\thigh-rssi: %d\n", RSSI_DEFAULT_HIGH_THRESHOLD); + bt_shell_printf("\t\tlow-rssi-timeout: %d\n", RSSI_DEFAULT_LOW_TIMEOUT); + bt_shell_printf("\t\thigh-rssi-timeout: %d\n", RSSI_DEFAULT_HIGH_TIMEOUT); + bt_shell_printf("\t\tsampling: %d\n", RSSI_DEFAULT_SAMPLING_PERIOD); bt_shell_printf("pattern format:\n" "\t \n"); bt_shell_printf("e.g.\n" - "\tadd-or-pattern-rssi -10, ,10 1 2 01ab55\n"); + "\tadd-or-pattern-rssi -10,,,10,0 1 2 01ab55\n"); bt_shell_printf("or\n" - "\tadd-or-pattern-rssi -50,-30 , 1 2 01ab55 3 4 23cd66\n"); + "\tadd-or-pattern-rssi -50,-30,,, 1 2 01ab55 3 4 23cd66\n"); } static void print_add_or_pattern_usage(void) @@ -2826,7 +2823,7 @@ static const struct bt_shell_menu advertise_monitor_menu = { .name = "monitor", .desc = "Advertisement Monitor Options Submenu", .entries = { - { "add-or-pattern-rssi", " " + { "add-or-pattern-rssi", " " "[patterns=pattern1 pattern2 ...]", cmd_adv_monitor_add_or_monitor_with_rssi, "Add 'or pattern' type monitor with RSSI "