From patchwork Thu Sep 10 04:41:26 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: 252784 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=-9.8 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 B6399C433E2 for ; Thu, 10 Sep 2020 04:42:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 76A1C20809 for ; Thu, 10 Sep 2020 04:42:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="mNCcUETr" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725912AbgIJEmU (ORCPT ); Thu, 10 Sep 2020 00:42:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725873AbgIJEmQ (ORCPT ); Thu, 10 Sep 2020 00:42:16 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37125C061573 for ; Wed, 9 Sep 2020 21:42:16 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id v15so3570420pgh.6 for ; Wed, 09 Sep 2020 21:42:16 -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=zLi61UtegLxZoRPn4T9W5e3L/HqdjAdjheNWCNy6w+M=; b=mNCcUETrSu4J2M5L+AAySz4MvYfQY3feIqA4hLyjHt/wbAGoX2kVRZUnWIZbfN5HHz 935JdnQDDEmWcS70AtOEwDqb+gZs0FJxIAd3ONu7hfkO+l9fzxKNKywh922FFsVclJHj h4VpUSDZqgg65RAXmljutzZpD/OJ7bMn52TJ4= 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=zLi61UtegLxZoRPn4T9W5e3L/HqdjAdjheNWCNy6w+M=; b=Q6sbdAGG45uWRqKkpoIrK5q2mElX/M4lzvNk+QgOqhGR2fiSjd+9nT4ixd9tQIkfuy cchVxVHkLSrXHmSCU2J1OIJHB9v9GlO84PaE75tdOPfPwRXG76VIj9L/4my0TgGg3qB3 pgJlreiFOH5T9y5QFkkmFVJjNbBUf5++EJXrF7S9rG5MTmLXTGC1IVDcFvZEnJtAlzi2 2jxdv1hdY2CGUdAXqYiZ7Nh1ksRlnICLG+gY35hpP+vq1BqXNmCdOMjBmauw5QuDi856 9MbbZuJ6L+HEWXteOOqJwYz6g1biVMUNNsoho8GokR0sbHJTstGuXOGAr+p2yZ6/AmwY iinA== X-Gm-Message-State: AOAM530PldC6nJTGxLVNwUrItHTFu1Vs4H6zn2L3YUVImiY51WEEsM31 +S2jOwXIgah/DW1K8ycA6d2KW2Fu9PaV/g== X-Google-Smtp-Source: ABdhPJxy/HuhFeimU+UuLjQ03fU2cdPjG6RTwnDDlWyrsaWZ5LGfbCUYy3nXsczIxHMLzizes3jAUA== X-Received: by 2002:aa7:9484:: with SMTP id z4mr3708258pfk.32.1599712935382; Wed, 09 Sep 2020 21:42:15 -0700 (PDT) Received: from mcchou0.mtv.corp.google.com ([2620:15c:202:201:de4a:3eff:fe75:1314]) by smtp.gmail.com with ESMTPSA id k4sm644888pjl.10.2020.09.09.21.42.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Sep 2020 21:42:14 -0700 (PDT) From: Miao-chen Chou To: Bluetooth Kernel Mailing List Cc: Manish Mandlik , Luiz Augusto von Dentz , Howard Chung , Alain Michaud , Miao-chen Chou , Abhishek Pandit-Subedi Subject: [BlueZ PATCH v3 2/7] adv_monitor: Implement Get functions of Adv monitor manager properties Date: Wed, 9 Sep 2020 21:41:26 -0700 Message-Id: <20200909214058.BlueZ.v3.2.I9960e45d36be3edb5f17de025e7eb8257d3cddef@changeid> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200909214058.BlueZ.v3.1.I205718871f4e636958904f3cfb171cfd381c54b1@changeid> References: <20200909214058.BlueZ.v3.1.I205718871f4e636958904f3cfb171cfd381c54b1@changeid> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This implements the Get functions of SupportedMonitorTypes and SupportedFeatures. The following test was performed. - Issue dbus-send to read SupportedMonitorTypes and SupportedFeatures. Reviewed-by: Yun-Hao Chung Reviewed-by: Manish Mandlik Reviewed-by: Abhishek Pandit-Subedi --- (no changes since v1) src/adv_monitor.c | 128 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 123 insertions(+), 5 deletions(-) diff --git a/src/adv_monitor.c b/src/adv_monitor.c index 8e7dad8dd..54e67bf2c 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -28,6 +28,9 @@ #include #include +#include "lib/bluetooth.h" +#include "lib/mgmt.h" + #include "adapter.h" #include "dbus-common.h" #include "log.h" @@ -43,6 +46,12 @@ struct btd_adv_monitor_manager { struct mgmt *mgmt; uint16_t adapter_id; char *path; + + uint32_t supported_features; /* MGMT_ADV_MONITOR_FEATURE_MASK_* */ + uint32_t enabled_features; /* MGMT_ADV_MONITOR_FEATURE_MASK_* */ + uint16_t max_num_monitors; + uint8_t max_num_patterns; + }; static const GDBusMethodTable adv_monitor_methods[] = { @@ -55,10 +64,78 @@ static const GDBusMethodTable adv_monitor_methods[] = { { } }; +enum monitor_type { + MONITOR_TYPE_OR_PATTERNS, +}; + +const struct adv_monitor_type { + enum monitor_type type; + const char *name; +} supported_types[] = { + { MONITOR_TYPE_OR_PATTERNS, "or_patterns" }, + { }, +}; + +/* Gets SupportedMonitorTypes property */ +static gboolean get_supported_monitor_types(const GDBusPropertyTable *property, + DBusMessageIter *iter, + void *data) +{ + DBusMessageIter entry; + const struct adv_monitor_type *t; + struct btd_adv_monitor_manager *manager = data; + + dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, + DBUS_TYPE_STRING_AS_STRING, + &entry); + + for (t = supported_types; t->name; t++) { + dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, + &t->name); + } + + dbus_message_iter_close_container(iter, &entry); + + return TRUE; +} + +const struct adv_monitor_feature { + uint32_t mask; + const char *name; +} supported_features[] = { + { MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS, "controller-patterns" }, + { } +}; + +/* Gets SupportedFeatures property */ +static gboolean get_supported_features(const GDBusPropertyTable *property, + DBusMessageIter *iter, + void *data) +{ + DBusMessageIter entry; + const struct adv_monitor_feature *f; + struct btd_adv_monitor_manager *manager = data; + + dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, + DBUS_TYPE_STRING_AS_STRING, + &entry); + + for (f = supported_features; f->name; f++) { + if (manager->supported_features & f->mask) { + dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, + &f->name); + } + } + + dbus_message_iter_close_container(iter, &entry); + + return TRUE; +} + static const GDBusPropertyTable adv_monitor_properties[] = { - {"SupportedMonitorTypes", "as", NULL, NULL, NULL, + {"SupportedMonitorTypes", "as", get_supported_monitor_types, NULL, NULL, G_DBUS_PROPERTY_FLAG_EXPERIMENTAL}, - {"SupportedFeatures", "as", NULL, NULL, NULL}, + {"SupportedFeatures", "as", get_supported_features, NULL, NULL}, { } }; @@ -106,6 +183,42 @@ static void manager_destroy(struct btd_adv_monitor_manager *manager) manager_free(manager); } +/* Initiates manager's members based on the return of + * MGMT_OP_READ_ADV_MONITOR_FEATURES + */ +static void read_adv_monitor_features_cb(uint8_t status, uint16_t length, + const void *param, + void *user_data) +{ + const struct mgmt_rp_read_adv_monitor_features *rp = param; + struct btd_adv_monitor_manager *manager = user_data; + + if (status != MGMT_STATUS_SUCCESS || !param) { + btd_error(manager->adapter_id, "Failed to Read Adv Monitor " + "Features with status 0x%02x", status); + return; + } + + if (length < sizeof(*rp)) { + btd_error(manager->adapter_id, + "Wrong size of Read Adv Monitor Features " + "response"); + return; + } + + manager->supported_features = le32_to_cpu(rp->supported_features); + manager->enabled_features = le32_to_cpu(rp->enabled_features); + manager->max_num_monitors = le16_to_cpu(rp->max_num_handles); + manager->max_num_patterns = rp->max_num_patterns; + + btd_info(manager->adapter_id, "Adv Monitor Manager created with " + "supported features:0x%08x, enabled features:0x%08x, " + "max number of supported monitors:%d, " + "max number of supported patterns:%d", + manager->supported_features, manager->enabled_features, + manager->max_num_monitors, manager->max_num_patterns); +} + /* Creates a manager and registers its D-Bus interface */ struct btd_adv_monitor_manager *btd_adv_monitor_manager_create( struct btd_adapter *adapter, @@ -129,9 +242,14 @@ struct btd_adv_monitor_manager *btd_adv_monitor_manager_create( return NULL; } - btd_info(manager->adapter_id, - "Adv Monitor Manager created for adapter %s", - adapter_get_path(manager->adapter)); + if (!mgmt_send(manager->mgmt, MGMT_OP_READ_ADV_MONITOR_FEATURES, + manager->adapter_id, 0, NULL, + read_adv_monitor_features_cb, manager, NULL)) { + btd_error(manager->adapter_id, + "Failed to send Read Adv Monitor Features"); + manager_destroy(manager); + return NULL; + } return manager; } From patchwork Thu Sep 10 04:41:30 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: 252783 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=-9.8 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 C5785C2BB84 for ; Thu, 10 Sep 2020 04:42:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6AAAE207DE for ; Thu, 10 Sep 2020 04:42:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="c/G21twh" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726079AbgIJEmr (ORCPT ); Thu, 10 Sep 2020 00:42:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725873AbgIJEmq (ORCPT ); Thu, 10 Sep 2020 00:42:46 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27D63C061573 for ; Wed, 9 Sep 2020 21:42:46 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d16so289621pll.13 for ; Wed, 09 Sep 2020 21:42:46 -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=wgoIVPRWXVNntcbMbldt7TpPEHI4+Eh2hW+gjZOt2Js=; b=c/G21twhE3vqjwC7Mil15+hpuCfBHlF/bSWjwhf0tDlfBOKffQDVIoXoBeevLzZnOB ezo7Zc20aIX4bC24CtGa9p2GbHQnt407Hc/cZQTw+dv7CsoSF9A5kssGwAIbO116PsXk DmoP3DWMigoYyzebiew3auQpzlyxYctvkaOHo= 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=wgoIVPRWXVNntcbMbldt7TpPEHI4+Eh2hW+gjZOt2Js=; b=lgDaBAmVwtzVAmiJOmdrj4bg4kUefRkgB1xgP0cF+bTE4YdF7ltixboUzlZ0NzBo5G +wljfrpk/6P2J35/+20QLoY1U1piIVryV8wYSiPPw75Vb6uLiMWnM0jz2PL99fkMGQvY LZYXcR7mzzt7E1J32JTzhW7Oi3gqlS0BpcnGd4ltJW+zZJ0fgM+2+GHsQOujY6bhUThE +ZBMdX/KEYxQMF0Y8SEs+GTQxdjKfe35RzU7EM0vHqkEhI3+2lhIWqtmU5tcUNB9FtK/ msuBrnRqI8GU1lyYaIN/qBweDBg+9aNgNuQPYU1nheCcqwLFsDVhPITiPRj1Ay3aF2U4 QU3w== X-Gm-Message-State: AOAM531DHw/PT/MNtbaMGcvDUe5LdmEhNfFUIA7/6c2/HkJDWQKIYNK6 sCh1q3Y7uJN2ulYU1u8RrZGJKoXB8lO0RA== X-Google-Smtp-Source: ABdhPJzZgcg7AuKV+NvveWUxz++oRUMA3zuyB+nk06Gc/+jY/MqJnDYgK4e9DfVJM/4bxbSX9rmBsA== X-Received: by 2002:a17:902:8d8f:b029:d0:cc02:8527 with SMTP id v15-20020a1709028d8fb02900d0cc028527mr3881909plo.33.1599712965143; Wed, 09 Sep 2020 21:42:45 -0700 (PDT) Received: from mcchou0.mtv.corp.google.com ([2620:15c:202:201:de4a:3eff:fe75:1314]) by smtp.gmail.com with ESMTPSA id k4sm644888pjl.10.2020.09.09.21.42.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Sep 2020 21:42:44 -0700 (PDT) From: Miao-chen Chou To: Bluetooth Kernel Mailing List Cc: Manish Mandlik , Luiz Augusto von Dentz , Howard Chung , Alain Michaud , Miao-chen Chou , Abhishek Pandit-Subedi Subject: [BlueZ PATCH v3 4/7] adv_monitor: Implement UnregisterMonitor() Date: Wed, 9 Sep 2020 21:41:30 -0700 Message-Id: <20200909214058.BlueZ.v3.4.I335e42ab9a238261c2492c308ce77c959f631483@changeid> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200909214058.BlueZ.v3.1.I205718871f4e636958904f3cfb171cfd381c54b1@changeid> References: <20200909214058.BlueZ.v3.1.I205718871f4e636958904f3cfb171cfd381c54b1@changeid> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This implements the UnregisterMonitor() method handler of ADV monitor manager interface. The following tests were performed. - Issue a UnregisterMonitor() call with a nonexistent path and expect org.bluez.Error.DoesNotExist as the return. - Issue a UnregisterMonitor() call with a invalid path and expect org.bluez.Error.InvalidArguments as the return. - Issue RegisterMonitor() with a path, issue UnregisterMonitor() and expect a successful method call return. Reviewed-by: Yun-Hao Chung Reviewed-by: Manish Mandlik Reviewed-by: Abhishek Pandit-Subedi --- (no changes since v1) src/adv_monitor.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/adv_monitor.c b/src/adv_monitor.c index b8b55d2fc..1a38e6994 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -213,13 +213,48 @@ static DBusMessage *register_monitor(DBusConnection *conn, DBusMessage *msg, return dbus_message_new_method_return(msg); } +/* Handles UnregisterMonitor D-Bus call */ +static DBusMessage *unregister_monitor(DBusConnection *conn, + DBusMessage *msg, void *user_data) +{ + DBusMessageIter args; + struct app_match_data match; + struct adv_monitor_app *app; + struct btd_adv_monitor_manager *manager = user_data; + + if (!dbus_message_iter_init(msg, &args)) + return btd_error_invalid_args(msg); + + if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) + return btd_error_invalid_args(msg); + + dbus_message_iter_get_basic(&args, &match.path); + + if (!strlen(match.path) || !g_str_has_prefix(match.path, "/")) + return btd_error_invalid_args(msg); + + match.owner = dbus_message_get_sender(msg); + + app = queue_find(manager->apps, app_match, &match); + if (!app) + return btd_error_does_not_exist(msg); + + queue_remove(manager->apps, app); + app_destroy(app); + + btd_info(manager->adapter_id, "Path %s removed along with Adv Monitor " + "app %s", match.path, match.owner); + + return dbus_message_new_method_return(msg); +} + static const GDBusMethodTable adv_monitor_methods[] = { { GDBUS_EXPERIMENTAL_METHOD("RegisterMonitor", GDBUS_ARGS({ "application", "o" }), NULL, register_monitor) }, { GDBUS_EXPERIMENTAL_ASYNC_METHOD("UnregisterMonitor", GDBUS_ARGS({ "application", "o" }), - NULL, NULL) }, + NULL, unregister_monitor) }, { } }; From patchwork Thu Sep 10 04:41:34 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: 252782 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=-9.8 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 3B74BC43461 for ; Thu, 10 Sep 2020 04:43:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CF83020809 for ; Thu, 10 Sep 2020 04:43:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="jwnMghW7" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725971AbgIJEnU (ORCPT ); Thu, 10 Sep 2020 00:43:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725873AbgIJEnR (ORCPT ); Thu, 10 Sep 2020 00:43:17 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0848C061573 for ; Wed, 9 Sep 2020 21:43:17 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id x18so302824pll.6 for ; Wed, 09 Sep 2020 21:43:17 -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=Or/eiyaOvU3jAqUaMpITQ6DaEt5eU+RuO6myI++hZZ8=; b=jwnMghW77kRC3wX3IefUXdob3lnKT7a07fI4Tn1MnqFs1SxNkIQX/eAU3d7aQPhpLj SEDds5J/M71k9PZ2n8jRQ1SdyMgzYLEXCFwykZ5FN9jjHtMB6Eawh4T9a+dnZ5RR/095 4aANjdBT2ywXT0UnKYAhjN/Hs2FuCXC+bfmhw= 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=Or/eiyaOvU3jAqUaMpITQ6DaEt5eU+RuO6myI++hZZ8=; b=E8d4EPjqJ0IURuwJHZ2RTp5RZWjpXq2pPxbOANuuFRGcAwKkISu5Lc9Le/D4efUUD+ 9xzl7hxlsDKzjVYrvgGpXenlNaxg+SDA64ifDJNNYnabbAw6cb3RolnUob1vBKkCexLc 9+heWN3jk6dJshFW7lT9+Rnpjwh9JWn+o+m8r0LygKr0p03gOl02q8npKnUD9iSE7F/+ XQp3n84m5u6P8uo+ud9Jsb4MDwd1zD0CfAG7VVTUN07/fhCVvMrcXW0/r1L0ok5aY+AH efO+T18CIz5Qk/zJda6VNZV+xkV63wpFcQu+Y/HSTCa5PnZDjETtT50CPRRVX843Ds7K KcCA== X-Gm-Message-State: AOAM530NqfUPIrSxh7Qf+ugbHjGyxD2mKGjAmlER9hvNhUBo3jvcpU2v RJdIxrhmE1cuBnOkFOjqcHgritlVOh5lwQ== X-Google-Smtp-Source: ABdhPJwEiOpEuSxlCcf3EjgBraGfOXZa/Jk3Anx1oeWC7FIg3WskHPoF+2U6sV1mmwIfUMENSy895A== X-Received: by 2002:a17:902:b686:: with SMTP id c6mr3795807pls.154.1599712995839; Wed, 09 Sep 2020 21:43:15 -0700 (PDT) Received: from mcchou0.mtv.corp.google.com ([2620:15c:202:201:de4a:3eff:fe75:1314]) by smtp.gmail.com with ESMTPSA id k4sm644888pjl.10.2020.09.09.21.43.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Sep 2020 21:43:15 -0700 (PDT) From: Miao-chen Chou To: Bluetooth Kernel Mailing List Cc: Manish Mandlik , Luiz Augusto von Dentz , Howard Chung , Alain Michaud , Miao-chen Chou Subject: [BlueZ PATCH v3 6/7] adv_monitor: Handle D-Bus proxy event of an ADV monitor Date: Wed, 9 Sep 2020 21:41:34 -0700 Message-Id: <20200909214058.BlueZ.v3.6.I47744c7eef31f44cf393dea2b3e437d7b41cef79@changeid> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200909214058.BlueZ.v3.1.I205718871f4e636958904f3cfb171cfd381c54b1@changeid> References: <20200909214058.BlueZ.v3.1.I205718871f4e636958904f3cfb171cfd381c54b1@changeid> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This adds two handlers, one for adding and one for removing, of D-Bus proxy events. The handler of property changes is set to NULL as intended, since for simplicity no further changes on monitor's properties would affect the ongoing monitoring. The following test steps were performed with bluetoothctl. - After registering the root path, expose two monitors and verify that the proxy added event are received. - Have two monitors added, unexpose the monitors, and verify that the proxy removed events are received for those two monitors. - Have two monitors added, unregister the monitors and verify that the proxy removed events are received for those two monitors. Reviewed-by: Yun-Hao Chung Reviewed-by: Manish Mandlik --- (no changes since v2) Changes in v2: - Use new0() instead of g_new0() - Remove the reset of monitor members - Use memcpy() instead of g_memmove() src/adv_monitor.c | 488 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 475 insertions(+), 13 deletions(-) diff --git a/src/adv_monitor.c b/src/adv_monitor.c index 23fdc9b14..b07d886bf 100644 --- a/src/adv_monitor.c +++ b/src/adv_monitor.c @@ -37,14 +37,23 @@ #include "dbus-common.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" +#define ADV_MONITOR_INTERFACE "org.bluez.AdvertisementMonitor1" #define ADV_MONITOR_MGR_INTERFACE "org.bluez.AdvertisementMonitorManager1" +#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 */ + struct btd_adv_monitor_manager { struct btd_adapter *adapter; struct mgmt *mgmt; @@ -66,6 +75,43 @@ struct adv_monitor_app { DBusMessage *reg; GDBusClient *client; + + struct queue *monitors; +}; + +enum monitor_type { + MONITOR_TYPE_NONE, + MONITOR_TYPE_OR_PATTERNS, +}; + +enum monitor_state { + MONITOR_STATE_NEW, /* New but not yet init'ed with actual values */ + 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 */ +}; + +struct pattern { + uint8_t ad_type; + uint8_t offset; + uint8_t length; + uint8_t value[BT_AD_MAX_DATA_LEN]; +}; + +struct adv_monitor { + struct adv_monitor_app *app; + GDBusProxy *proxy; + char *path; + + 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 */ + + enum monitor_type type; /* MONITOR_TYPE_* */ + struct queue *patterns; }; struct app_match_data { @@ -73,6 +119,14 @@ struct app_match_data { const char *path; }; +const struct adv_monitor_type { + enum monitor_type type; + const char *name; +} supported_types[] = { + { MONITOR_TYPE_OR_PATTERNS, "or_patterns" }, + { }, +}; + /* Replies to an app's D-Bus message and unref it */ static void app_reply_msg(struct adv_monitor_app *app, DBusMessage *reply) { @@ -84,6 +138,48 @@ static void app_reply_msg(struct adv_monitor_app *app, DBusMessage *reply) app->reg = NULL; } +/* Frees a pattern */ +static void pattern_free(void *data) +{ + struct pattern *pattern = data; + + if (!pattern) + return; + + free(pattern); +} + +/* Frees a monitor object */ +static void monitor_free(void *data) +{ + struct adv_monitor *monitor = data; + + if (!monitor) + return; + + g_dbus_proxy_unref(monitor->proxy); + g_free(monitor->path); + + queue_destroy(monitor->patterns, pattern_free); + + free(monitor); +} + +/* Calls Release() method of the remote Adv Monitor */ +static void monitor_release(void *data, void *user_data) +{ + struct adv_monitor *monitor = data; + + if (!monitor) + return; + + DBG("Calling Release() on Adv Monitor of owner %s at path %s", + monitor->app->owner, monitor->path); + + g_dbus_proxy_method_call(monitor->proxy, "Release", NULL, NULL, NULL, + NULL); +} + /* Destroys an app object along with related D-Bus handlers */ static void app_destroy(void *data) { @@ -94,6 +190,9 @@ static void app_destroy(void *data) DBG("Destroy Adv Monitor app %s at path %s", app->owner, app->path); + queue_foreach(app->monitors, monitor_release, NULL); + queue_destroy(app->monitors, monitor_free); + if (app->reg) { app_reply_msg(app, btd_error_failed(app->reg, "Adv Monitor app destroyed")); @@ -136,6 +235,372 @@ static void app_ready_cb(GDBusClient *client, void *user_data) app_reply_msg(app, dbus_message_new_method_return(app->reg)); } +/* Allocates an Adv Monitor */ +static struct adv_monitor *monitor_new(struct adv_monitor_app *app, + GDBusProxy *proxy) +{ + struct adv_monitor *monitor; + + if (!app || !proxy) + return NULL; + + monitor = new0(struct adv_monitor, 1); + if (!monitor) + return NULL; + + monitor->app = app; + monitor->proxy = g_dbus_proxy_ref(proxy); + monitor->path = g_strdup(g_dbus_proxy_get_path(proxy)); + + monitor->state = MONITOR_STATE_NEW; + + monitor->high_rssi = ADV_MONITOR_UNSET_RSSI; + monitor->high_rssi_timeout = ADV_MONITOR_UNSET_TIMER; + monitor->low_rssi = ADV_MONITOR_UNSET_RSSI; + monitor->low_rssi_timeout = ADV_MONITOR_UNSET_TIMER; + + monitor->type = MONITOR_TYPE_NONE; + monitor->patterns = NULL; + + return monitor; +} + +/* Matches a monitor based on its D-Bus path */ +static bool monitor_match(const void *a, const void *b) +{ + const GDBusProxy *proxy = b; + const struct adv_monitor *monitor = a; + + if (!proxy || !monitor) + return false; + + if (strcmp(g_dbus_proxy_get_path(proxy), monitor->path) != 0) + return false; + + return true; +} + +/* Retrieves Type from the remote Adv Monitor object, verifies the value and + * update the local Adv Monitor + */ +static bool parse_monitor_type(struct adv_monitor *monitor, const char *path) +{ + DBusMessageIter iter; + const struct adv_monitor_type *t; + const char *type_str; + uint16_t adapter_id = monitor->app->manager->adapter_id; + + if (!g_dbus_proxy_get_property(monitor->proxy, "Type", &iter)) { + btd_error(adapter_id, "Failed to retrieve property Type from " + "the Adv Monitor at path %s", path); + return false; + } + + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) + goto failed; + + dbus_message_iter_get_basic(&iter, &type_str); + + for (t = supported_types; t->name; t++) { + if (strcmp(t->name, type_str) == 0) { + monitor->type = t->type; + return true; + } + } + +failed: + btd_error(adapter_id, "Invalid argument of property Type of the Adv " + "Monitor at path %s", path); + + return false; +} + +/* Retrieves RSSIThresholdsAndTimers 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; + 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; + } + + 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 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 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 LowRSSIThresholdTimer */ + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT16) + goto failed; + dbus_message_iter_get_basic(&iter, &l_rssi_timer); + + /* 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. + */ + 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) { + goto done; + } + + 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) { + 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) { + 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; + +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); + + 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 of the Adv Monitor at path %s", + path); + + return false; +} + +/* Retrieves Patterns from the remote Adv Monitor object, verifies the values + * and update the local Adv Monitor + */ +static bool parse_patterns(struct adv_monitor *monitor, const char *path) +{ + DBusMessageIter array, array_iter; + uint16_t num_patterns = 0; + uint16_t adapter_id = monitor->app->manager->adapter_id; + + if (!g_dbus_proxy_get_property(monitor->proxy, "Patterns", &array)) { + btd_error(adapter_id, "Failed to retrieve property Patterns " + "from the Adv Monitor at path %s", path); + return false; + } + + monitor->patterns = queue_new(); + + if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY || + dbus_message_iter_get_element_type(&array) != + DBUS_TYPE_STRUCT) { + goto failed; + } + + dbus_message_iter_recurse(&array, &array_iter); + + while (dbus_message_iter_get_arg_type(&array_iter) == + DBUS_TYPE_STRUCT) { + int value_len; + uint8_t *value; + uint8_t offset, ad_type; + struct pattern *pattern; + DBusMessageIter struct_iter, value_iter; + + dbus_message_iter_recurse(&array_iter, &struct_iter); + + // Extract start position + if (dbus_message_iter_get_arg_type(&struct_iter) != + DBUS_TYPE_BYTE) { + goto failed; + } + dbus_message_iter_get_basic(&struct_iter, &offset); + if (!dbus_message_iter_next(&struct_iter)) + goto failed; + + // Extract AD data type + if (dbus_message_iter_get_arg_type(&struct_iter) != + DBUS_TYPE_BYTE) { + goto failed; + } + dbus_message_iter_get_basic(&struct_iter, &ad_type); + if (!dbus_message_iter_next(&struct_iter)) + goto failed; + + // Extract value of a pattern + if (dbus_message_iter_get_arg_type(&struct_iter) != + DBUS_TYPE_ARRAY) { + goto failed; + } + dbus_message_iter_recurse(&struct_iter, &value_iter); + 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); + 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); + } + + /* There must be at least one pattern. */ + if (queue_isempty(monitor->patterns)) + goto failed; + + return true; + +failed: + queue_destroy(monitor->patterns, pattern_free); + monitor->patterns = NULL; + + btd_error(adapter_id, "Invalid argument of property Patterns of the " + "Adv Monitor at path %s", path); + + return false; +} + +/* Processes the content of the remote Adv Monitor */ +static bool monitor_process(struct adv_monitor *monitor, + struct adv_monitor_app *app) +{ + const char *path = g_dbus_proxy_get_path(monitor->proxy); + + monitor->state = MONITOR_STATE_FAILED; + + if (!parse_monitor_type(monitor, path)) + goto done; + + if (!parse_rssi_and_timeout(monitor, path)) + goto done; + + if (monitor->type == MONITOR_TYPE_OR_PATTERNS && + parse_patterns(monitor, path)) { + monitor->state = MONITOR_STATE_INITED; + } + +done: + return monitor->state != MONITOR_STATE_FAILED; +} + +/* 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; + 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); + + if (strcmp(iface, ADV_MONITOR_INTERFACE) != 0 || + !g_str_has_prefix(path, app->path)) { + return; + } + + if (queue_find(app->monitors, monitor_match, proxy)) { + btd_error(adapter_id, "Adv Monitor proxy already exists with " + "path %s", path); + return; + } + + monitor = monitor_new(app, proxy); + if (!monitor) { + btd_error(adapter_id, "Failed to allocate an Adv Monitor for " + "the object at %s", path); + return; + } + + if (!monitor_process(monitor, app)) { + monitor_release(monitor, NULL); + monitor_free(monitor); + DBG("Adv Monitor at path %s released due to invalid content", + path); + return; + } + + queue_push_tail(app->monitors, monitor); + + DBG("Adv Monitor allocated for the object at path %s", path); +} + +/* 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 adv_monitor_app *app = user_data; + + monitor = queue_remove_if(app->monitors, monitor_match, proxy); + if (monitor) { + DBG("Adv Monitor removed for the object at path %s", + monitor->path); + + /* The object was gone, so we don't need to call Release() */ + monitor_free(monitor); + } +} + /* Creates an app object, initiates it and sets D-Bus event handlers */ static struct adv_monitor_app *app_create(DBusConnection *conn, DBusMessage *msg, const char *sender, @@ -162,8 +627,17 @@ static struct adv_monitor_app *app_create(DBusConnection *conn, return NULL; } + app->monitors = queue_new(); + g_dbus_client_set_disconnect_watch(app->client, app_disconnect_cb, app); - g_dbus_client_set_proxy_handlers(app->client, NULL, NULL, NULL, NULL); + + /* Note that any property changes on a monitor object would not affect + * the content of the corresponding monitor. + */ + g_dbus_client_set_proxy_handlers(app->client, monitor_proxy_added_cb, + monitor_proxy_removed_cb, NULL, + app); + g_dbus_client_set_ready_watch(app->client, app_ready_cb, app); app->reg = dbus_message_ref(msg); @@ -270,18 +744,6 @@ static const GDBusMethodTable adv_monitor_methods[] = { { } }; -enum monitor_type { - MONITOR_TYPE_OR_PATTERNS, -}; - -const struct adv_monitor_type { - enum monitor_type type; - const char *name; -} supported_types[] = { - { MONITOR_TYPE_OR_PATTERNS, "or_patterns" }, - { }, -}; - /* Gets SupportedMonitorTypes property */ static gboolean get_supported_monitor_types(const GDBusPropertyTable *property, DBusMessageIter *iter,