diff --git a/src/command.rs b/src/command.rs index 2f6b7c0..9c20964 100644 --- a/src/command.rs +++ b/src/command.rs @@ -76,14 +76,33 @@ impl Into for Request { } /// Send data error kind. -#[allow(unused)] #[derive(Debug, Clone, Copy)] pub enum SendDataError { - AckTimeout = 0x01, - InvalidChannel = 0x02, - ChannelBusy = 0x03, - ModuleBusy = 0x04, - PayloadInvalid = 0xFF, + /// No ACK received within a time-out after using all MAC retrys. + AckTimeout, + /// Invalid channel selected. + InvalidChannel, + /// Channel is busy. + ChannelBusy, + /// Module is currently busy. + ModuleBusy, + /// Payload too long. + PayloadInvalid, + /// Unrecognised error response. + Other(u8), +} + +impl From for SendDataError { + fn from(value: u8) -> Self { + match value { + 0x01 => SendDataError::AckTimeout, + 0x02 => SendDataError::InvalidChannel, + 0x03 => SendDataError::ChannelBusy, + 0x04 => SendDataError::ModuleBusy, + 0xFF => SendDataError::PayloadInvalid, + _ => SendDataError::Other(value), + } + } } /// Command response. diff --git a/src/lib.rs b/src/lib.rs index 0e9b83a..2cb57ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ use core::task::Poll; pub use command::{Event, Response}; -use command::{command, MAX_PAYLOAD_LEN, START}; +use command::{command, Request, SendDataError, MAX_PAYLOAD_LEN, START}; use embedded_io_async::{Read, Write}; use heapless::spsc::{Consumer, Producer, Queue}; use heapless::Vec; @@ -75,6 +75,22 @@ where .await } + pub async fn send(&mut self, data: &[u8]) -> Result<(), Error> { + let mut buf = [0; 224]; + let size = command(&mut buf, Request::SendData, data); + self.serial.write(&buf[..size]).await.map_err(Error::Io)?; + + let response = self.poll_response().await; + + let status: u8 = response.data[0].into(); + + if status == 0 { + Ok(()) + } else { + Err(Error::Status(status.into())) + } + } + /// Performs a soft-reset of the radio module. /// /// Returns [`Ok`] once the reset has been confirmed by the device. @@ -85,14 +101,12 @@ where let response = self.poll_response().await; - if let Some(status) = response.data.get(0) { - if *status == 0 { - return Ok(()); - } else { - return Err(Error::Status(())); - } + let status = response.data[0]; + + if status == 0 { + Ok(()) } else { - return Err(Error::Status(())); + Err(Error::Status(())) } }