bt_app_core.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "bt_app_core.h"
  8. #include <stdint.h>
  9. #include "esp_system.h"
  10. #include <string.h>
  11. #include <stdbool.h>
  12. #include "esp_log.h"
  13. #include "freertos/xtensa_api.h"
  14. #include "freertos/FreeRTOSConfig.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/queue.h"
  17. #include "freertos/task.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 = NULL;
  23. static xTaskHandle s_bt_app_task_handle = NULL;
  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. for (;;) {
  67. if (pdTRUE == xQueueReceive(s_bt_app_task_queue, &msg, (portTickType)portMAX_DELAY)) {
  68. ESP_LOGV(TAG,"%s, sig 0x%x, 0x%x", __func__, msg.sig, msg.event);
  69. switch (msg.sig) {
  70. case BT_APP_SIG_WORK_DISPATCH:
  71. bt_app_work_dispatched(&msg);
  72. break;
  73. default:
  74. ESP_LOGW(TAG,"%s, unhandled sig: %d", __func__, msg.sig);
  75. break;
  76. } // switch (msg.sig)
  77. if (msg.param) {
  78. free(msg.param);
  79. }
  80. }
  81. else
  82. {
  83. ESP_LOGW(TAG,"No messaged received from queue.");
  84. }
  85. }
  86. }
  87. void bt_app_task_start_up(void)
  88. {
  89. s_bt_app_task_queue = xQueueCreate(10, sizeof(bt_app_msg_t));
  90. assert(s_bt_app_task_queue!=NULL);
  91. assert(xTaskCreate(bt_app_task_handler, "BtAppT", 4096, NULL, configMAX_PRIORITIES - 3, &s_bt_app_task_handle)==pdPASS);
  92. return;
  93. }
  94. void bt_app_task_shut_down(void)
  95. {
  96. if (s_bt_app_task_handle) {
  97. vTaskDelete(s_bt_app_task_handle);
  98. s_bt_app_task_handle = NULL;
  99. }
  100. if (s_bt_app_task_queue) {
  101. vQueueDelete(s_bt_app_task_queue);
  102. s_bt_app_task_queue = NULL;
  103. }
  104. }