ble_app.c 6.0 KB

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