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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
// SPDX-FileCopyrightText: 2021 Kent Gibson <warthog618@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use bitflags::bitflags;
use bitmaps::Bitmap;
use std::convert::TryFrom;
use std::fmt;
use std::fs::File;
use std::mem;
use std::os::unix::prelude::{FromRawFd, RawFd};
use std::time::Duration;

use super::common::{ValidationResult, IOCTL_MAGIC};

// common to ABI v1 and v2.
pub use super::common::{
    get_chip_info, unwatch_line_info, ChipInfo, LineEdgeEventKind, LineInfoChangeKind, Offset,
    Offsets, Padding, UnderReadError, ValidationError,
};
use super::{Error, Name, Result};

#[repr(u8)]
enum Ioctl {
    GetLineInfo = 5,
    WatchLineInfo = 6,
    GetLine = 7,
    SetLineConfig = 0xD,
    GetLineValues = 0xE,
    SetLineValues = 0xF,
}

bitflags! {
    /// Flags indicating the configuration of a line.
    #[derive(Default)]
    pub struct LineFlags: u64 {
        /// The line is in use and is not available for request.
        const USED = 1;

        /// The line active state corresponds to a physical low.
        const ACTIVE_LOW = 2;

        /// The line is an input.
        const INPUT = 4;

        /// The line is an output.
        const OUTPUT = 8;

        /// The line detects rising (*inactive* to *active*) edges.
        const EDGE_RISING = 16;

        /// The line detects falling (*active* to *inactive*) edges.
        const EDGE_FALLING = 32;

        /// The line is an open drain output.
        const OPEN_DRAIN = 64;

        /// The line is an open source output.
        const OPEN_SOURCE = 128;

        /// The line has pull-up bias enabled.
        const BIAS_PULL_UP = 256;

        /// The line has pull-down bias enabled.
        const BIAS_PULL_DOWN = 512;

        /// The line has bias disabled.
        const BIAS_DISABLED = 1024;

        /// The line events contain **CLOCK_REALTIME** timestamps.
        const EVENT_CLOCK_REALTIME = 2048;

        /// The line events contain **HTE** timestamps.
        const EVENT_CLOCK_HTE = 4096;
    }
}

/// Values of GPIO lines.
///
/// Bits in the bitmaps correspond to the index into [`LineRequest.offsets`].
/// The first requested line, `offsets[0]`, is bit 0.
///
/// [`LineRequest.offsets`]: struct@LineRequest
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct LineValues {
    /// The value of the lines, set to 1 for *active* and 0 for *inactive*.
    pub bits: Bitmap<64>,

    /// The lines in a request to access, set to 1 to access and 0 to ignore.
    pub mask: Bitmap<64>,
}

impl LineValues {
    /// Create values from a slice.
    ///
    /// The values are in the same order as [`LineRequest.offsets`].
    ///
    /// [`LineRequest.offsets`]: struct@LineRequest
    pub fn from_slice(s: &[bool]) -> Self {
        let mut lv: LineValues = Default::default();
        for (idx, val) in s.iter().enumerate() {
            lv.set(idx, *val);
        }
        lv
    }
    /// Return the value of a line.
    ///
    /// Note that the [`LineValues`] need to be populated via a call to [`get_line_values`]
    /// to get values from the underlying hardware.
    ///
    /// Fails if the line of interest is not set in the mask.
    ///
    /// * `idx` - The index into the [`LineRequest.offsets`] for the line of interest.
    ///
    /// [`LineRequest.offsets`]: struct@LineRequest
    pub fn get(&self, idx: usize) -> Option<bool> {
        if !self.mask.get(idx) {
            return None;
        }
        Some(self.bits.get(idx))
    }

    /// Set the value of a line.
    ///
    /// Note that the values are not applied to hardware until passed to [`set_line_values`].
    ///
    /// * `idx` - The index into the [`LineRequest.offsets`] for the line of interest.
    /// * `active` - The logical state of the line to be set.
    ///
    /// [`LineRequest.offsets`]: struct@LineRequest
    pub fn set(&mut self, idx: usize, active: bool) {
        self.bits.set(idx, active);
        self.mask.set(idx, true);
    }

