ble_app.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "ble_app.h"
  2. #include "hci_tl.h"
  3. #include "ble.h"
  4. #include "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) == 49,
  16. "Ble stack config structure size mismatch");
  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();
  34. furi_thread_set_name(ble_app->thread, "BleHciDriver");
  35. furi_thread_set_stack_size(ble_app->thread, 1024);
  36. furi_thread_set_context(ble_app->thread, ble_app);
  37. furi_thread_set_callback(ble_app->thread, ble_app_hci_thread);
  38. furi_thread_start(ble_app->thread);
  39. // Initialize Ble Transport Layer
  40. HCI_TL_HciInitConf_t hci_tl_config = {
  41. .p_cmdbuffer = (uint8_t*)&ble_app_cmd_buffer,
  42. .StatusNotCallBack = ble_app_hci_status_not_handler,
  43. };
  44. hci_init(ble_app_hci_event_handler, (void*)&hci_tl_config);
  45. // Configure NVM store for pairing data
  46. SHCI_C2_CONFIG_Cmd_Param_t config_param = {
  47. .PayloadCmdSize = SHCI_C2_CONFIG_PAYLOAD_CMD_SIZE,
  48. .Config1 = SHCI_C2_CONFIG_CONFIG1_BIT0_BLE_NVM_DATA_TO_SRAM,
  49. .BleNvmRamAddress = (uint32_t)ble_app_nvm,
  50. .EvtMask1 = SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE,
  51. };
  52. status = SHCI_C2_Config(&config_param);
  53. if(status) {
  54. FURI_LOG_E(TAG, "Failed to configure 2nd core: %d", status);
  55. }
  56. // Start ble stack on 2nd core
  57. SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = {
  58. .Header = {{0, 0, 0}}, // Header unused
  59. .Param = {
  60. .pBleBufferAddress = 0, // pBleBufferAddress not used
  61. .BleBufferSize = 0, // BleBufferSize not used
  62. .NumAttrRecord = CFG_BLE_NUM_GATT_ATTRIBUTES,
  63. .NumAttrServ = CFG_BLE_NUM_GATT_SERVICES,
  64. .AttrValueArrSize = CFG_BLE_ATT_VALUE_ARRAY_SIZE,
  65. .NumOfLinks = CFG_BLE_NUM_LINK,
  66. .ExtendedPacketLengthEnable = CFG_BLE_DATA_LENGTH_EXTENSION,
  67. .PrWriteListSize = CFG_BLE_PREPARE_WRITE_LIST_SIZE,
  68. .MblockCount = CFG_BLE_MBLOCK_COUNT,
  69. .AttMtu = CFG_BLE_MAX_ATT_MTU,
  70. .SlaveSca = CFG_BLE_SLAVE_SCA,
  71. .MasterSca = CFG_BLE_MASTER_SCA,
  72. .LsSource = CFG_BLE_LSE_SOURCE,
  73. .MaxConnEventLength = CFG_BLE_MAX_CONN_EVENT_LENGTH,
  74. .HsStartupTime = CFG_BLE_HSE_STARTUP_TIME,
  75. .ViterbiEnable = CFG_BLE_VITERBI_MODE,
  76. .Options = CFG_BLE_OPTIONS,
  77. .HwVersion = 0,
  78. .max_coc_initiator_nbr = 32,
  79. .min_tx_power = 0,
  80. .max_tx_power = 0,
  81. .rx_model_config = 1,
  82. }};
  83. status = SHCI_C2_BLE_Init(&ble_init_cmd_packet);
  84. if(status) {
  85. FURI_LOG_E(TAG, "Failed to start ble stack: %d", status);
  86. }
  87. return status == SHCI_Success;
  88. }
  89. void ble_app_get_key_storage_buff(uint8_t** addr, uint16_t* size) {
  90. *addr = (uint8_t*)ble_app_nvm;
  91. *size = sizeof(ble_app_nvm);
  92. }
  93. void ble_app_thread_stop() {
  94. if(ble_app) {
  95. FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
  96. furi_assert(thread_id);
  97. furi_thread_flags_set(thread_id, BLE_APP_FLAG_KILL_THREAD);
  98. furi_thread_join(ble_app->thread);
  99. furi_thread_free(ble_app->thread);
  100. // Free resources
  101. furi_mutex_free(ble_app->hci_mtx);
  102. furi_semaphore_free(ble_app->hci_sem);
  103. free(ble_app);
  104. ble_app = NULL;
  105. memset(&ble_app_cmd_buffer, 0, sizeof(ble_app_cmd_buffer));
  106. }
  107. }
  108. static int32_t ble_app_hci_thread(void* arg) {
  109. UNUSED(arg);
  110. uint32_t flags = 0;
  111. while(1) {
  112. flags = furi_thread_flags_wait(BLE_APP_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
  113. if(flags & BLE_APP_FLAG_KILL_THREAD) {
  114. break;
  115. }
  116. if(flags & BLE_APP_FLAG_HCI_EVENT) {
  117. hci_user_evt_proc();
  118. }
  119. }
  120. return 0;
  121. }
  122. // Called by WPAN lib
  123. void hci_notify_asynch_evt(void* pdata) {
  124. UNUSED(pdata);
  125. if(ble_app) {
  126. FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
  127. furi_assert(thread_id);
  128. furi_thread_flags_set(thread_id, BLE_APP_FLAG_HCI_EVENT);
  129. }
  130. }
  131. void hci_cmd_resp_release(uint32_t flag) {
  132. UNUSED(flag);
  133. if(ble_app) {
  134. furi_semaphore_release(ble_app->hci_sem);
  135. }
  136. }
  137. void hci_cmd_resp_wait(uint32_t timeout) {
  138. UNUSED(timeout);
  139. if(ble_app) {
  140. furi_semaphore_acquire(ble_app->hci_sem, FuriWaitForever);
  141. }
  142. }
  143. static void ble_app_hci_event_handler(void* pPayload) {
  144. SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  145. tHCI_UserEvtRxParam* pParam = (tHCI_UserEvtRxParam*)pPayload;
  146. if(ble_app) {
  147. svctl_return_status = SVCCTL_UserEvtRx((void*)&(pParam->pckt->evtserial));
  148. if(svctl_return_status != SVCCTL_UserEvtFlowDisable) {
  149. pParam->status = HCI_TL_UserEventFlow_Enable;
  150. } else {
  151. pParam->status = HCI_TL_UserEventFlow_Disable;
  152. }
  153. }
  154. }
  155. static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status) {
  156. if(status == HCI_TL_CmdBusy) {
  157. furi_mutex_acquire(ble_app->hci_mtx, FuriWaitForever);
  158. } else if(status == HCI_TL_CmdAvailable) {
  159. furi_mutex_release(ble_app->hci_mtx);
  160. }
  161. }
  162. void SVCCTL_ResumeUserEventFlow(void) {
  163. hci_resume_flow();
  164. }