tauri-plugin-serialplugin 3.0.0

Tauri plugin for serial port access (desktop and Android).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Tauri command handlers for serial port I/O. See README and docs.rs.

use crate::api::SerialPort;
use crate::error::Error;
use crate::events::{Capabilities, ExchangeOptions, SerialEvent, WatchOptions};
use crate::state::{ClearBuffer, DataBits, FlowControl, Parity, StopBits};
use std::collections::HashMap;
use std::time::Duration;
use tauri::ipc::Channel;
use tauri::{AppHandle, Runtime, State};

/// Lists all available serial ports on the system
/// See README for examples.
#[tauri::command]
pub fn available_ports<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    single_port_per_device: Option<bool>,
) -> Result<HashMap<String, HashMap<String, String>>, Error> {
    serial.available_ports(single_port_per_device.unwrap_or(false))
}

/// Lists all currently managed serial ports
/// See README for examples.
#[tauri::command]
pub fn managed_ports<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
) -> Result<Vec<String>, Error> {
    serial.managed_ports()
}

/// Cancels an in-flight poll `read` on a serial port (does not stop an active `watch`).
/// See README for examples.
#[tauri::command]
pub fn cancel_read<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<(), Error> {
    serial.cancel_read(path)
}

/// Closes a serial port
/// See README for examples.
#[tauri::command]
pub fn close<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<(), Error> {
    serial.close(path)
}

/// Closes all open serial ports
/// See README for examples.
#[tauri::command]
pub fn close_all<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
) -> Result<(), Error> {
    serial.close_all()
}

/// Forcefully closes a serial port
/// See README for examples.
#[tauri::command]
pub fn force_close<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<(), Error> {
    serial.force_close(path)
}

/// Opens a serial port with the specified configuration
/// See README for examples.
#[tauri::command]
#[allow(clippy::too_many_arguments)]
pub fn open<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    baud_rate: u32,
    data_bits: Option<DataBits>,
    flow_control: Option<FlowControl>,
    parity: Option<Parity>,
    stop_bits: Option<StopBits>,
    timeout: Option<u64>,
) -> Result<String, Error> {
    serial.open(
        path,
        baud_rate,
        data_bits,
        flow_control,
        parity,
        stop_bits,
        timeout,
    )
}

/// Writes string data to a serial port
/// See README for examples.
#[tauri::command]
pub fn write<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    value: String,
) -> Result<usize, Error> {
    serial.write(path, value)
}

/// Writes binary data to a serial port
/// See README for examples.
#[tauri::command]
pub fn write_binary<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    value: Vec<u8>,
) -> Result<usize, Error> {
    serial.write_binary(path, value)
}

/// Reads string data from a serial port
/// See README for examples.
#[tauri::command]
pub fn read<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    timeout: Option<u64>,
    size: Option<usize>,
) -> Result<String, Error> {
    serial.read(path, timeout, size)
}

/// Reads binary data from a serial port
/// See README for examples.
#[tauri::command]
pub fn read_binary<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    timeout: Option<u64>,
    size: Option<usize>,
) -> Result<Vec<u8>, Error> {
    serial.read_binary(path, timeout, size)
}

/// Returns runtime capabilities (transport, platform, version) without window probing.
/// See README for examples.
#[tauri::command]
pub fn capabilities() -> Result<Capabilities, Error> {
    Ok(Capabilities::current())
}

/// Streams serial port events through a Tauri IPC channel.
/// See README for examples.
#[tauri::command]
pub fn watch<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    options: WatchOptions,
    channel: Channel<SerialEvent>,
) -> Result<u32, Error> {
    serial.watch(path, options, channel)
}

/// Stops a watch session by channel id.
/// See README for examples.
#[tauri::command]
pub fn unwatch<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    channel_id: u32,
) -> Result<(), Error> {
    serial.unwatch(channel_id)
}

/// Streams available-port attach/detach events through a Tauri IPC channel.
#[tauri::command]
pub fn watch_ports<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    options: Option<crate::events::WatchPortsOptions>,
    channel: tauri::ipc::Channel<crate::events::PortListEvent>,
) -> Result<u32, Error> {
    serial.watch_ports(options.unwrap_or_default(), channel)
}

