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

BLE_GATTS_VLOC_USER and volatile variable

When using BLE_GATTS_VLOC_USER I would tend to make the variable pointed to by ble_gatts_attr_t.p_value volatile to reflect the situation that the value can be changed by softdevice interrupt handlers at any time, but this entails all kinds of additional casts, e.g. to memset the value. Also, ble_gatts_attr_t.p_value itself isn't volatile.

Is my thinking process wrong?

  • Why would you use VLOC_USER? Would it not be better to increase the size of the attribute table to meet your requirements?

    You should not need to use volatile for this. And I think you should probably use the SD apis, sd_ble_gatts_value_set and get when interfacing it.

  • I can't use VLOC_STACK because there are too many characteristics (currently 18) and the call to add them returns NRF_ERROR_NO_MEM at some point

    But I'd like to generalize this question some more.

    Do sd_ble_gatts_value_set and get always work, even when not currently connected?

    When the characteristic values represent internal state of the device I see three options when using VLOC_USER:

    - Read and write directly to the variables allocated for VLOC_USER
      - This is very fast and seemed to work for my project.
    - Use sd_ble_gatts_value_set and get for all interactions with the characteristic values.
      - This is safer, but might add considerable overhead when the characteristic value is updated many times.
    - Use local copies of all variables to keep track of the state and set them dynamically when reading via sd_ble_gatts_rw_authorize_reply
      - This should be safe and fast, but requires twice the amount of RAM.

    What's the official stance on these approaches?

  • To use vloc_stack you need to increase the size of the attribute table, NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE.

    Set and get should work as long as the softdevice is enabled. However you need a conn handle for system attributes.

    Not sure we have an official recommendation. I would usually recommend to increase the attribute table size to keep the values in the SD region and use set and get to manage them.

Related