Skip to content

Commit

Permalink
Merge pull request #3993 from dhalbert/gattc-write-lengths
Browse files Browse the repository at this point in the history
don't check length for remote characteristic or descriptor
  • Loading branch information
tannewt authored Jan 14, 2021
2 parents 59003e9 + b05c6ba commit e6b49d9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
15 changes: 8 additions & 7 deletions ports/nrf/common-hal/_bleio/Characteristic.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ size_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *sel
}

void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
if (self->fixed_length && bufinfo->len != self->max_length) {
mp_raise_ValueError(translate("Value length != required fixed length"));
}
if (bufinfo->len > self->max_length) {
mp_raise_ValueError(translate("Value length > max_length"));
}

// Do GATT operations only if this characteristic has been added to a registered service.
if (self->handle != BLE_GATT_HANDLE_INVALID) {

Expand All @@ -148,6 +141,14 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
common_hal_bleio_gattc_write(self->handle, conn_handle, bufinfo,
(self->props & CHAR_PROP_WRITE_NO_RESPONSE));
} else {
// Validate data length for local characteristics only.
if (self->fixed_length && bufinfo->len != self->max_length) {
mp_raise_ValueError(translate("Value length != required fixed length"));
}
if (bufinfo->len > self->max_length) {
mp_raise_ValueError(translate("Value length > max_length"));
}

// Always write the value locally even if no connections are active.
// conn_handle is ignored for non-system attributes, so we use BLE_CONN_HANDLE_INVALID.
common_hal_bleio_gatts_write(self->handle, BLE_CONN_HANDLE_INVALID, bufinfo);
Expand Down
2 changes: 1 addition & 1 deletion ports/nrf/common-hal/_bleio/Connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ STATIC void on_char_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *response, bleio
common_hal_bleio_characteristic_construct(
characteristic, m_char_discovery_service, gattc_char->handle_value, uuid,
props, SECURITY_MODE_OPEN, SECURITY_MODE_OPEN,
GATT_MAX_DATA_LENGTH, false, // max_length, fixed_length: values may not matter for gattc
GATT_MAX_DATA_LENGTH, false, // max_length, fixed_length: values don't matter for gattc
mp_const_empty_bytes);

mp_obj_list_append(MP_OBJ_FROM_PTR(m_char_discovery_service->characteristic_list),
Expand Down
15 changes: 8 additions & 7 deletions ports/nrf/common-hal/_bleio/Descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,21 @@ size_t common_hal_bleio_descriptor_get_value(bleio_descriptor_obj_t *self, uint8
}

void common_hal_bleio_descriptor_set_value(bleio_descriptor_obj_t *self, mp_buffer_info_t *bufinfo) {
if (self->fixed_length && bufinfo->len != self->max_length) {
mp_raise_ValueError(translate("Value length != required fixed length"));
}
if (bufinfo->len > self->max_length) {
mp_raise_ValueError(translate("Value length > max_length"));
}

// Do GATT operations only if this descriptor has been registered.
if (self->handle != BLE_GATT_HANDLE_INVALID) {
uint16_t conn_handle = bleio_connection_get_conn_handle(self->characteristic->service->connection);
if (common_hal_bleio_service_get_is_remote(self->characteristic->service)) {
// false means WRITE_REQ, not write-no-response
common_hal_bleio_gattc_write(self->handle, conn_handle, bufinfo, false);
} else {
// Validate data length for local descriptors only.
if (self->fixed_length && bufinfo->len != self->max_length) {
mp_raise_ValueError(translate("Value length != required fixed length"));
}
if (bufinfo->len > self->max_length) {
mp_raise_ValueError(translate("Value length > max_length"));
}

common_hal_bleio_gatts_write(self->handle, conn_handle, bufinfo);
}
}
Expand Down

0 comments on commit e6b49d9

Please sign in to comment.