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- Zero-allocation parsing using string slices
- 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)
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 |
Core Types
AtContext Trait
The main trait for implementing command handlers. Override only the methods your command needs to support:
All methods return NotSupported by default.
AtResult and AtError
pub type AtResult<'a> = ;
Args Structure
Provides access to comma-separated arguments:
Usage Examples
1. Define Command Modules
Implement the AtContext trait for your command handlers:
use AtContext;
use ;
/// 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;
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
Advanced Example: UART Module
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:
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;
Best Practices
- Keep responses static: Return
&'static strwhen possible to avoid allocations - 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
embedded_basic.rs- Basic patterns and error handling for no_std/embedded environmentsembedded_error_handling.rs- Advanced patterns with custom error handling and macrosembedded_uart_config.rs- UART and device configuration with AtContext implementation
Run examples with:
# Standard examples
# Embedded examples (no_std)
License
This project is licensed under the same terms as the parent project.