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