From patchwork Wed Aug 19 04:11:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 11722483 X-Patchwork-Delegate: luiz.dentz@gmail.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D93A6109B for ; Wed, 19 Aug 2020 04:12:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BDDDF2078B for ; Wed, 19 Aug 2020 04:12:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="alXFtLaw" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725306AbgHSEML (ORCPT ); Wed, 19 Aug 2020 00:12:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725280AbgHSEMH (ORCPT ); Wed, 19 Aug 2020 00:12:07 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0D76C061389 for ; Tue, 18 Aug 2020 21:12:06 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id a75so19804434ybg.15 for ; Tue, 18 Aug 2020 21:12:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=AHZJoQheG4gkfi0dWS05sVsMgthHocN8pmTUWABp5co=; b=alXFtLawKWPZrZT1RnNOCtgWpKxEJZ3EElkArGiFPAsXjmNy7euTowqTBk6FcGnbTA Ml22ifn3GU3o3EqoNCmhS9ZvvQxT5khOLWoc82nuaHWxDWo/Vt1z1eEn/Bw09EMrHIqt dVtoIPk7x8/7JFlL3m7vuitsNCr/8rh9QX/8Ly5kEYOLCjtumV5jT5AFhLon8yr2Zpml 7C3oLhb3fZpxBGA5c8FxHrCBaKBQjLSCEw3N5LdNAVLoOgf9cjFscmDrb2qrkH3SgWSy OyguxULEi4oZ+YGwkOjuuU7yNIExujqy0zd+2iePWS349JqkEKmOtaLU0qC5g4ilFdr2 Ya9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=AHZJoQheG4gkfi0dWS05sVsMgthHocN8pmTUWABp5co=; b=mXTlWJLkKTPKt3eLBQDgDTNmxXT13aa+dFsQbEDI4CmqaIBJEc76Xer5ldkf9/Lfb9 lYAS1MB+Ff6BOlaHfrWcI3XiklIf+LVPX0+HkEoj3xcCscHxWuhf0Teq1Xq4nnjHoiiZ CYLX6OwXGixtQesnjjlUYt5kUmAwQzK2hzxJni9xcH5CzMq9o8UJOJXp0AG+l1OVmnX1 wlA5A9tay9iScAnAI4QZ4UF9/HM8e+BfPVbwdlNX1pr/w+oMTxoOBZRoshrgdR/lGiIG tYHTLyYunOGJBDpXw+gdjoE1441e+p9hsuhJ0sN6rr6ChEMBAeyW1+y9yiavH5x2V6an TwCg== X-Gm-Message-State: AOAM531wijNwdVKsZNrHMpDuUaYq8RvL+3Uq+/PXjPrszYM/r79OBOOO oZ4c3quz2KFuoNRQHAT2KQGI9v8oOy5kBk0UvXLSp1gOT4LVb07yrow4nc+5+VvIxyVmafa+Vfp 1eZ/8f8IxsSsFcnBLeEYS8+C1Ov5aIWejo5vwdd+tR3ePR+PC5Rwc7Z0sLnmVLenX8j8tHhQo7a 2SKfgq+1BlXJg= X-Google-Smtp-Source: ABdhPJwSBZjDNOQ62+VrNuhvAhlY5u6+R/jjpXhl42gM514U4HoWQtsT2R0Tmk1tqqd/YSNBIxirS0hoOycUD2FtUw== X-Received: by 2002:a25:6808:: with SMTP id d8mr31859217ybc.265.1597810325945; Tue, 18 Aug 2020 21:12:05 -0700 (PDT) Date: Wed, 19 Aug 2020 12:11:02 +0800 Message-Id: <20200819121048.BlueZ.v1.1.I3a57ea1eb3e3f5b87abc44ea478fc83817627ffc@changeid> Mime-Version: 1.0 X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [BlueZ PATCH v1 1/4] client: Implement basic interface of ADV monitor in bluetoothctl From: Howard Chung To: linux-bluetooth@vger.kernel.org Cc: alainm@chromium.org, mcchou@chromium.org, mmandlik@chromium.org, Howard Chung Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This patch implements some basic functions for ADV monitor in bluetoothctl [bluetooth]# show ... Advertisement Monitor Features: SupportedMonitorTypes: or_patterns Reviewed-by: Miao-chen Chou Reviewed-by: Manish Mandlik --- Makefile.tools | 2 + client/advertisement_monitor.c | 161 +++++++++++++++++++++++++++++++++ client/advertisement_monitor.h | 23 +++++ client/main.c | 28 ++++++ 4 files changed, 214 insertions(+) create mode 100644 client/advertisement_monitor.c create mode 100644 client/advertisement_monitor.h diff --git a/Makefile.tools b/Makefile.tools index 9b9236609..88d9684a7 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -7,6 +7,8 @@ client_bluetoothctl_SOURCES = client/main.c \ client/agent.h client/agent.c \ client/advertising.h \ client/advertising.c \ + client/advertisement_monitor.h \ + client/advertisement_monitor.c \ client/gatt.h client/gatt.c client_bluetoothctl_LDADD = gdbus/libgdbus-internal.la src/libshared-glib.la \ $(GLIB_LIBS) $(DBUS_LIBS) -lreadline diff --git a/client/advertisement_monitor.c b/client/advertisement_monitor.c new file mode 100644 index 000000000..bd2309537 --- /dev/null +++ b/client/advertisement_monitor.c @@ -0,0 +1,161 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2020 Google LLC + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include "gdbus/gdbus.h" +#include "src/shared/util.h" +#include "src/shared/shell.h" +#include "advertisement_monitor.h" + +#define ADV_MONITOR_APP_PATH "/org/bluez/adv_monitor_app" +#define ADV_MONITOR_INTERFACE "org.bluez.AdvertisementMonitor1" + +struct adv_monitor_manager { + GSList *supported_types; + GSList *supported_features; + GDBusProxy *proxy; + gboolean app_registered; +} manager = { NULL, NULL, NULL, FALSE }; + +static void set_supported_list(GSList **list, DBusMessageIter *iter) +{ + char *str; + DBusMessageIter subiter; + + dbus_message_iter_recurse(iter, &subiter); + while (dbus_message_iter_get_arg_type(&subiter) == + DBUS_TYPE_STRING) { + dbus_message_iter_get_basic(&subiter, &str); + *list = g_slist_append(*list, str); + dbus_message_iter_next(&subiter); + } +} + +void adv_monitor_add_manager(DBusConnection *conn, GDBusProxy *proxy) +{ + DBusMessageIter iter; + + if (manager.proxy != NULL || manager.supported_types != NULL || + manager.supported_features != NULL) { + bt_shell_printf("advertisement monitor manager already " + "added\n"); + return; + } + + manager.proxy = proxy; + + if (g_dbus_proxy_get_property(proxy, "SupportedMonitorTypes", &iter)) + set_supported_list(&(manager.supported_types), &iter); + + if (g_dbus_proxy_get_property(proxy, "SupportedFeatures", &iter)) + set_supported_list(&(manager.supported_features), &iter); + +} + +void adv_monitor_remove_manager(DBusConnection *conn) +{ + if (manager.supported_types != NULL) + g_slist_free(g_steal_pointer(&(manager.supported_types))); + if (manager.supported_features != NULL) + g_slist_free(g_steal_pointer(&(manager.supported_features))); + manager.proxy = NULL; + manager.app_registered = FALSE; +} + +static void register_setup(DBusMessageIter *iter, void *user_data) +{ + const char *path = ADV_MONITOR_APP_PATH; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path); +} + +static void register_reply(DBusMessage *message, void *user_data) +{ + DBusConnection *conn = user_data; + DBusError error; + + dbus_error_init(&error); + + if (dbus_set_error_from_message(&error, message) == FALSE) { + bt_shell_printf("AdvertisementMonitor path registered\n"); + } else { + bt_shell_printf("Failed to register path: %s\n", error.name); + dbus_error_free(&error); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } +} + +static void unregister_setup(DBusMessageIter *iter, void *user_data) +{ + const char *path = ADV_MONITOR_APP_PATH; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path); +} + +static void unregister_reply(DBusMessage *message, void *user_data) +{ + DBusConnection *conn = user_data; + DBusError error; + + dbus_error_init(&error); + + if (dbus_set_error_from_message(&error, message) == FALSE) { + bt_shell_printf("AdvertisementMonitor path unregistered\n"); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); + } + + bt_shell_printf("Failed to unregister Advertisement Monitor:" + " %s\n", error.name); + dbus_error_free(&error); + return bt_shell_noninteractive_quit(EXIT_FAILURE); +} + +void adv_monitor_register_app(DBusConnection *conn) +{ + if (manager.supported_types == NULL || manager.app_registered == TRUE || + g_dbus_proxy_method_call(manager.proxy, "RegisterMonitor", + register_setup, register_reply, + NULL, NULL) == FALSE) { + bt_shell_printf("Failed to register Advertisement Monitor\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + manager.app_registered = TRUE; +} + +void adv_monitor_unregister_app(DBusConnection *conn) +{ + if (manager.app_registered == FALSE || + g_dbus_proxy_method_call(manager.proxy, "UnregisterMonitor", + unregister_setup, unregister_reply, + NULL, NULL) == FALSE) { + bt_shell_printf("Failed to unregister Advertisement Monitor\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + manager.app_registered = FALSE; +} diff --git a/client/advertisement_monitor.h b/client/advertisement_monitor.h new file mode 100644 index 000000000..77b0b62c6 --- /dev/null +++ b/client/advertisement_monitor.h @@ -0,0 +1,23 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2020 Google LLC + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +void adv_monitor_add_manager(DBusConnection *conn, GDBusProxy *proxy); +void adv_monitor_remove_manager(DBusConnection *conn); +void adv_monitor_register_app(DBusConnection *conn); +void adv_monitor_unregister_app(DBusConnection *conn); diff --git a/client/main.c b/client/main.c index 9abada69f..7ddd13aa0 100644 --- a/client/main.c +++ b/client/main.c @@ -41,6 +41,7 @@ #include "agent.h" #include "gatt.h" #include "advertising.h" +#include "advertisement_monitor.h" /* String display constants */ #define COLORED_NEW COLOR_GREEN "NEW" COLOR_OFF @@ -58,6 +59,7 @@ static char *auto_register_agent = NULL; struct adapter { GDBusProxy *proxy; GDBusProxy *ad_proxy; + GDBusProxy *adv_monitor_proxy; GList *devices; }; @@ -528,6 +530,19 @@ static void ad_manager_added(GDBusProxy *proxy) adapter->ad_proxy = proxy; } +static void admon_manager_added(GDBusProxy *proxy) +{ + struct adapter *adapter; + + adapter = find_ctrl(ctrl_list, g_dbus_proxy_get_path(proxy)); + if (!adapter) + adapter = adapter_new(proxy); + + adapter->adv_monitor_proxy = proxy; + adv_monitor_add_manager(dbus_conn, proxy); + adv_monitor_register_app(dbus_conn); +} + static void proxy_added(GDBusProxy *proxy, void *user_data) { const char *interface; @@ -560,6 +575,9 @@ static void proxy_added(GDBusProxy *proxy, void *user_data) ad_manager_added(proxy); } else if (!strcmp(interface, "org.bluez.Battery1")) { battery_added(proxy); + } else if (!strcmp(interface, + "org.bluez.AdvertisementMonitorManager1")) { + admon_manager_added(proxy); } } @@ -653,6 +671,9 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data) ad_unregister(dbus_conn, NULL); } else if (!strcmp(interface, "org.bluez.Battery1")) { battery_removed(proxy); + } else if (!strcmp(interface, + "org.bluez.AdvertisementMonitorManager1")) { + adv_monitor_remove_manager(dbus_conn); } } @@ -935,6 +956,13 @@ static void cmd_show(int argc, char *argv[]) print_property(adapter->ad_proxy, "SupportedSecondaryChannels"); } + if (adapter->adv_monitor_proxy) { + bt_shell_printf("Advertisement Monitor Features:\n"); + print_property(adapter->adv_monitor_proxy, + "SupportedMonitorTypes"); + print_property(adapter->adv_monitor_proxy, "SupportedFeatures"); + } + return bt_shell_noninteractive_quit(EXIT_SUCCESS); } From patchwork Wed Aug 19 04:11:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 11722485 X-Patchwork-Delegate: luiz.dentz@gmail.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8864A109B for ; Wed, 19 Aug 2020 04:12:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 60E2520738 for ; Wed, 19 Aug 2020 04:12:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GnsKx0Bn" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725601AbgHSEMV (ORCPT ); Wed, 19 Aug 2020 00:12:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725280AbgHSEMU (ORCPT ); Wed, 19 Aug 2020 00:12:20 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7F02C061389 for ; Tue, 18 Aug 2020 21:12:19 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id a75so19804897ybg.15 for ; Tue, 18 Aug 2020 21:12:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=5S6/jfiMYy3835LYfWcy17MtiwNYRMUM+ig/mShYLAg=; b=GnsKx0Bn6nbI+MtoXbTYGaKT3E2MNTjxoDVlEs8Wq9Cyapk8+K6srjkI66ESJEQeGa lmvQwcR5s5PICkaO/dWKH0FJoPgnUYHNVnM+vr7Vb32sKKkN9wC765010sx3VODCACwC TF7/X/dSoCtlZRPMSuJv8BxN2+mCVhgSm8i0ZwwrTis9m+j7P0khBFAKF8bfxVk9DZcI 6/HuvsyVK01fnTgqr6cpqW2jhRxckODRwQVSdf0X8GnuZRAvUM+XO6ifN+QEeaQ8socK 7gjPwqzJf14/tdV5GWHz0sJWLdmSChzqiq8dPozyaauOtNIVzriLH2Oh73iyq5H3oBYO BrgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=5S6/jfiMYy3835LYfWcy17MtiwNYRMUM+ig/mShYLAg=; b=iwSA+zNJPBflghVM+Pd9OJ42O8zh1ZTqmdf7qKxN7/DKAr/5J5pHBzrJLjEtAx+36c Lp9d53+GsfreFMQZw/9KrKj2LKt1/cCASlA5qQ2ZVR0o4R7N/zkaXAvfP5szd19e4xIJ 9OOmh3acpuvW2RUFBfio3camw1x8knz40c03lD7jeddtjad2CIDuxnUcOp+Ddwbj2xVO d7XnxMv3ovsoL+P2LELA29B/w7wm/VXXRGELBItAeC1VKBX6vosx1Rc4jiGewWZMNyq9 1l22Gv7aWcC+kowJx2rorlSeNiv6h3osHgJuZkOiIc0x/gpc/R9mp4HQeXzOylghQGO5 fjtw== X-Gm-Message-State: AOAM530JywyrgpMbVkbimMwS2uNpP/rofcZVTC9SO5QnKJalhJ3hsvYv tHZUhUO0S/C/oRtnnw2gH0oTzsbNduFBCbNyLMgWGSsWebWypi0EzK5lDEHQd2Z+ImJtBEOPe5W eGytNPAuJI+WombvELochQ5MzZE1TehsbCmscxsT4zmR+Ewaimb2WgL2xKYnODgjp8TN3bs0CjW uVesU/Ob9F4ME= X-Google-Smtp-Source: ABdhPJwklEsXoWq+qBgeA+3VNxyKYft2BlyFe/UFcPuwOTEMP8R9Hp4/AjcUd+rYWUOK+qhPl6nLNId4V+a3/HA4vA== X-Received: by 2002:a25:2314:: with SMTP id j20mr31103344ybj.508.1597810339098; Tue, 18 Aug 2020 21:12:19 -0700 (PDT) Date: Wed, 19 Aug 2020 12:11:03 +0800 In-Reply-To: <20200819121048.BlueZ.v1.1.I3a57ea1eb3e3f5b87abc44ea478fc83817627ffc@changeid> Message-Id: <20200819121048.BlueZ.v1.2.I045d6f668c141c6b834ba6b31fc81618c0a7b8e8@changeid> Mime-Version: 1.0 References: <20200819121048.BlueZ.v1.1.I3a57ea1eb3e3f5b87abc44ea478fc83817627ffc@changeid> X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [BlueZ PATCH v1 2/4] client: Implement more interfaces of ADV monitor in bluetoothctl From: Howard Chung To: linux-bluetooth@vger.kernel.org Cc: alainm@chromium.org, mcchou@chromium.org, mmandlik@chromium.org, Howard Chung Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This patch creates a submenu in bluetoothctl and implements several commands. new commands: [bluetooth]# menu advmon [bluetooth]# add-pattern-monitor or_patterns -10,10,-30,5 1,2,ab0011 Advertisement Monitor 0 added [bluetooth]# get-pattern-monitor all Advertisement Monitor 0 path: /org/bluez/adv_monitor_app/0 type: or_patterns rssi: high threshold: -10 high threshold timer: 10 low threshold: -30 low threshold timer: 5 pattern 1: start position: 1 AD data type: 2 content: ab0011 [bluetooth]# get-supported-info Supported Features: Supported Moniter Types: or_patterns [bluetooth]# remove-pattern-monitor 0 Monitor 0 deleted Signed-off-by: Howard Chung --- client/advertisement_monitor.c | 328 ++++++++++++++++++++++++++++++++- client/advertisement_monitor.h | 4 + client/main.c | 70 +++++++ 3 files changed, 399 insertions(+), 3 deletions(-) diff --git a/client/advertisement_monitor.c b/client/advertisement_monitor.c index bd2309537..ec8f23711 100644 --- a/client/advertisement_monitor.c +++ b/client/advertisement_monitor.c @@ -29,6 +29,7 @@ #include #include "gdbus/gdbus.h" +#include "src/shared/ad.h" #include "src/shared/util.h" #include "src/shared/shell.h" #include "advertisement_monitor.h" @@ -36,6 +37,27 @@ #define ADV_MONITOR_APP_PATH "/org/bluez/adv_monitor_app" #define ADV_MONITOR_INTERFACE "org.bluez.AdvertisementMonitor1" +struct rssi_setting { + int16_t high_threshold; + uint16_t high_timer; + int16_t low_threshold; + uint16_t low_timer; +}; + +struct pattern { + uint8_t start_pos; + uint8_t ad_data_type; + uint8_t content_len; + uint8_t content[BT_AD_MAX_DATA_LEN]; +}; + +struct adv_monitor { + uint8_t idx; + char *type; + struct rssi_setting *rssi; + GSList *patterns; +}; + struct adv_monitor_manager { GSList *supported_types; GSList *supported_features; @@ -43,6 +65,9 @@ struct adv_monitor_manager { gboolean app_registered; } manager = { NULL, NULL, NULL, FALSE }; +static uint8_t adv_mon_idx; +static GSList *adv_mons; + static void set_supported_list(GSList **list, DBusMessageIter *iter) { char *str; @@ -138,7 +163,10 @@ static void unregister_reply(DBusMessage *message, void *user_data) void adv_monitor_register_app(DBusConnection *conn) { - if (manager.supported_types == NULL || manager.app_registered == TRUE || + if (manager.app_registered == TRUE) { + bt_shell_printf("Advertisement Monitor already registered\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } else if (manager.supported_types == NULL || g_dbus_proxy_method_call(manager.proxy, "RegisterMonitor", register_setup, register_reply, NULL, NULL) == FALSE) { @@ -150,8 +178,10 @@ void adv_monitor_register_app(DBusConnection *conn) void adv_monitor_unregister_app(DBusConnection *conn) { - if (manager.app_registered == FALSE || - g_dbus_proxy_method_call(manager.proxy, "UnregisterMonitor", + if (manager.app_registered == FALSE) { + bt_shell_printf("Advertisement Monitor not registered\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } else if (g_dbus_proxy_method_call(manager.proxy, "UnregisterMonitor", unregister_setup, unregister_reply, NULL, NULL) == FALSE) { bt_shell_printf("Failed to unregister Advertisement Monitor\n"); @@ -159,3 +189,295 @@ void adv_monitor_unregister_app(DBusConnection *conn) } manager.app_registered = FALSE; } + +static void free_pattern(void *user_data) +{ + struct pattern *p = user_data; + + g_free(p); +} + +static void free_adv_monitor(void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + + g_free(adv_monitor->type); + g_free(adv_monitor->rssi); + g_slist_free_full(adv_monitor->patterns, free_pattern); + g_free(adv_monitor); +} + +static uint8_t str2bytearray(char *str, uint8_t *arr) +{ + int idx, len = strlen(str), arr_len = 0; + + if (len%2 != 0) + return 0; + + for (idx = 0; idx < len; idx += 2) { + if (sscanf(str+idx, "%2hhx", &arr[arr_len++]) < 1) + return 0; + } + return arr_len; +} + +static struct rssi_setting *parse_rssi(char *rssi_str) +{ + struct rssi_setting *rssi; + int16_t high_threshold, low_threshold; + uint16_t high_timer, low_timer; + + if (sscanf(rssi_str, "%hd,%hu,%hd,%hu", &high_threshold, &high_timer, + &low_threshold, &low_timer) < 4) + return NULL; + + rssi = g_malloc0(sizeof(struct rssi_setting)); + + if (!rssi) { + bt_shell_printf("Failed to allocate rssi_setting"); + bt_shell_noninteractive_quit(EXIT_FAILURE); + return NULL; + } + + rssi->high_threshold = high_threshold; + rssi->high_timer = high_timer; + rssi->low_threshold = low_threshold; + rssi->low_timer = low_timer; + + return rssi; +} + +static struct pattern *parse_pattern(char *pattern) +{ + uint8_t start_pos, ad_data_type; + char content_str[BT_AD_MAX_DATA_LEN]; + struct pattern *pat; + + if (sscanf(pattern, "%hhu,%hhu,%s", &start_pos, &ad_data_type, + content_str) < 3) + return NULL; + + pat = g_malloc0(sizeof(struct pattern)); + + if (!pat) { + bt_shell_printf("Failed to allocate pattern"); + bt_shell_noninteractive_quit(EXIT_FAILURE); + return NULL; + } + + pat->start_pos = start_pos; + pat->ad_data_type = ad_data_type; + pat->content_len = str2bytearray(content_str, pat->content); + if (pat->content_len == 0) { + free_pattern(pat); + return NULL; + } + + return pat; +} + +static GSList *parse_patterns(char *pattern_list[], int num) +{ + GSList *patterns = NULL; + int cnt; + + if (num == 0) { + bt_shell_printf("No pattern provided\n"); + return NULL; + } + + for (cnt = 0; cnt < num; cnt++) { + struct pattern *pattern; + + pattern = parse_pattern(pattern_list[cnt]); + if (pattern == NULL) { + g_slist_free_full(patterns, free_pattern); + return NULL; + } + patterns = g_slist_append(patterns, pattern); + } + + return patterns; +} + +static gint cmp_adv_monitor_with_idx(gconstpointer a, gconstpointer b) +{ + const struct adv_monitor *adv_monitor = a; + uint8_t idx = *(uint8_t *)b; + + return adv_monitor->idx != idx; +} + +static struct adv_monitor *find_adv_monitor_with_idx(uint8_t monitor_idx) +{ + GSList *list; + + list = g_slist_find_custom(adv_mons, &monitor_idx, + cmp_adv_monitor_with_idx); + + if (list) + return (struct adv_monitor *)list->data; + return NULL; +} + +static void print_bytearray(char *prefix, uint8_t *arr, uint8_t len) +{ + int idx; + + bt_shell_printf("%s", prefix); + for (idx = 0; idx < len; idx++) + bt_shell_printf("%02hhx", arr[idx]); + bt_shell_printf("\n"); +} + +static void print_adv_monitor(struct adv_monitor *adv_monitor) +{ + GSList *l; + + bt_shell_printf("Advertisement Monitor %d\n", adv_monitor->idx); + bt_shell_printf("\ttype: %s\n", adv_monitor->type); + if (adv_monitor->rssi) { + bt_shell_printf("\trssi:\n"); + bt_shell_printf("\t\thigh threshold: %hd\n", + adv_monitor->rssi->high_threshold); + bt_shell_printf("\t\thigh threshold timer: %hu\n", + adv_monitor->rssi->high_timer); + bt_shell_printf("\t\tlow threshold: %hd\n", + adv_monitor->rssi->low_threshold); + bt_shell_printf("\t\tlow threshold timer: %hu\n", + adv_monitor->rssi->low_timer); + } + + if (adv_monitor->patterns) { + int idx = 1; + + for (l = adv_monitor->patterns; l; l = g_slist_next(l), idx++) { + struct pattern *pattern = l->data; + + bt_shell_printf("\tpattern %d:\n", idx); + bt_shell_printf("\t\tstart position: %hhu\n", + pattern->start_pos); + bt_shell_printf("\t\tAD data type: %hhu\n", + pattern->ad_data_type); + print_bytearray("\t\tcontent: ", pattern->content, + pattern->content_len); + } + } +} + +void adv_monitor_add_monitor(DBusConnection *conn, int argc, char *argv[]) +{ + struct adv_monitor *adv_monitor; + struct rssi_setting *rssi; + GSList *patterns = NULL; + char *type; + + if (g_slist_length(adv_mons) >= UINT8_MAX) { + bt_shell_printf("Number of advertisement monitor exceeds " + "the limit"); + return; + } + + while (find_adv_monitor_with_idx(adv_mon_idx)) + adv_mon_idx += 1; + + type = argv[1]; + + if (strcmp(argv[2], "-") == 0) + rssi = NULL; + else { + rssi = parse_rssi(argv[2]); + if (rssi == NULL) { + bt_shell_printf("RSSIThresholdAndTimers malformed\n"); + return; + } + } + + patterns = parse_patterns(argv+3, argc-3); + if (patterns == NULL) { + bt_shell_printf("pattern-list malformed\n"); + return; + } + + adv_monitor = g_malloc0(sizeof(struct adv_monitor)); + + if (!adv_monitor) { + bt_shell_printf("Failed to allocate adv_monitor"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + adv_monitor->idx = adv_mon_idx; + adv_monitor->type = g_strdup(type); + adv_monitor->rssi = rssi; + adv_monitor->patterns = patterns; + + adv_mons = g_slist_append(adv_mons, adv_monitor); + bt_shell_printf("Advertisement Monitor %d added\n", adv_monitor->idx); +} + +void adv_monitor_print_monitor(DBusConnection *conn, int monitor_idx) +{ + struct adv_monitor *adv_monitor; + GSList *l; + + if (monitor_idx < 0) { + for (l = adv_mons; l; l = g_slist_next(l)) { + adv_monitor = l->data; + print_adv_monitor(adv_monitor); + } + return; + } + + adv_monitor = find_adv_monitor_with_idx(monitor_idx); + + if (adv_monitor == NULL) { + bt_shell_printf("Can't find monitor with index %d\n", + monitor_idx); + return; + } + + print_adv_monitor(adv_monitor); +} + +void adv_monitor_remove_monitor(DBusConnection *conn, int monitor_idx) +{ + struct adv_monitor *adv_monitor; + + if (monitor_idx < 0) { + g_slist_free_full(g_steal_pointer(&adv_mons), free_adv_monitor); + return; + } + + adv_monitor = find_adv_monitor_with_idx(monitor_idx); + if (adv_monitor == NULL) { + bt_shell_printf("Can't find monitor with index %d\n", + monitor_idx); + return; + } + + adv_mons = g_slist_remove(adv_mons, adv_monitor); + free_adv_monitor(adv_monitor); + bt_shell_printf("Monitor %d deleted\n", monitor_idx); +} + +static void print_supported_list(GSList *list) +{ + GSList *iter; + + for (iter = list; iter; iter = g_slist_next(iter)) { + char *data = iter->data; + + printf(" %s", data); + } +} + +void adv_monitor_get_supported_info(void) +{ + bt_shell_printf("Supported Features:"); + print_supported_list(manager.supported_features); + bt_shell_printf("\n"); + + bt_shell_printf("Supported Moniter Types:"); + print_supported_list(manager.supported_types); + bt_shell_printf("\n"); +} diff --git a/client/advertisement_monitor.h b/client/advertisement_monitor.h index 77b0b62c6..f2a0caf77 100644 --- a/client/advertisement_monitor.h +++ b/client/advertisement_monitor.h @@ -21,3 +21,7 @@ void adv_monitor_add_manager(DBusConnection *conn, GDBusProxy *proxy); void adv_monitor_remove_manager(DBusConnection *conn); void adv_monitor_register_app(DBusConnection *conn); void adv_monitor_unregister_app(DBusConnection *conn); +void adv_monitor_add_monitor(DBusConnection *conn, int argc, char *argv[]); +void adv_monitor_print_monitor(DBusConnection *conn, int monitor_idx); +void adv_monitor_remove_monitor(DBusConnection *conn, int monitor_idx); +void adv_monitor_get_supported_info(void); diff --git a/client/main.c b/client/main.c index 7ddd13aa0..2b63ee62a 100644 --- a/client/main.c +++ b/client/main.c @@ -2686,6 +2686,53 @@ static void cmd_ad_clear(int argc, char *argv[]) return bt_shell_noninteractive_quit(EXIT_FAILURE); } +static void print_add_monitor_usage(void) +{ + bt_shell_usage(); + bt_shell_printf("RSSIThresholdAndTimers format:\n" + "\t,,,\n" + "\tor single '-' for not using RSSI as filter\n"); + bt_shell_printf("pattern format:\n" + "\t,,\n"); + bt_shell_printf("e.g.\n" + "\tadd-pattern-monitor or_patterns -10,10,-20,20 1,2,01ab55\n"); +} + +static void cmd_adv_monitor_add_monitor(int argc, char *argv[]) +{ + if (argc < 3) + print_add_monitor_usage(); + else + adv_monitor_add_monitor(dbus_conn, argc, argv); +} + +static void cmd_adv_monitor_print_monitor(int argc, char *argv[]) +{ + int monitor_idx; + + if (strcmp(argv[1], "all") == 0) + monitor_idx = -1; + else + monitor_idx = atoi(argv[1]); + adv_monitor_print_monitor(dbus_conn, monitor_idx); +} + +static void cmd_adv_monitor_remove_monitor(int argc, char *argv[]) +{ + int monitor_idx; + + if (strcmp(argv[1], "all") == 0) + monitor_idx = -1; + else + monitor_idx = atoi(argv[1]); + adv_monitor_remove_monitor(dbus_conn, monitor_idx); +} + +static void cmd_adv_monitor_get_supported_info(int argc, char *argv[]) +{ + adv_monitor_get_supported_info(); +} + static const struct bt_shell_menu advertise_menu = { .name = "advertise", .desc = "Advertise Options Submenu", @@ -2722,6 +2769,28 @@ static const struct bt_shell_menu advertise_menu = { { } }, }; +static const struct bt_shell_menu advertise_monitor_menu = { + .name = "advmon", + .desc = "Advertisement Monitor Options Submenu", + .entries = { + { "add-pattern-monitor", " " + "[RSSIThresholdAndTimers] " + "[patterns=pattern1 pattern2 ...]", + cmd_adv_monitor_add_monitor, + "Add pattern monitor" }, + { "get-pattern-monitor", "", + cmd_adv_monitor_print_monitor, + "Get advertisement monitor" }, + { "remove-pattern-monitor", "", + cmd_adv_monitor_remove_monitor, + "Remove advertisement monitor" }, + { "get-supported-info", NULL, + cmd_adv_monitor_get_supported_info, + "Get advertisement manager supported " + "features and supported monitor types" }, + { } }, +}; + static const struct bt_shell_menu scan_menu = { .name = "scan", .desc = "Scan Options Submenu", @@ -2897,6 +2966,7 @@ int main(int argc, char *argv[]) bt_shell_init(argc, argv, &opt); bt_shell_set_menu(&main_menu); bt_shell_add_submenu(&advertise_menu); + bt_shell_add_submenu(&advertise_monitor_menu); bt_shell_add_submenu(&scan_menu); bt_shell_add_submenu(&gatt_menu); bt_shell_set_prompt(PROMPT_OFF); From patchwork Wed Aug 19 04:11:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 11722487 X-Patchwork-Delegate: luiz.dentz@gmail.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D1FC5109B for ; Wed, 19 Aug 2020 04:12:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B58BA20738 for ; Wed, 19 Aug 2020 04:12:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="JQZI77mW" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725788AbgHSEMa (ORCPT ); Wed, 19 Aug 2020 00:12:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725803AbgHSEM2 (ORCPT ); Wed, 19 Aug 2020 00:12:28 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C2FEC061343 for ; Tue, 18 Aug 2020 21:12:28 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id t11so14262507pfq.21 for ; Tue, 18 Aug 2020 21:12:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=KDMAZBNBq+Q66R2m1YuL2nFJAbD1yqbyI9MMCt4su+g=; b=JQZI77mWDWM37eOZ3zFvzpCFX+2Iw2mWJF5wHXiHpDcwyiTiXrVGo014SZ+3rl3LKv qm/PAxkbFu2VcqPsVBbTqHkdSqB80WOQ6I2r8yqW/Cqc+L/a0yk0nBk1b3d3aE+zsWs1 1ObBl8AVf2amQwHwohT2X72+70P3sugB7lafxtFdh+XUyavyNaRE9K/+Oyl34bjmy5zw 2yq7eUx1miAahD791zfR/bJHwtUiDwH2b0jkHTGV2LayT9NjenXDBWXqn177kmytk5Cm XPxoueAT98iNcPwRaDjN9V0nfQ7eCiEwkn0K69lOtVvpojBVDTtWhdMnuo954W5AllSb h9ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=KDMAZBNBq+Q66R2m1YuL2nFJAbD1yqbyI9MMCt4su+g=; b=haK/6GHV4MREd/8+rj9goOe9+WsE+3GG5mgultR2JhaxjYzhEu78zIkG7sexO3xxQx jO+6FOZY7E7ocggU8uRz6zjdYSWZUfkZsn15aV35/JQAHYoWSfWsLKzuOhduoTapFOiG L77Drep/UkX8QDaSvzelayoEBNGwVgQmqi6IVeKWA2nToHQW9KoeFlv9rQjJUSQTF0TK tU9tkLF4RuWnqXaidnPdtQBoRTkH+m6Qy1A1l8jyLVWFYyoiPvBCaNuY9bRu0A336C6J O1Nzfhvo3a7ryDA2ToZPhndVlgp/A49gL2/V2CyJc6oHxctgQ7eNl6lADt5YLWTpr36b fTJQ== X-Gm-Message-State: AOAM531KrjjNl7aXE9uOnqxDLyKIgQkru9A8+T/KLSsMKub5j454YVPx ILOpRjVUMSvpv1br3tZ28Ouf0dCxwY/Mh6puHHBMGvfcodD7qU4o+wcvFObLbgU4uKJjG9OAQut 90HSkdXPDY7N9diSYvBNA4cijn0mcO+aXX8MrU6IQTBM0zRb20RLeoH89TvDJxqxqSqG47b0lo8 Mjo67+Zor43hM= X-Google-Smtp-Source: ABdhPJymvd3pwUrerSqA+hpGEjWZTpKw/cQxbIU/abX2D3oPGRelPiv7w+dQTSsjOIoafJAhHQgAyd7SADzkXnvzBw== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:f693:9fff:fef4:4e45]) (user=howardchung job=sendgmr) by 2002:a17:90a:bd82:: with SMTP id z2mr477740pjr.1.1597810347418; Tue, 18 Aug 2020 21:12:27 -0700 (PDT) Date: Wed, 19 Aug 2020 12:11:04 +0800 In-Reply-To: <20200819121048.BlueZ.v1.1.I3a57ea1eb3e3f5b87abc44ea478fc83817627ffc@changeid> Message-Id: <20200819121048.BlueZ.v1.3.I3037dbcd77b86f96f7f55350ad7ce5224208c20d@changeid> Mime-Version: 1.0 References: <20200819121048.BlueZ.v1.1.I3a57ea1eb3e3f5b87abc44ea478fc83817627ffc@changeid> X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [BlueZ PATCH v1 3/4] client: Expose ADV monitor objects From: Howard Chung To: linux-bluetooth@vger.kernel.org Cc: alainm@chromium.org, mcchou@chromium.org, mmandlik@chromium.org, Howard Chung Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This adds logic to expose user-defined advertisement monitor to dbus and also implements methods for exposed objects. Signed-off-by: Howard Chung --- client/advertisement_monitor.c | 187 ++++++++++++++++++++++++++++++++- 1 file changed, 183 insertions(+), 4 deletions(-) diff --git a/client/advertisement_monitor.c b/client/advertisement_monitor.c index ec8f23711..7ec5243e7 100644 --- a/client/advertisement_monitor.c +++ b/client/advertisement_monitor.c @@ -53,12 +53,13 @@ struct pattern { struct adv_monitor { uint8_t idx; + char *path; char *type; struct rssi_setting *rssi; GSList *patterns; }; -struct adv_monitor_manager { +static struct adv_monitor_manager { GSList *supported_types; GSList *supported_features; GDBusProxy *proxy; @@ -68,6 +69,163 @@ struct adv_monitor_manager { static uint8_t adv_mon_idx; static GSList *adv_mons; +static void remove_adv_monitor(void *data, void *user_data); + +static DBusMessage *release_adv_monitor(DBusConnection *conn, + DBusMessage *msg, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + + bt_shell_printf("Advertisement monitor %d released\n", + adv_monitor->idx); + remove_adv_monitor(adv_monitor, conn); + + return dbus_message_new_method_return(msg); +} + +static DBusMessage *activate_adv_monitor(DBusConnection *conn, + DBusMessage *msg, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + + bt_shell_printf("Advertisement monitor %d activated\n", + adv_monitor->idx); + return dbus_message_new_method_return(msg); +} + +static DBusMessage *device_found_adv_monitor(DBusConnection *conn, + DBusMessage *msg, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + const char *device; + + dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device, + DBUS_TYPE_INVALID); + bt_shell_printf("Advertisement monitor %d found device %s\n", + adv_monitor->idx, device); + return dbus_message_new_method_return(msg); +} + +static DBusMessage *device_lost_adv_monitor(DBusConnection *conn, + DBusMessage *msg, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + const char *device; + + dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device, + DBUS_TYPE_INVALID); + bt_shell_printf("Advertisement monitor %d lost device %s\n", + adv_monitor->idx, device); + return dbus_message_new_method_return(msg); +} + +static const GDBusMethodTable adv_monitor_methods[] = { + { GDBUS_ASYNC_METHOD("Release", NULL, NULL, release_adv_monitor) }, + { GDBUS_ASYNC_METHOD("Activate", NULL, NULL, activate_adv_monitor) }, + { GDBUS_ASYNC_METHOD("DeviceFound", GDBUS_ARGS({ "device", "o" }), + NULL, device_found_adv_monitor) }, + { GDBUS_ASYNC_METHOD("DeviceLost", GDBUS_ARGS({ "device", "o" }), + NULL, device_lost_adv_monitor) }, + { } +}; + + +static gboolean get_type(const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, + &adv_monitor->type); + return TRUE; +} + +static gboolean get_rssi(const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + struct rssi_setting *rssi = adv_monitor->rssi; + DBusMessageIter data_iter; + + dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, + NULL, &data_iter); + dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_INT16, + &rssi->high_threshold); + dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_UINT16, + &rssi->high_timer); + dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_INT16, + &rssi->low_threshold); + dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_UINT16, + &rssi->low_timer); + dbus_message_iter_close_container(iter, &data_iter); + return TRUE; +} + +static gboolean rssi_exists(const GDBusPropertyTable *property, void *data) +{ + struct adv_monitor *adv_monitor = data; + + return adv_monitor->rssi != NULL; +} + +static void append_pattern_content_to_dbus(DBusMessageIter *iter, + struct pattern *pattern) +{ + DBusMessageIter data_iter; + int idx; + + dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, + DBUS_TYPE_BYTE_AS_STRING, &data_iter); + for (idx = 0; idx < pattern->content_len; idx++) + dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_BYTE, + &pattern->content[idx]); + dbus_message_iter_close_container(iter, &data_iter); +} + +static void append_pattern_to_dbus(void *data, void *user_data) +{ + struct pattern *pattern = data; + DBusMessageIter *array_iter = user_data; + DBusMessageIter data_iter; + + dbus_message_iter_open_container(array_iter, DBUS_TYPE_STRUCT, + NULL, &data_iter); + dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_BYTE, + &pattern->start_pos); + dbus_message_iter_append_basic(&data_iter, DBUS_TYPE_BYTE, + &pattern->ad_data_type); + append_pattern_content_to_dbus(&data_iter, pattern); + dbus_message_iter_close_container(array_iter, &data_iter); +} + +static gboolean get_patterns(const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + struct adv_monitor *adv_monitor = user_data; + DBusMessageIter array_iter; + + dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "(yyay)", + &array_iter); + g_slist_foreach(adv_monitor->patterns, append_pattern_to_dbus, + &array_iter); + dbus_message_iter_close_container(iter, &array_iter); + return TRUE; +} + +static gboolean pattern_exists(const GDBusPropertyTable *property, void *data) +{ + struct adv_monitor *adv_monitor = data; + + return adv_monitor->patterns != NULL; +} + +static const GDBusPropertyTable adv_monitor_props[] = { + { "Type", "s", get_type }, + { "RSSIThresholdsAndTimers", "(nqnq)", get_rssi, NULL, rssi_exists }, + { "Patterns", "a(yyay)", get_patterns, NULL, pattern_exists }, + { } +}; + static void set_supported_list(GSList **list, DBusMessageIter *iter) { char *str; @@ -201,6 +359,7 @@ static void free_adv_monitor(void *user_data) { struct adv_monitor *adv_monitor = user_data; + g_free(adv_monitor->path); g_free(adv_monitor->type); g_free(adv_monitor->rssi); g_slist_free_full(adv_monitor->patterns, free_pattern); @@ -300,6 +459,16 @@ static GSList *parse_patterns(char *pattern_list[], int num) return patterns; } +static void remove_adv_monitor(void *data, void *user_data) +{ + struct adv_monitor *adv_monitor = data; + DBusConnection *conn = user_data; + + adv_mons = g_slist_remove(adv_mons, adv_monitor); + g_dbus_unregister_interface(conn, adv_monitor->path, + ADV_MONITOR_INTERFACE); +} + static gint cmp_adv_monitor_with_idx(gconstpointer a, gconstpointer b) { const struct adv_monitor *adv_monitor = a; @@ -335,6 +504,7 @@ static void print_adv_monitor(struct adv_monitor *adv_monitor) GSList *l; bt_shell_printf("Advertisement Monitor %d\n", adv_monitor->idx); + bt_shell_printf("\tpath: %s\n", adv_monitor->path); bt_shell_printf("\ttype: %s\n", adv_monitor->type); if (adv_monitor->rssi) { bt_shell_printf("\trssi:\n"); @@ -410,6 +580,16 @@ void adv_monitor_add_monitor(DBusConnection *conn, int argc, char *argv[]) adv_monitor->type = g_strdup(type); adv_monitor->rssi = rssi; adv_monitor->patterns = patterns; + adv_monitor->path = g_strdup_printf("%s/%hhu", ADV_MONITOR_APP_PATH, + adv_mon_idx); + if (g_dbus_register_interface(conn, adv_monitor->path, + ADV_MONITOR_INTERFACE, + adv_monitor_methods, NULL, + adv_monitor_props, adv_monitor, + free_adv_monitor) == FALSE) { + bt_shell_printf("Failed to register advertisement monitor\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } adv_mons = g_slist_append(adv_mons, adv_monitor); bt_shell_printf("Advertisement Monitor %d added\n", adv_monitor->idx); @@ -444,7 +624,7 @@ void adv_monitor_remove_monitor(DBusConnection *conn, int monitor_idx) struct adv_monitor *adv_monitor; if (monitor_idx < 0) { - g_slist_free_full(g_steal_pointer(&adv_mons), free_adv_monitor); + g_slist_foreach(adv_mons, remove_adv_monitor, conn); return; } @@ -455,8 +635,7 @@ void adv_monitor_remove_monitor(DBusConnection *conn, int monitor_idx) return; } - adv_mons = g_slist_remove(adv_mons, adv_monitor); - free_adv_monitor(adv_monitor); + remove_adv_monitor(adv_monitor, conn); bt_shell_printf("Monitor %d deleted\n", monitor_idx); } From patchwork Wed Aug 19 04:11:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 11722489 X-Patchwork-Delegate: luiz.dentz@gmail.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8DAE515E4 for ; Wed, 19 Aug 2020 04:12:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 71B552078B for ; Wed, 19 Aug 2020 04:12:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="j5YgV5NB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725931AbgHSEMk (ORCPT ); Wed, 19 Aug 2020 00:12:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725883AbgHSEMj (ORCPT ); Wed, 19 Aug 2020 00:12:39 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD34DC061389 for ; Tue, 18 Aug 2020 21:12:38 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e196so24609320ybh.6 for ; Tue, 18 Aug 2020 21:12:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=NAYmT5pippiwPtvep7OJSxG8Hwp8J5cDMvPe3QXl29I=; b=j5YgV5NBvP12iEfssdXHIWsPKz5HBBAQuYd6YstWZuGyJupdVuyLPK3SWHrzIKukL+ Oy2HMwUGM6ZKqbAltItRNRnNxn/GCosk9qyQDw9Hm8MALCg8V+TcTnVDbLFa7zXvqA4w HM35Kjh2o536ECsBh9zBUt7p6pVntJPII0uEchu5bvVhG8T89rnFGnAGsRKQMIywcDJ+ d/tVF33ZCGQYWNv2QvKwK63ZFZxVoYnZfo2DfhvZnH1pS3PAR8LuGOicUG1nis/Qib0z YqTrqjzrs11yHtcoSG0R/rGhCeo3NzJxnaVGvfnQG4pah7EJAY+EiDtv2vrlvqaLYfUe Ckww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=NAYmT5pippiwPtvep7OJSxG8Hwp8J5cDMvPe3QXl29I=; b=QSYDYqMHOvx8IYWvqptNoyezdeO9marpIukCwSA9SL+fdd4Awekt3I15rANc8WccMw DXJzzrRrB4O7BP26hm7XOvn4totpJUybNynTfP3GDNYkc4SewPohOwhPqcvoxeCikN6s k5uuejbEo4mld3TwFoQcIcQT+9ZbUMkkqjXGtZuZsOn9zEB0is/ilE47ivcEMLm7XvPi lyI+AFCOTC6tE1b2cUh28oqXUX1Je+Aqrc7l1eOkev22vHyZ6rJ62tyDEgnsfNKiUeyV mw/91YAjbtO8RqIAFVDo0Axm2DuJ5cBOWr/lzvZUpN+0mcejBplWsnGOQ7/ZhzuM9Won PzUQ== X-Gm-Message-State: AOAM530MUrVrz70rWchIH/qT1sYaOE3tZdGJO73Pu+GrFYx7DiRY27h0 GOVCgz6Bzk7I5bj+JiMxY1znjwg3EW/FNRZm4BvUjPnxsdI2c7aHtky5Z5iXeBkouA1+G7hM403 Huh3X7GrkR1hsHgPWzUtEyCi0taDH2ElbAubsv2ofxF5ZoME5l99UEqt31J74w1NaHeMpcjjhw5 k9K08s3SmN4tQ= X-Google-Smtp-Source: ABdhPJypSxy6Q+jDMoQkNt9PubSGX41e+45UZkBZgTsAhrKxur52gnS5xTyNt+js7mMypX5sTHnt8oR8MbWfo1NDFQ== X-Received: by 2002:a25:df02:: with SMTP id w2mr28905861ybg.189.1597810357890; Tue, 18 Aug 2020 21:12:37 -0700 (PDT) Date: Wed, 19 Aug 2020 12:11:05 +0800 In-Reply-To: <20200819121048.BlueZ.v1.1.I3a57ea1eb3e3f5b87abc44ea478fc83817627ffc@changeid> Message-Id: <20200819121048.BlueZ.v1.4.I0ebba5ae17d45060148f91000e6c622bcd0b30da@changeid> Mime-Version: 1.0 References: <20200819121048.BlueZ.v1.1.I3a57ea1eb3e3f5b87abc44ea478fc83817627ffc@changeid> X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [BlueZ PATCH v1 4/4] core: Add AdvertisementMonitor to bluetooth.conf From: Howard Chung To: linux-bluetooth@vger.kernel.org Cc: alainm@chromium.org, mcchou@chromium.org, mmandlik@chromium.org, Howard Chung Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org AdvertisementMonitor must be included in bluetooth.conf in order to be able to call Release Signed-off-by: Howard Chung --- src/bluetooth.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bluetooth.conf b/src/bluetooth.conf index 8a1e25801..b6c614908 100644 --- a/src/bluetooth.conf +++ b/src/bluetooth.conf @@ -10,6 +10,7 @@ +