ble_app.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. 0, // pBleBufferAddress not used
  59. 0, // BleBufferSize not used
  60. CFG_BLE_NUM_GATT_ATTRIBUTES,
  61. CFG_BLE_NUM_GATT_SERVICES,
  62. CFG_BLE_ATT_VALUE_ARRAY_SIZE,
  63. CFG_BLE_NUM_LINK,
  64. CFG_BLE_DATA_LENGTH_EXTENSION,
  65. CFG_BLE_PREPARE_WRITE_LIST_SIZE,
  66. CFG_BLE_MBLOCK_COUNT,
  67. CFG_BLE_MAX_ATT_MTU,
  68. CFG_BLE_SLAVE_SCA,
  69. CFG_BLE_MASTER_SCA,
  70. CFG_BLE_LSE_SOURCE,
  71. CFG_BLE_MAX_CONN_EVENT_LENGTH,
  72. CFG_BLE_HSE_STARTUP_TIME,
  73. CFG_BLE_VITERBI_MODE,
  74. CFG_BLE_LL_ONLY,
  75. 0,
  76. }
  77. };
  78. status = SHCI_C2_BLE_Init(&ble_init_cmd_packet);
  79. if(status) {
  80. FURI_LOG_E(TAG, "Failed to start ble stack: %d", status);
  81. }
  82. return status == SHCI_Success;
  83. }
  84. void ble_app_get_key_storage_buff(uint8_t** addr, uint16_t* size) {
  85. *addr = (uint8_t*)ble_app_nvm;
  86. *size = sizeof(ble_app_nvm);
  87. }
  88. void ble_app_thread_stop() {
  89. if(ble_app) {
  90. osEventFlagsSet(ble_app->event_flags, BLE_APP_FLAG_KILL_THREAD);
  91. furi_thread_join(ble_app->thread);
  92. furi_thread_free(ble_app->thread);
  93. // Wait to make sure that EventFlags delivers pending events before memory free
  94. osDelay(50);
  95. // Free resources
  96. osMutexDelete(ble_app->hci_mtx);
  97. osSemaphoreDelete(ble_app->hci_sem);
  98. osEventFlagsDelete(ble_app->event_flags);
  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. uint32_t flags = 0;
  106. while(1) {
  107. flags = osEventFlagsWait(ble_app->event_flags, BLE_APP_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  108. if(flags & BLE_APP_FLAG_KILL_THREAD) {
  109. break;
  110. }
  111. if(flags & BLE_APP_FLAG_HCI_EVENT) {
  112. hci_user_evt_proc();
  113. }
  114. }
  115. return 0;
  116. }
  117. // Called by WPAN lib
  118. void hci_notify_asynch_evt(void* pdata) {
  119. if(ble_app) {
  120. osEventFlagsSet(ble_app->event_flags, BLE_APP_FLAG_HCI_EVENT);
  121. }
  122. }
  123. void hci_cmd_resp_release(uint32_t flag) {
  124. if(ble_app) {
  125. osSemaphoreRelease(ble_app->hci_sem);
  126. }
  127. }
  128. void hci_cmd_resp_wait(uint32_t timeout) {
  129. if(ble_app) {
  130. osSemaphoreAcquire(ble_app->hci_sem, osWaitForever);
  131. }
  132. }
  133. static void ble_app_hci_event_handler( void * pPayload ) {
  134. SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  135. tHCI_UserEvtRxParam *pParam = (tHCI_UserEvtRxParam *)pPayload;
  136. if(ble_app) {
  137. svctl_return_status = SVCCTL_UserEvtRx((void *)&(pParam->pckt->evtserial));
  138. if (svctl_return_status != SVCCTL_UserEvtFlowDisable) {
  139. pParam->status = HCI_TL_UserEventFlow_Enable;
  140. } else {
  141. pParam->status = HCI_TL_UserEventFlow_Disable;
  142. }
  143. }
  144. }
  145. static void ble_app_hci_status_not_handler( HCI_TL_CmdStatus_t status ) {
  146. if(status == HCI_TL_CmdBusy) {
  147. osMutexAcquire(ble_app->hci_mtx, osWaitForever );
  148. } else if(status == HCI_TL_CmdAvailable) {
  149. osMutexRelease(ble_app->hci_mtx);
  150. }
  151. }
  152. void SVCCTL_ResumeUserEventFlow( void ) {
  153. hci_resume_flow();
  154. }