ble_app.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. typedef struct {
  14. osMutexId_t hci_mtx;
  15. osSemaphoreId_t hci_sem;
  16. FuriThread* thread;
  17. osEventFlagsId_t event_flags;
  18. } BleApp;
  19. static BleApp* ble_app = NULL;
  20. static int32_t ble_app_hci_thread(void* context);
  21. static void ble_app_hci_event_handler(void* pPayload);
  22. static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status);
  23. bool ble_app_init() {
  24. SHCI_CmdStatus_t status;
  25. ble_app = furi_alloc(sizeof(BleApp));
  26. // Allocate semafore and mutex for ble command buffer access
  27. ble_app->hci_mtx = osMutexNew(NULL);
  28. ble_app->hci_sem = osSemaphoreNew(1, 0, NULL);
  29. ble_app->event_flags = osEventFlagsNew(NULL);
  30. // HCI transport layer thread to handle user asynch events
  31. ble_app->thread = furi_thread_alloc();
  32. furi_thread_set_name(ble_app->thread, "BleHciWorker");
  33. furi_thread_set_stack_size(ble_app->thread, 1024);
  34. furi_thread_set_context(ble_app->thread, ble_app);
  35. furi_thread_set_callback(ble_app->thread, ble_app_hci_thread);
  36. furi_thread_start(ble_app->thread);
  37. // Initialize Ble Transport Layer
  38. HCI_TL_HciInitConf_t hci_tl_config = {
  39. .p_cmdbuffer = (uint8_t*)&ble_app_cmd_buffer,
  40. .StatusNotCallBack = ble_app_hci_status_not_handler,
  41. };
  42. hci_init(ble_app_hci_event_handler, (void*)&hci_tl_config);
  43. // Configure NVM store for pairing data
  44. SHCI_C2_CONFIG_Cmd_Param_t config_param = {
  45. .PayloadCmdSize = SHCI_C2_CONFIG_PAYLOAD_CMD_SIZE,
  46. .Config1 =SHCI_C2_CONFIG_CONFIG1_BIT0_BLE_NVM_DATA_TO_SRAM,
  47. .BleNvmRamAddress = (uint32_t)ble_app_nvm,
  48. .EvtMask1 = SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE,
  49. };
  50. status = SHCI_C2_Config(&config_param);
  51. if(status) {
  52. FURI_LOG_E(TAG, "Failed to configure 2nd core: %d", status);
  53. }
  54. // Start ble stack on 2nd core
  55. SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = {
  56. .Header = {{0,0,0}}, // Header unused
  57. .Param = {
  58. .pBleBufferAddress = 0, // pBleBufferAddress not used
  59. .BleBufferSize = 0, // BleBufferSize not used
  60. .NumAttrRecord = CFG_BLE_NUM_GATT_ATTRIBUTES,
  61. .NumAttrServ = CFG_BLE_NUM_GATT_SERVICES,
  62. .AttrValueArrSize = CFG_BLE_ATT_VALUE_ARRAY_SIZE,
  63. .NumOfLinks = CFG_BLE_NUM_LINK,
  64. .ExtendedPacketLengthEnable = CFG_BLE_DATA_LENGTH_EXTENSION,
  65. .PrWriteListSize = CFG_BLE_PREPARE_WRITE_LIST_SIZE,
  66. .MblockCount = CFG_BLE_MBLOCK_COUNT,
  67. .AttMtu = CFG_BLE_MAX_ATT_MTU,
  68. .SlaveSca = CFG_BLE_SLAVE_SCA,
  69. .MasterSca = CFG_BLE_MASTER_SCA,
  70. .LsSource = CFG_BLE_LSE_SOURCE,
  71. .MaxConnEventLength = CFG_BLE_MAX_CONN_EVENT_LENGTH,
  72. .HsStartupTime = CFG_BLE_HSE_STARTUP_TIME,
  73. .ViterbiEnable = CFG_BLE_VITERBI_MODE,
  74. .Options = CFG_BLE_OPTIONS,
  75. .HwVersion = 0,
  76. .max_coc_initiator_nbr = 32,
  77. .min_tx_power = 0,
  78. .max_tx_power = 0,
  79. .rx_model_config = 1,
  80. }
  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. osEventFlagsSet(ble_app->event_flags, BLE_APP_FLAG_KILL_THREAD);
  95. furi_thread_join(ble_app->thread);
  96. furi_thread_free(ble_app->thread);
  97. // Wait to make sure that EventFlags delivers pending events before memory free
  98. osDelay(50);
  99. // Free resources
  100. osMutexDelete(ble_app->hci_mtx);
  101. osSemaphoreDelete(ble_app->hci_sem);
  102. osEventFlagsDelete(ble_app->event_flags);
  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. uint32_t flags = 0;
  110. while(1) {
  111. flags = osEventFlagsWait(ble_app->event_flags, 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. if(ble_app) {
  124. osEventFlagsSet(ble_app->event_flags, BLE_APP_FLAG_HCI_EVENT);
  125. }
  126. }
  127. void hci_cmd_resp_release(uint32_t flag) {
  128. if(ble_app) {
  129. osSemaphoreRelease(ble_app->hci_sem);
  130. }
  131. }
  132. void hci_cmd_resp_wait(uint32_t timeout) {
  133. if(ble_app) {
  134. osSemaphoreAcquire(ble_app->hci_sem, osWaitForever);
  135. }
  136. }
  137. static void ble_app_hci_event_handler( void * pPayload ) {
  138. SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  139. tHCI_UserEvtRxParam *pParam = (tHCI_UserEvtRxParam *)pPayload;
  140. if(ble_app) {
  141. svctl_return_status = SVCCTL_UserEvtRx((void *)&(pParam->pckt->evtserial));
  142. if (svctl_return_status != SVCCTL_UserEvtFlowDisable) {
  143. pParam->status = HCI_TL_UserEventFlow_Enable;
  144. } else {
  145. pParam->status = HCI_TL_UserEventFlow_Disable;
  146. }
  147. }
  148. }
  149. static void ble_app_hci_status_not_handler( HCI_TL_CmdStatus_t status ) {
  150. if(status == HCI_TL_CmdBusy) {
  151. osMutexAcquire(ble_app->hci_mtx, osWaitForever );
  152. } else if(status == HCI_TL_CmdAvailable) {
  153. osMutexRelease(ble_app->hci_mtx);
  154. }
  155. }
  156. void SVCCTL_ResumeUserEventFlow( void ) {
  157. hci_resume_flow();
  158. }