pub struct Session<D: SerialDevice + 'static> { /* private fields */ }Expand description
Owns a serial device and a bus, and runs the I/O + command loop.
Session is generic over the device type so tests can substitute a
PTY pair (SerialPortDevice::pair) or, in the future, a fully mocked
backend without dynamic dispatch overhead.
Implementations§
Source§impl<D: SerialDevice + 'static> Session<D>
impl<D: SerialDevice + 'static> Session<D>
Sourcepub fn new(device: D) -> Self
pub fn new(device: D) -> Self
Builds a session with a fresh bus and cancellation token, no-op mappers on both directions.
Sourcepub fn with_bus(device: D, bus: EventBus) -> Self
pub fn with_bus(device: D, bus: EventBus) -> Self
Builds a session attached to a caller-supplied bus. Useful when several subsystems already share a bus and the session should join the existing fan-out instead of starting its own.
Sourcepub fn with_omap<M: Mapper + 'static>(self, mapper: M) -> Self
pub fn with_omap<M: Mapper + 'static>(self, mapper: M) -> Self
Replaces the outbound mapper applied to Event::TxBytes
payloads before they reach the device.
Sourcepub fn with_imap<M: Mapper + 'static>(self, mapper: M) -> Self
pub fn with_imap<M: Mapper + 'static>(self, mapper: M) -> Self
Replaces the inbound mapper applied to bytes read from the
device before they are republished as Event::RxBytes.
Sourcepub const fn with_initial_dtr(self, asserted: bool) -> Self
pub const fn with_initial_dtr(self, asserted: bool) -> Self
Tells the session what the DTR line’s actual state is on the
device. Use this when the caller has already issued a
set_dtr (e.g. main applying --lower-dtr right after
opening the port) so the cached state stays honest and the
first Command::ToggleDtr produces the right transition.
Defaults to true (asserted) — the typical OS state at open.
Sourcepub const fn with_initial_rts(self, asserted: bool) -> Self
pub const fn with_initial_rts(self, asserted: bool) -> Self
Tells the session what the RTS line’s actual state is. See
with_initial_dtr for the rationale.
Sourcepub const fn bus(&self) -> &EventBus
pub const fn bus(&self) -> &EventBus
Returns a reference to the bus. Clone it before calling
Session::run (which consumes self) if you need to publish or
subscribe from outside the session.
Sourcepub fn cancellation_token(&self) -> CancellationToken
pub fn cancellation_token(&self) -> CancellationToken
Returns a clone of the cancellation token.
Triggering CancellationToken::cancel on any clone causes
Session::run to wind down and return.
Sourcepub async fn run(self) -> Result<()>
pub async fn run(self) -> Result<()>
Drives the session to completion.
Subscribes to the bus, publishes Event::DeviceConnected, then
loops until the cancellation token trips or a fatal I/O error
terminates the device.
§Errors
Currently always returns Ok(()); the variant is reserved for
startup failures introduced by later issues (e.g. mapper
initialisation).