linux-gpib-rs 0.2.6

Low-level wrapper for Linux GPIB
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
use crate::error::GpibError;
use std::fmt;
use std::os::raw::{c_int, c_short};
use std::time::Duration;

pub enum IbOption {
    PAD,
    SAD,
    TMO,
    EOT,
    PPC,
    READDR,
    AUTOPOLL,
    CICPROT,
    SC,
    SRE,
    EOSrd,
    EOSwrt,
    EOScmp,
    EOSchar,
    PP2,
    TIMING,
    ReadAdjust,
    WriteAdjust,
    EventQueue,
    SPollBit,
    SendLLO,
    SPollTime,
    PPollTime,
    EndBitIsNormal,
    UnAddr,
    HSCableLength,
    Ist,
    Rsv,
    BNA,
    SevenBitEOS,
}

impl fmt::Display for IbOption {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            IbOption::PAD => {
                write!(f, "IbOption::PAD")
            }
            IbOption::SAD => {
                write!(f, "IbOption::SAD")
            }
            IbOption::TMO => {
                write!(f, "IbOption::TMO")
            }
            IbOption::EOT => {
                write!(f, "IbOption::EOT")
            }
            IbOption::PPC => {
                write!(f, "IbOption::PPC")
            }
            IbOption::READDR => {
                write!(f, "IbOption::READDR")
            }
            IbOption::AUTOPOLL => {
                write!(f, "IbOption::AUTOPOLL")
            }
            IbOption::CICPROT => {
                write!(f, "IbOption::CICPROT")
            }
            IbOption::SC => {
                write!(f, "IbOption::SC")
            }
            IbOption::SRE => {
                write!(f, "IbOption::SRE")
            }
            IbOption::EOSrd => {
                write!(f, "IbOption::EOSrd")
            }
            IbOption::EOSwrt => {
                write!(f, "IbOption::EOSwrt")
            }
            IbOption::EOScmp => {
                write!(f, "IbOption::EOScmp")
            }
            IbOption::EOSchar => {
                write!(f, "IbOption::EOSchar")
            }
            IbOption::PP2 => {
                write!(f, "IbOption::PP2")
            }
            IbOption::TIMING => {
                write!(f, "IbOption::TIMING")
            }
            IbOption::ReadAdjust => {
                write!(f, "IbOption::ReadAdjust")
            }
            IbOption::WriteAdjust => {
                write!(f, "IbOption::WriteAdjust")
            }
            IbOption::EventQueue => {
                write!(f, "IbOption::EventQueue")
            }
            IbOption::SPollBit => {
                write!(f, "IbOption::SPollBit")
            }
            IbOption::SendLLO => {
                write!(f, "IbOption::SendLLO")
            }
            IbOption::SPollTime => {
                write!(f, "IbOption::SPollTime")
            }
            IbOption::PPollTime => {
                write!(f, "IbOption::PPollTime")
            }
            IbOption::EndBitIsNormal => {
                write!(f, "IbOption::EndBitIsNormal")
            }
            IbOption::UnAddr => {
                write!(f, "IbOption::UnAddr")
            }
            IbOption::HSCableLength => {
                write!(f, "IbOption::HSCableLength")
            }
            IbOption::Ist => {
                write!(f, "IbOption::Ist")
            }
            IbOption::Rsv => {
                write!(f, "IbOption::Rsv")
            }
            IbOption::BNA => {
                write!(f, "IbOption::BNA")
            }
            IbOption::SevenBitEOS => {
                write!(f, "IbOption::SevenBitEOS")
            }
        }
    }
}

