From patchwork Tue Oct 6 00:51:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miao-chen Chou X-Patchwork-Id: 267938 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=-10.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, 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 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 A5817C4363A for ; Tue, 6 Oct 2020 00:51:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 621252076B for ; Tue, 6 Oct 2020 00:51:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="lV5lWGSi" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725872AbgJFAvV (ORCPT ); Mon, 5 Oct 2020 20:51:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725865AbgJFAvU (ORCPT ); Mon, 5 Oct 2020 20:51:20 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6C1DC0613CE for ; Mon, 5 Oct 2020 17:51:20 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id a200so3192254pfa.10 for ; Mon, 05 Oct 2020 17:51:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=eXb4GfCDJbKAuwFkODIqDnS3BMU8Z2lamS/ovD3tyfI=; b=lV5lWGSi8VFv4FeBnHPMtrHWOpQDSC5ZULYtZfdz7Gs3qsvowA5R/6NT2STJO773Td MYOLDx+xUG1HZOjDgPbSWjEkO3b1m0wqZNZrBpI1+3LcrtvuEk2eC5J2W1XDvM4LKbey 6lnfRT2wR1ziV3pcev6THAk6/7QETGPE4hnmk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=eXb4GfCDJbKAuwFkODIqDnS3BMU8Z2lamS/ovD3tyfI=; b=Y/7j79+oPWHcZ/0XB4RVTn/0Xl1z4TP5xDyI5O55nCpQifFoSNiDxAPLN8yqKY24CL AuWHr8RcKoe161qPxdHM9MEL+lMkaVwSXhAXVwkIN4WQ2y22vMAeBoWyVN/3f3mujYOC /WjXIStjCLnbJ53GZXNPI66SeFnhPEiDZ5XfqrQGaR33S/Zu+yjOqrCW4FgPmLYHPtQj 4tAYN0xUdfMpbXfhlAzkIV+iIcy+PqunXwV9Zg+xu5xyoYkf4cxlNiF0IKPpVY2254w8 ZOguDGGJQEpyIaY1quoD3z58s/I3XMdLJGzdhPw41ZwpSilv8Qbsxa+0dyeDE1Z+u51J lbmQ== X-Gm-Message-State: AOAM531CK8/n0LSZOlkAbS57os7Sa1Af7rYOdvX01hevaBPZ9gSmPxfK 0gnEVu2L/TqKsnDpLAmKuDiA+UCDlZYAPQ== X-Google-Smtp-Source: ABdhPJy2jsLForS/HtcAy9I+wbJh+qJqX9/krC8fj36DzZZj01LZKD/tZ8EO0YB/jkSTeG/KAID2SQ== X-Received: by 2002:a63:fd03:: with SMTP id d3mr1805087pgh.201.1601945479581; Mon, 05 Oct 2020 17:51:19 -0700 (PDT) Received: from mcchou0.mtv.corp.google.com ([2620:15c:202:201:de4a:3eff:fe75:1314]) by smtp.gmail.com with ESMTPSA id q5sm1197991pfb.184.2020.10.05.17.51.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Oct 2020 17:51:18 -0700 (PDT) From: Miao-chen Chou To: Bluetooth Kernel Mailing List Cc: Marcel Holtmann , Manish Mandlik , Howard Chung , Luiz Augusto von Dentz , Alain Michaud , chromeos-bluetooth-upstreaming@chromium.org, Manish Mandlik , Abhishek Pandit-Subedi , Miao-chen Chou Subject: [BlueZ PATCH v5 1/6] adv_monitor: Implement RSSI Filter logic for background scanning Date: Mon, 5 Oct 2020 17:51:02 -0700 Message-Id: <20201005175052.BlueZ.v5.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Manish Mandlik This patch implements the RSSI Filter logic for background scanning. Reviewed-by: Abhishek Pandit-Subedi Reviewed-by: Alain Michaud Reviewed-by: Miao-chen Chou Reviewed-by: Howard Chung --- Changes in v5: - Remove the use of unit test in commit message Changes in v3: - Fix commit message doc/advertisement-monitor-api.txt | 5 + src/adapter.c | 1 + src/adv_monitor.c | 286 +++++++++++++++++++++++++++++- src/adv_monitor.h | 4 + 4 files changed, 292 insertions(+), 4 deletions(-) diff --git a/doc/advertisement-monitor-api.txt b/doc/advertisement-monitor-api.txt index e09b6fd25..92c8ffc38 100644 --- a/doc/advertisement-monitor-api.txt +++ b/doc/advertisement-monitor-api.txt @@ -70,6 +70,11 @@ Properties string Type [read-only] 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. + array{(uint8, uint8, array{byte})} Patterns [read-only, optional] If Type is set to 0x01, this must exist and has at least diff --git a/src/adapter.c b/src/adapter.c index c0053000a..6d0114a6b 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -1214,6 +1214,7 @@ void btd_adapter_remove_device(struct btd_adapter *adapter, adapter->connect_list = g_slist_remove(adapter->connect_list, dev); adapter->devices = g_slist_remove(adapter->devices, dev); + btd_adv_monitor_device_remove(adapter->adv_monitor_manager, dev); adapter->discovery_found = g_slist_remove(adapter->discovery_found, dev); diff --git a/src/adv_monitor.c b/src/adv_monitor.c index e441a5566..31ed30a00 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -26,6 +26,7 @@ #include "adapter.h" #include "dbus-common.h" +#include "device.h" #include "log.h" #include "src/error.h" #include "src/shared/ad.h" @@ -35,6 +36,8 @@ #include "adv_monitor.h" +static void monitor_device_free(void *data); + #define ADV_MONITOR_INTERFACE "org.bluez.AdvertisementMonitor1" #define ADV_MONITOR_MGR_INTERFACE "org.bluez.AdvertisementMonitorManager1" @@ -95,15 +98,36 @@ struct adv_monitor { enum monitor_state state; /* MONITOR_STATE_* */ - int8_t high_rssi; /* high RSSI threshold */ - 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 */ + int8_t high_rssi; /* High RSSI threshold */ + 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 */ + struct queue *devices; /* List of adv_monitor_device objects */ enum monitor_type type; /* MONITOR_TYPE_* */ struct queue *patterns; }; +/* Some data like last_seen, timer/timeout values need to be maintained + * per device. struct adv_monitor_device maintains such data. + */ +struct adv_monitor_device { + struct adv_monitor *monitor; + struct btd_device *device; + + time_t high_rssi_first_seen; /* Start time when RSSI climbs above + * the high RSSI threshold + */ + time_t low_rssi_first_seen; /* Start time when RSSI drops below + * the low RSSI threshold + */ + time_t last_seen; /* Time when last Adv was received */ + bool device_found; /* State of the device - lost/found */ + guint device_lost_timer; /* Timer to track if the device goes + * offline/out-of-range + */ +}; + struct app_match_data { const char *owner; const char *path; @@ -150,6 +174,9 @@ static void monitor_free(void *data) g_dbus_proxy_unref(monitor->proxy); g_free(monitor->path); + queue_destroy(monitor->devices, monitor_device_free); + monitor->devices = NULL; + queue_destroy(monitor->patterns, pattern_free); free(monitor); @@ -248,6 +275,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->devices = queue_new(); monitor->type = MONITOR_TYPE_NONE; monitor->patterns = NULL; @@ -923,3 +951,253 @@ void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager) manager_destroy(manager); } + +/* Matches a device based on btd_device object */ +static bool monitor_device_match(const void *a, const void *b) +{ + const struct adv_monitor_device *dev = a; + const struct btd_device *device = b; + + if (!dev) + return false; + + if (dev->device != device) + return false; + + return true; +} + +/* Frees a monitor device object */ +static void monitor_device_free(void *data) +{ + struct adv_monitor_device *dev = data; + + if (!dev) + return; + + if (dev->device_lost_timer) { + g_source_remove(dev->device_lost_timer); + dev->device_lost_timer = 0; + } + + dev->monitor = NULL; + dev->device = NULL; + + g_free(dev); +} + +/* Removes a device from monitor->devices list */ +static void remove_device_from_monitor(void *data, void *user_data) +{ + struct adv_monitor *monitor = data; + struct btd_device *device = user_data; + struct adv_monitor_device *dev = NULL; + + if (!monitor) + return; + + dev = queue_remove_if(monitor->devices, monitor_device_match, device); + if (dev) { + DBG("Device removed from the Adv Monitor at path %s", + monitor->path); + monitor_device_free(dev); + } +} + +/* Removes a device from every monitor in an app */ +static void remove_device_from_app(void *data, void *user_data) +{ + struct adv_monitor_app *app = data; + struct btd_device *device = user_data; + + if (!app) + return; + + queue_foreach(app->monitors, remove_device_from_monitor, device); +} + +/* Removes a device from every monitor in all apps */ +void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager, + struct btd_device *device) +{ + if (!manager || !device) + return; + + queue_foreach(manager->apps, remove_device_from_app, device); +} + +/* Creates a device object to track the per-device information */ +static struct adv_monitor_device *monitor_device_create( + struct adv_monitor *monitor, + struct btd_device *device) +{ + struct adv_monitor_device *dev = NULL; + + dev = g_try_malloc0(sizeof(struct adv_monitor_device)); + if (!dev) + return NULL; + + dev->monitor = monitor; + dev->device = device; + + queue_push_tail(monitor->devices, dev); + + return dev; +} + +/* Includes found/lost device's object path into the dbus message */ +static void report_device_state_setup(DBusMessageIter *iter, void *user_data) +{ + const char *path = device_get_path(user_data); + + dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path); +} + +/* Handles a situation where the device goes offline/out-of-range */ +static gboolean handle_device_lost_timeout(gpointer user_data) +{ + struct adv_monitor_device *dev = user_data; + struct adv_monitor *monitor = dev->monitor; + time_t curr_time = time(NULL); + + DBG("Device Lost timeout triggered for device %p " + "for the Adv Monitor at path %s", dev->device, monitor->path); + + dev->device_lost_timer = 0; + + if (dev->device_found && dev->last_seen) { + /* We were tracking for the Low RSSI filter. Check if there is + * any Adv received after the timeout function is invoked. + * If not, report the Device Lost event. + */ + if (difftime(curr_time, dev->last_seen) >= + monitor->low_rssi_timeout) { + dev->device_found = false; + + DBG("Calling DeviceLost() on Adv Monitor of owner %s " + "at path %s", monitor->app->owner, monitor->path); + + g_dbus_proxy_method_call(monitor->proxy, "DeviceLost", + report_device_state_setup, + NULL, dev->device, NULL); + } + } + + return FALSE; +} + +/* Filters an Adv based on its RSSI value */ +static void adv_monitor_filter_rssi(struct adv_monitor *monitor, + struct btd_device *device, int8_t rssi) +{ + struct adv_monitor_device *dev = NULL; + time_t curr_time = time(NULL); + uint16_t adapter_id = monitor->app->manager->adapter_id; + + /* If the RSSI thresholds and timeouts are not specified, report the + * 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) { + DBG("Calling DeviceFound() on Adv Monitor of owner %s " + "at path %s", monitor->app->owner, monitor->path); + + g_dbus_proxy_method_call(monitor->proxy, "DeviceFound", + report_device_state_setup, NULL, + device, NULL); + + return; + } + + dev = queue_find(monitor->devices, monitor_device_match, device); + if (!dev) + dev = monitor_device_create(monitor, device); + if (!dev) { + btd_error(adapter_id, "Failed to create Adv Monitor " + "device object."); + return; + } + + if (dev->device_lost_timer) { + g_source_remove(dev->device_lost_timer); + dev->device_lost_timer = 0; + } + + /* Reset the timings of found/lost if a device has been offline for + * longer than the high/low timeouts. + */ + if (dev->last_seen) { + if (difftime(curr_time, dev->last_seen) > + monitor->high_rssi_timeout) { + dev->high_rssi_first_seen = 0; + } + + if (difftime(curr_time, dev->last_seen) > + monitor->low_rssi_timeout) { + dev->low_rssi_first_seen = 0; + } + } + dev->last_seen = curr_time; + + /* Check for the found devices (if the device is not already found) */ + if (!dev->device_found && rssi > monitor->high_rssi) { + if (dev->high_rssi_first_seen) { + if (difftime(curr_time, dev->high_rssi_first_seen) >= + monitor->high_rssi_timeout) { + dev->device_found = true; + + DBG("Calling DeviceFound() on Adv Monitor " + "of owner %s at path %s", + monitor->app->owner, monitor->path); + + g_dbus_proxy_method_call( + monitor->proxy, "DeviceFound", + report_device_state_setup, NULL, + dev->device, NULL); + } + } else { + dev->high_rssi_first_seen = curr_time; + } + } else { + dev->high_rssi_first_seen = 0; + } + + /* Check for the lost devices (only if the device is already found, as + * it doesn't make any sense to report the Device Lost event if the + * device is not found yet) + */ + if (dev->device_found && rssi < monitor->low_rssi) { + if (dev->low_rssi_first_seen) { + if (difftime(curr_time, dev->low_rssi_first_seen) >= + monitor->low_rssi_timeout) { + dev->device_found = false; + + DBG("Calling DeviceLost() on Adv Monitor " + "of owner %s at path %s", + monitor->app->owner, monitor->path); + + g_dbus_proxy_method_call( + monitor->proxy, "DeviceLost", + report_device_state_setup, NULL, + dev->device, NULL); + } + } else { + dev->low_rssi_first_seen = curr_time; + } + } else { + dev->low_rssi_first_seen = 0; + } + + /* Setup a timer to track if the device goes offline/out-of-range, only + * if we are tracking for the Low RSSI Threshold. If we are tracking + * the High RSSI Threshold, nothing needs to be done. + */ + if (dev->device_found) { + dev->device_lost_timer = + g_timeout_add_seconds(monitor->low_rssi_timeout, + handle_device_lost_timeout, dev); + } +} diff --git a/src/adv_monitor.h b/src/adv_monitor.h index 5cb372217..13d5d7282 100644 --- a/src/adv_monitor.h +++ b/src/adv_monitor.h @@ -12,6 +12,7 @@ #define __ADV_MONITOR_H struct mgmt; +struct btd_device; struct btd_adapter; struct btd_adv_monitor_manager; @@ -20,4 +21,7 @@ struct btd_adv_monitor_manager *btd_adv_monitor_manager_create( struct mgmt *mgmt); void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager); +void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager, + struct btd_device *device); + #endif /* __ADV_MONITOR_H */ From patchwork Tue Oct 6 00:48:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miao-chen Chou X-Patchwork-Id: 267940 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=-10.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, 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 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 A52FCC4363A for ; Tue, 6 Oct 2020 00:49:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5B4D6207F7 for ; Tue, 6 Oct 2020 00:49:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="OUKofAHb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726803AbgJFAtd (ORCPT ); Mon, 5 Oct 2020 20:49:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725865AbgJFAta (ORCPT ); Mon, 5 Oct 2020 20:49:30 -0400 Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44397C0613CE for ; Mon, 5 Oct 2020 17:49:29 -0700 (PDT) Received: by mail-pj1-x1043.google.com with SMTP id ds1so707747pjb.5 for ; Mon, 05 Oct 2020 17:49:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Lk7bODG5y3+0RhDUq3vlG8yhUFCiComhhZ2Ud4bQAQI=; b=OUKofAHbbSw6dLWFuK8VdvJJY+9q61lFzLKVoNToKFiVxol+gSh4Tk/68IkegyScoI MdCtg4ALhzgFrEYErVdjrnCF8g7kcrEuTs7LMszZu6t8wyQc+GrxUFlLdPp2sTeNbuTZ g0plEpZweTjK00IahMwg8N79zH3N1DZq5tHWE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Lk7bODG5y3+0RhDUq3vlG8yhUFCiComhhZ2Ud4bQAQI=; b=A1m3ldJoxWMhecaVbc9PXPuxSfxXXp4gGcGer4pgjGmF3G2CkdLjaX9+9wTLuPXfyE s3g3RBUpEMYDiMkNe2VkbdjR/iq4GfUg9zPFnJIcOpv41+nMfFjNc8q5Cqxb5yDckDdk rO0wF2UkH5w7AyUKAeqJ/qxdJMUogGYNHmY7uBDPP4RjTvqvywCJ8mC9AcXIAsGrpBeK 6JFiIU4qu/mCYMAvec7JyDYxVatNe7AUhBMF876rNLdHOefCcyoQk6GO1z0DEOPyeW8a wiEYRJWXtF1qm1tTfzYxExPHVtf1DKXKhyANWW1y/P0KE1P/eYJ/oLtcEPR9cwFJnbD2 vRjQ== X-Gm-Message-State: AOAM530Rn5/04WwkI/HYph/8R52hckItAywZz0kUT96vkFuyiJUtzPXC SRZB2yX+0oJDEXu+r6M6IfJnBl54GgAhHQ== X-Google-Smtp-Source: ABdhPJyCFFggrsJWuFmmKSH8Pq+vUkRtutTORNK4Rm24rq1DiE0C8G1edz85FbxjRMYsLeDe/fNShw== X-Received: by 2002:a17:90a:ba06:: with SMTP id s6mr2015401pjr.13.1601945368541; Mon, 05 Oct 2020 17:49:28 -0700 (PDT) Received: from mcchou0.mtv.corp.google.com ([2620:15c:202:201:de4a:3eff:fe75:1314]) by smtp.gmail.com with ESMTPSA id h2sm776947pjv.4.2020.10.05.17.49.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Oct 2020 17:49:27 -0700 (PDT) From: Miao-chen Chou To: Bluetooth Kernel Mailing List Cc: Manish Mandlik , Alain Michaud , Howard Chung , Luiz Augusto von Dentz , Marcel Holtmann , chromeos-bluetooth-upstreaming@chromium.org, Miao-chen Chou Subject: [BlueZ PATCH v5 3/6] adapter: Clear all Adv monitors upon bring-up Date: Mon, 5 Oct 2020 17:48:04 -0700 Message-Id: <20201005174640.BlueZ.v5.3.Id6bfe7838831ae01fddc8605689dd77b51673960@changeid> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201005174640.BlueZ.v5.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> References: <20201005174640.BlueZ.v5.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This clears all Adv monitors upon daemon bring-up by issuing MGMT_OP_REMOVE_ADV_MONITOR command with monitor_handle 0. The following test was performed: - Add an Adv Monitor using btmgmt, restart bluetoothd and observe the monitor got removed. Reviewed-by: Alain Michaud Reviewed-by: Manish Mandlik Reviewed-by: Howard Chung --- (no changes since v1) src/adapter.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/adapter.c b/src/adapter.c index fdd9b3808..c7179fc2e 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -9500,6 +9500,43 @@ failed: btd_adapter_unref(adapter); } +static void reset_adv_monitors_complete(uint8_t status, uint16_t length, + const void *param, void *user_data) +{ + const struct mgmt_rp_remove_adv_monitor *rp = param; + + if (status != MGMT_STATUS_SUCCESS) { + error("Failed to reset Adv Monitors: %s (0x%02x)", + mgmt_errstr(status), status); + return; + } + + if (length < sizeof(*rp)) { + error("Wrong size of remove Adv Monitor response for reset " + "all Adv Monitors"); + return; + } + + DBG("Removed all Adv Monitors"); +} + +static void reset_adv_monitors(uint16_t index) +{ + struct mgmt_cp_remove_adv_monitor cp; + + DBG("sending remove Adv Monitor command with handle 0"); + + /* Handle 0 indicates to remove all */ + cp.monitor_handle = 0; + if (mgmt_send(mgmt_master, MGMT_OP_REMOVE_ADV_MONITOR, index, + sizeof(cp), &cp, reset_adv_monitors_complete, NULL, + NULL) > 0) { + return; + } + + error("Failed to reset Adv Monitors"); +} + static void index_added(uint16_t index, uint16_t length, const void *param, void *user_data) { @@ -9514,6 +9551,8 @@ static void index_added(uint16_t index, uint16_t length, const void *param, return; } + reset_adv_monitors(index); + adapter = btd_adapter_new(index); if (!adapter) { btd_error(index, From patchwork Tue Oct 6 00:48:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miao-chen Chou X-Patchwork-Id: 267939 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=-10.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, 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 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 867DCC4363D for ; Tue, 6 Oct 2020 00:50:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 405CD2076B for ; Tue, 6 Oct 2020 00:50:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="k/GTK4tQ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726670AbgJFAua (ORCPT ); Mon, 5 Oct 2020 20:50:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725865AbgJFAu3 (ORCPT ); Mon, 5 Oct 2020 20:50:29 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6D9BC0613CE for ; Mon, 5 Oct 2020 17:50:29 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id q123so8037408pfb.0 for ; Mon, 05 Oct 2020 17:50:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NNRgRSderdSS3/6xE9zj19rptSYxh7kkWx+s2FGxQsU=; b=k/GTK4tQ63QibfmqIa43pUmMrnc6GD441aeYjV0IqJWl/jXToNZIE4audNeVDkqjhy gDyYXtV61vzvyqfJGY9lN4VXsENHKISZusHA/zG8AXJiJTpStcG+/K0yLsgxrR5bcrx5 TlSXaomULkP4HJOTViJHoV/tzIROEpe8sNdBs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NNRgRSderdSS3/6xE9zj19rptSYxh7kkWx+s2FGxQsU=; b=p7ZZ1zah0w81Hd3NpeDiItWKnQGWfBTNRBMSwV9X1yOvHsglHrFvGcUlRGBh5shuQB 7xI30DvvtqHr1fJZsV26Oi5knJEbP4bYakSKAeF7MqRO8BG7eLSnVFg5I7NCpr1RxXNN kC3/cXyIZ+Wn9p+MpB6+m6jo9roQjlQC7l8Ic6DGirpbXDKKHOeRkK07rP90Gq2WQsIE 6eblWP03W9Pw27LNiwtl9K0yl2H2E+uOYxU8ihnQEOBJ3jvdE/eFYdtMIpOREQuajA6z 1VMxqX9t7ohA/tXPtXoFZfwYSjhxdOn8kw5ZoUr9UFEEEu/xQJAGU/hqu19/zQxj8OdA e+bA== X-Gm-Message-State: AOAM530OQWq2vgH4E6ojCi+UltncbY9gayDSsD6vVk23MjOErDNZlch3 NE0rG6EmjM566Qjk9ADIQtMoMgqiZYGP8g== X-Google-Smtp-Source: ABdhPJzQrn461FWrd6TZmoneKWACE//whoxHMrM+Xlp5SSlDqepT0Irvjealw3gycdOOQQBtnIItfg== X-Received: by 2002:a63:e445:: with SMTP id i5mr1829198pgk.185.1601945428984; Mon, 05 Oct 2020 17:50:28 -0700 (PDT) Received: from mcchou0.mtv.corp.google.com ([2620:15c:202:201:de4a:3eff:fe75:1314]) by smtp.gmail.com with ESMTPSA id h2sm776947pjv.4.2020.10.05.17.50.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Oct 2020 17:50:28 -0700 (PDT) From: Miao-chen Chou To: Bluetooth Kernel Mailing List Cc: Manish Mandlik , Alain Michaud , Howard Chung , Luiz Augusto von Dentz , Marcel Holtmann , chromeos-bluetooth-upstreaming@chromium.org, Miao-chen Chou Subject: [BlueZ PATCH v5 5/6] adv_monitor: Fix return type of RegisterMonitor() method Date: Mon, 5 Oct 2020 17:48:08 -0700 Message-Id: <20201005174640.BlueZ.v5.5.Ic8dbe9115e82704b4c0c860eee27ad897db13237@changeid> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201005174640.BlueZ.v5.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> References: <20201005174640.BlueZ.v5.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This modifies the D-Bus call return type to be asynchronous for RegisterMonitor() method call. The following test was performed: - Enter bluetoothctl, exit the console and re-enter the console without AlreadyExist error for RegisterMonitor() upon bring-up of the console. Reviewed-by: Howard Chung Reviewed-by: Manish Mandlik --- (no changes since v1) src/adv_monitor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/adv_monitor.c b/src/adv_monitor.c index d5caf4dd3..f56c2cab2 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -737,6 +737,8 @@ static struct adv_monitor_app *app_create(DBusConnection *conn, app->monitors = queue_new(); + app->reg = dbus_message_ref(msg); + g_dbus_client_set_disconnect_watch(app->client, app_disconnect_cb, app); /* Note that any property changes on a monitor object would not affect @@ -748,8 +750,6 @@ static struct adv_monitor_app *app_create(DBusConnection *conn, g_dbus_client_set_ready_watch(app->client, app_ready_cb, app); - app->reg = dbus_message_ref(msg); - return app; } @@ -843,7 +843,7 @@ static DBusMessage *unregister_monitor(DBusConnection *conn, } static const GDBusMethodTable adv_monitor_methods[] = { - { GDBUS_EXPERIMENTAL_METHOD("RegisterMonitor", + { GDBUS_EXPERIMENTAL_ASYNC_METHOD("RegisterMonitor", GDBUS_ARGS({ "application", "o" }), NULL, register_monitor) }, { GDBUS_EXPERIMENTAL_ASYNC_METHOD("UnregisterMonitor",