Skip to content

Commit

Permalink
Properly handle 0-len buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Sep 18, 2023
1 parent 1034326 commit 2f3313a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 37 deletions.
8 changes: 6 additions & 2 deletions src/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,9 @@ where

pub async fn transmit(&self, frame: &Frame) -> Result<(), EspError> {
loop {
match self.driver.borrow().transmit(frame, delay::NON_BLOCK) {
let res = self.driver.borrow().transmit(frame, delay::NON_BLOCK);

match res {
Ok(()) => return Ok(()),
Err(e) if e.code() != ESP_ERR_TIMEOUT => return Err(e),
_ => (),
Expand All @@ -645,7 +647,9 @@ where

pub async fn receive(&self) -> Result<Frame, EspError> {
loop {
match self.driver.borrow().receive(delay::NON_BLOCK) {
let res = self.driver.borrow().receive(delay::NON_BLOCK);

match res {
Ok(frame) => return Ok(frame),
Err(e) if e.code() != ESP_ERR_TIMEOUT => return Err(e),
_ => (),
Expand Down
98 changes: 63 additions & 35 deletions src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ where
AsyncUartTxDriver<'_, UartTxDriver<'_>>,
AsyncUartRxDriver<'_, UartRxDriver<'_>>,
) {
let (tx, rx) = self.driver().split();
let (tx, rx) = self.driver_mut().split();

(
AsyncUartTxDriver {
Expand All @@ -1424,34 +1424,48 @@ where
}

pub async fn read(&self, buf: &mut [u8]) -> Result<usize, EspError> {
loop {
match self.driver.borrow().read(buf, delay::NON_BLOCK) {
Ok(len) => return Ok(len),
Err(e) if e.code() != ESP_ERR_TIMEOUT => return Err(e),
_ => (),
}
if buf.is_empty() {
Ok(0)
} else {
loop {
let res = self.driver.borrow().read(buf, delay::NON_BLOCK);

let port = self.driver.borrow().port as usize;
READ_NOTIFS[port].wait().await;
match res {
Ok(len) => return Ok(len),
Err(e) if e.code() != ESP_ERR_TIMEOUT => return Err(e),
_ => (),
}

let port = self.driver.borrow().port as usize;
READ_NOTIFS[port].wait().await;
}
}
}

pub async fn write(&self, bytes: &[u8]) -> Result<usize, EspError> {
loop {
match self.driver.borrow().write_nb(bytes) {
Ok(len) if len > 0 => return Ok(len),
Err(e) => return Err(e),
_ => (),
}
if bytes.is_empty() {
Ok(0)
} else {
loop {
let res = self.driver.borrow().write_nb(bytes);

let port = self.driver.borrow().port as usize;
WRITE_NOTIFS[port].wait().await;
match res {
Ok(len) if len > 0 => return Ok(len),
Err(e) => return Err(e),
_ => (),
}

let port = self.driver.borrow().port as usize;
WRITE_NOTIFS[port].wait().await;
}
}
}

pub async fn wait_tx_done(&self) -> Result<(), EspError> {
loop {
match self.driver.borrow().wait_tx_done(delay::NON_BLOCK) {
let res = self.driver.borrow().wait_tx_done(delay::NON_BLOCK);

match res {
Ok(()) => return Ok(()),
Err(e) if e.code() != ESP_ERR_TIMEOUT => return Err(e),
_ => (),
Expand Down Expand Up @@ -1562,15 +1576,21 @@ where
}

pub async fn read(&self, buf: &mut [u8]) -> Result<usize, EspError> {
loop {
match self.driver.borrow().read(buf, delay::NON_BLOCK) {
Ok(len) => return Ok(len),
Err(e) if e.code() != ESP_ERR_TIMEOUT => return Err(e),
_ => (),
}
if buf.is_empty() {
Ok(0)
} else {
loop {
let res = self.driver.borrow().read(buf, delay::NON_BLOCK);

let port = self.driver.borrow().port as usize;
READ_NOTIFS[port].wait().await;
match res {
Ok(len) => return Ok(len),
Err(e) if e.code() != ESP_ERR_TIMEOUT => return Err(e),
_ => (),
}

let port = self.driver.borrow().port as usize;
READ_NOTIFS[port].wait().await;
}
}
}
}
Expand Down Expand Up @@ -1660,21 +1680,29 @@ where
}

pub async fn write(&self, bytes: &[u8]) -> Result<usize, EspError> {
loop {
match self.driver.borrow().write_nb(bytes) {
Ok(len) if len > 0 => return Ok(len),
Err(e) => return Err(e),
_ => (),
}
if bytes.is_empty() {
Ok(0)
} else {
loop {
let res = self.driver.borrow().write_nb(bytes);

let port = self.driver.borrow().port as usize;
WRITE_NOTIFS[port].wait().await;
match res {
Ok(len) if len > 0 => return Ok(len),
Err(e) => return Err(e),
_ => (),
}

let port = self.driver.borrow().port as usize;
WRITE_NOTIFS[port].wait().await;
}
}
}

pub async fn wait_done(&self) -> Result<(), EspError> {
loop {
match self.driver.borrow().wait_done(delay::NON_BLOCK) {
let res = self.driver.borrow().wait_done(delay::NON_BLOCK);

match res {
Ok(()) => return Ok(()),
Err(e) if e.code() != ESP_ERR_TIMEOUT => return Err(e),
_ => (),
Expand Down

0 comments on commit 2f3313a

Please sign in to comment.