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

How do I use "sd_ble_gatts_value_get" to read the value of CCCD handle?

Hello, first, a little background info:

devzone.nordicsemi.com/.../

I want to know if I have successfully set the CCCD handle or not with my Android program.

And endnode recommended that I use this function "sd_ble_gatts_value_get".

I tried with the following code:

#ifdef DEBUG
	
	ble_gatts_value_t ble_gatts_value_cccd_acquire;  
	result = sd_ble_gatts_value_get (m_active_conn_handle,m_mesh_service.service_handle, &ble_gatts_value_cccd_acquire);
#endif

where: m_active_conn_handle is the current active gatt connection handle;

m_mesh_service.service_handle is the target gatt service I want to read one of its characteristic (and things get fuzzy from here), and its documentation is here:

ble_gatts_value_cccd_acquire is an empty struct that I plan to store my result in.

The final product: image description

isn't something very helpful...the length is 2, the offset is zero, and the p_value points to a part of the memory which aren't 0x0000 (notification disabled) or 0x0001 (notification enabled):

image description

So... how do I use this function? It has only 3 parameters, and 2 of which are used to id the target value...shouldn't I use 3, one for id-ing the gatt connection, one for id-ing the service, one for id-ing the characteristic? Golly why must everything be so confusing!

So... how?

Parents
  • Hi Mitch,

    Are you sure that handle you supply to sd_ble_gatts_value_get is CCCD handle? What type is m_mesh_service handle and how you set the value? Note that Service itself doesn't provide any Value handle (indeed!) but only Characteristic has one and Descriptors one... Note that Service has handle which holds UUID and similar for Characteristic, but I'm not sure you can read them by this SD function (you can definitely read them over BLE GATT methods).

    Also the structure ble_gatts_value_t ble_gatts_value_cccd_acquire should be allocated by you, don't expect to get pointers to SD memory area! See example from Nordic SDK (full text search for function which you want to use is great help, try it;)

    ret_code_t err_code;
    
    // Go straight to the characteristic
    uint8_t           value_buffer[ESCS_ADV_SLOT_CHAR_LENGTH_MAX] = {0};
    ble_gatts_value_t value = {.len = sizeof(value_buffer),
                               .offset = 0,
                               .p_value = &(value_buffer[0])};
    
    err_code = sd_ble_gatts_value_get(p_escs->conn_handle, val_handle, &value);
    RETURN_IF_ERROR(err_code);
    

    And last but not least I haven't tried sd_ble_gatts_value_get function on handle which isn't GATT Characteristic Value but something else (e.g. UUID or Descriptor). It's possible that it won't work and you will need to use something else (e.g. sd_ble_gatts_sys_attr_get).

    Cheers Jan

Reply
  • Hi Mitch,

    Are you sure that handle you supply to sd_ble_gatts_value_get is CCCD handle? What type is m_mesh_service handle and how you set the value? Note that Service itself doesn't provide any Value handle (indeed!) but only Characteristic has one and Descriptors one... Note that Service has handle which holds UUID and similar for Characteristic, but I'm not sure you can read them by this SD function (you can definitely read them over BLE GATT methods).

    Also the structure ble_gatts_value_t ble_gatts_value_cccd_acquire should be allocated by you, don't expect to get pointers to SD memory area! See example from Nordic SDK (full text search for function which you want to use is great help, try it;)

    ret_code_t err_code;
    
    // Go straight to the characteristic
    uint8_t           value_buffer[ESCS_ADV_SLOT_CHAR_LENGTH_MAX] = {0};
    ble_gatts_value_t value = {.len = sizeof(value_buffer),
                               .offset = 0,
                               .p_value = &(value_buffer[0])};
    
    err_code = sd_ble_gatts_value_get(p_escs->conn_handle, val_handle, &value);
    RETURN_IF_ERROR(err_code);
    

    And last but not least I haven't tried sd_ble_gatts_value_get function on handle which isn't GATT Characteristic Value but something else (e.g. UUID or Descriptor). It's possible that it won't work and you will need to use something else (e.g. sd_ble_gatts_sys_attr_get).

    Cheers Jan

Children
  • Hello Jan:

    Thank you yet again for your patient reply :)

    Are you sure that handle you supply to sd_ble_gatts_value_get is CCCD handle?

    No, as a matter of fact, after some study, I am now sure it is not.

    What type is m_mesh_service handle and how you set the value?

    I... I don't really know, I got the value from the same function that added the metadata characteristic function (which is errorneous, I won't be able to get it anyway, it's a purely SD thing now it seems, I should have gone for value characteristic.)

    Now this is what I beleive, after some googling:

    What I want to read is the CCCD descriptor, it can be accessed using a handle. If the result I read is 0x0001, then it means the notification is enabled. of it's 0x0002, meaning indication is enabled. if it's 0x0000, it means nothing was enabled. (Continued below)

  • Now here is a huge problem, according to this:

    devzone.nordicsemi.com/.../

    This descriptor will be added automatically by the S110 softdevice for any characteristic that has either the Notify or the Indicate properties.

    This means that the handle which I can use to access my desired CCCD is either very, very difficult to get, or outright impossible. (because it was produced by SD. And unless SD has an API which gives away this handle, I won't be able to get it)

    Again, I have zero clue what I should do at this point...

  • Hi Mitch, seems still crawling through the BLE architecture:) Summary:

    • Yes, CCCD is created automatically by SD for certain GATT Characteristic (remember it's SD which builds actual GATT table to be available as Server to connected Clients) BUT that depends on flags you set for that Characteristic! If you say during the creation that it supports Indication or Notify methods then SD will create CCCD.
    • All this Characteristic "creation" (or "provisioning" if you want) is done by calling sd_ble_gatts_characteristic_add function (I'm sure you are calling it in your code as well!). This function fills p_handles (pointer to structure supplied by your app) all created handles including CCCD. (1/x)
  • ...(2/2)

    • So this gives you things like value handle and CCCD handle. Can you use code snippet above and supply one of these handles to sd_ble_gatts_value_get function call? Is at least reading value handle working? What about CCCD?

    I haven't touched nRF52 code for couple of months now and it looks like you are forcing me to do it. It's kind of battle of laziness, mine vs. yours;)

  • Both Jan and I have pointed out that the CCCD characteristic handle (as well as the value and other handles) are returned by the sd_ble_gatts_characteristic_add() call. That's in my comment of 3 days ago. There is no 'huge problem', the handle is not added by the softdevice in secret, it's not hard or impossible to get. Somewhere in the code you have that call is being executed and you need to go and find it and find where it puts the returned structure with all the handles on it and then go get the CCCD handle out of it.

Related