AtContext

Trait AtContext 

Source
pub trait AtContext {
    // Provided methods
    fn exec(&self) -> AtResult<'static> { ... }
    fn query(&mut self) -> AtResult<'static> { ... }
    fn test(&mut self) -> AtResult<'static> { ... }
    fn set(&mut self, _args: Args<'_>) -> AtResult<'static> { ... }
}
Expand description

Trait that defines the context for AT command execution. Implementations of this trait handle the actual logic for each AT command form.

Provided Methods§

Source

fn exec(&self) -> AtResult<'static>

Execute command (AT+CMD) This is called when a command is invoked without any suffix.

Examples found in repository?
examples/complete_usage.rs (line 151)
145fn execute_command(cmd: &str, name: &str, module: &mut dyn AtContext) {
146    println!("\n> {}", cmd);
147    
148    let result = if let Some(rest) = cmd.strip_prefix(name) {
149        if rest.is_empty() {
150            // Execute form: AT+CMD
151            module.exec()
152        } else if rest == "?" {
153            // Query form: AT+CMD?
154            module.query()
155        } else if rest == "=?" {
156            // Test form: AT+CMD=?
157            module.test()
158        } else if let Some(args_str) = rest.strip_prefix('=') {
159            // Set form: AT+CMD=args
160            module.set(Args { raw: args_str })
161        } else {
162            Err(AtError::InvalidArgs)
163        }
164    } else {
165        Err(AtError::UnknownCommand)
166    };
167    
168    match result {
169        Ok(response) => println!("  Response: {}", response),
170        Err(AtError::UnknownCommand) => println!("  Error: Unknown command"),
171        Err(AtError::NotSupported) => println!("  Error: Operation not supported"),
172        Err(AtError::InvalidArgs) => println!("  Error: Invalid arguments"),
173    }
174}
Source

fn query(&mut self) -> AtResult<'static>

Query command (AT+CMD?) This is called to retrieve the current value/state of a command.

Examples found in repository?
examples/complete_usage.rs (line 154)
145fn execute_command(cmd: &str, name: &str, module: &mut dyn AtContext) {
146    println!("\n> {}", cmd);
147    
148    let result = if let Some(rest) = cmd.strip_prefix(name) {
149        if rest.is_empty() {
150            // Execute form: AT+CMD
151            module.exec()
152        } else if rest == "?" {
153            // Query form: AT+CMD?
154            module.query()
155        } else if rest == "=?" {
156            // Test form: AT+CMD=?
157            module.test()
158        } else if let Some(args_str) = rest.strip_prefix('=') {
159            // Set form: AT+CMD=args
160            module.set(Args { raw: args_str })
161        } else {
162            Err(AtError::InvalidArgs)
163        }
164    } else {
165        Err(AtError::UnknownCommand)
166    };
167    
168    match result {
169        Ok(response) => println!("  Response: {}", response),
170        Err(AtError::UnknownCommand) => println!("  Error: Unknown command"),
171        Err(AtError::NotSupported) => println!("  Error: Operation not supported"),
172        Err(AtError::InvalidArgs) => println!("  Error: Invalid arguments"),
173    }
174}
Source

fn test(&mut self) -> AtResult<'static>

Test command (AT+CMD=?) This is called to check if a command is supported or to get valid parameter ranges.

Examples found in repository?
examples/complete_usage.rs (line 157)
145fn execute_command(cmd: &str, name: &str, module: &mut dyn AtContext) {
146    println!("\n> {}", cmd);
147    
148    let result = if let Some(rest) = cmd.strip_prefix(name) {
149        if rest.is_empty() {
150            // Execute form: AT+CMD
151            module.exec()
152        } else if rest == "?" {
153            // Query form: AT+CMD?
154            module.query()
155        } else if rest == "=?" {
156            // Test form: AT+CMD=?
157            module.test()
158        } else if let Some(args_str) = rest.strip_prefix('=') {
159            // Set form: AT+CMD=args
160            module.set(Args { raw: args_str })
161        } else {
162            Err(AtError::InvalidArgs)
163        }
164    } else {
165        Err(AtError::UnknownCommand)
166    };
167    
168    match result {
169        Ok(response) => println!("  Response: {}", response),
170        Err(AtError::UnknownCommand) => println!("  Error: Unknown command"),
171        Err(AtError::NotSupported) => println!("  Error: Operation not supported"),
172        Err(AtError::InvalidArgs) => println!("  Error: Invalid arguments"),
173    }
174}
Source

fn set(&mut self, _args: Args<'_>) -> AtResult<'static>

Set command (AT+CMD=args) This is called to set parameters for a command.

Examples found in repository?
examples/complete_usage.rs (line 160)
145fn execute_command(cmd: &str, name: &str, module: &mut dyn AtContext) {
146    println!("\n> {}", cmd);
147    
148    let result = if let Some(rest) = cmd.strip_prefix(name) {
149        if rest.is_empty() {
150            // Execute form: AT+CMD
151            module.exec()
152        } else if rest == "?" {
153            // Query form: AT+CMD?
154            module.query()
155        } else if rest == "=?" {
156            // Test form: AT+CMD=?
157            module.test()
158        } else if let Some(args_str) = rest.strip_prefix('=') {
159            // Set form: AT+CMD=args
160            module.set(Args { raw: args_str })
161        } else {
162            Err(AtError::InvalidArgs)
163        }
164    } else {
165        Err(AtError::UnknownCommand)
166    };
167    
168    match result {
169        Ok(response) => println!("  Response: {}", response),
170        Err(AtError::UnknownCommand) => println!("  Error: Unknown command"),
171        Err(AtError::NotSupported) => println!("  Error: Operation not supported"),
172        Err(AtError::InvalidArgs) => println!("  Error: Invalid arguments"),
173    }
174}

Implementors§