peer_manager.h 43 KB

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