    /// Clear the mask bit for a line.
    ///
    /// The line will be ignored in subsequent calls to [`get_line_values`] and
    /// [`set_line_values`].
    ///
    /// * `idx` - The index into the [`LineRequest.offsets`] for the line of interest.
    ///
    /// [`LineRequest.offsets`]: struct@LineRequest
    pub fn unset_mask(&mut self, idx: usize) {
        self.mask.set(idx, false);
    }
}

/// Read values of requested lines.
///
/// * `lfd` - The fd of the file returned by [`get_line`].
/// * `lv` - The line values to be populated.
pub fn get_line_values(lfd: RawFd, lv: &mut LineValues) -> Result<()> {
    // SAFETY: returned struct contains raw byte arrays and bitfields that are safe to decode.
    match unsafe {
        libc::ioctl(
            lfd,
            nix::request_code_readwrite!(
                IOCTL_MAGIC,
                Ioctl::GetLineValues,
                mem::size_of::<LineValues>()
            ),
            lv,
        )
    } {
        0 => Ok(()),
        _ => Err(Error::from(errno::errno())),
    }
}

/// Set values of requested output lines.
///
/// Note that requesting a set on an input line is an error.
///
/// * `lfd` - The fd of the file returned by [`get_line`].
/// * `lv` - The line values to be set.
pub fn set_line_values(lfd: RawFd, lv: &LineValues) -> Result<()> {
    // SAFETY: lv is not modified.
    match unsafe {
        libc::ioctl(
            lfd,
            nix::request_code_readwrite!(
                IOCTL_MAGIC,
                Ioctl::SetLineValues,
                mem::size_of::<LineValues>()
            ),
            lv,
        )
    } {
        0 => Ok(()),
        _ => Err(Error::from(errno::errno())),
    }
}

/// An identifier for which field of the [`LineAttributeValueUnion`] is in use.
#[repr(u32)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LineAttributeKind {
    /// The attribute is *inactive* - no fields are in use.
    Unused = 0,

    /// The flags field is in use.
    Flags = 1,

    /// The values field is in use.
    Values = 2,

    /// The debounce_period_us field is in use.
    Debounce = 3,
}

impl Default for LineAttributeKind {
    fn default() -> Self {
        LineAttributeKind::Unused
    }
}

impl TryFrom<u32> for LineAttributeKind {
    type Error = String;

    fn try_from(v: u32) -> std::result::Result<Self, Self::Error> {
        use LineAttributeKind::*;
        Ok(match v {
            x if x == Unused as u32 => Unused,
            x if x == Flags as u32 => Flags,
            x if x == Values as u32 => Values,
            x if x == Debounce as u32 => Debounce,
            x => return Err(format!("invalid value: {}", x)),
        })
    }
}

impl LineAttributeKind {
    /// Confirm that the value read from the kernel is valid in Rust.
    fn validate(&self) -> std::result::Result<(), String> {
        LineAttributeKind::try_from(*self as u32).map(|_i| ())
    }
}

/// A configurable attribute of a line.
#[repr(C)]
#[derive(Clone, Copy, Default)]
pub struct LineAttribute {
    /// The type of attribute stored in `value`.
    pub kind: LineAttributeKind,

    /// Reserved for future use and must be zero filled.
    #[doc(hidden)]
    pub padding: Padding<1>,

    /// The attribute value.
    pub value: LineAttributeValueUnion,
}

impl LineAttribute {
    /// Set the attribute as debounce period.
    pub fn set_debounce_period_us(&mut self, debounce_period_us: u32) {
        self.kind = LineAttributeKind::Debounce;
        self.value.debounce_period_us = debounce_period_us;
    }

    /// Set the attribute as flags.
    pub fn set_flags(&mut self, flags: LineFlags) {
        self.kind = LineAttributeKind::Flags;
        self.value.flags = flags;
    }

    /// Set the attribute as output values.
    pub fn set_values(&mut self, values: Bitmap<64>) {
        self.kind = LineAttributeKind::Values;
        self.value.values = values;
    }

    /// Get the contained value.
    ///
    /// Converts the unsafe kind/union into a safe enum.
    pub fn to_value(&self) -> Option<LineAttributeValue> {
        // SAFETY: checks kind before accessing union
        unsafe {
            Some(match self.kind {
                LineAttributeKind::Unused => return None,
                LineAttributeKind::Flags => LineAttributeValue::Flags(self.value.flags),
                LineAttributeKind::Values => LineAttributeValue::Values(self.value.values),
                LineAttributeKind::Debounce => LineAttributeValue::DebouncePeriod(
                    Duration::from_micros(self.value.debounce_period_us as u64),
                ),
            })
        }
    }
}

