mabi-modbus 1.4.0

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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
//! Multi-unit manager implementation.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Instant;

use dashmap::DashMap;
use tracing::{debug, instrument, trace, warn};

use crate::error::{ModbusError, ModbusResult};
use crate::registers::SparseRegisterStore;
use crate::types::{RegisterConverter, WordOrder};

use super::config::{BroadcastMode, UnitConfig, UnitManagerConfig};

/// Information about a managed Modbus unit.
#[derive(Debug)]
pub struct UnitInfo {
    /// Unit ID (1-247).
    unit_id: u8,

    /// Unit configuration.
    config: UnitConfig,

    /// Register store for this unit.
    registers: Arc<SparseRegisterStore>,

    /// Value converter for this unit.
    converter: RegisterConverter,

    /// Creation timestamp.
    created_at: Instant,

    /// Read request counter.
    read_count: AtomicU64,

    /// Write request counter.
    write_count: AtomicU64,

    /// Error counter.
    error_count: AtomicU64,
}

impl UnitInfo {
    /// Create a new unit info.
    fn new(
        unit_id: u8,
        config: UnitConfig,
        default_word_order: WordOrder,
        default_register_config: &crate::registers::RegisterStoreConfig,
    ) -> Self {
        let word_order = config.effective_word_order(default_word_order);
        let register_config = config
            .register_config
            .clone()
            .unwrap_or_else(|| default_register_config.clone());

        Self {
            unit_id,
            config,
            registers: Arc::new(SparseRegisterStore::new(register_config)),
            converter: RegisterConverter::new(word_order),
            created_at: Instant::now(),
            read_count: AtomicU64::new(0),
            write_count: AtomicU64::new(0),
            error_count: AtomicU64::new(0),
        }
    }

    /// Get the unit ID.
    #[inline]
    pub fn unit_id(&self) -> u8 {
        self.unit_id
    }

    /// Get the unit configuration.
    #[inline]
    pub fn config(&self) -> &UnitConfig {
        &self.config
    }

    /// Get the unit name.
    #[inline]
    pub fn name(&self) -> &str {
        &self.config.name
    }

    /// Get the register store.
    #[inline]
    pub fn registers(&self) -> &Arc<SparseRegisterStore> {
        &self.registers
    }

    /// Get the value converter.
    #[inline]
    pub fn converter(&self) -> &RegisterConverter {
        &self.converter
    }

    /// Get the word order for this unit.
    #[inline]
    pub fn word_order(&self) -> WordOrder {
        self.converter.word_order()
    }

    /// Check if this unit is enabled.
    #[inline]
    pub fn is_enabled(&self) -> bool {
        self.config.enabled
    }

    /// Check if this unit responds to broadcasts.
    #[inline]
    pub fn broadcast_enabled(&self) -> bool {
        self.config.broadcast_enabled
    }

    /// Get the response delay in microseconds.
    #[inline]
    pub fn response_delay_us(&self) -> u64 {
        self.config.response_delay_us
    }

    /// Get the creation timestamp.
    #[inline]
    pub fn created_at(&self) -> Instant {
        self.created_at
    }

    /// Get the uptime duration.
    #[inline]
    pub fn uptime(&self) -> std::time::Duration {
        self.created_at.elapsed()
    }

    /// Get the read count.
    #[inline]
    pub fn read_count(&self) -> u64 {
        self.read_count.load(Ordering::Relaxed)
    }

    /// Get the write count.
    #[inline]
    pub fn write_count(&self) -> u64 {
        self.write_count.load(Ordering::Relaxed)
    }

    /// Get the error count.
    #[inline]
    pub fn error_count(&self) -> u64 {
        self.error_count.load(Ordering::Relaxed)
    }

