ble_app.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t ble_app_cmd_buffer;
  10. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint32_t ble_app_nvm[BLE_NVM_SRAM_SIZE];
  11. typedef struct {
  12. osMutexId_t hci_mtx;
  13. osSemaphoreId_t hci_sem;
  14. osThreadId_t hci_thread_id;
  15. osThreadAttr_t hci_thread_attr;
  16. } BleApp;
  17. static BleApp* ble_app;
  18. static void ble_app_hci_thread(void *arg);
  19. static void ble_app_hci_event_handler(void * pPayload);
  20. static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status);
  21. bool ble_app_init() {
  22. SHCI_CmdStatus_t status;
  23. ble_app = furi_alloc(sizeof(BleApp));
  24. // Allocate semafore and mutex for ble command buffer access
  25. ble_app->hci_mtx = osMutexNew(NULL);
  26. ble_app->hci_sem = osSemaphoreNew(1, 0, NULL);
  27. // HCI transport layer thread to handle user asynch events
  28. ble_app->hci_thread_attr.name = "BleHciWorker";
  29. ble_app->hci_thread_attr.stack_size = 1024;
  30. ble_app->hci_thread_id = osThreadNew(ble_app_hci_thread, NULL, &ble_app->hci_thread_attr);
  31. // Initialize Ble Transport Layer
  32. HCI_TL_HciInitConf_t hci_tl_config = {
  33. .p_cmdbuffer = (uint8_t*)&ble_app_cmd_buffer,
  34. .StatusNotCallBack = ble_app_hci_status_not_handler,
  35. };
  36. hci_init(ble_app_hci_event_handler, (void*)&hci_tl_config);
  37. // Configure NVM store for pairing data
  38. SHCI_C2_CONFIG_Cmd_Param_t config_param = {
  39. .PayloadCmdSize = SHCI_C2_CONFIG_PAYLOAD_CMD_SIZE,
  40. .Config1 =SHCI_C2_CONFIG_CONFIG1_BIT0_BLE_NVM_DATA_TO_SRAM,
  41. .BleNvmRamAddress = (uint32_t)ble_app_nvm,
  42. .EvtMask1 = SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE,
  43. };
  44. status = SHCI_C2_Config(&config_param);
  45. if(status) {
  46. FURI_LOG_E(TAG, "Failed to configure 2nd core: %d", status);
  47. }
  48. // Start ble stack on 2nd core
  49. SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = {
  50. .Header = {{0,0,0}}, // Header unused
  51. .Param = {
  52. 0, // pBleBufferAddress not used
  53. 0, // BleBufferSize not used
  54. CFG_BLE_NUM_GATT_ATTRIBUTES,
  55. CFG_BLE_NUM_GATT_SERVICES,
  56. CFG_BLE_ATT_VALUE_ARRAY_SIZE,
  57. CFG_BLE_NUM_LINK,
  58. CFG_BLE_DATA_LENGTH_EXTENSION,
  59. CFG_BLE_PREPARE_WRITE_LIST_SIZE,
  60. CFG_BLE_MBLOCK_COUNT,
  61. CFG_BLE_MAX_ATT_MTU,
  62. CFG_BLE_SLAVE_SCA,
  63. CFG_BLE_MASTER_SCA,
  64. CFG_BLE_LSE_SOURCE,
  65. CFG_BLE_MAX_CONN_EVENT_LENGTH,
  66. CFG_BLE_HSE_STARTUP_TIME,
  67. CFG_BLE_VITERBI_MODE,
  68. CFG_BLE_LL_ONLY,
  69. 0,
  70. }
  71. };
  72. status = SHCI_C2_BLE_Init(&ble_init_cmd_packet);
  73. if(status) {
  74. FURI_LOG_E(TAG, "Failed to start ble stack: %d", status);
  75. }
  76. return status == SHCI_Success;
  77. }
  78. void ble_app_get_key_storage_buff(uint8_t** addr, uint16_t* size) {
  79. *addr = (uint8_t*)ble_app_nvm;
  80. *size = sizeof(ble_app_nvm);
  81. }
  82. static void ble_app_hci_thread(void *arg) {
  83. while(1) {
  84. osThreadFlagsWait(1, osFlagsWaitAny, osWaitForever);
  85. hci_user_evt_proc();
  86. }
  87. }
  88. // Called by WPAN lib
  89. void hci_notify_asynch_evt(void* pdata) {
  90. osThreadFlagsSet(ble_app->hci_thread_id, 1);
  91. }
  92. void hci_cmd_resp_release(uint32_t flag) {
  93. osSemaphoreRelease(ble_app->hci_sem);
  94. }
  95. void hci_cmd_resp_wait(uint32_t timeout) {
  96. osSemaphoreAcquire(ble_app->hci_sem, osWaitForever);
  97. }
  98. static void ble_app_hci_event_handler( void * pPayload ) {
  99. SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  100. tHCI_UserEvtRxParam *pParam = (tHCI_UserEvtRxParam *)pPayload;
  101. svctl_return_status = SVCCTL_UserEvtRx((void *)&(pParam->pckt->evtserial));
  102. if (svctl_return_status != SVCCTL_UserEvtFlowDisable) {
  103. pParam->status = HCI_TL_UserEventFlow_Enable;
  104. } else {
  105. pParam->status = HCI_TL_UserEventFlow_Disable;
  106. }
  107. }
  108. static void ble_app_hci_status_not_handler( HCI_TL_CmdStatus_t status ) {
  109. if(status == HCI_TL_CmdBusy) {
  110. osMutexAcquire(ble_app->hci_mtx, osWaitForever );
  111. } else if(status == HCI_TL_CmdAvailable) {
  112. osMutexRelease(ble_app->hci_mtx);
  113. }
  114. }
  115. void SVCCTL_ResumeUserEventFlow( void ) {
  116. hci_resume_flow();
  117. }