Skip to content

Commit 2fd3d04

Browse files
authored
Fix espressif#4046 Details below: (espressif#4086)
Informed by the discussion in the bug and the code in 'that other branch' the fix was clear. Just set a flag if we start handling a write, and use that flag to guard the long write complete call.
1 parent b551310 commit 2fd3d04

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

Diff for: libraries/BLE/src/BLECharacteristic.cpp

+17-13
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,22 @@ void BLECharacteristic::handleGATTServerEvent(
219219
// - uint8_t exec_write_flag - Either ESP_GATT_PREP_WRITE_EXEC or ESP_GATT_PREP_WRITE_CANCEL
220220
//
221221
case ESP_GATTS_EXEC_WRITE_EVT: {
222-
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
223-
m_value.commit();
224-
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
225-
} else {
226-
m_value.cancel();
227-
}
228-
// ???
229-
esp_err_t errRc = ::esp_ble_gatts_send_response(
230-
gatts_if,
231-
param->write.conn_id,
232-
param->write.trans_id, ESP_GATT_OK, nullptr);
233-
if (errRc != ESP_OK) {
234-
log_e("esp_ble_gatts_send_response: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
222+
if(m_writeEvt){
223+
m_writeEvt = false;
224+
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
225+
m_value.commit();
226+
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
227+
} else {
228+
m_value.cancel();
229+
}
230+
// ???
231+
esp_err_t errRc = ::esp_ble_gatts_send_response(
232+
gatts_if,
233+
param->write.conn_id,
234+
param->write.trans_id, ESP_GATT_OK, nullptr);
235+
if (errRc != ESP_OK) {
236+
log_e("esp_ble_gatts_send_response: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
237+
}
235238
}
236239
break;
237240
} // ESP_GATTS_EXEC_WRITE_EVT
@@ -277,6 +280,7 @@ void BLECharacteristic::handleGATTServerEvent(
277280
if (param->write.handle == m_handle) {
278281
if (param->write.is_prep) {
279282
m_value.addPart(param->write.value, param->write.len);
283+
m_writeEvt = true;
280284
} else {
281285
setValue(param->write.value, param->write.len);
282286
}

Diff for: libraries/BLE/src/BLECharacteristic.h

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class BLECharacteristic {
107107
BLEService* m_pService;
108108
BLEValue m_value;
109109
esp_gatt_perm_t m_permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
110+
bool m_writeEvt = false; // If we have started a long write, this tells the commit code that we were the target
110111

111112
void handleGATTServerEvent(
112113
esp_gatts_cb_event_t event,

0 commit comments

Comments
 (0)