Skip to main content

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//! **Note**: This is a pattern demonstration example showing how the library
24//! can be used in no_std/embedded contexts. It illustrates API usage patterns
25//! and error handling approaches suitable for embedded systems.
26//!
27//! In a real embedded application, you would integrate these patterns into
28//! your firmware's main loop or RTOS tasks.
29
30#![allow(dead_code)]
31#![no_std]
32#![no_main]
33
34extern crate at_parser_rs;
35
36use at_parser_rs::{Args, AtError, AtResult};
37use osal_rs::utils::Bytes;
38
39const SIZE: usize = 64;
40
41// Example function using Args in no_std
42fn parse_args_example() -> AtResult<'static, SIZE> {
43    let args = Args { raw: "foo,bar,baz" };
44    match args.get(1) {
45        Some(val) => Ok(Bytes::from_str(val.as_ref())),
46        None => Err(AtError::InvalidArgs),
47    }
48}
49
50// Example of error handling
51fn handle_error_example() -> &'static str {
52    match parse_args_example() {
53        Ok(_) => "OK",
54        Err(AtError::InvalidArgs) => "Argomento non valido",
55        Err(_) => "Errore generico",
56    }
57}
58
59// In an embedded environment, these functions can be called from main or from a task.
60
61// Mock main for compilation (in real embedded code, this would be in your firmware)
62#[unsafe(no_mangle)]
63pub extern "C" fn main() -> ! {
64    // Example usage - in embedded this would be called from your main loop
65    let _result = handle_error_example();
66    loop {}
67}