blufi_init.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 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_example.h"
  12. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  13. #include "esp_bt.h"
  14. #include "esp_bt_main.h"
  15. #include "esp_bt_device.h"
  16. #endif
  17. #ifdef CONFIG_BT_NIMBLE_ENABLED
  18. #include "nimble/nimble_port.h"
  19. #include "nimble/nimble_port_freertos.h"
  20. #include "host/ble_hs.h"
  21. #include "host/util/util.h"
  22. #include "services/gap/ble_svc_gap.h"
  23. #include "services/gatt/ble_svc_gatt.h"
  24. #include "console/console.h"
  25. #endif
  26. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  27. esp_err_t esp_blufi_host_init(void)
  28. {
  29. int ret;
  30. ret = esp_bluedroid_init();
  31. if (ret) {
  32. BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  33. return ESP_FAIL;
  34. }
  35. ret = esp_bluedroid_enable();
  36. if (ret) {
  37. BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  38. return ESP_FAIL;
  39. }
  40. BLUFI_INFO("BD ADDR: "ESP_BD_ADDR_STR"\n", ESP_BD_ADDR_HEX(esp_bt_dev_get_address()));
  41. return ESP_OK;
  42. }
  43. esp_err_t esp_blufi_gap_register_callback(void)
  44. {
  45. int rc;
  46. rc = esp_ble_gap_register_callback(esp_blufi_gap_event_handler);
  47. if(rc){
  48. return rc;
  49. }
  50. return esp_blufi_profile_init();
  51. }
  52. esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks)
  53. {
  54. esp_err_t ret = ESP_OK;
  55. ret = esp_blufi_host_init();
  56. if (ret) {
  57. BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret));
  58. return ret;
  59. }
  60. ret = esp_blufi_register_callbacks(example_callbacks);
  61. if(ret){
  62. BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret);
  63. return ret;
  64. }
  65. ret = esp_blufi_gap_register_callback();
  66. if(ret){
  67. BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret);
  68. return ret;
  69. }
  70. return ESP_OK;
  71. }
  72. #endif /* CONFIG_BT_BLUEDROID_ENABLED */
  73. #ifdef CONFIG_BT_NIMBLE_ENABLED
  74. void ble_store_config_init(void);
  75. static void blufi_on_reset(int reason)
  76. {
  77. MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
  78. }
  79. static void
  80. blufi_on_sync(void)
  81. {
  82. esp_blufi_profile_init();
  83. }
  84. void bleprph_host_task(void *param)
  85. {
  86. ESP_LOGI("BLUFI_EXAMPLE", "BLE Host Task Started");
  87. /* This function will return only when nimble_port_stop() is executed */
  88. nimble_port_run();
  89. nimble_port_freertos_deinit();
  90. }
  91. esp_err_t esp_blufi_host_init(void)
  92. {
  93. esp_err_t err;
  94. err = esp_nimble_init();
  95. if (err) {
  96. BLUFI_ERROR("%s failed: %s\n", __func__, esp_err_to_name(err));
  97. return ESP_FAIL;
  98. }
  99. /* Initialize the NimBLE host configuration. */
  100. ble_hs_cfg.reset_cb = blufi_on_reset;
  101. ble_hs_cfg.sync_cb = blufi_on_sync;
  102. ble_hs_cfg.gatts_register_cb = esp_blufi_gatt_svr_register_cb;
  103. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  104. ble_hs_cfg.sm_io_cap = 4;
  105. #ifdef CONFIG_EXAMPLE_BONDING
  106. ble_hs_cfg.sm_bonding = 1;
  107. #endif
  108. #ifdef CONFIG_EXAMPLE_MITM
  109. ble_hs_cfg.sm_mitm = 1;
  110. #endif
  111. #ifdef CONFIG_EXAMPLE_USE_SC
  112. ble_hs_cfg.sm_sc = 1;
  113. #else
  114. ble_hs_cfg.sm_sc = 0;
  115. #ifdef CONFIG_EXAMPLE_BONDING
  116. ble_hs_cfg.sm_our_key_dist = 1;
  117. ble_hs_cfg.sm_their_key_dist = 1;
  118. #endif
  119. #endif
  120. int rc;
  121. rc = esp_blufi_gatt_svr_init();
  122. assert(rc == 0);
  123. /* Set the default device name. */
  124. rc = ble_svc_gap_device_name_set(BLUFI_DEVICE_NAME);
  125. assert(rc == 0);
  126. /* XXX Need to have template for store */
  127. ble_store_config_init();
  128. esp_blufi_btc_init();
  129. err = esp_nimble_enable(bleprph_host_task);
  130. if (err) {
  131. BLUFI_ERROR("%s failed: %s\n", __func__, esp_err_to_name(err));
  132. return ESP_FAIL;
  133. }
  134. return ESP_OK;
  135. }
  136. esp_err_t esp_blufi_gap_register_callback(void)
  137. {
  138. return ESP_OK;
  139. }
  140. esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks)
  141. {
  142. esp_err_t ret = ESP_OK;
  143. ret = esp_blufi_register_callbacks(example_callbacks);
  144. if(ret){
  145. BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret);
  146. return ret;
  147. }
  148. ret = esp_blufi_gap_register_callback();
  149. if(ret){
  150. BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret);
  151. return ret;
  152. }
  153. ret = esp_blufi_host_init();
  154. if (ret) {
  155. BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret));
  156. return ret;
  157. }
  158. return ret;
  159. }
  160. #endif /* CONFIG_BT_NIMBLE_ENABLED */