    /// Record a read operation.
    pub(crate) fn record_read(&self) {
        self.read_count.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a write operation.
    pub(crate) fn record_write(&self) {
        self.write_count.fetch_add(1, Ordering::Relaxed);
    }

    /// Record an error.
    pub(crate) fn record_error(&self) {
        self.error_count.fetch_add(1, Ordering::Relaxed);
    }
}

/// Manager for multiple Modbus units.
///
/// Provides centralized management of multiple Modbus slave units, each with
/// its own register store, word order configuration, and statistics.
///
/// # Thread Safety
///
/// `MultiUnitManager` is thread-safe and can be shared across threads.
/// All operations are lock-free using DashMap.
///
/// # Example
///
/// ```rust
/// use mabi_modbus::unit::{MultiUnitManager, UnitConfig, UnitManagerConfig};
///
/// let manager = MultiUnitManager::new(UnitManagerConfig::default());
///
/// // Add units
/// manager.add_unit(1, UnitConfig::new("Unit 1")).unwrap();
/// manager.add_unit(2, UnitConfig::new("Unit 2")).unwrap();
///
/// // Access unit
/// {
///     let unit = manager.get_unit(1).unwrap();
///     assert_eq!(unit.name(), "Unit 1");
/// }
/// ```
pub struct MultiUnitManager {
    /// Manager configuration.
    config: UnitManagerConfig,

    /// Units by ID.
    units: DashMap<u8, UnitInfo>,

    /// Total request counter.
    total_requests: AtomicU64,

    /// Total broadcast counter.
    broadcast_count: AtomicU64,
}

impl MultiUnitManager {
    /// Create a new multi-unit manager.
    pub fn new(config: UnitManagerConfig) -> Self {
        Self {
            config,
            units: DashMap::new(),
            total_requests: AtomicU64::new(0),
            broadcast_count: AtomicU64::new(0),
        }
    }

    /// Create with default configuration.
    pub fn with_defaults() -> Self {
        Self::new(UnitManagerConfig::default())
    }

    /// Get the manager configuration.
    pub fn config(&self) -> &UnitManagerConfig {
        &self.config
    }

    /// Get the number of registered units.
    pub fn unit_count(&self) -> usize {
        self.units.len()
    }

    /// Get all unit IDs.
    pub fn unit_ids(&self) -> Vec<u8> {
        self.units.iter().map(|entry| *entry.key()).collect()
    }

    /// Check if a unit exists.
    pub fn has_unit(&self, unit_id: u8) -> bool {
        self.units.contains_key(&unit_id)
    }

    // =========================================================================
    // Unit Management
    // =========================================================================

    /// Add a new unit.
    ///
    /// # Arguments
    ///
    /// * `unit_id` - Unit ID (1-247)
    /// * `config` - Unit configuration
    ///
    /// # Returns
    ///
    /// `Ok(())` if the unit was added, `Err` if:
    /// - Unit ID is 0 (reserved for broadcast)
    /// - Unit already exists
    /// - Maximum units reached
    #[instrument(skip(self, config), fields(unit_id = unit_id, name = %config.name))]
    pub fn add_unit(&self, unit_id: u8, config: UnitConfig) -> ModbusResult<()> {
        // Validate unit ID
        if unit_id == 0 {
            return Err(ModbusError::InvalidUnitId {
                unit_id: 0,
                reason: "Unit ID 0 is reserved for broadcast".to_string(),
            });
        }

        // Check max units
        if self.units.len() >= self.config.max_units {
            return Err(ModbusError::UnitLimitReached {
                max: self.config.max_units,
            });
        }

        // Check if already exists
        if self.units.contains_key(&unit_id) {
            return Err(ModbusError::UnitAlreadyExists { unit_id });
        }

        let unit_info = UnitInfo::new(
            unit_id,
            config,
            self.config.default_word_order,
            &self.config.default_register_config,
        );

        self.units.insert(unit_id, unit_info);
        debug!(unit_id, "Unit added");

        Ok(())
    }

    /// Remove a unit.
    ///
    /// # Returns
    ///
    /// The removed unit info if it existed.
    #[instrument(skip(self))]
    pub fn remove_unit(&self, unit_id: u8) -> Option<UnitInfo> {
        let removed = self.units.remove(&unit_id).map(|(_, info)| info);
        if removed.is_some() {
            debug!(unit_id, "Unit removed");
        }
        removed
    }

    /// Get a unit by ID.
    ///
    /// If `auto_create_units` is enabled and the unit doesn't exist,
    /// it will be created with default configuration.
    pub fn get_unit(&self, unit_id: u8) -> Option<dashmap::mapref::one::Ref<'_, u8, UnitInfo>> {
        // Never auto-create unit 0
        if unit_id == 0 {
            return None;
        }

