main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /* Includes */
  7. #include "common.h"
  8. #include "gap.h"
  9. #include "led.h"
  10. /* Library function declarations */
  11. void ble_store_config_init(void);
  12. /* Private function declarations */
  13. static void on_stack_reset(int reason);
  14. static void on_stack_sync(void);
  15. static void nimble_host_config_init(void);
  16. static void nimble_host_task(void *param);
  17. /* Private functions */
  18. /*
  19. * Stack event callback functions
  20. * - on_stack_reset is called when host resets BLE stack due to errors
  21. * - on_stack_sync is called when host has synced with controller
  22. */
  23. static void on_stack_reset(int reason) {
  24. /* On reset, print reset reason to console */
  25. ESP_LOGI(TAG, "nimble stack reset, reset reason: %d", reason);
  26. }
  27. static void on_stack_sync(void) {
  28. /* On stack sync, do advertising initialization */
  29. adv_init();
  30. }
  31. static void nimble_host_config_init(void) {
  32. /* Set host callbacks */
  33. ble_hs_cfg.reset_cb = on_stack_reset;
  34. ble_hs_cfg.sync_cb = on_stack_sync;
  35. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  36. // ble_hs_cfg.
  37. /* Store host configuration */
  38. ble_store_config_init();
  39. }
  40. static void nimble_host_task(void *param) {
  41. /* Task entry log */
  42. ESP_LOGI(TAG, "nimble host task has been started!");
  43. /* This function won't return until nimble_port_stop() is executed */
  44. nimble_port_run();
  45. /* Clean up at exit */
  46. vTaskDelete(NULL);
  47. }
  48. void app_main(void) {
  49. /* Local variables */
  50. int rc = 0;
  51. esp_err_t ret = ESP_OK;
  52. /* LED initialization */
  53. led_init();
  54. /* NVS flash initialization */
  55. ret = nvs_flash_init();
  56. if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
  57. ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  58. ESP_ERROR_CHECK(nvs_flash_erase());
  59. ret = nvs_flash_init();
  60. }
  61. if (ret != ESP_OK) {
  62. ESP_LOGE(TAG, "failed to initialize nvs flash, error code: %d ", ret);
  63. return;
  64. }
  65. /* NimBLE stack initialization */
  66. ret = nimble_port_init();
  67. if (ret != ESP_OK) {
  68. ESP_LOGE(TAG, "failed to initialize nimble stack, error code: %d ",
  69. ret);
  70. return;
  71. }
  72. /* GAP service initialization */
  73. rc = gap_init();
  74. if (rc != 0) {
  75. ESP_LOGE(TAG, "failed to initialize GAP service, error code: %d", rc);
  76. return;
  77. }
  78. /* NimBLE host configuration initialization */
  79. nimble_host_config_init();
  80. /* Start NimBLE host task thread and return */
  81. xTaskCreate(nimble_host_task, "NimBLE Host", 4*1024, NULL, 5, NULL);
  82. return;
  83. }