mabi-modbus 1.6.1

Mabinogion - Modbus TCP/RTU simulator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! Transport abstraction for Modbus RTU.
//!
//! This module provides a unified transport interface that abstracts over
//! different communication mechanisms (virtual serial, TCP bridge, etc.).
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                   RtuTransport Trait                        │
//! │                                                              │
//! │  ┌─────────────────┐ ┌──────────────┐ ┌─────────────────┐  │
//! │  │ VirtualSerial   │ │ TcpBridge    │ │ ChannelTransport│  │
//! │  │ (PTY-based)     │ │ (TCP→RTU)    │ │ (Testing)       │  │
//! │  └─────────────────┘ └──────────────┘ └─────────────────┘  │
//! └─────────────────────────────────────────────────────────────┘
//! ```

use std::io;
use std::net::SocketAddr;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;

use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::mpsc;

use super::serial::{SerialConfig, VirtualSerial, VirtualSerialConfig};

/// Transport configuration types.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum TransportConfig {
    /// Virtual serial port (PTY on Unix).
    VirtualSerial(VirtualSerialConfig),

    /// TCP bridge (TCP connections translated to RTU).
    TcpBridge(TcpBridgeConfig),

    /// Channel-based transport (for testing).
    Channel(ChannelConfig),
}

impl Default for TransportConfig {
    fn default() -> Self {
        Self::VirtualSerial(VirtualSerialConfig::default())
    }
}

/// TCP bridge configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TcpBridgeConfig {
    /// Address to bind the TCP server to.
    pub bind_address: SocketAddr,

    /// Serial configuration for timing.
    pub serial: SerialConfig,

    /// Maximum concurrent connections.
    pub max_connections: usize,

    /// Connection timeout.
    pub connection_timeout: Duration,

    /// Whether to strip TCP-specific headers.
    pub raw_mode: bool,
}

impl Default for TcpBridgeConfig {
    fn default() -> Self {
        Self {
            bind_address: "0.0.0.0:5020".parse().unwrap(),
            serial: SerialConfig::default(),
            max_connections: 10,
            connection_timeout: Duration::from_secs(60),
            raw_mode: true,
        }
    }
}

/// Channel-based transport configuration (for testing).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelConfig {
    /// Channel buffer size.
    pub buffer_size: usize,

    /// Serial configuration for timing simulation.
    pub serial: SerialConfig,

    /// Simulate transmission delays.
    pub simulate_delays: bool,
}

impl Default for ChannelConfig {
    fn default() -> Self {
        Self {
            buffer_size: 256,
            serial: SerialConfig::default(),
            simulate_delays: false,
        }
    }
}

/// Transport type identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TransportType {
    /// Virtual serial port.
    VirtualSerial,
    /// TCP bridge.
    TcpBridge,
    /// Channel (testing).
    Channel,
}

/// Trait for RTU transport implementations.
///
/// This trait abstracts over the underlying transport mechanism,
/// allowing the RTU server to work with different transports interchangeably.
#[async_trait]
pub trait RtuTransport: Send + Sync {
    /// Get the transport type.
    fn transport_type(&self) -> TransportType;

    /// Check if the transport is connected/ready.
    fn is_ready(&self) -> bool;

    /// Read data from the transport.
    ///
    /// Returns the number of bytes read, or 0 if no data is available.
    async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>;

    /// Write data to the transport.
    ///
    /// Returns the number of bytes written.
    async fn write(&mut self, data: &[u8]) -> io::Result<usize>;

    /// Flush any buffered data.
    async fn flush(&mut self) -> io::Result<()>;

    /// Get the serial configuration for timing.
    fn serial_config(&self) -> &SerialConfig;

    /// Get the transmission delay for a given number of bytes.
    fn transmission_delay(&self, bytes: usize) -> Duration {
        self.serial_config().transmission_time(bytes)
    }

    /// Get the inter-frame timeout.
    fn inter_frame_timeout(&self) -> Duration {
        self.serial_config().inter_frame_timeout()
    }

