main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include "esp_log.h"
  20. #include "nvs_flash.h"
  21. /* BLE */
  22. #include "nimble/nimble_port.h"
  23. #include "nimble/nimble_port_freertos.h"
  24. #include "host/ble_hs.h"
  25. #include "host/util/util.h"
  26. #include "console/console.h"
  27. #include "services/gap/ble_svc_gap.h"
  28. #include "blecent.h"
  29. static const char *tag = "NimBLE_BLE_CENT";
  30. static int blecent_gap_event(struct ble_gap_event *event, void *arg);
  31. static uint8_t peer_addr[6];
  32. void ble_store_config_init(void);
  33. /**
  34. * Application callback. Called when the attempt to subscribe to notifications
  35. * for the ANS Unread Alert Status characteristic has completed.
  36. */
  37. static int
  38. blecent_on_subscribe(uint16_t conn_handle,
  39. const struct ble_gatt_error *error,
  40. struct ble_gatt_attr *attr,
  41. void *arg)
  42. {
  43. MODLOG_DFLT(INFO, "Subscribe complete; status=%d conn_handle=%d "
  44. "attr_handle=%d\n",
  45. error->status, conn_handle, attr->handle);
  46. return 0;
  47. }
  48. /**
  49. * Application callback. Called when the write to the ANS Alert Notification
  50. * Control Point characteristic has completed.
  51. */
  52. static int
  53. blecent_on_write(uint16_t conn_handle,
  54. const struct ble_gatt_error *error,
  55. struct ble_gatt_attr *attr,
  56. void *arg)
  57. {
  58. MODLOG_DFLT(INFO,
  59. "Write complete; status=%d conn_handle=%d attr_handle=%d\n",
  60. error->status, conn_handle, attr->handle);
  61. /* Subscribe to notifications for the Unread Alert Status characteristic.
  62. * A central enables notifications by writing two bytes (1, 0) to the
  63. * characteristic's client-characteristic-configuration-descriptor (CCCD).
  64. */
  65. const struct peer_dsc *dsc;
  66. uint8_t value[2];
  67. int rc;
  68. const struct peer *peer = peer_find(conn_handle);
  69. dsc = peer_dsc_find_uuid(peer,
  70. BLE_UUID16_DECLARE(BLECENT_SVC_ALERT_UUID),
  71. BLE_UUID16_DECLARE(BLECENT_CHR_UNR_ALERT_STAT_UUID),
  72. BLE_UUID16_DECLARE(BLE_GATT_DSC_CLT_CFG_UUID16));
  73. if (dsc == NULL) {
  74. MODLOG_DFLT(ERROR, "Error: Peer lacks a CCCD for the Unread Alert "
  75. "Status characteristic\n");
  76. goto err;
  77. }
  78. value[0] = 1;
  79. value[1] = 0;
  80. rc = ble_gattc_write_flat(conn_handle, dsc->dsc.handle,
  81. value, sizeof value, blecent_on_subscribe, NULL);
  82. if (rc != 0) {
  83. MODLOG_DFLT(ERROR, "Error: Failed to subscribe to characteristic; "
  84. "rc=%d\n", rc);
  85. goto err;
  86. }
  87. return 0;
  88. err:
  89. /* Terminate the connection. */
  90. return ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
  91. }
  92. /**
  93. * Application callback. Called when the read of the ANS Supported New Alert
  94. * Category characteristic has completed.
  95. */
  96. static int
  97. blecent_on_read(uint16_t conn_handle,
  98. const struct ble_gatt_error *error,
  99. struct ble_gatt_attr *attr,
  100. void *arg)
  101. {
  102. MODLOG_DFLT(INFO, "Read complete; status=%d conn_handle=%d", error->status,
  103. conn_handle);
  104. if (error->status == 0) {
  105. MODLOG_DFLT(INFO, " attr_handle=%d value=", attr->handle);
  106. print_mbuf(attr->om);
  107. }
  108. MODLOG_DFLT(INFO, "\n");
  109. /* Write two bytes (99, 100) to the alert-notification-control-point
  110. * characteristic.
  111. */
  112. const struct peer_chr *chr;
  113. uint8_t value[2];
  114. int rc;
  115. const struct peer *peer = peer_find(conn_handle);
  116. chr = peer_chr_find_uuid(peer,
  117. BLE_UUID16_DECLARE(BLECENT_SVC_ALERT_UUID),
  118. BLE_UUID16_DECLARE(BLECENT_CHR_ALERT_NOT_CTRL_PT));
  119. if (chr == NULL) {
  120. MODLOG_DFLT(ERROR, "Error: Peer doesn't support the Alert "
  121. "Notification Control Point characteristic\n");
  122. goto err;
  123. }
  124. value[0] = 99;
  125. value[1] = 100;
  126. rc = ble_gattc_write_flat(conn_handle, chr->chr.val_handle,
  127. value, sizeof value, blecent_on_write, NULL);
  128. if (rc != 0) {
  129. MODLOG_DFLT(ERROR, "Error: Failed to write characteristic; rc=%d\n",
  130. rc);
  131. goto err;
  132. }
  133. return 0;
  134. err:
  135. /* Terminate the connection. */
  136. return ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
  137. }
  138. /**
  139. * Performs three GATT operations against the specified peer:
  140. * 1. Reads the ANS Supported New Alert Category characteristic.
  141. * 2. After read is completed, writes the ANS Alert Notification Control Point characteristic.
  142. * 3. After write is completed, subscribes to notifications for the ANS Unread Alert Status
  143. * characteristic.
  144. *
  145. * If the peer does not support a required service, characteristic, or
  146. * descriptor, then the peer lied when it claimed support for the alert
  147. * notification service! When this happens, or if a GATT procedure fails,
  148. * this function immediately terminates the connection.
  149. */
  150. static void
  151. blecent_read_write_subscribe(const struct peer *peer)
  152. {
  153. const struct peer_chr *chr;
  154. int rc;
  155. /* Read the supported-new-alert-category characteristic. */
  156. chr = peer_chr_find_uuid(peer,
  157. BLE_UUID16_DECLARE(BLECENT_SVC_ALERT_UUID),
  158. BLE_UUID16_DECLARE(BLECENT_CHR_SUP_NEW_ALERT_CAT_UUID));
  159. if (chr == NULL) {
  160. MODLOG_DFLT(ERROR, "Error: Peer doesn't support the Supported New "
  161. "Alert Category characteristic\n");
  162. goto err;
  163. }
  164. rc = ble_gattc_read(peer->conn_handle, chr->chr.val_handle,
  165. blecent_on_read, NULL);
  166. if (rc != 0) {
  167. MODLOG_DFLT(ERROR, "Error: Failed to read characteristic; rc=%d\n",
  168. rc);
  169. goto err;
  170. }
  171. return;
  172. err:
  173. /* Terminate the connection. */
  174. ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
  175. }
  176. /**
  177. * Called when service discovery of the specified peer has completed.
  178. */
  179. static void
  180. blecent_on_disc_complete(const struct peer *peer, int status, void *arg)
  181. {
  182. if (status != 0) {
  183. /* Service discovery failed. Terminate the connection. */
  184. MODLOG_DFLT(ERROR, "Error: Service discovery failed; status=%d "
  185. "conn_handle=%d\n", status, peer->conn_handle);
  186. ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
  187. return;
  188. }
  189. /* Service discovery has completed successfully. Now we have a complete
  190. * list of services, characteristics, and descriptors that the peer
  191. * supports.
  192. */
  193. MODLOG_DFLT(INFO, "Service discovery complete; status=%d "
  194. "conn_handle=%d\n", status, peer->conn_handle);
  195. /* Now perform three GATT procedures against the peer: read,
  196. * write, and subscribe to notifications.
  197. */
  198. blecent_read_write_subscribe(peer);
  199. }
  200. /**
  201. * Initiates the GAP general discovery procedure.
  202. */
  203. static void
  204. blecent_scan(void)
  205. {
  206. uint8_t own_addr_type;
  207. struct ble_gap_disc_params disc_params;
  208. int rc;
  209. /* Figure out address to use while advertising (no privacy for now) */
  210. rc = ble_hs_id_infer_auto(0, &own_addr_type);
  211. if (rc != 0) {
  212. MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
  213. return;
  214. }
  215. /* Tell the controller to filter duplicates; we don't want to process
  216. * repeated advertisements from the same device.
  217. */
  218. disc_params.filter_duplicates = 1;
  219. /**
  220. * Perform a passive scan. I.e., don't send follow-up scan requests to
  221. * each advertiser.
  222. */
  223. disc_params.passive = 1;
  224. /* Use defaults for the rest of the parameters. */
  225. disc_params.itvl = 0;
  226. disc_params.window = 0;
  227. disc_params.filter_policy = 0;
  228. disc_params.limited = 0;
  229. rc = ble_gap_disc(own_addr_type, BLE_HS_FOREVER, &disc_params,
  230. blecent_gap_event, NULL);
  231. if (rc != 0) {
  232. MODLOG_DFLT(ERROR, "Error initiating GAP discovery procedure; rc=%d\n",
  233. rc);
  234. }
  235. }
  236. /**
  237. * Indicates whether we should try to connect to the sender of the specified
  238. * advertisement. The function returns a positive result if the device
  239. * advertises connectability and support for the Alert Notification service.
  240. */
  241. #if CONFIG_EXAMPLE_EXTENDED_ADV
  242. static int
  243. ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
  244. {
  245. int offset = 0;
  246. int ad_struct_len = 0;
  247. if (disc->legacy_event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
  248. disc->legacy_event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
  249. return 0;
  250. }
  251. if (strlen(CONFIG_EXAMPLE_PEER_ADDR) && (strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen ("ADDR_ANY")) != 0)) {
  252. ESP_LOGI(tag, "Peer address from menuconfig: %s", CONFIG_EXAMPLE_PEER_ADDR);
  253. /* Convert string to address */
  254. sscanf(CONFIG_EXAMPLE_PEER_ADDR, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
  255. &peer_addr[5], &peer_addr[4], &peer_addr[3],
  256. &peer_addr[2], &peer_addr[1], &peer_addr[0]);
  257. if (memcmp(peer_addr, disc->addr.val, sizeof(disc->addr.val)) != 0) {
  258. return 0;
  259. }
  260. }
  261. /* The device has to advertise support for the Alert Notification
  262. * service (0x1811).
  263. */
  264. do {
  265. ad_struct_len = disc->data[offset];
  266. if (!ad_struct_len) {
  267. break;
  268. }
  269. /* Search if ANS UUID is advertised */
  270. if (disc->data[offset] == 0x03 && disc->data[offset + 1] == 0x03) {
  271. if ( disc->data[offset + 2] == 0x18 && disc->data[offset + 3] == 0x11 ) {
  272. return 1;
  273. }
  274. }
  275. offset += ad_struct_len + 1;
  276. } while ( offset < disc->length_data );
  277. return 0;
  278. }
  279. #else
  280. static int
  281. blecent_should_connect(const struct ble_gap_disc_desc *disc)
  282. {
  283. struct ble_hs_adv_fields fields;
  284. int rc;
  285. int i;
  286. /* The device has to be advertising connectability. */
  287. if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
  288. disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
  289. return 0;
  290. }
  291. rc = ble_hs_adv_parse_fields(&fields, disc->data, disc->length_data);
  292. if (rc != 0) {
  293. return rc;
  294. }
  295. if (strlen(CONFIG_EXAMPLE_PEER_ADDR) && (strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
  296. ESP_LOGI(tag, "Peer address from menuconfig: %s", CONFIG_EXAMPLE_PEER_ADDR);
  297. /* Convert string to address */
  298. sscanf(CONFIG_EXAMPLE_PEER_ADDR, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
  299. &peer_addr[5], &peer_addr[4], &peer_addr[3],
  300. &peer_addr[2], &peer_addr[1], &peer_addr[0]);
  301. if (memcmp(peer_addr, disc->addr.val, sizeof(disc->addr.val)) != 0) {
  302. return 0;
  303. }
  304. }
  305. /* The device has to advertise support for the Alert Notification
  306. * service (0x1811).
  307. */
  308. for (i = 0; i < fields.num_uuids16; i++) {
  309. if (ble_uuid_u16(&fields.uuids16[i].u) == BLECENT_SVC_ALERT_UUID) {
  310. return 1;
  311. }
  312. }
  313. return 0;
  314. }
  315. #endif
  316. /**
  317. * Connects to the sender of the specified advertisement of it looks
  318. * interesting. A device is "interesting" if it advertises connectability and
  319. * support for the Alert Notification service.
  320. */
  321. static void
  322. blecent_connect_if_interesting(void *disc)
  323. {
  324. uint8_t own_addr_type;
  325. int rc;
  326. ble_addr_t *addr;
  327. /* Don't do anything if we don't care about this advertiser. */
  328. #if CONFIG_EXAMPLE_EXTENDED_ADV
  329. if (!ext_blecent_should_connect((struct ble_gap_ext_disc_desc *)disc)) {
  330. return;
  331. }
  332. #else
  333. if (!blecent_should_connect((struct ble_gap_disc_desc *)disc)) {
  334. return;
  335. }
  336. #endif
  337. /* Scanning must be stopped before a connection can be initiated. */
  338. rc = ble_gap_disc_cancel();
  339. if (rc != 0) {
  340. MODLOG_DFLT(DEBUG, "Failed to cancel scan; rc=%d\n", rc);
  341. return;
  342. }
  343. /* Figure out address to use for connect (no privacy for now) */
  344. rc = ble_hs_id_infer_auto(0, &own_addr_type);
  345. if (rc != 0) {
  346. MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
  347. return;
  348. }
  349. /* Try to connect the the advertiser. Allow 30 seconds (30000 ms) for
  350. * timeout.
  351. */
  352. #if CONFIG_EXAMPLE_EXTENDED_ADV
  353. addr = &((struct ble_gap_ext_disc_desc *)disc)->addr;
  354. #else
  355. addr = &((struct ble_gap_disc_desc *)disc)->addr;
  356. #endif
  357. rc = ble_gap_connect(own_addr_type, addr, 30000, NULL,
  358. blecent_gap_event, NULL);
  359. if (rc != 0) {
  360. MODLOG_DFLT(ERROR, "Error: Failed to connect to device; addr_type=%d "
  361. "addr=%s; rc=%d\n",
  362. addr->type, addr_str(addr->val), rc);
  363. return;
  364. }
  365. }
  366. /**
  367. * The nimble host executes this callback when a GAP event occurs. The
  368. * application associates a GAP event callback with each connection that is
  369. * established. blecent uses the same callback for all connections.
  370. *
  371. * @param event The event being signalled.
  372. * @param arg Application-specified argument; unused by
  373. * blecent.
  374. *
  375. * @return 0 if the application successfully handled the
  376. * event; nonzero on failure. The semantics
  377. * of the return code is specific to the
  378. * particular GAP event being signalled.
  379. */
  380. static int
  381. blecent_gap_event(struct ble_gap_event *event, void *arg)
  382. {
  383. struct ble_gap_conn_desc desc;
  384. struct ble_hs_adv_fields fields;
  385. int rc;
  386. switch (event->type) {
  387. case BLE_GAP_EVENT_DISC:
  388. rc = ble_hs_adv_parse_fields(&fields, event->disc.data,
  389. event->disc.length_data);
  390. if (rc != 0) {
  391. return 0;
  392. }
  393. /* An advertisment report was received during GAP discovery. */
  394. print_adv_fields(&fields);
  395. /* Try to connect to the advertiser if it looks interesting. */
  396. blecent_connect_if_interesting(&event->disc);
  397. return 0;
  398. case BLE_GAP_EVENT_CONNECT:
  399. /* A new connection was established or a connection attempt failed. */
  400. if (event->connect.status == 0) {
  401. /* Connection successfully established. */
  402. MODLOG_DFLT(INFO, "Connection established ");
  403. rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
  404. assert(rc == 0);
  405. print_conn_desc(&desc);
  406. MODLOG_DFLT(INFO, "\n");
  407. /* Remember peer. */
  408. rc = peer_add(event->connect.conn_handle);
  409. if (rc != 0) {
  410. MODLOG_DFLT(ERROR, "Failed to add peer; rc=%d\n", rc);
  411. return 0;
  412. }
  413. /* Perform service discovery. */
  414. rc = peer_disc_all(event->connect.conn_handle,
  415. blecent_on_disc_complete, NULL);
  416. if (rc != 0) {
  417. MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc);
  418. return 0;
  419. }
  420. } else {
  421. /* Connection attempt failed; resume scanning. */
  422. MODLOG_DFLT(ERROR, "Error: Connection failed; status=%d\n",
  423. event->connect.status);
  424. blecent_scan();
  425. }
  426. return 0;
  427. case BLE_GAP_EVENT_DISCONNECT:
  428. /* Connection terminated. */
  429. MODLOG_DFLT(INFO, "disconnect; reason=%d ", event->disconnect.reason);
  430. print_conn_desc(&event->disconnect.conn);
  431. MODLOG_DFLT(INFO, "\n");
  432. /* Forget about peer. */
  433. peer_delete(event->disconnect.conn.conn_handle);
  434. /* Resume scanning. */
  435. blecent_scan();
  436. return 0;
  437. case BLE_GAP_EVENT_DISC_COMPLETE:
  438. MODLOG_DFLT(INFO, "discovery complete; reason=%d\n",
  439. event->disc_complete.reason);
  440. return 0;
  441. case BLE_GAP_EVENT_ENC_CHANGE:
  442. /* Encryption has been enabled or disabled for this connection. */
  443. MODLOG_DFLT(INFO, "encryption change event; status=%d ",
  444. event->enc_change.status);
  445. rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc);
  446. assert(rc == 0);
  447. print_conn_desc(&desc);
  448. return 0;
  449. case BLE_GAP_EVENT_NOTIFY_RX:
  450. /* Peer sent us a notification or indication. */
  451. MODLOG_DFLT(INFO, "received %s; conn_handle=%d attr_handle=%d "
  452. "attr_len=%d\n",
  453. event->notify_rx.indication ?
  454. "indication" :
  455. "notification",
  456. event->notify_rx.conn_handle,
  457. event->notify_rx.attr_handle,
  458. OS_MBUF_PKTLEN(event->notify_rx.om));
  459. /* Attribute data is contained in event->notify_rx.om. Use
  460. * `os_mbuf_copydata` to copy the data received in notification mbuf */
  461. return 0;
  462. case BLE_GAP_EVENT_MTU:
  463. MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d cid=%d mtu=%d\n",
  464. event->mtu.conn_handle,
  465. event->mtu.channel_id,
  466. event->mtu.value);
  467. return 0;
  468. case BLE_GAP_EVENT_REPEAT_PAIRING:
  469. /* We already have a bond with the peer, but it is attempting to
  470. * establish a new secure link. This app sacrifices security for
  471. * convenience: just throw away the old bond and accept the new link.
  472. */
  473. /* Delete the old bond. */
  474. rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
  475. assert(rc == 0);
  476. ble_store_util_delete_peer(&desc.peer_id_addr);
  477. /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should
  478. * continue with the pairing operation.
  479. */
  480. return BLE_GAP_REPEAT_PAIRING_RETRY;
  481. #if CONFIG_EXAMPLE_EXTENDED_ADV
  482. case BLE_GAP_EVENT_EXT_DISC:
  483. /* An advertisment report was received during GAP discovery. */
  484. ext_print_adv_report(&event->disc);
  485. blecent_connect_if_interesting(&event->disc);
  486. return 0;
  487. #endif
  488. default:
  489. return 0;
  490. }
  491. }
  492. static void
  493. blecent_on_reset(int reason)
  494. {
  495. MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
  496. }
  497. static void
  498. blecent_on_sync(void)
  499. {
  500. int rc;
  501. /* Make sure we have proper identity address set (public preferred) */
  502. rc = ble_hs_util_ensure_addr(0);
  503. assert(rc == 0);
  504. /* Begin scanning for a peripheral to connect to. */
  505. blecent_scan();
  506. }
  507. void blecent_host_task(void *param)
  508. {
  509. ESP_LOGI(tag, "BLE Host Task Started");
  510. /* This function will return only when nimble_port_stop() is executed */
  511. nimble_port_run();
  512. nimble_port_freertos_deinit();
  513. }
  514. void
  515. app_main(void)
  516. {
  517. int rc;
  518. /* Initialize NVS — it is used to store PHY calibration data */
  519. esp_err_t ret = nvs_flash_init();
  520. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  521. ESP_ERROR_CHECK(nvs_flash_erase());
  522. ret = nvs_flash_init();
  523. }
  524. ESP_ERROR_CHECK(ret);
  525. nimble_port_init();
  526. /* Configure the host. */
  527. ble_hs_cfg.reset_cb = blecent_on_reset;
  528. ble_hs_cfg.sync_cb = blecent_on_sync;
  529. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  530. /* Initialize data structures to track connected peers. */
  531. rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
  532. assert(rc == 0);
  533. /* Set the default device name. */
  534. rc = ble_svc_gap_device_name_set("nimble-blecent");
  535. assert(rc == 0);
  536. /* XXX Need to have template for store */
  537. ble_store_config_init();
  538. nimble_port_freertos_init(blecent_host_task);
  539. }