pokeys-lib 1.0.4

Pure Rust core library for PoKeys device control - USB/Network connectivity, I/O, PWM, encoders, SPI/I2C protocols
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
//! Unit tests for PoKeys library - No hardware required
//!
//! These tests can be run without any PoKeys hardware connected.
//! They test the library's internal logic, data structures, and error handling.
//!

#![allow(clippy::assertions_on_constants)]
//! Run with: cargo test

use pokeys_lib::encoders::{EncoderData, EncoderOptions};
use pokeys_lib::io::{PinCapability, PinData, PinFunction};
use pokeys_lib::keyboard_matrix::MatrixKeyboard;
use pokeys_lib::lcd::LcdData;
use pokeys_lib::pulse_engine::PulseEngineV2;
use pokeys_lib::pwm::PwmData;
use pokeys_lib::sensors::EasySensor;
use pokeys_lib::*;

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

    #[test]
    fn test_library_version() {
        let version = version();
        assert!(!version.is_empty());
        assert!(version.contains('.'));

        // Check version components
        assert_eq!(VERSION_MAJOR, 0);
        assert_eq!(VERSION_MINOR, 3);
        assert_eq!(VERSION_PATCH, 0);

        assert_eq!(version, "0.3.0");
    }

    #[test]
    fn test_device_types() {
        use DeviceTypeId::*;

        // Test device type enum values
        assert_eq!(Device55v1 as u8, 0);
        assert_eq!(Device55v2 as u8, 1);
        assert_eq!(Device56U as u8, 10);
        assert_eq!(Device57U as u8, 30);
        assert_eq!(Device58EU as u8, 40);

        // Test bootloader types
        assert_eq!(Bootloader55 as u8, 3);
        assert_eq!(Bootloader56U as u8, 15);
        assert_eq!(Bootloader58 as u8, 41);
    }

    #[test]
    fn test_connection_types() {
        use ConnectionParam::*;
        use DeviceConnectionType::*;

        assert_eq!(UsbDevice as u8, 0);
        assert_eq!(NetworkDevice as u8, 1);
        assert_eq!(FastUsbDevice as u8, 2);

        assert_eq!(Tcp as u8, 0);
        assert_eq!(Udp as u8, 1);
    }

    #[test]
    fn test_pin_functions() {
        // Test all pin function variants exist. `InvertPin` is deprecated in
        // favor of `set_pin_function_with_invert`, but we still want to
        // verify every declared variant constructs.
        #[allow(deprecated)]
        let _functions = [
            PinFunction::PinRestricted,
            PinFunction::Reserved,
            PinFunction::DigitalInput,
            PinFunction::DigitalOutput,
            PinFunction::AnalogInput,
            PinFunction::AnalogOutput,
            PinFunction::TriggeredInput,
            PinFunction::DigitalCounter,
            PinFunction::InvertPin,
        ];

        // Ensure they can be compared
        assert_ne!(PinFunction::DigitalInput, PinFunction::DigitalOutput);
        assert_eq!(PinFunction::DigitalInput, PinFunction::DigitalInput);
    }

    #[test]
    fn test_from_u8_combined_invert_byte() {
        // Combined bytes (base function | invert flag) decode to the base
        // function. The invert bit cannot be expressed through `PinFunction`;
        // callers recover it via `get_pin_invert` / `PinData::is_inverted`.
        assert_eq!(
            PinFunction::from_u8(0x82).unwrap(),
            PinFunction::DigitalInput
        );
        assert_eq!(
            PinFunction::from_u8(0x84).unwrap(),
            PinFunction::DigitalOutput
        );
        assert_eq!(
            PinFunction::from_u8(0xA0).unwrap(),
            PinFunction::TriggeredInput
        );
    }

    #[test]
    fn test_pin_data_invert_accessors_public() {
        let mut pin_data = PinData::new();
        pin_data.pin_function = 0x82; // DigitalInput | Invert
        assert!(pin_data.is_inverted());
        assert_eq!(pin_data.base_function(), PinFunction::DigitalInput);
        assert!(pin_data.is_digital_input());

        pin_data.pin_function = 0x02;
        assert!(!pin_data.is_inverted());
        assert_eq!(pin_data.base_function(), PinFunction::DigitalInput);
    }

    #[test]
    fn test_pin_capabilities() {
        let caps = [
            PinCapability::DigitalInput,
            PinCapability::DigitalOutput,
            PinCapability::AnalogInput,
            PinCapability::AnalogOutput,
            PinCapability::KeyboardMapping,
            PinCapability::TriggeredInput,
        ];

        // Test that capabilities can be used in collections
        let cap_vec: Vec<_> = caps.iter().collect();
        assert_eq!(cap_vec.len(), caps.len());
    }

    #[test]
    fn test_device_info_default() {
        let info = DeviceInfo::default();

        // Test default values are reasonable
        assert_eq!(info.pin_count, 0);
        assert_eq!(info.pwm_count, 0);
        assert_eq!(info.analog_inputs, 0);
        assert_eq!(info.encoders_count, 0);
        assert_eq!(info.fast_encoders, 0);
        assert_eq!(info.ultra_fast_encoders, 0);
        assert_eq!(info.pwm_internal_frequency, 0);
        assert_eq!(info.prot_i2c, 0);
        assert_eq!(info.prot_1wire, 0);
        assert_eq!(info.additional_options, 0);
    }

    #[test]
    fn test_device_data_default() {
        let data = DeviceData::default();

        assert_eq!(data.device_type_id, 0);
        assert_eq!(data.firmware_version_major, 0);
        assert_eq!(data.firmware_version_minor, 0);
        assert_eq!(data.serial_number, 0);
        assert_eq!(data.device_name(), "");
        assert_eq!(data.user_id, 0);
        assert!(!data.device_locked());
        assert_eq!(data.device_features(), 0);
    }

    #[test]
    fn test_encoder_options() {
        let mut options = EncoderOptions::new();

        // Test default values
        assert!(!options.enabled);
        assert!(!options.sampling_4x);
        assert!(!options.sampling_2x);
        assert!(!options.direct_key_mapping_a);
        assert!(!options.macro_mapping_a);
        assert!(!options.direct_key_mapping_b);
        assert!(!options.macro_mapping_b);

        // Test setting options
        options.enabled = true;
        options.sampling_4x = true;
        options.sampling_2x = true;

        assert!(options.enabled);
        assert!(options.sampling_4x);
        assert!(options.sampling_2x);
    }

    #[test]
    fn test_pwm_data() {
        let pwm_data = PwmData::new();

        // Test initial state - use actual field names and types
        assert_eq!(pwm_data.pwm_period, 0);
        assert_eq!(pwm_data.pwm_values.len(), 6); // Fixed array of 6 channels
        assert_eq!(pwm_data.enabled_channels, 0); // No channels enabled initially

        // Test all channels are initially disabled
        for i in 0..6 {
            assert!(!pwm_data.is_channel_enabled(i));
            assert_eq!(pwm_data.get_duty_cycle(i).unwrap(), 0);
        }
    }

    #[test]
    fn test_pwm_pin_mapping() {
        // Test pin to channel mapping
        assert_eq!(PwmData::pin_to_channel(22).unwrap(), 0); // PWM1
        assert_eq!(PwmData::pin_to_channel(21).unwrap(), 1); // PWM2
        assert_eq!(PwmData::pin_to_channel(20).unwrap(), 2); // PWM3
        assert_eq!(PwmData::pin_to_channel(19).unwrap(), 3); // PWM4
        assert_eq!(PwmData::pin_to_channel(18).unwrap(), 4); // PWM5
        assert_eq!(PwmData::pin_to_channel(17).unwrap(), 5); // PWM6

        // Test invalid pins
        assert!(PwmData::pin_to_channel(16).is_err());
        assert!(PwmData::pin_to_channel(23).is_err());

        // Test channel to pin mapping
        assert_eq!(PwmData::channel_to_pin(0).unwrap(), 22);
        assert_eq!(PwmData::channel_to_pin(1).unwrap(), 21);
        assert_eq!(PwmData::channel_to_pin(2).unwrap(), 20);
        assert_eq!(PwmData::channel_to_pin(3).unwrap(), 19);
        assert_eq!(PwmData::channel_to_pin(4).unwrap(), 18);
        assert_eq!(PwmData::channel_to_pin(5).unwrap(), 17);

        // Test invalid channels
        assert!(PwmData::channel_to_pin(6).is_err());
    }

    #[test]
    fn test_pwm_channel_operations() {
        let mut pwm_data = PwmData::new();

        // Test enabling channels
        pwm_data.set_channel_enabled(0, true).unwrap();
        pwm_data.set_channel_enabled(2, true).unwrap();

        assert!(pwm_data.is_channel_enabled(0));
        assert!(!pwm_data.is_channel_enabled(1));
        assert!(pwm_data.is_channel_enabled(2));
        assert_eq!(pwm_data.enabled_channels, 0b00000101); // Bits 0 and 2 set

        // Test disabling channels
        pwm_data.set_channel_enabled(0, false).unwrap();
        assert!(!pwm_data.is_channel_enabled(0));
        assert_eq!(pwm_data.enabled_channels, 0b00000100); // Only bit 2 set

        // Test duty cycle operations
        pwm_data.set_duty_cycle(1, 1500).unwrap();
        assert_eq!(pwm_data.get_duty_cycle(1).unwrap(), 1500);

        // Test invalid channel operations
        assert!(pwm_data.set_channel_enabled(6, true).is_err());
        assert!(pwm_data.set_duty_cycle(6, 1000).is_err());
        assert!(pwm_data.get_duty_cycle(6).is_err());
    }

    #[test]
    fn test_matrix_keyboard() {
        let keyboard = MatrixKeyboard::new();

        // Test initial state - use actual field names
        assert_eq!(keyboard.configuration, 0);
        assert_eq!(keyboard.width, 0);
        assert_eq!(keyboard.height, 0);
        assert_eq!(keyboard.scanning_decimation, 0);
        assert_eq!(keyboard.column_pins.len(), 8); // Fixed array size
        assert_eq!(keyboard.row_pins.len(), 16); // Fixed array size
    }

    #[test]
    fn test_lcd_data() {
        let lcd = LcdData::new();

        // Test initial state - use actual field names
        assert_eq!(lcd.configuration, 0);
        assert_eq!(lcd.rows, 0);
        assert_eq!(lcd.columns, 0);
        assert_eq!(lcd.row_refresh_flags, 0);
        assert_eq!(lcd.line1.len(), 20); // Fixed array size
        assert_eq!(lcd.line2.len(), 20); // Fixed array size
    }

    #[test]
    fn test_pulse_engine_v2() {
        let pe = PulseEngineV2::new();

        // Test initial state - use actual field names
        // Note: pe.info is a struct, not a simple integer
        assert_eq!(pe.axes_state.len(), 8); // Default number of axes
        assert_eq!(pe.axes_config.len(), 8);
        assert_eq!(pe.emergency_switch_polarity, 0);
    }

    #[test]
    fn test_real_time_clock() {
        let rtc = RealTimeClock::default();

        // Test default values - use actual field names
        assert_eq!(rtc.year, 0);
        assert_eq!(rtc.month, 0);
        assert_eq!(rtc.dom, 0); // day of month
        assert_eq!(rtc.hour, 0);
        assert_eq!(rtc.min, 0); // minute
        assert_eq!(rtc.sec, 0); // second
        assert_eq!(rtc.dow, 0); // day of week
    }

    #[test]
    fn test_error_types() {
        use PoKeysError::*;

        // Test error creation and display
        let errors = [
            DeviceNotFound,
            ConnectionFailed,
            InvalidParameter,
            CommunicationError,
            NotConnected,
            UnsupportedOperation,
            InternalError("test".to_string()),
        ];

        for error in &errors {
            let error_string = format!("{error}");
            assert!(!error_string.is_empty());

            // Test that errors can be compared
            assert_eq!(error, error);
        }
    }

    #[test]
    fn test_error_from_conversions() {
        // Test std::io::Error conversion
        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
        let pokeys_error: PoKeysError = io_error.into();

        match pokeys_error {
            PoKeysError::Io(_) => {}
            _ => panic!("Expected Io error"),
        }
    }

    #[test]
    fn test_buffer_constants() {
        // Test that buffer sizes are reasonable
        assert!(REQUEST_BUFFER_SIZE > 0);
        assert!(RESPONSE_BUFFER_SIZE > 0);
        assert!(REQUEST_BUFFER_SIZE <= 1024);
        assert!(RESPONSE_BUFFER_SIZE <= 1024);

        // Common buffer sizes in PoKeys protocol
        assert!(REQUEST_BUFFER_SIZE >= 64);
        assert!(RESPONSE_BUFFER_SIZE >= 64);
    }

    #[test]
    fn test_pin_data_validation() {
        // Test pin number validation logic
        fn is_valid_pin(pin: u8, max_pins: u8) -> bool {
            pin > 0 && pin <= max_pins
        }

        // Test various scenarios
        assert!(!is_valid_pin(0, 55)); // Pin 0 is invalid
        assert!(is_valid_pin(1, 55)); // Pin 1 is valid
        assert!(is_valid_pin(55, 55)); // Last pin is valid
        assert!(!is_valid_pin(56, 55)); // Beyond max is invalid
    }

    #[test]
    fn test_pwm_duty_cycle_validation() {
        fn is_valid_duty_cycle(duty: f32) -> bool {
            (0.0..=100.0).contains(&duty)
        }

        assert!(is_valid_duty_cycle(0.0));
        assert!(is_valid_duty_cycle(50.0));
        assert!(is_valid_duty_cycle(100.0));
        assert!(!is_valid_duty_cycle(-1.0));
        assert!(!is_valid_duty_cycle(101.0));
    }

    #[test]
    fn test_encoder_value_ranges() {
        // Test encoder value handling
        let max_encoder_value = i32::MAX;
        let min_encoder_value = i32::MIN;

        // Test that encoder values can handle full range
        assert!(max_encoder_value > 0);
        assert!(min_encoder_value < 0);

        // Test overflow behavior
        let test_value = max_encoder_value;
        let wrapped = test_value.wrapping_add(1);
        assert_eq!(wrapped, min_encoder_value);
    }

    #[test]
    fn test_network_device_info() {
        let net_info = NetworkDeviceInfo::default();

        // Test default values - use actual method calls
        assert_eq!(net_info.ip_address(), [0, 0, 0, 0]);
        assert_eq!(net_info.subnet_mask, [0, 0, 0, 0]);
        assert_eq!(net_info.gateway(), [0, 0, 0, 0]);
        assert_eq!(net_info.dns_server(), [0, 0, 0, 0]);
        assert_eq!(net_info.mac_address(), [0; 6]);
        assert_eq!(net_info.device_name(), "");
        assert_eq!(net_info.http_port(), 80);
        assert_eq!(net_info.tcp_port(), 20055);
        assert_eq!(net_info.udp_port(), 20055);
        assert!(!net_info.dhcp_enabled());

        // Note: Cannot test setting values as they are methods, not fields
        // This is a limitation of the current API design
    }

    #[test]
    fn test_easy_sensor() {
        let sensor = EasySensor::new();

        // Test initial state - use actual field names and types
        assert_eq!(sensor.sensor_id, [0; 8]); // sensor_id is an array
        assert_eq!(sensor.sensor_type, 0);
        assert_eq!(sensor.sensor_value, 0); // i32, not f32
        assert_eq!(sensor.sensor_refresh_period, 0); // actual field name
    }

    #[test]
    fn test_data_structure_sizes() {
        use std::mem::size_of;

        // Test that key structures have reasonable sizes
        assert!(size_of::<DeviceInfo>() > 0);
        assert!(size_of::<DeviceData>() > 0);
        assert!(size_of::<PinData>() > 0);
        assert!(size_of::<EncoderData>() > 0);

        // Structures shouldn't be excessively large
        assert!(size_of::<DeviceInfo>() < 1024);
        assert!(size_of::<DeviceData>() < 1024);
    }

    #[test]
    fn test_string_conversions() {
        // Test device name handling
        let device_name = "PoKeys57U";
        assert!(!device_name.is_empty());
        assert!(device_name.len() < 256); // Reasonable length limit

        // Test that device names can contain alphanumeric characters
        assert!(device_name.chars().all(|c| c.is_alphanumeric()));
    }

    #[test]
    fn test_bit_operations() {
        // Test bit manipulation functions that might be used internally
        fn set_bit(value: u8, bit: u8) -> u8 {
            value | (1 << bit)
        }

        fn clear_bit(value: u8, bit: u8) -> u8 {
            value & !(1 << bit)
        }

        fn test_bit(value: u8, bit: u8) -> bool {
            (value & (1 << bit)) != 0
        }

        let mut value = 0u8;

        // Test setting bits
        value = set_bit(value, 0);
        assert!(test_bit(value, 0));
        assert_eq!(value, 1);

        value = set_bit(value, 7);
        assert!(test_bit(value, 7));
        assert_eq!(value, 129); // 0b10000001

        // Test clearing bits
        value = clear_bit(value, 0);
        assert!(!test_bit(value, 0));
        assert_eq!(value, 128); // 0b10000000
    }

    #[test]
    fn test_checksum_calculation() {
        // Test checksum calculation that might be used in protocol
        fn calculate_checksum(data: &[u8]) -> u8 {
            data.iter().fold(0u8, |acc, &x| acc.wrapping_add(x))
        }

        let test_data = [0x01, 0x02, 0x03, 0x04];
        let checksum = calculate_checksum(&test_data);
        assert_eq!(checksum, 10);

        // Test empty data
        let empty_checksum = calculate_checksum(&[]);
        assert_eq!(empty_checksum, 0);

        // Test overflow behavior
        let overflow_data = [0xFF, 0xFF];
        let overflow_checksum = calculate_checksum(&overflow_data);
        assert_eq!(overflow_checksum, 254); // 0xFF + 0xFF = 0x1FE -> 0xFE
    }

    #[test]
    fn test_array_bounds() {
        // Test array access patterns used in the library
        let test_array = [1, 2, 3, 4, 5];

        // Test valid indices
        assert_eq!(test_array[0], 1);
        assert_eq!(test_array[4], 5);

        // Test slice operations
        let slice = &test_array[1..4];
        assert_eq!(slice, &[2, 3, 4]);

        // Test iterator
        let sum: i32 = test_array.iter().sum();
        assert_eq!(sum, 15);
    }
}

