Skip to main content

meshcore_rs/
lib.rs

1//! MeshCore - Rust library for communicating with MeshCore companion radio nodes
2//!
3//! This library provides an async interface for communicating with MeshCore devices
4//! over serial, TCP, or BLE connections.
5//!
6//! # Serial Example
7//!
8//! ```no_run
9//! use meshcore_rs::MeshCore;
10//!
11//! #[tokio::main]
12//! async fn main() -> Result<(), meshcore_rs::Error> {
13//!     // Connect via serial
14//!     let meshcore = MeshCore::serial("/dev/ttyUSB0", 115200).await?;
15//!
16//!     // Get device info
17//!     let info = meshcore.commands().lock().await.send_appstart().await?;
18//!     println!("Connected to: {}", info.name);
19//!
20//!     // Get contacts
21//!     let contacts = meshcore.commands().lock().await.get_contacts(0).await?;
22//!     println!("Found {} contacts", contacts.len());
23//!
24//!     meshcore.disconnect().await?;
25//!     Ok(())
26//! }
27//! ```
28//!
29//! # BLE Example
30//!
31//! Requires the `ble` feature.
32//!
33//! ```no_run
34//! use meshcore_rs::MeshCore;
35//! use std::time::Duration;
36//!
37//! #[tokio::main]
38//! async fn main() -> Result<(), meshcore_rs::Error> {
39//!     // First discover available MeshCore devices
40//!     let devices = MeshCore::ble_discover(Duration::from_secs(5)).await?;
41//!     println!("Found devices: {:?}", devices);
42//!
43//!     // Connect to a specific device by name
44//!     let meshcore = MeshCore::ble_connect("MyDevice").await?;
45//!
46//!     // Get device info
47//!     let info = meshcore.commands().lock().await.send_appstart().await?;
48//!     println!("Connected to: {}", info.name);
49//!
50//!     // Get contacts
51//!     let contacts = meshcore.commands().lock().await.get_contacts(0).await?;
52//!     println!("Found {} contacts", contacts.len());
53//!
54//!     meshcore.disconnect().await?;
55//!     Ok(())
56//! }
57//! ```
58
59pub mod commands;
60pub mod error;
61pub mod events;
62pub mod packets;
63pub mod parsing;
64pub mod reader;
65
66mod meshcore;
67
68// Protocol constants
69/// Length of channel name field in bytes
70pub const CHANNEL_NAME_LEN: usize = 32;
71/// Length of channel secret field in bytes
72pub const CHANNEL_SECRET_LEN: usize = 16;
73/// Total length of channel info payload (idx + name + secret)
74pub const CHANNEL_INFO_LEN: usize = 1 + CHANNEL_NAME_LEN + CHANNEL_SECRET_LEN;
75
76pub use error::Error;
77pub use events::{
78    ChannelMessage, ContactMessage, EventDispatcher, EventPayload, EventType, MeshCoreEvent,
79    MsgSentInfo, Subscription,
80};
81pub use meshcore::MeshCore;
82pub use packets::{AnonReqType, BinaryReqType, ControlType, PacketType};
83
84/// Result type alias using the library's Error type
85pub type Result<T> = std::result::Result<T, Error>;
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn test_channel_name_len() {
93        assert_eq!(CHANNEL_NAME_LEN, 32);
94    }
95
96    #[test]
97    fn test_channel_secret_len() {
98        assert_eq!(CHANNEL_SECRET_LEN, 16);
99    }
100
101    #[test]
102    fn test_channel_info_len() {
103        // 1 byte idx + 32 bytes name + 16 bytes secret = 49
104        assert_eq!(CHANNEL_INFO_LEN, 49);
105        assert_eq!(CHANNEL_INFO_LEN, 1 + CHANNEL_NAME_LEN + CHANNEL_SECRET_LEN);
106    }
107}