impl fmt::Debug for IbOption {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            IbOption::PAD => {
                write!(f, "IbOption::PAD (0x1): GPIB primary address")
            }
            IbOption::SAD => {
                write!(
                    f,
                    "IbOption::SAD (0x2): GPIB secondary address (0 for none, 0x60 to 0x7e for secondary addresses 0 to 30)"
                )
            }
            IbOption::TMO => {
                write!(
                    f,
                    "IbOption::TMO (0x3): Timeout setting for io operations (a number from 0 to 17). See ibmto()."
                )
            }
            IbOption::EOT => {
                write!(
                    f,
                    "IbOption::EOT (0x4): Nonzero if EOI is asserted with last byte on writes. See ibeot()."
                )
            }
            IbOption::PPC => {
                write!(
                    f,
                    "IbOption::PPC (0x5): Parallel poll configuration. See ibppc()."
                )
            }
            IbOption::READDR => {
                write!(
                    f,
                    "IbOption::READDR (0x6): Useless, included for compatibility only."
                )
            }
            IbOption::AUTOPOLL => {
                write!(
                    f,
                    "IbOption::AUTOPOLL (0x7): Nonzero if automatic serial polling is enabled."
                )
            }
            IbOption::CICPROT => {
                write!(
                    f,
                    "IbOption::CICPROT (0x8): Useless, included for compatibility only."
                )
            }
            IbOption::SC => {
                write!(
                    f,
                    "IbOption::SC (0xa): Nonzero if board is system controller. See ibrsc(). "
                )
            }
            IbOption::SRE => {
                write!(
                    f,
                    "IbOption::SRE (0xb): Nonzero if board autmatically asserts REN line when it becomes the system controller. See ibsre()."
                )
            }
            IbOption::EOSrd => {
                write!(
                    f,
                    "IbOption::EOSrd (0xc): Nonzero if termination of reads on reception of the end-of-string character is enabled. See ibeos(), in particular the REOS bit."
                )
            }
            IbOption::EOSwrt => {
                write!(
                    f,
                    "IbOption::EOSwrt (0xd): Nonzero if EOI is asserted whenever end-of-string character is sent. See ibeos(), in particular the XEOS bit."
                )
            }
            IbOption::EOScmp => {
                write!(
                    f,
                    "IbOption::EOScmp (0xe): Nonzero if all 8 bits are used to match end-of-string character. Zero if only least significant 7 bits are used. See ibeos(), in particular the BIN bit."
                )
            }
            IbOption::EOSchar => {
                write!(f, "IbOption::EOSchar (0xf): The end-of-string byte.")
            }
            IbOption::PP2 => {
                write!(
                    f,
                    "IbOption::PP2 (0x10): Nonzero if in local parallel poll configure mode. Zero if in remote parallel poll configure mode."
                )
            }
            IbOption::TIMING => {
                write!(
                    f,
                    "IbOption::TIMING (0x11): Number indicating T1 delay. 1 for 2 microseconds, 2 for 500 nanoseconds, 3 for 350 nanoseconds. The values are declared in the header files as the constants T1_DELAY_2000ns, T1_DELAY_500ns, and T1_DELAY_350ns."
                )
            }
            IbOption::ReadAdjust => {
                write!(
                    f,
                    "IbOption::ReadAdjust (0x13): Nonzero if byte pairs are automatically swapped during reads."
                )
            }
            IbOption::WriteAdjust => {
                write!(
                    f,
                    "IbOption::WriteAdjust (0x14): Nonzero if byte pairs are automatically swapped during writes."
                )
            }
            IbOption::EventQueue => {
                write!(
                    f,
                    "IbOption::EventQueue (0x15): Nonzero if event queue is enabled."
                )
            }
            IbOption::SPollBit => {
                write!(
                    f,
                    "IbOption::SPollBit (0x16): Nonzero if the use of the SPOLL bit in ibsta is enabled."
                )
            }
            IbOption::SendLLO => {
                write!(
                    f,
                    "IbOption::SendLLO (0x17): Nonzero if devices connected to this board are automatically put into local lockout mode when brought online with ibfind() or ibdev()."
                )
            }
            IbOption::SPollTime => {
                write!(
                    f,
                    "IbOption::SPollTime (0x18): Timeout for serial polls. The value of the result is between 0 and 17, and has the same meaning as in ibtmo()."
                )
            }
            IbOption::PPollTime => {
                write!(
                    f,
                    "IbOption::PPollTime (0x19): Timeout for parallel polls. The value of the result is between 0 and 17, and has the same meaning as in ibtmo()."
                )
            }
            IbOption::EndBitIsNormal => {
                write!(
                    f,
                    "IbOption::EndBitIsNormal (0x1a): Nonzero if END bit of ibsta is set on reception of end-of-string character or EOI. Zero if END bit is only set on EOI."
                )
            }
            IbOption::UnAddr => {
                write!(
                    f,
                    "IbOption::UnAddr (0x1b): Nonzero if UNT (untalk) and UNL (unlisten) commands are automatically sent after a completed io operation using this descriptor."
                )
            }
            IbOption::HSCableLength => {
                write!(
                    f,
                    "IbOption::HSCableLength (0x1f): Useless, included only for compatibility."
                )
            }
            IbOption::Ist => {
                write!(
                    f,
                    "IbOption::Ist (0x20): Individual status bit, a.k.a. 'ist'."
                )
            }
            IbOption::Rsv => {
                write!(
                    f,
                    "IbOption::Rsv (0x21): The current status byte this board will use to respond to serial polls."
                )
            }
            IbOption::BNA => {
                write!(
                    f,
                    "IbOption::BNA (0x200): For a device: the board index (minor number) of interface board through which the device is being accessed. For a board: the board index of the board itself."
                )
            }
            IbOption::SevenBitEOS => {
                write!(
                    f,
                    "IbOption::SevenBitEOS (0x1000): Nonzero if board supports 7 bit EOS comparisons. See ibeos(), in particular the BIN bit. This is a Linux-GPIB extension."
                )
            }
        }
    }
}

