|
|
|
|
@ -8,7 +8,6 @@
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
volatile int g_ble_scanning;
|
|
|
|
|
volatile int g_ble_initialized;
|
|
|
|
|
uint8_t g_own_addr_type;
|
|
|
|
|
uint8_t g_own_addr[6];
|
|
|
|
|
@ -42,8 +41,8 @@ static void ble_adv_start_internal(void)
|
|
|
|
|
struct ble_gap_adv_params params = {0};
|
|
|
|
|
params.conn_mode = BLE_GAP_CONN_MODE_NON;
|
|
|
|
|
params.disc_mode = BLE_GAP_DISC_MODE_NON;
|
|
|
|
|
params.itvl_min = 0x0320; // 500ms
|
|
|
|
|
params.itvl_max = 0x0320;
|
|
|
|
|
params.itvl_min = 0x00C8; // 125ms
|
|
|
|
|
params.itvl_max = 0x00C8;
|
|
|
|
|
ble_gap_adv_start(g_own_addr_type, NULL, BLE_HS_FOREVER, ¶ms, ble_gap_event_handler, NULL);
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
@ -55,11 +54,23 @@ static void ble_adv_start_internal(void)
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ble_scan_start(void)
|
|
|
|
|
{
|
|
|
|
|
struct ble_gap_disc_params disc_params = {0};
|
|
|
|
|
disc_params.itvl = 0x0100; disc_params.window = 0x0080; disc_params.passive = 1;
|
|
|
|
|
ble_gap_disc(g_own_addr_type, 5000, &disc_params, ble_gap_event_handler, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ble_adv_update(void)
|
|
|
|
|
{
|
|
|
|
|
if (!g_ble_initialized || g_ble_scanning) return;
|
|
|
|
|
if (!g_ble_initialized) return;
|
|
|
|
|
ble_gap_adv_stop();
|
|
|
|
|
if (g_ble_enabled) ble_adv_start_internal();
|
|
|
|
|
if (g_ble_enabled) {
|
|
|
|
|
ble_adv_start_internal();
|
|
|
|
|
if (!ble_gap_disc_active()) ble_scan_start();
|
|
|
|
|
} else {
|
|
|
|
|
ble_gap_disc_cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ble_update_peer(const ble_addr_t *addr, const ble_payload_t *p)
|
|
|
|
|
@ -125,8 +136,7 @@ static int ble_gap_event_handler(struct ble_gap_event *event, void *arg)
|
|
|
|
|
i += 1 + adlen;
|
|
|
|
|
}
|
|
|
|
|
} else if (event->type == BLE_GAP_EVENT_DISC_COMPLETE) {
|
|
|
|
|
g_ble_scanning = 0;
|
|
|
|
|
if (g_ble_enabled) ble_adv_start_internal();
|
|
|
|
|
if (g_ble_enabled) ble_scan_start();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
@ -136,7 +146,10 @@ static void ble_on_sync(void)
|
|
|
|
|
ble_hs_id_infer_auto(0, &g_own_addr_type);
|
|
|
|
|
ble_hs_id_copy_addr(g_own_addr_type, g_own_addr, NULL);
|
|
|
|
|
g_ble_initialized = 1;
|
|
|
|
|
if (g_ble_enabled) ble_adv_start_internal();
|
|
|
|
|
if (g_ble_enabled) {
|
|
|
|
|
ble_adv_start_internal();
|
|
|
|
|
ble_scan_start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ble_on_reset(int reason) { (void)reason; }
|
|
|
|
|
@ -147,32 +160,6 @@ static void ble_host_task(void *arg)
|
|
|
|
|
nimble_port_freertos_deinit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ble_manager_task(void *arg)
|
|
|
|
|
{
|
|
|
|
|
while (!g_ble_initialized) vTaskDelay(pdMS_TO_TICKS(100));
|
|
|
|
|
while (1) {
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(10000));
|
|
|
|
|
if (!g_ble_enabled || g_ble_scanning) continue;
|
|
|
|
|
|
|
|
|
|
g_ble_scanning = 1;
|
|
|
|
|
ble_gap_adv_stop();
|
|
|
|
|
|
|
|
|
|
struct ble_gap_disc_params disc_params = {0};
|
|
|
|
|
disc_params.itvl = 0x0100; disc_params.window = 0x0080; disc_params.passive = 1;
|
|
|
|
|
int rc = ble_gap_disc(g_own_addr_type, 5000, &disc_params, ble_gap_event_handler, NULL);
|
|
|
|
|
if (rc != 0) {
|
|
|
|
|
g_ble_scanning = 0;
|
|
|
|
|
if (g_ble_enabled) ble_adv_start_internal();
|
|
|
|
|
}
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(6000));
|
|
|
|
|
if (g_ble_scanning) {
|
|
|
|
|
ble_gap_disc_cancel();
|
|
|
|
|
g_ble_scanning = 0;
|
|
|
|
|
if (g_ble_enabled) ble_adv_start_internal();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ble_init(void)
|
|
|
|
|
{
|
|
|
|
|
nimble_port_init();
|
|
|
|
|
@ -180,5 +167,4 @@ void ble_init(void)
|
|
|
|
|
ble_hs_cfg.reset_cb = ble_on_reset;
|
|
|
|
|
ble_svc_gap_init();
|
|
|
|
|
nimble_port_freertos_init(ble_host_task);
|
|
|
|
|
xTaskCreate(ble_manager_task, "ble_mgr", 4096, NULL, 5, NULL);
|
|
|
|
|
}
|
|
|
|
|
|