Parse ATInformation
This commit is contained in:
parent
e21002744a
commit
e34121d5a6
@ -134,7 +134,7 @@ impl<'a, 'd> ATCommands<'a, 'd> {
|
|||||||
if i == 0 {
|
if i == 0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
response.push(line);
|
response.push(line.trim().to_string());
|
||||||
|
|
||||||
let mut parser = ATResponseParser::from(response.clone());
|
let mut parser = ATResponseParser::from(response.clone());
|
||||||
|
|
||||||
@ -150,13 +150,10 @@ impl<'a, 'd> ATCommands<'a, 'd> {
|
|||||||
}
|
}
|
||||||
return Some(output_lines);
|
return Some(output_lines);
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => match err {
|
||||||
log::info!("{:?}", err);
|
ATParseError::EOF => {}
|
||||||
match err {
|
ATParseError::InvalidResponse => panic!("Invalid response"),
|
||||||
ATParseError::EOF => {}
|
},
|
||||||
ATParseError::InvalidResponse => panic!("Invalid response"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -190,6 +187,7 @@ impl<'a, 'd> ATCommands<'a, 'd> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct ATResponseParser {
|
pub struct ATResponseParser {
|
||||||
|
curr_line: Option<String>,
|
||||||
text: Vec<String>,
|
text: Vec<String>,
|
||||||
index: usize,
|
index: usize,
|
||||||
lines: usize,
|
lines: usize,
|
||||||
@ -198,19 +196,38 @@ pub struct ATResponseParser {
|
|||||||
impl ATResponseParser {
|
impl ATResponseParser {
|
||||||
pub fn from(text: Vec<String>) -> ATResponseParser {
|
pub fn from(text: Vec<String>) -> ATResponseParser {
|
||||||
ATResponseParser {
|
ATResponseParser {
|
||||||
|
curr_line: None,
|
||||||
text,
|
text,
|
||||||
index: 0,
|
index: 0,
|
||||||
lines: 0,
|
lines: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn readline<'s>(&'s mut self) -> Option<&'s String> {
|
pub fn readline(&mut self) -> Option<String> {
|
||||||
|
if let Some(curr_line) = self.curr_line.take() {
|
||||||
|
return Some(curr_line);
|
||||||
|
}
|
||||||
let result = self.text.get(self.index);
|
let result = self.text.get(self.index);
|
||||||
self.index += 1;
|
self.index += 1;
|
||||||
if let Some(_) = result {
|
if let Some(_) = result {
|
||||||
self.lines += 1;
|
self.lines += 1;
|
||||||
}
|
}
|
||||||
result
|
result.cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn expect(&mut self, text: String) -> Result<(), ATParseError> {
|
||||||
|
let line = if let Some(curr_line) = self.curr_line.take() {
|
||||||
|
curr_line
|
||||||
|
} else {
|
||||||
|
self.readline().ok_or(ATParseError::EOF)?
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(text) = line.strip_prefix(&text) {
|
||||||
|
self.curr_line = Some(text.to_string());
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(ATParseError::InvalidResponse)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,19 +259,41 @@ impl<T: ATCommand> SimpleATCommand for T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ATInformation {
|
||||||
|
manufacturer: String,
|
||||||
|
model: String,
|
||||||
|
revision: String,
|
||||||
|
imei: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ATInformationCommand;
|
pub struct ATInformationCommand;
|
||||||
impl ATCommand for ATInformationCommand {
|
impl ATCommand for ATInformationCommand {
|
||||||
type Response = String;
|
type Response = ATInformation;
|
||||||
|
|
||||||
fn execute(&self, at_commands: &mut ATCommands) -> Vec<String> {
|
fn execute(&self, at_commands: &mut ATCommands) -> Vec<String> {
|
||||||
at_commands.raw_command(self, "ATI".to_string())
|
at_commands.raw_command(self, "ATI".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_response(parser: &mut ATResponseParser) -> Result<Self::Response, ATParseError> {
|
fn parse_response(parser: &mut ATResponseParser) -> Result<Self::Response, ATParseError> {
|
||||||
|
parser.expect("Manufacturer: ".to_string())?;
|
||||||
|
let manufacturer = parser.readline().unwrap();
|
||||||
|
parser.expect("Model: ".to_string())?;
|
||||||
|
let model = parser.readline().unwrap();
|
||||||
|
parser.expect("Revision: ".to_string())?;
|
||||||
|
let revision = parser.readline().unwrap();
|
||||||
|
parser.expect("IMEI: ".to_string())?;
|
||||||
|
let imei = parser.readline().unwrap();
|
||||||
|
|
||||||
while let Some(line) = parser.readline() {
|
while let Some(line) = parser.readline() {
|
||||||
if line.starts_with("OK") {
|
if line.starts_with("OK") {
|
||||||
return Ok("OK".to_owned());
|
return Ok(ATInformation {
|
||||||
|
manufacturer,
|
||||||
|
model,
|
||||||
|
revision,
|
||||||
|
imei,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(ATParseError::EOF)
|
Err(ATParseError::EOF)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user