Skip to content

Commit

Permalink
Separate concept of minlen and len for BLE chars
Browse files Browse the repository at this point in the history
In previous versions of BLE_API the GattCharacteristic initLen parameter is
named minLen as well. When the characteristic is committed to the SoftDevice
the value of initial length is also used as the minimum length of the
characteristic value. Furthermore, the test (max_length == min_length) is used
to determine whether the characteristic value has variable length. This is
slightly confusing and also causes problems if the user wishes to use a
characteristic with variable length but the initial lenght is equal to max
length.

To solve this problem the characteristic is now always committed to the
SoftDevice as variable. Furthermore, the API only maintains the current lenght
and the max length i.e. the field initialLen in the GattAttribute is removed.
In nRF5xGattServer all calls to getInitialLength() are removed and replaced
with getLength().

*NOTES:*
* This change requires updates to ble.
* Ideally we would like the characteristics to be declared as 'variable' only
when necessary, but this requires changing the signature of the
GattCharacteristic and GattAttribute constructors. Therefore, it will be part
of a separate pull request.
  • Loading branch information
Andres Amaya Garcia authored and Andres Amaya Garcia committed Nov 27, 2015
1 parent e19b28b commit 3fb32e1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions source/btle/custom/custom_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ error_t custom_add_in_characteristic(uint16_t service_handle,
uint8_t properties,
SecurityManager::SecurityMode_t requiredSecurity,
uint8_t *p_data,
uint16_t min_length,
uint16_t length,
uint16_t max_length,
const uint8_t *userDescriptionDescriptorValuePtr,
uint16_t userDescriptionDescriptorValueLen,
Expand Down Expand Up @@ -242,7 +242,8 @@ error_t custom_add_in_characteristic(uint16_t service_handle,
attr_md.wr_auth = writeAuthorization;

attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.vlen = (min_length == max_length) ? 0 : 1;
/* Always set variable size */
attr_md.vlen = 1;

if (char_props.read || char_props.notify || char_props.indicate) {
switch (requiredSecurity) {
Expand Down Expand Up @@ -292,7 +293,7 @@ error_t custom_add_in_characteristic(uint16_t service_handle,

attr_char_value.p_uuid = p_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = min_length;
attr_char_value.init_len = length;
attr_char_value.max_len = max_length;
attr_char_value.p_value = p_data;

Expand Down Expand Up @@ -325,15 +326,16 @@ error_t custom_add_in_characteristic(uint16_t service_handle,
error_t custom_add_in_descriptor(uint16_t char_handle,
ble_uuid_t *p_uuid,
uint8_t *p_data,
uint16_t min_length,
uint16_t length,
uint16_t max_length,
uint16_t *p_desc_handle)
{
/* Descriptor metadata */
ble_gatts_attr_md_t desc_md = {0};

desc_md.vloc = BLE_GATTS_VLOC_STACK;
desc_md.vlen = (min_length == max_length) ? 0 : 1;
/* Always set variable size */
desc_md.vlen = 1;

/* Make it readable and writable */
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.read_perm);
Expand All @@ -343,7 +345,7 @@ error_t custom_add_in_descriptor(uint16_t char_handle,

attr_desc.p_uuid = p_uuid;
attr_desc.p_attr_md = &desc_md;
attr_desc.init_len = min_length;
attr_desc.init_len = length;
attr_desc.max_len = max_length;
attr_desc.p_value = p_data;

Expand Down
4 changes: 2 additions & 2 deletions source/btle/custom/custom_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ error_t custom_add_in_characteristic(uint16_t service_handle,
uint8_t properties,
SecurityManager::SecurityMode_t requiredSecurity,
uint8_t *p_data,
uint16_t min_length,
uint16_t length,
uint16_t max_length,
const uint8_t *userDescriptionDescriptorValuePtr,
uint16_t userDescriptionDescriptorValueLen,
Expand All @@ -47,7 +47,7 @@ error_t custom_add_in_characteristic(uint16_t service_handle,
error_t custom_add_in_descriptor(uint16_t char_handle,
ble_uuid_t *p_uuid,
uint8_t *p_data,
uint16_t min_length,
uint16_t length,
uint16_t max_length,
uint16_t *p_desc_handle);

Expand Down
6 changes: 3 additions & 3 deletions source/nRF5xGattServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ ble_error_t nRF5xGattServer::addService(GattService &service)

/* Skip any incompletely defined, read-only characteristics. */
if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
(p_char->getValueAttribute().getInitialLength() == 0) &&
(p_char->getValueAttribute().getLength() == 0) &&
(p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
continue;
}
Expand All @@ -95,7 +95,7 @@ ble_error_t nRF5xGattServer::addService(GattService &service)
p_char->getProperties(),
p_char->getRequiredSecurity(),
p_char->getValueAttribute().getValuePtr(),
p_char->getValueAttribute().getInitialLength(),
p_char->getValueAttribute().getLength(),
p_char->getValueAttribute().getMaxLength(),
userDescriptionDescriptorValuePtr,
userDescriptionDescriptorValueLen,
Expand Down Expand Up @@ -127,7 +127,7 @@ ble_error_t nRF5xGattServer::addService(GattService &service)
custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
&nordicUUID,
p_desc->getValuePtr(),
p_desc->getInitialLength(),
p_desc->getLength(),
p_desc->getMaxLength(),
&nrfDescriptorHandles[descriptorCount]),
BLE_ERROR_PARAM_OUT_OF_RANGE);
Expand Down

0 comments on commit 3fb32e1

Please sign in to comment.