#[cfg(test)]
mod network_config_tests {
    use pokeys_lib::{NetworkDeviceConfig, NetworkDeviceInfo};

    #[test]
    fn test_network_device_config_defaults() {
        let cfg = NetworkDeviceConfig::new();
        assert_eq!(cfg.device_info.subnet_mask, [255, 255, 255, 0]);
        assert_eq!(cfg.device_info.tcp_timeout, 1000);
        assert_eq!(cfg.device_info.dhcp, 0);
        assert_eq!(cfg.device_info.additional_network_options, 0xA0);
        assert_eq!(cfg.device_info.ip_address_setup, [0, 0, 0, 0]);
        assert_eq!(cfg.device_info.gateway_ip, [0, 0, 0, 0]);
    }

    #[test]
    fn test_network_device_config_builder() {
        let mut cfg = NetworkDeviceConfig::new();
        cfg.set_ip_address([192, 168, 1, 50]);
        cfg.set_subnet_mask([255, 255, 255, 0]);
        cfg.set_default_gateway([192, 168, 1, 1]);
        cfg.set_dhcp(false);
        cfg.set_tcp_timeout(2000);
        cfg.set_network_options(false, false, false);

        assert_eq!(cfg.device_info.ip_address_setup, [192, 168, 1, 50]);
        assert_eq!(cfg.device_info.subnet_mask, [255, 255, 255, 0]);
        assert_eq!(cfg.device_info.gateway_ip, [192, 168, 1, 1]);
        assert_eq!(cfg.device_info.dhcp, 0);
        assert_eq!(cfg.device_info.tcp_timeout, 2000);
    }

