ble_conn_state.c 15 KB

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