gap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. #include "gap.h"
  2. #include "app_common.h"
  3. #include <ble/ble.h>
  4. #include <furi_hal.h>
  5. #include <furi.h>
  6. #define TAG "BtGap"
  7. #define FAST_ADV_TIMEOUT 30000
  8. #define INITIAL_ADV_TIMEOUT 60000
  9. #define GAP_INTERVAL_TO_MS(x) (uint16_t)((x)*1.25)
  10. typedef struct {
  11. uint16_t gap_svc_handle;
  12. uint16_t dev_name_char_handle;
  13. uint16_t appearance_char_handle;
  14. uint16_t connection_handle;
  15. uint8_t adv_svc_uuid_len;
  16. uint8_t adv_svc_uuid[20];
  17. char* adv_name;
  18. } GapSvc;
  19. typedef struct {
  20. GapSvc service;
  21. GapConfig* config;
  22. GapConnectionParams connection_params;
  23. GapState state;
  24. FuriMutex* state_mutex;
  25. GapEventCallback on_event_cb;
  26. void* context;
  27. FuriTimer* advertise_timer;
  28. FuriThread* thread;
  29. FuriMessageQueue* command_queue;
  30. bool enable_adv;
  31. } Gap;
  32. typedef enum {
  33. GapCommandAdvFast,
  34. GapCommandAdvLowPower,
  35. GapCommandAdvStop,
  36. GapCommandKillThread,
  37. } GapCommand;
  38. // Identity root key
  39. static const uint8_t gap_irk[16] =
  40. {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
  41. // Encryption root key
  42. static const uint8_t gap_erk[16] =
  43. {0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21};
  44. static Gap* gap = NULL;
  45. static void gap_advertise_start(GapState new_state);
  46. static int32_t gap_app(void* context);
  47. static void gap_verify_connection_parameters(Gap* gap) {
  48. furi_assert(gap);
  49. FURI_LOG_I(
  50. TAG,
  51. "Connection parameters: Connection Interval: %d (%d ms), Slave Latency: %d, Supervision Timeout: %d",
  52. gap->connection_params.conn_interval,
  53. GAP_INTERVAL_TO_MS(gap->connection_params.conn_interval),
  54. gap->connection_params.slave_latency,
  55. gap->connection_params.supervisor_timeout);
  56. // Send connection parameters request update if necessary
  57. GapConnectionParamsRequest* params = &gap->config->conn_param;
  58. if(params->conn_int_min > gap->connection_params.conn_interval ||
  59. params->conn_int_max < gap->connection_params.conn_interval) {
  60. FURI_LOG_W(TAG, "Unsupported connection interval. Request connection parameters update");
  61. if(aci_l2cap_connection_parameter_update_req(
  62. gap->service.connection_handle,
  63. params->conn_int_min,
  64. params->conn_int_max,
  65. gap->connection_params.slave_latency,
  66. gap->connection_params.supervisor_timeout)) {
  67. FURI_LOG_E(TAG, "Failed to request connection parameters update");
  68. }
  69. }
  70. }
  71. SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
  72. hci_event_pckt* event_pckt;
  73. evt_le_meta_event* meta_evt;
  74. evt_blecore_aci* blue_evt;
  75. hci_le_phy_update_complete_event_rp0* evt_le_phy_update_complete;
  76. uint8_t tx_phy;
  77. uint8_t rx_phy;
  78. tBleStatus ret = BLE_STATUS_INVALID_PARAMS;
  79. event_pckt = (hci_event_pckt*)((hci_uart_pckt*)pckt)->data;
  80. if(gap) {
  81. furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
  82. }
  83. switch(event_pckt->evt) {
  84. case HCI_DISCONNECTION_COMPLETE_EVT_CODE: {
  85. hci_disconnection_complete_event_rp0* disconnection_complete_event =
  86. (hci_disconnection_complete_event_rp0*)event_pckt->data;
  87. if(disconnection_complete_event->Connection_Handle == gap->service.connection_handle) {
  88. gap->service.connection_handle = 0;
  89. gap->state = GapStateIdle;
  90. FURI_LOG_I(
  91. TAG, "Disconnect from client. Reason: %02X", disconnection_complete_event->Reason);
  92. }
  93. // Enterprise sleep
  94. furi_delay_us(666 + 666);
  95. if(gap->enable_adv) {
  96. // Restart advertising
  97. gap_advertise_start(GapStateAdvFast);
  98. }
  99. GapEvent event = {.type = GapEventTypeDisconnected};
  100. gap->on_event_cb(event, gap->context);
  101. } break;
  102. case HCI_LE_META_EVT_CODE:
  103. meta_evt = (evt_le_meta_event*)event_pckt->data;
  104. switch(meta_evt->subevent) {
  105. case HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE: {
  106. hci_le_connection_update_complete_event_rp0* event =
  107. (hci_le_connection_update_complete_event_rp0*)meta_evt->data;
  108. gap->connection_params.conn_interval = event->Conn_Interval;
  109. gap->connection_params.slave_latency = event->Conn_Latency;
  110. gap->connection_params.supervisor_timeout = event->Supervision_Timeout;
  111. FURI_LOG_I(TAG, "Connection parameters event complete");
  112. gap_verify_connection_parameters(gap);
  113. break;
  114. }
  115. case HCI_LE_PHY_UPDATE_COMPLETE_SUBEVT_CODE:
  116. evt_le_phy_update_complete = (hci_le_phy_update_complete_event_rp0*)meta_evt->data;
  117. if(evt_le_phy_update_complete->Status) {
  118. FURI_LOG_E(
  119. TAG, "Update PHY failed, status %d", evt_le_phy_update_complete->Status);
  120. } else {
  121. FURI_LOG_I(TAG, "Update PHY succeed");
  122. }
  123. ret = hci_le_read_phy(gap->service.connection_handle, &tx_phy, &rx_phy);
  124. if(ret) {
  125. FURI_LOG_E(TAG, "Read PHY failed, status: %d", ret);
  126. } else {
  127. FURI_LOG_I(TAG, "PHY Params TX = %d, RX = %d ", tx_phy, rx_phy);
  128. }
  129. break;
  130. case HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE: {
  131. hci_le_connection_complete_event_rp0* event =
  132. (hci_le_connection_complete_event_rp0*)meta_evt->data;
  133. gap->connection_params.conn_interval = event->Conn_Interval;
  134. gap->connection_params.slave_latency = event->Conn_Latency;
  135. gap->connection_params.supervisor_timeout = event->Supervision_Timeout;
  136. // Stop advertising as connection completed
  137. furi_timer_stop(gap->advertise_timer);
  138. // Update connection status and handle
  139. gap->state = GapStateConnected;
  140. gap->service.connection_handle = event->Connection_Handle;
  141. gap_verify_connection_parameters(gap);
  142. // Start pairing by sending security request
  143. aci_gap_slave_security_req(event->Connection_Handle);
  144. } break;
  145. default:
  146. break;
  147. }
  148. break;
  149. case HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE:
  150. blue_evt = (evt_blecore_aci*)event_pckt->data;
  151. switch(blue_evt->ecode) {
  152. aci_gap_pairing_complete_event_rp0* pairing_complete;
  153. case ACI_GAP_LIMITED_DISCOVERABLE_VSEVT_CODE:
  154. FURI_LOG_I(TAG, "Limited discoverable event");
  155. break;
  156. case ACI_GAP_PASS_KEY_REQ_VSEVT_CODE: {
  157. // Generate random PIN code
  158. uint32_t pin = rand() % 999999; //-V1064
  159. aci_gap_pass_key_resp(gap->service.connection_handle, pin);
  160. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock)) {
  161. FURI_LOG_I(TAG, "Pass key request event. Pin: ******");
  162. } else {
  163. FURI_LOG_I(TAG, "Pass key request event. Pin: %06ld", pin);
  164. }
  165. GapEvent event = {.type = GapEventTypePinCodeShow, .data.pin_code = pin};
  166. gap->on_event_cb(event, gap->context);
  167. } break;
  168. case ACI_ATT_EXCHANGE_MTU_RESP_VSEVT_CODE: {
  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 ACI_GAP_AUTHORIZATION_REQ_VSEVT_CODE:
  177. FURI_LOG_D(TAG, "Authorization request event");
  178. break;
  179. case ACI_GAP_SLAVE_SECURITY_INITIATED_VSEVT_CODE:
  180. FURI_LOG_D(TAG, "Slave security initiated");
  181. break;
  182. case ACI_GAP_BOND_LOST_VSEVT_CODE:
  183. FURI_LOG_D(TAG, "Bond lost event. Start rebonding");
  184. aci_gap_allow_rebond(gap->service.connection_handle);
  185. break;
  186. case ACI_GAP_ADDR_NOT_RESOLVED_VSEVT_CODE:
  187. FURI_LOG_D(TAG, "Address not resolved event");
  188. break;
  189. case ACI_GAP_KEYPRESS_NOTIFICATION_VSEVT_CODE:
  190. FURI_LOG_D(TAG, "Key press notification event");
  191. break;
  192. case ACI_GAP_NUMERIC_COMPARISON_VALUE_VSEVT_CODE: {
  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 ACI_GAP_PAIRING_COMPLETE_VSEVT_CODE:
  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 ACI_L2CAP_CONNECTION_UPDATE_RESP_VSEVT_CODE:
  216. FURI_LOG_D(TAG, "Procedure complete event");
  217. break;
  218. case ACI_L2CAP_CONNECTION_UPDATE_REQ_VSEVT_CODE: {
  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. CFG_IDENTITY_ADDRESS);
  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. CFG_IDENTITY_ADDRESS,
  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. }