    #[test]
    fn test_dhcp_toggle() {
        let mut cfg = NetworkDeviceConfig::new();
        cfg.set_dhcp(true);
        assert_eq!(cfg.device_info.dhcp, 1);
        cfg.set_dhcp(false);
        assert_eq!(cfg.device_info.dhcp, 0);
    }

    #[test]
    fn test_network_options_flags() {
        let mut cfg = NetworkDeviceConfig::new();

        cfg.set_network_options(true, false, false);
        assert_eq!(cfg.device_info.additional_network_options, 0xA0 | 0x01);

        cfg.set_network_options(false, true, false);
        assert_eq!(cfg.device_info.additional_network_options, 0xA0 | 0x02);

        cfg.set_network_options(false, false, true);
        assert_eq!(cfg.device_info.additional_network_options, 0xA0 | 0x04);

        cfg.set_network_options(true, true, true);
        assert_eq!(cfg.device_info.additional_network_options, 0xA0 | 0x07);

        cfg.set_network_options(false, false, false);
        assert_eq!(cfg.device_info.additional_network_options, 0xA0);
    }

    #[test]
    fn test_tcp_timeout_unit_conversion() {
        // Wire encoding: ms / 100, minimum 1
        let cases: &[(u16, u16)] = &[
            (1000, 10),
            (100, 1),
            (500, 5),
            (0, 0), // saturating_mul(100) when reading back handles the 0 edge
        ];
        for &(input_ms, expected_units) in cases {
            let units = (input_ms / 100).max(if input_ms == 0 { 0 } else { 1 });
            assert_eq!(units, expected_units, "input_ms={input_ms}");
        }
    }

