From patchwork Wed Sep 27 16:29:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erik Schilling X-Patchwork-Id: 726803 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D428DE82CAF for ; Wed, 27 Sep 2023 16:29:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231784AbjI0Q3s (ORCPT ); Wed, 27 Sep 2023 12:29:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231659AbjI0Q3q (ORCPT ); Wed, 27 Sep 2023 12:29:46 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86076DE for ; Wed, 27 Sep 2023 09:29:43 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-533d6a8d6b6so10407434a12.2 for ; Wed, 27 Sep 2023 09:29:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695832182; x=1696436982; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AAuKYtZA2AkFMpgcdmfty2Sc0tQUPcoGpybNVArcQqU=; b=xHtTW225Fb1FR1TPqCCux+TrM/xI0itIeav/TuGfVIeoMkB8jWzFH9DDPZMmeyiGAz 6Wr24DRDq6E1xx0FIrsMA0jm1J702aHdS1X7L6qOiHAKAGsWdYnwMAmBQBYuTXvpxOgh qOZKf5WdelhNOuYAZCX1f+h31a1Rj6C399HQuR57xz4/PBKPSuTOmpRolV3xF9fZbYFy BIi+sG90yuHjmJhPtAJh2ywtDsqgC35Uoso/pYBtRWwCKhpZOICdLyxP7EwoMyAwCB48 T3Kck1CZT80TtDf2GZuRjudpb7vp9Byv9D5MO6WF7rp7JK3c97njCxJgznOcq7leTYgy 8cIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695832182; x=1696436982; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AAuKYtZA2AkFMpgcdmfty2Sc0tQUPcoGpybNVArcQqU=; b=L46i8y3idsWkBYZdD+DWb0h+V/3HDLs37C1NsR/p3hEwqPqrg7ytfJNQOz1fOOx+5O EiinaaUSs5OtcM7XTSaHjU2CtLUcyUrQF1Dkx+Cn1rEs9u1YHr1jKmjoJkmfcFDLPz2u 8YLHTwlTftkrptZKoF+QcQl4RmypWaTL1uxg0h0kJnaELDy9ll1XPA453MS2sgMIJq4j YNPR/ColDCO3Y98VOSgJIRVV/MzWrq6KPZfjw9LM8TCK/1IzyAsHFEQh3Ky9hi3ttwGE 89UqquL2euIKx6zx+7XwsIqTD59JiG/k/mWreYeGlmICz1+jv/1XrwLjws2wYQdeCks+ qmvQ== X-Gm-Message-State: AOJu0YzsJvBIel1K2l8fWcRhD+kS8B3rFDw7xsdfTLmQYSgcFeZBSlsz 1UBJDWpd8gtfaREnt4YJPRtlVA== X-Google-Smtp-Source: AGHT+IE5Vg+Dyz/bKbjOzlL3IhVdCKmEQ4+B0ZJog4XAHOE3jOi5ifn8BwFCXivW5nM8DHHe2ehqDg== X-Received: by 2002:aa7:df86:0:b0:52b:db31:3c48 with SMTP id b6-20020aa7df86000000b0052bdb313c48mr2505539edy.0.1695832181907; Wed, 27 Sep 2023 09:29:41 -0700 (PDT) Received: from [192.168.1.127] (i5C7438B3.versanet.de. [92.116.56.179]) by smtp.gmail.com with ESMTPSA id q8-20020a056402032800b00522828d438csm8384685edw.7.2023.09.27.09.29.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 09:29:41 -0700 (PDT) From: Erik Schilling Date: Wed, 27 Sep 2023 18:29:38 +0200 Subject: [libgpiod][PATCH 2/3] bindings: rust: allow cloning line::Info -> line::OwnedInfo MIME-Version: 1.0 Message-Id: <20230927-rust-line-info-soundness-v1-2-990dce6f18ab@linaro.org> References: <20230927-rust-line-info-soundness-v1-0-990dce6f18ab@linaro.org> In-Reply-To: <20230927-rust-line-info-soundness-v1-0-990dce6f18ab@linaro.org> To: Linux-GPIO Cc: Viresh Kumar , Manos Pitsidianakis , Erik Schilling X-Mailer: b4 0.13-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1695832179; l=4959; i=erik.schilling@linaro.org; s=20230523; h=from:subject:message-id; bh=wQpB0yQ8o4YDV6o9oiQRDwi7P1a+31s2CDhZtZJBIjM=; b=1DsvWKZUO+7dZ6A4qN0TVt45CBiPVHyhOS64O5J5R0LPk0q7TGWXS8Z9cWOWnVG7do67TpdpA UpEvCS+CSRVD1yvbuX48bpYfym955Gd2ix2xea59Qv3cUAjrPGOx2hC X-Developer-Key: i=erik.schilling@linaro.org; a=ed25519; pk=/nNqy8/YOEdthj1epXl5FgwCTKEiVqTqqnVN1jVal7s= Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org While one would usually use the ToOwned [1] contract in rust, libgpipd's API only allows copying that may fail. Thus, we cannot implement the existing trait and roll our own method. I went with `try_clone` since that seems to be used in similar cases across the `std` crate [2]. It also closes the gap of not having any way to clone owned instances. Though - again - not through the Clone trait which may not fail [3]. [1] https://doc.rust-lang.org/std/borrow/trait.ToOwned.html [2] https://doc.rust-lang.org/std/index.html?search=try_clone [3] https://doc.rust-lang.org/std/clone/trait.Clone.html Signed-off-by: Erik Schilling --- bindings/rust/libgpiod/src/lib.rs | 1 + bindings/rust/libgpiod/src/line_info.rs | 16 ++++++++++ bindings/rust/libgpiod/tests/line_info.rs | 53 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/bindings/rust/libgpiod/src/lib.rs b/bindings/rust/libgpiod/src/lib.rs index 3acc98c..fd95ed2 100644 --- a/bindings/rust/libgpiod/src/lib.rs +++ b/bindings/rust/libgpiod/src/lib.rs @@ -74,6 +74,7 @@ pub enum OperationType { LineConfigSetOutputValues, LineConfigGetOffsets, LineConfigGetSettings, + LineInfoCopy, LineRequestReconfigLines, LineRequestGetVal, LineRequestGetValSubset, diff --git a/bindings/rust/libgpiod/src/line_info.rs b/bindings/rust/libgpiod/src/line_info.rs index 32c4bb2..fe01a14 100644 --- a/bindings/rust/libgpiod/src/line_info.rs +++ b/bindings/rust/libgpiod/src/line_info.rs @@ -58,6 +58,22 @@ impl Info { self as *const _ as *mut _ } + /// Clones the [gpiod::gpiod_line_info] instance to an [InfoOwned] + pub fn try_clone(&self) -> Result { + // SAFETY: `gpiod_line_info` is guaranteed to be valid here. + let copy = unsafe { gpiod::gpiod_line_info_copy(self.as_raw_ptr()) }; + if copy.is_null() { + return Err(Error::OperationFailed( + crate::OperationType::LineInfoCopy, + errno::errno(), + )); + } + + // SAFETY: The copy succeeded, we are the owner and stop using the + // pointer after this. + Ok(unsafe { InfoOwned::from_raw_owned(copy) }) + } + /// Get the offset of the line within the GPIO chip. /// /// The offset uniquely identifies the line on the chip. The combination of the chip and offset diff --git a/bindings/rust/libgpiod/tests/line_info.rs b/bindings/rust/libgpiod/tests/line_info.rs index ce66a60..d02c9ea 100644 --- a/bindings/rust/libgpiod/tests/line_info.rs +++ b/bindings/rust/libgpiod/tests/line_info.rs @@ -19,6 +19,10 @@ mod line_info { const NGPIO: usize = 8; mod properties { + use std::thread; + + use libgpiod::{line, request}; + use super::*; #[test] @@ -271,5 +275,54 @@ mod line_info { assert!(info.is_debounced()); assert_eq!(info.debounce_period(), Duration::from_millis(100)); } + + fn generate_line_event(chip: &Chip) { + let mut line_config = line::Config::new().unwrap(); + line_config + .add_line_settings(&[0], line::Settings::new().unwrap()) + .unwrap(); + + let mut request = chip + .request_lines(Some(&request::Config::new().unwrap()), &line_config) + .unwrap(); + + let mut new_line_config = line::Config::new().unwrap(); + let mut settings = line::Settings::new().unwrap(); + settings.set_direction(Direction::Output).unwrap(); + new_line_config.add_line_settings(&[0], settings).unwrap(); + request.reconfigure_lines(&new_line_config).unwrap(); + } + + #[test] + fn ownership() { + let sim = Sim::new(Some(1), None, false).unwrap(); + sim.set_line_name(0, "Test line").unwrap(); + sim.enable().unwrap(); + + let chip = Chip::open(&sim.dev_path()).unwrap(); + + // start watching line + chip.watch_line_info(0).unwrap(); + + generate_line_event(&chip); + + // read generated event + let event = chip.read_info_event().unwrap(); + let info = event.line_info().unwrap(); + assert_eq!(info.name().unwrap(), "Test line"); + + // clone info and move to separate thread + let info = info.try_clone().unwrap(); + + // drop the original event with the associated line_info + drop(event); + + // assert that we can still read the name + thread::scope(|s| { + s.spawn(move || { + assert_eq!(info.name().unwrap(), "Test line"); + }); + }); + } } } From patchwork Wed Sep 27 16:29:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erik Schilling X-Patchwork-Id: 726802 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 730CCE82CAE for ; Wed, 27 Sep 2023 16:29:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231240AbjI0Q3t (ORCPT ); Wed, 27 Sep 2023 12:29:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231256AbjI0Q3r (ORCPT ); Wed, 27 Sep 2023 12:29:47 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A40EF9 for ; Wed, 27 Sep 2023 09:29:44 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-5335725cf84so12837578a12.2 for ; Wed, 27 Sep 2023 09:29:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695832183; x=1696436983; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Vo06My6sHAZQKm5bcwq+iQTqtJpbGcs8+Z9sow3dJjE=; b=uoZNBaUeajiyqB+afUHkgPrKwjSHk3AC9DO3L+4ECNCzkt8Fn+tbyjGXjsQ4jUNtK8 6kzrFGSHwug61vtnL5X7duV2J/joPnlNJgh/Rkg66UtUyrZljJ57OnCgWLSik9zVUzz2 PMgLNXB+l8wQSD1N71VdKmTvfjS8Tr3vnz4PVL6deAjj1Ng5yntFlUM34PK9WL3zDSlx I5qDxeXbM3Q/6wLRfQjfIa5f/j/dKrssx2RM7tQ6muSl0gv+jQtVLblym+jpbnDGUszT kuDrz24pR1oA3yP5pai99U3bnVKoWloZhmsqGhdgSz4QgnEK8CLtfvbhY5CYcXAoQcc3 ab5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695832183; x=1696436983; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Vo06My6sHAZQKm5bcwq+iQTqtJpbGcs8+Z9sow3dJjE=; b=Fe28lY+xBXzPmL4owWMmh1CswTnBACkvA96DVTFNCdHUTYAxC1UtJ3WUpESjXsjfRe RPtbABAuGqjT/I9yB3OWhiZ6JcpMzbZE91rmiYYoZs6PcGLl4UaWxGt8mk7Ela68DDbH JfPs+PSx4QbErFQuFtljW+jCmAK4IVZ1KMeU9nUcBqlY4tEbSCWK5EM5rClE1g2U98F5 0yMZBRujmNJclGhYICWUxwXfdzloBEKr0/oYTTWJOUE6kBmBnceqKr2NjGd1TA2a9YEy YmAoR+C4QbHFdNaeLDMoAJ0+A+ktoJLY6oDwk8GU7mhKReQkMZ8xlO9Xkb/4dGAMtWzE VDuA== X-Gm-Message-State: AOJu0YwCDPrRXJ/IpI7BI9ky+xRUVm6AS+FCtUQeM7doVwIqHFHqchHa OtrGqK2YPezm7Xi0TKBqMaI0qw== X-Google-Smtp-Source: AGHT+IFEZ5thY3L1iERKZQyWbVp89GbCHlEnEem3oDxbpPPtoxXhZaXuovdjvqJMclNHBggUOw5rBw== X-Received: by 2002:aa7:d38c:0:b0:52f:2bd3:6f4d with SMTP id x12-20020aa7d38c000000b0052f2bd36f4dmr2798420edq.0.1695832182719; Wed, 27 Sep 2023 09:29:42 -0700 (PDT) Received: from [192.168.1.127] (i5C7438B3.versanet.de. [92.116.56.179]) by smtp.gmail.com with ESMTPSA id q8-20020a056402032800b00522828d438csm8384685edw.7.2023.09.27.09.29.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 09:29:42 -0700 (PDT) From: Erik Schilling Date: Wed, 27 Sep 2023 18:29:39 +0200 Subject: [libgpiod][PATCH 3/3] bindings: rust: bump major for libgpiod crate MIME-Version: 1.0 Message-Id: <20230927-rust-line-info-soundness-v1-3-990dce6f18ab@linaro.org> References: <20230927-rust-line-info-soundness-v1-0-990dce6f18ab@linaro.org> In-Reply-To: <20230927-rust-line-info-soundness-v1-0-990dce6f18ab@linaro.org> To: Linux-GPIO Cc: Viresh Kumar , Manos Pitsidianakis , Erik Schilling X-Mailer: b4 0.13-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1695832179; l=1005; i=erik.schilling@linaro.org; s=20230523; h=from:subject:message-id; bh=PcOVUqQ7ca/3tD9NzySf7uY+U8FcDHCGFswfcvmUy7g=; b=g11dKo+5YPCSm00IapYC7B2FeohkFEvdzxUz8DF+UUsHqCmGO7jZ7E8m/sgiw2y6rPIzfC4uj +vSCWiBH6UZAdFl56Ou5GZGIzXvZSpKL7swiqwH3pCqPQ9nejrD6CGn X-Developer-Key: i=erik.schilling@linaro.org; a=ed25519; pk=/nNqy8/YOEdthj1epXl5FgwCTKEiVqTqqnVN1jVal7s= Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Since the type changes around ownership of line_info instances are not necessarily transparent to the user, we bump the major for the next release of the crate. Note: I am using the term "major" as defined in the Rust SemVer compatibility guide [1], where the first non-zero digit is considered as "major". [1] https://doc.rust-lang.org/cargo/reference/semver.html Signed-off-by: Erik Schilling --- bindings/rust/libgpiod/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/rust/libgpiod/Cargo.toml b/bindings/rust/libgpiod/Cargo.toml index 518e5e5..3be4aa0 100644 --- a/bindings/rust/libgpiod/Cargo.toml +++ b/bindings/rust/libgpiod/Cargo.toml @@ -4,7 +4,7 @@ [package] name = "libgpiod" -version = "0.1.0" +version = "0.2.0" authors = ["Viresh Kumar "] description = "libgpiod wrappers" repository = "https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git"