From patchwork Fri Jan 15 11:50:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Archie Pusaka X-Patchwork-Id: 12022339 X-Patchwork-Delegate: luiz.dentz@gmail.com 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 85736C433E0 for ; Fri, 15 Jan 2021 11:52:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E96C23877 for ; Fri, 15 Jan 2021 11:52:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728163AbhAOLwI (ORCPT ); Fri, 15 Jan 2021 06:52:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726384AbhAOLwH (ORCPT ); Fri, 15 Jan 2021 06:52:07 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77056C061757 for ; Fri, 15 Jan 2021 03:51:27 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id m203so5941116ybf.1 for ; Fri, 15 Jan 2021 03:51:27 -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=/XIu1yF15XLDDCBtIKD5HIRHdICPE4XqWji3+k8jq2Y=; b=hr2stXiRUMxi7ghwM2ScROiGLI0ZgJHG3Qu8n4NpT9RnPgeV+oJrxyqzEUk+dmSH9C qkAdtcF+Pv+rr0UEjQmeWtoH/u7YPNkZnr/8Ed9Bow7vUXYRhlvSg9evg/S1YuG0US8a 051tJglgOGwjIsJZNV/YILPaLxd2RftaxKsIFsGVc7UqWzEJAOz/1A0zKpc9TMKqVsae GwZDN/AmbY1OeUgaYdh28GUUisjQ+fm+xUXSR7PerBoF2S7ClB8yEC80khYaiyVeAYjE NLxnoBeb/H96GF+OJpVbyg4CE2jKIq1wTJkpxTg61lYefvY3FkW8MEscbewBa431lDfB P5ug== 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=/XIu1yF15XLDDCBtIKD5HIRHdICPE4XqWji3+k8jq2Y=; b=TpGwK6rCsmn/HXsvJobirw0WUkSHrjc4LbyLJi6t4KX/hIs9iwbOn2DW3YKAmiRQjB YQB8mmWQDY/8zNxIDNEVjLNtNsb3jLLrHLrTIxnTSNzKG6naZSDORGyW8Xb7w2OWnCvx Dg/wkO7z7zYQ+9zNTmTLsKeSJrcCUsQWjZ76xmc6TDcrEOw4e1v9+X7LCzuCHFCb4eIq tBQ58hcZlk9ChgVmVwsr++UH/6hLdaG/lkvBITiFyPKyL3X7HrOYHMBUrKnD+97NRXVv zKT45AUZKOfAnzWPvcsu7LdsoDCYKbbSmq+5qTUeS2w2SRGOD8Na+pSi/cXjuy7ZsxU6 fpUA== X-Gm-Message-State: AOAM530mjZ2qZb4nYDbupbLFit6VP8uVWsnDgLT1eyCrEgO8UBV+kB7P lt7hE3KVQ6aS4Un9nf3LmvYf/YvbsQ/ergVehNCqFZHtyqZ9+QyDGvAAkpFJNyFIoieCz8Kvu0B xPxvtYgQd3yi4RvAoqDg4B70KmafxRw6lvV619G+IFncfCm+S1BbfgJxIQBVTpNpETsz0ZrVZgw k6 X-Google-Smtp-Source: ABdhPJxXSz3BEWMp0H7J0iiOIdZm2OAF1XxhBpYO0dk84PdA3MVG+Cd3ga44UXrhx51kDFD9+9CN67ZesSPi 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:a25:5f06:: with SMTP id t6mr15336521ybb.136.1610711486689; Fri, 15 Jan 2021 03:51:26 -0800 (PST) Date: Fri, 15 Jan 2021 19:50:34 +0800 In-Reply-To: <20210115115036.3973761-1-apusaka@google.com> Message-Id: <20210115194853.Bluez.v4.1.I90bb4b25451e7f726fc2c8771cfe642b16312ce3@changeid> Mime-Version: 1.0 References: <20210115115036.3973761-1-apusaka@google.com> X-Mailer: git-send-email 2.30.0.296.g2bfb1c46d8-goog Subject: [Bluez PATCH v4 1/6] lib/mgmt: Adding Add Adv Patterns Monitor RSSI opcode From: Archie Pusaka To: linux-bluetooth , Luiz Augusto von Dentz Cc: CrosBT Upstreaming , Archie Pusaka , Miao-chen Chou , Yun-Hao Chung , Manish Mandlik Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Archie Pusaka The new op is to utilize RSSI in advertisement monitor Reviewed-by: Miao-chen Chou Reviewed-by: Yun-Hao Chung Reviewed-by: Manish Mandlik --- (no changes since v2) Changes in v2: * Remove trailing period and fix order of mgmt parameter lib/mgmt.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/mgmt.h b/lib/mgmt.h index f37f7e6540..76a03c9c24 100644 --- a/lib/mgmt.h +++ b/lib/mgmt.h @@ -738,6 +738,21 @@ struct mgmt_rp_add_ext_adv_data { uint8_t instance; } __packed; +struct mgmt_adv_rssi_thresholds { + int8_t high_threshold; + uint16_t high_threshold_timeout; + int8_t low_threshold; + uint16_t low_threshold_timeout; + uint8_t sampling_period; +} __packed; + +#define MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI 0x0056 +struct mgmt_cp_add_adv_patterns_monitor_rssi { + struct mgmt_adv_rssi_thresholds rssi; + uint8_t pattern_count; + struct mgmt_adv_pattern patterns[0]; +} __packed; + #define MGMT_EV_CMD_COMPLETE 0x0001 struct mgmt_ev_cmd_complete { uint16_t opcode; From patchwork Fri Jan 15 11:50:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Archie Pusaka X-Patchwork-Id: 12022341 X-Patchwork-Delegate: luiz.dentz@gmail.com 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=-21.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 F3342C433DB for ; Fri, 15 Jan 2021 11:52:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CC2D723730 for ; Fri, 15 Jan 2021 11:52:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726384AbhAOLwZ (ORCPT ); Fri, 15 Jan 2021 06:52:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725880AbhAOLwY (ORCPT ); Fri, 15 Jan 2021 06:52:24 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5606BC061793 for ; Fri, 15 Jan 2021 03:51:44 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id r185so5924974ybf.9 for ; Fri, 15 Jan 2021 03:51:44 -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=XSwZnH2kh3Ehe2Kkqwf/EwstAQB1eAD/12eZMBoUKH0=; b=U+BZTWn9OC/ox1fukWuanuAi9j0NbQf08J6c5kNeLXbRoWBjOsMkpyecqMEkg70E1R j4kECokR9igOMU2fKD0ZUxb35xUTAaa2WpCoMgS+Li+8JMGZ/CCfkbASHo8Q6jsynH6S tV6Y4JsLm2mILAi8Z6Isg3Q5ISxiN0WhEjsOyQ6F2654DMnTvaLsMH1+Ru0jqetdXEjc 9tntfXR2Edk1qiV+OhHw03HaMpV6qJAac1BcKyGtMLIU61/FQ1H83rdNAS38nslD9APE aHW0DsIDtoiU0871XrEluOciYX9hYMVs+t/+yHxUD1xypVxumM+W5S3xvszif8rPWldb 9BPg== 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=XSwZnH2kh3Ehe2Kkqwf/EwstAQB1eAD/12eZMBoUKH0=; b=lfXB+63E2xsKBlL1NviyVjD80kc7yRHmnA9kE/fANYj7hBuF6mYwXX+lVUTjh920Nf zzChh9UlTRLDaeaIwFIEJ5f9J80uK7y26KzG3k6H9VxdBn78XUV1V3gGRJLaoE8kmAg1 owgNBpQ5DHJHQuipoJkMZQy8pTyIu6zJRQavPZmI72CtqpdtXTLIwfTEP9SFe7tm47QK Q33Bl+QdsN76vfncueP8UUPLbwzhA/E+E8RoZWR2dZaY6c4O5kvvM17vmPR09zo4POCj XHAWkKtWPzecuFzKoW/JXVnd1VjhEqEkuWcLEb4IWuxY3w9nrAE+TyVmmkqrG5HSliNt w3nw== X-Gm-Message-State: AOAM531oCDRMuz1JlhFApRJ8SQUIIc86SXvtqfXiYtpMms2nEsD+YSW2 2sos//Jog5/YLlitbbgFTmC0aT5PFSUzcpJmK88o/BD+Dfqz9iycYA3OZiORaW+T9pqVuCtqbU9 XN+rGUSPVmsOM9Zlue3McTyI1a6YL6tYuJKQJ0ceRWFlVhAJn1M7IKkDlO5EfrZaawwax2rQd6k Sq X-Google-Smtp-Source: ABdhPJwYB1KcA/hgk/xHinzSZ0yFih/2qPAcyexphqHsQgZBKjFLNyiVUaJmf9sE4mruWz+pKAVvMzBW9MoG 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:a25:374c:: with SMTP id e73mr18082555yba.376.1610711503567; Fri, 15 Jan 2021 03:51:43 -0800 (PST) Date: Fri, 15 Jan 2021 19:50:37 +0800 In-Reply-To: <20210115115036.3973761-1-apusaka@google.com> Message-Id: <20210115194853.Bluez.v4.2.I5e5ea3e2d73840e65cd42fccafe14427f6f81de7@changeid> Mime-Version: 1.0 References: <20210115115036.3973761-1-apusaka@google.com> X-Mailer: git-send-email 2.30.0.296.g2bfb1c46d8-goog Subject: [Bluez PATCH v4 2/6] doc/advmon-api: Introduce sampling period property From: Archie Pusaka To: linux-bluetooth , Luiz Augusto von Dentz Cc: CrosBT Upstreaming , Archie Pusaka Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Archie Pusaka The sampling period is used to align with the new MGMT opcode which also have sampling period. --- Changes in v4: * update doc doc/advertisement-monitor-api.txt | 78 +++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/doc/advertisement-monitor-api.txt b/doc/advertisement-monitor-api.txt index 92c8ffc38e..8100717c03 100644 --- a/doc/advertisement-monitor-api.txt +++ b/doc/advertisement-monitor-api.txt @@ -49,31 +49,59 @@ Properties string Type [read-only] org.bluez.AdvertisementMonitorManager1 for the available options. - (Int16, Uint16, Int16, Uint16) RSSIThresholdsAndTimers [read-only, optional] - - This contains HighRSSIThreshold, HighRSSIThresholdTimer, - LowRSSIThreshold, LowRSSIThresholdTimer in order. The - unit of HighRSSIThreshold and LowRSSIThreshold is dBm. - The unit of HighRSSIThresholdTimer and - LowRSSIThresholdTimer is second. - - If these are provided, RSSI would be used as a factor to - notify the client of whether a device stays in range or - move out of range. A device is considered in-range when - the RSSIs of the received advertisement(s) during - HighRSSIThresholdTimer seconds exceed HighRSSIThreshold. - Likewise, a device is considered out-of-range when the - RSSIs of the received advertisement(s) during - LowRSSIThresholdTimer do not reach LowRSSIThreshold. - - The valid range of a RSSI is -127 to +20 dBm while 127 - dBm indicates unset. The valid range of a timer is 1 to - 300 seconds while 0 indicates unset. - - If the peer device advertising interval is greater than the - HighRSSIThresholdTimer, the device will never be found. Similarly, - if it is greater than LowRSSIThresholdTimer, the device will be - considered as lost. Consider configuring these values accordingly. + Int16 RSSILowThreshold [read-only, optional] + + Used in conjunction with RSSILowTimeout to determine + whether a device becomes out-of-range. Valid range is + -127 to 20 (dBm), while 127 indicates unset. + + Int16 RSSIHighThreshold [read-only, optional] + + Used in conjunction with RSSIHighTimeout to determine + whether a device becomes in-range. Valid range is + -127 to 20 (dBm), while 127 indicates unset. + + Uint16 RSSILowTimeout [read-only, optional] + + The time it takes to consider a device as out-of-range. + If this many seconds elapses without receiving any + signal at least as strong as RSSILowThreshold, a + currently in-range device will be considered as + out-of-range (lost). Valid range is 1 to 300 (seconds), + while 0 indicates unset. + + Uint16 RSSIHighTimeout [read-only, optional] + + The time it takes to consider a device as in-range. + If this many seconds elapses while we continuously + receive signals at least as strong as RSSIHighThreshold, + a currently out-of-range device will be considered as + in-range (found). Valid range is 1 to 300 (seconds), + while 0 indicates unset. + + Uint16 RSSISamplingPeriod [read-only, optional] + + Grouping rules on how to propagate the received + advertisement packets to the client. Valid range is 0 to + 255 while 256 indicates unset. + + The meaning of this property is as follows: + 0: + All advertisement packets from in-range devices + would be propagated. + 255: + Only the first advertisement packet of in-range + devices would be propagated. If the device + becomes lost, then the first packet when it is + found again will also be propagated. + 1 to 254: + Advertisement packets would be grouped into + 100ms * N time period. Packets in the same group + will only be reported once, with the RSSI value + being averaged out. + + Currently this is unimplemented in user space, so the + value is only used to be forwarded to the kernel. array{(uint8, uint8, array{byte})} Patterns [read-only, optional] From patchwork Fri Jan 15 11:50:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Archie Pusaka X-Patchwork-Id: 12022343 X-Patchwork-Delegate: luiz.dentz@gmail.com 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 6DCACC433E0 for ; Fri, 15 Jan 2021 11:52:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3B8932371F for ; Fri, 15 Jan 2021 11:52:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726046AbhAOLwd (ORCPT ); Fri, 15 Jan 2021 06:52:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726085AbhAOLwc (ORCPT ); Fri, 15 Jan 2021 06:52:32 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F810C061794 for ; Fri, 15 Jan 2021 03:51:52 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id e137so5958766ybf.16 for ; Fri, 15 Jan 2021 03:51:52 -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=ielgrcP49LHH1VpB3/ARC5cdWx2B3nQRImOXhZrClJk=; b=tfgbQeqEh07mZwFgWeb0868RHURUwTkPSNjNgye1SEkD8/5Z9yB0fll2MG+q0tQMM6 EGroXb4Ja75ngyUjOM3/jiGhYnczncZL+y7nH0LctaOv12FqenT6REk54Cg3uAFOLAeu NzWPRLzFpt+jef7XSFZiS4nwnQ7CtFr64eaQsiaALbmbeK5mQRXEVROhg4BzKtIx4zXY KWIaPMlSQVCJ/qAPYiVB6BD5uHzesWMyYTLn6KkVsnI73vo05gRpMjaZCjqt0ANGVxm1 FEa89/MVrtjYJdb2f+j9KMWgznsByXjDAmiWJvwp6gNnf9KYWJKEYnCeq4gsyBBhhmR8 ppYw== 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=ielgrcP49LHH1VpB3/ARC5cdWx2B3nQRImOXhZrClJk=; b=E6wcBHLBIa+yFiZEQ4d712fjAL91BvjIerIPfPZ2iOOmIhMFTFO1a6GcDwjgnIunq4 0AfoX/cWgrolpBNzn1NLh9N7i4MmtwXK7gU9FGXi1nruzEIF2NbjPdUWJbTEvmsdzbj8 ySW7ciu62wQqYFlGxwgMEMxcAP2Q3AhzWfhqyObAQRaXsmfE4gX2kIuVGRAPUYAXF9vN 1EZ9Ba+47NUUIGHOQzbpDqEAs1lFWDbXUmin+MoDrm9OzUfr0kNIh2As3WnKMcZXo5Dk +aE/xIpcdGl651MTSMYGnDmciStiB6L2yZr21WleXvDu3Jrwlu5l9MSXv3CsyWjIxOZl LnEg== X-Gm-Message-State: AOAM533E1n1sYx1X4JDUzTUteb39Ef8ghMc7UzgAckuxPhJjNS4ojBx5 wtYHdco7rLZ08lacXlSY27VKjddBPc/5nqrBJ8cZT86CrFiyFBMca4XgB9smeFjS9sIdUeNceZU +ViRgfP1QmGHoJoytip4Zij3cfMbciR7gLYYI69Mx9TSd3Ms2A1rC4z5706HJLKuyX68rbQu6FX QY X-Google-Smtp-Source: ABdhPJwu3iZkpWnzqEsOtMQZuQRpDcfft6+gGO5oWcJRX3jNVHJ2QKJM9tRAyWFxGZ9VU57gBfvMnXOI/ljJ 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:a25:6f86:: with SMTP id k128mr16946615ybc.313.1610711511783; Fri, 15 Jan 2021 03:51:51 -0800 (PST) Date: Fri, 15 Jan 2021 19:50:39 +0800 In-Reply-To: <20210115115036.3973761-1-apusaka@google.com> Message-Id: <20210115194853.Bluez.v4.3.I9a37334cdf4a6eaadc1850f5a777e724e9a7081c@changeid> Mime-Version: 1.0 References: <20210115115036.3973761-1-apusaka@google.com> X-Mailer: git-send-email 2.30.0.296.g2bfb1c46d8-goog Subject: [Bluez PATCH v4 3/6] 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 v3) Changes in v3: * split the struct RSSIThresholdsAndTimers src/adv_monitor.c | 267 ++++++++++++++++++++++++++++++---------------- 1 file changed, 174 insertions(+), 93 deletions(-) diff --git a/src/adv_monitor.c b/src/adv_monitor.c index 5a0498ec2e..54751db0b5 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -41,9 +41,14 @@ #define ADV_MONITOR_UNSET_RSSI 127 /* dBm */ #define ADV_MONITOR_MAX_RSSI 20 /* dBm */ #define ADV_MONITOR_MIN_RSSI -127 /* dBm */ -#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_TIMEOUT 0 /* second */ +#define ADV_MONITOR_MIN_TIMEOUT 1 /* second */ +#define ADV_MONITOR_MAX_TIMEOUT 300 /* second */ +#define ADV_MONITOR_DEFAULT_LOW_TIMEOUT 5 /* second */ +#define ADV_MONITOR_DEFAULT_HIGH_TIMEOUT 10 /* second */ +#define ADV_MONITOR_UNSET_SAMPLING_PERIOD 256 /* 100 ms */ +#define ADV_MONITOR_MAX_SAMPLING_PERIOD 255 /* 100 ms */ +#define ADV_MONITOR_DEFAULT_SAMPLING_PERIOD 0 /* 100 ms */ struct btd_adv_monitor_manager { struct btd_adapter *adapter; @@ -95,6 +100,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_* */ @@ -360,9 +369,10 @@ static struct adv_monitor *monitor_new(struct adv_monitor_app *app, monitor->state = MONITOR_STATE_NEW; monitor->high_rssi = ADV_MONITOR_UNSET_RSSI; - monitor->high_rssi_timeout = ADV_MONITOR_UNSET_TIMER; + monitor->high_rssi_timeout = ADV_MONITOR_UNSET_TIMEOUT; monitor->low_rssi = ADV_MONITOR_UNSET_RSSI; - monitor->low_rssi_timeout = ADV_MONITOR_UNSET_TIMER; + monitor->low_rssi_timeout = ADV_MONITOR_UNSET_TIMEOUT; + monitor->sampling_period = ADV_MONITOR_UNSET_SAMPLING_PERIOD; monitor->devices = queue_new(); monitor->type = MONITOR_TYPE_NONE; @@ -423,103 +433,119 @@ failed: return false; } -/* Retrieves RSSIThresholdsAndTimers from the remote Adv Monitor object, +/* Retrieves RSSI thresholds and timeouts from the remote Adv Monitor object, * verifies the values and update the local Adv Monitor */ static bool parse_rssi_and_timeout(struct adv_monitor *monitor, const char *path) { - DBusMessageIter prop_struct, iter; - int16_t h_rssi, l_rssi; - uint16_t h_rssi_timer, l_rssi_timer; + DBusMessageIter iter; + GDBusProxy *proxy = monitor->proxy; + int16_t h_rssi = ADV_MONITOR_UNSET_RSSI; + int16_t l_rssi = ADV_MONITOR_UNSET_RSSI; + uint16_t h_rssi_timeout = ADV_MONITOR_UNSET_TIMEOUT; + uint16_t l_rssi_timeout = ADV_MONITOR_UNSET_TIMEOUT; + int16_t sampling_period = ADV_MONITOR_UNSET_SAMPLING_PERIOD; uint16_t adapter_id = monitor->app->manager->adapter_id; - /* Property RSSIThresholdsAndTimers is optional */ - if (!g_dbus_proxy_get_property(monitor->proxy, - "RSSIThresholdsAndTimers", - &prop_struct)) { - DBG("Adv Monitor at path %s provides no RSSI thresholds and " - "timeouts", path); - return true; + /* Extract RSSIHighThreshold */ + if (g_dbus_proxy_get_property(proxy, "RSSIHighThreshold", &iter)) { + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT16) + goto failed; + dbus_message_iter_get_basic(&iter, &h_rssi); } - if (dbus_message_iter_get_arg_type(&prop_struct) != DBUS_TYPE_STRUCT) - goto failed; - - dbus_message_iter_recurse(&prop_struct, &iter); - - /* Extract HighRSSIThreshold */ - if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT16) - goto failed; - dbus_message_iter_get_basic(&iter, &h_rssi); - if (!dbus_message_iter_next(&iter)) - goto failed; + /* Extract RSSIHighTimeout */ + if (g_dbus_proxy_get_property(proxy, "RSSIHighTimeout", &iter)) { + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT16) + goto failed; + dbus_message_iter_get_basic(&iter, &h_rssi_timeout); + } - /* Extract HighRSSIThresholdTimer */ - if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT16) - goto failed; - dbus_message_iter_get_basic(&iter, &h_rssi_timer); - if (!dbus_message_iter_next(&iter)) - goto failed; + /* Extract RSSILowThreshold */ + if (g_dbus_proxy_get_property(proxy, "RSSILowThreshold", &iter)) { + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT16) + goto failed; + dbus_message_iter_get_basic(&iter, &l_rssi); + } - /* Extract LowRSSIThreshold */ - if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT16) - goto failed; - dbus_message_iter_get_basic(&iter, &l_rssi); - if (!dbus_message_iter_next(&iter)) - goto failed; + /* Extract RSSILowTimeout */ + if (g_dbus_proxy_get_property(proxy, "RSSILowTimeout", &iter)) { + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT16) + goto failed; + dbus_message_iter_get_basic(&iter, &l_rssi_timeout); + } - /* Extract LowRSSIThresholdTimer */ - if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT16) - goto failed; - dbus_message_iter_get_basic(&iter, &l_rssi_timer); + /* Extract RSSISamplingPeriod */ + if (g_dbus_proxy_get_property(proxy, "RSSISamplingPeriod", &iter)) { + 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 - * all are set to the unset values or all are set within valid ranges. + /* Verify the values of RSSIs and their timeouts. All fields should be + * either set to the unset values or are set within valid ranges. + * If the fields are only partially set, we would try our best to fill + * in with some sane values. */ 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) { + h_rssi_timeout == ADV_MONITOR_UNSET_TIMEOUT && + l_rssi_timeout == ADV_MONITOR_UNSET_TIMEOUT && + sampling_period == ADV_MONITOR_UNSET_SAMPLING_PERIOD) { goto done; } + if (l_rssi == ADV_MONITOR_UNSET_RSSI) + l_rssi = ADV_MONITOR_MIN_RSSI; + + if (h_rssi == ADV_MONITOR_UNSET_RSSI) + h_rssi = l_rssi; + + if (l_rssi_timeout == ADV_MONITOR_UNSET_TIMEOUT) + l_rssi_timeout = ADV_MONITOR_DEFAULT_LOW_TIMEOUT; + + if (h_rssi_timeout == ADV_MONITOR_UNSET_TIMEOUT) + h_rssi_timeout = ADV_MONITOR_DEFAULT_HIGH_TIMEOUT; + + if (sampling_period == ADV_MONITOR_UNSET_SAMPLING_PERIOD) + sampling_period = ADV_MONITOR_DEFAULT_SAMPLING_PERIOD; + if (h_rssi < ADV_MONITOR_MIN_RSSI || h_rssi > ADV_MONITOR_MAX_RSSI || l_rssi < ADV_MONITOR_MIN_RSSI || - l_rssi > ADV_MONITOR_MAX_RSSI || h_rssi <= l_rssi) { + l_rssi > ADV_MONITOR_MAX_RSSI || h_rssi < l_rssi) { goto failed; } - if (h_rssi_timer < ADV_MONITOR_MIN_TIMER || - h_rssi_timer > ADV_MONITOR_MAX_TIMER || - l_rssi_timer < ADV_MONITOR_MIN_TIMER || - l_rssi_timer > ADV_MONITOR_MAX_TIMER) { + if (h_rssi_timeout < ADV_MONITOR_MIN_TIMEOUT || + h_rssi_timeout > ADV_MONITOR_MAX_TIMEOUT || + l_rssi_timeout < ADV_MONITOR_MIN_TIMEOUT || + l_rssi_timeout > ADV_MONITOR_MAX_TIMEOUT) { 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->high_rssi_timeout = h_rssi_timeout; + monitor->low_rssi_timeout = l_rssi_timeout; + 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; failed: - 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; - btd_error(adapter_id, - "Invalid argument of property RSSIThresholdsAndTimers " + "Invalid argument of RSSI thresholds and timeouts " "of the Adv Monitor at path %s", path); @@ -673,16 +699,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_TIMEOUT && + monitor->low_rssi_timeout == ADV_MONITOR_UNSET_TIMEOUT && + monitor->sampling_period == ADV_MONITOR_UNSET_SAMPLING_PERIOD; +} - 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); - memcpy(cp->patterns + cp->pattern_count, pattern, sizeof(*pattern)); - cp->pattern_count++; + cp = malloc0(cp_len); + if (!cp) + return false; + + 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 +788,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 +821,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 +1512,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 Fri Jan 15 11:50:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Archie Pusaka X-Patchwork-Id: 12022345 X-Patchwork-Delegate: luiz.dentz@gmail.com 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 1CE61C433E0 for ; Fri, 15 Jan 2021 11:52:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DB85823730 for ; Fri, 15 Jan 2021 11:52:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726370AbhAOLwm (ORCPT ); Fri, 15 Jan 2021 06:52:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725910AbhAOLwl (ORCPT ); Fri, 15 Jan 2021 06:52:41 -0500 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09A6CC0613C1 for ; Fri, 15 Jan 2021 03:52:01 -0800 (PST) Received: by mail-qk1-x749.google.com with SMTP id l138so7685070qke.4 for ; Fri, 15 Jan 2021 03:52:01 -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=3LHP5tx+2jOMtQWFgOs6liyFkeh3GRIFgvOay/7wGag=; b=FOsXQl6BGzb/QPYSa/24S5KlQDoZxiuLzLbJivIWwRdAYnZavCkZ/+pFKmTAkegeal 1S0nqAadkqOfzvI4WNutQMvAbpAgcn6tg8iDED1kPloCOpDj0XRBoVbKjK58X5GHiusQ YgS2x6IpEaoDrtmPL4BF/ljuzyWX4STYL7XPkiNqlfqVUVUn/s+wKhRJ3MlhczwJJyeL j+zjF2jdZ1uwzrDqT4RhllPPWdLvNC0FvAfr8UAT4iQLy5jw3MPqfJ6fPq6/ak8jEp8U Spv/7q8d2dw0B5XD1yxaWo/WQRpUzGfXVV4fKsJR+Tcnuh/EmKs/nv2/BraYaFe45kG/ tYHw== 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=3LHP5tx+2jOMtQWFgOs6liyFkeh3GRIFgvOay/7wGag=; b=MnTOxVqnUZgZIXcMBA++KflEbn20cL79JJ2GvHm5jo8V+o8Qdsedweyyvt9QFxEf19 eQNmZsBbi0kYPt4seYnNcUJ14jomFQIBPzLrcygbSUyIu063COTrFiw29DvbMbGT/sB1 iWm8eVoDIdjICYmcmuettlm1MjGPiQwqjlNWbJihjJVbscdt8Qw0wkqUgmO/zOQ9kgxk OQou4inLg54QE8Cb8Dy8N5jQWPC0JhFc1IZKvZv+79MsGZ8P6ffoOA3ZVjIS41PkrqgB kXttNDOMnwTAU+HJr8GbFCaWQor8NjI136GASYwaxRo9QuD9cQyKfGe6ZJNzcJ2b95Fj OYwg== X-Gm-Message-State: AOAM531qgDsfdJ74qnOnnag44URSkRXYQEl5NeMw8X3Ify6J4ZkbKTB+ OrJu1bpzza3S7qP8rqFkMC7/tRYT52y7vvFG41KptoTkImtlPyhA6mZQPA0YVy28RNHjwfnCj+Q wGcOPwOpjnlGsgeSPl+ZYm1tez0gg8xBaDQSPVTGwnqF/XKXZLWLy0OGycD2C3kquI9/dtfxyWk O3 X-Google-Smtp-Source: ABdhPJw90YKcgd9uXCshJkpODUNpNbV+iCq/3QZQU8Lkt/+KRAmkcOki4CP1ibg3eDK/rPUcTgPI3uwYRgLu 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:a05:6214:14ee:: with SMTP id k14mr11458657qvw.36.1610711520148; Fri, 15 Jan 2021 03:52:00 -0800 (PST) Date: Fri, 15 Jan 2021 19:50:41 +0800 In-Reply-To: <20210115115036.3973761-1-apusaka@google.com> Message-Id: <20210115194853.Bluez.v4.4.I16fa24fb791fe886f6723373772b644783b3ab92@changeid> Mime-Version: 1.0 References: <20210115115036.3973761-1-apusaka@google.com> X-Mailer: git-send-email 2.30.0.296.g2bfb1c46d8-goog Subject: [Bluez PATCH v4 4/6] btmgmt: advmon add rssi support 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) tools/btmgmt.c | 160 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 134 insertions(+), 26 deletions(-) diff --git a/tools/btmgmt.c b/tools/btmgmt.c index c0e55f58e6..383e7199e4 100644 --- a/tools/btmgmt.c +++ b/tools/btmgmt.c @@ -4858,64 +4858,169 @@ static bool str2pattern(struct mgmt_adv_pattern *pattern, const char *str) return true; } -static void advmon_add_usage(void) +static struct option add_monitor_rssi_options[] = { + { "help", 0, 0, 'h' }, + { "high-threshold", 1, 0, 'R' }, + { "low-threshold", 1, 0, 'r' }, + { "high-timeout", 1, 0, 'T' }, + { "low-timeout", 1, 0, 't' }, + { "sampling", 1, 0, 's' }, + { 0, 0, 0, 0 } +}; + +static void advmon_add_pattern_usage(void) +{ + bt_shell_usage(); + print("patterns format:\n" + "\t [patterns]\n" + "e.g.:\n" + "\tadd-pattern 0:1:c504 ff:a:9a55beef"); +} + +static void advmon_add_pattern_rssi_usage(void) { bt_shell_usage(); - print("Monitor Types:\n\t-p ..." - "\tPattern Monitor\ne.g.:\n\tadd -p 0:1:c504 1:a:9a55beef"); + print("RSSI options:\n" + "\t -R, --high-threshold " + "RSSI high threshold. Default: -70\n" + "\t -r, --low-threshold " + "RSSI low threshold. Default: -50\n" + "\t -T, --high-timeout " + "RSSI high threshold duration. Default: 0\n" + "\t -t, --low-timeout " + "RSSI low threshold duration. Default: 5\n" + "\t -s, --sampling " + "RSSI sampling period. Default: 0\n" + "patterns format:\n" + "\t [patterns]\n" + "e.g.:\n" + "\tadd-pattern-rssi -R 0xb2 -r -102 0:1:c504 ff:a:9a55beef"); } -static bool advmon_add_pattern(int argc, char **argv) +static void cmd_advmon_add_pattern(int argc, char **argv) { + bool success = true; uint16_t index; int i, cp_len; struct mgmt_cp_add_adv_monitor *cp = NULL; - bool success = false; - index = mgmt_index; - if (index == MGMT_INDEX_NONE) - index = 0; + if (!strcmp(argv[1], "-h")) + goto done; - cp_len = sizeof(struct mgmt_cp_add_adv_monitor) + - argc * sizeof(struct mgmt_adv_pattern); + argc -= 1; + argv += 1; + cp_len = sizeof(*cp) + argc * sizeof(struct mgmt_adv_pattern); cp = malloc0(cp_len); cp->pattern_count = argc; for (i = 0; i < argc; i++) { if (!str2pattern(&cp->patterns[i], argv[i])) { error("Failed to parse monitor patterns."); + success = false; goto done; } } - if (!mgmt_send(mgmt, MGMT_OP_ADD_ADV_PATTERNS_MONITOR, index, cp_len, - cp, advmon_add_rsp, NULL, NULL)) { - error("Unable to send \"Add Advertising Monitor\" command"); + index = mgmt_index; + if (index == MGMT_INDEX_NONE) + index = 0; + + if (!mgmt_send(mgmt, MGMT_OP_ADD_ADV_PATTERNS_MONITOR, index, + cp_len, cp, advmon_add_rsp, NULL, NULL)) { + error("Unable to send Add Advertising Monitor command"); + success = false; goto done; } - success = true; + free(cp); + return; done: free(cp); - return success; + advmon_add_pattern_usage(); + bt_shell_noninteractive_quit(success ? EXIT_SUCCESS : EXIT_FAILURE); } -static void cmd_advmon_add(int argc, char **argv) +static void cmd_advmon_add_pattern_rssi(int argc, char **argv) { - bool success = false; + bool success = true; + int opt; + int8_t rssi_low = -70; + int8_t rssi_high = -50; + uint16_t rssi_low_timeout = 5; + uint16_t rssi_high_timeout = 0; + uint8_t rssi_sampling_period = 0; + uint16_t index; + int i, cp_len; + struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = NULL; - if (strcasecmp(argv[1], "-p") == 0 && argc > 2) { - argc -= 2; - argv += 2; - success = advmon_add_pattern(argc, argv); + while ((opt = getopt_long(argc, argv, "+hr:R:t:T:s:", + add_monitor_rssi_options, NULL)) != -1) { + switch (opt) { + case 'h': + goto done; + case 'r': + rssi_low = strtol(optarg, NULL, 0); + break; + case 'R': + rssi_high = strtol(optarg, NULL, 0); + break; + case 't': + rssi_low_timeout = strtol(optarg, NULL, 0); + break; + case 'T': + rssi_high_timeout = strtol(optarg, NULL, 0); + break; + case 's': + rssi_sampling_period = strtol(optarg, NULL, 0); + break; + default: + success = false; + goto done; + } } - if (!success) { - advmon_add_usage(); - bt_shell_noninteractive_quit(EXIT_FAILURE); + argc -= optind; + argv += optind; + optind = 0; + + cp_len = sizeof(*cp) + argc * sizeof(struct mgmt_adv_pattern); + cp = malloc0(cp_len); + cp->pattern_count = argc; + cp->rssi.high_threshold = rssi_high; + cp->rssi.low_threshold = rssi_low; + cp->rssi.high_threshold_timeout = htobs(rssi_high_timeout); + cp->rssi.low_threshold_timeout = htobs(rssi_low_timeout); + cp->rssi.sampling_period = rssi_sampling_period; + + for (i = 0; i < argc; i++) { + if (!str2pattern(&cp->patterns[i], argv[i])) { + error("Failed to parse monitor patterns."); + success = false; + goto done; + } + } + + index = mgmt_index; + if (index == MGMT_INDEX_NONE) + index = 0; + + if (!mgmt_send(mgmt, MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, index, + cp_len, cp, advmon_add_rsp, NULL, NULL)) { + error("Unable to send Add Advertising Monitor RSSI command"); + success = false; + goto done; } + + free(cp); + return; + +done: + free(cp); + optind = 0; + advmon_add_pattern_rssi_usage(); + bt_shell_noninteractive_quit(success ? EXIT_SUCCESS : EXIT_FAILURE); } static void advmon_remove_rsp(uint8_t status, uint16_t len, const void *param, @@ -5037,8 +5142,11 @@ static const struct bt_shell_menu monitor_menu = { "features" }, { "remove", "", cmd_advmon_remove, "Remove advertisement monitor " }, - { "add", "<-p|-h> [options...]", - cmd_advmon_add, "Add advertisement monitor" }, + { "add-pattern", "[-h] ", + cmd_advmon_add_pattern, "Add advertisement monitor pattern" }, + { "add-pattern-rssi", "[options] ", + cmd_advmon_add_pattern_rssi, + "Add advertisement monitor pattern with RSSI options" }, { } }, }; From patchwork Fri Jan 15 11:50:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Archie Pusaka X-Patchwork-Id: 12022347 X-Patchwork-Delegate: luiz.dentz@gmail.com 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=-21.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 194CBC433DB for ; Fri, 15 Jan 2021 11:53:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BC94323730 for ; Fri, 15 Jan 2021 11:53:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726801AbhAOLxC (ORCPT ); Fri, 15 Jan 2021 06:53:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726091AbhAOLxB (ORCPT ); Fri, 15 Jan 2021 06:53:01 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 167E3C061757 for ; Fri, 15 Jan 2021 03:52:21 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id x12so5352089plr.21 for ; Fri, 15 Jan 2021 03:52:21 -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=uDx22S7e9joL0PJbtxtYhItXcvmuDuR1QrV7+m5uHUE=; b=CYWt8vdZDwTsR/A4vu2qNjXv/H530FE6+J8I+ORPSsqZMkdfSwrryQWXsUrEHzQ0NH bJ35BYEwjiqsABgG5cUA7FVkXcSWFvnTGYpw8YnqcBMLcvbMyXYOMfExkG5yNrqHfqeK 3cL6Bx1Wao1Bhjx3J17wlrSURoa4oQlnaZncDnnSaH+HuCorvMQqfKvtveEAzmywYWmD eobwhe9QdTvaxLkjiEjLtyE1PdWDvBkrBBgtFT5raoulWbyr9rQ1HtiTGIjLZBLSys9a Rb0aekKOCcyqdSZpXuynpSb8Thl53OGEm5nuFRmvXET/QSNa1eTb3uKl02EWJy/lm9uo PpXw== 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=uDx22S7e9joL0PJbtxtYhItXcvmuDuR1QrV7+m5uHUE=; b=Y934n56nZgPRDQJ/S44SCg7f9KVUj/I4Ogs3Ct8x62vbwGLkAWvbaSAzYB3zDmnjEn ZfIbkRO8LnDIiI6dEvf59184GZl9fOpVAVFTC20q14DEXAwuH/U47hqXIkKW1isZplZV S2hgcfbpGl016v0Mw4XlyHbOIRMWZu93BndfvnyQMkUocNQpbIyPSuHy5pB0ObQDF8MC njJbTGCOccPfbuGV7MTDz70xMskuV9LaQ5ibDbpR5+RW2lYtJhemAfSlticG//InYCdH DayVrvabnFctcipuNvi6glNMl6SbsGcmS69Bb79KRExdp3KIS2+0I5k3rhJy+wCj1b3j /CWA== X-Gm-Message-State: AOAM531N6Q1Kf5NiAlnG/jHjcrfzh2jR6Q4D5wMj6lTODkqh88xUIx0C 7QoQNTfzTVIG+6KAXQHpIjYJYm86FaGTzikTc03CoeNQfYBmNwP1jrvWRpU3OxZ6R8QUurKmLMW t+AUt/AvdaW/+B5znvj3URcypu5NAJscRygEBnfFERC1Ayq/6pPoanIYOZ30okFvjgJwFAF2kgF D5 X-Google-Smtp-Source: ABdhPJzGJJhgnOMyFkdPnOxEUi9z8ZCIaZVimlM6YUiQT3a0nHvonYiMdZIhCC1RDiWMgAyXb6XI51b4IjEl 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:aa8d:: with SMTP id l13mr1113908pjq.0.1610711540110; Fri, 15 Jan 2021 03:52:20 -0800 (PST) Date: Fri, 15 Jan 2021 19:50:43 +0800 In-Reply-To: <20210115115036.3973761-1-apusaka@google.com> Message-Id: <20210115194853.Bluez.v4.5.I20391efb1b5a40cd2b0cb6069d88b7fb9f7ed66b@changeid> Mime-Version: 1.0 References: <20210115115036.3973761-1-apusaka@google.com> X-Mailer: git-send-email 2.30.0.296.g2bfb1c46d8-goog Subject: [Bluez PATCH v4 5/6] bluetoothctl: advmon rssi support for mgmt From: Archie Pusaka To: linux-bluetooth , Luiz Augusto von Dentz Cc: CrosBT Upstreaming , Archie Pusaka 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. --- Changes in v4: * split the add-or-pattern-rssi command Changes in v3: * split the struct RSSIThresholdsAndTimers client/adv_monitor.c | 258 ++++++++++++++++++++++++++++--------------- client/adv_monitor.h | 11 +- client/main.c | 72 ++++++------ 3 files changed, 210 insertions(+), 131 deletions(-) diff --git a/client/adv_monitor.c b/client/adv_monitor.c index f62e9f4442..792379fc40 100644 --- a/client/adv_monitor.c +++ b/client/adv_monitor.c @@ -28,11 +28,16 @@ #define ADV_MONITOR_APP_PATH "/org/bluez/adv_monitor_app" #define ADV_MONITOR_INTERFACE "org.bluez.AdvertisementMonitor1" +#define RSSI_UNSET_THRESHOLD 127 +#define RSSI_UNSET_TIMEOUT 0 +#define RSSI_UNSET_SAMPLING_PERIOD 256 + struct rssi_setting { int16_t high_threshold; - uint16_t high_timer; + uint16_t high_timeout; int16_t low_threshold; - uint16_t low_timer; + uint16_t low_timeout; + uint16_t sampling_period; }; struct pattern { @@ -59,6 +64,7 @@ static struct adv_monitor_manager { static uint8_t adv_mon_idx; static GSList *adv_mons; +static struct rssi_setting *current_rssi; static void remove_adv_monitor(void *data, void *user_data); @@ -131,32 +137,105 @@ static gboolean get_type(const GDBusPropertyTable *property, return TRUE; } -static gboolean get_rssi(const GDBusPropertyTable *property, +static gboolean get_low_threshold(const GDBusPropertyTable *property, DBusMessageIter *iter, void *user_data) { struct adv_monitor *adv_monitor = user_data; struct rssi_setting *rssi = adv_monitor->rssi; - DBusMessageIter data_iter; - dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, - NULL, &data_iter); - dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_INT16, - &rssi->high_threshold); - dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_UINT16, - &rssi->high_timer); - dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_INT16, + dbus_message_iter_append_basic(iter, DBUS_TYPE_INT16, &rssi->low_threshold); - dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_UINT16, - &rssi->low_timer); - dbus_message_iter_close_container(iter, &data_iter); return TRUE; } -static gboolean rssi_exists(const GDBusPropertyTable *property, void *data) +static gboolean get_high_threshold(const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + struct rssi_setting *rssi = adv_monitor->rssi; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_INT16, + &rssi->high_threshold); + return TRUE; +} + +static gboolean get_low_timeout(const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + struct rssi_setting *rssi = adv_monitor->rssi; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16, + &rssi->low_timeout); + return TRUE; +} + +static gboolean get_high_timeout(const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + struct rssi_setting *rssi = adv_monitor->rssi; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16, + &rssi->high_timeout); + return TRUE; +} + +static gboolean get_sampling_period(const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + struct rssi_setting *rssi = adv_monitor->rssi; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16, + &rssi->sampling_period); + return TRUE; +} + +static gboolean low_threshold_exists(const GDBusPropertyTable *property, + void *data) +{ + struct adv_monitor *adv_monitor = data; + + return adv_monitor->rssi != NULL && + adv_monitor->rssi->low_threshold != RSSI_UNSET_THRESHOLD; +} + +static gboolean high_threshold_exists(const GDBusPropertyTable *property, + void *data) +{ + struct adv_monitor *adv_monitor = data; + + return adv_monitor->rssi != NULL && + adv_monitor->rssi->high_threshold != RSSI_UNSET_THRESHOLD; +} + +static gboolean low_timeout_exists(const GDBusPropertyTable *property, + void *data) +{ + struct adv_monitor *adv_monitor = data; + + return adv_monitor->rssi != NULL && + adv_monitor->rssi->low_timeout != RSSI_UNSET_TIMEOUT; +} + +static gboolean high_timeout_exists(const GDBusPropertyTable *property, + void *data) +{ + struct adv_monitor *adv_monitor = data; + + return adv_monitor->rssi != NULL && + adv_monitor->rssi->high_timeout != RSSI_UNSET_TIMEOUT; +} + +static gboolean sampling_period_exists(const GDBusPropertyTable *property, + void *data) { struct adv_monitor *adv_monitor = data; - return adv_monitor->rssi != NULL; + return adv_monitor->rssi != NULL && + adv_monitor->rssi->sampling_period != + RSSI_UNSET_SAMPLING_PERIOD; } static void append_pattern_content_to_dbus(DBusMessageIter *iter, @@ -212,7 +291,14 @@ 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 }, + { "RSSILowThreshold", "n", get_low_threshold, NULL, + low_threshold_exists }, + { "RSSIHighThreshold", "n", get_high_threshold, NULL, + high_threshold_exists }, + { "RSSILowTimeout", "q", get_low_timeout, NULL, low_timeout_exists }, + { "RSSIHighTimeout", "q", get_high_timeout, NULL, high_timeout_exists }, + { "RSSISamplingPeriod", "q", get_sampling_period, NULL, + sampling_period_exists }, { "Patterns", "a(yyay)", get_patterns, NULL, pattern_exists }, { } }; @@ -266,6 +352,9 @@ void adv_monitor_remove_manager(DBusConnection *conn) manager.proxy = NULL; manager.app_registered = FALSE; + + g_free(current_rssi); + current_rssi = NULL; } static void register_setup(DBusMessageIter *iter, void *user_data) @@ -376,58 +465,6 @@ 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) -{ - 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; - - parse_rssi_value_pair(range, &low_threshold, &high_threshold); - parse_rssi_value_pair(timeout, &low_timer, &high_timer); - - rssi = g_malloc0(sizeof(struct rssi_setting)); - - if (!rssi) { - bt_shell_printf("Failed to allocate rssi_setting"); - 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; - - return rssi; -} - static struct pattern *parse_pattern(char *parameter_list[]) { struct pattern *pat; @@ -435,7 +472,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; } @@ -531,12 +568,14 @@ static void print_adv_monitor(struct adv_monitor *adv_monitor) bt_shell_printf("\trssi:\n"); bt_shell_printf("\t\thigh threshold: %hd\n", adv_monitor->rssi->high_threshold); - bt_shell_printf("\t\thigh threshold timer: %hu\n", - adv_monitor->rssi->high_timer); + bt_shell_printf("\t\thigh threshold timeout: %hu\n", + adv_monitor->rssi->high_timeout); bt_shell_printf("\t\tlow threshold: %hd\n", adv_monitor->rssi->low_threshold); - bt_shell_printf("\t\tlow threshold timer: %hu\n", - adv_monitor->rssi->low_timer); + bt_shell_printf("\t\tlow threshold timeout: %hu\n", + adv_monitor->rssi->low_timeout); + bt_shell_printf("\t\tsampling period: %hu\n", + adv_monitor->rssi->sampling_period); } if (adv_monitor->patterns) { @@ -556,11 +595,62 @@ static void print_adv_monitor(struct adv_monitor *adv_monitor) } } +static struct rssi_setting *get_current_rssi(void) +{ + if (current_rssi) + return current_rssi; + + current_rssi = g_malloc0(sizeof(struct rssi_setting)); + + if (!current_rssi) + bt_shell_printf("Failed to allocate rssi setting"); + + current_rssi->low_threshold = RSSI_UNSET_THRESHOLD; + current_rssi->high_threshold = RSSI_UNSET_THRESHOLD; + current_rssi->low_timeout = RSSI_UNSET_TIMEOUT; + current_rssi->high_timeout = RSSI_UNSET_TIMEOUT; + current_rssi->sampling_period = RSSI_UNSET_SAMPLING_PERIOD; + + return current_rssi; +} + +void adv_monitor_set_rssi_threshold(int16_t low_threshold, + int16_t high_threshold) +{ + struct rssi_setting *rssi = get_current_rssi(); + + if (!rssi) + return; + + rssi->low_threshold = low_threshold; + rssi->high_threshold = high_threshold; +} + +void adv_monitor_set_rssi_timeout(uint16_t low_timeout, uint16_t high_timeout) +{ + struct rssi_setting *rssi = get_current_rssi(); + + if (!rssi) + return; + + rssi->low_timeout = low_timeout; + rssi->high_timeout = high_timeout; +} + +void adv_monitor_set_rssi_sampling_period(uint16_t sampling) +{ + struct rssi_setting *rssi = get_current_rssi(); + + if (!rssi) + return; + + rssi->sampling_period = sampling; +} + void adv_monitor_add_monitor(DBusConnection *conn, char *type, - gboolean rssi_enabled, int argc, char *argv[]) + int argc, char *argv[]) { struct adv_monitor *adv_monitor; - struct rssi_setting *rssi; GSList *patterns = NULL; if (g_slist_length(adv_mons) >= UINT8_MAX) { @@ -572,17 +662,6 @@ 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) - rssi = NULL; - else { - rssi = parse_rssi(argv[1], argv[2]); - if (rssi == NULL) - return; - - argv += 2; - argc -= 2; - } - patterns = parse_patterns(argv+1, argc-1); if (patterns == NULL) { bt_shell_printf("pattern-list malformed\n"); @@ -598,16 +677,19 @@ void adv_monitor_add_monitor(DBusConnection *conn, char *type, adv_monitor->idx = adv_mon_idx; adv_monitor->type = g_strdup(type); - adv_monitor->rssi = rssi; + adv_monitor->rssi = current_rssi; adv_monitor->patterns = patterns; adv_monitor->path = g_strdup_printf("%s/%hhu", ADV_MONITOR_APP_PATH, adv_mon_idx); + current_rssi = NULL; + if (g_dbus_register_interface(conn, adv_monitor->path, ADV_MONITOR_INTERFACE, adv_monitor_methods, NULL, adv_monitor_props, adv_monitor, free_adv_monitor) == FALSE) { bt_shell_printf("Failed to register advertisement monitor\n"); + free_adv_monitor(adv_monitor); return bt_shell_noninteractive_quit(EXIT_FAILURE); } diff --git a/client/adv_monitor.h b/client/adv_monitor.h index dd6f615799..2c25af3339 100644 --- a/client/adv_monitor.h +++ b/client/adv_monitor.h @@ -8,17 +8,16 @@ * */ -#define RSSI_DEFAULT_HIGH_THRESHOLD -50 -#define RSSI_DEFAULT_LOW_THRESHOLD -70 -#define RSSI_DEFAULT_HIGH_TIMEOUT 10 -#define RSSI_DEFAULT_LOW_TIMEOUT 5 - void adv_monitor_add_manager(DBusConnection *conn, GDBusProxy *proxy); void adv_monitor_remove_manager(DBusConnection *conn); void adv_monitor_register_app(DBusConnection *conn); void adv_monitor_unregister_app(DBusConnection *conn); +void adv_monitor_set_rssi_threshold(int16_t low_threshold, + int16_t high_threshold); +void adv_monitor_set_rssi_timeout(uint16_t low_timeout, uint16_t high_timeout); +void adv_monitor_set_rssi_sampling_period(uint16_t sampling); void adv_monitor_add_monitor(DBusConnection *conn, char *type, - gboolean rssi_enabled, int argc, char *argv[]); + int argc, char *argv[]); void adv_monitor_print_monitor(DBusConnection *conn, int monitor_idx); void adv_monitor_remove_monitor(DBusConnection *conn, int monitor_idx); void adv_monitor_get_supported_info(void); diff --git a/client/main.c b/client/main.c index 9403f1af6e..79658a463f 100644 --- a/client/main.c +++ b/client/main.c @@ -2707,30 +2707,6 @@ static void cmd_ad_clear(int argc, char *argv[]) return bt_shell_noninteractive_quit(EXIT_FAILURE); } -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, - RSSI_DEFAULT_HIGH_TIMEOUT); - bt_shell_printf("pattern format:\n" - "\t \n"); - bt_shell_printf("e.g.\n" - "\tadd-or-pattern-rssi -10, ,10 1 2 01ab55\n"); - bt_shell_printf("or\n" - "\tadd-or-pattern-rssi -50,-30 , 1 2 01ab55 3 4 23cd66\n"); -} - static void print_add_or_pattern_usage(void) { bt_shell_printf("pattern format:\n" @@ -2743,20 +2719,38 @@ static void cmd_adv_monitor_print_usage(int argc, char *argv[]) { if (strcmp(argv[1], "add-or-pattern") == 0) print_add_or_pattern_usage(); - else if (strcmp(argv[1], "add-or-pattern-rssi") == 0) - print_add_or_pattern_with_rssi_usage(); else bt_shell_printf("Invalid argument %s", argv[1]); } -static void cmd_adv_monitor_add_or_monitor_with_rssi(int argc, char *argv[]) +static void cmd_adv_monitor_set_rssi_threshold(int argc, char *argv[]) +{ + int low_threshold, high_threshold; + + low_threshold = atoi(argv[1]); + high_threshold = atoi(argv[2]); + adv_monitor_set_rssi_threshold(low_threshold, high_threshold); +} + +static void cmd_adv_monitor_set_rssi_timeout(int argc, char *argv[]) { - adv_monitor_add_monitor(dbus_conn, "or_patterns", TRUE, argc, argv); + int low_timeout, high_timeout; + + low_timeout = atoi(argv[1]); + high_timeout = atoi(argv[2]); + adv_monitor_set_rssi_timeout(low_timeout, high_timeout); +} + +static void cmd_adv_monitor_set_rssi_sampling_period(int argc, char *argv[]) +{ + int sampling = atoi(argv[1]); + + adv_monitor_set_rssi_sampling_period(sampling); } static void cmd_adv_monitor_add_or_monitor(int argc, char *argv[]) { - adv_monitor_add_monitor(dbus_conn, "or_patterns", FALSE, argc, argv); + adv_monitor_add_monitor(dbus_conn, "or_patterns", argc, argv); } static void cmd_adv_monitor_print_monitor(int argc, char *argv[]) @@ -2826,15 +2820,19 @@ static const struct bt_shell_menu advertise_monitor_menu = { .name = "monitor", .desc = "Advertisement Monitor Options Submenu", .entries = { - { "add-or-pattern-rssi", " " - "[patterns=pattern1 pattern2 ...]", - cmd_adv_monitor_add_or_monitor_with_rssi, - "Add 'or pattern' type monitor with RSSI " - "filter" }, + { "set-rssi-threshold", " ", + cmd_adv_monitor_set_rssi_threshold, + "Set RSSI threshold parameter" }, + { "set-rssi-timeout", " ", + cmd_adv_monitor_set_rssi_timeout, + "Set RSSI timeout parameter" }, + { "set-rssi-sampling-period", "", + cmd_adv_monitor_set_rssi_sampling_period, + "Set RSSI sampling period parameter" }, { "add-or-pattern", "[patterns=pattern1 pattern2 ...]", cmd_adv_monitor_add_or_monitor, - "Add 'or pattern' type monitor without RSSI " - "filter" }, + "Register 'or pattern' type monitor with the " + "specified RSSI parameters" }, { "get-pattern", "", cmd_adv_monitor_print_monitor, "Get advertisement monitor" }, @@ -2845,7 +2843,7 @@ static const struct bt_shell_menu advertise_monitor_menu = { cmd_adv_monitor_get_supported_info, "Get advertisement manager supported " "features and supported monitor types" }, - { "print-usage", "", + { "print-usage", "", cmd_adv_monitor_print_usage, "Print the command usage"}, { } }, From patchwork Fri Jan 15 11:50:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Archie Pusaka X-Patchwork-Id: 12022349 X-Patchwork-Delegate: luiz.dentz@gmail.com 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 4C660C433E0 for ; Fri, 15 Jan 2021 11:53:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 22BB723730 for ; Fri, 15 Jan 2021 11:53:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727357AbhAOLxN (ORCPT ); Fri, 15 Jan 2021 06:53:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726808AbhAOLxM (ORCPT ); Fri, 15 Jan 2021 06:53:12 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83B5CC0613D3 for ; Fri, 15 Jan 2021 03:52:32 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id l10so3611724ybt.6 for ; Fri, 15 Jan 2021 03:52:32 -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=gUtBImmLVlFO2Y56vbj2nXodQnxN0F5pBsc2kcdtUFg=; b=oP6ZpSItcvNWmw/OC4S/woxu/bH8RGkLxFPCGqb8Xf7AVzAZ2uFJXtW+t2TBSPJ5Y6 7+v4+a5oM60uUSJzW+kGKq9jvKSVCQVRZSutkRysrR3P62Ucn/jwOobuzSUnWDRAoHdI 86Hdh1ZsCVD5yu3Hu0QMK/sLWWhfHpw8k+n67lZ7H+XkzoM5C+4879kDggfUBAdhvdwu vH1v9qhZcLQJxalry+aqav/qFJZkXSZZGqF+mGiGJNMvMq5RZGl/Hr+n6xKPbI9lPGei sddwAFzCA31TEN0zlhNRYYJNcNXw9aEyYy3WSCavpsIGSdcUSJpAVGVg3YLqUL0Fa5Hf jVAA== 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=gUtBImmLVlFO2Y56vbj2nXodQnxN0F5pBsc2kcdtUFg=; b=s1rKeaiXRGfdG3uX1nep6syDQhhfFK7akushHDixwgt1wM9oCM5ndz79PNKLgGevaz XOebDfj7cbFgn+mPsHoXPe2AFiDjhb2TO5HfIViHa1FZUSLwFRs2auD3XZcuZg2pnT0L i1Mo97+3LB9lgFbiqnQrh0jm4pfwYcq1grMS4RkEW4R84eN+TuP/oVC3W89q56OvogsZ Xq5d3pJ5kc6Wi13/lZv3YP1gxdQm9FWXczzT5EnavE+7OenCIrLtXXutxcYRVIM8kRmc zs5hlSLFQqmCBh2cFPRDFF+ozlMYJkbyaQmSD2s+3WMYXwHi/GpzHJUCytl0fmpbmDkO AiFw== X-Gm-Message-State: AOAM5317E4mPPBr1mztATZ9MK0HBl9cS8VPUjHBPX/KK+ZYfDlU+e4To Pe0/flS0QGMxvom6eRwmDaA2X8NSgCsGwfQMau8Efic1fEWQj2sR+XILOoahwWIRe7jJnfhkzBu sikzno0tuO0h1UTrQEutPNN6jA2pC4Maj6JCff3/nS3yACESsHkmLOOoYzgDq4kKmG/0XUMHD3+ 9e X-Google-Smtp-Source: ABdhPJzyMppn6dyWLhczub/Zs19wyyjJUhmIluS3synpKU/onKNAM/7Yb4PEK9ra9ELQ9SPWVDnflrRPdCBI 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:a25:457:: with SMTP id 84mr17222929ybe.224.1610711551734; Fri, 15 Jan 2021 03:52:31 -0800 (PST) Date: Fri, 15 Jan 2021 19:50:45 +0800 In-Reply-To: <20210115115036.3973761-1-apusaka@google.com> Message-Id: <20210115194853.Bluez.v4.6.I0777ec8d6a8874f4d0308d1568aac8c33b04803c@changeid> Mime-Version: 1.0 References: <20210115115036.3973761-1-apusaka@google.com> X-Mailer: git-send-email 2.30.0.296.g2bfb1c46d8-goog Subject: [Bluez PATCH v4 6/6] monitor: Decode add advmon with RSSI parameter 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 Add support for MGMT command of add advertisement monitor with RSSI parameter (0x0056). @ MGMT Command: Add Advertisement.. (0x0056) plen 76 {0x0003} RSSI data: high threshold: 1 dBm high timeout: 0 seconds low threshold: -2 dBm low timeout: 3 seconds sampling: just once (0xFF) Number of patterns: 2 Pattern 1: AD type: 0 Offset: 1 Length: 2 Value : c504 Pattern 2: AD type: 255 Offset: 10 Length: 4 Value : 9a55beef Reviewed-by: Yun-Hao Chung --- (no changes since v1) monitor/packet.c | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/monitor/packet.c b/monitor/packet.c index c91b91e2b2..fcd698d92a 100644 --- a/monitor/packet.c +++ b/monitor/packet.c @@ -13261,18 +13261,22 @@ static void mgmt_print_adv_monitor_patterns(const void *data, uint8_t len) /* Reference: struct mgmt_adv_pattern in lib/mgmt.h. */ while (data_idx + 34 <= len) { - uint8_t ad_type = get_u8(data + data_idx); - uint8_t offset = get_u8(data + data_idx + 1); - uint8_t length = get_u8(data + data_idx + 2); + uint8_t ad_type = get_u8(data); + uint8_t offset = get_u8(data + 1); + uint8_t length = get_u8(data + 2); print_field(" Pattern %d:", pattern_idx); print_field(" AD type: %d", ad_type); print_field(" Offset: %d", offset); print_field(" Length: %d", length); - print_hex_field(" Value ", data + data_idx + 3, 31); + if (length <= 31) + print_hex_field(" Value ", data + 3, length); + else + print_text(COLOR_ERROR, " invalid length"); pattern_idx += 1; data_idx += 34; + data += 34; } } @@ -13284,6 +13288,33 @@ static void mgmt_add_adv_monitor_patterns_cmd(const void *data, uint16_t size) mgmt_print_adv_monitor_patterns(data + 1, size - 1); } +static void mgmt_add_adv_monitor_patterns_rssi_cmd(const void *data, + uint16_t size) +{ + int8_t high_rssi = get_s8(data); + uint16_t high_rssi_timeout = get_le16(data + 1); + int8_t low_rssi = get_s8(data + 3); + uint16_t low_rssi_timeout = get_le16(data + 4); + uint8_t sampling_period = get_u8(data + 6); + uint8_t pattern_count = get_u8(data + 7); + + print_field("RSSI data:"); + print_field(" high threshold: %d dBm", high_rssi); + print_field(" high timeout: %d seconds", high_rssi_timeout); + print_field(" low threshold: %d dBm", low_rssi); + print_field(" low timeout: %d seconds", low_rssi_timeout); + + if (sampling_period == 0) + print_field(" sampling: propagate all (0x00)"); + else if (sampling_period == 0xff) + print_field(" sampling: just once (0xFF)"); + else + print_field(" sampling: every %d ms", 100 * sampling_period); + + print_field("Number of patterns: %d", pattern_count); + mgmt_print_adv_monitor_patterns(data + 8, size - 8); +} + static void mgmt_add_adv_monitor_patterns_rsp(const void *data, uint16_t size) { uint16_t handle = get_le16(data); @@ -13553,6 +13584,10 @@ static const struct mgmt_data mgmt_command_table[] = { { 0x0055, "Add Ext Adv Data", mgmt_add_ext_adv_data_cmd, 3, false, mgmt_add_ext_adv_data_rsp, 1, true }, + { 0x0056, "Add Advertisement Monitor With RSSI", + mgmt_add_adv_monitor_patterns_rssi_cmd, 8, + false, + mgmt_add_adv_monitor_patterns_rsp, 2, true}, { } };