diff options
author | Bartosz Golaszewski <bartosz.golaszewski@linaro.org> | 2023-07-20 16:47:47 +0200 |
---|---|---|
committer | Bartosz Golaszewski <bartosz.golaszewski@linaro.org> | 2023-07-22 14:29:04 +0200 |
commit | d12ce74a91037471126dc50aa0b365f5753b0b17 (patch) | |
tree | b41e357c75e94b8cc36848de4df725546b4101a1 | |
parent | eb9816ebe0e0961379a6d271ed7f5203048c7bdf (diff) | |
download | libgpiod-d12ce74a91037471126dc50aa0b365f5753b0b17.tar.gz |
bindings: rust: provide LineRequest::chip_name()
Provide a wrapper around gpiod_line_request_get_chip_name() for Rust
bindings and add a test-case.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Erik Schilling <erik.schilling@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
-rw-r--r-- | bindings/rust/libgpiod/src/line_request.rs | 14 | ||||
-rw-r--r-- | bindings/rust/libgpiod/tests/line_request.rs | 14 |
2 files changed, 28 insertions, 0 deletions
diff --git a/bindings/rust/libgpiod/src/line_request.rs b/bindings/rust/libgpiod/src/line_request.rs index 1140aa9f..a5697d6c 100644 --- a/bindings/rust/libgpiod/src/line_request.rs +++ b/bindings/rust/libgpiod/src/line_request.rs @@ -2,6 +2,7 @@ // SPDX-FileCopyrightText: 2022 Linaro Ltd. // SPDX-FileCopyrightText: 2022 Viresh Kumar <viresh.kumar@linaro.org> +use std::ffi::CStr; use std::os::unix::prelude::AsRawFd; use std::time::Duration; @@ -25,6 +26,19 @@ impl Request { Ok(Self { request }) } + /// Get the name of the chip this request was made on. + pub fn chip_name(&self) -> Result<&str> { + // SAFETY: The `gpiod_line_request` is guaranteed to be live as long + // as `&self` + let name = unsafe { gpiod::gpiod_line_request_get_chip_name(self.request) }; + + // SAFETY: The string is guaranteed to be valid, non-null and immutable + // by the C API for the lifetime of the `gpiod_line_request`. + unsafe { CStr::from_ptr(name) } + .to_str() + .map_err(Error::StringNotUtf8) + } + /// Get the number of lines in the request. pub fn num_lines(&self) -> usize { // SAFETY: `gpiod_line_request` is guaranteed to be valid here. diff --git a/bindings/rust/libgpiod/tests/line_request.rs b/bindings/rust/libgpiod/tests/line_request.rs index d49874f7..9af52268 100644 --- a/bindings/rust/libgpiod/tests/line_request.rs +++ b/bindings/rust/libgpiod/tests/line_request.rs @@ -60,6 +60,20 @@ mod line_request { use super::*; #[test] + fn chip_name() { + const GPIO: Offset = 2; + let mut config = TestConfig::new(NGPIO).unwrap(); + config.lconfig_add_settings(&[GPIO]); + config.request_lines().unwrap(); + + let arc = config.sim(); + let sim = arc.lock().unwrap(); + let chip_name = sim.chip_name().clone(); + + assert_eq!(config.request().chip_name().unwrap(), chip_name); + } + + #[test] fn custom_consumer() { const GPIO: Offset = 2; const CONSUMER: &str = "foobar"; |