tauri_plugin_serialplugin/lib.rs
1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use crate::commands::*;
6use tauri::{
7 plugin::{Builder, TauriPlugin},
8 Manager, Runtime,
9};
10
11#[cfg(target_os = "android")]
12const PLUGIN_IDENTIFIER: &str = "app.tauri.serialplugin";
13
14#[cfg(desktop)]
15use crate::desktop_api::SerialPort;
16#[cfg(target_os = "android")]
17use crate::mobile_api::SerialPort;
18#[cfg(desktop)]
19use std::collections::HashMap;
20#[cfg(desktop)]
21use std::sync::{Arc, Mutex};
22
23/// Commands module providing Tauri commands for serial port operations
24///
25/// This module contains all the Tauri commands that can be invoked from the frontend
26/// to interact with serial ports. Each command is designed to be used with the
27/// `tauri::invoke` function or through the plugin's JavaScript API.
28///
29/// # Examples
30///
31/// ```rust
32/// use tauri_plugin_serialplugin::commands;
33/// use tauri::{AppHandle, State};
34///
35/// #[tauri::command]
36/// async fn open_serial_port(
37/// app: AppHandle<tauri::Wry>,
38/// serial: State<'_, tauri_plugin_serialplugin::desktop_api::SerialPort<tauri::Wry>>
39/// ) -> Result<(), String> {
40/// commands::open(app, serial, "COM1".to_string(), 9600, None, None, None, None, None)
41/// .map_err(|e| e.to_string())
42/// }
43/// ```
44pub mod commands;
45#[cfg(test)]
46mod tests {
47 mod mock;
48 mod commands_test;
49 mod state_test;
50 mod error_test;
51 mod desktop_api_test;
52 mod mobile_api_test;
53 mod serial_test;
54}
55
56#[cfg(desktop)]
57/// Desktop API module providing serial port functionality for desktop platforms
58///
59/// This module contains the desktop-specific implementation of serial port
60/// operations. It provides a unified interface for managing serial ports
61/// across different desktop operating systems (Windows, macOS, Linux).
62///
63/// # Examples
64///
65/// ```rust
66/// use tauri_plugin_serialplugin::desktop_api::SerialPort;
67/// use tauri_plugin_serialplugin::state::{DataBits, FlowControl, Parity, StopBits};
68/// use tauri::AppHandle;
69/// use std::time::Duration;
70///
71/// // Note: In a real Tauri app, you would get the AppHandle from the command context
72/// // let serial = SerialPort::new(app_handle);
73/// // serial.open("COM1".to_string(), 9600, Some(DataBits::Eight),
74/// // Some(FlowControl::None), Some(Parity::None),
75/// // Some(StopBits::One), Some(1000))
76/// // .expect("Failed to open port");
77/// ```
78pub mod desktop_api;
79/// Error types for serial port operations
80///
81/// This module defines the error types used throughout the serial plugin.
82/// It provides a unified error handling interface for both desktop and
83/// mobile platforms.
84///
85/// # Examples
86///
87/// ```rust
88/// use tauri_plugin_serialplugin::error::Error;
89///
90/// // Example of error handling
91/// fn handle_operation_result(result: Result<(), Error>) {
92/// match result {
93/// Ok(_) => println!("Operation successful"),
94/// Err(Error::Io(msg)) => println!("IO error: {}", msg),
95/// Err(Error::SerialPort(msg)) => println!("Serial port error: {}", msg),
96/// Err(Error::String(msg)) => println!("Error: {}", msg),
97/// }
98/// }
99/// ```
100pub mod error;
101#[cfg(mobile)]
102/// Mobile API module providing serial port functionality for mobile platforms
103///
104/// This module contains the mobile-specific implementation of serial port
105/// operations. It provides a unified interface for managing serial ports
106/// on Android devices.
107///
108/// # Examples
109///
110/// ```rust
111/// use tauri_plugin_serialplugin::mobile_api::SerialPort;
112/// use tauri_plugin_serialplugin::state::{DataBits, FlowControl, Parity, StopBits};
113/// use tauri::AppHandle;
114/// use std::time::Duration;
115///
116/// // Note: In a real Tauri app, you would get the AppHandle from the command context
117/// // let serial = SerialPort::new(app_handle);
118/// // serial.open("/dev/ttyUSB0".to_string(), 9600, Some(DataBits::Eight),
119/// // Some(FlowControl::None), Some(Parity::None),
120/// // Some(StopBits::One), Some(1000))
121/// // .expect("Failed to open port");
122/// ```
123pub mod mobile_api;
124/// State types and enums for serial port configuration
125///
126/// This module defines the data structures and enums used for serial port
127/// configuration. It includes types for baud rates, data bits, flow control,
128/// parity, stop bits, and other serial port settings.
129///
130/// # Examples
131///
132/// ```rust
133/// use tauri_plugin_serialplugin::state::{DataBits, FlowControl, Parity, StopBits};
134///
135/// // Configure serial port settings
136/// let data_bits = DataBits::Eight;
137/// let flow_control = FlowControl::None;
138/// let parity = Parity::None;
139/// let stop_bits = StopBits::One;
140/// ```
141pub mod state;
142
143/// Initializes the serial plugin for Tauri
144///
145/// This function creates and configures the serial plugin with all available
146/// commands for serial port operations. It sets up the necessary state management
147/// and registers the plugin with the Tauri application.
148///
149/// # Returns
150///
151/// A configured `TauriPlugin` instance that can be added to your Tauri app.
152///
153/// # Example
154///
155/// ```rust,ignore
156/// use tauri_plugin_serialplugin::init;
157///
158/// fn main() {
159/// tauri::Builder::default()
160/// .plugin(init())
161/// // .run(tauri::generate_context!())
162/// // .expect("error while running tauri application");
163/// }
164/// ```
165pub fn init<R: Runtime>() -> TauriPlugin<R> {
166 Builder::new("serialplugin")
167 .invoke_handler(tauri::generate_handler![
168 available_ports,
169 available_ports_direct,
170 managed_ports,
171 cancel_read,
172 close,
173 close_all,
174 force_close,
175 open,
176 start_listening,
177 stop_listening,
178 read,
179 read_binary,
180 write,
181 write_binary,
182 set_baud_rate,
183 set_data_bits,
184 set_flow_control,
185 set_parity,
186 set_stop_bits,
187 set_timeout,
188 write_request_to_send,
189 write_data_terminal_ready,
190 read_clear_to_send,
191 read_data_set_ready,
192 read_ring_indicator,
193 read_carrier_detect,
194 bytes_to_read,
195 bytes_to_write,
196 clear_buffer,
197 set_break,
198 clear_break,
199 ])
200 .setup(|app, _api| {
201 #[cfg(target_os = "android")]
202 let handle = _api.register_android_plugin(PLUGIN_IDENTIFIER, "SerialPlugin")?;
203 #[cfg(target_os = "android")]
204 let serialplugin = SerialPort(handle);
205 // app.manage(SerialPort(handle));
206 #[cfg(desktop)]
207 let serialplugin = SerialPort {
208 app: app.clone(),
209 serialports: Arc::new(Mutex::new(HashMap::new())),
210 };
211
212 app.manage(serialplugin);
213 Ok(())
214 })
215 .build()
216}