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

Constant descriptor and BLE_GATTS_VLOC_USER

Hi.

I want to add constant descriptor. Can I save the memory using const variable and BLE_GATTS_VLOC_USER. Will it occur some faults in future?

static const uint8_t value[] = {0, 1, 2, 3};
ble_gatts_attr_t attr;
ble_gatts_attr_md_t desc_md;
ble_uuid_t ble_uuid;

memset(&desc_md, 0x00, sizeof(desc_md));
memset(&attr, 0x00, sizeof(attr));

BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&desc_md.write_perm);

desc_md.vloc = BLE_GATTS_VLOC_USER;
desc_md.vlen = 0;

ble_uuid.type = p_service->uuid_type;
ble_uuid.uuid = MYUUID;

attr.p_uuid = &ble_uuid;
attr.p_attr_md = &desc_md;
attr.init_len = sizeof(value);
attr.init_offs = 0;
attr.max_len = sizeof(value);
attr.p_value = (uint8_t*)&value;

return sd_ble_gatts_descriptor_add(p_service->char_handles.value_handle, &attr, &p_service->desc_handle);

Parents
  • Hi,

    If you make sure that the value is never overwritten you can use VLOC_USER. The important thing is to keep that specific part of memory “alive” as long as the SoftDevice is running. That is because the SoftDevice might read and write to it without asking the application whether the data is still valid.

    As a side note: If you need to update the value you can always use sd_ble_gatts_value_set() even though you are using VLOC_USER. You can also write the value directly in memory, but you cannot however, directly update the length of the value, since the length variable is always stored in stack memory. Hence, it is always safest to use sd_ble_gatts_value_set() because that will update both the value and the length.

Reply
  • Hi,

    If you make sure that the value is never overwritten you can use VLOC_USER. The important thing is to keep that specific part of memory “alive” as long as the SoftDevice is running. That is because the SoftDevice might read and write to it without asking the application whether the data is still valid.

    As a side note: If you need to update the value you can always use sd_ble_gatts_value_set() even though you are using VLOC_USER. You can also write the value directly in memory, but you cannot however, directly update the length of the value, since the length variable is always stored in stack memory. Hence, it is always safest to use sd_ble_gatts_value_set() because that will update both the value and the length.

Children
No Data
Related