Error When Reading Characteristic

Hi,

I am using nRF5 SDK v17.2.0 on a legacy product. I have code that looks like this:

    ret_code_t rc = sd_ble_gatts_rw_authorize_reply(event->evt.common_evt.conn_handle, &authParams);

    if (rc == NRF_SUCCESS)
    {
        NRF_LOG_INFO("Event history - read %d events starting at %d", eventListContent.events().get_length(), originalOffset);
    }
    else
    {
        NRF_LOG_ERROR("Failed to reply to event history read request. RC: %d", rc);
    }

When reading the characteristic repeatedly, I sometimes see something like this printed out:

00> <info> app: Event history - read 8 events starting at 0
00> <info> app: Event history - read 8 events starting at 8
00> <info> app: Event history - read 8 events starting at 16
00> <info> app: Event history - read 8 events starting at 24
00> <info> app: Event history - read 8 events starting at 32
00> <error> app: Failed to reply to event history read request. RC: 8
00> <info> app: Event history - read 8 events starting at 48
00> <info> app: Event history - read 8 events starting at 56
00> <info> app: Event history - read 0 events starting at 64

Return code of 8 corresponds to NRF_ERROR_INVALID_STATE. Why would I get that?

  • I did a Wireshark capture while the mobile app was reading the characteristic in a loop. Everything matched up and I did not see anything that would stand out. At the same time, I did see "NRF_ERROR_INVALID_STATE" on the firmware side. I think the idea that "sd_ble_gatts_rw_authorize_reply()" is somehow called twice (or more times) makes sense.

    I searched through the codebase for calls to "OnAuthorizeHandleReadEventHistory" function which is calling the "sd_ble_gatts_rw_authorize_reply()". I only found one result, which is the switch statement I showed earlier.

    Note that sd_ble_gatts_rw_authorize_reply() can be called from SDK code as well (and if it is, you cannot call it again from the same request, as the second call will then fail with NRF_ERROR_INVALID_STATE, as i should as the request is then already handled).

    Could you explain? I have a custom service / characteristic. How does SDK come into play here?

  • Hi,

    sd_ble_gatts_rw_authorize_reply() can be called from quite a few places, depening on which modules you use (it is part of the implementation for servics in the SDK code, and also in the queued write module). I do not have the knowledge about your code to suggest where it could be called in your applicaton, but one way is to simply log at every possible call to sd_ble_gatts_rw_authorize_reply() (search the codebase and add logging), to see where it happens in your application.

Related