pub trait AtContext<const SIZE: usize> {
// Provided methods
fn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> { ... }
fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> { ... }
fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> { ... }
fn set(
&mut self,
at_response: &'static str,
_args: Args<'_>,
) -> AtResult<'_, SIZE> { ... }
}Expand description
Trait that defines the context for AT command execution.
Implement this trait for each AT command your device exposes.
Each method corresponds to one of the four standard AT command forms.
All methods have a default implementation that returns
AtError::NotSupported, so you only need to override the forms your
command actually needs.
The const generic SIZE defines the capacity (in bytes) of the
Bytes response buffer returned by each handler.
All handlers registered in the same AtParser
must use the same SIZE.
§Example — minimal handler
use at_parser_rs::context::AtContext;
use at_parser_rs::{AtResult, at_response};
const SIZE: usize = 64;
struct ResetModule;
impl AtContext<SIZE> for ResetModule {
fn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
// AT+RST performs a system reset
Ok(at_response!(SIZE, at_response; "OK"))
}
}§Example — full handler with all forms
use at_parser_rs::context::AtContext;
use at_parser_rs::{Args, AtResult, AtError, at_response};
const SIZE: usize = 64;
struct EchoModule { enabled: bool }
impl AtContext<SIZE> for EchoModule {
fn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; if self.enabled { "ON" } else { "OFF" }))
}
fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; if self.enabled { 1u8 } else { 0u8 }))
}
fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; "(0,1)"))
}
fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
let value = args.get(0).ok_or((at_response, AtError::InvalidArgs))?;
match value.as_ref() {
"0" => { self.enabled = false; Ok(at_response!(SIZE, at_response; "OK")) }
"1" => { self.enabled = true; Ok(at_response!(SIZE, at_response; "OK")) }
_ => Err((at_response, AtError::InvalidArgs)),
}
}
}Provided Methods§
Sourcefn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE>
fn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE>
Execute command (AT+CMD)
Called when the command is invoked without any suffix. Use this to implement an action that does not require parameters.
§Arguments
at_response— AT response prefix registered for this command (e.g."+RST: ")
§Returns
Ok((at_response, Bytes<SIZE>))— response to send back to the callerErr((at_response, AtError::NotSupported))— default when not overridden
§Example
struct PingModule;
impl AtContext<SIZE> for PingModule {
fn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; "PONG"))
}
}
// AT+PING → Ok(("+PING: ", "PONG"))Sourcefn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE>
fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE>
Query command (AT+CMD?)
Called to retrieve the current value or state of the command.
§Arguments
at_response— AT response prefix registered for this command
§Returns
Ok((at_response, Bytes<SIZE>))— current value/stateErr((at_response, AtError::NotSupported))— default when not overridden
§Example
struct VolumeModule { level: u8 }
impl AtContext<SIZE> for VolumeModule {
fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; self.level))
}
}
// AT+VOL? → Ok(("+VOL: ", "75")) (if level == 75)Sourcefn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE>
fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE>
Test command (AT+CMD=?)
Called to report whether a command is supported or to return the
valid parameter ranges accepted by set.
§Arguments
at_response— AT response prefix registered for this command
§Returns
Ok((at_response, Bytes<SIZE>))— human-readable description of valid parametersErr((at_response, AtError::NotSupported))— default when not overridden
§Example
struct VolumeModule { level: u8 }
impl AtContext<SIZE> for VolumeModule {
fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
Ok(at_response!(SIZE, at_response; "(0-100)"))
}
}
// AT+VOL=? → Ok(("+VOL: ", "(0-100)"))Sourcefn set(
&mut self,
at_response: &'static str,
_args: Args<'_>,
) -> AtResult<'_, SIZE>
fn set( &mut self, at_response: &'static str, _args: Args<'_>, ) -> AtResult<'_, SIZE>
Set command (AT+CMD=<args>)
Called to configure the command with one or more parameters.
Arguments are accessible via Args::get using a
0-based comma-separated index. Quoted arguments are unquoted and
escape sequences such as \" are decoded automatically.
§Arguments
at_response— AT response prefix registered for this commandargs— parsed argument list; useargs.get(n)to retrieve the n-th comma-separated token (0-indexed)
§Returns
Ok((at_response, Bytes<SIZE>))— confirmation/responseErr((at_response, AtError::InvalidArgs))— when arguments are missing or invalidErr((at_response, AtError::NotSupported))— default when not overridden
§Example
struct VolumeModule { level: u8 }
impl AtContext<SIZE> for VolumeModule {
fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
let val: u8 = args.get(0)
.ok_or((at_response, AtError::InvalidArgs))?
.parse()
.map_err(|_| (at_response, AtError::InvalidArgs))?;
if val > 100 { return Err((at_response, AtError::InvalidArgs)); }
self.level = val;
Ok(at_response!(SIZE, at_response; "OK"))
}
}
// AT+VOL=75 → Ok(("+VOL: ", "OK"))
// AT+VOL=200 → Err(("+VOL: ", InvalidArgs))
// AT+VOL= → Err(("+VOL: ", InvalidArgs))§use at_parser_rs::context::AtContext;
§use at_parser_rs::{Args, AtResult, AtError};
§use osal_rs::utils::Bytes;
§const SIZE: usize = 64;
struct VolumeModule { level: u8 }
impl AtContext