id_manager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. #ifndef PEER_ID_MANAGER_H__
  41. #define PEER_ID_MANAGER_H__
  42. #include <stdint.h>
  43. #include "sdk_errors.h"
  44. #include "ble.h"
  45. #include "ble_gap.h"
  46. #include "peer_manager_types.h"
  47. #include "peer_manager_internal.h"
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /**
  52. * @cond NO_DOXYGEN
  53. * @defgroup id_manager ID Manager
  54. * @ingroup peer_manager
  55. * @{
  56. * @brief An internal module of @ref peer_manager. A module for keeping track of peer identities
  57. * (IRK and peer address).
  58. */
  59. /**@brief Function for dispatching SoftDevice events to the ID Manager module.
  60. *
  61. * @param[in] p_ble_evt The SoftDevice event.
  62. */
  63. void im_ble_evt_handler(ble_evt_t const * p_ble_evt);
  64. /**@brief Function for getting the corresponding peer ID from a connection handle.
  65. *
  66. * @param[in] conn_handle The connection handle.
  67. *
  68. * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved.
  69. */
  70. pm_peer_id_t im_peer_id_get_by_conn_handle(uint16_t conn_handle);
  71. /**@brief Function for getting the corresponding peer ID from a master ID (EDIV and rand).
  72. *
  73. * @param[in] p_master_id The master ID.
  74. *
  75. * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved.
  76. */
  77. pm_peer_id_t im_peer_id_get_by_master_id(ble_gap_master_id_t const * p_master_id);
  78. /**@brief Function for getting the corresponding connection handle from a peer ID.
  79. *
  80. * @param[in] peer_id The peer ID.
  81. *
  82. * @return The corresponding connection handle, or @ref BLE_CONN_HANDLE_INVALID if none could be
  83. * resolved. The conn_handle can refer to a recently disconnected connection.
  84. */
  85. uint16_t im_conn_handle_get(pm_peer_id_t peer_id);
  86. /**@brief Function for comparing two master ids
  87. * @note Two invalid master IDs will not match.
  88. *
  89. * @param[in] p_master_id1 First master id for comparison
  90. * @param[in] p_master_id2 Second master id for comparison
  91. *
  92. * @return True if the input matches, false if it does not.
  93. */
  94. bool im_master_ids_compare(ble_gap_master_id_t const * p_master_id1,
  95. ble_gap_master_id_t const * p_master_id2);
  96. /**@brief Function for getting the BLE address used by the peer when connecting.
  97. *
  98. * @param[in] conn_handle The connection handle.
  99. * @param[out] p_ble_addr The BLE address used by the peer when the connection specified by
  100. * conn_handle was established. Cannot be NULL.
  101. *
  102. * @retval NRF_SUCCESS The address was found and copied.
  103. * @retval BLE_ERROR_INVALID_CONN_HANDLE conn_handle does not refer to an active connection.
  104. */
  105. ret_code_t im_ble_addr_get(uint16_t conn_handle, ble_gap_addr_t * p_ble_addr);
  106. /**@brief Function for checking if a master ID is valid or invalid
  107. *
  108. * @param[in] p_master_id The master ID.
  109. *
  110. * @retval true The master id is valid.
  111. * @retval false The master id is invalid (i.e. all zeros).
  112. */
  113. bool im_master_id_is_valid(ble_gap_master_id_t const * p_master_id);
  114. /**@brief Function for checking if two pieces of bonding data correspond to the same peer.
  115. *
  116. * @param[in] p_bonding_data1 The first piece of bonding data to check.
  117. * @param[in] p_bonding_data2 The second piece of bonding data to check.
  118. *
  119. * @retval true The bonding data correspond to the same peer.
  120. * @retval false The bonding data do not correspond to the same peer.
  121. */
  122. bool im_is_duplicate_bonding_data(pm_peer_data_bonding_t const * p_bonding_data1,
  123. pm_peer_data_bonding_t const * p_bonding_data2);
  124. /**@brief Function for finding if we are already bonded to a peer.
  125. *
  126. * @param[in] p_bonding_data The bonding data to check.
  127. * @param[in] peer_id_skip Optional peer to ignore when searching for duplicates.
  128. *
  129. * @return An existing peer ID for the peer, or PM_PEER_ID_INVALID if none was found.
  130. */
  131. pm_peer_id_t im_find_duplicate_bonding_data(pm_peer_data_bonding_t const * p_bonding_data,
  132. pm_peer_id_t peer_id_skip);
  133. /**@brief Function for reporting that a new peer ID has been allocated for a specified connection.
  134. *
  135. * @param[in] conn_handle The connection.
  136. * @param[in] peer_id The new peer ID.
  137. */
  138. void im_new_peer_id(uint16_t conn_handle, pm_peer_id_t peer_id);
  139. /**@brief Function for deleting all of a peer's data from flash and disassociating it from any
  140. * connection handles it is associated with.
  141. *
  142. * @param[in] peer_id The peer to free.
  143. *
  144. * @return Any error code returned by @ref pdb_peer_free.
  145. */
  146. ret_code_t im_peer_free(pm_peer_id_t peer_id);
  147. /**@brief Function to set the local Bluetooth identity address.
  148. *
  149. * @details The local Bluetooth identity address is the address that identifies this device to other
  150. * peers. The address type must be either @ref BLE_GAP_ADDR_TYPE_PUBLIC or @ref
  151. * BLE_GAP_ADDR_TYPE_RANDOM_STATIC. The identity address cannot be changed while roles are
  152. * running.
  153. *
  154. * @note This address will be distributed to the peer during bonding.
  155. * If the address changes, the address stored in the peer device will not be valid and the
  156. * ability to reconnect using the old address will be lost.
  157. *
  158. * @note By default the SoftDevice will set an address of type @ref BLE_GAP_ADDR_TYPE_RANDOM_STATIC
  159. * upon being enabled. The address is a random number populated during the IC manufacturing
  160. * process and remains unchanged for the lifetime of each IC.
  161. *
  162. * @param[in] p_addr Pointer to address structure.
  163. *
  164. * @retval NRF_SUCCESS Address successfully set.
  165. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the GAP address is invalid.
  166. * @retval NRF_ERROR_BUSY Could not process at this time. Process SoftDevice events
  167. * and retry.
  168. * @retval NRF_ERROR_INVALID_STATE The identity address cannot be changed while advertising,
  169. * scanning, or while in a connection.
  170. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  171. */
  172. ret_code_t im_id_addr_set(ble_gap_addr_t const * p_addr);
  173. /**@brief Function to get the local Bluetooth identity address.
  174. *
  175. * @note This will always return the identity address irrespective of the privacy settings,
  176. * i.e. the address type will always be either @ref BLE_GAP_ADDR_TYPE_PUBLIC or @ref
  177. * BLE_GAP_ADDR_TYPE_RANDOM_STATIC.
  178. *
  179. * @param[out] p_addr Pointer to address structure to be filled in.
  180. *
  181. * @retval NRF_SUCCESS If the address was successfully retrieved.
  182. */
  183. ret_code_t im_id_addr_get(ble_gap_addr_t * p_addr);
  184. /**@brief Function to set privacy settings.
  185. *
  186. * @details Privacy settings cannot be set while advertising, scanning, or while in a connection.
  187. *
  188. * @param[in] p_privacy_params Privacy settings.
  189. *
  190. * @retval NRF_SUCCESS If privacy options were set successfully.
  191. * @retval NRF_ERROR_NULL If @p p_privacy_params is NULL.
  192. * @retval NRF_ERROR_INVALID_PARAM If the address type is not valid.
  193. * @retval NRF_ERROR_BUSY If the request could not be processed at this time.
  194. * Process SoftDevice events and retry.
  195. * @retval NRF_ERROR_INVALID_STATE Privacy settings cannot be changed while BLE roles using
  196. * privacy are enabled.
  197. */
  198. ret_code_t im_privacy_set(pm_privacy_params_t const * p_privacy_params);
  199. /**@brief Function to retrieve the current privacy settings.
  200. *
  201. * @details The privacy settings returned include the current device irk as well.
  202. *
  203. * @param[in] p_privacy_params Privacy settings.
  204. *
  205. * @retval NRF_SUCCESS Successfully retrieved privacy settings.
  206. * @retval NRF_ERROR_NULL @c p_privacy_params is NULL.
  207. * @retval NRF_ERROR_INTERNAL If an internal error occurred.
  208. */
  209. ret_code_t im_privacy_get(pm_privacy_params_t * p_privacy_params);
  210. /**@brief Function for resolving a resolvable address with an identity resolution key (IRK).
  211. *
  212. * @details This function will use the ECB peripheral to resolve a resolvable address.
  213. * This can be used to resolve the identity of a device distributing a random
  214. * resolvable address based on any IRKs you have received earlier. If an address is
  215. * resolved by an IRK, the device distributing the address must also know the IRK.
  216. *
  217. * @param[in] p_addr A random resolvable address.
  218. * @param[in] p_irk An identity resolution key (IRK).
  219. *
  220. * @retval true The irk used matched the one used to create the address.
  221. * @retval false The irk used did not match the one used to create the address, or an argument was
  222. * NULL.
  223. */
  224. bool im_address_resolve(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * p_irk);
  225. /**@brief Function for setting / clearing the whitelist.
  226. *
  227. * @param p_peers The peers to whitelist. Pass NULL to clear the whitelist.
  228. * @param peer_cnt The number of peers to whitelist. Pass zero to clear the whitelist.
  229. *
  230. * @retval NRF_SUCCESS If the whitelist was successfully set or cleared.
  231. * @retval BLE_GAP_ERROR_WHITELIST_IN_USE If a whitelist is in use.
  232. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If any peer has an address which can not be used
  233. * for whitelisting.
  234. * @retval NRF_ERROR_NOT_FOUND If any peer or its data could not be found.
  235. * @retval NRF_ERROR_DATA_SIZE If @p peer_cnt is greater than
  236. * @ref BLE_GAP_WHITELIST_ADDR_MAX_COUNT.
  237. */
  238. ret_code_t im_whitelist_set(pm_peer_id_t const * p_peers,
  239. uint32_t const peer_cnt);
  240. /**@brief Retrieves the current whitelist, set by a previous call to @ref im_whitelist_set.
  241. *
  242. * @param[out] A buffer where to copy the GAP addresses.
  243. * @param[inout] In: the size of the @p p_addrs buffer.
  244. * Out: the number of address copied into the buffer.
  245. * @param[out] A buffer where to copy the IRKs.
  246. * @param[inout] In: the size of the @p p_irks buffer.
  247. * Out: the number of IRKs copied into the buffer.
  248. *
  249. * @retval NRF_SUCCESS If the whitelist was successfully retrieved.
  250. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If any peer has an address which can not be used for
  251. * whitelisting.
  252. * @retval NRF_ERROR_NOT_FOUND If the data for any of the cached whitelisted peers
  253. * can not be found anymore. It might have been deleted in
  254. * the meanwhile.
  255. * @retval NRF_ERROR_NO_MEM If the provided buffers are too small.
  256. */
  257. ret_code_t im_whitelist_get(ble_gap_addr_t * p_addrs,
  258. uint32_t * p_addr_cnt,
  259. ble_gap_irk_t * p_irks,
  260. uint32_t * p_irk_cnt);
  261. /**@brief Set the device identities list.
  262. */
  263. ret_code_t im_device_identities_list_set(pm_peer_id_t const * p_peers,
  264. uint32_t peer_cnt);
  265. /** @}
  266. * @endcond
  267. */
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* PEER_ID_MANAGER_H__ */