Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Adding NUS to existing profile - how to get a 'link context' ?

Hi, 

I'm trying to add the NUS service to an existing application (freertos based, SDK15.2, already use a custom service)

  • I copied the relevant code snippets from the example
  • Modified the project setup - Add ble_nus.c & ble_link_ctx_manager.c, increased start address + 0x10 bytes (for the additional service uuid)
  • Updated sdk_config.h - increased NRF_SDH_BLE_VS_UUID_COUNT to 2 (was 1 for the prev proprietary service), BLE_NUS_ENABLED = 1

The app builds and runs to the point where I do BLE connect.

The program crash when ble_nus.c on_connect() function calls blcm_link_ctx_get(), as the 1st parameters p_nus->p_link_ctx_storage == NULL.

I had to add ble_link_ctx_manager.c to the project following the example project. However, I couldn't find what it does and how it is used/initialized

Any advice will be appreciated.

Thanks

Parents Reply Children
  • Hi, 

    BLE Link Ctx storage instance is created together with the NUS service instance. p_link_ctx_storage->p_ctx_data_pool can never be null because its populated during Ctx storage variable declaration in the macro. p_ctx_data_pool gets the value on the connected callback in the ble_nus.c file: on_connect().

    p_link_ctx_storage->p_ctx_data_pool is not changed anywhere in the code and it's a constant pointer so we would get a warning if somebody tried to overwrite it.

    It's hard to say what causes it. It would be useful to track if some other part of the software accidentally resets this part of memory to NULL. You can check if p_ctx_data_pool is non-null after NUS initialization.

    -Amanda H.

  • Hi Amanda, 

    I've followed your advice.

    The structure is indeed initialized upon program startup and its been overwritten in the call to ble_nus_init(), without being detected because it returns NRF_SUCCESS.

    I've digged deeper and found that the problem occurs after the service is added when the first characteristic (BLE_UUID_NUS_RX_CHARACTERISTIC) is added (on the call to sd_ble_gatts_characteristic_add() in characteristic_add())

    I've increased RAM_ADDRESS_START (by more than 1000 bytes) just in case its a memory problem but the problem remains.

    Please note that, as I've mentioned above, there is no error code returned (NRF_SUCCESS).

  • I may have found the root cause (still not a workaround).

    The problem lies in the call to ble_nus_init() from services_init().

    It seems that m_nus is handled as PACKED structure on services_init() but as UNPACKED structure when its pointer is passed to ble_nus_init().

    Is it an IDE issue ???

    Note the members addresses in the attached screen cap

Related