Skip to content

Commit 9348661

Browse files
committed
export: add export command and clean up exportall
Signed-off-by: Paul Osborne <[email protected]>
1 parent a4ea101 commit 9348661

File tree

5 files changed

+72
-12
lines changed

5 files changed

+72
-12
lines changed

src/commands/gpio_export.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (C) 2016, The Gpio Util Project Developers.
2+
3+
use options::GpioExportOptions;
4+
use config::GpioConfig;
5+
use std::process::exit;
6+
use export;
7+
8+
pub fn main(config: &GpioConfig, opts: &GpioExportOptions) {
9+
let pin = match config.get_pin(&opts.pin[..]) {
10+
Some(pin) => pin,
11+
None => {
12+
println!("Unable to find config entry for pin '{}'", opts.pin);
13+
exit(1)
14+
}
15+
};
16+
17+
let symlink_root = match opts.symlink_root {
18+
Some(ref slr) => &slr[..],
19+
None => config.get_symlink_root(),
20+
};
21+
22+
if let Err(e) = export::export(pin, Some(symlink_root)) {
23+
println!("Error occurred while exporting pin: {:?}", pin);
24+
println!("{}", e);
25+
exit(1);
26+
}
27+
}

src/commands/gpio_exportall.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ use std::process::exit;
66
use export;
77

88
pub fn main(config: &GpioConfig, opts: &GpioExportAllOptions) {
9+
let symlink_root = match opts.symlink_root {
10+
Some(ref slr) => &slr[..],
11+
None => config.get_symlink_root(),
12+
};
13+
914
for pin in config.get_pins() {
10-
let symlink_root = opts.symlink_root
11-
.clone()
12-
.unwrap_or(String::from(config.get_symlink_root()));
13-
match export::export(pin, Some(&symlink_root[..])) {
14-
Ok(_) => {}
15-
Err(e) => {
16-
println!("Error occurred while exporting pin: {:?}", pin);
17-
println!("{}", e);
18-
exit(1);
19-
}
15+
if let Err(e) = export::export(pin, Some(symlink_root)) {
16+
println!("Error occurred while exporting pin: {:?}", pin);
17+
println!("{}", e);
18+
exit(1);
2019
}
2120
}
2221
}

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (C) 2016, Paul Osborne <[email protected]>
22

33
pub mod gpio_read;
4+
pub mod gpio_export;
45
pub mod gpio_exportall;

src/config.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,16 @@ impl GpioConfig {
213213

214214
/// Get the pin with the provided name if present in this configuration
215215
pub fn get_pin(&self, name: &str) -> Option<&PinConfig> {
216-
self.pins.iter().find(|p| p.names.contains(name))
216+
// first, try to find pin by name
217+
if let Some(pin) = self.pins.iter().find(|p| p.names.contains(name)) {
218+
return Some(pin)
219+
}
220+
221+
// Try to parse the name as a 64-bit integer and match against that
222+
match name.parse::<u64>() {
223+
Ok(pin_num) => self.pins.iter().find(|p| p.num == pin_num),
224+
Err(_) => None,
225+
}
217226
}
218227

219228
/// Get a reference to all the pins in this config
@@ -364,6 +373,19 @@ names = ["wildcard"]
364373
assert_eq!(config.get_pin("missing"), None);
365374
}
366375

376+
#[test]
377+
fn test_get_pin_by_number() {
378+
let config = GpioConfig::from_str(BASIC_CFG).unwrap();
379+
let status_led = config.get_pin("37").unwrap();
380+
assert_eq!(status_led.num, 37);
381+
}
382+
383+
#[test]
384+
fn test_get_pin_by_number_not_found() {
385+
let config = GpioConfig::from_str(BASIC_CFG).unwrap();
386+
assert_eq!(config.get_pin("64"), None);
387+
}
388+
367389
#[test]
368390
fn test_parser_compact() {
369391
let config = GpioConfig::from_str(COMPACT_CFG).unwrap();

src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,18 @@ fn main() {
129129
}
130130
("poll", Some(_)) => {}
131131
("write", Some(_)) => {}
132-
("export", Some(_)) => {}
132+
("export", Some(m)) => {
133+
let export_options = GpioExportOptions {
134+
gpio_opts: gpio_options,
135+
pin: String::from(m.value_of("pin").unwrap()),
136+
symlink_root: match m.value_of("symlink-root") {
137+
Some(slr) => Some(String::from(slr)),
138+
None => None,
139+
},
140+
};
141+
142+
gpio_export::main(&cfg, &export_options);
143+
}
133144
("export-all", Some(m)) => {
134145
let exportall_options = GpioExportAllOptions {
135146
gpio_opts: gpio_options,

0 commit comments

Comments
 (0)