    /// Close the transport.
    async fn close(&mut self) -> io::Result<()>;
}

/// Channel-based transport for testing.
///
/// Uses tokio channels to simulate a bidirectional transport.
pub struct ChannelTransport {
    /// Receive channel.
    rx: mpsc::Receiver<Bytes>,

    /// Send channel.
    tx: mpsc::Sender<Bytes>,

    /// Configuration.
    config: ChannelConfig,

    /// Read buffer for partial reads.
    read_buffer: BytesMut,
}

impl ChannelTransport {
    /// Create a connected pair of channel transports.
    pub fn pair(config: ChannelConfig) -> (Self, Self) {
        let (tx1, rx1) = mpsc::channel(config.buffer_size);
        let (tx2, rx2) = mpsc::channel(config.buffer_size);

        let transport1 = Self {
            rx: rx1,
            tx: tx2,
            config: config.clone(),
            read_buffer: BytesMut::new(),
        };

        let transport2 = Self {
            rx: rx2,
            tx: tx1,
            config,
            read_buffer: BytesMut::new(),
        };

        (transport1, transport2)
    }

    /// Create from existing channels.
    pub fn new(tx: mpsc::Sender<Bytes>, rx: mpsc::Receiver<Bytes>, config: ChannelConfig) -> Self {
        Self {
            rx,
            tx,
            config,
            read_buffer: BytesMut::new(),
        }
    }
}

#[async_trait]
impl RtuTransport for ChannelTransport {
    fn transport_type(&self) -> TransportType {
        TransportType::Channel
    }

    fn is_ready(&self) -> bool {
        !self.tx.is_closed()
    }

    async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        // First, try to satisfy from buffer
        if !self.read_buffer.is_empty() {
            let len = std::cmp::min(buf.len(), self.read_buffer.len());
            buf[..len].copy_from_slice(&self.read_buffer.split_to(len));
            return Ok(len);
        }

        // Read from channel
        match self.rx.recv().await {
            Some(data) => {
                let len = std::cmp::min(buf.len(), data.len());
                buf[..len].copy_from_slice(&data[..len]);

                // Buffer remaining data
                if data.len() > len {
                    self.read_buffer.extend_from_slice(&data[len..]);
                }

                Ok(len)
            }
            None => Ok(0), // Channel closed
        }
    }

    async fn write(&mut self, data: &[u8]) -> io::Result<usize> {
        // Simulate transmission delay if enabled
        if self.config.simulate_delays {
            let delay = self.config.serial.transmission_time(data.len());
            tokio::time::sleep(delay).await;
        }

        self.tx
            .send(Bytes::copy_from_slice(data))
            .await
            .map_err(|_| io::Error::new(io::ErrorKind::BrokenPipe, "Channel closed"))?;

        Ok(data.len())
    }

    async fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }

    fn serial_config(&self) -> &SerialConfig {
        &self.config.serial
    }

    async fn close(&mut self) -> io::Result<()> {
        // Channels close automatically when dropped
        Ok(())
    }
}

#[cfg(unix)]
pub struct VirtualSerialTransport {
    io: tokio::fs::File,
    config: VirtualSerialConfig,
}

#[cfg(unix)]
impl VirtualSerialTransport {
    fn new(io: tokio::fs::File, config: VirtualSerialConfig) -> Self {
        Self { io, config }
    }
}

#[cfg(unix)]
#[async_trait]
impl RtuTransport for VirtualSerialTransport {
    fn transport_type(&self) -> TransportType {
        TransportType::VirtualSerial
    }

    fn is_ready(&self) -> bool {
        true
    }

    async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.io.read(buf).await
    }

    async fn write(&mut self, data: &[u8]) -> io::Result<usize> {
        if self.config.simulate_delays {
            tokio::time::sleep(self.config.serial.transmission_time(data.len())).await;
        }
        self.io.write(data).await
    }

    async fn flush(&mut self) -> io::Result<()> {
        self.io.flush().await
    }

    fn serial_config(&self) -> &SerialConfig {
        &self.config.serial
    }

    async fn close(&mut self) -> io::Result<()> {
        self.io.flush().await
    }
}