impl IbOption {
    pub fn as_option(&self) -> c_int {
        match self {
            IbOption::PAD => 0x1,
            IbOption::SAD => 0x2,
            IbOption::TMO => 0x3,
            IbOption::EOT => 0x4,
            IbOption::PPC => 0x5,
            IbOption::READDR => 0x6,
            IbOption::AUTOPOLL => 0x7,
            IbOption::CICPROT => 0x8,
            IbOption::SC => 0xa,
            IbOption::SRE => 0xb,
            IbOption::EOSrd => 0xc,
            IbOption::EOSwrt => 0xd,
            IbOption::EOScmp => 0xe,
            IbOption::EOSchar => 0xf,
            IbOption::PP2 => 0x10,
            IbOption::TIMING => 0x11,
            IbOption::ReadAdjust => 0x13,
            IbOption::WriteAdjust => 0x14,
            IbOption::EventQueue => 0x15,
            IbOption::SPollBit => 0x16,
            IbOption::SendLLO => 0x17,
            IbOption::SPollTime => 0x18,
            IbOption::PPollTime => 0x19,
            IbOption::EndBitIsNormal => 0x1a,
            IbOption::UnAddr => 0x1b,
            IbOption::HSCableLength => 0x1f,
            IbOption::Ist => 0x20,
            IbOption::Rsv => 0x21,
            IbOption::BNA => 0x200,
            IbOption::SevenBitEOS => 0x1000,
        }
    }
}

#[derive(Clone, Copy)]
pub enum IbTimeout {
    TNone,
    T10us,
    T30us,
    T100us,
    T300us,
    T1ms,
    T3ms,
    T10ms,
    T30ms,
    T100ms,
    T300ms,
    T1s,
    T3s,
    T10s,
    T30s,
    T100s,
    T300s,
    T1000s,
}

impl fmt::Display for IbTimeout {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            IbTimeout::TNone => {
                write!(f, "Never timeout")
            }
            IbTimeout::T10us => {
                write!(f, "10 microseconds")
            }
            IbTimeout::T30us => {
                write!(f, "30 microseconds")
            }
            IbTimeout::T100us => {
                write!(f, "100 microseconds")
            }
            IbTimeout::T300us => {
                write!(f, "300 microseconds")
            }
            IbTimeout::T1ms => {
                write!(f, "1 millisecond")
            }
            IbTimeout::T3ms => {
                write!(f, "3 milliseconds")
            }
            IbTimeout::T10ms => {
                write!(f, "10 milliseconds")
            }
            IbTimeout::T30ms => {
                write!(f, "30 milliseconds")
            }
            IbTimeout::T100ms => {
                write!(f, "100 milliseconds")
            }
            IbTimeout::T300ms => {
                write!(f, "300 milliseconds")
            }
            IbTimeout::T1s => {
                write!(f, "1 second")
            }
            IbTimeout::T3s => {
                write!(f, "3 seconds")
            }
            IbTimeout::T10s => {
                write!(f, "10 seconds")
            }
            IbTimeout::T30s => {
                write!(f, "30 seconds")
            }
            IbTimeout::T100s => {
                write!(f, "100 seconds")
            }
            IbTimeout::T300s => {
                write!(f, "300 seconds")
            }
            IbTimeout::T1000s => {
                write!(f, "1000 seconds")
            }
        }
    }
}

