aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>2023-03-10 11:16:30 +0100
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>2023-03-13 10:15:23 +0100
commitb44ce359d5686950da9b32be8551b60b6835fcf5 (patch)
tree3c1b89b1271c87bd942e626c39abe39eb5e9ea8e
parent40252603516b510b8c728a7d22b1ac99dcefbe1d (diff)
downloadlibgpiod-b44ce359d5686950da9b32be8551b60b6835fcf5.tar.gz
bindings: rust: examples: add a reimplementation of gpionotify
Add a new rust example - a simplified reimplementation of the gpionotify tool. Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org> Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
-rw-r--r--bindings/rust/libgpiod/examples/Makefile.am1
-rw-r--r--bindings/rust/libgpiod/examples/gpionotify.rs53
2 files changed, 54 insertions, 0 deletions
diff --git a/bindings/rust/libgpiod/examples/Makefile.am b/bindings/rust/libgpiod/examples/Makefile.am
index 6028fff9..2e1ccbd6 100644
--- a/bindings/rust/libgpiod/examples/Makefile.am
+++ b/bindings/rust/libgpiod/examples/Makefile.am
@@ -9,6 +9,7 @@ EXTRA_DIST = \
gpioget.rs \
gpioinfo.rs \
gpiomon.rs \
+ gpionotify.rs \
gpioset.rs \
gpio_threaded_info_events.rs \
gpiowatch.rs
diff --git a/bindings/rust/libgpiod/examples/gpionotify.rs b/bindings/rust/libgpiod/examples/gpionotify.rs
new file mode 100644
index 00000000..54445d29
--- /dev/null
+++ b/bindings/rust/libgpiod/examples/gpionotify.rs
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
+// SPDX-FileCopyrightText: 2023 Linaro Ltd.
+// SPDX-FileCopyrightTest: 2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
+//
+// Simplified Rust implementation of the gpionotify tool.
+
+use std::env;
+
+use libgpiod::{
+ chip::Chip,
+ line::{Offset, InfoChangeKind},
+ Error, Result,
+};
+
+fn usage(name: &str) {
+ println!("Usage: {} <chip> <offset0> ...", name);
+}
+
+fn main() -> Result<()> {
+ let args: Vec<String> = env::args().collect();
+ if args.len() < 3 {
+ usage(&args[0]);
+ return Err(Error::InvalidArguments);
+ }
+
+ let mut offsets = Vec::<Offset>::new();
+
+ for arg in &args[2..] {
+ let offset = arg.parse::<Offset>().map_err(|_| Error::InvalidArguments)?;
+ offsets.push(offset);
+ }
+
+ let path = format!("/dev/gpiochip{}", args[1]);
+ let chip = Chip::open(&path)?;
+
+ for &offset in offsets.iter() {
+ let _info = chip.watch_line_info(offset).unwrap();
+ }
+
+ loop {
+ let event = chip.read_info_event().unwrap();
+ println!(
+ "event: {}, line: {}, timestamp: {:?}",
+ match event.event_type()? {
+ InfoChangeKind::LineRequested => "Line requested",
+ InfoChangeKind::LineReleased => "Line released",
+ InfoChangeKind::LineConfigChanged => "Line config changed",
+ },
+ event.line_info().unwrap().offset(),
+ event.timestamp()
+ );
+ }
+}