gap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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: %06lu", 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); //-V595
  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. // Configure mac address
  252. aci_hal_write_config_data(
  253. CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, gap->config->mac_address);
  254. /* Static random Address
  255. * The two upper bits shall be set to 1
  256. * The lowest 32bits is read from the UDN to differentiate between devices
  257. * The RNG may be used to provide a random number on each power on
  258. */
  259. srd_bd_addr[1] = 0x0000ED6E;
  260. srd_bd_addr[0] = LL_FLASH_GetUDN();
  261. aci_hal_write_config_data(
  262. CONFIG_DATA_RANDOM_ADDRESS_OFFSET, CONFIG_DATA_RANDOM_ADDRESS_LEN, (uint8_t*)srd_bd_addr);
  263. // Set Identity root key used to derive LTK and CSRK
  264. aci_hal_write_config_data(CONFIG_DATA_IR_OFFSET, CONFIG_DATA_IR_LEN, (uint8_t*)gap_irk);
  265. // Set Encryption root key used to derive LTK and CSRK
  266. aci_hal_write_config_data(CONFIG_DATA_ER_OFFSET, CONFIG_DATA_ER_LEN, (uint8_t*)gap_erk);
  267. // Set TX Power to 0 dBm
  268. aci_hal_set_tx_power_level(1, 0x19);
  269. // Initialize GATT interface
  270. aci_gatt_init();
  271. // Initialize GAP interface
  272. // Skip fist symbol AD_TYPE_COMPLETE_LOCAL_NAME
  273. char* name = gap->service.adv_name + 1;
  274. aci_gap_init(
  275. GAP_PERIPHERAL_ROLE,
  276. 0,
  277. strlen(name),
  278. &gap->service.gap_svc_handle,
  279. &gap->service.dev_name_char_handle,
  280. &gap->service.appearance_char_handle);
  281. // Set GAP characteristics
  282. status = aci_gatt_update_char_value(
  283. gap->service.gap_svc_handle,
  284. gap->service.dev_name_char_handle,
  285. 0,
  286. strlen(name),
  287. (uint8_t*)name);
  288. if(status) {
  289. FURI_LOG_E(TAG, "Failed updating name characteristic: %d", status);
  290. }
  291. uint8_t gap_appearence_char_uuid[2] = {
  292. gap->config->appearance_char & 0xff, gap->config->appearance_char >> 8};
  293. status = aci_gatt_update_char_value(
  294. gap->service.gap_svc_handle,
  295. gap->service.appearance_char_handle,
  296. 0,
  297. 2,
  298. gap_appearence_char_uuid);
  299. if(status) {
  300. FURI_LOG_E(TAG, "Failed updating appearence characteristic: %d", status);
  301. }
  302. // Set default PHY
  303. hci_le_set_default_phy(ALL_PHYS_PREFERENCE, TX_2M_PREFERRED, RX_2M_PREFERRED);
  304. // Set I/O capability
  305. bool keypress_supported = false;
  306. if(gap->config->pairing_method == GapPairingPinCodeShow) {
  307. aci_gap_set_io_capability(IO_CAP_DISPLAY_ONLY);
  308. } else if(gap->config->pairing_method == GapPairingPinCodeVerifyYesNo) {
  309. aci_gap_set_io_capability(IO_CAP_DISPLAY_YES_NO);
  310. keypress_supported = true;
  311. }
  312. // Setup authentication
  313. aci_gap_set_authentication_requirement(
  314. gap->config->bonding_mode,
  315. CFG_MITM_PROTECTION,
  316. CFG_SC_SUPPORT,
  317. keypress_supported,
  318. CFG_ENCRYPTION_KEY_SIZE_MIN,
  319. CFG_ENCRYPTION_KEY_SIZE_MAX,
  320. CFG_USED_FIXED_PIN,
  321. 0,
  322. PUBLIC_ADDR);
  323. // Configure whitelist
  324. aci_gap_configure_whitelist();
  325. }
  326. static void gap_advertise_start(GapState new_state) {
  327. tBleStatus status;
  328. uint16_t min_interval;
  329. uint16_t max_interval;
  330. if(new_state == GapStateAdvFast) {
  331. min_interval = 0x80; // 80 ms
  332. max_interval = 0xa0; // 100 ms
  333. } else {
  334. min_interval = 0x0640; // 1 s
  335. max_interval = 0x0fa0; // 2.5 s
  336. }
  337. // Stop advertising timer
  338. furi_timer_stop(gap->advertise_timer);
  339. if((new_state == GapStateAdvLowPower) &&
  340. ((gap->state == GapStateAdvFast) || (gap->state == GapStateAdvLowPower))) {
  341. // Stop advertising
  342. status = aci_gap_set_non_discoverable();
  343. if(status) {
  344. FURI_LOG_E(TAG, "set_non_discoverable failed %d", status);
  345. } else {
  346. FURI_LOG_D(TAG, "set_non_discoverable success");
  347. }
  348. }
  349. // Configure advertising
  350. status = aci_gap_set_discoverable(
  351. ADV_IND,
  352. min_interval,
  353. max_interval,
  354. PUBLIC_ADDR,
  355. 0,
  356. strlen(gap->service.adv_name),
  357. (uint8_t*)gap->service.adv_name,
  358. gap->service.adv_svc_uuid_len,
  359. gap->service.adv_svc_uuid,
  360. 0,
  361. 0);
  362. if(status) {
  363. FURI_LOG_E(TAG, "set_discoverable failed %d", status);
  364. }
  365. gap->state = new_state;
  366. GapEvent event = {.type = GapEventTypeStartAdvertising};
  367. gap->on_event_cb(event, gap->context);
  368. furi_timer_start(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
  369. }
  370. static void gap_advertise_stop() {
  371. tBleStatus ret;
  372. if(gap->state > GapStateIdle) {
  373. if(gap->state == GapStateConnected) {
  374. // Terminate connection
  375. ret = aci_gap_terminate(gap->service.connection_handle, 0x13);
  376. if(ret != BLE_STATUS_SUCCESS) {
  377. FURI_LOG_E(TAG, "terminate failed %d", ret);
  378. } else {
  379. FURI_LOG_D(TAG, "terminate success");
  380. }
  381. }
  382. // Stop advertising
  383. furi_timer_stop(gap->advertise_timer);
  384. ret = aci_gap_set_non_discoverable();
  385. if(ret != BLE_STATUS_SUCCESS) {
  386. FURI_LOG_E(TAG, "set_non_discoverable failed %d", ret);
  387. } else {
  388. FURI_LOG_D(TAG, "set_non_discoverable success");
  389. }
  390. gap->state = GapStateIdle;
  391. }
  392. GapEvent event = {.type = GapEventTypeStopAdvertising};
  393. gap->on_event_cb(event, gap->context);
  394. }
  395. void gap_start_advertising() {
  396. furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
  397. if(gap->state == GapStateIdle) {
  398. gap->state = GapStateStartingAdv;
  399. FURI_LOG_I(TAG, "Start advertising");
  400. gap->enable_adv = true;
  401. GapCommand command = GapCommandAdvFast;
  402. furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
  403. }
  404. furi_mutex_release(gap->state_mutex);
  405. }
  406. void gap_stop_advertising() {
  407. furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
  408. if(gap->state > GapStateIdle) {
  409. FURI_LOG_I(TAG, "Stop advertising");
  410. gap->enable_adv = false;
  411. GapCommand command = GapCommandAdvStop;
  412. furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
  413. }
  414. furi_mutex_release(gap->state_mutex);
  415. }
  416. static void gap_advetise_timer_callback(void* context) {
  417. UNUSED(context);
  418. GapCommand command = GapCommandAdvLowPower;
  419. furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
  420. }
  421. bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
  422. if(!ble_glue_is_radio_stack_ready()) {
  423. return false;
  424. }
  425. gap = malloc(sizeof(Gap));
  426. gap->config = config;
  427. // Create advertising timer
  428. gap->advertise_timer = furi_timer_alloc(gap_advetise_timer_callback, FuriTimerTypeOnce, NULL);
  429. // Initialization of GATT & GAP layer
  430. gap->service.adv_name = config->adv_name;
  431. gap_init_svc(gap);
  432. // Initialization of the BLE Services
  433. SVCCTL_Init();
  434. // Initialization of the GAP state
  435. gap->state_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  436. gap->state = GapStateIdle;
  437. gap->service.connection_handle = 0xFFFF;
  438. gap->enable_adv = true;
  439. // Thread configuration
  440. gap->thread = furi_thread_alloc_ex("BleGapDriver", 1024, gap_app, gap);
  441. furi_thread_start(gap->thread);
  442. // Command queue allocation
  443. gap->command_queue = furi_message_queue_alloc(8, sizeof(GapCommand));
  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. furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
  458. state = gap->state;
  459. furi_mutex_release(gap->state_mutex);
  460. } else {
  461. state = GapStateUninitialized;
  462. }
  463. return state;
  464. }
  465. void gap_thread_stop() {
  466. if(gap) {
  467. furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
  468. gap->enable_adv = false;
  469. GapCommand command = GapCommandKillThread;
  470. furi_message_queue_put(gap->command_queue, &command, FuriWaitForever);
  471. furi_mutex_release(gap->state_mutex);
  472. furi_thread_join(gap->thread);
  473. furi_thread_free(gap->thread);
  474. // Free resources
  475. furi_mutex_free(gap->state_mutex);
  476. furi_message_queue_free(gap->command_queue);
  477. furi_timer_stop(gap->advertise_timer);
  478. while(xTimerIsTimerActive(gap->advertise_timer) == pdTRUE) furi_delay_tick(1);
  479. furi_timer_free(gap->advertise_timer);
  480. free(gap);
  481. gap = NULL;
  482. }
  483. }
  484. static int32_t gap_app(void* context) {
  485. UNUSED(context);
  486. GapCommand command;
  487. while(1) {
  488. FuriStatus status = furi_message_queue_get(gap->command_queue, &command, FuriWaitForever);
  489. if(status != FuriStatusOk) {
  490. FURI_LOG_E(TAG, "Message queue get error: %d", status);
  491. continue;
  492. }
  493. furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
  494. if(command == GapCommandKillThread) {
  495. break;
  496. }
  497. if(command == GapCommandAdvFast) {
  498. gap_advertise_start(GapStateAdvFast);
  499. } else if(command == GapCommandAdvLowPower) {
  500. gap_advertise_start(GapStateAdvLowPower);
  501. } else if(command == GapCommandAdvStop) {
  502. gap_advertise_stop();
  503. }
  504. furi_mutex_release(gap->state_mutex);
  505. }
  506. return 0;
  507. }