peer_manager.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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. /**
  41. * @file peer_manager.h
  42. *
  43. * @defgroup peer_manager Peer Manager
  44. * @ingroup ble_sdk_lib
  45. * @{
  46. * @brief Module for managing BLE bonding, which includes controlling encryption and pairing
  47. * procedures as well as persistently storing different pieces of data that must be stored
  48. * when bonded.
  49. *
  50. * @details The API consists of functions for configuring the pairing and encryption behavior of the
  51. * device and functions for manipulating the stored data.
  52. *
  53. * This module uses Flash Data Storage (FDS) to interface with persistent storage. The
  54. * Peer Manager needs exclusive use of certain FDS file IDs and record keys. See
  55. * @ref lib_fds_functionality_keys for more information.
  56. */
  57. #ifndef PEER_MANAGER_H__
  58. #define PEER_MANAGER_H__
  59. #include <stdint.h>
  60. #include <stdbool.h>
  61. #include "sdk_common.h"
  62. #include "ble.h"
  63. #include "ble_gap.h"
  64. #include "peer_manager_types.h"
  65. #include "peer_database.h"
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. /**@brief Peer list filtrations. They determine which peer ID will be added to list.
  70. */
  71. typedef enum
  72. {
  73. PM_PEER_ID_LIST_ALL_ID, /**< Add all peers. */
  74. PM_PEER_ID_LIST_SKIP_NO_ID_ADDR = BIT_0, /**< Add only peers with an ID address (static address). */
  75. PM_PEER_ID_LIST_SKIP_NO_IRK = BIT_1, /**< Add only peers with a valid IRK. This implies @ref PM_PEER_ID_LIST_SKIP_NO_ID_ADDR, since all peers with IRKs have ID addresses. */
  76. PM_PEER_ID_LIST_SKIP_NO_CAR = BIT_2, /**< Add only peers with Central Address Resolution characteristic set to 0. */
  77. PM_PEER_ID_LIST_SKIP_ALL = PM_PEER_ID_LIST_SKIP_NO_IRK | /**< All above filters applied. */
  78. PM_PEER_ID_LIST_SKIP_NO_CAR
  79. } pm_peer_id_list_skip_t;
  80. /**@brief Function for initializing the Peer Manager.
  81. *
  82. * @details You must initialize the Peer Manager before you can call any other Peer Manager
  83. * functions.
  84. *
  85. * @retval NRF_SUCCESS If initialization was successful.
  86. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  87. */
  88. ret_code_t pm_init(void);
  89. /**@brief Function for registering an event handler with the Peer Manager.
  90. *
  91. * @param[in] event_handler Callback for events from the @ref peer_manager module. @p event_handler
  92. * is called for every event that the Peer Manager sends after this
  93. * function is called.
  94. *
  95. * @retval NRF_SUCCESS If initialization was successful.
  96. * @retval NRF_ERROR_NULL If @p event_handler was NULL.
  97. * @retval NRF_ERROR_NO_MEM If no more registrations can happen.
  98. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  99. */
  100. ret_code_t pm_register(pm_evt_handler_t event_handler);
  101. /**@brief Function for providing pairing and bonding parameters to use for pairing procedures.
  102. *
  103. * @details Until this function is called, all bonding procedures that are initiated by the
  104. * peer are rejected.
  105. *
  106. * This function can be called multiple times with different parameters, even with NULL as
  107. * @p p_sec_params, in which case the Peer Manager starts rejecting all procedures again.
  108. *
  109. * @param[in] p_sec_params Security parameters to be used for subsequent security procedures.
  110. *
  111. * @retval NRF_SUCCESS If the parameters were set successfully.
  112. * @retval NRF_ERROR_INVALID_PARAM If the combination of parameters is invalid.
  113. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  114. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  115. */
  116. ret_code_t pm_sec_params_set(ble_gap_sec_params_t * p_sec_params);
  117. /**@brief Function for establishing encryption on a connection, and optionally establishing a bond.
  118. *
  119. * @details This function attempts to secure the link that is specified by @p conn_handle. It uses
  120. * the parameters that were previously provided in a call to @ref pm_sec_params_set.
  121. *
  122. * If the connection is a master connection, calling this function starts a security
  123. * procedure on the link. If we have keys from a previous bonding procedure with this peer
  124. * and the keys meet the security requirements in the currently active security parameters,
  125. * the function attempts to establish encryption with the existing keys. If no key exists,
  126. * the function attempts to perform pairing and bonding according to the currently active
  127. * security parameters.
  128. *
  129. * If the function completes successfully, a @ref PM_EVT_CONN_SEC_START event is sent.
  130. * The procedure might be queued, in which case the @ref PM_EVT_CONN_SEC_START event is
  131. * delayed until the procedure is initiated in the SoftDevice.
  132. *
  133. * If the connection is a slave connection, the function sends a security request to
  134. * the peer (master). It is up to the peer then to initiate pairing or encryption.
  135. * If the peer ignores the request, a @ref BLE_GAP_EVT_AUTH_STATUS event occurs
  136. * with the status @ref BLE_GAP_SEC_STATUS_TIMEOUT. Otherwise, the peer initiates
  137. * security, in which case things happen as if the peer had initiated security itself.
  138. * See @ref PM_EVT_CONN_SEC_START for information about peer-initiated security.
  139. *
  140. * @param[in] conn_handle Connection handle of the link as provided by the SoftDevice.
  141. * @param[in] force_repairing Whether to force a pairing procedure even if there is an existing
  142. * encryption key. This argument is relevant only for
  143. * the central role. Recommended value: false.
  144. *
  145. * @retval NRF_SUCCESS If the operation completed successfully.
  146. * @retval NRF_ERROR_BUSY If a security procedure is already in progress on the link,
  147. * or if the link is disconnecting or disconnected.
  148. * @retval NRF_ERROR_TIMEOUT If there was an SMP time-out, so that no more security
  149. * operations can be performed on this link.
  150. * @retval BLE_ERROR_INVALID_CONN_HANDLE If the connection handle is invalid.
  151. * @retval NRF_ERROR_NOT_FOUND If the security parameters have not been set, either by
  152. * @ref pm_sec_params_set or by @ref pm_conn_sec_params_reply.
  153. * @retval NRF_ERROR_INVALID_DATA If the peer is bonded, but no LTK was found in the stored
  154. * bonding data. Repairing was not requested.
  155. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  156. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  157. */
  158. ret_code_t pm_conn_secure(uint16_t conn_handle, bool force_repairing);
  159. /**@brief Function for providing security configuration for a link.
  160. *
  161. * @details This function is optional, and must be called in reply to a @ref
  162. * PM_EVT_CONN_SEC_CONFIG_REQ event, before the Peer Manager event handler returns. If it
  163. * is not called in time, a default configuration is used. See @ref pm_conn_sec_config_t
  164. * for the value of the default.
  165. *
  166. * @param[in] conn_handle The connection to set the configuration for.
  167. * @param[in] p_conn_sec_config The configuration.
  168. */
  169. void pm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t * p_conn_sec_config);
  170. /**@brief Function for providing security parameters for a link.
  171. *
  172. * @details This function is optional, and must be called in reply to a @ref
  173. * PM_EVT_CONN_SEC_PARAMS_REQ event, before the Peer Manager event handler returns. If it
  174. * is not called in time, the parameters given in @ref pm_sec_params_set are used.
  175. *
  176. * @param[in] conn_handle The connection to set the parameters for.
  177. * @param[in] p_sec_params The parameters. If NULL, the security procedure is rejected.
  178. * @param[in] p_context The context found in the request event that this function replies to.
  179. *
  180. * @retval NRF_SUCCESS Successful reply.
  181. * @retval NRF_ERROR_NULL p_sec_params or p_context was null.
  182. * @retval NRF_ERROR_INVALID_PARAM Value of p_sec_params was invalid.
  183. * @retval NRF_ERROR_INVALID_STATE This module is not initialized.
  184. */
  185. ret_code_t pm_conn_sec_params_reply(uint16_t conn_handle,
  186. ble_gap_sec_params_t * p_sec_params,
  187. void const * p_context);
  188. /**@brief Function for manually informing that the local database has changed.
  189. *
  190. * @details This function sends a service changed indication to all bonded and/or connected peers
  191. * that subscribe to this indication. If a bonded peer is not connected, the indication is
  192. * sent when it reconnects. Every time an indication is sent, a @ref
  193. * PM_EVT_SERVICE_CHANGED_IND_SENT event occurs, followed by a @ref
  194. * PM_EVT_SERVICE_CHANGED_IND_CONFIRMED when the peer sends its confirmation. Peers that
  195. * are not subscribed to the service changed indication when this function is called do not
  196. * receive an indication, and no events are sent to the user. Likewise, if the service
  197. * changed characteristic is not present in the local database, or if the @ref
  198. * PM_SERVICE_CHANGED_ENABLED is set to 0, no indications are sent peers, and no events are
  199. * sent to the user.
  200. */
  201. void pm_local_database_has_changed(void);
  202. /**@brief Function for getting the security status of a connection.
  203. *
  204. * @param[in] conn_handle Connection handle of the link as provided by the SoftDevice.
  205. * @param[out] p_conn_sec_status Security status of the link.
  206. *
  207. * @retval NRF_SUCCESS If pairing was initiated successfully.
  208. * @retval BLE_ERROR_INVALID_CONN_HANDLE If the connection handle is invalid.
  209. * @retval NRF_ERROR_NULL If @p p_conn_sec_status was NULL.
  210. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  211. */
  212. ret_code_t pm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t * p_conn_sec_status);
  213. /**@brief Function for comparing the security status of a connection against a baseline.
  214. *
  215. * @param[in] conn_handle Connection handle of the link as provided by the SoftDevice.
  216. * @param[out] p_sec_status_req Target baseline security status to compare against.
  217. *
  218. * @retval true If the security status of the connection matches or exceeds the baseline on all
  219. * points.
  220. * @retval false If the security status of the connection does not fulfil the baseline, or could
  221. * not be retrieved.
  222. */
  223. bool pm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t * p_sec_status_req);
  224. /**@brief Experimental function for specifying the public key to use for LESC operations.
  225. *
  226. * @details This function can be called multiple times. The specified public key will be used for
  227. * all subsequent LESC (LE Secure Connections) operations until the next time this function
  228. * is called.
  229. *
  230. * @note The key must continue to reside in application memory as it is not copied by Peer Manager.
  231. *
  232. * @note This function is deprecated. LESC keys are now handled internally if @ref PM_LESC_ENABLED
  233. * is true. If @ref PM_LESC_ENABLED is false, this function works as before.
  234. *
  235. * @param[in] p_public_key The public key to use for all subsequent LESC operations.
  236. *
  237. * @retval NRF_SUCCESS If pairing was initiated successfully.
  238. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  239. * @retval NRF_ERROR_FORBIDDEN If LESC module support is enabled (see @ref PM_LESC_ENABLED).
  240. */
  241. ret_code_t pm_lesc_public_key_set(ble_gap_lesc_p256_pk_t * p_public_key);
  242. /**@brief Function for setting or clearing the whitelist.
  243. *
  244. * When using the S13x SoftDevice v3.x, this function sets or clears the whitelist.
  245. * When using the S13x SoftDevice v2.x, this function caches a list of
  246. * peers that can be retrieved later by @ref pm_whitelist_get to pass to the @ref lib_ble_advertising.
  247. *
  248. * To clear the current whitelist, pass either NULL as @p p_peers or zero as @p peer_cnt.
  249. *
  250. * @param[in] p_peers The peers to add to the whitelist. Pass NULL to clear the current whitelist.
  251. * @param[in] peer_cnt The number of peers to add to the whitelist. The number must not be greater than
  252. * @ref BLE_GAP_WHITELIST_ADDR_MAX_COUNT. Pass zero to clear the current
  253. * whitelist.
  254. *
  255. * @retval NRF_SUCCESS If the whitelist was successfully set or cleared.
  256. * @retval BLE_GAP_ERROR_WHITELIST_IN_USE If a whitelist is already in use and cannot be set.
  257. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If a peer in @p p_peers has an address that cannot
  258. * be used for whitelisting.
  259. * @retval NRF_ERROR_NOT_FOUND If any of the peers in @p p_peers cannot be found.
  260. * @retval NRF_ERROR_DATA_SIZE If @p peer_cnt is greater than
  261. * @ref BLE_GAP_WHITELIST_ADDR_MAX_COUNT.
  262. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  263. */
  264. ret_code_t pm_whitelist_set(pm_peer_id_t const * p_peers,
  265. uint32_t peer_cnt);
  266. /**@brief Function for retrieving the previously set whitelist.
  267. *
  268. * The function retrieves the whitelist of GAP addresses and IRKs that was
  269. * previously set by @ref pm_whitelist_set.
  270. *
  271. * To retrieve only GAP addresses or only IRKs, provide only one of the
  272. * buffers. If a buffer is provided, its size must be specified.
  273. *
  274. * @param[out] p_addrs The buffer where to store GAP addresses. Pass NULL to retrieve
  275. * only IRKs (in that case, @p p_irks must not be NULL).
  276. * @param[in,out] p_addr_cnt In: The size of the @p p_addrs buffer.
  277. * May be NULL if and only if @p p_addrs is NULL.
  278. * Out: The number of GAP addresses copied into the buffer.
  279. * If @p p_addrs is NULL, this parameter remains unchanged.
  280. * @param[out] p_irks The buffer where to store IRKs. Pass NULL to retrieve
  281. * only GAP addresses (in that case, @p p_addrs must not NULL).
  282. * @param[in,out] p_irk_cnt In: The size of the @p p_irks buffer.
  283. * May be NULL if and only if @p p_irks is NULL.
  284. * Out: The number of IRKs copied into the buffer.
  285. * If @p p_irks is NULL, this paramater remains unchanged.
  286. *
  287. * @retval NRF_SUCCESS If the whitelist was successfully retrieved.
  288. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If a peer has an address that cannot be used for
  289. * whitelisting (this error can occur only
  290. * when using the S13x SoftDevice v2.x).
  291. * @retval NRF_ERROR_NULL If a required parameter is NULL.
  292. * @retval NRF_ERROR_NO_MEM If the provided buffers are too small.
  293. * @retval NRF_ERROR_NOT_FOUND If the data for any of the cached whitelisted peers
  294. * cannot be found. It might have been deleted.
  295. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  296. */
  297. ret_code_t pm_whitelist_get(ble_gap_addr_t * p_addrs,
  298. uint32_t * p_addr_cnt,
  299. ble_gap_irk_t * p_irks,
  300. uint32_t * p_irk_cnt);
  301. /**@brief Function for setting and clearing the device identities list.
  302. *
  303. * @note When entering directed advertising, make sure the active identities list does not contain
  304. * peers that have no Central Address Resolution. See @ref pm_peer_id_list with skip_id
  305. * @ref PM_PEER_ID_LIST_SKIP_NO_CAR.
  306. *
  307. * @param[in] p_peers The peers to add to the device identities list. Pass NULL to clear
  308. * the device identities list.
  309. * @param[in] peer_cnt The number of peers. Pass zero to clear the device identities list.
  310. *
  311. * @retval NRF_SUCCESS If the device identities list was successfully
  312. * set or cleared.
  313. * @retval NRF_ERROR_NOT_FOUND If a peer is invalid or its data could not
  314. * be found in flash.
  315. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If a peer has an address that cannot be
  316. * used for whitelisting.
  317. * @retval BLE_ERROR_GAP_DEVICE_IDENTITIES_IN_USE If the device identities list is in use and
  318. * cannot be set.
  319. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  320. * @retval NRF_ERROR_NOT_SUPPORTED If using a SoftDevice that does not support
  321. * device identities, e.g. S130 v2.0.
  322. */
  323. ret_code_t pm_device_identities_list_set(pm_peer_id_t const * p_peers,
  324. uint32_t peer_cnt);
  325. /**@brief Function for setting the local <em>Bluetooth</em> identity address.
  326. *
  327. * @details The local <em>Bluetooth</em> identity address is the address that identifies the device
  328. * to other peers. The address type must be either @ref BLE_GAP_ADDR_TYPE_PUBLIC or @ref
  329. * BLE_GAP_ADDR_TYPE_RANDOM_STATIC. The identity address cannot be changed while roles are running.
  330. *
  331. * The SoftDevice sets a default address of type @ref BLE_GAP_ADDR_TYPE_RANDOM_STATIC when it is
  332. * enabled. This default address is a random number that is populated during the IC manufacturing
  333. * process. It remains unchanged for the lifetime of each IC, but the application can use this
  334. * function to assign a different identity address.
  335. *
  336. * The identity address is distributed to the peer during bonding. Changing the identity address
  337. * means bonded devices might not recognize us.
  338. *
  339. * @note The SoftDevice functions @ref sd_ble_gap_addr_set and @ref sd_ble_gap_privacy_set must not
  340. * be called when using the Peer Manager. Use the Peer Manager equivalents instead.
  341. *
  342. * @param[in] p_addr The GAP address to be set.
  343. *
  344. * @retval NRF_SUCCESS If the identity address was set successfully.
  345. * @retval NRF_ERROR_NULL If @p p_addr is NULL.
  346. * @retval NRF_ERROR_INVALID_ADDR If the @p p_addr pointer is invalid.
  347. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the BLE address is invalid.
  348. * @retval NRF_ERROR_BUSY If the SoftDevice was busy. Process SoftDevice events
  349. * and retry.
  350. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized or if this function
  351. * was called while advertising, scanning, or while connected.
  352. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  353. */
  354. ret_code_t pm_id_addr_set(ble_gap_addr_t const * p_addr);
  355. /**@brief Function for retrieving the local <em>Bluetooth</em> identity address.
  356. *
  357. * This function always returns the identity address, irrespective of the privacy settings.
  358. * This means that the address type will always be either @ref BLE_GAP_ADDR_TYPE_PUBLIC or @ref
  359. * BLE_GAP_ADDR_TYPE_RANDOM_STATIC.
  360. *
  361. * @param[out] p_addr Pointer to the address structure to be filled in.
  362. *
  363. * @retval NRF_SUCCESS If the address was retrieved successfully.
  364. * @retval NRF_ERROR_NULL If @p p_addr is NULL.
  365. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  366. */
  367. ret_code_t pm_id_addr_get(ble_gap_addr_t * p_addr);
  368. /**@brief Function for configuring privacy settings.
  369. *
  370. * The privacy settings cannot be configured while advertising, scanning, or while in a connection.
  371. *
  372. * @note The SoftDevice functions @ref sd_ble_gap_addr_set
  373. * and @ref sd_ble_gap_privacy_set must not be called when using the Peer Manager.
  374. * Use this function instead.
  375. *
  376. * @param[in] p_privacy_params Privacy settings.
  377. *
  378. * @retval NRF_SUCCESS If the privacy settings were configured successfully.
  379. * @retval NRF_ERROR_NULL If @p p_privacy_params is NULL.
  380. * @retval NRF_ERROR_BUSY If the operation could not be performed at this time.
  381. * Process SoftDevice events and retry.
  382. * @retval NRF_ERROR_INVALID_PARAM If the address type is invalid.
  383. * @retval NRF_ERROR_INVALID_STATE If this function is called while BLE roles using
  384. * privacy are enabled.
  385. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  386. */
  387. ret_code_t pm_privacy_set(pm_privacy_params_t const * p_privacy_params);
  388. /**@brief Function for retrieving privacy settings.
  389. *
  390. * The privacy settings that are returned include the current IRK as well.
  391. *
  392. * @param[out] p_privacy_params Privacy settings.
  393. *
  394. * @retval NRF_SUCCESS If the privacy settings were retrieved successfully.
  395. * @retval NRF_ERROR_NULL If @p p_privacy_params or @p p_privacy_params->p_device_irk is
  396. * NULL.
  397. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  398. */
  399. ret_code_t pm_privacy_get(pm_privacy_params_t * p_privacy_params);
  400. /**@brief Function for resolving a resolvable address with an identity resolution key (IRK).
  401. *
  402. * @param[in] p_addr A private random resolvable address.
  403. * @param[in] p_irk An identity resolution key (IRK).
  404. *
  405. * @retval true The IRK used matched the one used to create the address.
  406. * @retval false The IRK used did not match the one used to create the address, or an argument was
  407. * NULL or invalid.
  408. */
  409. bool pm_address_resolve(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * p_irk);
  410. /**@brief Function for getting the connection handle of the connection with a bonded peer.
  411. *
  412. * @param[in] peer_id The peer ID of the bonded peer.
  413. * @param[out] p_conn_handle Connection handle, or @ref BLE_CONN_HANDLE_INVALID if the peer
  414. * is not connected.
  415. *
  416. * @retval NRF_SUCCESS If the connection handle was retrieved successfully.
  417. * @retval NRF_ERROR_NULL If @p p_conn_handle was NULL.
  418. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  419. */
  420. ret_code_t pm_conn_handle_get(pm_peer_id_t peer_id, uint16_t * p_conn_handle);
  421. /**@brief Function for retrieving the ID of a peer, given its connection handle.
  422. *
  423. * @param[in] conn_handle The connection handle of the peer.
  424. * @param[out] p_peer_id The peer ID, or @ref PM_PEER_ID_INVALID if the peer is not bonded or
  425. * @p conn_handle does not refer to a valid connection.
  426. *
  427. * @retval NRF_SUCCESS If the peer ID was retrieved successfully.
  428. * @retval NRF_ERROR_NULL If @p p_peer_id was NULL.
  429. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  430. */
  431. ret_code_t pm_peer_id_get(uint16_t conn_handle, pm_peer_id_t * p_peer_id);
  432. /**@brief Function for retrieving a filtered list of peer IDs.
  433. *
  434. * @details This function starts searching from @p first_peer_id. IDs ordering
  435. * is the same as for @ref pm_next_peer_id_get(). If the first_peer_id
  436. * is @ref PM_PEER_ID_INVALID, the function starts searching from the first ID.
  437. * The function looks for the ID's number specified by @p p_list_size. Only those IDs that
  438. * match @p skip_id are added to the list. The number of returned elements is determined
  439. * by @p p_list_size.
  440. *
  441. * @warning The size of the @p p_peer_list buffer must be equal or greater than @p p_list_size.
  442. *
  443. * @param[out] p_peer_list Pointer to peer IDs list buffer.
  444. * @param[in,out] p_list_size The amount of IDs to return / The number of returned IDs.
  445. * @param[in] first_peer_id The first ID from which the search begins. IDs ordering
  446. * is the same as for @ref pm_next_peer_id_get()
  447. * @param[in] skip_id It determines which peer ID will be added to list.
  448. *
  449. * @retval NRF_SUCCESS If the ID list has been filled out.
  450. * @retval NRF_ERROR_INVALID_PARAM If @p skip_id was invalid.
  451. * @retval NRF_ERROR_NULL If peer_list or list_size was NULL.
  452. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  453. */
  454. ret_code_t pm_peer_id_list(pm_peer_id_t * p_peer_list,
  455. uint32_t * const p_list_size,
  456. pm_peer_id_t first_peer_id,
  457. pm_peer_id_list_skip_t skip_id);
  458. /**@brief Function for getting the next peer ID in the sequence of all used peer IDs.
  459. *
  460. * @details This function can be used to loop through all used peer IDs. The order in which
  461. * peer IDs are returned should be considered unpredictable. @ref PM_PEER_ID_INVALID
  462. * is considered to be before the first and after the last used peer ID.
  463. *
  464. * @details To loop through all peer IDs exactly once, use the following constuct:
  465. * @code{c}
  466. * pm_peer_id_t current_peer_id = pm_next_peer_id_get(PM_PEER_ID_INVALID);
  467. * while (current_peer_id != PM_PEER_ID_INVALID)
  468. * {
  469. * // Do something with current_peer_id.
  470. * current_peer_id = pm_next_peer_id_get(current_peer_id)
  471. * }
  472. * @endcode
  473. *
  474. * @note This function does not report peer IDs that are pending deletion.
  475. *
  476. * @param[in] prev_peer_id The previous peer ID.
  477. *
  478. * @return The next peer ID. If @p prev_peer_id was @ref PM_PEER_ID_INVALID, the
  479. * next peer ID is the first used peer ID. If @p prev_peer_id was the last
  480. * used peer ID, the function returns @ref PM_PEER_ID_INVALID.
  481. */
  482. pm_peer_id_t pm_next_peer_id_get(pm_peer_id_t prev_peer_id);
  483. /**@brief Function for querying the number of valid peer IDs that are available.
  484. *
  485. * @details This function returns the number of peers for which there is data in persistent storage.
  486. *
  487. * @return The number of valid peer IDs.
  488. */
  489. uint32_t pm_peer_count(void);
  490. /**@anchor PM_PEER_DATA_FUNCTIONS
  491. * @name Functions (Peer Data)
  492. * Functions for manipulating peer data.
  493. * @{
  494. */
  495. /**
  496. * @{
  497. */
  498. /**@brief Function for retrieving stored data of a peer.
  499. *
  500. * @note The length of the provided buffer must be a multiple of 4.
  501. *
  502. * @param[in] peer_id Peer ID to get data for.
  503. * @param[in] data_id Which type of data to read.
  504. * @param[out] p_data Where to put the retrieved data. The documentation for
  505. * @ref pm_peer_data_id_t specifies what data type each data ID is stored as.
  506. * @param[in,out] p_len In: The length in bytes of @p p_data.
  507. * Out: The length in bytes of the read data, if the read was successful.
  508. *
  509. * @retval NRF_SUCCESS If the data was read successfully.
  510. * @retval NRF_ERROR_INVALID_PARAM If the data type or the peer ID was invalid or unallocated.
  511. * @retval NRF_ERROR_NULL If a pointer parameter was NULL.
  512. * @retval NRF_ERROR_NOT_FOUND If no stored data was found for this peer ID/data ID combination.
  513. * @retval NRF_ERROR_DATA_SIZE If the provided buffer was not large enough. The data is still
  514. * copied, filling the provided buffer. Note that this error can
  515. * occur even if loading the same size as was stored, because the
  516. * underlying layers round the length up to the nearest word (4 bytes)
  517. * when storing.
  518. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  519. */
  520. ret_code_t pm_peer_data_load(pm_peer_id_t peer_id,
  521. pm_peer_data_id_t data_id,
  522. void * p_data,
  523. uint32_t * p_len);
  524. /**@brief Function for reading a peer's bonding data (@ref PM_PEER_DATA_ID_BONDING).
  525. * @details See @ref pm_peer_data_load for parameters and return values. */
  526. ret_code_t pm_peer_data_bonding_load(pm_peer_id_t peer_id,
  527. pm_peer_data_bonding_t * p_data);
  528. /**@brief Function for reading a peer's remote DB values. (@ref PM_PEER_DATA_ID_GATT_REMOTE).
  529. * @details See @ref pm_peer_data_load for parameters and return values. */
  530. ret_code_t pm_peer_data_remote_db_load(pm_peer_id_t peer_id,
  531. ble_gatt_db_srv_t * p_data,
  532. uint32_t * p_len);
  533. /**@brief Function for reading a peer's application data. (@ref PM_PEER_DATA_ID_APPLICATION).
  534. * @details See @ref pm_peer_data_load for parameters and return values. */
  535. ret_code_t pm_peer_data_app_data_load(pm_peer_id_t peer_id,
  536. void * p_data,
  537. uint32_t * p_len);
  538. /** @}*/
  539. /**
  540. * @{
  541. */
  542. /**@brief Function for setting or updating stored data of a peer.
  543. *
  544. * @note Writing the data to persistent storage happens asynchronously. Therefore, the buffer
  545. * that contains the data must be kept alive until the operation has completed.
  546. *
  547. * @note The data written using this function might later be overwritten as a result of internal
  548. * operations in the Peer Manager. A Peer Manager event is sent each time data is updated,
  549. * regardless of whether the operation originated internally or from action by the user.
  550. * Data with @p data_id @ref PM_PEER_DATA_ID_GATT_REMOTE @ref PM_PEER_DATA_ID_APPLICATION is
  551. * never (over)written internally.
  552. *
  553. * @param[in] peer_id Peer ID to set data for.
  554. * @param[in] data_id Which type of data to set.
  555. * @param[in] p_data New value to set. The documentation for @ref pm_peer_data_id_t specifies
  556. * what data type each data ID should be stored as.
  557. * @param[in] len The length in bytes of @p p_data.
  558. * @param[out] p_token A token that identifies this particular store operation. The token can be
  559. * used to identify events that pertain to this operation. This parameter can
  560. * be NULL.
  561. *
  562. * @retval NRF_SUCCESS If the data is scheduled to be written to persistent storage.
  563. * @retval NRF_ERROR_NULL If @p p_data is NULL.
  564. * @retval NRF_ERROR_NOT_FOUND If no peer was found for the peer ID.
  565. * @retval NRF_ERROR_INVALID_ADDR If @p p_data is not word-aligned (4 bytes).
  566. * @retval NRF_ERROR_BUSY If the underlying flash handler is busy with other flash
  567. * operations. Try again after receiving a Peer Manager event.
  568. * @retval NRF_ERROR_STORAGE_FULL If there is not enough space in persistent storage.
  569. * @retval NRF_ERROR_FORBIDDEN If data ID is @ref PM_PEER_DATA_ID_BONDING and the new bonding
  570. * data also corresponds to another bonded peer. No data is written
  571. * so duplicate entries are avoided.
  572. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  573. */
  574. ret_code_t pm_peer_data_store(pm_peer_id_t peer_id,
  575. pm_peer_data_id_t data_id,
  576. void const * p_data,
  577. uint32_t len,
  578. pm_store_token_t * p_token);
  579. /**@brief Function for setting or updating a peer's bonding data (@ref PM_PEER_DATA_ID_BONDING).
  580. * @details See @ref pm_peer_data_store for parameters and return values. */
  581. ret_code_t pm_peer_data_bonding_store(pm_peer_id_t peer_id,
  582. pm_peer_data_bonding_t const * p_data,
  583. pm_store_token_t * p_token);
  584. /**@brief Function for setting or updating a peer's remote DB values. (@ref PM_PEER_DATA_ID_GATT_REMOTE).
  585. * @details See @ref pm_peer_data_store for parameters and return values. */
  586. ret_code_t pm_peer_data_remote_db_store(pm_peer_id_t peer_id,
  587. ble_gatt_db_srv_t const * p_data,
  588. uint32_t len,
  589. pm_store_token_t * p_token);
  590. /**@brief Function for setting or updating a peer's application data. (@ref PM_PEER_DATA_ID_APPLICATION).
  591. * @details See @ref pm_peer_data_store for parameters and return values. */
  592. ret_code_t pm_peer_data_app_data_store(pm_peer_id_t peer_id,
  593. void const * p_data,
  594. uint32_t len,
  595. pm_store_token_t * p_token);
  596. /** @}*/
  597. /**
  598. * @{
  599. */
  600. /**@brief Function for deleting a peer's stored pieces of data.
  601. *
  602. * @details This function deletes specific data that is stored for a peer. Note that bonding data
  603. * cannot be cleared separately.
  604. *
  605. * To delete all data for a peer (including bonding data), use @ref pm_peer_delete.
  606. *
  607. * @note Clearing data in persistent storage happens asynchronously.
  608. *
  609. * @param[in] peer_id Peer ID to clear data for.
  610. * @param[in] data_id Which data to clear.
  611. *
  612. * @retval NRF_SUCCESS If the clear procedure was initiated successfully.
  613. * @retval NRF_ERROR_INVALID_PARAM If @p data_id was PM_PEER_DATA_ID_BONDING or invalid, or
  614. * @p peer_id was invalid.
  615. * @retval NRF_ERROR_NOT_FOUND If there was no data to clear for this peer ID/data ID combination.
  616. * @retval NRF_ERROR_BUSY If the underlying flash handler is busy with other flash
  617. * operations. Try again after receiving a Peer Manager event.
  618. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  619. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  620. */
  621. ret_code_t pm_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id);
  622. /**@brief Function for manually adding a peer to the persistent storage.
  623. *
  624. * @details This function allocates a new peer ID and stores bonding data for the new peer. The
  625. * bonding data is necessary to prevent ambiguity/inconsistency in peer data.
  626. *
  627. * @param[in] p_bonding_data The bonding data of the new peer (must contain a public/static
  628. * address or a non-zero IRK).
  629. * @param[out] p_new_peer_id Peer ID for the new peer, or an existing peer if a match was found.
  630. * @param[out] p_token A token that identifies this particular store operation (storing the
  631. * bonding data). The token can be used to identify events that pertain
  632. * to this operation. This parameter can be NULL.
  633. *
  634. * @retval NRF_SUCCESS If the store operation for bonding data was initiated successfully.
  635. * @retval NRF_ERROR_NULL If @p p_bonding_data or @p p_new_peer_id is NULL.
  636. * @retval NRF_ERROR_INVALID_ADDR If @p p_bonding_data is not word-aligned (4 bytes).
  637. * @retval NRF_ERROR_STORAGE_FULL If there is not enough space in persistent storage.
  638. * @retval NRF_ERROR_NO_MEM If there are no more available peer IDs.
  639. * @retval NRF_ERROR_BUSY If the underlying flash filesystem is busy with other flash
  640. * operations. Try again after receiving a Peer Manager event.
  641. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  642. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  643. */
  644. ret_code_t pm_peer_new(pm_peer_id_t * p_new_peer_id,
  645. pm_peer_data_bonding_t * p_bonding_data,
  646. pm_store_token_t * p_token);
  647. /**@brief Function for freeing persistent storage for a peer.
  648. *
  649. * @details This function deletes every piece of data that is associated with the specified peer and
  650. * frees the peer ID to be used for another peer. The deletion happens asynchronously, and
  651. * the peer ID is not freed until the data is deleted. When the operation finishes, a @ref
  652. * PM_EVT_PEER_DELETE_SUCCEEDED or @ref PM_EVT_PEER_DELETE_FAILED event is sent.
  653. *
  654. * @warning Use this function only when not connected to or connectable for the peer that is being
  655. * deleted. If the peer is or becomes connected or data is manually written in flash during
  656. * this procedure (until the success or failure event happens), the behavior is undefined.
  657. *
  658. * @param[in] peer_id Peer ID to be freed and have all associated data deleted.
  659. *
  660. * @retval NRF_SUCCESS If the operation was initiated successfully.
  661. * @retval NRF_ERROR_INVALID_PARAM If the peer ID was not valid.
  662. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  663. */
  664. ret_code_t pm_peer_delete(pm_peer_id_t peer_id);
  665. /**@brief Function for deleting all data stored for all peers.
  666. *
  667. * @details This function sends either a @ref PM_EVT_PEERS_DELETE_SUCCEEDED or a @ref
  668. * PM_EVT_PEERS_DELETE_FAILED event. In addition, a @ref PM_EVT_PEER_DELETE_SUCCEEDED or
  669. * @ref PM_EVT_PEER_DELETE_FAILED event is sent for each deleted peer.
  670. *
  671. * @note When there is no peer data in flash the @ref PM_EVT_PEER_DELETE_SUCCEEDED event is sent synchronously.
  672. *
  673. * @warning Use this function only when not connected or connectable. If a peer is or becomes
  674. * connected or a @ref PM_PEER_DATA_FUNCTIONS function is used during this procedure (until
  675. * the success or failure event happens), the behavior is undefined.
  676. *
  677. * @retval NRF_SUCCESS If the deletion process was initiated successfully.
  678. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  679. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  680. */
  681. ret_code_t pm_peers_delete(void);
  682. /** @}*/
  683. /**
  684. * @{
  685. */
  686. /**@brief Function for finding the highest and lowest ranked peers.
  687. *
  688. * @details The rank is saved in persistent storage under the data ID @ref PM_PEER_DATA_ID_PEER_RANK.
  689. *
  690. * @details The interpretation of rank is up to the user, because the rank is only updated by
  691. * calling @ref pm_peer_rank_highest or by manipulating the value using a @ref
  692. * PM_PEER_DATA_FUNCTIONS function.
  693. *
  694. * @note Peers with no stored rank are not considered.
  695. * @note Any argument that is NULL is ignored.
  696. *
  697. * @param[out] p_highest_ranked_peer The peer ID with the highest rank of all peers, for example,
  698. * the most recently used peer.
  699. * @param[out] p_highest_rank The highest rank.
  700. * @param[out] p_lowest_ranked_peer The peer ID with the lowest rank of all peers, for example,
  701. * the least recently used peer.
  702. * @param[out] p_lowest_rank The lowest rank.
  703. *
  704. * @retval NRF_SUCCESS If the operation completed successfully.
  705. * @retval NRF_ERROR_NOT_FOUND If no peer with stored peer rank was found.
  706. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  707. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  708. * @retval NRF_ERROR_NOT_SUPPORTED If peer rank functionality has been disabled via the @ref
  709. * PM_PEER_RANKS_ENABLED configuration option.
  710. */
  711. ret_code_t pm_peer_ranks_get(pm_peer_id_t * p_highest_ranked_peer,
  712. uint32_t * p_highest_rank,
  713. pm_peer_id_t * p_lowest_ranked_peer,
  714. uint32_t * p_lowest_rank);
  715. /**@brief Function for updating the rank of a peer to be highest among all stored peers.
  716. *
  717. * @details If this function returns @ref NRF_SUCCESS, either a @ref PM_EVT_PEER_DATA_UPDATE_SUCCEEDED or a
  718. * @ref PM_EVT_PEER_DATA_UPDATE_FAILED event is sent with a @ref
  719. * PM_STORE_TOKEN_INVALID store token when the operation is complete. Until the operation
  720. * is complete, this function returns @ref NRF_ERROR_BUSY.
  721. *
  722. * When the operation is complete, the peer is the highest ranked peer as reported by
  723. * @ref pm_peer_ranks_get.
  724. *
  725. * @note The @ref PM_EVT_PEER_DATA_UPDATE_SUCCEEDED event can arrive before the function returns if the peer
  726. * is already ranked highest. In this case, the @ref pm_peer_data_update_succeeded_evt_t::flash_changed flag
  727. * in the event will be false.
  728. *
  729. * @param[in] peer_id The peer to rank highest.
  730. *
  731. * @retval NRF_SUCCESS If the peer's rank is, or will be updated to be highest.
  732. * @retval NRF_ERROR_INVALID_PARAM If @p peer_id is invalid, or doesn't exist in flash.
  733. * @retval NRF_ERROR_STORAGE_FULL If there is not enough space in persistent storage.
  734. * @retval NRF_ERROR_BUSY If the underlying flash handler is busy with other flash
  735. * operations, or if a previous call to this function has not
  736. * completed. Try again after receiving a Peer Manager event.
  737. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized.
  738. * @retval NRF_ERROR_RESOURCES If the highest rank is UINT32_MAX, so the new rank would wrap
  739. * around to 0. To fix this, manually update all ranks to smaller
  740. * values, while still keeping their order.
  741. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  742. * @retval NRF_ERROR_NOT_SUPPORTED If peer rank functionality has been disabled via the @ref
  743. * PM_PEER_RANKS_ENABLED configuration option.
  744. */
  745. ret_code_t pm_peer_rank_highest(pm_peer_id_t peer_id);
  746. /** @}*/
  747. /** @} */
  748. /** @} */
  749. #ifdef __cplusplus
  750. }
  751. #endif
  752. #endif // PEER_MANAGER_H__