esp_bt_main.c 4.7 KB

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