impl fmt::Debug for LineAttribute {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // SAFETY: checks kind before accessing union
        unsafe {
            match self.kind {
                LineAttributeKind::Unused => write!(f, "unused"),
                LineAttributeKind::Flags => write!(f, "flags: {:?}", self.value.flags),
                LineAttributeKind::Values => {
                    write!(f, "values: {:08x}", self.value.values.as_value())
                }
                LineAttributeKind::Debounce => {
                    write!(f, "debounce_period_us: {}", self.value.debounce_period_us)
                }
            }
        }
    }
}

impl PartialEq for LineAttribute {
    fn eq(&self, other: &Self) -> bool {
        if self.kind != other.kind {
            return false;
        }
        // SAFETY: checks kind before accessing union
        unsafe {
            match self.kind {
                LineAttributeKind::Unused => true,
                LineAttributeKind::Flags => self.value.flags == other.value.flags,
                LineAttributeKind::Values => self.value.values == other.value.values,
                LineAttributeKind::Debounce => {
                    self.value.debounce_period_us == other.value.debounce_period_us
                }
            }
        }
    }
}
impl Eq for LineAttribute {}

/// The value of a particular line attribute.
#[repr(C)]
#[derive(Clone, Copy)]
pub union LineAttributeValueUnion {
    /// The line configuration flags.
    pub flags: LineFlags,

    /// The values to which the lines will be set, with each bit number
    ///  corresponding to the index into [`LineRequest.offsets`].
    ///
    /// [`LineRequest.offsets`]: struct@LineRequest
    pub values: Bitmap<64>,

    /// The debounce period, in microseconds.
    pub debounce_period_us: u32,
}

impl Default for LineAttributeValueUnion {
    fn default() -> Self {
        LineAttributeValueUnion {
            flags: Default::default(),
        }
    }
}

/// The attribute value contained within a [`LineAttribute`].
pub enum LineAttributeValue {
    /// The debounce period attribute as a Duration.
    DebouncePeriod(Duration),

    /// The configuration flags.
    Flags(LineFlags),

    /// The line values.
    Values(Bitmap<64>),
}

/// A configuration attribute associated with one or more of the requested lines.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct LineConfigAttribute {
    /// The configurable attribute.
    pub attr: LineAttribute,

    /// The lines to which the attribute applies, with each bit number corresponding
    /// to the index into [`LineRequest.offsets`].
    ///
    /// [`LineRequest.offsets`]: struct@LineRequest
    pub mask: Bitmap<64>,
}

/// The set of additional configuration attributes for a line request.
///
/// [`LineConfig.num_attrs`] specifies the number of entries in use.
///
/// Any attribute should only be associated with a particular line once.
/// If an attribute is associated with a line multiple times then the
/// first occurrence (i.e. lowest index) has precedence.
///
/// [`LineConfig.num_attrs`]: struct@LineConfig
#[repr(C)]
#[derive(Clone, Debug, Default)]
pub struct LineConfigAttributes(pub [LineConfigAttribute; NUM_ATTRS_MAX]);

/// Configuration for a set of requested lines.
#[repr(C)]
#[derive(Clone, Debug, Default)]
pub struct LineConfig {
    /// Flags for the GPIO lines.  This is the default for all requested lines but
    /// may be overridden for particular lines using `attrs`.
    pub flags: LineFlags,

    /// The number of attributes active in `attrs`.
    pub num_attrs: u32,

    /// Reserved for future use and must be zero filled.
    #[doc(hidden)]
    pub padding: Padding<5>,

    /// The configuration attributes associated with the requested lines.
    ///
    /// The number of active attributes in the array is specified by `num_attrs`.
    pub attrs: LineConfigAttributes,
}

impl LineConfig {
    /// The nth attribute in the attrs
    pub fn attr(&self, idx: usize) -> &LineConfigAttribute {
        &self.attrs.0[idx]
    }

    /// The nth attribute in the attrs
    pub fn attr_mut(&mut self, idx: usize) -> &mut LineConfigAttribute {
        &mut self.attrs.0[idx]
    }
}

