esp_bt_main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "common/bt_target.h"
  7. #include "esp_bt_main.h"
  8. #include "btc/btc_task.h"
  9. #include "btc/btc_main.h"
  10. #if (BT_CONTROLLER_INCLUDED == TRUE)
  11. #include "esp_bt.h"
  12. #endif
  13. #include "osi/future.h"
  14. #include "osi/allocator.h"
  15. #include "config/stack_config.h"
  16. static bool bd_already_enable = false;
  17. static bool bd_already_init = false;
  18. esp_bluedroid_status_t esp_bluedroid_get_status(void)
  19. {
  20. if (bd_already_init) {
  21. if (bd_already_enable) {
  22. return ESP_BLUEDROID_STATUS_ENABLED;
  23. } else {
  24. return ESP_BLUEDROID_STATUS_INITIALIZED;
  25. }
  26. } else {
  27. return ESP_BLUEDROID_STATUS_UNINITIALIZED;
  28. }
  29. }
  30. esp_err_t esp_bluedroid_enable(void)
  31. {
  32. btc_msg_t msg;
  33. future_t **future_p;
  34. if (!bd_already_init) {
  35. LOG_ERROR("Bludroid not initialised\n");
  36. return ESP_ERR_INVALID_STATE;
  37. }
  38. if (bd_already_enable) {
  39. LOG_ERROR("Bluedroid already enabled\n");
  40. return ESP_ERR_INVALID_STATE;
  41. }
  42. future_p = btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE);
  43. *future_p = future_new();
  44. if (*future_p == NULL) {
  45. LOG_ERROR("Bluedroid enable failed\n");
  46. return ESP_ERR_NO_MEM;
  47. }
  48. msg.sig = BTC_SIG_API_CALL;
  49. msg.pid = BTC_PID_MAIN_INIT;
  50. msg.act = BTC_MAIN_ACT_ENABLE;
  51. if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
  52. LOG_ERROR("Bluedroid enable failed\n");
  53. return ESP_FAIL;
  54. }
  55. if (future_await(*future_p) == FUTURE_FAIL) {
  56. LOG_ERROR("Bluedroid enable failed\n");
  57. return ESP_FAIL;
  58. }
  59. bd_already_enable = true;
  60. return ESP_OK;
  61. }
  62. esp_err_t esp_bluedroid_disable(void)
  63. {
  64. btc_msg_t msg;
  65. future_t **future_p;
  66. if (!bd_already_enable) {
  67. LOG_ERROR("Bluedroid already disabled\n");
  68. return ESP_ERR_INVALID_STATE;
  69. }
  70. future_p = btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE);
  71. *future_p = future_new();
  72. if (*future_p == NULL) {
  73. LOG_ERROR("Bluedroid disable failed\n");
  74. return ESP_ERR_NO_MEM;
  75. }
  76. msg.sig = BTC_SIG_API_CALL;
  77. msg.pid = BTC_PID_MAIN_INIT;
  78. msg.act = BTC_MAIN_ACT_DISABLE;
  79. if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
  80. LOG_ERROR("Bluedroid disable failed\n");
  81. return ESP_FAIL;
  82. }
  83. if (future_await(*future_p) == FUTURE_FAIL) {
  84. LOG_ERROR("Bluedroid disable failed\n");
  85. return ESP_FAIL;
  86. }
  87. bd_already_enable = false;
  88. return ESP_OK;
  89. }
  90. esp_err_t esp_bluedroid_init(void)
  91. {
  92. esp_bluedroid_config_t cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT();
  93. return esp_bluedroid_init_with_cfg(&cfg);
  94. }
  95. esp_err_t esp_bluedroid_init_with_cfg(esp_bluedroid_config_t *cfg)
  96. {
  97. btc_msg_t msg;
  98. future_t **future_p;
  99. bt_status_t ret;
  100. if (!cfg) {
  101. LOG_ERROR("%s cfg is NULL", __func__);
  102. return ESP_ERR_INVALID_ARG;
  103. }
  104. #if (BT_CONTROLLER_INCLUDED == TRUE)
  105. if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  106. LOG_ERROR("Controller not initialised\n");
  107. return ESP_ERR_INVALID_STATE;
  108. }
  109. #endif
  110. if (bd_already_init) {
  111. LOG_ERROR("Bluedroid already initialised\n");
  112. return ESP_ERR_INVALID_STATE;
  113. }
  114. #if HEAP_MEMORY_DEBUG
  115. osi_mem_dbg_init();
  116. #endif
  117. ret = bluedriod_config_init(cfg);
  118. if (ret != BT_STATUS_SUCCESS) {
  119. LOG_ERROR("Bluedroid stack initialize fail, ret:%d", ret);
  120. return ESP_FAIL;
  121. }
  122. /*
  123. * BTC Init
  124. */
  125. ret = btc_init();
  126. if (ret != BT_STATUS_SUCCESS) {
  127. LOG_ERROR("Bluedroid Initialize Fail");
  128. return ESP_FAIL;
  129. }
  130. future_p = btc_main_get_future_p(BTC_MAIN_INIT_FUTURE);
  131. *future_p = future_new();
  132. if (*future_p == NULL) {
  133. LOG_ERROR("Bluedroid Initialize Fail!");
  134. return ESP_ERR_NO_MEM;
  135. }
  136. msg.sig = BTC_SIG_API_CALL;
  137. msg.pid = BTC_PID_MAIN_INIT;
  138. msg.act = BTC_MAIN_ACT_INIT;
  139. if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
  140. LOG_ERROR("Bluedroid Initialize Fail");
  141. return ESP_FAIL;
  142. }
  143. if (future_await(*future_p) == FUTURE_FAIL) {
  144. LOG_ERROR("Bluedroid Initialize Fail");
  145. return ESP_FAIL;
  146. }
  147. bd_already_init = true;
  148. return ESP_OK;
  149. }
  150. esp_err_t esp_bluedroid_deinit(void)
  151. {
  152. btc_msg_t msg;
  153. future_t **future_p;
  154. if (!bd_already_init) {
  155. LOG_ERROR("Bluedroid already de-initialised\n");
  156. return ESP_ERR_INVALID_STATE;
  157. }
  158. if (bd_already_enable) {
  159. LOG_ERROR("Bludroid already enabled, do disable first\n");
  160. return ESP_ERR_INVALID_STATE;
  161. }
  162. future_p = btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE);
  163. *future_p = future_new();
  164. if (*future_p == NULL) {
  165. LOG_ERROR("Bluedroid de-initialise failed\n");
  166. return ESP_ERR_NO_MEM;
  167. }
  168. msg.sig = BTC_SIG_API_CALL;
  169. msg.pid = BTC_PID_MAIN_INIT;
  170. msg.act = BTC_MAIN_ACT_DEINIT;
  171. if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
  172. LOG_ERROR("Bluedroid de-initialise failed\n");
  173. return ESP_FAIL;
  174. }
  175. if (future_await(*future_p) == FUTURE_FAIL) {
  176. LOG_ERROR("Bluedroid de-initialise failed\n");
  177. return ESP_FAIL;
  178. }
  179. btc_deinit();
  180. bluedriod_config_deinit();
  181. bd_already_init = false;
  182. return ESP_OK;
  183. }