main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "gatt_svc.h"
  10. #include "heart_rate.h"
  11. #include "led.h"
  12. /* Library function declarations */
  13. void ble_store_config_init(void);
  14. /* Private function declarations */
  15. static void on_stack_reset(int reason);
  16. static void on_stack_sync(void);
  17. static void nimble_host_config_init(void);
  18. static void nimble_host_task(void *param);
  19. /* Private functions */
  20. /*
  21. * Stack event callback functions
  22. * - on_stack_reset is called when host resets BLE stack due to errors
  23. * - on_stack_sync is called when host has synced with controller
  24. */
  25. static void on_stack_reset(int reason) {
  26. /* On reset, print reset reason to console */
  27. ESP_LOGI(TAG, "nimble stack reset, reset reason: %d", reason);
  28. }
  29. static void on_stack_sync(void) {
  30. /* On stack sync, do advertising initialization */
  31. adv_init();
  32. }
  33. static void nimble_host_config_init(void) {
  34. /* Set host callbacks */
  35. ble_hs_cfg.reset_cb = on_stack_reset;
  36. ble_hs_cfg.sync_cb = on_stack_sync;
  37. ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb;
  38. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  39. /* Store host configuration */
  40. ble_store_config_init();
  41. }
  42. static void nimble_host_task(void *param) {
  43. /* Task entry log */
  44. ESP_LOGI(TAG, "nimble host task has been started!");
  45. /* This function won't return until nimble_port_stop() is executed */
  46. nimble_port_run();
  47. /* Clean up at exit */
  48. vTaskDelete(NULL);
  49. }
  50. static void heart_rate_task(void *param) {
  51. /* Task entry log */
  52. ESP_LOGI(TAG, "heart rate task has been started!");
  53. /* Loop forever */
  54. while (1) {
  55. /* Update heart rate value every 1 second */
  56. update_heart_rate();
  57. ESP_LOGI(TAG, "heart rate updated to %d", get_heart_rate());
  58. /* Send heart rate indication if enabled */
  59. send_heart_rate_indication();
  60. /* Sleep */
  61. vTaskDelay(HEART_RATE_TASK_PERIOD);
  62. }
  63. /* Clean up at exit */
  64. vTaskDelete(NULL);
  65. }
  66. void app_main(void) {
  67. /* Local variables */
  68. int rc;
  69. esp_err_t ret;
  70. /* LED initialization */
  71. led_init();
  72. /*
  73. * NVS flash initialization
  74. * Dependency of BLE stack to store configurations
  75. */
  76. ret = nvs_flash_init();
  77. if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
  78. ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  79. ESP_ERROR_CHECK(nvs_flash_erase());
  80. ret = nvs_flash_init();
  81. }
  82. if (ret != ESP_OK) {
  83. ESP_LOGE(TAG, "failed to initialize nvs flash, error code: %d ", ret);
  84. return;
  85. }
  86. /* NimBLE stack initialization */
  87. ret = nimble_port_init();
  88. if (ret != ESP_OK) {
  89. ESP_LOGE(TAG, "failed to initialize nimble stack, error code: %d ",
  90. ret);
  91. return;
  92. }
  93. /* GAP service initialization */
  94. rc = gap_init();
  95. if (rc != 0) {
  96. ESP_LOGE(TAG, "failed to initialize GAP service, error code: %d", rc);
  97. return;
  98. }
  99. /* GATT server initialization */
  100. rc = gatt_svc_init();
  101. if (rc != 0) {
  102. ESP_LOGE(TAG, "failed to initialize GATT server, error code: %d", rc);
  103. return;
  104. }
  105. /* NimBLE host configuration initialization */
  106. nimble_host_config_init();
  107. /* Start NimBLE host task thread and return */
  108. xTaskCreate(nimble_host_task, "NimBLE Host", 4*1024, NULL, 5, NULL);
  109. xTaskCreate(heart_rate_task, "Heart Rate", 4*1024, NULL, 5, NULL);
  110. return;
  111. }