gpiocdev 0.8.0

Access GPIO lines on Linux using the GPIO character device
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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
/// SPDX-FileCopyrightText: 2021 Kent Gibson <warthog618@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
use crate::chip::Chip;
use crate::line::{self, Bias, Direction, Drive, EdgeDetection, EventClock, Offset, Value, Values};
use crate::request::{Config, Request};
#[cfg(feature = "uapi_v1")]
use crate::AbiVersion;
use crate::{Error, Result, UapiCall};
#[cfg(feature = "uapi_v1")]
use gpiocdev_uapi::v1;
#[cfg(feature = "uapi_v2")]
use gpiocdev_uapi::v2;
use gpiocdev_uapi::NUM_LINES_MAX;
use std::cmp::max;
use std::collections::HashMap;
use std::fs::File;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::time::Duration;

/// A builder of line requests.
///
/// Apply mutators to specify the request configuration, then use [`request`]
/// to request the lines from the kernel.
///
/// Most mutators operate on the request configuration as per the [`Config`].
///
/// # Examples
/// Request and read a basic input line:
/// ```no_run
/// # fn example() -> Result<(), gpiocdev::Error> {
/// let l3 = gpiocdev::Request::builder()
///     .on_chip("/dev/gpiochip0")
///     .with_line(3)
///     .request()?;
/// let value = l3.value(3)?;
/// # Ok(())
/// # }
/// ```
///
/// Several lines in one request:
///
/// ```no_run
/// # use gpiocdev::line::Values;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// let offsets = &[3,5];
/// let req = gpiocdev::Request::builder()
///     .on_chip("/dev/gpiochip0")
///     .with_lines(offsets)
///     .request()?;
/// let mut values = Values::from_offsets(offsets);
/// req.values(&mut values)?;
/// # Ok(())
/// # }
/// ```
/// Complex configurations are specified by chaining line selection and configuration mutators.
/// The configuration for a subset of lines is updated by selecting the lines and then calling
/// the appropriate mutators. If no lines are selected then the mutators modify the base configuration
/// that lines inherit when they are first added:
///
/// ```no_run
/// # use gpiocdev::line::{Bias::*, Value::*};
/// # use gpiocdev::request::Config;
///
/// # fn example() -> Result<(), gpiocdev::Error> {
/// let req = gpiocdev::Request::builder()
///     .on_chip("/dev/gpiochip0")
///     .as_input()
///     .with_bias(PullUp)
///     // -- base config ends here - just before lines are first added.
///     .with_lines(&[3, 5, 8]) // lines 3,5,8 will be input with pull-up bias...
///     // -- config added here would apply to lines 3,5 and 8
///     .with_line(3) // make line 3 pull-down instead...
///     .with_bias(PullDown)
///     .with_line(4) // and line 4 an output set inactive (and pull-up from the base config)
///     .as_output(Inactive)
///     .request()?;
/// let value = req.value(3)?;
/// # Ok(())
/// # }
/// ```
///
/// Complex configurations can be built separately and provided to the `Builder`:
///
/// ```no_run
/// # use gpiocdev::request::Config;
/// # use gpiocdev::line::Value;
/// # fn example() -> Result<(), gpiocdev::Error> {
/// let mut cfg = Config::default();
/// // build complex config (this just a simple example)...
/// cfg.with_line(5).as_output(Value::Active);
/// let req = gpiocdev::Request::from_config(cfg)
///     .on_chip("/dev/gpiochip0")
///     .request()?;
/// req.set_value(5,Value::Inactive)?;
/// # Ok(())
/// # }
/// ```
///
/// [`request`]: #method.request
#[derive(Clone, Default, Eq, PartialEq)]
#[cfg_attr(test, derive(Debug))]
pub struct Builder {
    pub(super) cfg: Config,
    pub(super) consumer: String,
    pub(super) kernel_event_buffer_size: u32,
    pub(super) user_event_buffer_size: usize,
    err: Option<Error>,
    /// The ABI version used to create the request, and so determines how to decode events.
    #[cfg(all(feature = "uapi_v1", feature = "uapi_v2"))]
    pub(super) abiv: Option<AbiVersion>,
}

impl Builder {
    /// Start building a new request using the provided config.
    pub(crate) fn from_config(cfg: Config) -> Self {
        Builder {
            cfg,
            ..Default::default()
        }
    }

