From patchwork Wed Oct 7 00:14: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: 268260 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 92083C4363D for ; Wed, 7 Oct 2020 00:14:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2CEC520739 for ; Wed, 7 Oct 2020 00:14:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="IbXBCxUF" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726645AbgJGAOn (ORCPT ); Tue, 6 Oct 2020 20:14:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725972AbgJGAOn (ORCPT ); Tue, 6 Oct 2020 20:14:43 -0400 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D75FC061755 for ; Tue, 6 Oct 2020 17:14:43 -0700 (PDT) Received: by mail-pl1-x642.google.com with SMTP id d23so98279pll.7 for ; Tue, 06 Oct 2020 17:14:43 -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=+r599FHAhWvPt7WJozfeRu96qgvTR2YS5E9U7hx3KSk=; b=IbXBCxUF2X/WNv5j4eHCh4QGCK13Q31KDL5En208LHQ7c4RobP2FzkozeC8p3JQJ1n xvZ64uVOitQ0YY5K91dNKEhonMD6jo/qJ3fCoGz/ORLje0eucWYFWDiXxwu4h+OpSL5c zsexF9Asc2Qa0teaon3cvW/AwAgUyPGcKBSVk= 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=+r599FHAhWvPt7WJozfeRu96qgvTR2YS5E9U7hx3KSk=; b=W6gHQoFXj0OB+rhrCXD0C3B44x9iay/r5eqLAJV5x0rWPGcdJ8lCpLaUxMwIkXiKZD vXXgXJoUUio3EnJlXWHwhJpf6wVYaS6hcBnGutRucdXfpReWOIhZdQVqc2ViFV4lqftM 08ENEEg4Ui2gwtGDHD/4do67Yjjz4yBjyLzLrbVy+WrDC077QFJTVtQg9erKCrmvtPES i716sv3IvSTiqPZy+R3JjlNv5PFGh8i8cwIbAVRHsElFrPVzlX8tRA1MV8tx2tv9GMG4 PGOPePFh6E5Qt0b5RNY9ynRmSFfITD63fpey12Z1JMYsnQYWMBJPOsQuLUnAaEKR9wvd 7HDw== X-Gm-Message-State: AOAM532PquHZ2yDYkUchXnM2wfgKu1wNYPDpd3VLK2gqtkMxlNHsYzeV 5Lj0twDZPoALFZRTdlYg9rj6DeKqN6U87Q== X-Google-Smtp-Source: ABdhPJxuEbOZ1zXOOhIcU4baV0XKqLAUulsC2slqyCAoHxdmB7udvT/ZFzDfywdkkuD49FkroawGaw== X-Received: by 2002:a17:902:be0c:b029:d2:8ceb:f39c with SMTP id r12-20020a170902be0cb02900d28cebf39cmr389200pls.71.1602029682225; Tue, 06 Oct 2020 17:14:42 -0700 (PDT) Received: from mcchou0.mtv.corp.google.com ([2620:15c:202:201:de4a:3eff:fe75:1314]) by smtp.gmail.com with ESMTPSA id s13sm329722pgo.56.2020.10.06.17.14.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2020 17:14:41 -0700 (PDT) From: Miao-chen Chou To: Bluetooth Kernel Mailing List Cc: Luiz Augusto von Dentz , Howard Chung , chromeos-bluetooth-upstreaming@chromium.org, Alain Michaud , Marcel Holtmann , Manish Mandlik , Miao-chen Chou , Abhishek Pandit-Subedi Subject: [BlueZ PATCH v6 2/6] adv_monitor: Implement Adv matching based on stored monitors Date: Tue, 6 Oct 2020 17:14:08 -0700 Message-Id: <20201006171333.BlueZ.v6.2.I578ae5e76fcf7243206a27d4f5a25783662a5f14@changeid> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201006171333.BlueZ.v6.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> References: <20201006171333.BlueZ.v6.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This implements create an entry point in adapter to start the matching of Adv based on all monitors and invoke the RSSI tracking for Adv reporting. Reviewed-by: Abhishek Pandit-Subedi Reviewed-by: Alain Michaud Reviewed-by: Manish Mandlik --- Changes in v6: - Fix the termination condition of AD data paring and remove unnecessary length check Changes in v5: - Remove unittest helper functions Changes in v3: - Remove unused variables - Fix signature of queue_find() src/adapter.c | 35 ++++++-- src/adv_monitor.c | 219 ++++++++++++++++++++++++++++++++++++++++------ src/adv_monitor.h | 12 +++ 3 files changed, 234 insertions(+), 32 deletions(-) diff --git a/src/adapter.c b/src/adapter.c index 6d0114a6b..fdd9b3808 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -6601,6 +6601,15 @@ static void update_found_devices(struct btd_adapter *adapter, bool name_known, discoverable; char addr[18]; bool duplicate = false; + GSList *matched_monitors; + + /* During the background scanning, update the device only when the data + * match at least one Adv monitor + */ + matched_monitors = btd_adv_monitor_content_filter( + adapter->adv_monitor_manager, data, data_len); + if (!adapter->discovering && !matched_monitors) + return; memset(&eir_data, 0, sizeof(eir_data)); eir_parse(&eir_data, data, data_len); @@ -6646,18 +6655,22 @@ static void update_found_devices(struct btd_adapter *adapter, device_store_cached_name(dev, eir_data.name); /* - * Only skip devices that are not connected, are temporary and there - * is no active discovery session ongoing. + * Only skip devices that are not connected, are temporary, and there + * is no active discovery session ongoing and no matched Adv monitors */ - if (!btd_device_is_connected(dev) && (device_is_temporary(dev) && - !adapter->discovery_list)) { + if (!btd_device_is_connected(dev) && + (device_is_temporary(dev) && !adapter->discovery_list) && + !matched_monitors) { eir_data_free(&eir_data); return; } - /* Don't continue if not discoverable or if filter don't match */ - if (!discoverable || (adapter->filtered_discovery && - !is_filter_match(adapter->discovery_list, &eir_data, rssi))) { + /* If there is no matched Adv monitors, don't continue if not + * discoverable or if active discovery filter don't match. + */ + if (!matched_monitors && (!discoverable || + (adapter->filtered_discovery && !is_filter_match( + adapter->discovery_list, &eir_data, rssi)))) { eir_data_free(&eir_data); return; } @@ -6714,6 +6727,14 @@ static void update_found_devices(struct btd_adapter *adapter, eir_data_free(&eir_data); + /* After the device is updated, notify the matched Adv monitors */ + if (matched_monitors) { + btd_adv_monitor_notify_monitors(adapter->adv_monitor_manager, + dev, rssi, matched_monitors); + g_slist_free(matched_monitors); + matched_monitors = NULL; + } + /* * Only if at least one client has requested discovery, maintain * list of found devices and name confirming for legacy devices. diff --git a/src/adv_monitor.c b/src/adv_monitor.c index 31ed30a00..fcb127cd4 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -29,15 +29,12 @@ #include "device.h" #include "log.h" #include "src/error.h" -#include "src/shared/ad.h" #include "src/shared/mgmt.h" #include "src/shared/queue.h" #include "src/shared/util.h" #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" @@ -84,7 +81,7 @@ enum monitor_state { MONITOR_STATE_HONORED, /* Accepted by kernel */ }; -struct pattern { +struct btd_adv_monitor_pattern { uint8_t ad_type; uint8_t offset; uint8_t length; @@ -133,6 +130,23 @@ struct app_match_data { const char *path; }; +struct adv_content_filter_info { + uint8_t eir_len; + const uint8_t *eir; + + bool matched; /* Intermediate state per monitor */ + GSList *matched_monitors; /* List of matched monitors */ +}; + +struct adv_rssi_filter_info { + struct btd_device *device; + int8_t rssi; +}; + +static void monitor_device_free(void *data); +static void adv_monitor_filter_rssi(struct adv_monitor *monitor, + struct btd_device *device, int8_t rssi); + const struct adv_monitor_type { enum monitor_type type; const char *name; @@ -155,7 +169,7 @@ static void app_reply_msg(struct adv_monitor_app *app, DBusMessage *reply) /* Frees a pattern */ static void pattern_free(void *data) { - struct pattern *pattern = data; + struct btd_adv_monitor_pattern *pattern = data; if (!pattern) return; @@ -435,6 +449,36 @@ failed: return false; } +/* Allocates and initiates a pattern with the given content */ +static struct btd_adv_monitor_pattern *pattern_create( + uint8_t ad_type, uint8_t offset, uint8_t length, const uint8_t *value) +{ + struct btd_adv_monitor_pattern *pattern; + + if (offset > BT_AD_MAX_DATA_LEN - 1) + return NULL; + + if ((ad_type > BT_AD_3D_INFO_DATA && + ad_type != BT_AD_MANUFACTURER_DATA) || + ad_type < BT_AD_FLAGS) { + return NULL; + } + + if (!value || !length || offset + length > BT_AD_MAX_DATA_LEN) + return NULL; + + pattern = new0(struct btd_adv_monitor_pattern, 1); + if (!pattern) + return NULL; + + pattern->ad_type = ad_type; + pattern->offset = offset; + pattern->length = length; + memcpy(pattern->value, value, pattern->length); + + return pattern; +} + /* Retrieves Patterns from the remote Adv Monitor object, verifies the values * and update the local Adv Monitor */ @@ -464,7 +508,7 @@ static bool parse_patterns(struct adv_monitor *monitor, const char *path) int value_len; uint8_t *value; uint8_t offset, ad_type; - struct pattern *pattern; + struct btd_adv_monitor_pattern *pattern; DBusMessageIter struct_iter, value_iter; dbus_message_iter_recurse(&array_iter, &struct_iter); @@ -496,28 +540,10 @@ static bool parse_patterns(struct adv_monitor *monitor, const char *path) dbus_message_iter_get_fixed_array(&value_iter, &value, &value_len); - // Verify the values - if (offset > BT_AD_MAX_DATA_LEN - 1) - goto failed; - - if ((ad_type > BT_AD_3D_INFO_DATA && - ad_type != BT_AD_MANUFACTURER_DATA) || - ad_type < BT_AD_FLAGS) { - goto failed; - } - - if (!value || value_len <= 0 || value_len > BT_AD_MAX_DATA_LEN) - goto failed; - - pattern = new0(struct pattern, 1); + pattern = pattern_create(ad_type, offset, value_len, value); if (!pattern) goto failed; - pattern->ad_type = ad_type; - pattern->offset = offset; - pattern->length = value_len; - memcpy(pattern->value, value, pattern->length); - queue_push_tail(monitor->patterns, pattern); dbus_message_iter_next(&array_iter); @@ -952,6 +978,149 @@ void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager) manager_destroy(manager); } +static bool pattern_match(const uint8_t *eir, uint8_t eir_len, + const struct btd_adv_monitor_pattern *pattern) +{ + const uint8_t *data; + uint8_t idx = 0; + uint8_t field_len, data_len, data_type; + + while (idx < eir_len - 1) { + field_len = eir[0]; + + /* Check for the end of EIR */ + if (field_len == 0) + break; + + idx += field_len + 1; + + /* Do not continue filtering if got incorrect length */ + if (idx > eir_len) + break; + + data = &eir[2]; + data_type = eir[1]; + data_len = field_len - 1; + + eir += field_len + 1; + + if (data_type != pattern->ad_type) + continue; + + if (data_len < pattern->offset + pattern->length) + continue; + + if (!memcmp(data + pattern->offset, pattern->value, + pattern->length)) + return true; + } + + return false; +} + +/* Processes the content matching based on a pattern */ +static void adv_match_per_pattern(void *data, void *user_data) +{ + struct btd_adv_monitor_pattern *pattern = data; + struct adv_content_filter_info *info = user_data; + + if (!pattern || info->matched) + return; + + info->matched = pattern_match(info->eir, info->eir_len, pattern); +} + +/* Processes the content matching based pattern(s) of a monitor */ +static void adv_match_per_monitor(void *data, void *user_data) +{ + struct adv_monitor *monitor = data; + struct adv_content_filter_info *info = user_data; + + if (!monitor && monitor->state != MONITOR_STATE_HONORED) + return; + + /* Reset the intermediate matched status */ + info->matched = false; + + if (monitor->type == MONITOR_TYPE_OR_PATTERNS) { + queue_foreach(monitor->patterns, adv_match_per_pattern, info); + if (info->matched) + goto matched; + } + + return; + +matched: + info->matched_monitors = g_slist_prepend(info->matched_monitors, + monitor); +} + +/* Processes the content matching for the monitor(s) of an app */ +static void adv_match_per_app(void *data, void *user_data) +{ + struct adv_monitor_app *app = data; + + if (!app) + return; + + queue_foreach(app->monitors, adv_match_per_monitor, user_data); +} + +/* Processes the content matching for every app without RSSI filtering and + * notifying monitors. The caller is responsible of releasing the memory of the + * list but not the data. + * Returns the list of monitors whose content match eir. + */ +GSList *btd_adv_monitor_content_filter(struct btd_adv_monitor_manager *manager, + const uint8_t *eir, uint8_t eir_len) +{ + struct adv_content_filter_info info; + + if (!manager || !eir || !eir_len) + return NULL; + + info.eir_len = eir_len; + info.eir = eir; + info.matched_monitors = NULL; + + queue_foreach(manager->apps, adv_match_per_app, &info); + + return info.matched_monitors; +} + +/* Wraps adv_monitor_filter_rssi() to processes the content-matched monitor with + * RSSI filtering and notifies it on device found/lost event + */ +static void monitor_filter_rssi(gpointer a, gpointer b) +{ + struct adv_monitor *monitor = a; + struct adv_rssi_filter_info *info = b; + + if (!monitor || !info) + return; + + adv_monitor_filter_rssi(monitor, info->device, info->rssi); +} + +/* Processes every content-matched monitor with RSSI filtering and notifies on + * device found/lost event. The caller is responsible of releasing the memory + * of matched_monitors list but not its data. + */ +void btd_adv_monitor_notify_monitors(struct btd_adv_monitor_manager *manager, + struct btd_device *device, int8_t rssi, + GSList *matched_monitors) +{ + struct adv_rssi_filter_info info; + + if (!manager || !device || !matched_monitors) + return; + + info.device = device; + info.rssi = rssi; + + g_slist_foreach(matched_monitors, monitor_filter_rssi, &info); +} + /* Matches a device based on btd_device object */ static bool monitor_device_match(const void *a, const void *b) { diff --git a/src/adv_monitor.h b/src/adv_monitor.h index 13d5d7282..e2482e11e 100644 --- a/src/adv_monitor.h +++ b/src/adv_monitor.h @@ -11,16 +11,28 @@ #ifndef __ADV_MONITOR_H #define __ADV_MONITOR_H +#include + +#include "src/shared/ad.h" + struct mgmt; struct btd_device; struct btd_adapter; struct btd_adv_monitor_manager; +struct btd_adv_monitor_pattern; struct btd_adv_monitor_manager *btd_adv_monitor_manager_create( struct btd_adapter *adapter, struct mgmt *mgmt); void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager); +GSList *btd_adv_monitor_content_filter(struct btd_adv_monitor_manager *manager, + const uint8_t *eir, uint8_t eir_len); + +void btd_adv_monitor_notify_monitors(struct btd_adv_monitor_manager *manager, + struct btd_device *device, int8_t rssi, + GSList *matched_monitors); + void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager, struct btd_device *device); From patchwork Wed Oct 7 00:14:12 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: 268259 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 7DE9EC4363D for ; Wed, 7 Oct 2020 00:15:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4657120760 for ; Wed, 7 Oct 2020 00:15:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="RHG4Ef2p" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726760AbgJGAPO (ORCPT ); Tue, 6 Oct 2020 20:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725972AbgJGAPN (ORCPT ); Tue, 6 Oct 2020 20:15:13 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 507C8C061755 for ; Tue, 6 Oct 2020 17:15:12 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id d6so85002plo.13 for ; Tue, 06 Oct 2020 17:15:12 -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=xFbsQt7QSfdThIVO9V8CS0xb2S+SQXYSOQ607Jphq0c=; b=RHG4Ef2pJHG7EvWzrKlzZfg4G8oeTgCfFmBOhX6qdJWHhp9doB/zgAgy93/3h4AoRO t5pWAAQPhoaBJsn+btLeRUIkrSPcD2fk6Db1VNHp5K7Q6OqBjayi4++Xgl3649C5gTxF 2E5wQwCB5ByCpuR4siF/aRYKnVOTuOUwkHt3c= 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=xFbsQt7QSfdThIVO9V8CS0xb2S+SQXYSOQ607Jphq0c=; b=pYcGOGlTEwk4M/siSAs8Ado1D8xfwd2+uHmfuovYt99VjVOdutkP4BvwtsImWs6k6g m2jAqI7PHRr29xd+p8w4JcwU8XIUxvj39RzafPBBx//0inCqVBTIp84JM6iffRtkIm11 Aj5j2IFt0fmIReYwzypD5ijlzd6WSTYhJw2EqEwI44K8cxAousMd+CteHgR2FWfiUoeB 09J+iYIuPvDV6usZCn3+OJyDh875s31QEhJ16gf0yXjlvnCTnimdFYbC//8XLRU4frxM lHQAiZQUCfFfS1faagaf/kGP1ftGk7Cot26t+HlnIOO4ZC77FdkiTCYcTRbsWHrAwR35 TkSQ== X-Gm-Message-State: AOAM531I6apQm7V7s1TCERoSTmP3MRsPdSnphsyrWgKcQUCC/JGb5cxw 3tarJDTCLIUDahcN/VKjOUEohr7LG6unig== X-Google-Smtp-Source: ABdhPJx+5JJwGIZpCzfv4VOyBh5Mh06eR4ECAbFgHfiKgBbh3pP4zhRMOoy0GgQ/KDqkQudnB01jxw== X-Received: by 2002:a17:90a:c28f:: with SMTP id f15mr512824pjt.143.1602029711400; Tue, 06 Oct 2020 17:15:11 -0700 (PDT) Received: from mcchou0.mtv.corp.google.com ([2620:15c:202:201:de4a:3eff:fe75:1314]) by smtp.gmail.com with ESMTPSA id s13sm329722pgo.56.2020.10.06.17.15.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2020 17:15:10 -0700 (PDT) From: Miao-chen Chou To: Bluetooth Kernel Mailing List Cc: Luiz Augusto von Dentz , Howard Chung , chromeos-bluetooth-upstreaming@chromium.org, Alain Michaud , Marcel Holtmann , Manish Mandlik , Miao-chen Chou Subject: [BlueZ PATCH v6 4/6] adv_monitor: Implement Add Adv Patterns Monitor cmd handler Date: Tue, 6 Oct 2020 17:14:12 -0700 Message-Id: <20201006171333.BlueZ.v6.4.Ibbcb11712b613ef95c31b41207c3ea945c830018@changeid> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201006171333.BlueZ.v6.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> References: <20201006171333.BlueZ.v6.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Howard Chung - Send the MGMT_OP command to kernel upon registration of a Adv patterns monitor. - Call Activate() or Release() to client depending on the reply from kernel the call through syslog Reviewed-by: Alain Michaud Reviewed-by: Miao-chen Chou Reviewed-by: Manish Mandlik --- (no changes since v1) src/adv_monitor.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/adv_monitor.c b/src/adv_monitor.c index fcb127cd4..582cc9a46 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -588,11 +588,59 @@ done: return monitor->state != MONITOR_STATE_FAILED; } +/* Handles the callback of Add Adv Patterns Monitor command */ +static void add_adv_patterns_monitor_cb(uint8_t status, uint16_t length, + const void *param, void *user_data) +{ + const struct mgmt_rp_add_adv_patterns_monitor *rp = param; + struct adv_monitor *monitor = user_data; + uint16_t adapter_id = monitor->app->manager->adapter_id; + + if (status != MGMT_STATUS_SUCCESS || !param) { + btd_error(adapter_id, "Failed to Add Adv Patterns Monitor " + "with status 0x%02x", status); + monitor_release(monitor, NULL); + return; + } + + if (length < sizeof(*rp)) { + btd_error(adapter_id, "Wrong size of Add Adv Patterns Monitor " + "response"); + monitor_release(monitor, NULL); + return; + } + + monitor->state = MONITOR_STATE_HONORED; + + DBG("Calling Activate() on Adv Monitor of owner %s at path %s", + monitor->app->owner, monitor->path); + + g_dbus_proxy_method_call(monitor->proxy, "Activate", NULL, NULL, NULL, + NULL); + + DBG("Adv Monitor with handle:0x%04x added", + le16_to_cpu(rp->monitor_handle)); +} + +static void monitor_copy_patterns(void *data, void *user_data) +{ + struct btd_adv_monitor_pattern *pattern = data; + struct mgmt_cp_add_adv_monitor *cp = user_data; + + if (!pattern) + return; + + memcpy(cp->patterns + cp->pattern_count, pattern, sizeof(*pattern)); + cp->pattern_count++; +} + /* Handles an Adv Monitor D-Bus proxy added event */ 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); @@ -625,7 +673,24 @@ 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; + } + DBG("Adv Monitor allocated for the object at path %s", path); + +done: + free(cp); } /* Handles the removal of an Adv Monitor D-Bus proxy */ @@ -1036,7 +1101,7 @@ static void adv_match_per_monitor(void *data, void *user_data) struct adv_monitor *monitor = data; struct adv_content_filter_info *info = user_data; - if (!monitor && monitor->state != MONITOR_STATE_HONORED) + if (!monitor || monitor->state != MONITOR_STATE_HONORED) return; /* Reset the intermediate matched status */ From patchwork Wed Oct 7 00:14:16 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: 268258 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 1575CC4363D for ; Wed, 7 Oct 2020 00:15:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BA9B0206F0 for ; Wed, 7 Oct 2020 00:15:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="XoYPdBfc" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726702AbgJGAPo (ORCPT ); Tue, 6 Oct 2020 20:15:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725972AbgJGAPo (ORCPT ); Tue, 6 Oct 2020 20:15:44 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EBF8C061755 for ; Tue, 6 Oct 2020 17:15:44 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id x5so101664plo.6 for ; Tue, 06 Oct 2020 17:15:44 -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=dZCn9oH2uJ0FfOYiLVTz7nvZ+6XiseTudHsjrzTgHz4=; b=XoYPdBfcjuVOHPMYX6eHopm/kXh1YERsbeimaewLctuJsy3WQRh8OXr1WFrSmKUcKX GrZNqu4Rg1fYlGkpJY/DxWnOThS+1aO1VhKexbXrriwS9j/8EUsP3f/X/KV6hizn3utQ kwvD+dq0B87y+5nbMg+VvqkqL85fgZiDNX+3I= 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=dZCn9oH2uJ0FfOYiLVTz7nvZ+6XiseTudHsjrzTgHz4=; b=f+bXTa/VlcD+FZrKwfg09EdidDO/8uzDAf4J/E9MjIFfN8mbEOpUObfwdS5TmvTk4h psqq5NHB0PrPSkPtpbAnN+AnHVAh8Z3WNWKs4eprC5I2Aan6IN+ZZyLAfFJMqQ43CxeI h5dJgYmL0gsWUZKoK/w3WfMf6sjiOWI5YxH4L0jl68nQK7o3K2XKjUHKxJ0EXUwk8VH3 MCs9oDgZBOh/gjtQFME8Y1rMKwWUGZwH8ZmJhgPQwcZdIPMnXMxAOOyb/mbfCdLblYrl cxJEtjV9Otu/Uj0CGN3OWrdtjKasipiRrChftqKkVY7HiQRloeoxWTOymH33ozoiBAJB ONLg== X-Gm-Message-State: AOAM532XXFUcYMpHWNMJqXJHXxY/Q/uC5/7Ec6Pg2jntKEw8liYlMpuZ jf+k8xTnWVapmWUJIdn8lE+K4saZ/MAnng== X-Google-Smtp-Source: ABdhPJwlLJnRjkNKFiy4buk4eXMYuVx6eWnJXUUMfvxrRBvNcxJRpWx+zVd42ywrNWDp5I2vPnHH2g== X-Received: by 2002:a17:90a:fd03:: with SMTP id cv3mr540277pjb.137.1602029743417; Tue, 06 Oct 2020 17:15:43 -0700 (PDT) Received: from mcchou0.mtv.corp.google.com ([2620:15c:202:201:de4a:3eff:fe75:1314]) by smtp.gmail.com with ESMTPSA id s13sm329722pgo.56.2020.10.06.17.15.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2020 17:15:42 -0700 (PDT) From: Miao-chen Chou To: Bluetooth Kernel Mailing List Cc: Luiz Augusto von Dentz , Howard Chung , chromeos-bluetooth-upstreaming@chromium.org, Alain Michaud , Marcel Holtmann , Manish Mandlik Subject: [BlueZ PATCH v6 6/6] adv_monitor: Issue Remove Adv Monitor mgmt call Date: Tue, 6 Oct 2020 17:14:16 -0700 Message-Id: <20201006171333.BlueZ.v6.6.Ifda683c92ff520bf58ac37c02dc40b8d9598d1b0@changeid> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201006171333.BlueZ.v6.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> References: <20201006171333.BlueZ.v6.1.I2830b9c1212a64b062201ed9f2b71294f50ad22d@changeid> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Alain Michaud This calls Remove Adv Monitor command to kernel and handles the callback during a monitor removal initiated by a D-Bus client. This also registers callback for getting notified on Adv Monitor Removed event, so that the Adv monitor manager can invalidate the monitor by calling Release() on its proxy. The following tests were performed. - In bluetoothctl console, add a monitor and remove the monitor by its index and verify the removal in both the output of btmgmt and syslog. - In bluetoothctl console, add a monitor, remove the monitor via btmgmt and verify the removal in syslog. Reviewed-by: Howard Chung Reviewed-by: Alain Michaud --- (no changes since v4) Changes in v4: - Fix build error Changes in v3: - Fix const qualifier of a pointer src/adv_monitor.c | 132 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 124 insertions(+), 8 deletions(-) diff --git a/src/adv_monitor.c b/src/adv_monitor.c index 33edbf00c..08adc7fa4 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -79,6 +79,7 @@ enum monitor_state { MONITOR_STATE_FAILED, /* Failed to be init'ed */ MONITOR_STATE_INITED, /* Init'ed but not yet sent to kernel */ MONITOR_STATE_HONORED, /* Accepted by kernel */ + MONITOR_STATE_REMOVING, /* Removing from kernel */ }; struct btd_adv_monitor_pattern { @@ -94,6 +95,7 @@ struct adv_monitor { char *path; enum monitor_state state; /* MONITOR_STATE_* */ + uint16_t monitor_handle; /* Kernel Monitor Handle */ int8_t high_rssi; /* High RSSI threshold */ uint16_t high_rssi_timeout; /* High RSSI threshold timeout */ @@ -610,6 +612,7 @@ static void add_adv_patterns_monitor_cb(uint8_t status, uint16_t length, return; } + monitor->monitor_handle = le16_to_cpu(rp->monitor_handle); monitor->state = MONITOR_STATE_HONORED; DBG("Calling Activate() on Adv Monitor of owner %s at path %s", @@ -618,8 +621,7 @@ static void add_adv_patterns_monitor_cb(uint8_t status, uint16_t length, g_dbus_proxy_method_call(monitor->proxy, "Activate", NULL, NULL, NULL, NULL); - DBG("Adv Monitor with handle:0x%04x added", - le16_to_cpu(rp->monitor_handle)); + DBG("Adv monitor with handle:0x%04x added", monitor->monitor_handle); } static void monitor_copy_patterns(void *data, void *user_data) @@ -693,20 +695,77 @@ done: free(cp); } +/* Handles the callback of Remove Adv Monitor command */ +static void remove_adv_monitor_cb(uint8_t status, uint16_t length, + const void *param, void *user_data) +{ + struct adv_monitor *monitor = user_data; + const struct mgmt_rp_remove_adv_monitor *rp = param; + uint16_t adapter_id = monitor->app->manager->adapter_id; + + if (status != MGMT_STATUS_SUCCESS || !param) { + btd_error(adapter_id, "Failed to Remove Adv Monitor with " + "status 0x%02x", status); + goto done; + } + + if (length < sizeof(*rp)) { + btd_error(adapter_id, "Wrong size of Remove Adv Monitor " + "response"); + goto done; + } + +done: + queue_remove(monitor->app->monitors, monitor); + + DBG("Adv Monitor removed with handle:0x%04x, path %s", + monitor->monitor_handle, monitor->path); + + monitor_free(monitor); +} + + /* Handles the removal of an Adv Monitor D-Bus proxy */ static void monitor_proxy_removed_cb(GDBusProxy *proxy, void *user_data) { struct adv_monitor *monitor; + struct mgmt_cp_remove_adv_monitor cp; struct adv_monitor_app *app = user_data; + uint16_t adapter_id = app->manager->adapter_id; - monitor = queue_remove_if(app->monitors, monitor_match, proxy); - if (monitor) { - DBG("Adv Monitor removed for the object at path %s", - monitor->path); + monitor = queue_find(app->monitors, monitor_match, proxy); - /* The object was gone, so we don't need to call Release() */ - monitor_free(monitor); + /* A monitor removed event from kernel can remove a monitor and notify + * the app on Release() where this callback can be invoked, so we + * simply skip here. + */ + if (!monitor) + return; + + if (monitor->state != MONITOR_STATE_HONORED) + goto done; + + monitor->state = MONITOR_STATE_REMOVING; + + cp.monitor_handle = cpu_to_le16(monitor->monitor_handle); + + if (!mgmt_send(app->manager->mgmt, MGMT_OP_REMOVE_ADV_MONITOR, + adapter_id, sizeof(cp), &cp, remove_adv_monitor_cb, + monitor, NULL)) { + btd_error(adapter_id, "Unable to send Remove Advt Monitor " + "command"); + goto done; } + + return; + +done: + queue_remove(app->monitors, monitor); + + DBG("Adv Monitor removed in state %02x with path %s", monitor->state, + monitor->path); + + monitor_free(monitor); } /* Creates an app object, initiates it and sets D-Bus event handlers */ @@ -915,6 +974,59 @@ static const GDBusPropertyTable adv_monitor_properties[] = { { } }; +/* Matches a monitor based on its handle */ +static bool removed_monitor_match(const void *data, const void *user_data) +{ + const uint16_t *handle = user_data; + const struct adv_monitor *monitor = data; + + if (!data || !handle) + return false; + + return monitor->monitor_handle == *handle; +} + +/* Remove the matched monitor and reports the removal to the app */ +static void app_remove_monitor(void *data, void *user_data) +{ + struct adv_monitor_app *app = data; + struct adv_monitor *monitor; + + monitor = queue_find(app->monitors, removed_monitor_match, user_data); + if (monitor) { + if (monitor->state == MONITOR_STATE_HONORED) + monitor_release(monitor, NULL); + + queue_remove(app->monitors, monitor); + + DBG("Adv Monitor at path %s removed", monitor->path); + + monitor_free(monitor); + } +} + +/* Processes Adv Monitor removed event from kernel */ +static void adv_monitor_removed_callback(uint16_t index, uint16_t length, + const void *param, void *user_data) +{ + struct btd_adv_monitor_manager *manager = user_data; + const struct mgmt_ev_adv_monitor_removed *ev = param; + uint16_t handle = ev->monitor_handle; + const uint16_t adapter_id = manager->adapter_id; + + if (length < sizeof(*ev)) { + btd_error(adapter_id, "Wrong size of Adv Monitor Removed " + "event"); + return; + } + + /* Traverse the apps to find the monitor */ + queue_foreach(manager->apps, app_remove_monitor, &handle); + + DBG("Adv Monitor removed event with handle 0x%04x processed", + ev->monitor_handle); +} + /* Allocates a manager object */ static struct btd_adv_monitor_manager *manager_new( struct btd_adapter *adapter, @@ -934,6 +1046,10 @@ static struct btd_adv_monitor_manager *manager_new( manager->adapter_id = btd_adapter_get_index(adapter); manager->apps = queue_new(); + mgmt_register(manager->mgmt, MGMT_EV_ADV_MONITOR_REMOVED, + manager->adapter_id, adv_monitor_removed_callback, + manager, NULL); + return manager; }