Skip to content

Commit 54b2221

Browse files
authored
Merge pull request #44 from rust-embedded/relax-extract-pin-from-path
Relax extract pin from path
2 parents 6bb6aaa + e4c1d7f commit 54b2221

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: rust
22
sudo: false
33
rust:
4-
- 1.20.0
4+
- 1.21.0
55
- stable
66
- beta
77
- nightly

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## [master] - Unreleased
44

5+
## [0.5.3] - 2018-04-19
6+
7+
### Fixed
8+
9+
- Relaxed path restrictions on `Pin::from_path` to allow for directories
10+
outside of `/sys/class/gpio`, required for some SOCs that symlink outside of
11+
that directory.
12+
513
## [0.5.2] - 2018-03-02
614

715
### Changed
@@ -158,7 +166,8 @@
158166
- Initial version of the library with basic functionality
159167
- Support for `export`/`unexport`/`get_value`/`set_value`/`set_direction`
160168

161-
[master]: https://github.com/posborne/rust-sysfs-gpio/compare/0.5.2...master
169+
[master]: https://github.com/posborne/rust-sysfs-gpio/compare/0.5.3...master
170+
[0.5.3]: https://github.com/posborne/rust-sysfs-gpio/compare/0.5.2...0.5.3
162171
[0.5.2]: https://github.com/posborne/rust-sysfs-gpio/compare/0.5.1...0.5.2
163172
[0.5.1]: https://github.com/posborne/rust-sysfs-gpio/compare/0.5.0...0.5.1
164173
[0.5.0]: https://github.com/posborne/rust-sysfs-gpio/compare/0.4.4...0.5.0

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sysfs_gpio"
3-
version = "0.5.2"
3+
version = "0.5.3"
44
authors = ["Paul Osborne <osbpau@gmail.com>"]
55
license = "MIT/Apache-2.0"
66
repository = "https://github.com/rust-embedded/rust-sysfs-gpio"

src/lib.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ use tokio_core::reactor::{Handle, PollEvented};
7777
mod error;
7878
pub use error::Error;
7979

80-
const GPIO_PATH_PREFIX: &'static str = "/sys/class/gpio/gpio";
81-
8280
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8381
pub struct Pin {
8482
pin_num: u64,
@@ -177,21 +175,17 @@ impl Pin {
177175
a directory"
178176
.to_owned()));
179177
}
180-
let num = Pin::extract_pin_from_path(&pb.to_str().unwrap_or(""))?;
178+
let num = Pin::extract_pin_from_path(&pb)?;
181179
Ok(Pin::new(num))
182180
}
183181

184182
/// Extract pin number from paths like /sys/class/gpio/gpioXXX
185-
fn extract_pin_from_path(path: &str) -> Result<u64> {
186-
if path.starts_with(GPIO_PATH_PREFIX) {
187-
path.split_at(GPIO_PATH_PREFIX.len()).1.parse::<u64>().or(
188-
Err(
189-
Error::InvalidPath(format!("{:?}", path)),
190-
),
191-
)
192-
} else {
193-
Err(Error::InvalidPath(format!("{:?}", path)))
194-
}
183+
fn extract_pin_from_path<P: AsRef<Path>>(path: P) -> Result<u64> {
184+
path.as_ref()
185+
.file_name()
186+
.and_then(|filename| filename.to_str())
187+
.and_then(|filename_str| filename_str.trim_left_matches("gpio").parse::<u64>().ok())
188+
.ok_or(Error::InvalidPath(format!("{:?}", path.as_ref())))
195189
}
196190

197191
/// Get the pin number
@@ -483,14 +477,16 @@ impl Pin {
483477

484478
#[test]
485479
fn extract_pin_fom_path_test() {
486-
let tok = Pin::extract_pin_from_path(&"/sys/class/gpio/gpio951");
487-
assert_eq!(951, tok.unwrap());
488-
let err1 = Pin::extract_pin_from_path(&"/sys/is/error/gpio/gpio111");
480+
let tok1 = Pin::extract_pin_from_path(&"/sys/class/gpio/gpio951");
481+
assert_eq!(951, tok1.unwrap());
482+
let tok2 = Pin::extract_pin_from_path(&"/sys/CLASS/gpio/gpio951/");
483+
assert_eq!(951, tok2.unwrap());
484+
let tok3 = Pin::extract_pin_from_path(&"../../devices/soc0/gpiochip3/gpio/gpio124");
485+
assert_eq!(124, tok3.unwrap());
486+
let err1 = Pin::extract_pin_from_path(&"/sys/CLASS/gpio/gpio");
489487
assert_eq!(true, err1.is_err());
490-
let err2 = Pin::extract_pin_from_path(&"/sys/CLASS/gpio/gpio");
488+
let err2 = Pin::extract_pin_from_path(&"/sys/class/gpio/gpioSDS");
491489
assert_eq!(true, err2.is_err());
492-
let err3 = Pin::extract_pin_from_path(&"/sys/class/gpio/gpioSDS");
493-
assert_eq!(true, err3.is_err());
494490
}
495491

496492
#[derive(Debug)]

0 commit comments

Comments
 (0)