id_manager.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef PEER_ID_MANAGER_H__
  13. #define PEER_ID_MANAGER_H__
  14. #include <stdint.h>
  15. #include "sdk_errors.h"
  16. #include "ble.h"
  17. #include "ble_gap.h"
  18. #include "peer_manager_types.h"
  19. /**
  20. * @cond NO_DOXYGEN
  21. * @defgroup id_manager ID Manager
  22. * @ingroup peer_manager
  23. * @{
  24. * @brief An internal module of @ref peer_manager. A module for keeping track of peer identities
  25. * (IRK and peer address).
  26. */
  27. /**@brief Events that can come from the ID Manager module.
  28. */
  29. typedef enum
  30. {
  31. IM_EVT_DUPLICATE_ID, /**< The ID Manager module has detected that two stored peers represent the same peer. */
  32. IM_EVT_BONDED_PEER_CONNECTED, /**< A connected peer has been identified as one of the bonded peers. This can happen immediately on connection, or at a later time. */
  33. } im_evt_id_t;
  34. typedef struct
  35. {
  36. im_evt_id_t evt_id;
  37. uint16_t conn_handle;
  38. union
  39. {
  40. struct
  41. {
  42. pm_peer_id_t peer_id_1;
  43. pm_peer_id_t peer_id_2;
  44. } duplicate_id;
  45. } params;
  46. } im_evt_t;
  47. /**@brief Event handler for events from the ID Manager module.
  48. *
  49. * @param[in] p_event The event that has happened.
  50. */
  51. typedef void (*im_evt_handler_t)(im_evt_t const * p_event);
  52. /**@brief Function for registering for events from the ID Manager module.
  53. *
  54. * @note This will also initialize the module if needed.
  55. *
  56. * @param[in] evt_handler Callback for events from the ID Manager module.
  57. *
  58. * @retval NRF_SUCCESS Registration was successful.
  59. * @retval NRF_ERROR_NO_MEM No more registrations possible.
  60. * @retval NRF_ERROR_NULL evt_handler was NULL.
  61. */
  62. ret_code_t im_register(im_evt_handler_t evt_handler);
  63. /**@brief Function for dispatching SoftDevice events to the ID Manager module.
  64. *
  65. * @param[in] p_ble_evt The SoftDevice event.
  66. */
  67. void im_ble_evt_handler(ble_evt_t * p_ble_evt);
  68. /**@brief Function for getting the corresponding peer ID from a connection handle.
  69. *
  70. * @param[in] conn_handle The connection handle.
  71. *
  72. * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved.
  73. */
  74. pm_peer_id_t im_peer_id_get_by_conn_handle(uint16_t conn_handle);
  75. /**@brief Function for getting the corresponding peer ID from a master ID (EDIV and rand).
  76. *
  77. * @param[in] p_master_id The master ID.
  78. *
  79. * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved.
  80. */
  81. pm_peer_id_t im_peer_id_get_by_master_id(ble_gap_master_id_t * p_master_id);
  82. /**@brief Function for getting the corresponding peer ID from an IRK match index, see @ref
  83. * ble_gap_evt_connected_t.
  84. *
  85. * @param[in] irk_match_idx The IRK match index.
  86. *
  87. * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved.
  88. */
  89. pm_peer_id_t im_peer_id_get_by_irk_match_idx(uint8_t irk_match_idx);
  90. /**@brief Function for getting the corresponding connection handle from a peer ID.
  91. *
  92. * @param[in] peer_id The peer ID.
  93. *
  94. * @return The corresponding connection handle, or @ref BLE_CONN_HANDLE_INVALID if none could be
  95. * resolved.
  96. */
  97. uint16_t im_conn_handle_get(pm_peer_id_t peer_id);
  98. /**@brief Function for comparing two master ids
  99. * @note Two invalid master IDs will not match.
  100. *
  101. * @param[in] p_master_id1 First master id for comparison
  102. * @param[in] p_master_id2 Second master id for comparison
  103. *
  104. * @return True if the input matches, false if it does not.
  105. */
  106. bool im_master_ids_compare(ble_gap_master_id_t const * p_master_id1,
  107. ble_gap_master_id_t const * p_master_id2);
  108. /**@brief Function for getting the BLE address used by the peer when connecting.
  109. *
  110. * @param[in] conn_handle The connection handle.
  111. * @param[out] p_ble_addr The BLE address used by the peer when the connection specified by
  112. * conn_handle was established.
  113. *
  114. * @retval NRF_SUCCESS The address was found and copied.
  115. * @retval NRF_ERROR_INVALID_STATE Module not initialized.
  116. * @retval BLE_ERROR_CONN_HANDLE_INVALID conn_handle does not refer to an active connection.
  117. * @retval NRF_ERROR_NULL p_ble_addr was NULL.
  118. */
  119. ret_code_t im_ble_addr_get(uint16_t conn_handle, ble_gap_addr_t * p_ble_addr);
  120. /**@brief Function for checking whether a master ID is valid or invalid
  121. *
  122. * @param[in] p_master_id The master ID.
  123. *
  124. * @retval true The master id is valid.
  125. * @retval true The master id is invalid (i.e. all zeros).
  126. */
  127. bool im_master_id_is_valid(ble_gap_master_id_t const * p_master_id);
  128. /**@brief Function for reporting that a new peer ID has been allocated for a specified connection.
  129. *
  130. * @param[in] conn_handle The connection.
  131. * @param[in] peer_id The new peer ID.
  132. */
  133. void im_new_peer_id(uint16_t conn_handle, pm_peer_id_t peer_id);
  134. /**@brief Function for deleting all of a peer's data from flash and disassociating it from any
  135. * connection handles it is associated with.
  136. *
  137. * @param[in] peer_id The peer to free.
  138. *
  139. * @return Any error code returned by @ref pdb_peer_free.
  140. */
  141. ret_code_t im_peer_free(pm_peer_id_t peer_id);
  142. /**@brief Function for informing this module of what whitelist will be used.
  143. *
  144. * @details This function is meant to be used when the app wants to use a custom whitelist.
  145. * When using peer manager, this function must be used if a custom whitelist is used.
  146. * Only whitelisted IRKs are of any importance for this function.
  147. *
  148. * @note When using a whitelist, always use the whitelist created/set by the most recent
  149. * call to @ref im_whitelist_create or to this function, whichever happened most recently.
  150. * @note Do not call this function while scanning with another whitelist.
  151. * @note Do not add any irks to the whitelist that are not present in the bonding data of a peer in
  152. * the peer database.
  153. * @note The whitelist is not altered in any way by calling this function. Only the internal state
  154. * of the module is changed.
  155. *
  156. * @param[in] p_whitelist The whitelist.
  157. *
  158. * @retval NRF_SUCCESS Whitelist successfully set.
  159. * @retval NRF_ERROR_NULL p_whitelist was NULL.
  160. * @retval NRF_ERROR_NOT_FOUND One or more of the whitelists irks was not found in the peer_database.
  161. */
  162. ret_code_t im_whitelist_custom(ble_gap_whitelist_t const * p_whitelist);
  163. /**
  164. * @brief Function for constructing a whitelist for use when advertising.
  165. *
  166. * @note When advertising with whitelist, always use the whitelist created/set by the most recent
  167. * call to this function or to @ref im_whitelist_custom, whichever happened most recently.
  168. * @note Do not call this function while advertising with another whitelist.
  169. *
  170. * @param[in] p_peer_ids The ids of the peers to be added to the whitelist.
  171. * @param[in] n_peer_ids The number of peer ids in p_peer_ids.
  172. * @param[in,out] p_whitelist The constructed whitelist. Note that p_adv_whitelist->pp_addrs
  173. * must be NULL or point to an array with size @ref
  174. * BLE_GAP_WHITELIST_ADDR_MAX_COUNT and p_adv_whitelist->pp_irks
  175. * must be NULL or point to an array with size @ref
  176. * BLE_GAP_WHITELIST_IRK_MAX_COUNT.
  177. *
  178. * @retval NRF_SUCCESS Whitelist successfully created.
  179. * @retval NRF_ERROR_NULL p_whitelist was NULL.
  180. */
  181. ret_code_t im_whitelist_create(pm_peer_id_t * p_peer_ids,
  182. uint8_t n_peer_ids,
  183. ble_gap_whitelist_t * p_whitelist);
  184. /**
  185. * @brief Function for resolving a resolvable address with an identity resolution key (IRK).
  186. *
  187. * @details This function will use the ECB peripheral to resolve a resolvable address.
  188. * This can be used to resolve the identity of a device distributing a random
  189. * resolvable address based on any IRKs you have received earlier. If an address is
  190. * resolved by an IRK, the device disributing the address must also know the IRK.
  191. *
  192. * @param[in] p_addr A random resolvable address.
  193. * @param[in] p_irk An identity resolution key (IRK).
  194. *
  195. * @retval true The irk used matched the one used to create the address.
  196. * @retval false The irk used did not match the one used to create the address, or an argument was
  197. * NULL.
  198. */
  199. bool im_address_resolve(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * p_irk);
  200. /** @}
  201. * @endcond
  202. */
  203. #endif /* PEER_ID_MANAGER_H__ */