    #[test]
    fn test_gateway_subnet_set_flag() {
        // Non-zero gateway → flag should be 1
        let info = NetworkDeviceInfo {
            gateway_ip: [192, 168, 1, 1],
            subnet_mask: [0, 0, 0, 0],
            ..Default::default()
        };
        let flag = if info.gateway_ip != [0, 0, 0, 0] || info.subnet_mask != [0, 0, 0, 0] {
            1u8
        } else {
            0u8
        };
        assert_eq!(flag, 1);

        // All-zero → flag should be 0
        let info2 = NetworkDeviceInfo::default();
        let flag2 = if info2.gateway_ip != [0, 0, 0, 0] || info2.subnet_mask != [0, 0, 0, 0] {
            1u8
        } else {
            0u8
        };
        assert_eq!(flag2, 0);
    }

    #[test]
    fn test_additional_options_nibble() {
        // Upper nibble must always be 0xA regardless of lower-nibble input
        for lower in 0u8..=0x0F {
            let options = (lower & 0x0F) | 0xA0;
            assert_eq!(
                options >> 4,
                0xA,
                "upper nibble not 0xA for lower={lower:#x}"
            );
            assert_eq!(options & 0x0F, lower);
        }
    }

    #[test]
    fn test_device_name_truncation() {
        let long_name = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 26 chars, max is 20
        let truncated = if long_name.len() > 20 {
            &long_name[..20]
        } else {
            long_name
        };
        assert_eq!(truncated.len(), 20);
        assert_eq!(truncated, "ABCDEFGHIJKLMNOPQRST");

        let short_name = "MyDevice"; // 8 chars, no truncation
        let not_truncated = if short_name.len() > 20 {
            &short_name[..20]
        } else {
            short_name
        };
        assert_eq!(not_truncated, "MyDevice");
    }

