Parse some more commands

This commit is contained in:
Sofia 2026-05-30 18:46:23 +03:00
parent e34121d5a6
commit 42904cc685

View File

@ -226,6 +226,7 @@ impl ATResponseParser {
self.curr_line = Some(text.to_string()); self.curr_line = Some(text.to_string());
Ok(()) Ok(())
} else { } else {
self.curr_line = Some(line);
Err(ATParseError::InvalidResponse) Err(ATParseError::InvalidResponse)
} }
} }
@ -300,10 +301,17 @@ impl ATCommand for ATInformationCommand {
} }
} }
#[derive(Debug)]
pub enum EnterPinResult {
Ok,
Error,
ErrorMessage(String),
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EnterPinCommand(pub String); pub struct EnterPinCommand(pub String);
impl ATCommand for EnterPinCommand { impl ATCommand for EnterPinCommand {
type Response = String; type Response = EnterPinResult;
fn execute(&self, at_commands: &mut ATCommands) -> Vec<String> { fn execute(&self, at_commands: &mut ATCommands) -> Vec<String> {
at_commands.raw_command(self, format!("AT+CPIN={}", self.0)) at_commands.raw_command(self, format!("AT+CPIN={}", self.0))
@ -312,17 +320,27 @@ impl ATCommand for EnterPinCommand {
fn parse_response(parser: &mut ATResponseParser) -> Result<Self::Response, ATParseError> { fn parse_response(parser: &mut ATResponseParser) -> Result<Self::Response, ATParseError> {
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(EnterPinResult::Ok);
} else if line.starts_with("ERROR") {
return Ok(EnterPinResult::Error);
} else if let Some(status) = line.strip_prefix("+CME ERROR: ") {
return Ok(EnterPinResult::ErrorMessage(status.to_string()));
} }
} }
Err(ATParseError::EOF) Err(ATParseError::EOF)
} }
} }
#[derive(Debug)]
pub enum CheckPinResult {
Ok,
Error,
ErrorMessage(String),
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CheckPinCommand; pub struct CheckPinCommand;
impl ATCommand for CheckPinCommand { impl ATCommand for CheckPinCommand {
type Response = String; type Response = CheckPinResult;
fn execute(&self, at_commands: &mut ATCommands) -> Vec<String> { fn execute(&self, at_commands: &mut ATCommands) -> Vec<String> {
at_commands.raw_command(self, "AT+CPIN?".to_string()) at_commands.raw_command(self, "AT+CPIN?".to_string())
@ -331,13 +349,23 @@ impl ATCommand for CheckPinCommand {
fn parse_response(parser: &mut ATResponseParser) -> Result<Self::Response, ATParseError> { fn parse_response(parser: &mut ATResponseParser) -> Result<Self::Response, ATParseError> {
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(CheckPinResult::Ok);
} else if line.starts_with("ERROR") {
return Ok(CheckPinResult::Error);
} else if let Some(status) = line.strip_prefix("+CME ERROR: ") {
return Ok(CheckPinResult::ErrorMessage(status.to_string()));
} }
} }
Err(ATParseError::EOF) Err(ATParseError::EOF)
} }
} }
#[derive(Debug)]
pub enum SelectSMSFormatResult {
Ok,
Error,
}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum SMSFormat { pub enum SMSFormat {
PDUMode = 0, PDUMode = 0,
@ -347,7 +375,7 @@ pub enum SMSFormat {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SelectSMSFormatCommand(pub SMSFormat); pub struct SelectSMSFormatCommand(pub SMSFormat);
impl ATCommand for SelectSMSFormatCommand { impl ATCommand for SelectSMSFormatCommand {
type Response = String; type Response = SelectSMSFormatResult;
fn execute(&self, at_commands: &mut ATCommands) -> Vec<String> { fn execute(&self, at_commands: &mut ATCommands) -> Vec<String> {
at_commands.raw_command(self, format!("AT+CMGF={}", self.0 as u8)) at_commands.raw_command(self, format!("AT+CMGF={}", self.0 as u8))
@ -356,7 +384,9 @@ impl ATCommand for SelectSMSFormatCommand {
fn parse_response(parser: &mut ATResponseParser) -> Result<Self::Response, ATParseError> { fn parse_response(parser: &mut ATResponseParser) -> Result<Self::Response, ATParseError> {
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(SelectSMSFormatResult::Ok);
} else if line.starts_with("ERROR") {
return Ok(SelectSMSFormatResult::Error);
} }
} }
Err(ATParseError::EOF) Err(ATParseError::EOF)