ble_app.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #define TAG "Bt"
  8. #define BLE_APP_FLAG_HCI_EVENT (1UL << 0)
  9. #define BLE_APP_FLAG_KILL_THREAD (1UL << 1)
  10. #define BLE_APP_FLAG_ALL (BLE_APP_FLAG_HCI_EVENT | BLE_APP_FLAG_KILL_THREAD)
  11. PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t ble_app_cmd_buffer;
  12. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint32_t ble_app_nvm[BLE_NVM_SRAM_SIZE];
  13. _Static_assert(
  14. sizeof(SHCI_C2_Ble_Init_Cmd_Packet_t) == 49,
  15. "Ble stack config structure size mismatch");
  16. typedef struct {
  17. osMutexId_t hci_mtx;
  18. osSemaphoreId_t hci_sem;
  19. FuriThread* thread;
  20. osEventFlagsId_t event_flags;
  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 = osMutexNew(NULL);
  31. ble_app->hci_sem = osSemaphoreNew(1, 0, NULL);
  32. ble_app->event_flags = osEventFlagsNew(NULL);
  33. // HCI transport layer thread to handle user asynch events
  34. ble_app->thread = furi_thread_alloc();
  35. furi_thread_set_name(ble_app->thread, "BleHciDriver");
  36. furi_thread_set_stack_size(ble_app->thread, 1024);
  37. furi_thread_set_context(ble_app->thread, ble_app);
  38. furi_thread_set_callback(ble_app->thread, ble_app_hci_thread);
  39. furi_thread_start(ble_app->thread);
  40. // Initialize Ble Transport Layer
  41. HCI_TL_HciInitConf_t hci_tl_config = {
  42. .p_cmdbuffer = (uint8_t*)&ble_app_cmd_buffer,
  43. .StatusNotCallBack = ble_app_hci_status_not_handler,
  44. };
  45. hci_init(ble_app_hci_event_handler, (void*)&hci_tl_config);
  46. // Configure NVM store for pairing data
  47. SHCI_C2_CONFIG_Cmd_Param_t config_param = {
  48. .PayloadCmdSize = SHCI_C2_CONFIG_PAYLOAD_CMD_SIZE,
  49. .Config1 = SHCI_C2_CONFIG_CONFIG1_BIT0_BLE_NVM_DATA_TO_SRAM,
  50. .BleNvmRamAddress = (uint32_t)ble_app_nvm,
  51. .EvtMask1 = SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE,
  52. };
  53. status = SHCI_C2_Config(&config_param);
  54. if(status) {
  55. FURI_LOG_E(TAG, "Failed to configure 2nd core: %d", status);
  56. }
  57. // Start ble stack on 2nd core
  58. SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = {
  59. .Header = {{0, 0, 0}}, // Header unused
  60. .Param = {
  61. .pBleBufferAddress = 0, // pBleBufferAddress not used
  62. .BleBufferSize = 0, // BleBufferSize not used
  63. .NumAttrRecord = CFG_BLE_NUM_GATT_ATTRIBUTES,
  64. .NumAttrServ = CFG_BLE_NUM_GATT_SERVICES,
  65. .AttrValueArrSize = CFG_BLE_ATT_VALUE_ARRAY_SIZE,
  66. .NumOfLinks = CFG_BLE_NUM_LINK,
  67. .ExtendedPacketLengthEnable = CFG_BLE_DATA_LENGTH_EXTENSION,
  68. .PrWriteListSize = CFG_BLE_PREPARE_WRITE_LIST_SIZE,
  69. .MblockCount = CFG_BLE_MBLOCK_COUNT,
  70. .AttMtu = CFG_BLE_MAX_ATT_MTU,
  71. .SlaveSca = CFG_BLE_SLAVE_SCA,
  72. .MasterSca = CFG_BLE_MASTER_SCA,
  73. .LsSource = CFG_BLE_LSE_SOURCE,
  74. .MaxConnEventLength = CFG_BLE_MAX_CONN_EVENT_LENGTH,
  75. .HsStartupTime = CFG_BLE_HSE_STARTUP_TIME,
  76. .ViterbiEnable = CFG_BLE_VITERBI_MODE,
  77. .Options = CFG_BLE_OPTIONS,
  78. .HwVersion = 0,
  79. .max_coc_initiator_nbr = 32,
  80. .min_tx_power = 0,
  81. .max_tx_power = 0,
  82. .rx_model_config = 1,
  83. }};
  84. status = SHCI_C2_BLE_Init(&ble_init_cmd_packet);
  85. if(status) {
  86. FURI_LOG_E(TAG, "Failed to start ble stack: %d", status);
  87. }
  88. return status == SHCI_Success;
  89. }
  90. void ble_app_get_key_storage_buff(uint8_t** addr, uint16_t* size) {
  91. *addr = (uint8_t*)ble_app_nvm;
  92. *size = sizeof(ble_app_nvm);
  93. }
  94. void ble_app_thread_stop() {
  95. if(ble_app) {
  96. osEventFlagsSet(ble_app->event_flags, BLE_APP_FLAG_KILL_THREAD);
  97. furi_thread_join(ble_app->thread);
  98. furi_thread_free(ble_app->thread);
  99. // Wait to make sure that EventFlags delivers pending events before memory free
  100. osDelay(50);
  101. // Free resources
  102. osMutexDelete(ble_app->hci_mtx);
  103. osSemaphoreDelete(ble_app->hci_sem);
  104. osEventFlagsDelete(ble_app->event_flags);
  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. uint32_t flags = 0;
  112. while(1) {
  113. flags = osEventFlagsWait(
  114. ble_app->event_flags, BLE_APP_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  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. if(ble_app) {
  127. osEventFlagsSet(ble_app->event_flags, BLE_APP_FLAG_HCI_EVENT);
  128. }
  129. }
  130. void hci_cmd_resp_release(uint32_t flag) {
  131. if(ble_app) {
  132. osSemaphoreRelease(ble_app->hci_sem);
  133. }
  134. }
  135. void hci_cmd_resp_wait(uint32_t timeout) {
  136. if(ble_app) {
  137. osSemaphoreAcquire(ble_app->hci_sem, osWaitForever);
  138. }
  139. }
  140. static void ble_app_hci_event_handler(void* pPayload) {
  141. SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  142. tHCI_UserEvtRxParam* pParam = (tHCI_UserEvtRxParam*)pPayload;
  143. if(ble_app) {
  144. svctl_return_status = SVCCTL_UserEvtRx((void*)&(pParam->pckt->evtserial));
  145. if(svctl_return_status != SVCCTL_UserEvtFlowDisable) {
  146. pParam->status = HCI_TL_UserEventFlow_Enable;
  147. } else {
  148. pParam->status = HCI_TL_UserEventFlow_Disable;
  149. }
  150. }
  151. }
  152. static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status) {
  153. if(status == HCI_TL_CmdBusy) {
  154. osMutexAcquire(ble_app->hci_mtx, osWaitForever);
  155. } else if(status == HCI_TL_CmdAvailable) {
  156. osMutexRelease(ble_app->hci_mtx);
  157. }
  158. }
  159. void SVCCTL_ResumeUserEventFlow(void) {
  160. hci_resume_flow();
  161. }