ble_app.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "ble_app.h"
  2. #include "hci_tl.h"
  3. #include "ble.h"
  4. #include "shci.h"
  5. #include "cmsis_os.h"
  6. #include "gap.h"
  7. #include <furi-hal.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. typedef struct {
  15. osMutexId_t hci_mtx;
  16. osSemaphoreId_t hci_sem;
  17. FuriThread* thread;
  18. osEventFlagsId_t event_flags;
  19. } BleApp;
  20. static BleApp* ble_app = NULL;
  21. static int32_t ble_app_hci_thread(void* context);
  22. static void ble_app_hci_event_handler(void* pPayload);
  23. static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status);
  24. bool ble_app_init() {
  25. SHCI_CmdStatus_t status;
  26. ble_app = furi_alloc(sizeof(BleApp));
  27. // Allocate semafore and mutex for ble command buffer access
  28. ble_app->hci_mtx = osMutexNew(NULL);
  29. ble_app->hci_sem = osSemaphoreNew(1, 0, NULL);
  30. ble_app->event_flags = osEventFlagsNew(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, "BleHciWorker");
  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. 0, // pBleBufferAddress not used
  60. 0, // BleBufferSize not used
  61. CFG_BLE_NUM_GATT_ATTRIBUTES,
  62. CFG_BLE_NUM_GATT_SERVICES,
  63. CFG_BLE_ATT_VALUE_ARRAY_SIZE,
  64. CFG_BLE_NUM_LINK,
  65. CFG_BLE_DATA_LENGTH_EXTENSION,
  66. CFG_BLE_PREPARE_WRITE_LIST_SIZE,
  67. CFG_BLE_MBLOCK_COUNT,
  68. CFG_BLE_MAX_ATT_MTU,
  69. CFG_BLE_SLAVE_SCA,
  70. CFG_BLE_MASTER_SCA,
  71. CFG_BLE_LSE_SOURCE,
  72. CFG_BLE_MAX_CONN_EVENT_LENGTH,
  73. CFG_BLE_HSE_STARTUP_TIME,
  74. CFG_BLE_VITERBI_MODE,
  75. CFG_BLE_LL_ONLY,
  76. 0,
  77. }
  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. osEventFlagsSet(ble_app->event_flags, BLE_APP_FLAG_KILL_THREAD);
  92. furi_thread_join(ble_app->thread);
  93. furi_thread_free(ble_app->thread);
  94. // Wait to make sure that EventFlags delivers pending events before memory free
  95. osDelay(50);
  96. // Free resources
  97. osMutexDelete(ble_app->hci_mtx);
  98. osSemaphoreDelete(ble_app->hci_sem);
  99. osEventFlagsDelete(ble_app->event_flags);
  100. free(ble_app);
  101. ble_app = NULL;
  102. memset(&ble_app_cmd_buffer, 0, sizeof(ble_app_cmd_buffer));
  103. }
  104. }
  105. static int32_t ble_app_hci_thread(void *arg) {
  106. uint32_t flags = 0;
  107. while(1) {
  108. flags = osEventFlagsWait(ble_app->event_flags, BLE_APP_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  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. if(ble_app) {
  121. osEventFlagsSet(ble_app->event_flags, BLE_APP_FLAG_HCI_EVENT);
  122. }
  123. }
  124. void hci_cmd_resp_release(uint32_t flag) {
  125. if(ble_app) {
  126. osSemaphoreRelease(ble_app->hci_sem);
  127. }
  128. }
  129. void hci_cmd_resp_wait(uint32_t timeout) {
  130. if(ble_app) {
  131. osSemaphoreAcquire(ble_app->hci_sem, osWaitForever);
  132. }
  133. }
  134. static void ble_app_hci_event_handler( void * pPayload ) {
  135. SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  136. tHCI_UserEvtRxParam *pParam = (tHCI_UserEvtRxParam *)pPayload;
  137. if(ble_app) {
  138. svctl_return_status = SVCCTL_UserEvtRx((void *)&(pParam->pckt->evtserial));
  139. if (svctl_return_status != SVCCTL_UserEvtFlowDisable) {
  140. pParam->status = HCI_TL_UserEventFlow_Enable;
  141. } else {
  142. pParam->status = HCI_TL_UserEventFlow_Disable;
  143. }
  144. }
  145. }
  146. static void ble_app_hci_status_not_handler( HCI_TL_CmdStatus_t status ) {
  147. if(status == HCI_TL_CmdBusy) {
  148. osMutexAcquire(ble_app->hci_mtx, osWaitForever );
  149. } else if(status == HCI_TL_CmdAvailable) {
  150. osMutexRelease(ble_app->hci_mtx);
  151. }
  152. }
  153. void SVCCTL_ResumeUserEventFlow( void ) {
  154. hci_resume_flow();
  155. }