peer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include "host/ble_hs.h"
  9. #include "esp_central.h"
  10. static void *peer_svc_mem;
  11. static struct os_mempool peer_svc_pool;
  12. static void *peer_chr_mem;
  13. static struct os_mempool peer_chr_pool;
  14. static void *peer_dsc_mem;
  15. static struct os_mempool peer_dsc_pool;
  16. static void *peer_mem;
  17. static struct os_mempool peer_pool;
  18. static SLIST_HEAD(, peer) peers;
  19. static struct peer_svc *
  20. peer_svc_find_range(struct peer *peer, uint16_t attr_handle);
  21. static struct peer_svc *
  22. peer_svc_find(struct peer *peer, uint16_t svc_start_handle,
  23. struct peer_svc **out_prev);
  24. int
  25. peer_svc_is_empty(const struct peer_svc *svc);
  26. uint16_t
  27. chr_end_handle(const struct peer_svc *svc, const struct peer_chr *chr);
  28. int
  29. chr_is_empty(const struct peer_svc *svc, const struct peer_chr *chr);
  30. static struct peer_chr *
  31. peer_chr_find(const struct peer_svc *svc, uint16_t chr_def_handle,
  32. struct peer_chr **out_prev);
  33. static void
  34. peer_disc_chrs(struct peer *peer);
  35. static int
  36. peer_dsc_disced(uint16_t conn_handle, const struct ble_gatt_error *error,
  37. uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc,
  38. void *arg);
  39. struct peer *
  40. peer_find(uint16_t conn_handle)
  41. {
  42. struct peer *peer;
  43. SLIST_FOREACH(peer, &peers, next) {
  44. if (peer->conn_handle == conn_handle) {
  45. return peer;
  46. }
  47. }
  48. return NULL;
  49. }
  50. static void
  51. peer_disc_complete(struct peer *peer, int rc)
  52. {
  53. peer->disc_prev_chr_val = 0;
  54. /* Notify caller that discovery has completed. */
  55. if (peer->disc_cb != NULL) {
  56. peer->disc_cb(peer, rc, peer->disc_cb_arg);
  57. }
  58. }
  59. static struct peer_dsc *
  60. peer_dsc_find_prev(const struct peer_chr *chr, uint16_t dsc_handle)
  61. {
  62. struct peer_dsc *prev;
  63. struct peer_dsc *dsc;
  64. prev = NULL;
  65. SLIST_FOREACH(dsc, &chr->dscs, next) {
  66. if (dsc->dsc.handle >= dsc_handle) {
  67. break;
  68. }
  69. prev = dsc;
  70. }
  71. return prev;
  72. }
  73. static struct peer_dsc *
  74. peer_dsc_find(const struct peer_chr *chr, uint16_t dsc_handle,
  75. struct peer_dsc **out_prev)
  76. {
  77. struct peer_dsc *prev;
  78. struct peer_dsc *dsc;
  79. prev = peer_dsc_find_prev(chr, dsc_handle);
  80. if (prev == NULL) {
  81. dsc = SLIST_FIRST(&chr->dscs);
  82. } else {
  83. dsc = SLIST_NEXT(prev, next);
  84. }
  85. if (dsc != NULL && dsc->dsc.handle != dsc_handle) {
  86. dsc = NULL;
  87. }
  88. if (out_prev != NULL) {
  89. *out_prev = prev;
  90. }
  91. return dsc;
  92. }
  93. static int
  94. peer_dsc_add(struct peer *peer, uint16_t chr_val_handle,
  95. const struct ble_gatt_dsc *gatt_dsc)
  96. {
  97. struct peer_dsc *prev;
  98. struct peer_dsc *dsc;
  99. struct peer_svc *svc;
  100. struct peer_chr *chr;
  101. svc = peer_svc_find_range(peer, chr_val_handle);
  102. if (svc == NULL) {
  103. /* Can't find service for discovered descriptor; this shouldn't
  104. * happen.
  105. */
  106. assert(0);
  107. return BLE_HS_EUNKNOWN;
  108. }
  109. chr = peer_chr_find(svc, chr_val_handle, NULL);
  110. if (chr == NULL) {
  111. /* Can't find characteristic for discovered descriptor; this shouldn't
  112. * happen.
  113. */
  114. assert(0);
  115. return BLE_HS_EUNKNOWN;
  116. }
  117. dsc = peer_dsc_find(chr, gatt_dsc->handle, &prev);
  118. if (dsc != NULL) {
  119. /* Descriptor already discovered. */
  120. return 0;
  121. }
  122. dsc = os_memblock_get(&peer_dsc_pool);
  123. if (dsc == NULL) {
  124. /* Out of memory. */
  125. return BLE_HS_ENOMEM;
  126. }
  127. memset(dsc, 0, sizeof * dsc);
  128. dsc->dsc = *gatt_dsc;
  129. if (prev == NULL) {
  130. SLIST_INSERT_HEAD(&chr->dscs, dsc, next);
  131. } else {
  132. SLIST_NEXT(prev, next) = dsc;
  133. }
  134. return 0;
  135. }
  136. static void
  137. peer_disc_dscs(struct peer *peer)
  138. {
  139. struct peer_chr *chr;
  140. struct peer_svc *svc;
  141. int rc;
  142. /* Search through the list of discovered characteristics for the first
  143. * characteristic that contains undiscovered descriptors. Then, discover
  144. * all descriptors belonging to that characteristic.
  145. */
  146. SLIST_FOREACH(svc, &peer->svcs, next) {
  147. SLIST_FOREACH(chr, &svc->chrs, next) {
  148. if (!chr_is_empty(svc, chr) &&
  149. SLIST_EMPTY(&chr->dscs) &&
  150. peer->disc_prev_chr_val <= chr->chr.def_handle) {
  151. rc = ble_gattc_disc_all_dscs(peer->conn_handle,
  152. chr->chr.val_handle,
  153. chr_end_handle(svc, chr),
  154. peer_dsc_disced, peer);
  155. if (rc != 0) {
  156. peer_disc_complete(peer, rc);
  157. }
  158. peer->disc_prev_chr_val = chr->chr.val_handle;
  159. return;
  160. }
  161. }
  162. }
  163. /* All descriptors discovered. */
  164. peer_disc_complete(peer, 0);
  165. }
  166. static int
  167. peer_dsc_disced(uint16_t conn_handle, const struct ble_gatt_error *error,
  168. uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc,
  169. void *arg)
  170. {
  171. struct peer *peer;
  172. int rc;
  173. peer = arg;
  174. assert(peer->conn_handle == conn_handle);
  175. switch (error->status) {
  176. case 0:
  177. rc = peer_dsc_add(peer, chr_val_handle, dsc);
  178. break;
  179. case BLE_HS_EDONE:
  180. /* All descriptors in this characteristic discovered; start discovering
  181. * descriptors in the next characteristic.
  182. */
  183. if (peer->disc_prev_chr_val > 0) {
  184. peer_disc_dscs(peer);
  185. }
  186. rc = 0;
  187. break;
  188. default:
  189. /* Error; abort discovery. */
  190. rc = error->status;
  191. break;
  192. }
  193. if (rc != 0) {
  194. /* Error; abort discovery. */
  195. peer_disc_complete(peer, rc);
  196. }
  197. return rc;
  198. }
  199. uint16_t
  200. chr_end_handle(const struct peer_svc *svc, const struct peer_chr *chr)
  201. {
  202. const struct peer_chr *next_chr;
  203. next_chr = SLIST_NEXT(chr, next);
  204. if (next_chr != NULL) {
  205. return next_chr->chr.def_handle - 1;
  206. } else {
  207. return svc->svc.end_handle;
  208. }
  209. }
  210. int
  211. chr_is_empty(const struct peer_svc *svc, const struct peer_chr *chr)
  212. {
  213. return chr_end_handle(svc, chr) <= chr->chr.val_handle;
  214. }
  215. static struct peer_chr *
  216. peer_chr_find_prev(const struct peer_svc *svc, uint16_t chr_val_handle)
  217. {
  218. struct peer_chr *prev;
  219. struct peer_chr *chr;
  220. prev = NULL;
  221. SLIST_FOREACH(chr, &svc->chrs, next) {
  222. if (chr->chr.val_handle >= chr_val_handle) {
  223. break;
  224. }
  225. prev = chr;
  226. }
  227. return prev;
  228. }
  229. static struct peer_chr *
  230. peer_chr_find(const struct peer_svc *svc, uint16_t chr_val_handle,
  231. struct peer_chr **out_prev)
  232. {
  233. struct peer_chr *prev;
  234. struct peer_chr *chr;
  235. prev = peer_chr_find_prev(svc, chr_val_handle);
  236. if (prev == NULL) {
  237. chr = SLIST_FIRST(&svc->chrs);
  238. } else {
  239. chr = SLIST_NEXT(prev, next);
  240. }
  241. if (chr != NULL && chr->chr.val_handle != chr_val_handle) {
  242. chr = NULL;
  243. }
  244. if (out_prev != NULL) {
  245. *out_prev = prev;
  246. }
  247. return chr;
  248. }
  249. static void
  250. peer_chr_delete(struct peer_chr *chr)
  251. {
  252. struct peer_dsc *dsc;
  253. while ((dsc = SLIST_FIRST(&chr->dscs)) != NULL) {
  254. SLIST_REMOVE_HEAD(&chr->dscs, next);
  255. os_memblock_put(&peer_dsc_pool, dsc);
  256. }
  257. os_memblock_put(&peer_chr_pool, chr);
  258. }
  259. static int
  260. peer_chr_add(struct peer *peer, uint16_t svc_start_handle,
  261. const struct ble_gatt_chr *gatt_chr)
  262. {
  263. struct peer_chr *prev;
  264. struct peer_chr *chr;
  265. struct peer_svc *svc;
  266. svc = peer_svc_find(peer, svc_start_handle, NULL);
  267. if (svc == NULL) {
  268. /* Can't find service for discovered characteristic; this shouldn't
  269. * happen.
  270. */
  271. assert(0);
  272. return BLE_HS_EUNKNOWN;
  273. }
  274. chr = peer_chr_find(svc, gatt_chr->def_handle, &prev);
  275. if (chr != NULL) {
  276. /* Characteristic already discovered. */
  277. return 0;
  278. }
  279. chr = os_memblock_get(&peer_chr_pool);
  280. if (chr == NULL) {
  281. /* Out of memory. */
  282. return BLE_HS_ENOMEM;
  283. }
  284. memset(chr, 0, sizeof * chr);
  285. chr->chr = *gatt_chr;
  286. if (prev == NULL) {
  287. SLIST_INSERT_HEAD(&svc->chrs, chr, next);
  288. } else {
  289. SLIST_NEXT(prev, next) = chr;
  290. }
  291. return 0;
  292. }
  293. static int
  294. peer_chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error,
  295. const struct ble_gatt_chr *chr, void *arg)
  296. {
  297. struct peer *peer;
  298. int rc;
  299. peer = arg;
  300. assert(peer->conn_handle == conn_handle);
  301. switch (error->status) {
  302. case 0:
  303. rc = peer_chr_add(peer, peer->cur_svc->svc.start_handle, chr);
  304. break;
  305. case BLE_HS_EDONE:
  306. /* All characteristics in this service discovered; start discovering
  307. * characteristics in the next service.
  308. */
  309. if (peer->disc_prev_chr_val > 0) {
  310. peer_disc_chrs(peer);
  311. }
  312. rc = 0;
  313. break;
  314. default:
  315. rc = error->status;
  316. break;
  317. }
  318. if (rc != 0) {
  319. /* Error; abort discovery. */
  320. peer_disc_complete(peer, rc);
  321. }
  322. return rc;
  323. }
  324. static void
  325. peer_disc_chrs(struct peer *peer)
  326. {
  327. struct peer_svc *svc;
  328. int rc;
  329. /* Search through the list of discovered service for the first service that
  330. * contains undiscovered characteristics. Then, discover all
  331. * characteristics belonging to that service.
  332. */
  333. SLIST_FOREACH(svc, &peer->svcs, next) {
  334. if (!peer_svc_is_empty(svc) && SLIST_EMPTY(&svc->chrs)) {
  335. peer->cur_svc = svc;
  336. rc = ble_gattc_disc_all_chrs(peer->conn_handle,
  337. svc->svc.start_handle,
  338. svc->svc.end_handle,
  339. peer_chr_disced, peer);
  340. if (rc != 0) {
  341. peer_disc_complete(peer, rc);
  342. }
  343. return;
  344. }
  345. }
  346. /* All characteristics discovered. */
  347. peer_disc_dscs(peer);
  348. }
  349. int
  350. peer_svc_is_empty(const struct peer_svc *svc)
  351. {
  352. return svc->svc.end_handle <= svc->svc.start_handle;
  353. }
  354. static struct peer_svc *
  355. peer_svc_find_prev(struct peer *peer, uint16_t svc_start_handle)
  356. {
  357. struct peer_svc *prev;
  358. struct peer_svc *svc;
  359. prev = NULL;
  360. SLIST_FOREACH(svc, &peer->svcs, next) {
  361. if (svc->svc.start_handle >= svc_start_handle) {
  362. break;
  363. }
  364. prev = svc;
  365. }
  366. return prev;
  367. }
  368. static struct peer_svc *
  369. peer_svc_find(struct peer *peer, uint16_t svc_start_handle,
  370. struct peer_svc **out_prev)
  371. {
  372. struct peer_svc *prev;
  373. struct peer_svc *svc;
  374. prev = peer_svc_find_prev(peer, svc_start_handle);
  375. if (prev == NULL) {
  376. svc = SLIST_FIRST(&peer->svcs);
  377. } else {
  378. svc = SLIST_NEXT(prev, next);
  379. }
  380. if (svc != NULL && svc->svc.start_handle != svc_start_handle) {
  381. svc = NULL;
  382. }
  383. if (out_prev != NULL) {
  384. *out_prev = prev;
  385. }
  386. return svc;
  387. }
  388. static struct peer_svc *
  389. peer_svc_find_range(struct peer *peer, uint16_t attr_handle)
  390. {
  391. struct peer_svc *svc;
  392. SLIST_FOREACH(svc, &peer->svcs, next) {
  393. if (svc->svc.start_handle <= attr_handle &&
  394. svc->svc.end_handle >= attr_handle) {
  395. return svc;
  396. }
  397. }
  398. return NULL;
  399. }
  400. const struct peer_svc *
  401. peer_svc_find_uuid(const struct peer *peer, const ble_uuid_t *uuid)
  402. {
  403. const struct peer_svc *svc;
  404. SLIST_FOREACH(svc, &peer->svcs, next) {
  405. if (ble_uuid_cmp(&svc->svc.uuid.u, uuid) == 0) {
  406. return svc;
  407. }
  408. }
  409. return NULL;
  410. }
  411. const struct peer_chr *
  412. peer_chr_find_uuid(const struct peer *peer, const ble_uuid_t *svc_uuid,
  413. const ble_uuid_t *chr_uuid)
  414. {
  415. const struct peer_svc *svc;
  416. const struct peer_chr *chr;
  417. svc = peer_svc_find_uuid(peer, svc_uuid);
  418. if (svc == NULL) {
  419. return NULL;
  420. }
  421. SLIST_FOREACH(chr, &svc->chrs, next) {
  422. if (ble_uuid_cmp(&chr->chr.uuid.u, chr_uuid) == 0) {
  423. return chr;
  424. }
  425. }
  426. return NULL;
  427. }
  428. const struct peer_dsc *
  429. peer_dsc_find_uuid(const struct peer *peer, const ble_uuid_t *svc_uuid,
  430. const ble_uuid_t *chr_uuid, const ble_uuid_t *dsc_uuid)
  431. {
  432. const struct peer_chr *chr;
  433. const struct peer_dsc *dsc;
  434. chr = peer_chr_find_uuid(peer, svc_uuid, chr_uuid);
  435. if (chr == NULL) {
  436. return NULL;
  437. }
  438. SLIST_FOREACH(dsc, &chr->dscs, next) {
  439. if (ble_uuid_cmp(&dsc->dsc.uuid.u, dsc_uuid) == 0) {
  440. return dsc;
  441. }
  442. }
  443. return NULL;
  444. }
  445. static int
  446. peer_svc_add(struct peer *peer, const struct ble_gatt_svc *gatt_svc)
  447. {
  448. struct peer_svc *prev;
  449. struct peer_svc *svc;
  450. svc = peer_svc_find(peer, gatt_svc->start_handle, &prev);
  451. if (svc != NULL) {
  452. /* Service already discovered. */
  453. return 0;
  454. }
  455. svc = os_memblock_get(&peer_svc_pool);
  456. if (svc == NULL) {
  457. /* Out of memory. */
  458. return BLE_HS_ENOMEM;
  459. }
  460. memset(svc, 0, sizeof * svc);
  461. svc->svc = *gatt_svc;
  462. SLIST_INIT(&svc->chrs);
  463. if (prev == NULL) {
  464. SLIST_INSERT_HEAD(&peer->svcs, svc, next);
  465. } else {
  466. SLIST_INSERT_AFTER(prev, svc, next);
  467. }
  468. return 0;
  469. }
  470. static void
  471. peer_svc_delete(struct peer_svc *svc)
  472. {
  473. struct peer_chr *chr;
  474. while ((chr = SLIST_FIRST(&svc->chrs)) != NULL) {
  475. SLIST_REMOVE_HEAD(&svc->chrs, next);
  476. peer_chr_delete(chr);
  477. }
  478. os_memblock_put(&peer_svc_pool, svc);
  479. }
  480. static int
  481. peer_svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error,
  482. const struct ble_gatt_svc *service, void *arg)
  483. {
  484. struct peer *peer;
  485. int rc;
  486. peer = arg;
  487. assert(peer->conn_handle == conn_handle);
  488. switch (error->status) {
  489. case 0:
  490. rc = peer_svc_add(peer, service);
  491. break;
  492. case BLE_HS_EDONE:
  493. /* All services discovered; start discovering characteristics. */
  494. if (peer->disc_prev_chr_val > 0) {
  495. peer_disc_chrs(peer);
  496. }
  497. rc = 0;
  498. break;
  499. default:
  500. rc = error->status;
  501. break;
  502. }
  503. if (rc != 0) {
  504. /* Error; abort discovery. */
  505. peer_disc_complete(peer, rc);
  506. }
  507. return rc;
  508. }
  509. int
  510. peer_disc_svc_by_uuid(uint16_t conn_handle, const ble_uuid_t *uuid, peer_disc_fn *disc_cb,
  511. void *disc_cb_arg)
  512. {
  513. struct peer_svc *svc;
  514. struct peer *peer;
  515. int rc;
  516. peer = peer_find(conn_handle);
  517. if (peer == NULL) {
  518. return BLE_HS_ENOTCONN;
  519. }
  520. /* Undiscover everything first. */
  521. while ((svc = SLIST_FIRST(&peer->svcs)) != NULL) {
  522. SLIST_REMOVE_HEAD(&peer->svcs, next);
  523. peer_svc_delete(svc);
  524. }
  525. peer->disc_prev_chr_val = 1;
  526. peer->disc_cb = disc_cb;
  527. peer->disc_cb_arg = disc_cb_arg;
  528. rc = ble_gattc_disc_svc_by_uuid(conn_handle, uuid, peer_svc_disced, peer);
  529. if (rc != 0) {
  530. return rc;
  531. }
  532. return 0;
  533. }
  534. int
  535. peer_disc_all(uint16_t conn_handle, peer_disc_fn *disc_cb, void *disc_cb_arg)
  536. {
  537. struct peer_svc *svc;
  538. struct peer *peer;
  539. int rc;
  540. peer = peer_find(conn_handle);
  541. if (peer == NULL) {
  542. return BLE_HS_ENOTCONN;
  543. }
  544. /* Undiscover everything first. */
  545. while ((svc = SLIST_FIRST(&peer->svcs)) != NULL) {
  546. SLIST_REMOVE_HEAD(&peer->svcs, next);
  547. peer_svc_delete(svc);
  548. }
  549. peer->disc_prev_chr_val = 1;
  550. peer->disc_cb = disc_cb;
  551. peer->disc_cb_arg = disc_cb_arg;
  552. rc = ble_gattc_disc_all_svcs(conn_handle, peer_svc_disced, peer);
  553. if (rc != 0) {
  554. return rc;
  555. }
  556. return 0;
  557. }
  558. int
  559. peer_delete(uint16_t conn_handle)
  560. {
  561. struct peer_svc *svc;
  562. struct peer *peer;
  563. int rc;
  564. peer = peer_find(conn_handle);
  565. if (peer == NULL) {
  566. return BLE_HS_ENOTCONN;
  567. }
  568. SLIST_REMOVE(&peers, peer, peer, next);
  569. while ((svc = SLIST_FIRST(&peer->svcs)) != NULL) {
  570. SLIST_REMOVE_HEAD(&peer->svcs, next);
  571. peer_svc_delete(svc);
  572. }
  573. rc = os_memblock_put(&peer_pool, peer);
  574. if (rc != 0) {
  575. return BLE_HS_EOS;
  576. }
  577. return 0;
  578. }
  579. int
  580. peer_add(uint16_t conn_handle)
  581. {
  582. struct peer *peer;
  583. /* Make sure the connection handle is unique. */
  584. peer = peer_find(conn_handle);
  585. if (peer != NULL) {
  586. return BLE_HS_EALREADY;
  587. }
  588. peer = os_memblock_get(&peer_pool);
  589. if (peer == NULL) {
  590. /* Out of memory. */
  591. return BLE_HS_ENOMEM;
  592. }
  593. memset(peer, 0, sizeof * peer);
  594. peer->conn_handle = conn_handle;
  595. SLIST_INSERT_HEAD(&peers, peer, next);
  596. return 0;
  597. }
  598. void
  599. peer_traverse_all(peer_traverse_fn *trav_cb, void *arg)
  600. {
  601. struct peer *peer;
  602. if (!trav_cb) {
  603. return;
  604. }
  605. SLIST_FOREACH(peer, &peers, next) {
  606. if (trav_cb(peer, arg)) {
  607. return;
  608. }
  609. }
  610. }
  611. #if MYNEWT_VAL(ENC_ADV_DATA)
  612. int
  613. peer_set_addr(uint16_t conn_handle, uint8_t *peer_addr)
  614. {
  615. struct peer *peer;
  616. peer = peer_find(conn_handle);
  617. if (peer == NULL) {
  618. return BLE_HS_ENOTCONN;
  619. }
  620. memcpy(&peer->peer_addr, peer_addr, PEER_ADDR_VAL_SIZE);
  621. return 0;
  622. }
  623. #endif
  624. static void
  625. peer_free_mem(void)
  626. {
  627. free(peer_mem);
  628. peer_mem = NULL;
  629. free(peer_svc_mem);
  630. peer_svc_mem = NULL;
  631. free(peer_chr_mem);
  632. peer_chr_mem = NULL;
  633. free(peer_dsc_mem);
  634. peer_dsc_mem = NULL;
  635. }
  636. int
  637. peer_init(int max_peers, int max_svcs, int max_chrs, int max_dscs)
  638. {
  639. int rc;
  640. /* Free memory first in case this function gets called more than once. */
  641. peer_free_mem();
  642. peer_mem = malloc(
  643. OS_MEMPOOL_BYTES(max_peers, sizeof (struct peer)));
  644. if (peer_mem == NULL) {
  645. rc = BLE_HS_ENOMEM;
  646. goto err;
  647. }
  648. rc = os_mempool_init(&peer_pool, max_peers,
  649. sizeof (struct peer), peer_mem,
  650. "peer_pool");
  651. if (rc != 0) {
  652. rc = BLE_HS_EOS;
  653. goto err;
  654. }
  655. peer_svc_mem = malloc(
  656. OS_MEMPOOL_BYTES(max_svcs, sizeof (struct peer_svc)));
  657. if (peer_svc_mem == NULL) {
  658. rc = BLE_HS_ENOMEM;
  659. goto err;
  660. }
  661. rc = os_mempool_init(&peer_svc_pool, max_svcs,
  662. sizeof (struct peer_svc), peer_svc_mem,
  663. "peer_svc_pool");
  664. if (rc != 0) {
  665. rc = BLE_HS_EOS;
  666. goto err;
  667. }
  668. peer_chr_mem = malloc(
  669. OS_MEMPOOL_BYTES(max_chrs, sizeof (struct peer_chr)));
  670. if (peer_chr_mem == NULL) {
  671. rc = BLE_HS_ENOMEM;
  672. goto err;
  673. }
  674. rc = os_mempool_init(&peer_chr_pool, max_chrs,
  675. sizeof (struct peer_chr), peer_chr_mem,
  676. "peer_chr_pool");
  677. if (rc != 0) {
  678. rc = BLE_HS_EOS;
  679. goto err;
  680. }
  681. peer_dsc_mem = malloc(
  682. OS_MEMPOOL_BYTES(max_dscs, sizeof (struct peer_dsc)));
  683. if (peer_dsc_mem == NULL) {
  684. rc = BLE_HS_ENOMEM;
  685. goto err;
  686. }
  687. rc = os_mempool_init(&peer_dsc_pool, max_dscs,
  688. sizeof (struct peer_dsc), peer_dsc_mem,
  689. "peer_dsc_pool");
  690. if (rc != 0) {
  691. rc = BLE_HS_EOS;
  692. goto err;
  693. }
  694. return 0;
  695. err:
  696. peer_free_mem();
  697. return rc;
  698. }