gap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. FURI_LOG_I(TAG, "Pass key request event. Pin: %06d", pin);
  165. GapEvent event = {.type = GapEventTypePinCodeShow, .data.pin_code = pin};
  166. gap->on_event_cb(event, gap->context);
  167. } break;
  168. case EVT_BLUE_ATT_EXCHANGE_MTU_RESP: {
  169. aci_att_exchange_mtu_resp_event_rp0* pr = (void*)blue_evt->data;
  170. FURI_LOG_I(TAG, "Rx MTU size: %d", pr->Server_RX_MTU);
  171. // Set maximum packet size given header size is 3 bytes
  172. GapEvent event = {
  173. .type = GapEventTypeUpdateMTU, .data.max_packet_size = pr->Server_RX_MTU - 3};
  174. gap->on_event_cb(event, gap->context);
  175. } break;
  176. case EVT_BLUE_GAP_AUTHORIZATION_REQUEST:
  177. FURI_LOG_D(TAG, "Authorization request event");
  178. break;
  179. case EVT_BLUE_GAP_SLAVE_SECURITY_INITIATED:
  180. FURI_LOG_D(TAG, "Slave security initiated");
  181. break;
  182. case EVT_BLUE_GAP_BOND_LOST:
  183. FURI_LOG_D(TAG, "Bond lost event. Start rebonding");
  184. aci_gap_allow_rebond(gap->service.connection_handle);
  185. break;
  186. case EVT_BLUE_GAP_DEVICE_FOUND:
  187. FURI_LOG_D(TAG, "Device found event");
  188. break;
  189. case EVT_BLUE_GAP_ADDR_NOT_RESOLVED:
  190. FURI_LOG_D(TAG, "Address not resolved event");
  191. break;
  192. case EVT_BLUE_GAP_KEYPRESS_NOTIFICATION:
  193. FURI_LOG_D(TAG, "Key press notification event");
  194. break;
  195. case EVT_BLUE_GAP_NUMERIC_COMPARISON_VALUE: {
  196. uint32_t pin =
  197. ((aci_gap_numeric_comparison_value_event_rp0*)(blue_evt->data))->Numeric_Value;
  198. FURI_LOG_I(TAG, "Verify numeric comparison: %06d", pin);
  199. GapEvent event = {.type = GapEventTypePinCodeVerify, .data.pin_code = pin};
  200. bool result = gap->on_event_cb(event, gap->context);
  201. aci_gap_numeric_comparison_value_confirm_yesno(gap->service.connection_handle, result);
  202. break;
  203. }
  204. case EVT_BLUE_GAP_PAIRING_CMPLT:
  205. pairing_complete = (aci_gap_pairing_complete_event_rp0*)blue_evt->data;
  206. if(pairing_complete->Status) {
  207. FURI_LOG_E(
  208. TAG,
  209. "Pairing failed with status: %d. Terminating connection",
  210. pairing_complete->Status);
  211. aci_gap_terminate(gap->service.connection_handle, 5);
  212. } else {
  213. FURI_LOG_I(TAG, "Pairing complete");
  214. GapEvent event = {.type = GapEventTypeConnected};
  215. gap->on_event_cb(event, gap->context);
  216. }
  217. break;
  218. case EVT_BLUE_GAP_PROCEDURE_COMPLETE:
  219. FURI_LOG_D(TAG, "Procedure complete event");
  220. break;
  221. case EVT_BLUE_L2CAP_CONNECTION_UPDATE_RESP: {
  222. uint16_t result =
  223. ((aci_l2cap_connection_update_resp_event_rp0*)(blue_evt->data))->Result;
  224. if(result == 0) {
  225. FURI_LOG_D(TAG, "Connection parameters accepted");
  226. } else if(result == 1) {
  227. FURI_LOG_D(TAG, "Connection parameters denied");
  228. }
  229. break;
  230. }
  231. }
  232. default:
  233. break;
  234. }
  235. if(gap) {
  236. osMutexRelease(gap->state_mutex);
  237. }
  238. return SVCCTL_UserEvtFlowEnable;
  239. }
  240. static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) {
  241. if(uid_len == 2) {
  242. gap->service.adv_svc_uuid[0] = AD_TYPE_16_BIT_SERV_UUID;
  243. } else if(uid_len == 4) {
  244. gap->service.adv_svc_uuid[0] = AD_TYPE_32_BIT_SERV_UUID;
  245. } else if(uid_len == 16) {
  246. gap->service.adv_svc_uuid[0] = AD_TYPE_128_BIT_SERV_UUID_CMPLT_LIST;
  247. }
  248. memcpy(&gap->service.adv_svc_uuid[gap->service.adv_svc_uuid_len], uid, uid_len);
  249. gap->service.adv_svc_uuid_len += uid_len;
  250. }
  251. static void gap_init_svc(Gap* gap) {
  252. tBleStatus status;
  253. uint32_t srd_bd_addr[2];
  254. // HCI Reset to synchronise BLE Stack
  255. hci_reset();
  256. // Configure mac address
  257. aci_hal_write_config_data(
  258. CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, gap->config->mac_address);
  259. /* Static random Address
  260. * The two upper bits shall be set to 1
  261. * The lowest 32bits is read from the UDN to differentiate between devices
  262. * The RNG may be used to provide a random number on each power on
  263. */
  264. srd_bd_addr[1] = 0x0000ED6E;
  265. srd_bd_addr[0] = LL_FLASH_GetUDN();
  266. aci_hal_write_config_data(
  267. CONFIG_DATA_RANDOM_ADDRESS_OFFSET, CONFIG_DATA_RANDOM_ADDRESS_LEN, (uint8_t*)srd_bd_addr);
  268. // Set Identity root key used to derive LTK and CSRK
  269. aci_hal_write_config_data(CONFIG_DATA_IR_OFFSET, CONFIG_DATA_IR_LEN, (uint8_t*)gap_irk);
  270. // Set Encryption root key used to derive LTK and CSRK
  271. aci_hal_write_config_data(CONFIG_DATA_ER_OFFSET, CONFIG_DATA_ER_LEN, (uint8_t*)gap_erk);
  272. // Set TX Power to 0 dBm
  273. aci_hal_set_tx_power_level(1, 0x19);
  274. // Initialize GATT interface
  275. aci_gatt_init();
  276. // Initialize GAP interface
  277. // Skip fist symbol AD_TYPE_COMPLETE_LOCAL_NAME
  278. char* name = gap->service.adv_name + 1;
  279. aci_gap_init(
  280. GAP_PERIPHERAL_ROLE,
  281. 0,
  282. strlen(name),
  283. &gap->service.gap_svc_handle,
  284. &gap->service.dev_name_char_handle,
  285. &gap->service.appearance_char_handle);
  286. // Set GAP characteristics
  287. status = aci_gatt_update_char_value(
  288. gap->service.gap_svc_handle,
  289. gap->service.dev_name_char_handle,
  290. 0,
  291. strlen(name),
  292. (uint8_t*)name);
  293. if(status) {
  294. FURI_LOG_E(TAG, "Failed updating name characteristic: %d", status);
  295. }
  296. uint8_t gap_appearence_char_uuid[2] = {
  297. gap->config->appearance_char & 0xff, gap->config->appearance_char >> 8};
  298. status = aci_gatt_update_char_value(
  299. gap->service.gap_svc_handle,
  300. gap->service.appearance_char_handle,
  301. 0,
  302. 2,
  303. gap_appearence_char_uuid);
  304. if(status) {
  305. FURI_LOG_E(TAG, "Failed updating appearence characteristic: %d", status);
  306. }
  307. // Set default PHY
  308. hci_le_set_default_phy(ALL_PHYS_PREFERENCE, TX_2M_PREFERRED, RX_2M_PREFERRED);
  309. // Set I/O capability
  310. bool keypress_supported = false;
  311. if(gap->config->pairing_method == GapPairingPinCodeShow) {
  312. aci_gap_set_io_capability(IO_CAP_DISPLAY_ONLY);
  313. } else if(gap->config->pairing_method == GapPairingPinCodeVerifyYesNo) {
  314. aci_gap_set_io_capability(IO_CAP_DISPLAY_YES_NO);
  315. keypress_supported = true;
  316. }
  317. // Setup authentication
  318. aci_gap_set_authentication_requirement(
  319. gap->config->bonding_mode,
  320. CFG_MITM_PROTECTION,
  321. CFG_SC_SUPPORT,
  322. keypress_supported,
  323. CFG_ENCRYPTION_KEY_SIZE_MIN,
  324. CFG_ENCRYPTION_KEY_SIZE_MAX,
  325. CFG_USED_FIXED_PIN,
  326. 0,
  327. PUBLIC_ADDR);
  328. // Configure whitelist
  329. aci_gap_configure_whitelist();
  330. }
  331. static void gap_advertise_start(GapState new_state) {
  332. tBleStatus status;
  333. uint16_t min_interval;
  334. uint16_t max_interval;
  335. if(new_state == GapStateAdvFast) {
  336. min_interval = 0x80; // 80 ms
  337. max_interval = 0xa0; // 100 ms
  338. } else {
  339. min_interval = 0x0640; // 1 s
  340. max_interval = 0x0fa0; // 2.5 s
  341. }
  342. // Stop advertising timer
  343. osTimerStop(gap->advertise_timer);
  344. if((new_state == GapStateAdvLowPower) &&
  345. ((gap->state == GapStateAdvFast) || (gap->state == GapStateAdvLowPower))) {
  346. // Stop advertising
  347. status = aci_gap_set_non_discoverable();
  348. if(status) {
  349. FURI_LOG_E(TAG, "Stop Advertising Failed, result: %d", status);
  350. }
  351. }
  352. // Configure advertising
  353. status = aci_gap_set_discoverable(
  354. ADV_IND,
  355. min_interval,
  356. max_interval,
  357. PUBLIC_ADDR,
  358. 0,
  359. strlen(gap->service.adv_name),
  360. (uint8_t*)gap->service.adv_name,
  361. gap->service.adv_svc_uuid_len,
  362. gap->service.adv_svc_uuid,
  363. 0,
  364. 0);
  365. if(status) {
  366. FURI_LOG_E(TAG, "Set discoverable err: %d", status);
  367. }
  368. gap->state = new_state;
  369. GapEvent event = {.type = GapEventTypeStartAdvertising};
  370. gap->on_event_cb(event, gap->context);
  371. osTimerStart(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
  372. }
  373. static void gap_advertise_stop() {
  374. if(gap->state > GapStateIdle) {
  375. if(gap->state == GapStateConnected) {
  376. // Terminate connection
  377. aci_gap_terminate(gap->service.connection_handle, 0x13);
  378. }
  379. // Stop advertising
  380. osTimerStop(gap->advertise_timer);
  381. aci_gap_set_non_discoverable();
  382. gap->state = GapStateIdle;
  383. }
  384. GapEvent event = {.type = GapEventTypeStopAdvertising};
  385. gap->on_event_cb(event, gap->context);
  386. }
  387. void gap_start_advertising() {
  388. osMutexAcquire(gap->state_mutex, osWaitForever);
  389. if(gap->state == GapStateIdle) {
  390. gap->state = GapStateStartingAdv;
  391. FURI_LOG_I(TAG, "Start advertising");
  392. gap->enable_adv = true;
  393. GapCommand command = GapCommandAdvFast;
  394. furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
  395. }
  396. osMutexRelease(gap->state_mutex);
  397. }
  398. void gap_stop_advertising() {
  399. osMutexAcquire(gap->state_mutex, osWaitForever);
  400. if(gap->state > GapStateIdle) {
  401. FURI_LOG_I(TAG, "Stop advertising");
  402. gap->enable_adv = false;
  403. GapCommand command = GapCommandAdvStop;
  404. furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
  405. }
  406. osMutexRelease(gap->state_mutex);
  407. }
  408. static void gap_advetise_timer_callback(void* context) {
  409. GapCommand command = GapCommandAdvLowPower;
  410. furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
  411. }
  412. bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
  413. if(!ble_glue_is_radio_stack_ready()) {
  414. return false;
  415. }
  416. gap = furi_alloc(sizeof(Gap));
  417. gap->config = config;
  418. srand(DWT->CYCCNT);
  419. // Create advertising timer
  420. gap->advertise_timer = osTimerNew(gap_advetise_timer_callback, osTimerOnce, NULL, NULL);
  421. // Initialization of GATT & GAP layer
  422. gap->service.adv_name = config->adv_name;
  423. gap_init_svc(gap);
  424. // Initialization of the BLE Services
  425. SVCCTL_Init();
  426. // Initialization of the GAP state
  427. gap->state_mutex = osMutexNew(NULL);
  428. gap->state = GapStateIdle;
  429. gap->service.connection_handle = 0xFFFF;
  430. gap->enable_adv = true;
  431. // Thread configuration
  432. gap->thread = furi_thread_alloc();
  433. furi_thread_set_name(gap->thread, "BleGapWorker");
  434. furi_thread_set_stack_size(gap->thread, 1024);
  435. furi_thread_set_context(gap->thread, gap);
  436. furi_thread_set_callback(gap->thread, gap_app);
  437. furi_thread_start(gap->thread);
  438. // Command queue allocation
  439. gap->command_queue = osMessageQueueNew(8, sizeof(GapCommand), NULL);
  440. uint8_t adv_service_uid[2];
  441. gap->service.adv_svc_uuid_len = 1;
  442. adv_service_uid[0] = gap->config->adv_service_uuid & 0xff;
  443. adv_service_uid[1] = gap->config->adv_service_uuid >> 8;
  444. set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
  445. // Set callback
  446. gap->on_event_cb = on_event_cb;
  447. gap->context = context;
  448. return true;
  449. }
  450. GapState gap_get_state() {
  451. GapState state;
  452. if(gap) {
  453. osMutexAcquire(gap->state_mutex, osWaitForever);
  454. state = gap->state;
  455. osMutexRelease(gap->state_mutex);
  456. } else {
  457. state = GapStateUninitialized;
  458. }
  459. return state;
  460. }
  461. void gap_start_scan(GapScanCallback callback, void* context) {
  462. furi_assert(callback);
  463. gap_scan = furi_alloc(sizeof(GapScan));
  464. gap_scan->callback = callback;
  465. gap_scan->context = context;
  466. // Scan interval 250 ms
  467. hci_le_set_scan_parameters(1, 4000, 200, 0, 0);
  468. hci_le_set_scan_enable(1, 1);
  469. }
  470. void gap_stop_scan() {
  471. furi_assert(gap_scan);
  472. hci_le_set_scan_enable(0, 1);
  473. free(gap_scan);
  474. gap_scan = NULL;
  475. }
  476. void gap_thread_stop() {
  477. if(gap) {
  478. osMutexAcquire(gap->state_mutex, osWaitForever);
  479. gap->enable_adv = false;
  480. GapCommand command = GapCommandKillThread;
  481. osMessageQueuePut(gap->command_queue, &command, 0, osWaitForever);
  482. osMutexRelease(gap->state_mutex);
  483. furi_thread_join(gap->thread);
  484. furi_thread_free(gap->thread);
  485. // Free resources
  486. osMutexDelete(gap->state_mutex);
  487. osMessageQueueDelete(gap->command_queue);
  488. osTimerStop(gap->advertise_timer);
  489. while(xTimerIsTimerActive(gap->advertise_timer) == pdTRUE) osDelay(1);
  490. furi_check(osTimerDelete(gap->advertise_timer) == osOK);
  491. free(gap);
  492. gap = NULL;
  493. }
  494. }
  495. static int32_t gap_app(void* context) {
  496. GapCommand command;
  497. while(1) {
  498. osStatus_t status = osMessageQueueGet(gap->command_queue, &command, NULL, osWaitForever);
  499. if(status != osOK) {
  500. FURI_LOG_E(TAG, "Message queue get error: %d", status);
  501. continue;
  502. }
  503. osMutexAcquire(gap->state_mutex, osWaitForever);
  504. if(command == GapCommandKillThread) {
  505. break;
  506. }
  507. if(command == GapCommandAdvFast) {
  508. gap_advertise_start(GapStateAdvFast);
  509. } else if(command == GapCommandAdvLowPower) {
  510. gap_advertise_start(GapStateAdvLowPower);
  511. } else if(command == GapCommandAdvStop) {
  512. gap_advertise_stop();
  513. }
  514. osMutexRelease(gap->state_mutex);
  515. }
  516. return 0;
  517. }