Architecture Overview
Relevant source files
The following files were used as context for generating this wiki page:
The tauri-plugin-serialplugin is a cross-platform Tauri extension designed to provide serial port communication capabilities for both desktop (Windows, macOS, Linux) and mobile (Android) platforms. It bridges the gap between a web-based frontend and hardware serial interfaces using a Rust backend, with a dedicated Kotlin implementation for Android USB host support.
Sources: src/lib.rs:13-100, android/BUILD_INSTRUCTIONS.md
High-Level Component Structure
The plugin architecture is divided into three primary layers: the Frontend TypeScript API, the Rust Bridge (Tauri Plugin Core), and the Platform-Specific Implementations.
The diagram shows the communication flow from the frontend through the Rust bridge to the respective platform-specific hardware managers.
Sources: src/lib.rs:135-165, guest-js/index.ts, debug.md:30-50
Backend Architecture (Rust)
The Rust layer acts as the orchestrator, managing state and routing commands to the correct platform driver.
Plugin Initialization
The init() function registers the plugin with Tauri and sets up platform-specific state management. On desktop, it initializes a HashMap protected by an Arc<Mutex> to manage multiple serial port instances. On Android, it registers the Kotlin plugin module.
Sources: src/lib.rs:135-170
Platform Abstraction
The backend uses conditional compilation attributes (#[cfg(desktop)] and #[cfg(target_os = "android")]) to switch between the desktop serialport crate and the mobile JNI-based interface.
| Component | Desktop Implementation | Android Implementation |
|---|---|---|
| Provider | serialport crate | SerialPlugin.kt via JNI |
| State Storage | Arc<Mutex<HashMap<String, Port>>> | Android Plugin Handle |
| Communication | Direct OS syscalls | USB Host API via usb-serial-for-android |
Sources: src/lib.rs:13-25, debug.md:140-155
Frontend API (TypeScript)
The frontend provides a class-based interface (SerialPort) that abstracts the IPC calls to the backend. It includes support for both text and binary data, event-driven listening, and automatic reconnection logic.
The sequence illustrates the instantiation and opening of a port from the web frontend.
Sources: guest-js/index.ts, tests/README.md:40-60
Android Implementation Details
The Android portion of the plugin is implemented in Kotlin and relies on the usb-serial-for-android library. It manages USB permissions and provides a SerialPortManager to handle device attachment/detachment.
Build and Manifest Integration
The build.rs script automatically injects the necessary USB intent filters into the AndroidManifest.xml during the build process, ensuring the app can respond to hardware insertion events.
tauri_plugin::mobile::update_android_manifest(
"SERIAL PLUGIN",
"activity",
r#"<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>"#
)
Sources: build.rs:45-55, android/BUILD_INSTRUCTIONS.md
Type Flexibility
The Android implementation was specifically enhanced to handle type variations in configuration (e.g., supporting both numeric and string representations for DataBits and StopBits) to improve compatibility with JS-side numeric enums.
Sources: debug.md:15-25, debug.md:85-100
Security and Permissions
The plugin utilizes Tauri's Access Control List (ACL) system. Permissions are defined in JSON schemas that control which frontend commands are allowed to reach the backend.
- Default Permissions: Includes a set of "allow" rules for common operations like
available_ports,open,read, andwrite. - Scope Control: Allows for fine-grained restriction of access to specific port paths.
Sources: permissions/schemas/schema.json, examples/serialport-test/src-tauri/gen/schemas/desktop-schema.json
Summary
The architecture of tauri-plugin-serialplugin provides a unified developer experience by abstracting platform-specific hardware complexities. It combines the safety of Rust for desktop operations with a robust Kotlin-based USB manager for Android, all exposed through a familiar Svelte/TypeScript interface for the web frontend. Reconnection logic and flexible type handling ensure resilience in varied hardware environments.
Sources: src/lib.rs:135-175, debug.md:5-15