bt_app_core.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10. #include "esp_log.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/queue.h"
  13. #include "freertos/task.h"
  14. #include "esp_bt.h"
  15. #include "esp_bt_main.h"
  16. #include "esp_gap_bt_api.h"
  17. #include "bt_app_core.h"
  18. static const char *TAG = "btappcore";
  19. static void bt_app_task_handler(void *arg);
  20. static bool bt_app_send_msg(bt_app_msg_t *msg);
  21. static void bt_app_work_dispatched(bt_app_msg_t *msg);
  22. static xQueueHandle s_bt_app_task_queue;
  23. static bool running;
  24. bool bt_app_work_dispatch(bt_app_cb_t p_cback, uint16_t event, void *p_params, int param_len, bt_app_copy_cb_t p_copy_cback)
  25. {
  26. ESP_LOGV(TAG,"%s event 0x%x, param len %d", __func__, event, param_len);
  27. bt_app_msg_t msg;
  28. memset(&msg, 0, sizeof(bt_app_msg_t));
  29. msg.sig = BT_APP_SIG_WORK_DISPATCH;
  30. msg.event = event;
  31. msg.cb = p_cback;
  32. if (param_len == 0) {
  33. return bt_app_send_msg(&msg);
  34. } else if (p_params && param_len > 0) {
  35. if ((msg.param = malloc(param_len)) != NULL) {
  36. memcpy(msg.param, p_params, param_len);
  37. /* check if caller has provided a copy callback to do the deep copy */
  38. if (p_copy_cback) {
  39. p_copy_cback(&msg, msg.param, p_params);
  40. }
  41. return bt_app_send_msg(&msg);
  42. }
  43. }
  44. return false;
  45. }
  46. static bool bt_app_send_msg(bt_app_msg_t *msg)
  47. {
  48. if (msg == NULL) {
  49. return false;
  50. }
  51. if (xQueueSend(s_bt_app_task_queue, msg, 10 / portTICK_RATE_MS) != pdTRUE) {
  52. ESP_LOGE(TAG,"%s xQueue send failed", __func__);
  53. return false;
  54. }
  55. return true;
  56. }
  57. static void bt_app_work_dispatched(bt_app_msg_t *msg)
  58. {
  59. if (msg->cb) {
  60. msg->cb(msg->event, msg->param);
  61. }
  62. }
  63. static void bt_app_task_handler(void *arg)
  64. {
  65. bt_app_msg_t msg;
  66. esp_err_t err;
  67. s_bt_app_task_queue = xQueueCreate(10, sizeof(bt_app_msg_t));
  68. esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
  69. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  70. if ((err = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
  71. ESP_LOGE(TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(err));
  72. goto exit;
  73. }
  74. if ((err = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) {
  75. ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(err));
  76. goto exit;
  77. }
  78. if ((err = esp_bluedroid_init()) != ESP_OK) {
  79. ESP_LOGE(TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err));
  80. goto exit;
  81. }
  82. if ((err = esp_bluedroid_enable()) != ESP_OK) {
  83. ESP_LOGE(TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err));
  84. goto exit;
  85. }
  86. /* Bluetooth device name, connection mode and profile set up */
  87. bt_app_work_dispatch((bt_av_hdl_stack_evt_t*) arg, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
  88. #if (CONFIG_BT_SSP_ENABLED)
  89. /* Set default parameters for Secure Simple Pairing */
  90. esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  91. esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
  92. esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
  93. #endif
  94. running = true;
  95. while (running) {
  96. if (pdTRUE == xQueueReceive(s_bt_app_task_queue, &msg, (portTickType)portMAX_DELAY)) {
  97. ESP_LOGV(TAG,"%s, sig 0x%x, 0x%x", __func__, msg.sig, msg.event);
  98. switch (msg.sig) {
  99. case BT_APP_SIG_WORK_DISPATCH:
  100. bt_app_work_dispatched(&msg);
  101. break;
  102. default:
  103. ESP_LOGW(TAG,"%s, unhandled sig: %d", __func__, msg.sig);
  104. break;
  105. }
  106. if (msg.param) {
  107. free(msg.param);
  108. }
  109. } else {
  110. ESP_LOGW(TAG,"No messaged received from queue.");
  111. }
  112. }
  113. ESP_LOGD(TAG, "bt_app_task shutting down");
  114. if (esp_bluedroid_disable() != ESP_OK) goto exit;
  115. // this disable has a sleep timer BTA_DISABLE_DELAY in bt_target.h and
  116. // if we don't wait for it then disable crashes... don't know why
  117. vTaskDelay(2*200 / portTICK_PERIOD_MS);
  118. ESP_LOGD(TAG, "esp_bluedroid_disable called successfully");
  119. if (esp_bluedroid_deinit() != ESP_OK) goto exit;
  120. ESP_LOGD(TAG, "esp_bluedroid_deinit called successfully");
  121. if (esp_bt_controller_disable() != ESP_OK) goto exit;
  122. ESP_LOGD(TAG, "esp_bt_controller_disable called successfully");
  123. if (esp_bt_controller_deinit() != ESP_OK) goto exit;
  124. ESP_LOGD(TAG, "bt stopped successfully");
  125. exit:
  126. vQueueDelete(s_bt_app_task_queue);
  127. running = false;
  128. vTaskDelete(NULL);
  129. }
  130. void bt_app_task_start_up(bt_av_hdl_stack_evt_t* handler)
  131. {
  132. xTaskCreate(bt_app_task_handler, "BtAppT", 4096, handler, configMAX_PRIORITIES - 3, NULL);
  133. }
  134. void bt_app_task_shut_down(void)
  135. {
  136. running = false;
  137. }