    /// Perform the request.
    ///
    /// Sends the request to the kernel using the appropriate uAPI call.
    ///
    /// On success returns the [`Request`] that provides access to the requested lines.
    pub fn request(&mut self) -> Result<Request> {
        if let Some(e) = &self.err {
            return Err(e.clone());
        }
        if self.cfg.chip.as_os_str().is_empty() {
            return Err(Error::InvalidArgument("No chip specified.".into()));
        }
        let chip = Chip::from_path(&self.cfg.chip)?;
        self.cfg.offsets.sort_unstable();
        self.do_request(&chip).map(|f| self.to_request(f))
    }
    #[cfg(all(feature = "uapi_v1", feature = "uapi_v2"))]
    fn do_request(&mut self, chip: &Chip) -> Result<File> {
        if self.abiv.is_none() {
            self.abiv = Some(chip.detect_abi_version()?);
        }
        match self.to_uapi()? {
            UapiRequest::Handle(hr) => v1::get_line_handle(&chip.f, hr)
                .map_err(|e| Error::Uapi(UapiCall::GetLineHandle, e)),
            UapiRequest::Event(er) => {
                v1::get_line_event(&chip.f, er).map_err(|e| Error::Uapi(UapiCall::GetLineEvent, e))
            }
            UapiRequest::Line(lr) => {
                v2::get_line(&chip.f, lr).map_err(|e| Error::Uapi(UapiCall::GetLine, e))
            }
        }
    }
    #[cfg(not(feature = "uapi_v2"))]
    fn do_request(&self, chip: &Chip) -> Result<File> {
        match self.to_uapi()? {
            UapiRequest::Handle(hr) => v1::get_line_handle(&chip.f, hr)
                .map_err(|e| Error::Uapi(UapiCall::GetLineHandle, e)),
            UapiRequest::Event(er) => {
                v1::get_line_event(&chip.f, er).map_err(|e| Error::Uapi(UapiCall::GetLineEvent, e))
            }
        }
    }
    #[cfg(not(feature = "uapi_v1"))]
    fn do_request(&self, chip: &Chip) -> Result<File> {
        match self.to_uapi()? {
            UapiRequest::Line(lr) => {
                v2::get_line(&chip.f, lr).map_err(|e| Error::Uapi(UapiCall::GetLine, e))
            }
        }
    }

    fn to_request(&self, f: File) -> Request {
        Request {
            f,
            offsets: self.cfg.offsets.clone(),
            cfg: Arc::new(RwLock::new(self.cfg.clone())),
            user_event_buffer_size: max(self.user_event_buffer_size, 1),
            #[cfg(all(feature = "uapi_v1", feature = "uapi_v2"))]
            abiv: self.abiv.unwrap(),
        }
    }

    /// Replace the request configuration with the new one provided.
    pub fn with_config(&mut self, cfg: Config) -> &mut Self {
        self.cfg = cfg;
        self
    }

    /// Get a snapshot of the current request configuration.
    ///
    /// Can be applied to other requests with [`with_config`] or [`Request::from_config`].
    ///
    /// [`with_config`]: #method.with_config
    /// [`Request.from_config`]: struct.Request.html#method.from_config
    pub fn config(&self) -> Config {
        self.cfg.clone()
    }

    // mutators for attributes that cannot be changed after the request is made.

    /// Specify the consumer label to be applied to the request, and so to all lines
    /// in the request.
    ///
    /// If not specified, a label *"gpiocdev-p**PID**"* is applied by [`request`],
    /// where **PID** is the process id of the application.
    ///
    /// # Examples
    /// ```no_run
    /// # use gpiocdev::{Request, Result};
    /// # use gpiocdev::line::Values;
    /// # fn example() -> Result<()> {
    /// let req = Request::builder()
    ///     .on_chip("/dev/gpiochip0")
    ///     .with_lines(&[3,5])
    ///     .with_consumer("spice_weasel")
    ///     .request()?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`request`]: #method.request
    pub fn with_consumer<N: Into<String>>(&mut self, consumer: N) -> &mut Self {
        self.consumer = consumer.into();
        self
    }

    /// Set the event buffer size for edge events buffered in the kernel.
    ///
    /// This method is only required in unusual circumstances.
    ///
    /// The size provides a hint to the kernel as to the size of the buffer to store edge
    /// events until they are read from the Request.
    /// The default size is 16 times the number of lines in the request.
    /// The default size is typically sufficient unless the hardware may generate bursts
    /// of events faster than the application can service them.
    ///
    /// Altering the buffer size does NOT affect latency.
    /// Buffering is provided in the kernel to reduce the likelihood of event loss when
    /// user space is slow servicing events.
    /// In all cases the events are provided to user space as quickly as user space allows.
    pub fn with_kernel_event_buffer_size(&mut self, event_buffer_size: u32) -> &mut Self {
        self.kernel_event_buffer_size = event_buffer_size;
        self
    }

    /// Set the event buffer size for edge events buffered in user space.
    ///
    /// This method is only required in unusual circumstances.
    ///
    /// The size defines the number of events that may be buffered in user space by the
    /// [`Request.edge_events`] iterator.  The user space buffer is a performance optimisation to
    /// reduce the number of system calls required to read events.
    ///
    /// Altering the buffer size does NOT affect latency.
    /// In all cases the events are provided to user space as quickly as user space allows.
    ///
    /// [`Request.edge_events`]: struct.Request.html#method.edge_events
    pub fn with_user_event_buffer_size(&mut self, event_buffer_size: usize) -> &mut Self {
        self.user_event_buffer_size = event_buffer_size;
        self
    }

    /// Select the ABI version to use when requesting the lines and for subsequent operations.
    ///
    /// This is not normally required - the library will determine the available ABI versions
    /// and use the latest.
    ///
    /// This is only required where an application supports both uAPI versions but wishes
    /// to force the usage of a particular version at runtime, bypassing the automatic
    /// selection process.
    ///
    /// The version can be tested for availability using [`supports_abi_version`].
    ///
    /// [`supports_abi_version`]: fn@crate::supports_abi_version
    #[cfg(all(feature = "uapi_v1", feature = "uapi_v2"))]
    pub fn using_abi_version(&mut self, abiv: AbiVersion) -> &mut Self {
        self.abiv = Some(abiv);
        self
    }

