ble_conn_state.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. #include "ble_conn_state.h"
  41. #include <stdbool.h>
  42. #include <stdint.h>
  43. #include <string.h>
  44. #include "ble.h"
  45. #include "nrf_atflags.h"
  46. #include "app_error.h"
  47. #include "nrf_sdh_ble.h"
  48. #include "app_util_platform.h"
  49. #define DEFAULT_FLAG_COLLECTION_COUNT 6 /**< The number of flags kept for each connection, excluding user flags. */
  50. #define TOTAL_FLAG_COLLECTION_COUNT (DEFAULT_FLAG_COLLECTION_COUNT \
  51. + BLE_CONN_STATE_USER_FLAG_COUNT) /**< The number of flags kept for each connection, including user flags. */
  52. /**@brief Structure containing all the flag collections maintained by the Connection State module.
  53. */
  54. typedef struct
  55. {
  56. nrf_atflags_t valid_flags; /**< Flags indicating which connection handles are valid. */
  57. nrf_atflags_t connected_flags; /**< Flags indicating which connections are connected, since disconnected connection handles will not immediately be invalidated. */
  58. nrf_atflags_t central_flags; /**< Flags indicating in which connections the local device is the central. */
  59. nrf_atflags_t encrypted_flags; /**< Flags indicating which connections are encrypted. */
  60. nrf_atflags_t mitm_protected_flags; /**< Flags indicating which connections have encryption with protection from man-in-the-middle attacks. */
  61. nrf_atflags_t lesc_flags; /**< Flags indicating which connections have bonded using LE Secure Connections (LESC). */
  62. nrf_atflags_t user_flags[BLE_CONN_STATE_USER_FLAG_COUNT]; /**< Flags that can be reserved by the user. The flags will be cleared when a connection is invalidated, otherwise, the user is wholly responsible for the flag states. */
  63. } ble_conn_state_flag_collections_t;
  64. ANON_UNIONS_ENABLE;
  65. /**@brief Structure containing the internal state of the Connection State module.
  66. */
  67. typedef struct
  68. {
  69. nrf_atflags_t acquired_flags; /**< Bitmap for keeping track of which user flags have been acquired. */
  70. union
  71. {
  72. ble_conn_state_flag_collections_t flags; /**< Flag collections kept by the Connection State module. */
  73. nrf_atflags_t flag_array[TOTAL_FLAG_COLLECTION_COUNT]; /**< Flag collections as array to allow iterating over all flag collections. */
  74. };
  75. } ble_conn_state_t;
  76. ANON_UNIONS_DISABLE;
  77. static ble_conn_state_t m_bcs = {0}; /**< Instantiation of the internal state. */
  78. /**@brief Function for resetting all internal memory to the values it had at initialization.
  79. */
  80. void bcs_internal_state_reset(void)
  81. {
  82. memset( &m_bcs, 0, sizeof(ble_conn_state_t) );
  83. }
  84. ble_conn_state_conn_handle_list_t conn_handle_list_get(nrf_atflags_t flags)
  85. {
  86. ble_conn_state_conn_handle_list_t conn_handle_list;
  87. conn_handle_list.len = 0;
  88. if (flags != 0)
  89. {
  90. for (uint32_t i = 0; i < BLE_CONN_STATE_MAX_CONNECTIONS; i++)
  91. {
  92. if (nrf_atflags_get(&flags, i))
  93. {
  94. conn_handle_list.conn_handles[conn_handle_list.len++] = i;
  95. }
  96. }
  97. }
  98. return conn_handle_list;
  99. }
  100. uint32_t active_flag_count(nrf_atflags_t flags)
  101. {
  102. uint32_t set_flag_count = 0;
  103. for (uint32_t i = 0; i < BLE_CONN_STATE_MAX_CONNECTIONS; i++)
  104. {
  105. if (nrf_atflags_get(&flags, i))
  106. {
  107. set_flag_count += 1;
  108. }
  109. }
  110. return set_flag_count;
  111. }
  112. /**@brief Function for activating a connection record.
  113. *
  114. * @param p_record The record to activate.
  115. * @param conn_handle The connection handle to copy into the record.
  116. * @param role The role of the connection.
  117. *
  118. * @return whether the record was activated successfully.
  119. */
  120. static bool record_activate(uint16_t conn_handle)
  121. {
  122. if (conn_handle >= BLE_CONN_STATE_MAX_CONNECTIONS)
  123. {
  124. return false;
  125. }
  126. nrf_atflags_set(&m_bcs.flags.connected_flags, conn_handle);
  127. nrf_atflags_set(&m_bcs.flags.valid_flags, conn_handle);
  128. return true;
  129. }
  130. /**@brief Function for marking a connection record as invalid and resetting the values.
  131. *
  132. * @param p_record The record to invalidate.
  133. */
  134. static void record_invalidate(uint16_t conn_handle)
  135. {
  136. for (uint32_t i = 0; i < TOTAL_FLAG_COLLECTION_COUNT; i++)
  137. {
  138. nrf_atflags_clear(&m_bcs.flag_array[i], conn_handle);
  139. }
  140. }
  141. /**@brief Function for marking a connection as disconnected. See @ref BLE_CONN_STATUS_DISCONNECTED.
  142. *
  143. * @param p_record The record of the connection to set as disconnected.
  144. */
  145. static void record_set_disconnected(uint16_t conn_handle)
  146. {
  147. nrf_atflags_clear(&m_bcs.flags.connected_flags, conn_handle);
  148. }
  149. /**@brief Function for invalidating records with a @ref BLE_CONN_STATUS_DISCONNECTED
  150. * connection status
  151. */
  152. static void record_purge_disconnected()
  153. {
  154. nrf_atflags_t disconnected_flags = ~m_bcs.flags.connected_flags;
  155. ble_conn_state_conn_handle_list_t disconnected_list;
  156. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&disconnected_flags, m_bcs.flags.valid_flags));
  157. disconnected_list = conn_handle_list_get(disconnected_flags);
  158. for (uint32_t i = 0; i < disconnected_list.len; i++)
  159. {
  160. record_invalidate(disconnected_list.conn_handles[i]);
  161. }
  162. }
  163. /**@brief Function for checking if a user flag has been acquired.
  164. *
  165. * @param[in] flag_id Which flag to check.
  166. *
  167. * @return Whether the flag has been acquired.
  168. */
  169. static bool user_flag_is_acquired(ble_conn_state_user_flag_id_t flag_id)
  170. {
  171. return nrf_atflags_get(&m_bcs.acquired_flags, flag_id);
  172. }
  173. void ble_conn_state_init(void)
  174. {
  175. bcs_internal_state_reset();
  176. }
  177. static void flag_toggle(nrf_atflags_t * p_flags, uint16_t conn_handle, bool value)
  178. {
  179. if (value)
  180. {
  181. nrf_atflags_set(p_flags, conn_handle);
  182. }
  183. else
  184. {
  185. nrf_atflags_clear(p_flags, conn_handle);
  186. }
  187. }
  188. /**
  189. * @brief Function for handling BLE events.
  190. *
  191. * @param[in] p_ble_evt Event received from the BLE stack.
  192. * @param[in] p_context Context.
  193. */
  194. static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
  195. {
  196. uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  197. switch (p_ble_evt->header.evt_id)
  198. {
  199. case BLE_GAP_EVT_CONNECTED:
  200. record_purge_disconnected();
  201. if ( !record_activate(conn_handle) )
  202. {
  203. // No more records available. Should not happen.
  204. APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
  205. }
  206. #ifdef BLE_GAP_ROLE_CENTRAL
  207. else if ((p_ble_evt->evt.gap_evt.params.connected.role == BLE_GAP_ROLE_CENTRAL))
  208. {
  209. // Central
  210. nrf_atflags_set(&m_bcs.flags.central_flags, conn_handle);
  211. }
  212. #endif // BLE_GAP_ROLE_CENTRAL
  213. else
  214. {
  215. // No implementation required.
  216. }
  217. break;
  218. case BLE_GAP_EVT_DISCONNECTED:
  219. record_set_disconnected(conn_handle);
  220. break;
  221. case BLE_GAP_EVT_CONN_SEC_UPDATE:
  222. {
  223. uint8_t sec_lv = p_ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.lv;
  224. // Set/unset flags based on security level.
  225. flag_toggle(&m_bcs.flags.lesc_flags, conn_handle, sec_lv >= 4);
  226. flag_toggle(&m_bcs.flags.mitm_protected_flags, conn_handle, sec_lv >= 3);
  227. flag_toggle(&m_bcs.flags.encrypted_flags, conn_handle, sec_lv >= 2);
  228. break;
  229. }
  230. case BLE_GAP_EVT_AUTH_STATUS:
  231. if (p_ble_evt->evt.gap_evt.params.auth_status.auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
  232. {
  233. bool lesc = p_ble_evt->evt.gap_evt.params.auth_status.lesc;
  234. flag_toggle(&m_bcs.flags.lesc_flags, conn_handle, lesc);
  235. }
  236. break;
  237. }
  238. }
  239. NRF_SDH_BLE_OBSERVER(m_ble_evt_observer, BLE_CONN_STATE_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  240. bool ble_conn_state_valid(uint16_t conn_handle)
  241. {
  242. if (conn_handle >= BLE_CONN_STATE_MAX_CONNECTIONS)
  243. {
  244. return false;
  245. }
  246. return nrf_atflags_get(&m_bcs.flags.valid_flags, conn_handle);
  247. }
  248. uint8_t ble_conn_state_role(uint16_t conn_handle)
  249. {
  250. uint8_t role = BLE_GAP_ROLE_INVALID;
  251. if (ble_conn_state_valid(conn_handle))
  252. {
  253. #if defined (BLE_GAP_ROLE_PERIPH) && defined (BLE_GAP_ROLE_CENTRAL)
  254. bool central = nrf_atflags_get(&m_bcs.flags.central_flags, conn_handle);
  255. role = central ? BLE_GAP_ROLE_CENTRAL : BLE_GAP_ROLE_PERIPH;
  256. #elif defined (BLE_GAP_ROLE_CENTRAL)
  257. role = BLE_GAP_ROLE_CENTRAL;
  258. #else
  259. role = BLE_GAP_ROLE_PERIPH;
  260. #endif // defined (BLE_GAP_ROLE_PERIPH) && defined (BLE_GAP_ROLE_CENTRAL)
  261. }
  262. return role;
  263. }
  264. ble_conn_state_status_t ble_conn_state_status(uint16_t conn_handle)
  265. {
  266. ble_conn_state_status_t conn_status = BLE_CONN_STATUS_INVALID;
  267. if (ble_conn_state_valid(conn_handle))
  268. {
  269. bool connected = nrf_atflags_get(&m_bcs.flags.connected_flags, conn_handle);
  270. conn_status = connected ? BLE_CONN_STATUS_CONNECTED : BLE_CONN_STATUS_DISCONNECTED;
  271. }
  272. return conn_status;
  273. }
  274. bool ble_conn_state_encrypted(uint16_t conn_handle)
  275. {
  276. if (ble_conn_state_valid(conn_handle))
  277. {
  278. return nrf_atflags_get(&m_bcs.flags.encrypted_flags, conn_handle);
  279. }
  280. return false;
  281. }
  282. bool ble_conn_state_mitm_protected(uint16_t conn_handle)
  283. {
  284. if (ble_conn_state_valid(conn_handle))
  285. {
  286. return nrf_atflags_get(&m_bcs.flags.mitm_protected_flags, conn_handle);
  287. }
  288. return false;
  289. }
  290. bool ble_conn_state_lesc(uint16_t conn_handle)
  291. {
  292. if (ble_conn_state_valid(conn_handle))
  293. {
  294. return nrf_atflags_get(&m_bcs.flags.lesc_flags, conn_handle);
  295. }
  296. return false;
  297. }
  298. uint32_t ble_conn_state_conn_count(void)
  299. {
  300. return active_flag_count(m_bcs.flags.connected_flags);
  301. }
  302. uint32_t ble_conn_state_central_conn_count(void)
  303. {
  304. nrf_atflags_t central_conn_flags = m_bcs.flags.central_flags;
  305. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&central_conn_flags, m_bcs.flags.connected_flags));
  306. return active_flag_count(central_conn_flags);
  307. }
  308. uint32_t ble_conn_state_peripheral_conn_count(void)
  309. {
  310. nrf_atflags_t peripheral_conn_flags = ~m_bcs.flags.central_flags;
  311. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&peripheral_conn_flags, m_bcs.flags.connected_flags));
  312. return active_flag_count(peripheral_conn_flags);
  313. }
  314. ble_conn_state_conn_handle_list_t ble_conn_state_conn_handles(void)
  315. {
  316. return conn_handle_list_get(m_bcs.flags.valid_flags);
  317. }
  318. ble_conn_state_conn_handle_list_t ble_conn_state_central_handles(void)
  319. {
  320. nrf_atflags_t central_conn_flags = m_bcs.flags.central_flags;
  321. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&central_conn_flags, m_bcs.flags.connected_flags));
  322. return conn_handle_list_get(central_conn_flags);
  323. }
  324. ble_conn_state_conn_handle_list_t ble_conn_state_periph_handles(void)
  325. {
  326. nrf_atflags_t peripheral_conn_flags = ~m_bcs.flags.central_flags;
  327. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&peripheral_conn_flags, m_bcs.flags.connected_flags));
  328. return conn_handle_list_get(peripheral_conn_flags);
  329. }
  330. uint16_t ble_conn_state_conn_idx(uint16_t conn_handle)
  331. {
  332. if (ble_conn_state_valid(conn_handle))
  333. {
  334. return conn_handle;
  335. }
  336. else
  337. {
  338. return BLE_CONN_STATE_MAX_CONNECTIONS;
  339. }
  340. }
  341. ble_conn_state_user_flag_id_t ble_conn_state_user_flag_acquire(void)
  342. {
  343. uint32_t acquired_flag = nrf_atflags_find_and_set_flag(&m_bcs.acquired_flags,
  344. BLE_CONN_STATE_USER_FLAG_COUNT);
  345. if (acquired_flag == BLE_CONN_STATE_USER_FLAG_COUNT)
  346. {
  347. return BLE_CONN_STATE_USER_FLAG_INVALID;
  348. }
  349. return (ble_conn_state_user_flag_id_t)acquired_flag;
  350. }
  351. bool ble_conn_state_user_flag_get(uint16_t conn_handle, ble_conn_state_user_flag_id_t flag_id)
  352. {
  353. if (user_flag_is_acquired(flag_id) && ble_conn_state_valid(conn_handle))
  354. {
  355. return nrf_atflags_get(&m_bcs.flags.user_flags[flag_id], conn_handle);
  356. }
  357. else
  358. {
  359. return false;
  360. }
  361. }
  362. void ble_conn_state_user_flag_set(uint16_t conn_handle,
  363. ble_conn_state_user_flag_id_t flag_id,
  364. bool value)
  365. {
  366. if (user_flag_is_acquired(flag_id) && ble_conn_state_valid(conn_handle))
  367. {
  368. flag_toggle(&m_bcs.flags.user_flags[flag_id], conn_handle, value);
  369. }
  370. }
  371. static uint32_t for_each_set_flag(nrf_atflags_t flags,
  372. ble_conn_state_user_function_t user_function,
  373. void * p_context)
  374. {
  375. if (user_function == NULL)
  376. {
  377. return 0;
  378. }
  379. uint32_t call_count = 0;
  380. if (flags != 0)
  381. {
  382. for (uint32_t i = 0; i < BLE_CONN_STATE_MAX_CONNECTIONS; i++)
  383. {
  384. if (nrf_atflags_get(&flags, i))
  385. {
  386. user_function(i, p_context);
  387. call_count += 1;
  388. }
  389. }
  390. }
  391. return call_count;
  392. }
  393. uint32_t ble_conn_state_for_each_connected(ble_conn_state_user_function_t user_function,
  394. void * p_context)
  395. {
  396. return for_each_set_flag(m_bcs.flags.connected_flags, user_function, p_context);
  397. }
  398. uint32_t ble_conn_state_for_each_set_user_flag(ble_conn_state_user_flag_id_t flag_id,
  399. ble_conn_state_user_function_t user_function,
  400. void * p_context)
  401. {
  402. if (!user_flag_is_acquired(flag_id))
  403. {
  404. return 0;
  405. }
  406. return for_each_set_flag(m_bcs.flags.user_flags[flag_id], user_function, p_context);
  407. }