at_parser_rs/lib.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//! AT Command Parser Library
22//!
23//! This library provides a flexible parser for AT commands, commonly used in
24//! embedded systems and communication devices. It supports no_std environments.
25
26#![cfg_attr(any(feature = "enable_panic", feature = "osal_rs"), no_std)]
27
28#[cfg(any(feature = "enable_panic", feature = "osal_rs"))]
29extern crate alloc;
30
31#[cfg(feature = "osal_rs")]
32extern crate osal_rs;
33
34#[cfg(any(feature = "enable_panic", feature = "osal_rs"))]
35use core::prelude::rust_2021::*;
36use core::iter::Iterator;
37use core::option::Option;
38use core::result::Result;
39
40#[cfg(feature = "enable_panic")]
41#[panic_handler]
42fn panic(_info: &core::panic::PanicInfo) -> ! {
43 loop {}
44}
45
46pub mod context;
47pub mod parser;
48
49
50/// Error types that can occur during AT command processing
51#[derive(Debug)]
52pub enum AtError {
53 /// The command is not recognized
54 UnknownCommand,
55 /// The command is recognized but not supported
56 NotSupported,
57 /// The command arguments are invalid
58 InvalidArgs,
59}
60
61/// Result type for AT command operations
62/// Returns either a static string response or an AtError
63pub type AtResult<'a> = Result<&'a str, AtError>;
64
65/// Structure holding the arguments passed to an AT command
66pub struct Args<'a> {
67 /// Raw argument string (comma-separated values)
68 pub raw: &'a str,
69}
70
71impl<'a> Args<'a> {
72 /// Get an argument by index (0-based)
73 /// Arguments are separated by commas
74 pub fn get(&self, index: usize) -> Option<&'a str> {
75 self.raw.split(',').nth(index)
76 }
77}
78
79
80/// Macro to define AT command modules
81/// Creates a static array of command names and their associated context handlers
82#[macro_export]
83macro_rules! at_modules {
84 (
85 $( $name:expr => $module:ident ),* $(,)?
86 ) => {
87 static COMMANDS: &[(&'static str, &mut dyn AtContext)] = unsafe {
88 &[
89 $(
90 ($name, &mut $module),
91 )*
92 ]
93 };
94 };
95}