    /// Set the chip from which to request lines.
    ///
    /// This applies to all lines in the request. It is not possible to request lines
    /// from different chips in the same request.
    ///
    /// The `Builder` locks to the first chip provided to it, for example by [`on_chip`]
    /// or [`with_found_line`].
    /// Any subsequent attempts to change the chip will result in an error when [`request`] is called.
    ///
    /// The chip is identified by a path which must resolve to a GPIO character device.
    ///
    /// [`on_chip`]: #method.on_chip
    /// [`with_found_line`]: #method.with_found_line
    /// [`request`]: #method.request
    pub fn on_chip<P: Into<PathBuf>>(&mut self, path: P) -> &mut Self {
        if self.cfg.chip.as_os_str().is_empty() {
            self.cfg.on_chip(path);
        } else if self.cfg.chip != path.into() {
            self.err = Some(Error::InvalidArgument("Multiple chips requested.".into()))
        }
        self
    }

    // Pass through mutators to the contained Config

    /// Set the selected lines to input.
    ///
    /// This is a short form of [`with_direction(Input)`].
    ///
    /// This is the default direction setting.
    ///
    /// [`with_direction(Input)`]: #method.with_direction
    pub fn as_input(&mut self) -> &mut Self {
        self.cfg.as_input();
        self
    }

    /// Set the selected lines to output with the given value.
    ///
    /// This is a short form of [`with_direction(Output)`] and [`with_value(value)`]
    /// that allows the direction and value to be set in the one call.
    ///
    /// [`with_direction(Output)`]: #method.with_direction
    /// [`with_value(value)`]: #method.with_value
    pub fn as_output(&mut self, value: Value) -> &mut Self {
        self.cfg.as_output(value);
        self
    }

    /// Do not set the direction of the selected lines.
    pub fn as_is(&mut self) -> &mut Self {
        self.cfg.as_is();
        self
    }

    /// Set the selected lines to active low.
    pub fn as_active_low(&mut self) -> &mut Self {
        self.cfg.as_active_low();
        self
    }

    /// Set the selected lines to active high.
    ///
    /// This is the default active level setting.
    pub fn as_active_high(&mut self) -> &mut Self {
        self.cfg.as_active_high();
        self
    }

    /// Set the bias setting for the selected lines.
    pub fn with_bias<B: Into<Option<Bias>>>(&mut self, bias: B) -> &mut Self {
        self.cfg.with_bias(bias);
        self
    }

    /// Set the debounce period for the selected lines.
    ///
    /// A value of zero means no debounce.
    ///
    /// Implicitly sets the selected lines as inputs, if they weren't already, and
    /// removes any output specific settings.
    pub fn with_debounce_period(&mut self, period: Duration) -> &mut Self {
        self.cfg.with_debounce_period(period);
        self
    }

    /// Set the direction of the selected lines.
    ///
    /// Setting to input removes any output specific settings.
    ///
    /// Setting to output removes any input specific settings.
    pub fn with_direction(&mut self, direction: Direction) -> &mut Self {
        self.cfg.with_direction(direction);
        self
    }

    /// Set the drive setting for the selected lines.
    ///
    /// Implicitly sets the selected lines as outputs, if they weren't already, and
    /// removes any input specific settings.
    pub fn with_drive(&mut self, drive: Drive) -> &mut Self {
        self.cfg.with_drive(drive);
        self
    }

    /// Set the edge detection for the selected lines.
    ///
    /// Implicitly sets the lines as inputs and removes any output specific settings.
    pub fn with_edge_detection<E: Into<Option<EdgeDetection>>>(&mut self, edge: E) -> &mut Self {
        self.cfg.with_edge_detection(edge);
        self
    }

    /// Set the clock source for edge events on the selected lines.
    pub fn with_event_clock<E: Into<Option<EventClock>>>(&mut self, event_clock: E) -> &mut Self {
        self.cfg.with_event_clock(event_clock);
        self
    }

