main.c 4.2 KB

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