Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/scanner_ble' into eko
Browse files Browse the repository at this point in the history
  • Loading branch information
JahazielLem committed Nov 7, 2024
2 parents 125bf3c + ce02bdb commit a7b61a5
Show file tree
Hide file tree
Showing 16 changed files with 527 additions and 6 deletions.
21 changes: 21 additions & 0 deletions firmware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.*/
```
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 ".")
110 changes: 110 additions & 0 deletions firmware/components/ble_scann/ble_scann.c
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;
}
77 changes: 77 additions & 0 deletions firmware/components/ble_scann/ble_scann.h
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
6 changes: 5 additions & 1 deletion firmware/components/minino_config/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions firmware/components/uart_sender/CMakeLists.txt
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 ".")
91 changes: 91 additions & 0 deletions firmware/components/uart_sender/uart_sender.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@
#include <stdio.h>
#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
*
Expand Down Expand Up @@ -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");
}
5 changes: 4 additions & 1 deletion firmware/components/uart_sender/uart_sender.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once
#include <stdint.h>
#include "esp_gap_ble_api.h"
#ifndef UART_SENDER
#define UART_SENDER

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 All @@ -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
*
Expand Down
Loading

0 comments on commit a7b61a5

Please sign in to comment.