Skip to content

Commit d527abb

Browse files
lib: add conn_state
Add Connection state library. Signed-off-by: Eivind Jølsgard <[email protected]> Co-authored-by: Mirko Covizzi <[email protected]>
1 parent dd8ac15 commit d527abb

File tree

12 files changed

+1891
-0
lines changed

12 files changed

+1891
-0
lines changed

CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
/lib/bm_zms/ @nrfconnect/ncs-bm @rghaddab
4747
/lib/ble_adv/ @nrfconnect/ncs-bm
4848
/lib/ble_conn_params/ @nrfconnect/ncs-bm
49+
/lib/ble_conn_state/ @nrfconnect/ncs-bm
4950
/lib/ble_gq/ @nrfconnect/ncs-bm
5051
/lib/ble_qwr/ @nrfconnect/ncs-bm
5152
/lib/ble_racp/ @nrfconnect/ncs-bm
@@ -87,6 +88,7 @@
8788

8889
# Tests
8990
/tests/lib/bm_zms/ @nrfconnect/ncs-bm @rghaddab
91+
/tests/lib/ble_conn_state/ @nrfconnect/ncs-bm
9092
/tests/lib/ble_qwr/ @nrfconnect/ncs-bm
9193
/tests/lib/ble_racp/ @nrfconnect/ncs-bm
9294
/tests/lib/ble_adv/ @nrfconnect/ncs-bm-test

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ No changes since the latest nRF Connect SDK Bare Metal release.
5454
Libraries
5555
=========
5656

57+
* Added the ``ble_conn_state`` library.
58+
5759
* :ref:`lib_bm_zms` library:
5860

5961
* Updated:

