linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: Viresh Kumar <viresh.kumar@linaro.org>
Cc: "Linus Walleij" <linus.walleij@linaro.org>,
	"Bartosz Golaszewski" <brgl@bgdev.pl>,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	linux-gpio@vger.kernel.org,
	"Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@google.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	stratos-dev@op-lists.linaro.org,
	"Gerard Ryan" <g.m0n3y.2503@gmail.com>
Subject: Re: [PATCH V4 5/8] libgpiod: Add rust examples
Date: Wed, 27 Jul 2022 10:58:15 +0800	[thread overview]
Message-ID: <20220727025815.GE88787@sol> (raw)
In-Reply-To: <acd12e70cfb30f04761f3c2efc868ec138c90c63.1657279685.git.viresh.kumar@linaro.org>

On Fri, Jul 08, 2022 at 05:04:58PM +0530, Viresh Kumar wrote:
> Add examples for the usage of the rust bindings, quite similar to the
> ones in cxx bindings.
> 
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  bindings/rust/examples/gpiodetect.rs | 37 ++++++++++++
>  bindings/rust/examples/gpiofind.rs   | 42 +++++++++++++
>  bindings/rust/examples/gpioget.rs    | 42 +++++++++++++
>  bindings/rust/examples/gpioinfo.rs   | 89 ++++++++++++++++++++++++++++
>  bindings/rust/examples/gpiomon.rs    | 68 +++++++++++++++++++++
>  bindings/rust/examples/gpioset.rs    | 52 ++++++++++++++++
>  6 files changed, 330 insertions(+)
>  create mode 100644 bindings/rust/examples/gpiodetect.rs
>  create mode 100644 bindings/rust/examples/gpiofind.rs
>  create mode 100644 bindings/rust/examples/gpioget.rs
>  create mode 100644 bindings/rust/examples/gpioinfo.rs
>  create mode 100644 bindings/rust/examples/gpiomon.rs
>  create mode 100644 bindings/rust/examples/gpioset.rs
> 
> diff --git a/bindings/rust/examples/gpiodetect.rs b/bindings/rust/examples/gpiodetect.rs
> new file mode 100644
> index 000000000000..82307e4eecea
> --- /dev/null
> +++ b/bindings/rust/examples/gpiodetect.rs
> @@ -0,0 +1,37 @@
> +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
> +//
> +// Copyright 2022 Linaro Ltd. All Rights Reserved.
> +//     Viresh Kumar <viresh.kumar@linaro.org>
> +//
> +// Simplified Rust implementation of gpiodetect tool.
> +
> +use std::env;
> +use std::fs;
> +use std::path::Path;
> +
> +use libgpiod::{gpiod_is_gpiochip_device, Chip};
> +
> +fn main() {
> +    let args: Vec<String> = env::args().collect();
> +    if args.len() > 1 {
> +        println!("Usage: {}", args[0]);
> +        return;
> +    }
> +
> +    for entry in fs::read_dir(Path::new("/dev")).unwrap() {

use .flatten() to have the iterator unwrap the entry so it is actually
an entry, not a Result.

> +        let pathbuf = entry.unwrap().path();
> +        let path = pathbuf.to_str().unwrap();
> +

is_gpiochip_device() and Chip::open() (and ChipInternal) should accept
anything that can be converted into a &Path, e.g. a PathBuf, so the path
variable becomes redundant.

e.g. 
    pub(crate) fn open<P: AsRef<std::path::Path>>(path: &P) -> Result<Self> {
        // Null-terminate the string
        let path = path.as_ref().to_string_lossy() + "\0";
        ...

and then example code becomes:

    for entry in fs::read_dir(Path::new("/dev")).unwrap().flatten() {
        let path = entry.path();

        if gpiod_is_gpiochip_device(&path) {
            let chip = Chip::open(&path).unwrap();
            ...

(renaming pathbuf to path)

Similarly other examples.

> +        if gpiod_is_gpiochip_device(path) {
> +            let chip = Chip::open(path).unwrap();
> +            let ngpio = chip.get_num_lines();
> +

Why does ngpio get a variable, unlike name and label?

> +            println!(
> +                "{} [{}] ({})",
> +                chip.get_name().unwrap(),
> +                chip.get_label().unwrap(),
> +                ngpio
> +            );
> +        }
> +    }
> +}

Avoid using unwrap().  Have main return a Result and use ?.
Not so important here, but below for helper functions returning
Results allows the caller to determine how to handle the error.
And it reads better.

> diff --git a/bindings/rust/examples/gpiofind.rs b/bindings/rust/examples/gpiofind.rs
> new file mode 100644
> index 000000000000..bbbd7a87ece8
> --- /dev/null
> +++ b/bindings/rust/examples/gpiofind.rs
> @@ -0,0 +1,42 @@
> +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
> +//
> +// Copyright 2022 Linaro Ltd. All Rights Reserved.
> +//     Viresh Kumar <viresh.kumar@linaro.org>
> +//
> +// Simplified Rust implementation of gpiofind tool.
> +
> +use std::env;
> +use std::fs;
> +use std::path::Path;
> +
> +use libgpiod::{gpiod_is_gpiochip_device, Chip};
> +
> +fn main() {
> +    let args: Vec<String> = env::args().collect();
> +    if args.len() != 2 {
> +        println!("Usage: {} <line-name>", args[0]);
> +        return;
> +    }
> +
> +    for entry in fs::read_dir(Path::new("/dev")).unwrap() {
> +        let pathbuf = entry.unwrap().path();
> +        let path = pathbuf.to_str().unwrap();
> +
> +        if gpiod_is_gpiochip_device(path) {

Perhaps have the bindings provide an iterator that returns the paths of
available gpiochips?

> +            let chip = Chip::open(path).unwrap();
> +
> +            let offset = chip.find_line(&args[1]);
> +            if offset.is_ok() {
> +                println!(
> +                    "Line {} found: Chip: {}, offset: {}",
> +                    args[1],
> +                    chip.get_name().unwrap(),
> +                    offset.unwrap()
> +                );
> +                return;
> +            }
> +        }
> +    }
> +
> +    println!("Failed to find line: {}", args[1]);
> +}
> diff --git a/bindings/rust/examples/gpioget.rs b/bindings/rust/examples/gpioget.rs
> new file mode 100644
> index 000000000000..c3bc35fcfdb6
> --- /dev/null
> +++ b/bindings/rust/examples/gpioget.rs
> @@ -0,0 +1,42 @@
> +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
> +//
> +// Copyright 2022 Linaro Ltd. All Rights Reserved.
> +//     Viresh Kumar <viresh.kumar@linaro.org>
> +//
> +// Simplified Rust implementation of gpioget tool.
> +
> +use std::env;
> +
> +use libgpiod::{Chip, Direction, LineConfig, RequestConfig};
> +
> +fn main() {
> +    let args: Vec<String> = env::args().collect();
> +    if args.len() < 3 {
> +        println!("Usage: {} <chip> <line_offset0> ...", args[0]);
> +        return;
> +    }
> +
> +    let mut config = LineConfig::new().unwrap();
> +    let mut offsets = Vec::<u32>::new();
> +
> +    for arg in &args[2..] {
> +        let offset = arg.parse::<u32>().unwrap();
> +
> +        offsets.push(offset);
> +        config.set_direction_override(Direction::Input, offset);
> +    }
> +
> +    let path = format!("/dev/gpiochip{}", args[1]);
> +    let chip = Chip::open(&path).unwrap();
> +
> +    let rconfig = RequestConfig::new().unwrap();
> +    rconfig.set_consumer(&args[0]);
> +    rconfig.set_offsets(&offsets);
> +
> +    let request = chip.request_lines(&rconfig, &config).unwrap();
> +
> +    let mut values: Vec<i32> = vec![0; offsets.len()];
> +    request.get_values(&mut values).unwrap();
> +
> +    println!("{:?}", values);
> +}
> diff --git a/bindings/rust/examples/gpioinfo.rs b/bindings/rust/examples/gpioinfo.rs
> new file mode 100644
> index 000000000000..bd30d9096ce8
> --- /dev/null
> +++ b/bindings/rust/examples/gpioinfo.rs
> @@ -0,0 +1,89 @@
> +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
> +//
> +// Copyright 2022 Linaro Ltd. All Rights Reserved.
> +//     Viresh Kumar <viresh.kumar@linaro.org>
> +//
> +// Simplified Rust implementation of gpioinfo tool.
> +
> +use std::env;
> +use std::fs;
> +use std::path::Path;
> +
> +use libgpiod::{gpiod_is_gpiochip_device, Chip, Direction};
> +
> +fn line_info(chip: &Chip, offset: u32) {
> +    let info = chip.line_info(offset).unwrap();
> +    let off = info.get_offset();
> +
> +    let name = match info.get_name() {
> +        Ok(name) => name,
> +        _ => "unused",
> +    };
> +
> +    let consumer = match info.get_consumer() {
> +        Ok(name) => name,
> +        _ => "unnamed",
> +    };
> +
> +    let low = if info.is_active_low() {
> +        "active-low"
> +    } else {
> +        "active-high"
> +    };
> +
> +    let dir = match info.get_direction().unwrap() {
> +        Direction::AsIs => "None",
> +        Direction::Input => "Input",
> +        Direction::Output => "Output",
> +    };
> +
> +    println!(
> +        "\tline {:>3}\
> +              \t{:>10}\
> +              \t{:>10}\
> +              \t{:>6}\
> +              \t{:>14}",
> +        off, name, consumer, dir, low
> +    );
> +}
> +
> +fn chip_info(path: &str) {
> +    if gpiod_is_gpiochip_device(path) {
> +        let chip = Chip::open(path).unwrap();
> +        let ngpio = chip.get_num_lines();
> +
> +        println!("GPIO Chip name: {}", chip.get_name().unwrap());
> +        println!("\tlabel: {}", chip.get_label().unwrap());
> +        println!("\tpath: {}", chip.get_path().unwrap());
> +        println!("\tngpio: {}\n", ngpio);
> +
> +        println!("\tLine information:");
> +
> +        for offset in 0..ngpio {
> +            line_info(&chip, offset);
> +        }
> +        println!("\n");
> +    }
> +}
> +
> +fn main() {
> +    let args: Vec<String> = env::args().collect();
> +    if args.len() > 2 {
> +        println!("Usage: {}", args[0]);
> +        return;
> +    }
> +
> +    if args.len() == 1 {
> +        for entry in fs::read_dir(Path::new("/dev")).unwrap() {
> +            let pathbuf = entry.unwrap().path();
> +            let path = pathbuf.to_str().unwrap();
> +
> +            chip_info(path);
> +        }
> +    } else {
> +        let index = args[1].parse::<u32>().unwrap();
> +        let path = format!("/dev/gpiochip{}", index);
> +
> +        chip_info(&path);
> +    }
> +}
> diff --git a/bindings/rust/examples/gpiomon.rs b/bindings/rust/examples/gpiomon.rs
> new file mode 100644
> index 000000000000..872907b386f3
> --- /dev/null
> +++ b/bindings/rust/examples/gpiomon.rs
> @@ -0,0 +1,68 @@
> +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
> +//
> +// Copyright 2022 Linaro Ltd. All Rights Reserved.
> +//     Viresh Kumar <viresh.kumar@linaro.org>
> +//
> +// Simplified Rust implementation of the gpiomon tool.
> +
> +use std::env;
> +use std::time::Duration;
> +
> +use libgpiod::{Chip, Edge, EdgeEventBuffer, Error, LineConfig, LineEdgeEvent, RequestConfig};
> +
> +fn usage(name: &str) {
> +    println!("Usage: {} <chip> <offset0> ...", name);
> +}
> +
> +fn main() {
> +    let args: Vec<String> = env::args().collect();
> +    if args.len() < 3 {
> +        usage(&args[0]);
> +        return;
> +    }
> +
> +    let mut config = LineConfig::new().unwrap();
> +    let mut offsets = Vec::<u32>::new();
> +
> +    for arg in &args[2..] {
> +        let offset = arg.parse::<u32>().unwrap();
> +
> +        offsets.push(offset);
> +    }
> +
> +    config.set_edge_detection_default(Edge::Both);
> +
> +    let path = format!("/dev/gpiochip{}", args[1]);
> +    let chip = Chip::open(&path).unwrap();
> +
> +    let rconfig = RequestConfig::new().unwrap();
> +    rconfig.set_offsets(&offsets);
> +
> +    let buffer = EdgeEventBuffer::new(1).unwrap();
> +    let request = chip.request_lines(&rconfig, &config).unwrap();
> +
> +    loop {
> +        match request.wait_edge_event(Duration::new(1, 0)) {
> +            Err(Error::OperationTimedOut) => continue,

timeout/continue required as you can't (currently) block indefinitely?

> +            Err(x) => {
> +                println!("{:?}", x);
> +                return;
> +            }
> +            Ok(()) => (),
> +        }
> +
> +        let count = request.read_edge_event(&buffer, 1).unwrap();
> +        if count == 1 {
> +            let event = buffer.get_event(0).unwrap();
> +            println!(
> +                "line: {} type: {}, time: {:?}",
> +                event.get_line_offset(),
> +                match event.get_event_type().unwrap() {
> +                    LineEdgeEvent::Rising => "Rising",
> +                    LineEdgeEvent::Falling => "Falling",
> +                },
> +                event.get_timestamp()
> +            );
> +        }
> +    }
> +}
> diff --git a/bindings/rust/examples/gpioset.rs b/bindings/rust/examples/gpioset.rs
> new file mode 100644
> index 000000000000..ef70e8edbaae
> --- /dev/null
> +++ b/bindings/rust/examples/gpioset.rs
> @@ -0,0 +1,52 @@
> +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
> +//
> +// Copyright 2022 Linaro Ltd. All Rights Reserved.
> +//     Viresh Kumar <viresh.kumar@linaro.org>
> +//
> +// Simplified Rust implementation of the gpioset tool.
> +
> +use std::env;
> +
> +use libgpiod::{Chip, Direction, LineConfig, RequestConfig};
> +
> +fn usage(name: &str) {
> +    println!("Usage: {} <chip> <line_offset0>=<value0> ...", name);
> +}
> +
> +fn main() {
> +    let args: Vec<String> = env::args().collect();
> +    if args.len() < 3 {
> +        usage(&args[0]);
> +        return;
> +    }
> +
> +    let mut config = LineConfig::new().unwrap();
> +    let mut offsets = Vec::<u32>::new();
> +    let mut values = Vec::<i32>::new();
> +
> +    for arg in &args[2..] {
> +        let pair: Vec<&str> = arg.split('=').collect();
> +        if pair.len() != 2 {
> +            usage(&args[0]);
> +            return;
> +        }
> +
> +        let offset = pair[0].parse::<u32>().unwrap();
> +        let value = pair[1].parse::<u32>().unwrap();
> +
> +        offsets.push(offset);
> +        values.push(value as i32);
> +    }
> +
> +    config.set_direction_default(Direction::Output);
> +    config.set_output_values(&offsets, &values).unwrap();
> +
> +    let path = format!("/dev/gpiochip{}", args[1]);
> +    let chip = Chip::open(&path).unwrap();
> +
> +    let rconfig = RequestConfig::new().unwrap();
> +    rconfig.set_consumer(&args[0]);
> +    rconfig.set_offsets(&offsets);
> +
> +    chip.request_lines(&rconfig, &config).unwrap();

Wait rather than exiting immediately?

> +}
> -- 
> 2.31.1.272.g89b43f80a514
> 

And, as mentioned elsewhere, add a gpiowatch example.

Cheers,
Kent.

  reply	other threads:[~2022-07-27  2:58 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-08 11:34 [PATCH V4 0/8] libgpiod: Add Rust bindings Viresh Kumar
2022-07-08 11:34 ` [PATCH V4 1/8] libgpiod: Add libgpiod-sys rust crate Viresh Kumar
2022-07-27  2:57   ` Kent Gibson
2022-07-27  4:51     ` Viresh Kumar
2022-07-27  5:17       ` Kent Gibson
2022-07-27  5:45         ` Viresh Kumar
2022-08-01 12:11         ` Viresh Kumar
2022-08-01 15:56           ` Kent Gibson
2022-08-02  8:50             ` Viresh Kumar
2022-08-02  9:36               ` Kent Gibson
2022-07-08 11:34 ` [PATCH V4 2/8] libgpiod: Add pre generated rust bindings Viresh Kumar
2022-07-27  2:57   ` Kent Gibson
2022-07-27  5:15     ` Viresh Kumar
2022-07-27  5:31       ` Kent Gibson
2022-07-27  6:00         ` Viresh Kumar
2022-07-27  6:06           ` Kent Gibson
2022-07-08 11:34 ` [PATCH V4 3/8] libgpiod-sys: Add support to generate gpiosim bindings Viresh Kumar
2022-07-27  2:57   ` Kent Gibson
2022-07-27  5:30     ` Viresh Kumar
2022-07-08 11:34 ` [PATCH V4 4/8] libgpiod: Add rust wrapper crate Viresh Kumar
2022-07-27  2:57   ` Kent Gibson
2022-07-27  9:07     ` Viresh Kumar
2022-07-27 10:08       ` Kent Gibson
2022-07-27 11:06         ` Miguel Ojeda
2022-07-27 12:40           ` Kent Gibson
2022-07-27 13:02             ` Miguel Ojeda
2022-07-28  3:11               ` Kent Gibson
2022-07-29  4:40                 ` Viresh Kumar
2022-07-28  3:10         ` Kent Gibson
2022-08-01 12:05         ` Viresh Kumar
2022-08-01 13:20           ` Kent Gibson
2022-08-01 13:28             ` Miguel Ojeda
2022-07-28  8:52     ` Viresh Kumar
2022-07-28  9:59       ` Kent Gibson
2022-07-08 11:34 ` [PATCH V4 5/8] libgpiod: Add rust examples Viresh Kumar
2022-07-27  2:58   ` Kent Gibson [this message]
2022-07-27  9:23     ` Viresh Kumar
2022-07-27  9:59       ` Kent Gibson
2022-07-27 10:06         ` Viresh Kumar
2022-07-27 10:32           ` Kent Gibson
2022-07-27 10:33             ` Viresh Kumar
2022-07-08 11:34 ` [PATCH V4 6/8] libgpiod: Derive debug traits for few definitions Viresh Kumar
2022-07-27  2:58   ` Kent Gibson
2022-07-27  6:20     ` Viresh Kumar
2022-07-08 11:35 ` [PATCH V4 7/8] libgpiod: Add rust tests Viresh Kumar
2022-07-27  2:58   ` Kent Gibson
2022-07-27  9:59     ` Viresh Kumar
2022-07-27 10:27       ` Kent Gibson
2022-08-01 11:54     ` Viresh Kumar
2022-08-01 12:38       ` Kent Gibson
2022-08-02  5:44         ` Viresh Kumar
2022-08-02  5:47           ` Kent Gibson
2022-07-08 11:35 ` [PATCH V4 8/8] libgpiod: Integrate building of rust bindings with make Viresh Kumar
2022-07-27  2:59   ` Kent Gibson
2022-07-27  6:18     ` Viresh Kumar
2022-07-27  6:25       ` Kent Gibson
2022-07-27  6:35         ` Viresh Kumar
2022-07-27  6:45           ` Kent Gibson
2022-07-27  6:51             ` Viresh Kumar
2022-07-15 19:07 ` [PATCH V4 0/8] libgpiod: Add Rust bindings Bartosz Golaszewski
2022-07-15 19:17   ` Miguel Ojeda
2022-07-15 19:27     ` Miguel Ojeda
2022-07-16  9:43       ` Miguel Ojeda
2022-07-16 10:43         ` Bartosz Golaszewski
2022-07-16 12:23           ` Kent Gibson
2022-07-16 13:46           ` Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220727025815.GE88787@sol \
    --to=warthog618@gmail.com \
    --cc=alex.bennee@linaro.org \
    --cc=brgl@bgdev.pl \
    --cc=g.m0n3y.2503@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=stratos-dev@op-lists.linaro.org \
    --cc=vincent.guittot@linaro.org \
    --cc=viresh.kumar@linaro.org \
    --cc=wedsonaf@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).