main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_log.h"
  7. #include "nvs_flash.h"
  8. #include "freertos/FreeRTOSConfig.h"
  9. /* BLE */
  10. #include "nimble/nimble_port.h"
  11. #include "nimble/nimble_port_freertos.h"
  12. #include "host/ble_hs.h"
  13. #include "host/util/util.h"
  14. #include "console/console.h"
  15. #include "services/gap/ble_svc_gap.h"
  16. #include "ble_cts_prph.h"
  17. #include "services/cts/ble_svc_cts.h"
  18. #if CONFIG_EXAMPLE_EXTENDED_ADV
  19. static uint8_t ext_adv_pattern_1[] = {
  20. 0x02, 0x01, 0x06,
  21. 0x03, 0x03, 0xab, 0xcd,
  22. 0x03, 0x03, 0x05, 0x18,
  23. 0x12, 0X09, 'n', 'i', 'm', 'b', 'l', 'e', '-', 'c', 't', 's', '-', 'p', 'r', 'p', 'h', '-', 'e',
  24. };
  25. #endif
  26. static const char *tag = "NimBLE_CTS_PRPH";
  27. static const char *device_name = "ble_cts_prph";
  28. static int ble_cts_prph_gap_event(struct ble_gap_event *event, void *arg);
  29. static uint8_t ble_cts_prph_addr_type;
  30. /**
  31. * Utility function to log an array of bytes.
  32. */
  33. void
  34. print_bytes(const uint8_t *bytes, int len)
  35. {
  36. int i;
  37. for (i = 0; i < len; i++) {
  38. MODLOG_DFLT(INFO, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
  39. }
  40. }
  41. void
  42. print_addr(const void *addr)
  43. {
  44. const uint8_t *u8p;
  45. u8p = addr;
  46. MODLOG_DFLT(INFO, "%02x:%02x:%02x:%02x:%02x:%02x",
  47. u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
  48. }
  49. #if CONFIG_EXAMPLE_EXTENDED_ADV
  50. /**
  51. * Enables advertising with the following parameters:
  52. * o General discoverable mode.
  53. * o Undirected connectable mode.
  54. */
  55. static void
  56. ext_ble_cts_prph_advertise(void)
  57. {
  58. struct ble_gap_ext_adv_params params;
  59. struct os_mbuf *data;
  60. uint8_t instance = 0;
  61. int rc;
  62. /* First check if any instance is already active */
  63. if (ble_gap_ext_adv_active(instance)) {
  64. return;
  65. }
  66. /* use defaults for non-set params */
  67. memset (&params, 0, sizeof(params));
  68. /* enable connectable advertising */
  69. params.connectable = 1;
  70. /* advertise using random addr */
  71. params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
  72. params.primary_phy = BLE_HCI_LE_PHY_1M;
  73. params.secondary_phy = BLE_HCI_LE_PHY_2M;
  74. params.sid = 1;
  75. params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
  76. params.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
  77. /* configure instance 0 */
  78. rc = ble_gap_ext_adv_configure(instance, &params, NULL,
  79. ble_cts_prph_gap_event, NULL);
  80. assert (rc == 0);
  81. /* in this case only scan response is allowed */
  82. /* get mbuf for scan rsp data */
  83. data = os_msys_get_pkthdr(sizeof(ext_adv_pattern_1), 0);
  84. assert(data);
  85. /* fill mbuf with scan rsp data */
  86. rc = os_mbuf_append(data, ext_adv_pattern_1, sizeof(ext_adv_pattern_1));
  87. assert(rc == 0);
  88. rc = ble_gap_ext_adv_set_data(instance, data);
  89. assert (rc == 0);
  90. /* start advertising */
  91. rc = ble_gap_ext_adv_start(instance, 0, 0);
  92. assert (rc == 0);
  93. }
  94. #else
  95. static void
  96. ble_cts_prph_advertise(void)
  97. {
  98. struct ble_gap_adv_params adv_params;
  99. struct ble_hs_adv_fields fields;
  100. int rc;
  101. /*
  102. * Set the advertisement data included in our advertisements:
  103. * o Flags (indicates advertisement type and other general info)
  104. * o Advertising tx power
  105. * o Device name
  106. */
  107. memset(&fields, 0, sizeof(fields));
  108. /*
  109. * Advertise two flags:
  110. * o Discoverability in forthcoming advertisement (general)
  111. * o BLE-only (BR/EDR unsupported)
  112. */
  113. fields.flags = BLE_HS_ADV_F_DISC_GEN |
  114. BLE_HS_ADV_F_BREDR_UNSUP;
  115. /*
  116. * Indicate that the TX power level field should be included; have the
  117. * stack fill this value automatically. This is done by assigning the
  118. * special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
  119. */
  120. fields.tx_pwr_lvl_is_present = 1;
  121. fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
  122. fields.name = (uint8_t *)device_name;
  123. fields.name_len = strlen(device_name);
  124. fields.name_is_complete = 1;
  125. fields.uuids16 = (ble_uuid16_t[]) {
  126. BLE_UUID16_INIT(BLE_SVC_CTS_UUID16)
  127. };
  128. fields.num_uuids16 = 1;
  129. fields.uuids16_is_complete = 1;
  130. rc = ble_gap_adv_set_fields(&fields);
  131. if (rc != 0) {
  132. MODLOG_DFLT(ERROR, "error setting advertisement data; rc=%d\n", rc);
  133. return;
  134. }
  135. /* Begin advertising */
  136. memset(&adv_params, 0, sizeof(adv_params));
  137. adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
  138. adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
  139. rc = ble_gap_adv_start(ble_cts_prph_addr_type, NULL, BLE_HS_FOREVER,
  140. &adv_params, ble_cts_prph_gap_event, NULL);
  141. if (rc != 0) {
  142. MODLOG_DFLT(ERROR, "error enabling advertisement; rc=%d\n", rc);
  143. return;
  144. }
  145. }
  146. #endif
  147. static int
  148. ble_cts_prph_gap_event(struct ble_gap_event *event, void *arg)
  149. {
  150. switch (event->type) {
  151. case BLE_GAP_EVENT_CONNECT:
  152. /* A new connection was established or a connection attempt failed */
  153. MODLOG_DFLT(INFO, "connection %s; status=%d\n",
  154. event->connect.status == 0 ? "established" : "failed",
  155. event->connect.status);
  156. if (event->connect.status != 0) {
  157. /* Connection failed; resume advertising */
  158. #if CONFIG_EXAMPLE_EXTENDED_ADV
  159. ext_ble_cts_prph_advertise();
  160. #else
  161. ble_cts_prph_advertise();
  162. #endif
  163. }
  164. break;
  165. case BLE_GAP_EVENT_DISCONNECT:
  166. MODLOG_DFLT(INFO, "disconnect; reason=%d\n", event->disconnect.reason);
  167. /* Connection terminated; resume advertising */
  168. #if CONFIG_EXAMPLE_EXTENDED_ADV
  169. ext_ble_cts_prph_advertise();
  170. #else
  171. ble_cts_prph_advertise();
  172. #endif
  173. break;
  174. case BLE_GAP_EVENT_ADV_COMPLETE:
  175. MODLOG_DFLT(INFO, "adv complete\n");
  176. #if CONFIG_EXAMPLE_EXTENDED_ADV
  177. ext_ble_cts_prph_advertise();
  178. #else
  179. ble_cts_prph_advertise();
  180. #endif
  181. break;
  182. case BLE_GAP_EVENT_SUBSCRIBE:
  183. MODLOG_DFLT(INFO, "subscribe event; cur_notify=%d\n value handle; "
  184. "val_handle=%d\n",
  185. event->subscribe.cur_notify, event->subscribe.attr_handle);
  186. break;
  187. case BLE_GAP_EVENT_MTU:
  188. MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d mtu=%d\n",
  189. event->mtu.conn_handle,
  190. event->mtu.value);
  191. break;
  192. }
  193. return 0;
  194. }
  195. static void
  196. ble_cts_prph_on_sync(void)
  197. {
  198. int rc;
  199. rc = ble_hs_id_infer_auto(0, &ble_cts_prph_addr_type);
  200. assert(rc == 0);
  201. uint8_t addr_val[6] = {0};
  202. rc = ble_hs_id_copy_addr(ble_cts_prph_addr_type, addr_val, NULL);
  203. MODLOG_DFLT(INFO, "Device Address: ");
  204. print_addr(addr_val);
  205. MODLOG_DFLT(INFO, "\n");
  206. /* Begin advertising */
  207. #if CONFIG_EXAMPLE_EXTENDED_ADV
  208. ext_ble_cts_prph_advertise();
  209. #else
  210. ble_cts_prph_advertise();
  211. #endif
  212. }
  213. static void
  214. ble_cts_prph_on_reset(int reason)
  215. {
  216. MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
  217. }
  218. void ble_cts_prph_host_task(void *param)
  219. {
  220. ESP_LOGI(tag, "BLE Host Task Started");
  221. /* This function will return only when nimble_port_stop() is executed */
  222. nimble_port_run();
  223. nimble_port_freertos_deinit();
  224. }
  225. void app_main(void)
  226. {
  227. int rc;
  228. /* Initialize NVS — it is used to store PHY calibration data */
  229. esp_err_t ret = nvs_flash_init();
  230. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  231. ESP_ERROR_CHECK(nvs_flash_erase());
  232. ret = nvs_flash_init();
  233. }
  234. ESP_ERROR_CHECK(ret);
  235. ret = nimble_port_init();
  236. if (ret != ESP_OK) {
  237. MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
  238. return;
  239. }
  240. /* Initialize the NimBLE host configuration */
  241. ble_hs_cfg.sync_cb = ble_cts_prph_on_sync;
  242. ble_hs_cfg.reset_cb = ble_cts_prph_on_reset;
  243. /* Enable bonding */
  244. ble_hs_cfg.sm_bonding = 1;
  245. ble_hs_cfg.sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
  246. ble_hs_cfg.sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
  247. ble_hs_cfg.sm_sc = 1;
  248. ble_hs_cfg.sm_mitm = 1;
  249. rc = gatt_svr_init();
  250. assert(rc == 0);
  251. /* Set the default device name */
  252. rc = ble_svc_gap_device_name_set(device_name);
  253. assert(rc == 0);
  254. /* Start the task */
  255. nimble_port_freertos_init(ble_cts_prph_host_task);
  256. }