impl IbTimeout {
    pub(crate) fn as_timeout(&self) -> c_int {
        match self {
            IbTimeout::TNone => 0,
            IbTimeout::T10us => 1,
            IbTimeout::T30us => 2,
            IbTimeout::T100us => 3,
            IbTimeout::T300us => 4,
            IbTimeout::T1ms => 5,
            IbTimeout::T3ms => 6,
            IbTimeout::T10ms => 7,
            IbTimeout::T30ms => 8,
            IbTimeout::T100ms => 9,
            IbTimeout::T300ms => 10,
            IbTimeout::T1s => 11,
            IbTimeout::T3s => 12,
            IbTimeout::T10s => 13,
            IbTimeout::T30s => 14,
            IbTimeout::T100s => 15,
            IbTimeout::T300s => 16,
            IbTimeout::T1000s => 17,
        }
    }

    pub(crate) fn as_duration(&self) -> Duration {
        match self {
            IbTimeout::TNone => Duration::MAX,
            IbTimeout::T10us => Duration::from_micros(10),
            IbTimeout::T30us => Duration::from_micros(30),
            IbTimeout::T100us => Duration::from_micros(100),
            IbTimeout::T300us => Duration::from_micros(300),
            IbTimeout::T1ms => Duration::from_millis(1),
            IbTimeout::T3ms => Duration::from_millis(3),
            IbTimeout::T10ms => Duration::from_millis(10),
            IbTimeout::T30ms => Duration::from_millis(30),
            IbTimeout::T100ms => Duration::from_millis(100),
            IbTimeout::T300ms => Duration::from_millis(300),
            IbTimeout::T1s => Duration::from_secs(1),
            IbTimeout::T3s => Duration::from_secs(3),
            IbTimeout::T10s => Duration::from_secs(10),
            IbTimeout::T30s => Duration::from_secs(30),
            IbTimeout::T100s => Duration::from_secs(100),
            IbTimeout::T300s => Duration::from_secs(300),
            IbTimeout::T1000s => Duration::from_secs(1000),
        }
    }

    /// Returns the smallest timeout value larger or equal to provided value
    pub fn closest_from(timeout: Duration) -> Self {
        for tmo in [
            IbTimeout::T10us,
            IbTimeout::T30us,
            IbTimeout::T100us,
            IbTimeout::T300us,
            IbTimeout::T1ms,
            IbTimeout::T3ms,
            IbTimeout::T10ms,
            IbTimeout::T30ms,
            IbTimeout::T100ms,
            IbTimeout::T300ms,
            IbTimeout::T1s,
            IbTimeout::T3s,
            IbTimeout::T10s,
            IbTimeout::T30s,
            IbTimeout::T100s,
            IbTimeout::T300s,
            IbTimeout::T1000s,
        ] {
            if tmo.as_duration() >= timeout {
                return tmo;
            }
        }
        log::warn!(
            "It is not possible to set a timeout larger than 1000s. There will be no timeout."
        );
        IbTimeout::TNone
    }
}

#[derive(Copy, Clone)]
pub struct PrimaryAddress {
    pad: c_int,
}

impl PrimaryAddress {
    pub fn new(pad: c_int) -> Result<PrimaryAddress, GpibError> {
        if pad >= 0 && pad <= 30 {
            Ok(PrimaryAddress { pad })
        } else {
            Err(GpibError::ValueError(format!(
                "Primary address must be between 0 and 30. Got: {}.",
                pad
            )))
        }
    }

