main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 "phy_prph.h"
  16. static uint8_t ext_adv_pattern_1M[] = {
  17. 0x02, 0x01, 0x06,
  18. 0x03, 0x03, 0xab, 0xcd,
  19. 0x03, 0x03, 0xAB, 0xF2,
  20. 0x0e, 0X09, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', '1', 'M',
  21. };
  22. static uint8_t ext_adv_pattern_2M[] = {
  23. 0x02, 0x01, 0x06,
  24. 0x03, 0x03, 0xab, 0xcd,
  25. 0x03, 0x03, 0xAB, 0xF2,
  26. 0x0e, 0X09, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', '2', 'M',
  27. };
  28. static uint8_t ext_adv_pattern_coded[] = {
  29. 0x02, 0x01, 0x06,
  30. 0x03, 0x03, 0xab, 0xcd,
  31. 0x03, 0x03, 0xAB, 0xF2,
  32. 0x11, 0X09, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', 'c', 'o', 'd', 'e',
  33. 'd',
  34. };
  35. static const char *tag = "NimBLE_BLE_PHY_PRPH";
  36. static int bleprph_gap_event(struct ble_gap_event *event, void *arg);
  37. static uint8_t own_addr_type;
  38. static uint8_t s_current_phy;
  39. void ble_store_config_init(void);
  40. /* Set default LE PHY before establishing connection */
  41. void set_default_le_phy(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
  42. {
  43. int rc = ble_gap_set_prefered_default_le_phy(tx_phys_mask, rx_phys_mask);
  44. if (rc == 0) {
  45. MODLOG_DFLT(INFO, "Default LE PHY set successfully");
  46. } else {
  47. MODLOG_DFLT(ERROR, "Failed to set default LE PHY");
  48. }
  49. }
  50. /**
  51. * Logs information about a connection to the console.
  52. */
  53. static void
  54. bleprph_print_conn_desc(struct ble_gap_conn_desc *desc)
  55. {
  56. MODLOG_DFLT(INFO, "handle=%d our_ota_addr_type=%d our_ota_addr=",
  57. desc->conn_handle, desc->our_ota_addr.type);
  58. print_addr(desc->our_ota_addr.val);
  59. MODLOG_DFLT(INFO, " our_id_addr_type=%d our_id_addr=",
  60. desc->our_id_addr.type);
  61. print_addr(desc->our_id_addr.val);
  62. MODLOG_DFLT(INFO, " peer_ota_addr_type=%d peer_ota_addr=",
  63. desc->peer_ota_addr.type);
  64. print_addr(desc->peer_ota_addr.val);
  65. MODLOG_DFLT(INFO, " peer_id_addr_type=%d peer_id_addr=",
  66. desc->peer_id_addr.type);
  67. print_addr(desc->peer_id_addr.val);
  68. MODLOG_DFLT(INFO, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
  69. "encrypted=%d authenticated=%d bonded=%d\n",
  70. desc->conn_itvl, desc->conn_latency,
  71. desc->supervision_timeout,
  72. desc->sec_state.encrypted,
  73. desc->sec_state.authenticated,
  74. desc->sec_state.bonded);
  75. }
  76. static struct os_mbuf *
  77. ext_get_data(uint8_t ext_adv_pattern[], int size)
  78. {
  79. struct os_mbuf *data;
  80. int rc;
  81. data = os_msys_get_pkthdr(size, 0);
  82. assert(data);
  83. rc = os_mbuf_append(data, ext_adv_pattern, size);
  84. assert(rc == 0);
  85. return data;
  86. }
  87. /**
  88. * Enables advertising with the following parameters:
  89. * o General discoverable mode.
  90. * o Undirected connectable mode.
  91. */
  92. static void
  93. ext_bleprph_advertise(void)
  94. {
  95. struct ble_gap_ext_adv_params params;
  96. struct os_mbuf *data = NULL;
  97. uint8_t instance = 1;
  98. int rc;
  99. /* use defaults for non-set params */
  100. memset (&params, 0, sizeof(params));
  101. if (s_current_phy == BLE_HCI_LE_PHY_1M_PREF_MASK) {
  102. params.scannable = 1;
  103. params.legacy_pdu = 1;
  104. }
  105. /*enable connectable advertising for all Phy*/
  106. params.connectable = 1;
  107. /* advertise using random addr */
  108. params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
  109. /* Set current phy; get mbuf for scan rsp data; fill mbuf with scan rsp data */
  110. switch (s_current_phy) {
  111. case BLE_HCI_LE_PHY_1M_PREF_MASK:
  112. params.primary_phy = BLE_HCI_LE_PHY_1M;
  113. params.secondary_phy = BLE_HCI_LE_PHY_1M;
  114. data = ext_get_data(ext_adv_pattern_1M, sizeof(ext_adv_pattern_1M));
  115. params.sid = 0;
  116. break;
  117. case BLE_HCI_LE_PHY_2M_PREF_MASK:
  118. params.primary_phy = BLE_HCI_LE_PHY_1M;
  119. params.secondary_phy = BLE_HCI_LE_PHY_2M;
  120. data = ext_get_data(ext_adv_pattern_2M, sizeof(ext_adv_pattern_2M));
  121. params.sid = 1;
  122. break;
  123. case BLE_HCI_LE_PHY_CODED_PREF_MASK:
  124. params.primary_phy = BLE_HCI_LE_PHY_CODED;
  125. params.secondary_phy = BLE_HCI_LE_PHY_CODED;
  126. data = ext_get_data(ext_adv_pattern_coded, sizeof(ext_adv_pattern_coded));
  127. params.sid = 2;
  128. break;
  129. }
  130. params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
  131. params.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
  132. /* configure instance 0 */
  133. rc = ble_gap_ext_adv_configure(instance, &params, NULL,
  134. bleprph_gap_event, NULL);
  135. assert (rc == 0);
  136. rc = ble_gap_ext_adv_set_data(instance, data);
  137. assert (rc == 0);
  138. /* start advertising */
  139. rc = ble_gap_ext_adv_start(instance, 0, 0);
  140. assert (rc == 0);
  141. }
  142. /**
  143. * The nimble host executes this callback when a GAP event occurs. The
  144. * application associates a GAP event callback with each connection that forms.
  145. * bleprph uses the same callback for all connections.
  146. *
  147. * @param event The type of event being signalled.
  148. * @param ctxt Various information pertaining to the event.
  149. * @param arg Application-specified argument; unused by
  150. * bleprph.
  151. *
  152. * @return 0 if the application successfully handled the
  153. * event; nonzero on failure. The semantics
  154. * of the return code is specific to the
  155. * particular GAP event being signalled.
  156. */
  157. static int
  158. bleprph_gap_event(struct ble_gap_event *event, void *arg)
  159. {
  160. struct ble_gap_conn_desc desc;
  161. int rc;
  162. switch (event->type) {
  163. case BLE_GAP_EVENT_CONNECT:
  164. /* A new connection was established or a connection attempt failed. */
  165. MODLOG_DFLT(INFO, "connection %s; status=%d ",
  166. event->connect.status == 0 ? "established" : "failed",
  167. event->connect.status);
  168. if (event->connect.status == 0) {
  169. rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
  170. assert(rc == 0);
  171. bleprph_print_conn_desc(&desc);
  172. }
  173. MODLOG_DFLT(INFO, "\n");
  174. if (event->connect.status != 0) {
  175. /* Connection failed; resume advertising. */
  176. ext_bleprph_advertise();
  177. }
  178. return 0;
  179. case BLE_GAP_EVENT_DISCONNECT:
  180. MODLOG_DFLT(INFO, "disconnect; reason=%d ", event->disconnect.reason);
  181. bleprph_print_conn_desc(&event->disconnect.conn);
  182. MODLOG_DFLT(INFO, "\n");
  183. /* Connection terminated; resume advertising. */
  184. switch (s_current_phy) {
  185. case BLE_HCI_LE_PHY_1M_PREF_MASK:
  186. /* Setting current phy to create connection on 2M PHY */
  187. s_current_phy = BLE_HCI_LE_PHY_2M_PREF_MASK;
  188. break;
  189. case BLE_HCI_LE_PHY_2M_PREF_MASK:
  190. /* Setting current phy to create connection on CODED PHY */
  191. s_current_phy = BLE_HCI_LE_PHY_CODED_PREF_MASK;
  192. break;
  193. case BLE_HCI_LE_PHY_CODED_PREF_MASK:
  194. return 0;
  195. default:
  196. return 0;
  197. }
  198. ext_bleprph_advertise();
  199. return 0;
  200. case BLE_GAP_EVENT_CONN_UPDATE:
  201. /* The central has updated the connection parameters. */
  202. MODLOG_DFLT(INFO, "connection updated; status=%d ",
  203. event->conn_update.status);
  204. rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
  205. assert(rc == 0);
  206. bleprph_print_conn_desc(&desc);
  207. MODLOG_DFLT(INFO, "\n");
  208. return 0;
  209. case BLE_GAP_EVENT_ADV_COMPLETE:
  210. MODLOG_DFLT(INFO, "advertise complete; reason=%d",
  211. event->adv_complete.reason);
  212. return 0;
  213. }
  214. return 0;
  215. }
  216. static void
  217. bleprph_on_reset(int reason)
  218. {
  219. MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
  220. }
  221. static void
  222. bleprph_on_sync(void)
  223. {
  224. int rc;
  225. uint8_t all_phy;
  226. /* Make sure we have proper identity address set (public preferred) */
  227. rc = ble_hs_util_ensure_addr(0);
  228. assert(rc == 0);
  229. /* Figure out address to use while advertising (no privacy for now) */
  230. rc = ble_hs_id_infer_auto(0, &own_addr_type);
  231. if (rc != 0) {
  232. MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
  233. return;
  234. }
  235. /* Printing ADDR */
  236. uint8_t addr_val[6] = {0};
  237. rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
  238. MODLOG_DFLT(INFO, "Device Address: ");
  239. print_addr(addr_val);
  240. MODLOG_DFLT(INFO, "\n");
  241. s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK;
  242. all_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK | BLE_HCI_LE_PHY_CODED_PREF_MASK;
  243. set_default_le_phy(all_phy, all_phy);
  244. /* Begin advertising. */
  245. ext_bleprph_advertise();
  246. }
  247. void bleprph_host_task(void *param)
  248. {
  249. ESP_LOGI(tag, "BLE Host Task Started");
  250. /* This function will return only when nimble_port_stop() is executed */
  251. nimble_port_run();
  252. nimble_port_freertos_deinit();
  253. }
  254. void
  255. app_main(void)
  256. {
  257. int rc;
  258. /* Initialize NVS — it is used to store PHY calibration data */
  259. esp_err_t ret = nvs_flash_init();
  260. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  261. ESP_ERROR_CHECK(nvs_flash_erase());
  262. ret = nvs_flash_init();
  263. }
  264. ESP_ERROR_CHECK(ret);
  265. //ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
  266. ret = nimble_port_init();
  267. if (ret != ESP_OK) {
  268. MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
  269. return;
  270. }
  271. /* Initialize the NimBLE host configuration. */
  272. ble_hs_cfg.reset_cb = bleprph_on_reset;
  273. ble_hs_cfg.sync_cb = bleprph_on_sync;
  274. ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb;
  275. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  276. ble_hs_cfg.sm_io_cap = CONFIG_EXAMPLE_IO_TYPE;
  277. #ifdef CONFIG_EXAMPLE_BONDING
  278. ble_hs_cfg.sm_bonding = 1;
  279. #endif
  280. #ifdef CONFIG_EXAMPLE_MITM
  281. ble_hs_cfg.sm_mitm = 1;
  282. #endif
  283. #ifdef CONFIG_EXAMPLE_USE_SC
  284. ble_hs_cfg.sm_sc = 1;
  285. #else
  286. ble_hs_cfg.sm_sc = 0;
  287. #endif
  288. #ifdef CONFIG_EXAMPLE_BONDING
  289. ble_hs_cfg.sm_our_key_dist = 1;
  290. ble_hs_cfg.sm_their_key_dist = 1;
  291. #endif
  292. rc = gatt_svr_init_le_phy();
  293. assert(rc == 0);
  294. /* Set the default device name. */
  295. rc = ble_svc_gap_device_name_set("bleprph-phy");
  296. assert(rc == 0);
  297. /* XXX Need to have template for store */
  298. ble_store_config_init();
  299. nimble_port_freertos_init(bleprph_host_task);
  300. /* Initialize command line interface to accept input from user */
  301. rc = scli_init();
  302. if (rc != ESP_OK) {
  303. ESP_LOGE(tag, "scli_init() failed");
  304. }
  305. }