serialport-stream 0.3.0

Async Stream and AsyncRead/AsyncWrite for serial ports using platform-specific I/O
Documentation
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
//! Async serial port I/O as [`Stream`], [`AsyncRead`], and [`AsyncWrite`].
//!
//! Configure and open ports with [`new`] → [`SerialPortStreamBuilder`] → [`.open()`](SerialPortStreamBuilder::open).
//! Line settings, DTR, and buffer clearing are applied at open time.
//!
//! POSIX `termios` on Unix; Win32 COMM APIs on Windows. Configuration types ([`DataBits`],
//! [`Parity`], [`StopBits`], [`FlowControl`], [`ClearBuffer`]) are defined in this crate.
//!
//! The first read poll starts a background thread that appends incoming bytes to an in-memory FIFO
//! shared by [`Stream`] and [`AsyncRead`]. There is no backpressure. The first write poll starts
//! a separate background thread.
//!
//! [`Stream`] / [`TryStreamExt::try_next`] drains the full FIFO per item; [`AsyncRead`] reads
//! partially and leaves the remainder cached. Use one read style per open port.
//!
//! [`AsyncWriteExt`], [`AsyncReadExt`], and [`TryStreamExt`] are re-exported from `futures`.
//!
//! ```no_run
//! use serialport_stream::{new, AsyncWriteExt, TryStreamExt};
//!
//! # async fn example() -> std::io::Result<()> {
//! let mut stream = new("/dev/ttyUSB0", 115200).open()?;
//! stream.write_all(b"PING\r\n").await?;
//! while let Some(chunk) = stream.try_next().await? {
//!     println!("{chunk:?}");
//! }
//! # Ok(())
//! # }
//! ```
//!

use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};

mod platform;
mod types;

pub mod line_settings;
pub use types::{ClearBuffer, DataBits, FlowControl, Parity, StopBits};

use crate::platform::PlatformStream;
use futures::task::AtomicWaker;

pub use futures::io::{AsyncRead, AsyncReadExt};
pub use futures::io::{AsyncWrite, AsyncWriteExt};
pub use futures::stream::{Stream, TryStreamExt};

#[derive(Debug)]
pub(crate) struct EventsInner {
    pub(crate) in_buffer: Mutex<Vec<u8>>,
    pub(crate) stream_error: Mutex<Option<std::io::Error>>,
    pub(crate) waker: AtomicWaker,
}

impl EventsInner {
    pub(crate) fn new() -> Self {
        Self {
            in_buffer: Mutex::new(Vec::new()),
            stream_error: Mutex::new(None),
            waker: AtomicWaker::new(),
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) enum PendingWrite {
    Idle,
    Buffer(Vec<u8>),
    Completed(usize),
}

#[derive(Debug)]
pub(crate) struct EventsInnerWrite {
    pub(crate) pending: Mutex<PendingWrite>,
    pub(crate) write_error: Mutex<Option<std::io::Error>>,
    pub(crate) waker: AtomicWaker,
}

impl EventsInnerWrite {
    pub(crate) fn new() -> Self {
        Self {
            pending: Mutex::new(PendingWrite::Idle),
            write_error: Mutex::new(None),
            waker: AtomicWaker::new(),
        }
    }
}

/// Builder for serial port path, line settings, and one-shot open options.
///
/// Created with [`new()`], configured with chained methods, then finalized with
/// [`open()`](SerialPortStreamBuilder::open).
///
/// # Example
///
/// ```no_run
/// use serialport_stream::new;
/// use serialport_stream::{ClearBuffer, DataBits, FlowControl, Parity, StopBits};
///
/// # fn example() -> std::io::Result<()> {
/// let stream = new("/dev/ttyUSB0", 115200)
///     .data_bits(DataBits::Eight)
///     .parity(Parity::None)
///     .stop_bits(StopBits::One)
///     .flow_control(FlowControl::None)
///     .dtr_on_open(true)
///     .clear(ClearBuffer::All)
///     .open()?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SerialPortStreamBuilder {
    pub(crate) path: String,
    pub(crate) baud_rate: u32,
    pub(crate) data_bits: DataBits,
    pub(crate) flow_control: FlowControl,
    pub(crate) parity: Parity,
    pub(crate) stop_bits: StopBits,
    pub(crate) dtr_on_open: bool,
    pub(crate) clear_buffer: Option<ClearBuffer>,
}

impl SerialPortStreamBuilder {
    /// Sets the path to the serial port device.
    ///
    /// # Examples
    /// - Unix: `"/dev/ttyUSB0"`, `"/dev/ttyACM0"`
    /// - Windows: `"COM3"`, `"COM10"`
    #[allow(clippy::assigning_clones)]
    #[must_use]
    pub fn path<'a>(mut self, path: impl Into<std::borrow::Cow<'a, str>>) -> Self {
        self.path = path.into().as_ref().to_owned();
        self
    }

