ble_app.c 3.5 KB

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