main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "esp_log.h"
  7. #include "nvs_flash.h"
  8. /* BLE */
  9. #include "nimble/nimble_port.h"
  10. #include "nimble/nimble_port_freertos.h"
  11. #include "host/ble_hs.h"
  12. #include "host/util/util.h"
  13. #include "console/console.h"
  14. #include "services/gap/ble_svc_gap.h"
  15. #include "periodic_sync.h"
  16. #include "host/ble_gap.h"
  17. static const char *tag = "NimBLE_BLE_PERIODIC_SYNC";
  18. static int synced = 0;
  19. static int periodic_sync_gap_event(struct ble_gap_event *event, void *arg);
  20. void ble_store_config_init(void);
  21. static void
  22. periodic_sync_scan(void)
  23. {
  24. uint8_t own_addr_type;
  25. struct ble_gap_disc_params disc_params;
  26. int rc;
  27. /* Figure out address to use while advertising (no privacy for now) */
  28. rc = ble_hs_id_infer_auto(0, &own_addr_type);
  29. if (rc != 0) {
  30. MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
  31. return;
  32. }
  33. /* Tell the controller to filter duplicates; we don't want to process
  34. * repeated advertisements from the same device.
  35. */
  36. disc_params.filter_duplicates = 0;
  37. /**
  38. * Perform a passive scan. I.e., don't send follow-up scan requests to
  39. * each advertiser.
  40. */
  41. disc_params.passive = 1;
  42. /* Use defaults for the rest of the parameters. */
  43. disc_params.itvl = 0;
  44. disc_params.window = 0;
  45. disc_params.filter_policy = 0;
  46. disc_params.limited = 0;
  47. rc = ble_gap_disc(own_addr_type, BLE_HS_FOREVER, &disc_params,
  48. periodic_sync_gap_event, NULL);
  49. if (rc != 0) {
  50. MODLOG_DFLT(ERROR, "Error initiating GAP discovery procedure; rc=%d\n",
  51. rc);
  52. }
  53. }
  54. void print_periodic_sync_data(struct ble_gap_event *event)
  55. {
  56. MODLOG_DFLT(DEBUG, "status : %d\nperiodic_sync_handle : %d\nsid : %d\n", event->periodic_sync.status, event->periodic_sync.sync_handle, event->periodic_sync.sid);
  57. MODLOG_DFLT(DEBUG, "adv addr : ");
  58. for (int i = 0; i < 6; i++) {
  59. MODLOG_DFLT(DEBUG, "%d ", event->periodic_sync.adv_addr.val[i]);
  60. }
  61. MODLOG_DFLT(DEBUG, "\nadv_phy : %s\n", event->periodic_sync.adv_phy == 1 ? "1m" : (event->periodic_sync.adv_phy == 2 ? "2m" : "coded"));
  62. MODLOG_DFLT(DEBUG, "per_adv_ival : %d\n", event->periodic_sync.per_adv_ival);
  63. MODLOG_DFLT(DEBUG, "adv_clk_accuracy : %d\n", event->periodic_sync.adv_clk_accuracy);
  64. }
  65. void print_periodic_adv_data(struct ble_gap_event *event)
  66. {
  67. MODLOG_DFLT(DEBUG, "sync_handle : %d\n", event->periodic_report.sync_handle);
  68. MODLOG_DFLT(DEBUG, "tx_power : %d\n", event->periodic_report.tx_power);
  69. MODLOG_DFLT(DEBUG, "rssi : %d\n", event->periodic_report.rssi);
  70. MODLOG_DFLT(DEBUG, "data_status : %d\n", event->periodic_report.data_status);
  71. MODLOG_DFLT(DEBUG, "data_length : %d\n", event->periodic_report.data_length);
  72. MODLOG_DFLT(DEBUG, "data : ");
  73. for (int i = 0; i < event->periodic_report.data_length; i++) {
  74. MODLOG_DFLT(DEBUG, "%c", ((char *)event->periodic_report.data)[i]);
  75. }
  76. MODLOG_DFLT(DEBUG, "\n");
  77. }
  78. void print_periodic_sync_lost_data(struct ble_gap_event *event)
  79. {
  80. MODLOG_DFLT(DEBUG, "sync_handle : %d\n", event->periodic_sync_lost.sync_handle);
  81. MODLOG_DFLT(DEBUG, "reason : %s\n", event->periodic_sync_lost.reason == 13 ? "timeout" : (event->periodic_sync_lost.reason == 14 ? "terminated locally" : "Unknown reason"));
  82. }
  83. /**
  84. * The nimble host executes this callback when a GAP event occurs. The
  85. * application associates a GAP event callback with each connection that is
  86. * established. periodic_sync uses the same callback for all connections.
  87. *
  88. * @param event The event being signalled.
  89. * @param arg Application-specified argument; unused by
  90. * periodic_sync.
  91. *
  92. * @return 0 if the application successfully handled the
  93. * event; nonzero on failure. The semantics
  94. * of the return code is specific to the
  95. * particular GAP event being signalled.
  96. */
  97. static int
  98. periodic_sync_gap_event(struct ble_gap_event *event, void *arg)
  99. {
  100. switch (event->type) {
  101. #if CONFIG_EXAMPLE_EXTENDED_ADV
  102. case BLE_GAP_EVENT_EXT_DISC:
  103. /* An advertisment report was received during GAP discovery. */
  104. struct ble_gap_ext_disc_desc *disc = ((struct ble_gap_ext_disc_desc *)(&event->disc));
  105. if (disc->sid == 2 && synced == 0) {
  106. synced++;
  107. const ble_addr_t addr;
  108. uint8_t adv_sid;
  109. struct ble_gap_periodic_sync_params params;
  110. int rc;
  111. memcpy((void *)&addr, (void *)&disc->addr, sizeof(disc->addr));
  112. memcpy(&adv_sid, &disc->sid, sizeof(disc->sid));
  113. params.skip = 10;
  114. params.sync_timeout = 1000;
  115. #if CONFIG_EXAMPLE_PERIODIC_ADV_ENH
  116. /* This way the periodic advertising reports will not be
  117. delivered to host unless the advertising data is changed
  118. or the Data-Id is updated by the advertiser */
  119. params.filter_duplicates = 1;
  120. #endif
  121. rc = ble_gap_periodic_adv_sync_create(&addr, adv_sid, &params, periodic_sync_gap_event, NULL);
  122. assert(rc == 0);
  123. }
  124. return 0;
  125. case BLE_GAP_EVENT_PERIODIC_REPORT:
  126. MODLOG_DFLT(INFO, "Periodic adv report event: \n");
  127. print_periodic_adv_data(event);
  128. return 0;
  129. case BLE_GAP_EVENT_PERIODIC_SYNC_LOST:
  130. MODLOG_DFLT(INFO, "Periodic sync lost\n");
  131. print_periodic_sync_lost_data(event);
  132. synced = 0;
  133. return 0;
  134. case BLE_GAP_EVENT_PERIODIC_SYNC:
  135. MODLOG_DFLT(INFO, "Periodic sync event : \n");
  136. print_periodic_sync_data(event);
  137. return 0;
  138. #endif
  139. default:
  140. return 0;
  141. }
  142. }
  143. static void
  144. periodic_sync_on_reset(int reason)
  145. {
  146. MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
  147. }
  148. static void
  149. periodic_sync_on_sync(void)
  150. {
  151. int rc;
  152. /* Make sure we have proper identity address set (public preferred) */
  153. rc = ble_hs_util_ensure_addr(0);
  154. assert(rc == 0);
  155. /* Begin scanning for a peripheral to connect to. */
  156. periodic_sync_scan();
  157. }
  158. void periodic_sync_host_task(void *param)
  159. {
  160. ESP_LOGI(tag, "BLE Host Task Started");
  161. /* This function will return only when nimble_port_stop() is executed */
  162. nimble_port_run();
  163. nimble_port_freertos_deinit();
  164. }
  165. void
  166. app_main(void)
  167. {
  168. int rc;
  169. /* Initialize NVS — it is used to store PHY calibration data */
  170. esp_err_t ret = nvs_flash_init();
  171. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  172. ESP_ERROR_CHECK(nvs_flash_erase());
  173. ret = nvs_flash_init();
  174. }
  175. ESP_ERROR_CHECK(ret);
  176. ret = nimble_port_init();
  177. if (ret != ESP_OK) {
  178. MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
  179. return;
  180. }
  181. /* Configure the host. */
  182. ble_hs_cfg.reset_cb = periodic_sync_on_reset;
  183. ble_hs_cfg.sync_cb = periodic_sync_on_sync;
  184. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  185. /* Initialize data structures to track connected peers. */
  186. rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
  187. assert(rc == 0);
  188. /* Set the default device name. */
  189. rc = ble_svc_gap_device_name_set("nimble_periodic_sync");
  190. assert(rc == 0);
  191. /* XXX Need to have template for store */
  192. ble_store_config_init();
  193. nimble_port_freertos_init(periodic_sync_host_task);
  194. }