    /// Sets the baud rate (bits per second).
    ///
    /// Common values: 9600, 19200, 38400, 57600, 115200
    #[must_use]
    pub fn baud_rate(mut self, baud_rate: u32) -> Self {
        self.baud_rate = baud_rate;
        self
    }

    /// Sets the number of data bits per character.
    ///
    /// Default: `DataBits::Eight`
    #[must_use]
    pub fn data_bits(mut self, data_bits: DataBits) -> Self {
        self.data_bits = data_bits;
        self
    }

    /// Sets the flow control mode.
    ///
    /// Default: `FlowControl::None`
    #[must_use]
    pub fn flow_control(mut self, flow_control: FlowControl) -> Self {
        self.flow_control = flow_control;
        self
    }

    /// Sets the parity checking mode.
    ///
    /// Default: `Parity::None`
    #[must_use]
    pub fn parity(mut self, parity: Parity) -> Self {
        self.parity = parity;
        self
    }

    /// Sets the number of stop bits.
    ///
    /// Default: `StopBits::One`
    #[must_use]
    pub fn stop_bits(mut self, stop_bits: StopBits) -> Self {
        self.stop_bits = stop_bits;
        self
    }

    /// Sets the DTR (Data Terminal Ready) signal state applied when opening the port.
    ///
    /// `true` asserts DTR, `false` clears it. The line is always driven on open.
    ///
    /// Default: `false`
    #[must_use]
    pub fn dtr_on_open(mut self, state: bool) -> Self {
        self.dtr_on_open = state;
        self
    }

    /// Clears RX and/or TX driver buffers when the port is opened, before async I/O starts.
    ///
    /// See [`ClearBuffer`] (`Input`, `Output`, or `All`).
    #[must_use]
    pub fn clear(mut self, buffer: ClearBuffer) -> Self {
        self.clear_buffer = Some(buffer);
        self
    }

    /// Opens the serial port and returns a [`SerialPortStream`].
    ///
    /// Applies line settings, [`dtr_on_open`](Self::dtr_on_open), and optional
    /// [`clear`](Self::clear) before any background read/write threads are started.
    pub fn open(self) -> std::io::Result<SerialPortStream> {
        let inner = Arc::new(EventsInner::new());
        let write_inner = Arc::new(EventsInnerWrite::new());
        Ok(SerialPortStream {
            platform: PlatformStream::new(self, inner.clone(), write_inner.clone())?,
            inner,
            write_inner,
            flush_task: None,
        })
    }
}

/// Creates a [`SerialPortStreamBuilder`] with default line settings (8N1, no flow control).
///
/// # Examples
///
/// Unix device path:
///
/// ```no_run
/// # use serialport_stream::new;
/// # fn example() -> std::io::Result<()> {
/// let _stream = new("/dev/ttyUSB0", 115200).open()?;
/// # Ok(())
/// # }
/// ```
///
/// Windows COM port:
///
/// ```no_run
/// # use serialport_stream::new;
/// # fn example() -> std::io::Result<()> {
/// let _stream = new("COM3", 9600).open()?;
/// # Ok(())
/// # }
/// ```
pub fn new<'a>(
    path: impl Into<std::borrow::Cow<'a, str>>,
    baud_rate: u32,
) -> SerialPortStreamBuilder {
    SerialPortStreamBuilder {
        path: path.into().into_owned(),
        baud_rate,
        data_bits: DataBits::Eight,
        flow_control: FlowControl::None,
        parity: Parity::None,
        stop_bits: StopBits::One,
        dtr_on_open: false,
        clear_buffer: None,
    }
}

/// An opened serial port for async reads and writes.
///
/// - [`Stream`] / [`AsyncRead`]: shared in-memory receive FIFO (background read thread).
/// - [`AsyncWrite`]: dedicated background write thread.
///
/// Configure the port only via [`SerialPortStreamBuilder`] before [`SerialPortStreamBuilder::open`].
///
/// # Example
///
/// ```no_run
/// use serialport_stream::new;
/// use futures::io::AsyncWriteExt;
/// use futures::stream::TryStreamExt;
///
/// # async fn example() -> std::io::Result<()> {
/// let mut stream = new("COM3", 115200).open()?;
/// stream.write_all(&[0x0a, 0xC0]).await?;
/// if let Some(bytes) = stream.try_next().await? {
///     println!("{bytes:?}");
/// }
/// # Ok(())
/// # }
/// ```
pub struct SerialPortStream {
    platform: PlatformStream,
    inner: Arc<EventsInner>,
    write_inner: Arc<EventsInnerWrite>,
    flush_task: Option<blocking::Task<std::io::Result<()>>>,
}

