embedded_basic/embedded_basic.rs
1/***************************************************************************
2 *
3 * AT Command Parser
4 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
18 *
19 ***************************************************************************/
20
21//! Basic usage example demonstrating no_std compatible code
22//!
23//! Shows Args parsing and error handling patterns with the updated AtResult
24//! tuple type `Result<(&'static str, Bytes<SIZE>), (&'static str, AtError)>`.
25
26#![allow(dead_code)]
27#![no_std]
28#![no_main]
29
30extern crate at_parser_rs;
31
32use at_parser_rs::{Args, AtError, AtResult, at_response};
33
34const SIZE: usize = 64;
35const AT_RESP: &str = "+DEMO: ";
36
37// Parse the second argument and echo it back in the response
38fn parse_args_example() -> AtResult<'static, SIZE> {
39 let args = Args { raw: "foo,bar,baz" };
40 match args.get(1) {
41 Some(val) => Ok(at_response!(SIZE, AT_RESP; val.as_ref())),
42 None => Err((AT_RESP, AtError::InvalidArgs)),
43 }
44}
45
46// Demonstrate matching on the new tuple error
47fn handle_error_example() -> &'static str {
48 match parse_args_example() {
49 Ok((_, _)) => "OK",
50 Err((_, AtError::InvalidArgs)) => "Argomento non valido",
51 Err((_, AtError::UnknownCommand)) => "Comando sconosciuto",
52 Err(_) => "Errore generico",
53 }
54}
55
56#[unsafe(no_mangle)]
57pub extern "C" fn main() -> ! {
58 let _result = handle_error_example();
59 loop {}
60}
61