Skip to main content

mbus_async/client/
mod.rs

1//! Async client module.
2//!
3//! Public entry points:
4//! - [`AsyncTcpClient`] (TCP)
5//! - [`AsyncSerialClient`] (RTU/ASCII)
6//!
7//! # Module layout
8//!
9//! | Module | Contents |
10//! |---|---|
11//! | `command` | `ClientRequest` and `TaskCommand` channel envelopes |
12//! | `response` | `ClientResponse` typed result enum |
13//! | `notifier` | [`AsyncClientTrafficNotifier`] traffic hook trait (`traffic` feature) |
14//! | `client_core` | [`AsyncClientCore`] — public request API |
15//! | `network_client` | [`AsyncTcpClient`] — TCP constructor |
16//! | `serial_client` | [`AsyncSerialClient`] — serial constructor |
17
18pub mod command;
19pub mod decode;
20pub mod encode;
21#[cfg(feature = "traffic")]
22pub mod notifier;
23pub mod response;
24pub(crate) mod task;
25
26mod client_core;
27mod network_client;
28mod serial_client;
29
30pub use client_core::AsyncClientCore;
31pub use command::ClientRequest;
32pub use network_client::AsyncTcpClient;
33#[cfg(feature = "traffic")]
34pub use notifier::AsyncClientTrafficNotifier;
35pub use response::ClientResponse;
36#[cfg(any(feature = "serial-rtu", feature = "serial-ascii"))]
37pub use serial_client::AsyncSerialClientKind;
38pub use serial_client::{AsyncAsciiClient, AsyncRtuClient, AsyncSerialClient};
39
40use mbus_core::errors::MbusError;
41#[cfg(feature = "diagnostics")]
42use mbus_core::function_codes::public::DiagnosticSubFunction;
43#[cfg(feature = "diagnostics")]
44pub use mbus_core::models::diagnostic::{DeviceIdentificationResponse, ObjectId, ReadDeviceIdCode};
45#[cfg(feature = "file-record")]
46pub use mbus_core::models::file_record::{SubRequest, SubRequestParams};
47
48#[cfg(feature = "diagnostics")]
49/// Diagnostics response payload returned by FC 08.
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct DiagnosticsDataResponse {
52    /// Echoed diagnostic sub-function code.
53    pub sub_function: DiagnosticSubFunction,
54    /// Echoed diagnostic data words.
55    pub data: Vec<u16>,
56}
57#[cfg(feature = "diagnostics")]
58/// Communication event log payload `(status, event_count, message_count, events)` returned by FC 12.
59pub type CommEventLogResponse = (u16, u16, u16, Vec<u8>);
60
61/// Async facade error type.
62#[derive(Debug, PartialEq, Eq)]
63pub enum AsyncError {
64    /// Error propagated from the underlying Modbus client stack.
65    Mbus(MbusError),
66    /// Background worker channel is closed or worker thread has stopped.
67    WorkerClosed,
68    /// Internal response routing mismatch between request and callback payload type.
69    UnexpectedResponseType,
70    /// Per-request timeout elapsed before the server responded.
71    ///
72    /// Set via [`AsyncClientCore::set_request_timeout`].  The in-flight entry
73    /// remains in the background task until the transport delivers or errors;
74    /// call [`connect`](AsyncClientCore::connect) to reset transport state.
75    ///
76    /// [`AsyncClientCore::set_request_timeout`]: crate::client::AsyncClientCore::set_request_timeout
77    /// [`connect`]: crate::client::AsyncClientCore::connect
78    Timeout,
79}
80
81impl From<MbusError> for AsyncError {
82    fn from(value: MbusError) -> Self {
83        Self::Mbus(value)
84    }
85}
86
87impl core::fmt::Display for AsyncError {
88    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
89        match self {
90            Self::Mbus(err) => write!(f, "Modbus error: {err:?}"),
91            Self::WorkerClosed => write!(f, "async worker channel closed"),
92            Self::UnexpectedResponseType => write!(f, "unexpected response type from worker"),
93            Self::Timeout => write!(f, "request timed out"),
94        }
95    }
96}
97
98impl std::error::Error for AsyncError {}