/// Stops a port-list watch session by channel id.
#[tauri::command]
pub fn unwatch_ports<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    channel_id: u32,
) -> Result<(), Error> {
    serial.unwatch_ports(channel_id)
}

/// Sets the baud rate for a serial port
/// See README for examples.
#[tauri::command]
pub fn set_baud_rate<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    baud_rate: u32,
) -> Result<(), Error> {
    serial.set_baud_rate(path, baud_rate)
}

/// Sets the number of data bits for a serial port
/// See README for examples.
#[tauri::command]
pub fn set_data_bits<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    data_bits: DataBits,
) -> Result<(), Error> {
    serial.set_data_bits(path, data_bits)
}

/// Sets the flow control mode for a serial port
/// See README for examples.
#[tauri::command]
pub fn set_flow_control<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    flow_control: FlowControl,
) -> Result<(), Error> {
    serial.set_flow_control(path, flow_control)
}

/// Sets the parity checking mode for a serial port
/// See README for examples.
#[tauri::command]
pub fn set_parity<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    parity: Parity,
) -> Result<(), Error> {
    serial.set_parity(path, parity)
}

/// Sets the number of stop bits for a serial port
/// See README for examples.
#[tauri::command]
pub fn set_stop_bits<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    stop_bits: StopBits,
) -> Result<(), Error> {
    serial.set_stop_bits(path, stop_bits)
}

/// Sets the read timeout for a serial port
/// See README for examples.
#[tauri::command]
pub fn set_timeout<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    timeout: u64,
) -> Result<(), Error> {
    let timeout_duration = Duration::from_millis(timeout);
    serial.set_timeout(path, timeout_duration)
}

/// Sets the RTS (Request To Send) control signal
/// See README for examples.
#[tauri::command]
pub fn write_request_to_send<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    level: bool,
) -> Result<(), Error> {
    serial.write_request_to_send(path, level)
}

/// Sets the DTR (Data Terminal Ready) control signal
/// See README for examples.
#[tauri::command]
pub fn write_data_terminal_ready<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    level: bool,
) -> Result<(), Error> {
    serial.write_data_terminal_ready(path, level)
}

/// Reads the CTS (Clear To Send) control signal state
/// See README for examples.
#[tauri::command]
pub fn read_clear_to_send<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<bool, Error> {
    serial.read_clear_to_send(path)
}

/// Reads the DSR (Data Set Ready) control signal state
/// See README for examples.
#[tauri::command]
pub fn read_data_set_ready<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<bool, Error> {
    serial.read_data_set_ready(path)
}

/// Reads the RI (Ring Indicator) control signal state
/// See README for examples.
#[tauri::command]
pub fn read_ring_indicator<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<bool, Error> {
    serial.read_ring_indicator(path)
}

/// Reads the CD (Carrier Detect) control signal state
/// See README for examples.
#[tauri::command]
pub fn read_carrier_detect<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<bool, Error> {
    serial.read_carrier_detect(path)
}

/// Gets the number of bytes available to read from the serial port
/// See README for examples.
#[tauri::command]
pub fn bytes_to_read<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<u32, Error> {
    serial.bytes_to_read(path)
}

/// Gets the number of bytes available to write to the serial port
/// See README for examples.
#[tauri::command]
pub fn bytes_to_write<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<u32, Error> {
    serial.bytes_to_write(path)
}

/// Clears the specified buffer of the serial port
/// See README for examples.
#[tauri::command]
pub fn clear_buffer<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    buffer_type: ClearBuffer,
) -> Result<(), Error> {
    serial.clear_buffer(path, buffer_type)
}

/// Sets the break condition on the serial port
/// See README for examples.
#[tauri::command]
pub fn set_break<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<(), Error> {
    serial.set_break(path)
}

/// Clears the break condition on the serial port
/// See README for examples.
#[tauri::command]
pub fn clear_break<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<(), Error> {
    serial.clear_break(path)
}

/// Sets the global log level for the plugin
/// See README for examples.
#[tauri::command]
pub fn set_log_level<R: Runtime>(
    _app: AppHandle<R>,
    _serial: State<'_, SerialPort<R>>,
    level: crate::state::LogLevel,
) -> Result<(), Error> {
    crate::state::set_log_level(level);
    Ok(())
}

