btmgmt: Add command "remove" into "monitor" btmgmt submenu
authorMichael Sun <michaelfsun@google.com>
Fri, 19 Jun 2020 22:56:22 +0000 (15:56 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 12 Apr 2021 09:00:49 +0000 (14:30 +0530)
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)

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
tools/btmgmt.c

index 2ed44f4..8ccf4b6 100755 (executable)
@@ -1012,6 +1012,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)
 {
@@ -4645,6 +4659,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,
@@ -4697,6 +4749,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)
@@ -4720,6 +4774,8 @@ static const struct bt_shell_menu monitor_menu = {
        { "features",           NULL,
                cmd_advmon_features,    "Show advertisement monitor "
                                        "features"                      },
+       { "remove",             "<handle>",
+               cmd_advmon_remove,      "Remove advertisement monitor " },
        { } },
 };