    #[test]
    fn test_network_device_info_dhcp_enabled() {
        let mut info = NetworkDeviceInfo::default();
        assert!(!info.dhcp_enabled());
        info.dhcp = 1;
        assert!(info.dhcp_enabled());
        info.dhcp = 0;
        assert!(!info.dhcp_enabled());
    }
}

/// Mock tests for testing without hardware
#[cfg(test)]
mod mock_tests {
    use super::*;

    /// Mock device for testing
    struct MockDevice {
        connected: bool,
        pin_states: std::collections::HashMap<u8, bool>,
        analog_values: std::collections::HashMap<u8, u16>,
        encoder_values: std::collections::HashMap<u8, i32>,
    }

    impl MockDevice {
        fn new() -> Self {
            Self {
                connected: false,
                pin_states: std::collections::HashMap::new(),
                analog_values: std::collections::HashMap::new(),
                encoder_values: std::collections::HashMap::new(),
            }
        }

        fn connect(&mut self) -> Result<()> {
            self.connected = true;
            Ok(())
        }

        fn disconnect(&mut self) {
            self.connected = false;
        }

        fn is_connected(&self) -> bool {
            self.connected
        }

        fn set_digital_output(&mut self, pin: u8, state: bool) -> Result<()> {
            if !self.connected {
                return Err(PoKeysError::NotConnected);
            }
            if pin == 0 || pin > 55 {
                return Err(PoKeysError::InvalidParameter);
            }
            self.pin_states.insert(pin, state);
            Ok(())
        }