pub struct TcpBridgeTransport {
    listener: TcpListener,
    stream: Option<TcpStream>,
    config: TcpBridgeConfig,
}

impl TcpBridgeTransport {
    pub async fn bind(config: TcpBridgeConfig) -> io::Result<Self> {
        let listener = TcpListener::bind(config.bind_address).await?;
        Ok(Self {
            listener,
            stream: None,
            config,
        })
    }

    async fn ensure_stream(&mut self) -> io::Result<&mut TcpStream> {
        if self.stream.is_none() {
            let (stream, _) = self.listener.accept().await?;
            self.stream = Some(stream);
        }
        Ok(self.stream.as_mut().expect("stream initialized"))
    }
}

#[async_trait]
impl RtuTransport for TcpBridgeTransport {
    fn transport_type(&self) -> TransportType {
        TransportType::TcpBridge
    }

    fn is_ready(&self) -> bool {
        true
    }

    async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let result = {
            let stream = self.ensure_stream().await?;
            stream.read(buf).await
        };
        match result {
            Ok(0) => {
                self.stream = None;
                Ok(0)
            }
            other => other,
        }
    }

    async fn write(&mut self, data: &[u8]) -> io::Result<usize> {
        let stream = self.ensure_stream().await?;
        stream.write(data).await
    }

    async fn flush(&mut self) -> io::Result<()> {
        if let Some(stream) = &mut self.stream {
            stream.flush().await
        } else {
            Ok(())
        }
    }

    fn serial_config(&self) -> &SerialConfig {
        &self.config.serial
    }

    async fn close(&mut self) -> io::Result<()> {
        self.stream.take();
        Ok(())
    }
}

/// Async I/O wrapper that implements AsyncRead and AsyncWrite.
///
/// This allows using an RtuTransport with tokio's framed codecs.
pub struct TransportIo<T: RtuTransport> {
    transport: T,
    read_buffer: BytesMut,
}

impl<T: RtuTransport> TransportIo<T> {
    /// Create a new TransportIo wrapper.
    pub fn new(transport: T) -> Self {
        Self {
            transport,
            read_buffer: BytesMut::with_capacity(256),
        }
    }

    /// Get a reference to the underlying transport.
    pub fn transport(&self) -> &T {
        &self.transport
    }

    /// Get a mutable reference to the underlying transport.
    pub fn transport_mut(&mut self) -> &mut T {
        &mut self.transport
    }

    /// Consume and return the underlying transport.
    pub fn into_inner(self) -> T {
        self.transport
    }
}

impl<T: RtuTransport + Unpin> AsyncRead for TransportIo<T> {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        // This is a simplified implementation that uses poll_fn
        // In production, you'd want a proper async implementation
        let this = self.get_mut();

        // Try to read from buffer first
        if !this.read_buffer.is_empty() {
            let len = std::cmp::min(buf.remaining(), this.read_buffer.len());
            buf.put_slice(&this.read_buffer.split_to(len));
            return Poll::Ready(Ok(()));
        }

        // We need to convert the async read to a poll-based one
        // This is a simplified version - in production, use a proper async adapter
        cx.waker().wake_by_ref();
        Poll::Pending
    }
}

impl<T: RtuTransport + Unpin> AsyncWrite for TransportIo<T> {
    fn poll_write(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        _buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        // Simplified implementation
        Poll::Pending
    }

    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }

    fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }
}

/// Transport factory for creating transports from configuration.
pub struct TransportFactory;