    pub(crate) fn as_pad(&self) -> c_int {
        self.pad
    }
}

impl fmt::Display for PrimaryAddress {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.pad)
    }
}

#[derive(Copy, Clone)]
pub struct SecondaryAddress {
    sad: c_int,
}

impl SecondaryAddress {
    pub fn new(sad: c_int) -> Result<SecondaryAddress, GpibError> {
        let desc = "Secondary address must be between 0 and 30 (without the 0x60 prefix), or equivalently between 0x60 and 0x7e (with the 0x60 addition). sad = 0 disables secondary address.";
        let sad = if sad < 0 {
            return Err(GpibError::ValueError(desc.to_owned()));
        } else if sad == 0 {
            // disable secondary address
            sad
        } else if sad <= 30 {
            // sad are between 0 and 30 but
            // NI convention adds 0x60 to the secondary address
            sad + 0x60
        } else if sad >= 0x60 && sad <= 0x7e {
            sad
        } else {
            return Err(GpibError::ValueError(desc.to_owned()));
        };
        Ok(SecondaryAddress { sad })
    }

    pub(crate) fn as_sad(&self) -> c_int {
        self.sad
    }
}

impl Default for SecondaryAddress {
    fn default() -> SecondaryAddress {
        SecondaryAddress { sad: 0 }
    }
}

impl fmt::Display for SecondaryAddress {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.sad)
    }
}

#[derive(Copy, Clone)]
pub enum IbSendEOI {
    Disabled,
    Enabled(c_int),
}

impl IbSendEOI {
    pub(crate) fn as_eot(&self) -> c_int {
        match self {
            IbSendEOI::Disabled => 0,
            IbSendEOI::Enabled(val) => *val,
        }
    }
}

impl Default for IbSendEOI {
    fn default() -> IbSendEOI {
        IbSendEOI::Disabled
    }
}

impl fmt::Display for IbSendEOI {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            IbSendEOI::Disabled => {
                write!(f, "IbSendEOI::Disabled")
            }
            IbSendEOI::Enabled(value) => {
                write!(f, "IbSendEOI::Enabled({})", value)
            }
        }
    }
}

#[derive(Copy, Clone)]
pub struct IbEosMode {
    pub reos: bool,
    pub xeos: bool,
    pub bin: bool,
    pub eos_char: u8,
}

impl fmt::Display for IbEosMode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut description = String::new();
        if self.reos {
            description.push_str("REOS ");
        }
        if self.xeos {
            description.push_str("XEOS ");
        }
        if self.bin {
            description.push_str("BIN");
        }
        if description.len() > 0 {
            description.push_str(&format!("{}", self.eos_char));
            write!(f, "EosMod({description})")
        } else {
            write!(f, "EosMod(No flag set)")
        }
    }
}

impl fmt::Debug for IbEosMode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut description = String::new();
        if self.reos {
            description.push_str(
                "REOS (0x400) Enable termination of reads when eos character is received. ",
            );
        }
        if self.xeos {
            description.push_str("XEOS (0x800) Assert the EOI line whenever the eos character is sent during writes. ");
        }
        if self.bin {
            description.push_str("BIN (0x1000) Match eos character using all 8 bits (instead of only looking at the 7 least significant bits). ");
        }
        if description.len() > 0 {
            description.push_str("End-of-char = {self.eos_char}");
            write!(f, "EosMod({description})")
        } else {
            write!(f, "EosMod(No flag set)")
        }
    }
}

impl IbEosMode {
    pub fn as_mode(&self) -> c_int {
        let mut mode: c_int = 0;
        if self.reos {
            mode = mode | 0x400;
        }
        if self.xeos {
            mode = mode | 0x800;
        }
        if self.bin {
            mode = mode | 0x1000;
        }
        mode | (self.eos_char as c_int)
    }
}

impl Default for IbEosMode {
    fn default() -> IbEosMode {
        IbEosMode {
            reos: true,
            xeos: false,
            bin: false,
            eos_char: b'\n',
        }
    }
}

