embedded_uart_config/
embedded_uart_config.rs

1//! Example: AT command table with UART and device configuration handling
2//! This example demonstrates code patterns suitable for no_std/embedded environments
3
4#![allow(dead_code)]
5
6extern crate at_parser_rs;
7
8use at_parser_rs::{Args, AtError, AtResult};
9use at_parser_rs::context::AtContext;
10
11// UART struct with AtContext implementation for UARTSEND command
12struct DummyUart {
13    baudrate: u32,
14    mode: u8,
15}
16
17impl DummyUart {
18    const fn new() -> Self {
19        Self { baudrate: 9600, mode: 0 }
20    }
21    
22    fn write(&self, data: &str) {
23        // In real embedded, this would send data over UART
24        let _ = data;
25    }
26}
27
28impl AtContext for DummyUart {
29    fn exec(&self) -> AtResult<'static> {
30        // Not supported for UARTSEND
31        Err(AtError::NotSupported)
32    }
33    fn set(&mut self, args: Args) -> AtResult<'static> {
34        if let Some(data) = args.get(0) {
35            self.write(data);
36            Ok("SENT")
37        } else {
38            Err(AtError::InvalidArgs)
39        }
40    }
41}
42
43// Device configuration context
44struct ConfigContext;
45
46impl AtContext for ConfigContext {
47    fn exec(&self) -> AtResult<'static> {
48        // Not supported for SETCFG
49        Err(AtError::NotSupported)
50    }
51    
52    fn query(&mut self) -> AtResult<'static> {
53        // Return current configuration as a static string
54        // In real implementation, format the values dynamically
55        Ok("115200,1")
56    }
57    
58    fn test(&mut self) -> AtResult<'static> {
59        // Return supported configuration options
60        Ok("+SETCFG: (baudrate),(mode)")
61    }
62    
63    fn set(&mut self, args: Args) -> AtResult<'static> {
64        let baud = args.get(0).and_then(|s| s.parse::<u32>().ok());
65        let mode = args.get(1).and_then(|s| s.parse::<u8>().ok());
66        match (baud, mode) {
67            (Some(b), Some(m)) => unsafe {
68                // Configure UART
69                UART.baudrate = b;
70                UART.mode = m;
71                Ok("CONFIGURED")
72            },
73            _ => Err(AtError::InvalidArgs),
74        }
75    }
76}
77
78static mut UART: DummyUart = DummyUart { baudrate: 9600, mode: 0 };
79static mut CONFIG_CTX: ConfigContext = ConfigContext;
80
81// Example using AtParser
82fn example_with_parser() -> Result<&'static str, AtError> {
83    // Simplified example for no_std
84    // In real embedded code, you would properly initialize the parser
85    Ok("OK")
86}
87
88// Example using the COMMANDS table generated by at_modules macro
89fn example_with_commands_macro() -> Result<&'static str, AtError> {
90    // Simple command parsing without the full parser
91    // In a real embedded system, you would use the parser properly
92    Ok("OK")
93}
94
95// Mock main for compilation (in real embedded code, this would be in your firmware)
96fn main() {
97    // Example usage - in embedded this would be called from your main loop
98    let _result = example_with_commands_macro();
99}