Skip to main content

zigbee_host/
port.rs

1//! Open a coordinator's port by name: a serial device, or `tcp://host:port`
2//! for network-attached coordinators and the simulator.
3
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
8
9/// Serial flow control.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum FlowControl {
12    /// None (most USB coordinators with EmberZNet's default firmware).
13    #[default]
14    None,
15    /// RTS/CTS hardware flow control.
16    Hardware,
17    /// XON/XOFF software flow control.
18    Software,
19}
20
21/// An open coordinator port.
22pub enum Port {
23    /// A serial device.
24    Serial(tokio_serial::SerialStream),
25    /// A TCP connection.
26    Tcp(tokio::net::TcpStream),
27}
28
29impl std::fmt::Debug for Port {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        match self {
32            Port::Serial(_) => f.write_str("Port::Serial"),
33            Port::Tcp(_) => f.write_str("Port::Tcp"),
34        }
35    }
36}
37
38/// Open `name`: `tcp://host:port`, or a serial device path at `baud`.
39pub async fn open(name: &str, baud: u32, flow: FlowControl) -> std::io::Result<Port> {
40    if let Some(addr) = name.strip_prefix("tcp://") {
41        let stream = tokio::net::TcpStream::connect(addr).await?;
42        stream.set_nodelay(true)?;
43        return Ok(Port::Tcp(stream));
44    }
45    use tokio_serial::SerialPortBuilderExt;
46    let flow = match flow {
47        FlowControl::None => tokio_serial::FlowControl::None,
48        FlowControl::Hardware => tokio_serial::FlowControl::Hardware,
49        FlowControl::Software => tokio_serial::FlowControl::Software,
50    };
51    let stream = tokio_serial::new(name, baud)
52        .data_bits(tokio_serial::DataBits::Eight)
53        .parity(tokio_serial::Parity::None)
54        .stop_bits(tokio_serial::StopBits::One)
55        .flow_control(flow)
56        .open_native_async()
57        .map_err(std::io::Error::from)?;
58    Ok(Port::Serial(stream))
59}
60
61impl AsyncRead for Port {
62    fn poll_read(
63        self: Pin<&mut Self>,
64        cx: &mut Context<'_>,
65        buf: &mut ReadBuf<'_>,
66    ) -> Poll<std::io::Result<()>> {
67        match self.get_mut() {
68            Port::Serial(s) => Pin::new(s).poll_read(cx, buf),
69            Port::Tcp(s) => Pin::new(s).poll_read(cx, buf),
70        }
71    }
72}
73
74impl AsyncWrite for Port {
75    fn poll_write(
76        self: Pin<&mut Self>,
77        cx: &mut Context<'_>,
78        buf: &[u8],
79    ) -> Poll<std::io::Result<usize>> {
80        match self.get_mut() {
81            Port::Serial(s) => Pin::new(s).poll_write(cx, buf),
82            Port::Tcp(s) => Pin::new(s).poll_write(cx, buf),
83        }
84    }
85    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
86        match self.get_mut() {
87            Port::Serial(s) => Pin::new(s).poll_flush(cx),
88            Port::Tcp(s) => Pin::new(s).poll_flush(cx),
89        }
90    }
91    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
92        match self.get_mut() {
93            Port::Serial(s) => Pin::new(s).poll_shutdown(cx),
94            Port::Tcp(s) => Pin::new(s).poll_shutdown(cx),
95        }
96    }
97}