include/ble_conn_state.h

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
/*
2+
* Copyright (c) 2015 - 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
/**
8+
* @file
9+
*
10+
* @defgroup ble_conn_state Connection state
11+
* @ingroup ble_sdk_lib
12+
* @{
13+
* @brief Module for storing data on BLE connections.
14+
*
15+
* @details This module stores certain states for each connection, which can be queried by
16+
* connection handle. The module uses BLE events to keep the states updated.
17+
*
18+
* In addition to the preprogrammed states, this module can also keep track of a number of
19+
* binary user states, or user flags. These are reset to 0 for new connections, but
20+
* otherwise not touched by this module.
21+
*
22+
* This module uses atomics to make the flag operations thread-safe.
23+
*/
24+
25+
#ifndef BLE_CONN_STATE_H__
26+
#define BLE_CONN_STATE_H__
27+
28+
#include <stdbool.h>
29+
#include <stdint.h>
30+
#include <ble.h>
31+
#include <ble_gap.h>
32+
33+
#ifdef __cplusplus
34+
extern "C" {
35+
#endif
36+
37+
/** @brief Connection handle statuses. */
38+
enum ble_conn_state_status {
39+
/** The connection handle is invalid. */
40+
BLE_CONN_STATUS_INVALID,
41+
/** The connection handle refers to a connection that has been disconnected,
42+
* but not yet invalidated.
43+
*/
44+
BLE_CONN_STATUS_DISCONNECTED,
45+
/** The connection handle refers to an active connection. */
46+
BLE_CONN_STATUS_CONNECTED,
47+
};
48+
49+
/** The maximum number of connections supported. */
50+
#define BLE_CONN_STATE_MAX_CONNECTIONS BLE_GAP_ROLE_COUNT_COMBINED_MAX
51+
52+
/** Invalid user flag. */
53+
#define BLE_CONN_STATE_USER_FLAG_INVALID CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT
54+
55+
/** @brief Type used to present a list of conn_handles. */
56+
struct ble_conn_state_conn_handle_list {
57+
/** The length of the list. */
58+
uint32_t len;
59+
/** The list of handles. */
60+
uint16_t conn_handles[BLE_CONN_STATE_MAX_CONNECTIONS];
61+
};
62+
63+
/**
64+
* @brief Function to be called when a flag ID is set.
65+
*
66+
* See @ref ble_conn_state_for_each_set_user_flag.
67+
*
68+
* @param[in] conn_handle The connection the flag is set for.
69+
* @param[in] ctx Arbitrary pointer provided by the caller of
70+
* @ref ble_conn_state_for_each_set_user_flag.
71+
*/
72+
typedef void (*ble_conn_state_user_function_t)(uint16_t conn_handle, void *ctx);
73+
74+
/**
75+
* @defgroup ble_conn_state_functions BLE connection state functions
76+
* @{
77+
*/
78+
79+
/**
80+
* @brief Initialize or reset the module.
81+
*
82+
* @details This function sets all states to their default,
83+
* removing all records of connection handles.
84+
*/
85+
void ble_conn_state_init(void);
86+
87+
/**
88+
* @brief Check whether a connection handle represents a valid connection.
89+
*
90+
* @details A connection might be valid and have a BLE_CONN_STATUS_DISCONNECTED status.
91+
* Those connections are invalidated after a new connection occurs.
92+
*
93+
* @param[in] conn_handle Handle of the connection.
94+
*
95+
* @retval true If @c conn_handle represents a valid connection, thus a connection for which
96+
we have a record.
97+
* @retval false If @c conn_handle is @ref BLE_GAP_ROLE_INVALID, or if it has never been recorded.
98+
*/
99+
bool ble_conn_state_valid(uint16_t conn_handle);
100+
101+
/**
102+
* @brief Get the role of the local device in a connection.
103+
*
104+
* @param[in] conn_handle Handle of the connection to get the role for.
105+
*
106+
* @return The role of the local device in the connection (see @ref BLE_GAP_ROLES).
107+
* If conn_handle is not valid, the function returns @ref BLE_GAP_ROLE_INVALID.
108+
*/
109+
uint8_t ble_conn_state_role(uint16_t conn_handle);
110+
111+
/**
112+
* @brief Get the status of a connection.
113+
*
114+
* @param[in] conn_handle Handle of the connection.
115+
*
116+
* @return The status of the connection.
117+
* If conn_handle is not valid, the function returns @ref BLE_CONN_STATE_INVALID.
118+
*/
119+
enum ble_conn_state_status ble_conn_state_status(uint16_t conn_handle);
120+
121+
/**
122+
* @brief Check whether a connection is encrypted.
123+
*
124+
* @param[in] conn_handle Handle of connection to get the encryption state for.
125+
*
126+
* @retval true If the connection is encrypted.
127+
* @retval false If the connection is not encrypted or conn_handle is invalid.
128+
*/
129+
bool ble_conn_state_encrypted(uint16_t conn_handle);
130+
131+
/**
132+
* @brief Check whether a connection encryption is protected from Man in the Middle
133+
* (MITM) attacks.
134+
*
135+
* @param[in] conn_handle Handle of connection to get the MITM state for.
136+
*
137+
* @retval true If the connection is encrypted with MITM protection.
138+
* @retval false If the connection is not encrypted, or encryption is not MITM protected, or
139+
* conn_handle is invalid.
140+
*/
141+
bool ble_conn_state_mitm_protected(uint16_t conn_handle);
142+
143+
/**
144+
* @brief Check whether a connection was bonded using LE Secure Connections (LESC).
145+
*
146+
* The connection must currently be encrypted.
147+
*
148+
* @note This function will report false if bonded, and the LESC bonding was unauthenticated
149+
* ("Just Works") and happened in a previous connection. To detect such cases as well, check
150+
* the stored bonding key, e.g. in Peer Manager, which has a LESC flag associated with it.
151+
*
152+
* @param[in] conn_handle Handle of connection to get the LESC state for.
153+
*
154+
* @retval true If the connection was bonded using LESC.
155+
* @retval false If the connection has not been bonded using LESC, or @c conn_handle is invalid.
156+
*/
157+
bool ble_conn_state_lesc(uint16_t conn_handle);
158+
159+
/**
160+
* @brief Get the total number of connections.
161+
*
162+
* @return The total number of valid connections for which the module has a record.
163+
*/
164+
uint32_t ble_conn_state_conn_count(void);
165+
166+
/**
167+
* @brief Get the total number of connections in which the role of the local
168+
* device is @ref BLE_GAP_ROLE_CENTRAL.
169+
*
170+
* @return The number of connections in which the role of the local device is
171+
* @ref BLE_GAP_ROLE_CENTRAL.
172+
*/
173+
uint32_t ble_conn_state_central_conn_count(void);
174+
175+
/**
176+
* @brief Get the total number of connections in which the role of the local
177+
* device is @ref BLE_GAP_ROLE_PERIPH.
178+
*
179+
* @return The number of connections in which the role of the local device is
180+
* @ref BLE_GAP_ROLE_PERIPH.
181+
*/
182+
uint32_t ble_conn_state_peripheral_conn_count(void);
183+
184+
/**
185+
* @brief Get a list of all connection handles for which the module has a record.
186+
*
187+
* @details This function takes into account connections whose state is
188+
* @ref BLE_CONN_STATUS_DISCONNECTED.
189+
*
190+
* @return A list of all valid connection handles for which the module has a record.
191+
*/
192+
struct ble_conn_state_conn_handle_list ble_conn_state_conn_handles(void);
193+
194+
/**
195+
* @brief Get a list of connection handles in which the role of the local
196+
* device is @ref BLE_GAP_ROLE_CENTRAL.
197+
*
198+
* @details This function takes into account connections whose state is
199+
* @ref BLE_CONN_STATUS_DISCONNECTED.
200+
*
201+
* @return A list of all valid connection handles for which the module has a record and in which
202+
* the role of local device is @ref BLE_GAP_ROLE_CENTRAL.
203+
*/
204+
struct ble_conn_state_conn_handle_list ble_conn_state_central_handles(void);
205+
206+
/**
207+
* @brief Get the handle for the connection in which the role of the local device
208+
* is @ref BLE_GAP_ROLE_PERIPH.
209+
*
210+
* @details This function takes into account connections whose state is
211+
* @ref BLE_CONN_STATUS_DISCONNECTED.
212+
*
213+
* @return A list of all valid connection handles for which the module has a record and in which
214+
* the role of local device is @ref BLE_GAP_ROLE_PERIPH.
215+
*/
216+
struct ble_conn_state_conn_handle_list ble_conn_state_periph_handles(void);
217+
218+
/**
219+
* @brief Translate a connection handle to a value that can be used as an array index.
220+
*
221+
* @details Function for mapping connection handles onto the range <0 - MAX_CONNECTIONS>.
222+
*
223+
* @note The index will be the same as long as a connection is invalid. A subsequent connection with
224+
* the same connection handle might have a different index.
225+
*
226+
* @param[in] conn_handle The connection for which to retrieve an index.
227+
*
228+
* @return An index unique to this connection. Or @ref BLE_CONN_STATE_MAX_CONNECTIONS if
229+
* @c conn_handle refers to an invalid connection.
230+
*/
231+
uint16_t ble_conn_state_conn_idx(uint16_t conn_handle);
232+
233+
/**
234+
* @brief Obtain exclusive access to one of the user flag collections.
235+
*
236+
* @details The acquired collection contains one flag for each connection. These flags can be set
237+
* and read individually for each connection.
238+
*
239+
* The state of user flags will not be modified by the connection state module, except to
240+
* set it to 0 for a connection when that connection is invalidated.
241+
*
242+
* @return The index of the acquired flag,
243+
* or @ref BLE_CONN_STATE_USER_FLAG_INVALID if none are available.
244+
*/
245+
int ble_conn_state_user_flag_acquire(void);
246+
247+
/**
248+
* @brief Read the value of a user flag.
249+
*
250+
* @param[in] conn_handle Handle of connection to get the flag state for.
251+
* @param[in] flag_index Which flag to get the state for.
252+
*
253+
* @return The state of the flag. If @c conn_handle is invalid, the function returns false.
254+
*/
255+
bool ble_conn_state_user_flag_get(uint16_t conn_handle, uint16_t flag_index);
256+
257+
/**
258+
* @brief Set the value of a user flag.
259+
*
260+
* @param[in] conn_handle Handle of connection to set the flag state for.
261+
* @param[in] flag_id Which flag to set the state for.
262+
* @param[in] value Value to set the flag state to.
263+
*/
264+
void ble_conn_state_user_flag_set(uint16_t conn_handle, uint16_t flag_index, bool value);
265+
266+
/**
267+
* @brief Run a function for each active connection.
268+
*
269+
* @param[in] user_function The function to run for each connection.
270+
* @param[in] ctx Arbitrary context to be passed to @p user_function.
271+
*
272+
* @return The number of times @p user_function was run.
273+
*/
274+
uint32_t ble_conn_state_for_each_connected(ble_conn_state_user_function_t user_function,
275+
void *ctx);
276+
277+
/**
278+
* @brief Run a function for each connection that has a user flag set.
279+
*
280+
* @param[in] flag_index Which flag to check.
281+
* @param[in] user_function The function to run when a flag is set.
282+
* @param[in] ctx Arbitrary context to be passed to @p user_function.
283+
*
284+
* @return The number of times @p user_function was run.
285+
*/
286+
uint32_t ble_conn_state_for_each_set_user_flag(uint16_t flag_index,
287+
ble_conn_state_user_function_t user_function,
288+
void *ctx);
289+
290+
/** @} */
291+
/** @} */
292+
293+
#ifdef __cplusplus
294+
}
295+
#endif
296+
297+
#endif /* BLE_CONN_STATE_H__ */

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
add_subdirectory_ifdef(CONFIG_BLE_ADV ble_adv)
88
add_subdirectory_ifdef(CONFIG_BLE_CONN_PARAMS ble_conn_params)
9+
add_subdirectory_ifdef(CONFIG_BLE_CONN_STATE ble_conn_state)
910
add_subdirectory_ifdef(CONFIG_BLE_GATT_QUEUE ble_gq)
1011
add_subdirectory_ifdef(CONFIG_BLE_RACP ble_racp)
1112
add_subdirectory_ifdef(CONFIG_EVENT_SCHEDULER event_scheduler)

lib/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ menu "Libraries"
77

88
rsource "ble_adv/Kconfig"
99
rsource "ble_conn_params/Kconfig"
10+
rsource "ble_conn_state/Kconfig"
1011
rsource "ble_gq/Kconfig"
1112
rsource "ble_racp/Kconfig"
1213
rsource "event_scheduler/Kconfig"

lib/ble_conn_state/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
zephyr_library()
7+
zephyr_library_sources(ble_conn_state.c)

lib/ble_conn_state/Kconfig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
menuconfig BLE_CONN_STATE
7+
bool "Connection state"
8+
depends on NRF_SDH_BLE
9+
help
10+
A library to defer event processing to main().
11+
12+
if BLE_CONN_STATE
13+
14+
config BLE_CONN_STATE_USER_FLAG_COUNT
15+
int "User flag count"
16+
range 0 24
17+
default 24
18+
help
19+
The number of available user flags.
20+
21+
config BLE_CONN_STATE_BLE_OBSERVER_PRIO
22+
int "BLE observer priority"
23+
default 0
24+
25+
module=BLE_CONN_STATE
26+
module-dep=LOG
27+
module-str=BLE Connection state
28+
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"
29+
30+
endif

0 commit comments

Comments
 (0)