Skip to content

Commit 894886d

Browse files
fix prepare writes to write in chunks
1 parent bf20286 commit 894886d

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

connectivity/FEATURE_BLE/libraries/ble-api-implementation/source/GattClientImpl.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -637,11 +637,14 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
637637
using ProcedureControlBlock::connection_handle;
638638

639639
WriteControlBlock(
640-
connection_handle_t connection_handle, uint16_t attribute_handle,
641-
uint8_t* data, uint16_t len
640+
connection_handle_t connection_handle,
641+
uint16_t attribute_handle,
642+
uint8_t* data,
643+
uint16_t write_length,
644+
uint16_t chunk_size
642645
) : ProcedureControlBlock(WRITE_PROCEDURE, connection_handle),
643-
attribute_handle(attribute_handle), len(len), offset(0), data(data),
644-
prepare_success(false), status(BLE_ERROR_UNSPECIFIED), error_code(0xFF) {
646+
attribute_handle(attribute_handle), write_length(write_length), chunk_size(chunk_size), offset(0), data(data),
647+
prepare_success(false), status(BLE_ERROR_INITIALIZATION_INCOMPLETE), error_code(0xFF) {
645648
}
646649

647650
virtual ~WriteControlBlock() {
@@ -722,19 +725,21 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
722725

723726
void handle_prepare_write_response(GattClient* client, const AttPrepareWriteResponse& write_response) {
724727
ble_error_t err = BLE_ERROR_UNSPECIFIED;
725-
726728
uint16_t mtu_size = client->get_mtu(connection_handle);
727-
offset = write_response.offset + write_response.partial_value.size();
728-
if (offset < len) {
729+
offset += chunk_size;
730+
uint16_t data_left = write_length - offset; /* offset is guaranteed to be less of equal to write_length */
731+
if (data_left) {
732+
if (chunk_size > data_left) {
733+
chunk_size = data_left;
734+
}
729735
err = client->_pal_client.queue_prepare_write(
730-
connection_handle, attribute_handle,
731-
make_const_Span(
732-
data + offset,
733-
std::min((len - offset), (mtu_size - 5))
734-
),
736+
connection_handle,
737+
attribute_handle,
738+
make_const_Span(data + offset, chunk_size),
735739
offset
736740
);
737741
} else {
742+
prepare_success = true;
738743
err = client->_pal_client.execute_write_queue(
739744
connection_handle, true
740745
);
@@ -829,8 +834,9 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
829834
}
830835

831836
uint16_t attribute_handle;
832-
uint16_t len;
837+
uint16_t write_length;
833838
uint16_t offset;
839+
uint16_t chunk_size;
834840
uint8_t* data;
835841
bool prepare_success;
836842
ble_error_t status;
@@ -1184,7 +1190,8 @@ ble_error_t GattClient::write(
11841190
connection_handle,
11851191
attribute_handle,
11861192
data,
1187-
length
1193+
length,
1194+
mtu - PREPARE_WRITE_HEADER_LENGTH
11881195
);
11891196

11901197
if (write_pcb == nullptr) {
@@ -1199,7 +1206,7 @@ ble_error_t GattClient::write(
11991206
err = _pal_client.queue_prepare_write(
12001207
connection_handle,
12011208
attribute_handle,
1202-
make_const_Span(value, mtu - PREPARE_WRITE_HEADER_LENGTH),
1209+
make_const_Span(data, mtu - PREPARE_WRITE_HEADER_LENGTH),
12031210
/* offset */0
12041211
);
12051212
} else {

0 commit comments

Comments
 (0)