        // Try to get existing unit
        if let Some(unit) = self.units.get(&unit_id) {
            return Some(unit);
        }

        // Auto-create if enabled
        if self.config.auto_create_units {
            let config = UnitConfig::new(format!("Auto-created Unit {}", unit_id));
            if self.add_unit(unit_id, config).is_ok() {
                return self.units.get(&unit_id);
            }
        }

        None
    }

    /// Get a mutable reference to a unit.
    pub fn get_unit_mut(
        &self,
        unit_id: u8,
    ) -> Option<dashmap::mapref::one::RefMut<'_, u8, UnitInfo>> {
        if unit_id == 0 {
            return None;
        }

        self.units.get_mut(&unit_id)
    }

    /// Update a unit's configuration.
    #[instrument(skip(self, update_fn))]
    pub fn update_unit<F>(&self, unit_id: u8, update_fn: F) -> ModbusResult<()>
    where
        F: FnOnce(&mut UnitConfig),
    {
        if let Some(mut unit) = self.units.get_mut(&unit_id) {
            update_fn(&mut unit.config);
            debug!(unit_id, "Unit configuration updated");
            Ok(())
        } else {
            Err(ModbusError::UnitNotFound { unit_id })
        }
    }

    // =========================================================================
    // Register Operations with Unit Selection
    // =========================================================================

    /// Read holding registers from a unit.
    #[instrument(skip(self), level = "trace")]
    pub fn read_holding_registers(
        &self,
        unit_id: u8,
        address: u16,
        quantity: u16,
    ) -> ModbusResult<Vec<u16>> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        let unit = self
            .get_unit(unit_id)
            .ok_or(ModbusError::UnitNotFound { unit_id })?;

        if !unit.is_enabled() {
            unit.record_error();
            return Err(ModbusError::UnitDisabled { unit_id });
        }

        unit.record_read();
        unit.registers().read_holding_registers(address, quantity)
    }

    /// Read input registers from a unit.
    #[instrument(skip(self), level = "trace")]
    pub fn read_input_registers(
        &self,
        unit_id: u8,
        address: u16,
        quantity: u16,
    ) -> ModbusResult<Vec<u16>> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        let unit = self
            .get_unit(unit_id)
            .ok_or(ModbusError::UnitNotFound { unit_id })?;

        if !unit.is_enabled() {
            unit.record_error();
            return Err(ModbusError::UnitDisabled { unit_id });
        }

        unit.record_read();
        unit.registers().read_input_registers(address, quantity)
    }

    /// Read coils from a unit.
    #[instrument(skip(self), level = "trace")]
    pub fn read_coils(&self, unit_id: u8, address: u16, quantity: u16) -> ModbusResult<Vec<bool>> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        let unit = self
            .get_unit(unit_id)
            .ok_or(ModbusError::UnitNotFound { unit_id })?;

        if !unit.is_enabled() {
            unit.record_error();
            return Err(ModbusError::UnitDisabled { unit_id });
        }

        unit.record_read();
        unit.registers().read_coils(address, quantity)
    }

    /// Read discrete inputs from a unit.
    #[instrument(skip(self), level = "trace")]
    pub fn read_discrete_inputs(
        &self,
        unit_id: u8,
        address: u16,
        quantity: u16,
    ) -> ModbusResult<Vec<bool>> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        let unit = self
            .get_unit(unit_id)
            .ok_or(ModbusError::UnitNotFound { unit_id })?;

        if !unit.is_enabled() {
            unit.record_error();
            return Err(ModbusError::UnitDisabled { unit_id });
        }

        unit.record_read();
        unit.registers().read_discrete_inputs(address, quantity)
    }

    /// Write a single holding register to a unit.
    #[instrument(skip(self), level = "trace")]
    pub fn write_holding_register(
        &self,
        unit_id: u8,
        address: u16,
        value: u16,
    ) -> ModbusResult<()> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        // Handle broadcast
        if unit_id == 0 {
            return self.broadcast_write_holding_register(address, value);
        }

        let unit = self
            .get_unit(unit_id)
            .ok_or(ModbusError::UnitNotFound { unit_id })?;

        if !unit.is_enabled() {
            unit.record_error();
            return Err(ModbusError::UnitDisabled { unit_id });
        }

        unit.record_write();
        unit.registers().write_holding_register(address, value)
    }

    /// Write multiple holding registers to a unit.
    #[instrument(skip(self, values), level = "trace")]
    pub fn write_holding_registers(
        &self,
        unit_id: u8,
        address: u16,
        values: &[u16],
    ) -> ModbusResult<()> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        // Handle broadcast
        if unit_id == 0 {
            return self.broadcast_write_holding_registers(address, values);
        }

        let unit = self
            .get_unit(unit_id)
            .ok_or(ModbusError::UnitNotFound { unit_id })?;

        if !unit.is_enabled() {
            unit.record_error();
            return Err(ModbusError::UnitDisabled { unit_id });
        }

        unit.record_write();
        unit.registers().write_holding_registers(address, values)
    }

    /// Write a single coil to a unit.
    #[instrument(skip(self), level = "trace")]
    pub fn write_coil(&self, unit_id: u8, address: u16, value: bool) -> ModbusResult<()> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        // Handle broadcast
        if unit_id == 0 {
            return self.broadcast_write_coil(address, value);
        }

        let unit = self
            .get_unit(unit_id)
            .ok_or(ModbusError::UnitNotFound { unit_id })?;

        if !unit.is_enabled() {
            unit.record_error();
            return Err(ModbusError::UnitDisabled { unit_id });
        }

        unit.record_write();
        unit.registers().write_coil(address, value)
    }

    /// Write multiple coils to a unit.
    #[instrument(skip(self, values), level = "trace")]
    pub fn write_coils(&self, unit_id: u8, address: u16, values: &[bool]) -> ModbusResult<()> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        // Handle broadcast
        if unit_id == 0 {
            return self.broadcast_write_coils(address, values);
        }

        let unit = self
            .get_unit(unit_id)
            .ok_or(ModbusError::UnitNotFound { unit_id })?;

        if !unit.is_enabled() {
            unit.record_error();
            return Err(ModbusError::UnitDisabled { unit_id });
        }

        unit.record_write();
        unit.registers().write_coils(address, values)
    }

    // =========================================================================
    // Broadcast Operations
    // =========================================================================

    /// Broadcast write holding register to all applicable units.
    #[instrument(skip(self), level = "debug")]
    pub fn broadcast_write_holding_register(&self, address: u16, value: u16) -> ModbusResult<()> {
        self.broadcast_count.fetch_add(1, Ordering::Relaxed);
        let units = self.get_broadcast_targets();

        trace!(
            address,
            value,
            unit_count = units.len(),
            "Broadcasting write holding register"
        );

        for unit_id in units {
            if let Some(unit) = self.units.get(&unit_id) {
                if unit.is_enabled() && unit.broadcast_enabled() {
                    let _ = unit.registers().write_holding_register(address, value);
                    unit.record_write();
                }
            }
        }

        Ok(())
    }

    /// Broadcast write multiple holding registers to all applicable units.
    #[instrument(skip(self, values), level = "debug")]
    pub fn broadcast_write_holding_registers(
        &self,
        address: u16,
        values: &[u16],
    ) -> ModbusResult<()> {
        self.broadcast_count.fetch_add(1, Ordering::Relaxed);
        let units = self.get_broadcast_targets();

        trace!(
            address,
            count = values.len(),
            unit_count = units.len(),
            "Broadcasting write multiple holding registers"
        );

        for unit_id in units {
            if let Some(unit) = self.units.get(&unit_id) {
                if unit.is_enabled() && unit.broadcast_enabled() {
                    let _ = unit.registers().write_holding_registers(address, values);
                    unit.record_write();
                }
            }
        }

        Ok(())
    }

    /// Broadcast write coil to all applicable units.
    #[instrument(skip(self), level = "debug")]
    pub fn broadcast_write_coil(&self, address: u16, value: bool) -> ModbusResult<()> {
        self.broadcast_count.fetch_add(1, Ordering::Relaxed);
        let units = self.get_broadcast_targets();

        trace!(
            address,
            value,
            unit_count = units.len(),
            "Broadcasting write coil"
        );

        for unit_id in units {
            if let Some(unit) = self.units.get(&unit_id) {
                if unit.is_enabled() && unit.broadcast_enabled() {
                    let _ = unit.registers().write_coil(address, value);
                    unit.record_write();
                }
            }
        }

        Ok(())
    }

    /// Broadcast write multiple coils to all applicable units.
    #[instrument(skip(self, values), level = "debug")]
    pub fn broadcast_write_coils(&self, address: u16, values: &[bool]) -> ModbusResult<()> {
        self.broadcast_count.fetch_add(1, Ordering::Relaxed);
        let units = self.get_broadcast_targets();

        trace!(
            address,
            count = values.len(),
            unit_count = units.len(),
            "Broadcasting write multiple coils"
        );

        for unit_id in units {
            if let Some(unit) = self.units.get(&unit_id) {
                if unit.is_enabled() && unit.broadcast_enabled() {
                    let _ = unit.registers().write_coils(address, values);
                    unit.record_write();
                }
            }
        }

        Ok(())
    }

    /// Get the list of units that should receive broadcast messages.
    fn get_broadcast_targets(&self) -> Vec<u8> {
        match &self.config.broadcast_mode {
            BroadcastMode::WriteAll => self.unit_ids(),
            BroadcastMode::Disabled => vec![],
            BroadcastMode::SelectiveList(units) => units.clone(),
            BroadcastMode::EchoToUnit(id) => vec![*id],
        }
    }

    // =========================================================================
    // Statistics
    // =========================================================================

    /// Get total request count.
    pub fn total_requests(&self) -> u64 {
        self.total_requests.load(Ordering::Relaxed)
    }

    /// Get total broadcast count.
    pub fn broadcast_count(&self) -> u64 {
        self.broadcast_count.load(Ordering::Relaxed)
    }

    /// Get statistics for all units.
    pub fn unit_statistics(&self) -> Vec<UnitStatistics> {
        self.units
            .iter()
            .map(|entry| UnitStatistics {
                unit_id: *entry.key(),
                name: entry.value().config.name.clone(),
                enabled: entry.value().is_enabled(),
                read_count: entry.value().read_count(),
                write_count: entry.value().write_count(),
                error_count: entry.value().error_count(),
                register_count: entry.value().registers().entry_count(),
            })
            .collect()
    }

    /// Reset all statistics.
    pub fn reset_statistics(&self) {
        self.total_requests.store(0, Ordering::Relaxed);
        self.broadcast_count.store(0, Ordering::Relaxed);

        for entry in self.units.iter() {
            entry.value().read_count.store(0, Ordering::Relaxed);
            entry.value().write_count.store(0, Ordering::Relaxed);
            entry.value().error_count.store(0, Ordering::Relaxed);
        }
    }
}