impl std::fmt::Debug for SerialPortStream {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SerialPortStream")
            .field("platform", &self.platform)
            .field("inner", &self.inner)
            .field("write_inner", &self.write_inner)
            .field("flush_task", &self.flush_task.as_ref().map(|_| "..."))
            .finish()
    }
}

impl SerialPortStream {
    fn poll_receiver_ready(&mut self, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        self.inner.waker.register(cx.waker());

        if let Some(err) = self.inner.stream_error.lock().unwrap().as_ref() {
            return Poll::Ready(Err(std::io::Error::new(err.kind(), err.to_string())));
        }

        if !self.platform.is_read_thread_started() {
            self.platform.start_read_thread();
            return Poll::Pending;
        }

        Poll::Ready(Ok(()))
    }

    fn poll_writer_ready(&mut self, cx: &mut Context<'_>) -> std::io::Result<()> {
        self.write_inner.waker.register(cx.waker());

        if let Some(err) = self.write_inner.write_error.lock().unwrap().as_ref() {
            return Err(std::io::Error::new(err.kind(), err.to_string()));
        }

        if !self.platform.is_write_thread_started() {
            self.platform.start_write_thread();
        }

        Ok(())
    }

    /// Polls for the next received chunk, same as [`Stream::poll_next`].
    ///
    /// When ready, returns `Poll::Ready(Some(Ok(vec)))` with every byte currently buffered,
    /// or `Poll::Pending` if the read thread has not yet delivered data.
    pub fn try_poll_next(
        &mut self,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Vec<u8>, std::io::Error>>> {
        match self.poll_receiver_ready(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
            Poll::Ready(Ok(())) => {
                let mut buffer = self.inner.in_buffer.lock().unwrap();
                if !buffer.is_empty() {
                    // Drain all available data
                    let data = buffer.drain(..).collect();
                    return Poll::Ready(Some(Ok(data)));
                }

                Poll::Pending
            }
        }
    }
}

unsafe impl Send for SerialPortStream {}

unsafe impl Sync for SerialPortStream {}

impl AsyncRead for SerialPortStream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<std::io::Result<usize>> {
        assert!(!buf.is_empty());
        let this = self.as_mut().get_mut();
        match this.poll_receiver_ready(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
            Poll::Ready(Ok(())) => {
                let mut buffer = this.inner.in_buffer.lock().unwrap();
                if buffer.is_empty() {
                    return Poll::Pending;
                }
                let n = buf.len().min(buffer.len());
                buf[..n].copy_from_slice(&buffer[..n]);
                buffer.drain(..n);
                let cached_bytes = buffer.len();
                if cached_bytes > 0 {
                    tracing::info!(
                        read_bytes = n,
                        cached_bytes,
                        "serialport-stream receive buffer after AsyncRead read"
                    );
                }
                Poll::Ready(Ok(n))
            }
        }
    }
}

impl Stream for SerialPortStream {
    type Item = Result<Vec<u8>, std::io::Error>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.try_poll_next(cx)
    }
}

impl AsyncWrite for SerialPortStream {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        assert!(!buf.is_empty());
        let this = self.as_mut().get_mut();
        match this.poll_writer_ready(cx) {
            Err(e) => Poll::Ready(Err(e)),
            Ok(()) => {
                let mut pending = this.write_inner.pending.lock().unwrap();
                match *pending {
                    crate::PendingWrite::Idle => {
                        // new transaction
                        *pending = crate::PendingWrite::Buffer(buf.to_vec());
                        drop(pending);
                        self.platform.signal_write();
                        Poll::Pending
                    }
                    crate::PendingWrite::Buffer(_) => {
                        // write still pending
                        Poll::Pending
                    }
                    crate::PendingWrite::Completed(n) => {
                        *pending = crate::PendingWrite::Idle;
                        Poll::Ready(Ok(n))
                    }
                }
            }
        }
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        let this = self.as_mut().get_mut();

        this.write_inner.waker.register(cx.waker());

        if let Some(err) = this.write_inner.write_error.lock().unwrap().as_ref() {
            return Poll::Ready(Err(std::io::Error::new(err.kind(), err.to_string())));
        }

        if matches!(
            *this.write_inner.pending.lock().unwrap(),
            crate::PendingWrite::Buffer(_)
        ) {
            return Poll::Pending;
        }

        if this.flush_task.is_none() {
            this.flush_task = Some(this.platform.flush_tx_unblocked());
        }

        let task = this.flush_task.as_mut().expect("flush task");
        match Pin::new(task).poll(cx) {
            Poll::Ready(result) => {
                this.flush_task = None;
                Poll::Ready(result)
            }
            Poll::Pending => Poll::Pending,
        }
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        self.poll_flush(cx)
    }
}