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

Where is characteristic value data kept?

Hello,

Is the characteristic value data supplied to sd_ble_gatts_characteristic_add() and sd_ble_gatts_value_set() copied into the softdevice RAM, or does the softdevice refer to application RAM whenever client requests a characteristic read? I cannot find this information in the SDK docs.

Consider the following code:

#define BUFSIZE 64

char *str = malloc(BUFSIZE);
strcpy(str, "Hello world");

[...]

attr_char_value.init_len  = strlen(str)+1;
attr_char_value.init_offs = 0;
attr_char_value.max_len   = BUFSIZE;
attr_char_value.p_value   = (uint8_t*) str;

uint32_t err_code = sd_ble_gatts_characteristic_add(
		BLE_GATT_HANDLE_INVALID, &char_md, &attr_char_value, &blue_char_handle);

[...]

free(str);

In this example, will the "Hello world" string be copied into softdevice RAM, and thus can be free()'d safely, or will the application crash on read due to invalid pointer?

  • Found it .

    It's defined by vloc field in ble_gatts_attr_md_t structure which is also passed to sd_ble_gatts_characteristic_add().

    Possible values are:

    #define BLE_GATTS_VLOC_STACK 0x01

    Attribute Value is located in stack memory, no user memory is required.

    #define BLE_GATTS_VLOC_USER 0x02

    Attribute Value is located in user memory. This requires the user to maintain a valid buffer through the lifetime of the attribute, since the stack will read and write directly to the memory using the pointer provided in the APIs. There are no alignment requirements for the buffer.

Related