From patchwork Wed Sep 16 07:16:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 11779151 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 572AD618 for ; Wed, 16 Sep 2020 07:17:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2CA41208E4 for ; Wed, 16 Sep 2020 07:17:05 +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="QjneQPVO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726210AbgIPHRE (ORCPT ); Wed, 16 Sep 2020 03:17:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726068AbgIPHRD (ORCPT ); Wed, 16 Sep 2020 03:17:03 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1602AC06174A for ; Wed, 16 Sep 2020 00:17:03 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id e28so3349167pgm.15 for ; Wed, 16 Sep 2020 00:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=cWZeVLgiIS+2wh6pBORQRboLsHbieihMlsn38o9JgxU=; b=QjneQPVOkaCrXizWoqvlhAmstEUYm+MuZh0J4TLQQhVyFeSirGp5lrMIfri9spzN/c dt5DmwhTstfcXbO3ufFEvqGVc/e0jCwORj5DkZOu0nDRw0CCfuAw2C981jI9YQ3p7Yj7 InMsd+nn7B9HYeXoY0XuP+UprN1lSnQBMNbTo7GFjQRx95eSvBpaeMeXDGAxpOfkV9IO Dqa6/B5r2mTUHQtJIIgoL2tsjmmKK7G2pCxDSPUvz4VN0nGf08h9osxPQ6LPEB3/E7PX l4pgG6xoMxDIf5di5WS7bTJRbGyhmuuzCqyhOFNND+UUZP5k9j0RRYLgiKMjHNqVdM/3 95bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=cWZeVLgiIS+2wh6pBORQRboLsHbieihMlsn38o9JgxU=; b=FQSe+U6uV4cFFuZ8KYHJ4CFsdSbPrxwpiv9Siu0fJnkKU0Xx33SCdtW59/Zs26vDJo v49QQuPiuAhJZ/tfJ9k/Yxy8ESsZUTeGmemF4vjhRm5luc9uVd7mcDRGm5cQ6SlEsl5G 4awX619tmtzCSivZKIHEQPgJgNq0uBb9hmsm8D0dG83fLQEkIPmd0oAhKk319B/l4oRu xPBpZPvLaed4FMY0rpyZLoCAMXlWnYq65Z+qqSd/4NVWqVx42UXSWBfHNpr8kQwCEC3w 1D/fFQAi2EMJtlO+IeqOG78wgl1BN1/sN0dBVe6cf7GiFfX3zSoCZ2qIO1jEDNySmZb3 ZDDA== X-Gm-Message-State: AOAM530ZgptzOkmWj9jwx2XXr3L/M3lS2RWRSTM3Iy663+f9lE9gbQ12 8KZdHP7zfS7Lo+Y0kQytYePayCJR0WWhs5QnKWtNtGKsIvg+FXu2PALmXaCr+58lQTyJRBd2wBD smiDQXQlCFjRBi1d6rdsrfCt8nlfhNUJjweEeblJhuTjqdEu6E+1eL8K3yUOAtKJCzvHEeHM4+E nY+o/YFNF64Ns= X-Google-Smtp-Source: ABdhPJw9+po2UXCtmownjpx6rPsHH1kjExY3SLKPFXHtyU6NGF/1e119Xw/6EhYeGqqc3X8Tfl34XLXkqyMESEhboA== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:f693:9fff:fef4:4e45]) (user=howardchung job=sendgmr) by 2002:a17:90b:941:: with SMTP id dw1mr299106pjb.1.1600240621957; Wed, 16 Sep 2020 00:17:01 -0700 (PDT) Date: Wed, 16 Sep 2020 15:16:49 +0800 Message-Id: <20200916151617.BlueZ.v5.1.I8e067a74d324751fc788f53e0c14f60923683d01@changeid> Mime-Version: 1.0 X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7-goog Subject: [BlueZ PATCH v5 1/4] client: Implement basic interface of ADV monitor in bluetoothctl From: Howard Chung To: linux-bluetooth@vger.kernel.org Cc: luiz.von.dentz@intel.com, mmandlik@chromium.org, mcchou@chromium.org, alainm@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 --- Changes in v5: - Rephrase condition == TRUE/FALSE to condition/!condition - Add bt_shell_noninteractive_quit(EXIT_SUCCESS) in register_reply - Remove unused variable 'conn' - Move a change regarding checking the status of app registered from patch 2/4 to 1/4 Changes in v4: - Remove PRE-UPSTREAM in commit title Changes in v3: - Rename advertisement_monitor to adv_monitor Changes in v2: - Update the add-pattern usage - Fix styling issue in patch 1 - Fix storage class declaration issue Makefile.tools | 2 + client/adv_monitor.c | 165 +++++++++++++++++++++++++++++++++++++++++++ client/adv_monitor.h | 23 ++++++ client/main.c | 28 ++++++++ 4 files changed, 218 insertions(+) create mode 100644 client/adv_monitor.c create mode 100644 client/adv_monitor.h diff --git a/Makefile.tools b/Makefile.tools index 9b9236609..ed0fbf8a3 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/adv_monitor.h \ + client/adv_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/adv_monitor.c b/client/adv_monitor.c new file mode 100644 index 000000000..dda0eaf90 --- /dev/null +++ b/client/adv_monitor.c @@ -0,0 +1,165 @@ +/* + * + * 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 "adv_monitor.h" + +#define ADV_MONITOR_APP_PATH "/org/bluez/adv_monitor_app" +#define ADV_MONITOR_INTERFACE "org.bluez.AdvertisementMonitor1" + +static 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) +{ + DBusError error; + + dbus_error_init(&error); + + if (!dbus_set_error_from_message(&error, message)) { + bt_shell_printf("AdvertisementMonitor path registered\n"); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); + } + + 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) +{ + DBusError error; + + dbus_error_init(&error); + + if (!dbus_set_error_from_message(&error, message)) { + 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.app_registered) { + 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)) { + 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) { + 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)) { + bt_shell_printf("Failed to unregister Advertisement Monitor\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + manager.app_registered = FALSE; +} diff --git a/client/adv_monitor.h b/client/adv_monitor.h new file mode 100644 index 000000000..77b0b62c6 --- /dev/null +++ b/client/adv_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 da877b546..75f8bc462 100644 --- a/client/main.c +++ b/client/main.c @@ -41,6 +41,7 @@ #include "agent.h" #include "gatt.h" #include "advertising.h" +#include "adv_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 Sep 16 07:16:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 11779153 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 A708A59D for ; Wed, 16 Sep 2020 07:17:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7F0E920771 for ; Wed, 16 Sep 2020 07:17:13 +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="itClPvXi" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726245AbgIPHRM (ORCPT ); Wed, 16 Sep 2020 03:17:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726068AbgIPHRI (ORCPT ); Wed, 16 Sep 2020 03:17:08 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0D55C06174A for ; Wed, 16 Sep 2020 00:17:07 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id u6so5360292qte.8 for ; Wed, 16 Sep 2020 00:17:07 -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=/sZaUwkMRhy5nGDqEj/npb5gumg8a9ZMAwv0bXjQQfY=; b=itClPvXihlayARe++RPVG8CSXs69gWYE2L3+mgHIq1X7FQgMCfvACy2YL02SCbFMpe VCp2ZFLHujKhbT0ddlq0PyaJZXhVT4oDfd/pqiKM9/Ic40YAk/jDcw/zDQ7dBBYxtUvU oqDKdxLr6QjN1ZVO/JPsT6THzU9SwAVwY5TSU3mE0IT1odVLqm9kG/migWkfolzz94eq mYOFZJTf9w1zpMHV3T/NX10uuUllbaV4SJCYmd6bUCLmcA73Z1SnpUeh6VjQffB4Vhaq dbaocYkcr7kr2s9IL7/5hMMLCwBOVhWrQR86tsNdz4t8r9gwcurwA5qoyQurHM30qaBB 6BEg== 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=/sZaUwkMRhy5nGDqEj/npb5gumg8a9ZMAwv0bXjQQfY=; b=s+hP33AoOsAFloIHaOJRR4PyLfRMAlrfwohywqZGjz9NnZgQiQOnC1lV9+CYPZpdHm FFykgVQYXXsW28AXQ0rJkR2C00WUOyN8IEAdosNm8w1Rsdo7J4gkETLfGMDFz1mchpMK Mf6CRKv6xE0tycWUt8BHbjZqaeJzuj+WeyTi9tWbMQA9O4MA0K+u75+fsM1XivSMAADL T4sIG5eA0iPXI8hclpUKvIHI1fdL4Oiegd5dr8tYUrXDEDI+WNxHsH894Na3mp4+2CvZ b2WjPk/FXG2z/pioa89o2XNHxGzRggkImwUIwiYgarTLCX30c8ZLeB+tiUAwtbDYQ4g0 pwxg== X-Gm-Message-State: AOAM530FH82GE2Cw0kkq5+6OhiBCPck4aCbS0vklK9celF1FFOzF5mIk bYYDQL3JNC1U4pnr/dNiT50GOd3w3clonblT+kPYErkpBgesD+4lFXu8MKxU5rcfXPH7FpoDcht zb/eaHzqnsJEXVsCJ29eEoO9KYesRMrFv34yMxycqyRgJsBuqYEAadmF4tF16SaC9RzeGprEaec ig7e8GOZOIi8k= X-Google-Smtp-Source: ABdhPJxrkoph8jzWjPDbb2/sxrKWgQVECfA7gaDehYfzSyRzx5/7w/AMJaG5xkAcTHmJ5HHogEQCq0GfneaOwKwFMA== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:f693:9fff:fef4:4e45]) (user=howardchung job=sendgmr) by 2002:ad4:5387:: with SMTP id i7mr5454479qvv.43.1600240625860; Wed, 16 Sep 2020 00:17:05 -0700 (PDT) Date: Wed, 16 Sep 2020 15:16:50 +0800 In-Reply-To: <20200916151617.BlueZ.v5.1.I8e067a74d324751fc788f53e0c14f60923683d01@changeid> Message-Id: <20200916151617.BlueZ.v5.2.Iec6b15e23f4228b80f9b364eb76515d146d592d5@changeid> Mime-Version: 1.0 References: <20200916151617.BlueZ.v5.1.I8e067a74d324751fc788f53e0c14f60923683d01@changeid> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7-goog Subject: [BlueZ PATCH v5 2/4] client: Implement more interfaces of ADV monitor in bluetoothctl From: Howard Chung To: linux-bluetooth@vger.kernel.org Cc: luiz.von.dentz@intel.com, mmandlik@chromium.org, mcchou@chromium.org, alainm@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 monitor [bluetooth]# add-or-pattern 1 2 ab0011 Advertisement Monitor 0 added [bluetooth]# add-or-pattern-rssi -80, ,5 1 2 aa 3 4 ff Advertisement Monitor 1 added [bluetooth]# get-pattern all Advertisement Monitor 0 path: /org/bluez/adv_monitor_app/0 type: or_patterns pattern 1: start position: 1 AD data type: 2 content: ab0011 Advertisement Monitor 1 path: /org/bluez/adv_monitor_app/1 type: or_patterns rssi: high threshold: -50 high threshold timer: 5 low threshold: -80 low threshold timer: 5 pattern 1: start position: 1 AD data type: 2 content: aa pattern 2: start position: 3 AD data type: 4 content: ff [bluetooth]# get-supported-info Supported Features: Supported Moniter Types: or_patterns [bluetooth]# remove-pattern 0 Monitor 0 deleted Reviewed-by: Miao-chen Chou Reviewed-by: Manish Mandlik --- Changes in v5: - Move a change regarding checking the status of app registered to patch 1/4 Changes in v4: - Remove PRE-UPSTREAM in commit title Changes in v2: - Update the add-pattern usage client/adv_monitor.c | 341 +++++++++++++++++++++++++++++++++++++++++++ client/adv_monitor.h | 10 ++ client/main.c | 109 ++++++++++++++ 3 files changed, 460 insertions(+) diff --git a/client/adv_monitor.c b/client/adv_monitor.c index dda0eaf90..d4914b7b1 100644 --- a/client/adv_monitor.c +++ b/client/adv_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 "adv_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; +}; + static struct adv_monitor_manager { GSList *supported_types; GSList *supported_features; @@ -43,6 +65,9 @@ static 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; @@ -163,3 +188,319 @@ 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 void parse_rssi_value_pair(char *value_pair, int *low, int *high) +{ + char *val1, *val2; + bool flag = value_pair[0] == ','; + + val1 = strtok(value_pair, ","); + + if (!val1) + return; + + val2 = strtok(NULL, ","); + + if (!val2) { + if (!flag) + *low = atoi(val1); + else + *high = atoi(val1); + } else { + *low = atoi(val1); + *high = atoi(val2); + } +} + +static struct rssi_setting *parse_rssi(char *range, char *timeout) +{ + struct rssi_setting *rssi; + int high_threshold, low_threshold, high_timer, low_timer; + + high_threshold = RSSI_DEFAULT_HIGH_THRESHOLD; + low_threshold = RSSI_DEFAULT_LOW_THRESHOLD; + high_timer = RSSI_DEFAULT_HIGH_TIMEOUT; + low_timer = RSSI_DEFAULT_LOW_TIMEOUT; + + parse_rssi_value_pair(range, &low_threshold, &high_threshold); + parse_rssi_value_pair(timeout, &low_timer, &high_timer); + + rssi = g_malloc0(sizeof(struct rssi_setting)); + + if (!rssi) { + bt_shell_printf("Failed to allocate rssi_setting"); + bt_shell_noninteractive_quit(EXIT_FAILURE); + return NULL; + } + + rssi->high_threshold = high_threshold; + rssi->high_timer = high_timer; + rssi->low_threshold = low_threshold; + rssi->low_timer = low_timer; + + return rssi; +} + +static struct pattern *parse_pattern(char *parameter_list[]) +{ + struct pattern *pat; + + 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 = atoi(parameter_list[0]); + pat->ad_data_type = atoi(parameter_list[1]); + pat->content_len = str2bytearray(parameter_list[2], 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; + } + + if (num%3) { + bt_shell_printf("Expected %d more arguments\n", 3 - num%3); + return NULL; + } + + for (cnt = 0; cnt < num; cnt += 3) { + 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, char *type, + gboolean rssi_enabled, int argc, char *argv[]) +{ + struct adv_monitor *adv_monitor; + struct rssi_setting *rssi; + GSList *patterns = NULL; + + 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; + + if (rssi_enabled == FALSE) + rssi = NULL; + else { + rssi = parse_rssi(argv[1], argv[2]); + if (rssi == NULL) + return; + + argv += 2; + argc -= 2; + } + + patterns = parse_patterns(argv+1, argc-1); + if (patterns == NULL) { + bt_shell_printf("pattern-list malformed\n"); + 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/adv_monitor.h b/client/adv_monitor.h index 77b0b62c6..12c01bd3f 100644 --- a/client/adv_monitor.h +++ b/client/adv_monitor.h @@ -17,7 +17,17 @@ * */ +#define RSSI_DEFAULT_HIGH_THRESHOLD -50 +#define RSSI_DEFAULT_LOW_THRESHOLD -70 +#define RSSI_DEFAULT_HIGH_TIMEOUT 10 +#define RSSI_DEFAULT_LOW_TIMEOUT 5 + void adv_monitor_add_manager(DBusConnection *conn, GDBusProxy *proxy); void adv_monitor_remove_manager(DBusConnection *conn); void adv_monitor_register_app(DBusConnection *conn); void adv_monitor_unregister_app(DBusConnection *conn); +void adv_monitor_add_monitor(DBusConnection *conn, char *type, + gboolean rssi_enabled, 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 75f8bc462..2b0243308 100644 --- a/client/main.c +++ b/client/main.c @@ -2719,6 +2719,85 @@ static void cmd_ad_clear(int argc, char *argv[]) return bt_shell_noninteractive_quit(EXIT_FAILURE); } +static void print_add_or_pattern_with_rssi_usage(void) +{ + bt_shell_printf("rssi-range format:\n" + "\t,\n" + "\tBoth parameters can be skipped, in that case the\n" + "\tparamter will be set to its pre-defined value\n"); + bt_shell_printf("\tPre-defined low-rssi,high-rssi: %d,%d\n", + RSSI_DEFAULT_LOW_THRESHOLD, + RSSI_DEFAULT_HIGH_THRESHOLD); + bt_shell_printf("timeout format:\n" + "\t,\n" + "\tBoth parameters can be skipped, in that case the\n" + "\tparamter will be set to its pre-defined value\n"); + bt_shell_printf("\tPre-defined low-timeout,high-timeout: %d,%d\n", + RSSI_DEFAULT_LOW_TIMEOUT, + RSSI_DEFAULT_HIGH_TIMEOUT); + bt_shell_printf("pattern format:\n" + "\t \n"); + bt_shell_printf("e.g.\n" + "\tadd-or-pattern-rssi -10, ,10 1 2 01ab55\n"); + bt_shell_printf("or\n" + "\tadd-or-pattern-rssi -50,-30 , 1 2 01ab55 3 4 23cd66\n"); +} + +static void print_add_or_pattern_usage(void) +{ + bt_shell_printf("pattern format:\n" + "\t \n"); + bt_shell_printf("e.g.\n" + "\tadd-or-pattern 1 2 01ab55 3 4 23cd66\n"); +} + +static void cmd_adv_monitor_print_usage(int argc, char *argv[]) +{ + if (strcmp(argv[1], "add-or-pattern") == 0) + print_add_or_pattern_usage(); + else if (strcmp(argv[1], "add-or-pattern-rssi") == 0) + print_add_or_pattern_with_rssi_usage(); + else + bt_shell_printf("Invalid argument %s", argv[1]); +} + +static void cmd_adv_monitor_add_or_monitor_with_rssi(int argc, char *argv[]) +{ + adv_monitor_add_monitor(dbus_conn, "or_patterns", TRUE, argc, argv); +} + +static void cmd_adv_monitor_add_or_monitor(int argc, char *argv[]) +{ + adv_monitor_add_monitor(dbus_conn, "or_patterns", FALSE, 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", @@ -2755,6 +2834,35 @@ static const struct bt_shell_menu advertise_menu = { { } }, }; +static const struct bt_shell_menu advertise_monitor_menu = { + .name = "monitor", + .desc = "Advertisement Monitor Options Submenu", + .entries = { + { "add-or-pattern-rssi", " " + "[patterns=pattern1 pattern2 ...]", + cmd_adv_monitor_add_or_monitor_with_rssi, + "Add 'or pattern' type monitor with RSSI " + "filter" }, + { "add-or-pattern", "[patterns=pattern1 pattern2 ...]", + cmd_adv_monitor_add_or_monitor, + "Add 'or pattern' type monitor without RSSI " + "filter" }, + { "get-pattern", "", + cmd_adv_monitor_print_monitor, + "Get advertisement monitor" }, + { "remove-pattern", "", + 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" }, + { "print-usage", "", + cmd_adv_monitor_print_usage, + "Print the command usage"}, + { } }, +}; + static const struct bt_shell_menu scan_menu = { .name = "scan", .desc = "Scan Options Submenu", @@ -2932,6 +3040,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 Sep 16 07:16:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 11779155 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 03E4D139F for ; Wed, 16 Sep 2020 07:17:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D4A8E20771 for ; Wed, 16 Sep 2020 07:17:13 +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="pj27Jc1Y" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726262AbgIPHRN (ORCPT ); Wed, 16 Sep 2020 03:17:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726149AbgIPHRL (ORCPT ); Wed, 16 Sep 2020 03:17:11 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33175C061788 for ; Wed, 16 Sep 2020 00:17:10 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id c19so5393391qkk.20 for ; Wed, 16 Sep 2020 00:17:10 -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=jI/5VReMm0tTNSWTPn8NUEGnZNxUSUFoa3fJlfjTJJo=; b=pj27Jc1YVh47n8UPHdgaT/laL75+FvslrGhcq1J5HJnlAptYsh+OCt6nvfbezucZHO rkkb3c6sArikpmgJCH9UX+Aog6HhG7QFwEVc/od0yZ+i3RSjiQvy0KqOhVvFDP/NxGof P8iSKdMwD1KLc/kGOe60HabBTl3PIOCnvAnQamWbNz6I9HdRIPn9JULaQWsvhEgQL1eD 1Ih0yGvfawU6qQer1bzVRQKp7cQyemZIa776Lb1U9ZLz2Fj8cBHQkL82B3N+6qy4Xu0P zPCprlWgQx2otjm2XG1zDwuZbS3GZxyaIsCkC+9QXE4O2faFXyfq6wEyIQQzkMAIlrjm TSOg== 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=jI/5VReMm0tTNSWTPn8NUEGnZNxUSUFoa3fJlfjTJJo=; b=l/0KPm+kWUMJGGa2922jflGlNhjYMcSKmx+qrpzMSO6pNAQzZkGD4vbv7+2hVehmmC pxgsCSPKmpXIe/TegpX9uPeVN+cxFhESmNdCmbDbW14L0+zFa/uf7IfXI0PpvHJNzhv+ wj0lsa+Jn2TXg5EUb2st0AaTC3XHZKPROgVOt/5VvJxMkZ3Y7TUyxjWQaCH6pJqNIYWO KyuWEbcky9Rv7Tnd49Tf5KShV0CY9U0PcHHs/ddJF9tRHVLhUzcrxa+G4q2lts0V/j7d CkoNoTSDc/t1feJhS+wRBPjnCZjD2e5/lt11cgaZ0h1SMnw7h/9PcCLM0HVgeTGFjMlP c2Yw== X-Gm-Message-State: AOAM5313qV7Th5PprOJvzhbnBme5xQcY5rIX5OAaJ8uMXYJx8NDQyEF8 rgnOxgEDiy2ne/Wam2mZ+1sBCELX4BKADcnkcv4e8vLRhoZHLGQezNByzvykp1JzPbGZ3mfY45C 78Yuv91/6cS1LjaDvelPZMLC7AQPCka2zNfM7arwoACJ3ZDDyaLbQCoWhZJWh6WJ6UuUlJLHNlR ZLNmn09Fx3asM= X-Google-Smtp-Source: ABdhPJxtKfd+ASL9Pkb1wuiVvivO2YSVOAYdi9KRT9dWnDfSn89fGSGfrPHK0JnU+wUgVa2D2ym2OY+x5axGSHPHUQ== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:f693:9fff:fef4:4e45]) (user=howardchung job=sendgmr) by 2002:ad4:4f30:: with SMTP id fc16mr21833088qvb.6.1600240629267; Wed, 16 Sep 2020 00:17:09 -0700 (PDT) Date: Wed, 16 Sep 2020 15:16:51 +0800 In-Reply-To: <20200916151617.BlueZ.v5.1.I8e067a74d324751fc788f53e0c14f60923683d01@changeid> Message-Id: <20200916151617.BlueZ.v5.3.I680cda25cec9c45ca2b2c5ac3a8e437772ea76d9@changeid> Mime-Version: 1.0 References: <20200916151617.BlueZ.v5.1.I8e067a74d324751fc788f53e0c14f60923683d01@changeid> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7-goog Subject: [BlueZ PATCH v5 3/4] client: Expose ADV monitor objects From: Howard Chung To: linux-bluetooth@vger.kernel.org Cc: luiz.von.dentz@intel.com, mmandlik@chromium.org, mcchou@chromium.org, alainm@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. Reviewed-by: Manish Mandlik --- (no changes since v4) Changes in v4: - Remove PRE-UPSTREAM in commit title Reviewed-by: Miao-chen Chou client/adv_monitor.c | 185 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 182 insertions(+), 3 deletions(-) diff --git a/client/adv_monitor.c b/client/adv_monitor.c index d4914b7b1..22bb3c670 100644 --- a/client/adv_monitor.c +++ b/client/adv_monitor.c @@ -53,6 +53,7 @@ struct pattern { struct adv_monitor { uint8_t idx; + char *path; char *type; struct rssi_setting *rssi; GSList *patterns; @@ -68,6 +69,163 @@ static 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; @@ -200,6 +358,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); @@ -324,6 +483,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; @@ -359,6 +528,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"); @@ -433,6 +603,16 @@ void adv_monitor_add_monitor(DBusConnection *conn, char *type, 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); @@ -467,7 +647,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; } @@ -478,8 +658,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 Sep 16 07:16:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 11779157 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 1749C618 for ; Wed, 16 Sep 2020 07:17:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EA9D0208E4 for ; Wed, 16 Sep 2020 07:17:25 +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="mYcGRXqi" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726359AbgIPHRW (ORCPT ); Wed, 16 Sep 2020 03:17:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726172AbgIPHRP (ORCPT ); Wed, 16 Sep 2020 03:17:15 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D7D5C061788 for ; Wed, 16 Sep 2020 00:17:15 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id i196so1156129pgc.8 for ; Wed, 16 Sep 2020 00:17:15 -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=/616tPVzI5drLVga8v2z0esoWN1KtNkrALjdF3/yzcs=; b=mYcGRXqib4+CZhdWggECEZywwDwXvzm0oXykEa2nbGDBPpfVPAXq0qGO2OvgTtqbaP GT95PHs/sBRaPZDy9qEm1uxfpvW9OE9UeT+0CPO0fnL1c1erb7xsMPaUDib/pWn4m/Sh VQ9//0W9OeaW0COezLyWapGW3XKk3YJGDqVPCrBZ5K5SZNu06kg1VFKsbzvXOrf5d/nL LeVfu+zgobavmjem176REed1F95s4yEZuzZrefiGExABWUlmqWIGknBl+9Wrle4TSjAA +eiFRW7etGRFj+X7nHZJoEHSfmkJNypPmYdpqNj41NCFl1rZGNsdNEhUc3cBGcUTWwsZ QNPQ== 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=/616tPVzI5drLVga8v2z0esoWN1KtNkrALjdF3/yzcs=; b=OADmBOPKv1wINQDO247yS4vwQ5FCeiCVa02SEBZSZc662QwmuSKSe8UdEZJj2evo3u qoDOajojU9hENfpPbZRIqyH8s7AuC8a2K4U2M8nbkN3oGWToee6iI7rnmJJpygcC4OpG +EbadDWc6gd7MoqFRzjVAFNZeG5hv7GIxqvLqPx1WKvZYFEphzJNFl0fLWcWPm7o6nxO Q2WV8aTdXYeGpUEOvw0Iw5wqikKGSp+vuPEJ8tRJDUEKFgckas4uIdhYNPJkQBEEv1au taVj6NnIZd0Of6qWW/TAJKk7XDfXQ5kRvSf0y4X6WKAvMBIblQN11IjEsQqpDPYcsR5D NYPw== X-Gm-Message-State: AOAM531gVH99F49jL0f4C7s1IJubrnQevfnyW6lVXGWJV7TF2qNUMmlC 6Qugh+4MmHbzCklYO3LWw8Z2m+tdRW7HAaMSM6rxp3Gl+4YZpXk5R0QuSUSJuy3TrjV2e9WxO5r Zgk/hymxhZfgdcs0wElHA0y0DBJ2lkqiVaxfCYM8pKOwutCa8u0xZBPWcv4BmOru0TUms3UHs0t OAzponEaAVeSo= X-Google-Smtp-Source: ABdhPJw/o1x3aNHEkNFEfrNyiZeKO3ygZO3KZYnijBB8bR+9RY77lZseYqzaK4wYiwoK2oJjOmdWotWyslAXHj1Ubg== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:f693:9fff:fef4:4e45]) (user=howardchung job=sendgmr) by 2002:a17:902:c14b:b029:d1:ec9a:aaae with SMTP id 11-20020a170902c14bb02900d1ec9aaaaemr2115689plj.62.1600240633333; Wed, 16 Sep 2020 00:17:13 -0700 (PDT) Date: Wed, 16 Sep 2020 15:16:52 +0800 In-Reply-To: <20200916151617.BlueZ.v5.1.I8e067a74d324751fc788f53e0c14f60923683d01@changeid> Message-Id: <20200916151617.BlueZ.v5.4.Ieffbe3452565ace2af46a63501c767c8798fd88c@changeid> Mime-Version: 1.0 References: <20200916151617.BlueZ.v5.1.I8e067a74d324751fc788f53e0c14f60923683d01@changeid> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7-goog Subject: [BlueZ PATCH v5 4/4] core: Add AdvertisementMonitor to bluetooth.conf From: Howard Chung To: linux-bluetooth@vger.kernel.org Cc: luiz.von.dentz@intel.com, mmandlik@chromium.org, mcchou@chromium.org, alainm@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 Reviewed-by: Manish Mandlik --- (no changes since v4) Changes in v4: - Remove PRE-UPSTREAM in commit title Reviewed-by: Miao-chen Chou 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 @@ +