        fn get_digital_input(&self, pin: u8) -> Result<bool> {
            if !self.connected {
                return Err(PoKeysError::NotConnected);
            }
            if pin == 0 || pin > 55 {
                return Err(PoKeysError::InvalidParameter);
            }
            Ok(self.pin_states.get(&pin).copied().unwrap_or(false))
        }

        fn get_analog_input(&self, pin: u8) -> Result<u16> {
            if !self.connected {
                return Err(PoKeysError::NotConnected);
            }
            if pin == 0 || pin > 8 {
                return Err(PoKeysError::InvalidParameter);
            }
            Ok(self.analog_values.get(&pin).copied().unwrap_or(0))
        }

        fn set_encoder_value(&mut self, encoder: u8, value: i32) {
            self.encoder_values.insert(encoder, value);
        }

        fn get_encoder_value(&self, encoder: u8) -> Result<i32> {
            if !self.connected {
                return Err(PoKeysError::NotConnected);
            }
            if encoder >= 25 {
                return Err(PoKeysError::InvalidParameter);
            }
            Ok(self.encoder_values.get(&encoder).copied().unwrap_or(0))
        }
    }

    #[test]
    fn test_mock_device_connection() {
        let mut device = MockDevice::new();

        assert!(!device.is_connected());

        device.connect().unwrap();
        assert!(device.is_connected());

        device.disconnect();
        assert!(!device.is_connected());
    }