    /// Add a found line to the request.
    ///
    /// The line must be on the same chip as any existing lines in the request, else the line is
    /// ignored and an error returned when [`request`](#method.request) is called.
    ///
    /// Note that all configuration mutators applied subsequently only apply to this line.
    ///
    /// # Examples
    /// ```no_run
    /// # use gpiocdev::line::Value;
    /// # fn example() -> Result<(), gpiocdev::Error> {
    /// let led0 = gpiocdev::find_named_line("LED0").unwrap();
    /// let req = gpiocdev::Request::builder()
    ///     .with_found_line(&led0)
    ///     .as_output(Value::Active)
    ///     .request()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_found_line(&mut self, line: &crate::FoundLine) -> &mut Self {
        if let Err(e) = self.cfg.with_found_line(line) {
            self.err = Some(e);
        }
        self
    }

    /// Add a set of found lines to the request.
    ///
    /// The lines must be on the same chip as any existing lines in the request, else the line is
    /// ignored and an error returned when [`request`](#method.request) is called.
    ///
    /// Note that all configuration mutators applied subsequently only apply to these lines.
    ///
    /// # Examples
    /// ```no_run
    /// # use gpiocdev::line::EdgeDetection;
    /// # fn example() -> Result<(), gpiocdev::Error> {
    /// let buttons = gpiocdev::find_named_lines(&["BUTTON0","BUTTON1"], true)?;
    /// let req = gpiocdev::Request::builder()
    ///     .with_found_lines(&buttons)
    ///     .with_edge_detection(EdgeDetection::BothEdges)
    ///     .request()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_found_lines(&mut self, lines: &HashMap<&str, crate::FoundLine>) -> &mut Self {
        if let Err(e) = self.cfg.with_found_lines(lines) {
            self.err = Some(e);
        }
        self
    }

    /// Add a line to the request.
    ///
    /// Note that all configuration mutators applied subsequently only
    /// apply to this line.
    pub fn with_line(&mut self, offset: Offset) -> &mut Self {
        self.cfg.with_line(offset);
        self
    }

    /// Remove a line from the request.
    pub fn without_line(&mut self, offset: Offset) -> &mut Self {
        self.cfg.without_line(offset);
        self
    }

    /// Add a set of lines to the request.
    ///
    /// Note that all configuration mutators applied subsequently only
    /// apply to this subset of lines.
    pub fn with_lines(&mut self, offsets: &[Offset]) -> &mut Self {
        self.cfg.with_lines(offsets);
        self
    }

    /// Add a set of output lines, with values, to the selected lines.
    ///
    /// This is a short form of [`with_line(offset)`](#method.with_line) and
    /// [`as_output(value)`](#method.as_output), applied to a set of line values.
    pub fn with_output_lines(&mut self, values: &Values) -> &mut Self {
        self.cfg.with_output_lines(values);
        self
    }

    /// Remove a set of lines from the request.
    pub fn without_lines(&mut self, offsets: &[Offset]) -> &mut Self {
        self.cfg.without_lines(offsets);
        self
    }

    /// Set the value of the selected lines.
    pub fn with_value(&mut self, value: Value) -> &mut Self {
        self.cfg.with_value(value);
        self
    }

    /// Apply the configuration based on a snapshot from a single line.
    pub fn from_line_config(&mut self, cfg: &line::Config) -> &mut Self {
        self.cfg.from_line_config(cfg);
        self
    }

    // Conversions into uAPI types.
    fn to_uapi(&self) -> Result<UapiRequest> {
        if self.cfg.num_lines() == 0 {
            return Err(Error::InvalidArgument("No lines specified.".into()));
        }
        if self.cfg.offsets.len() > NUM_LINES_MAX {
            return Err(Error::InvalidArgument(format!(
                "Requested {} lines is greater than the maximum of {}.",
                self.cfg.offsets.len(),
                NUM_LINES_MAX,
            )));
        }
        self.do_to_uapi()
    }
    #[cfg(all(feature = "uapi_v1", feature = "uapi_v2"))]
    fn do_to_uapi(&self) -> Result<UapiRequest> {
        match self.abiv.unwrap() {
            AbiVersion::V1 => self.to_v1(),
            AbiVersion::V2 => self.to_v2(),
        }
    }
    #[cfg(not(feature = "uapi_v2"))]
    fn do_to_uapi(&self) -> Result<UapiRequest> {
        self.to_v1()
    }
    #[cfg(not(feature = "uapi_v1"))]
    fn do_to_uapi(&self) -> Result<UapiRequest> {
        self.to_v2()
    }

    #[cfg(feature = "uapi_v1")]
    fn to_v1(&self) -> Result<UapiRequest> {
        if self.kernel_event_buffer_size != 0 {
            return Err(Error::AbiLimitation(
                AbiVersion::V1,
                "does not support setting event buffer size".into(),
            ));
        }
        let lcfg = self.cfg.unique()?;
        if lcfg.debounce_period.is_some() {
            return Err(Error::AbiLimitation(
                AbiVersion::V1,
                "does not support debounce".into(),
            ));
        }
        if lcfg.event_clock.is_some() {
            return Err(Error::AbiLimitation(
                AbiVersion::V1,
                "does not support selecting the event clock source".into(),
            ));
        }
        let consumer = if self.consumer.is_empty() {
            default_consumer().as_str().into()
        } else {
            self.consumer.as_str().into()
        };
        if lcfg.edge_detection.is_some() {
            if self.cfg.offsets.len() != 1 {
                return Err(Error::AbiLimitation(
                    AbiVersion::V1,
                    "only supports edge detection on single line requests".into(),
                ));
            }
            Ok(UapiRequest::Event(v1::EventRequest {
                offset: self.cfg.offsets[0],
                handleflags: lcfg.into(),
                eventflags: lcfg.into(),
                consumer,
                ..Default::default()
            }))
        } else {
            Ok(UapiRequest::Handle(v1::HandleRequest {
                offsets: v1::Offsets::from_slice(&self.cfg.offsets),
                flags: lcfg.into(),
                values: self.cfg.to_v1_values()?,
                consumer,
                num_lines: self.cfg.offsets.len() as u32,
                ..Default::default()
            }))
        }
    }

    #[cfg(any(feature = "uapi_v2", not(feature = "uapi_v1")))]
    fn to_v2(&self) -> Result<UapiRequest> {
        let consumer = if self.consumer.is_empty() {
            default_consumer().as_str().into()
        } else {
            self.consumer.as_str().into()
        };
        Ok(UapiRequest::Line(v2::LineRequest {
            offsets: v2::Offsets::from_slice(&self.cfg.offsets),
            consumer,
            event_buffer_size: self.kernel_event_buffer_size,
            num_lines: self.cfg.offsets.len() as u32,
            config: self.cfg.to_v2()?,
            ..Default::default()
        }))
    }
}

#[cfg_attr(test, derive(Debug))]
#[allow(clippy::large_enum_variant)]
enum UapiRequest {
    #[cfg(feature = "uapi_v1")]
    Handle(v1::HandleRequest),

