AT-Parser-RS
A lightweight, no_std AT command parser library for embedded Rust applications.
Overview
AT-Parser-RS provides a flexible framework for implementing AT command interfaces in embedded systems. It supports the standard AT command syntax including execution, query, test, and set operations.
Features
no_stdcompatible - suitable for bare-metal and embedded environments- Fixed-size response buffers via
Bytes<SIZE>— no heap allocation - Support for all AT command forms:
AT+CMD- Execute commandAT+CMD?- Query current valueAT+CMD=?- Test supported valuesAT+CMD=<args>- Set new value(s)
- Type-safe command registration via traits
- Static command definitions (suitable for embedded/RTOS)
Feature Flags
The library supports the following optional features:
osal_rs- Enables integration with FreeRTOS through the osal-rs library for RTOS-based applications. Provides synchronization primitives likeMutexfor thread-safe command handling.enable_panic- Enables a custom panic handler forno_stdenvironments, providing a minimal panic implementation for embedded targets.
By default, no features are enabled, providing pure no_std compatibility without external dependencies.
# Build with FreeRTOS support
# Build with custom panic handler
# Build with both features
Command Forms
The parser supports four standard AT command forms:
| Form | Syntax | Purpose | Example |
|---|---|---|---|
| Execute | AT+CMD |
Execute an action | AT+RST |
| Query | AT+CMD? |
Get current setting | AT+ECHO? |
| Test | AT+CMD=? |
Get supported values | AT+ECHO=? |
| Set | AT+CMD=<args> |
Set new value(s) | AT+ECHO=1 |
Note: All commands must start with the
ATprefix (e.g.,AT+CMD, not just+CMD). The parser expects the full AT command syntax.
Core Types
AtContext<SIZE> Trait
The main trait for implementing command handlers. The const generic SIZE defines the response buffer size in bytes. Override only the methods your command needs:
All methods return Err(AtError::NotSupported) by default.
AtResult<SIZE> and AtError
pub type AtResult<const SIZE: usize> = ;
Bytes<SIZE>
Bytes<SIZE> is a fixed-size byte buffer from osal-rs (re-exported by this crate) used to return responses without heap allocation:
use Bytes;
// Create from a string slice (truncated to SIZE if longer)
let response = from_str;
AtParser<T, SIZE>
The parser is generic over both the handler type T and the response buffer size SIZE:
;
Args Structure
Provides access to comma-separated arguments:
Usage Examples
1. Define Command Modules
Implement the AtContext<SIZE> trait for your command handlers. Choose a buffer size that fits your largest response string:
use AtContext;
use ;
const SIZE: usize = 64;
/// Echo command - returns/sets echo state
/// Reset command - executes system reset
;
2. Create Module Instances
For standard applications, create instances on the stack:
let mut echo = EchoModule ;
let mut reset = ResetModule;
For embedded/no_std environments with static mut (single-threaded only):
static mut ECHO: EchoModule = EchoModule ;
static mut RESET: ResetModule = ResetModule;
Note:
static mutrequiresunsafeblocks and is only safe in single-threaded contexts. For RTOS or multi-threaded applications, use proper synchronization primitives.
3. Initialize Parser and Register Commands
use AtParser;
use AtContext;
const SIZE: usize = 64;
let mut parser: = new;
let commands: &mut = &mut ;
parser.set_commands;
4. Execute Commands
// Execute: show current state
match parser.execute
// Test: show valid values
match parser.execute
// Set: enable echo
match parser.execute
// Query: get current value
match parser.execute
// Execute reset
match parser.execute
// Unknown command
match parser.execute
Bytes<SIZE> implements Display, so it can be printed directly with {} or converted to a string via .to_string().
Advanced Example: UART Module
use ;
use AtContext;
const SIZE: usize = 64;
Usage:
parser.execute; // "AT+UART=<baudrate>,<data_bits> where..."
parser.execute; // "OK"
parser.execute; // "115200,8"
Parsing Arguments
The Args structure provides a simple interface for accessing comma-separated arguments:
Important: Args::get() uses 0-based indexing. For a command like AT+CMD=foo,bar,baz:
args.get(0)returnsSome("foo")args.get(1)returnsSome("bar")args.get(2)returnsSome("baz")args.get(3)returnsNone
For numeric arguments:
let value = args.get
.ok_or?
.
.map_err?;
Thread Safety
Single-threaded (bare-metal)
static mut MODULE: MyModule = new;
// Safe in single-threaded context
Multi-threaded (RTOS)
use RefCell;
use Mutex;
static MODULE: = new;
Using the at_modules! Macro
The library provides an at_modules! macro for defining static command arrays. The first argument is the SIZE const:
use at_modules;
use AtContext;
const SIZE: usize = 64;
static mut ECHO: EchoModule = EchoModule ;
static mut RESET: ResetModule = ResetModule;
at_modules!
Limitations and Considerations
⚠️ Important: This macro has significant limitations:
- Unsafe: The macro creates mutable references to static data, requiring
unsafeblocks - Single-threaded only: Not suitable for multi-threaded or RTOS environments
- Limited flexibility: Cannot mix different command handler types
Recommended Alternative
For most applications, the manual approach shown in the examples is preferred:
use AtContext;
use AtParser;
const SIZE: usize = 64;
let mut echo = EchoModule ;
let mut reset = ResetModule;
let commands: &mut = &mut ;
parser.set_commands;
This approach is safer, more flexible, and works in all contexts (stack, heap, RTOS).
Best Practices
- Choose an appropriate
SIZE: Pick a buffer size that fits your largest response string; responses longer thanSIZEare silently truncated - Validate arguments: Always check argument count and validity before processing
- Handle errors gracefully: Use appropriate
AtErrorvariants for different failure modes - Document test responses: Use
test()to provide clear usage information - Minimize state: Keep module state simple and thread-safe
Examples
The library includes several example files demonstrating different usage patterns:
Standard Examples
complete_usage.rs- Complete demonstration with multiple command types (Echo, Reset, Info, LED)basic_parser.rs- Shows direct usage of theAtParserwith comprehensive test cases
Embedded/no_std Examples
These examples demonstrate code patterns suitable for no_std environments:
embedded_basic.rs- Basic patterns and error handling for no_std/embedded environmentsembedded_error_handling.rs- Patterns for custom error handling and type conversionsembedded_uart_config.rs- UART and device configuration patterns withAtContextimplementation
Note: The embedded examples are designed to show code patterns and best practices rather than being fully functional standalone programs. They demonstrate how to structure code for embedded/no_std contexts.
Run examples with:
# Standard examples (fully functional)
# Embedded examples (demonstrate patterns)
License
This project is licensed under the GNU Lesser General Public License v2.1 or later (LGPL-2.1-or-later) - see the LICENSE file for details.