peer_manager.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /**
  2. * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(PEER_MANAGER)
  42. #include "ble_err.h"
  43. #include "peer_manager.h"
  44. #include <string.h>
  45. #include "security_manager.h"
  46. #include "security_dispatcher.h"
  47. #include "gatt_cache_manager.h"
  48. #include "gatts_cache_manager.h"
  49. #include "peer_database.h"
  50. #include "peer_data_storage.h"
  51. #include "id_manager.h"
  52. #include "ble_conn_state.h"
  53. #include "peer_manager_internal.h"
  54. #include "nrf_sdh_ble.h"
  55. #define NRF_LOG_MODULE_NAME peer_manager
  56. #if PM_LOG_ENABLED
  57. #define NRF_LOG_LEVEL PM_LOG_LEVEL
  58. #define NRF_LOG_INFO_COLOR PM_LOG_INFO_COLOR
  59. #define NRF_LOG_DEBUG_COLOR PM_LOG_DEBUG_COLOR
  60. #else
  61. #define NRF_LOG_LEVEL 0
  62. #endif // PM_LOG_ENABLED
  63. #include "nrf_log.h"
  64. #include "nrf_log_ctrl.h"
  65. NRF_LOG_MODULE_REGISTER();
  66. #include "nrf_strerror.h"
  67. #ifndef PM_PEER_RANKS_ENABLED
  68. #define PM_PEER_RANKS_ENABLED 1
  69. #endif
  70. #define MODULE_INITIALIZED (m_module_initialized) /**< Macro indicating whether the module has been initialized properly. */
  71. static bool m_module_initialized; /**< Whether or not @ref pm_init has been called successfully. */
  72. static bool m_peer_rank_initialized; /**< Whether or not @ref rank_init has been called successfully. */
  73. static bool m_deleting_all; /**< True from when @ref pm_peers_delete is called until all peers have been deleted. */
  74. static pm_store_token_t m_peer_rank_token; /**< The store token of an ongoing peer rank update via a call to @ref pm_peer_rank_highest. If @ref PM_STORE_TOKEN_INVALID, there is no ongoing update. */
  75. static uint32_t m_current_highest_peer_rank; /**< The current highest peer rank. Used by @ref pm_peer_rank_highest. */
  76. static pm_peer_id_t m_highest_ranked_peer; /**< The peer with the highest peer rank. Used by @ref pm_peer_rank_highest. */
  77. static pm_evt_handler_t m_evt_handlers[PM_MAX_REGISTRANTS];/**< The subscribers to Peer Manager events, as registered through @ref pm_register. */
  78. static uint8_t m_n_registrants; /**< The number of event handlers registered through @ref pm_register. */
  79. /**@brief Function for sending a Peer Manager event to all subscribers.
  80. *
  81. * @param[in] p_pm_evt The event to send.
  82. */
  83. static void evt_send(pm_evt_t const * p_pm_evt)
  84. {
  85. for (int i = 0; i < m_n_registrants; i++)
  86. {
  87. m_evt_handlers[i](p_pm_evt);
  88. }
  89. }
  90. #if PM_PEER_RANKS_ENABLED == 1
  91. /**@brief Function for initializing peer rank static variables.
  92. */
  93. static void rank_vars_update(void)
  94. {
  95. ret_code_t err_code = pm_peer_ranks_get(&m_highest_ranked_peer,
  96. &m_current_highest_peer_rank,
  97. NULL,
  98. NULL);
  99. if (err_code == NRF_ERROR_NOT_FOUND)
  100. {
  101. m_highest_ranked_peer = PM_PEER_ID_INVALID;
  102. m_current_highest_peer_rank = 0;
  103. }
  104. m_peer_rank_initialized = ((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NOT_FOUND));
  105. }
  106. #endif
  107. /**@brief Event handler for events from the Peer Database module.
  108. * This handler is extern in the Peer Database module.
  109. *
  110. * @param[in] p_pdb_evt The incoming Peer Database event.
  111. */
  112. void pm_pdb_evt_handler(pm_evt_t * p_pdb_evt)
  113. {
  114. bool send_evt = true;
  115. p_pdb_evt->conn_handle = im_conn_handle_get(p_pdb_evt->peer_id);
  116. switch (p_pdb_evt->evt_id)
  117. {
  118. #if PM_PEER_RANKS_ENABLED == 1
  119. case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
  120. if (p_pdb_evt->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE)
  121. {
  122. if ( (m_peer_rank_token != PM_STORE_TOKEN_INVALID)
  123. && (m_peer_rank_token == p_pdb_evt->params.peer_data_update_succeeded.token))
  124. {
  125. m_peer_rank_token = PM_STORE_TOKEN_INVALID;
  126. m_highest_ranked_peer = p_pdb_evt->peer_id;
  127. p_pdb_evt->params.peer_data_update_succeeded.token = PM_STORE_TOKEN_INVALID;
  128. }
  129. else if ( m_peer_rank_initialized
  130. && (p_pdb_evt->peer_id == m_highest_ranked_peer)
  131. && (p_pdb_evt->params.peer_data_update_succeeded.data_id
  132. == PM_PEER_DATA_ID_PEER_RANK))
  133. {
  134. // Update peer rank variable if highest ranked peer has changed its rank.
  135. rank_vars_update();
  136. }
  137. }
  138. else if (p_pdb_evt->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_DELETE)
  139. {
  140. if ( m_peer_rank_initialized
  141. && (p_pdb_evt->peer_id == m_highest_ranked_peer)
  142. && (p_pdb_evt->params.peer_data_update_succeeded.data_id == PM_PEER_DATA_ID_PEER_RANK))
  143. {
  144. // Update peer rank variable if highest ranked peer has deleted its rank.
  145. rank_vars_update();
  146. }
  147. }
  148. break;
  149. case PM_EVT_PEER_DATA_UPDATE_FAILED:
  150. if (p_pdb_evt->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE)
  151. {
  152. if ( (m_peer_rank_token != PM_STORE_TOKEN_INVALID)
  153. && (m_peer_rank_token == p_pdb_evt->params.peer_data_update_failed.token))
  154. {
  155. m_peer_rank_token = PM_STORE_TOKEN_INVALID;
  156. m_current_highest_peer_rank -= 1;
  157. p_pdb_evt->params.peer_data_update_succeeded.token = PM_STORE_TOKEN_INVALID;
  158. }
  159. }
  160. break;
  161. #endif
  162. case PM_EVT_PEER_DELETE_SUCCEEDED:
  163. // Check that no peers marked for deletion are left.
  164. if (m_deleting_all
  165. && (pds_next_peer_id_get(PM_PEER_ID_INVALID) == PM_PEER_ID_INVALID)
  166. && (pds_next_deleted_peer_id_get(PM_PEER_ID_INVALID) == PM_PEER_ID_INVALID))
  167. {
  168. // pm_peers_delete() has been called and this is the last peer to be deleted.
  169. m_deleting_all = false;
  170. pm_evt_t pm_delete_all_evt;
  171. memset(&pm_delete_all_evt, 0, sizeof(pm_evt_t));
  172. pm_delete_all_evt.evt_id = PM_EVT_PEERS_DELETE_SUCCEEDED;
  173. pm_delete_all_evt.peer_id = PM_PEER_ID_INVALID;
  174. pm_delete_all_evt.conn_handle = BLE_CONN_HANDLE_INVALID;
  175. send_evt = false;
  176. // Forward the event to all registered Peer Manager event handlers.
  177. evt_send(p_pdb_evt); // Ensure that PEER_DELETE_SUCCEEDED arrives before PEERS_DELETE_SUCCEEDED.
  178. evt_send(&pm_delete_all_evt);
  179. }
  180. #if PM_PEER_RANKS_ENABLED == 1
  181. if (m_peer_rank_initialized && (p_pdb_evt->peer_id == m_highest_ranked_peer))
  182. {
  183. // Update peer rank variable if highest ranked peer has been deleted.
  184. rank_vars_update();
  185. }
  186. #endif
  187. break;
  188. case PM_EVT_PEER_DELETE_FAILED:
  189. if (m_deleting_all)
  190. {
  191. // pm_peers_delete() was called and has thus failed.
  192. m_deleting_all = false;
  193. pm_evt_t pm_delete_all_evt;
  194. memset(&pm_delete_all_evt, 0, sizeof(pm_evt_t));
  195. pm_delete_all_evt.evt_id = PM_EVT_PEERS_DELETE_FAILED;
  196. pm_delete_all_evt.peer_id = PM_PEER_ID_INVALID;
  197. pm_delete_all_evt.conn_handle = BLE_CONN_HANDLE_INVALID;
  198. pm_delete_all_evt.params.peers_delete_failed_evt.error
  199. = p_pdb_evt->params.peer_delete_failed.error;
  200. send_evt = false;
  201. // Forward the event to all registered Peer Manager event handlers.
  202. evt_send(p_pdb_evt); // Ensure that PEER_DELETE_FAILED arrives before PEERS_DELETE_FAILED.
  203. evt_send(&pm_delete_all_evt);
  204. }
  205. break;
  206. default:
  207. // Do nothing.
  208. break;
  209. }
  210. if (send_evt)
  211. {
  212. // Forward the event to all registered Peer Manager event handlers.
  213. evt_send(p_pdb_evt);
  214. }
  215. }
  216. /**@brief Event handler for events from the Security Manager module.
  217. * This handler is extern in the Security Manager module.
  218. *
  219. * @param[in] p_sm_evt The incoming Security Manager event.
  220. */
  221. void pm_sm_evt_handler(pm_evt_t * p_sm_evt)
  222. {
  223. VERIFY_PARAM_NOT_NULL_VOID(p_sm_evt);
  224. // Forward the event to all registered Peer Manager event handlers.
  225. evt_send(p_sm_evt);
  226. }
  227. /**@brief Event handler for events from the GATT Cache Manager module.
  228. * This handler is extern in GATT Cache Manager.
  229. *
  230. * @param[in] p_gcm_evt The incoming GATT Cache Manager event.
  231. */
  232. void pm_gcm_evt_handler(pm_evt_t * p_gcm_evt)
  233. {
  234. // Forward the event to all registered Peer Manager event handlers.
  235. evt_send(p_gcm_evt);
  236. }
  237. /**@brief Event handler for events from the GATTS Cache Manager module.
  238. * This handler is extern in GATTS Cache Manager.
  239. *
  240. * @param[in] p_gscm_evt The incoming GATTS Cache Manager event.
  241. */
  242. void pm_gscm_evt_handler(pm_evt_t * p_gscm_evt)
  243. {
  244. // Forward the event to all registered Peer Manager event handlers.
  245. evt_send(p_gscm_evt);
  246. }
  247. /**@brief Event handler for events from the ID Manager module.
  248. * This function is registered in the ID Manager.
  249. *
  250. * @param[in] p_im_evt The incoming ID Manager event.
  251. */
  252. void pm_im_evt_handler(pm_evt_t * p_im_evt)
  253. {
  254. // Forward the event to all registered Peer Manager event handlers.
  255. evt_send(p_im_evt);
  256. }
  257. /**
  258. * @brief Function for handling BLE events.
  259. *
  260. * @param[in] p_ble_evt Event received from the BLE stack.
  261. * @param[in] p_context Context.
  262. */
  263. static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
  264. {
  265. VERIFY_MODULE_INITIALIZED_VOID();
  266. im_ble_evt_handler(p_ble_evt);
  267. sm_ble_evt_handler(p_ble_evt);
  268. gcm_ble_evt_handler(p_ble_evt);
  269. }
  270. NRF_SDH_BLE_OBSERVER(m_ble_evt_observer, PM_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  271. /**@brief Function for resetting the internal state of this module.
  272. */
  273. static void internal_state_reset()
  274. {
  275. m_highest_ranked_peer = PM_PEER_ID_INVALID;
  276. m_peer_rank_token = PM_STORE_TOKEN_INVALID;
  277. }
  278. ret_code_t pm_init(void)
  279. {
  280. ret_code_t err_code;
  281. err_code = pds_init();
  282. if (err_code != NRF_SUCCESS)
  283. {
  284. NRF_LOG_ERROR("%s failed because pds_init() returned %s.", __func__, nrf_strerror_get(err_code));
  285. return NRF_ERROR_INTERNAL;
  286. }
  287. err_code = pdb_init();
  288. if (err_code != NRF_SUCCESS)
  289. {
  290. NRF_LOG_ERROR("%s failed because pdb_init() returned %s.", __func__, nrf_strerror_get(err_code));
  291. return NRF_ERROR_INTERNAL;
  292. }
  293. err_code = sm_init();
  294. if (err_code != NRF_SUCCESS)
  295. {
  296. NRF_LOG_ERROR("%s failed because sm_init() returned %s.", __func__, nrf_strerror_get(err_code));
  297. return NRF_ERROR_INTERNAL;
  298. }
  299. err_code = smd_init();
  300. if (err_code != NRF_SUCCESS)
  301. {
  302. NRF_LOG_ERROR("%s failed because smd_init() returned %s.", __func__, nrf_strerror_get(err_code));
  303. return NRF_ERROR_INTERNAL;
  304. }
  305. err_code = gcm_init();
  306. if (err_code != NRF_SUCCESS)
  307. {
  308. NRF_LOG_ERROR("%s failed because gcm_init() returned %s.", __func__, nrf_strerror_get(err_code));
  309. return NRF_ERROR_INTERNAL;
  310. }
  311. err_code = gscm_init();
  312. if (err_code != NRF_SUCCESS)
  313. {
  314. NRF_LOG_ERROR("%s failed because gscm_init() returned %s.", __func__, nrf_strerror_get(err_code));
  315. return NRF_ERROR_INTERNAL;
  316. }
  317. internal_state_reset();
  318. m_peer_rank_initialized = false;
  319. m_module_initialized = true;
  320. // If PM_PEER_RANKS_ENABLED is 0, these variables are unused.
  321. UNUSED_VARIABLE(m_peer_rank_initialized);
  322. UNUSED_VARIABLE(m_peer_rank_token);
  323. UNUSED_VARIABLE(m_current_highest_peer_rank);
  324. UNUSED_VARIABLE(m_highest_ranked_peer);
  325. return NRF_SUCCESS;
  326. }
  327. ret_code_t pm_register(pm_evt_handler_t event_handler)
  328. {
  329. VERIFY_MODULE_INITIALIZED();
  330. if (m_n_registrants >= PM_MAX_REGISTRANTS)
  331. {
  332. return NRF_ERROR_NO_MEM;
  333. }
  334. m_evt_handlers[m_n_registrants] = event_handler;
  335. m_n_registrants += 1;
  336. return NRF_SUCCESS;
  337. }
  338. ret_code_t pm_sec_params_set(ble_gap_sec_params_t * p_sec_params)
  339. {
  340. VERIFY_MODULE_INITIALIZED();
  341. ret_code_t err_code;
  342. err_code = sm_sec_params_set(p_sec_params);
  343. // NRF_ERROR_INVALID_PARAM if parameters are invalid,
  344. // NRF_SUCCESS otherwise.
  345. return err_code;
  346. }
  347. ret_code_t pm_conn_secure(uint16_t conn_handle, bool force_repairing)
  348. {
  349. VERIFY_MODULE_INITIALIZED();
  350. ret_code_t err_code;
  351. err_code = sm_link_secure(conn_handle, force_repairing);
  352. if (err_code == NRF_ERROR_INVALID_STATE)
  353. {
  354. err_code = NRF_ERROR_BUSY;
  355. }
  356. return err_code;
  357. }
  358. void pm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t * p_conn_sec_config)
  359. {
  360. if (p_conn_sec_config != NULL)
  361. {
  362. sm_conn_sec_config_reply(conn_handle, p_conn_sec_config);
  363. }
  364. }
  365. ret_code_t pm_conn_sec_params_reply(uint16_t conn_handle,
  366. ble_gap_sec_params_t * p_sec_params,
  367. void const * p_context)
  368. {
  369. VERIFY_MODULE_INITIALIZED();
  370. return sm_sec_params_reply(conn_handle, p_sec_params, p_context);
  371. }
  372. void pm_local_database_has_changed(void)
  373. {
  374. #if !defined(PM_SERVICE_CHANGED_ENABLED) || (PM_SERVICE_CHANGED_ENABLED == 1)
  375. VERIFY_MODULE_INITIALIZED_VOID();
  376. gcm_local_database_has_changed();
  377. #endif
  378. }
  379. ret_code_t pm_id_addr_set(ble_gap_addr_t const * p_addr)
  380. {
  381. VERIFY_MODULE_INITIALIZED();
  382. return im_id_addr_set(p_addr);
  383. }
  384. ret_code_t pm_id_addr_get(ble_gap_addr_t * p_addr)
  385. {
  386. VERIFY_MODULE_INITIALIZED();
  387. VERIFY_PARAM_NOT_NULL(p_addr);
  388. return im_id_addr_get(p_addr);
  389. }
  390. ret_code_t pm_privacy_set(pm_privacy_params_t const * p_privacy_params)
  391. {
  392. VERIFY_MODULE_INITIALIZED();
  393. VERIFY_PARAM_NOT_NULL(p_privacy_params);
  394. return im_privacy_set(p_privacy_params);
  395. }
  396. ret_code_t pm_privacy_get(pm_privacy_params_t * p_privacy_params)
  397. {
  398. VERIFY_MODULE_INITIALIZED();
  399. VERIFY_PARAM_NOT_NULL(p_privacy_params);
  400. VERIFY_PARAM_NOT_NULL(p_privacy_params->p_device_irk);
  401. return im_privacy_get(p_privacy_params);
  402. }
  403. bool pm_address_resolve(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * p_irk)
  404. {
  405. VERIFY_MODULE_INITIALIZED();
  406. if ((p_addr == NULL) || (p_irk == NULL))
  407. {
  408. return false;
  409. }
  410. else
  411. {
  412. return im_address_resolve(p_addr, p_irk);
  413. }
  414. }
  415. ret_code_t pm_whitelist_set(pm_peer_id_t const * p_peers,
  416. uint32_t peer_cnt)
  417. {
  418. VERIFY_MODULE_INITIALIZED();
  419. return im_whitelist_set(p_peers, peer_cnt);
  420. }
  421. ret_code_t pm_whitelist_get(ble_gap_addr_t * p_addrs,
  422. uint32_t * p_addr_cnt,
  423. ble_gap_irk_t * p_irks,
  424. uint32_t * p_irk_cnt)
  425. {
  426. VERIFY_MODULE_INITIALIZED();
  427. if (((p_addrs == NULL) && (p_irks == NULL)) ||
  428. ((p_addrs != NULL) && (p_addr_cnt == NULL)) ||
  429. ((p_irks != NULL) && (p_irk_cnt == NULL)))
  430. {
  431. // The buffers can't be both NULL, and if a buffer is provided its size must be specified.
  432. return NRF_ERROR_NULL;
  433. }
  434. return im_whitelist_get(p_addrs, p_addr_cnt, p_irks, p_irk_cnt);
  435. }
  436. ret_code_t pm_device_identities_list_set(pm_peer_id_t const * p_peers,
  437. uint32_t peer_cnt)
  438. {
  439. VERIFY_MODULE_INITIALIZED();
  440. return im_device_identities_list_set(p_peers, peer_cnt);
  441. }
  442. ret_code_t pm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t * p_conn_sec_status)
  443. {
  444. VERIFY_MODULE_INITIALIZED();
  445. return sm_conn_sec_status_get(conn_handle, p_conn_sec_status);
  446. }
  447. bool pm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t * p_sec_status_req)
  448. {
  449. VERIFY_MODULE_INITIALIZED_BOOL();
  450. return sm_sec_is_sufficient(conn_handle, p_sec_status_req);
  451. }
  452. ret_code_t pm_lesc_public_key_set(ble_gap_lesc_p256_pk_t * p_public_key)
  453. {
  454. VERIFY_MODULE_INITIALIZED();
  455. return sm_lesc_public_key_set(p_public_key);
  456. }
  457. ret_code_t pm_conn_handle_get(pm_peer_id_t peer_id, uint16_t * p_conn_handle)
  458. {
  459. VERIFY_MODULE_INITIALIZED();
  460. VERIFY_PARAM_NOT_NULL(p_conn_handle);
  461. *p_conn_handle = im_conn_handle_get(peer_id);
  462. return NRF_SUCCESS;
  463. }
  464. ret_code_t pm_peer_id_get(uint16_t conn_handle, pm_peer_id_t * p_peer_id)
  465. {
  466. VERIFY_MODULE_INITIALIZED();
  467. VERIFY_PARAM_NOT_NULL(p_peer_id);
  468. *p_peer_id = im_peer_id_get_by_conn_handle(conn_handle);
  469. return NRF_SUCCESS;
  470. }
  471. uint32_t pm_peer_count(void)
  472. {
  473. if (!MODULE_INITIALIZED)
  474. {
  475. return 0;
  476. }
  477. return pds_peer_count_get();
  478. }
  479. pm_peer_id_t pm_next_peer_id_get(pm_peer_id_t prev_peer_id)
  480. {
  481. pm_peer_id_t next_peer_id = prev_peer_id;
  482. if (!MODULE_INITIALIZED)
  483. {
  484. return PM_PEER_ID_INVALID;
  485. }
  486. do
  487. {
  488. next_peer_id = pds_next_peer_id_get(next_peer_id);
  489. } while (pds_peer_id_is_deleted(next_peer_id));
  490. return next_peer_id;
  491. }
  492. /**@brief Function for checking if the peer has a valid Identity Resolving Key.
  493. *
  494. * @param[in] p_irk Pointer to the Identity Resolving Key.
  495. */
  496. static bool peer_is_irk(ble_gap_irk_t const * const p_irk)
  497. {
  498. for (uint32_t i = 0; i < ARRAY_SIZE(p_irk->irk); i++)
  499. {
  500. if (p_irk->irk[i] != 0)
  501. {
  502. return true;
  503. }
  504. }
  505. return false;
  506. }
  507. ret_code_t pm_peer_id_list(pm_peer_id_t * p_peer_list,
  508. uint32_t * const p_list_size,
  509. pm_peer_id_t first_peer_id,
  510. pm_peer_id_list_skip_t skip_id)
  511. {
  512. VERIFY_MODULE_INITIALIZED();
  513. VERIFY_PARAM_NOT_NULL(p_list_size);
  514. VERIFY_PARAM_NOT_NULL(p_peer_list);
  515. ret_code_t err_code;
  516. uint32_t size = *p_list_size;
  517. uint32_t current_size = 0;
  518. pm_peer_data_t pm_car_data;
  519. pm_peer_data_t pm_bond_data;
  520. pm_peer_id_t current_peer_id = first_peer_id;
  521. ble_gap_addr_t const * p_gap_addr;
  522. bool skip_no_addr = skip_id & PM_PEER_ID_LIST_SKIP_NO_ID_ADDR;
  523. bool skip_no_irk = skip_id & PM_PEER_ID_LIST_SKIP_NO_IRK;
  524. bool skip_no_car = skip_id & PM_PEER_ID_LIST_SKIP_NO_CAR;
  525. //lint -save -e685
  526. if ((*p_list_size < 1) ||
  527. (skip_id > (PM_PEER_ID_LIST_SKIP_NO_ID_ADDR | PM_PEER_ID_LIST_SKIP_ALL)))
  528. {
  529. return NRF_ERROR_INVALID_PARAM;
  530. }
  531. //lint -restore
  532. *p_list_size = 0;
  533. if (current_peer_id == PM_PEER_ID_INVALID)
  534. {
  535. current_peer_id = pm_next_peer_id_get(current_peer_id);
  536. if (current_peer_id == PM_PEER_ID_INVALID)
  537. {
  538. return NRF_SUCCESS;
  539. }
  540. }
  541. memset(&pm_car_data, 0, sizeof(pm_peer_data_t));
  542. memset(&pm_bond_data, 0, sizeof(pm_peer_data_t));
  543. while (current_peer_id != PM_PEER_ID_INVALID)
  544. {
  545. bool skip = false;
  546. if (skip_no_addr || skip_no_irk)
  547. {
  548. // Get data
  549. pm_bond_data.p_bonding_data = NULL;
  550. err_code = pds_peer_data_read(current_peer_id,
  551. PM_PEER_DATA_ID_BONDING,
  552. &pm_bond_data,
  553. NULL);
  554. if (err_code == NRF_ERROR_NOT_FOUND)
  555. {
  556. skip = true;
  557. }
  558. else
  559. {
  560. VERIFY_SUCCESS(err_code);
  561. }
  562. // Check data
  563. if (skip_no_addr)
  564. {
  565. p_gap_addr = &pm_bond_data.p_bonding_data->peer_ble_id.id_addr_info;
  566. if ((p_gap_addr->addr_type != BLE_GAP_ADDR_TYPE_PUBLIC) &&
  567. (p_gap_addr->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_STATIC))
  568. {
  569. skip = true;
  570. }
  571. }
  572. if (skip_no_irk)
  573. {
  574. if (!peer_is_irk(&pm_bond_data.p_bonding_data->peer_ble_id.id_info))
  575. {
  576. skip = true;
  577. }
  578. }
  579. }
  580. if (skip_no_car)
  581. {
  582. // Get data
  583. pm_car_data.p_central_addr_res = NULL;
  584. err_code = pds_peer_data_read(current_peer_id,
  585. PM_PEER_DATA_ID_CENTRAL_ADDR_RES,
  586. &pm_car_data,
  587. NULL);
  588. if (err_code == NRF_ERROR_NOT_FOUND)
  589. {
  590. skip = true;
  591. }
  592. else
  593. {
  594. VERIFY_SUCCESS(err_code);
  595. }
  596. // Check data
  597. if (*pm_car_data.p_central_addr_res == 0)
  598. {
  599. skip = true;
  600. }
  601. }
  602. if (!skip)
  603. {
  604. p_peer_list[current_size++] = current_peer_id;
  605. if (current_size >= size)
  606. {
  607. break;
  608. }
  609. }
  610. current_peer_id = pm_next_peer_id_get(current_peer_id);
  611. }
  612. *p_list_size = current_size;
  613. return NRF_SUCCESS;
  614. }
  615. ret_code_t pm_peer_data_load(pm_peer_id_t peer_id,
  616. pm_peer_data_id_t data_id,
  617. void * p_data,
  618. uint32_t * p_length)
  619. {
  620. VERIFY_MODULE_INITIALIZED();
  621. VERIFY_PARAM_NOT_NULL(p_data);
  622. VERIFY_PARAM_NOT_NULL(p_length);
  623. pm_peer_data_t peer_data;
  624. memset(&peer_data, 0, sizeof(peer_data));
  625. peer_data.p_all_data = p_data;
  626. return pds_peer_data_read(peer_id, data_id, &peer_data, p_length);
  627. }
  628. ret_code_t pm_peer_data_bonding_load(pm_peer_id_t peer_id,
  629. pm_peer_data_bonding_t * p_data)
  630. {
  631. uint32_t length = sizeof(pm_peer_data_bonding_t);
  632. return pm_peer_data_load(peer_id,
  633. PM_PEER_DATA_ID_BONDING,
  634. p_data,
  635. &length);
  636. }
  637. ret_code_t pm_peer_data_remote_db_load(pm_peer_id_t peer_id,
  638. ble_gatt_db_srv_t * p_data,
  639. uint32_t * p_length)
  640. {
  641. return pm_peer_data_load(peer_id,
  642. PM_PEER_DATA_ID_GATT_REMOTE,
  643. p_data,
  644. p_length);
  645. }
  646. ret_code_t pm_peer_data_app_data_load(pm_peer_id_t peer_id,
  647. void * p_data,
  648. uint32_t * p_length)
  649. {
  650. return pm_peer_data_load(peer_id,
  651. PM_PEER_DATA_ID_APPLICATION,
  652. p_data,
  653. p_length);
  654. }
  655. ret_code_t pm_peer_data_store(pm_peer_id_t peer_id,
  656. pm_peer_data_id_t data_id,
  657. void const * p_data,
  658. uint32_t length,
  659. pm_store_token_t * p_token)
  660. {
  661. VERIFY_MODULE_INITIALIZED();
  662. VERIFY_PARAM_NOT_NULL(p_data);
  663. if (ALIGN_NUM(4, length) != length)
  664. {
  665. return NRF_ERROR_INVALID_PARAM;
  666. }
  667. if (data_id == PM_PEER_DATA_ID_BONDING)
  668. {
  669. pm_peer_id_t dupl_peer_id;
  670. dupl_peer_id = im_find_duplicate_bonding_data((pm_peer_data_bonding_t *) p_data, peer_id);
  671. if (dupl_peer_id != PM_PEER_ID_INVALID)
  672. {
  673. return NRF_ERROR_FORBIDDEN;
  674. }
  675. }
  676. pm_peer_data_flash_t peer_data;
  677. memset(&peer_data, 0, sizeof(peer_data));
  678. peer_data.length_words = BYTES_TO_WORDS(length);
  679. peer_data.data_id = data_id;
  680. peer_data.p_all_data = p_data;
  681. return pds_peer_data_store(peer_id, &peer_data, p_token);
  682. }
  683. ret_code_t pm_peer_data_bonding_store(pm_peer_id_t peer_id,
  684. pm_peer_data_bonding_t const * p_data,
  685. pm_store_token_t * p_token)
  686. {
  687. return pm_peer_data_store(peer_id,
  688. PM_PEER_DATA_ID_BONDING,
  689. p_data,
  690. ALIGN_NUM(4, sizeof(pm_peer_data_bonding_t)),
  691. p_token);
  692. }
  693. ret_code_t pm_peer_data_remote_db_store(pm_peer_id_t peer_id,
  694. ble_gatt_db_srv_t const * p_data,
  695. uint32_t length,
  696. pm_store_token_t * p_token)
  697. {
  698. return pm_peer_data_store(peer_id,
  699. PM_PEER_DATA_ID_GATT_REMOTE,
  700. p_data,
  701. length,
  702. p_token);
  703. }
  704. ret_code_t pm_peer_data_app_data_store(pm_peer_id_t peer_id,
  705. void const * p_data,
  706. uint32_t length,
  707. pm_store_token_t * p_token)
  708. {
  709. return pm_peer_data_store(peer_id,
  710. PM_PEER_DATA_ID_APPLICATION,
  711. p_data,
  712. length,
  713. p_token);
  714. }
  715. ret_code_t pm_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id)
  716. {
  717. VERIFY_MODULE_INITIALIZED();
  718. if (data_id == PM_PEER_DATA_ID_BONDING)
  719. {
  720. return NRF_ERROR_INVALID_PARAM;
  721. }
  722. return pds_peer_data_delete(peer_id, data_id);
  723. }
  724. ret_code_t pm_peer_new(pm_peer_id_t * p_new_peer_id,
  725. pm_peer_data_bonding_t * p_bonding_data,
  726. pm_store_token_t * p_token)
  727. {
  728. ret_code_t err_code;
  729. pm_peer_id_t peer_id;
  730. pm_peer_data_flash_t peer_data;
  731. VERIFY_MODULE_INITIALIZED();
  732. VERIFY_PARAM_NOT_NULL(p_bonding_data);
  733. VERIFY_PARAM_NOT_NULL(p_new_peer_id);
  734. memset(&peer_data, 0, sizeof(pm_peer_data_flash_t));
  735. // Search through existing bonds to look for a duplicate.
  736. pds_peer_data_iterate_prepare();
  737. // @note This check is not thread safe since data is not copied while iterating.
  738. while (pds_peer_data_iterate(PM_PEER_DATA_ID_BONDING, &peer_id, &peer_data))
  739. {
  740. if (im_is_duplicate_bonding_data(p_bonding_data, peer_data.p_bonding_data))
  741. {
  742. *p_new_peer_id = peer_id;
  743. return NRF_SUCCESS;
  744. }
  745. }
  746. // If no duplicate data is found, prepare to write a new bond to flash.
  747. *p_new_peer_id = pds_peer_id_allocate();
  748. if (*p_new_peer_id == PM_PEER_ID_INVALID)
  749. {
  750. return NRF_ERROR_NO_MEM;
  751. }
  752. memset(&peer_data, 0, sizeof(pm_peer_data_flash_t));
  753. peer_data.data_id = PM_PEER_DATA_ID_BONDING;
  754. peer_data.p_bonding_data = p_bonding_data;
  755. peer_data.length_words = BYTES_TO_WORDS(sizeof(pm_peer_data_bonding_t));
  756. err_code = pds_peer_data_store(*p_new_peer_id, &peer_data, p_token);
  757. if (err_code != NRF_SUCCESS)
  758. {
  759. ret_code_t err_code_free = im_peer_free(*p_new_peer_id);
  760. if (err_code_free != NRF_SUCCESS)
  761. {
  762. NRF_LOG_ERROR("Fatal error during cleanup of a failed call to %s. im_peer_free() "\
  763. "returned %s. peer_id: %d",
  764. __func__,
  765. nrf_strerror_get(err_code_free),
  766. *p_new_peer_id);
  767. return NRF_ERROR_INTERNAL;
  768. }
  769. // NRF_ERROR_STORAGE_FULL, if no space in flash.
  770. // NRF_ERROR_BUSY, if flash filesystem was busy.
  771. // NRF_ERROR_INVALID_ADDR, if bonding data is unaligned.
  772. // NRF_ERROR_INTENRAL, on internal error.
  773. return err_code;
  774. }
  775. return NRF_SUCCESS;
  776. }
  777. ret_code_t pm_peer_delete(pm_peer_id_t peer_id)
  778. {
  779. VERIFY_MODULE_INITIALIZED();
  780. return im_peer_free(peer_id);
  781. }
  782. ret_code_t pm_peers_delete(void)
  783. {
  784. VERIFY_MODULE_INITIALIZED();
  785. m_deleting_all = true;
  786. pm_peer_id_t current_peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID);
  787. if (current_peer_id == PM_PEER_ID_INVALID)
  788. {
  789. // No peers bonded.
  790. m_deleting_all = false;
  791. pm_evt_t pm_delete_all_evt;
  792. memset(&pm_delete_all_evt, 0, sizeof(pm_evt_t));
  793. pm_delete_all_evt.evt_id = PM_EVT_PEERS_DELETE_SUCCEEDED;
  794. pm_delete_all_evt.peer_id = PM_PEER_ID_INVALID;
  795. pm_delete_all_evt.conn_handle = BLE_CONN_HANDLE_INVALID;
  796. evt_send(&pm_delete_all_evt);
  797. }
  798. while (current_peer_id != PM_PEER_ID_INVALID)
  799. {
  800. ret_code_t err_code = pm_peer_delete(current_peer_id);
  801. if (err_code != NRF_SUCCESS)
  802. {
  803. NRF_LOG_ERROR("%s() failed because a call to pm_peer_delete() returned %s. peer_id: %d",
  804. __func__,
  805. nrf_strerror_get(err_code),
  806. current_peer_id);
  807. return NRF_ERROR_INTERNAL;
  808. }
  809. current_peer_id = pds_next_peer_id_get(current_peer_id);
  810. }
  811. return NRF_SUCCESS;
  812. }
  813. ret_code_t pm_peer_ranks_get(pm_peer_id_t * p_highest_ranked_peer,
  814. uint32_t * p_highest_rank,
  815. pm_peer_id_t * p_lowest_ranked_peer,
  816. uint32_t * p_lowest_rank)
  817. {
  818. #if PM_PEER_RANKS_ENABLED == 0
  819. return NRF_ERROR_NOT_SUPPORTED;
  820. #else
  821. VERIFY_MODULE_INITIALIZED();
  822. pm_peer_id_t peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID);
  823. uint32_t peer_rank = 0;
  824. //lint -save -e65 -e64
  825. uint32_t length = sizeof(peer_rank);
  826. pm_peer_data_t peer_data = {.p_peer_rank = &peer_rank};
  827. //lint -restore
  828. ret_code_t err_code = pds_peer_data_read(peer_id,
  829. PM_PEER_DATA_ID_PEER_RANK,
  830. &peer_data,
  831. &length);
  832. uint32_t highest_rank = 0;
  833. uint32_t lowest_rank = 0xFFFFFFFF;
  834. pm_peer_id_t highest_ranked_peer = PM_PEER_ID_INVALID;
  835. pm_peer_id_t lowest_ranked_peer = PM_PEER_ID_INVALID;
  836. if (err_code == NRF_ERROR_INVALID_PARAM)
  837. {
  838. // No peer IDs exist.
  839. return NRF_ERROR_NOT_FOUND;
  840. }
  841. while ((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NOT_FOUND))
  842. {
  843. if (err_code == NRF_SUCCESS)
  844. {
  845. if (peer_rank >= highest_rank)
  846. {
  847. highest_rank = peer_rank;
  848. highest_ranked_peer = peer_id;
  849. }
  850. if (peer_rank < lowest_rank)
  851. {
  852. lowest_rank = peer_rank;
  853. lowest_ranked_peer = peer_id;
  854. }
  855. }
  856. peer_id = pds_next_peer_id_get(peer_id);
  857. err_code = pds_peer_data_read(peer_id, PM_PEER_DATA_ID_PEER_RANK, &peer_data, &length);
  858. }
  859. if (peer_id == PM_PEER_ID_INVALID)
  860. {
  861. if ((highest_ranked_peer == PM_PEER_ID_INVALID) || (lowest_ranked_peer == PM_PEER_ID_INVALID))
  862. {
  863. err_code = NRF_ERROR_NOT_FOUND;
  864. }
  865. else
  866. {
  867. err_code = NRF_SUCCESS;
  868. }
  869. if (p_highest_ranked_peer != NULL)
  870. {
  871. *p_highest_ranked_peer = highest_ranked_peer;
  872. }
  873. if (p_highest_rank != NULL)
  874. {
  875. *p_highest_rank = highest_rank;
  876. }
  877. if (p_lowest_ranked_peer != NULL)
  878. {
  879. *p_lowest_ranked_peer = lowest_ranked_peer;
  880. }
  881. if (p_lowest_rank != NULL)
  882. {
  883. *p_lowest_rank = lowest_rank;
  884. }
  885. }
  886. else
  887. {
  888. NRF_LOG_ERROR("Could not retreive ranks. pdb_peer_data_load() returned %s. peer_id: %d",
  889. nrf_strerror_get(err_code),
  890. peer_id);
  891. err_code = NRF_ERROR_INTERNAL;
  892. }
  893. return err_code;
  894. #endif
  895. }
  896. #if PM_PEER_RANKS_ENABLED == 1
  897. /**@brief Function for initializing peer rank functionality.
  898. */
  899. static void rank_init(void)
  900. {
  901. rank_vars_update();
  902. }
  903. #endif
  904. ret_code_t pm_peer_rank_highest(pm_peer_id_t peer_id)
  905. {
  906. #if PM_PEER_RANKS_ENABLED == 0
  907. return NRF_ERROR_NOT_SUPPORTED;
  908. #else
  909. VERIFY_MODULE_INITIALIZED();
  910. ret_code_t err_code;
  911. //lint -save -e65 -e64
  912. pm_peer_data_flash_t peer_data = {.length_words = BYTES_TO_WORDS(sizeof(m_current_highest_peer_rank)),
  913. .data_id = PM_PEER_DATA_ID_PEER_RANK,
  914. .p_peer_rank = &m_current_highest_peer_rank};
  915. //lint -restore
  916. if (!m_peer_rank_initialized)
  917. {
  918. rank_init();
  919. }
  920. if (!m_peer_rank_initialized || (m_peer_rank_token != PM_STORE_TOKEN_INVALID))
  921. {
  922. err_code = NRF_ERROR_BUSY;
  923. }
  924. else
  925. {
  926. if ((peer_id == m_highest_ranked_peer) && (m_current_highest_peer_rank > 0))
  927. {
  928. pm_evt_t pm_evt;
  929. // The reported peer is already regarded as highest (provided it has an index at all)
  930. err_code = NRF_SUCCESS;
  931. memset(&pm_evt, 0, sizeof(pm_evt));
  932. pm_evt.evt_id = PM_EVT_PEER_DATA_UPDATE_SUCCEEDED;
  933. pm_evt.conn_handle = im_conn_handle_get(peer_id);
  934. pm_evt.peer_id = peer_id;
  935. pm_evt.params.peer_data_update_succeeded.data_id = PM_PEER_DATA_ID_PEER_RANK;
  936. pm_evt.params.peer_data_update_succeeded.action = PM_PEER_DATA_OP_UPDATE;
  937. pm_evt.params.peer_data_update_succeeded.token = PM_STORE_TOKEN_INVALID;
  938. pm_evt.params.peer_data_update_succeeded.flash_changed = false;
  939. evt_send(&pm_evt);
  940. }
  941. else
  942. {
  943. if (m_current_highest_peer_rank == UINT32_MAX)
  944. {
  945. err_code = NRF_ERROR_RESOURCES;
  946. }
  947. else
  948. {
  949. m_current_highest_peer_rank += 1;
  950. err_code = pds_peer_data_store(peer_id, &peer_data, &m_peer_rank_token);
  951. if (err_code != NRF_SUCCESS)
  952. {
  953. m_peer_rank_token = PM_STORE_TOKEN_INVALID;
  954. m_current_highest_peer_rank -= 1;
  955. if ((err_code != NRF_ERROR_BUSY) &&
  956. (err_code != NRF_ERROR_STORAGE_FULL) &&
  957. (err_code != NRF_ERROR_INVALID_PARAM)) // Assume INVALID_PARAM refers to peer_id, not data_id.
  958. {
  959. NRF_LOG_ERROR("Could not update rank. pdb_raw_store() returned %s. "\
  960. "peer_id: %d",
  961. nrf_strerror_get(err_code),
  962. peer_id);
  963. err_code = NRF_ERROR_INTERNAL;
  964. }
  965. }
  966. }
  967. }
  968. }
  969. return err_code;
  970. #endif
  971. }
  972. #endif // NRF_MODULE_ENABLED(PEER_MANAGER)