orouter_serial/lib.rs
1//! This crate provides typed messages used for serial communication between host and oRouter.
2//! It also contains codecs for different serial HW used in oRouter.
3//!
4//! As a wire codec oRouter uses [COBS](https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing)
5//! encoding, specifically [cobs](https://docs.rs/cobs/0.1.4/cobs/index.html) rust crate but this
6//! is completely abstracted from user of this library.
7//!
8//! For reading of messages one can use instance of [`crate::host::MessageReader`] -
9//! after it's created it can be fed incoming slices of bytes (`u8`s) and messages are returned
10//! in a [`crate::heapless::Vec`] when successfully decoded.
11//!
12//! Example:
13//!
14//! ```rust,no_run
15//! use orouter_serial::host::{
16//! calculate_cobs_overhead,
17//! codec::{UsbCodec, MAX_USB_FRAME_LENGTH},
18//! Message, MessageReader, StatusCode, RAWIQ_DATA_LENGTH,
19//! };
20//!
21//! const TEST_DATA: [u8; 4] = [0x03, 0xc5, 0x02, 0x00];
22//!
23//! fn main() {
24//! let mut message_reader =
25//! MessageReader::<{ calculate_cobs_overhead(RAWIQ_DATA_LENGTH) }, 17>::default();
26//! // feed the message_reader with test data representing a Status message
27//! let messages = message_reader.process_bytes::<UsbCodec>(&TEST_DATA[..]).unwrap();
28//! println!("received messages = {:?}", messages);
29//! assert_eq!(
30//! messages.get(0),
31//! Some(Message::Status { code: StatusCode::FrameReceived}).as_ref()
32//! );
33//! }
34//! ```
35//!
36//! for more complete read example refer to [examples/read.rs](examples/read.rs)
37//!
38//! For encoding a message for being send over the serial line a method
39//! [`crate::host::Message::encode`] is used. However, if you know a type of serial line you will
40//! be using to send a message (USB or BLE) you can use [`crate::host::Message::as_frames`] method
41//! with a specific codec from [`crate::host::codec`] module to get message bytes framed in a way
42//! needed by the particular serial line type.
43//!
44//!
45//! Example:
46//!
47//! ```rust,no_run
48//! use std::str::FromStr;
49//! use orouter_serial::host::{codec::UsbCodec, Message};
50//!
51//! fn main() {
52//! // let's create a configuration message to set oRouter to EU region with spreading factor 7
53//! let message = Message::from_str("config@1|7").unwrap();
54//! // for simplicity just output the bytes
55//! println!("bytes = {:02x?}", message.encode().unwrap().as_slice());
56//!
57//! // now let's get frames for sending over USB
58//! let _frames = message.as_frames::<UsbCodec>().unwrap();
59//! }
60//! ```
61//!
62//! for more complete write example refer to [examples/write.rs](examples/write.rs)
63//!
64//! ## Cargo `features`
65//!
66//! - `std` - on by default, exposes things which need standard library (e.g. `Debug` derives)
67//! - `defmt-impl` - add [`defmt::Format`] derive implementations
68
69#![cfg_attr(any(not(feature = "std"), not(test)), no_std)]
70
71pub mod host;
72
73// include defmt::Format implementations
74// we don't want them derive()d in the modules unless defmt-impl feature is set
75#[cfg(feature = "defmt-impl")]
76pub mod defmt;
77
78// reexport heapless
79pub use heapless;
80
81pub(crate) const MAX_LORA_PAYLOAD_LENGTH: usize = 255;