This is an automated email from the ASF dual-hosted git repository.

janc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git


The following commit(s) were added to refs/heads/master by this push:
     new 3c561218 apps/btshell: Add scan filtering based on device name
3c561218 is described below

commit 3c561218daacf06c30d0a726978c4105ac2c3a22
Author: Szymon Janc <szymon.j...@codecoup.pl>
AuthorDate: Thu Apr 14 13:55:39 2022 +0200

    apps/btshell: Add scan filtering based on device name
    
    This allows to specify name prefix that is used to filter out devices
    when scanning. Useful in busy environment.
---
 apps/btshell/src/btshell.h |  4 ++++
 apps/btshell/src/cmd.c     | 14 ++++++++++++++
 apps/btshell/src/main.c    | 22 ++++++++++++++++++++++
 3 files changed, 40 insertions(+)

diff --git a/apps/btshell/src/btshell.h b/apps/btshell/src/btshell.h
index 7c978221..f52d65f5 100644
--- a/apps/btshell/src/btshell.h
+++ b/apps/btshell/src/btshell.h
@@ -83,10 +83,14 @@ struct btshell_conn {
     struct btshell_l2cap_coc_list coc_list;
 };
 
+#define NAME_FILTER_LEN_MAX 20
+
 struct btshell_scan_opts {
     uint16_t limit;
     uint8_t ignore_legacy:1;
     uint8_t periodic_only:1;
+    uint8_t name_filter_len;
+    char name_filter[NAME_FILTER_LEN_MAX];
 };
 
 extern struct btshell_conn btshell_conns[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
diff --git a/apps/btshell/src/cmd.c b/apps/btshell/src/cmd.c
index 69a3095a..ddb76c82 100644
--- a/apps/btshell/src/cmd.c
+++ b/apps/btshell/src/cmd.c
@@ -1107,11 +1107,14 @@ static const struct shell_cmd_help disconnect_help = {
 static struct btshell_scan_opts g_scan_opts = {
         .limit = UINT16_MAX,
         .ignore_legacy = 0,
+        .periodic_only = 0,
+        .name_filter_len = 0,
 };
 
 static int
 cmd_set_scan_opts(int argc, char **argv)
 {
+    char *name_filter;
     int rc;
 
     rc = parse_arg_all(argc - 1, argv + 1);
@@ -1137,6 +1140,16 @@ cmd_set_scan_opts(int argc, char **argv)
         return rc;
     }
 
+    name_filter = parse_arg_extract("name_filter");
+    if (name_filter) {
+        strncpy(g_scan_opts.name_filter, name_filter, NAME_FILTER_LEN_MAX);
+        g_scan_opts.name_filter[NAME_FILTER_LEN_MAX - 1] = '\0';
+    } else {
+        g_scan_opts.name_filter[0] = '\0';
+    }
+
+    g_scan_opts.name_filter_len = strlen(g_scan_opts.name_filter);
+
     return rc;
 }
 
@@ -1145,6 +1158,7 @@ static const struct shell_param set_scan_opts_params[] = {
     {"decode_limit", "usage: =[0-UINT16_MAX], default: UINT16_MAX"},
     {"ignore_legacy", "usage: =[0-1], default: 0"},
     {"periodic_only", "usage: =[0-1], default: 0"},
+    {"name_filter", "usage: =name, default: {none}"},
     {NULL, NULL}
 };
 
diff --git a/apps/btshell/src/main.c b/apps/btshell/src/main.c
index e6f2f565..cc69e5d1 100644
--- a/apps/btshell/src/main.c
+++ b/apps/btshell/src/main.c
@@ -963,9 +963,31 @@ btshell_decode_adv_data(const uint8_t *adv_data, uint8_t 
adv_data_len, void *arg
 static void
 btshell_decode_event_type(struct ble_gap_ext_disc_desc *desc, void *arg)
 {
+    const struct ble_hs_adv_field *ad_name = NULL;
     struct btshell_scan_opts *scan_opts = arg;
     uint8_t directed = 0;
 
+    if (scan_opts && scan_opts->name_filter_len) {
+        if (ble_hs_adv_find_field(BLE_HS_ADV_TYPE_COMP_NAME, desc->data,
+                                  desc->length_data, &ad_name)) {
+            ble_hs_adv_find_field(BLE_HS_ADV_TYPE_INCOMP_NAME, desc->data,
+                                  desc->length_data, &ad_name);
+        }
+
+        if (!ad_name) {
+            return;
+        }
+
+        if (ad_name->length < scan_opts->name_filter_len) {
+            return;
+        }
+
+        if (strncasecmp(scan_opts->name_filter, (const char *)ad_name->value,
+                        scan_opts->name_filter_len)) {
+            return;
+        }
+    }
+
     if (desc->props & BLE_HCI_ADV_LEGACY_MASK) {
         if (scan_opts && scan_opts->ignore_legacy) {
             return;

Reply via email to