This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

debugging sd_ble_gatts_hvx

Hi

I am trying to get to the bottom of a problem I am having with sending a buffer of data on connection and indication enable. Here's what I do: I have a process running, which is capturing sensor data every 10 seconds, and keeping it in a circular buffer. When a master connects, I want to send the circular buffer to the master app. I am doing that by triggering on the event that the CCCD is written to enable indications (I am using indication because I want to make sure that the values end up in the app). When this CCCD write is happening, I send the first item in my circular buffer, and then use the BLE_GATTS_EVT_HVC event to see if I have gotten a confirmation, just as in the health thermometer example.

I am running into a number of problems

  • when I trigger on the CCCD write and immediately do a sd_ble_gatts_hvx, I get an error 8 (illegal state); if I start a timer and do the same after 10 ms, then it is ok
  • I sometimes also have a BLE_ERROR_GATTS_SYS_ATTR_MISSING error, how can I avoid that? when should I normally do a sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0) (i do not use bonding)

I have the impression that timing issues or race conditions are causing my problems, can anyone give me hint on how to solve this?

  • Reading your last comment, and as Ulrich says, here's a list of possible causes to your issue:

    1. You may be waiting for the wrong CCCD write. Double-check that your CCCD handle matches the one you got when you added the characteristic
    2. You may be calling hvx() with the wrong characteristic value handle. Remember that this has to be the characteristic value handle and not a CCCD or any other handle.
    3. You may be calling hvx() twice with the right handle. The HVC event will take a while to come back and so you need to exit your event processing loop while waiting for it.

    If none of this helps you could always send us a PM with your code and we can take a look at it.

  • Maybe there is some light at the end of the tunnel. Currently I was trying to enable an indication by writing 0x02 0x00 (little endian) to the CCCD attribute value of my characteristic that I want to send indications from. And it used to trigger something in the past. But now I clicked the 'enable services' button in the MCP. And then it does seem to work. What is the difference between the 2 actions? The downside of using the enable services button seems to be that all characteristics are enabled, and hence starting to send data.

    And then, I was wondering: when clicking on an attribute of a characteristic in MCP, one can see the UUID and the handle of each of them. How are the UUID and the handle of e.g. the CCCD defined? I do not have to configure it myself I think, it just happens - I only have defined the UUID of each characterstic, and that seems to end up in the UUID attribute. Am I correct in this?

  • maybe to demonstrate what I mean, a piece of log in MCP: [14:02:29.6] UpdateAttributeValue(mode=0) [14:02:29.6] WriteRequest sent to handle 0x0015 with value [2, 0] [14:02:29.8] Updated handle 0015 with value [2, 0] [14:02:41.5] EnableServices({0x000D:1,0x0011:1,0x0015:2,0x0019:2,}) [14:02:41.7] Updated handle 0015 with value [2, 0] [14:02:41.7] Successfully updated the store value of CCCD [14:02:41.9] Updated handle 0011 with value [1, 0] [14:02:41.9] Successfully updated the store value of CCCD [14:02:42.1] Updated handle 0019 with value [2, 0] [14:02:42.1] Successfully updated the store value of CCCD [14:02:42.2] Received a HandleValueIndication on handle 0017 with value 050000003200000000000000 [14:02:42.3] Updated handle 000D with value [1, 0] [14:02:42.3] Successfully updated the store value of CCCD [14:02:42.4] Received a HandleValueIndication on handle 0013 with value 050000003200000000000000 [14:02:42.6] Received a HandleValueIndication on handle 0017 with value 050000003200000000000000 [14:02:42.8] Received a HandleValueIndication on handle 0013 with value 050000003200000000000000 [14:02:43.0] Received a HandleValueIndication on handle 0017 with value 050000003200000000000000 [14:02:43.2] Received a HandleValueIndication on handle 0013 with value 050000003200000000000000 [14:02:43.4] Received a HandleValueIndication on handle 0017 with value 050000003200000000000000 [14:02:43.6] Received a HandleValueIndication on handle 0013 with value 050000003200000000000000 [14:02:43.8] Received a HandleValueIndication on handle 0017 with value 050000003200000000000000 [14:02:44.0] Received a HandleValueIndication on handle 0013 with value 050000003200000000000000 [14:02:44.0] DisableServices({0x000D:1,0x0011:1,0x0015:2,0x0019:2,}) [14:02:44.2] Received a HandleValueIndication on handle 0017 with value 050000003200000000000000 [14:02:44.2] Updated handle 0015 with value [0, 0] [14:02:44.2] Successfully updated the store value of CCCD [14:02:44.4] Updated handle 0011 with value [0, 0] [14:02:44.4] Successfully updated the store value of CCCD [14:02:44.6] Updated handle 0019 with value [0, 0] [14:02:44.6] Successfully updated the store value of CCCD [14:02:44.8] Updated handle 000D with value [0, 0] [14:02:44.8] Successfully updated the store value of CCCD

    On the first write of the CCCD (handle 15) at [14:02:29.6], nothing seems to happen. However, when I do the 'enable services' (at[14:02:41.5]) I start getting values on 2 of the 4 indicate/notify characteristics.

    So what is the difference between writing directly to the CCCD with the right value, and doing a 'start services'?

  • All attributes have a handle and an UUID. CCCDs in particular use the 0x2901 (BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) UUID. You don't need to configure it yourself, it's added automatically by the SoftDevice when you add a notifiable or indicatable characteristic.

    The log you sent from MCP is enabling the following CCCDs for the corresponding char values:

    • CCCD at handle 0x0015 for char value at handle 0x0013
    • CCCD at handle 0x0019 for char value at handle 0x0017
Related