embedded_uart_config/
embedded_uart_config.rs1#![allow(dead_code)]
28#![no_std]
29#![no_main]
30
31extern crate at_parser_rs;
32
33use at_parser_rs::context::AtContext;
34use at_parser_rs::parser::AtParser;
35use at_parser_rs::{Args, AtError, AtResult, at_response};
36
37const SIZE: usize = 64;
38
39struct UartSendModule {
41 baudrate: u32,
42}
43
44impl UartSendModule {
45 const fn new() -> Self {
46 Self { baudrate: 9600 }
47 }
48
49 fn write(&self, _data: &str) {
50 }
52}
53
54impl AtContext<SIZE> for UartSendModule {
55 fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
57 Ok(at_response!(SIZE, at_response; self.baudrate))
58 }
59
60 fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
62 Ok(at_response!(SIZE, at_response; "\"<data>\""))
63 }
64
65 fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
67 let data = args.get(0).ok_or((at_response, AtError::InvalidArgs))?;
68 self.write(data.as_ref());
69 Ok(at_response!(SIZE, at_response; "SENT"))
70 }
71}
72
73struct ConfigModule {
75 baudrate: u32,
76 mode: u8,
77}
78
79impl ConfigModule {
80 const fn new() -> Self {
81 Self { baudrate: 115200, mode: 0 }
82 }
83}
84
85impl AtContext<SIZE> for ConfigModule {
86 fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
88 Ok(at_response!(SIZE, at_response; self.baudrate, self.mode))
89 }
90
91 fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
93 Ok(at_response!(SIZE, at_response; "<baudrate: 9600-115200>,<mode: 0|1>"))
94 }
95
96 fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
98 let baudrate = args.get(0)
99 .ok_or((at_response, AtError::InvalidArgs))?
100 .parse::<u32>()
101 .map_err(|_| (at_response, AtError::InvalidArgs))?;
102
103 let mode = args.get(1)
104 .ok_or((at_response, AtError::InvalidArgs))?
105 .parse::<u8>()
106 .map_err(|_| (at_response, AtError::InvalidArgs))?;
107
108 if mode > 1 {
109 return Err((at_response, AtError::InvalidArgs));
110 }
111
112 self.baudrate = baudrate;
113 self.mode = mode;
114 Ok(at_response!(SIZE, at_response; "OK"))
117 }
118}
119
120#[unsafe(no_mangle)]
121pub extern "C" fn main() -> ! {
122 let mut uart = UartSendModule::new();
123 let mut config = ConfigModule::new();
124
125 let mut parser: AtParser<dyn AtContext<SIZE>, SIZE> = AtParser::new();
126 let commands: &mut [(&str, &str, &mut dyn AtContext<SIZE>)] = &mut [
127 ("AT+UARTSEND", "+UARTSEND: ", &mut uart),
128 ("AT+CFG", "+CFG: ", &mut config),
129 ];
130 parser.set_commands(commands);
131
132 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 {}
151}
152