impl Default for MultiUnitManager {
    fn default() -> Self {
        Self::with_defaults()
    }
}

impl std::fmt::Debug for MultiUnitManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MultiUnitManager")
            .field("unit_count", &self.unit_count())
            .field("total_requests", &self.total_requests())
            .field("broadcast_count", &self.broadcast_count())
            .finish()
    }
}

/// Statistics for a single unit.
#[derive(Debug, Clone)]
pub struct UnitStatistics {
    pub unit_id: u8,
    pub name: String,
    pub enabled: bool,
    pub read_count: u64,
    pub write_count: u64,
    pub error_count: u64,
    pub register_count: usize,
}

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

    #[test]
    fn test_create_manager() {
        let manager = MultiUnitManager::with_defaults();
        assert_eq!(manager.unit_count(), 0);
    }

    #[test]
    fn test_add_and_get_unit() {
        let manager = MultiUnitManager::with_defaults();

        manager.add_unit(1, UnitConfig::new("Test Unit")).unwrap();

        assert!(manager.has_unit(1));
        assert!(!manager.has_unit(2));

        let unit = manager.get_unit(1).unwrap();
        assert_eq!(unit.name(), "Test Unit");
    }

    #[test]
    fn test_cannot_add_unit_zero() {
        let manager = MultiUnitManager::with_defaults();

        let result = manager.add_unit(0, UnitConfig::new("Broadcast"));
        assert!(result.is_err());
    }

    #[test]
    fn test_cannot_add_duplicate_unit() {
        let manager = MultiUnitManager::with_defaults();

        manager.add_unit(1, UnitConfig::new("Unit 1")).unwrap();
        let result = manager.add_unit(1, UnitConfig::new("Unit 1 Again"));
        assert!(result.is_err());
    }

    #[test]
    fn test_remove_unit() {
        let manager = MultiUnitManager::with_defaults();

        manager.add_unit(1, UnitConfig::new("Unit 1")).unwrap();
        assert!(manager.has_unit(1));

        let removed = manager.remove_unit(1);
        assert!(removed.is_some());
        assert!(!manager.has_unit(1));
    }

    #[test]
    fn test_read_write_operations() {
        let manager = MultiUnitManager::with_defaults();
        manager.add_unit(1, UnitConfig::new("Unit 1")).unwrap();

        // Write
        manager.write_holding_register(1, 0, 12345).unwrap();

        // Read
        let values = manager.read_holding_registers(1, 0, 1).unwrap();
        assert_eq!(values[0], 12345);
    }

    #[test]
    fn test_broadcast_write() {
        let manager = MultiUnitManager::with_defaults();
        manager.add_unit(1, UnitConfig::new("Unit 1")).unwrap();
        manager.add_unit(2, UnitConfig::new("Unit 2")).unwrap();
        manager.add_unit(3, UnitConfig::new("Unit 3")).unwrap();

        // Broadcast write (unit_id = 0)
        manager.write_holding_register(0, 100, 999).unwrap();

        // All units should have the value
        let v1 = manager.read_holding_registers(1, 100, 1).unwrap();
        let v2 = manager.read_holding_registers(2, 100, 1).unwrap();
        let v3 = manager.read_holding_registers(3, 100, 1).unwrap();

        assert_eq!(v1[0], 999);
        assert_eq!(v2[0], 999);
        assert_eq!(v3[0], 999);
    }

    #[test]
    fn test_broadcast_with_disabled_unit() {
        let manager = MultiUnitManager::with_defaults();
        manager.add_unit(1, UnitConfig::new("Unit 1")).unwrap();
        manager
            .add_unit(2, UnitConfig::new("Unit 2").with_broadcast(false))
            .unwrap();

        // Broadcast write
        manager.broadcast_write_holding_register(100, 888).unwrap();

        // Unit 1 should have the value
        let v1 = manager.read_holding_registers(1, 100, 1).unwrap();
        assert_eq!(v1[0], 888);

        // Unit 2 should NOT have the value (broadcast disabled)
        let v2 = manager.read_holding_registers(2, 100, 1).unwrap();
        assert_ne!(v2[0], 888);
    }

    #[test]
    fn test_auto_create_units() {
        let config = UnitManagerConfig::default().with_auto_create(true);
        let manager = MultiUnitManager::new(config);

        // Unit doesn't exist yet
        assert!(!manager.has_unit(5));

        // Access creates it
        let unit = manager.get_unit(5);
        assert!(unit.is_some());
        assert!(manager.has_unit(5));
    }

    #[test]
    fn test_statistics() {
        let manager = MultiUnitManager::with_defaults();
        manager.add_unit(1, UnitConfig::new("Unit 1")).unwrap();

        // Perform some operations
        manager.write_holding_register(1, 0, 100).unwrap();
        manager.read_holding_registers(1, 0, 1).unwrap();
        manager.read_holding_registers(1, 0, 1).unwrap();

        let stats = manager.unit_statistics();
        assert_eq!(stats.len(), 1);
        assert_eq!(stats[0].read_count, 2);
        assert_eq!(stats[0].write_count, 1);
    }

    #[test]
    fn test_different_word_orders() {
        let manager = MultiUnitManager::with_defaults();

        manager
            .add_unit(
                1,
                UnitConfig::with_word_order("BE Unit", WordOrder::BigEndian),
            )
            .unwrap();
        manager
            .add_unit(
                2,
                UnitConfig::with_word_order("LE Unit", WordOrder::LittleEndian),
            )
            .unwrap();

        let unit1 = manager.get_unit(1).unwrap();
        let unit2 = manager.get_unit(2).unwrap();

        assert_eq!(unit1.word_order(), WordOrder::BigEndian);
        assert_eq!(unit2.word_order(), WordOrder::LittleEndian);
    }
}