From 0bd7a7e2328b20aa18a6cb46d1808f3acbe8872a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eivind=20J=C3=B8lsgard?= Date: Fri, 7 Nov 2025 10:06:13 +0100 Subject: [PATCH] bluetooth: services: nus: Move context to nus structure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move context to NUS structure. Signed-off-by: Eivind Jølsgard --- .../release_notes/release_notes_changelog.rst | 4 ++++ include/bm/bluetooth/services/ble_nus.h | 2 +- subsys/bluetooth/services/ble_nus/nus.c | 15 +++++++-------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/doc/nrf-bm/release_notes/release_notes_changelog.rst b/doc/nrf-bm/release_notes/release_notes_changelog.rst index 921b8eb7df..fc7d16a498 100644 --- a/doc/nrf-bm/release_notes/release_notes_changelog.rst +++ b/doc/nrf-bm/release_notes/release_notes_changelog.rst @@ -122,6 +122,10 @@ Libraries * Updated to use errno instead of nrf_errors. +* :ref:`lib_ble_service_nus` service: + + * Fixed an issue where the client context was shared between all instances. + Samples ======= diff --git a/include/bm/bluetooth/services/ble_nus.h b/include/bm/bluetooth/services/ble_nus.h index 663c2435bf..5d58a6bf7e 100644 --- a/include/bm/bluetooth/services/ble_nus.h +++ b/include/bm/bluetooth/services/ble_nus.h @@ -149,7 +149,7 @@ struct ble_nus { /** Handles related to the RX characteristic (as provided by the SoftDevice). */ ble_gatts_char_handles_t rx_handles; /** Link context with handles of all current connections and its context. */ - struct ble_nus_ctx *const ctx; + struct ble_nus_client_context contexts[CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT]; /** Event handler to be called for handling received data. */ ble_nus_evt_handler_t evt_handler; }; diff --git a/subsys/bluetooth/services/ble_nus/nus.c b/subsys/bluetooth/services/ble_nus/nus.c index 64a55c56a5..9fd502ca44 100644 --- a/subsys/bluetooth/services/ble_nus/nus.c +++ b/subsys/bluetooth/services/ble_nus/nus.c @@ -14,13 +14,12 @@ LOG_MODULE_REGISTER(ble_nus, CONFIG_BLE_NUS_LOG_LEVEL); -static struct ble_nus_client_context contexts[CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT]; - -static struct ble_nus_client_context *ble_nus_client_context_get(uint16_t conn_handle) +static struct ble_nus_client_context *ble_nus_client_context_get(struct ble_nus *nus, + uint16_t conn_handle) { const int idx = nrf_sdh_ble_idx_get(conn_handle); - return ((idx >= 0) ? &contexts[idx] : NULL); + return ((idx >= 0) ? &nus->contexts[idx] : NULL); } static uint32_t nus_rx_char_add(struct ble_nus *nus, struct ble_nus_config const *cfg) @@ -116,7 +115,7 @@ static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt) }; struct ble_nus_client_context *ctx; - ctx = ble_nus_client_context_get(conn_handle); + ctx = ble_nus_client_context_get(nus, conn_handle); if (ctx == NULL) { LOG_ERR("Could not fetch nus context for connection handle %#x", conn_handle); } @@ -152,7 +151,7 @@ static void on_write(struct ble_nus *nus, ble_evt_t const *ble_evt) }; struct ble_nus_client_context *ctx; - ctx = ble_nus_client_context_get(conn_handle); + ctx = ble_nus_client_context_get(nus, conn_handle); if (ctx == NULL) { LOG_ERR("Could not fetch nus context for connection handle %#x", conn_handle); } @@ -202,7 +201,7 @@ static void on_hvx_tx_complete(struct ble_nus *nus, ble_evt_t const *ble_evt) }; struct ble_nus_client_context *ctx; - ctx = ble_nus_client_context_get(conn_handle); + ctx = ble_nus_client_context_get(nus, conn_handle); if (ctx == NULL) { LOG_ERR("Could not fetch nus context for connection handle %#x", conn_handle); return; @@ -317,7 +316,7 @@ int ble_nus_data_send(struct ble_nus *nus, uint8_t *data, return -ENOENT; } - ctx = ble_nus_client_context_get(conn_handle); + ctx = ble_nus_client_context_get(nus, conn_handle); if (ctx == NULL) { return -ENOENT; }