blufi_init.c 7.1 KB

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