#![allow(dead_code)]
#![no_std]
#![no_main]
extern crate at_parser_rs;
use at_parser_rs::context::AtContext;
use at_parser_rs::parser::AtParser;
use at_parser_rs::{Args, AtError, AtResult, at_response};
const SIZE: usize = 64;
struct DiagModule {
armed: bool,
}
impl AtContext<SIZE> for DiagModule {
fn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
if self.armed {
Ok(at_response!(SIZE, at_response; "TRIGGERED"))
} else {
Err((at_response, AtError::Unhandled("not armed")))
}
}
fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; if self.armed { 1u8 } else { 0u8 }))
}
fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; "(0,1)"))
}
fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
let val = args.get(0).ok_or((at_response, AtError::InvalidArgs))?;
match val.as_ref() {
"0" => { self.armed = false; Ok(at_response!(SIZE, at_response; "OK")) }
"1" => { self.armed = true; Ok(at_response!(SIZE, at_response; "OK")) }
_ => Err((at_response, AtError::InvalidArgs)),
}
}
}
fn check_result(result: AtResult<SIZE>) -> u8 {
match result {
Ok((_, _)) => 0, Err((_, AtError::InvalidArgs)) => 1,
Err((_, AtError::NotSupported)) => 2,
Err((_, AtError::UnknownCommand)) => 3,
Err((_, AtError::Unhandled(_))) => 4,
Err((_, AtError::UnhandledOwned(_))) => 5,
}
}
#[unsafe(no_mangle)]
pub extern "C" fn main() -> ! {
let mut diag = DiagModule { armed: false };
let mut parser: AtParser<DiagModule, SIZE> = AtParser::new();
let commands: &mut [(&str, &str, &mut DiagModule)] = &mut [
("AT+DIAG", "+DIAG: ", &mut diag),
];
parser.set_commands(commands);
let _s = check_result(parser.execute("AT+DIAG"));
let _s = check_result(parser.execute("AT+DIAG=1"));
let _s = check_result(parser.execute("AT+DIAG"));
let _s = check_result(parser.execute("AT+DIAG?"));
let _s = check_result(parser.execute("AT+DIAG=2"));
let _s = check_result(parser.execute("AT+UNKNOWN"));
loop {}
}