Skip to content

Commit

Permalink
Removed URC echo matching
Browse files Browse the repository at this point in the history
  • Loading branch information
marius-meissner committed Apr 7, 2023
1 parent 465437c commit b91a27c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 61 deletions.
42 changes: 1 addition & 41 deletions src/tests/urc.rs
Original file line number Diff line number Diff line change
@@ -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()
);
}
10 changes: 2 additions & 8 deletions src/urc.rs
Original file line number Diff line number Diff line change
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 @@ -219,8 +212,9 @@ impl<'a> LineBasedMatcher<'a> {

/// True if a regular CRLF terminated URC message was matched
fn matches_lines_based_urc(&self, line: &str) -> bool {
// info!("URC: {line}");

line == "ready"
|| &line[..3] == "AT+"
|| &line[..4] == "+IPD"
|| line == "SEND OK"
|| line == "SEND FAIL"
Expand Down
35 changes: 23 additions & 12 deletions src/wifi.rs
Original file line number Diff line number Diff line change
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 @@ -192,6 +191,9 @@ pub enum AddressErrors {
/// Error while parsing addresses
AddressParseError,

/// Address was not encoded as valid UTF-8
InvalidEncoding,

/// Received an unexpected WouldBlock. The most common cause of errors is an incorrect mode of the client.
/// This must be either timeout or blocking.
UnexpectedWouldBlock,
Expand Down Expand Up @@ -386,28 +388,37 @@ impl LocalAddress {
let mut data = Self::default();

for response in responses {
// let address = match core::str::from_utf8(response.address.as_slice()) {
// Ok(decoded) => {
// if decoded.len() < 3 {
// ""
// } else {
// let end = decoded.len() - 1;
// &decoded[1..end]
// }
// },
// Err(_) => return Err(AddressErrors::InvalidEncoding)
// };

let address = response.address.as_str();

match response.address_type.as_slice() {
b"STAIP" => {
data.ipv4 = Some(
Ipv4Addr::from_str(response.address.as_str()).map_err(|_| AddressErrors::AddressParseError)?,
)
data.ipv4 = Some(Ipv4Addr::from_str(address).map_err(|_| AddressErrors::AddressParseError)?)
}
b"STAIP6LL" => {
data.ipv6_link_local = Some(
Ipv6Addr::from_str(response.address.as_str()).map_err(|_| AddressErrors::AddressParseError)?,
)
data.ipv6_link_local =
Some(Ipv6Addr::from_str(address).map_err(|_| AddressErrors::AddressParseError)?)
}
b"STAIP6GL" => {
data.ipv6_global = Some(
Ipv6Addr::from_str(response.address.as_str()).map_err(|_| AddressErrors::AddressParseError)?,
)
data.ipv6_global = Some(Ipv6Addr::from_str(address).map_err(|_| AddressErrors::AddressParseError)?)
}
b"STAMAC" => {
if response.address.len() > 17 {
if address.len() > 17 {
return Err(AddressErrors::AddressParseError);
}

data.mac = Some(String::from(response.address.as_str()));
data.mac = Some(String::from(address));
}
&_ => {}
}
Expand Down

0 comments on commit b91a27c

Please sign in to comment.