    #[cfg(feature = "uapi_v1")]
    Event(v1::EventRequest),

    #[cfg(any(feature = "uapi_v2", not(feature = "uapi_v1")))]
    Line(v2::LineRequest),
}

fn default_consumer() -> String {
    format!("gpiocdev-p{}", std::process::id())
}

#[cfg(test)]
mod tests {
    use super::*;
    use Bias::*;
    use Direction::*;
    use Drive::*;
    use EdgeDetection::*;
    use EventClock::*;
    use Value::*;

    #[test]
    fn default() {
        let b = Builder::default();
        assert_eq!(b.cfg.chip.as_os_str(), "");
        assert_eq!(b.cfg.num_lines(), 0);
        assert_eq!(b.consumer.as_str(), "");
        assert_eq!(b.kernel_event_buffer_size, 0);
        assert_eq!(b.user_event_buffer_size, 0);
        #[cfg(all(feature = "uapi_v1", feature = "uapi_v2"))]
        assert_eq!(b.abiv, None);
    }

    #[test]
    fn from_config() {
        let mut cfg = Config::default();
        cfg.as_input().with_lines(&[1, 7, 4]);
        let b = Builder::from_config(cfg);
        assert_eq!(b.cfg.offsets, &[1, 7, 4]);
        assert_eq!(b.cfg.num_lines(), 3);
    }

    #[test]
    fn request_no_chip() {
        let res = Builder::default().with_line(2).request();
        assert!(res.is_err());
        assert_eq!(res.err().unwrap().to_string(), "No chip specified.");
    }

    #[test]
    fn request_multiple_chips() {
        let res = Builder::default()
            .on_chip("/dev/gpiochip0")
            .with_line(2)
            .on_chip("/dev/gpiochip1")
            .with_line(3)
            .request();
        assert!(res.is_err());
        assert_eq!(res.err().unwrap().to_string(), "Multiple chips requested.");
    }

    #[test]
    fn with_config() {
        let mut b = Builder::default();
        let mut cfg = Config::default();
        cfg.as_input().with_lines(&[1, 7, 4]);
        b.with_config(cfg);
        assert_eq!(b.cfg.offsets, &[1, 7, 4]);
        assert_eq!(b.cfg.num_lines(), 3);
    }

    #[test]
    fn config() {
        let mut b = Builder::default();
        b.as_input().with_lines(&[1, 7, 4]);
        let cfg = b.config();
        assert_eq!(cfg.num_lines(), 3);
        assert_eq!(cfg.offsets, &[1, 7, 4]);
    }

    #[test]
    fn with_consumer() {
        let mut b = Builder::default();
        assert_eq!(b.consumer.as_str(), "");

        b.with_consumer("builder test");
        assert_eq!(b.consumer.as_str(), "builder test");
    }

    #[test]
    fn with_kernel_event_buffer_size() {
        let mut b = Builder::default();
        assert_eq!(b.kernel_event_buffer_size, 0);

        b.with_kernel_event_buffer_size(42);
        assert_eq!(b.kernel_event_buffer_size, 42);
    }
    #[test]
    fn with_user_event_buffer_size() {
        let mut b = Builder::default();
        assert_eq!(b.user_event_buffer_size, 0);

        b.with_user_event_buffer_size(67);
        assert_eq!(b.user_event_buffer_size, 67);
    }

    #[test]
    #[cfg(all(feature = "uapi_v1", feature = "uapi_v2"))]
    fn using_abi_version() {
        let mut b = Builder::default();
        assert_eq!(b.abiv, None);

        b.using_abi_version(AbiVersion::V1);
        assert_eq!(b.abiv, Some(AbiVersion::V1));

        b.using_abi_version(AbiVersion::V2);
        assert_eq!(b.abiv, Some(AbiVersion::V2));
    }

    #[test]
    fn on_chip() {
        let mut b = Builder::default();
        assert_eq!(b.cfg.chip.as_os_str(), "");

        b.on_chip("test chip");
        assert_eq!(b.cfg.chip.as_os_str(), "test chip");

        b.on_chip("test chip2");
        assert_eq!(b.cfg.chip.as_os_str(), "test chip");
    }

    #[test]
    fn as_input() {
        let mut b = Builder::default();
        b.as_output(Active);
        assert_eq!(b.cfg.base.direction, Some(Output));

        b.as_input();
        assert_eq!(b.cfg.base.direction, Some(Input));
    }

    #[test]
    fn as_output() {
        let mut b = Builder::default();
        b.as_input();
        assert_eq!(b.cfg.base.direction, Some(Input));

        b.as_output(Active);
        assert_eq!(b.cfg.base.direction, Some(Output));
    }

    #[test]
    fn as_active_low() {
        let mut b = Builder::default();
        assert!(!b.cfg.base.active_low);

        b.as_active_low();
        assert!(b.cfg.base.active_low);
    }

    #[test]
    fn as_active_high() {
        let mut b = Builder::default();
        assert!(!b.cfg.base.active_low);

        b.as_active_low();
        assert!(b.cfg.base.active_low);

        b.as_active_high();
        assert!(!b.cfg.base.active_low);
    }