    #[test]
    fn test_mock_digital_io() {
        let mut device = MockDevice::new();
        device.connect().unwrap();

        // Test setting and reading digital outputs
        device.set_digital_output(1, true).unwrap();
        assert!(device.get_digital_input(1).unwrap());

        device.set_digital_output(1, false).unwrap();
        assert!(!device.get_digital_input(1).unwrap());

        // Test multiple pins
        device.set_digital_output(5, true).unwrap();
        device.set_digital_output(10, false).unwrap();

        assert!(device.get_digital_input(5).unwrap());
        assert!(!device.get_digital_input(10).unwrap());
    }

    #[test]
    fn test_mock_error_handling() {
        let mut device = MockDevice::new();

        // Test operations on disconnected device
        assert!(device.set_digital_output(1, true).is_err());
        assert!(device.get_digital_input(1).is_err());
        assert!(device.get_analog_input(1).is_err());
        assert!(device.get_encoder_value(0).is_err());

        device.connect().unwrap();

        // Test invalid parameters
        assert!(device.set_digital_output(0, true).is_err());
        assert!(device.set_digital_output(56, true).is_err());
        assert!(device.get_digital_input(0).is_err());
        assert!(device.get_digital_input(56).is_err());
        assert!(device.get_analog_input(0).is_err());
        assert!(device.get_analog_input(9).is_err());
        assert!(device.get_encoder_value(25).is_err());
    }

    #[test]
    fn test_mock_analog_inputs() {
        let mut device = MockDevice::new();
        device.connect().unwrap();

        // Test default analog values
        for pin in 1..=8 {
            assert_eq!(device.get_analog_input(pin).unwrap(), 0);
        }

        // Test setting analog values (simulating external input)
        device.analog_values.insert(1, 2048); // Mid-scale
        device.analog_values.insert(2, 4095); // Full-scale

        assert_eq!(device.get_analog_input(1).unwrap(), 2048);
        assert_eq!(device.get_analog_input(2).unwrap(), 4095);
    }

    #[test]
    fn test_mock_encoders() {
        let mut device = MockDevice::new();
        device.connect().unwrap();

        // Test default encoder values
        for encoder in 0..25 {
            assert_eq!(device.get_encoder_value(encoder).unwrap(), 0);
        }

        // Test setting encoder values
        device.set_encoder_value(0, 100);
        device.set_encoder_value(1, -50);
        device.set_encoder_value(2, i32::MAX);
        device.set_encoder_value(3, i32::MIN);

        assert_eq!(device.get_encoder_value(0).unwrap(), 100);
        assert_eq!(device.get_encoder_value(1).unwrap(), -50);
        assert_eq!(device.get_encoder_value(2).unwrap(), i32::MAX);
        assert_eq!(device.get_encoder_value(3).unwrap(), i32::MIN);
    }

    #[test]
    fn test_mock_state_persistence() {
        let mut device = MockDevice::new();
        device.connect().unwrap();

        // Set various states
        device.set_digital_output(1, true).unwrap();
        device.set_digital_output(2, false).unwrap();
        device.analog_values.insert(1, 1234);
        device.set_encoder_value(0, 567);

        // Verify states persist
        assert!(device.get_digital_input(1).unwrap());
        assert!(!device.get_digital_input(2).unwrap());
        assert_eq!(device.get_analog_input(1).unwrap(), 1234);
        assert_eq!(device.get_encoder_value(0).unwrap(), 567);

        // Disconnect and reconnect
        device.disconnect();
        device.connect().unwrap();

        // States should still be there (in this mock implementation)
        assert!(device.get_digital_input(1).unwrap());
        assert!(!device.get_digital_input(2).unwrap());
        assert_eq!(device.get_analog_input(1).unwrap(), 1234);
        assert_eq!(device.get_encoder_value(0).unwrap(), 567);
    }

