Skip to content

Commit

Permalink
Upgrade to use embedded-hal version 1.0-alpha.4 (#71)
Browse files Browse the repository at this point in the history
* Upgrade to use embedded-hal version 1.0-alpha.4

* Use embedded-storage, and alpha.4 of embedded-hal

* Use crates version of stm32l4xx-hal as dev-dependency

* Formatting

* Fix clippy error
  • Loading branch information
MathiasKoch committed Dec 10, 2020
1 parent c2d3ec2 commit 63e4524
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -12,7 +12,7 @@
<img style="vertical-align:middle; padding-bottom: 20px; padding-right: 40px;" src="https://w7.pngwing.com/pngs/154/828/png-transparent-star-wars-patent-all-terrain-armored-transport-printmaking-atat-uuml-rk-monochrome-film-mecha.png" alt="ATAT" width="250" />
</div>

`#![no_std]` crate for parsing AT commands
`#![no_std]` crate for parsing AT commands ([Hayes command set](https://en.wikipedia.org/wiki/Hayes_command_set))

A driver support crate for AT-command based serial modules, using the [embedded-hal] traits.

Expand Down
13 changes: 6 additions & 7 deletions atat/Cargo.toml
Expand Up @@ -18,24 +18,23 @@ name = "atat"
maintenance = { status = "actively-developed" }

[dependencies]
embedded-hal = "^0.2"
embedded-hal = { version = "1.0.0-alpha.4" }
nb = "^1"
void = { version = "^1", default-features = false }
heapless = { version = "0.5.5", features = ["serde"] }
serde_at = { path = "../serde_at", version = "^0.5.0"}
heapless = { version = "0.5.6", features = ["serde"] }
serde_at = { path = "../serde_at", version = "^0.5.0" }
atat_derive = { path = "../atat_derive", version = "^0.5.0", optional = true }
serde = {version = "^1", default-features = false}
serde = { version = "^1", default-features = false }
typenum = "^1"

log = { version = "^0.4", default-features = false, optional = true }
defmt = { git = "https://github.com/knurling-rs/defmt", branch = "main" }
defmt = { version = "^0.1" }

[dev-dependencies]
cortex-m = "0.6.3"
cortex-m-rt = "0.6.12"
cortex-m-rtic = "0.5.4"
panic-halt = "0.2.0"
stm32l4xx-hal = { git = "https://github.com/stm32-rs/stm32l4xx-hal", features = ["stm32l4x5", "rt"] }
stm32l4xx-hal = { version = "0.5", features = ["stm32l4x5", "rt"] }

[features]
default = ["derive"]
Expand Down
36 changes: 19 additions & 17 deletions atat/src/client.rs
Expand Up @@ -110,7 +110,7 @@ where
// compare the time of the last response or URC and ensure at least
// `self.config.cmd_cooldown` ms have passed before sending a new
// command
nb::block!(self.timer.wait()).ok();
nb::block!(self.timer.try_wait()).ok();
let cmd_buf = cmd.as_bytes();

match core::str::from_utf8(&cmd_buf) {
Expand All @@ -133,25 +133,25 @@ where
};

for c in cmd_buf {
nb::block!(self.tx.write(c)).map_err(|_e| Error::Write)?;
nb::block!(self.tx.try_write(c)).map_err(|_e| Error::Write)?;
}
nb::block!(self.tx.flush()).map_err(|_e| Error::Write)?;
nb::block!(self.tx.try_flush()).map_err(|_e| Error::Write)?;
self.state = ClientState::AwaitingResponse;
}

match self.config.mode {
Mode::Blocking => Ok(nb::block!(self.check_response(cmd))?),
Mode::NonBlocking => self.check_response(cmd),
Mode::Timeout => {
self.timer.start(cmd.max_timeout_ms());
self.timer.try_start(cmd.max_timeout_ms()).ok();
Ok(nb::block!(self.check_response(cmd))?)
}
}
}

fn peek_urc_with<URC: AtatUrc, F: FnOnce(URC::Response) -> bool>(&mut self, f: F) {
if let Some(urc) = self.urc_c.peek() {
self.timer.start(self.config.cmd_cooldown);
self.timer.try_start(self.config.cmd_cooldown).ok();
if let Ok(urc) = URC::parse(urc) {
if !f(urc) {
return;
Expand All @@ -166,17 +166,21 @@ where
return match result {
Ok(ref resp) => {
if let ClientState::AwaitingResponse = self.state {
self.timer.start(self.config.cmd_cooldown);
self.timer.try_start(self.config.cmd_cooldown).ok();
self.state = ClientState::Idle;
Ok(cmd.parse(resp).map_err(nb::Error::Other)?)
} else {
Err(nb::Error::WouldBlock)
}
}
Err(e) => Err(nb::Error::Other(e)),
Err(e) => {
self.timer.try_start(self.config.cmd_cooldown).ok();
self.state = ClientState::Idle;
Err(nb::Error::Other(e))
}
};
} else if let Mode::Timeout = self.config.mode {
if self.timer.wait().is_ok() {
if self.timer.try_wait().is_ok() {
self.state = ClientState::Idle;
// Tell the parser to clear the buffer due to timeout
if self.com_p.enqueue(Command::ClearBuffer).is_err() {
Expand All @@ -202,21 +206,22 @@ mod test {
use crate::queues;
use heapless::{consts, spsc::Queue, String, Vec};
use nb;
use void::Void;

struct CdMock {
time: u32,
}

impl CountDown for CdMock {
type Error = core::convert::Infallible;
type Time = u32;
fn start<T>(&mut self, count: T)
fn try_start<T>(&mut self, count: T) -> Result<(), Self::Error>
where
T: Into<Self::Time>,
{
self.time = count.into();
Ok(())
}
fn wait(&mut self) -> nb::Result<(), Void> {
fn try_wait(&mut self) -> nb::Result<(), Self::Error> {
Ok(())
}
}
Expand All @@ -234,11 +239,11 @@ mod test {
impl serial::Write<u8> for TxMock {
type Error = ();

fn write(&mut self, c: u8) -> nb::Result<(), Self::Error> {
fn try_write(&mut self, c: u8) -> nb::Result<(), Self::Error> {
self.s.push(c as char).map_err(nb::Error::Other)
}

fn flush(&mut self) -> nb::Result<(), Self::Error> {
fn try_flush(&mut self) -> nb::Result<(), Self::Error> {
Ok(())
}
}
Expand Down Expand Up @@ -498,17 +503,14 @@ mod test {
Vec::<u8, TestRxBufLen>::from_slice(b"+CUN: 22,16,\"0123456789012345\"").unwrap();
p.enqueue(Ok(response)).unwrap();

let res_vec: Vec<u8, TestRxBufLen> =
"0123456789012345".as_bytes().iter().cloned().collect();

assert_eq!(client.state, ClientState::Idle);

assert_eq!(
client.send(&cmd),
Ok(TestResponseVec {
socket: 22,
length: 16,
data: res_vec
data: Vec::from_slice(b"0123456789012345").unwrap()
})
);
assert_eq!(client.state, ClientState::Idle);
Expand Down
28 changes: 14 additions & 14 deletions atat/src/ingress_manager.rs
Expand Up @@ -360,20 +360,20 @@ where
Command::ClearBuffer => {
self.state = State::Idle;
self.buf_incomplete = false;
#[allow(clippy::single_match)]
match core::str::from_utf8(&self.buf) {
Ok(_s) => {
#[cfg(not(feature = "log-logging"))]
atat_log!(debug, "Clearing buffer on timeout / {:str}", _s);
#[cfg(feature = "log-logging")]
atat_log!(debug, "Clearing buffer on timeout / {:?}", _s);
}
Err(_) => atat_log!(
debug,
"Clearing buffer on timeout / {:?}",
core::convert::AsRef::<[u8]>::as_ref(&self.buf)
),
};
// #[allow(clippy::single_match)]
// match core::str::from_utf8(&self.buf) {
// Ok(_s) => {
// #[cfg(not(feature = "log-logging"))]
// atat_log!(debug, "Clearing buffer on timeout / {:str}", _s);
// #[cfg(feature = "log-logging")]
// atat_log!(debug, "Clearing buffer on timeout / {:?}", _s);
// }
// Err(_) => atat_log!(
// debug,
// "Clearing buffer on timeout / {:?}",
// core::convert::AsRef::<[u8]>::as_ref(&self.buf)
// ),
// };

self.clear_buf(true);
}
Expand Down
2 changes: 2 additions & 0 deletions atat/src/lib.rs
Expand Up @@ -229,6 +229,8 @@
#![allow(clippy::unused_unit)]
#![allow(clippy::clippy::used_underscore_binding)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::unused_unit)]
#![allow(clippy::used_underscore_binding)]
#![cfg_attr(not(test), no_std)]

mod client;
Expand Down
2 changes: 1 addition & 1 deletion atat_derive/src/urc.rs
Expand Up @@ -40,7 +40,7 @@ pub fn atat_urc(input: TokenStream) -> TokenStream {
panic!("cannot handle variants with more than one field")
}
quote! {
#code => #ident::#variant_ident(atat::serde_at::from_slice::<#first_field>(resp).map_err(|e| {
#code => #ident::#variant_ident(atat::serde_at::from_slice::<#first_field>(&resp[index..]).map_err(|e| {
atat::Error::ParseString
})?),
}
Expand Down
2 changes: 1 addition & 1 deletion serde_at/src/de/mod.rs
Expand Up @@ -111,7 +111,7 @@ impl<'a> Deserializer<'a> {
let end = self.index;
self.eat_char();
return str::from_utf8(&self.slice[start..end])
.map_err(|_| Error::InvalidUnicodeCodePoint);
.map_err(|_e| Error::InvalidUnicodeCodePoint);
}
Some(_) => self.eat_char(),
None => return Err(Error::EofWhileParsingString),
Expand Down

0 comments on commit 63e4524

Please sign in to comment.