-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/scanner_ble' into eko
- Loading branch information
Showing
16 changed files
with
527 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ".") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#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_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); | ||
|
||
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); | ||
// #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()}; | ||
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}; | ||
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_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: | ||
break; | ||
default: | ||
break; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void ble_scanner_register_cb(bluetooth_adv_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(); | ||
} | ||
ble_scan_duration++; | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#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 60 | ||
|
||
/** | ||
* @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_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_adv_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(); | ||
void set_filter_type(uint8_t filter_type); | ||
void set_scan_type(uint8_t scan_type); | ||
#endif // BLE_SCANNER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
idf_component_register(SRCS "uart_sender.c" | ||
PRIV_REQUIRES bt bt_gattc | ||
INCLUDE_DIRS ".") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.