From patchwork Fri Jan 13 21:51:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 644784 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 E7220C6379F for ; Fri, 13 Jan 2023 21:52:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229707AbjAMVwV (ORCPT ); Fri, 13 Jan 2023 16:52:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230303AbjAMVwU (ORCPT ); Fri, 13 Jan 2023 16:52:20 -0500 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 884E289BCD for ; Fri, 13 Jan 2023 13:52:18 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id g10so16106786wmo.1 for ; Fri, 13 Jan 2023 13:52:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Xf3VCHX9G7NEnm6DH5J+oX0WrIsiqfcIHSHFq9HHsok=; b=f57bzUxHI4FdG1+93JOTETjh1Y4o5qnb/fezZ0isN2oaroqx3crVWElxcEqNsirdmL qnmUVaOIncyIx5cZz6OUczgkmiPPrrkzsph1P5tw+3okDbSYg+gP56n2Dw5v6IGeXvET bnGuqTnIP97BEdn2a8E8KedrSZC6RJmCxv2Bh3Q5IA/4+HdelMGYXBr1IhC4km7KarL6 6gUcuPlkgcdlPjLUUAZZXsr+Qh2ymnhY+ghAtxldZBWktKskt2nyyiRbq130Ud0MXFwy Fbs/ouLXdb98HjWmkMA/3Vy61T3fMWAUsWsAhW4kT4QXrU4Bc5+2KpANl2WnRXt+iKbn Jd4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xf3VCHX9G7NEnm6DH5J+oX0WrIsiqfcIHSHFq9HHsok=; b=i0/iasTOfFF3IiNLETC7YajThx8gXaC8QJU1RU8M8iPr6Pw3ckIMVTrImZJBkYRDXE EXY0HWRsKygfrc+DQ+9rgkM+Jb5y4udlf6N+wqtugJVZXvmmH+LSC1YhkbKzY0KVXudO twZqYLYK0aghth4A8YONNm6ZLs/sJmcpS73c+nSnxTw/XjIDGhZBKVxZesXICS1FCTqJ tuKzVPjWbtdZyjSncqPGvSqsJ7UCp2NilsgHSt+tp2Zb71C7DFpgYmYEH9QK0SoQ4/K1 HO+5bxFJuFBEUidb/wth+D+fSheD653mCbdrsLWjYYW8xU7HUwBMzb01gNy07J09z6hR 2fcA== X-Gm-Message-State: AFqh2kr1AcWBssKJijoSeXcMszLvjyuYtGK1PCjDSeW7fpPVhT8IeZj8 gbCZ/MQc0lXExBrSVlWFUHq4sw== X-Google-Smtp-Source: AMrXdXug5/egwuoRcgMsnfpEIZLuuH6i1LGlVjKN6ajpkMDlYRI27MvRYGe0J0PTla0y0KTvbNKNHg== X-Received: by 2002:a05:600c:348b:b0:3d1:fcb4:4074 with SMTP id a11-20020a05600c348b00b003d1fcb44074mr70800254wmq.22.1673646737083; Fri, 13 Jan 2023 13:52:17 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:16 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 01/16] README: update for libgpiod v2 Date: Fri, 13 Jan 2023 22:51:55 +0100 Message-Id: <20230113215210.616812-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Certain parts of the README file still refer to concepts removed from libgpiod v2. Update whatever needs updating. Signed-off-by: Bartosz Golaszewski --- README | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/README b/README index d51d701..894fc5d 100644 --- a/README +++ b/README @@ -1,5 +1,5 @@ # SPDX-License-Identifier: CC-BY-SA-4.0 -# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski +# SPDX-FileCopyrightText: 2017-2023 Bartosz Golaszewski libgpiod ======== @@ -30,14 +30,10 @@ allow an easy conversion of user scripts to using the character device. BUILDING -------- -This is a pretty standard autotools project. It does not depend on any -libraries other than the standard C library with GNU extensions. +This is a pretty standard autotools project. The core C library does not have +any external dependencies other than the standard C library with GNU extensions. -The autoconf version needed to compile the project is 2.61. - -Recent kernel headers are also required for the GPIO user API definitions. For -the exact version of kernel headers required, please refer to the configure.ac -contents. +The command-line tools optionally depend on libedit for the interactive feature. To build the project (including command-line utilities) run: @@ -51,6 +47,8 @@ arguments to it. If building from release tarballs, the configure script is already provided and there's no need to invoke autogen.sh. +For all configure features, see: ./configure --help. + TOOLS ----- @@ -231,10 +229,10 @@ interface. The minimum kernel version required to run the tests can be checked in the tests/gpiod-test.c source file (it's subject to change if new features are -added to the kernel). The tests work together with the gpio-mockup kernel -module which must be enabled. NOTE: the module must not be built-in. A helper -library - libgpiomockup - is included to enable straightforward interaction -with the module. +added to the kernel). The tests work together with the gpio-sim kernel which +must either be built-in or available for loading using kmod. A helper +library - libgpiosim - is included to enable straightforward interaction with +the module. To build the testing executable add the '--enable-tests' option when running the configure script. If enabled, the tests will be installed next to @@ -251,12 +249,12 @@ The gpio-tools programs can be tested separately using the gpio-tools-test.bats script. It requires bats[1] to run and assumes that the tested executables are in the same directory as the script. -Both C++ and Python bindings also include their own test-suites. Both reuse the -libgpiomockup library to avoid code duplication when interacting with -gpio-mockup. +C++, Rust and Python bindings also include their own test-suites. Both reuse the +libgpiosim library to avoid code duplication when interacting with gpio-sim. Python test-suite uses the standard unittest package. C++ tests use an external -testing framework - Catch2 - which must be installed in the system. +testing framework - Catch2 - which must be installed in the system. Rust +bindings use the standard tests module layout and the #[test] attribute. DOCUMENTATION ------------- @@ -268,6 +266,8 @@ doxygen markup blocks. Doxygen documentation can be generated by executing Python bindings contain help strings that can be accessed with the help builtin. +Rust bindings use rustdoc. + Man pages for command-line programs are generated automatically if gpio-tools were selected and help2man is available in the system. From patchwork Fri Jan 13 21:51:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 644783 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 85961C678D7 for ; Fri, 13 Jan 2023 21:52:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229703AbjAMVwW (ORCPT ); Fri, 13 Jan 2023 16:52:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230417AbjAMVwU (ORCPT ); Fri, 13 Jan 2023 16:52:20 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83111892EC for ; Fri, 13 Jan 2023 13:52:19 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id p3-20020a05600c1d8300b003d9ee5f125bso13157488wms.4 for ; Fri, 13 Jan 2023 13:52:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XRf2SFNNUkAMCj4FMHJWyVLMOnnDr1hVEaTrQOcK0nU=; b=UEP2lxy5YcwaP4ijTBG7ZiJkemlP+O4YiyjwFzSC6u5uIFdxXhCwBbKVcNdK9ZJd9y eTJAfykZCcjtDuB+QsfrbuPbWh4BOerAjJXXqMYMddlGcNs1kfTHIDip02rFyVrF4O++ UJnJq0TnfIIwO5r+1ihpG3sLzEn/oubSQUa1PHnR3WKO+y6KgL0H+GnRbBnpi07dsoO+ AWdZln88UbCWsui9eKzNU8UKF9+sZqLjDT2cB8t7JhWs+8K2VGWIAmeRSW7Gn9Yb4cV5 kzjE64k/OEYsNxvFlsiEKTEsN7GkHibNflZS+kIwoN5N7qr/w7pvFlpmVolMalay+5nL PyvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XRf2SFNNUkAMCj4FMHJWyVLMOnnDr1hVEaTrQOcK0nU=; b=N03EHxmuJx4JBDAgQRMrX76Syl8kPTE0nYmEGasJHA4sgz95S0CiWbGrpmxJijRNBL WvZD21jU/epW4piYobxGqUGc2OosbsNXQrq+U3/NdD4RqxNQ+abPQXE50BSfIb7g6NkY XZiyYnQrEhD9Y8YAgt5ZzcKpzF0sPP5Zf60+HXmRqO9HeszkOKc8DSdPU99Db9T1s+sm dQrG54VF8C3/x73L0nj8xqBUiUgNAB4nnHdOOz/SWFOBxM/0qAsgBuN29MIEE4whgqnp 2AD7YKUsE1/tY7jsjw6Hnhyif76asw/0g4/MxmXWrWApxR9/+G6+02CtPwrwPV6ot7JR rhxw== X-Gm-Message-State: AFqh2koHJUzY6bqC/csYo7z+Veq+Wsucpj2xsVLyVSZZExVv5RcExCyX p3gaHVFCg9fD8pldTT/dBcDQ6w== X-Google-Smtp-Source: AMrXdXuxrMdWUl7b7LtnT4dBtPH+HWdlRpqPW+ZnAL3m0MXd8Czm0nWeBHmx1nzU+xeJ5vZsDD5w7w== X-Received: by 2002:a05:600c:3b93:b0:3d3:43ae:4d10 with SMTP id n19-20020a05600c3b9300b003d343ae4d10mr62444123wms.11.1673646738035; Fri, 13 Jan 2023 13:52:18 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:17 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 02/16] tests: avoid shadowing local variables with common names in macros Date: Fri, 13 Jan 2023 22:51:56 +0100 Message-Id: <20230113215210.616812-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski The name 'ret' if very common for local variables so change it to _ret in test helper macros to avoid potential shadowing. Signed-off-by: Bartosz Golaszewski --- tests/gpiod-test-helpers.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/gpiod-test-helpers.h b/tests/gpiod-test-helpers.h index 2d86345..b40b820 100644 --- a/tests/gpiod-test-helpers.h +++ b/tests/gpiod-test-helpers.h @@ -118,11 +118,11 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_edge_event_buffer, #define gpiod_test_line_config_add_line_settings_or_fail(_line_cfg, _offsets, \ _num_offsets, _settings) \ do { \ - gint ret = gpiod_line_config_add_line_settings(_line_cfg, \ - _offsets, \ - _num_offsets, \ - _settings); \ - g_assert_cmpint(ret, ==, 0); \ + gint _ret = gpiod_line_config_add_line_settings(_line_cfg, \ + _offsets, \ + _num_offsets, \ + _settings); \ + g_assert_cmpint(_ret, ==, 0); \ gpiod_test_return_if_failed(); \ } while (0) @@ -147,9 +147,9 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_edge_event_buffer, #define gpiod_test_reconfigure_lines_or_fail(_request, _line_cfg) \ do { \ - gint ret = gpiod_line_request_reconfigure_lines(_request, \ - _line_cfg); \ - g_assert_cmpint(ret, ==, 0); \ + gint _ret = gpiod_line_request_reconfigure_lines(_request, \ + _line_cfg); \ + g_assert_cmpint(_ret, ==, 0); \ gpiod_test_return_if_failed(); \ } while (0) From patchwork Fri Jan 13 21:51:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 642316 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 2FF05C6379F for ; Fri, 13 Jan 2023 21:52:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230472AbjAMVwY (ORCPT ); Fri, 13 Jan 2023 16:52:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230432AbjAMVwX (ORCPT ); Fri, 13 Jan 2023 16:52:23 -0500 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DCEF89BCE for ; Fri, 13 Jan 2023 13:52:20 -0800 (PST) Received: by mail-wm1-x336.google.com with SMTP id ja17so16103817wmb.3 for ; Fri, 13 Jan 2023 13:52:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Tx/vmoqXDJzlG1fFQqvSxnzJDDAgyKL8zbXzXS4i5rM=; b=hzKBKxMUnrtI5vGDEahfP25dhgv+049XsUJl3hkyg9Juyc216mIXjt6Auf/FDIqEWp D2k/geWWN2S5vfcjujd9Cfc/Qv27UkKet+p7mcVtHcT70AlUE288KYdA0LBTZpIbUzfh 2biXJ4XzoaAUaxegZA4Th5CusKGzCGASw5Yk7W5ZYRccSBKHOm77FROgyksAUCffYasc k2nRvRdNIdmpJgUqQFFyPnfdd1dgaWD8sfIu64AUabmVjLvTrR/GDb5QmrF3bw18+bl5 c6AfIDee6x6rc/eYZCgVps6fS7w85s3Py5vR/AccDzxorZp269HREyGoPpgXmHnsp05s NRQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Tx/vmoqXDJzlG1fFQqvSxnzJDDAgyKL8zbXzXS4i5rM=; b=O52sCBXO7Vd59jSZqvELfFY6dM0UtccEVskwDdG6wL66Yh0mwy6dSVNEGsC7TWIp/7 PG6bl8oU7Xr+eQAXupmRcRA1xyU5th4K6UsXwXe6S45yBtzgRvIqamfVMuVhfoNugSOt 60g27qGh1fgj2lTp4y6kbf2UpRYMHtNW288grfDEQPVGykm5Nz0svrqgzqiv1doIY8zB HzJi7c16EutwuDYXfl/F/E8LZPSJl4Shb0coA/Cdf10s376avfoVxhepJUXiOUnJgxam qseiSZTMeBMSbIk7bhW7bfdjYNRzOAhZ4YRFEZddi0RWZnGfI9PNyybmr/7KPQf2jHoW +Rzg== X-Gm-Message-State: AFqh2koteH+yceuisuZACkHodzhbyA7GL8eBgMGCTMhhduWocv+N4u9v WCiRl228a33EflGH2mA8/myLAqbOFxP1SMx7 X-Google-Smtp-Source: AMrXdXt3Vt7hZvdVLU/+Bysc7h2hkK+VL5VzjMp1CG9Ts4FPpAlClcp2kHABmLHC88GzvMxa6Gw1nw== X-Received: by 2002:a05:600c:755:b0:3d9:7065:5b9c with SMTP id j21-20020a05600c075500b003d970655b9cmr994820wmn.6.1673646738994; Fri, 13 Jan 2023 13:52:18 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:18 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 03/16] build: unify the coding style of source files lists in Makefiles Date: Fri, 13 Jan 2023 22:51:57 +0100 Message-Id: <20230113215210.616812-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Use the most common and readable convention for listing source files in Makefiles wherever it's not consistent yet. Signed-off-by: Bartosz Golaszewski --- bindings/cxx/Makefile.am | 32 +++++++++++++-------------- bindings/cxx/examples/Makefile.am | 12 +++++------ bindings/cxx/tests/Makefile.am | 36 +++++++++++++++---------------- lib/Makefile.am | 27 ++++++++++++----------- man/Makefile.am | 8 ++++++- tests/Makefile.am | 34 ++++++++++++++--------------- 6 files changed, 78 insertions(+), 71 deletions(-) diff --git a/bindings/cxx/Makefile.am b/bindings/cxx/Makefile.am index f719072..f2fc884 100644 --- a/bindings/cxx/Makefile.am +++ b/bindings/cxx/Makefile.am @@ -2,22 +2,22 @@ # SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski lib_LTLIBRARIES = libgpiodcxx.la -libgpiodcxx_la_SOURCES = \ - chip.cpp \ - chip-info.cpp \ - edge-event-buffer.cpp \ - edge-event.cpp \ - exception.cpp \ - info-event.cpp \ - internal.cpp \ - internal.hpp \ - line.cpp \ - line-config.cpp \ - line-info.cpp \ - line-request.cpp \ - line-settings.cpp \ - misc.cpp \ - request-builder.cpp \ +libgpiodcxx_la_SOURCES = \ + chip.cpp \ + chip-info.cpp \ + edge-event-buffer.cpp \ + edge-event.cpp \ + exception.cpp \ + info-event.cpp \ + internal.cpp \ + internal.hpp \ + line.cpp \ + line-config.cpp \ + line-info.cpp \ + line-request.cpp \ + line-settings.cpp \ + misc.cpp \ + request-builder.cpp \ request-config.cpp libgpiodcxx_la_CXXFLAGS = -Wall -Wextra -g -std=gnu++17 diff --git a/bindings/cxx/examples/Makefile.am b/bindings/cxx/examples/Makefile.am index c7ec3cf..e4136f5 100644 --- a/bindings/cxx/examples/Makefile.am +++ b/bindings/cxx/examples/Makefile.am @@ -6,12 +6,12 @@ AM_CXXFLAGS += -Wall -Wextra -g -std=gnu++17 AM_LDFLAGS = -lgpiodcxx -L$(top_builddir)/bindings/cxx/ noinst_PROGRAMS = \ - gpiodetectcxx \ - gpiofindcxx \ - gpiogetcxx \ - gpioinfocxx \ - gpiomoncxx \ - gpiosetcxx + gpiodetectcxx \ + gpiofindcxx \ + gpiogetcxx \ + gpioinfocxx \ + gpiomoncxx \ + gpiosetcxx gpiodetectcxx_SOURCES = gpiodetectcxx.cpp diff --git a/bindings/cxx/tests/Makefile.am b/bindings/cxx/tests/Makefile.am index 4971dd4..924a3cd 100644 --- a/bindings/cxx/tests/Makefile.am +++ b/bindings/cxx/tests/Makefile.am @@ -12,21 +12,21 @@ AM_LDFLAGS += -pthread bin_PROGRAMS = gpiod-cxx-test -gpiod_cxx_test_SOURCES = \ - check-kernel.cpp \ - gpiod-cxx-test-main.cpp \ - gpiosim.cpp \ - gpiosim.hpp \ - helpers.cpp \ - helpers.hpp \ - tests-chip.cpp \ - tests-chip-info.cpp \ - tests-edge-event.cpp \ - tests-info-event.cpp \ - tests-line.cpp \ - tests-line-config.cpp \ - tests-line-info.cpp \ - tests-line-request.cpp \ - tests-line-settings.cpp \ - tests-misc.cpp \ - tests-request-config.cpp \ No newline at end of file +gpiod_cxx_test_SOURCES = \ + check-kernel.cpp \ + gpiod-cxx-test-main.cpp \ + gpiosim.cpp \ + gpiosim.hpp \ + helpers.cpp \ + helpers.hpp \ + tests-chip.cpp \ + tests-chip-info.cpp \ + tests-edge-event.cpp \ + tests-info-event.cpp \ + tests-line.cpp \ + tests-line-config.cpp \ + tests-line-info.cpp \ + tests-line-request.cpp \ + tests-line-settings.cpp \ + tests-misc.cpp \ + tests-request-config.cpp diff --git a/lib/Makefile.am b/lib/Makefile.am index dd90abd..3e7114b 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -2,19 +2,20 @@ # SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski lib_LTLIBRARIES = libgpiod.la -libgpiod_la_SOURCES = chip.c \ - chip-info.c \ - edge-event.c \ - info-event.c \ - internal.h \ - internal.c \ - line-config.c \ - line-info.c \ - line-request.c \ - line-settings.c \ - misc.c \ - request-config.c \ - uapi/gpio.h +libgpiod_la_SOURCES = \ + chip.c \ + chip-info.c \ + edge-event.c \ + info-event.c \ + internal.h \ + internal.c \ + line-config.c \ + line-info.c \ + line-request.c \ + line-settings.c \ + misc.c \ + request-config.c \ + uapi/gpio.h libgpiod_la_CFLAGS = -Wall -Wextra -g -std=gnu89 libgpiod_la_CFLAGS += -fvisibility=hidden -I$(top_srcdir)/include/ diff --git a/man/Makefile.am b/man/Makefile.am index 201a52b..1b52e0f 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -3,7 +3,13 @@ if WITH_MANPAGES -dist_man1_MANS = gpiodetect.man gpioinfo.man gpioget.man gpioset.man gpiomon.man gpionotify.man +dist_man1_MANS = \ + gpiodetect.man \ + gpioinfo.man \ + gpioget.man \ + gpioset.man \ + gpiomon.man \ + gpionotify.man %.man: $(top_builddir)/tools/$(*F) help2man $(top_builddir)/tools/$(*F) --include=$(srcdir)/template --output=$(builddir)/$@ --no-info diff --git a/tests/Makefile.am b/tests/Makefile.am index 4dd5297..0fdbe5b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -14,20 +14,20 @@ LDADD += $(GLIB_LIBS) $(GOBJECT_LIBS) bin_PROGRAMS = gpiod-test -gpiod_test_SOURCES = \ - gpiod-test.c \ - gpiod-test.h \ - gpiod-test-helpers.c \ - gpiod-test-helpers.h \ - gpiod-test-sim.c \ - gpiod-test-sim.h \ - tests-chip.c \ - tests-chip-info.c \ - tests-edge-event.c \ - tests-info-event.c \ - tests-line-config.c \ - tests-line-info.c \ - tests-line-request.c \ - tests-line-settings.c \ - tests-misc.c \ - tests-request-config.c +gpiod_test_SOURCES = \ + gpiod-test.c \ + gpiod-test.h \ + gpiod-test-helpers.c \ + gpiod-test-helpers.h \ + gpiod-test-sim.c \ + gpiod-test-sim.h \ + tests-chip.c \ + tests-chip-info.c \ + tests-edge-event.c \ + tests-info-event.c \ + tests-line-config.c \ + tests-line-info.c \ + tests-line-request.c \ + tests-line-settings.c \ + tests-misc.c \ + tests-request-config.c From patchwork Fri Jan 13 21:51:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 642315 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 4E687C3DA78 for ; Fri, 13 Jan 2023 21:52:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230398AbjAMVw0 (ORCPT ); Fri, 13 Jan 2023 16:52:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230411AbjAMVwY (ORCPT ); Fri, 13 Jan 2023 16:52:24 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8A4689BC8 for ; Fri, 13 Jan 2023 13:52:21 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id m26-20020a05600c3b1a00b003d9811fcaafso18284272wms.5 for ; Fri, 13 Jan 2023 13:52:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DQ+m10ubW+gpPgL0aywQHnwuQvQuckXI8k/5ON4pCnY=; b=tqnzBV7XSkKVpNsquUUhVzEC2i2npaEs/pj8fMUdojr9ejlVGGacB+GgcXlAaVoMPt eVpWCuJuMJ8mczVPAFyNtfY3s5d/MH9CUoHn6Hl3CG2zGNHdqTfSZs7bNUOZsTAI1k+2 64F2kdHIBkfFwq41TmzzoIr6aSiG8cO4NHLdaBWsYCRiewMs9WhLsi1uO03SJ3kKMXoz e1NUvXrh8rqM1B/sJ1h2sDAVNkFsYp1+TVJo6OTLTmeTye3aFzFdfWyK4dtekRfiubTn GEp3X1ixdNAUnxJR+9NqMLub3ycScQDuIQUHtoPG7Jlw+t9UkmRy4A/mxxcKWWOZBkm4 H4EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DQ+m10ubW+gpPgL0aywQHnwuQvQuckXI8k/5ON4pCnY=; b=XIQMZoV8/GJauKN+sMPozHECnY2WuGNpw0skue7ypjCBDSxHfnV6GtTme9jt+SPSmj VtiElvKVpnUQ3v10hFUbZsf8aTaD/GioEFuDdX85Fbrz2WJqZcI7W/abLqmIUbgSeHzP lI6u2rmtL+tB2SgTb3oxXm04X6q4T96QSLR60rHokDlsTgxXzKqsp+NkSAm/l/FZHrGv WDFx1RAL79N2tfpc2m1VqYVEfgdJlHT+0J0dL7z6r/yzmlWoBHOFW+i5BU9ucwjcXphv /pt6Hwv/m4s/aZTIWUvgKNYUiLxLYRk+9ORSTasg+O3MT4pOKo9BE9+wFiTOKoLvJQAG tzSw== X-Gm-Message-State: AFqh2kqblqkFZ4mcfE822JUsto2JPtXDZcNaGbWVYlTQ6JB7TU4ibOte W9Cf40jG3l9aSbm0LiWrCuXtvWe1/sDbdXdp X-Google-Smtp-Source: AMrXdXueUvqPs19b06dbsY6o9HOc77QE1BImNwiuNqiJCkOD6DGuHGDxLq9JmwGeF3wrmAc1n5hxAw== X-Received: by 2002:a05:600c:44c9:b0:3d1:f6b3:2ce3 with SMTP id f9-20020a05600c44c900b003d1f6b32ce3mr70769224wmo.35.1673646740016; Fri, 13 Jan 2023 13:52:20 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:19 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 04/16] treewide: unify gpiod_line_config/request_get_offsets() functions Date: Fri, 13 Jan 2023 22:51:58 +0100 Message-Id: <20230113215210.616812-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski We have two functions in the C API that allow users to retrieve a list of offsets from objects: gpiod_line_request_get_offsets() and gpiod_line_config_get_offsets(). Even though they serve pretty much the same purpose, they have different signatures and one of them also requires the user to free the memory allocated within the libgpiod library with a non-libgpiod free() function. Unify them: make them take the array in which to store offsets and the size of this array. Make them return the number of offsets actually stored in the array and make them impossible to fail. Change their names to be more descriptive and in the case of line_config: add a new function that allows users to get the number of configured offsets. Update the entire tree to use the new interfaces. For rust bindings: also unify the line config interface to return a map of line settings like C++ bindings do instead of having a function to get settings by offset. A map returned from a single call is easier to iterate over with a for loop than using an integer and calling the previous line_settings() method. Signed-off-by: Bartosz Golaszewski --- bindings/cxx/line-config.cpp | 18 ++---- bindings/cxx/line-request.cpp | 6 +- bindings/python/gpiod/ext/request.c | 8 +-- bindings/rust/libgpiod/src/line_config.rs | 71 ++++++++------------- bindings/rust/libgpiod/src/line_request.rs | 6 +- bindings/rust/libgpiod/tests/line_config.rs | 24 ++----- configure.ac | 1 + include/gpiod.h | 54 ++++++++++------ lib/line-config.c | 38 +++++------ lib/line-request.c | 23 ++++--- tests/tests-line-config.c | 32 ++++------ tests/tests-line-request.c | 10 +-- 12 files changed, 139 insertions(+), 152 deletions(-) diff --git a/bindings/cxx/line-config.cpp b/bindings/cxx/line-config.cpp index f7f1bfa..3ec99f0 100644 --- a/bindings/cxx/line-config.cpp +++ b/bindings/cxx/line-config.cpp @@ -102,31 +102,27 @@ GPIOD_CXX_API line_config& line_config::add_line_settings(const line::offsets& o GPIOD_CXX_API ::std::map line_config::get_line_settings() const { + ::std::size_t num_offsets = ::gpiod_line_config_get_num_configured_offsets( + this->_m_priv->config.get()); ::std::map settings_map; - ::std::size_t num_offsets; - unsigned int *offsets_ptr; - int ret; - - ret = ::gpiod_line_config_get_offsets(this->_m_priv->config.get(), - &num_offsets, &offsets_ptr); - if (ret) - throw_from_errno("unable to retrieve line offsets"); + ::std::vector offsets(num_offsets); if (num_offsets == 0) return settings_map; - ::std::unique_ptr offsets(offsets_ptr); + ::gpiod_line_config_get_configured_offsets(this->_m_priv->config.get(), + offsets.data(), num_offsets); for (size_t i = 0; i < num_offsets; i++) { line_settings settings; settings._m_priv->settings.reset(::gpiod_line_config_get_line_settings( this->_m_priv->config.get(), - offsets.get()[i])); + offsets[i])); if (!settings._m_priv->settings) throw_from_errno("unable to retrieve line settings"); - settings_map[offsets.get()[i]] = ::std::move(settings); + settings_map[offsets[i]] = ::std::move(settings); } return settings_map; diff --git a/bindings/cxx/line-request.cpp b/bindings/cxx/line-request.cpp index 34c5850..438c0fa 100644 --- a/bindings/cxx/line-request.cpp +++ b/bindings/cxx/line-request.cpp @@ -18,7 +18,7 @@ void line_request::impl::throw_if_released() const void line_request::impl::set_request_ptr(line_request_ptr& ptr) { this->request = ::std::move(ptr); - this->offset_buf.resize(::gpiod_line_request_get_num_lines(this->request.get())); + this->offset_buf.resize(::gpiod_line_request_get_num_requested_lines(this->request.get())); } void line_request::impl::fill_offset_buf(const line::offsets& offsets) @@ -67,7 +67,7 @@ GPIOD_CXX_API ::std::size_t line_request::num_lines() const { this->_m_priv->throw_if_released(); - return ::gpiod_line_request_get_num_lines(this->_m_priv->request.get()); + return ::gpiod_line_request_get_num_requested_lines(this->_m_priv->request.get()); } GPIOD_CXX_API line::offsets line_request::offsets() const @@ -78,7 +78,7 @@ GPIOD_CXX_API line::offsets line_request::offsets() const ::std::vector buf(num_lines); line::offsets offsets(num_lines); - ::gpiod_line_request_get_offsets(this->_m_priv->request.get(), buf.data()); + ::gpiod_line_request_get_requested_offsets(this->_m_priv->request.get(), buf.data(), buf.size()); for (unsigned int i = 0; i < num_lines; i++) offsets[i] = buf[i]; diff --git a/bindings/python/gpiod/ext/request.c b/bindings/python/gpiod/ext/request.c index d3e1448..a32ff8f 100644 --- a/bindings/python/gpiod/ext/request.c +++ b/bindings/python/gpiod/ext/request.c @@ -41,7 +41,7 @@ static PyObject * request_num_lines(request_object *self, void *Py_UNUSED(ignored)) { return PyLong_FromUnsignedLong( - gpiod_line_request_get_num_lines(self->request)); + gpiod_line_request_get_num_requested_lines(self->request)); } static PyObject *request_offsets(request_object *self, void *Py_UNUSED(ignored)) @@ -51,13 +51,13 @@ static PyObject *request_offsets(request_object *self, void *Py_UNUSED(ignored)) size_t num_lines, i; int ret; - num_lines = gpiod_line_request_get_num_lines(self->request); + num_lines = gpiod_line_request_get_num_requested_lines(self->request); offsets = PyMem_Calloc(num_lines, sizeof(unsigned int)); if (!offsets) return PyErr_NoMemory(); - gpiod_line_request_get_offsets(self->request, offsets); + gpiod_line_request_get_requested_offsets(self->request, offsets, num_lines); lines = PyList_New(num_lines); if (!lines) { @@ -365,7 +365,7 @@ PyObject *Py_gpiod_MakeRequestObject(struct gpiod_line_request *request, unsigned int *offsets; size_t num_lines; - num_lines = gpiod_line_request_get_num_lines(request); + num_lines = gpiod_line_request_get_num_requested_lines(request); req_obj = PyObject_New(request_object, &request_type); if (!req_obj) diff --git a/bindings/rust/libgpiod/src/line_config.rs b/bindings/rust/libgpiod/src/line_config.rs index 19dc187..0c8b293 100644 --- a/bindings/rust/libgpiod/src/line_config.rs +++ b/bindings/rust/libgpiod/src/line_config.rs @@ -2,8 +2,8 @@ // SPDX-FileCopyrightText: 2022 Linaro Ltd. // SPDX-FileCopyrightTest: 2022 Viresh Kumar -use std::os::raw::{c_ulong, c_void}; -use std::slice; +use std::os::raw::c_ulong; +use std::collections::HashMap; use super::{ gpiod, @@ -77,51 +77,32 @@ impl Config { } } - /// Get line settings for offset. - pub fn line_settings(&self, offset: Offset) -> Result { - // SAFETY: `gpiod_line_config` is guaranteed to be valid here. - let settings = unsafe { gpiod::gpiod_line_config_get_line_settings(self.config, offset) }; - - if settings.is_null() { - return Err(Error::OperationFailed( - OperationType::LineConfigGetSettings, - errno::errno(), - )); + /// Get a mapping of offsets to line settings stored by this object. + pub fn line_settings(&self) -> Result> { + let mut map: HashMap = HashMap::new(); + let num_lines = unsafe { gpiod::gpiod_line_config_get_num_configured_offsets(self.config) }; + let mut offsets = vec![0; num_lines as usize]; + + // SAFETY: gpiod_line_config is guaranteed to be valid here. + unsafe { gpiod::gpiod_line_config_get_configured_offsets(self.config, + offsets.as_mut_ptr(), + num_lines) }; + + for offset in offsets { + // SAFETY: `gpiod_line_config` is guaranteed to be valid here. + let settings = unsafe { gpiod::gpiod_line_config_get_line_settings(self.config, + offset) }; + if settings.is_null() { + return Err(Error::OperationFailed( + OperationType::LineConfigGetSettings, + errno::errno(), + )); + } + + map.insert(offset, Settings::new_with_settings(settings)); } - Ok(Settings::new_with_settings(settings)) - } - - /// Get configured offsets. - pub fn offsets(&self) -> Result> { - let mut num: u64 = 0; - let mut ptr: *mut Offset = std::ptr::null_mut(); - - // SAFETY: The `ptr` array returned by libgpiod is guaranteed to live as long - // as it is not explicitly freed with `free()`. - let ret = unsafe { - gpiod::gpiod_line_config_get_offsets( - self.config, - &mut num as *mut _ as *mut _, - &mut ptr, - ) - }; - - if ret == -1 { - return Err(Error::OperationFailed( - OperationType::LineConfigGetOffsets, - errno::errno(), - )); - } - - // SAFETY: The `ptr` array returned by libgpiod is guaranteed to live as long - // as it is not explicitly freed with `free()`. - let offsets = unsafe { slice::from_raw_parts(ptr as *const Offset, num as usize).to_vec() }; - - // SAFETY: The `ptr` array is guaranteed to be valid here. - unsafe { libc::free(ptr as *mut c_void) }; - - Ok(offsets) + Ok(map) } } diff --git a/bindings/rust/libgpiod/src/line_request.rs b/bindings/rust/libgpiod/src/line_request.rs index c16ec9f..b843862 100644 --- a/bindings/rust/libgpiod/src/line_request.rs +++ b/bindings/rust/libgpiod/src/line_request.rs @@ -28,7 +28,7 @@ impl Request { /// Get the number of lines in the request. pub fn num_lines(&self) -> usize { // SAFETY: `gpiod_line_request` is guaranteed to be valid here. - unsafe { gpiod::gpiod_line_request_get_num_lines(self.request) as usize } + unsafe { gpiod::gpiod_line_request_get_num_requested_lines(self.request) as usize } } /// Get the offsets of lines in the request. @@ -36,7 +36,9 @@ impl Request { let mut offsets = vec![0; self.num_lines() as usize]; // SAFETY: `gpiod_line_request` is guaranteed to be valid here. - unsafe { gpiod::gpiod_line_request_get_offsets(self.request, offsets.as_mut_ptr()) }; + unsafe { gpiod::gpiod_line_request_get_requested_offsets(self.request, + offsets.as_mut_ptr(), + self.num_lines() as u64) }; offsets } diff --git a/bindings/rust/libgpiod/tests/line_config.rs b/bindings/rust/libgpiod/tests/line_config.rs index bebf106..95f2178 100644 --- a/bindings/rust/libgpiod/tests/line_config.rs +++ b/bindings/rust/libgpiod/tests/line_config.rs @@ -37,8 +37,10 @@ mod line_config { lconfig.add_line_settings(&[0, 1, 2], lsettings1).unwrap(); lconfig.add_line_settings(&[4, 5], lsettings2).unwrap(); + let settings_map = lconfig.line_settings().unwrap(); + // Retrieve settings - let lsettings = lconfig.line_settings(1).unwrap(); + let lsettings = settings_map.get(&1).unwrap(); assert_eq!( lsettings.prop(SettingKind::Direction).unwrap(), SettingVal::Direction(Direction::Input) @@ -56,7 +58,7 @@ mod line_config { SettingVal::Drive(Drive::PushPull) ); - let lsettings = lconfig.line_settings(5).unwrap(); + let lsettings = settings_map.get(&5).unwrap(); assert_eq!( lsettings.prop(SettingKind::Direction).unwrap(), SettingVal::Direction(Direction::Output) @@ -74,22 +76,4 @@ mod line_config { SettingVal::OutputValue(Value::Active) ); } - - #[test] - fn offsets() { - let mut lsettings1 = line::Settings::new().unwrap(); - lsettings1.set_direction(Direction::Input).unwrap(); - - let mut lsettings2 = line::Settings::new().unwrap(); - lsettings2.set_event_clock(EventClock::Realtime).unwrap(); - - // Add settings for multiple lines - let lconfig = line::Config::new().unwrap(); - lconfig.add_line_settings(&[0, 1, 2], lsettings1).unwrap(); - lconfig.add_line_settings(&[4, 5], lsettings2).unwrap(); - - // Verify offsets - let offsets = lconfig.offsets().unwrap(); - assert_eq!(offsets, [0, 1, 2, 4, 5]); - } } diff --git a/configure.ac b/configure.ac index 599c598..a4b92ce 100644 --- a/configure.ac +++ b/configure.ac @@ -94,6 +94,7 @@ AC_CHECK_HEADERS([dirent.h], [], [HEADER_NOT_FOUND_LIB([dirent.h])]) AC_CHECK_HEADERS([poll.h], [], [HEADER_NOT_FOUND_LIB([poll.h])]) AC_CHECK_HEADERS([sys/sysmacros.h], [], [HEADER_NOT_FOUND_LIB([sys/sysmacros.h])]) AC_CHECK_HEADERS([sys/ioctl.h], [], [HEADER_NOT_FOUND_LIB([sys/ioctl.h])]) +AC_CHECK_HEADERS([sys/param.h], [], [HEADER_NOT_FOUND_LIB([sys/param.h])]) AC_CHECK_HEADERS([sys/stat.h], [], [HEADER_NOT_FOUND_LIB([sys/stat.h])]) AC_CHECK_HEADERS([sys/types.h], [], [HEADER_NOT_FOUND_LIB([sys/types.h])]) AC_CHECK_HEADERS([linux/const.h], [], [HEADER_NOT_FOUND_LIB([linux/const.h])]) diff --git a/include/gpiod.h b/include/gpiod.h index 56c182f..dfc5334 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -780,19 +780,29 @@ struct gpiod_line_settings * gpiod_line_config_get_line_settings(struct gpiod_line_config *config, unsigned int offset); +/** + * @brief Get the number of configured line offsets. + * @param config Line config object. + * @return Number of offsets for which line settings have been added. + */ +size_t +gpiod_line_config_get_num_configured_offsets(struct gpiod_line_config *config); + /** * @brief Get configured offsets. * @param config Line config object. - * @param num_offsets Pointer to a variable in which the number of line offsets - * will be stored. - * @param offsets Pointer to a pointer which will be set to point to an array - * containing the configured offsets. The array will be allocated - * using malloc() and must be freed using free(). - * @return 0 on success, -1 on failure. + * @param offsets Array to store offsets. + * @param max_offsets Number of offsets that can be stored in the offsets array. + * @return Number of offsets stored in the offsets array. + * + * If max_offsets is lower than the number of lines actually requested (this + * value can be retrieved using ::gpiod_line_config_get_num_configured_offsets), + * then only up to max_lines offsets will be stored in offsets. */ -int gpiod_line_config_get_offsets(struct gpiod_line_config *config, - size_t *num_offsets, - unsigned int **offsets); +size_t +gpiod_line_config_get_configured_offsets(struct gpiod_line_config *config, + unsigned int *offsets, + size_t max_offsets); /** * @} @@ -880,16 +890,24 @@ void gpiod_line_request_release(struct gpiod_line_request *request); * @param request Line request object. * @return Number of requested lines. */ -size_t gpiod_line_request_get_num_lines(struct gpiod_line_request *request); +size_t +gpiod_line_request_get_num_requested_lines(struct gpiod_line_request *request); /** * @brief Get the offsets of the lines in the request. * @param request Line request object. - * @param offsets Array to store offsets. Must be sized to hold the number of - * lines returned by ::gpiod_line_request_get_num_lines. + * @param offsets Array to store offsets. + * @param max_offsets Number of offsets that can be stored in the offsets array. + * @return Number of offsets stored in the offsets array. + * + * If max_offsets is lower than the number of lines actually requested (this + * value can be retrieved using ::gpiod_line_request_get_num_requested_lines), + * then only up to max_lines offsets will be stored in offsets. */ -void gpiod_line_request_get_offsets(struct gpiod_line_request *request, - unsigned int *offsets); +size_t +gpiod_line_request_get_requested_offsets(struct gpiod_line_request *request, + unsigned int *offsets, + size_t max_offsets); /** * @brief Get the value of a single requested line. @@ -922,10 +940,10 @@ int gpiod_line_request_get_values_subset(struct gpiod_line_request *request, * @param request GPIO line request. * @param values Array in which the values will be stored. Must be sized to * hold the number of lines returned by - * ::gpiod_line_request_get_num_lines. + * ::gpiod_line_request_get_num_requested_lines. * Each value is associated with the line identified by the * corresponding entry in the offset array returned by - * ::gpiod_line_request_get_offsets. + * ::gpiod_line_request_get_requested_offsets. * @return 0 on success, -1 on failure. */ int gpiod_line_request_get_values(struct gpiod_line_request *request, @@ -963,10 +981,10 @@ int gpiod_line_request_set_values_subset(struct gpiod_line_request *request, * @param request GPIO line request. * @param values Array containing the values to set. Must be sized to * contain the number of lines returned by - * ::gpiod_line_request_get_num_lines. + * ::gpiod_line_request_get_num_requested_lines. * Each value is associated with the line identified by the * corresponding entry in the offset array returned by - * ::gpiod_line_request_get_offsets. + * ::gpiod_line_request_get_requested_offsets. */ int gpiod_line_request_set_values(struct gpiod_line_request *request, const enum gpiod_line_value *values); diff --git a/lib/line-config.c b/lib/line-config.c index bc10059..b00e5e6 100644 --- a/lib/line-config.c +++ b/lib/line-config.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "internal.h" @@ -152,36 +153,35 @@ gpiod_line_config_get_line_settings(struct gpiod_line_config *config, return NULL; } -GPIOD_API int gpiod_line_config_get_offsets(struct gpiod_line_config *config, - size_t *num_offsets, - unsigned int **offsets) +GPIOD_API size_t +gpiod_line_config_get_num_configured_offsets(struct gpiod_line_config *config) { - unsigned int *offs; - size_t i; + assert(config); + + return config->num_configs; +} + +GPIOD_API size_t +gpiod_line_config_get_configured_offsets(struct gpiod_line_config *config, + unsigned int *offsets, + size_t max_offsets) +{ + size_t num_offsets, i; assert(config); - if (!num_offsets || !offsets) { - errno = EINVAL; - return -1; - } + if (!offsets || !max_offsets) + return 0; - *num_offsets = config->num_configs; - *offsets = NULL; + num_offsets = MIN(config->num_configs, max_offsets); if (!config->num_configs) return 0; - offs = calloc(config->num_configs, sizeof(unsigned int)); - if (!offs) - return -1; - for (i = 0; i < config->num_configs; i++) - offs[i] = config->line_configs[i].offset; - - *offsets = offs; + offsets[i] = config->line_configs[i].offset; - return 0; + return num_offsets; } static void set_offsets(struct gpiod_line_config *config, diff --git a/lib/line-request.c b/lib/line-request.c index c9ad337..e536355 100644 --- a/lib/line-request.c +++ b/lib/line-request.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "internal.h" @@ -45,24 +46,30 @@ GPIOD_API void gpiod_line_request_release(struct gpiod_line_request *request) } GPIOD_API size_t -gpiod_line_request_get_num_lines(struct gpiod_line_request *request) +gpiod_line_request_get_num_requested_lines(struct gpiod_line_request *request) { assert(request); return request->num_lines; } -GPIOD_API void -gpiod_line_request_get_offsets(struct gpiod_line_request *request, - unsigned int *offsets) +GPIOD_API size_t +gpiod_line_request_get_requested_offsets(struct gpiod_line_request *request, + unsigned int *offsets, + size_t max_offsets) { + size_t num_offsets; + assert(request); - if (!offsets) - return; + if (!offsets || !max_offsets) + return 0; + + num_offsets = MIN(request->num_lines, max_offsets); + + memcpy(offsets, request->offsets, sizeof(*offsets) * num_offsets); - memcpy(offsets, request->offsets, - sizeof(*offsets) * request->num_lines); + return num_offsets; } GPIOD_API enum gpiod_line_value diff --git a/tests/tests-line-config.c b/tests/tests-line-config.c index 4b5b3bd..5afdf7b 100644 --- a/tests/tests-line-config.c +++ b/tests/tests-line-config.c @@ -190,10 +190,8 @@ GPIOD_TEST_CASE(get_offsets) { g_autoptr(struct_gpiod_line_settings) settings = NULL; g_autoptr(struct_gpiod_line_config) config = NULL; - g_autofree guint *config_offs = NULL; - guint offsets[8]; + guint offsets[8], offsets_in[4]; size_t num_offsets; - gint ret; settings = gpiod_test_create_line_settings_or_fail(); config = gpiod_test_create_line_config_or_fail(); @@ -211,39 +209,37 @@ GPIOD_TEST_CASE(get_offsets) gpiod_test_line_config_add_line_settings_or_fail(config, offsets, 2, settings); - ret = gpiod_line_config_get_offsets(config, &num_offsets, &config_offs); - g_assert_cmpint(ret, ==, 0); + num_offsets = gpiod_line_config_get_configured_offsets(config, + offsets_in, 4); g_assert_cmpuint(num_offsets, ==, 4); - g_assert_cmpuint(config_offs[0], ==, 2); - g_assert_cmpuint(config_offs[1], ==, 4); - g_assert_cmpuint(config_offs[2], ==, 6); - g_assert_cmpuint(config_offs[3], ==, 7); + g_assert_cmpuint(offsets_in[0], ==, 2); + g_assert_cmpuint(offsets_in[1], ==, 4); + g_assert_cmpuint(offsets_in[2], ==, 6); + g_assert_cmpuint(offsets_in[3], ==, 7); } GPIOD_TEST_CASE(get_0_offsets) { g_autoptr(struct_gpiod_line_config) config = NULL; - g_autofree guint *offsets = NULL; size_t num_offsets; - gint ret; + guint offsets[3]; config = gpiod_test_create_line_config_or_fail(); - ret = gpiod_line_config_get_offsets(config, &num_offsets, &offsets); - g_assert_cmpint(ret, ==, 0); + num_offsets = gpiod_line_config_get_configured_offsets(config, + offsets, 0); g_assert_cmpuint(num_offsets, ==, 0); - g_assert_null(offsets); } GPIOD_TEST_CASE(get_null_offsets) { g_autoptr(struct_gpiod_line_config) config = NULL; g_autofree guint *offsets = NULL; - gint ret; + size_t num_offsets; config = gpiod_test_create_line_config_or_fail(); - ret = gpiod_line_config_get_offsets(config, NULL, &offsets); - g_assert_cmpint(ret, ==, -1); - gpiod_test_expect_errno(EINVAL); + num_offsets = gpiod_line_config_get_configured_offsets(config, + NULL, 10); + g_assert_cmpuint(num_offsets, ==, 0); } diff --git a/tests/tests-line-request.c b/tests/tests-line-request.c index 2c2af01..fa02289 100644 --- a/tests/tests-line-request.c +++ b/tests/tests-line-request.c @@ -45,9 +45,10 @@ GPIOD_TEST_CASE(request_fails_with_duplicate_offsets) request = gpiod_chip_request_lines(chip, NULL, line_cfg); g_assert_nonnull(request); - num_requested_offsets = gpiod_line_request_get_num_lines(request); + num_requested_offsets = + gpiod_line_request_get_num_requested_lines(request); g_assert_cmpuint(num_requested_offsets, ==, 3); - gpiod_line_request_get_offsets(request, requested_offsets); + gpiod_line_request_get_requested_offsets(request, requested_offsets, 4); g_assert_cmpuint(requested_offsets[0], ==, 0); g_assert_cmpuint(requested_offsets[1], ==, 2); g_assert_cmpuint(requested_offsets[2], ==, 3); @@ -401,9 +402,10 @@ GPIOD_TEST_CASE(num_lines_and_offsets) request = gpiod_test_request_lines_or_fail(chip, NULL, line_cfg); - g_assert_cmpuint(gpiod_line_request_get_num_lines(request), ==, 8); + g_assert_cmpuint(gpiod_line_request_get_num_requested_lines(request), + ==, 8); gpiod_test_return_if_failed(); - gpiod_line_request_get_offsets(request, read_back); + gpiod_line_request_get_requested_offsets(request, read_back, 8); for (i = 0; i < 8; i++) g_assert_cmpuint(read_back[i], ==, offsets[i]); } From patchwork Fri Jan 13 21:51:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 644782 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 B04FAC677F1 for ; Fri, 13 Jan 2023 21:52:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229505AbjAMVwZ (ORCPT ); Fri, 13 Jan 2023 16:52:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230455AbjAMVwX (ORCPT ); Fri, 13 Jan 2023 16:52:23 -0500 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D10B889BD4 for ; Fri, 13 Jan 2023 13:52:22 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id m26-20020a05600c3b1a00b003d9811fcaafso18284289wms.5 for ; Fri, 13 Jan 2023 13:52:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fKu9LAcoRnaVXGMhpbfRSf4sOsGlFFiS1EsvYro8gCU=; b=NFX63P6lxhp+15PkMI8roN4WHyfypbukueL8oEZQN2ZZXc1uaa73m2TAz5p6b7uYwr tg0dPjwha7MNjf8pTsmH8RNwlnStvQVNdfqDHf8yQYWDdrOF9+xBRwAJisU3pPqrcRXm fJsj3Mae/oe8kgO2AoM5Gdd69AeZg/Zu6BrjWsDXTYhtNn4fwhAeL3jdbKYxUHZXUbR0 aAe2udGXYOMieEodLURkQ7w6JMewsxv7HI5BaOxd5YNc3lKns6UgjWPosSUMNOJF0CSt F10t7gjXKxZMHQ2WsxAoXksViYjTh0pnUlZ+SrKwODXhiEh5qh37rC0JVzIvmLaU7aZ0 z5ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fKu9LAcoRnaVXGMhpbfRSf4sOsGlFFiS1EsvYro8gCU=; b=XRMN87q2rycBPQY644rAb867VXXvhfKwINJf52ZWr3N5KSVq7ZdQojx5yVbOErmj98 0/4qyOfm6lrV6/rhUitaH1feNxrTjgewRC3sdzw1AGp5M4wPNoDhnVLpndhj7bfnfdiH AmBfkDHgP7exOs1QbIIY8l1olQUBEAlA3xD4sflRCpL7uv6Ryu59zknys0Cs7zmYsyj4 m51MgFVk5ldIeHed0fM43o7oEINUmseHH0GJ814/PVTI49FVjRmaC50x7tTFTiMEW81M hFL4Osxo/NhgoKUjBsQlj95GNUieBTylNMUe69qVDNJQo2xqTjSe1v9gx9xkhhm4k3gq f9mA== X-Gm-Message-State: AFqh2kroroG8oL+0re1hveCt1PHPAKrvt0rxf6yr/VXcpyoEd5KcmH7j rF+au3rMOJFZnHxDgxMU6j1KgA== X-Google-Smtp-Source: AMrXdXvBmufh+gmjWeqzs/wKYMyYMoEOSo7B5ZZ3QJYkpeCOPvBn1e2eAnclhaDPXNGSEIK/tWM2TA== X-Received: by 2002:a05:600c:4e0c:b0:3d9:eedd:e7f1 with SMTP id b12-20020a05600c4e0c00b003d9eedde7f1mr972459wmq.12.1673646741028; Fri, 13 Jan 2023 13:52:21 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:20 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 05/16] doc: update docs for libgpiod v2 Date: Fri, 13 Jan 2023 22:51:59 +0100 Message-Id: <20230113215210.616812-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Update docs in gpiod.h wherever they're outdated or make incorrect statements. While at it: fix formatting in some places. Signed-off-by: Bartosz Golaszewski --- include/gpiod.h | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/include/gpiod.h b/include/gpiod.h index dfc5334..8cede47 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -22,8 +22,9 @@ extern "C" { * This is the complete documentation of the public API made available to * users of libgpiod. * - *

