diff --git a/CODEOWNERS b/CODEOWNERS index ea8a3dc8ec..9c8955fddc 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -46,6 +46,7 @@ /lib/bm_zms/ @nrfconnect/ncs-bm @rghaddab /lib/ble_adv/ @nrfconnect/ncs-bm /lib/ble_conn_params/ @nrfconnect/ncs-bm +/lib/ble_conn_state/ @nrfconnect/ncs-bm /lib/ble_gq/ @nrfconnect/ncs-bm /lib/ble_qwr/ @nrfconnect/ncs-bm /lib/ble_racp/ @nrfconnect/ncs-bm @@ -87,6 +88,7 @@ # Tests /tests/lib/bm_zms/ @nrfconnect/ncs-bm @rghaddab +/tests/lib/ble_conn_state/ @nrfconnect/ncs-bm /tests/lib/ble_qwr/ @nrfconnect/ncs-bm /tests/lib/ble_racp/ @nrfconnect/ncs-bm /tests/lib/ble_adv/ @nrfconnect/ncs-bm-test diff --git a/doc/nrf-bm/release_notes/release_notes_changelog.rst b/doc/nrf-bm/release_notes/release_notes_changelog.rst index e19bea6259..aecc61da30 100644 --- a/doc/nrf-bm/release_notes/release_notes_changelog.rst +++ b/doc/nrf-bm/release_notes/release_notes_changelog.rst @@ -28,6 +28,7 @@ SoftDevice Handler ================== * Updated the system initialization to initialize on application level. +* Added the :c:func:`nrf_sdh_ble_conn_handle_get` function. Boards ====== diff --git a/include/ble_conn_state.h b/include/ble_conn_state.h new file mode 100644 index 0000000000..05ddc4cafb --- /dev/null +++ b/include/ble_conn_state.h @@ -0,0 +1,302 @@ +/* + * Copyright (c) 2015 - 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +/** + * @file + * + * @defgroup ble_conn_state Connection state + * @ingroup ble_sdk_lib + * @{ + * @brief Module for storing data on BLE connections. + * + * @details This module stores certain states for each connection, which can be queried by + * connection handle. The module uses BLE events to keep the states updated. + * + * In addition to the preprogrammed states, this module can also keep track of a number of + * binary user states, or user flags. These are reset to 0 for new connections, but + * otherwise not touched by this module. + * + * This module uses atomics to make the flag operations thread-safe. + * + * @note A connection handle is not immediately invalidated when it is disconnected. Certain states, + * such as the role, can still be queried until the next time a new connection is established + * to any device. + * + */ + +#ifndef BLE_CONN_STATE_H__ +#define BLE_CONN_STATE_H__ + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @brief Connection handle statuses. */ +enum ble_conn_state_status { + /** The connection handle is invalid. */ + BLE_CONN_STATUS_INVALID, + /** The connection handle refers to a connection that has been disconnected, + * but not yet invalidated. + */ + BLE_CONN_STATUS_DISCONNECTED, + /** The connection handle refers to an active connection. */ + BLE_CONN_STATUS_CONNECTED, +}; + +/** The maximum number of connections supported. */ +#define BLE_CONN_STATE_MAX_CONNECTIONS BLE_GAP_ROLE_COUNT_COMBINED_MAX + +/** UInvalid user flag. */ +#define BLE_CONN_STATE_USER_FLAG_INVALID CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT + +/** @brief Type used to present a list of conn_handles. */ +struct ble_conn_state_conn_handle_list { + /** The length of the list. */ + uint32_t len; + /** The list of handles. */ + uint16_t conn_handles[BLE_CONN_STATE_MAX_CONNECTIONS]; +}; + +/** + * @brief Function to be called when a flag ID is set. + * + * See @ref ble_conn_state_for_each_set_user_flag. + * + * @param[in] conn_handle The connection the flag is set for. + * @param[in] ctx Arbitrary pointer provided by the caller of + * @ref ble_conn_state_for_each_set_user_flag. + */ +typedef void (*ble_conn_state_user_function_t)(uint16_t conn_handle, void *ctx); + +/** + * @defgroup ble_conn_state_functions BLE connection state functions + * @{ + */ + +/** + * @brief Initialize or reset the module. + * + * @details This function sets all states to their default, + * removing all records of connection handles. + */ +void ble_conn_state_init(void); + +/** + * @brief Check whether a connection handle represents a valid connection. + * + * @details A connection might be valid and have a BLE_CONN_STATUS_DISCONNECTED status. + * Those connections are invalidated after a new connection occurs. + * + * @param[in] conn_handle Handle of the connection. + * + * @retval true If @c conn_handle represents a valid connection, thus a connection for which + we have a record. + * @retval false If @c conn_handle is @ref BLE_GAP_ROLE_INVALID, or if it has never been recorded. + */ +bool ble_conn_state_valid(uint16_t conn_handle); + +/** + * @brief Get the role of the local device in a connection. + * + * @param[in] conn_handle Handle of the connection to get the role for. + * + * @return The role of the local device in the connection (see @ref BLE_GAP_ROLES). + * If conn_handle is not valid, the function returns @ref BLE_GAP_ROLE_INVALID. + */ +uint8_t ble_conn_state_role(uint16_t conn_handle); + +/** + * @brief Get the status of a connection. + * + * @param[in] conn_handle Handle of the connection. + * + * @return The status of the connection. + * If conn_handle is not valid, the function returns @ref BLE_CONN_STATE_INVALID. + */ +enum ble_conn_state_status ble_conn_state_status(uint16_t conn_handle); + +/** + * @brief Check whether a connection is encrypted. + * + * @param[in] conn_handle Handle of connection to get the encryption state for. + * + * @retval true If the connection is encrypted. + * @retval false If the connection is not encrypted or conn_handle is invalid. + */ +bool ble_conn_state_encrypted(uint16_t conn_handle); + +/** + * @brief Check whether a connection encryption is protected from Man in the Middle + * (MITM) attacks. + * + * @param[in] conn_handle Handle of connection to get the MITM state for. + * + * @retval true If the connection is encrypted with MITM protection. + * @retval false If the connection is not encrypted, or encryption is not MITM protected, or + * conn_handle is invalid. + */ +bool ble_conn_state_mitm_protected(uint16_t conn_handle); + +/** + * @brief Check whether a connection was bonded using LE Secure Connections (LESC). + * + * The connection must currently be encrypted. + * + * @note This function will report false if bonded, and the LESC bonding was unauthenticated + * ("Just Works") and happened in a previous connection. To detect such cases as well, check + * the stored bonding key, e.g. in Peer Manager, which has a LESC flag associated with it. + * + * @param[in] conn_handle Handle of connection to get the LESC state for. + * + * @retval true If the connection was bonded using LESC. + * @retval false If the connection has not been bonded using LESC, or @c conn_handle is invalid. + */ +bool ble_conn_state_lesc(uint16_t conn_handle); + +/** + * @brief Get the total number of connections. + * + * @return The total number of valid connections for which the module has a record. + */ +uint32_t ble_conn_state_conn_count(void); + +/** + * @brief Get the total number of connections in which the role of the local + * device is @ref BLE_GAP_ROLE_CENTRAL. + * + * @return The number of connections in which the role of the local device is + * @ref BLE_GAP_ROLE_CENTRAL. + */ +uint32_t ble_conn_state_central_conn_count(void); + +/** + * @brief Get the total number of connections in which the role of the local + * device is @ref BLE_GAP_ROLE_PERIPH. + * + * @return The number of connections in which the role of the local device is + * @ref BLE_GAP_ROLE_PERIPH. + */ +uint32_t ble_conn_state_peripheral_conn_count(void); + +/** + * @brief Get a list of all connection handles for which the module has a record. + * + * @details This function takes into account connections whose state is + * @ref BLE_CONN_STATUS_DISCONNECTED. + * + * @return A list of all valid connection handles for which the module has a record. + */ +struct ble_conn_state_conn_handle_list ble_conn_state_conn_handles(void); + +/** + * @brief Get a list of connection handles in which the role of the local + * device is @ref BLE_GAP_ROLE_CENTRAL. + * + * @details This function takes into account connections whose state is + * @ref BLE_CONN_STATUS_DISCONNECTED. + * + * @return A list of all valid connection handles for which the module has a record and in which + * the role of local device is @ref BLE_GAP_ROLE_CENTRAL. + */ +struct ble_conn_state_conn_handle_list ble_conn_state_central_handles(void); + +/** + * @brief Get the handle for the connection in which the role of the local device + * is @ref BLE_GAP_ROLE_PERIPH. + * + * @details This function takes into account connections whose state is + * @ref BLE_CONN_STATUS_DISCONNECTED. + * + * @return A list of all valid connection handles for which the module has a record and in which + * the role of local device is @ref BLE_GAP_ROLE_PERIPH. + */ +struct ble_conn_state_conn_handle_list ble_conn_state_periph_handles(void); + +/** + * @brief Translate a connection handle to a value that can be used as an array index. + * + * @details Function for mapping connection handles onto the range <0 - MAX_CONNECTIONS>. + * + * @note The index will be the same as long as a connection is invalid. A subsequent connection with + * the same connection handle might have a different index. + * + * @param[in] conn_handle The connection for which to retrieve an index. + * + * @return An index unique to this connection. Or @ref BLE_CONN_STATE_MAX_CONNECTIONS if + * @c conn_handle refers to an invalid connection. + */ +uint16_t ble_conn_state_conn_idx(uint16_t conn_handle); + +/** + * @brief Obtain exclusive access to one of the user flag collections. + * + * @details The acquired collection contains one flag for each connection. These flags can be set + * and read individually for each connection. + * + * The state of user flags will not be modified by the connection state module, except to + * set it to 0 for a connection when that connection is invalidated. + * + * @return The index of the acquired flag, + * or BLE_CONN_STATE_USER_FLAG_INVALID if none are available. + */ +int ble_conn_state_user_flag_acquire(void); + +/** + * @brief Read the value of a user flag. + * + * @param[in] conn_handle Handle of connection to get the flag state for. + * @param[in] flag_index Which flag to get the state for. + * + * @return The state of the flag. If @c conn_handle is invalid, the function returns false. + */ +bool ble_conn_state_user_flag_get(uint16_t conn_handle, uint16_t flag_index); + +/** + * @brief Set the value of a user flag. + * + * @param[in] conn_handle Handle of connection to set the flag state for. + * @param[in] flag_id Which flag to set the state for. + * @param[in] value Value to set the flag state to. + */ +void ble_conn_state_user_flag_set(uint16_t conn_handle, uint16_t flag_index, bool value); + +/** + * @brief Run a function for each active connection. + * + * @param[in] user_function The function to run for each connection. + * @param[in] ctx Arbitrary context to be passed to @p user_function. + * + * @return The number of times @p user_function was run. + */ +uint32_t ble_conn_state_for_each_connected(ble_conn_state_user_function_t user_function, + void *ctx); + +/** + * @brief Run a function for each connection that has a user flag set. + * + * @param[in] flag_index Which flag to check. + * @param[in] user_function The function to run when a flag is set. + * @param[in] ctx Arbitrary context to be passed to @p user_function. + * + * @return The number of times @p user_function was run. + */ +uint32_t ble_conn_state_for_each_set_user_flag(uint16_t flag_index, + ble_conn_state_user_function_t user_function, + void *ctx); + +/** @} */ +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* BLE_CONN_STATE_H__ */ diff --git a/include/nrf_sdh_ble.h b/include/nrf_sdh_ble.h index 97f8c563b8..6a54a0b453 100644 --- a/include/nrf_sdh_ble.h +++ b/include/nrf_sdh_ble.h @@ -106,6 +106,15 @@ static inline int nrf_sdh_ble_idx_get(uint16_t conn_handle) return _nrf_sdh_ble_idx_get(conn_handle); } +/** + * @brief Get the connection handle for an assigned index. + * + * @param[in] idx Assigned index. + * + * @returns The connection handle for the given index or @c BLE_CONN_HANDLE_INVALID if not found. + */ +uint16_t nrf_sdh_ble_conn_handle_get(int idx); + #ifdef __cplusplus } #endif diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 7cd28e8ee8..6549406a14 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -8,6 +8,7 @@ add_subdirectory_ifdef(CONFIG_BLE_ADV ble_adv) add_subdirectory_ifdef(CONFIG_BLE_CONN_PARAMS ble_conn_params) add_subdirectory_ifdef(CONFIG_BLE_GATT_QUEUE ble_gq) add_subdirectory_ifdef(CONFIG_BLE_RACP ble_racp) +add_subdirectory_ifdef(CONFIG_BLE_CONN_STATE ble_conn_state) add_subdirectory_ifdef(CONFIG_EVENT_SCHEDULER event_scheduler) add_subdirectory_ifdef(CONFIG_BM_BUTTONS bm_buttons) add_subdirectory_ifdef(CONFIG_BM_STORAGE bm_storage) diff --git a/lib/Kconfig b/lib/Kconfig index a1fe1029d0..7292652078 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -9,6 +9,7 @@ rsource "ble_adv/Kconfig" rsource "ble_conn_params/Kconfig" rsource "ble_gq/Kconfig" rsource "ble_racp/Kconfig" +rsource "ble_conn_state/Kconfig" rsource "event_scheduler/Kconfig" rsource "bm_buttons/Kconfig" rsource "bm_storage/Kconfig" diff --git a/lib/ble_conn_state/CMakeLists.txt b/lib/ble_conn_state/CMakeLists.txt new file mode 100644 index 0000000000..26089e31a5 --- /dev/null +++ b/lib/ble_conn_state/CMakeLists.txt @@ -0,0 +1,7 @@ +# +# Copyright (c) 2025 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +zephyr_library() +zephyr_library_sources(ble_conn_state.c) diff --git a/lib/ble_conn_state/Kconfig b/lib/ble_conn_state/Kconfig new file mode 100644 index 0000000000..c90ccac6a9 --- /dev/null +++ b/lib/ble_conn_state/Kconfig @@ -0,0 +1,25 @@ +# +# Copyright (c) 2025 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +menuconfig BLE_CONN_STATE + bool "Event scheduler" + help + A library to defer event processing to main(). + +if BLE_CONN_STATE + +config BLE_CONN_STATE_USER_FLAG_COUNT + int "User flag count" + range 0 24 + default 24 + help + The number of available user flags. + +module=BLE_CONN_STATE +module-dep=LOG +module-str=BLE Connection state +source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config" + +endif diff --git a/lib/ble_conn_state/ble_conn_state.c b/lib/ble_conn_state/ble_conn_state.c new file mode 100644 index 0000000000..eef4ba3b3c --- /dev/null +++ b/lib/ble_conn_state/ble_conn_state.c @@ -0,0 +1,424 @@ +/* + * Copyright (c) 2015 - 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(ble_bms, CONFIG_BLE_CONN_STATE_LOG_LEVEL); + +#define BLE_CONN_STATE_DEFAULT_FLAG_COLLECTION_COUNT 6 + +/** The number of flags kept for each connection, including user flags. */ +#define TOTAL_FLAG_COLLECTION_COUNT (BLE_CONN_STATE_DEFAULT_FLAG_COLLECTION_COUNT + \ + CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT) + +/** + * @brief Structure containing all the flag collections maintained by the Connection State module. + */ +struct ble_conn_state_flag_collections { + /** Flags indicating which connection handles are valid. */ + atomic_t valid_flags; + /** Flags indicating which connections are connected, since disconnected connection handles + * will not immediately be invalidated. + */ + atomic_t connected_flags; + /** Flags indicating in which connections the local device is the central. */ + atomic_t central_flags; + /** Flags indicating which connections are encrypted. */ + atomic_t encrypted_flags; + /** Flags indicating which connections have encryption with protection from + * man-in-the-middle attacks. + */ + atomic_t mitm_protected_flags; + /** Flags indicating which connections have bonded using LE Secure Connections (LESC). */ + atomic_t lesc_flags; + /** Flags that can be reserved by the user. The flags will be cleared when a connection is + * invalidated, otherwise, the user is entirely responsible for the flag states. + */ + atomic_t user_flags[CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT]; +}; + +/** @brief Structure containing the internal state of the Connection State module. */ +struct ble_conn_state { + /** Bitmap for keeping track of which user flags have been acquired. */ + atomic_t acquired_flags; + union { + /** Flag collection kept by the Connections State module. */ + struct ble_conn_state_flag_collections flags; + /** Flag collections as array to allow iterating over all flag collections. */ + atomic_t flag_array[TOTAL_FLAG_COLLECTION_COUNT]; + }; +}; + +/** Instantiation of the internal state. */ +static struct ble_conn_state bcs = {0}; + +static bool ble_conn_state_valid_idx(int idx) +{ + if (idx < 0) { + return false; + } + + return atomic_test_bit(&bcs.flags.valid_flags, idx); +} + +static void flag_toggle(atomic_t *flags, int idx, bool value) +{ + if (value) { + atomic_set_bit(flags, idx); + } else { + atomic_clear_bit(flags, idx); + } +} + +static uint32_t active_flag_count(atomic_t flags) +{ + uint32_t set_flag_count = 0; + + for (uint32_t i = 0; i < BLE_CONN_STATE_MAX_CONNECTIONS; i++) { + if (atomic_test_bit(&flags, i)) { + set_flag_count += 1; + } + } + + return set_flag_count; +} + +static bool record_activate(int idx) +{ + if (idx < 0) { + return false; + } + + atomic_set_bit(&bcs.flags.connected_flags, idx); + atomic_set_bit(&bcs.flags.valid_flags, idx); + return true; +} + +static void record_set_disconnected(int idx) +{ + atomic_clear_bit(&bcs.flags.connected_flags, idx); +} + +static struct ble_conn_state_conn_handle_list conn_handle_list_get(atomic_t flags); + +static void record_purge_disconnected(void) +{ + atomic_t disconnected_flags = ~bcs.flags.connected_flags; + struct ble_conn_state_conn_handle_list disconnected_list; + + (void)atomic_and(&disconnected_flags, bcs.flags.valid_flags); + disconnected_list = conn_handle_list_get(disconnected_flags); + + for (uint32_t i = 0; i < disconnected_list.len; i++) { + /* Invalidate record */ + for (uint32_t j = 0; j < TOTAL_FLAG_COLLECTION_COUNT; j++) { + atomic_clear_bit(&bcs.flag_array[j], + nrf_sdh_ble_idx_get(disconnected_list.conn_handles[i])); + } + } +} + +static bool user_flag_is_acquired(uint32_t flag_index) +{ + return atomic_test_bit(&bcs.acquired_flags, flag_index); +} + +static uint32_t for_each_set_flag(atomic_t flags, ble_conn_state_user_function_t user_function, + void *ctx) +{ + uint32_t call_count = 0; + + if (!user_function || !flags) { + return 0; + } + + for (uint32_t idx = 0; idx < BLE_CONN_STATE_MAX_CONNECTIONS; idx++) { + if (atomic_test_bit(&flags, idx)) { + user_function(nrf_sdh_ble_conn_handle_get(idx), ctx); + call_count += 1; + } + } + + return call_count; +} + +static struct ble_conn_state_conn_handle_list conn_handle_list_get(atomic_t flags) +{ + uint16_t conn_handle; + struct ble_conn_state_conn_handle_list conn_handle_list = { + .len = 0 + }; + + if (!flags) { + /* return empty list */ + return conn_handle_list; + } + + for (uint32_t i = 0; i < BLE_CONN_STATE_MAX_CONNECTIONS; i++) { + if (atomic_test_bit(&flags, i)) { + conn_handle = nrf_sdh_ble_conn_handle_get(i); + conn_handle_list.conn_handles[conn_handle_list.len++] = conn_handle; + } + } + + return conn_handle_list; +} + +void ble_conn_state_init(void) +{ + memset(&bcs, 0, sizeof(struct ble_conn_state)); +} + +bool ble_conn_state_valid(uint16_t conn_handle) +{ + int idx = nrf_sdh_ble_idx_get(conn_handle); + + return ble_conn_state_valid_idx(idx); +} + +uint8_t ble_conn_state_role(uint16_t conn_handle) +{ + int idx = nrf_sdh_ble_idx_get(conn_handle); + uint8_t role = BLE_GAP_ROLE_INVALID; + + if (!ble_conn_state_valid_idx(idx)) { + return BLE_GAP_ROLE_INVALID; + } + +#if defined(CONFIG_SOFTDEVICE_PERIPHERAL) && defined(CONFIG_SOFTDEVICE_CENTRAL) + bool central; + + central = atomic_test_bit(&bcs.flags.central_flags, idx); + role = central ? BLE_GAP_ROLE_CENTRAL : BLE_GAP_ROLE_PERIPH; +#elif defined(CONFIG_SOFTDEVICE_CENTRAL) + role = BLE_GAP_ROLE_CENTRAL; +#else + role = BLE_GAP_ROLE_PERIPH; +#endif + + return role; +} + +enum ble_conn_state_status ble_conn_state_status(uint16_t conn_handle) +{ + int idx = nrf_sdh_ble_idx_get(conn_handle); + bool connected; + enum ble_conn_state_status conn_status = BLE_CONN_STATUS_INVALID; + + if (!ble_conn_state_valid_idx(idx)) { + return BLE_CONN_STATUS_INVALID; + } + + connected = atomic_test_bit(&bcs.flags.connected_flags, idx); + conn_status = connected ? BLE_CONN_STATUS_CONNECTED : BLE_CONN_STATUS_DISCONNECTED; + + return conn_status; +} + +bool ble_conn_state_encrypted(uint16_t conn_handle) +{ + int idx = nrf_sdh_ble_idx_get(conn_handle); + + if (!ble_conn_state_valid_idx(idx)) { + return false; + } + + return atomic_test_bit(&bcs.flags.encrypted_flags, idx); +} + +bool ble_conn_state_mitm_protected(uint16_t conn_handle) +{ + int idx = nrf_sdh_ble_idx_get(conn_handle); + + if (!ble_conn_state_valid_idx(idx)) { + return false; + } + + return atomic_test_bit(&bcs.flags.mitm_protected_flags, idx); +} + +bool ble_conn_state_lesc(uint16_t conn_handle) +{ + int idx = nrf_sdh_ble_idx_get(conn_handle); + + if (!ble_conn_state_valid_idx(idx)) { + return false; + } + + return atomic_test_bit(&bcs.flags.lesc_flags, idx); +} + +uint32_t ble_conn_state_conn_count(void) +{ + return active_flag_count(bcs.flags.connected_flags); +} + +uint32_t ble_conn_state_central_conn_count(void) +{ + atomic_t central_conn_flags; + + central_conn_flags = bcs.flags.central_flags; + (void)atomic_and(¢ral_conn_flags, bcs.flags.connected_flags); + + return active_flag_count(central_conn_flags); +} + +uint32_t ble_conn_state_peripheral_conn_count(void) +{ + atomic_t peripheral_conn_flags; + + peripheral_conn_flags = ~bcs.flags.central_flags; + (void)atomic_and(&peripheral_conn_flags, bcs.flags.connected_flags); + + return active_flag_count(peripheral_conn_flags); +} + +struct ble_conn_state_conn_handle_list ble_conn_state_conn_handles(void) +{ + return conn_handle_list_get(bcs.flags.valid_flags); +} + +struct ble_conn_state_conn_handle_list ble_conn_state_central_handles(void) +{ + atomic_t central_conn_flags; + + central_conn_flags = bcs.flags.central_flags; + (void)atomic_and(¢ral_conn_flags, bcs.flags.connected_flags); + + return conn_handle_list_get(central_conn_flags); +} + +struct ble_conn_state_conn_handle_list ble_conn_state_periph_handles(void) +{ + atomic_t peripheral_conn_flags; + + peripheral_conn_flags = ~bcs.flags.central_flags; + (void)atomic_and(&peripheral_conn_flags, bcs.flags.connected_flags); + + return conn_handle_list_get(peripheral_conn_flags); +} + +uint16_t ble_conn_state_conn_idx(uint16_t conn_handle) +{ + int idx = nrf_sdh_ble_idx_get(conn_handle); + + if (!ble_conn_state_valid_idx(idx)) { + return BLE_CONN_STATE_MAX_CONNECTIONS; + } + + + return idx; +} + +int ble_conn_state_user_flag_acquire(void) +{ + for (int i = 0; i < CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT; i++) { + if (atomic_test_and_set_bit(&bcs.acquired_flags, i) == false) { + /* Bit was not set before */ + return i; + } + } + + return BLE_CONN_STATE_USER_FLAG_INVALID; +} + +bool ble_conn_state_user_flag_get(uint16_t conn_handle, uint16_t flag_index) +{ + int idx = nrf_sdh_ble_idx_get(conn_handle); + + if (!user_flag_is_acquired(flag_index) || !ble_conn_state_valid_idx(idx)) { + return false; + } + + return atomic_test_bit(&bcs.flags.user_flags[flag_index], idx); +} + +void ble_conn_state_user_flag_set(uint16_t conn_handle, uint16_t flag_index, bool value) +{ + int idx = nrf_sdh_ble_idx_get(conn_handle); + + if (!user_flag_is_acquired(flag_index) || !ble_conn_state_valid_idx(idx)) { + return; + } + + flag_toggle(&bcs.flags.user_flags[flag_index], idx, value); +} + +uint32_t ble_conn_state_for_each_connected(ble_conn_state_user_function_t user_function, void *ctx) +{ + return for_each_set_flag(bcs.flags.connected_flags, user_function, ctx); +} + +uint32_t ble_conn_state_for_each_set_user_flag(uint16_t flag_index, + ble_conn_state_user_function_t user_function, + void *ctx) +{ + if (!user_flag_is_acquired(flag_index)) { + return 0; + } + + return for_each_set_flag(bcs.flags.user_flags[flag_index], user_function, ctx); +} + +#ifdef CONFIG_UNITY +void ble_evt_handler(ble_evt_t const *ble_evt, void *ctx) +#else +static void ble_evt_handler(ble_evt_t const *ble_evt, void *ctx) +#endif +{ + int idx = nrf_sdh_ble_idx_get(ble_evt->evt.gap_evt.conn_handle); + + switch (ble_evt->header.evt_id) { + case BLE_GAP_EVT_CONNECTED: + record_purge_disconnected(); + + if (!record_activate(idx)) { + /* No more records available. Should not happen. */ + LOG_ERR("No more records available"); + __ASSERT(false, "No more records available"); +#ifdef CONFIG_BLE_GAP_ROLE_CENTRAL + } else if (ble_evt->evt.gap_evt.params.connected.role == BLE_GAP_ROLE_CENTRAL) { + /* Central */ + atomic_set_bit(&bcs.flags.central_flags, idx); +#endif + } else { + /* No implementation required. */ + } + + break; + case BLE_GAP_EVT_DISCONNECTED: + record_set_disconnected(idx); + break; + case BLE_GAP_EVT_CONN_SEC_UPDATE: + uint8_t sec_lv = ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.lv; + + /* Set/unset flags based on security level. */ + flag_toggle(&bcs.flags.lesc_flags, idx, sec_lv >= 4); + flag_toggle(&bcs.flags.mitm_protected_flags, idx, sec_lv >= 3); + flag_toggle(&bcs.flags.encrypted_flags, idx, sec_lv >= 2); + break; + case BLE_GAP_EVT_AUTH_STATUS: + if (ble_evt->evt.gap_evt.params.auth_status.auth_status == + BLE_GAP_SEC_STATUS_SUCCESS) { + bool lesc = ble_evt->evt.gap_evt.params.auth_status.lesc; + + flag_toggle(&bcs.flags.lesc_flags, idx, lesc); + } + + break; + } +} + +NRF_SDH_BLE_OBSERVER(ble_evt_observer, ble_evt_handler, NULL, BLE_CONN_STATE_BLE_OBSERVER_PRIO); diff --git a/subsys/softdevice_handler/nrf_sdh_ble.c b/subsys/softdevice_handler/nrf_sdh_ble.c index 134106cab1..57a72ddaaf 100644 --- a/subsys/softdevice_handler/nrf_sdh_ble.c +++ b/subsys/softdevice_handler/nrf_sdh_ble.c @@ -224,6 +224,15 @@ int _nrf_sdh_ble_idx_get(uint16_t conn_handle) return -1; } +uint16_t nrf_sdh_ble_conn_handle_get(int idx) +{ + if (idx >= 0 && idx < ARRAY_SIZE(conn_handles)) { + return conn_handles[idx]; + } + + return BLE_CONN_HANDLE_INVALID; +} + static void idx_assign(uint16_t conn_handle) { __ASSERT(conn_handle != BLE_CONN_HANDLE_INVALID, "Got invalid conn_handle from SoftDevice"); diff --git a/tests/lib/ble_conn_state/CMakeLists.txt b/tests/lib/ble_conn_state/CMakeLists.txt new file mode 100644 index 0000000000..a66da69e4a --- /dev/null +++ b/tests/lib/ble_conn_state/CMakeLists.txt @@ -0,0 +1,40 @@ +# +# Copyright (c) 2025 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(unit_test_ble_conn_state) + +set(SOFTDEVICE_VARIANT "s115") +set(SOFTDEVICE_INCLUDE_DIR "${ZEPHYR_NRF_BM_MODULE_DIR}/components/softdevice/\ +${SOFTDEVICE_VARIANT}/${SOFTDEVICE_VARIANT}_API/include") + +target_compile_definitions( app PRIVATE + SVCALL_AS_NORMAL_FUNCTION + SUPPRESS_INLINE_IMPLEMENTATION + NRF54L15_XXAA + CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT=64 + CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT=24 + CONFIG_BLE_CONN_STATE_DEFAULT_FLAG_COLLECTION_COUNT=6 +) + +# Generate and add test file +test_runner_generate(src/unity_test.c) +target_sources(app PRIVATE src/unity_test.c) + +target_include_directories(app PRIVATE + ${ZEPHYR_NRF_BM_MODULE_DIR}/include + ${ZEPHYR_NRF_BM_MODULE_DIR}/include + ${SOFTDEVICE_INCLUDE_DIR} + ${ZEPHYR_HAL_NORDIC_MODULE_DIR}/nrfx/mdk + ${ZEPHYR_CMSIS_MODULE_DIR}/CMSIS/Core/Include +) + +target_sources(app PRIVATE + ${ZEPHYR_NRF_BM_MODULE_DIR}/lib/ble_conn_state/ble_conn_state.c +) diff --git a/tests/lib/ble_conn_state/prj.conf b/tests/lib/ble_conn_state/prj.conf new file mode 100644 index 0000000000..a8788847da --- /dev/null +++ b/tests/lib/ble_conn_state/prj.conf @@ -0,0 +1,6 @@ +# +# Copyright (c) 2025 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +CONFIG_UNITY=y diff --git a/tests/lib/ble_conn_state/src/unity_test.c b/tests/lib/ble_conn_state/src/unity_test.c new file mode 100644 index 0000000000..7154bb9ab9 --- /dev/null +++ b/tests/lib/ble_conn_state/src/unity_test.c @@ -0,0 +1,1078 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ +#include +#include +#include +#include +#include +#include + +#include + +static uint16_t conn_handle1; +static uint16_t conn_handle2 = 1; +static uint16_t conn_handle3 = 2; +static uint16_t conn_handle4 = 3; +static uint16_t conn_handle5 = 4; +static uint16_t conn_handle6 = 5; +static uint16_t conn_handle7 = 10; +static uint16_t conn_handle8 = 19; + +static uint16_t conn_handles_registered[CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT] = { + [0 ... CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT - 1] = BLE_CONN_HANDLE_INVALID, +}; + +void conn_handle_register(uint16_t conn_handle) +{ + for (int i = 0; i < ARRAY_SIZE(conn_handles_registered) ; i++) { + if (conn_handles_registered[i] == BLE_CONN_HANDLE_INVALID) { + conn_handles_registered[i] = conn_handle; + return; + } + } +} + +void conn_handle_deregister(uint16_t conn_handle) +{ + for (int i = 0; i < ARRAY_SIZE(conn_handles_registered) ; i++) { + if (conn_handles_registered[i] == conn_handle) { + conn_handles_registered[i] = BLE_CONN_HANDLE_INVALID; + return; + } + } +} + +int _nrf_sdh_ble_idx_get(uint16_t conn_handle) +{ + + for (int i = 0; i < ARRAY_SIZE(conn_handles_registered) ; i++) { + if (conn_handles_registered[i] == conn_handle) { + return i; + } + } + + return -1; +} + +uint16_t nrf_sdh_ble_conn_handle_get(int idx) +{ + return conn_handles_registered[idx]; +} + +extern void ble_evt_handler(ble_evt_t const *ble_evt, void *ctx); + +static uint32_t arbitrary_context; +/* Conn handle that will cause flag operations to overflow into next flag if not checked. */ +static uint16_t conn_handle_overflow = 32; + +static uint32_t calls; +static uint16_t conn_handles[10] = {BLE_CONN_HANDLE_INVALID}; +static void *contexts[10] = {NULL}; + +void ble_evt_init(ble_evt_t *ble_evt, uint8_t evt_id, uint16_t conn_handle) +{ + memset(ble_evt, 0, sizeof(ble_evt_t)); + ble_evt->header.evt_id = evt_id; + ble_evt->evt.gap_evt.conn_handle = conn_handle; + ble_evt->header.evt_len = sizeof(ble_gap_evt_t); +} + +void connected_evt_constuct(ble_evt_t *ble_evt, uint16_t conn_handle, uint8_t role) +{ + ble_evt_init(ble_evt, BLE_GAP_EVT_CONNECTED, conn_handle); + ble_evt->evt.gap_evt.params.connected.role = role; +} + +void disconnected_evt_constuct(ble_evt_t *ble_evt, uint16_t conn_handle) +{ + ble_evt_init(ble_evt, BLE_GAP_EVT_DISCONNECTED, conn_handle); +} + +void conn_sec_update_evt_constuct(ble_evt_t *ble_evt, uint16_t conn_handle, uint8_t level) +{ + ble_evt_init(ble_evt, BLE_GAP_EVT_CONN_SEC_UPDATE, conn_handle); + ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.lv = level; +} + +void auth_status_evt_constuct(ble_evt_t *ble_evt, uint16_t conn_handle, bool lesc, + uint8_t auth_status) +{ + ble_evt_init(ble_evt, BLE_GAP_EVT_AUTH_STATUS, conn_handle); + ble_evt->evt.gap_evt.params.auth_status.auth_status = auth_status; + ble_evt->evt.gap_evt.params.auth_status.lesc = lesc; +} + +ble_evt_t *connected_evt(uint16_t conn_handle, uint8_t role) +{ + static ble_evt_t ble_evt; + + connected_evt_constuct(&ble_evt, conn_handle, role); + return &ble_evt; +} + +ble_evt_t *disconnected_evt(uint16_t conn_handle) +{ + static ble_evt_t ble_evt; + + disconnected_evt_constuct(&ble_evt, conn_handle); + return &ble_evt; +} + +ble_evt_t *conn_sec_update_evt(uint16_t conn_handle, uint8_t level) +{ + static ble_evt_t ble_evt; + + conn_sec_update_evt_constuct(&ble_evt, conn_handle, level); + return &ble_evt; +} + +ble_evt_t *auth_status_evt(uint16_t conn_handle, bool lesc, uint8_t auth_status) +{ + static ble_evt_t ble_evt; + + auth_status_evt_constuct(&ble_evt, conn_handle, lesc, auth_status); + return &ble_evt; +} + +void test_ble_conn_state_init(void) +{ + uint8_t dummy_role = BLE_GAP_ROLE_PERIPH; + uint32_t valid_conn_handles; + + conn_handle1 = 0; + conn_handle2 = 1; + conn_handle3 = 2; + conn_handle4 = 3; + conn_handle5 = 4; + conn_handle6 = 5; + conn_handle7 = 10; + conn_handle8 = 19; + + conn_handle_register(conn_handle1); + conn_handle_register(conn_handle2); + conn_handle_register(conn_handle3); + conn_handle_register(conn_handle4); + conn_handle_register(conn_handle5); + conn_handle_register(conn_handle6); + conn_handle_register(conn_handle7); + conn_handle_register(conn_handle8); + + ble_evt_handler(connected_evt(conn_handle1, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle2, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle3, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle4, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle5, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle6, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle7, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle8, dummy_role), NULL); + + valid_conn_handles = ble_conn_state_conn_count(); + TEST_ASSERT(valid_conn_handles > 0); + + ble_conn_state_init(); + + valid_conn_handles = ble_conn_state_conn_count(); + TEST_ASSERT_EQUAL(0, valid_conn_handles); +} + +void test_ble_conn_state_valid(void) +{ + bool valid; + + conn_handle1 = 0; + conn_handle2 = 1; + conn_handle3 = 2; + conn_handle4 = 3; + conn_handle5 = 4; + conn_handle6 = 5; + conn_handle7 = 10; + conn_handle8 = BLE_CONN_STATE_MAX_CONNECTIONS-1; + uint8_t dummy_role = BLE_GAP_ROLE_PERIPH; + char msg[] = "conn_handle = 65535"; + + valid = ble_conn_state_valid(BLE_CONN_HANDLE_INVALID); + TEST_ASSERT_FALSE(valid); + + /* Testing that all conn. handles are invalid at first. */ + for (uint16_t conn_handle = 0; conn_handle < (65535); conn_handle++) { + valid = ble_conn_state_valid(conn_handle); + TEST_ASSERT_FALSE(valid); + } + + conn_handle_register(conn_handle1); + conn_handle_register(conn_handle2); + conn_handle_register(conn_handle3); + conn_handle_register(conn_handle4); + conn_handle_register(conn_handle5); + conn_handle_register(conn_handle6); + conn_handle_register(conn_handle7); + conn_handle_register(conn_handle8); + + /* Activate some conn. handles and check that those are reported as valid. */ + ble_evt_handler(connected_evt(conn_handle1, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle2, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle3, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle4, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle5, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle6, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle7, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle8, dummy_role), NULL); + + for (uint16_t conn_handle = 0; conn_handle < (65535); conn_handle++) { + valid = ble_conn_state_valid(conn_handle); + if ((conn_handle == conn_handle1) || (conn_handle == conn_handle2) || + (conn_handle == conn_handle3) || (conn_handle == conn_handle4) || + (conn_handle == conn_handle5) || (conn_handle == conn_handle6) || + (conn_handle == conn_handle7) || (conn_handle == conn_handle8)) { + TEST_ASSERT(valid); + } else { + TEST_ASSERT_FALSE(valid); + } + } + + /* Deactivate some conn handles and check that they are still valid + * Handles which are disconnected should still be valid until a connect event occurs + */ + ble_evt_handler(disconnected_evt(conn_handle2), NULL); + ble_evt_handler(disconnected_evt(conn_handle3), NULL); + ble_evt_handler(disconnected_evt(conn_handle7), NULL); + + for (uint16_t conn_handle = 0; conn_handle < (65535); conn_handle++) { + valid = ble_conn_state_valid(conn_handle); + if ((conn_handle == conn_handle1) || (conn_handle == conn_handle2) || + (conn_handle == conn_handle3) || (conn_handle == conn_handle4) || + (conn_handle == conn_handle5) || (conn_handle == conn_handle6) || + (conn_handle == conn_handle7) || (conn_handle == conn_handle8)) { + /* Handles which are disconnected are still valid until a connect + * event occurs + */ + TEST_ASSERT(valid); + } else { + TEST_ASSERT_FALSE_MESSAGE(valid, msg); + } + } + + /* Reactivating a connection handle and checking that the disconnected handles are now + * invalid + */ + conn_handle_register(conn_handle3); + ble_evt_handler(connected_evt(conn_handle3, dummy_role), NULL); + + for (uint16_t conn_handle = 0; conn_handle < (65535); conn_handle++) { + sprintf(msg, "conn_handle = %d", conn_handle); + valid = ble_conn_state_valid(conn_handle); + if ((conn_handle == conn_handle1) || (conn_handle == conn_handle3) || + (conn_handle == conn_handle4) || (conn_handle == conn_handle5) || + (conn_handle == conn_handle6) || (conn_handle == conn_handle8)) { + TEST_ASSERT(valid); + } else { + TEST_ASSERT_FALSE_MESSAGE(valid, msg); + } + } +} + +void test_ble_conn_state_role(void) +{ + uint8_t role; + + uint16_t conn_handle1 = 15; + uint16_t conn_handle2 = 16; + + conn_handle_register(conn_handle1); + conn_handle_register(conn_handle2); + + /* Testing that invalid handle has an invalid role. */ + role = ble_conn_state_role(BLE_CONN_HANDLE_INVALID); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_INVALID, role); + + /* Testing that invalid handle (not yet recorded) has an invalid role. */ + role = ble_conn_state_role(conn_handle1); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_INVALID, role); + +#if defined(BLE_GAP_ROLE_CENTRAL) + /* Activating a connection with CENTRAL role. */ + conn_handle_register(conn_handle1); + ble_evt_handler(connected_evt(conn_handle1, BLE_GAP_ROLE_CENTRAL), NULL); + + /* Test that the role is properly returned. */ + role = ble_conn_state_role(conn_handle1); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_CENTRAL, role); + + /* The role should still be invalid for this other handle. */ + role = ble_conn_state_role(conn_handle2); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_INVALID, role); + + /* Disconnect a handle and test that it still has a valid role, + * until a new connection occurs. + */ + ble_evt_handler(disconnected_evt(conn_handle1), NULL); + role = ble_conn_state_role(conn_handle1); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_CENTRAL, role); + + /* Test that a disconnected handle is invalidated after a connection has occurred. */ + conn_handle_register(conn_handle2); + ble_evt_handler(connected_evt(conn_handle2, BLE_GAP_ROLE_CENTRAL), NULL); + role = ble_conn_state_role(conn_handle1); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_INVALID, role); +#endif + /* (Re)activate both connections */ + ble_evt_handler(disconnected_evt(conn_handle1), NULL); + ble_evt_handler(disconnected_evt(conn_handle2), NULL); +#if defined(BLE_GAP_ROLE_CENTRAL) + conn_handle_register(conn_handle1); + ble_evt_handler(connected_evt(conn_handle1, BLE_GAP_ROLE_CENTRAL), NULL); +#endif + + conn_handle_register(conn_handle2); + ble_evt_handler(connected_evt(conn_handle2, BLE_GAP_ROLE_PERIPH), NULL); + +#if defined(BLE_GAP_ROLE_CENTRAL) + role = ble_conn_state_role(conn_handle1); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_CENTRAL, role); +#endif + + role = ble_conn_state_role(conn_handle2); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_PERIPH, role); + + /* Testing overflow of conn_handle. */ + ble_conn_state_init(); + role = ble_conn_state_role(conn_handle_overflow); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_INVALID, role); + role = ble_conn_state_role(BLE_CONN_HANDLE_INVALID); + TEST_ASSERT_EQUAL_UINT(BLE_GAP_ROLE_INVALID, role); +} + +void test_ble_conn_state_encrypted(void) +{ + bool encrypted, mitm_protected, lesc; + + conn_handle1 = 12; + conn_handle2 = 17; /* dummy conn handle */ + + conn_handle_register(conn_handle1); + conn_handle_register(conn_handle2); + + /* Testing that an invalid handle returns unencrypted. */ + encrypted = ble_conn_state_encrypted(BLE_CONN_HANDLE_INVALID); + mitm_protected = ble_conn_state_mitm_protected(BLE_CONN_HANDLE_INVALID); + lesc = ble_conn_state_lesc(BLE_CONN_HANDLE_INVALID); + TEST_ASSERT_FALSE(encrypted); + TEST_ASSERT_FALSE(mitm_protected); + TEST_ASSERT_FALSE(lesc); + + /* Testing that an inactive handle returns unencrypted. */ + encrypted = ble_conn_state_encrypted(conn_handle1); + mitm_protected = ble_conn_state_mitm_protected(conn_handle1); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT_FALSE(encrypted); + TEST_ASSERT_FALSE(mitm_protected); + TEST_ASSERT_FALSE(lesc); + + /* Testing that an active, unencrypted handle returns unencrypted. */ + ble_evt_handler(connected_evt(conn_handle1, BLE_GAP_ROLE_PERIPH), NULL); + encrypted = ble_conn_state_encrypted(conn_handle1); + mitm_protected = ble_conn_state_mitm_protected(conn_handle1); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT_FALSE(encrypted); + TEST_ASSERT_FALSE(mitm_protected); + TEST_ASSERT_FALSE(lesc); + + /* Testing that a security level of 2 or greater returns encrypted */ + ble_evt_handler(conn_sec_update_evt(conn_handle1, 2), NULL); + encrypted = ble_conn_state_encrypted(conn_handle1); + mitm_protected = ble_conn_state_mitm_protected(conn_handle1); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT(encrypted); + TEST_ASSERT_FALSE(mitm_protected); + TEST_ASSERT_FALSE(lesc); + + /* Testing that a successful auth_status with LESC returns LESC. */ + ble_evt_handler(auth_status_evt(conn_handle1, false, BLE_GAP_SEC_STATUS_SUCCESS), NULL); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT_FALSE(lesc); + ble_evt_handler(auth_status_evt(conn_handle1, true, BLE_GAP_SEC_STATUS_UNSPECIFIED), NULL); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT_FALSE(lesc); + ble_evt_handler(auth_status_evt(conn_handle1, true, BLE_GAP_SEC_STATUS_SUCCESS), NULL); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT(lesc); + + /* level 3 returns MITM protected. */ + ble_evt_handler(conn_sec_update_evt(conn_handle1, 3), NULL); + encrypted = ble_conn_state_encrypted(conn_handle1); + mitm_protected = ble_conn_state_mitm_protected(conn_handle1); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT(encrypted); + TEST_ASSERT(mitm_protected); + TEST_ASSERT_FALSE(lesc); + + /* level 4 returns LESC. */ + ble_evt_handler(conn_sec_update_evt(conn_handle1, 4), NULL); + encrypted = ble_conn_state_encrypted(conn_handle1); + mitm_protected = ble_conn_state_mitm_protected(conn_handle1); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT(encrypted); + TEST_ASSERT(mitm_protected); + TEST_ASSERT(lesc); + + /* Testing that a security level of less than 2 returns unencrypted. */ + ble_evt_handler(conn_sec_update_evt(conn_handle1, 0), NULL); + encrypted = ble_conn_state_encrypted(conn_handle1); + mitm_protected = ble_conn_state_mitm_protected(conn_handle1); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT_FALSE(encrypted); + TEST_ASSERT_FALSE(mitm_protected); + TEST_ASSERT_FALSE(lesc); + + ble_evt_handler(conn_sec_update_evt(conn_handle1, 1), NULL); + encrypted = ble_conn_state_encrypted(conn_handle1); + mitm_protected = ble_conn_state_mitm_protected(conn_handle1); + lesc = ble_conn_state_lesc(conn_handle1); + TEST_ASSERT_FALSE(encrypted); + TEST_ASSERT_FALSE(mitm_protected); + TEST_ASSERT_FALSE(lesc); + + /* Adding a second connection. */ + ble_evt_handler(connected_evt(conn_handle2, BLE_GAP_ROLE_PERIPH), NULL); + ble_evt_handler(conn_sec_update_evt(conn_handle2, 4), NULL); + encrypted = ble_conn_state_encrypted(conn_handle2); + mitm_protected = ble_conn_state_mitm_protected(conn_handle2); + lesc = ble_conn_state_lesc(conn_handle2); + TEST_ASSERT(encrypted); + TEST_ASSERT(mitm_protected); + TEST_ASSERT(lesc); + + /* Testing overflow of conn_handle. */ + ble_conn_state_init(); + + /* Make sure this doesn't read from next flag (mitm_protected) */ + encrypted = ble_conn_state_encrypted(conn_handle_overflow); + /* Make sure this doesn't read from next flag (user flag 0) */ + mitm_protected = ble_conn_state_mitm_protected(conn_handle_overflow); + /* Make sure this doesn't read from next flag (user flag 0) */ + lesc = ble_conn_state_lesc(conn_handle_overflow); + TEST_ASSERT_FALSE(encrypted); + TEST_ASSERT_FALSE(mitm_protected); + TEST_ASSERT_FALSE(lesc); + encrypted = ble_conn_state_encrypted(BLE_CONN_HANDLE_INVALID); + mitm_protected = ble_conn_state_mitm_protected(BLE_CONN_HANDLE_INVALID); + lesc = ble_conn_state_lesc(BLE_CONN_HANDLE_INVALID); + TEST_ASSERT_FALSE(encrypted); + TEST_ASSERT_FALSE(mitm_protected); + TEST_ASSERT_FALSE(lesc); +} + +void test_ble_conn_state_status(void) +{ + bool valid; + + conn_handle1 = 0; + conn_handle_register(conn_handle1); +#if defined(BLE_GAP_ROLE_CENTRAL) + conn_handle2 = 12; + conn_handle_register(conn_handle2); +#endif + conn_handle3 = 19; + conn_handle_register(conn_handle3); + uint8_t dummy_role; + +#if defined(BLE_GAP_ROLE_CENTRAL) + dummy_role = BLE_GAP_ROLE_CENTRAL; + + /* Test that invalid connections have BLE_CONN_STATUS_INVALID as their connection status */ + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(8172), BLE_CONN_STATUS_INVALID); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(BLE_CONN_HANDLE_INVALID), + BLE_CONN_STATUS_INVALID); + + /* Activating some conn handles. */ + ble_evt_handler(connected_evt(conn_handle1, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle2, dummy_role), NULL); + ble_evt_handler(connected_evt(conn_handle3, dummy_role), NULL); + + /* Let's test they are connected */ + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle1), BLE_CONN_STATUS_CONNECTED); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle2), BLE_CONN_STATUS_CONNECTED); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle3), BLE_CONN_STATUS_CONNECTED); + + /* Disconnect one handle */ + ble_evt_handler(disconnected_evt(conn_handle2), NULL); + /* Its status should be DISCONNECTED now */ + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle1), BLE_CONN_STATUS_CONNECTED); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle2), BLE_CONN_STATUS_DISCONNECTED); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle3), BLE_CONN_STATUS_CONNECTED); + + /* Disconnect another handle */ + ble_evt_handler(disconnected_evt(conn_handle3), NULL); + /* There should be two connections whose status is DISCONNECTED */ + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle1), BLE_CONN_STATUS_CONNECTED); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle2), BLE_CONN_STATUS_DISCONNECTED); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle3), BLE_CONN_STATUS_DISCONNECTED); + + /* Handles of connection whose status is DISCONNECTED should still be valid */ + valid = ble_conn_state_valid(conn_handle1); + valid = valid && ble_conn_state_valid(conn_handle2); + valid = valid && ble_conn_state_valid(conn_handle3); + + TEST_ASSERT(valid); + + /* Reactivate a connection handle */ + ble_evt_handler(connected_evt(conn_handle3, dummy_role), NULL); + + /* After a connection event is received, disconnected connections are purged */ + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle1), BLE_CONN_STATUS_CONNECTED); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle2), BLE_CONN_STATUS_INVALID); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle3), BLE_CONN_STATUS_CONNECTED); + + valid = ble_conn_state_status(conn_handle2); + TEST_ASSERT_FALSE(valid); + + /* Let's disconnect another handle */ + ble_evt_handler(disconnected_evt(conn_handle1), NULL); + + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle1), BLE_CONN_STATUS_DISCONNECTED); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle2), BLE_CONN_STATUS_INVALID); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle3), BLE_CONN_STATUS_CONNECTED); + + valid = ble_conn_state_status(conn_handle1); + TEST_ASSERT(valid); + valid = ble_conn_state_status(conn_handle2); + TEST_ASSERT_FALSE(valid); + + ble_evt_handler(disconnected_evt(conn_handle3), NULL); +#endif + + dummy_role = BLE_GAP_ROLE_PERIPH; + + ble_evt_handler(connected_evt(conn_handle1, dummy_role), NULL); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle1), BLE_CONN_STATUS_CONNECTED); + + ble_evt_handler(disconnected_evt(conn_handle1), NULL); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle1), BLE_CONN_STATUS_DISCONNECTED); + + ble_evt_handler(connected_evt(conn_handle3, dummy_role), NULL); + TEST_ASSERT_EQUAL_UINT(ble_conn_state_status(conn_handle1), BLE_CONN_STATUS_INVALID); + + /* Testing overflow of conn_handle. */ + ble_conn_state_init(); + valid = ble_conn_state_status(conn_handle_overflow); + TEST_ASSERT_FALSE(valid); + valid = ble_conn_state_status(BLE_CONN_HANDLE_INVALID); + TEST_ASSERT_FALSE(valid); +} + +void test_ble_conn_state_connections_and_list(void) +{ + uint32_t connections; + struct ble_conn_state_conn_handle_list conn_handle_list; + uint16_t conn_handles[8] = {0, 1, 2, 3, 10, 12, 18, 19}; + + /* Testing that n is initially 0 and list is empty. */ + connections = ble_conn_state_conn_count(); + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(0, connections); + TEST_ASSERT_EQUAL_UINT(0, conn_handle_list.len); + + /* Activating all connections. Testing that n is updated. */ + for (int i = 0; i < 8; i++) { + conn_handle_register(conn_handles[i]); + ble_evt_handler(connected_evt(conn_handles[i], BLE_GAP_ROLE_PERIPH), NULL); + } + + connections = ble_conn_state_conn_count(); + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(8, connections); + TEST_ASSERT_EQUAL_UINT(8, conn_handle_list.len); + TEST_ASSERT_EQUAL_UINT16_ARRAY(conn_handles, conn_handle_list.conn_handles, + conn_handle_list.len); + + /* Deactivating all but one connection. Testing that n is updated */ + for (int i = 1; i < 8; i++) { + ble_evt_handler(disconnected_evt(conn_handles[i]), NULL); + } + + connections = ble_conn_state_conn_count(); + TEST_ASSERT_EQUAL_UINT(1, connections); + + /* The connections should still be valid after being disconnected, + * until a new connection event is received + */ + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(8, conn_handle_list.len); + TEST_ASSERT_EQUAL_UINT16_ARRAY(conn_handles, conn_handle_list.conn_handles, + conn_handle_list.len); + + /* Activate one connection. Testing that n is updated (should now be one). */ + ble_evt_handler(connected_evt(conn_handles[0], BLE_GAP_ROLE_PERIPH), NULL); + + connections = ble_conn_state_conn_count(); + TEST_ASSERT_EQUAL_UINT(1, connections); + + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(1, conn_handle_list.len); + + /* Activating all connections. Testing that n is updated (should now be 8). */ + for (int i = 0; i < 8; i++) { + ble_evt_handler(connected_evt(conn_handles[i], BLE_GAP_ROLE_PERIPH), NULL); + } + + connections = ble_conn_state_conn_count(); + TEST_ASSERT_EQUAL_UINT(8, connections); + + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(8, conn_handle_list.len); + TEST_ASSERT_EQUAL_UINT16_ARRAY(conn_handles, conn_handle_list.conn_handles, + conn_handle_list.len); +} + +void test_ble_conn_state_centrals_and_list(void) +{ +#if defined(BLE_GAP_ROLE_CENTRAL) + bool valid + uint32_t n_centrals; + uint32_t connections; + uint16_t conn_handles[8] = {0, 1, 2, 3, 10, 12, 17, 18}; + struct ble_conn_state_conn_handle_list conn_handle_list; + + /* Testing that n is initially 0 and list is empty. */ + connections = ble_conn_state_conn_count(); + TEST_ASSERT_EQUAL_UINT(0, connections); + + n_centrals = ble_conn_state_central_conn_count(); + TEST_ASSERT_EQUAL_UINT(0, n_centrals); + + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(0, conn_handle_list.len); + + /* Activating all connections. Testing that n is updated. */ + for (int i = 0; i < 8; i++) { + ble_evt_handler(connected_evt(conn_handles[i], BLE_GAP_ROLE_CENTRAL), NULL); + } + + n_centrals = ble_conn_state_central_conn_count(); + TEST_ASSERT_EQUAL_UINT(8, n_centrals); + + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(8, conn_handle_list.len); + TEST_ASSERT_EQUAL_UINT16_ARRAY(conn_handles, conn_handle_list.conn_handles, + conn_handle_list.len); + + /* Deactivating all but one connection. Testing that n is unchanged */ + for (int i = 1; i < 8; i++) { + ble_evt_handler(disconnected_evt(conn_handles[i]), NULL); + /* Should still be valid */ + valid = ble_conn_state_valid(conn_handles[i]); + TEST_ASSERT(valid); + } + + n_centrals = ble_conn_state_central_conn_count(); + TEST_ASSERT_EQUAL_UINT(1, n_centrals); + + /* The connections should still be valid after being disconnected, + * until a new connection event is received + */ + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(8, conn_handle_list.len); + TEST_ASSERT_EQUAL_UINT16_ARRAY(conn_handles, conn_handle_list.conn_handles, + conn_handle_list.len); + + /* Activate one connection. Testing that n is updated. */ + ble_evt_handler(connected_evt(conn_handles[0], BLE_GAP_ROLE_CENTRAL), NULL); + + n_centrals = ble_conn_state_central_conn_count(); + TEST_ASSERT_EQUAL_UINT(1, n_centrals); + + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(1, conn_handle_list.len); + + /* Activating all connections. Testing that n is updated. */ + for (int i = 0; i < 8; i++) { + ble_evt_handler(connected_evt(conn_handles[i], BLE_GAP_ROLE_CENTRAL), NULL); + } + + n_centrals = ble_conn_state_central_conn_count(); + TEST_ASSERT_EQUAL_UINT(8, n_centrals); + + conn_handle_list = ble_conn_state_conn_handles(); + TEST_ASSERT_EQUAL_UINT(8, conn_handle_list.len); + TEST_ASSERT_EQUAL_UINT16_ARRAY(conn_handles, conn_handle_list.conn_handles, + conn_handle_list.len); +#endif +} + +void test_ble_conn_status_n_peripherals_and_handle(void) +{ + bool valid; + + /* No peripherals should be connected */ + TEST_ASSERT_EQUAL(0, ble_conn_state_peripheral_conn_count()); + + /* Connect one device as peripheral, and check that the number of peripherals is + * correctly updated + */ + conn_handle_register(BLE_CONN_STATE_MAX_CONNECTIONS-1); + ble_evt_handler(connected_evt(BLE_CONN_STATE_MAX_CONNECTIONS-1, BLE_GAP_ROLE_PERIPH), NULL); + TEST_ASSERT_EQUAL(1, ble_conn_state_peripheral_conn_count()); + + /* Disconnect the peripheral and check that the number is updated */ + ble_evt_handler(disconnected_evt(BLE_CONN_STATE_MAX_CONNECTIONS-1), NULL); + TEST_ASSERT_EQUAL(0, ble_conn_state_peripheral_conn_count()); + + /* The handle should not be in the list, + * but should be valid until a new connection occurs + */ + TEST_ASSERT_EQUAL(0, ble_conn_state_periph_handles().len); + valid = ble_conn_state_valid(BLE_CONN_STATE_MAX_CONNECTIONS-1); + TEST_ASSERT(valid); + + /* app_error_handler_bare_Expect(NRF_ERROR_NO_MEM); */ + ble_evt_handler(connected_evt(BLE_CONN_STATE_MAX_CONNECTIONS, BLE_GAP_ROLE_PERIPH), NULL); + + /* app_error_handler_bare_Expect(NRF_ERROR_NO_MEM); */ + conn_handle_register(1000); + ble_evt_handler(connected_evt(1000, BLE_GAP_ROLE_PERIPH), NULL); + + /* Connect some handles */ + for (int i = 0; i < BLE_CONN_STATE_MAX_CONNECTIONS-1; i++) { + conn_handle_register(i); + ble_evt_handler(connected_evt(i, BLE_GAP_ROLE_PERIPH), NULL); + } + /* Should report all peripherals */ + TEST_ASSERT_EQUAL(BLE_CONN_STATE_MAX_CONNECTIONS-1, ble_conn_state_peripheral_conn_count()); + + /* This handle should have been invalidated by now */ + valid = ble_conn_state_valid(BLE_CONN_STATE_MAX_CONNECTIONS-1); + TEST_ASSERT_FALSE(valid); +} + +void test_ble_conn_state_user_flag_acquire(void) +{ + uint32_t acquired_ids[CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT]; + + char msg1[] = "i = 00"; + char msg2[] = "i = 00, j = 00"; + + for (int i = 0; i < CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT; i++) { + acquired_ids[i] = ble_conn_state_user_flag_acquire(); + } + + for (uint32_t i = 0; i < CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT; i++) { + sprintf(msg1, "i = %d", i); + + TEST_ASSERT_MESSAGE(acquired_ids[i] != CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT, msg1); + for (uint32_t j = i + 1; j < CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT; j++) { + sprintf(msg2, "i = %d, j = %d", i, j); + + TEST_ASSERT_MESSAGE(acquired_ids[i] != acquired_ids[j], msg2); + } + } + + TEST_ASSERT_EQUAL(BLE_CONN_STATE_USER_FLAG_INVALID, ble_conn_state_user_flag_acquire()); + TEST_ASSERT_EQUAL(BLE_CONN_STATE_USER_FLAG_INVALID, ble_conn_state_user_flag_acquire()); +} + +void test_ble_conn_state_user_flag_set_get(void) +{ + bool flag_state; + uint32_t acquired_ids[CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT] = {}; + + uint16_t conn_handles[BLE_CONN_STATE_MAX_CONNECTIONS]; + uint16_t invalid_conn_handle = 35354; + + for (uint32_t i = 0; i < BLE_CONN_STATE_MAX_CONNECTIONS; i++) { + conn_handles[i] = i; + } + + char out_str[] = "flag_id index: 00, conn_handle index: 00."; + + for (int i = 0; i < BLE_CONN_STATE_MAX_CONNECTIONS; i++) { + conn_handle_register(conn_handles[i]); + ble_evt_handler(connected_evt(conn_handles[i], BLE_GAP_ROLE_PERIPH), NULL); + } + + for (int i = 0; i < CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT; i++) { + for (int j = 0; j < BLE_CONN_STATE_MAX_CONNECTIONS; j++) { + /* Testing that setting and getting has no effect for invalid + * connection handles. + */ + sprintf(out_str, "flag_id index: %d, conn_handle index: %d.", i, j); + + flag_state = ble_conn_state_user_flag_get(conn_handles[j], acquired_ids[i]); + TEST_ASSERT_FALSE(flag_state); + + ble_conn_state_user_flag_set(conn_handles[j], acquired_ids[i], true); + flag_state = ble_conn_state_user_flag_get(conn_handles[j], acquired_ids[i]); + TEST_ASSERT_FALSE(flag_state); + } + } + + for (int i = 0; i < CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT; i++) { + acquired_ids[i] = ble_conn_state_user_flag_acquire(); + } + + for (int i = 0; i < CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT; i++) { + for (int j = 0; j < BLE_CONN_STATE_MAX_CONNECTIONS; j++) { + /* Testing that setting and getting works. */ + sprintf(out_str, "flag_id index: %d, conn_handle index: %d.", i, j); + flag_state = ble_conn_state_user_flag_get(conn_handles[j], acquired_ids[i]); + TEST_ASSERT_FALSE_MESSAGE(flag_state, out_str); + + ble_conn_state_user_flag_set(conn_handles[j], acquired_ids[i], true); + flag_state = ble_conn_state_user_flag_get(conn_handles[j], acquired_ids[i]); + TEST_ASSERT_MESSAGE(flag_state, out_str); + + ble_conn_state_user_flag_set(conn_handles[j], acquired_ids[i], false); + flag_state = ble_conn_state_user_flag_get(conn_handles[j], acquired_ids[i]); + TEST_ASSERT_FALSE_MESSAGE(flag_state, out_str); + + ble_conn_state_user_flag_set(conn_handles[j], acquired_ids[i], true); + flag_state = ble_conn_state_user_flag_get(conn_handles[j], acquired_ids[i]); + TEST_ASSERT_MESSAGE(flag_state, out_str); + } + + /* Testing that setting and getting has no effect for invalid connection handles, + * even after others are connected. + */ + sprintf(out_str, "flag_id index: %d.", i); + + ble_conn_state_user_flag_set(BLE_CONN_STATE_MAX_CONNECTIONS, acquired_ids[i], true); + flag_state = ble_conn_state_user_flag_get(BLE_CONN_STATE_MAX_CONNECTIONS, + acquired_ids[i]); + TEST_ASSERT_FALSE_MESSAGE(flag_state, out_str); + + ble_conn_state_user_flag_set(BLE_CONN_HANDLE_INVALID, acquired_ids[i], true); + flag_state = ble_conn_state_user_flag_get(BLE_CONN_HANDLE_INVALID, acquired_ids[i]); + TEST_ASSERT_FALSE_MESSAGE(flag_state, out_str); + + ble_conn_state_user_flag_set(invalid_conn_handle, acquired_ids[i], true); + flag_state = ble_conn_state_user_flag_get(invalid_conn_handle, acquired_ids[i]); + TEST_ASSERT_FALSE_MESSAGE(flag_state, out_str); + + ble_conn_state_user_flag_set(invalid_conn_handle, acquired_ids[i], false); + flag_state = ble_conn_state_user_flag_get(invalid_conn_handle, acquired_ids[i]); + TEST_ASSERT_FALSE_MESSAGE(flag_state, out_str); + } + + /* The rest of the test will be on two arbitrary indices. */ + int arbitrary_index1 = 3; + int arbitrary_index2 = 5; + + for (int i = 0; i < CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT; i++) { + /* The flags should still be 1. */ + sprintf(out_str, "flag_id index: %d.", i); + flag_state = ble_conn_state_user_flag_get(conn_handles[0], acquired_ids[i]); + TEST_ASSERT_MESSAGE(flag_state, out_str); + flag_state = ble_conn_state_user_flag_get(conn_handles[arbitrary_index1], + acquired_ids[i]); + TEST_ASSERT_MESSAGE(flag_state, out_str); + flag_state = ble_conn_state_user_flag_get(conn_handles[arbitrary_index2], + acquired_ids[i]); + TEST_ASSERT_MESSAGE(flag_state, out_str); + /* Test that it doesn't overflow into next flags. */ + ble_conn_state_user_flag_set(conn_handle_overflow, acquired_ids[i], false); + } + + /* Invalidate the connection handles by disconnecting them and reconnecting. */ + ble_evt_handler(disconnected_evt(conn_handles[arbitrary_index1]), NULL); + ble_evt_handler(disconnected_evt(conn_handles[arbitrary_index2]), NULL); + ble_evt_handler(connected_evt(conn_handles[arbitrary_index1], BLE_GAP_ROLE_PERIPH), NULL); + ble_evt_handler(connected_evt(conn_handles[arbitrary_index2], BLE_GAP_ROLE_PERIPH), NULL); + + for (int i = 0; i < CONFIG_BLE_CONN_STATE_USER_FLAG_COUNT; i++) { + /* Test that the flags are now 0 because of being invalidated. */ + sprintf(out_str, "flag_id index: %d.", i); + flag_state = ble_conn_state_user_flag_get(conn_handles[arbitrary_index1], + acquired_ids[i]); + TEST_ASSERT_FALSE_MESSAGE(flag_state, out_str); + flag_state = ble_conn_state_user_flag_get(conn_handles[arbitrary_index2], + acquired_ids[i]); + TEST_ASSERT_FALSE_MESSAGE(flag_state, out_str); + } +} + +void test_ble_conn_state_conn_idx(void) +{ +#if defined(BLE_GAP_ROLE_CENTRAL) + uint16_t conn_handle_err = BLE_CONN_STATE_MAX_CONNECTIONS+1; + uint16_t conn_handle_last = BLE_CONN_STATE_MAX_CONNECTIONS-1; + + ble_evt_handler(connected_evt(0, BLE_GAP_ROLE_CENTRAL), NULL); + ble_evt_handler(connected_evt(1, BLE_GAP_ROLE_PERIPH), NULL); + ble_evt_handler(connected_evt(5, BLE_GAP_ROLE_CENTRAL), NULL); + ble_evt_handler(connected_evt(conn_handle_last, BLE_GAP_ROLE_CENTRAL), NULL); + + TEST_ASSERT_EQUAL(0, ble_conn_state_conn_idx(0)); + TEST_ASSERT_EQUAL(1, ble_conn_state_conn_idx(1)); + TEST_ASSERT_EQUAL(5, ble_conn_state_conn_idx(5)); + TEST_ASSERT_EQUAL(conn_handle_last, ble_conn_state_conn_idx(conn_handle_last)); + TEST_ASSERT_EQUAL(BLE_CONN_STATE_MAX_CONNECTIONS, ble_conn_state_conn_idx(conn_handle_err)); + TEST_ASSERT_EQUAL(BLE_CONN_STATE_MAX_CONNECTIONS, ble_conn_state_conn_idx(2)); + TEST_ASSERT_EQUAL(BLE_CONN_STATE_MAX_CONNECTIONS, ble_conn_state_conn_idx(3)); + TEST_ASSERT_EQUAL(BLE_CONN_STATE_MAX_CONNECTIONS, ble_conn_state_conn_idx(4)); + for (uint16_t i = 6; i < conn_handle_last; i++) { + TEST_ASSERT_EQUAL(BLE_CONN_STATE_MAX_CONNECTIONS, ble_conn_state_conn_idx(i)); + } +#endif +} + +void user_flag_function(uint16_t conn_handle, void *context) +{ + TEST_ASSERT(calls < 10); + TEST_ASSERT_EQUAL_UINT(conn_handles[calls], conn_handle); + TEST_ASSERT_EQUAL_PTR(contexts[calls], context); + calls++; +} + +void expect_user_function_user_flag(uint16_t conn_handle, + uint32_t flag_id, + void *context, + uint32_t call) +{ + conn_handles[call] = conn_handle; + contexts[call] = context; +} + +void expect_user_function_connected(uint16_t conn_handle, void *context, uint32_t call) +{ + conn_handles[call] = conn_handle; + contexts[call] = context; +} + +void test_ble_conn_state_for_each_set_user_flag(void) +{ + uint32_t flag_id1 = ble_conn_state_user_flag_acquire(); + uint32_t flag_id2 = ble_conn_state_user_flag_acquire(); + uint32_t calls_ret; + uint16_t conn_handles[10]; + + for (uint32_t i = 0; i < 10; i++) { + conn_handles[i] = i; + } + + for (int i = 0; i < 10; i++) { + conn_handle_register(conn_handles[i]); + ble_evt_handler(connected_evt(conn_handles[i], BLE_GAP_ROLE_PERIPH), NULL); + } + + /* No set flags. */ + calls_ret = ble_conn_state_for_each_set_user_flag(flag_id1, user_flag_function, NULL); + TEST_ASSERT_EQUAL(0, calls_ret); + TEST_ASSERT_EQUAL(0, calls); + + /* One set flag */ + ble_conn_state_user_flag_set(conn_handles[0], flag_id1, 1); + expect_user_function_user_flag(conn_handles[0], flag_id1, NULL, 0); + calls_ret = ble_conn_state_for_each_set_user_flag(flag_id1, user_flag_function, NULL); + TEST_ASSERT_EQUAL(1, calls_ret); + TEST_ASSERT_EQUAL(1, calls); + calls = 0; + + /* One set flag, other flag id. */ + ble_conn_state_user_flag_set(conn_handles[1], flag_id2, 1); + expect_user_function_user_flag(conn_handles[1], flag_id2, NULL, 0); + calls_ret = ble_conn_state_for_each_set_user_flag(flag_id2, user_flag_function, NULL); + TEST_ASSERT_EQUAL(1, calls_ret); + TEST_ASSERT_EQUAL(1, calls); + calls = 0; + + /* Two set flags, context */ + ble_conn_state_user_flag_set(conn_handles[3], flag_id1, 1); + expect_user_function_user_flag(conn_handles[0], flag_id1, &arbitrary_context, 0); + expect_user_function_user_flag(conn_handles[3], flag_id1, &arbitrary_context, 1); + calls_ret = ble_conn_state_for_each_set_user_flag(flag_id1, user_flag_function, + &arbitrary_context); + TEST_ASSERT_EQUAL(2, calls_ret); + TEST_ASSERT_EQUAL(2, calls); + calls = 0; + + /* One set flag */ + ble_conn_state_user_flag_set(conn_handles[0], flag_id1, 0); + expect_user_function_user_flag(conn_handles[3], flag_id1, NULL, 0); + calls_ret = ble_conn_state_for_each_set_user_flag(flag_id1, user_flag_function, NULL); + TEST_ASSERT_EQUAL(1, calls_ret); + TEST_ASSERT_EQUAL(1, calls); + calls = 0; + + /* All set flags */ + for (int i = 0; i < 10; i++) { + ble_conn_state_user_flag_set(conn_handles[i], flag_id2, 1); + expect_user_function_user_flag(conn_handles[i], flag_id2, NULL, i); + } + calls_ret = ble_conn_state_for_each_set_user_flag(flag_id2, user_flag_function, NULL); + TEST_ASSERT_EQUAL(10, calls_ret); + TEST_ASSERT_EQUAL(10, calls); + calls = 0; + + /* No set flags. */ + ble_conn_state_user_flag_set(conn_handles[3], flag_id1, 0); + calls_ret = ble_conn_state_for_each_set_user_flag(flag_id1, user_flag_function, NULL); + TEST_ASSERT_EQUAL(0, calls_ret); + TEST_ASSERT_EQUAL(0, calls); + + /* No set flags. */ + for (int i = 0; i < 10; i++) { + ble_evt_handler(disconnected_evt(conn_handles[i]), NULL); + ble_evt_handler(connected_evt(conn_handles[i], BLE_GAP_ROLE_PERIPH), NULL); + } + ble_conn_state_user_flag_set(conn_handles[3], flag_id2, 0); + calls_ret = ble_conn_state_for_each_set_user_flag(flag_id1, user_flag_function, NULL); + TEST_ASSERT_EQUAL(0, calls_ret); + TEST_ASSERT_EQUAL(0, calls); +} + +void test_ble_conn_state_for_each_connected(void) +{ +#if defined(BLE_GAP_ROLE_CENTRAL) + uint32_t calls_ret; + + calls_ret = ble_conn_state_for_each_connected(user_flag_function, NULL); + TEST_ASSERT_EQUAL(0, calls_ret); + + ble_evt_handler(connected_evt(1, BLE_GAP_ROLE_CENTRAL), NULL); + ble_evt_handler(connected_evt(2, BLE_GAP_ROLE_CENTRAL), NULL); + ble_evt_handler(connected_evt(8, BLE_GAP_ROLE_PERIPH), NULL); + + expect_user_function_connected(1, NULL, 0); + expect_user_function_connected(2, NULL, 1); + expect_user_function_connected(8, NULL, 2); + + calls_ret = ble_conn_state_for_each_connected(user_flag_function, NULL); + TEST_ASSERT_EQUAL(3, calls_ret); + TEST_ASSERT_EQUAL(3, calls); + calls = 0; + + ble_evt_handler(disconnected_evt(1), NULL); + ble_evt_handler(connected_evt(5, BLE_GAP_ROLE_PERIPH), NULL); + ble_evt_handler(disconnected_evt(8), NULL); + + expect_user_function_connected(2, NULL, 0); + expect_user_function_connected(5, NULL, 1); + + calls_ret = ble_conn_state_for_each_connected(user_flag_function, NULL); + TEST_ASSERT_EQUAL(2, calls_ret); + TEST_ASSERT_EQUAL(2, calls); + calls = 0; +#endif +} + +void setUp(void) +{ + ble_conn_state_init(); +} +void tearDown(void) +{ + /* Maximum connection handle allowed is 100*/ + for (int i = 0; i < 100; i++) { + conn_handle_deregister(i); + } +} + + +extern int unity_main(void); + +int main(void) +{ + return unity_main(); +} diff --git a/tests/lib/ble_conn_state/testcase.yaml b/tests/lib/ble_conn_state/testcase.yaml new file mode 100644 index 0000000000..121d1421dd --- /dev/null +++ b/tests/lib/ble_conn_state/testcase.yaml @@ -0,0 +1,4 @@ +tests: + lib.ble_conn_state: + platform_allow: native_sim + tags: unittest