From patchwork Fri Jun 19 22:56:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Sun X-Patchwork-Id: 11615301 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 43E7E913 for ; Fri, 19 Jun 2020 22:56:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2BC9E223FB for ; Fri, 19 Jun 2020 22:56:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aCfziuW5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729883AbgFSW4h (ORCPT ); Fri, 19 Jun 2020 18:56:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729852AbgFSW4h (ORCPT ); Fri, 19 Jun 2020 18:56:37 -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 25382C06174E for ; Fri, 19 Jun 2020 15:56:37 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id l19so8294485qtp.12 for ; Fri, 19 Jun 2020 15:56:37 -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=roAyZpjVSESurkaK6RP9hHgfsiB7rFjIxv3cT1jDzMg=; b=aCfziuW590Vw5kWK+wketdQr5Z+o2Cm9O2UiG1XhEUyDmNstt0Y+0p1UCjsmNC5/sn P1Cp0Vd54wqoBtAq2z0/rEtOSz3G+5n8amvNOlwPKyFCaXCwtKZWx9/Y86lqrGAM4RmX I5l9bsvAdneovsW7JJ09/mNf0KbPjcjR2KRBNwTFywJ4w1nT76iJvLJOk8lCsocTfH+D aeKeqHJge/hHd4rOYAmZQRdxKOQkt2MPJBmU7s0I9f4lEuoS3nLK5Ivki7efptXRl61X odhhEDZrhfddPRuuxwZv0wk0WTnASWv9mkkIZBQ7rgv67QkwABzvVf0wyiVonA+Fz3YF Ghuw== 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=roAyZpjVSESurkaK6RP9hHgfsiB7rFjIxv3cT1jDzMg=; b=bpZBu4+C8AOzJbXEtQqXXmRjKUqB+IpmvnbxP6QQW8sHidG8CQCci7cLbLBvZe/AjU fozazZTgCrnK3k9k2ZLWrsmojWOtLMwfV5Yydql3LxgBwlrqU/Lc1k0xOVI4mykKXlGl 5dLsqpbl2Ez1YRPl1bZg4umT59XCdQM3pSzHTkNgLQ2ViX008+K4eq5Sdb7EClcZnTqg YnVgmODEQSNn72Bu0m1Om8wB8zx67DwRgOkwA6oJsALzZfG/y7IM0EYbuEE81EJy1IRA FPH132awtamv2aCzxQSjos8CDc91kYuDeGU+lUU3XJqCBAQCJiZj4vhhCF5TneTa2Rcx CJmw== X-Gm-Message-State: AOAM532XMEU+lgHkWWDvZOnklEDViRi9GeLtYd93eXka+SanyNisGDao TP+VNS27M43tWsaFnHXag1SzmrF/ba6VB2h3ZjiQ5XyN4T6EL71qYXEFwSk+NZ5txSAUzZUGKw7 M3iUbUeqn6v3Alb9poLxqrBaMOczeAJVv2V6RZezWGugVETI3MmX8FLn9Jd8RgGKfGMPf8nqrA6 wH49PzzSA3M8o= X-Google-Smtp-Source: ABdhPJy2bytPUwEqwWeMfLZE00f/RAmHZ5fZkYcyxm/rLyJFUVLl0VIjzTbsw1+MwUYgVwj6qAbXmxxejwf9+q641w== X-Received: by 2002:ad4:556e:: with SMTP id w14mr11073069qvy.137.1592607396161; Fri, 19 Jun 2020 15:56:36 -0700 (PDT) Date: Fri, 19 Jun 2020 15:56:21 -0700 In-Reply-To: <20200619225623.10200-1-michaelfsun@google.com> Message-Id: <20200619155612.bluez.v4.1.Ia9d7e5ca8da3bb6e1356b2b0e6fa18fafa022af0@changeid> Mime-Version: 1.0 References: <20200619225623.10200-1-michaelfsun@google.com> X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [bluez PATCH v4 1/3] btmgmt: Add "monitor" btmgmt submenu and command "features" From: Michael Sun To: linux-bluetooth@vger.kernel.org Cc: luiz.von.dentz@intel.com, mcchou@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, 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. Tested on Atlas Chromebook with a sample stdout below: [mgmt]# features Supported features: None Enabled features: None Max number of handles: 32 Max number of patterns: 16 Handles list with 2 items: 0x0001 0x0002 Reviewed-by: Miao-chen Chou Signed-off-by: Michael Sun --- Changes in v4: None 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..f50d29651346 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]; + unsigned 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); + } + + supported_features = le32_to_cpu(rp->supported_features); + enabled_features = le32_to_cpu(rp->enabled_features); + num_handles = le16_to_cpu(rp->num_handles); + + if (len < sizeof(*rp) + num_handles * sizeof(uint16_t)) { + error("Handles count (%u) doesn't match reply length (%u)", + num_handles, len); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + 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 Fri Jun 19 22:56:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Sun X-Patchwork-Id: 11615303 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 18F38913 for ; Fri, 19 Jun 2020 22:56:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0158122400 for ; Fri, 19 Jun 2020 22:56:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hMfnMWNM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729887AbgFSW4l (ORCPT ); Fri, 19 Jun 2020 18:56:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729852AbgFSW4k (ORCPT ); Fri, 19 Jun 2020 18:56:40 -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 C789DC06174E for ; Fri, 19 Jun 2020 15:56:40 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id c17so11721197ybf.7 for ; Fri, 19 Jun 2020 15:56:40 -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=A1e11u7HNsaVCh/raWH9GxHfgJx+PWPc1jfYmJ1V0lU=; b=hMfnMWNMn27o2KaghyB1Z3B5n6jw2ENw+Typiktn6Ixmu//Ez7uOwtzHX1z0GISlmD DLlS/BDIY4FUxftFHmY+Cbu4L7Aae+E0ZPr574JkhZJAtJGR4H5v8zX0tYnQLApbWr3o OeezGvsIqaHgF5pmMsB8HrWPRNRaEUBg2jlRuzjfXRRCtmRxqyGf6KSfgoV0TIZGghGd eEqTkhpqRJ5Ls31Qho1DtS0tgrL0fwcg1oKqUCZsOKMljQs712bD2iHmVHM7dUYX4v1Y FzVh699kkGBpHQhyuLod2CXgX1q+oQ2K9YGeGzXL9ZlL7weoBs70qT/C87dlz9l67h5l d3FQ== 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=A1e11u7HNsaVCh/raWH9GxHfgJx+PWPc1jfYmJ1V0lU=; b=OrJiuN2W1M9G+6Bt7umZTnNt1YqwEsRg5mjV16iXdFL0IklZP5cLcqLp2XFZLrBHHV Eywyde7ZhlvE7OhzZp9bdWkWjjRvO7iS3tniqQZ7S16geqvhUt4hDWZx7WN0MM3RLyCy iRYFoD4hFk/bPlvE2Axd5HhINjPUksR3/NsGCL5+Yus27uWo4tyq9uwPIVQPmEz1mZwb shldMDI8mdK+igaQzJ4ZxrA4pbvTLFDjUtqZwcLcRz+ndJ9nn6Bn5aNbQeMVdSGw8P74 RdVghawjjFT1PqmaBCT6rnFSAyQEsK8H9SfMNakxEvAjPBeZX4ZDOu7OILe/hHf6bFO4 eC9w== X-Gm-Message-State: AOAM532eeenpZcCwE7imWxrJpqCmem/xKFMIFMhjhaLfE09gK0Whou43 PkJthno9edPOoA7EKsmzCyJkDwta4c5OfX5V4733trsnYmybltNcsdlQ7Ju0cKvOEqx7q7JWdd0 O1h4Dmg00FBXxCISITbUxQOX5pExv7+Q10GXki9HLFqmlG1b5LeF9rFO1THgqCySwIgmoxqWLx+ pBx+eIiVaoVRI= X-Google-Smtp-Source: ABdhPJwTmInh8CdMqD6XeoH2v/heJNj9oPacoqlSfUuDbSBCDk4j+9WD4IsBIoLaL5yhvSFZIhN4FO+SVDqLtG9I7w== X-Received: by 2002:a25:32d1:: with SMTP id y200mr10090665yby.507.1592607400013; Fri, 19 Jun 2020 15:56:40 -0700 (PDT) Date: Fri, 19 Jun 2020 15:56:22 -0700 In-Reply-To: <20200619225623.10200-1-michaelfsun@google.com> Message-Id: <20200619155612.bluez.v4.2.Ib8ae2d78936aa9f16a318aca14cf8c7198449bac@changeid> Mime-Version: 1.0 References: <20200619225623.10200-1-michaelfsun@google.com> X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [bluez PATCH v4 2/3] btmgmt: Add command "remove" into "monitor" btmgmt submenu From: Michael Sun To: linux-bluetooth@vger.kernel.org Cc: luiz.von.dentz@intel.com, mcchou@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, 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. Tested on Atlas Chromebook with a sample stdout below: [mgmt]# remove 1 Advertisement monitor with handle: 0x0001 removed [mgmt]# remove 1234 Could not remove advertisement monitor with status 0x11 (Invalid Index) Reviewed-by: Miao-chen Chou Signed-off-by: Michael Sun --- Changes in v4: - Fix endianness Changes in v3: None Changes in v2: - Move remove command into submenu and fix build warnings tools/btmgmt.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tools/btmgmt.c b/tools/btmgmt.c index f50d29651346..1c46041ccb18 100644 --- a/tools/btmgmt.c +++ b/tools/btmgmt.c @@ -1013,6 +1013,20 @@ 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__, + le16_to_cpu(ev->monitor_handle)); +} + static void version_rsp(uint8_t status, uint16_t len, const void *param, void *user_data) { @@ -4644,6 +4658,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", + le16_to_cpu(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 = cpu_to_le16(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 +4748,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 +4773,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 Fri Jun 19 22:56:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Sun X-Patchwork-Id: 11615305 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 09AFC913 for ; Fri, 19 Jun 2020 22:56:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DFF1A21D7D for ; Fri, 19 Jun 2020 22:56:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="esvSys0Z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729906AbgFSW4n (ORCPT ); Fri, 19 Jun 2020 18:56:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729852AbgFSW4m (ORCPT ); Fri, 19 Jun 2020 18:56:42 -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 C9D70C06174E for ; Fri, 19 Jun 2020 15:56:42 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id f16so11850330ybp.5 for ; Fri, 19 Jun 2020 15:56:42 -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=SXhIo9LNigC0v9REfYKXNQY9BqzfuMrJgk9FRI6OKaY=; b=esvSys0ZYYAsXBYjdomUT2HU6LMKSFWwQsKVzFA3HdQHvH3+DD0eY1iHmhYRUxugVJ L6lRy/SaBg9dXLgzz4hEyfK5aPV6Eeax6vwqHImcv4i/bhsDZ8nAiPJbmtegDlCi7Z/z n65A590w9cak5OTKR4VhfZQtIydGkJarJrxe7nFCOEyTtuzcH7xpoQ2o38rIyVWpyLt6 NH9VDeL1vbSZnGNrtLom+y2eM6IogVZBsYgK0ZPJJzDm0MGLsY9ZLJosIJkXLeZoLB2z dCF7mCdL2wBX0nwcsPOOdYSRHFNVbYxZKoSL3IkYdkKtNMT+PYK45d6tLDfDq9weIXZW nsAA== 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=SXhIo9LNigC0v9REfYKXNQY9BqzfuMrJgk9FRI6OKaY=; b=X/b2YHaNOhbz53Umedy/risY3vkPeDcS89oGefrjMpQCIqiXfjGOM5xv9CtAB9Ly30 IqbhNyHuLkSo89JmKZpc/+9sQ0ARiWsG5koDhum402fposiTcYWE8nYDWMXDAQsWdPd8 Td506EVDbE5thomYEXPFpTVV45k7II5mdfb/jULafkFMvUXJ67a+Gx6Y5cFg+xpnf+eG v6dvJwaEGPbfYUwL3LJv7CuDBDSJjyEtMrU44JjlvspiTGwFYARXgSDiJWeeyYeHPOjw znGF8aonzZmVaM8fgjAA4U3+A0qLvi4hnZaJuQ75/bA43DT40UQH1bCcH0vgyRjTFAAN lCQg== X-Gm-Message-State: AOAM532QwvJXdVB8USSx/M8wv/Q7IUxp7HcCOdnx7jMbWWpND3B5DC7m hOEbegsnJ1yJRkd3diHeLKYzyrRWX6BcllFeumRolp3Jl0BmtVCwC/oqyuhFgF1chjc47kGj1vb b4WA5ALKFmV292FwZXJk/P6mLAc2+4FcRStGQziV2n8GKKOPF7xH8qD3aOvE3G3iZN4PlHy12TA ORq0hX18pi9Og= X-Google-Smtp-Source: ABdhPJznVjr4OQuKL+Vv/Ga/q4TlPBZilzMbW07fCgu1pDppmKIO75blu5Xx8kP4pn8ZBltg8LT3b3d8FnOrJDsZyw== X-Received: by 2002:a25:ca45:: with SMTP id a66mr9966011ybg.164.1592607401939; Fri, 19 Jun 2020 15:56:41 -0700 (PDT) Date: Fri, 19 Jun 2020 15:56:23 -0700 In-Reply-To: <20200619225623.10200-1-michaelfsun@google.com> Message-Id: <20200619155612.bluez.v4.3.I55df963e4055bf1778db6f9e46f166b88472e051@changeid> Mime-Version: 1.0 References: <20200619225623.10200-1-michaelfsun@google.com> X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [bluez PATCH v4 3/3] btmgmt: Add command "add" into "monitor" btmgmt submenu From: Michael Sun To: linux-bluetooth@vger.kernel.org Cc: luiz.von.dentz@intel.com, mcchou@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, 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. Tested on Atlas Chromebook with a sample stdout below: [mgmt]# add -p 0:1:c504 1:a:9a55beef Advertisement monitor with handle:0x0005 added [mgmt]# add -p 0:1:xxxx Failed to parse monitor patterns. Usage: add <-p|-h> [options...] Monitor Types: -p ... Pattern Monitor e.g.: add -p 0:1:c504 1:a:9a55beef Reviewed-by: Miao-chen Chou Signed-off-by: Michael Sun --- Changes in v4: - Use regular options instead of optget options per feedback, and fix endianness Changes in v3: - Fix build errors Changes in v2: - Move add command into submenu and fix build warnings tools/btmgmt.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/tools/btmgmt.c b/tools/btmgmt.c index 1c46041ccb18..48c9e58872f3 100644 --- a/tools/btmgmt.c +++ b/tools/btmgmt.c @@ -1013,6 +1013,20 @@ 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__, + le16_to_cpu(ev->monitor_handle)); +} + static void advmon_removed(uint16_t index, uint16_t len, const void *param, void *user_data) { @@ -4658,6 +4672,112 @@ 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", + le16_to_cpu(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("Monitor Types:\n\t-p ..." + "\tPattern Monitor\ne.g.:\n\tadd -p 0:1:c504 1:a:9a55beef"); +} + +static bool advmon_add_pattern(int argc, char **argv) +{ + uint16_t index; + int i, cp_len; + struct mgmt_cp_add_adv_monitor *cp = NULL; + bool success = false; + + index = mgmt_index; + if (index == MGMT_INDEX_NONE) + index = 0; + + cp_len = sizeof(struct mgmt_cp_add_adv_monitor) + + argc * sizeof(struct mgmt_adv_pattern); + + cp = malloc0(cp_len); + cp->pattern_count = argc; + + for (i = 0; i < argc; i++) { + if (!str2pattern(&cp->patterns[i], argv[i])) { + error("Failed to parse monitor patterns."); + 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: + free(cp); + return success; +} + +static void cmd_advmon_add(int argc, char **argv) +{ + bool success = false; + + if (strcasecmp(argv[1], "-p") == 0 && argc > 2) { + argc -= 2; + argv += 2; + success = advmon_add_pattern(argc, argv); + } + + if (!success) { + advmon_add_usage(); + bt_shell_noninteractive_quit(EXIT_FAILURE); + } +} + static void advmon_remove_rsp(uint8_t status, uint16_t len, const void *param, void *user_data) { @@ -4748,6 +4868,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); } @@ -4775,6 +4897,8 @@ static const struct bt_shell_menu monitor_menu = { "features" }, { "remove", "", cmd_advmon_remove, "Remove advertisement monitor " }, + { "add", "<-p|-h> [options...]", + cmd_advmon_add, "Add advertisement monitor" }, { } }, };