pub enum IbEvent {
    None,
    DevTrg,
    DevClr,
    IFC,
}

impl fmt::Display for IbEvent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            IbEvent::None => {
                write!(f, "None")
            }
            IbEvent::DevTrg => {
                write!(f, "DevTrg")
            }
            IbEvent::DevClr => {
                write!(f, "DevClr")
            }
            IbEvent::IFC => {
                write!(f, "IFC")
            }
        }
    }
}

impl fmt::Debug for IbEvent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            IbEvent::None => {
                write!(f, "None (The board's event queue is empty)")
            }
            IbEvent::DevTrg => {
                write!(
                    f,
                    "DevTrg (The board has received a trigger command from the controller-in-charge)"
                )
            }
            IbEvent::DevClr => {
                write!(
                    f,
                    "DevClr (The board has received a clear command from the controller-in-charge)"
                )
            }
            IbEvent::IFC => {
                write!(
                    f,
                    "IFC (The board has received an interface clear from the system controller. Note, some models of GPIB interface board lack the ability to report interface clear events)"
                )
            }
        }
    }
}

impl IbEvent {
    pub(crate) fn from_value(value: c_short) -> Result<IbEvent, GpibError> {
        match value {
            0 => Ok(IbEvent::None),
            1 => Ok(IbEvent::DevTrg),
            2 => Ok(IbEvent::DevClr),
            3 => Ok(IbEvent::IFC),
            other => Err(GpibError::ValueError(format!(
                "Unexpected value ({}) for event.",
                other,
            ))),
        }
    }
}

pub struct IbLineStatus {
    pub valid_dav: bool,
    pub valid_ndac: bool,
    pub valid_nrfd: bool,
    pub valid_ifc: bool,
    pub valid_ren: bool,
    pub valid_srq: bool,
    pub valid_atn: bool,
    pub valid_eoi: bool,
    pub bus_dav: bool,
    pub bus_ndac: bool,
    pub bus_nrfd: bool,
    pub bus_ifc: bool,
    pub bus_ren: bool,
    pub bus_srq: bool,
    pub bus_atn: bool,
    pub bus_eoi: bool,
}

impl IbLineStatus {
    pub(crate) fn from_line_status(line_status: c_short) -> IbLineStatus {
        let valid_dav = (line_status & 0x1) != 0;
        let valid_ndac = (line_status & 0x2) != 0;
        let valid_nrfd = (line_status & 0x4) != 0;
        let valid_ifc = (line_status & 0x8) != 0;
        let valid_ren = (line_status & 0x10) != 0;
        let valid_srq = (line_status & 0x20) != 0;
        let valid_atn = (line_status & 0x40) != 0;
        let valid_eoi = (line_status & 0x80) != 0;
        let bus_dav = (line_status & 0x100) != 0;
        let bus_ndac = (line_status & 0x200) != 0;
        let bus_nrfd = (line_status & 0x400) != 0;
        let bus_ifc = (line_status & 0x800) != 0;
        let bus_ren = (line_status & 0x1000) != 0;
        let bus_srq = (line_status & 0x2000) != 0;
        let bus_atn = (line_status & 0x4000) != 0;
        let bus_eoi = (line_status & 0x8000u16 as i16) != 0;
        Self {
            valid_dav,
            valid_ndac,
            valid_nrfd,
            valid_ifc,
            valid_ren,
            valid_srq,
            valid_atn,
            valid_eoi,
            bus_dav,
            bus_ndac,
            bus_nrfd,
            bus_ifc,
            bus_ren,
            bus_srq,
            bus_atn,
            bus_eoi,
        }
    }
}

pub enum IbOnline {
    Close,
    Reset(c_int),
}

impl IbOnline {
    pub(crate) fn as_online(&self) -> c_int {
        match self {
            IbOnline::Close => 0,
            IbOnline::Reset(val) => *val,
        }
    }
}

impl fmt::Display for IbOnline {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            IbOnline::Close => {
                write!(f, "IbOnline::Close")
            }
            IbOnline::Reset(val) => {
                write!(f, "IbOnline::Reset({})", val)
            }
        }
    }
}