gap.c 20 KB

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