diff mbox series

[libgpiod] bindings: rust: examples: add a reimplementation of gpionotify

Message ID 20230310104555.62813-1-brgl@bgdev.pl
State New
Headers show
Series [libgpiod] bindings: rust: examples: add a reimplementation of gpionotify | expand

Commit Message

Bartosz Golaszewski March 10, 2023, 10:45 a.m. UTC
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Add a new rust example - a simplified reimplementation of the gpionotify
tool.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
Viresh, please take a look at my attempt at a rust sample. :)

 bindings/rust/libgpiod/examples/Makefile.am   |  1 +
 bindings/rust/libgpiod/examples/gpionotify.rs | 53 +++++++++++++++++++
 2 files changed, 54 insertions(+)
 create mode 100644 bindings/rust/libgpiod/examples/gpionotify.rs

Comments

Viresh Kumar March 13, 2023, 4:25 a.m. UTC | #1
On 10-03-23, 11:45, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> Add a new rust example - a simplified reimplementation of the gpionotify
> tool.
> 
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> ---
> Viresh, please take a look at my attempt at a rust sample. :)
> 
>  bindings/rust/libgpiod/examples/Makefile.am   |  1 +
>  bindings/rust/libgpiod/examples/gpionotify.rs | 53 +++++++++++++++++++
>  2 files changed, 54 insertions(+)
>  create mode 100644 bindings/rust/libgpiod/examples/gpionotify.rs

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
diff mbox series

Patch

diff --git a/bindings/rust/libgpiod/examples/Makefile.am b/bindings/rust/libgpiod/examples/Makefile.am
index 6028fff..2e1ccbd 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 0000000..54445d2
--- /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()
+        );
+    }
+}