embedded_uart_config/embedded_uart_config.rs
1/***************************************************************************
2 *
3 * AT Command Parser
4 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
18 *
19 ***************************************************************************/
20
21//! Example: AT command table with UART and device configuration handling
22//! This example demonstrates code patterns suitable for no_std/embedded environments
23
24#![allow(dead_code)]
25
26extern crate at_parser_rs;
27
28use at_parser_rs::{Args, AtError, AtResult};
29use at_parser_rs::context::AtContext;
30
31// UART struct with AtContext implementation for UARTSEND command
32struct DummyUart {
33 baudrate: u32,
34 mode: u8,
35}
36
37impl DummyUart {
38 const fn new() -> Self {
39 Self { baudrate: 9600, mode: 0 }
40 }
41
42 fn write(&self, data: &str) {
43 // In real embedded, this would send data over UART
44 let _ = data;
45 }
46}
47
48impl AtContext for DummyUart {
49 fn exec(&self) -> AtResult<'static> {
50 // Not supported for UARTSEND
51 Err(AtError::NotSupported)
52 }
53 fn set(&mut self, args: Args) -> AtResult<'static> {
54 if let Some(data) = args.get(0) {
55 self.write(data);
56 Ok("SENT")
57 } else {
58 Err(AtError::InvalidArgs)
59 }
60 }
61}
62
63// Device configuration context
64struct ConfigContext;
65
66impl AtContext for ConfigContext {
67 fn exec(&self) -> AtResult<'static> {
68 // Not supported for SETCFG
69 Err(AtError::NotSupported)
70 }
71
72 fn query(&mut self) -> AtResult<'static> {
73 // Return current configuration as a static string
74 // In real implementation, format the values dynamically
75 Ok("115200,1")
76 }
77
78 fn test(&mut self) -> AtResult<'static> {
79 // Return supported configuration options
80 Ok("+SETCFG: (baudrate),(mode)")
81 }
82
83 fn set(&mut self, args: Args) -> AtResult<'static> {
84 let baud = args.get(0).and_then(|s| s.parse::<u32>().ok());
85 let mode = args.get(1).and_then(|s| s.parse::<u8>().ok());
86 match (baud, mode) {
87 (Some(b), Some(m)) => unsafe {
88 // Configure UART
89 UART.baudrate = b;
90 UART.mode = m;
91 Ok("CONFIGURED")
92 },
93 _ => Err(AtError::InvalidArgs),
94 }
95 }
96}
97
98static mut UART: DummyUart = DummyUart { baudrate: 9600, mode: 0 };
99static mut CONFIG_CTX: ConfigContext = ConfigContext;
100
101// Example using AtParser
102fn example_with_parser() -> Result<&'static str, AtError> {
103 // Simplified example for no_std
104 // In real embedded code, you would properly initialize the parser
105 Ok("OK")
106}
107
108// Example using the COMMANDS table generated by at_modules macro
109fn example_with_commands_macro() -> Result<&'static str, AtError> {
110 // Simple command parsing without the full parser
111 // In a real embedded system, you would use the parser properly
112 Ok("OK")
113}
114
115// Mock main for compilation (in real embedded code, this would be in your firmware)
116fn main() {
117 // Example usage - in embedded this would be called from your main loop
118 let _result = example_with_commands_macro();
119}