summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Shawcroft <scott@adafruit.com>2021-01-14 12:56:29 -0800
committerGitHub <noreply@github.com>2021-01-14 12:56:29 -0800
commite6b49d95ee8e035f1b6ed33f10cbd11288a77a18 (patch)
tree78de158b5309ae13069245bc0e84049b72291a17
parent59003e96af8d955fecdf3648b92fdb0e0a35657e (diff)
parentb05c6bac21dedafd60d0a81ea6c6b68c313a57b2 (diff)
Merge pull request #3993 from dhalbert/gattc-write-lengths
don't check length for remote characteristic or descriptor
-rw-r--r--ports/nrf/common-hal/_bleio/Characteristic.c15
-rw-r--r--ports/nrf/common-hal/_bleio/Connection.c2
-rw-r--r--ports/nrf/common-hal/_bleio/Descriptor.c15
3 files changed, 17 insertions, 15 deletions
diff --git a/ports/nrf/common-hal/_bleio/Characteristic.c b/ports/nrf/common-hal/_bleio/Characteristic.c
index 57c481448..c0f0372c3 100644
--- a/ports/nrf/common-hal/_bleio/Characteristic.c
+++ b/ports/nrf/common-hal/_bleio/Characteristic.c
@@ -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) {
@@ -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);
diff --git a/ports/nrf/common-hal/_bleio/Connection.c b/ports/nrf/common-hal/_bleio/Connection.c
index 4f747bf97..36fda0bb3 100644
--- a/ports/nrf/common-hal/_bleio/Connection.c
+++ b/ports/nrf/common-hal/_bleio/Connection.c
@@ -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),
diff --git a/ports/nrf/common-hal/_bleio/Descriptor.c b/ports/nrf/common-hal/_bleio/Descriptor.c
index 9e9110723..d848659fc 100644
--- a/ports/nrf/common-hal/_bleio/Descriptor.c
+++ b/ports/nrf/common-hal/_bleio/Descriptor.c
@@ -73,13 +73,6 @@ 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);
@@ -87,6 +80,14 @@ void common_hal_bleio_descriptor_set_value(bleio_descriptor_obj_t *self, mp_buff
// 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);
}
}