blufi_init.c 4.2 KB

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