/// Update the configuration of an existing line request.
///
/// * `lfd` - The fd of the file returned by [`get_line`].
/// * `lc` - The configuration to be applied.
pub fn set_line_config(lfd: RawFd, lc: LineConfig) -> Result<()> {
    // SAFETY: lc is consumed.
    unsafe {
        match libc::ioctl(
            lfd,
            nix::request_code_readwrite!(
                IOCTL_MAGIC,
                Ioctl::SetLineConfig,
                mem::size_of::<LineConfig>()
            ),
            &lc,
        ) {
            0 => Ok(()),
            _ => Err(Error::from(errno::errno())),
        }
    }
}

/// Information about a request for GPIO lines.
#[repr(C)]
#[derive(Clone, Debug, Default)]
pub struct LineRequest {
    /// An array of requested lines, identified by offset on the associated GPIO chip.
    pub offsets: Offsets,

    /// The requested consumer label for the selected GPIO lines such as
    /// "*my-bitbanged-relay*".
    pub consumer: Name,

    /// The requested configuration for the lines.
    pub config: LineConfig,

    /// The number of lines requested in this request.
    /// i.e. the number of valid elements in `offsets`.
    ///
    /// Set to 1 to request a single line.
    pub num_lines: u32,

    /// A suggested minimum number of line events that the kernel should buffer.
    ///
    /// This is only relevant if edge detection is enabled in the configuration.
    ///
    /// Note that this is only a suggested value and the kernel may allocate a
    /// larger buffer or cap the size of the buffer.
    /// If this field is zero then the buffer size defaults to a minimum of `num_lines*16`.
    pub event_buffer_size: u32,

    /// Reserved for future use and must be zero filled.
    #[doc(hidden)]
    pub padding: Padding<5>,

    /// This field is only present for the underlying ioctl call and is only used internally.
    #[doc(hidden)]
    pub fd: i32,
}

/// Request a line or set of lines for exclusive access.
///
/// * `cfd` - The fd of the open chip.
/// * `lr` - The line request.
pub fn get_line(cfd: RawFd, lr: LineRequest) -> Result<File> {
    // SAFETY: lr is consumed and the returned file is drawn from the returned fd.
    unsafe {
        match libc::ioctl(
            cfd,
            nix::request_code_readwrite!(
                IOCTL_MAGIC,
                Ioctl::GetLine,
                mem::size_of::<LineRequest>()
            ),
            &lr,
        ) {
            0 => Ok(File::from_raw_fd(lr.fd)),
            _ => Err(Error::from(errno::errno())),
        }
    }
}

/// The set of potential configuration attributes for a line.
///
/// [`LineInfo.num_attrs`] specifies the number of entries in use.
///
/// [`LineInfo.num_attrs`]: struct@LineInfo
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct LineAttributes([LineAttribute; NUM_ATTRS_MAX]);

/// The capacity of [`LineAttributes`] and [`LineConfigAttributes`] arrays.
pub const NUM_ATTRS_MAX: usize = 10;

/// Information about a certain GPIO line.
#[repr(C)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct LineInfo {
    /// The name of this GPIO line, such as the output pin of the line on the chip,
    /// a rail or a pin header name on a board, as specified by the GPIO chip.
    ///
    /// May be empty.
    pub name: Name,

    /// A functional name for the consumer of this GPIO line as set by whatever is using it.
    ///
    /// Will be empty if there is no current user.
    /// May also be empty if the consumer requests doesn't set this up.
    pub consumer: Name,

    /// The local offset on this GPIO chip.
    pub offset: Offset,

    /// The number of attributes active in `attrs`.
    pub num_attrs: u32,

    /// The configuration flags for this GPIO line.
    pub flags: LineFlags,

    /// Additilnal configuration attributes associated with the line.
    ///
    /// The number of active attributes in the array is specified by `num_attrs`.
    pub attrs: LineAttributes,

    /// Reserved for future use.
    #[doc(hidden)]
    pub padding: Padding<4>,
}

impl LineInfo {
    /// The nth attribute in the attrs
    pub fn attr(&self, idx: usize) -> &LineAttribute {
        &self.attrs.0[idx]
    }
    /// Check that a LineInfo read from the kernel is valid in Rust.
    fn validate(&self) -> ValidationResult {
        if self.num_attrs > NUM_ATTRS_MAX as u32 {
            return Err(ValidationError::new(
                "num_attrs",
                format!("out of range: {}", self.num_attrs),
            ));
        }
        for i in 0..NUM_ATTRS_MAX {
            if let Err(e) = self.attrs.0[i].kind.validate() {
                return Err(ValidationError::new(format!("attrs[{}].kind", i), e));
            }
        }
        Ok(())
    }
}

