blufi_init.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include "esp_err.h"
  8. #include "esp_blufi_api.h"
  9. #include "esp_log.h"
  10. #include "esp_blufi.h"
  11. #include "blufi.h"
  12. #if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
  13. #include "esp_bt.h"
  14. #endif
  15. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  16. #include "esp_bt_main.h"
  17. #include "esp_bt_device.h"
  18. #endif
  19. #ifdef CONFIG_BT_NIMBLE_ENABLED
  20. #include "nimble/nimble_port.h"
  21. #include "nimble/nimble_port_freertos.h"
  22. #include "host/ble_hs.h"
  23. #include "host/util/util.h"
  24. #include "services/gap/ble_svc_gap.h"
  25. #include "services/gatt/ble_svc_gatt.h"
  26. #include "console/console.h"
  27. #endif
  28. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  29. esp_err_t esp_blufi_host_init(void)
  30. {
  31. int ret;
  32. ret = esp_bluedroid_init();
  33. if (ret) {
  34. BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  35. return ESP_FAIL;
  36. }
  37. ret = esp_bluedroid_enable();
  38. if (ret) {
  39. BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  40. return ESP_FAIL;
  41. }
  42. BLUFI_INFO("BD ADDR: "ESP_BD_ADDR_STR"\n", ESP_BD_ADDR_HEX(esp_bt_dev_get_address()));
  43. return ESP_OK;
  44. }
  45. esp_err_t esp_blufi_host_deinit(void)
  46. {
  47. int ret;
  48. ret = esp_blufi_profile_deinit();
  49. if(ret != ESP_OK) {
  50. return ret;
  51. }
  52. ret = esp_bluedroid_disable();
  53. if (ret) {
  54. BLUFI_ERROR("%s deinit bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  55. return ESP_FAIL;
  56. }
  57. ret = esp_bluedroid_deinit();
  58. if (ret) {
  59. BLUFI_ERROR("%s deinit bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  60. return ESP_FAIL;
  61. }
  62. return ESP_OK;
  63. }
  64. esp_err_t esp_blufi_gap_register_callback(void)
  65. {
  66. int rc;
  67. rc = esp_ble_gap_register_callback(esp_blufi_gap_event_handler);
  68. if(rc){
  69. return rc;
  70. }
  71. return esp_blufi_profile_init();
  72. }
  73. esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks)
  74. {
  75. esp_err_t ret = ESP_OK;
  76. ret = esp_blufi_host_init();
  77. if (ret) {
  78. BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret));
  79. return ret;
  80. }
  81. ret = esp_blufi_register_callbacks(example_callbacks);
  82. if(ret){
  83. BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret);
  84. return ret;
  85. }
  86. ret = esp_blufi_gap_register_callback();
  87. if(ret){
  88. BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret);
  89. return ret;
  90. }
  91. return ESP_OK;
  92. }
  93. #endif /* CONFIG_BT_BLUEDROID_ENABLED */
  94. #if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
  95. esp_err_t esp_blufi_controller_init() {
  96. esp_err_t ret = ESP_OK;
  97. #if CONFIG_IDF_TARGET_ESP32
  98. ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
  99. #endif
  100. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  101. ret = esp_bt_controller_init(&bt_cfg);
  102. if (ret) {
  103. BLUFI_ERROR("%s initialize bt controller failed: %s\n", __func__, esp_err_to_name(ret));
  104. return ret;
  105. }
  106. ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
  107. if (ret) {
  108. BLUFI_ERROR("%s enable bt controller failed: %s\n", __func__, esp_err_to_name(ret));
  109. return ret;
  110. }
  111. return ret;
  112. }
  113. #endif
  114. #if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
  115. esp_err_t esp_blufi_controller_deinit() {
  116. esp_err_t ret = ESP_OK;
  117. ret = esp_bt_controller_disable();
  118. if (ret) {
  119. BLUFI_ERROR("%s disable bt controller failed: %s\n", __func__, esp_err_to_name(ret));
  120. return ret;
  121. }
  122. ret = esp_bt_controller_deinit();
  123. if (ret) {
  124. BLUFI_ERROR("%s deinit bt controller failed: %s\n", __func__, esp_err_to_name(ret));
  125. return ret;
  126. }
  127. return ret;
  128. }
  129. #endif
  130. #ifdef CONFIG_BT_NIMBLE_ENABLED
  131. void ble_store_config_init(void);
  132. static void blufi_on_reset(int reason)
  133. {
  134. MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
  135. }
  136. static void
  137. blufi_on_sync(void)
  138. {
  139. esp_blufi_profile_init();
  140. }
  141. void bleprph_host_task(void *param)
  142. {
  143. ESP_LOGI("BLUFI_EXAMPLE", "BLE Host Task Started");
  144. /* This function will return only when nimble_port_stop() is executed */
  145. nimble_port_run();
  146. nimble_port_freertos_deinit();
  147. }
  148. esp_err_t esp_blufi_host_init(void)
  149. {
  150. esp_err_t err;
  151. err = esp_nimble_init();
  152. if (err) {
  153. BLUFI_ERROR("%s failed: %s\n", __func__, esp_err_to_name(err));
  154. return ESP_FAIL;
  155. }
  156. /* Initialize the NimBLE host configuration. */
  157. ble_hs_cfg.reset_cb = blufi_on_reset;
  158. ble_hs_cfg.sync_cb = blufi_on_sync;
  159. ble_hs_cfg.gatts_register_cb = esp_blufi_gatt_svr_register_cb;
  160. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  161. ble_hs_cfg.sm_io_cap = 4;
  162. #ifdef CONFIG_EXAMPLE_BONDING
  163. ble_hs_cfg.sm_bonding = 1;
  164. #endif
  165. #ifdef CONFIG_EXAMPLE_MITM
  166. ble_hs_cfg.sm_mitm = 1;
  167. #endif
  168. #ifdef CONFIG_EXAMPLE_USE_SC
  169. ble_hs_cfg.sm_sc = 1;
  170. #else
  171. ble_hs_cfg.sm_sc = 0;
  172. #ifdef CONFIG_EXAMPLE_BONDING
  173. ble_hs_cfg.sm_our_key_dist = 1;
  174. ble_hs_cfg.sm_their_key_dist = 1;
  175. #endif
  176. #endif
  177. int rc;
  178. rc = esp_blufi_gatt_svr_init();
  179. assert(rc == 0);
  180. /* Set the default device name. */
  181. rc = ble_svc_gap_device_name_set(BLUFI_DEVICE_NAME);
  182. assert(rc == 0);
  183. /* XXX Need to have template for store */
  184. ble_store_config_init();
  185. esp_blufi_btc_init();
  186. err = esp_nimble_enable(bleprph_host_task);
  187. if (err) {
  188. BLUFI_ERROR("%s failed: %s\n", __func__, esp_err_to_name(err));
  189. return ESP_FAIL;
  190. }
  191. return ESP_OK;
  192. }
  193. esp_err_t esp_blufi_host_deinit(void)
  194. {
  195. esp_err_t ret = ESP_OK;
  196. esp_blufi_gatt_svr_deinit();
  197. ret = nimble_port_stop();
  198. if (ret != ESP_OK) {
  199. return ret;
  200. }
  201. if (ret == 0) {
  202. esp_nimble_deinit();
  203. }
  204. ret = esp_blufi_profile_deinit();
  205. if (ret != ESP_OK) {
  206. return ret;
  207. }
  208. esp_blufi_btc_deinit();
  209. return ret;
  210. }
  211. esp_err_t esp_blufi_gap_register_callback(void)
  212. {
  213. return ESP_OK;
  214. }
  215. esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks)
  216. {
  217. esp_err_t ret = ESP_OK;
  218. ret = esp_blufi_register_callbacks(example_callbacks);
  219. if(ret){
  220. BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret);
  221. return ret;
  222. }
  223. ret = esp_blufi_gap_register_callback();
  224. if(ret){
  225. BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret);
  226. return ret;
  227. }
  228. ret = esp_blufi_host_init();
  229. if (ret) {
  230. BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret));
  231. return ret;
  232. }
  233. return ret;
  234. }
  235. #endif /* CONFIG_BT_NIMBLE_ENABLED */