From 6ed09dba7a0502cb464a137cbe764ea0f092255b Mon Sep 17 00:00:00 2001 From: Kevin Jahaziel Leon Morales Date: Tue, 8 Oct 2024 10:39:02 -0600 Subject: [PATCH 1/6] feat: Add BLE ADV Scanner --- firmware/components/ble_scann/CMakeLists.txt | 3 + firmware/components/ble_scann/ble_scann.c | 97 +++++++++++++++++++ firmware/components/ble_scann/ble_scann.h | 75 ++++++++++++++ firmware/components/uart_sender/uart_sender.h | 2 +- firmware/main/main.c | 2 + 5 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 firmware/components/ble_scann/CMakeLists.txt create mode 100644 firmware/components/ble_scann/ble_scann.c create mode 100644 firmware/components/ble_scann/ble_scann.h diff --git a/firmware/components/ble_scann/CMakeLists.txt b/firmware/components/ble_scann/CMakeLists.txt new file mode 100644 index 00000000..e0576fa8 --- /dev/null +++ b/firmware/components/ble_scann/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "ble_scann.c" +PRIV_REQUIRES bt bt_gattc uart_sender +INCLUDE_DIRS ".") diff --git a/firmware/components/ble_scann/ble_scann.c b/firmware/components/ble_scann/ble_scann.c new file mode 100644 index 00000000..5189550f --- /dev/null +++ b/firmware/components/ble_scann/ble_scann.c @@ -0,0 +1,97 @@ +#include "ble_scann.h" +#include "bt_gattc.h" +#include "esp_bt.h" +#include "esp_log.h" +#include "inttypes.h" +#include "uart_sender.h" + +static TaskHandle_t ble_scan_timer_task = NULL; +static bluetooth_traker_scanner_cb_t display_records_cb = NULL; +static int ble_scan_duration = 0; +static bool ble_scanner_active = false; + +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); + +void ble_scanner_begin() { + // #if !defined(CONFIG_TRACKERS_SCANNER_DEBUG) + // esp_log_level_set(TAG_BLE_CLIENT_MODULE, ESP_LOG_NONE); + // #endif + + gattc_scan_params_t scan_params = { + .remote_filter_service_uuid = + bt_gattc_set_default_ble_filter_service_uuid(), + .remote_filter_char_uuid = bt_gattc_set_default_ble_filter_char_uuid(), + .notify_descr_uuid = bt_gattc_set_default_ble_notify_descr_uuid(), + .ble_scan_params = bt_gattc_set_default_ble_scan_params()}; + 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}; + bt_gattc_set_cb(event_cb); + bt_gattc_task_begin(); + ble_scanner_active = true; + xTaskCreate(task_scanner_timer, "ble_scanner", 4096, NULL, 5, + &ble_scan_timer_task); +} + +static void handle_bt_gapc_events(esp_gap_ble_cb_event_t event_type, + esp_ble_gap_cb_param_t* param) { + switch (event_type) { + case ESP_GAP_BLE_SCAN_RESULT_EVT: + esp_ble_gap_cb_param_t* scan_result = (esp_ble_gap_cb_param_t*) param; + switch (scan_result->scan_rst.search_evt) { + case ESP_GAP_SEARCH_INQ_RES_EVT: + if (!ble_scanner_active) { + break; + } + uart_sender_send_packet(UART_SENDER_PACKET_TYPE_BLE, + scan_result->scan_rst.ble_adv, + scan_result->scan_rst.adv_data_len); + ESP_LOGI(TAG_BLE_CLIENT_MODULE, "New ADV found"); + break; + case ESP_GAP_SEARCH_INQ_CMPL_EVT: + break; + default: + break; + } + break; + default: + break; + } +} + +void ble_scanner_register_cb(bluetooth_traker_scanner_cb_t callback) { + display_records_cb = callback; +} + +static void task_scanner_timer() { + ESP_LOGI(TAG_BLE_CLIENT_MODULE, "Trackers task started"); + ble_scan_duration = 0; + while (ble_scanner_active) { + if (ble_scan_duration >= SCANNER_SCAN_DURATION) { + ESP_LOGI(TAG_BLE_CLIENT_MODULE, "Trackers task stopped"); + ble_scanner_stop(); + } + vTaskDelay(1000 / portTICK_PERIOD_MS); + } +} + +void ble_scanner_stop() { + ble_scanner_active = false; + ESP_LOGI(TAG_BLE_CLIENT_MODULE, "Trackers task stopped"); + if (ble_scan_timer_task != NULL) { + ESP_LOGI(TAG_BLE_CLIENT_MODULE, "Trackers task stopped"); + vTaskSuspend(ble_scan_timer_task); + } + ESP_LOGI(TAG_BLE_CLIENT_MODULE, "Trackers task stopped"); + ble_scan_duration = 0; + vTaskDelete(NULL); + // TODO: When this is called, the BLE stopping bricks the device + // bt_gattc_task_stop(); + ESP_LOGI(TAG_BLE_CLIENT_MODULE, "Trackers task stopped"); +} + +bool ble_scanner_is_active() { + return ble_scanner_active; +} diff --git a/firmware/components/ble_scann/ble_scann.h b/firmware/components/ble_scann/ble_scann.h new file mode 100644 index 00000000..cb944f8e --- /dev/null +++ b/firmware/components/ble_scann/ble_scann.h @@ -0,0 +1,75 @@ +#include "esp_bt.h" +#include "esp_bt_defs.h" +#include "esp_bt_main.h" +#include "esp_gap_ble_api.h" +#include "esp_gatt_common_api.h" +#include "esp_gattc_api.h" +#include "esp_gatts_api.h" +#ifndef BLE_SCANNER_H + #define BLE_SCANNER_H + #define TAG_BLE_CLIENT_MODULE "scanner_module:main" + #define SCANNER_REMOTE_SERVICE_UUID 0x00FF + #define SCANNER_REMOTE_NOTIFY_CHAR_UUID 0xFF01 + #define SCANNER_PROFILE_NUM 1 + #define SCANNER_PROFILE_A_APP_ID 0 + #define SCANNER_INVALID_HANDLE 0 + #define SCANNER_SCAN_DURATION 240 + +/** + * @brief Structure to store the tracker profile + * + */ +typedef struct { + int rssi; + char* name; + char* vendor; + uint8_t mac_address[6]; + uint8_t adv_data[31]; + uint8_t adv_data_length; + bool is_tracker; +} device_profile; + +/** + * @brief Structure to store the tracker advertisement comparison + * + */ +typedef struct { + uint8_t adv_cmp[4]; + char* name; + char* vendor; +} scanner_adv_cmp_t; + +/** + * @brief Callback to handle the bluetooth scanner + * + * @param record The tracker profile record + */ +typedef void (*bluetooth_traker_scanner_cb_t)(device_profile record); + +/** + * @brief Register the callback to handle the bluetooth scanner + * + * @param cb The callback to handle the bluetooth scanner + */ +void ble_scanner_register_cb(bluetooth_traker_scanner_cb_t cb); + +/** + * @brief Start the bluetooth scanner + * + */ +void ble_scanner_begin(); + +/** + * @brief Stop the bluetooth scanner + * + */ +void ble_scanner_stop(); + +/** + * @brief Check if the bluetooth scanner is active + * + * @return true The bluetooth scanner is active + * @return false The bluetooth scanner is not active + */ +bool ble_scanner_is_active(); +#endif // BLE_SCANNER_H diff --git a/firmware/components/uart_sender/uart_sender.h b/firmware/components/uart_sender/uart_sender.h index fbb91ece..9b50950e 100644 --- a/firmware/components/uart_sender/uart_sender.h +++ b/firmware/components/uart_sender/uart_sender.h @@ -5,7 +5,7 @@ typedef enum { UART_SENDER_PACKET_TYPE_ZIGBEE = 0, - UART_SENDER_PACKET_TYPE_BT, + UART_SENDER_PACKET_TYPE_BLE, UART_SENDER_PACKET_TYPE_WIFI, UART_SENDER_PACKET_TYPE_THREAD, } uart_sender_packet_type; diff --git a/firmware/main/main.c b/firmware/main/main.c index effd4b71..9c930111 100644 --- a/firmware/main/main.c +++ b/firmware/main/main.c @@ -1,6 +1,7 @@ #include #include "apps/ble/hid_device/hid_module.h" #include "apps/ble/trackers/trackers_module.h" +#include "ble_scann.h" #include "buzzer.h" #include "cat_console.h" #include "esp_log.h" @@ -35,6 +36,7 @@ void app_main() { buzzer_begin(BUZZER_PIN); sd_card_begin(); flash_fs_begin(flash_fs_screens_handler); + ble_scanner_begin(); keyboard_module_begin(); menus_module_begin(); leds_off(); From 52da9432b5b0daaa0867c9caa3efd642f1ee3ec7 Mon Sep 17 00:00:00 2001 From: Kevin Jahaziel Leon Morales Date: Thu, 24 Oct 2024 18:56:03 -0600 Subject: [PATCH 2/6] feat: Add Packet BLE - Fail CRC --- firmware/components/ble_scann/ble_scann.c | 4 +- .../components/uart_sender/CMakeLists.txt | 1 + firmware/components/uart_sender/uart_sender.c | 91 +++++++++++++++++++ firmware/components/uart_sender/uart_sender.h | 3 + 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/firmware/components/ble_scann/ble_scann.c b/firmware/components/ble_scann/ble_scann.c index 5189550f..4c7f48e2 100644 --- a/firmware/components/ble_scann/ble_scann.c +++ b/firmware/components/ble_scann/ble_scann.c @@ -45,9 +45,7 @@ static void handle_bt_gapc_events(esp_gap_ble_cb_event_t event_type, if (!ble_scanner_active) { break; } - uart_sender_send_packet(UART_SENDER_PACKET_TYPE_BLE, - scan_result->scan_rst.ble_adv, - scan_result->scan_rst.adv_data_len); + uart_sender_send_packet_ble(UART_SENDER_PACKET_TYPE_BLE, scan_result); ESP_LOGI(TAG_BLE_CLIENT_MODULE, "New ADV found"); break; case ESP_GAP_SEARCH_INQ_CMPL_EVT: diff --git a/firmware/components/uart_sender/CMakeLists.txt b/firmware/components/uart_sender/CMakeLists.txt index d208e3e0..a2a011bb 100644 --- a/firmware/components/uart_sender/CMakeLists.txt +++ b/firmware/components/uart_sender/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "uart_sender.c" +PRIV_REQUIRES bt bt_gattc INCLUDE_DIRS ".") diff --git a/firmware/components/uart_sender/uart_sender.c b/firmware/components/uart_sender/uart_sender.c index 273c4454..bad38bf0 100644 --- a/firmware/components/uart_sender/uart_sender.c +++ b/firmware/components/uart_sender/uart_sender.c @@ -4,8 +4,24 @@ #include #include "esp_log.h" +#define BLE_ADDRESS_SIZE 6 +#define BLE_PDU_INFO_OFFSET 19 +#define BLE_ADDRESS_OFFSET 21 +#define BLE_PAYLOAD_OFFSET 27 + static bool is_uart_sender_initialized = false; +static unsigned char base_packet[] = { + 0x40, 0x53, // Start - TI packet + 0xc0, // Packet Info - TI Packet + 0x36, 0x00, // Packet Length - TI Packet + 0x8c, 0xc9, 0xaf, 0x01, 0x00, 0x00, // Timestamp - TI Packet + 0x25, // Channel - TI Packet + 0x00, 0x00, // Connection Event - TI Packet + 0x00, // Connection Info - TI Packet + 0xd6, 0xbe, 0x89, 0x8e, // Access Address - TI Packet +}; + /** * @brief Initialize the UART sender * @@ -55,3 +71,78 @@ void uart_sender_send_packet(uart_sender_packet_type type, printf("@E"); // End of packet printf("\n"); } + +// unsigned char array[] = { + +// // Header - BLE Packet +// 0x42, // PDU Info - BLE Packet +// 0x21, // AdvLen - BLE Packet +// 0x15, 0xc7, 0xbf, 0x84, 0x50, 0x4b, // AdvA - BLE Packet +// // Payload - BLE Packet +// 0x02, 0x01, 0x1a, 0x17, 0xff, +// 0x4c, 0x00, 0x09, 0x08, 0x13, 0x03, 0xc0, 0xa8, 0x00, 0xb2, 0x1b, 0x58, +// 0x16, 0x08, 0x00, 0xa6, 0x48, 0x4c, 0x4f, 0x71, 0xdf, 0xb7, 0xf1, 0x9b, +// 0xee, 0xc7, // RSSI - TI Packet 0x80, // Status - TI Packet 0x40, 0x45 // +// END - TI packet +// }; + +uint32_t calculate_ble_crc24(const uint8_t* data, size_t len) { + uint32_t crc = 0x555555; // Inicialización del CRC con 0x555555 + uint32_t poly = 0x65B; // Polinomio 0x65B + + for (size_t i = 0; i < len; i++) { + crc ^= ((uint32_t) data[i] << 16); // XOR el byte actual al CRC + + for (int j = 0; j < 8; j++) { // Procesar bit a bit + if (crc & 0x800000) { + crc = (crc << 1) ^ poly; + } else { + crc <<= 1; + } + } + } + + return (crc & 0xFFFFFF); // Retornar solo los 24 bits inferiores +} + +void uart_sender_send_packet_ble(uart_sender_packet_type type, + esp_ble_gap_cb_param_t* packet) { + uart_sender_init(); + uint8_t packet_length = + 1 + 1 + BLE_ADDRESS_SIZE + packet->scan_rst.adv_data_len; + for (int i = 0; i < sizeof(base_packet); i++) { + printf("%c", base_packet[i]); + } + unsigned char temp_packet[packet_length]; + uint8_t index = 0; + temp_packet[index] = packet->scan_rst.ble_evt_type; + index++; + temp_packet[index] = packet->scan_rst.adv_data_len; + index++; + for (int i = 0; i < BLE_ADDRESS_SIZE; i++) { + temp_packet[index] = packet->scan_rst.bda[i]; + index++; + } + for (int i = 0; i < packet->scan_rst.adv_data_len; i++) { + temp_packet[index] = packet->scan_rst.ble_adv[i]; + index++; + } + // printf("%c", packet->scan_rst.ble_evt_type); + // printf("%c", packet->scan_rst.adv_data_len); + // for (int i = 0; i < BLE_ADDRESS_SIZE; i++) { + // printf("%c", packet->scan_rst.bda[i]); + // } + // for (int i = 0; i < packet->scan_rst.adv_data_len; i++) { + // printf("%c", packet->scan_rst.ble_adv[i]); + // } + for (int i = 0; i < packet_length; i++) { + printf("%c", temp_packet[i]); + } + + uint16_t crc = calculate_ble_crc24(temp_packet, packet_length); + printf("%02x%02x", (crc >> 8) & 0xFF, crc & 0xFF); + printf("%c", packet->scan_rst.rssi); + printf("\x80"); // Status + printf("@E"); // End of packet + printf("\n"); +} diff --git a/firmware/components/uart_sender/uart_sender.h b/firmware/components/uart_sender/uart_sender.h index 9b50950e..b8c01ee1 100644 --- a/firmware/components/uart_sender/uart_sender.h +++ b/firmware/components/uart_sender/uart_sender.h @@ -1,5 +1,6 @@ #pragma once #include +#include "esp_gap_ble_api.h" #ifndef UART_SENDER #define UART_SENDER @@ -21,6 +22,8 @@ void uart_sender_send_packet(uart_sender_packet_type type, uint8_t* packet, uint8_t len); +void uart_sender_send_packet_ble(uart_sender_packet_type type, + esp_ble_gap_cb_param_t* packet); /** * @brief DeInitialize the UART sender * From 07e14efb0ff5b20837e1bbf0327743682996bb3b Mon Sep 17 00:00:00 2001 From: Kevin Jahaziel Leon Morales Date: Fri, 25 Oct 2024 13:33:53 -0600 Subject: [PATCH 3/6] feat: Add screens --- firmware/components/ble_scann/ble_scann.c | 15 +++- firmware/components/ble_scann/ble_scann.h | 7 +- .../minino_config/Kconfig.projbuild | 6 +- .../apps/ble/adv_scanner/adv_scan_module.c | 70 +++++++++++++++++++ .../apps/ble/adv_scanner/adv_scan_module.h | 5 ++ .../apps/ble/adv_scanner/adv_scan_screens.c | 69 ++++++++++++++++++ .../apps/ble/adv_scanner/adv_scan_screens.h | 26 +++++++ firmware/main/general/general_screens.c | 10 +-- firmware/main/main.c | 3 +- .../menus_module/menus_include/menus.h | 11 +++ 10 files changed, 209 insertions(+), 13 deletions(-) create mode 100644 firmware/main/apps/ble/adv_scanner/adv_scan_module.c create mode 100644 firmware/main/apps/ble/adv_scanner/adv_scan_module.h create mode 100644 firmware/main/apps/ble/adv_scanner/adv_scan_screens.c create mode 100644 firmware/main/apps/ble/adv_scanner/adv_scan_screens.h diff --git a/firmware/components/ble_scann/ble_scann.c b/firmware/components/ble_scann/ble_scann.c index 4c7f48e2..d363043c 100644 --- a/firmware/components/ble_scann/ble_scann.c +++ b/firmware/components/ble_scann/ble_scann.c @@ -6,14 +6,18 @@ #include "uart_sender.h" static TaskHandle_t ble_scan_timer_task = NULL; -static bluetooth_traker_scanner_cb_t display_records_cb = NULL; +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 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); +void set_filter_type(uint8_t filter_type) { + ble_scan_filter = filter_type; +} + void ble_scanner_begin() { // #if !defined(CONFIG_TRACKERS_SCANNER_DEBUG) // esp_log_level_set(TAG_BLE_CLIENT_MODULE, ESP_LOG_NONE); @@ -25,6 +29,7 @@ void ble_scanner_begin() { .remote_filter_char_uuid = bt_gattc_set_default_ble_filter_char_uuid(), .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; 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}; @@ -46,6 +51,9 @@ static void handle_bt_gapc_events(esp_gap_ble_cb_event_t event_type, break; } uart_sender_send_packet_ble(UART_SENDER_PACKET_TYPE_BLE, scan_result); + if (display_records_cb != NULL) { + display_records_cb(scan_result); + } ESP_LOGI(TAG_BLE_CLIENT_MODULE, "New ADV found"); break; case ESP_GAP_SEARCH_INQ_CMPL_EVT: @@ -59,7 +67,7 @@ static void handle_bt_gapc_events(esp_gap_ble_cb_event_t event_type, } } -void ble_scanner_register_cb(bluetooth_traker_scanner_cb_t callback) { +void ble_scanner_register_cb(bluetooth_adv_scanner_cb_t callback) { display_records_cb = callback; } @@ -71,6 +79,7 @@ static void task_scanner_timer() { ESP_LOGI(TAG_BLE_CLIENT_MODULE, "Trackers task stopped"); ble_scanner_stop(); } + // 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 cb944f8e..4e0bfba3 100644 --- a/firmware/components/ble_scann/ble_scann.h +++ b/firmware/components/ble_scann/ble_scann.h @@ -13,7 +13,7 @@ #define SCANNER_PROFILE_NUM 1 #define SCANNER_PROFILE_A_APP_ID 0 #define SCANNER_INVALID_HANDLE 0 - #define SCANNER_SCAN_DURATION 240 + #define SCANNER_SCAN_DURATION 60 /** * @brief Structure to store the tracker profile @@ -44,14 +44,14 @@ typedef struct { * * @param record The tracker profile record */ -typedef void (*bluetooth_traker_scanner_cb_t)(device_profile record); +typedef void (*bluetooth_adv_scanner_cb_t)(esp_ble_gap_cb_param_t* record); /** * @brief Register the callback to handle the bluetooth scanner * * @param cb The callback to handle the bluetooth scanner */ -void ble_scanner_register_cb(bluetooth_traker_scanner_cb_t cb); +void ble_scanner_register_cb(bluetooth_adv_scanner_cb_t cb); /** * @brief Start the bluetooth scanner @@ -72,4 +72,5 @@ void ble_scanner_stop(); * @return false The bluetooth scanner is not active */ bool ble_scanner_is_active(); +void set_filter_type(uint8_t filter_type); #endif // BLE_SCANNER_H diff --git a/firmware/components/minino_config/Kconfig.projbuild b/firmware/components/minino_config/Kconfig.projbuild index 46ab71d6..181b9669 100644 --- a/firmware/components/minino_config/Kconfig.projbuild +++ b/firmware/components/minino_config/Kconfig.projbuild @@ -620,8 +620,12 @@ config BLUETOOTH_APP_HID default true help Enable or disable the Bluetooth HID application. +config BLUETOOTH_APP_ADV + bool "Enable ADV Scanner App" + default true + help + Enable or disable the Bluetooth ADV Scanner application. endif # BLUETOOTH_APPS_ENABLE - ################################# ZIGBEE ################################### config ZIGBEE_APPS_ENABLE diff --git a/firmware/main/apps/ble/adv_scanner/adv_scan_module.c b/firmware/main/apps/ble/adv_scanner/adv_scan_module.c new file mode 100644 index 00000000..eb9cc96f --- /dev/null +++ b/firmware/main/apps/ble/adv_scanner/adv_scan_module.c @@ -0,0 +1,70 @@ +#include "adv_scan_screens.h" +#include "ble_scann.h" +#include "esp_log.h" +#include "esp_mac.h" +#include "general_submenu.h" +#include "menus_module.h" + +static uint16_t current_item = 0; + +static void adv_scanner_module_cb_event(uint8_t button_name, + uint8_t button_event); + +static void adv_scanner_module_reset_menu() { + current_item = 0; + adv_scanner_module_register_menu(GENERAL_TREE_APP_MENU); + adv_scanner_module_display_menu(current_item); + menus_module_set_app_state(true, adv_scanner_module_cb_event); +} + +static void adv_filter_selection(uint8_t selection) { + set_filter_type(selection); +} + +void adv_scanner_display_filter() { + general_submenu_menu_t adv_menu_filter; + adv_menu_filter.options = scan_filter_items; + adv_menu_filter.options_count = 4; + adv_menu_filter.select_cb = adv_filter_selection; + adv_menu_filter.exit_cb = adv_scanner_module_reset_menu; + general_submenu(adv_menu_filter); +} + +static void adv_scanner_module_cb_event(uint8_t button_name, + uint8_t button_event) { + if (button_event != BUTTON_PRESS_DOWN) { + return; + } + switch (button_name) { + case BUTTON_UP: + current_item = current_item-- == 0 ? SCAN_MENU_COUNT - 1 : current_item; + adv_scanner_module_display_menu(current_item); + break; + case BUTTON_DOWN: + current_item = ++current_item > SCAN_MENU_COUNT - 1 ? 0 : current_item; + adv_scanner_module_display_menu(current_item); + break; + case BUTTON_RIGHT: + if (current_item == SCAN_TYPE) { + 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(); + } + current_item = 0; + + break; + case BUTTON_LEFT: + menus_module_restart(); + break; + default: + break; + } +} + +void adv_scanner_module_begin() { + adv_scanner_module_register_menu(GENERAL_TREE_APP_MENU); + adv_scanner_module_display_menu(current_item); + menus_module_set_app_state(true, adv_scanner_module_cb_event); +} diff --git a/firmware/main/apps/ble/adv_scanner/adv_scan_module.h b/firmware/main/apps/ble/adv_scanner/adv_scan_module.h new file mode 100644 index 00000000..9a694271 --- /dev/null +++ b/firmware/main/apps/ble/adv_scanner/adv_scan_module.h @@ -0,0 +1,5 @@ +#include +#ifndef ADV_SCAN_MODULE_H + #define ADV_SCAN_MODULE_H +void adv_scanner_module_begin(); +#endif // ADV_SCAN_MODULE_H diff --git a/firmware/main/apps/ble/adv_scanner/adv_scan_screens.c b/firmware/main/apps/ble/adv_scanner/adv_scan_screens.c new file mode 100644 index 00000000..965d96ac --- /dev/null +++ b/firmware/main/apps/ble/adv_scanner/adv_scan_screens.c @@ -0,0 +1,69 @@ +#include "adv_scan_screens.h" +#include +#include +#include "animations_task.h" +#include "freertos/FreeRTOS.h" +#include "general/general_screens.h" +#include "led_events.h" +#include "oled_screen.h" + +#define MAX_ITEMS_PER_SCREEN 5 + +static uint16_t hid_current_item = 0; +static esp_ble_gap_cb_param_t adv_list[MAX_ITEMS_PER_SCREEN]; +static uint8_t adv_list_count = 0; + +static const general_menu_t adv_menu_main = { + .menu_items = scan_menu_items, + .menu_count = SCAN_MENU_COUNT, + .menu_level = GENERAL_TREE_APP_MENU, +}; + +// static const general_menu_t adv_type = { +// .menu_items = scan_type_items, +// .menu_count = 2, +// .menu_level = GENERAL_TREE_APP_MENU, +// }; + +// static const general_menu_t adv_filter = { +// .menu_items = scan_filter_items, +// .menu_count = 4, +// .menu_level = GENERAL_TREE_APP_MENU, +// }; + +void adv_scanner_module_register_menu(menu_tree_t menu) { + switch (menu) { + case GENERAL_TREE_APP_MENU: + general_register_menu(&adv_menu_main); + break; + default: + general_register_menu(&adv_menu_main); + break; + } +} + +void adv_scanner_display_record(esp_ble_gap_cb_param_t* record) { + if (adv_list_count >= MAX_ITEMS_PER_SCREEN) { + adv_list_count = 0; + } + adv_list[adv_list_count] = *record; + + oled_screen_clear_buffer(); + oled_screen_display_text("< Back", 0, 0, OLED_DISPLAY_NORMAL); + oled_screen_display_text("ADV Type | RSSI", 0, 1, OLED_DISPLAY_NORMAL); + + char* row = (char*) malloc(17); + for (int i = 0; i < MAX_ITEMS_PER_SCREEN; i++) { + sprintf(row, "%s %d", evt_adv_type[adv_list[i].scan_rst.ble_evt_type], + adv_list[i].scan_rst.rssi); + oled_screen_display_text(row, 0, i + 2, OLED_DISPLAY_NORMAL); + } + oled_screen_display_show(); + adv_list_count++; +} + +void adv_scanner_module_display_menu(uint16_t current_item) { + led_control_run_effect(led_control_pulse_leds); + hid_current_item = current_item; + general_screen_display_menu(current_item); +} \ No newline at end of file diff --git a/firmware/main/apps/ble/adv_scanner/adv_scan_screens.h b/firmware/main/apps/ble/adv_scanner/adv_scan_screens.h new file mode 100644 index 00000000..eb4279e3 --- /dev/null +++ b/firmware/main/apps/ble/adv_scanner/adv_scan_screens.h @@ -0,0 +1,26 @@ +#include +#include +#include "esp_gap_ble_api.h" +#include "general/general_screens.h" +#ifndef ADV_SCAN_SCREENS_H + #define ADV_SCAN_SCREENS_H + +enum { + SCAN_TYPE, + SCAN_FILTER, + SCAN_START, + SCAN_MENU_COUNT +} scan_menu_item = SCAN_FILTER; +char* scan_menu_items[SCAN_MENU_COUNT] = {"Scan Type", "ADV Filter", "Start"}; + +char* scan_type_items[2] = {"Active", "Passive"}; +char* scan_filter_items[4] = {"Allow All", "Allow Only WLST", "Allow UND RPA", + "Allow WLST & RPA"}; +char* evt_adv_type[5] = {"IND", "DIRECT_IND", "SCAN_IND", "NONCONN_IND", + "SCAN_RSP"}; + +void adv_scanner_module_register_menu(menu_tree_t menu); +void adv_scanner_clear_screen(); +void adv_scanner_module_display_menu(uint16_t current_item); +void adv_scanner_display_record(esp_ble_gap_cb_param_t* record); +#endif // ADV_SCAN_SCREENS_H diff --git a/firmware/main/general/general_screens.c b/firmware/main/general/general_screens.c index 7e192482..432b45b3 100644 --- a/firmware/main/general/general_screens.c +++ b/firmware/main/general/general_screens.c @@ -7,11 +7,13 @@ #define MAX_LINE_CHAR 16 #ifdef CONFIG_RESOLUTION_128X64 - #define ITEMOFFSET 2 - #define ITEM_PAGE_OFFSET 2 + #define ITEMOFFSET 2 + #define ITEM_PAGE_OFFSET 2 + #define MAX_ITEMS_PER_SCREEN 5 #else // CONFIG_RESOLUTION_128X32 - #define ITEMOFFSET 1 - #define ITEM_PAGE_OFFSET 1 + #define ITEMOFFSET 1 + #define ITEM_PAGE_OFFSET 1 + #define MAX_ITEMS_PER_SCREEN 3 #endif static uint8_t scrolling_option = 0; static const general_menu_t* current_menu_ctx = NULL; diff --git a/firmware/main/main.c b/firmware/main/main.c index 9c930111..15d71118 100644 --- a/firmware/main/main.c +++ b/firmware/main/main.c @@ -1,7 +1,7 @@ #include #include "apps/ble/hid_device/hid_module.h" #include "apps/ble/trackers/trackers_module.h" -#include "ble_scann.h" + #include "buzzer.h" #include "cat_console.h" #include "esp_log.h" @@ -36,7 +36,6 @@ void app_main() { buzzer_begin(BUZZER_PIN); sd_card_begin(); flash_fs_begin(flash_fs_screens_handler); - ble_scanner_begin(); keyboard_module_begin(); menus_module_begin(); leds_off(); diff --git a/firmware/main/modules/menus_module/menus_include/menus.h b/firmware/main/modules/menus_module/menus_include/menus.h index 6b513112..8fde279e 100644 --- a/firmware/main/modules/menus_module/menus_include/menus.h +++ b/firmware/main/modules/menus_module/menus_include/menus.h @@ -7,6 +7,7 @@ #include "trackers_module.h" #include "z_switch_module.h" +#include "adv_scan_module.h" #include "catdos_module.h" #include "deauth_module.h" #include "display_settings.h" @@ -55,6 +56,7 @@ typedef enum { MENU_BLUETOOTH_TRAKERS_SCAN, MENU_BLUETOOTH_SPAM, MENU_BLUETOOTH_HID, + MENU_BLUETOOTH_ADV, /* Zigbee applications */ MENU_ZIGBEE_SPOOFING, MENU_ZIGBEE_SWITCH, @@ -280,6 +282,15 @@ menu_t menus[] = { ////////////////////////////////// .on_exit_cb = NULL, .is_visible = true}, #endif + #ifdef CONFIG_BLUETOOTH_APP_ADV + {.display_name = "ADV Scanner", + .menu_idx = MENU_BLUETOOTH_ADV, + .parent_idx = MENU_BLUETOOTH_APPS, + .last_selected_submenu = 0, + .on_enter_cb = adv_scanner_module_begin, + .on_exit_cb = NULL, + .is_visible = true}, + #endif #endif #ifdef CONFIG_ZIGBEE_APPS_ENABLE {.display_name = "Zigbee", 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 4/6] 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 8ca20dc0..81218460 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 d363043c..35cac31a 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 4e0bfba3..e7457639 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 eb9cc96f..607e4911 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(); } From 13e740186011abad5433b2d42b8410ed89487109 Mon Sep 17 00:00:00 2001 From: Kevin Jahaziel Leon Morales Date: Fri, 25 Oct 2024 16:47:43 -0600 Subject: [PATCH 5/6] feat: Fix button handler advs --- firmware/main/apps/ble/adv_scanner/adv_scan_module.c | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 607e4911..7d7e3319 100644 --- a/firmware/main/apps/ble/adv_scanner/adv_scan_module.c +++ b/firmware/main/apps/ble/adv_scanner/adv_scan_module.c @@ -17,6 +17,12 @@ static void adv_scanner_module_reset_menu() { menus_module_set_app_state(true, adv_scanner_module_cb_event); } +static void adv_scanner_module_reset_start_menu() { + ble_scanner_register_cb(NULL); + menus_module_reset(); + adv_scanner_module_reset_menu(); +} + static void adv_filter_selection(uint8_t selection) { set_filter_type(selection); adv_scanner_module_reset_menu(); @@ -65,6 +71,10 @@ static void adv_scanner_module_cb_event(uint8_t button_name, } else if (current_item == SCAN_FILTER) { adv_scanner_display_filter(); } else if (current_item == SCAN_START) { + general_screen_display_card_information_handler( + "Scanning", "Scanning for devices", + adv_scanner_module_reset_start_menu, + adv_scanner_module_reset_start_menu); ble_scanner_register_cb(adv_scanner_display_record); ble_scanner_begin(); } From ce02bdb27daf694ebb7fd3b8040feff6b1df859d Mon Sep 17 00:00:00 2001 From: Roberto Arellano Date: Mon, 28 Oct 2024 11:51:51 -0600 Subject: [PATCH 6/6] feat: BT adv menu enabled in defaults --- firmware/sdkconfig.defaults | 1 + 1 file changed, 1 insertion(+) diff --git a/firmware/sdkconfig.defaults b/firmware/sdkconfig.defaults index 8dae4dfd..d57fa54d 100644 --- a/firmware/sdkconfig.defaults +++ b/firmware/sdkconfig.defaults @@ -99,6 +99,7 @@ CONFIG_BLUETOOTH_APPS_ENABLE=y CONFIG_BLUETOOTH_APP_TRAKERS=y CONFIG_BLUETOOTH_APP_SPAM=y CONFIG_BLUETOOTH_APP_HID=y +CONFIG_BLUETOOTH_APP_ADV=y ## ZIGBEE ## CONFIG_ZIGBEE_APPS_ENABLE=y CONFIG_ZIGBEE_APP_SPOOFING=y