/// Get the publicly available information for a line.
///
/// This does not include the line value.
/// The line must be requested to access the value.
///
/// * `cfd` - The fd of the open chip.
/// * `offset` - The offset of the line.
pub fn get_line_info(cfd: RawFd, offset: Offset) -> Result<LineInfo> {
    let li = LineInfo {
        offset,
        ..Default::default()
    };
    // SAFETY: returned struct is explicitly validated before being returned.
    match unsafe {
        libc::ioctl(
            cfd,
            nix::request_code_readwrite!(
                IOCTL_MAGIC,
                Ioctl::GetLineInfo,
                mem::size_of::<LineInfo>()
            ),
            &li,
        )
    } {
        0 => li.validate().map(|_| li).map_err(Error::from),
        _ => Err(Error::from(errno::errno())),
    }
}

/// Add a watch on changes to the [`LineInfo`] for a line.
///
/// Returns the current state of that information.
///
/// This does not include the line value.
/// The line must be requested to access the value.
///
/// * `cfd` - The fd of the open chip.
/// * `offset` - The offset of the line to watch.
pub fn watch_line_info(cfd: RawFd, offset: Offset) -> Result<LineInfo> {
    let li = LineInfo {
        offset,
        ..Default::default()
    };
    // SAFETY: returned struct is explicitly validated before being returned.
    match unsafe {
        libc::ioctl(
            cfd,
            nix::request_code_readwrite!(
                IOCTL_MAGIC,
                Ioctl::WatchLineInfo,
                mem::size_of::<LineInfo>()
            ),
            &li,
        )
    } {
        0 => li.validate().map(|_| li).map_err(Error::from),
        _ => Err(Error::from(errno::errno())),
    }
}

/// An event indicating a change to the info for a line.
#[repr(C)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LineInfoChangeEvent {
    /// The new line info.
    pub info: LineInfo,

    /// The best estimate of time of event occurrence, in nanoseconds.
    pub timestamp_ns: u64,

    /// The trigger for the change.
    pub kind: LineInfoChangeKind,

    /// Reserved for future use.
    #[doc(hidden)]
    pub padding: Padding<5>,
}

impl LineInfoChangeEvent {
    /// Read an info change event from a buffer.
    ///
    /// The buffer is assumed to have been populated by a read of the chip File,
    /// so the content is validated before being returned.
    pub fn from_slice(d: &[u8]) -> Result<&LineInfoChangeEvent> {
        if d.len() < mem::size_of::<LineInfoChangeEvent>() {
            return Err(Error::from(UnderReadError::new(
                "LineInfoChangeEvent",
                mem::size_of::<LineInfoChangeEvent>(),
                d.len(),
            )));
        }
        // SAFETY: returned struct is explicitly validated before being returned.
        let ice = unsafe { &*(d as *const [u8] as *const LineInfoChangeEvent) };
        ice.validate().map(|_| ice).map_err(Error::from)
    }

    /// Check that a LineInfoChangeEvent read from the kernel is valid in Rust.
    fn validate(&self) -> ValidationResult {
        self.kind
            .validate()
            .map_err(|e| ValidationError::new("kind", e))
    }
}

/// Information about an edge event on a requested line.
#[repr(C)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LineEdgeEvent {
    /// The best estimate of time of event occurrence, in nanoseconds.
    ///
    /// By default the timestamp is read from **CLOCK_MONOTONIC** and is
    /// intended to allow the accurate measurement of the time between events.
    /// It does not provide the wall-clock time.
    ///
    /// If the [`LineFlags::EVENT_CLOCK_REALTIME`] flag is set then the
    /// timestamp is read from **CLOCK_REALTIME**.
    pub timestamp_ns: u64,

    /// The event trigger identifier.
    pub kind: LineEdgeEventKind,

    /// The offset of the line that triggered the event.
    pub offset: Offset,

    /// The sequence number for this event in the sequence of events for all
    /// the lines in this line request.
    pub seqno: u32,

    /// The sequence number for this event in the sequence of events on this
    /// particular line.
    pub line_seqno: u32,

    /// Reserved for future use.
    #[doc(hidden)]
    pub padding: Padding<6>,
}

