#![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 UartSendModule {
baudrate: u32,
}
impl UartSendModule {
const fn new() -> Self {
Self { baudrate: 9600 }
}
fn write(&self, _data: &str) {
}
}
impl AtContext<SIZE> for UartSendModule {
fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; self.baudrate))
}
fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; "\"<data>\""))
}
fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
let data = args.get(0).ok_or((at_response, AtError::InvalidArgs))?;
self.write(data.as_ref());
Ok(at_response!(SIZE, at_response; "SENT"))
}
}
struct ConfigModule {
baudrate: u32,
mode: u8,
}
impl ConfigModule {
const fn new() -> Self {
Self { baudrate: 115200, mode: 0 }
}
}
impl AtContext<SIZE> for ConfigModule {
fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; self.baudrate, self.mode))
}
fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; "<baudrate: 9600-115200>,<mode: 0|1>"))
}
fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
let baudrate = args.get(0)
.ok_or((at_response, AtError::InvalidArgs))?
.parse::<u32>()
.map_err(|_| (at_response, AtError::InvalidArgs))?;
let mode = args.get(1)
.ok_or((at_response, AtError::InvalidArgs))?
.parse::<u8>()
.map_err(|_| (at_response, AtError::InvalidArgs))?;
if mode > 1 {
return Err((at_response, AtError::InvalidArgs));
}
self.baudrate = baudrate;
self.mode = mode;
Ok(at_response!(SIZE, at_response; "OK"))
}
}
#[unsafe(no_mangle)]
pub extern "C" fn main() -> ! {
let mut uart = UartSendModule::new();
let mut config = ConfigModule::new();
let mut parser: AtParser<dyn AtContext<SIZE>, SIZE> = AtParser::new();
let commands: &mut [(&str, &str, &mut dyn AtContext<SIZE>)] = &mut [
("AT+UARTSEND", "+UARTSEND: ", &mut uart),
("AT+CFG", "+CFG: ", &mut config),
];
parser.set_commands(commands);
let _ = parser.execute("AT+UARTSEND=?"); let _ = parser.execute("AT+CFG=?");
let _ = parser.execute("AT+CFG?");
let _ = parser.execute("AT+UARTSEND=\"hello\"");
let _ = parser.execute("AT+CFG=9600,1"); let _ = parser.execute("AT+CFG?");
let _ = parser.execute("AT+CFG=9600,5"); let _ = parser.execute("AT+CFG=abc,0");
loop {}
}