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§
Sourcefn exec(&self) -> AtResult<'static>
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 171)
165fn execute_command(cmd: &str, name: &str, module: &mut dyn AtContext) {
166 println!("\n> {}", cmd);
167
168 let result = if let Some(rest) = cmd.strip_prefix(name) {
169 if rest.is_empty() {
170 // Execute form: AT+CMD
171 module.exec()
172 } else if rest == "?" {
173 // Query form: AT+CMD?
174 module.query()
175 } else if rest == "=?" {
176 // Test form: AT+CMD=?
177 module.test()
178 } else if let Some(args_str) = rest.strip_prefix('=') {
179 // Set form: AT+CMD=args
180 module.set(Args { raw: args_str })
181 } else {
182 Err(AtError::InvalidArgs)
183 }
184 } else {
185 Err(AtError::UnknownCommand)
186 };
187
188 match result {
189 Ok(response) => println!(" Response: {}", response),
190 Err(AtError::UnknownCommand) => println!(" Error: Unknown command"),
191 Err(AtError::NotSupported) => println!(" Error: Operation not supported"),
192 Err(AtError::InvalidArgs) => println!(" Error: Invalid arguments"),
193 }
194}Sourcefn query(&mut self) -> AtResult<'static>
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 174)
165fn execute_command(cmd: &str, name: &str, module: &mut dyn AtContext) {
166 println!("\n> {}", cmd);
167
168 let result = if let Some(rest) = cmd.strip_prefix(name) {
169 if rest.is_empty() {
170 // Execute form: AT+CMD
171 module.exec()
172 } else if rest == "?" {
173 // Query form: AT+CMD?
174 module.query()
175 } else if rest == "=?" {
176 // Test form: AT+CMD=?
177 module.test()
178 } else if let Some(args_str) = rest.strip_prefix('=') {
179 // Set form: AT+CMD=args
180 module.set(Args { raw: args_str })
181 } else {
182 Err(AtError::InvalidArgs)
183 }
184 } else {
185 Err(AtError::UnknownCommand)
186 };
187
188 match result {
189 Ok(response) => println!(" Response: {}", response),
190 Err(AtError::UnknownCommand) => println!(" Error: Unknown command"),
191 Err(AtError::NotSupported) => println!(" Error: Operation not supported"),
192 Err(AtError::InvalidArgs) => println!(" Error: Invalid arguments"),
193 }
194}Sourcefn test(&mut self) -> AtResult<'static>
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 177)
165fn execute_command(cmd: &str, name: &str, module: &mut dyn AtContext) {
166 println!("\n> {}", cmd);
167
168 let result = if let Some(rest) = cmd.strip_prefix(name) {
169 if rest.is_empty() {
170 // Execute form: AT+CMD
171 module.exec()
172 } else if rest == "?" {
173 // Query form: AT+CMD?
174 module.query()
175 } else if rest == "=?" {
176 // Test form: AT+CMD=?
177 module.test()
178 } else if let Some(args_str) = rest.strip_prefix('=') {
179 // Set form: AT+CMD=args
180 module.set(Args { raw: args_str })
181 } else {
182 Err(AtError::InvalidArgs)
183 }
184 } else {
185 Err(AtError::UnknownCommand)
186 };
187
188 match result {
189 Ok(response) => println!(" Response: {}", response),
190 Err(AtError::UnknownCommand) => println!(" Error: Unknown command"),
191 Err(AtError::NotSupported) => println!(" Error: Operation not supported"),
192 Err(AtError::InvalidArgs) => println!(" Error: Invalid arguments"),
193 }
194}Sourcefn set(&mut self, _args: Args<'_>) -> AtResult<'static>
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 180)
165fn execute_command(cmd: &str, name: &str, module: &mut dyn AtContext) {
166 println!("\n> {}", cmd);
167
168 let result = if let Some(rest) = cmd.strip_prefix(name) {
169 if rest.is_empty() {
170 // Execute form: AT+CMD
171 module.exec()
172 } else if rest == "?" {
173 // Query form: AT+CMD?
174 module.query()
175 } else if rest == "=?" {
176 // Test form: AT+CMD=?
177 module.test()
178 } else if let Some(args_str) = rest.strip_prefix('=') {
179 // Set form: AT+CMD=args
180 module.set(Args { raw: args_str })
181 } else {
182 Err(AtError::InvalidArgs)
183 }
184 } else {
185 Err(AtError::UnknownCommand)
186 };
187
188 match result {
189 Ok(response) => println!(" Response: {}", response),
190 Err(AtError::UnknownCommand) => println!(" Error: Unknown command"),
191 Err(AtError::NotSupported) => println!(" Error: Operation not supported"),
192 Err(AtError::InvalidArgs) => println!(" Error: Invalid arguments"),
193 }
194}