    #[test]
    fn with_bias() {
        let mut b = Builder::default();
        b.with_bias(PullUp);
        assert_eq!(b.cfg.base.bias, Some(PullUp));

        b.with_bias(PullDown);
        assert_eq!(b.cfg.base.bias, Some(PullDown));

        b.with_bias(Disabled);
        assert_eq!(b.cfg.base.bias, Some(Disabled));

        b.with_bias(None);
        assert_eq!(b.cfg.base.bias, None);
    }

    #[test]
    fn with_debounce_period() {
        let d_us = Duration::from_micros(1234);
        let d_ns = Duration::from_nanos(234);
        let mut b = Builder::default();

        b.with_drive(OpenSource);
        assert_eq!(b.cfg.base.direction, Some(Output));

        b.with_debounce_period(Duration::from_micros(1234));
        assert_eq!(b.cfg.base.debounce_period, Some(d_us));
        assert_eq!(b.cfg.base.direction, Some(Input));

        b.with_debounce_period(Duration::from_nanos(234));
        assert_eq!(b.cfg.base.debounce_period, Some(d_ns));

        b.with_debounce_period(Duration::ZERO);
        assert!(b.cfg.base.debounce_period.is_none());
    }

    #[test]
    fn with_direction() {
        let mut b = Builder::default();
        b.with_direction(Output);
        assert_eq!(b.cfg.base.direction, Some(Output));

        b.with_direction(Input);
        assert_eq!(b.cfg.base.direction, Some(Input));
    }

    #[test]
    fn with_drive() {
        let mut b = Builder::default();
        b.with_bias(PullUp)
            .with_debounce_period(Duration::from_millis(10))
            .with_edge_detection(RisingEdge);
        assert_eq!(b.cfg.base.direction, Some(Input));
        assert_eq!(b.cfg.base.bias, Some(PullUp));
        assert_eq!(b.cfg.base.debounce_period, Some(Duration::from_millis(10)));
        assert_eq!(b.cfg.base.edge_detection, Some(RisingEdge));

        b.with_drive(PushPull);
        assert_eq!(b.cfg.base.direction, Some(Output));
        assert_eq!(b.cfg.base.drive, Some(PushPull));
        assert_eq!(b.cfg.base.bias, Some(PullUp));
        assert!(b.cfg.base.debounce_period.is_none());
        assert_eq!(b.cfg.base.edge_detection, None);

        b.with_drive(OpenDrain);
        assert_eq!(b.cfg.base.drive, Some(OpenDrain));

        b.with_drive(OpenSource);
        assert_eq!(b.cfg.base.drive, Some(OpenSource));
    }

    #[test]
    fn with_edge_detection() {
        let mut b = Builder::default();
        b.with_drive(OpenSource);
        assert_eq!(b.cfg.base.direction, Some(Output));

        b.with_edge_detection(RisingEdge);
        assert_eq!(b.cfg.base.edge_detection, Some(RisingEdge));
        assert_eq!(b.cfg.base.drive, None);
        assert_eq!(b.cfg.base.direction, Some(Input));

        b.with_edge_detection(FallingEdge);
        assert_eq!(b.cfg.base.edge_detection, Some(FallingEdge));

        b.with_edge_detection(BothEdges);
        assert_eq!(b.cfg.base.edge_detection, Some(BothEdges));

        b.with_edge_detection(None);
        assert_eq!(b.cfg.base.edge_detection, None);
    }

    #[test]
    fn with_event_clock() {
        let mut b = Builder::default();
        assert_eq!(b.cfg.base.event_clock, None);

        b.with_event_clock(Realtime);
        assert_eq!(b.cfg.base.event_clock, Some(Realtime));

        b.with_event_clock(Monotonic);
        assert_eq!(b.cfg.base.event_clock, Some(Monotonic));

        b.with_event_clock(Hte);
        assert_eq!(b.cfg.base.event_clock, Some(Hte));

        b.with_event_clock(None);
        assert_eq!(b.cfg.base.event_clock, None);
    }

    #[test]
    fn with_line() {
        let mut b = Builder::default();
        assert_eq!(b.cfg.num_lines(), 0);

        b.with_line(3);
        assert_eq!(b.cfg.num_lines(), 1);

        b.with_line(5);
        assert_eq!(b.cfg.num_lines(), 2);

        b.with_line(3);
        assert_eq!(b.cfg.num_lines(), 2);
    }

    #[test]
    fn without_line() {
        let mut b = Builder::default();

        b.with_lines(&[3, 1]);
        assert_eq!(b.cfg.num_lines(), 2);

        b.without_line(3);
        assert_eq!(b.cfg.num_lines(), 1);
        assert_eq!(b.cfg.offsets, &[1]);
    }

    #[test]
    fn with_lines() {
        let mut b = Builder::default();
        assert_eq!(b.cfg.num_lines(), 0);

        b.with_lines(&[3, 1]);
        assert_eq!(b.cfg.num_lines(), 2);
        assert_eq!(b.cfg.offsets, &[3, 1]);

        b.with_lines(&[1, 5]);
        assert_eq!(b.cfg.num_lines(), 3);
        assert_eq!(b.cfg.offsets, &[3, 1, 5]);
    }

    #[test]
    fn without_lines() {
        let mut b = Builder::default();
        b.with_lines(&[3, 1, 0, 5, 7]);
        assert_eq!(b.cfg.num_lines(), 5);

        b.without_lines(&[3, 5]);
        assert_eq!(b.cfg.num_lines(), 3);
        assert_eq!(b.cfg.offsets, &[1, 0, 7]);
    }

