ble_app.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "ble_app.h"
  2. #include <ble/ble.h>
  3. #include <interface/patterns/ble_thread/tl/hci_tl.h>
  4. #include <interface/patterns/ble_thread/shci/shci.h>
  5. #include "gap.h"
  6. #include <furi_hal.h>
  7. #include <furi.h>
  8. #define TAG "Bt"
  9. #define BLE_APP_FLAG_HCI_EVENT (1UL << 0)
  10. #define BLE_APP_FLAG_KILL_THREAD (1UL << 1)
  11. #define BLE_APP_FLAG_ALL (BLE_APP_FLAG_HCI_EVENT | BLE_APP_FLAG_KILL_THREAD)
  12. PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t ble_app_cmd_buffer;
  13. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint32_t ble_app_nvm[BLE_NVM_SRAM_SIZE];
  14. _Static_assert(
  15. sizeof(SHCI_C2_Ble_Init_Cmd_Packet_t) == 57,
  16. "Ble stack config structure size mismatch (check new config options - last updated for v.1.15.0)");
  17. typedef struct {
  18. FuriMutex* hci_mtx;
  19. FuriSemaphore* hci_sem;
  20. FuriThread* thread;
  21. } BleApp;
  22. static BleApp* ble_app = NULL;
  23. static int32_t ble_app_hci_thread(void* context);
  24. static void ble_app_hci_event_handler(void* pPayload);
  25. static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status);
  26. bool ble_app_init() {
  27. SHCI_CmdStatus_t status;
  28. ble_app = malloc(sizeof(BleApp));
  29. // Allocate semafore and mutex for ble command buffer access
  30. ble_app->hci_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
  31. ble_app->hci_sem = furi_semaphore_alloc(1, 0);
  32. // HCI transport layer thread to handle user asynch events
  33. ble_app->thread = furi_thread_alloc_ex("BleHciDriver", 1024, ble_app_hci_thread, ble_app);
  34. furi_thread_start(ble_app->thread);
  35. // Initialize Ble Transport Layer
  36. HCI_TL_HciInitConf_t hci_tl_config = {
  37. .p_cmdbuffer = (uint8_t*)&ble_app_cmd_buffer,
  38. .StatusNotCallBack = ble_app_hci_status_not_handler,
  39. };
  40. hci_init(ble_app_hci_event_handler, (void*)&hci_tl_config);
  41. // Configure NVM store for pairing data
  42. SHCI_C2_CONFIG_Cmd_Param_t config_param = {
  43. .PayloadCmdSize = SHCI_C2_CONFIG_PAYLOAD_CMD_SIZE,
  44. .Config1 = SHCI_C2_CONFIG_CONFIG1_BIT0_BLE_NVM_DATA_TO_SRAM,
  45. .BleNvmRamAddress = (uint32_t)ble_app_nvm,
  46. .EvtMask1 = SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE,
  47. };
  48. status = SHCI_C2_Config(&config_param);
  49. if(status) {
  50. FURI_LOG_E(TAG, "Failed to configure 2nd core: %d", status);
  51. }
  52. // Start ble stack on 2nd core
  53. SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = {
  54. .Header = {{0, 0, 0}}, // Header unused
  55. .Param = {
  56. .pBleBufferAddress = 0, // pBleBufferAddress not used
  57. .BleBufferSize = 0, // BleBufferSize not used
  58. .NumAttrRecord = CFG_BLE_NUM_GATT_ATTRIBUTES,
  59. .NumAttrServ = CFG_BLE_NUM_GATT_SERVICES,
  60. .AttrValueArrSize = CFG_BLE_ATT_VALUE_ARRAY_SIZE,
  61. .NumOfLinks = CFG_BLE_NUM_LINK,
  62. .ExtendedPacketLengthEnable = CFG_BLE_DATA_LENGTH_EXTENSION,
  63. .PrWriteListSize = CFG_BLE_PREPARE_WRITE_LIST_SIZE,
  64. .MblockCount = CFG_BLE_MBLOCK_COUNT,
  65. .AttMtu = CFG_BLE_MAX_ATT_MTU,
  66. .SlaveSca = CFG_BLE_SLAVE_SCA,
  67. .MasterSca = CFG_BLE_MASTER_SCA,
  68. .LsSource = CFG_BLE_LSE_SOURCE,
  69. .MaxConnEventLength = CFG_BLE_MAX_CONN_EVENT_LENGTH,
  70. .HsStartupTime = CFG_BLE_HSE_STARTUP_TIME,
  71. .ViterbiEnable = CFG_BLE_VITERBI_MODE,
  72. .Options = CFG_BLE_OPTIONS,
  73. .HwVersion = 0,
  74. .max_coc_initiator_nbr = 32,
  75. .min_tx_power = 0,
  76. .max_tx_power = 0,
  77. .rx_model_config = 1,
  78. /* New stack (13.3->15.0) */
  79. .max_adv_set_nbr = 1, // Only used if SHCI_C2_BLE_INIT_OPTIONS_EXT_ADV is set
  80. .max_adv_data_len = 31, // Only used if SHCI_C2_BLE_INIT_OPTIONS_EXT_ADV is set
  81. .tx_path_compens = 0, // RF TX Path Compensation, * 0.1 dB
  82. .rx_path_compens = 0, // RF RX Path Compensation, * 0.1 dB
  83. .ble_core_version = 11, // BLE Core Version: 11(5.2), 12(5.3)
  84. }};
  85. status = SHCI_C2_BLE_Init(&ble_init_cmd_packet);
  86. if(status) {
  87. FURI_LOG_E(TAG, "Failed to start ble stack: %d", status);
  88. }
  89. return status == SHCI_Success;
  90. }
  91. void ble_app_get_key_storage_buff(uint8_t** addr, uint16_t* size) {
  92. *addr = (uint8_t*)ble_app_nvm;
  93. *size = sizeof(ble_app_nvm);
  94. }
  95. void ble_app_thread_stop() {
  96. if(ble_app) {
  97. FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
  98. furi_assert(thread_id);
  99. furi_thread_flags_set(thread_id, BLE_APP_FLAG_KILL_THREAD);
  100. furi_thread_join(ble_app->thread);
  101. furi_thread_free(ble_app->thread);
  102. // Free resources
  103. furi_mutex_free(ble_app->hci_mtx);
  104. furi_semaphore_free(ble_app->hci_sem);
  105. free(ble_app);
  106. ble_app = NULL;
  107. memset(&ble_app_cmd_buffer, 0, sizeof(ble_app_cmd_buffer));
  108. }
  109. }
  110. static int32_t ble_app_hci_thread(void* arg) {
  111. UNUSED(arg);
  112. uint32_t flags = 0;
  113. while(1) {
  114. flags = furi_thread_flags_wait(BLE_APP_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
  115. if(flags & BLE_APP_FLAG_KILL_THREAD) {
  116. break;
  117. }
  118. if(flags & BLE_APP_FLAG_HCI_EVENT) {
  119. hci_user_evt_proc();
  120. }
  121. }
  122. return 0;
  123. }
  124. // Called by WPAN lib
  125. void hci_notify_asynch_evt(void* pdata) {
  126. UNUSED(pdata);
  127. furi_check(ble_app);
  128. FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
  129. furi_assert(thread_id);
  130. furi_thread_flags_set(thread_id, BLE_APP_FLAG_HCI_EVENT);
  131. }
  132. void hci_cmd_resp_release(uint32_t flag) {
  133. UNUSED(flag);
  134. furi_check(ble_app);
  135. furi_check(furi_semaphore_release(ble_app->hci_sem) == FuriStatusOk);
  136. }
  137. void hci_cmd_resp_wait(uint32_t timeout) {
  138. furi_check(ble_app);
  139. furi_check(furi_semaphore_acquire(ble_app->hci_sem, timeout) == FuriStatusOk);
  140. }
  141. static void ble_app_hci_event_handler(void* pPayload) {
  142. SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  143. tHCI_UserEvtRxParam* pParam = (tHCI_UserEvtRxParam*)pPayload;
  144. furi_check(ble_app);
  145. svctl_return_status = SVCCTL_UserEvtRx((void*)&(pParam->pckt->evtserial));
  146. if(svctl_return_status != SVCCTL_UserEvtFlowDisable) {
  147. pParam->status = HCI_TL_UserEventFlow_Enable;
  148. } else {
  149. pParam->status = HCI_TL_UserEventFlow_Disable;
  150. }
  151. }
  152. static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status) {
  153. if(status == HCI_TL_CmdBusy) {
  154. furi_mutex_acquire(ble_app->hci_mtx, FuriWaitForever);
  155. } else if(status == HCI_TL_CmdAvailable) {
  156. furi_mutex_release(ble_app->hci_mtx);
  157. }
  158. }
  159. void SVCCTL_ResumeUserEventFlow(void) {
  160. hci_resume_flow();
  161. }