impl LineEdgeEvent {
    /// Read an edge event from a buffer.
    ///
    /// The buffer is assumed to have been populated by a read of the line request File,
    /// so the content is validated before being returned.
    pub fn from_slice(d: &[u8]) -> Result<&LineEdgeEvent> {
        if d.len() < mem::size_of::<LineEdgeEvent>() {
            return Err(Error::from(UnderReadError::new(
                "LineEdgeEvent",
                mem::size_of::<LineEdgeEvent>(),
                d.len(),
            )));
        }
        // SAFETY: returned struct is explicitly validated before being returned.
        let le = unsafe { &*(d as *const [u8] as *const LineEdgeEvent) };
        le.validate().map(|_| le).map_err(Error::from)
    }

    /// Check that a LineEdgeEvent read from the kernel is valid in Rust.
    fn validate(&self) -> ValidationResult {
        self.kind
            .validate()
            .map_err(|e| ValidationError::new("kind", e))
    }
}

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

    mod line_attribute {
        use super::LineAttribute;

        #[test]
        fn size() {
            assert_eq!(
                super::mem::size_of::<LineAttribute>(),
                16usize,
                concat!("Size of: ", stringify!(LineAttribute))
            );
        }
    }

    mod line_attribute_value_union {
        use super::LineAttributeValueUnion;

        #[test]
        fn size() {
            assert_eq!(
                super::mem::size_of::<LineAttributeValueUnion>(),
                8usize,
                concat!("Size of: ", stringify!(LineAttributeValueUnion))
            );
        }
    }

    mod line_config_attribute {
        use super::LineConfigAttribute;

        #[test]
        fn line_config_attribute() {
            assert_eq!(
                super::mem::size_of::<LineConfigAttribute>(),
                24usize,
                concat!("Size of: ", stringify!(LineConfigAttribute))
            );
        }
    }

    mod line_config {
        use super::LineConfig;

        #[test]
        fn line_config() {
            assert_eq!(
                super::mem::size_of::<LineConfig>(),
                272usize,
                concat!("Size of: ", stringify!(LineConfig))
            );
        }
    }

    mod line_request {
        use super::LineRequest;

        #[test]
        fn line_request() {
            assert_eq!(
                super::mem::size_of::<LineRequest>(),
                592usize,
                concat!("Size of: ", stringify!(LineRequest))
            );
        }
    }

    mod line_values {
        use super::{Bitmap, LineValues};

        #[test]
        fn get() {
            let mut a = LineValues::default();
            for idx in [0, 2] {
                assert!(!a.bits.get(idx), "idx: {}", idx);
                assert!(a.get(idx).is_none(), "idx: {}", idx);

                a.mask.set(idx, true);
                assert!(!a.get(idx).unwrap(), "idx: {}", idx);

                a.bits.set(idx, true);
                assert!(a.get(idx).unwrap(), "idx: {}", idx);
            }
        }

        #[test]
        fn set() {
            let mut a = LineValues::default();
            for idx in [0, 2] {
                a.set(idx, false);
                assert!(a.mask.get(idx), "idx: {}", idx);
                assert!(!a.bits.get(idx), "idx: {}", idx);

                a.set(idx, true);
                assert!(a.mask.get(idx), "idx: {}", idx);
                assert!(a.bits.get(idx), "idx: {}", idx);
            }
        }

        #[test]
        fn unset_mask() {
            let mut a = LineValues {
                mask: Bitmap::mask(6),
                ..Default::default()
            };
            assert_eq!(a.mask.len(), 6);
            assert!(a.mask.get(0));

            a.unset_mask(0);
            assert!(!a.mask.get(0));
            assert_eq!(a.mask.len(), 5);
            assert!(a.mask.get(3));

            a.unset_mask(3);
            assert!(!a.mask.get(3));
            assert_eq!(a.mask.len(), 4);
            assert!(a.mask.get(5));

            a.unset_mask(5);
            assert!(!a.mask.get(5));
            assert_eq!(a.mask.len(), 3);
            assert!(!a.mask.get(6));

            a.unset_mask(5);
            assert!(!a.mask.get(5));
            assert_eq!(a.mask.len(), 3);
        }

        #[test]
        fn size() {
            assert_eq!(
                super::mem::size_of::<LineValues>(),
                16usize,
                concat!("Size of: ", stringify!(LineValues))
            );
        }
    }

    mod line_attribute_kind {
        use super::LineAttributeKind;

        #[test]
        fn validate() {
            let mut a = LineAttributeKind::Flags;
            assert!(a.validate().is_ok());

            a = LineAttributeKind::Unused;
            assert!(a.validate().is_ok());

            unsafe {
                a = *(&4 as *const i32 as *const LineAttributeKind);
                assert_eq!(a.validate().unwrap_err(), "invalid value: 4");
                a = *(&3 as *const i32 as *const LineAttributeKind);
                assert!(a.validate().is_ok());
            }
        }
    }

    mod line_info {
        use super::{LineAttributeKind, LineInfo, NUM_ATTRS_MAX};

        #[test]
        fn validate() {
            let mut a = LineInfo::default();
            assert!(a.validate().is_ok());

            a.num_attrs = NUM_ATTRS_MAX as u32;
            assert!(a.validate().is_ok());

            a.num_attrs += 1;
            let e = a.validate().unwrap_err();
            assert_eq!(e.field, "num_attrs");
            assert_eq!(e.msg, "out of range: 11");

            a.num_attrs = NUM_ATTRS_MAX as u32;
            for idx in [0, 1, 4, 7] {
                unsafe {
                    a.attrs.0[idx].kind = *(&4 as *const i32 as *const LineAttributeKind);
                }
                let e = a.validate().unwrap_err();
                assert_eq!(e.field, format!("attrs[{}].kind", idx));
                assert_eq!(e.msg, "invalid value: 4");
                a.attrs.0[idx].kind = LineAttributeKind::Unused;
            }
        }

        #[test]
        fn size() {
            assert_eq!(
                super::mem::size_of::<LineInfo>(),
                256usize,
                concat!("Size of: ", stringify!(LineInfo))
            );
        }
    }

    mod line_info_changed {
        use super::{LineInfoChangeEvent, LineInfoChangeKind};

        #[test]
        fn validate() {
            let mut a = LineInfoChangeEvent {
                info: Default::default(),
                timestamp_ns: 0,
                kind: LineInfoChangeKind::Released,
                padding: Default::default(),
            };
            assert!(a.validate().is_ok());

            a.timestamp_ns = 1234;
            assert!(a.validate().is_ok());
            unsafe {
                a.kind = *(&0 as *const i32 as *const LineInfoChangeKind);
                let e = a.validate().unwrap_err();
                assert_eq!(e.field, "kind");
                assert_eq!(e.msg, "invalid value: 0");

                a.kind = *(&4 as *const i32 as *const LineInfoChangeKind);
                let e = a.validate().unwrap_err();
                assert_eq!(e.field, "kind");
                assert_eq!(e.msg, "invalid value: 4");

                a.kind = *(&1 as *const i32 as *const LineInfoChangeKind);
                assert!(a.validate().is_ok());
            }
        }
        #[test]
        fn size() {
            assert_eq!(
                super::mem::size_of::<LineInfoChangeEvent>(),
                288usize,
                concat!("Size of: ", stringify!(LineInfoChangeEvent))
            );
        }
    }

    mod line_event {
        use super::{LineEdgeEvent, LineEdgeEventKind};

        #[test]
        fn validate() {
            let mut a = LineEdgeEvent {
                timestamp_ns: 0,
                kind: LineEdgeEventKind::RisingEdge,
                offset: 0,
                seqno: 0,
                line_seqno: 0,
                padding: Default::default(),
            };
            assert!(a.validate().is_ok());

            a.timestamp_ns = 1234;
            assert!(a.validate().is_ok());
            unsafe {
                a.kind = *(&0 as *const i32 as *const LineEdgeEventKind);
                let e = a.validate().unwrap_err();
                assert_eq!(e.field, "kind");
                assert_eq!(e.msg, "invalid value: 0");

                a.kind = *(&3 as *const i32 as *const LineEdgeEventKind);
                let e = a.validate().unwrap_err();
                assert_eq!(e.field, "kind");
                assert_eq!(e.msg, "invalid value: 3");

                a.kind = *(&1 as *const i32 as *const LineEdgeEventKind);
                assert!(a.validate().is_ok());
            }
        }

        #[test]
        fn size() {
            assert_eq!(
                super::mem::size_of::<LineEdgeEvent>(),
                48usize,
                concat!("Size of: ", stringify!(LineEdgeEvent))
            );
        }
    }
}