/// Gets the current global log level
/// See README for examples.
#[tauri::command]
pub fn get_log_level<R: Runtime>(
    _app: AppHandle<R>,
    _serial: State<'_, SerialPort<R>>,
) -> Result<crate::state::LogLevel, Error> {
    Ok(crate::state::get_log_level())
}

/// Write and read-until-response (AT-style exchange).
/// See README for examples.
#[tauri::command]
pub async fn exchange<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    value: String,
    options: Option<ExchangeOptions>,
) -> Result<crate::at::parse::ExchangeResponse, Error> {
    let serial = serial.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        serial.exchange(path, value, options.unwrap_or_default())
    })
    .await
    .map_err(|e| Error::String(format!("exchange task failed: {e}")))?
}

/// Cancel an in-flight exchange on a port.
/// See README for examples.
#[tauri::command]
pub fn cancel_exchange<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<(), Error> {
    serial.cancel_exchange(path)
}

/// Send one AT command with native session defaults and FIFO queue.
#[tauri::command]
pub async fn at<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    command: String,
    options: Option<crate::at::session::AtCommandOptions>,
) -> Result<crate::at::parse::AtCommandResult, Error> {
    let serial = serial.inner().clone();
    tauri::async_runtime::spawn_blocking(move || serial.at(path, command, options))
        .await
        .map_err(|e| Error::String(format!("at task failed: {e}")))?
}

/// Multi-phase AT flow (e.g. CMGS) as one atomic queue job.
#[tauri::command]
pub async fn at_phases<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    phases: Vec<crate::at::session::AtPhase>,
) -> Result<Vec<crate::at::parse::AtCommandResult>, Error> {
    let serial = serial.inner().clone();
    tauri::async_runtime::spawn_blocking(move || serial.at_phases(path, phases))
        .await
        .map_err(|e| Error::String(format!("at_phases task failed: {e}")))?
}

/// Built-in CMGS recipe through the native transaction queue.
#[tauri::command]
pub async fn send_sms_pdu<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    length: u32,
    pdu: Vec<u8>,
    options: Option<crate::at::session::SendSmsPduOptions>,
) -> Result<Vec<crate::at::parse::AtCommandResult>, Error> {
    let serial = serial.inner().clone();
    tauri::async_runtime::spawn_blocking(move || serial.send_sms_pdu(path, length, pdu, options))
        .await
        .map_err(|e| Error::String(format!("send_sms_pdu task failed: {e}")))?
}

/// Configure AT session defaults for native `at` on a port.
#[tauri::command]
pub fn configure_at_session<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    session: crate::at::session::AtSessionConfig,
) -> Result<(), Error> {
    serial.configure_at_session(path, session)
}

/// Binary write + read-until-response (e.g. CMGS PDU).
#[tauri::command]
pub async fn exchange_binary<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    value: Vec<u8>,
    options: Option<crate::events::ExchangeOptions>,
) -> Result<crate::at::parse::ExchangeResponse, Error> {
    let serial = serial.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        serial.exchange_binary(path, value, options.unwrap_or_default())
    })
    .await
    .map_err(|e| Error::String(format!("exchange_binary task failed: {e}")))?
}

#[derive(Debug, Clone, Default, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EnableMuxOptions {
    pub command: Option<String>,
    pub timeout_ms: Option<u64>,
}

/// Enter GSM 07.10 CMUX mode on a physical port (desktop).
#[tauri::command]
pub async fn enable_mux<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    options: Option<EnableMuxOptions>,
) -> Result<(), Error> {
    let opts = options.unwrap_or_default();
    let command = opts
        .command
        .unwrap_or_else(|| "AT+CMUX=0,0,5,31,10,2".to_string());
    let timeout_ms = opts.timeout_ms.unwrap_or(5000);
    let serial = serial.inner().clone();
    tauri::async_runtime::spawn_blocking(move || serial.enable_mux(path, command, timeout_ms))
        .await
        .map_err(|e| Error::String(format!("enable_mux task failed: {e}")))?
}

/// Open a virtual CMUX channel (returns composite path).
#[tauri::command]
pub fn open_mux_channel<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    dlci: u8,
) -> Result<String, Error> {
    serial.open_mux_channel(path, dlci)
}

/// Tear down CMUX and close virtual channels.
#[tauri::command]
pub fn disable_mux<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
) -> Result<(), Error> {
    serial.disable_mux(path)
}