Closed codepainters closed 8 months ago
It looks like this simple change indeed solved the problem for me:
diff --git a/src/client/ble_writer.rs b/src/client/ble_writer.rs
index 100b61b..5703129 100644
--- a/src/client/ble_writer.rs
+++ b/src/client/ble_writer.rs
@@ -17,7 +17,12 @@ impl BLEWriter {
pub async fn write_value(&mut self, data: &[u8], response: bool) -> Result<(), BLEReturnCode> {
unsafe {
- let mtu = { esp_idf_sys::ble_att_mtu(self.conn_handle) - 3 } as usize;
+ // ble_att_mtu() returns 0 for a closed connection
+ let mtu = esp_idf_sys::ble_att_mtu(self.conn_handle);
+ if mtu == 0 {
+ return Err(BLEReturnCode(esp_idf_sys::BLE_HS_ENOTCONN));
+ }
+ let mtu = { mtu - 3 } as usize;
if !response && data.len() <= mtu {
return ble!(esp_idf_sys::ble_gattc_write_no_rsp_flat(
Shall I open a PR?
Yes, please.
I observe a crash using
esp32-nimble v0.5.1
. The scenario is as follows:BLEClient::connect()
, followed byget_service()
andget_characteristic()
BLERemoteCharacteristic::write_value()
to control the device.At that point my app panics:
Just before the panic I can see it disconnected the device:
Corresponding line:
Obviously the subtraction underflows, because
ble_att_mtu()
returns 0 if connection is gone:I could check the connection state before calling
write_value()
, but there's probably a race condition here (toctou error). I assumed thatwrite_value()
should handle such a situation gracefully.Do I miss something important here (I'm both Rust and BLE noob)?