From f0faa61a921bbd12fadf5e1bfeb4480d267eb0f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Wed, 5 Feb 2025 11:57:42 +0100 Subject: [PATCH] Read more bytes in one go in BleConnector (#3098) --- esp-wifi/src/ble/controller/mod.rs | 45 ++++++++++++++---------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/esp-wifi/src/ble/controller/mod.rs b/esp-wifi/src/ble/controller/mod.rs index 439c7394658..6e838164372 100644 --- a/esp-wifi/src/ble/controller/mod.rs +++ b/esp-wifi/src/ble/controller/mod.rs @@ -50,20 +50,17 @@ impl ErrorType for BleConnector<'_> { } impl Read for BleConnector<'_> { - fn read(&mut self, buf: &mut [u8]) -> Result { + fn read(&mut self, mut buf: &mut [u8]) -> Result { let mut total = 0; - for b in buf { - let mut buffer = [0u8]; - let len = read_hci(&mut buffer); - - if len == 1 { - *b = buffer[0]; - total += 1; - } else { - return Ok(total); + while !buf.is_empty() { + let len = read_hci(buf); + if len == 0 { + break; } - } + buf = &mut buf[len..]; + total += len; + } Ok(total) } } @@ -105,24 +102,24 @@ pub(crate) mod asynch { } impl embedded_io_async::Read for BleConnector<'_> { - async fn read(&mut self, buf: &mut [u8]) -> Result { - if !have_hci_read_data() { - HciReadyEventFuture.await; + async fn read(&mut self, mut buf: &mut [u8]) -> Result { + if buf.is_empty() { + return Ok(0); } let mut total = 0; - for b in buf { - let mut buffer = [0u8]; - let len = read_hci(&mut buffer); - - if len == 1 { - *b = buffer[0]; - total += 1; - } else { - return Ok(total); - } + if !have_hci_read_data() { + HciReadyEventFuture.await; } + while !buf.is_empty() { + let len = read_hci(buf); + if len == 0 { + break; + } + buf = &mut buf[len..]; + total += len; + } Ok(total) } }