esp_bt_main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "esp_bt_main.h"
  14. #include "btc/btc_task.h"
  15. #include "btc/btc_main.h"
  16. #include "esp_bt.h"
  17. #include "osi/future.h"
  18. #include "osi/allocator.h"
  19. static bool bd_already_enable = false;
  20. static bool bd_already_init = false;
  21. esp_bluedroid_status_t esp_bluedroid_get_status(void)
  22. {
  23. if (bd_already_init) {
  24. if (bd_already_enable) {
  25. return ESP_BLUEDROID_STATUS_ENABLED;
  26. } else {
  27. return ESP_BLUEDROID_STATUS_INITIALIZED;
  28. }
  29. } else {
  30. return ESP_BLUEDROID_STATUS_UNINITIALIZED;
  31. }
  32. }
  33. esp_err_t esp_bluedroid_enable(void)
  34. {
  35. btc_msg_t msg;
  36. future_t **future_p;
  37. if (!bd_already_init) {
  38. LOG_ERROR("Bludroid not initialised\n");
  39. return ESP_ERR_INVALID_STATE;
  40. }
  41. if (bd_already_enable) {
  42. LOG_ERROR("Bluedroid already enabled\n");
  43. return ESP_ERR_INVALID_STATE;
  44. }
  45. future_p = btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE);
  46. *future_p = future_new();
  47. if (*future_p == NULL) {
  48. LOG_ERROR("Bluedroid enable failed\n");
  49. return ESP_ERR_NO_MEM;
  50. }
  51. msg.sig = BTC_SIG_API_CALL;
  52. msg.pid = BTC_PID_MAIN_INIT;
  53. msg.act = BTC_MAIN_ACT_ENABLE;
  54. if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
  55. LOG_ERROR("Bluedroid enable failed\n");
  56. return ESP_FAIL;
  57. }
  58. if (future_await(*future_p) == FUTURE_FAIL) {
  59. LOG_ERROR("Bluedroid enable failed\n");
  60. return ESP_FAIL;
  61. }
  62. bd_already_enable = true;
  63. return ESP_OK;
  64. }
  65. esp_err_t esp_bluedroid_disable(void)
  66. {
  67. btc_msg_t msg;
  68. future_t **future_p;
  69. if (!bd_already_enable) {
  70. LOG_ERROR("Bluedroid already disabled\n");
  71. return ESP_ERR_INVALID_STATE;
  72. }
  73. future_p = btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE);
  74. *future_p = future_new();
  75. if (*future_p == NULL) {
  76. LOG_ERROR("Bluedroid disable failed\n");
  77. return ESP_ERR_NO_MEM;
  78. }
  79. msg.sig = BTC_SIG_API_CALL;
  80. msg.pid = BTC_PID_MAIN_INIT;
  81. msg.act = BTC_MAIN_ACT_DISABLE;
  82. if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
  83. LOG_ERROR("Bluedroid disable failed\n");
  84. return ESP_FAIL;
  85. }
  86. if (future_await(*future_p) == FUTURE_FAIL) {
  87. LOG_ERROR("Bluedroid disable failed\n");
  88. return ESP_FAIL;
  89. }
  90. bd_already_enable = false;
  91. return ESP_OK;
  92. }
  93. esp_err_t esp_bluedroid_init(void)
  94. {
  95. btc_msg_t msg;
  96. future_t **future_p;
  97. bt_status_t ret;
  98. if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  99. LOG_ERROR("Controller not initialised\n");
  100. return ESP_ERR_INVALID_STATE;
  101. }
  102. if (bd_already_init) {
  103. LOG_ERROR("Bluedroid already initialised\n");
  104. return ESP_ERR_INVALID_STATE;
  105. }
  106. #if HEAP_MEMORY_DEBUG
  107. osi_mem_dbg_init();
  108. #endif
  109. /*
  110. * BTC Init
  111. */
  112. ret = btc_init();
  113. if (ret != BT_STATUS_SUCCESS) {
  114. LOG_ERROR("Bluedroid Initialize Fail");
  115. return ESP_FAIL;
  116. }
  117. future_p = btc_main_get_future_p(BTC_MAIN_INIT_FUTURE);
  118. *future_p = future_new();
  119. if (*future_p == NULL) {
  120. LOG_ERROR("Bluedroid Initialize Fail!");
  121. return ESP_ERR_NO_MEM;
  122. }
  123. msg.sig = BTC_SIG_API_CALL;
  124. msg.pid = BTC_PID_MAIN_INIT;
  125. msg.act = BTC_MAIN_ACT_INIT;
  126. if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
  127. LOG_ERROR("Bluedroid Initialize Fail");
  128. return ESP_FAIL;
  129. }
  130. if (future_await(*future_p) == FUTURE_FAIL) {
  131. LOG_ERROR("Bluedroid Initialize Fail");
  132. return ESP_FAIL;
  133. }
  134. bd_already_init = true;
  135. return ESP_OK;
  136. }
  137. esp_err_t esp_bluedroid_deinit(void)
  138. {
  139. btc_msg_t msg;
  140. future_t **future_p;
  141. if (!bd_already_init) {
  142. LOG_ERROR("Bluedroid already de-initialised\n");
  143. return ESP_ERR_INVALID_STATE;
  144. }
  145. if (bd_already_enable) {
  146. LOG_ERROR("Bludroid already enabled, do disable first\n");
  147. return ESP_ERR_INVALID_STATE;
  148. }
  149. future_p = btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE);
  150. *future_p = future_new();
  151. if (*future_p == NULL) {
  152. LOG_ERROR("Bluedroid de-initialise failed\n");
  153. return ESP_ERR_NO_MEM;
  154. }
  155. msg.sig = BTC_SIG_API_CALL;
  156. msg.pid = BTC_PID_MAIN_INIT;
  157. msg.act = BTC_MAIN_ACT_DEINIT;
  158. if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
  159. LOG_ERROR("Bluedroid de-initialise failed\n");
  160. return ESP_FAIL;
  161. }
  162. if (future_await(*future_p) == FUTURE_FAIL) {
  163. LOG_ERROR("Bluedroid de-initialise failed\n");
  164. return ESP_FAIL;
  165. }
  166. btc_deinit();
  167. bd_already_init = false;
  168. return ESP_OK;
  169. }