8000 Merge pull request #3993 from dhalbert/gattc-write-lengths · kmatch98/circuitpython@e6b49d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6b49d9

Browse files
authored
Merge pull request adafruit#3993 from dhalbert/gattc-write-lengths
don't check length for remote characteristic or descriptor
2 parents 59003e9 + b05c6ba commit e6b49d9

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

ports/nrf/common-hal/_bleio/Characteristic.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,6 @@ size_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *sel
132132
}
133133

134134
void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
135-
if (self->fixed_length && bufinfo->len != self->max_length) {
136-
mp_raise_ValueError(translate("Value length != required fixed length"));
137-
}
138-
if (bufinfo->len > self->max_length) {
139-
mp_raise_ValueError(translate("Value length > max_length"));
140-
}
141-
142135
// Do GATT operations only if this characteristic has been added to a registered service.
143136
if (self->handle != BLE_GATT_HANDLE_INVALID) {
144137

@@ -148,6 +141,14 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
148141
common_hal_bleio_gattc_write(self->handle, conn_handle, bufinfo,
149142
(self->props & CHAR_PROP_WRITE_NO_RESPONSE));
150143
} else {
144+
// Validate data length for local characteristics only.
145+
if (self->fixed_length && bufinfo->len != self->max_length) {
146+
mp_raise_ValueError(translate("Value length != required fixed length"));
147+
}
148+
if (bufinfo->len > self->max_length) {
149+
mp_raise_ValueError(translate("Value length > max_length"));
150+
}
151+
151152
// Always write the value locally even if no connections are active.
152153
// conn_handle is ignored for non-system attributes, so we use BLE_CONN_HANDLE_INVALID.
153154
common_hal_bleio_gatts_write(self->handle, BLE_CONN_HANDLE_INVALID, bufinfo);

ports/nrf/common-hal/_bleio/Connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ STATIC void on_char_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *response, bleio
522522
common_hal_bleio_characteristic_construct(
523523
characteristic, m_char_discovery_service, gattc_char->handle_value, uuid,
524524
props, SECURITY_MODE_OPEN, SECURITY_MODE_OPEN,
525-
GATT_MAX_DATA_LENGTH, false, // max_length, fixed_length: values may not matter for gattc
525+
GATT_MAX_DATA_LENGTH, false, // max_length, fixed_length: values don't matter for gattc
526526
mp_const_empty_bytes);
527527

528528
mp_obj_list_append(MP_OBJ_FROM_PTR(m_char_discovery_service->characteristic_list),

ports/nrf/common-hal/_bleio/Descriptor.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,21 @@ size_t common_hal_bleio_descriptor_get_value(bleio_descriptor_obj_t *self, uint8
7373
}
7474

7575
void common_hal_bleio_descriptor_set_value(bleio_descriptor_obj_t *self, mp_buffer_info_t *bufinfo) {
76-
if (self->fixed_length && bufinfo->len != self->max_length) {
77-
mp_raise_ValueError(translate("Value length != required fixed length"));
78-
}
79-
if (bufinfo->len > self->max_length) {
80-
mp_raise_ValueError(translate("Value length > max_length"));
81-
}
82-
8376
// Do GATT operations only if this descriptor has been registered.
8477
if (self->handle != BLE_GATT_HANDLE_INVALID) {
8578
uint16_t conn_handle = bleio_connection_get_conn_handle(self->characteristic->service->connection);
8679
if (common_hal_bleio_service_get_is_remote(self->characteristic->service)) {
8780
// false means WRITE_REQ, not write-no-response
8881
common_hal_bleio_gattc_write(self->handle, conn_handle, bufinfo, false);
8982
} else {
83+
// Validate data length for local descriptors only.
84+
if (self->fixed_length && bufinfo->len != self->max_length) {
85+
mp_raise_ValueError(translate("Value length != required fixed length"));
86+
}
87+
if (bufinfo->len > self->max_length) {
88+
mp_raise_ValueError(translate("Value length > max_length"));
89+
}
90+
9091
common_hal_bleio_gatts_write(self->handle, conn_handle, bufinfo);
9192
}
9293
}

0 commit comments

Comments
 (0)
0