complete_usage/
complete_usage.rs1#![no_std]
27#![no_main]
28#![allow(dead_code, unused_variables)]
29
30extern crate at_parser_rs;
31
32use at_parser_rs::context::AtContext;
33use at_parser_rs::{Args, AtError, AtResult, Bytes};
34
35const SIZE: usize = 64;
36
37pub struct EchoModule {
39 pub echo: bool,
40}
41
42impl AtContext<SIZE> for EchoModule {
43 fn exec(&self) -> AtResult<SIZE> {
45 if self.echo {
46 Ok(Bytes::from_str("ECHO: ON"))
47 } else {
48 Ok(Bytes::from_str("ECHO: OFF"))
49 }
50 }
51
52 fn query(&mut self) -> AtResult<SIZE> {
54 if self.echo {
55 Ok(Bytes::from_str("1"))
56 } else {
57 Ok(Bytes::from_str("0"))
58 }
59 }
60
61 fn test(&mut self) -> AtResult<SIZE> {
63 Ok(Bytes::from_str("Valid values: 0 (OFF), 1 (ON)"))
64 }
65
66 fn set(&mut self, args: Args) -> AtResult<SIZE> {
68 let value = args.get(0).ok_or(AtError::InvalidArgs)?;
69 match value {
70 "0" => {
71 self.echo = false;
72 Ok(Bytes::from_str("ECHO OFF"))
73 }
74 "1" => {
75 self.echo = true;
76 Ok(Bytes::from_str("ECHO ON"))
77 }
78 _ => Err(AtError::InvalidArgs),
79 }
80 }
81}
82
83pub struct ResetModule;
85
86impl AtContext<SIZE> for ResetModule {
87 fn exec(&self) -> AtResult<SIZE> {
89 Ok(Bytes::from_str("OK - System reset"))
90 }
91
92 fn test(&mut self) -> AtResult<SIZE> {
94 Ok(Bytes::from_str("Reset the system"))
95 }
96}
97
98pub struct InfoModule {
100 pub version: &'static str,
101}
102
103impl AtContext<SIZE> for InfoModule {
104 fn exec(&self) -> AtResult<SIZE> {
106 Ok(Bytes::from_str(self.version))
107 }
108
109 fn query(&mut self) -> AtResult<SIZE> {
111 Ok(Bytes::from_str("AT-Parser-RS v1.0.0 - AT Command Parser Library"))
112 }
113}
114
115pub struct LedModule {
117 pub state: bool,
118 pub brightness: u8,
119}
120
121impl AtContext<SIZE> for LedModule {
122 fn exec(&self) -> AtResult<SIZE> {
124 if self.state {
125 Ok(Bytes::from_str("LED: ON"))
126 } else {
127 Ok(Bytes::from_str("LED: OFF"))
128 }
129 }
130
131 fn query(&mut self) -> AtResult<SIZE> {
133 if self.state {
134 Ok(Bytes::from_str("1,100"))
135 } else {
136 Ok(Bytes::from_str("0,0"))
137 }
138 }
139
140 fn test(&mut self) -> AtResult<SIZE> {
142 Ok(Bytes::from_str("AT+LED=<state>,<brightness> where state: 0|1, brightness: 0-100"))
143 }
144
145 fn set(&mut self, args: Args) -> AtResult<SIZE> {
147 let state_str = args.get(0).ok_or(AtError::InvalidArgs)?;
148
149 self.state = match state_str {
150 "0" => false,
151 "1" => true,
152 _ => return Err(AtError::InvalidArgs),
153 };
154
155 if let Some(brightness_str) = args.get(1) {
157 self.brightness = brightness_str
158 .parse::<u8>()
159 .map_err(|_| AtError::InvalidArgs)?;
160
161 if self.brightness > 100 {
162 return Err(AtError::InvalidArgs);
163 }
164 }
165
166 if self.state {
167 Ok(Bytes::from_str("LED ON"))
168 } else {
169 Ok(Bytes::from_str("LED OFF"))
170 }
171 }
172}
173
174fn execute_command(cmd: &str, name: &str, module: &mut dyn AtContext<SIZE>) {
176 let result = if let Some(rest) = cmd.strip_prefix(name) {
177 if rest.is_empty() {
178 module.exec()
179 } else if rest == "?" {
180 module.query()
181 } else if rest == "=?" {
182 module.test()
183 } else if let Some(args_str) = rest.strip_prefix('=') {
184 module.set(Args { raw: args_str })
185 } else {
186 Err(AtError::InvalidArgs)
187 }
188 } else {
189 Err(AtError::UnknownCommand)
190 };
191 let _ = result;
192}
193
194#[unsafe(no_mangle)]
195pub extern "C" fn main() -> ! {
196 let mut echo = EchoModule { echo: false };
197 let mut reset = ResetModule;
198 let mut info = InfoModule { version: "v1.0.0" };
199 let mut led = LedModule { state: false, brightness: 0 };
200
201 execute_command("AT+INFO", "AT+INFO", &mut info);
203 execute_command("AT+INFO?", "AT+INFO", &mut info);
204
205 execute_command("AT+ECHO", "AT+ECHO", &mut echo);
207 execute_command("AT+ECHO=?", "AT+ECHO", &mut echo);
208 execute_command("AT+ECHO=1", "AT+ECHO", &mut echo);
209 execute_command("AT+ECHO?", "AT+ECHO", &mut echo);
210 execute_command("AT+ECHO=0", "AT+ECHO", &mut echo);
211
212 execute_command("AT+LED=?", "AT+LED", &mut led);
214 execute_command("AT+LED=1", "AT+LED", &mut led);
215 execute_command("AT+LED?", "AT+LED", &mut led);
216 execute_command("AT+LED=1,75","AT+LED", &mut led);
217 execute_command("AT+LED=0", "AT+LED", &mut led);
218
219 execute_command("AT+RST=?", "AT+RST", &mut reset);
221 execute_command("AT+RST", "AT+RST", &mut reset);
222
223 execute_command("AT+ECHO=2", "AT+ECHO", &mut echo); execute_command("AT+INFO=1", "AT+INFO", &mut info); loop {}
228}