@@ -637,11 +637,14 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
637
637
using ProcedureControlBlock::connection_handle;
638
638
639
639
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
642
645
) : 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 ) {
645
648
}
646
649
647
650
virtual ~WriteControlBlock () {
@@ -722,19 +725,21 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
722
725
723
726
void handle_prepare_write_response (GattClient* client, const AttPrepareWriteResponse& write_response) {
724
727
ble_error_t err = BLE_ERROR_UNSPECIFIED;
725
-
726
728
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
+ }
729
735
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),
735
739
offset
736
740
);
737
741
} else {
742
+ prepare_success = true ;
738
743
err = client->_pal_client .execute_write_queue (
739
744
connection_handle, true
740
745
);
@@ -829,8 +834,9 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
829
834
}
830
835
831
836
uint16_t attribute_handle;
832
- uint16_t len ;
837
+ uint16_t write_length ;
833
838
uint16_t offset;
839
+ uint16_t chunk_size;
834
840
uint8_t * data;
835
841
bool prepare_success;
836
842
ble_error_t status;
@@ -1184,7 +1190,8 @@ ble_error_t GattClient::write(
1184
1190
connection_handle,
1185
1191
attribute_handle,
1186
1192
data,
1187
- length
1193
+ length,
1194
+ mtu - PREPARE_WRITE_HEADER_LENGTH
1188
1195
);
1189
1196
1190
1197
if (write_pcb == nullptr ) {
@@ -1199,7 +1206,7 @@ ble_error_t GattClient::write(
1199
1206
err = _pal_client.queue_prepare_write (
1200
1207
connection_handle,
1201
1208
attribute_handle,
1202
- make_const_Span (value , mtu - PREPARE_WRITE_HEADER_LENGTH),
1209
+ make_const_Span (data , mtu - PREPARE_WRITE_HEADER_LENGTH),
1203
1210
/* offset */ 0
1204
1211
);
1205
1212
} else {
0 commit comments