    fn sorted(s: &[u32]) -> Vec<u32> {
        let mut x = s.to_vec();
        x.sort();
        x
    }

    #[test]
    fn with_output_lines() {
        let mut b = Builder::default();
        assert_eq!(b.cfg.num_lines(), 0);

        let vv: Values = [(3, Value::Active), (5, Value::Inactive)]
            .into_iter()
            .collect();

        b.with_output_lines(&vv);
        assert_eq!(b.cfg.num_lines(), 2);
        assert_eq!(sorted(&b.cfg.offsets), &[3, 5]);
        assert!(b.cfg.lcfg.contains_key(&3));
        assert_eq!(b.cfg.lcfg.get(&3).unwrap().direction, Some(Output));
        assert_eq!(b.cfg.lcfg.get(&3).unwrap().value, Some(Value::Active));
        assert!(b.cfg.lcfg.contains_key(&5));
        assert_eq!(b.cfg.lcfg.get(&5).unwrap().direction, Some(Output));
        assert_eq!(b.cfg.lcfg.get(&5).unwrap().value, Some(Value::Inactive));

        let vv: Values = [(3, Value::Inactive), (1, Value::Active)]
            .into_iter()
            .collect();

        b.with_output_lines(&vv);
        assert_eq!(b.cfg.num_lines(), 3);
        assert_eq!(sorted(&b.cfg.offsets), &[1, 3, 5]);
        assert!(b.cfg.lcfg.contains_key(&3));
        assert_eq!(b.cfg.lcfg.get(&3).unwrap().direction, Some(Output));
        assert_eq!(b.cfg.lcfg.get(&3).unwrap().value, Some(Value::Inactive));
        assert!(b.cfg.lcfg.contains_key(&1));
        assert_eq!(b.cfg.lcfg.get(&1).unwrap().direction, Some(Output));
        assert_eq!(b.cfg.lcfg.get(&1).unwrap().value, Some(Value::Active));
    }

    #[test]
    fn with_value() {
        let mut b = Builder::default();
        assert_eq!(b.cfg.base.value, None);

        b.as_input().with_value(Active);
        assert_eq!(b.cfg.base.value, Some(Active));

        b.as_input().with_value(Inactive);
        assert_eq!(b.cfg.base.value, Some(Inactive));
    }

    #[test]
    fn from_line_config() {
        let mut b = Builder::default();
        let d_us = Duration::from_micros(1234);
        let lc = line::Config {
            direction: Some(Output),
            active_low: true,
            bias: Some(Disabled),
            drive: Some(OpenSource),
            edge_detection: Some(BothEdges),
            event_clock: Some(Realtime),
            debounce_period: Some(d_us),
            value: Some(Active),
        };
        b.from_line_config(&lc);
        assert!(b.cfg.lcfg.is_empty());
        assert!(b.cfg.selected.is_empty());
        let base = b.cfg.base;
        assert_eq!(base.direction, Some(Output));
        assert!(base.active_low);
        assert_eq!(base.direction, Some(Output));
        assert_eq!(base.drive, Some(OpenSource));
        assert_eq!(base.edge_detection, Some(BothEdges));
        assert_eq!(base.event_clock, Some(Realtime));
        assert_eq!(base.value, Some(Active));
        assert_eq!(base.debounce_period, Some(d_us));
    }

    #[test]
    fn to_uapi() {
        let res = Builder::default().to_uapi();
        assert!(res.is_err());
        assert_eq!(res.err().unwrap().to_string(), "No lines specified.");
    }

