From patchwork Wed Jun 17 06:26:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Sun X-Patchwork-Id: 11609111 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 33F2490 for ; Wed, 17 Jun 2020 06:26:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1B474208E4 for ; Wed, 17 Jun 2020 06:26:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LBZBHrTK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726845AbgFQG0w (ORCPT ); Wed, 17 Jun 2020 02:26:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726497AbgFQG0w (ORCPT ); Wed, 17 Jun 2020 02:26:52 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D917C061573 for ; Tue, 16 Jun 2020 23:26:52 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id p7so1037712qkf.21 for ; Tue, 16 Jun 2020 23:26:52 -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=GcwHGX7RGWUKJO3An6LOhwQ6jtAqhn84pbCpNrhGuIw=; b=LBZBHrTK66Dm+WO7c869NlobEzRClR0CQr56XuhSNl3Fi3gqNfBGyg8jA2HyL2ehIX yhX9fAcfy8eSpN9fvTU5s8z7Ia2zgqxuim8VXus+ot3MFkGctlZGZ1UxFH80FgLu4ZIF c9eKERI/nmSPoFTEd/nqeDfAnquMUz5eAmH+gdlPp9HDC1kcwDRRaws9rwOiBU3RkMWY QIn0IJTrxKRDdpOyp/6bEJspi2anXFkAhh3lfcM41O0UAtm+8k/Q/A45QWXwQ0vTHBoG C99yh618i6znSZq4vm59tni0b6pbDLL/3+Y8wgc5u+z+Sv6Izlm4/Lu9IZ7oZMCIPXbc pQxA== 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=GcwHGX7RGWUKJO3An6LOhwQ6jtAqhn84pbCpNrhGuIw=; b=TsY7RMmxgTmrSEQ/Pd+5918NzlyCIAnZ7uciozYr/vDZsso3jo6YvDrB8dgLQatf0m E9v/eW+WQr5jXpFMcDy7fPkyvbsvCSlMRjWx1krXfm9tmyZQhkwdlFz08XDpE/xRw/Cz sRgW4MckosoMNQHoioFdGtC4xlRDqpTWMtBBhw39g+mvclMGwa3ezkhgjdIHr7IbfPbP ro6m+lUAZAO9TgjZz0FMiKv9sjZX5BF8Ir+BVEsFtHa1C7DWHPWtkVVc4MNREzOn91bK yfg1qRgwhs7fX+Pqd/s17ep+wI4/5Mr8LdZ8wU6co0lO21yvKg93qviahhji4BA1m6tj NNTw== X-Gm-Message-State: AOAM5328TIrjTCH8IqBtiMFBK2/CJ8+I2XJgVHKUZurwxgSG9nciHwVe g6qKLPiaPM6gJfNzXRhrevK78q4/oP8aDH/nWxcpn/QVMgmdAkjY832uNuAdQRA9Tc6GyLJCe+i bZd/S4RYxE/VcLtyyqbSB53GRkYyqg6Xf3+bpVLKj7b/9fS9l+RWgnQuBwo1hWFlaC02jjrCd0g Rc/JKfOUoAVG8= X-Google-Smtp-Source: ABdhPJytL4NA3gwdJh/kPJqAQItz7sQYO3UTF44JXLX26mEo8zoWt2TYmGBDRyYRuo5ufe01yWxSSN0fg7P3imuN/Q== X-Received: by 2002:a0c:f84c:: with SMTP id g12mr5955486qvo.31.1592375211270; Tue, 16 Jun 2020 23:26:51 -0700 (PDT) Date: Tue, 16 Jun 2020 23:26:44 -0700 In-Reply-To: <20200617062646.165307-1-michaelfsun@google.com> Message-Id: <20200616232642.bluez.v2.1.Id1b811687f1245eca676a6c5fae4fd13a70c7732@changeid> Mime-Version: 1.0 References: <20200617062646.165307-1-michaelfsun@google.com> X-Mailer: git-send-email 2.27.0.290.gba653c62da-goog Subject: [bluez PATCH v2 1/3] btmgmt: Add "monitor" btmgmt submenu and command "features" From: Michael Sun To: linux-bluetooth@vger.kernel.org Cc: alainm@chromium.org, mcchou@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, luiz.von.dentz@intel.com, 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 v2: - Move advertisement monitor commands into monitor submenu. - Fixing 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 06:26:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Sun X-Patchwork-Id: 11609113 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 593D11392 for ; Wed, 17 Jun 2020 06:26:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 439C7208D5 for ; Wed, 17 Jun 2020 06:26:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ehyPmaAb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726853AbgFQG0z (ORCPT ); Wed, 17 Jun 2020 02:26:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726497AbgFQG0z (ORCPT ); Wed, 17 Jun 2020 02:26:55 -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 DB599C061573 for ; Tue, 16 Jun 2020 23:26:53 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id a188so1393376ybg.20 for ; Tue, 16 Jun 2020 23:26:53 -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=r9vfWzzZ6gr06ZsUMT6gZTsgrP11R9hvw6rTrzGGJSU=; b=ehyPmaAbJpCT71V+0gLnS5vWSsCS7ra/KHNWyBVk2OML+fXh9IFI4hFiWvTo44FBxQ NYcoqi5/8Ig1SR5dG1fICYMRk2cBCZNPD8J9GZjFuiE54VWHtJeJUaZhra+ux3PsyNLB 14GcgKYDHAkBJGQ2K2Bn+KWnMok39TqN1Ive/g5AAuSqoGIYfAqPP7yfa97DFuLeKhiZ Z7Pvlyt4KlogPbktQKjjyjAAhb2yCY9GIdQGnv3WmQNQHpJO71vPCJgFtWI+BX4CpwbH UZk6jZ/M7Yx5I8VJcbPUHwPoh/jAKqCue20cD9aqbkDN0bzLPnaEkYK8sKUYwUrPPf6R g9tA== 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=r9vfWzzZ6gr06ZsUMT6gZTsgrP11R9hvw6rTrzGGJSU=; b=lUxf19Q5qMbwf5Rr6DS3V1NgPeztOJEeY6b8oidCqxqz/b3M+7w9HOLxKUEiKx2Fpq +/ng+mkXgz5AgNmiBFfZz5nhdT+gGVfal7gMmFuuIRCr8AK0r3DiavbLtAXySWhT28us sWg7+1bac3ztoU4tkh4f1DZfLrSfmKqqpZRqPCvRIYr/TZlyboDzfdBM1+K5jlR/dVEW wX2MKxlKKB+EqVABBXPTopGiFA/7M5snfzOCBNsXCRfB34YM9SZMBbSAnqN8uleIKI0r 3FDAe2FrKHtAbzMcFquwWlCUzAx7aHRUiIZHcRymnxiYoiDoyx/oZiqPj+0l4OgvncnF ZPZA== X-Gm-Message-State: AOAM533jVXRmzKXiIImwX+gAPW8TzTNJA/CrRBQ8NuxEVzL1Kp8pAJPE tGQ+IBHHRziPfDfaY9A91dmTnhIa0xcrQEUCZLLKeJiBkoap+HvFUxvdwlg343RNVCv+2Oc35LQ 9KYvMGHFUxUAHn0Wi9NumZtvNFMSb7OXSmNW6SbJDbLiCl9ZBiStxRo4aBcoTvkcogiPHPsZrrZ jON1em1kej3c0= X-Google-Smtp-Source: ABdhPJxR57AJd7w0+k5McT5G+wzAR+012Vl+zDWA8h448S6IVvCL3jB5TwBo0GO9mouMAySZWMDBnRqx8wJqL6Uexg== X-Received: by 2002:a25:dfcc:: with SMTP id w195mr10735032ybg.372.1592375213053; Tue, 16 Jun 2020 23:26:53 -0700 (PDT) Date: Tue, 16 Jun 2020 23:26:45 -0700 In-Reply-To: <20200617062646.165307-1-michaelfsun@google.com> Message-Id: <20200616232642.bluez.v2.2.I6847f3a5339713811d88b3a234e6454e11f0f042@changeid> Mime-Version: 1.0 References: <20200617062646.165307-1-michaelfsun@google.com> X-Mailer: git-send-email 2.27.0.290.gba653c62da-goog Subject: [bluez PATCH v2 2/3] btmgmt: Add command "remove" into "monitor" btmgmt submenu From: Michael Sun To: linux-bluetooth@vger.kernel.org Cc: alainm@chromium.org, mcchou@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, luiz.von.dentz@intel.com, 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 v2: None 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 06:26:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Sun X-Patchwork-Id: 11609115 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 A27641392 for ; Wed, 17 Jun 2020 06:26:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 84707208D5 for ; Wed, 17 Jun 2020 06:26:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TZhLlwWO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726858AbgFQG05 (ORCPT ); Wed, 17 Jun 2020 02:26:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726497AbgFQG04 (ORCPT ); Wed, 17 Jun 2020 02:26:56 -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 D6172C061573 for ; Tue, 16 Jun 2020 23:26:55 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id e82so1423086ybh.12 for ; Tue, 16 Jun 2020 23:26:55 -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=SoYlWLI+33p19wNmYw7plTycfez+rmjmJaOynsuUNes=; b=TZhLlwWO1LDE91onGjzLhhPOYaIX08XNk+NAXAjNeOHQ6AmDkz+MKeId5cPxMv3fDt PGuf5fBUB0Hy1nvmCi2djt/4yX9+vjN7idImWpeZ1JmU/dYwodpYb9El+w53YNaP5WBV 1GdlBtVHT8zaQClicocxpHptALufP+PprbXICKbIIbpwrgO+LYN2EZg+7TrIPptdvPIl Ay/VY+/sjTtd7yRA2+66r1wW11sWeCwtJEhdcp3wctWZH0P0No58KVfJHgg+W6nrKLTq reNItVM6oWv+vWijbjJaVLVI4sHK6rZh3R6yd7pW9CFvCpEix1WXEQChDhGJ+00I1RcJ jZOQ== 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=SoYlWLI+33p19wNmYw7plTycfez+rmjmJaOynsuUNes=; b=uL8xrK6jzhJ/du7FRVsQFnB3tgSqQHs1Ke4BnKaEjt276jnkvb33WL/uWy3iBLZ28O xMCJ/IseCygBGbzd8LWjIdgyMS1PxU4mFKt9GClbOTyok6AZVmpqoa1E0P7lmSQNR/0E dMTdCMWUiSD6XYCQM+STaRdhUff2FgKzTf4hVSyFnZnRtUb1ByYHdB+f7Og0tun4Kp2J Wwj8RROTWqNcmbZez+uGMh6VkfyHUe1tvxatxCTLvKVRLYswBapo9EiG6QmI8NgwPK67 RM/lE8T6ZChojq1i52mNAiWGMZgvOGJLPBjOLqvXSLzy8YW9zhMbRgNh5VGVO+tAeNc3 LjWg== X-Gm-Message-State: AOAM533mmmyN+l01TjjdGyFGCyNuuDAisaQ1GdJEU/gWALkuWihFc+wz iJQJPB5RcY7jXGBRimq8j4AfKwEpDCJ4bx3tFBz5EPswgSIFfsZyoj1GgncckIAojNKjZet1nHn 7h9zZtkeiI9cmDHLFg3GKsmz5IC4u++nRHSTSotSx4bOCv7D422es8lsP0L9o7CDDUrXVNZy6B5 BY2Qyq4l0KOcQ= X-Google-Smtp-Source: ABdhPJx30w8wThqD1dT0adDxA9vrSz19+Xh+bTcViw1YgPy1/I4Wrnz2yogieqOtHpGgP8fpWmP6ZXh+Dtx5juINyw== X-Received: by 2002:a25:2604:: with SMTP id m4mr9945084ybm.470.1592375214994; Tue, 16 Jun 2020 23:26:54 -0700 (PDT) Date: Tue, 16 Jun 2020 23:26:46 -0700 In-Reply-To: <20200617062646.165307-1-michaelfsun@google.com> Message-Id: <20200616232642.bluez.v2.3.I29854cfe9ccae25c34666856326c07cd680151b1@changeid> Mime-Version: 1.0 References: <20200617062646.165307-1-michaelfsun@google.com> X-Mailer: git-send-email 2.27.0.290.gba653c62da-goog Subject: [bluez PATCH v2 3/3] btmgmt: Add command "add" into "monitor" btmgmt submenu From: Michael Sun To: linux-bluetooth@vger.kernel.org Cc: alainm@chromium.org, mcchou@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, luiz.von.dentz@intel.com, 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 v2: None tools/btmgmt.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) diff --git a/tools/btmgmt.c b/tools/btmgmt.c index 5a70e9e1c3e9..22d59ccb3011 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) { @@ -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, length, 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, i; + int pattern_count = 0, patterns_len, cp_len; + 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" }, { } }, };