1 // Copyright (c) 2018 The rust-gpio-cdev Project Developers.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 //! Clone of functionality of linux/tools/gpio/lsgpio.c
10 
11 use gpio_cdev::*;
12 
main()13 fn main() {
14     let chip_iterator = match chips() {
15         Ok(chips) => chips,
16         Err(e) => {
17             println!("Failed to get chip iterator: {:?}", e);
18             return;
19         }
20     };
21 
22     for chip in chip_iterator {
23         if let Ok(chip) = chip {
24             println!(
25                 "GPIO chip: {}, \"{}\", \"{}\", {} GPIO Lines",
26                 chip.path().to_string_lossy(),
27                 chip.name(),
28                 chip.label(),
29                 chip.num_lines()
30             );
31             for line in chip.lines() {
32                 match line.info() {
33                     Ok(info) => {
34                         let mut flags = vec![];
35 
36                         if info.is_kernel() {
37                             flags.push("kernel");
38                         }
39 
40                         if info.direction() == LineDirection::Out {
41                             flags.push("output");
42                         }
43 
44                         if info.is_active_low() {
45                             flags.push("active-low");
46                         }
47                         if info.is_open_drain() {
48                             flags.push("open-drain");
49                         }
50                         if info.is_open_source() {
51                             flags.push("open-source");
52                         }
53 
54                         let usage = if !flags.is_empty() {
55                             format!("[{}]", flags.join(" "))
56                         } else {
57                             "".to_owned()
58                         };
59 
60                         println!(
61                             "\tline {lineno:>3}: {name} {consumer} {usage}",
62                             lineno = info.line().offset(),
63                             name = info.name().unwrap_or("unused"),
64                             consumer = info.consumer().unwrap_or("unused"),
65                             usage = usage,
66                         );
67                     }
68                     Err(e) => println!("\tError getting line info: {:?}", e),
69                 }
70             }
71             println!();
72         }
73     }
74 }
75