From patchwork Wed Jun 17 07:55:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Sun X-Patchwork-Id: 11609341 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 ED64760D for ; Wed, 17 Jun 2020 07:55:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D4B5C21475 for ; Wed, 17 Jun 2020 07:55:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="k6EcvM3S" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725941AbgFQHzo (ORCPT ); Wed, 17 Jun 2020 03:55:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725554AbgFQHzn (ORCPT ); Wed, 17 Jun 2020 03:55:43 -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 C7519C061573 for ; Wed, 17 Jun 2020 00:55:41 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id s7so1627232ybg.10 for ; Wed, 17 Jun 2020 00:55:41 -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:content-transfer-encoding; bh=kMhRDzlGP4OVBvVNpjBjO9yKhZDDBHzVjRXl2j9FDQc=; b=k6EcvM3SYylF/mj6Frwg2IKJFXg8eZz88W7J6Rf2Lwk9Zvj1+a9pn9LFbBlfBnNS0/ cdQAOqLiknjdYJxsKKVPLzCPVNwDuH91m3aEGy38IpgbfxqUzzac58l5N0RHDITVJmti tDrEjXmWdW9uuQtcF0ljqRSauUWKOCnB22EXb7/3Xb4PEFPDW+8FZRzUVGjPdWRhdhKS 8/tv6pPPHmzJmIl2qMl/0e/9z09xl644kptNlhRdbPzoO08KfKr7I4dyxS1WUCndfnzG Ml0I8f1Z/qn1p4AXwm5zfZgIWrw3uEfm9cc4NF4++ZdRGYla7gYlahBMQ/r9Af5tuLQ2 lFxA== 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:content-transfer-encoding; bh=kMhRDzlGP4OVBvVNpjBjO9yKhZDDBHzVjRXl2j9FDQc=; b=nmWrDi2x9vNx4Cxkf0l9lcVRFs21Ll5Z8vNN4ikwrhbWkYvzZq+rLJ10qXkIqJbATp 5/uP535P8ZS1hZUOJBdJk1WRTAhZ/LCc6ki/U62YeiE+Ct+Q9pm1VTImC01LfCb6491Y XAEDXPpD0kM+KJe+r4ilYhxnaBbLlzIOyvEu4dFEDxD1//yrR2zac7YX+e3g90yqEWoH 2BF/r/ci4s4QJmiPoE0K/m+meinU2gU2IJcVljnRrwUwzScpA0701sWvhXpimzBFbVSY vWJS7MCuArncuqKUdcL/hX6GBNYXojCzBCnDb3XTB2aAc+CuEVgPFsIiukXMyFBzM5CX jj6g== X-Gm-Message-State: AOAM532QRCoVkVeauUa+2xX647oSsdXnKOQhTXX2mNvYv5UzgJ+oNN3T LBXW/WCzRKN6jNbcIj2R1Raz96wdgUEDHWzyQNWmiqUZQNgjzTu7vu9n6h7YKacvUkY+qICSakO EpQoDxhXZubjsK+j/y8Gkn0FfdBcKUq+MeMFhceUnNjX+gWR+kMsUPl5NhIV+N/F6Df/4/9gNVg u7ZWGCSK0ZcQM= X-Google-Smtp-Source: ABdhPJxN48bh0OBAqWE3ZnE4QeSjU3qocVBtf+NPv+esu70SAH/PQE8cdLCwji51wljNZ9KouzOMNqrpbNcSz9KwFw== X-Received: by 2002:a25:3784:: with SMTP id e126mr11365271yba.267.1592380540948; Wed, 17 Jun 2020 00:55:40 -0700 (PDT) Date: Wed, 17 Jun 2020 00:55:34 -0700 In-Reply-To: <20200617075536.98672-1-michaelfsun@google.com> Message-Id: <20200617005531.bluez.v3.1.Ia9d7e5ca8da3bb6e1356b2b0e6fa18fafa022af0@changeid> Mime-Version: 1.0 References: <20200617075536.98672-1-michaelfsun@google.com> X-Mailer: git-send-email 2.27.0.290.gba653c62da-goog Subject: [bluez PATCH v3 1/3] btmgmt: Add "monitor" btmgmt submenu and command "features" From: Michael Sun To: linux-bluetooth@vger.kernel.org Cc: mcchou@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, luiz.von.dentz@intel.com, alainm@chromium.org, Michael Sun Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This patch introduces a new submenu "monitor" along with command ‘features’ to help user query for supported advertisement features. The command will work with the new MGMT opcode MGMT_OP_READ_ADV_MONITOR_FEATURES. Reviewed-by: Alain Michaud Reviewed-by: Miao-chen Chou Signed-off-by: Michael Sun --- Changes in v3: None Changes in v2: - Move features command into submenu and fix build warnings tools/btmgmt.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tools/btmgmt.c b/tools/btmgmt.c index 46e7465b31c9..a0487d4de178 100644 --- a/tools/btmgmt.c +++ b/tools/btmgmt.c @@ -4567,6 +4567,83 @@ static void cmd_wbs(int argc, char **argv) cmd_setting(MGMT_OP_SET_WIDEBAND_SPEECH, argc, argv); } +static const char * const advmon_features_str[] = { + "Pattern monitor with logic OR.", +}; + +static const char *advmon_features2str(uint32_t features) +{ + static char str[512]; + int off, i; + + off = 0; + snprintf(str, sizeof(str), "\n\tNone"); + + for (i = 0; i < NELEM(advmon_features_str); i++) { + if ((features & (1 << i)) != 0 && off < sizeof(str)) + off += snprintf(str + off, sizeof(str) - off, "\n\t%s", + advmon_features_str[i]); + } + + return str; +} + +static void advmon_features_rsp(uint8_t status, uint16_t len, const void *param, + void *user_data) +{ + const struct mgmt_rp_read_adv_monitor_features *rp = param; + uint32_t supported_features, enabled_features; + uint16_t num_handles; + int i; + + if (status != MGMT_STATUS_SUCCESS) { + error("Reading adv monitor features failed with status 0x%02x " + "(%s)", status, mgmt_errstr(status)); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + if (len < sizeof(*rp)) { + error("Too small adv monitor features reply (%u bytes)", len); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + if (len < sizeof(*rp) + rp->num_handles * sizeof(uint16_t)) { + error("Handles count (%u) doesn't match reply length (%u)", + rp->num_handles, len); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + supported_features = le32_to_cpu(rp->supported_features); + enabled_features = le32_to_cpu(rp->enabled_features); + num_handles = le16_to_cpu(rp->num_handles); + + print("Supported features:%s", advmon_features2str(supported_features)); + print("Enabled features:%s", advmon_features2str(enabled_features)); + print("Max number of handles: %u", le16_to_cpu(rp->max_num_handles)); + print("Max number of patterns: %u", rp->max_num_patterns); + print("Handles list with %u item%s", num_handles, + num_handles == 0 ? "" : num_handles == 1 ? ":" : "s:"); + for (i = 0; i < num_handles; i++) + print("\t0x%04x ", le16_to_cpu(rp->handles[i])); + + return bt_shell_noninteractive_quit(EXIT_SUCCESS); +} + +static void cmd_advmon_features(int argc, char **argv) +{ + uint16_t index; + + index = mgmt_index; + if (index == MGMT_INDEX_NONE) + index = 0; + + if (!mgmt_send(mgmt, MGMT_OP_READ_ADV_MONITOR_FEATURES, index, 0, NULL, + advmon_features_rsp, NULL, NULL)) { + error("Unable to send advertising monitor features command"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } +} + static void register_mgmt_callbacks(struct mgmt *mgmt, uint16_t index) { mgmt_register(mgmt, MGMT_EV_CONTROLLER_ERROR, index, controller_error, @@ -4635,6 +4712,16 @@ static void cmd_select(int argc, char **argv) update_prompt(mgmt_index); } +static const struct bt_shell_menu monitor_menu = { + .name = "monitor", + .desc = "Advertisement Monitor Submenu", + .entries = { + { "features", NULL, + cmd_advmon_features, "Show advertisement monitor " + "features" }, + { } }, +}; + static const struct bt_shell_menu main_menu = { .name = "main", .entries = { @@ -4815,6 +4902,7 @@ int main(int argc, char *argv[]) bt_shell_init(argc, argv, &opt); bt_shell_set_menu(&main_menu); + bt_shell_add_submenu(&monitor_menu); mgmt = mgmt_new_default(); if (!mgmt) { From patchwork Wed Jun 17 07:55:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Sun X-Patchwork-Id: 11609343 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 B401D60D for ; Wed, 17 Jun 2020 07:55:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9894D2151B for ; Wed, 17 Jun 2020 07:55:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="t9OfjYlN" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725979AbgFQHzp (ORCPT ); Wed, 17 Jun 2020 03:55:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725894AbgFQHzn (ORCPT ); Wed, 17 Jun 2020 03:55:43 -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 B4BACC061573 for ; Wed, 17 Jun 2020 00:55:43 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id o140so1594576yba.16 for ; Wed, 17 Jun 2020 00:55:43 -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:content-transfer-encoding; bh=a19SkpFrQn+XChAVdQe+Vd7tw8P3zyx3W3OzzMc/4m8=; b=t9OfjYlNb2dnPKnVzIIiZcrQX0sKDA2p5rVc03wQoMOY1mhwm+pLuXt5F89XchSiL6 WP5n272lA7DwuJmV/oL0zQKpKz6GX9diNI6e9zpOhqGjR8mQ4bzWJVjDaxlB66g0BT8D BepAh1q4iZJQ7MStk6dbbtlM4W858LtJGZDYr1g7c7Qzw1R4jgBEm+00BoGi0fgYh1mo 3Epj5SZBp+IlPTi+jMfqjncxhIkPJZpRFZXRDZrASpK/j2c25w5QDOjwaRIkexNHE9Fr 5HbFydw6q+y2MVe8i2G9HeFnwVStpavHiRM7DkeN1yafrsWpleGhlT4yGCgc3alj+LmY GtLQ== 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:content-transfer-encoding; bh=a19SkpFrQn+XChAVdQe+Vd7tw8P3zyx3W3OzzMc/4m8=; b=dgOqX1j5CP1MFTBQ25oy6qClv+YDzj8McT3V6dRcVQ73wkABH+lfD8wqMGVVnzscuc 2LA7vRoG3yenJY3fqutYAmADBcGA/E8bb9kzru1BrDO1qoOcGP/Ls4Bbpwa3uSS0GQp+ 2gZdjmggYSq+PolyN33BHpcfktdMlNjhl1bGfBXE5Sh8Zut0P/Xc8cEOK3cCRhKRrdrh THqGu+lfBYn/K6zzwCwGFLiF3VoZ54n69rfBvTI56Tfzpx67OJnT16EZ8uYW+jsYKVJ6 bKmHkys1742WXSHun78uNjENgSOZoV5VqyllpLzx0RRuYMQq16GrObxJGPS1CwZO0QnZ 6K1Q== X-Gm-Message-State: AOAM531M/sKlLtDTM5KmpnuHAr6IUTzxsNYTbZ5H4/akH9/tpjxOk8J8 UGnrgozwmFDC9mhM5uGRBfJRBPDKF42G1RzprGQ1gqfv5uqNLmwPZc6+IxgRrI7QZmSAtv0Susj q9Rzr0VkQfIy1zqUCvLi0N+tWN2TulX/U/f7UiHhVSfhHjKF/o5ftIN/wQcaPG9kFYw3KwHtDSD kIwFhgydVadpo= X-Google-Smtp-Source: ABdhPJwp5WdxOAORnf4wngrTelEOy/+OjJcZJ4uetYzEJl2DS4Pk4GA6MDHcloKomK+Yi6Xh8mhsxajTNzwK0gcw3w== X-Received: by 2002:a25:744f:: with SMTP id p76mr9608187ybc.89.1592380542926; Wed, 17 Jun 2020 00:55:42 -0700 (PDT) Date: Wed, 17 Jun 2020 00:55:35 -0700 In-Reply-To: <20200617075536.98672-1-michaelfsun@google.com> Message-Id: <20200617005531.bluez.v3.2.Ib8ae2d78936aa9f16a318aca14cf8c7198449bac@changeid> Mime-Version: 1.0 References: <20200617075536.98672-1-michaelfsun@google.com> X-Mailer: git-send-email 2.27.0.290.gba653c62da-goog Subject: [bluez PATCH v3 2/3] btmgmt: Add command "remove" into "monitor" btmgmt submenu From: Michael Sun To: linux-bluetooth@vger.kernel.org Cc: mcchou@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, luiz.von.dentz@intel.com, alainm@chromium.org, Michael Sun Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This patch introduces a new btmgmt command ‘remove’ wihtin "monitor" submenu to allow users to remove previously added filters along with a event handler to handle kernel issued MGMT_EV_ADV_MONITOR_REMOVED event. The command will work with the new MGMT opcode MGMT_OP_REMOVE_ADV_MONITOR. Reviewed-by: Alain Michaud Reviewed-by: Miao-chen Chou Signed-off-by: Michael Sun --- Changes in v3: None Changes in v2: - Move remove command into submenu and fix build warnings tools/btmgmt.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tools/btmgmt.c b/tools/btmgmt.c index a0487d4de178..5a70e9e1c3e9 100644 --- a/tools/btmgmt.c +++ b/tools/btmgmt.c @@ -1013,6 +1013,19 @@ static void advertising_removed(uint16_t index, uint16_t len, print("hci%u advertising_removed: instance %u", index, ev->instance); } +static void advmon_removed(uint16_t index, uint16_t len, const void *param, + void *user_data) +{ + const struct mgmt_ev_adv_monitor_removed *ev = param; + + if (len < sizeof(*ev)) { + error("Too small (%u bytes) %s event", len, __func__); + return; + } + + print("hci%u %s: handle %u", index, __func__, ev->monitor_handle); +} + static void version_rsp(uint8_t status, uint16_t len, const void *param, void *user_data) { @@ -4644,6 +4657,44 @@ static void cmd_advmon_features(int argc, char **argv) } } +static void advmon_remove_rsp(uint8_t status, uint16_t len, const void *param, + void *user_data) +{ + const struct mgmt_rp_remove_adv_monitor *rp = param; + + if (status != MGMT_STATUS_SUCCESS) { + error("Could not remove advertisement monitor with status " + "0x%02x (%s)", status, mgmt_errstr(status)); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + print("Advertisement monitor with handle: 0x%04x removed", + rp->monitor_handle); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); +} + +static void cmd_advmon_remove(int argc, char **argv) +{ + struct mgmt_cp_remove_adv_monitor cp; + uint16_t index, monitor_handle; + + index = mgmt_index; + if (index == MGMT_INDEX_NONE) + index = 0; + + if (sscanf(argv[1], "%hx", &monitor_handle) != 1) { + error("Wrong formatted handle argument"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + cp.monitor_handle = monitor_handle; + if (mgmt_send(mgmt, MGMT_OP_REMOVE_ADV_MONITOR, index, sizeof(cp), &cp, + advmon_remove_rsp, NULL, NULL) == 0) { + error("Unable to send appearance cmd"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } +} + static void register_mgmt_callbacks(struct mgmt *mgmt, uint16_t index) { mgmt_register(mgmt, MGMT_EV_CONTROLLER_ERROR, index, controller_error, @@ -4696,6 +4747,8 @@ static void register_mgmt_callbacks(struct mgmt *mgmt, uint16_t index) advertising_added, NULL, NULL); mgmt_register(mgmt, MGMT_EV_ADVERTISING_REMOVED, index, advertising_removed, NULL, NULL); + mgmt_register(mgmt, MGMT_EV_ADV_MONITOR_REMOVED, index, advmon_removed, + NULL, NULL); } static void cmd_select(int argc, char **argv) @@ -4719,6 +4772,8 @@ static const struct bt_shell_menu monitor_menu = { { "features", NULL, cmd_advmon_features, "Show advertisement monitor " "features" }, + { "remove", "", + cmd_advmon_remove, "Remove advertisement monitor " }, { } }, }; From patchwork Wed Jun 17 07:55:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Sun X-Patchwork-Id: 11609345 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 7A8C460D for ; Wed, 17 Jun 2020 07:55:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5ECB721508 for ; Wed, 17 Jun 2020 07:55:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oeGUhcKd" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726134AbgFQHzr (ORCPT ); Wed, 17 Jun 2020 03:55:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725894AbgFQHzp (ORCPT ); Wed, 17 Jun 2020 03:55:45 -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 A11EFC061573 for ; Wed, 17 Jun 2020 00:55:45 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id r17so1576062ybj.22 for ; Wed, 17 Jun 2020 00:55:45 -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:content-transfer-encoding; bh=AHug7416TrGAN+DxUi/X/EFoDospBelapopGrAcTGls=; b=oeGUhcKdwtkA2Ait2uyV15TDLHTnqC+pxZVXO/POSYMRy0nH+mk4bHbKZuOR2lrQVl aYRLvJqfa1YY8pBlTtKimAkY0RM19cI5Ti835XG1ZA3KHWfb/ZY/v8PrmKQW4AVSGKoI Rh7+NMol+oXKGDJi5X/dhrXr8iHkmMT4OW/ZTMiDDKhAe25mxNSXziALCE6KePBUenSX i7j8rGl/gBHP9s/v+NvHZYwlbBSuvAYNijwqb1vvRfijk63vwTK1brZypjJYQ+auyu0S f1MOb6lwsujP7ippWeS2Uy0d/25CNXkxCzWdqn1GAJi+GxRsod6lEj1SCqDDPYYQEc4p gFNA== 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:content-transfer-encoding; bh=AHug7416TrGAN+DxUi/X/EFoDospBelapopGrAcTGls=; b=EHaXlQoq2W8eDk/Peqj/8KLvFbdn3K7DElRYYYT6MdAg20YivxDVfZPSVHVrpWpaF2 7gEZqNKo4h1Kx+sT+9DpqSJOi4yALRbJvvFaYoOz0P8obvTm/B9v8UUMRT/HBhInxBDF lPM7KL57DMhR2/wZWkUkhOzzEsF2s4b322UEbjIjO36nMB+IoyEHVYakQVYDk7kiQAkf 8DuEzqqY7CxK7BAV26wR9+GhZsixZDksox0VMghmdSJwaHqujR/KmNQ404JUCgLAatB0 KDPn4PHNrw4EOFuvgCTM1UoCRTqUTPY9FYY9gBPFi0+pZWrZJI/VNaPPDhGRq6yhCyd1 aivA== X-Gm-Message-State: AOAM533TjqD5D96s3c3Wj4W3rWQiwZFFfFK7KP2/of0qkXkNGuwjAwKT d82tgZa+gWau2pUqLG/HAfeyPdvR3IZZQskZr0G+Eo3zM9kezylx4Uw+cyI2ou8W4CcBCKUGnJM s/TC9GlYltJ3Gpg9RNwkmzYo8QsfdPxTmhNIE6TprGgb6hSL59/8ESCw7TZ018sx6f6sr/7KcM4 9xy2RonW2NsL4= X-Google-Smtp-Source: ABdhPJw/Cvm7nFrB1civXfysyEwJsAXJHWQ1dcwURTplo/trq8v6saZmwmwC4vMSMOi797Rn6OwJoXHZpLsRJBK4uA== X-Received: by 2002:a25:b88d:: with SMTP id w13mr10576465ybj.520.1592380544789; Wed, 17 Jun 2020 00:55:44 -0700 (PDT) Date: Wed, 17 Jun 2020 00:55:36 -0700 In-Reply-To: <20200617075536.98672-1-michaelfsun@google.com> Message-Id: <20200617005531.bluez.v3.3.I55df963e4055bf1778db6f9e46f166b88472e051@changeid> Mime-Version: 1.0 References: <20200617075536.98672-1-michaelfsun@google.com> X-Mailer: git-send-email 2.27.0.290.gba653c62da-goog Subject: [bluez PATCH v3 3/3] btmgmt: Add command "add" into "monitor" btmgmt submenu From: Michael Sun To: linux-bluetooth@vger.kernel.org Cc: mcchou@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, luiz.von.dentz@intel.com, alainm@chromium.org, Michael Sun Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This patch introduces a new command ‘add’ within "monitor" submenu to allow users to add advertisement monitor filters and get back monitor handlers. An event handler is also added to handle kernel issued MGMT_EV_ADV_MONITOR_ADDED events. The command will work with the new MGMT opcode MGMT_OP_ADD_ADV_MONITOR. This patch only adds support for adding pattern based filters. Reviewed-by: Alain Michaud Reviewed-by: Miao-chen Chou Signed-off-by: Michael Sun --- Changes in v3: - Fix build errors Changes in v2: - Move add command into submenu and fix build warnings tools/btmgmt.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 1 deletion(-) diff --git a/tools/btmgmt.c b/tools/btmgmt.c index 5a70e9e1c3e9..ce2a198b1b29 100644 --- a/tools/btmgmt.c +++ b/tools/btmgmt.c @@ -1013,6 +1013,19 @@ static void advertising_removed(uint16_t index, uint16_t len, print("hci%u advertising_removed: instance %u", index, ev->instance); } +static void advmon_added(uint16_t index, uint16_t len, const void *param, + void *user_data) +{ + const struct mgmt_ev_adv_monitor_added *ev = param; + + if (len < sizeof(*ev)) { + error("Too small (%u bytes) %s event", len, __func__); + return; + } + + print("hci%u %s: handle %u", index, __func__, ev->monitor_handle); +} + static void advmon_removed(uint16_t index, uint16_t len, const void *param, void *user_data) { @@ -4587,7 +4600,7 @@ static const char * const advmon_features_str[] = { static const char *advmon_features2str(uint32_t features) { static char str[512]; - int off, i; + unsigned int off, i; off = 0; snprintf(str, sizeof(str), "\n\tNone"); @@ -4657,6 +4670,148 @@ static void cmd_advmon_features(int argc, char **argv) } } +static void advmon_add_rsp(uint8_t status, uint16_t len, const void *param, + void *user_data) +{ + const struct mgmt_rp_add_adv_patterns_monitor *rp = param; + + if (status != MGMT_STATUS_SUCCESS) { + error("Could not add advertisement monitor with status " + "0x%02x (%s)", status, mgmt_errstr(status)); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + print("Advertisement monitor with handle:0x%04x added", + rp->monitor_handle); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); +} + +static bool str2pattern(struct mgmt_adv_pattern *pattern, const char *str) +{ + int type_len, offset_len, offset_end_pos, str_len; + int i, j; + char pattern_str[62] = { 0 }; + char tmp; + + if (sscanf(str, "%2hhx%n:%2hhx%n:%s", &pattern->ad_type, &type_len, + &pattern->offset, &offset_end_pos, pattern_str) != 3) + return false; + + offset_len = offset_end_pos - type_len - 1; + str_len = strlen(pattern_str); + pattern->length = str_len / 2 + str_len % 2; + + if (type_len > 2 || offset_len > 2 || + pattern->offset + pattern->length > 31) + return false; + + for (i = 0, j = 0; i < str_len; i++, j++) { + if (sscanf(&pattern_str[i++], "%2hhx", &pattern->value[j]) + != 1) + return false; + if (i < str_len && sscanf(&pattern_str[i], "%1hhx", &tmp) != 1) + return false; + } + + return true; +} + +static void advmon_add_usage(void) +{ + bt_shell_usage(); + print("Options:\n" + "\t-P, --pattern " + "Advertising data bytes\n" + "Monitor Types:\n" + "\t-p, --pattern-monitor " + "Pattern Monitor\n" + "e.g.:\n" + "\tadd -P 0:1:c504 -P 1:a:9a55beef -p"); +} + +static struct option advmon_add_options[] = { + { "help", 0, 0, 'h' }, + { "pattern-monitor", 0, 0, 'p' }, + { "pattern", 1, 0, 'P' }, + { 0, 0, 0, 0 } }; + +static void cmd_advmon_add(int argc, char **argv) +{ + + uint16_t index; + void *cp = NULL; + struct mgmt_adv_pattern *patterns = NULL; + int opt, patterns_len; + int pattern_count = 0, cp_len = 0; + bool success = false, type_selected = false; + + index = mgmt_index; + if (index == MGMT_INDEX_NONE) + index = 0; + + while ((opt = getopt_long(argc, argv, "P:ph", advmon_add_options, + NULL)) != -1) { + switch (opt) { + case 'P': + patterns_len = (pattern_count + 1) * + sizeof(struct mgmt_adv_pattern); + patterns = realloc(patterns, patterns_len); + + if (!str2pattern(&patterns[pattern_count++], optarg)) { + error("Failed to parse monitor patterns."); + goto done; + } + break; + case 'p': + if (!pattern_count) { + advmon_add_usage(); + goto done; + } + cp_len = + sizeof(struct mgmt_cp_add_adv_monitor) + + patterns_len; + cp = realloc(cp, cp_len); + + ((struct mgmt_cp_add_adv_monitor *)cp) + ->pattern_count = pattern_count; + + memcpy(((struct mgmt_cp_add_adv_monitor *)cp) + ->patterns, patterns, patterns_len); + type_selected = true; + break; + case 'h': + success = true; + /* fall through */ + default: + advmon_add_usage(); + goto done; + } + } + + argc -= optind; + argv += optind; + + if (argc || !type_selected) { + advmon_add_usage(); + goto done; + } + + if (!mgmt_send(mgmt, MGMT_OP_ADD_ADV_PATTERNS_MONITOR, index, cp_len, + cp, advmon_add_rsp, NULL, NULL)) { + error("Unable to send \"Add Advertising Monitor\" command"); + goto done; + } + + success = true; + +done: + optind = 0; + free(patterns); + free(cp); + if (!success) + bt_shell_noninteractive_quit(EXIT_FAILURE); +} + static void advmon_remove_rsp(uint8_t status, uint16_t len, const void *param, void *user_data) { @@ -4747,6 +4902,8 @@ static void register_mgmt_callbacks(struct mgmt *mgmt, uint16_t index) advertising_added, NULL, NULL); mgmt_register(mgmt, MGMT_EV_ADVERTISING_REMOVED, index, advertising_removed, NULL, NULL); + mgmt_register(mgmt, MGMT_EV_ADV_MONITOR_ADDED, index, advmon_added, + NULL, NULL); mgmt_register(mgmt, MGMT_EV_ADV_MONITOR_REMOVED, index, advmon_removed, NULL, NULL); } @@ -4774,6 +4931,8 @@ static const struct bt_shell_menu monitor_menu = { "features" }, { "remove", "", cmd_advmon_remove, "Remove advertisement monitor " }, + { "add", "[options] <-p|-h>", + cmd_advmon_add, "Add advertisement monitor" }, { } }, };