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).
Sourcepub async fn apply_config(&mut self, new: SerialConfig) -> Result<()>
pub async fn apply_config(&mut self, new: SerialConfig) -> Result<()>
Apply a new SerialConfig to the device atomically.
Applies baud_rate → data_bits → stop_bits → parity → flow_control
in that fixed order. On the first failing step, best-effort-rolls
back the previously-applied steps to the configuration that was
live at entry, returns the Error from the failing
step, and does not publish Event::ConfigChanged. On full
success, publishes Event::ConfigChanged with the device’s
post-apply configuration and returns Ok(()).
Fields whose new value equals the current value still go through the setter call — the backend is free to short-circuit, and keeping the apply sequence uniform avoids branchy rollback state.
This method is async for forward compatibility with backends
whose setters may need to await (e.g. remote devices); the current
serialport backend is synchronous so the body performs no
awaits.
§Errors
Returns the first setter failure encountered. Rollback failures are best-effort and silently swallowed — the device is already in an inconsistent state by that point and surfacing a secondary error would mask the original cause.