Skip to content

Commit

Permalink
Merge pull request #14 from atlas-aero/urc_echo_consuming
Browse files Browse the repository at this point in the history
Removed URC echo matching
  • Loading branch information
marius-meissner committed Apr 19, 2023
2 parents 465437c + cc1575c commit 96209a5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 53 deletions.
20 changes: 18 additions & 2 deletions src/commands.rs
Expand Up @@ -107,8 +107,7 @@ impl CommandErrorHandler for AccessPointConnectCommand {
}

/// Command for receiving local address information including IP and MAC
#[derive(Clone, AtatCmd)]
#[at_cmd("+CIFSR", Vec<LocalAddressResponse, 4>, timeout_ms = 5_000)]
#[derive(Clone)]
pub struct ObtainLocalAddressCommand {}

impl ObtainLocalAddressCommand {
Expand All @@ -117,6 +116,23 @@ impl ObtainLocalAddressCommand {
}
}

impl AtatCmd<10> for ObtainLocalAddressCommand {
type Response = Vec<LocalAddressResponse, 4>;
const MAX_TIMEOUT_MS: u32 = 5_000;

fn as_bytes(&self) -> Vec<u8, 10> {
Vec::from_slice("AT+CIFSR\r\n".as_bytes()).unwrap()
}

fn parse(&self, resp: Result<&[u8], InternalError>) -> Result<Self::Response, AtError> {
if resp.is_err() {
return Err(AtError::InvalidResponse);
}

atat::serde_at::from_slice::<Vec<LocalAddressResponse, 4>>(resp.unwrap()).map_err(|_| AtError::Parse)
}
}

impl CommandErrorHandler for ObtainLocalAddressCommand {
type Error = AddressErrors;
const WOULD_BLOCK_ERROR: Self::Error = AddressErrors::UnexpectedWouldBlock;
Expand Down
42 changes: 1 addition & 41 deletions src/tests/urc.rs
@@ -1,12 +1,5 @@
use crate::commands::{
AccessPointConnectCommand, ConnectCommand, ObtainLocalAddressCommand, SetMultipleConnectionsCommand,
SetSocketReceivingModeCommand, TransmissionPrepareCommand, WifiModeCommand,
};
use crate::urc::URCMessages;
use atat::heapless::String;
use atat::{AtatCmd, AtatUrc, Parser};
use core::str::FromStr;
use embedded_nal::SocketAddrV4;
use atat::{AtatUrc, Parser};
use heapless::Vec;

#[test]
Expand Down Expand Up @@ -307,23 +300,6 @@ fn test_second_parse_longer_then_block_size() {
assert!(<URCMessages<4> as AtatUrc>::parse(b"+CIPRECVDATA,5:abcde").is_none())
}

#[test]
fn test_matching_cmd_echo() {
assert_cmd_echo_matching(WifiModeCommand::station_mode());
assert_cmd_echo_matching(SetSocketReceivingModeCommand::passive_mode());
assert_cmd_echo_matching(SetMultipleConnectionsCommand::multiple());
assert_cmd_echo_matching(AccessPointConnectCommand::new(
String::from_str("test_network").unwrap(),
String::from_str("secret").unwrap(),
));
assert_cmd_echo_matching(TransmissionPrepareCommand::new(0, 8));
assert_cmd_echo_matching(ConnectCommand::tcp_v4(
0,
SocketAddrV4::from_str("10.0.0.1:5000").unwrap(),
));
assert_cmd_echo_matching(ObtainLocalAddressCommand::new());
}

fn assert_result(string: &[u8], size: usize, data: &[u8]) {
match <URCMessages<32> as Parser>::parse(data) {
Ok(result) => {
Expand All @@ -335,19 +311,3 @@ fn assert_result(string: &[u8], size: usize, data: &[u8]) {
}
}
}

/// Asserts that command echo is matched
fn assert_cmd_echo_matching<Cmd: AtatCmd<LEN>, const LEN: usize>(command: Cmd) {
let encoded = command.as_bytes();

// Assert that first parser ist matching
assert_result(encoded.as_slice(), encoded.len(), encoded.as_slice());

// Assert that echo gets converted to Unknown URC
assert_eq!(
URCMessages::Echo,
<URCMessages<32> as AtatUrc>::parse(encoded.as_slice()).unwrap(),
"Echo of command {} did not return URCMessages::Echo on second parser.",
core::str::from_utf8(encoded.as_slice()).unwrap()
);
}
8 changes: 0 additions & 8 deletions src/urc.rs
Expand Up @@ -34,8 +34,6 @@ pub enum URCMessages<const RX_SIZE: usize> {
DataAvailable(usize, usize),
/// Received the following data requested by CIPRECVDATA command.
Data(Vec<u8, RX_SIZE>),
/// Echo of a command
Echo,
/// Unknown URC message
Unknown,
}
Expand All @@ -44,11 +42,6 @@ impl<const RX_SIZE: usize> AtatUrc for URCMessages<RX_SIZE> {
type Response = Self;

fn parse(resp: &[u8]) -> Option<Self::Response> {
// Command echo
if &resp[..3] == b"AT+" {
return Some(Self::Echo);
}

if &resp[..4] == b"+IPD" {
return URCMessages::parse_data_available(resp);
}
Expand Down Expand Up @@ -220,7 +213,6 @@ impl<'a> LineBasedMatcher<'a> {
/// True if a regular CRLF terminated URC message was matched
fn matches_lines_based_urc(&self, line: &str) -> bool {
line == "ready"
|| &line[..3] == "AT+"
|| &line[..4] == "+IPD"
|| line == "SEND OK"
|| line == "SEND FAIL"
Expand Down
3 changes: 1 addition & 2 deletions src/wifi.rs
Expand Up @@ -154,7 +154,6 @@ impl<const RX_SIZE: usize> Session<RX_SIZE> {
}
}
URCMessages::Data(data) => self.data = Some(data),
URCMessages::Echo => {}
URCMessages::Unknown => {}
}
}
Expand Down Expand Up @@ -260,7 +259,7 @@ impl<A: AtatClient, T: Timer<TIMER_HZ>, const TIMER_HZ: u32, const TX_SIZE: usiz

/// Returns local address information
fn get_address(&mut self) -> Result<LocalAddress, AddressErrors> {
let responses = self.send_command(ObtainLocalAddressCommand::new())?;
let responses = self.send_command::<_, 10>(ObtainLocalAddressCommand::new())?;
LocalAddress::from_responses(responses)
}

Expand Down

0 comments on commit 96209a5

Please sign in to comment.