    #[test]
    #[cfg(feature = "uapi_v1")]
    fn to_v1() {
        let mut b = Builder::default();
        b.with_consumer("test builder")
            .with_lines(&[1, 8, 4])
            .as_active_low()
            .as_input();
        if let UapiRequest::Handle(hr) = b.to_v1().unwrap() {
            assert_eq!(hr.num_lines, 3);
            assert_eq!(hr.offsets.get(0), 1);
            assert_eq!(hr.offsets.get(1), 8);
            assert_eq!(hr.offsets.get(2), 4);
            assert_eq!(String::from(&hr.consumer).as_str(), "test builder");
            assert!(hr
                .flags
                .contains(v1::HandleRequestFlags::INPUT | v1::HandleRequestFlags::ACTIVE_LOW));
            assert_eq!(hr.values.get(0), 0);
            assert_eq!(hr.values.get(1), 0);
            assert_eq!(hr.values.get(2), 0);
        } else {
            panic!("not a handle request");
        }

        let mut b = Builder::default();
        b.with_consumer("test builder")
            .with_line(8)
            .with_edge_detection(RisingEdge);
        if let UapiRequest::Event(er) = b.to_v1().unwrap() {
            assert_eq!(er.offset, 8);
            assert_eq!(String::from(&er.consumer).as_str(), "test builder");
            assert!(er.handleflags.contains(v1::HandleRequestFlags::INPUT));
            assert!(er.eventflags.contains(v1::EventRequestFlags::RISING_EDGE));
        } else {
            panic!("not an event request");
        }

        let mut b = Builder::default();
        b.with_lines(&[2, 6])
            .as_output(Value::Active)
            .with_lines(&[5, 7])
            .as_output(Value::Inactive);
        if let UapiRequest::Handle(hr) = b.to_v1().unwrap() {
            assert_eq!(hr.num_lines, 4);
            assert_eq!(hr.offsets.get(0), 2);
            assert_eq!(hr.offsets.get(1), 6);
            assert_eq!(hr.offsets.get(2), 5);
            assert_eq!(hr.offsets.get(3), 7);
            assert_eq!(
                String::from(&hr.consumer).as_str(),
                format!("gpiocdev-p{}", std::process::id()).as_str()
            );
            assert!(hr.flags.contains(v1::HandleRequestFlags::OUTPUT));
            assert_eq!(hr.values.get(0), 1);
            assert_eq!(hr.values.get(1), 1);
            assert_eq!(hr.values.get(2), 0);
            assert_eq!(hr.values.get(3), 0);
        } else {
            panic!("not a handle request");
        }

        // ABI limitations

        let mut b = Builder::default();
        assert_eq!(
            b.with_kernel_event_buffer_size(42)
                .with_lines(&[1, 8, 4])
                .as_input()
                .to_v1()
                .unwrap_err()
                .to_string(),
            "uAPI ABI v1 does not support setting event buffer size."
        );

        let mut b = Builder::default();
        assert_eq!(
            b.with_lines(&[1, 8, 4])
                .as_input()
                .with_line(3)
                .as_output(Value::Active)
                .to_v1()
                .unwrap_err()
                .to_string(),
            "uAPI ABI v1 requires all lines to share the same configuration."
        );

        let mut b = Builder::default();
        assert_eq!(
            b.with_lines(&[1, 8, 4])
                .with_edge_detection(RisingEdge)
                .to_v1()
                .unwrap_err()
                .to_string(),
            "uAPI ABI v1 only supports edge detection on single line requests."
        );

        let mut b = Builder::default();
        assert_eq!(
            b.with_lines(&[1, 8, 4])
                .with_debounce_period(Duration::from_millis(23))
                .to_v1()
                .unwrap_err()
                .to_string(),
            "uAPI ABI v1 does not support debounce."
        );

        let mut b = Builder::default();
        assert_eq!(
            b.with_line(4)
                .with_edge_detection(RisingEdge)
                .with_event_clock(EventClock::Realtime)
                .to_v1()
                .unwrap_err()
                .to_string(),
            "uAPI ABI v1 does not support selecting the event clock source."
        );
    }

    #[test]
    #[allow(irrefutable_let_patterns)]
    #[cfg(any(feature = "uapi_v2", not(feature = "uapi_v1")))]
    fn to_v2() {
        let mut b = Builder::default();
        b.with_consumer("test builder")
            .with_kernel_event_buffer_size(42)
            .with_lines(&[1, 8, 4])
            .with_edge_detection(RisingEdge)
            .with_event_clock(EventClock::Realtime);
        if let UapiRequest::Line(lr) = b.to_v2().unwrap() {
            assert_eq!(lr.num_lines, 3);
            assert_eq!(lr.offsets.get(0), 1);
            assert_eq!(lr.offsets.get(1), 8);
            assert_eq!(lr.offsets.get(2), 4);
            assert_eq!(lr.event_buffer_size, 42);
            assert_eq!(String::from(&lr.consumer).as_str(), "test builder");
            assert!(lr.config.flags.contains(
                v2::LineFlags::INPUT
                    | v2::LineFlags::EDGE_RISING
                    | v2::LineFlags::EVENT_CLOCK_REALTIME
            ));
            assert_eq!(lr.config.num_attrs, 0);
        } else {
            panic!("not a line request");
        }

        let mut b = Builder::default();
        b.with_lines(&[1, 8, 4])
            .as_input()
            .as_active_low()
            .with_lines(&[2, 6])
            .as_output(Value::Active)
            .with_lines(&[5, 7])
            .as_output(Value::Inactive);
        if let UapiRequest::Line(lr) = b.to_v2().unwrap() {
            assert_eq!(lr.num_lines, 7);
            assert_eq!(lr.offsets.get(0), 1);
            assert_eq!(lr.offsets.get(1), 8);
            assert_eq!(lr.offsets.get(2), 4);
            assert_eq!(lr.offsets.get(3), 2);
            assert_eq!(lr.offsets.get(4), 6);
            assert_eq!(lr.offsets.get(5), 5);
            assert_eq!(lr.offsets.get(6), 7);
            assert_eq!(lr.event_buffer_size, 0);
            assert_eq!(
                String::from(&lr.consumer).as_str(),
                format!("gpiocdev-p{}", std::process::id()).as_str()
            );
            // default flags match most lines, so output
            assert!(lr.config.flags.contains(v2::LineFlags::OUTPUT));
            assert_eq!(lr.config.num_attrs, 2);
            // first is flags for line inputs
            let lca = lr.config.attrs.0[0];
            assert_eq!(lca.mask, 0b000111);
            assert_eq!(lca.attr.kind, v2::LineAttributeKind::Flags as u32);
            assert_eq!(
                lca.attr.to_value().unwrap(),
                v2::LineAttributeValue::Flags(v2::LineFlags::INPUT | v2::LineFlags::ACTIVE_LOW)
            );
            // second is values for outputs
            let lca = lr.config.attrs.0[1];
            assert_eq!(lca.mask, 0b1111000);
            assert_eq!(lca.attr.kind, v2::LineAttributeKind::Values as u32);
            // inputs should be inactive, outputs as per config
            assert_eq!(
                lca.attr.to_value().unwrap(),
                v2::LineAttributeValue::Values(0b0011000)
            );
        } else {
            panic!("not a line request");
        }
    }
}