impl TransportFactory {
    /// Create a transport from configuration.
    ///
    /// Returns a boxed trait object for dynamic dispatch.
    pub async fn create(config: TransportConfig) -> io::Result<Box<dyn RtuTransport>> {
        match config {
            TransportConfig::Channel(cfg) => {
                let (transport, _peer) = ChannelTransport::pair(cfg);
                Ok(Box::new(transport))
            }
            TransportConfig::VirtualSerial(cfg) => {
                #[cfg(unix)]
                {
                    let serial = VirtualSerial::create(cfg.clone())
                        .map_err(|error| io::Error::new(io::ErrorKind::Other, error.to_string()))?;
                    let io = serial.into_async_io()?;
                    Ok(Box::new(VirtualSerialTransport::new(io, cfg)))
                }
                #[cfg(not(unix))]
                {
                    let _ = cfg;
                    Err(io::Error::new(
                        io::ErrorKind::Unsupported,
                        "virtual serial transport is not available on this platform",
                    ))
                }
            }
            TransportConfig::TcpBridge(cfg) => {
                let transport = TcpBridgeTransport::bind(cfg).await?;
                Ok(Box::new(transport))
            }
        }
    }
}

/// Transport metrics.
#[derive(Debug, Clone, Default)]
pub struct TransportMetrics {
    /// Total bytes received.
    pub bytes_received: u64,

    /// Total bytes sent.
    pub bytes_sent: u64,

    /// Total frames received.
    pub frames_received: u64,

    /// Total frames sent.
    pub frames_sent: u64,

    /// CRC errors.
    pub crc_errors: u64,

    /// Framing errors.
    pub framing_errors: u64,

    /// Timeouts.
    pub timeouts: u64,
}

impl TransportMetrics {
    /// Create new metrics.
    pub fn new() -> Self {
        Self::default()
    }

    /// Record bytes received.
    pub fn record_bytes_received(&mut self, bytes: usize) {
        self.bytes_received += bytes as u64;
    }

    /// Record bytes sent.
    pub fn record_bytes_sent(&mut self, bytes: usize) {
        self.bytes_sent += bytes as u64;
    }

    /// Record frame received.
    pub fn record_frame_received(&mut self) {
        self.frames_received += 1;
    }

    /// Record frame sent.
    pub fn record_frame_sent(&mut self) {
        self.frames_sent += 1;
    }

    /// Record CRC error.
    pub fn record_crc_error(&mut self) {
        self.crc_errors += 1;
    }

    /// Record framing error.
    pub fn record_framing_error(&mut self) {
        self.framing_errors += 1;
    }

    /// Record timeout.
    pub fn record_timeout(&mut self) {
        self.timeouts += 1;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_channel_transport_pair() {
        let config = ChannelConfig::default();
        let (mut transport1, mut transport2) = ChannelTransport::pair(config);

        // Transport 1 writes, Transport 2 reads
        let data = b"Hello, RTU!";
        transport1.write(data).await.unwrap();

        let mut buf = [0u8; 32];
        let n = transport2.read(&mut buf).await.unwrap();

        assert_eq!(&buf[..n], data);
    }

    #[tokio::test]
    async fn test_channel_transport_bidirectional() {
        let config = ChannelConfig::default();
        let (mut server, mut client) = ChannelTransport::pair(config);

        // Client sends request
        let request = [0x01, 0x03, 0x00, 0x00, 0x00, 0x0A];
        client.write(&request).await.unwrap();

        // Server receives
        let mut buf = [0u8; 32];
        let n = server.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], &request);

        // Server sends response
        let response = [0x01, 0x03, 0x14];
        server.write(&response).await.unwrap();

        // Client receives
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], &response);
    }

    #[test]
    fn test_transport_metrics() {
        let mut metrics = TransportMetrics::new();

        metrics.record_bytes_received(100);
        metrics.record_bytes_sent(50);
        metrics.record_frame_received();
        metrics.record_crc_error();

        assert_eq!(metrics.bytes_received, 100);
        assert_eq!(metrics.bytes_sent, 50);
        assert_eq!(metrics.frames_received, 1);
        assert_eq!(metrics.crc_errors, 1);
    }

    #[test]
    fn test_tcp_bridge_config_default() {
        let config = TcpBridgeConfig::default();
        assert_eq!(config.bind_address.port(), 5020);
        assert_eq!(config.max_connections, 10);
        assert!(config.raw_mode);
    }
}