The API is logically split into several parts such as: GPIO chip & line - * operators, GPIO events handling etc. + *

The API is logically split into several sections. For each opaque data + * class, there's a set of functions for manipulating it. Together they can be + * thought of as objects and their methods in OOP parlance. * *

General note on error handling: all functions exported by libgpiod that * can fail, set errno to one of the error values defined in errno.h upon @@ -34,10 +35,13 @@ extern "C" { * codes for every function as they propagate errors from the underlying libc * functions. * - *

In general libgpiod functions are not NULL-aware and it's expected that - * users pass valid pointers to objects as arguments. An exception to this rule - * are the functions that free/close/release resources - which work when passed - * a NULL-pointer as argument. Other exceptions are documented. + *

In general libgpiod functions are NULL-aware. For functions that are + * logically methods of data classes - ones that take a pointer to the object + * of that class as the first argument - passing a NULL pointer will result in + * the program aborting the execution. For non-methods, init functions and + * methods that take a pointer as any of the subsequent arguments, the handling + * of a NULL-pointer depends on the implementation and may range from gracefully + * handling it, ignoring it or returning an error. */ struct gpiod_chip; @@ -260,7 +264,8 @@ enum gpiod_line_direction { GPIOD_LINE_DIRECTION_AS_IS = 1, /**< Request the line(s), but don't change direction. */ GPIOD_LINE_DIRECTION_INPUT, - /**< Direction is input - for reading the value of an externally driven GPIO line. */ + /**< Direction is input - for reading the value of an externally driven + * GPIO line. */ GPIOD_LINE_DIRECTION_OUTPUT, /**< Direction is output - for driving the GPIO line. */ }; @@ -926,8 +931,8 @@ gpiod_line_request_get_value(struct gpiod_line_request *request, * @param offsets Array of offsets identifying the subset of requested lines * from which to read values. * @param values Array in which the values will be stored. Must be sized - * to hold \p num_values entries. Each value is associated with the - * line identified by the corresponding entry in \p offsets. + * to hold \p num_values entries. Each value is associated with + * the line identified by the corresponding entry in \p offsets. * @return 0 on success, -1 on failure. */ int gpiod_line_request_get_values_subset(struct gpiod_line_request *request, From patchwork Fri Jan 13 21:52:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 644781 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 2C864C6379F for ; Fri, 13 Jan 2023 21:52:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231137AbjAMVw2 (ORCPT ); Fri, 13 Jan 2023 16:52:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230482AbjAMVwZ (ORCPT ); Fri, 13 Jan 2023 16:52:25 -0500 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B935B89BE5 for ; Fri, 13 Jan 2023 13:52:23 -0800 (PST) Received: by mail-wm1-x331.google.com with SMTP id p1-20020a05600c1d8100b003d8c9b191e0so18297024wms.4 for ; Fri, 13 Jan 2023 13:52:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2VmQt1HMTwporVXhsXfzr+GKqhg/ppWUGA/M4/1vroo=; b=nCnJFNAs/S3L0pz1i0z8opxrfjl54XhmTcWTzZqR9veUl7c99/JAOvoGj+mfQ4WgwD tLc4uqA6hKyy3B7aF5XLDKagIEs4S1Z2rIM9ZmZ7qTtQQKe3tczGxMgf2OZVNX4DQhA8 AgCliFi9G5l+N9ftGKp6p5+y1aSvLOJuYJGz8FNmkJuPB3CicE6mAp2DGV1CmMChp9A0 3AU9R6DXA2Ns3Ps3AI4YJkrN/QagyTwO8GeZP1CKB5jDxWJZ+k7LQYU0ADbluiDtvVQO uU/SB14ig5+BwfF1A8pkfZpPsDA8+RUAIBVYuMrJlanZ4kSWQAzOsLHiRGJy5PP/5ssy wItw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2VmQt1HMTwporVXhsXfzr+GKqhg/ppWUGA/M4/1vroo=; b=PyrruWRMxj1vWc42w+t8ieD3JZMOKbZRXMX/yeFAO0aXaX32BwEP1TJEsXWghZ7Y7Q /YSbAnzanDpKL4lO+ADfF/nfUM+aXWx9x9lbq/j1OknEaV8UAWXUTHTa7ylahSd95odk 4k5Yg+S/Q5enzmmGzcx+zCu5DYUg0JhVlpR0zBI4Kon53hk9/4FLc+ru5IehRmM9CPp/ 4+yDhhMkBPDZJnyPNzDE8uzOkmfwLzLFTGsW3piDg72kDIsq/xJVHhyZUAIIbu25K3XW fZeRqEfuMzOoj1bBvzTMI5dGrV79kK52Psit4W0PUc26Nu3lZKvvAdDJ+Bum5Sfce5UF QHBw== X-Gm-Message-State: AFqh2kr6V21tzJuhrIWHlLPiw7HrOp4Rfs90dBFW8ClM9w1z5JsiQ5MW bDEzFz4Rsa32GhUKv04gG+ei8Q== X-Google-Smtp-Source: AMrXdXufm/GJigJAxHO8DOdFPAwH2GWIClpZBaLN9rs6py9IJdMT78AAyunGP7XtV4tFFalsOKF9CA== X-Received: by 2002:a05:600c:331d:b0:3d6:ecc4:6279 with SMTP id q29-20020a05600c331d00b003d6ecc46279mr948112wmp.27.1673646742239; Fri, 13 Jan 2023 13:52:22 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:21 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 06/16] bindings: cxx: prepend all C symbols with the scope resolution operator Date: Fri, 13 Jan 2023 22:52:00 +0100 Message-Id: <20230113215210.616812-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski We explicitly resolve all global C symbols from libgpiod to the top-level namespace. Fix it wherever its missing (mostly for C enum types). Signed-off-by: Bartosz Golaszewski --- bindings/cxx/line-request.cpp | 4 +-- bindings/cxx/line-settings.cpp | 52 ++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/bindings/cxx/line-request.cpp b/bindings/cxx/line-request.cpp index 438c0fa..b0723c3 100644 --- a/bindings/cxx/line-request.cpp +++ b/bindings/cxx/line-request.cpp @@ -118,7 +118,7 @@ GPIOD_CXX_API void line_request::get_values(const line::offsets& offsets, line:: int ret = ::gpiod_line_request_get_values_subset( this->_m_priv->request.get(), offsets.size(), this->_m_priv->offset_buf.data(), - reinterpret_cast(values.data())); + reinterpret_cast<::gpiod_line_value*>(values.data())); if (ret) throw_from_errno("unable to retrieve line values"); } @@ -161,7 +161,7 @@ GPIOD_CXX_API line_request& line_request::set_values(const line::offsets& offset int ret = ::gpiod_line_request_set_values_subset( this->_m_priv->request.get(), offsets.size(), this->_m_priv->offset_buf.data(), - reinterpret_cast(values.data())); + reinterpret_cast(values.data())); if (ret) throw_from_errno("unable to set line values"); diff --git a/bindings/cxx/line-settings.cpp b/bindings/cxx/line-settings.cpp index 7d3d6a5..5ded953 100644 --- a/bindings/cxx/line-settings.cpp +++ b/bindings/cxx/line-settings.cpp @@ -22,56 +22,60 @@ make_reverse_maping(const ::std::map& mapping) return ret; } -const ::std::map direction_mapping = { +const ::std::map direction_mapping = { { line::direction::AS_IS, GPIOD_LINE_DIRECTION_AS_IS }, { line::direction::INPUT, GPIOD_LINE_DIRECTION_INPUT }, { line::direction::OUTPUT, GPIOD_LINE_DIRECTION_OUTPUT }, }; -const ::std::map +const ::std::map<::gpiod_line_direction, line::direction> reverse_direction_mapping = make_reverse_maping(direction_mapping); -const ::std::map edge_mapping = { +const ::std::map edge_mapping = { { line::edge::NONE, GPIOD_LINE_EDGE_NONE }, { line::edge::FALLING, GPIOD_LINE_EDGE_FALLING }, { line::edge::RISING, GPIOD_LINE_EDGE_RISING }, { line::edge::BOTH, GPIOD_LINE_EDGE_BOTH }, }; -const ::std::map reverse_edge_mapping = make_reverse_maping(edge_mapping); +const ::std::map<::gpiod_line_edge, line::edge> +reverse_edge_mapping = make_reverse_maping(edge_mapping); -const ::std::map bias_mapping = { +const ::std::map bias_mapping = { { line::bias::AS_IS, GPIOD_LINE_BIAS_AS_IS }, { line::bias::DISABLED, GPIOD_LINE_BIAS_DISABLED }, { line::bias::PULL_UP, GPIOD_LINE_BIAS_PULL_UP }, { line::bias::PULL_DOWN, GPIOD_LINE_BIAS_PULL_DOWN }, }; -const ::std::map reverse_bias_mapping = make_reverse_maping(bias_mapping); +const ::std::map<::gpiod_line_bias, line::bias> +reverse_bias_mapping = make_reverse_maping(bias_mapping); -const ::std::map drive_mapping = { +const ::std::map drive_mapping = { { line::drive::PUSH_PULL, GPIOD_LINE_DRIVE_PUSH_PULL }, { line::drive::OPEN_DRAIN, GPIOD_LINE_DRIVE_OPEN_DRAIN }, { line::drive::OPEN_SOURCE, GPIOD_LINE_DRIVE_OPEN_SOURCE }, }; -const ::std::map reverse_drive_mapping = make_reverse_maping(drive_mapping); +const ::std::map<::gpiod_line_drive, line::drive> +reverse_drive_mapping = make_reverse_maping(drive_mapping); -const ::std::map clock_mapping = { +const ::std::map clock_mapping = { { line::clock::MONOTONIC, GPIOD_LINE_CLOCK_MONOTONIC }, { line::clock::REALTIME, GPIOD_LINE_CLOCK_REALTIME }, { line::clock::HTE, GPIOD_LINE_CLOCK_HTE }, }; -const ::std::map +const ::std::map<::gpiod_line_clock, line::clock> reverse_clock_mapping = make_reverse_maping(clock_mapping); -const ::std::map value_mapping = { +const ::std::map value_mapping = { { line::value::INACTIVE, GPIOD_LINE_VALUE_INACTIVE }, { line::value::ACTIVE, GPIOD_LINE_VALUE_ACTIVE }, }; -const ::std::map reverse_value_mapping = make_reverse_maping(value_mapping); +const ::std::map<::gpiod_line_value, line::value> +reverse_value_mapping = make_reverse_maping(value_mapping); line_settings_ptr make_line_settings() { @@ -165,7 +169,7 @@ GPIOD_CXX_API line_settings& line_settings::reset(void) noexcept GPIOD_CXX_API line_settings& line_settings::set_direction(line::direction direction) { - set_mapped_value(this->_m_priv->settings.get(), direction, direction_mapping); @@ -174,7 +178,7 @@ GPIOD_CXX_API line_settings& line_settings::set_direction(line::direction direct GPIOD_CXX_API line::direction line_settings::direction() const { - return get_mapped_value( this->_m_priv->settings.get(), reverse_direction_mapping); @@ -182,7 +186,7 @@ GPIOD_CXX_API line::direction line_settings::direction() const GPIOD_CXX_API line_settings& line_settings::set_edge_detection(line::edge edge) { - set_mapped_value(this->_m_priv->settings.get(), edge, edge_mapping); @@ -191,7 +195,7 @@ GPIOD_CXX_API line_settings& line_settings::set_edge_detection(line::edge edge) GPIOD_CXX_API line::edge line_settings::edge_detection() const { - return get_mapped_value( this->_m_priv->settings.get(), reverse_edge_mapping); @@ -199,7 +203,7 @@ GPIOD_CXX_API line::edge line_settings::edge_detection() const GPIOD_CXX_API line_settings& line_settings::set_bias(line::bias bias) { - set_mapped_value(this->_m_priv->settings.get(), bias, bias_mapping); @@ -208,14 +212,14 @@ GPIOD_CXX_API line_settings& line_settings::set_bias(line::bias bias) GPIOD_CXX_API line::bias line_settings::bias() const { - return get_mapped_value(this->_m_priv->settings.get(), reverse_bias_mapping); } GPIOD_CXX_API line_settings& line_settings::set_drive(line::drive drive) { - set_mapped_value(this->_m_priv->settings.get(), drive, drive_mapping); @@ -224,7 +228,7 @@ GPIOD_CXX_API line_settings& line_settings::set_drive(line::drive drive) GPIOD_CXX_API line::drive line_settings::drive() const { - return get_mapped_value(this->_m_priv->settings.get(), reverse_drive_mapping); } @@ -257,7 +261,7 @@ GPIOD_CXX_API ::std::chrono::microseconds line_settings::debounce_period() const GPIOD_CXX_API line_settings& line_settings::set_event_clock(line::clock event_clock) { - set_mapped_value(this->_m_priv->settings.get(), event_clock, clock_mapping); @@ -266,7 +270,7 @@ GPIOD_CXX_API line_settings& line_settings::set_event_clock(line::clock event_cl GPIOD_CXX_API line::clock line_settings::event_clock() const { - return get_mapped_value( this->_m_priv->settings.get(), reverse_clock_mapping); @@ -274,7 +278,7 @@ GPIOD_CXX_API line::clock line_settings::event_clock() const GPIOD_CXX_API line_settings& line_settings::set_output_value(line::value value) { - set_mapped_value(this->_m_priv->settings.get(), value, value_mapping); @@ -283,7 +287,7 @@ GPIOD_CXX_API line_settings& line_settings::set_output_value(line::value value) GPIOD_CXX_API line::value line_settings::output_value() const { - return get_mapped_value( this->_m_priv->settings.get(), reverse_value_mapping); From patchwork Fri Jan 13 21:52:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 642314 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 916AEC3DA78 for ; Fri, 13 Jan 2023 21:52:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230455AbjAMVw3 (ORCPT ); Fri, 13 Jan 2023 16:52:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230347AbjAMVw1 (ORCPT ); Fri, 13 Jan 2023 16:52:27 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C060C89BCB for ; Fri, 13 Jan 2023 13:52:24 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id f25-20020a1c6a19000000b003da221fbf48so1975439wmc.1 for ; Fri, 13 Jan 2023 13:52:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dFQ2H51r7PsBqopCZJpFuBJ7mXoJ8Zf6r1MD3c0aSxo=; b=pyvF9IbyKUIKukdj1z+pmNxJLTj3bQeA62xGhZafRHwYzs5Bo0gz0CbifDaReOoZv7 Rt/74WfSSr0C2N5fhB9JaoBGrWveGJQyScq3ohUTUmakaM96aUpGXkiHKvDdTQInsjtW MpEg3ISROU4fn85EJuY+ALzYhxV8ySH+IL+3dk9R6hzSeKGcojAMBsiNF+37c+vmcwkU X0+oHJCJuSENcwiKDuUMOmfDH3odZHIoQXh7pogR/Hb6X32yKtqhVn581JTJSQNfGTMJ ZBPpYbPTfnzXtp235X/pr5oTi6olN/cwvrDw9KD9dTKNVYiHjhiCwPNfz69TlnR5wmQc J+fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dFQ2H51r7PsBqopCZJpFuBJ7mXoJ8Zf6r1MD3c0aSxo=; b=p1U83oAucKe0Y4nx2/cwQfsRY46Nr0qvJKdPR21J9qNulaDF1jHeAkC6Wpw/foO9pB mO4N+jmRAOC1d+Di+Omhm9UsakavBwPgz9lT/U2iyXPEK6iZTCoyHoju++cH/c1+a0cE W7CjRNd2SMhyOGZLE1hxHyKf6elSZ2welOiYF9a5NtElU5zm2Ftpob6SCz2zsUchVL74 EzSvuYPIEIf3V0tmSD+fL+1q4QZpv1rPeEbdV7SH7QWQhLXF33yqSTKai+J6VD3BbDhs 4dzFFLsZXI/0H+Vn9X/+wCCfw+9Q77RczRb7/aMF4lBP0VEl3lwoK8tBjX7rMGPcGpoc 3xWA== X-Gm-Message-State: AFqh2kpttRBOaL3WEbnJTUsulHRHePuGnIl9cnrnAQNRo27r3/1drA1y ppCFCry4qJKkeMWsnEC9hQVjYw== X-Google-Smtp-Source: AMrXdXu179e3pLr/bqzSdi35dzps/D35lV1kHSWutktUzvucPu5JgT7+QITBZ0ecTSL1CMUlEu55TA== X-Received: by 2002:a05:600c:1c9d:b0:3d1:f3eb:c718 with SMTP id k29-20020a05600c1c9d00b003d1f3ebc718mr71028956wms.19.1673646743239; Fri, 13 Jan 2023 13:52:23 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:22 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 07/16] bindings: cxx: allow to copy line_settings Date: Fri, 13 Jan 2023 22:52:01 +0100 Message-Id: <20230113215210.616812-8-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Implement the copy operator for line_settings. We have a copy() method for line settings in C API while in C++ it's useful to copy line_settings returned in an std::map from line_config.get_line_settings(). Signed-off-by: Bartosz Golaszewski --- bindings/cxx/gpiodcxx/line-settings.hpp | 13 ++++++- bindings/cxx/internal.hpp | 2 +- bindings/cxx/line-settings.cpp | 28 ++++++++++++++ bindings/cxx/tests/tests-line-settings.cpp | 43 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/bindings/cxx/gpiodcxx/line-settings.hpp b/bindings/cxx/gpiodcxx/line-settings.hpp index c1477b1..c18dc52 100644 --- a/bindings/cxx/gpiodcxx/line-settings.hpp +++ b/bindings/cxx/gpiodcxx/line-settings.hpp @@ -38,7 +38,11 @@ public: */ line_settings(); - line_settings(const line_settings& other) = delete; + /** + * @brief Copy constructor. + * @param other Object to copy. + */ + line_settings(const line_settings& other); /** * @brief Move constructor. @@ -48,7 +52,12 @@ public: ~line_settings(); - line_settings& operator=(const line_settings& other) = delete; + /** + * @brief Copy assignment operator. + * @param other Object to copy. + * @return Reference to self. + */ + line_settings& operator=(const line_settings& other); /** * @brief Move assignment operator. diff --git a/bindings/cxx/internal.hpp b/bindings/cxx/internal.hpp index d27aa22..6aceac1 100644 --- a/bindings/cxx/internal.hpp +++ b/bindings/cxx/internal.hpp @@ -120,7 +120,7 @@ struct info_event::impl struct line_settings::impl { impl(); - impl(const impl& other) = delete; + impl(const impl& other); impl(impl&& other) = delete; impl& operator=(const impl& other) = delete; impl& operator=(impl&& other) = delete; diff --git a/bindings/cxx/line-settings.cpp b/bindings/cxx/line-settings.cpp index 5ded953..32f21a3 100644 --- a/bindings/cxx/line-settings.cpp +++ b/bindings/cxx/line-settings.cpp @@ -86,6 +86,15 @@ line_settings_ptr make_line_settings() return settings; } +line_settings_ptr copy_line_settings(const line_settings_ptr& ptr) +{ + line_settings_ptr settings(::gpiod_line_settings_copy(ptr.get())); + if (!settings) + throw_from_errno("Unable to copy the line settings object"); + + return settings; +} + template value_type map_setting(const key_type& key, const ::std::map& mapping) { @@ -136,12 +145,24 @@ line_settings::impl::impl() } +line_settings::impl::impl(const impl& other) + : settings(copy_line_settings(other.settings)) +{ + +} + GPIOD_CXX_API line_settings::line_settings() : _m_priv(new impl) { } +GPIOD_CXX_API line_settings::line_settings(const line_settings& other) + : _m_priv(new impl(*other._m_priv)) +{ + +} + GPIOD_CXX_API line_settings::line_settings(line_settings&& other) noexcept : _m_priv(::std::move(other._m_priv)) { @@ -153,6 +174,13 @@ GPIOD_CXX_API line_settings::~line_settings() } +GPIOD_CXX_API line_settings& line_settings::operator=(const line_settings& other) +{ + this->_m_priv.reset(new impl(*other._m_priv)); + + return *this; +} + GPIOD_CXX_API line_settings& line_settings::operator=(line_settings&& other) { this->_m_priv = ::std::move(other._m_priv); diff --git a/bindings/cxx/tests/tests-line-settings.cpp b/bindings/cxx/tests/tests-line-settings.cpp index a3f4bc5..dc821bb 100644 --- a/bindings/cxx/tests/tests-line-settings.cpp +++ b/bindings/cxx/tests/tests-line-settings.cpp @@ -124,6 +124,49 @@ TEST_CASE("line_settings mutators work", "[line-settings]") } } +TEST_CASE("line_settings can be moved and copied", "[line-settings]") +{ + ::gpiod::line_settings settings; + + settings + .set_direction(direction::INPUT) + .set_edge_detection(edge::BOTH); + + SECTION("copy constructor works") + { + auto copy(settings); + settings.set_direction(direction::OUTPUT); + settings.set_edge_detection(edge::NONE); + REQUIRE(copy.direction() == direction::INPUT); + REQUIRE(copy.edge_detection() == edge::BOTH); + } + + SECTION("assignment operator works") + { + ::gpiod::line_settings copy; + copy = settings; + settings.set_direction(direction::OUTPUT); + settings.set_edge_detection(edge::NONE); + REQUIRE(copy.direction() == direction::INPUT); + REQUIRE(copy.edge_detection() == edge::BOTH); + } + + SECTION("move constructor works") + { + auto copy(::std::move(settings)); + REQUIRE(copy.direction() == direction::INPUT); + REQUIRE(copy.edge_detection() == edge::BOTH); + } + + SECTION("move assignment operator works") + { + ::gpiod::line_settings copy; + copy = ::std::move(settings); + REQUIRE(copy.direction() == direction::INPUT); + REQUIRE(copy.edge_detection() == edge::BOTH); + } +} + TEST_CASE("line_settings stream insertion operator works", "[line-settings]") { ::gpiod::line_settings settings; From patchwork Fri Jan 13 21:52:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 644780 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 306C8C677F1 for ; Fri, 13 Jan 2023 21:52:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231144AbjAMVwa (ORCPT ); Fri, 13 Jan 2023 16:52:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230411AbjAMVw1 (ORCPT ); Fri, 13 Jan 2023 16:52:27 -0500 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFDB289BCF for ; Fri, 13 Jan 2023 13:52:25 -0800 (PST) Received: by mail-wm1-x335.google.com with SMTP id q8so4305742wmo.5 for ; Fri, 13 Jan 2023 13:52:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zVWShqrtG6nhs2mzsOMgfRA5OJ9/LyNVBuy1z6RwGsU=; b=AiSzFfJtCmbBbWd224QTTW/Rna5dg66LsbdpNjGoaihHGGr57d+X/vL65OSnpib8cf ONObSiQweuS0+tyRKLObafgcSzkvzm5PsF4BaTTjjjVNZ0LaNJg4BkheLYiXeEgf+gNg vpVtvZHhbHhJtKNeeslyupTZfGAl+W4HIcPOG28+AXvqbDlgNQdJO1eOQe39TMj3VgY0 tdl/IpvM9hEJCo2lgVsKbIO67ATrzI3buLm5JputMpVqC6JSrMESuLjc7sf5YvKuUYB3 RMdX+fHxrMyrWEdaRIEbrQCAxywelfMkFHpZ4vO7rC4zbaAeZzCy+sURH/BqcVb8j7eg yq4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zVWShqrtG6nhs2mzsOMgfRA5OJ9/LyNVBuy1z6RwGsU=; b=tGuUYNAZWeS2D0pMWQBcE38V5RfmRN95al6zIWtGzi+UobIFjtVGrBFL0OWmhqNlKL UlVyM5HJ2g2adxEARLT6lFVSEEguBwB2aOk0zY81MtMQiJA1yz/aeP+a+l8FeJEhWHRu jpzEHHK/xYRqIQvu6LQNORJvSqLXfo96Gb0B6VvEFJ2f3LxKsEXrG+5rNHKSO4pJe/pg l4QzTlw5AGNKYLB+Y1sMnS+8rsD70bZco6r+uCIMJAg7f6hW437cf6xm+4o9SBfFrptB eQgI7HCgT2Hjnt72I7zELonSNcXSeeOlOaqDckgFwr+TsWP3icAriGE391jaPgFDeLun tV2g== X-Gm-Message-State: AFqh2kpHFxEO8nSYPBenph9F411ICA9GXnqGGBs9uEZwPRqXiLqwmgUj Xsk5hKnIw9r3Fpx4UDHymbYWl1nKKai0s9GS X-Google-Smtp-Source: AMrXdXu5c0q0IAmzmEJ4LgM29sSe9+E5D/y5GcK5z4+8fQ5cFAvrX65awN/a/7t/qm0H9F2DShr14g== X-Received: by 2002:a05:600c:b4d:b0:3d3:5d69:7aa5 with SMTP id k13-20020a05600c0b4d00b003d35d697aa5mr57972051wmr.25.1673646744326; Fri, 13 Jan 2023 13:52:24 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:23 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 08/16] tests: fix the line config reset test case Date: Fri, 13 Jan 2023 22:52:02 +0100 Message-Id: <20230113215210.616812-9-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski We're using testing wrong variables in the reset_config test case. We should be checking the retrieved0 settings which are read back from the line config object. Signed-off-by: Bartosz Golaszewski --- tests/tests-line-config.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests-line-config.c b/tests/tests-line-config.c index 5afdf7b..ef85c3a 100644 --- a/tests/tests-line-config.c +++ b/tests/tests-line-config.c @@ -175,9 +175,9 @@ GPIOD_TEST_CASE(reset_config) g_assert_nonnull(retrieved0); gpiod_test_return_if_failed(); - g_assert_cmpint(gpiod_line_settings_get_direction(settings), ==, + g_assert_cmpint(gpiod_line_settings_get_direction(retrieved0), ==, GPIOD_LINE_DIRECTION_INPUT); - g_assert_cmpint(gpiod_line_settings_get_bias(settings), ==, + g_assert_cmpint(gpiod_line_settings_get_bias(retrieved0), ==, GPIOD_LINE_BIAS_PULL_DOWN); gpiod_line_config_reset(config); From patchwork Fri Jan 13 21:52:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 642313 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 3D0BFC6379F for ; Fri, 13 Jan 2023 21:52:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230411AbjAMVwa (ORCPT ); Fri, 13 Jan 2023 16:52:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231182AbjAMVw2 (ORCPT ); Fri, 13 Jan 2023 16:52:28 -0500 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCDEB58F94 for ; Fri, 13 Jan 2023 13:52:26 -0800 (PST) Received: by mail-wr1-x42b.google.com with SMTP id bk16so22237150wrb.11 for ; Fri, 13 Jan 2023 13:52:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oY8/6WY3HsxwuOetEGEoZ4t9eIVGCq5j+RCrFrdVSxg=; b=PIGG269TuzftPJwEIxNCKmo9NDKwMvMTUCBRDUdoPtiKxsbWpukXJGZZtNcTvtMdYj aodR9DXFNYYR/FQlWgb7xzlSGQKxqvqgDGfeC3VZvIPl8tdxqn9ZPXjHCx3Q0/Ihh4Jl RLjk+T4TQx4gEsu5qosptF8I0ifbtrxv65tVlkBiYL6XcU9XVu79LN/BvgJllJUiJ9+P 3oMlj0VH+kfS8hNWVl4dbUX21ikhRzO3E8TVt0LgAXguDk/+SxR1/VFWXKK0ECXEa9Gl MmUqMB9xX4gn4Hu2MiUR1T8AYFLyREAAbvpyxbBLFjEfbsLzFCbvNtQANm1QBKz/9Xf/ qKxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oY8/6WY3HsxwuOetEGEoZ4t9eIVGCq5j+RCrFrdVSxg=; b=Ms9AcjjYvhd/w8M4VhkEtLMDqKAh1IokE+Pco0RpoGfQXNNVyTaWm+AONsOvxysp6X 7EZt97dA+SQDwJrTDyAflohuY2iCBvxTSoWVqN/Ee71A2vmhuIf2k/KtPcsoCpE97+MI +d972+EAjimniy7+XlC8N2mXBv+MJPb9PBajHmrRtaJKWKVdtoyr91jGlGutcx+9C8LW GIy8QpIMGawrnGxESd9BOOrY9Ox0NJ9HKSAjC5XUSB/bCG8rLRlbRRYL61ak1aDOZdJo 33zQUPf7jsTRUxNEW9Py8nCLuEoPhznlqiVnaVWkbBKZOixFhlQK2JXyyUT1JWxmnVWa zeXQ== X-Gm-Message-State: AFqh2krfz9hqeJ14Rnwu31CWDcNHU1X1jfGSC0I3ZhvbIetVrxeocHQ3 shwHgx9IIBV9S3pUYVa7r+P9hA== X-Google-Smtp-Source: AMrXdXv7L83FyrWoZSq1/0vJMeUWgHrvHBhaIMKwCBmZ/cnk1cFSbwgxXbASiaE/JVr4MBAvqP1xXA== X-Received: by 2002:a5d:4d85:0:b0:2bd:d8f1:2efe with SMTP id b5-20020a5d4d85000000b002bdd8f12efemr864901wru.34.1673646745333; Fri, 13 Jan 2023 13:52:25 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:24 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 09/16] tests: add a helper for reading back line settings from line config Date: Fri, 13 Jan 2023 22:52:03 +0100 Message-Id: <20230113215210.616812-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Add a helper for getting line settings from line config that allows to shrink the test code a bit. Signed-off-by: Bartosz Golaszewski --- tests/gpiod-test-helpers.h | 10 ++++++++++ tests/tests-line-config.c | 13 ++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/gpiod-test-helpers.h b/tests/gpiod-test-helpers.h index b40b820..fb3fd7d 100644 --- a/tests/gpiod-test-helpers.h +++ b/tests/gpiod-test-helpers.h @@ -126,6 +126,16 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_edge_event_buffer, gpiod_test_return_if_failed(); \ } while (0) +#define gpiod_test_line_config_get_line_settings_or_fail(_line_cfg, _offset) \ + ({ \ + struct gpiod_line_settings *_settings = \ + gpiod_line_config_get_line_settings(_line_cfg, \ + _offset); \ + g_assert_nonnull(_settings); \ + gpiod_test_return_if_failed(); \ + _settings; \ + }) + #define gpiod_test_create_request_config_or_fail() \ ({ \ struct gpiod_request_config *_config = \ diff --git a/tests/tests-line-config.c b/tests/tests-line-config.c index ef85c3a..7f5e4b1 100644 --- a/tests/tests-line-config.c +++ b/tests/tests-line-config.c @@ -46,9 +46,7 @@ GPIOD_TEST_CASE(get_line_settings) gpiod_test_line_config_add_line_settings_or_fail(config, offsets, 4, settings); - retrieved = gpiod_line_config_get_line_settings(config, 2); - g_assert_nonnull(retrieved); - gpiod_test_return_if_failed(); + retrieved = gpiod_test_line_config_get_line_settings_or_fail(config, 2); g_assert_cmpint(gpiod_line_settings_get_direction(settings), ==, GPIOD_LINE_DIRECTION_INPUT); @@ -146,9 +144,7 @@ GPIOD_TEST_CASE(null_settings) gpiod_test_line_config_add_line_settings_or_fail(config, offsets, 4, NULL); - settings = gpiod_line_config_get_line_settings(config, 2); - g_assert_nonnull(settings); - gpiod_test_return_if_failed(); + settings = gpiod_test_line_config_get_line_settings_or_fail(config, 2); g_assert_cmpint(gpiod_line_settings_get_drive(settings), ==, GPIOD_LINE_DIRECTION_AS_IS); @@ -171,9 +167,8 @@ GPIOD_TEST_CASE(reset_config) gpiod_test_line_config_add_line_settings_or_fail(config, offsets, 4, settings); - retrieved0 = gpiod_line_config_get_line_settings(config, 2); - g_assert_nonnull(retrieved0); - gpiod_test_return_if_failed(); + retrieved0 = gpiod_test_line_config_get_line_settings_or_fail(config, + 2); g_assert_cmpint(gpiod_line_settings_get_direction(retrieved0), ==, GPIOD_LINE_DIRECTION_INPUT); From patchwork Fri Jan 13 21:52:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 642312 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 B0311C678D6 for ; Fri, 13 Jan 2023 21:52:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230463AbjAMVwc (ORCPT ); Fri, 13 Jan 2023 16:52:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230482AbjAMVw3 (ORCPT ); Fri, 13 Jan 2023 16:52:29 -0500 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1137989BF2 for ; Fri, 13 Jan 2023 13:52:27 -0800 (PST) Received: by mail-wm1-x32c.google.com with SMTP id m3so16121833wmq.0 for ; Fri, 13 Jan 2023 13:52:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M0MI4lw5cWdwPbslfrt2Bdc0oat3npjZrM1m0HACgPI=; b=MfnDgV40sOhf8kxOivxKpTWoEGYsoTJrGKdLGB9a7Rd4z2ELHXmZ/C+d/0WFwCeBt6 drDfY8GIF9JsqzUqm0H/HR6uy48zKUFooHZUqDJ8DfEqJa27uGs8w/RpjLsq0SCFYNOa +egwcaO+Z1r6gT90Q2B1Q80773ggik1Yy/AZRmJqvfpPd+Lvan0PnhNpPPQ7+En4LBlp fMBLIbCHCjtmdrrqTmwm7NNPAPtHBZ4vGAs6bVMZKZXzzh8pH3FmdntUDw++tGRABClh ZjaDn4TMSfb5AB7KADlOzdSPzgZoPelqPKw17jb5w5QCeSRhsMxTq2IzkoCF/FYEYpA6 unZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M0MI4lw5cWdwPbslfrt2Bdc0oat3npjZrM1m0HACgPI=; b=tDrfhL8y2oDVfnim7FzmJmqAyim8iTK/ETPKdbbD7NZRNEz60FFO7JulsDpDyOx+dF fLxHolrxERv57v/udv0jacLdstaNJhKx7zbZ8W5Gnp4v0Pg5xxIGC0WUHcGjguzwZF7v 2MrMxf6EZs7A8rDKxUjHwmSM0heDv+gqtm7yoNegNzFxYc5Uhs+wyLXat07rJhQT3nK8 iofiusyMwpKwE6RHnX2KDcXYgY+0HZ76ipny87+oluJBv504DiRAiyGvROSzvP8pRyj8 +Tz9TbbyyrT+iMwiwn1bw9kAg2za1PdOATr4Ag355LKNrlhzvhzTwQXyqocPB2tKmNEa bRMA== X-Gm-Message-State: AFqh2kr4S9Rnz3qsvKiBHAVIdmLR4SAAeavxRJeYtWf0Q6q0e4WkPZto PCovGd7KaZZidN7kE3cgpJxhRw== X-Google-Smtp-Source: AMrXdXumZqhm1kku2PmguPO53Ew+lsN/3fCAGpbcV+RyAo3MicZhyX8uEef+AQeRclE9QfsKiX7+kQ== X-Received: by 2002:a05:600c:1603:b0:3d1:c895:930c with SMTP id m3-20020a05600c160300b003d1c895930cmr59446218wmn.35.1673646746414; Fri, 13 Jan 2023 13:52:26 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:25 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 10/16] core: provide gpiod_line_config_set_output_values() Date: Fri, 13 Jan 2023 22:52:04 +0100 Message-Id: <20230113215210.616812-11-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Currently if user wants to use the same settings for a set of requested lines with the exception of the output value - they need to go through hoops by updating the line settings object and adding it one by one to the line config. Provide a helper function that allows to set a global list of output values that override the settings. For details on the interface: see documentation in this commit. Signed-off-by: Bartosz Golaszewski --- include/gpiod.h | 27 +++++++++++++++ lib/line-config.c | 60 +++++++++++++++++++++++++++++++--- tests/gpiod-test-helpers.h | 10 ++++++ tests/tests-line-config.c | 67 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+), 4 deletions(-) diff --git a/include/gpiod.h b/include/gpiod.h index 8cede47..c552135 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -785,6 +785,33 @@ struct gpiod_line_settings * gpiod_line_config_get_line_settings(struct gpiod_line_config *config, unsigned int offset); +/** + * @brief Set output values for a number of lines. + * @param config Line config object. + * @param values Buffer containing the output values. + * @param num_values Number of values in the buffer. + * @return 0 on success, -1 on error. + * + * This is a helper that allows users to set multiple (potentially different) + * output values at once while using the same line settings object. Instead of + * modifying the output value in the settings object and calling + * ::gpiod_line_config_add_line_settings multiple times, we can specify the + * settings, add them for a set of offsets and then call this function to + * set the output values. + * + * Values set by this function override whatever values were specified in the + * regular line settings. + * + * The order of the output values in the array corresponds with the order in + * which offsets were added by ::gpiod_line_config_add_line_settings. For + * example calling add_settings([1, 3]) and add_settings([2, 4]) and then + * calling this function with the following logicall values : [0, 1, 0, 1] + * will result in the following offset->value mapping: 1->0, 2->0, 3->1, 4->1. + */ +int gpiod_line_config_set_output_values(struct gpiod_line_config *config, + const enum gpiod_line_value *values, + size_t num_values); + /** * @brief Get the number of configured line offsets. * @param config Line config object. diff --git a/lib/line-config.c b/lib/line-config.c index b00e5e6..901eb02 100644 --- a/lib/line-config.c +++ b/lib/line-config.c @@ -25,6 +25,8 @@ struct per_line_config { struct gpiod_line_config { struct per_line_config line_configs[LINES_MAX]; size_t num_configs; + enum gpiod_line_value output_values[LINES_MAX]; + size_t num_output_values; struct settings_node *sref_list; }; @@ -136,23 +138,60 @@ GPIOD_API struct gpiod_line_settings * gpiod_line_config_get_line_settings(struct gpiod_line_config *config, unsigned int offset) { + struct gpiod_line_settings *settings; struct per_line_config *per_line; size_t i; + int ret; assert(config); for (i = 0; i < config->num_configs; i++) { per_line = &config->line_configs[i]; - if (per_line->offset == offset) - return gpiod_line_settings_copy( + if (per_line->offset == offset) { + settings = gpiod_line_settings_copy( per_line->node->settings); + if (!settings) + return NULL; + + /* + * If a global output value was set for this line - use + * it and override the one stored in settings. + */ + if (config->num_output_values > i) { + ret = gpiod_line_settings_set_output_value( + settings, + config->output_values[i]); + if (ret) { + gpiod_line_settings_free(settings); + return NULL; + } + } + + return settings; + } } errno = ENOENT; return NULL; } +GPIOD_API int +gpiod_line_config_set_output_values(struct gpiod_line_config *config, + const enum gpiod_line_value *values, + size_t num_values) +{ + if (num_values > LINES_MAX) { + errno = EINVAL; + return -1; + } + + memcpy(config->output_values, values, num_values * sizeof(*values)); + config->num_output_values = num_values; + + return 0; +} + GPIOD_API size_t gpiod_line_config_get_num_configured_offsets(struct gpiod_line_config *config) { @@ -209,6 +248,13 @@ static bool has_at_least_one_output_direction(struct gpiod_line_config *config) return false; } +static void set_output_value(uint64_t *vals, size_t bit, + enum gpiod_line_value value) +{ + gpiod_line_mask_assign_bit(vals, bit, + value == GPIOD_LINE_VALUE_ACTIVE ? 1 : 0); +} + static void set_kernel_output_values(uint64_t *mask, uint64_t *vals, struct gpiod_line_config *config) { @@ -230,8 +276,14 @@ static void set_kernel_output_values(uint64_t *mask, uint64_t *vals, gpiod_line_mask_set_bit(mask, i); value = gpiod_line_settings_get_output_value( per_line->node->settings); - gpiod_line_mask_assign_bit( - vals, i, value == GPIOD_LINE_VALUE_ACTIVE ? 1 : 0); + set_output_value(vals, i, value); + } + + /* "Global" output values override the ones from per-line settings. */ + for (i = 0; i < config->num_output_values; i++) { + gpiod_line_mask_set_bit(mask, i); + value = config->output_values[i]; + set_output_value(vals, i, value); } } diff --git a/tests/gpiod-test-helpers.h b/tests/gpiod-test-helpers.h index fb3fd7d..760949e 100644 --- a/tests/gpiod-test-helpers.h +++ b/tests/gpiod-test-helpers.h @@ -136,6 +136,16 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_edge_event_buffer, _settings; \ }) +#define gpiod_test_line_config_set_output_values_or_fail(_line_cfg, _values, \ + _num_values) \ + do { \ + gint _ret = gpiod_line_config_set_output_values(_line_cfg, \ + _values, \ + _num_values); \ + g_assert_cmpint(_ret, ==, 0); \ + gpiod_test_return_if_failed(); \ + } while (0) + #define gpiod_test_create_request_config_or_fail() \ ({ \ struct gpiod_request_config *_config = \ diff --git a/tests/tests-line-config.c b/tests/tests-line-config.c index 7f5e4b1..78a4632 100644 --- a/tests/tests-line-config.c +++ b/tests/tests-line-config.c @@ -238,3 +238,70 @@ GPIOD_TEST_CASE(get_null_offsets) NULL, 10); g_assert_cmpuint(num_offsets, ==, 0); } + +GPIOD_TEST_CASE(set_global_output_values) +{ + static const guint offsets[] = { 0, 1, 2, 3 }; + static const enum gpiod_line_value values[] = { + GPIOD_LINE_VALUE_ACTIVE, + GPIOD_LINE_VALUE_INACTIVE, + GPIOD_LINE_VALUE_ACTIVE, + GPIOD_LINE_VALUE_INACTIVE, + }; + + g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); + g_autoptr(struct_gpiod_line_settings) settings = NULL; + g_autoptr(struct_gpiod_line_config) config = NULL; + g_autoptr(struct_gpiod_line_request) request = NULL; + g_autoptr(struct_gpiod_chip) chip = NULL; + + chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); + settings = gpiod_test_create_line_settings_or_fail(); + config = gpiod_test_create_line_config_or_fail(); + + gpiod_line_settings_set_direction(settings, + GPIOD_LINE_DIRECTION_OUTPUT); + gpiod_test_line_config_add_line_settings_or_fail(config, offsets, 4, + settings); + gpiod_test_line_config_set_output_values_or_fail(config, values, 4); + + request = gpiod_test_request_lines_or_fail(chip, NULL, config); + + g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, + GPIOD_LINE_VALUE_ACTIVE); + g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, + GPIOD_LINE_VALUE_INACTIVE); + g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==, + GPIOD_LINE_VALUE_ACTIVE); + g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, + GPIOD_LINE_VALUE_INACTIVE); +} + +GPIOD_TEST_CASE(read_back_global_output_values) +{ + static const guint offsets[] = { 0, 1, 2, 3 }; + static const enum gpiod_line_value values[] = { + GPIOD_LINE_VALUE_ACTIVE, + GPIOD_LINE_VALUE_INACTIVE, + GPIOD_LINE_VALUE_ACTIVE, + GPIOD_LINE_VALUE_INACTIVE, + }; + + g_autoptr(struct_gpiod_line_settings) settings = NULL; + g_autoptr(struct_gpiod_line_settings) retrieved = NULL; + g_autoptr(struct_gpiod_line_config) config = NULL; + + settings = gpiod_test_create_line_settings_or_fail(); + config = gpiod_test_create_line_config_or_fail(); + + gpiod_line_settings_set_direction(settings, + GPIOD_LINE_DIRECTION_OUTPUT); + gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_ACTIVE); + gpiod_test_line_config_add_line_settings_or_fail(config, offsets, 4, + settings); + gpiod_test_line_config_set_output_values_or_fail(config, values, 4); + + retrieved = gpiod_test_line_config_get_line_settings_or_fail(config, 1); + g_assert_cmpint(gpiod_line_settings_get_output_value(retrieved), ==, + GPIOD_LINE_VALUE_INACTIVE); +} From patchwork Fri Jan 13 21:52:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 644779 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 F2BC5C3DA78 for ; Fri, 13 Jan 2023 21:52:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231169AbjAMVwb (ORCPT ); Fri, 13 Jan 2023 16:52:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230428AbjAMVw2 (ORCPT ); Fri, 13 Jan 2023 16:52:28 -0500 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 112D089BF0 for ; Fri, 13 Jan 2023 13:52:27 -0800 (PST) Received: by mail-wm1-x331.google.com with SMTP id p1-20020a05600c1d8100b003d8c9b191e0so18297110wms.4 for ; Fri, 13 Jan 2023 13:52:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QqYkAVDDJHqckoBGfhi4xsYYLbZddtA7wLw1qqPJGNc=; b=z4cWGqMJ5Z59D9JfHVDzxPPHVnRxz9KaYgGNk5H8pWDcxxSUtzDM4pJHPS+egPHWLJ QKiER924sqlxMvv+XYKJsWwkM4anB4IBhGCkKvPRkskwAM4tJG8saqGndy5ZX+hIiqGe 6PjxoFJ4ATyIwVylrWhE1uRJ4rtEsUdqR9aZAxJ8aDxY1WBfhBRmpHBGrtcNzNmKycv6 2tAyRFW0HEsFj7ZXzoUs3JZEahQh9jQ5Jk98wOIEdQOFqG4/flkU3mAofcOTA6EUmxvH Jto/rZnCkCyJqjCt5R2aXNZhC18oRa/yszmyaYlJWaRjFPNN18pyaSfK6GGh5yeExWC0 hu0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QqYkAVDDJHqckoBGfhi4xsYYLbZddtA7wLw1qqPJGNc=; b=QCTtsk9xArUOiu8bByhXrZGw/1vZvsey4Aw2F+NdCqckY6YQKli0vKUfcfmBScWdVj UBGhyp1gdFtPISfXQPFieUTPR7QNwzlzL+QnkidogNDMIM7vBVdabx4B8wF95YLBu2zO tSFtExlp5LKSKEYfEKzSKVMoCU4tiMvzgsnpZI/Wd78qmiLuzFU3oBzMhz22mWQWrrra RTJouJbH/I9hVM8sMeH7fMelkyPYj4kJz+5GSH7PGJIBwMHyfd1R7WEmXn6CL7Dbyszi 3hpjSodMrLXp9imb2HXPdlDcFVTbq4JakX0XG2btOiJJzzWWKw2qQa58I+zjXhgQK2C9 eQkQ== X-Gm-Message-State: AFqh2ko6cuAdaNKtBE5eYk9MtJO8Ln+fiU4U07k/80lhtLQD0vWofv6p bZt+l/4Tq13oNZHXyfeYzDNCSA== X-Google-Smtp-Source: AMrXdXuRggr3qfGgM8FysCW7OkcVpeQsH5Vp0MHfUzp33pxq6SkXqDARhLreOp37FaGRfo38XKzFuQ== X-Received: by 2002:a05:600c:1f12:b0:3cf:8155:2adc with SMTP id bd18-20020a05600c1f1200b003cf81552adcmr61056616wmb.33.1673646747533; Fri, 13 Jan 2023 13:52:27 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:27 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 11/16] gpioset: use gpiod_line_config_set_output_values() Date: Fri, 13 Jan 2023 22:52:05 +0100 Message-Id: <20230113215210.616812-12-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Use the new line config function to shrink the gpioset code and drop one for loop. Signed-off-by: Bartosz Golaszewski --- tools/gpioset.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tools/gpioset.c b/tools/gpioset.c index a871a16..1429d65 100644 --- a/tools/gpioset.c +++ b/tools/gpioset.c @@ -870,9 +870,9 @@ int main(int argc, char **argv) struct gpiod_line_config *line_cfg; struct line_resolver *resolver; enum gpiod_line_value *values; - int i, j, num_lines, ret; struct gpiod_chip *chip; unsigned int *offsets; + int i, num_lines, ret; struct config cfg; char **lines; @@ -933,15 +933,16 @@ int main(int argc, char **argv) values); gpiod_line_config_reset(line_cfg); - for (j = 0; j < num_lines; j++) { - gpiod_line_settings_set_output_value(settings, - values[j]); - - ret = gpiod_line_config_add_line_settings( - line_cfg, &offsets[j], 1, settings); - if (ret) - die_perror("unable to add line settings"); - } + + ret = gpiod_line_config_add_line_settings(line_cfg, offsets, + num_lines, settings); + if (ret) + die_perror("unable to add line settings"); + + ret = gpiod_line_config_set_output_values(line_cfg, + values, num_lines); + if (ret) + die_perror("unable to set output values"); chip = gpiod_chip_open(resolver->chips[i].path); if (!chip) From patchwork Fri Jan 13 21:52:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 644778 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 049A3C3DA78 for ; Fri, 13 Jan 2023 21:52:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230439AbjAMVwe (ORCPT ); Fri, 13 Jan 2023 16:52:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231168AbjAMVwa (ORCPT ); Fri, 13 Jan 2023 16:52:30 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28D6D58F94 for ; Fri, 13 Jan 2023 13:52:29 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id f25-20020a1c6a19000000b003da221fbf48so1975551wmc.1 for ; Fri, 13 Jan 2023 13:52:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WgI3Fk/2gyiGGUplRcEoLYgVoGEyCL5gPGqpn2TaikQ=; b=q68+/sXwusDmmwW92XsrdVJJb3YgBYJh9FrYWP73MrgOLeNV5JHO6v6L1bKnuhpHph ZfngntSf6s9m7O7xLW8plNN+b4WSUcC5X7kWvSt2Qh3o5qLhA6+3r7qBkB+cTqb3M+r/ YgZ/R+Yho/iKPwqYcA+xBx4orrkew1xPrhfVAJfoDsCUXz02WtRa77BqvaVR3w5lUI0n EXtA6asv1L1WlzMtKwlxbrGyDnyVogMCDAq46sJzsltYxrebveuMjfTaGfqNAmhw4+wL mGMcjrOnOiFeFFEJP2RMym++/4FoEy7vzlxRE0YZXaphopKifQ5Nnl3OxJDJ3J4HDyP4 b0/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WgI3Fk/2gyiGGUplRcEoLYgVoGEyCL5gPGqpn2TaikQ=; b=2AF3bX7JE3DkSD3pyYkQnKnwLmkUA2AYAXPasQ52C+mQe2B8sM2nQPSsNMxxRzjh7s m+9ChrHdARZRrjcQ/YKxgB3BdUSW4A1YcKUonO2NyjcNa2JKdFU2b4N7My2kMKq8LAyh C0pGPxRDqNDRXreL4St/T2xvwIDLpAYbnlZ+St2RdcngyNSjw/5JSsVNAZVu9V8vUOXM zFVfQZpGMJWmIQknfqmorX5EmbsAjHmIEuD5u6LDqnCITAkUHmZ+Mt4eySft7mzKKs6s OzSQQN0A8vWrr3o167eRFRFZNgB33UGw//ejK2D3ut7mntkl3cQd2WZy/1u8TVQJxv1p rfTA== X-Gm-Message-State: AFqh2kqcB19WM1xyEbRmC4JhTZ38gy4F7GAcFScozSJ+EVsPAvEP2rdz AthHxAk0tkRrUnRoZGgidwvpdg== X-Google-Smtp-Source: AMrXdXsrbV3Man8fBOAovMdWUZLVkUvAv9NN4RVUFfAKQYbnCfriCgkRGX7QLzpKvjRoxhuyE+1feA== X-Received: by 2002:a05:600c:2046:b0:3da:1357:4ca2 with SMTP id p6-20020a05600c204600b003da13574ca2mr6685809wmg.11.1673646748705; Fri, 13 Jan 2023 13:52:28 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:28 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 12/16] bindings: cxx: add line_config.set_output_values() Date: Fri, 13 Jan 2023 22:52:06 +0100 Message-Id: <20230113215210.616812-13-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Extend line_config to expose a new method - set_output_values() - which wraps the new C function for setting multiple output values at once. Signed-off-by: Bartosz Golaszewski --- bindings/cxx/gpiodcxx/line-config.hpp | 7 ++++ bindings/cxx/internal.hpp | 1 + bindings/cxx/line-config.cpp | 15 +++++++ bindings/cxx/line-settings.cpp | 5 +++ bindings/cxx/tests/tests-line-config.cpp | 51 ++++++++++++++++++++++++ 5 files changed, 79 insertions(+) diff --git a/bindings/cxx/gpiodcxx/line-config.hpp b/bindings/cxx/gpiodcxx/line-config.hpp index a917913..b76fdff 100644 --- a/bindings/cxx/gpiodcxx/line-config.hpp +++ b/bindings/cxx/gpiodcxx/line-config.hpp @@ -76,6 +76,13 @@ public: */ line_config& add_line_settings(const line::offsets& offsets, const line_settings& settings); + /** + * @brief Set output values for a number of lines. + * @param values Buffer containing the output values. + * @return Reference to self. + */ + line_config& set_output_values(const line::values& values); + /** * @brief Get a mapping of offsets to line settings stored by this * object. diff --git a/bindings/cxx/internal.hpp b/bindings/cxx/internal.hpp index 6aceac1..8322e12 100644 --- a/bindings/cxx/internal.hpp +++ b/bindings/cxx/internal.hpp @@ -31,6 +31,7 @@ map_enum_c_to_cxx(c_enum_type value, const ::std::map struct deleter { diff --git a/bindings/cxx/line-config.cpp b/bindings/cxx/line-config.cpp index 3ec99f0..233ba33 100644 --- a/bindings/cxx/line-config.cpp +++ b/bindings/cxx/line-config.cpp @@ -100,6 +100,21 @@ GPIOD_CXX_API line_config& line_config::add_line_settings(const line::offsets& o return *this; } +GPIOD_CXX_API line_config& line_config::set_output_values(const line::values& values) +{ + ::std::vector<::gpiod_line_value> mapped_values(values.size()); + + for (unsigned int i = 0; i < values.size(); i++) + mapped_values[i] = map_output_value(values[i]); + + auto ret = ::gpiod_line_config_set_output_values(this->_m_priv->config.get(), + mapped_values.data(), mapped_values.size()); + if (ret) + throw_from_errno("unable to set output values"); + + return *this; +} + GPIOD_CXX_API ::std::map line_config::get_line_settings() const { ::std::size_t num_offsets = ::gpiod_line_config_get_num_configured_offsets( diff --git a/bindings/cxx/line-settings.cpp b/bindings/cxx/line-settings.cpp index 32f21a3..2159062 100644 --- a/bindings/cxx/line-settings.cpp +++ b/bindings/cxx/line-settings.cpp @@ -139,6 +139,11 @@ cxx_enum_type get_mapped_value(::gpiod_line_settings* settings, } /* namespace */ +::gpiod_line_value map_output_value(line::value value) +{ + return do_map_value(value, value_mapping); +} + line_settings::impl::impl() : settings(make_line_settings()) { diff --git a/bindings/cxx/tests/tests-line-config.cpp b/bindings/cxx/tests/tests-line-config.cpp index 5fa0f94..5e439a1 100644 --- a/bindings/cxx/tests/tests-line-config.cpp +++ b/bindings/cxx/tests/tests-line-config.cpp @@ -4,12 +4,17 @@ #include #include +#include "gpiosim.hpp" #include "helpers.hpp" +using ::gpiosim::make_sim; using namespace ::std::chrono_literals; using direction = ::gpiod::line::direction; using drive = ::gpiod::line::drive; using edge = ::gpiod::line::edge; +using simval = ::gpiosim::chip::value; +using value = ::gpiod::line::value; +using values = ::gpiod::line::values; namespace { @@ -72,6 +77,52 @@ TEST_CASE("line_config can be reset", "[line-config]") REQUIRE(cfg.get_line_settings().size() == 0); } +TEST_CASE("output values can be set globally", "[line-config]") +{ + const values vals = { value::ACTIVE, value::INACTIVE, value::ACTIVE, value::INACTIVE }; + + auto sim = make_sim() + .set_num_lines(4) + .build(); + + ::gpiod::line_config cfg; + + SECTION("request with globally set output values") + { + cfg + .add_line_settings( + {0, 1, 2, 3}, + ::gpiod::line_settings().set_direction(direction::OUTPUT) + ) + .set_output_values(vals); + + auto request = ::gpiod::chip(sim.dev_path()) + .prepare_request() + .set_line_config(cfg) + .do_request(); + + REQUIRE(sim.get_value(0) == simval::ACTIVE); + REQUIRE(sim.get_value(1) == simval::INACTIVE); + REQUIRE(sim.get_value(2) == simval::ACTIVE); + REQUIRE(sim.get_value(3) == simval::INACTIVE); + } + + SECTION("read back global output values") + { + cfg + .add_line_settings( + {0, 1, 2, 3}, + ::gpiod::line_settings() + .set_direction(direction::OUTPUT) + .set_output_value(value::ACTIVE) + ) + .set_output_values(vals); + + auto settings = cfg.get_line_settings()[1]; + REQUIRE(settings.output_value() == value::INACTIVE); + } +} + TEST_CASE("line_config stream insertion operator works", "[line-config]") { ::gpiod::line_config cfg; From patchwork Fri Jan 13 21:52:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 642311 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 1301EC678D6 for ; Fri, 13 Jan 2023 21:52:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229996AbjAMVwe (ORCPT ); Fri, 13 Jan 2023 16:52:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230428AbjAMVwc (ORCPT ); Fri, 13 Jan 2023 16:52:32 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35BF389BDF for ; Fri, 13 Jan 2023 13:52:31 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id c4-20020a1c3504000000b003d9e2f72093so14832218wma.1 for ; Fri, 13 Jan 2023 13:52:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oB6udgfE8PDU+ZgHX4U7uUPgQ1WYJTiJGyqYxNBagAA=; b=0TU1EhhqLNN6zb54L7MnIXu4k7oSXEYzdcK9oUQgOuG3HzY1L9nYAhiAScRVsZC+mI wnTuNoZ5eLZ3BHL0hUTRRNmAUmxQZF2+4tn9ocmCaQc6WZokgYmrTK10G04/yGc+Ffwb yQRksuN7CcOC7tHvgJEIYDFnOcwxHb2bvBggxgC1uwQJ5VdKCqtRAm5426ZKvaa8FD2h FMVShraCRpCX+pGYyF82Er7ACFnGz6tBN/4efjnRJ5j2bibogzT6yHQHHPi9tC5Fgqep t25tkErunAMTwZaRqSDVGpux5hXeV+Rp8p/HGHgcBxCexig72XgZJi+ad6+NXEXyvXmt a5Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oB6udgfE8PDU+ZgHX4U7uUPgQ1WYJTiJGyqYxNBagAA=; b=t5LV/GQrVZf6uJPaDJ/fzFfkrZoFbUK7LXyPfiV3V9qFqyDveyT3eVE3ncpa8Jt2Tg UytNWPyA/CfmrDINAMCFyaVWLdtUvOc0ytmsJNIh45ZpJ/+LJkHeTFaoFstDT+LSkf9o Xmx6aY9qmVicfOBPayJpf6f4nZCPW/DF0cDqU7O7ekP/1UO0UT46cS/gL0/TD5IQrGpp 3SnLkkLhx3mOsWkJN6PwzefGbNWup3bIUjIhAceTWDNXaeTCL2stXKEJ69/zOcuIeaBM Bd1VPcP/dBViJdnSSrf+Gz6c4EdDUp4mooxza8u/BfvZ/9D9Q/bo3V3MQp6I03vgIE+n TmBQ== X-Gm-Message-State: AFqh2kpR9f8VExiumBGXIlncrtyvWnjhxLPUNicx5oQ01YSPIia3DQyb pDe99cjVlTUI4Z/IsLfoKJ7DETbk0vmVLBll X-Google-Smtp-Source: AMrXdXvSh3hs2LrHzS09PaAFGI8imKapj9UueL9j4iKd4yfWK8i26gFJK+yHwd0aR5+lxObneSXHUA== X-Received: by 2002:a05:600c:5012:b0:3d3:5a4a:9103 with SMTP id n18-20020a05600c501200b003d35a4a9103mr58557642wmr.31.1673646749765; Fri, 13 Jan 2023 13:52:29 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:29 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 13/16] bindings: python: provide line_config.set_output_values() Date: Fri, 13 Jan 2023 22:52:07 +0100 Message-Id: <20230113215210.616812-14-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Add a new argument to Chip.request_lines() that allows the user to pass a list of output values for configured lines instead of using several LineSettings objects between which the only difference is the output value. Signed-off-by: Bartosz Golaszewski --- bindings/python/gpiod/chip.py | 6 ++ bindings/python/gpiod/ext/line-config.c | 64 +++++++++++++++++++++ bindings/python/tests/tests_line_request.py | 14 +++++ 3 files changed, 84 insertions(+) diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py index ad2eddd..4f5f9b4 100644 --- a/bindings/python/gpiod/chip.py +++ b/bindings/python/gpiod/chip.py @@ -6,10 +6,12 @@ from .chip_info import ChipInfo from .exception import ChipClosedError from .info_event import InfoEvent from .internal import poll_fd +from .line import Value from .line_info import LineInfo from .line_settings import LineSettings, _line_settings_to_ext from .line_request import LineRequest from collections import Counter +from collections.abc import Iterable from datetime import timedelta from errno import ENOENT from select import select @@ -221,6 +223,7 @@ class Chip: config: dict[tuple[Union[int, str]], Optional[LineSettings]], consumer: Optional[str] = None, event_buffer_size: Optional[int] = None, + output_values: Optional[Iterable[Value]] = None, ) -> LineRequest: """ Request a set of lines for exclusive usage. @@ -279,6 +282,9 @@ class Chip: offsets, _line_settings_to_ext(settings or LineSettings()) ) + if output_values: + line_cfg.set_output_values(output_values) + req_internal = self._chip.request_lines(line_cfg, consumer, event_buffer_size) request = LineRequest(req_internal) diff --git a/bindings/python/gpiod/ext/line-config.c b/bindings/python/gpiod/ext/line-config.c index 173ca6b..0bba112 100644 --- a/bindings/python/gpiod/ext/line-config.c +++ b/bindings/python/gpiod/ext/line-config.c @@ -89,12 +89,76 @@ line_config_add_line_settings(line_config_object *self, PyObject *args) Py_RETURN_NONE; } +static PyObject * +line_config_set_output_values(line_config_object *self, PyObject *args) +{ + PyObject *values, *iter, *next, *val_stripped; + enum gpiod_line_value *valbuf; + Py_ssize_t num_values, pos; + int ret; + + values = PyTuple_GetItem(args, 0); + if (!values) + return NULL; + + num_values = PyObject_Size(values); + if (num_values < 0) + return NULL; + + valbuf = PyMem_Calloc(num_values, sizeof(*valbuf)); + if (!valbuf) + return PyErr_NoMemory(); + + iter = PyObject_GetIter(values); + if (!iter) { + PyMem_Free(valbuf); + return NULL; + } + + for (pos = 0;; pos++) { + next = PyIter_Next(iter); + if (!next) { + Py_DECREF(iter); + break; + } + + val_stripped = PyObject_GetAttrString(next, "value"); + Py_DECREF(next); + if (!val_stripped) { + PyMem_Free(valbuf); + Py_DECREF(iter); + return NULL; + } + + valbuf[pos] = PyLong_AsLong(val_stripped); + Py_DECREF(val_stripped); + if (PyErr_Occurred()) { + PyMem_Free(valbuf); + Py_DECREF(iter); + return NULL; + } + } + + ret = gpiod_line_config_set_output_values(self->cfg, + valbuf, num_values); + PyMem_Free(valbuf); + if (ret) + return Py_gpiod_SetErrFromErrno(); + + Py_RETURN_NONE; +} + static PyMethodDef line_config_methods[] = { { .ml_name = "add_line_settings", .ml_meth = (PyCFunction)line_config_add_line_settings, .ml_flags = METH_VARARGS, }, + { + .ml_name = "set_output_values", + .ml_meth = (PyCFunction)line_config_set_output_values, + .ml_flags = METH_VARARGS, + }, { } }; diff --git a/bindings/python/tests/tests_line_request.py b/bindings/python/tests/tests_line_request.py index c0ac768..1dc2c71 100644 --- a/bindings/python/tests/tests_line_request.py +++ b/bindings/python/tests/tests_line_request.py @@ -402,6 +402,20 @@ class LineRequestConsumerString(TestCase): self.assertEqual(info.consumer, "?") +class LineRequestSetOutputValues(TestCase): + def test_request_with_globally_set_output_values(self): + sim = gpiosim.Chip(num_lines=4) + with gpiod.request_lines( + sim.dev_path, + config={(0, 1, 2, 3): gpiod.LineSettings(direction=Direction.OUTPUT)}, + output_values=(Value.ACTIVE, Value.INACTIVE, Value.ACTIVE, Value.INACTIVE), + ) as request: + self.assertEqual(sim.get_value(0), SimVal.ACTIVE) + self.assertEqual(sim.get_value(1), SimVal.INACTIVE) + self.assertEqual(sim.get_value(2), SimVal.ACTIVE) + self.assertEqual(sim.get_value(3), SimVal.INACTIVE) + + class ReconfigureRequestedLines(TestCase): def setUp(self): self.sim = gpiosim.Chip(num_lines=8, line_names={3: "foo", 4: "bar", 6: "baz"}) From patchwork Fri Jan 13 21:52:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 644777 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 9B7B2C6379F for ; Fri, 13 Jan 2023 21:52:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230417AbjAMVwf (ORCPT ); Fri, 13 Jan 2023 16:52:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230482AbjAMVwe (ORCPT ); Fri, 13 Jan 2023 16:52:34 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42E5089BE3 for ; Fri, 13 Jan 2023 13:52:31 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id f25-20020a1c6a19000000b003da221fbf48so1975590wmc.1 for ; Fri, 13 Jan 2023 13:52:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6XLMg9CuAafuTq2nWOWQf1Mzz+IC/toxbU6U7qxH2IQ=; b=rG7Wt9Xv2i7ZTb0ayunyZcEDFLKjyFyRjUxtKoNHKgmN46jnHL8cvU2bRAWxhK64kX nlx0cWQG/+Y6cCtDZtuAPV+im2m1YE6BFtrmr5d69D3zwfb9wK1SMBh92Q/X+dddG3wX 3kgfJ8vBpE4vvBDgGNMcYEzo2+KDTMOSx0v/IT8rySLUFjpMb0mQAq2/WBIQsFj9Gh2U kf/GY0dkicyI8fIsk/aFPNoEhYTMI1y7sbdT1PAGDvhmhJoZd3skBHJn0HOpMjGT0sf8 +ZAsaRDpkdoayYObNasx/m6abM3J/a455EROS7jsIy0qXj7JldEL/goi7Xy3NrG2KjYi d2dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6XLMg9CuAafuTq2nWOWQf1Mzz+IC/toxbU6U7qxH2IQ=; b=o5G843L0eOsqycL2Ig4MzEyoT6OPcSy8D0u6xBSfzIT14zP7ZD491pvP+yy7hZpFnc P6ZymCDJFQD/EZW51DQf5RJPyApoT0+3RaJdk7Bpt/yJPYGd+sD1cUpvRz38zQc790bQ PoqwtfOJRHeMqb1truvLKX1WEhW20BjRl+imR8+7/OJLRx528e2vYklOk9i3OkdZAACx 1KBPY9DStYBRZULALIO5O7Q+qnffDEINCqgrlmC+M6jLsUxfYWw2UoDkwyAatOvuYE05 01Xi/t68mo6qLI+skjM5uPxPGvFNiTu5Zm8m3NvgtIoVU3fT3R93RR+RugPktWAqxKh3 rB5g== X-Gm-Message-State: AFqh2kp6E08HCNcscOMMNZ5YXx/r4b51llIfx568n4muJ1FnyeojuYqE zl2KiUaGP6e16Myge1hrzTD7nw== X-Google-Smtp-Source: AMrXdXs9oith08H2bjTnEEAo96igNiPeWnnxjnLmwJFAErf97ujJm2XfT929A9LFnV8QdCqZUOVldA== X-Received: by 2002:a05:600c:1d10:b0:3d9:ef75:ea6e with SMTP id l16-20020a05600c1d1000b003d9ef75ea6emr916892wms.38.1673646750771; Fri, 13 Jan 2023 13:52:30 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:30 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 14/16] bindings: rust: make request_config optional in Chip.request_lines() Date: Fri, 13 Jan 2023 22:52:08 +0100 Message-Id: <20230113215210.616812-15-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Request config is not necessary to request lines. In C API we accept a NULL pointer, in C++ it's not necessary to assign a request_config to the request builder, in Python the consumer and event buffer size arguments are optional. Let's make rust bindings consistent and not require the request config to be always present. Convert the argument in request_lines to Option and update the rest of the code. Signed-off-by: Bartosz Golaszewski --- bindings/rust/libgpiod/examples/gpio_events.rs | 2 +- .../libgpiod/examples/gpio_threaded_info_events.rs | 2 +- bindings/rust/libgpiod/examples/gpioget.rs | 2 +- bindings/rust/libgpiod/examples/gpiomon.rs | 2 +- bindings/rust/libgpiod/examples/gpioset.rs | 2 +- bindings/rust/libgpiod/src/chip.rs | 10 ++++++++-- bindings/rust/libgpiod/tests/common/config.rs | 2 +- bindings/rust/libgpiod/tests/info_event.rs | 2 +- 8 files changed, 15 insertions(+), 9 deletions(-) diff --git a/bindings/rust/libgpiod/examples/gpio_events.rs b/bindings/rust/libgpiod/examples/gpio_events.rs index 04267d9..cbdf1b5 100644 --- a/bindings/rust/libgpiod/examples/gpio_events.rs +++ b/bindings/rust/libgpiod/examples/gpio_events.rs @@ -42,7 +42,7 @@ fn main() -> Result<()> { let rconfig = request::Config::new()?; let mut buffer = request::Buffer::new(1)?; - let request = chip.request_lines(&rconfig, &lconfig)?; + let request = chip.request_lines(Some(&rconfig), &lconfig)?; loop { match request.wait_edge_events(None) { diff --git a/bindings/rust/libgpiod/examples/gpio_threaded_info_events.rs b/bindings/rust/libgpiod/examples/gpio_threaded_info_events.rs index e17f0f0..367b2f6 100644 --- a/bindings/rust/libgpiod/examples/gpio_threaded_info_events.rs +++ b/bindings/rust/libgpiod/examples/gpio_threaded_info_events.rs @@ -40,7 +40,7 @@ fn request_reconfigure_line( let request = chip .lock() .unwrap() - .request_lines(&rconfig, &lconfig) + .request_lines(Some(&rconfig), &lconfig) .unwrap(); // Signal the parent to continue diff --git a/bindings/rust/libgpiod/examples/gpioget.rs b/bindings/rust/libgpiod/examples/gpioget.rs index 6e60833..74baf30 100644 --- a/bindings/rust/libgpiod/examples/gpioget.rs +++ b/bindings/rust/libgpiod/examples/gpioget.rs @@ -37,7 +37,7 @@ fn main() -> Result<()> { let rconfig = request::Config::new()?; rconfig.set_consumer(&args[0])?; - let request = chip.request_lines(&rconfig, &lconfig)?; + let request = chip.request_lines(Some(&rconfig), &lconfig)?; let map = request.values()?; println!("{:?}", map); diff --git a/bindings/rust/libgpiod/examples/gpiomon.rs b/bindings/rust/libgpiod/examples/gpiomon.rs index f17a81f..a09ddfc 100644 --- a/bindings/rust/libgpiod/examples/gpiomon.rs +++ b/bindings/rust/libgpiod/examples/gpiomon.rs @@ -41,7 +41,7 @@ fn main() -> Result<()> { let rconfig = request::Config::new()?; let mut buffer = request::Buffer::new(1)?; - let request = chip.request_lines(&rconfig, &lconfig)?; + let request = chip.request_lines(Some(&rconfig), &lconfig)?; loop { match request.wait_edge_events(None) { diff --git a/bindings/rust/libgpiod/examples/gpioset.rs b/bindings/rust/libgpiod/examples/gpioset.rs index 875a3ad..6247996 100644 --- a/bindings/rust/libgpiod/examples/gpioset.rs +++ b/bindings/rust/libgpiod/examples/gpioset.rs @@ -54,7 +54,7 @@ fn main() -> Result<()> { let rconfig = request::Config::new()?; rconfig.set_consumer(&args[0])?; - chip.request_lines(&rconfig, &lconfig)?; + chip.request_lines(Some(&rconfig), &lconfig)?; // Wait for keypress, let user verify line status. stdin().read_exact(&mut [0u8]).unwrap(); diff --git a/bindings/rust/libgpiod/src/chip.rs b/bindings/rust/libgpiod/src/chip.rs index 91b4c94..9c3c2b4 100644 --- a/bindings/rust/libgpiod/src/chip.rs +++ b/bindings/rust/libgpiod/src/chip.rs @@ -11,6 +11,7 @@ use std::cmp::Ordering; use std::ffi::{CStr, CString}; use std::os::{raw::c_char, unix::prelude::AsRawFd}; use std::path::Path; +use std::ptr; use std::str; use std::sync::Arc; use std::time::Duration; @@ -195,13 +196,18 @@ impl Chip { /// Request a set of lines for exclusive usage. pub fn request_lines( &self, - rconfig: &request::Config, + rconfig: Option<&request::Config>, lconfig: &line::Config, ) -> Result { + let req_cfg = match rconfig { + Some(cfg) => cfg.config, + _ => ptr::null(), + } as *mut gpiod::gpiod_request_config; + // SAFETY: The `gpiod_line_request` returned by libgpiod is guaranteed to live as long // as the `struct Request`. let request = unsafe { - gpiod::gpiod_chip_request_lines(self.ichip.chip, rconfig.config, lconfig.config) + gpiod::gpiod_chip_request_lines(self.ichip.chip, req_cfg, lconfig.config) }; if request.is_null() { diff --git a/bindings/rust/libgpiod/tests/common/config.rs b/bindings/rust/libgpiod/tests/common/config.rs index 842a70a..b838b66 100644 --- a/bindings/rust/libgpiod/tests/common/config.rs +++ b/bindings/rust/libgpiod/tests/common/config.rs @@ -106,7 +106,7 @@ impl TestConfig { pub(crate) fn request_lines(&mut self) -> Result<()> { let chip = Chip::open(&self.sim.lock().unwrap().dev_path())?; - self.request = Some(chip.request_lines(&self.rconfig, &self.lconfig)?); + self.request = Some(chip.request_lines(Some(&self.rconfig), &self.lconfig)?); self.chip = Some(chip); Ok(()) diff --git a/bindings/rust/libgpiod/tests/info_event.rs b/bindings/rust/libgpiod/tests/info_event.rs index bfa0058..6bf7a0f 100644 --- a/bindings/rust/libgpiod/tests/info_event.rs +++ b/bindings/rust/libgpiod/tests/info_event.rs @@ -32,7 +32,7 @@ mod info_event { let request = chip .lock() .unwrap() - .request_lines(&rconfig, &lconfig1) + .request_lines(Some(&rconfig), &lconfig1) .unwrap(); // Signal the parent to continue From patchwork Fri Jan 13 21:52:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 642310 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 D398EC6379F for ; Fri, 13 Jan 2023 21:52:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230303AbjAMVwi (ORCPT ); Fri, 13 Jan 2023 16:52:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230527AbjAMVwg (ORCPT ); Fri, 13 Jan 2023 16:52:36 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDD5E892EC for ; Fri, 13 Jan 2023 13:52:33 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id g19-20020a05600c4ed300b003d9eb1dbc0aso15322156wmq.3 for ; Fri, 13 Jan 2023 13:52:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cUI9FCk3CQi9FNc7n1gvqx5GWMZktjxafQ9YjEyhhxA=; b=4hwGwHPmN0O8tmWdFlFz8yg+tnnfQu+lQ5+FOnzLeBIYvPF10P0M9LCW7k6R3k1Kxp KCcARmyVTMPx/D1IarovsbxJYHi5MVWv+T8JIX/n+iMVphC/ijoSWVmRFZ2yr7aLnT9s 9XmBMm3J8ETdbiUdTE+aEFr46nga8Gd6FDNtdbzvJWtzWyAShP4HLwLa09KflWRpFmgl 6nX0csKdSADUHjv3zB9phArI7uqizsUEIon/YY4wlFvlHVsxr6bmh4mukL7E7D3fyuTY iq8882sVyJjDN90dQFrG8/ZCRFEnLnUSQZ8gOL5Gq1OU/Gr5Nj3Ym5eCUc6wSL6F1ucn m1Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cUI9FCk3CQi9FNc7n1gvqx5GWMZktjxafQ9YjEyhhxA=; b=dxwlpZHlq0B2hKLBM0LrdmVTP0ZlTwmWKz/fnS9j72kQywCN6I4GszXDlJQLMGAqbN vOJspqw/+5ZulctgpWUVkOXxhUW1VxPWrM3yMpX/r328lwMzbM+7o1CUCTqT1kMZCmsX qhi0lUwwEhzrQ9WkN5QFLrZ8yE4IZWnYAQRG7ZgXjcfpiHxQdvZ5jklcO6hCb5gJG9Fa ri0TVdtS/4uwG9R8DYH98Q0QyyY+Zi46vOb1XLmkPsrbtcVolcCBcD3x8N1BLG3rDerU RzUBaXvbl/Z2wOf8U/ViniJ5CbZWzCKATwbYSGUiPT52kXluepDWBT8t5MdsTvKdhU0a Ztcw== X-Gm-Message-State: AFqh2koxcOS7wQsOT5JWi35zbm3plkqPy/D8HYuy8lxaHTBqhxCRrzCr geI1rFQYpvKUweTUWJ+3v0CLkw== X-Google-Smtp-Source: AMrXdXvSWgOSQIwROnGmSBlm0SNSMb7WxTzwWgEx/vHelUtFDM01IwD0lgPLRvAHU7mgJZ/lD2lOZQ== X-Received: by 2002:a05:600c:3d8b:b0:3d9:ee3c:7730 with SMTP id bi11-20020a05600c3d8b00b003d9ee3c7730mr18191302wmb.34.1673646752025; Fri, 13 Jan 2023 13:52:32 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:31 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 15/16] bindings: rust: make mutators return &mut self Date: Fri, 13 Jan 2023 22:52:09 +0100 Message-Id: <20230113215210.616812-16-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski In C++ bindings we can chain the mutators as they all return a reference to the object they modify. It's a common practice to allow that in Rust too so make all mutators that don't already do it return a mutable reference to self. It's also logically incorrect to make mutators borrow an immutable reference to self. Even if that builds - as we're fiddling with C pointers - it could change in the future. It's fine for getters but setters should all use mutable references. Signed-off-by: Bartosz Golaszewski --- .../rust/libgpiod/examples/gpio_events.rs | 2 +- .../examples/gpio_threaded_info_events.rs | 6 +- bindings/rust/libgpiod/examples/gpioget.rs | 4 +- bindings/rust/libgpiod/examples/gpiomon.rs | 2 +- bindings/rust/libgpiod/examples/gpioset.rs | 4 +- bindings/rust/libgpiod/src/line_config.rs | 4 +- bindings/rust/libgpiod/src/line_request.rs | 16 +-- bindings/rust/libgpiod/src/request_config.rs | 8 +- bindings/rust/libgpiod/tests/common/config.rs | 8 +- bindings/rust/libgpiod/tests/info_event.rs | 6 +- bindings/rust/libgpiod/tests/line_config.rs | 2 +- bindings/rust/libgpiod/tests/line_request.rs | 99 ++++++++----------- .../rust/libgpiod/tests/request_config.rs | 2 +- 13 files changed, 74 insertions(+), 89 deletions(-) diff --git a/bindings/rust/libgpiod/examples/gpio_events.rs b/bindings/rust/libgpiod/examples/gpio_events.rs index cbdf1b5..b26c60b 100644 --- a/bindings/rust/libgpiod/examples/gpio_events.rs +++ b/bindings/rust/libgpiod/examples/gpio_events.rs @@ -25,7 +25,7 @@ fn main() -> Result<()> { } let mut lsettings = line::Settings::new()?; - let lconfig = line::Config::new()?; + let mut lconfig = line::Config::new()?; let mut offsets = Vec::::new(); for arg in &args[2..] { diff --git a/bindings/rust/libgpiod/examples/gpio_threaded_info_events.rs b/bindings/rust/libgpiod/examples/gpio_threaded_info_events.rs index 367b2f6..620f4ec 100644 --- a/bindings/rust/libgpiod/examples/gpio_threaded_info_events.rs +++ b/bindings/rust/libgpiod/examples/gpio_threaded_info_events.rs @@ -32,12 +32,12 @@ fn request_reconfigure_line( rx: Receiver<()>, ) { thread::spawn(move || { - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let lsettings = line::Settings::new().unwrap(); lconfig.add_line_settings(&[offset], lsettings).unwrap(); let rconfig = request::Config::new().unwrap(); - let request = chip + let mut request = chip .lock() .unwrap() .request_lines(Some(&rconfig), &lconfig) @@ -49,7 +49,7 @@ fn request_reconfigure_line( // Wait for parent to signal rx.recv().expect("Could not receive from channel"); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings.set_direction(Direction::Output).unwrap(); lconfig.add_line_settings(&[offset], lsettings).unwrap(); diff --git a/bindings/rust/libgpiod/examples/gpioget.rs b/bindings/rust/libgpiod/examples/gpioget.rs index 74baf30..a71612b 100644 --- a/bindings/rust/libgpiod/examples/gpioget.rs +++ b/bindings/rust/libgpiod/examples/gpioget.rs @@ -20,7 +20,7 @@ fn main() -> Result<()> { } let mut lsettings = line::Settings::new()?; - let lconfig = line::Config::new()?; + let mut lconfig = line::Config::new()?; let mut offsets = Vec::::new(); for arg in &args[2..] { @@ -34,7 +34,7 @@ fn main() -> Result<()> { let path = format!("/dev/gpiochip{}", args[1]); let chip = Chip::open(&path)?; - let rconfig = request::Config::new()?; + let mut rconfig = request::Config::new()?; rconfig.set_consumer(&args[0])?; let request = chip.request_lines(Some(&rconfig), &lconfig)?; diff --git a/bindings/rust/libgpiod/examples/gpiomon.rs b/bindings/rust/libgpiod/examples/gpiomon.rs index a09ddfc..8f2a71a 100644 --- a/bindings/rust/libgpiod/examples/gpiomon.rs +++ b/bindings/rust/libgpiod/examples/gpiomon.rs @@ -24,7 +24,7 @@ fn main() -> Result<()> { } let mut lsettings = line::Settings::new()?; - let lconfig = line::Config::new()?; + let mut lconfig = line::Config::new()?; let mut offsets = Vec::::new(); for arg in &args[2..] { diff --git a/bindings/rust/libgpiod/examples/gpioset.rs b/bindings/rust/libgpiod/examples/gpioset.rs index 6247996..4b43010 100644 --- a/bindings/rust/libgpiod/examples/gpioset.rs +++ b/bindings/rust/libgpiod/examples/gpioset.rs @@ -24,7 +24,7 @@ fn main() -> Result<()> { return Err(Error::InvalidArguments); } - let lconfig = line::Config::new()?; + let mut lconfig = line::Config::new()?; for arg in &args[2..] { let pair: Vec<&str> = arg.split('=').collect(); @@ -51,7 +51,7 @@ fn main() -> Result<()> { let path = format!("/dev/gpiochip{}", args[1]); let chip = Chip::open(&path)?; - let rconfig = request::Config::new()?; + let mut rconfig = request::Config::new()?; rconfig.set_consumer(&args[0])?; chip.request_lines(Some(&rconfig), &lconfig)?; diff --git a/bindings/rust/libgpiod/src/line_config.rs b/bindings/rust/libgpiod/src/line_config.rs index 0c8b293..2169bf1 100644 --- a/bindings/rust/libgpiod/src/line_config.rs +++ b/bindings/rust/libgpiod/src/line_config.rs @@ -56,7 +56,7 @@ impl Config { } /// Add line settings for a set of offsets. - pub fn add_line_settings(&self, offsets: &[Offset], settings: Settings) -> Result<()> { + pub fn add_line_settings(&mut self, offsets: &[Offset], settings: Settings) -> Result<&mut Self> { // SAFETY: `gpiod_line_config` is guaranteed to be valid here. let ret = unsafe { gpiod::gpiod_line_config_add_line_settings( @@ -73,7 +73,7 @@ impl Config { errno::errno(), )) } else { - Ok(()) + Ok(self) } } diff --git a/bindings/rust/libgpiod/src/line_request.rs b/bindings/rust/libgpiod/src/line_request.rs index b843862..8ad8e25 100644 --- a/bindings/rust/libgpiod/src/line_request.rs +++ b/bindings/rust/libgpiod/src/line_request.rs @@ -93,7 +93,7 @@ impl Request { } /// Set the value of a single line associated with the request. - pub fn set_value(&self, offset: Offset, value: Value) -> Result<()> { + pub fn set_value(&mut self, offset: Offset, value: Value) -> Result<&mut Self> { // SAFETY: `gpiod_line_request` is guaranteed to be valid here. let ret = unsafe { gpiod::gpiod_line_request_set_value(self.request, offset, value.value()) }; @@ -104,12 +104,12 @@ impl Request { errno::errno(), )) } else { - Ok(()) + Ok(self) } } /// Get values of a subset of lines associated with the request. - pub fn set_values_subset(&self, map: ValueMap) -> Result<()> { + pub fn set_values_subset(&mut self, map: ValueMap) -> Result<&mut Self> { let mut offsets = Vec::new(); let mut values = Vec::new(); @@ -134,12 +134,12 @@ impl Request { errno::errno(), )) } else { - Ok(()) + Ok(self) } } /// Get values of all lines associated with the request. - pub fn set_values(&self, values: &[Value]) -> Result<()> { + pub fn set_values(&mut self, values: &[Value]) -> Result<&mut Self> { if values.len() != self.num_lines() as usize { return Err(Error::InvalidArguments); } @@ -159,12 +159,12 @@ impl Request { errno::errno(), )) } else { - Ok(()) + Ok(self) } } /// Update the configuration of lines associated with the line request. - pub fn reconfigure_lines(&self, lconfig: &line::Config) -> Result<()> { + pub fn reconfigure_lines(&mut self, lconfig: &line::Config) -> Result<&mut Self> { // SAFETY: `gpiod_line_request` is guaranteed to be valid here. let ret = unsafe { gpiod::gpiod_line_request_reconfigure_lines(self.request, lconfig.config) }; @@ -175,7 +175,7 @@ impl Request { errno::errno(), )) } else { - Ok(()) + Ok(self) } } diff --git a/bindings/rust/libgpiod/src/request_config.rs b/bindings/rust/libgpiod/src/request_config.rs index 9d38548..939838c 100644 --- a/bindings/rust/libgpiod/src/request_config.rs +++ b/bindings/rust/libgpiod/src/request_config.rs @@ -40,7 +40,7 @@ impl Config { /// /// If the consumer string is too long, it will be truncated to the max /// accepted length. - pub fn set_consumer(&self, consumer: &str) -> Result<()> { + pub fn set_consumer(&mut self, consumer: &str) -> Result<&mut Self> { let consumer = CString::new(consumer).map_err(|_| Error::InvalidString)?; // SAFETY: `gpiod_request_config` is guaranteed to be valid here. @@ -51,7 +51,7 @@ impl Config { ) } - Ok(()) + Ok(self) } /// Get the consumer name configured in the request config. @@ -73,9 +73,11 @@ impl Config { } /// Set the size of the kernel event buffer for the request. - pub fn set_event_buffer_size(&self, size: usize) { + pub fn set_event_buffer_size(&mut self, size: usize) -> &mut Self { // SAFETY: `gpiod_request_config` is guaranteed to be valid here. unsafe { gpiod::gpiod_request_config_set_event_buffer_size(self.config, size as c_ulong) } + + self } /// Get the edge event buffer size setting for the request config. diff --git a/bindings/rust/libgpiod/tests/common/config.rs b/bindings/rust/libgpiod/tests/common/config.rs index b838b66..36ccc94 100644 --- a/bindings/rust/libgpiod/tests/common/config.rs +++ b/bindings/rust/libgpiod/tests/common/config.rs @@ -43,7 +43,7 @@ impl TestConfig { } } - pub(crate) fn rconfig_set_consumer(&self, consumer: &str) { + pub(crate) fn rconfig_set_consumer(&mut self, consumer: &str) { self.rconfig.set_consumer(consumer).unwrap(); } @@ -100,7 +100,7 @@ impl TestConfig { pub(crate) fn lconfig_add_settings(&mut self, offsets: &[Offset]) { self.lconfig .add_line_settings(offsets, self.lsettings.take().unwrap()) - .unwrap() + .unwrap(); } pub(crate) fn request_lines(&mut self) -> Result<()> { @@ -128,8 +128,8 @@ impl TestConfig { self.lsettings.as_mut().unwrap() } - pub(crate) fn request(&self) -> &request::Request { - self.request.as_ref().unwrap() + pub(crate) fn request(&mut self) -> &mut request::Request { + self.request.as_mut().unwrap() } } diff --git a/bindings/rust/libgpiod/tests/info_event.rs b/bindings/rust/libgpiod/tests/info_event.rs index 6bf7a0f..f06dd2d 100644 --- a/bindings/rust/libgpiod/tests/info_event.rs +++ b/bindings/rust/libgpiod/tests/info_event.rs @@ -24,12 +24,12 @@ mod info_event { fn request_reconfigure_line(chip: Arc>, tx: Sender<()>, rx: Receiver<()>) { thread::spawn(move || { - let lconfig1 = line::Config::new().unwrap(); + let mut lconfig1 = line::Config::new().unwrap(); let lsettings = line::Settings::new().unwrap(); lconfig1.add_line_settings(&[7], lsettings).unwrap(); let rconfig = request::Config::new().unwrap(); - let request = chip + let mut request = chip .lock() .unwrap() .request_lines(Some(&rconfig), &lconfig1) @@ -41,7 +41,7 @@ mod info_event { // Wait for parent to signal rx.recv().expect("Could not receive from channel"); - let lconfig2 = line::Config::new().unwrap(); + let mut lconfig2 = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings.set_direction(Direction::Output).unwrap(); lconfig2.add_line_settings(&[7], lsettings).unwrap(); diff --git a/bindings/rust/libgpiod/tests/line_config.rs b/bindings/rust/libgpiod/tests/line_config.rs index 95f2178..92a7af3 100644 --- a/bindings/rust/libgpiod/tests/line_config.rs +++ b/bindings/rust/libgpiod/tests/line_config.rs @@ -33,7 +33,7 @@ mod line_config { .unwrap(); // Add settings for multiple lines - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); lconfig.add_line_settings(&[0, 1, 2], lsettings1).unwrap(); lconfig.add_line_settings(&[4, 5], lsettings2).unwrap(); diff --git a/bindings/rust/libgpiod/tests/line_request.rs b/bindings/rust/libgpiod/tests/line_request.rs index c3fc37b..561f4e8 100644 --- a/bindings/rust/libgpiod/tests/line_request.rs +++ b/bindings/rust/libgpiod/tests/line_request.rs @@ -97,13 +97,11 @@ mod line_request { config.lconfig_add_settings(&offsets); config.request_lines().unwrap(); - let request = config.request(); - // Single values read properly - assert_eq!(request.value(7).unwrap(), Value::Active); + assert_eq!(config.request().value(7).unwrap(), Value::Active); // Values read properly - let map = request.values().unwrap(); + let map = config.request().values().unwrap(); for i in 0..offsets.len() { assert_eq!( *map.get(offsets[i].into()).unwrap(), @@ -115,7 +113,7 @@ mod line_request { } // Subset of values read properly - let map = request.values_subset(&[2, 0, 6]).unwrap(); + let map = config.request().values_subset(&[2, 0, 6]).unwrap(); assert_eq!(*map.get(2).unwrap(), Value::InActive); assert_eq!(*map.get(0).unwrap(), Value::InActive); assert_eq!(*map.get(6).unwrap(), Value::Active); @@ -123,10 +121,10 @@ mod line_request { // Value read properly after reconfigure let mut lsettings = line::Settings::new().unwrap(); lsettings.set_active_low(true); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); lconfig.add_line_settings(&offsets, lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); - assert_eq!(request.value(7).unwrap(), Value::InActive); + config.request().reconfigure_lines(&lconfig).unwrap(); + assert_eq!(config.request().value(7).unwrap(), Value::InActive); } #[test] @@ -153,22 +151,21 @@ mod line_request { config.lconfig_val(Some(Direction::Output), Some(Value::InActive)); config.lconfig_add_settings(&offsets); config.request_lines().unwrap(); - let request = config.request(); // Set single value - request.set_value(1, Value::Active).unwrap(); + config.request().set_value(1, Value::Active).unwrap(); assert_eq!(config.sim_val(0).unwrap(), SimValue::InActive); assert_eq!(config.sim_val(1).unwrap(), SimValue::Active); assert_eq!(config.sim_val(3).unwrap(), SimValue::InActive); assert_eq!(config.sim_val(4).unwrap(), SimValue::InActive); - request.set_value(1, Value::InActive).unwrap(); + config.request().set_value(1, Value::InActive).unwrap(); assert_eq!(config.sim_val(1).unwrap(), SimValue::InActive); // Set values of subset let mut map = ValueMap::new(); map.insert(4, Value::Active); map.insert(3, Value::Active); - request.set_values_subset(map).unwrap(); + config.request().set_values_subset(map).unwrap(); assert_eq!(config.sim_val(0).unwrap(), SimValue::InActive); assert_eq!(config.sim_val(1).unwrap(), SimValue::InActive); assert_eq!(config.sim_val(3).unwrap(), SimValue::Active); @@ -177,12 +174,12 @@ mod line_request { let mut map = ValueMap::new(); map.insert(4, Value::InActive); map.insert(3, Value::InActive); - request.set_values_subset(map).unwrap(); + config.request().set_values_subset(map).unwrap(); assert_eq!(config.sim_val(3).unwrap(), SimValue::InActive); assert_eq!(config.sim_val(4).unwrap(), SimValue::InActive); // Set all values - request + config.request() .set_values(&[ Value::Active, Value::InActive, @@ -194,7 +191,7 @@ mod line_request { assert_eq!(config.sim_val(1).unwrap(), SimValue::InActive); assert_eq!(config.sim_val(3).unwrap(), SimValue::Active); assert_eq!(config.sim_val(4).unwrap(), SimValue::InActive); - request + config.request() .set_values(&[ Value::InActive, Value::InActive, @@ -246,12 +243,10 @@ mod line_request { config.lconfig_add_settings(&offsets); config.request_lines().unwrap(); - let request = config.request(); - // Reconfigure let mut lsettings = line::Settings::new().unwrap(); lsettings.set_direction(Direction::Input).unwrap(); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); // The uAPI config has only 10 attribute slots, this should pass. for offset in offsets { @@ -261,7 +256,7 @@ mod line_request { .unwrap(); } - assert!(request.reconfigure_lines(&lconfig).is_ok()); + assert!(config.request().reconfigure_lines(&lconfig).is_ok()); // The uAPI config has only 10 attribute slots, and this is the 11th entry. // This should fail with E2BIG. @@ -269,7 +264,7 @@ mod line_request { lconfig.add_line_settings(&[11], lsettings).unwrap(); assert_eq!( - request.reconfigure_lines(&lconfig).unwrap_err(), + config.request().reconfigure_lines(&lconfig).unwrap_err(), ChipError::OperationFailed( OperationType::LineRequestReconfigLines, errno::Errno(E2BIG), @@ -285,10 +280,8 @@ mod line_request { let info = config.chip().line_info(0).unwrap(); assert_eq!(info.bias().unwrap(), None); - let request = config.request(); - // Reconfigure - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -297,11 +290,11 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.bias().unwrap(), Some(Bias::PullUp)); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -310,11 +303,11 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.bias().unwrap(), Some(Bias::PullDown)); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -323,7 +316,7 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.bias().unwrap(), Some(Bias::Disabled)); } @@ -336,10 +329,8 @@ mod line_request { let info = config.chip().line_info(0).unwrap(); assert_eq!(info.drive().unwrap(), Drive::PushPull); - let request = config.request(); - // Reconfigure - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -348,11 +339,11 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.drive().unwrap(), Drive::PushPull); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -361,11 +352,11 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.drive().unwrap(), Drive::OpenDrain); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -374,7 +365,7 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.drive().unwrap(), Drive::OpenSource); } @@ -387,10 +378,8 @@ mod line_request { let info = config.chip().line_info(0).unwrap(); assert_eq!(info.edge_detection().unwrap(), None); - let request = config.request(); - // Reconfigure - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -399,11 +388,11 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.edge_detection().unwrap(), Some(Edge::Both)); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -412,11 +401,11 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.edge_detection().unwrap(), Some(Edge::Rising)); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -425,7 +414,7 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.edge_detection().unwrap(), Some(Edge::Falling)); } @@ -438,22 +427,20 @@ mod line_request { let info = config.chip().line_info(0).unwrap(); assert_eq!(info.event_clock().unwrap(), EventClock::Monotonic); - let request = config.request(); - // Reconfigure - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings.set_event_clock(EventClock::Monotonic).unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.event_clock().unwrap(), EventClock::Monotonic); - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings.set_event_clock(EventClock::Realtime).unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.event_clock().unwrap(), EventClock::Realtime); } @@ -467,14 +454,12 @@ mod line_request { let info = config.chip().line_info(0).unwrap(); assert_eq!(info.event_clock().unwrap(), EventClock::Monotonic); - let request = config.request(); - // Reconfigure - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings.set_event_clock(EventClock::HTE).unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert_eq!(info.event_clock().unwrap(), EventClock::HTE); } @@ -488,10 +473,8 @@ mod line_request { assert!(!info.is_debounced()); assert_eq!(info.debounce_period(), Duration::from_millis(0)); - let request = config.request(); - // Reconfigure - let lconfig = line::Config::new().unwrap(); + let mut lconfig = line::Config::new().unwrap(); let mut lsettings = line::Settings::new().unwrap(); lsettings .set_prop(&[ @@ -500,7 +483,7 @@ mod line_request { ]) .unwrap(); lconfig.add_line_settings(&[0], lsettings).unwrap(); - request.reconfigure_lines(&lconfig).unwrap(); + config.request().reconfigure_lines(&lconfig).unwrap(); let info = config.chip().line_info(0).unwrap(); assert!(info.is_debounced()); assert_eq!(info.debounce_period(), Duration::from_millis(100)); diff --git a/bindings/rust/libgpiod/tests/request_config.rs b/bindings/rust/libgpiod/tests/request_config.rs index 8c67638..d78c4bd 100644 --- a/bindings/rust/libgpiod/tests/request_config.rs +++ b/bindings/rust/libgpiod/tests/request_config.rs @@ -27,7 +27,7 @@ mod request_config { #[test] fn initialized() { const CONSUMER: &str = "foobar"; - let rconfig = request::Config::new().unwrap(); + let mut rconfig = request::Config::new().unwrap(); rconfig.set_consumer(CONSUMER).unwrap(); rconfig.set_event_buffer_size(64); From patchwork Fri Jan 13 21:52:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 644776 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 4C050C3DA78 for ; Fri, 13 Jan 2023 21:52:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230527AbjAMVwj (ORCPT ); Fri, 13 Jan 2023 16:52:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231168AbjAMVwf (ORCPT ); Fri, 13 Jan 2023 16:52:35 -0500 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A720489BE3 for ; Fri, 13 Jan 2023 13:52:34 -0800 (PST) Received: by mail-wm1-x32c.google.com with SMTP id j34-20020a05600c1c2200b003da1b054057so3936903wms.5 for ; Fri, 13 Jan 2023 13:52:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gQcLeZIkcK2mK3eX1QbdQa48abhjGftWAhXaMFjukPc=; b=aQ+rFbitz6NDPCMORsc7vtSasntegG+7mknPc/BxyPuXTQ6qeC8CoZdRFjdKhu9lRJ nvD9zFSEY3HRkDCYuXG2oG+q4D95rL439h0MLlhwoVqAP3a0DBxqAit/ezclu7iEzwTu 1XzpbgqMe2vpdtK1ABhXqWqcf1mPWgv8gGp+JQARAQ0x3AZd9Ptgx8ZCj0C8dKQTn+dL e9sk3Vr/4D6Zdy/FUCyiDVMn6j9IatJsyHFQeU5GZc/XFvA0AGn+Zuj7nRsw3vA52xY+ R7PypL6vtvzOJWLtKv0hF8EeGfcO77tqwVE1ijnZiPoN6t+fVCT0KvF7Hg2iRODWpD0h VAAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gQcLeZIkcK2mK3eX1QbdQa48abhjGftWAhXaMFjukPc=; b=cE9zU73jK+EEtd/8+gsbc+LyAL+AekpicKEnTAhSafhsN0rgyOtYnBLdxJt/3Rj1tn eLzSu0H2e7qsurqf/gx6HvlBhWQtmQT0MwisV7stgmm/0P14uZigb1A2TUNJcXcDSXrf PsueVXSSli3GDR7Vryl/XQR59c0lpzy+FsDs3cqPxo+hpI5kWmJuUJpzThK+mTOD1GdF J0hA5gTTa/vHw1qjg+cynw334Hre19gUbqzV+Y49xh2SeRFNHjxGnDzTx7KNXsNpcAoZ 3sDGAwOt1ClLfZ9A/9dn4BKSkjeoSAvM50xaK1kli9BF2T6kHWfuQGnTTLtyN5Da7J+w kBXQ== X-Gm-Message-State: AFqh2kp9gyPRkYWq1MI8Fb991fyqN+E1v3dsaYRefJuzBpoNohmzFlwB rdF6Zq6A+gIl3gBP8NkbQ9Be0g== X-Google-Smtp-Source: AMrXdXuHg/ow3Au/0tyZ3Z39e4trXOLaOr5WZRjHnT0fJ3gQrNnCvCGQSDbIu6ud5oYXc84gXA0NlA== X-Received: by 2002:a05:600c:3b14:b0:3cf:d18e:528b with SMTP id m20-20020a05600c3b1400b003cfd18e528bmr906746wms.39.1673646753212; Fri, 13 Jan 2023 13:52:33 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ba79:38ad:100f:e9ee]) by smtp.gmail.com with ESMTPSA id g12-20020a05600c310c00b003c70191f267sm33179234wmo.39.2023.01.13.13.52.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:52:32 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 16/16] bindings: rust: provide line_config.set_output_values() Date: Fri, 13 Jan 2023 22:52:10 +0100 Message-Id: <20230113215210.616812-17-brgl@bgdev.pl> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113215210.616812-1-brgl@bgdev.pl> References: <20230113215210.616812-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Add a new function to line config allowing to set a list of output values for requested lines. This works very similarily to the C++ version of the new C interface. Signed-off-by: Bartosz Golaszewski --- bindings/rust/libgpiod/src/lib.rs | 1 + bindings/rust/libgpiod/src/line_config.rs | 24 +++++++- bindings/rust/libgpiod/tests/line_config.rs | 62 +++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/bindings/rust/libgpiod/src/lib.rs b/bindings/rust/libgpiod/src/lib.rs index a5d018c..f268ceb 100644 --- a/bindings/rust/libgpiod/src/lib.rs +++ b/bindings/rust/libgpiod/src/lib.rs @@ -71,6 +71,7 @@ pub enum OperationType { InfoEventGetLineInfo, LineConfigNew, LineConfigAddSettings, + LineConfigSetOutputValues, LineConfigGetOffsets, LineConfigGetSettings, LineRequestReconfigLines, diff --git a/bindings/rust/libgpiod/src/line_config.rs b/bindings/rust/libgpiod/src/line_config.rs index 2169bf1..b276cf0 100644 --- a/bindings/rust/libgpiod/src/line_config.rs +++ b/bindings/rust/libgpiod/src/line_config.rs @@ -7,7 +7,7 @@ use std::collections::HashMap; use super::{ gpiod, - line::{Offset, Settings}, + line::{Offset, Settings, Value}, Error, OperationType, Result, }; @@ -77,6 +77,28 @@ impl Config { } } + /// Set output values for a number of lines. + pub fn set_output_values(&mut self, values: &[Value]) -> Result<&mut Self> { + let mut mapped_values = Vec::new(); + for value in values { + mapped_values.push(value.value()); + } + + let ret = unsafe { + gpiod::gpiod_line_config_set_output_values(self.config, mapped_values.as_ptr(), + values.len() as u64) + }; + + if ret == -1 { + Err(Error::OperationFailed( + OperationType::LineConfigSetOutputValues, + errno::errno(), + )) + } else { + Ok(self) + } + } + /// Get a mapping of offsets to line settings stored by this object. pub fn line_settings(&self) -> Result> { let mut map: HashMap = HashMap::new(); diff --git a/bindings/rust/libgpiod/tests/line_config.rs b/bindings/rust/libgpiod/tests/line_config.rs index 92a7af3..96ce127 100644 --- a/bindings/rust/libgpiod/tests/line_config.rs +++ b/bindings/rust/libgpiod/tests/line_config.rs @@ -5,9 +5,11 @@ mod common; mod line_config { + use libgpiod::chip::Chip; use libgpiod::line::{ self, Bias, Direction, Drive, Edge, EventClock, SettingKind, SettingVal, Value, }; + use gpiosim_sys::Sim; #[test] fn settings() { @@ -76,4 +78,64 @@ mod line_config { SettingVal::OutputValue(Value::Active) ); } + + #[test] + fn set_global_output_values() { + let sim = Sim::new(Some(4), None, true).unwrap(); + let mut settings = line::Settings::new().unwrap(); + settings.set_direction(Direction::Output).unwrap(); + + let mut config = line::Config::new().unwrap(); + config + .add_line_settings(&[0, 1, 2, 3], settings) + .unwrap() + .set_output_values(&[ + Value::Active, + Value::InActive, + Value::Active, + Value::InActive + ]) + .unwrap(); + + let chip = Chip::open(&sim.dev_path()).unwrap(); + let _request = chip.request_lines(None, &config); + + assert_eq!(sim.val(0).unwrap(), gpiosim_sys::Value::Active); + assert_eq!(sim.val(1).unwrap(), gpiosim_sys::Value::InActive); + assert_eq!(sim.val(2).unwrap(), gpiosim_sys::Value::Active); + assert_eq!(sim.val(3).unwrap(), gpiosim_sys::Value::InActive); + } + + #[test] + fn read_back_global_output_values() { + let mut settings = line::Settings::new().unwrap(); + settings + .set_direction(Direction::Output) + .unwrap() + .set_output_value(Value::Active) + .unwrap(); + + let mut config = line::Config::new().unwrap(); + config + .add_line_settings(&[0, 1, 2, 3], settings) + .unwrap() + .set_output_values(&[ + Value::Active, + Value::InActive, + Value::Active, + Value::InActive, + ]) + .unwrap(); + + assert_eq!( + config + .line_settings() + .unwrap() + .get(&1) + .unwrap() + .output_value() + .unwrap(), + Value::InActive + ); + } }