Skip to content

Commit

Permalink
feat: Add BLE ADV Scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
JahazielLem committed Oct 8, 2024
1 parent 6796513 commit 6ed09db
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 1 deletion.
3 changes: 3 additions & 0 deletions firmware/components/ble_scann/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
idf_component_register(SRCS "ble_scann.c"
PRIV_REQUIRES bt bt_gattc uart_sender
INCLUDE_DIRS ".")
97 changes: 97 additions & 0 deletions firmware/components/ble_scann/ble_scann.c
Original file line number Diff line number Diff line change
@@ -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;
}
75 changes: 75 additions & 0 deletions firmware/components/ble_scann/ble_scann.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion firmware/components/uart_sender/uart_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions firmware/main/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#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"
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6ed09db

Please sign in to comment.