From 13d7e140df6c7e182f9ddf76dcb18f9f5e52d5b2 Mon Sep 17 00:00:00 2001 From: Kevin Jahaziel Leon Morales Date: Fri, 25 Oct 2024 16:01:18 -0600 Subject: [PATCH] feat: ADV scanner --- firmware/README.md | 21 +++++++++++++++++++ firmware/components/ble_scann/ble_scann.c | 8 ++++++- firmware/components/ble_scann/ble_scann.h | 1 + .../apps/ble/adv_scanner/adv_scan_module.c | 18 +++++++++++++++- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/firmware/README.md b/firmware/README.md index 8ca20dc..8121846 100644 --- a/firmware/README.md +++ b/firmware/README.md @@ -160,4 +160,25 @@ The build files will be in the `build_files.zip` file. Now you can create a rele | 0x20000 | minino.bin | ```bash python -m esptool --chip esp32c6 -b 460800 --before default_reset --after hard_reset write_flash --flash_mode dio --flash_size 8MB --flash_freq 80m 0x0 bootloader.bin 0x8000 partition-table.bin 0x20000 minino.bin +``` + + + +## BLE + +### ADV Filters +``` +BLE_SCAN_FILTER_ALLOW_ALL = 0x0, /*!< Accept all : + 1. advertisement packets except directed advertising packets not addressed to this device (default). */ + BLE_SCAN_FILTER_ALLOW_ONLY_WLST = 0x1, /*!< Accept only : + 1. advertisement packets from devices where the advertiser’s address is in the White list. + 2. Directed advertising packets which are not addressed for this device shall be ignored. */ + BLE_SCAN_FILTER_ALLOW_UND_RPA_DIR = 0x2, /*!< Accept all : + 1. undirected advertisement packets, and + 2. directed advertising packets where the initiator address is a resolvable private address, and + 3. directed advertising packets addressed to this device. */ + BLE_SCAN_FILTER_ALLOW_WLIST_RPA_DIR = 0x3, /*!< Accept all : + 1. advertisement packets from devices where the advertiser’s address is in the White list, and + 2. directed advertising packets where the initiator address is a resolvable private address, and + 3. directed advertising packets addressed to this device.*/ ``` \ No newline at end of file diff --git a/firmware/components/ble_scann/ble_scann.c b/firmware/components/ble_scann/ble_scann.c index d363043..35cac31 100644 --- a/firmware/components/ble_scann/ble_scann.c +++ b/firmware/components/ble_scann/ble_scann.c @@ -10,6 +10,7 @@ static bluetooth_adv_scanner_cb_t display_records_cb = NULL; static int ble_scan_duration = 0; static bool ble_scanner_active = false; static esp_ble_scan_filter_t ble_scan_filter = BLE_SCAN_FILTER_ALLOW_ALL; +static esp_ble_scan_type_t ble_scan_type = BLE_SCAN_TYPE_ACTIVE; static void task_scanner_timer(); static void handle_bt_gapc_events(esp_gap_ble_cb_event_t event_type, esp_ble_gap_cb_param_t* param); @@ -18,6 +19,10 @@ void set_filter_type(uint8_t filter_type) { ble_scan_filter = filter_type; } +void set_scan_type(uint8_t scan_type) { + ble_scan_type = scan_type; +} + void ble_scanner_begin() { // #if !defined(CONFIG_TRACKERS_SCANNER_DEBUG) // esp_log_level_set(TAG_BLE_CLIENT_MODULE, ESP_LOG_NONE); @@ -30,6 +35,7 @@ void ble_scanner_begin() { .notify_descr_uuid = bt_gattc_set_default_ble_notify_descr_uuid(), .ble_scan_params = bt_gattc_set_default_ble_scan_params()}; scan_params.ble_scan_params.scan_filter_policy = ble_scan_filter; + scan_params.ble_scan_params.scan_type = ble_scan_type; bt_gattc_set_ble_scan_params(&scan_params); bt_client_event_cb_t event_cb = {.handler_gattc_cb = NULL, .handler_gapc_cb = handle_bt_gapc_events}; @@ -79,7 +85,7 @@ static void task_scanner_timer() { ESP_LOGI(TAG_BLE_CLIENT_MODULE, "Trackers task stopped"); ble_scanner_stop(); } - // ble_scan_duration++; + ble_scan_duration++; vTaskDelay(1000 / portTICK_PERIOD_MS); } } diff --git a/firmware/components/ble_scann/ble_scann.h b/firmware/components/ble_scann/ble_scann.h index 4e0bfba..e745763 100644 --- a/firmware/components/ble_scann/ble_scann.h +++ b/firmware/components/ble_scann/ble_scann.h @@ -73,4 +73,5 @@ void ble_scanner_stop(); */ bool ble_scanner_is_active(); void set_filter_type(uint8_t filter_type); +void set_scan_type(uint8_t scan_type); #endif // BLE_SCANNER_H diff --git a/firmware/main/apps/ble/adv_scanner/adv_scan_module.c b/firmware/main/apps/ble/adv_scanner/adv_scan_module.c index eb9cc96..607e491 100644 --- a/firmware/main/apps/ble/adv_scanner/adv_scan_module.c +++ b/firmware/main/apps/ble/adv_scanner/adv_scan_module.c @@ -19,6 +19,12 @@ static void adv_scanner_module_reset_menu() { static void adv_filter_selection(uint8_t selection) { set_filter_type(selection); + adv_scanner_module_reset_menu(); +} + +static void adv_type_selection(uint8_t selection) { + set_scan_type(selection); + adv_scanner_module_reset_menu(); } void adv_scanner_display_filter() { @@ -30,6 +36,15 @@ void adv_scanner_display_filter() { general_submenu(adv_menu_filter); } +void adv_scanner_display_type() { + general_submenu_menu_t adv_menu_type; + adv_menu_type.options = scan_type_items; + adv_menu_type.options_count = 2; + adv_menu_type.select_cb = adv_type_selection; + adv_menu_type.exit_cb = adv_scanner_module_reset_menu; + general_submenu(adv_menu_type); +} + static void adv_scanner_module_cb_event(uint8_t button_name, uint8_t button_event) { if (button_event != BUTTON_PRESS_DOWN) { @@ -46,9 +61,10 @@ static void adv_scanner_module_cb_event(uint8_t button_name, break; case BUTTON_RIGHT: if (current_item == SCAN_TYPE) { + adv_scanner_display_type(); + } else if (current_item == SCAN_FILTER) { adv_scanner_display_filter(); } else if (current_item == SCAN_START) { - ESP_LOGI(TAG_BLE_CLIENT_MODULE, "Scan start"); ble_scanner_register_cb(adv_scanner_display_record); ble_scanner_begin(); }