serialplugin_core/lib.rs
1//! Serial port core of `tauri-plugin-serialplugin`, usable from plain Rust (no Tauri).
2//!
3//! [`SerialManager`] owns every open port: one RX hub thread per port, watch streams
4//! with native framing ([`events::WatchOptions::parser`]), request/response
5//! [`exchange`](SerialManager::exchange), queued AT commands, GSM 07.10 CMUX virtual
6//! channels, `tcp://host:port` bridges and (feature `ble`) Bluetooth LE serial.
7//! Events are delivered to an [`EventSink`] closure you pass to
8//! [`SerialManager::watch`].
9//!
10//! New in 4.2 (core only, the 4.x API is unchanged): methods returning [`CoreError`]
11//! with an [`ErrorKind`] (`open_core`, `write_core`, `exchange_core`, …), any number
12//! of [`SerialManager::watch_core`] streams of [`CoreEvent`]s with bounded queues,
13//! port-state subscriptions ([`SerialManager::subscribe_core`]) and
14//! [`SerialManager::insert_port`] for your own `SerialPort` (a fake in tests).
15//!
16//! ```no_run
17//! use serialplugin_core::{events::{SerialEvent, WatchOptions}, EventSink, SerialManager};
18//! use std::sync::Arc;
19//!
20//! let serial = SerialManager::new();
21//! let path = serial.open("/dev/ttyUSB0".into(), 115_200, None, None, None, None, None)?;
22//! let sink: EventSink = Arc::new(|event| {
23//! if let SerialEvent::Data { data, .. } = event {
24//! println!("{}", String::from_utf8_lossy(&data));
25//! }
26//! true // keep the subscription
27//! });
28//! let watch_id = serial.watch(path.clone(), WatchOptions::default(), sink)?;
29//! let reply = serial.at(path.clone(), "AT+CSQ".into(), None)?;
30//! println!("{:?}", reply.status);
31//! serial.unwatch(watch_id)?;
32//! serial.close(path)?;
33//! # Ok::<(), serialplugin_core::error::Error>(())
34//! ```
35
36/// Logging macros that respect the global log level ([`state::set_log_level`]).
37pub mod logger;
38
39#[cfg(any(test, target_os = "android"))]
40mod jni_ready;
41
42#[cfg(target_os = "android")]
43pub mod android;
44pub mod api;
45pub mod at;
46pub mod cmux;
47pub mod error;
48pub mod events;
49pub mod exchange;
50pub mod hub;
51pub mod port;
52pub mod state;
53pub mod sync_util;
54
55#[cfg(target_os = "ios")]
56#[path = "serialport_ios.rs"]
57pub mod serialport;
58
59#[cfg(any(not(target_os = "android"), debug_assertions, feature = "test-util"))]
60pub mod mock_serial;
61
62/// The `log` crate, for the logging macros (feature `log`, and always on Android).
63#[doc(hidden)]
64#[cfg(any(feature = "log", target_os = "android"))]
65pub use log as __log;
66
67pub use api::SerialManager;
68pub use error::{CoreError, Error, ErrorKind};
69pub use events::{CoreEvent, CoreSink, EventSink, PortListSink, SubscriptionId};