    #[test]
    fn test_servo_types() {
        // Test 180-degree servo
        let servo_180 = ServoConfig::one_eighty(22, 25000, 50000);
        assert_eq!(servo_180.pin, 22);
        match servo_180.servo_type {
            ServoType::OneEighty { pos_0, pos_180 } => {
                assert_eq!(pos_0, 25000);
                assert_eq!(pos_180, 50000);
            }
            _ => panic!("Expected OneEighty servo type"),
        }

        // Test 360-degree position servo
        let servo_360_pos = ServoConfig::three_sixty_position(21, 30000, 60000);
        assert_eq!(servo_360_pos.pin, 21);
        match servo_360_pos.servo_type {
            ServoType::ThreeSixtyPosition { pos_0, pos_360 } => {
                assert_eq!(pos_0, 30000);
                assert_eq!(pos_360, 60000);
            }
            _ => panic!("Expected ThreeSixtyPosition servo type"),
        }

        // Test 360-degree speed servo
        let servo_360_speed = ServoConfig::three_sixty_speed(20, 37500, 50000, 25000);
        assert_eq!(servo_360_speed.pin, 20);
        match servo_360_speed.servo_type {
            ServoType::ThreeSixtySpeed {
                stop,
                clockwise,
                anti_clockwise,
            } => {
                assert_eq!(stop, 37500);
                assert_eq!(clockwise, 50000);
                assert_eq!(anti_clockwise, 25000);
            }
            _ => panic!("Expected ThreeSixtySpeed servo type"),
        }
    }

    #[test]
    fn test_servo_angle_calculations() {
        // Test 180-degree servo angle validation
        let servo_180 = ServoConfig::one_eighty(22, 25000, 50000);

        // Test angle range calculations
        match servo_180.servo_type {
            ServoType::OneEighty { pos_0, pos_180 } => {
                let range = pos_180 as f32 - pos_0 as f32;
                let angle_90 = (pos_0 as f32 + (90.0 / 180.0) * range) as u32;
                assert_eq!(angle_90, 37500); // Should be midpoint
            }
            _ => panic!("Expected OneEighty servo type"),
        }

        // Test 360-degree position servo calculations
        let servo_360 = ServoConfig::three_sixty_position(21, 30000, 60000);

        match servo_360.servo_type {
            ServoType::ThreeSixtyPosition { pos_0, pos_360 } => {
                let range = pos_360 as f32 - pos_0 as f32;
                let angle_180 = (pos_0 as f32 + (180.0 / 360.0) * range) as u32;
                assert_eq!(angle_180, 45000); // Should be midpoint
            }
            _ => panic!("Expected ThreeSixtyPosition servo type"),
        }
    }

    #[test]
    fn test_servo_speed_calculations() {
        let servo_speed = ServoConfig::three_sixty_speed(20, 37500, 50000, 25000);

        match servo_speed.servo_type {
            ServoType::ThreeSixtySpeed {
                stop,
                clockwise,
                anti_clockwise,
            } => {
                // Test speed calculations
                let cw_range = clockwise as f32 - stop as f32;
                let acw_range = anti_clockwise as f32 - stop as f32;

                // 50% clockwise should be halfway between stop and clockwise
                let speed_50_cw = (stop as f32 + (50.0 / 100.0) * cw_range) as u32;
                assert_eq!(speed_50_cw, 43750);

                // 50% anti-clockwise should be halfway between stop and anti_clockwise
                let speed_50_acw = (stop as f32 + (50.0 / 100.0) * acw_range) as u32;
                assert_eq!(speed_50_acw, 31250);
            }
            _ => panic!("Expected ThreeSixtySpeed servo type"),
        }
    }

    #[test]
    fn test_servo_type_validation() {
        // Test that servo types are correctly identified
        let servo_180 = ServoConfig::one_eighty(22, 25000, 50000);
        let servo_360_pos = ServoConfig::three_sixty_position(21, 30000, 60000);
        let servo_360_speed = ServoConfig::three_sixty_speed(20, 37500, 50000, 25000);

        // Verify servo types
        assert!(matches!(servo_180.servo_type, ServoType::OneEighty { .. }));
        assert!(matches!(
            servo_360_pos.servo_type,
            ServoType::ThreeSixtyPosition { .. }
        ));
        assert!(matches!(
            servo_360_speed.servo_type,
            ServoType::ThreeSixtySpeed { .. }
        ));

        // Verify pins
        assert_eq!(servo_180.pin, 22);
        assert_eq!(servo_360_pos.pin, 21);
        assert_eq!(servo_360_speed.pin, 20);
    }
}