use crate::{builder::BridgeBuilder, connection::Connection, rtu::ModbusRtu, NoDelay};
use embedded_hal::digital::OutputPin;
pub struct Bridge<S, TX, D = NoDelay> {
pub(crate) rtu: ModbusRtu<S, TX>,
pub(crate) rtu_timeout_ms: Option<u32>,
pub(crate) tcp_timeout_ms: Option<u32>,
pub(crate) delay: D,
}
impl<S, TX, D> Bridge<S, TX, D> {
pub fn builder() -> BridgeBuilder<(), (), NoDelay> {
BridgeBuilder::new()
}
pub(crate) fn from_parts(
serial: S,
tx_en: TX,
delay: D,
rtu_timeout_ms: Option<u32>,
tcp_timeout_ms: Option<u32>,
) -> Self {
Self {
rtu: ModbusRtu::new(serial, tx_en),
rtu_timeout_ms,
tcp_timeout_ms,
delay,
}
}
pub fn into_inner(self) -> (S, TX, D) {
let (s, tx) = self.rtu.into_inner();
(s, tx, self.delay)
}
}
#[cfg(feature = "async")]
impl<S, TX, D> Bridge<S, TX, D>
where
S: embedded_io_async::Read + embedded_io_async::Write,
TX: OutputPin,
{
pub fn accept<TS>(&mut self, stream: TS) -> Connection<'_, S, TX, TS, D>
where
TS: embedded_io_async::Read + embedded_io_async::Write,
{
Connection::new(self, stream)
}
}
#[cfg(feature = "sync")]
impl<S, TX, D> Bridge<S, TX, D>
where
S: embedded_io::Read + embedded_io::Write,
TX: OutputPin,
{
pub fn accept<TS>(&mut self, stream: TS) -> Connection<'_, S, TX, TS, D>
where
TS: embedded_io::Read + embedded_io::Write,
{
Connection::new(self, stream)
}
}