esp_bt_main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_task.h"
  15. #include "btc_main.h"
  16. #include "bt.h"
  17. #include "future.h"
  18. static bool bd_already_enable = false;
  19. static bool bd_already_init = false;
  20. esp_bluedroid_status_t esp_bluedroid_get_status(void)
  21. {
  22. if (bd_already_init) {
  23. if (bd_already_enable) {
  24. return ESP_BLUEDROID_STATUS_ENABLED;
  25. } else {
  26. return ESP_BLUEDROID_STATUS_INITIALIZED;
  27. }
  28. } else {
  29. return ESP_BLUEDROID_STATUS_UNINITIALIZED;
  30. }
  31. }
  32. esp_err_t esp_bluedroid_enable(void)
  33. {
  34. btc_msg_t msg;
  35. future_t **future_p;
  36. if (!bd_already_init) {
  37. LOG_ERROR("Bludroid not initialised\n");
  38. return ESP_ERR_INVALID_STATE;
  39. }
  40. if (bd_already_enable) {
  41. LOG_ERROR("Bluedroid already enabled\n");
  42. return ESP_ERR_INVALID_STATE;
  43. }
  44. future_p = btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE);
  45. *future_p = future_new();
  46. if (*future_p == NULL) {
  47. LOG_ERROR("Bluedroid enable failed\n");
  48. return ESP_ERR_NO_MEM;
  49. }
  50. msg.sig = BTC_SIG_API_CALL;
  51. msg.pid = BTC_PID_MAIN_INIT;
  52. msg.act = BTC_MAIN_ACT_ENABLE;
  53. btc_transfer_context(&msg, NULL, 0, NULL);
  54. if (future_await(*future_p) == FUTURE_FAIL) {
  55. LOG_ERROR("Bluedroid enable failed\n");
  56. return ESP_FAIL;
  57. }
  58. bd_already_enable = true;
  59. return ESP_OK;
  60. }
  61. esp_err_t esp_bluedroid_disable(void)
  62. {
  63. btc_msg_t msg;
  64. future_t **future_p;
  65. if (!bd_already_enable) {
  66. LOG_ERROR("Bluedroid already disabled\n");
  67. return ESP_ERR_INVALID_STATE;
  68. }
  69. future_p = btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE);
  70. *future_p = future_new();
  71. if (*future_p == NULL) {
  72. LOG_ERROR("Bluedroid disable failed\n");
  73. return ESP_ERR_NO_MEM;
  74. }
  75. msg.sig = BTC_SIG_API_CALL;
  76. msg.pid = BTC_PID_MAIN_INIT;
  77. msg.act = BTC_MAIN_ACT_DISABLE;
  78. btc_transfer_context(&msg, NULL, 0, NULL);
  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. if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
  91. LOG_ERROR("Conroller not initialised\n");
  92. return ESP_ERR_INVALID_STATE;
  93. }
  94. if (bd_already_init) {
  95. LOG_ERROR("Bluedroid already initialised\n");
  96. return ESP_ERR_INVALID_STATE;
  97. }
  98. future_p = btc_main_get_future_p(BTC_MAIN_INIT_FUTURE);
  99. *future_p = future_new();
  100. if (*future_p == NULL) {
  101. LOG_ERROR("Bluedroid initialise failed\n");
  102. return ESP_ERR_NO_MEM;
  103. }
  104. btc_init();
  105. msg.sig = BTC_SIG_API_CALL;
  106. msg.pid = BTC_PID_MAIN_INIT;
  107. msg.act = BTC_MAIN_ACT_INIT;
  108. btc_transfer_context(&msg, NULL, 0, NULL);
  109. if (future_await(*future_p) == FUTURE_FAIL) {
  110. LOG_ERROR("Bluedroid initialise failed\n");
  111. return ESP_FAIL;
  112. }
  113. bd_already_init = true;
  114. return ESP_OK;
  115. }
  116. esp_err_t esp_bluedroid_deinit(void)
  117. {
  118. btc_msg_t msg;
  119. future_t **future_p;
  120. if (!bd_already_init) {
  121. LOG_ERROR("Bluedroid already de-initialised\n");
  122. return ESP_ERR_INVALID_STATE;
  123. }
  124. if (bd_already_enable) {
  125. LOG_ERROR("Bludroid already enabled, do disable first\n");
  126. return ESP_ERR_INVALID_STATE;
  127. }
  128. future_p = btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE);
  129. *future_p = future_new();
  130. if (*future_p == NULL) {
  131. LOG_ERROR("Bluedroid de-initialise failed\n");
  132. return ESP_ERR_NO_MEM;
  133. }
  134. msg.sig = BTC_SIG_API_CALL;
  135. msg.pid = BTC_PID_MAIN_INIT;
  136. msg.act = BTC_MAIN_ACT_DEINIT;
  137. btc_transfer_context(&msg, NULL, 0, NULL);
  138. if (future_await(*future_p) == FUTURE_FAIL) {
  139. LOG_ERROR("Bluedroid de-initialise failed\n");
  140. return ESP_FAIL;
  141. }
  142. btc_deinit();
  143. bd_already_init = false;
  144. return ESP_OK;
  145. }