ble_app.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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) == 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_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. }};
  79. status = SHCI_C2_BLE_Init(&ble_init_cmd_packet);
  80. if(status) {
  81. FURI_LOG_E(TAG, "Failed to start ble stack: %d", status);
  82. }
  83. return status == SHCI_Success;
  84. }
  85. void ble_app_get_key_storage_buff(uint8_t** addr, uint16_t* size) {
  86. *addr = (uint8_t*)ble_app_nvm;
  87. *size = sizeof(ble_app_nvm);
  88. }
  89. void ble_app_thread_stop() {
  90. if(ble_app) {
  91. FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
  92. furi_assert(thread_id);
  93. furi_thread_flags_set(thread_id, BLE_APP_FLAG_KILL_THREAD);
  94. furi_thread_join(ble_app->thread);
  95. furi_thread_free(ble_app->thread);
  96. // Free resources
  97. furi_mutex_free(ble_app->hci_mtx);
  98. furi_semaphore_free(ble_app->hci_sem);
  99. free(ble_app);
  100. ble_app = NULL;
  101. memset(&ble_app_cmd_buffer, 0, sizeof(ble_app_cmd_buffer));
  102. }
  103. }
  104. static int32_t ble_app_hci_thread(void* arg) {
  105. UNUSED(arg);
  106. uint32_t flags = 0;
  107. while(1) {
  108. flags = furi_thread_flags_wait(BLE_APP_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
  109. if(flags & BLE_APP_FLAG_KILL_THREAD) {
  110. break;
  111. }
  112. if(flags & BLE_APP_FLAG_HCI_EVENT) {
  113. hci_user_evt_proc();
  114. }
  115. }
  116. return 0;
  117. }
  118. // Called by WPAN lib
  119. void hci_notify_asynch_evt(void* pdata) {
  120. UNUSED(pdata);
  121. if(ble_app) {
  122. FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
  123. furi_assert(thread_id);
  124. furi_thread_flags_set(thread_id, BLE_APP_FLAG_HCI_EVENT);
  125. }
  126. }
  127. void hci_cmd_resp_release(uint32_t flag) {
  128. UNUSED(flag);
  129. if(ble_app) {
  130. furi_semaphore_release(ble_app->hci_sem);
  131. }
  132. }
  133. void hci_cmd_resp_wait(uint32_t timeout) {
  134. UNUSED(timeout);
  135. if(ble_app) {
  136. furi_semaphore_acquire(ble_app->hci_sem, FuriWaitForever);
  137. }
  138. }
  139. static void ble_app_hci_event_handler(void* pPayload) {
  140. SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  141. tHCI_UserEvtRxParam* pParam = (tHCI_UserEvtRxParam*)pPayload;
  142. if(ble_app) {
  143. svctl_return_status = SVCCTL_UserEvtRx((void*)&(pParam->pckt->evtserial));
  144. if(svctl_return_status != SVCCTL_UserEvtFlowDisable) {
  145. pParam->status = HCI_TL_UserEventFlow_Enable;
  146. } else {
  147. pParam->status = HCI_TL_UserEventFlow_Disable;
  148. }
  149. }
  150. }
  151. static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status) {
  152. if(status == HCI_TL_CmdBusy) {
  153. furi_mutex_acquire(ble_app->hci_mtx, FuriWaitForever);
  154. } else if(status == HCI_TL_CmdAvailable) {
  155. furi_mutex_release(ble_app->hci_mtx);
  156. }
  157. }
  158. void SVCCTL_ResumeUserEventFlow(void) {
  159. hci_resume_flow();
  160. }