ble_conn_state.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. /**
  13. * @file
  14. *
  15. * @defgroup ble_conn_state Connection state
  16. * @ingroup ble_sdk_lib
  17. * @{
  18. * @brief Module for storing data on BLE connections.
  19. *
  20. * @details This module stores certain states for each connection, which can be queried by
  21. * connection handle. The module uses BLE events to keep the states updated.
  22. *
  23. * In addition to the preprogrammed states, this module can also keep track of a number of
  24. * binary user states, or <i>user flags</i>. These are reset to 0 for new connections, but
  25. * otherwise not touched by this module.
  26. *
  27. * This module uses the @ref sdk_mapped_flags module, with connection handles as keys and
  28. * the connection states as flags.
  29. *
  30. * @note A connection handle is not immediately invalidated when it is disconnected. Certain states,
  31. * such as the role, can still be queried until the next time a new connection is established
  32. * to any device.
  33. *
  34. * To function properly, this module must be provided with BLE events from the SoftDevice
  35. * through the @ref ble_conn_state_on_ble_evt() function. This module should be the first
  36. * to receive BLE events if they are dispatched to multiple modules.
  37. */
  38. #ifndef BLE_CONN_STATE_H__
  39. #define BLE_CONN_STATE_H__
  40. #include <stdbool.h>
  41. #include <stdint.h>
  42. #include "ble.h"
  43. #include "sdk_mapped_flags.h"
  44. /**@brief Connection handle statuses.
  45. */
  46. typedef enum
  47. {
  48. BLE_CONN_STATUS_INVALID, /**< The connection handle is invalid. */
  49. BLE_CONN_STATUS_DISCONNECTED, /**< The connection handle refers to a connection that has been disconnected, but not yet invalidated. */
  50. BLE_CONN_STATUS_CONNECTED, /**< The connection handle refers to an active connection. */
  51. } ble_conn_state_status_t;
  52. #define BLE_CONN_STATE_N_USER_FLAGS 24 /**< The number of available user flags. */
  53. /**@brief One ID for each user flag collection.
  54. *
  55. * @details These IDs are used to identify user flag collections in the API calls.
  56. */
  57. typedef enum
  58. {
  59. BLE_CONN_STATE_USER_FLAG0 = 0,
  60. BLE_CONN_STATE_USER_FLAG1,
  61. BLE_CONN_STATE_USER_FLAG2,
  62. BLE_CONN_STATE_USER_FLAG3,
  63. BLE_CONN_STATE_USER_FLAG4,
  64. BLE_CONN_STATE_USER_FLAG5,
  65. BLE_CONN_STATE_USER_FLAG6,
  66. BLE_CONN_STATE_USER_FLAG7,
  67. BLE_CONN_STATE_USER_FLAG8,
  68. BLE_CONN_STATE_USER_FLAG9,
  69. BLE_CONN_STATE_USER_FLAG10,
  70. BLE_CONN_STATE_USER_FLAG11,
  71. BLE_CONN_STATE_USER_FLAG12,
  72. BLE_CONN_STATE_USER_FLAG13,
  73. BLE_CONN_STATE_USER_FLAG14,
  74. BLE_CONN_STATE_USER_FLAG15,
  75. BLE_CONN_STATE_USER_FLAG16,
  76. BLE_CONN_STATE_USER_FLAG17,
  77. BLE_CONN_STATE_USER_FLAG18,
  78. BLE_CONN_STATE_USER_FLAG19,
  79. BLE_CONN_STATE_USER_FLAG20,
  80. BLE_CONN_STATE_USER_FLAG21,
  81. BLE_CONN_STATE_USER_FLAG22,
  82. BLE_CONN_STATE_USER_FLAG23,
  83. BLE_CONN_STATE_USER_FLAG_INVALID,
  84. } ble_conn_state_user_flag_id_t;
  85. /**
  86. * @defgroup ble_conn_state_functions BLE connection state functions
  87. * @{
  88. */
  89. /**@brief Function for initializing or resetting the module.
  90. *
  91. * @details This function sets all states to their default, removing all records of connection handles.
  92. */
  93. void ble_conn_state_init(void);
  94. /**@brief Function for providing BLE SoftDevice events to the connection state module.
  95. *
  96. * @param[in] p_ble_evt The SoftDevice event.
  97. */
  98. void ble_conn_state_on_ble_evt(ble_evt_t * p_ble_evt);
  99. /**@brief Function for querying whether a connection handle represents a valid connection.
  100. *
  101. * @details A connection might be valid and have a BLE_CONN_STATUS_DISCONNECTED status.
  102. * Those connections are invalidated after a new connection occurs.
  103. *
  104. * @param[in] conn_handle Handle of the connection.
  105. *
  106. * @retval true If conn_handle represents a valid connection, thus a connection for which
  107. we have a record.
  108. * @retval false If conn_handle is @ref BLE_GAP_ROLE_INVALID, or if it has never been recorded.
  109. */
  110. bool ble_conn_state_valid(uint16_t conn_handle);
  111. /**@brief Function for querying the role of the local device in a connection.
  112. *
  113. * @param[in] conn_handle Handle of the connection to get the role for.
  114. *
  115. * @return The role of the local device in the connection (see @ref BLE_GAP_ROLES).
  116. * If conn_handle is not valid, the function returns BLE_GAP_ROLE_INVALID.
  117. */
  118. uint8_t ble_conn_state_role(uint16_t conn_handle);
  119. /**@brief Function for querying the status of a connection.
  120. *
  121. * @param[in] conn_handle Handle of the connection.
  122. *
  123. * @return The status of the connection.
  124. * If conn_handle is not valid, the function returns BLE_CONN_STATE_INVALID.
  125. */
  126. ble_conn_state_status_t ble_conn_state_status(uint16_t conn_handle);
  127. /**@brief Function for querying whether a connection is encrypted.
  128. *
  129. * @param[in] conn_handle Handle of connection to get the encryption state for.
  130. *
  131. * @retval true If the connection is encrypted.
  132. * @retval false If the connection is not encrypted or conn_handle is invalid.
  133. */
  134. bool ble_conn_state_encrypted(uint16_t conn_handle);
  135. /**@brief Function for querying whether a connection encryption is protected from Man in the Middle
  136. * attacks.
  137. *
  138. * @param[in] conn_handle Handle of connection to get the MITM state for.
  139. *
  140. * @retval true If the connection is encrypted with MITM protection.
  141. * @retval false If the connection is not encrypted, or encryption is not MITM protected, or
  142. * conn_handle is invalid.
  143. */
  144. bool ble_conn_state_mitm_protected(uint16_t conn_handle);
  145. /**@brief Function for querying the total number of connections.
  146. *
  147. * @return The total number of valid connections for which the module has a record.
  148. */
  149. uint32_t ble_conn_state_n_connections(void);
  150. /**@brief Function for querying the total number of connections in which the role of the local
  151. * device is @ref BLE_GAP_ROLE_CENTRAL.
  152. *
  153. * @return The number of connections in which the role of the local device is
  154. * @ref BLE_GAP_ROLE_CENTRAL.
  155. */
  156. uint32_t ble_conn_state_n_centrals(void);
  157. /**@brief Function for querying the total number of connections in which the role of the local
  158. * device is @ref BLE_GAP_ROLE_PERIPH.
  159. *
  160. * @return The number of connections in which the role of the local device is
  161. * @ref BLE_GAP_ROLE_PERIPH.
  162. */
  163. uint32_t ble_conn_state_n_peripherals(void);
  164. /**@brief Function for obtaining a list of all connection handles for which the module has a record.
  165. *
  166. * @details This function takes into account connections whose state is BLE_CONN_STATUS_DISCONNECTED.
  167. *
  168. * @return A list of all valid connection handles for which the module has a record.
  169. */
  170. sdk_mapped_flags_key_list_t ble_conn_state_conn_handles(void);
  171. /**@brief Function for obtaining a list of connection handles in which the role of the local
  172. * device is @ref BLE_GAP_ROLE_CENTRAL.
  173. *
  174. * @details This function takes into account connections whose state is BLE_CONN_STATUS_DISCONNECTED.
  175. *
  176. * @return A list of all valid connection handles for which the module has a record and in which
  177. * the role of local device is @ref BLE_GAP_ROLE_CENTRAL.
  178. */
  179. sdk_mapped_flags_key_list_t ble_conn_state_central_handles(void);
  180. /**@brief Function for obtaining the handle for the connection in which the role of the local device
  181. * is @ref BLE_GAP_ROLE_PERIPH.
  182. *
  183. * @details This function takes into account connections whose state is BLE_CONN_STATUS_DISCONNECTED.
  184. *
  185. * @return A list of all valid connection handles for which the module has a record and in which
  186. * the role of local device is @ref BLE_GAP_ROLE_PERIPH.
  187. */
  188. sdk_mapped_flags_key_list_t ble_conn_state_periph_handles(void);
  189. /**@brief Function for obtaining exclusive access to one of the user flag collections.
  190. *
  191. * @details The acquired collection contains one flag for each connection. These flags can be set
  192. * and read individually for each connection.
  193. *
  194. * The state of user flags will not be modified by the connection state module, except to
  195. * set it to 0 for a connection when that connection is invalidated.
  196. *
  197. * @return The ID of the acquired flag, or BLE_CONN_STATE_USER_FLAG_INVALID if none are available.
  198. */
  199. ble_conn_state_user_flag_id_t ble_conn_state_user_flag_acquire(void);
  200. /**@brief Function for reading the value of a user flag.
  201. *
  202. * @param[in] conn_handle Handle of connection to get the flag state for.
  203. * @param[in] flag_id Which flag to get the state for.
  204. *
  205. * @return The state of the flag. If conn_handle is invalid, the function returns false.
  206. */
  207. bool ble_conn_state_user_flag_get(uint16_t conn_handle, ble_conn_state_user_flag_id_t flag_id);
  208. /**@brief Function for setting the value of a user flag.
  209. *
  210. * @param[in] conn_handle Handle of connection to set the flag state for.
  211. * @param[in] flag_id Which flag to set the state for.
  212. * @param[in] value Value to set the flag state to.
  213. */
  214. void ble_conn_state_user_flag_set(uint16_t conn_handle,
  215. ble_conn_state_user_flag_id_t flag_id,
  216. bool value);
  217. /**@brief Function for getting the state of a user flag for all connection handles.
  218. *
  219. * @details The returned collection can be used with the @ref sdk_mapped_flags API. The returned
  220. * collection is a copy, so modifying it has no effect on the conn_state module.
  221. *
  222. * @param[in] flag_id Which flag to get states for.
  223. *
  224. * @return The collection of flag states. The collection is always all zeros when the flag_id is
  225. * unregistered.
  226. */
  227. sdk_mapped_flags_t ble_conn_state_user_flag_collection(ble_conn_state_user_flag_id_t flag_id);
  228. /** @} */
  229. /** @} */
  230. #endif /* BLE_CONN_STATE_H__ */