@@ -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,17 @@ impl Request {
Ok(Self { request })
}
+ pub fn chip_path(&self) -> Result<&str> {
+ // SAFETY: The string returned by libgpiod is guaranteed to live as long
+ // as the `struct LineRequest`.
+ let path = unsafe { gpiod::gpiod_line_request_get_chip_path(self.request) };
+
+ // SAFETY: The string is guaranteed to be valid here by the C API.
+ unsafe { CStr::from_ptr(path) }
+ .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.
@@ -59,6 +59,19 @@ mod line_request {
mod verify {
use super::*;
+ #[test]
+ fn chip_path() {
+ const GPIO: Offset = 2;
+ let mut config = TestConfig::new(NGPIO).unwrap();
+ config.lconfig_add_settings(&[GPIO]);
+ config.request_lines().unwrap();
+
+ let dev_path = config.sim().lock().unwrap().dev_path();
+ let s = dev_path.into_os_string().into_string().unwrap();
+
+ assert_eq!(config.request().chip_path().unwrap(), s);
+ }
+
#[test]
fn custom_consumer() {
const GPIO: Offset = 2;