ble_conn_state.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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
  42. *
  43. * @defgroup ble_conn_state Connection state
  44. * @ingroup ble_sdk_lib
  45. * @{
  46. * @brief Module for storing data on BLE connections.
  47. *
  48. * @details This module stores certain states for each connection, which can be queried by
  49. * connection handle. The module uses BLE events to keep the states updated.
  50. *
  51. * In addition to the preprogrammed states, this module can also keep track of a number of
  52. * binary user states, or <i>user flags</i>. These are reset to 0 for new connections, but
  53. * otherwise not touched by this module.
  54. *
  55. * This module uses the @ref nrf_atomic module to make the flag operations thread-safe.
  56. *
  57. * @note A connection handle is not immediately invalidated when it is disconnected. Certain states,
  58. * such as the role, can still be queried until the next time a new connection is established
  59. * to any device.
  60. *
  61. */
  62. #ifndef BLE_CONN_STATE_H__
  63. #define BLE_CONN_STATE_H__
  64. #include <stdbool.h>
  65. #include <stdint.h>
  66. #include "ble.h"
  67. #include "ble_gap.h"
  68. #include "nrf_atomic.h"
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. /**@brief Connection handle statuses.
  73. */
  74. typedef enum
  75. {
  76. BLE_CONN_STATUS_INVALID, /**< The connection handle is invalid. */
  77. BLE_CONN_STATUS_DISCONNECTED, /**< The connection handle refers to a connection that has been disconnected, but not yet invalidated. */
  78. BLE_CONN_STATUS_CONNECTED, /**< The connection handle refers to an active connection. */
  79. } ble_conn_state_status_t;
  80. #define BLE_CONN_STATE_MAX_CONNECTIONS BLE_GAP_ROLE_COUNT_COMBINED_MAX /**< The maximum number of connections supported. */
  81. #define BLE_CONN_STATE_USER_FLAG_COUNT 24 /**< The number of available user flags. */
  82. /**@brief Type used to present a list of conn_handles.
  83. */
  84. typedef struct
  85. {
  86. uint32_t len; /**< The length of the list. */
  87. uint16_t conn_handles[BLE_CONN_STATE_MAX_CONNECTIONS]; /**< The list of handles. */
  88. } ble_conn_state_conn_handle_list_t;
  89. /**@brief One ID for each user flag collection.
  90. *
  91. * @details These IDs are used to identify user flag collections in the API calls.
  92. */
  93. typedef enum
  94. {
  95. BLE_CONN_STATE_USER_FLAG0 = 0,
  96. BLE_CONN_STATE_USER_FLAG1,
  97. BLE_CONN_STATE_USER_FLAG2,
  98. BLE_CONN_STATE_USER_FLAG3,
  99. BLE_CONN_STATE_USER_FLAG4,
  100. BLE_CONN_STATE_USER_FLAG5,
  101. BLE_CONN_STATE_USER_FLAG6,
  102. BLE_CONN_STATE_USER_FLAG7,
  103. BLE_CONN_STATE_USER_FLAG8,
  104. BLE_CONN_STATE_USER_FLAG9,
  105. BLE_CONN_STATE_USER_FLAG10,
  106. BLE_CONN_STATE_USER_FLAG11,
  107. BLE_CONN_STATE_USER_FLAG12,
  108. BLE_CONN_STATE_USER_FLAG13,
  109. BLE_CONN_STATE_USER_FLAG14,
  110. BLE_CONN_STATE_USER_FLAG15,
  111. BLE_CONN_STATE_USER_FLAG16,
  112. BLE_CONN_STATE_USER_FLAG17,
  113. BLE_CONN_STATE_USER_FLAG18,
  114. BLE_CONN_STATE_USER_FLAG19,
  115. BLE_CONN_STATE_USER_FLAG20,
  116. BLE_CONN_STATE_USER_FLAG21,
  117. BLE_CONN_STATE_USER_FLAG22,
  118. BLE_CONN_STATE_USER_FLAG23,
  119. BLE_CONN_STATE_USER_FLAG_INVALID,
  120. } ble_conn_state_user_flag_id_t;
  121. /**@brief Function to be called when a flag ID is set. See @ref ble_conn_state_for_each_set_user_flag.
  122. *
  123. * @param[in] conn_handle The connection the flag is set for.
  124. * @param[in] p_context Arbitrary pointer provided by the caller of
  125. * @ref ble_conn_state_for_each_set_user_flag.
  126. */
  127. typedef void (*ble_conn_state_user_function_t)(uint16_t conn_handle, void * p_context);
  128. /**
  129. * @defgroup ble_conn_state_functions BLE connection state functions
  130. * @{
  131. */
  132. /**@brief Function for initializing or resetting the module.
  133. *
  134. * @details This function sets all states to their default, removing all records of connection handles.
  135. */
  136. void ble_conn_state_init(void);
  137. /**@brief Function for querying whether a connection handle represents a valid connection.
  138. *
  139. * @details A connection might be valid and have a BLE_CONN_STATUS_DISCONNECTED status.
  140. * Those connections are invalidated after a new connection occurs.
  141. *
  142. * @param[in] conn_handle Handle of the connection.
  143. *
  144. * @retval true If conn_handle represents a valid connection, thus a connection for which
  145. we have a record.
  146. * @retval false If conn_handle is @ref BLE_GAP_ROLE_INVALID, or if it has never been recorded.
  147. */
  148. bool ble_conn_state_valid(uint16_t conn_handle);
  149. /**@brief Function for querying the role of the local device in a connection.
  150. *
  151. * @param[in] conn_handle Handle of the connection to get the role for.
  152. *
  153. * @return The role of the local device in the connection (see @ref BLE_GAP_ROLES).
  154. * If conn_handle is not valid, the function returns BLE_GAP_ROLE_INVALID.
  155. */
  156. uint8_t ble_conn_state_role(uint16_t conn_handle);
  157. /**@brief Function for querying the status of a connection.
  158. *
  159. * @param[in] conn_handle Handle of the connection.
  160. *
  161. * @return The status of the connection.
  162. * If conn_handle is not valid, the function returns BLE_CONN_STATE_INVALID.
  163. */
  164. ble_conn_state_status_t ble_conn_state_status(uint16_t conn_handle);
  165. /**@brief Function for querying whether a connection is encrypted.
  166. *
  167. * @param[in] conn_handle Handle of connection to get the encryption state for.
  168. *
  169. * @retval true If the connection is encrypted.
  170. * @retval false If the connection is not encrypted or conn_handle is invalid.
  171. */
  172. bool ble_conn_state_encrypted(uint16_t conn_handle);
  173. /**@brief Function for querying whether a connection encryption is protected from Man in the Middle
  174. * attacks.
  175. *
  176. * @param[in] conn_handle Handle of connection to get the MITM state for.
  177. *
  178. * @retval true If the connection is encrypted with MITM protection.
  179. * @retval false If the connection is not encrypted, or encryption is not MITM protected, or
  180. * conn_handle is invalid.
  181. */
  182. bool ble_conn_state_mitm_protected(uint16_t conn_handle);
  183. /**@brief Function for querying whether a connection was bonded using LE Secure Connections (LESC).
  184. *
  185. * The connection must currently be encrypted.
  186. *
  187. * @note This function will report false if bonded, and the LESC bonding was unauthenticated
  188. * ("Just Works") and happened in a previous connection. To detect such cases as well, check
  189. * the stored bonding key, e.g. in Peer Manager, which has a LESC flag associated with it.
  190. *
  191. * @param[in] conn_handle Handle of connection to get the LESC state for.
  192. *
  193. * @retval true If the connection was bonded using LESC.
  194. * @retval false If the connection has not been bonded using LESC, or conn_handle is invalid.
  195. */
  196. bool ble_conn_state_lesc(uint16_t conn_handle);
  197. /**@brief Function for querying the total number of connections.
  198. *
  199. * @return The total number of valid connections for which the module has a record.
  200. */
  201. uint32_t ble_conn_state_conn_count(void);
  202. /**@brief Function for querying the total number of connections in which the role of the local
  203. * device is @ref BLE_GAP_ROLE_CENTRAL.
  204. *
  205. * @return The number of connections in which the role of the local device is
  206. * @ref BLE_GAP_ROLE_CENTRAL.
  207. */
  208. uint32_t ble_conn_state_central_conn_count(void);
  209. /**@brief Function for querying the total number of connections in which the role of the local
  210. * device is @ref BLE_GAP_ROLE_PERIPH.
  211. *
  212. * @return The number of connections in which the role of the local device is
  213. * @ref BLE_GAP_ROLE_PERIPH.
  214. */
  215. uint32_t ble_conn_state_peripheral_conn_count(void);
  216. /**@brief Function for obtaining a list of all connection handles for which the module has a record.
  217. *
  218. * @details This function takes into account connections whose state is BLE_CONN_STATUS_DISCONNECTED.
  219. *
  220. * @return A list of all valid connection handles for which the module has a record.
  221. */
  222. ble_conn_state_conn_handle_list_t ble_conn_state_conn_handles(void);
  223. /**@brief Function for obtaining a list of connection handles in which the role of the local
  224. * device is @ref BLE_GAP_ROLE_CENTRAL.
  225. *
  226. * @details This function takes into account connections whose state is BLE_CONN_STATUS_DISCONNECTED.
  227. *
  228. * @return A list of all valid connection handles for which the module has a record and in which
  229. * the role of local device is @ref BLE_GAP_ROLE_CENTRAL.
  230. */
  231. ble_conn_state_conn_handle_list_t ble_conn_state_central_handles(void);
  232. /**@brief Function for obtaining the handle for the connection in which the role of the local device
  233. * is @ref BLE_GAP_ROLE_PERIPH.
  234. *
  235. * @details This function takes into account connections whose state is BLE_CONN_STATUS_DISCONNECTED.
  236. *
  237. * @return A list of all valid connection handles for which the module has a record and in which
  238. * the role of local device is @ref BLE_GAP_ROLE_PERIPH.
  239. */
  240. ble_conn_state_conn_handle_list_t ble_conn_state_periph_handles(void);
  241. /**@brief Function for translating a connection handle to a value that can be used as an array index.
  242. *
  243. * @details Function for mapping connection handles onto the range <0 - MAX_CONNECTIONS>.
  244. *
  245. * @note The index will be the same as long as a connection is invalid. A subsequent connection with
  246. * the same connection handle might have a different index.
  247. *
  248. * @param[in] conn_handle The connection for which to retrieve an index.
  249. *
  250. * @return An index unique to this connection. Or @ref BLE_CONN_STATE_MAX_CONNECTIONS if
  251. * @p conn_handle refers to an invalid connection.
  252. */
  253. uint16_t ble_conn_state_conn_idx(uint16_t conn_handle);
  254. /**@brief Function for obtaining exclusive access to one of the user flag collections.
  255. *
  256. * @details The acquired collection contains one flag for each connection. These flags can be set
  257. * and read individually for each connection.
  258. *
  259. * The state of user flags will not be modified by the connection state module, except to
  260. * set it to 0 for a connection when that connection is invalidated.
  261. *
  262. * @return The ID of the acquired flag, or BLE_CONN_STATE_USER_FLAG_INVALID if none are available.
  263. */
  264. ble_conn_state_user_flag_id_t ble_conn_state_user_flag_acquire(void);
  265. /**@brief Function for reading the value of a user flag.
  266. *
  267. * @param[in] conn_handle Handle of connection to get the flag state for.
  268. * @param[in] flag_id Which flag to get the state for.
  269. *
  270. * @return The state of the flag. If conn_handle is invalid, the function returns false.
  271. */
  272. bool ble_conn_state_user_flag_get(uint16_t conn_handle, ble_conn_state_user_flag_id_t flag_id);
  273. /**@brief Function for setting the value of a user flag.
  274. *
  275. * @param[in] conn_handle Handle of connection to set the flag state for.
  276. * @param[in] flag_id Which flag to set the state for.
  277. * @param[in] value Value to set the flag state to.
  278. */
  279. void ble_conn_state_user_flag_set(uint16_t conn_handle,
  280. ble_conn_state_user_flag_id_t flag_id,
  281. bool value);
  282. /**@brief Function for running a function for each active connection.
  283. *
  284. * @param[in] user_function The function to run for each connection.
  285. * @param[in] p_context Arbitrary context to be passed to \p user_function.
  286. *
  287. * @return The number of times \p user_function was run.
  288. */
  289. uint32_t ble_conn_state_for_each_connected(ble_conn_state_user_function_t user_function,
  290. void * p_context);
  291. /**@brief Function for running a function for each flag that is set in a user flag collection.
  292. *
  293. * @param[in] flag_id Which flags to check.
  294. * @param[in] user_function The function to run when a flag is set.
  295. * @param[in] p_context Arbitrary context to be passed to \p user_function.
  296. *
  297. * @return The number of times \p user_function was run.
  298. */
  299. uint32_t ble_conn_state_for_each_set_user_flag(ble_conn_state_user_flag_id_t flag_id,
  300. ble_conn_state_user_function_t user_function,
  301. void * p_context);
  302. /** @} */
  303. /** @} */
  304. #ifdef __cplusplus
  305. }
  306. #endif
  307. #endif /* BLE_CONN_STATE_H__ */