gap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #include "gap.h"
  2. #include "ble.h"
  3. #include <furi_hal.h>
  4. #include <furi.h>
  5. #define TAG "BtGap"
  6. #define FAST_ADV_TIMEOUT 30000
  7. #define INITIAL_ADV_TIMEOUT 60000
  8. typedef struct {
  9. uint16_t gap_svc_handle;
  10. uint16_t dev_name_char_handle;
  11. uint16_t appearance_char_handle;
  12. uint16_t connection_handle;
  13. uint8_t adv_svc_uuid_len;
  14. uint8_t adv_svc_uuid[20];
  15. char* adv_name;
  16. } GapSvc;
  17. typedef struct {
  18. GapSvc service;
  19. GapConfig* config;
  20. GapState state;
  21. osMutexId_t state_mutex;
  22. GapEventCallback on_event_cb;
  23. void* context;
  24. osTimerId_t advertise_timer;
  25. FuriThread* thread;
  26. osMessageQueueId_t command_queue;
  27. bool enable_adv;
  28. } Gap;
  29. typedef enum {
  30. GapCommandAdvFast,
  31. GapCommandAdvLowPower,
  32. GapCommandAdvStop,
  33. GapCommandKillThread,
  34. } GapCommand;
  35. typedef struct {
  36. GapScanCallback callback;
  37. void* context;
  38. } GapScan;
  39. // Identity root key
  40. static const uint8_t gap_irk[16] =
  41. {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
  42. // Encryption root key
  43. static const uint8_t gap_erk[16] =
  44. {0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21};
  45. static Gap* gap = NULL;
  46. static GapScan* gap_scan = NULL;
  47. static void gap_advertise_start(GapState new_state);
  48. static int32_t gap_app(void* context);
  49. SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
  50. hci_event_pckt* event_pckt;
  51. evt_le_meta_event* meta_evt;
  52. evt_blue_aci* blue_evt;
  53. hci_le_phy_update_complete_event_rp0* evt_le_phy_update_complete;
  54. uint8_t tx_phy;
  55. uint8_t rx_phy;
  56. tBleStatus ret = BLE_STATUS_INVALID_PARAMS;
  57. event_pckt = (hci_event_pckt*)((hci_uart_pckt*)pckt)->data;
  58. if(gap) {
  59. osMutexAcquire(gap->state_mutex, osWaitForever);
  60. }
  61. switch(event_pckt->evt) {
  62. case EVT_DISCONN_COMPLETE: {
  63. hci_disconnection_complete_event_rp0* disconnection_complete_event =
  64. (hci_disconnection_complete_event_rp0*)event_pckt->data;
  65. if(disconnection_complete_event->Connection_Handle == gap->service.connection_handle) {
  66. gap->service.connection_handle = 0;
  67. gap->state = GapStateIdle;
  68. FURI_LOG_I(
  69. TAG, "Disconnect from client. Reason: %02X", disconnection_complete_event->Reason);
  70. }
  71. if(gap->enable_adv) {
  72. // Restart advertising
  73. gap_advertise_start(GapStateAdvFast);
  74. furi_hal_power_insomnia_exit();
  75. }
  76. GapEvent event = {.type = GapEventTypeDisconnected};
  77. gap->on_event_cb(event, gap->context);
  78. } break;
  79. case EVT_LE_META_EVENT:
  80. meta_evt = (evt_le_meta_event*)event_pckt->data;
  81. switch(meta_evt->subevent) {
  82. case EVT_LE_CONN_UPDATE_COMPLETE: {
  83. hci_le_connection_update_complete_event_rp0* event =
  84. (hci_le_connection_update_complete_event_rp0*)meta_evt->data;
  85. FURI_LOG_I(
  86. TAG,
  87. "Connection interval: %d, latency: %d, supervision timeout: %d",
  88. event->Conn_Interval,
  89. event->Conn_Latency,
  90. event->Supervision_Timeout);
  91. break;
  92. }
  93. case EVT_LE_PHY_UPDATE_COMPLETE:
  94. evt_le_phy_update_complete = (hci_le_phy_update_complete_event_rp0*)meta_evt->data;
  95. if(evt_le_phy_update_complete->Status) {
  96. FURI_LOG_E(
  97. TAG, "Update PHY failed, status %d", evt_le_phy_update_complete->Status);
  98. } else {
  99. FURI_LOG_I(TAG, "Update PHY succeed");
  100. }
  101. ret = hci_le_read_phy(gap->service.connection_handle, &tx_phy, &rx_phy);
  102. if(ret) {
  103. FURI_LOG_E(TAG, "Read PHY failed, status: %d", ret);
  104. } else {
  105. FURI_LOG_I(TAG, "PHY Params TX = %d, RX = %d ", tx_phy, rx_phy);
  106. }
  107. break;
  108. case EVT_LE_CONN_COMPLETE:
  109. furi_hal_power_insomnia_enter();
  110. hci_le_connection_complete_event_rp0* connection_complete_event =
  111. (hci_le_connection_complete_event_rp0*)meta_evt->data;
  112. FURI_LOG_I(
  113. TAG,
  114. "Connection complete for connection handle 0x%x",
  115. connection_complete_event->Connection_Handle);
  116. // Stop advertising as connection completed
  117. osTimerStop(gap->advertise_timer);
  118. // Update connection status and handle
  119. gap->state = GapStateConnected;
  120. gap->service.connection_handle = connection_complete_event->Connection_Handle;
  121. GapConnectionParams* params = &gap->config->conn_param;
  122. if(aci_l2cap_connection_parameter_update_req(
  123. gap->service.connection_handle,
  124. params->conn_int_min,
  125. params->conn_int_max,
  126. params->slave_latency,
  127. params->supervisor_timeout)) {
  128. FURI_LOG_W(TAG, "Failed to request connection parameters update");
  129. }
  130. // Start pairing by sending security request
  131. aci_gap_slave_security_req(connection_complete_event->Connection_Handle);
  132. break;
  133. case EVT_LE_ADVERTISING_REPORT: {
  134. if(gap_scan) {
  135. GapAddress address;
  136. hci_le_advertising_report_event_rp0* evt =
  137. (hci_le_advertising_report_event_rp0*)meta_evt->data;
  138. for(uint8_t i = 0; i < evt->Num_Reports; i++) {
  139. Advertising_Report_t* rep = &evt->Advertising_Report[i];
  140. address.type = rep->Address_Type;
  141. // Original MAC addres is in inverted order
  142. for(uint8_t j = 0; j < sizeof(address.mac); j++) {
  143. address.mac[j] = rep->Address[sizeof(address.mac) - j - 1];
  144. }
  145. gap_scan->callback(address, gap_scan->context);
  146. }
  147. }
  148. } break;
  149. default:
  150. break;
  151. }
  152. break;
  153. case EVT_VENDOR:
  154. blue_evt = (evt_blue_aci*)event_pckt->data;
  155. switch(blue_evt->ecode) {
  156. aci_gap_pairing_complete_event_rp0* pairing_complete;
  157. case EVT_BLUE_GAP_LIMITED_DISCOVERABLE:
  158. FURI_LOG_I(TAG, "Limited discoverable event");
  159. break;
  160. case EVT_BLUE_GAP_PASS_KEY_REQUEST: {
  161. // Generate random PIN code
  162. uint32_t pin = rand() % 999999;
  163. aci_gap_pass_key_resp(gap->service.connection_handle, pin);
  164. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock)) {
  165. FURI_LOG_I(TAG, "Pass key request event. Pin: ******");
  166. } else {
  167. FURI_LOG_I(TAG, "Pass key request event. Pin: %06d", pin);
  168. }
  169. GapEvent event = {.type = GapEventTypePinCodeShow, .data.pin_code = pin};
  170. gap->on_event_cb(event, gap->context);
  171. } break;
  172. case EVT_BLUE_ATT_EXCHANGE_MTU_RESP: {
  173. aci_att_exchange_mtu_resp_event_rp0* pr = (void*)blue_evt->data;
  174. FURI_LOG_I(TAG, "Rx MTU size: %d", pr->Server_RX_MTU);
  175. // Set maximum packet size given header size is 3 bytes
  176. GapEvent event = {
  177. .type = GapEventTypeUpdateMTU, .data.max_packet_size = pr->Server_RX_MTU - 3};
  178. gap->on_event_cb(event, gap->context);
  179. } break;
  180. case EVT_BLUE_GAP_AUTHORIZATION_REQUEST:
  181. FURI_LOG_D(TAG, "Authorization request event");
  182. break;
  183. case EVT_BLUE_GAP_SLAVE_SECURITY_INITIATED:
  184. FURI_LOG_D(TAG, "Slave security initiated");
  185. break;
  186. case EVT_BLUE_GAP_BOND_LOST:
  187. FURI_LOG_D(TAG, "Bond lost event. Start rebonding");
  188. aci_gap_allow_rebond(gap->service.connection_handle);
  189. break;
  190. case EVT_BLUE_GAP_DEVICE_FOUND:
  191. FURI_LOG_D(TAG, "Device found event");
  192. break;
  193. case EVT_BLUE_GAP_ADDR_NOT_RESOLVED:
  194. FURI_LOG_D(TAG, "Address not resolved event");
  195. break;
  196. case EVT_BLUE_GAP_KEYPRESS_NOTIFICATION:
  197. FURI_LOG_D(TAG, "Key press notification event");
  198. break;
  199. case EVT_BLUE_GAP_NUMERIC_COMPARISON_VALUE: {
  200. uint32_t pin =
  201. ((aci_gap_numeric_comparison_value_event_rp0*)(blue_evt->data))->Numeric_Value;
  202. FURI_LOG_I(TAG, "Verify numeric comparison: %06d", pin);
  203. GapEvent event = {.type = GapEventTypePinCodeVerify, .data.pin_code = pin};
  204. bool result = gap->on_event_cb(event, gap->context);
  205. aci_gap_numeric_comparison_value_confirm_yesno(gap->service.connection_handle, result);
  206. break;
  207. }
  208. case EVT_BLUE_GAP_PAIRING_CMPLT:
  209. pairing_complete = (aci_gap_pairing_complete_event_rp0*)blue_evt->data;
  210. if(pairing_complete->Status) {
  211. FURI_LOG_E(
  212. TAG,
  213. "Pairing failed with status: %d. Terminating connection",
  214. pairing_complete->Status);
  215. aci_gap_terminate(gap->service.connection_handle, 5);
  216. } else {
  217. FURI_LOG_I(TAG, "Pairing complete");
  218. GapEvent event = {.type = GapEventTypeConnected};
  219. gap->on_event_cb(event, gap->context);
  220. }
  221. break;
  222. case EVT_BLUE_GAP_PROCEDURE_COMPLETE:
  223. FURI_LOG_D(TAG, "Procedure complete event");
  224. break;
  225. case EVT_BLUE_L2CAP_CONNECTION_UPDATE_RESP: {
  226. uint16_t result =
  227. ((aci_l2cap_connection_update_resp_event_rp0*)(blue_evt->data))->Result;
  228. if(result == 0) {
  229. FURI_LOG_D(TAG, "Connection parameters accepted");
  230. } else if(result == 1) {
  231. FURI_LOG_D(TAG, "Connection parameters denied");
  232. }
  233. break;
  234. }
  235. }
  236. default:
  237. break;
  238. }
  239. if(gap) {
  240. osMutexRelease(gap->state_mutex);
  241. }
  242. return SVCCTL_UserEvtFlowEnable;
  243. }
  244. static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) {
  245. if(uid_len == 2) {
  246. gap->service.adv_svc_uuid[0] = AD_TYPE_16_BIT_SERV_UUID;
  247. } else if(uid_len == 4) {
  248. gap->service.adv_svc_uuid[0] = AD_TYPE_32_BIT_SERV_UUID;
  249. } else if(uid_len == 16) {
  250. gap->service.adv_svc_uuid[0] = AD_TYPE_128_BIT_SERV_UUID_CMPLT_LIST;
  251. }
  252. memcpy(&gap->service.adv_svc_uuid[gap->service.adv_svc_uuid_len], uid, uid_len);
  253. gap->service.adv_svc_uuid_len += uid_len;
  254. }
  255. static void gap_init_svc(Gap* gap) {
  256. tBleStatus status;
  257. uint32_t srd_bd_addr[2];
  258. // HCI Reset to synchronise BLE Stack
  259. hci_reset();
  260. // Configure mac address
  261. aci_hal_write_config_data(
  262. CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, gap->config->mac_address);
  263. /* Static random Address
  264. * The two upper bits shall be set to 1
  265. * The lowest 32bits is read from the UDN to differentiate between devices
  266. * The RNG may be used to provide a random number on each power on
  267. */
  268. srd_bd_addr[1] = 0x0000ED6E;
  269. srd_bd_addr[0] = LL_FLASH_GetUDN();
  270. aci_hal_write_config_data(
  271. CONFIG_DATA_RANDOM_ADDRESS_OFFSET, CONFIG_DATA_RANDOM_ADDRESS_LEN, (uint8_t*)srd_bd_addr);
  272. // Set Identity root key used to derive LTK and CSRK
  273. aci_hal_write_config_data(CONFIG_DATA_IR_OFFSET, CONFIG_DATA_IR_LEN, (uint8_t*)gap_irk);
  274. // Set Encryption root key used to derive LTK and CSRK
  275. aci_hal_write_config_data(CONFIG_DATA_ER_OFFSET, CONFIG_DATA_ER_LEN, (uint8_t*)gap_erk);
  276. // Set TX Power to 0 dBm
  277. aci_hal_set_tx_power_level(1, 0x19);
  278. // Initialize GATT interface
  279. aci_gatt_init();
  280. // Initialize GAP interface
  281. // Skip fist symbol AD_TYPE_COMPLETE_LOCAL_NAME
  282. char* name = gap->service.adv_name + 1;
  283. aci_gap_init(
  284. GAP_PERIPHERAL_ROLE,
  285. 0,
  286. strlen(name),
  287. &gap->service.gap_svc_handle,
  288. &gap->service.dev_name_char_handle,
  289. &gap->service.appearance_char_handle);
  290. // Set GAP characteristics
  291. status = aci_gatt_update_char_value(
  292. gap->service.gap_svc_handle,
  293. gap->service.dev_name_char_handle,
  294. 0,
  295. strlen(name),
  296. (uint8_t*)name);
  297. if(status) {
  298. FURI_LOG_E(TAG, "Failed updating name characteristic: %d", status);
  299. }
  300. uint8_t gap_appearence_char_uuid[2] = {
  301. gap->config->appearance_char & 0xff, gap->config->appearance_char >> 8};
  302. status = aci_gatt_update_char_value(
  303. gap->service.gap_svc_handle,
  304. gap->service.appearance_char_handle,
  305. 0,
  306. 2,
  307. gap_appearence_char_uuid);
  308. if(status) {
  309. FURI_LOG_E(TAG, "Failed updating appearence characteristic: %d", status);
  310. }
  311. // Set default PHY
  312. hci_le_set_default_phy(ALL_PHYS_PREFERENCE, TX_2M_PREFERRED, RX_2M_PREFERRED);
  313. // Set I/O capability
  314. bool keypress_supported = false;
  315. if(gap->config->pairing_method == GapPairingPinCodeShow) {
  316. aci_gap_set_io_capability(IO_CAP_DISPLAY_ONLY);
  317. } else if(gap->config->pairing_method == GapPairingPinCodeVerifyYesNo) {
  318. aci_gap_set_io_capability(IO_CAP_DISPLAY_YES_NO);
  319. keypress_supported = true;
  320. }
  321. // Setup authentication
  322. aci_gap_set_authentication_requirement(
  323. gap->config->bonding_mode,
  324. CFG_MITM_PROTECTION,
  325. CFG_SC_SUPPORT,
  326. keypress_supported,
  327. CFG_ENCRYPTION_KEY_SIZE_MIN,
  328. CFG_ENCRYPTION_KEY_SIZE_MAX,
  329. CFG_USED_FIXED_PIN,
  330. 0,
  331. PUBLIC_ADDR);
  332. // Configure whitelist
  333. aci_gap_configure_whitelist();
  334. }
  335. static void gap_advertise_start(GapState new_state) {
  336. tBleStatus status;
  337. uint16_t min_interval;
  338. uint16_t max_interval;
  339. if(new_state == GapStateAdvFast) {
  340. min_interval = 0x80; // 80 ms
  341. max_interval = 0xa0; // 100 ms
  342. } else {
  343. min_interval = 0x0640; // 1 s
  344. max_interval = 0x0fa0; // 2.5 s
  345. }
  346. // Stop advertising timer
  347. osTimerStop(gap->advertise_timer);
  348. if((new_state == GapStateAdvLowPower) &&
  349. ((gap->state == GapStateAdvFast) || (gap->state == GapStateAdvLowPower))) {
  350. // Stop advertising
  351. status = aci_gap_set_non_discoverable();
  352. if(status) {
  353. FURI_LOG_E(TAG, "Stop Advertising Failed, result: %d", status);
  354. }
  355. }
  356. // Configure advertising
  357. status = aci_gap_set_discoverable(
  358. ADV_IND,
  359. min_interval,
  360. max_interval,
  361. PUBLIC_ADDR,
  362. 0,
  363. strlen(gap->service.adv_name),
  364. (uint8_t*)gap->service.adv_name,
  365. gap->service.adv_svc_uuid_len,
  366. gap->service.adv_svc_uuid,
  367. 0,
  368. 0);
  369. if(status) {
  370. FURI_LOG_E(TAG, "Set discoverable err: %d", status);
  371. }
  372. gap->state = new_state;
  373. GapEvent event = {.type = GapEventTypeStartAdvertising};
  374. gap->on_event_cb(event, gap->context);
  375. osTimerStart(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
  376. }
  377. static void gap_advertise_stop() {
  378. if(gap->state > GapStateIdle) {
  379. if(gap->state == GapStateConnected) {
  380. // Terminate connection
  381. aci_gap_terminate(gap->service.connection_handle, 0x13);
  382. }
  383. // Stop advertising
  384. osTimerStop(gap->advertise_timer);
  385. aci_gap_set_non_discoverable();
  386. gap->state = GapStateIdle;
  387. }
  388. GapEvent event = {.type = GapEventTypeStopAdvertising};
  389. gap->on_event_cb(event, gap->context);
  390. }
  391. void gap_start_advertising() {
  392. osMutexAcquire(gap->state_mutex, osWaitForever);
  393. if(gap->state == GapStateIdle) {
  394. gap->state = GapStateStartingAdv;
  395. FURI_LOG_I(TAG, "Start advertising");
  396. gap->enable_adv = true;
  397. GapCommand command = GapCommandAdvFast;
  398. furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
  399. }
  400. osMutexRelease(gap->state_mutex);
  401. }
  402. void gap_stop_advertising() {
  403. osMutexAcquire(gap->state_mutex, osWaitForever);
  404. if(gap->state > GapStateIdle) {
  405. FURI_LOG_I(TAG, "Stop advertising");
  406. gap->enable_adv = false;
  407. GapCommand command = GapCommandAdvStop;
  408. furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
  409. }
  410. osMutexRelease(gap->state_mutex);
  411. }
  412. static void gap_advetise_timer_callback(void* context) {
  413. GapCommand command = GapCommandAdvLowPower;
  414. furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
  415. }
  416. bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
  417. if(!ble_glue_is_radio_stack_ready()) {
  418. return false;
  419. }
  420. gap = malloc(sizeof(Gap));
  421. gap->config = config;
  422. srand(DWT->CYCCNT);
  423. // Create advertising timer
  424. gap->advertise_timer = osTimerNew(gap_advetise_timer_callback, osTimerOnce, NULL, NULL);
  425. // Initialization of GATT & GAP layer
  426. gap->service.adv_name = config->adv_name;
  427. gap_init_svc(gap);
  428. // Initialization of the BLE Services
  429. SVCCTL_Init();
  430. // Initialization of the GAP state
  431. gap->state_mutex = osMutexNew(NULL);
  432. gap->state = GapStateIdle;
  433. gap->service.connection_handle = 0xFFFF;
  434. gap->enable_adv = true;
  435. // Thread configuration
  436. gap->thread = furi_thread_alloc();
  437. furi_thread_set_name(gap->thread, "BleGapDriver");
  438. furi_thread_set_stack_size(gap->thread, 1024);
  439. furi_thread_set_context(gap->thread, gap);
  440. furi_thread_set_callback(gap->thread, gap_app);
  441. furi_thread_start(gap->thread);
  442. // Command queue allocation
  443. gap->command_queue = osMessageQueueNew(8, sizeof(GapCommand), NULL);
  444. uint8_t adv_service_uid[2];
  445. gap->service.adv_svc_uuid_len = 1;
  446. adv_service_uid[0] = gap->config->adv_service_uuid & 0xff;
  447. adv_service_uid[1] = gap->config->adv_service_uuid >> 8;
  448. set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
  449. // Set callback
  450. gap->on_event_cb = on_event_cb;
  451. gap->context = context;
  452. return true;
  453. }
  454. GapState gap_get_state() {
  455. GapState state;
  456. if(gap) {
  457. osMutexAcquire(gap->state_mutex, osWaitForever);
  458. state = gap->state;
  459. osMutexRelease(gap->state_mutex);
  460. } else {
  461. state = GapStateUninitialized;
  462. }
  463. return state;
  464. }
  465. void gap_start_scan(GapScanCallback callback, void* context) {
  466. furi_assert(callback);
  467. gap_scan = malloc(sizeof(GapScan));
  468. gap_scan->callback = callback;
  469. gap_scan->context = context;
  470. // Scan interval 250 ms
  471. hci_le_set_scan_parameters(1, 4000, 200, 0, 0);
  472. hci_le_set_scan_enable(1, 1);
  473. }
  474. void gap_stop_scan() {
  475. furi_assert(gap_scan);
  476. hci_le_set_scan_enable(0, 1);
  477. free(gap_scan);
  478. gap_scan = NULL;
  479. }
  480. void gap_thread_stop() {
  481. if(gap) {
  482. osMutexAcquire(gap->state_mutex, osWaitForever);
  483. gap->enable_adv = false;
  484. GapCommand command = GapCommandKillThread;
  485. osMessageQueuePut(gap->command_queue, &command, 0, osWaitForever);
  486. osMutexRelease(gap->state_mutex);
  487. furi_thread_join(gap->thread);
  488. furi_thread_free(gap->thread);
  489. // Free resources
  490. osMutexDelete(gap->state_mutex);
  491. osMessageQueueDelete(gap->command_queue);
  492. osTimerStop(gap->advertise_timer);
  493. while(xTimerIsTimerActive(gap->advertise_timer) == pdTRUE) osDelay(1);
  494. furi_check(osTimerDelete(gap->advertise_timer) == osOK);
  495. free(gap);
  496. gap = NULL;
  497. }
  498. }
  499. static int32_t gap_app(void* context) {
  500. GapCommand command;
  501. while(1) {
  502. osStatus_t status = osMessageQueueGet(gap->command_queue, &command, NULL, osWaitForever);
  503. if(status != osOK) {
  504. FURI_LOG_E(TAG, "Message queue get error: %d", status);
  505. continue;
  506. }
  507. osMutexAcquire(gap->state_mutex, osWaitForever);
  508. if(command == GapCommandKillThread) {
  509. break;
  510. }
  511. if(command == GapCommandAdvFast) {
  512. gap_advertise_start(GapStateAdvFast);
  513. } else if(command == GapCommandAdvLowPower) {
  514. gap_advertise_start(GapStateAdvLowPower);
  515. } else if(command == GapCommandAdvStop) {
  516. gap_advertise_stop();
  517. }
  518. osMutexRelease(gap->state_mutex);
  519. }
  520. return 0;
  521. }