From patchwork Mon Nov 14 04:00:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 624968 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 93E0FC433FE for ; Mon, 14 Nov 2022 04:01:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235709AbiKNEBg (ORCPT ); Sun, 13 Nov 2022 23:01:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235468AbiKNEBe (ORCPT ); Sun, 13 Nov 2022 23:01:34 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63F5911C16 for ; Sun, 13 Nov 2022 20:01:31 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id y4so8991600plb.2 for ; Sun, 13 Nov 2022 20:01:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.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=gm0YjHEAS3haysUbR7ldAyNuhTY3SPamX9ZiU/QswcE=; b=DyvnLGj8iuxhS5rojVdH1RfWkzE79VWuVePPGGmQ0uoD94hRTTBfXMCUyZCIzIQnbY BT3jILPfV2cWnbjAHp5DlnEfTkoXBh44wUzi5kTX2rxmdUfPsQg0T7u278xOi28ILDbm xizpyv4Wxty9oUoh+mj6NIag8LF10bRU75Inw861uhuOhszWoLDe5xOQMgEWZI2ajZ+Y Rt6k2iMg7UVvCGIz6Slcrclk7699AhOXBRfz3wr1q4L9k6PqyHRZZe04QOCLme1n5vmb AI9R+wibbyKZfbkyB/wqA1ZNYmj/bMjI5wG641eVJXA8SLUVNIu8J6wvoJ8JALLxnfC2 4kgQ== 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=gm0YjHEAS3haysUbR7ldAyNuhTY3SPamX9ZiU/QswcE=; b=xEh5qa3htAstXNCTqCMnwNNEIWrYCE23VqxPCWA5uzLIUBPmwmrKurzmVh2QLiyiuq Mnx/pwo3EiDJ/LSNROy0v4z9Tp79xgHxBvkb9T1Q1tDuhT6Mti3n0I2ih6Ha1sVW21BC djs7ubrKvszdGX9PMZmzXXtHj7O7bcCQneq7rSa96aVYHnXukX2oHID4+tKiOQLanAeL Ax9XBxkoiB7XEOIkTyCLHE2GOzL7VHRrzwJZQyTDSbBznUFmwV2fKPV0J6OqazrPcO7k rlJtMG6wi02vVQsnQlpMoTgPQIMbjc4vC03OMijdzkIisuvOnsvOfyzUZwPpqQ1hV+nC Xg/Q== X-Gm-Message-State: ANoB5pmn8Z4d9luBzMCFvzCxpiEPzlRkiiNgaLIUNOpfKRH37A3UGegt ePataD9C+Dnxk6xdcR5h2zhun+cgGPE= X-Google-Smtp-Source: AA0mqf4ciMVCl47fd8cWY7M9vKvWKLuC763VeMyoFv9DpK4g4RuZTaI9b2mfqxDVVRG+HNmXJZuv+A== X-Received: by 2002:a17:902:ec87:b0:187:282c:9ba4 with SMTP id x7-20020a170902ec8700b00187282c9ba4mr12133476plg.42.1668398489653; Sun, 13 Nov 2022 20:01:29 -0800 (PST) Received: from sol.home.arpa (14-200-229-209.tpgi.com.au. [14.200.229.209]) by smtp.gmail.com with ESMTPSA id f11-20020a170902684b00b001801aec1f6bsm6018636pln.141.2022.11.13.20.01.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:01:29 -0800 (PST) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod v2][PATCH v4 1/5] tools: remove old code to simplify review Date: Mon, 14 Nov 2022 12:00:58 +0800 Message-Id: <20221114040102.66031-2-warthog618@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221114040102.66031-1-warthog618@gmail.com> References: <20221114040102.66031-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Remove functions that are so heavily modified in the subsequent patch that they are effectively complete re-writes, so the old code just obfuscates the new. The gpiofind tool is removed completely, as its functionality is absorbed into the other tools. Signed-off-by: Kent Gibson --- man/Makefile.am | 2 +- tools/.gitignore | 1 - tools/Makefile.am | 4 +- tools/gpio-tools-test.bats | 893 ------------------------------------- tools/gpiodetect.c | 79 ---- tools/gpiofind.c | 93 ---- tools/gpioget.c | 153 ------- tools/gpioinfo.c | 240 ---------- tools/gpiomon.c | 314 ------------- tools/gpioset.c | 330 -------------- tools/tools-common.c | 81 ---- 11 files changed, 2 insertions(+), 2188 deletions(-) delete mode 100644 tools/gpiofind.c diff --git a/man/Makefile.am b/man/Makefile.am index 4d2c29b..8d1d9b3 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -3,7 +3,7 @@ if WITH_MANPAGES -dist_man1_MANS = gpiodetect.man gpioinfo.man gpioget.man gpioset.man gpiofind.man gpiomon.man +dist_man1_MANS = gpiodetect.man gpioinfo.man gpioget.man gpioset.man gpiomon.man %.man: $(top_builddir)/tools/$(*F) help2man $(top_builddir)/tools/$(*F) --include=$(srcdir)/template --output=$(builddir)/$@ --no-info diff --git a/tools/.gitignore b/tools/.gitignore index 0d53de9..d6b2f44 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -6,4 +6,3 @@ gpioinfo gpioget gpioset gpiomon -gpiofind diff --git a/tools/Makefile.am b/tools/Makefile.am index 4a13266..fc074b9 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -9,7 +9,7 @@ libtools_common_la_SOURCES = tools-common.c tools-common.h LDADD = libtools-common.la $(top_builddir)/lib/libgpiod.la -bin_PROGRAMS = gpiodetect gpioinfo gpioget gpioset gpiomon gpiofind +bin_PROGRAMS = gpiodetect gpioinfo gpioget gpioset gpiomon gpiodetect_SOURCES = gpiodetect.c @@ -21,8 +21,6 @@ gpioset_SOURCES = gpioset.c gpiomon_SOURCES = gpiomon.c -gpiofind_SOURCES = gpiofind.c - EXTRA_DIST = gpio-tools-test gpio-tools-test.bats if WITH_TESTS diff --git a/tools/gpio-tools-test.bats b/tools/gpio-tools-test.bats index a259eae..d200df1 100755 --- a/tools/gpio-tools-test.bats +++ b/tools/gpio-tools-test.bats @@ -220,896 +220,3 @@ teardown() { gpiosim_cleanup } -# -# gpiodetect test cases -# - -@test "gpiodetect: list chips" { - gpiosim_chip sim0 num_lines=4 - gpiosim_chip sim1 num_lines=8 - gpiosim_chip sim2 num_lines=16 - - run_tool gpiodetect - - test "$status" -eq 0 - output_contains_line "$(gpiosim_chip_name sim0) [$(gpiosim_dev_name sim0)-node0] (4 lines)" - output_contains_line "$(gpiosim_chip_name sim1) [$(gpiosim_dev_name sim1)-node0] (8 lines)" - output_contains_line "$(gpiosim_chip_name sim2) [$(gpiosim_dev_name sim2)-node0] (16 lines)" -} - -@test "gpiodetect: invalid args" { - run_tool gpiodetect unimplemented-arg - test "$status" -eq 1 -} - -# -# gpioinfo test cases -# - -@test "gpioinfo: dump all chips" { - gpiosim_chip sim0 num_lines=4 - gpiosim_chip sim1 num_lines=8 - - run_tool gpioinfo - - test "$status" -eq 0 - output_contains_line "$(gpiosim_chip_name sim0) - 4 lines:" - output_contains_line "$(gpiosim_chip_name sim1) - 8 lines:" - - output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+unused\\s+input\\s+active-high" - output_regex_match "\\s+line\\s+7:\\s+unnamed\\s+unused\\s+input\\s+active-high" -} - -@test "gpioinfo: dump all chips with one line exported" { - gpiosim_chip sim0 num_lines=4 - gpiosim_chip sim1 num_lines=8 - - coproc_run_tool gpioset --mode=signal --active-low "$(gpiosim_chip_name sim1)" 7=1 - - run_tool gpioinfo - - test "$status" -eq 0 - output_contains_line "$(gpiosim_chip_name sim0) - 4 lines:" - output_contains_line "$(gpiosim_chip_name sim1) - 8 lines:" - output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+unused\\s+input\\s+active-high" - output_regex_match "\\s+line\\s+7:\\s+unnamed\\s+\\\"gpioset\\\"\\s+output\\s+active-low" - - coproc_tool_kill - coproc_tool_wait -} - -@test "gpioinfo: dump one chip" { - gpiosim_chip sim0 num_lines=8 - gpiosim_chip sim1 num_lines=4 - - run_tool gpioinfo "$(gpiosim_chip_name sim1)" - - test "$status" -eq 0 - assert_fail output_contains_line "$(gpiosim_chip_name sim0) - 8 lines:" - output_contains_line "$(gpiosim_chip_name sim1) - 4 lines:" - output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+unused\\s+input\\s+active-high" - assert_fail output_regex_match "\\s+line\\s+7:\\s+unnamed\\s+unused\\s+input\\s+active-high" -} - -@test "gpioinfo: dump all but one chip" { - gpiosim_chip sim0 num_lines=4 - gpiosim_chip sim1 num_lines=4 - gpiosim_chip sim2 num_lines=8 - gpiosim_chip sim3 num_lines=4 - - run_tool gpioinfo "$(gpiosim_chip_name sim0)" \ - "$(gpiosim_chip_name sim1)" "$(gpiosim_chip_name sim3)" - - test "$status" -eq 0 - output_contains_line "$(gpiosim_chip_name sim0) - 4 lines:" - output_contains_line "$(gpiosim_chip_name sim1) - 4 lines:" - assert_fail output_contains_line "$(gpiosim_chip_name sim2) - 8 lines:" - output_contains_line "$(gpiosim_chip_name sim3) - 4 lines:" - output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+unused\\s+input\\s+active-high" - assert_fail output_regex_match "\\s+line\\s+7:\\s+unnamed\\s+unused\\s+input\\s+active-high" -} - -@test "gpioinfo: inexistent chip" { - run_tool gpioinfo "inexistent" - - test "$status" -eq 1 -} - -# -# gpiofind test cases -# - -@test "gpiofind: line found" { - gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=3:bar - gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=4:xyz line_name=7:foobar - gpiosim_chip sim2 num_lines=16 - - run_tool gpiofind foobar - - test "$status" -eq "0" - test "$output" = "$(gpiosim_chip_name sim1) 7" -} - -@test "gpiofind: line not found" { - gpiosim_chip sim0 num_lines=4 - gpiosim_chip sim1 num_lines=8 - gpiosim_chip sim2 num_lines=16 - - run_tool gpiofind nonexistent-line - - test "$status" -eq "1" -} - -@test "gpiofind: invalid args" { - run_tool gpiodetect unimplemented-arg - test "$status" -eq 1 -} - -# -# gpioget test cases -# - -@test "gpioget: read all lines" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 2 pull-up - gpiosim_set_pull sim0 3 pull-up - gpiosim_set_pull sim0 5 pull-up - gpiosim_set_pull sim0 7 pull-up - - run_tool gpioget "$(gpiosim_chip_name sim0)" 0 1 2 3 4 5 6 7 - - test "$status" -eq "0" - test "$output" = "0 0 1 1 0 1 0 1" -} - -@test "gpioget: read all lines (active-low)" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 2 pull-up - gpiosim_set_pull sim0 3 pull-up - gpiosim_set_pull sim0 5 pull-up - gpiosim_set_pull sim0 7 pull-up - - run_tool gpioget --active-low "$(gpiosim_chip_name sim0)" 0 1 2 3 4 5 6 7 - - test "$status" -eq "0" - test "$output" = "1 1 0 0 1 0 1 0" -} - -@test "gpioget: read all lines (pull-up)" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 2 pull-up - gpiosim_set_pull sim0 3 pull-up - gpiosim_set_pull sim0 5 pull-up - gpiosim_set_pull sim0 7 pull-up - - run_tool gpioget --bias=pull-up "$(gpiosim_chip_name sim0)" 0 1 2 3 4 5 6 7 - - test "$status" -eq "0" - test "$output" = "1 1 1 1 1 1 1 1" -} - -@test "gpioget: read all lines (pull-down)" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 2 pull-up - gpiosim_set_pull sim0 3 pull-up - gpiosim_set_pull sim0 5 pull-up - gpiosim_set_pull sim0 7 pull-up - - run_tool gpioget --bias=pull-down "$(gpiosim_chip_name sim0)" 0 1 2 3 4 5 6 7 - - test "$status" -eq "0" - test "$output" = "0 0 0 0 0 0 0 0" -} - -@test "gpioget: read some lines" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 1 pull-up - gpiosim_set_pull sim0 4 pull-up - gpiosim_set_pull sim0 6 pull-up - - run_tool gpioget "$(gpiosim_chip_name sim0)" 0 1 4 6 - - test "$status" -eq "0" - test "$output" = "0 1 1 1" -} - -@test "gpioget: no arguments" { - run_tool gpioget - - test "$status" -eq "1" - output_regex_match ".*gpiochip must be specified" -} - -@test "gpioget: no lines specified" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioget "$(gpiosim_chip_name sim0)" - - test "$status" -eq "1" - output_regex_match ".*at least one GPIO line offset must be specified" -} - -@test "gpioget: too many lines specified" { - gpiosim_chip sim0 num_lines=4 - - run_tool gpioget "$(gpiosim_chip_name sim0)" 0 1 2 3 4 - - test "$status" -eq "1" - output_regex_match ".*unable to request lines.*" -} - -@test "gpioget: same line twice" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioget "$(gpiosim_chip_name sim0)" 0 0 - - test "$status" -eq "1" - output_regex_match ".*offsets must be unique" -} - -@test "gpioget: invalid bias" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioget --bias=bad "$(gpiosim_chip_name sim0)" 0 1 - - test "$status" -eq "1" - output_regex_match ".*invalid bias.*" -} - -# -# gpioset test cases -# - -@test "gpioset: set lines and wait for SIGTERM" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpioset --mode=signal "$(gpiosim_chip_name sim0)" \ - 0=0 1=0 2=1 3=1 4=1 5=1 6=0 7=1 - - gpiosim_check_value sim0 0 0 - gpiosim_check_value sim0 1 0 - gpiosim_check_value sim0 2 1 - gpiosim_check_value sim0 3 1 - gpiosim_check_value sim0 4 1 - gpiosim_check_value sim0 5 1 - gpiosim_check_value sim0 6 0 - gpiosim_check_value sim0 7 1 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" -} - -@test "gpioset: set lines and wait for SIGTERM (active-low)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpioset --active-low --mode=signal "$(gpiosim_chip_name sim0)" \ - 0=0 1=0 2=1 3=1 4=1 5=1 6=0 7=1 - - gpiosim_check_value sim0 0 1 - gpiosim_check_value sim0 1 1 - gpiosim_check_value sim0 2 0 - gpiosim_check_value sim0 3 0 - gpiosim_check_value sim0 4 0 - gpiosim_check_value sim0 5 0 - gpiosim_check_value sim0 6 1 - gpiosim_check_value sim0 7 0 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" -} - -@test "gpioset: set lines and wait for SIGTERM (push-pull)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpioset --drive=push-pull --mode=signal "$(gpiosim_chip_name sim0)" \ - 0=0 1=0 2=1 3=1 4=1 5=1 6=0 7=1 - - gpiosim_check_value sim0 0 0 - gpiosim_check_value sim0 1 0 - gpiosim_check_value sim0 2 1 - gpiosim_check_value sim0 3 1 - gpiosim_check_value sim0 4 1 - gpiosim_check_value sim0 5 1 - gpiosim_check_value sim0 6 0 - gpiosim_check_value sim0 7 1 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" -} - -@test "gpioset: set lines and wait for SIGTERM (open-drain)" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 2 pull-up - gpiosim_set_pull sim0 3 pull-up - gpiosim_set_pull sim0 5 pull-up - gpiosim_set_pull sim0 7 pull-up - - coproc_run_tool gpioset --drive=open-drain --mode=signal "$(gpiosim_chip_name sim0)" \ - 0=0 1=0 2=1 3=1 4=1 5=1 6=0 7=1 - - gpiosim_check_value sim0 0 0 - gpiosim_check_value sim0 1 0 - gpiosim_check_value sim0 2 1 - gpiosim_check_value sim0 3 1 - gpiosim_check_value sim0 4 0 - gpiosim_check_value sim0 5 1 - gpiosim_check_value sim0 6 0 - gpiosim_check_value sim0 7 1 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" -} - -@test "gpioset: set lines and wait for SIGTERM (open-source)" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 2 pull-up - gpiosim_set_pull sim0 3 pull-up - gpiosim_set_pull sim0 5 pull-up - gpiosim_set_pull sim0 7 pull-up - - coproc_run_tool gpioset --drive=open-source --mode=signal "$(gpiosim_chip_name sim0)" \ - 0=0 1=0 2=1 3=0 4=1 5=1 6=0 7=1 - - gpiosim_check_value sim0 0 0 - gpiosim_check_value sim0 1 0 - gpiosim_check_value sim0 2 1 - gpiosim_check_value sim0 3 1 - gpiosim_check_value sim0 4 1 - gpiosim_check_value sim0 5 1 - gpiosim_check_value sim0 6 0 - gpiosim_check_value sim0 7 1 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" -} - -@test "gpioset: set some lines and wait for ENTER" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpioset --mode=wait "$(gpiosim_chip_name sim0)" \ - 1=0 2=1 5=1 6=0 7=1 - - gpiosim_check_value sim0 1 0 - gpiosim_check_value sim0 2 1 - gpiosim_check_value sim0 5 1 - gpiosim_check_value sim0 6 0 - gpiosim_check_value sim0 7 1 - - coproc_tool_stdin_write "" - coproc_tool_wait - - test "$status" -eq "0" -} - -@test "gpioset: set some lines and wait for SIGINT" { - gpiosim_chip sim0 num_lines=4 - - coproc_run_tool gpioset --mode=signal "$(gpiosim_chip_name sim0)" 0=1 - - gpiosim_check_value sim0 0 1 - - coproc_tool_kill -SIGINT - coproc_tool_wait - - test "$status" -eq "0" -} - -@test "gpioset: set some lines and wait with --mode=time" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpioset --mode=time --sec=1 --usec=200000 \ - "$(gpiosim_chip_name sim0)" 0=1 5=0 7=1 - - gpiosim_check_value sim0 0 1 - gpiosim_check_value sim0 5 0 - gpiosim_check_value sim0 7 1 - - coproc_tool_wait - - test "$status" -eq "0" -} - -@test "gpioset: no arguments" { - run_tool gpioset - - test "$status" -eq "1" - output_regex_match ".*gpiochip must be specified" -} - -@test "gpioset: no lines specified" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset "$(gpiosim_chip_name sim1)" - - test "$status" -eq "1" - output_regex_match ".*at least one GPIO line offset to value mapping must be specified" -} - -@test "gpioset: too many lines specified" { - gpiosim_chip sim0 num_lines=4 - - run_tool gpioset "$(gpiosim_chip_name sim0)" 0=1 1=1 2=1 3=1 4=1 5=1 - - test "$status" -eq "1" - output_regex_match ".*unable to request lines.*" -} - -@test "gpioset: use --sec without --mode=time" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset --mode=exit --sec=1 "$(gpiosim_chip_name sim0)" 0=1 - - test "$status" -eq "1" - output_regex_match ".*can't specify wait time in this mode" -} - -@test "gpioset: use --usec without --mode=time" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset --mode=exit --usec=1 "$(gpiosim_chip_name sim1)" 0=1 - - test "$status" -eq "1" - output_regex_match ".*can't specify wait time in this mode" -} - -@test "gpioset: default mode" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset "$(gpiosim_chip_name sim0)" 0=1 - - test "$status" -eq "0" -} - -@test "gpioset: invalid mapping" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset "$(gpiosim_chip_name sim1)" 0=c - - test "$status" -eq "1" - output_regex_match ".*invalid offset<->value mapping" -} - -@test "gpioset: invalid value" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset "$(gpiosim_chip_name sim1)" 0=3 - - test "$status" -eq "1" - output_regex_match ".*value must be 0 or 1" -} - -@test "gpioset: invalid offset" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset "$(gpiosim_chip_name sim1)" 4000000000=0 - - test "$status" -eq "1" - output_regex_match ".*invalid offset" -} - -@test "gpioset: invalid bias" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset --bias=bad "$(gpiosim_chip_name sim1)" 0=1 1=1 - - test "$status" -eq "1" - output_regex_match ".*invalid bias.*" -} - -@test "gpioset: invalid drive" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset --drive=bad "$(gpiosim_chip_name sim1)" 0=1 1=1 - - test "$status" -eq "1" - output_regex_match ".*invalid drive.*" -} - -@test "gpioset: daemonize in invalid mode" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset --background "$(gpiosim_chip_name sim1)" 0=1 - - test "$status" -eq "1" - output_regex_match ".*can't daemonize in this mode" -} - -@test "gpioset: same line twice" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpioset "$(gpiosim_chip_name sim0)" 0=1 0=1 - - test "$status" -eq "1" - output_regex_match ".*offsets must be unique" -} - -# -# gpiomon test cases -# - -@test "gpiomon: single rising edge event" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon --rising-edge "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - output_regex_match \ -"event:\\s+RISING\\s+EDGE\\s+offset:\\s+4\\s+timestamp:\\s+\[\s*[0-9]+\.[0-9]+\]" -} - -@test "gpiomon: single falling edge event" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon --falling-edge "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - gpiosim_set_pull sim0 4 pull-down - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - output_regex_match \ -"event:\\s+FALLING\\s+EDGE\\s+offset:\\s+4\\s+timestamp:\\s+\[\s*[0-9]+\.[0-9]+\]" -} - -@test "gpiomon: single falling edge event (pull-up)" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 4 pull-down - - coproc_run_tool gpiomon --bias=pull-up "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-down - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - output_regex_match \ -"event:\\s+FALLING\\s+EDGE\\s+offset:\\s+4\\s+timestamp:\\s+\[\s*[0-9]+\.[0-9]+\]" -} - -@test "gpiomon: single rising edge event (pull-down)" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 4 pull-up - - coproc_run_tool gpiomon --bias=pull-down "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - output_regex_match \ -"event:\\s+RISING\\s+EDGE\\s+offset:\\s+4\\s+timestamp:\\s+\[\s*[0-9]+\.[0-9]+\]" -} - -@test "gpiomon: single rising edge event (active-low)" { - gpiosim_chip sim0 num_lines=8 - - gpiosim_set_pull sim0 4 pull-up - - coproc_run_tool gpiomon --rising-edge --active-low "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-down - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - output_regex_match \ -"event:\\s+RISING\\s+EDGE\\s+offset:\\s+4\\s+timestamp:\\s+\[\s*[0-9]+\.[0-9]+\]" -} - -@test "gpiomon: single rising edge event (silent mode)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon --rising-edge --silent "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test -z "$output" -} - -@test "gpiomon: four alternating events" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon --num-events=4 "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - gpiosim_set_pull sim0 4 pull-down - sleep 0.2 - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - gpiosim_set_pull sim0 4 pull-down - sleep 0.2 - - coproc_tool_wait - - test "$status" -eq "0" - output_regex_match \ -"event\\:\\s+FALLING\\s+EDGE\\s+offset\\:\\s+4\\s+timestamp:\\s+\\[\s*[0-9]+\\.[0-9]+\\]" - output_regex_match \ -"event\\:\\s+RISING\\s+EDGE\\s+offset\\:\\s+4\\s+timestamp:\\s+\\[\s*[0-9]+\\.[0-9]+\\]" -} - -@test "gpiomon: exit after SIGINT" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "$(gpiosim_chip_name sim0)" 4 - - coproc_tool_kill -SIGINT - coproc_tool_wait - - test "$status" -eq "0" - test -z "$output" -} - -@test "gpiomon: exit after SIGTERM" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "$(gpiosim_chip_name sim0)" 4 - - coproc_tool_kill -SIGTERM - coproc_tool_wait - - test "$status" -eq "0" - test -z "$output" -} - -@test "gpiomon: both event flags" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon --falling-edge --rising-edge "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - gpiosim_set_pull sim0 4 pull-down - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - output_regex_match \ -"event\\:\\s+FALLING\\s+EDGE\\s+offset\\:\\s+4\\s+timestamp:\\s+\\[\s*[0-9]+\\.[0-9]+\\]" - output_regex_match \ -"event\\:\\s+RISING\\s+EDGE\\s+offset\\:\\s+4\\s+timestamp:\\s+\\[\s*[0-9]+\\.[0-9]+\\]" -} - -@test "gpiomon: watch multiple lines" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon --format=%o "$(gpiosim_chip_name sim0)" 1 2 3 4 5 - - gpiosim_set_pull sim0 2 pull-up - gpiosim_set_pull sim0 3 pull-up - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test "${lines[0]}" = "2" - test "${lines[1]}" = "3" - test "${lines[2]}" = "4" -} - -@test "gpiomon: watch multiple lines (lines in mixed-up order)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon --format=%o "$(gpiosim_chip_name sim0)" 5 2 7 1 6 - - gpiosim_set_pull sim0 2 pull-up - gpiosim_set_pull sim0 1 pull-up - gpiosim_set_pull sim0 6 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test "${lines[0]}" = "2" - test "${lines[1]}" = "1" - test "${lines[2]}" = "6" -} - -@test "gpiomon: same line twice" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpiomon "$(gpiosim_chip_name sim0)" 0 0 - - test "$status" -eq "1" - output_regex_match ".*offsets must be unique" -} - -@test "gpiomon: no arguments" { - run_tool gpiomon - - test "$status" -eq "1" - output_regex_match ".*gpiochip must be specified" -} - -@test "gpiomon: line not specified" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpiomon "$(gpiosim_chip_name sim0)" - - test "$status" -eq "1" - output_regex_match ".*GPIO line offset must be specified" -} - -@test "gpiomon: line out of range" { - gpiosim_chip sim0 num_lines=4 - - run_tool gpiomon "$(gpiosim_chip_name sim0)" 5 - - test "$status" -eq "1" - output_regex_match ".*unable to request lines" -} - -@test "gpiomon: invalid bias" { - gpiosim_chip sim0 num_lines=8 - - run_tool gpiomon --bias=bad "$(gpiosim_chip_name sim0)" 0 1 - - test "$status" -eq "1" - output_regex_match ".*invalid bias.*" -} - -@test "gpiomon: custom format (event type + offset)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "--format=%e %o" "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test "$output" = "1 4" -} - -@test "gpiomon: custom format (event type + offset joined)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "--format=%e%o" "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test "$output" = "14" -} - -@test "gpiomon: custom format (timestamp)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "--format=%e %o %s.%n" "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - output_regex_match "1 4 [0-9]+\\.[0-9]+" -} - -@test "gpiomon: custom format (double percent sign)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "--format=%%" "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test "$output" = "%" -} - -@test "gpiomon: custom format (double percent sign + event type specifier)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "--format=%%e" "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test "$output" = "%e" -} - -@test "gpiomon: custom format (single percent sign)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "--format=%" "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test "$output" = "%" -} - -@test "gpiomon: custom format (single percent sign between other characters)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "--format=foo % bar" "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test "$output" = "foo % bar" -} - -@test "gpiomon: custom format (unknown specifier)" { - gpiosim_chip sim0 num_lines=8 - - coproc_run_tool gpiomon "--format=%x" "$(gpiosim_chip_name sim0)" 4 - - gpiosim_set_pull sim0 4 pull-up - sleep 0.2 - - coproc_tool_kill - coproc_tool_wait - - test "$status" -eq "0" - test "$output" = "%x" -} diff --git a/tools/gpiodetect.c b/tools/gpiodetect.c index 8f6e8b3..30bde32 100644 --- a/tools/gpiodetect.c +++ b/tools/gpiodetect.c @@ -11,82 +11,3 @@ #include "tools-common.h" -static const struct option longopts[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'v' }, - { GETOPT_NULL_LONGOPT }, -}; - -static const char *const shortopts = "+hv"; - -static void print_help(void) -{ - printf("Usage: %s [OPTIONS]\n", get_progname()); - printf("\n"); - printf("List all GPIO chips, print their labels and number of GPIO lines.\n"); - printf("\n"); - printf("Options:\n"); - printf(" -h, --help:\t\tdisplay this message and exit\n"); - printf(" -v, --version:\tdisplay the version and exit\n"); -} - -int main(int argc, char **argv) -{ - int optc, opti, num_chips, i; - struct gpiod_chip *chip; - struct gpiod_chip_info *info; - struct dirent **entries; - - for (;;) { - optc = getopt_long(argc, argv, shortopts, longopts, &opti); - if (optc < 0) - break; - - switch (optc) { - case 'h': - print_help(); - return EXIT_SUCCESS; - case 'v': - print_version(); - return EXIT_SUCCESS; - case '?': - die("try %s --help", get_progname()); - default: - abort(); - } - } - - argc -= optind; - argv += optind; - - if (argc > 0) - die("unrecognized argument: %s", argv[0]); - - num_chips = scandir("/dev/", &entries, chip_dir_filter, alphasort); - if (num_chips < 0) - die_perror("unable to scan /dev"); - - for (i = 0; i < num_chips; i++) { - chip = chip_open_by_name(entries[i]->d_name); - if (!chip) - die_perror("unable to open %s", entries[i]->d_name); - - info = gpiod_chip_get_info(chip); - if (!info) - die_perror("unable to get info for %s", entries[i]->d_name); - - - printf("%s [%s] (%zu lines)\n", - gpiod_chip_info_get_name(info), - gpiod_chip_info_get_label(info), - gpiod_chip_info_get_num_lines(info)); - - gpiod_chip_info_free(info); - gpiod_chip_close(chip); - free(entries[i]); - } - - free(entries); - - return EXIT_SUCCESS; -} diff --git a/tools/gpiofind.c b/tools/gpiofind.c deleted file mode 100644 index 03b15c9..0000000 --- a/tools/gpiofind.c +++ /dev/null @@ -1,93 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -// SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski - -#include -#include -#include -#include -#include -#include -#include - -#include "tools-common.h" - -static const struct option longopts[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'v' }, - { GETOPT_NULL_LONGOPT }, -}; - -static const char *const shortopts = "+hv"; - -static void print_help(void) -{ - printf("Usage: %s [OPTIONS] \n", get_progname()); - printf("\n"); - printf("Find a GPIO line by name. The output of this command can be used as input for gpioget/set.\n"); - printf("\n"); - printf("Options:\n"); - printf(" -h, --help:\t\tdisplay this message and exit\n"); - printf(" -v, --version:\tdisplay the version and exit\n"); -} - -int main(int argc, char **argv) -{ - int i, num_chips, optc, opti, offset; - struct gpiod_chip *chip; - struct gpiod_chip_info *info; - struct dirent **entries; - - for (;;) { - optc = getopt_long(argc, argv, shortopts, longopts, &opti); - if (optc < 0) - break; - - switch (optc) { - case 'h': - print_help(); - return EXIT_SUCCESS; - case 'v': - print_version(); - return EXIT_SUCCESS; - case '?': - die("try %s --help", get_progname()); - default: - abort(); - } - } - - argc -= optind; - argv += optind; - - if (argc != 1) - die("exactly one GPIO line name must be specified"); - - num_chips = scandir("/dev/", &entries, chip_dir_filter, alphasort); - if (num_chips < 0) - die_perror("unable to scan /dev"); - - for (i = 0; i < num_chips; i++) { - chip = chip_open_by_name(entries[i]->d_name); - if (!chip) { - if (errno == EACCES) - continue; - - die_perror("unable to open %s", entries[i]->d_name); - } - - offset = gpiod_chip_get_line_offset_from_name(chip, argv[0]); - if (offset >= 0) { - info = gpiod_chip_get_info(chip); - if (!info) - die_perror("unable to get info for %s", entries[i]->d_name); - - printf("%s %u\n", - gpiod_chip_info_get_name(info), offset); - gpiod_chip_info_free(info); - gpiod_chip_close(chip); - return EXIT_SUCCESS; - } - } - - return EXIT_FAILURE; -} diff --git a/tools/gpioget.c b/tools/gpioget.c index b68212d..1b3e666 100644 --- a/tools/gpioget.c +++ b/tools/gpioget.c @@ -10,156 +10,3 @@ #include "tools-common.h" -static const struct option longopts[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'v' }, - { "active-low", no_argument, NULL, 'l' }, - { "dir-as-is", no_argument, NULL, 'n' }, - { "bias", required_argument, NULL, 'B' }, - { GETOPT_NULL_LONGOPT }, -}; - -static const char *const shortopts = "+hvlnB:"; - -static void print_help(void) -{ - printf("Usage: %s [OPTIONS] ...\n", - get_progname()); - printf("\n"); - printf("Read line value(s) from a GPIO chip\n"); - printf("\n"); - printf("Options:\n"); - printf(" -h, --help:\t\tdisplay this message and exit\n"); - printf(" -v, --version:\tdisplay the version and exit\n"); - printf(" -l, --active-low:\tset the line active state to low\n"); - printf(" -n, --dir-as-is:\tdon't force-reconfigure line direction\n"); - printf(" -B, --bias=[as-is|disable|pull-down|pull-up] (defaults to 'as-is'):\n"); - printf(" set the line bias\n"); - printf("\n"); - print_bias_help(); -} - -int main(int argc, char **argv) -{ - int direction = GPIOD_LINE_DIRECTION_INPUT; - int optc, opti, bias = 0, ret, *values; - struct gpiod_line_settings *settings; - struct gpiod_request_config *req_cfg; - struct gpiod_line_request *request; - struct gpiod_line_config *line_cfg; - struct gpiod_chip *chip; - bool active_low = false; - unsigned int *offsets; - size_t i, num_lines; - char *device, *end; - - for (;;) { - optc = getopt_long(argc, argv, shortopts, longopts, &opti); - if (optc < 0) - break; - - switch (optc) { - case 'h': - print_help(); - return EXIT_SUCCESS; - case 'v': - print_version(); - return EXIT_SUCCESS; - case 'l': - active_low = true; - break; - case 'n': - direction = GPIOD_LINE_DIRECTION_AS_IS; - break; - case 'B': - bias = parse_bias(optarg); - break; - case '?': - die("try %s --help", get_progname()); - default: - abort(); - } - } - - argc -= optind; - argv += optind; - - if (argc < 1) - die("gpiochip must be specified"); - - if (argc < 2) - die("at least one GPIO line offset must be specified"); - - device = argv[0]; - num_lines = argc - 1; - - offsets = calloc(num_lines, sizeof(*offsets)); - values = calloc(num_lines, sizeof(*values)); - if (!offsets || ! values) - die("out of memory"); - - for (i = 0; i < num_lines; i++) { - offsets[i] = strtoul(argv[i + 1], &end, 10); - if (*end != '\0' || offsets[i] > INT_MAX) - die("invalid GPIO offset: %s", argv[i + 1]); - } - - if (has_duplicate_offsets(num_lines, offsets)) - die("offsets must be unique"); - - chip = chip_open_lookup(device); - if (!chip) - die_perror("unable to open %s", device); - - settings = gpiod_line_settings_new(); - if (!settings) - die_perror("unable to allocate line settings"); - - gpiod_line_settings_set_direction(settings, direction); - - if (bias) - gpiod_line_settings_set_bias(settings, bias); - - if (active_low) - gpiod_line_settings_set_active_low(settings, active_low); - - req_cfg = gpiod_request_config_new(); - if (!req_cfg) - die_perror("unable to allocate the request config structure"); - - gpiod_request_config_set_consumer(req_cfg, "gpioget"); - - line_cfg = gpiod_line_config_new(); - if (!line_cfg) - die_perror("unable to allocate the line config structure"); - - ret = gpiod_line_config_add_line_settings(line_cfg, offsets, - num_lines, settings); - if (ret) - die_perror("unable to add line settings"); - - request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); - if (!request) - die_perror("unable to request lines"); - - ret = gpiod_line_request_get_values(request, values); - if (ret) - die_perror("unable to read GPIO line values"); - - for (i = 0; i < num_lines; i++) { - printf("%d", values[i]); - if (i != num_lines - 1) - printf(" "); - } - printf("\n"); - - gpiod_line_request_release(request); - gpiod_request_config_free(req_cfg); - gpiod_line_config_free(line_cfg); - gpiod_line_settings_free(settings); - gpiod_chip_close(chip); - free(offsets); - free(values); - - return EXIT_SUCCESS; -} diff --git a/tools/gpioinfo.c b/tools/gpioinfo.c index fbe2a13..ae368fa 100644 --- a/tools/gpioinfo.c +++ b/tools/gpioinfo.c @@ -12,243 +12,3 @@ #include "tools-common.h" -typedef bool (*is_set_func)(struct gpiod_line_info *); - -struct flag { - const char *name; - is_set_func is_set; -}; - -static bool line_bias_is_pullup(struct gpiod_line_info *info) -{ - return gpiod_line_info_get_bias(info) == GPIOD_LINE_BIAS_PULL_UP; -} - -static bool line_bias_is_pulldown(struct gpiod_line_info *info) -{ - return gpiod_line_info_get_bias(info) == GPIOD_LINE_BIAS_PULL_DOWN; -} - -static bool line_bias_is_disabled(struct gpiod_line_info *info) -{ - return gpiod_line_info_get_bias(info) == GPIOD_LINE_BIAS_DISABLED; -} - -static bool line_drive_is_open_drain(struct gpiod_line_info *info) -{ - return gpiod_line_info_get_drive(info) == GPIOD_LINE_DRIVE_OPEN_DRAIN; -} - -static bool line_drive_is_open_source(struct gpiod_line_info *info) -{ - return gpiod_line_info_get_drive(info) == GPIOD_LINE_DRIVE_OPEN_SOURCE; -} - -static const struct flag flags[] = { - { - .name = "used", - .is_set = gpiod_line_info_is_used, - }, - { - .name = "open-drain", - .is_set = line_drive_is_open_drain, - }, - { - .name = "open-source", - .is_set = line_drive_is_open_source, - }, - { - .name = "pull-up", - .is_set = line_bias_is_pullup, - }, - { - .name = "pull-down", - .is_set = line_bias_is_pulldown, - }, - { - .name = "bias-disabled", - .is_set = line_bias_is_disabled, - }, -}; - -static const struct option longopts[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'v' }, - { GETOPT_NULL_LONGOPT }, -}; - -static const char *const shortopts = "+hv"; - -static void print_help(void) -{ - printf("Usage: %s [OPTIONS] ...\n", get_progname()); - printf("\n"); - printf("Print information about all lines of the specified GPIO chip(s) (or all gpiochips if none are specified).\n"); - printf("\n"); - printf("Options:\n"); - printf(" -h, --help:\t\tdisplay this message and exit\n"); - printf(" -v, --version:\tdisplay the version and exit\n"); -} - -static PRINTF(3, 4) void prinfo(bool *of, - unsigned int prlen, const char *fmt, ...) -{ - char *buf, *buffmt = NULL; - size_t len; - va_list va; - int rv; - - va_start(va, fmt); - rv = vasprintf(&buf, fmt, va); - va_end(va); - if (rv < 0) - die("vasprintf: %s\n", strerror(errno)); - - len = strlen(buf) - 1; - - if (len >= prlen || *of) { - *of = true; - printf("%s", buf); - } else { - rv = asprintf(&buffmt, "%%%us", prlen); - if (rv < 0) - die("asprintf: %s\n", strerror(errno)); - - printf(buffmt, buf); - } - - free(buf); - if (fmt) - free(buffmt); -} - -static void list_lines(struct gpiod_chip *chip) -{ - bool flag_printed, of, active_low; - struct gpiod_chip_info *chip_info; - struct gpiod_line_info *info; - const char *name, *consumer; - size_t i, offset, num_lines; - int direction; - - chip_info = gpiod_chip_get_info(chip); - if (!chip_info) - die_perror("unable to retrieve the chip info from chip"); - - num_lines = gpiod_chip_info_get_num_lines(chip_info); - printf("%s - %zu lines:\n", - gpiod_chip_info_get_name(chip_info), num_lines); - - for (offset = 0; offset < num_lines; offset++) { - info = gpiod_chip_get_line_info(chip, offset); - if (!info) - die_perror("unable to retrieve the line info from chip"); - name = gpiod_line_info_get_name(info); - consumer = gpiod_line_info_get_consumer(info); - direction = gpiod_line_info_get_direction(info); - active_low = gpiod_line_info_is_active_low(info); - - of = false; - - printf("\tline "); - prinfo(&of, 3, "%zu", offset); - printf(": "); - - name ? prinfo(&of, 12, "\"%s\"", name) - : prinfo(&of, 12, "unnamed"); - printf(" "); - - if (!gpiod_line_info_is_used(info)) - prinfo(&of, 12, "unused"); - else - consumer ? prinfo(&of, 12, "\"%s\"", consumer) - : prinfo(&of, 12, "kernel"); - - printf(" "); - - prinfo(&of, 8, "%s ", direction == GPIOD_LINE_DIRECTION_INPUT - ? "input" : "output"); - prinfo(&of, 13, "%s ", - active_low ? "active-low" : "active-high"); - - flag_printed = false; - for (i = 0; i < ARRAY_SIZE(flags); i++) { - if (flags[i].is_set(info)) { - if (flag_printed) - printf(" "); - else - printf("["); - printf("%s", flags[i].name); - flag_printed = true; - } - } - if (flag_printed) - printf("]"); - - printf("\n"); - - gpiod_line_info_free(info); - } - gpiod_chip_info_free(chip_info); -} - -int main(int argc, char **argv) -{ - int num_chips, i, optc, opti; - struct gpiod_chip *chip; - struct dirent **entries; - - for (;;) { - optc = getopt_long(argc, argv, shortopts, longopts, &opti); - if (optc < 0) - break; - - switch (optc) { - case 'h': - print_help(); - return EXIT_SUCCESS; - case 'v': - print_version(); - return EXIT_SUCCESS; - case '?': - die("try %s --help", get_progname()); - default: - abort(); - } - } - - argc -= optind; - argv += optind; - - if (argc == 0) { - num_chips = scandir("/dev/", &entries, - chip_dir_filter, alphasort); - if (num_chips < 0) - die_perror("unable to scan /dev"); - - for (i = 0; i < num_chips; i++) { - chip = chip_open_by_name(entries[i]->d_name); - if (!chip) - die_perror("unable to open %s", - entries[i]->d_name); - - list_lines(chip); - - gpiod_chip_close(chip); - free(entries[i]); - } - free(entries); - } else { - for (i = 0; i < argc; i++) { - chip = chip_open_lookup(argv[i]); - if (!chip) - die_perror("looking up chip %s", argv[i]); - - list_lines(chip); - - gpiod_chip_close(chip); - } - } - - return EXIT_SUCCESS; -} diff --git a/tools/gpiomon.c b/tools/gpiomon.c index dff12ea..6fa19b6 100644 --- a/tools/gpiomon.c +++ b/tools/gpiomon.c @@ -17,317 +17,3 @@ #define EVENT_BUF_SIZE 32 -static const struct option longopts[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'v' }, - { "active-low", no_argument, NULL, 'l' }, - { "bias", required_argument, NULL, 'B' }, - { "num-events", required_argument, NULL, 'n' }, - { "silent", no_argument, NULL, 's' }, - { "rising-edge", no_argument, NULL, 'r' }, - { "falling-edge", no_argument, NULL, 'f' }, - { "line-buffered", no_argument, NULL, 'b' }, - { "format", required_argument, NULL, 'F' }, - { GETOPT_NULL_LONGOPT }, -}; - -static const char *const shortopts = "+hvlB:n:srfbF:"; - -static void print_help(void) -{ - printf("Usage: %s [OPTIONS] ...\n", - get_progname()); - printf("\n"); - printf("Wait for events on GPIO lines and print them to standard output\n"); - printf("\n"); - printf("Options:\n"); - printf(" -h, --help:\t\tdisplay this message and exit\n"); - printf(" -v, --version:\tdisplay the version and exit\n"); - printf(" -l, --active-low:\tset the line active state to low\n"); - printf(" -B, --bias=[as-is|disable|pull-down|pull-up] (defaults to 'as-is'):\n"); - printf(" set the line bias\n"); - printf(" -n, --num-events=NUM:\texit after processing NUM events\n"); - printf(" -s, --silent:\t\tdon't print event info\n"); - printf(" -r, --rising-edge:\tonly process rising edge events\n"); - printf(" -f, --falling-edge:\tonly process falling edge events\n"); - printf(" -b, --line-buffered:\tset standard output as line buffered\n"); - printf(" -F, --format=FMT\tspecify custom output format\n"); - printf("\n"); - print_bias_help(); - printf("\n"); - printf("Format specifiers:\n"); - printf(" %%o: GPIO line offset\n"); - printf(" %%e: event type (0 - falling edge, 1 rising edge)\n"); - printf(" %%s: seconds part of the event timestamp\n"); - printf(" %%n: nanoseconds part of the event timestamp\n"); -} - -struct mon_ctx { - unsigned int offset; - bool silent; - char *fmt; -}; - -static void event_print_custom(unsigned int offset, uint64_t timeout, - int event_type, struct mon_ctx *ctx) -{ - char *prev, *curr, fmt; - - for (prev = curr = ctx->fmt;;) { - curr = strchr(curr, '%'); - if (!curr) { - fputs(prev, stdout); - break; - } - - if (prev != curr) - fwrite(prev, curr - prev, 1, stdout); - - fmt = *(curr + 1); - - switch (fmt) { - case 'o': - printf("%u", offset); - break; - case 'e': - if (event_type == GPIOD_EDGE_EVENT_RISING_EDGE) - fputc('1', stdout); - else - fputc('0', stdout); - break; - case 's': - printf("%"PRIu64, timeout / 1000000000); - break; - case 'n': - printf("%"PRIu64, timeout % 1000000000); - break; - case '%': - fputc('%', stdout); - break; - case '\0': - fputc('%', stdout); - goto end; - default: - fwrite(curr, 2, 1, stdout); - break; - } - - curr += 2; - prev = curr; - } - -end: - fputc('\n', stdout); -} - -static void event_print_human_readable(unsigned int offset, - uint64_t timeout, int event_type) -{ - char *evname; - - if (event_type == GPIOD_EDGE_EVENT_RISING_EDGE) - evname = " RISING EDGE"; - else - evname = "FALLING EDGE"; - - printf("event: %s offset: %u timestamp: [%8"PRIu64".%09"PRIu64"]\n", - evname, offset, timeout / 1000000000, timeout % 1000000000); -} - -static void handle_event(unsigned int line_offset, unsigned int event_type, - uint64_t timestamp, struct mon_ctx *ctx) -{ - if (!ctx->silent) { - if (ctx->fmt) - event_print_custom(line_offset, timestamp, - event_type, ctx); - else - event_print_human_readable(line_offset, - timestamp, event_type); - } -} - -static void handle_signal(int signum UNUSED) -{ - exit(EXIT_SUCCESS); -} - -int main(int argc, char **argv) -{ - bool watch_rising = false, watch_falling = false, active_low = false; - size_t num_lines = 0, events_wanted = 0, events_done = 0; - struct gpiod_edge_event_buffer *event_buffer; - int optc, opti, ret, i, edge, bias = 0; - uint64_t timeout = 10 * 1000000000LLU; - struct gpiod_line_settings *settings; - struct gpiod_request_config *req_cfg; - struct gpiod_line_request *request; - struct gpiod_line_config *line_cfg; - unsigned int offsets[64], offset; - struct gpiod_edge_event *event; - struct gpiod_chip *chip; - struct mon_ctx ctx; - char *end; - - /* - * FIXME: use signalfd once the API has been converted to using a single file - * descriptor as provided by uAPI v2. - */ - signal(SIGINT, handle_signal); - signal(SIGTERM, handle_signal); - - memset(&ctx, 0, sizeof(ctx)); - - for (;;) { - optc = getopt_long(argc, argv, shortopts, longopts, &opti); - if (optc < 0) - break; - - switch (optc) { - case 'h': - print_help(); - return EXIT_SUCCESS; - case 'v': - print_version(); - return EXIT_SUCCESS; - case 'l': - active_low = true; - break; - case 'B': - bias = parse_bias(optarg); - break; - case 'n': - events_wanted = strtoul(optarg, &end, 10); - if (*end != '\0') - die("invalid number: %s", optarg); - break; - case 's': - ctx.silent = true; - break; - case 'r': - watch_rising = true; - break; - case 'f': - watch_falling = true; - break; - case 'b': - setlinebuf(stdout); - break; - case 'F': - ctx.fmt = optarg; - break; - case '?': - die("try %s --help", get_progname()); - default: - abort(); - } - } - - argc -= optind; - argv += optind; - - if (watch_rising && !watch_falling) - edge = GPIOD_LINE_EDGE_RISING; - else if (watch_falling && !watch_rising) - edge = GPIOD_LINE_EDGE_FALLING; - else - edge = GPIOD_LINE_EDGE_BOTH; - - if (argc < 1) - die("gpiochip must be specified"); - - if (argc < 2) - die("at least one GPIO line offset must be specified"); - - if (argc > 65) - die("too many offsets given"); - - for (i = 1; i < argc; i++) { - offset = strtoul(argv[i], &end, 10); - if (*end != '\0' || offset > INT_MAX) - die("invalid GPIO offset: %s", argv[i]); - - offsets[i - 1] = offset; - num_lines++; - } - - if (has_duplicate_offsets(num_lines, offsets)) - die("offsets must be unique"); - - chip = chip_open_lookup(argv[0]); - if (!chip) - die_perror("unable to open %s", argv[0]); - - settings = gpiod_line_settings_new(); - if (!settings) - die_perror("unable to allocate line settings"); - - if (bias) - gpiod_line_settings_set_bias(settings, bias); - if (active_low) - gpiod_line_settings_set_active_low(settings, active_low); - gpiod_line_settings_set_edge_detection(settings, edge); - - req_cfg = gpiod_request_config_new(); - if (!req_cfg) - die_perror("unable to allocate the request config structure"); - - gpiod_request_config_set_consumer(req_cfg, "gpiomon"); - - line_cfg = gpiod_line_config_new(); - if (!line_cfg) - die_perror("unable to allocate the line config structure"); - - ret = gpiod_line_config_add_line_settings(line_cfg, offsets, - num_lines, settings); - if (ret) - die_perror("unable to add line settings"); - - request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); - if (!request) - die_perror("unable to request lines"); - - event_buffer = gpiod_edge_event_buffer_new(EVENT_BUF_SIZE); - if (!event_buffer) - die_perror("unable to allocate the line event buffer"); - - for (;;) { - ret = gpiod_line_request_wait_edge_event(request, timeout); - if (ret < 0) - die_perror("error waiting for events"); - if (ret == 0) - continue; - - ret = gpiod_line_request_read_edge_event(request, event_buffer, - EVENT_BUF_SIZE); - if (ret < 0) - die_perror("error reading line events"); - - for (i = 0; i < ret; i++) { - event = gpiod_edge_event_buffer_get_event(event_buffer, - i); - if (!event) - die_perror("unable to retrieve the event from the buffer"); - - handle_event(gpiod_edge_event_get_line_offset(event), - gpiod_edge_event_get_event_type(event), - gpiod_edge_event_get_timestamp_ns(event), - &ctx); - - events_done++; - - if (events_wanted && events_done >= events_wanted) - goto done; - } - } - -done: - gpiod_edge_event_buffer_free(event_buffer); - gpiod_line_request_release(request); - gpiod_request_config_free(req_cfg); - gpiod_line_config_free(line_cfg); - gpiod_line_settings_free(settings); - gpiod_chip_close(chip); - - return EXIT_SUCCESS; -} diff --git a/tools/gpioset.c b/tools/gpioset.c index 290d1a3..0e3a1e4 100644 --- a/tools/gpioset.c +++ b/tools/gpioset.c @@ -14,333 +14,3 @@ #include "tools-common.h" -static const struct option longopts[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'v' }, - { "active-low", no_argument, NULL, 'l' }, - { "bias", required_argument, NULL, 'B' }, - { "drive", required_argument, NULL, 'D' }, - { "mode", required_argument, NULL, 'm' }, - { "sec", required_argument, NULL, 's' }, - { "usec", required_argument, NULL, 'u' }, - { "background", no_argument, NULL, 'b' }, - { GETOPT_NULL_LONGOPT }, -}; - -static const char *const shortopts = "+hvlB:D:m:s:u:b"; - -static void print_help(void) -{ - printf("Usage: %s [OPTIONS] = = ...\n", - get_progname()); - printf("\n"); - printf("Set GPIO line values of a GPIO chip and maintain the state until the process exits\n"); - printf("\n"); - printf("Options:\n"); - printf(" -h, --help:\t\tdisplay this message and exit\n"); - printf(" -v, --version:\tdisplay the version and exit\n"); - printf(" -l, --active-low:\tset the line active state to low\n"); - printf(" -B, --bias=[as-is|disable|pull-down|pull-up] (defaults to 'as-is'):\n"); - printf(" set the line bias\n"); - printf(" -D, --drive=[push-pull|open-drain|open-source] (defaults to 'push-pull'):\n"); - printf(" set the line drive mode\n"); - printf(" -m, --mode=[exit|wait|time|signal] (defaults to 'exit'):\n"); - printf(" tell the program what to do after setting values\n"); - printf(" -s, --sec=SEC:\tspecify the number of seconds to wait (only valid for --mode=time)\n"); - printf(" -u, --usec=USEC:\tspecify the number of microseconds to wait (only valid for --mode=time)\n"); - printf(" -b, --background:\tafter setting values: detach from the controlling terminal\n"); - printf("\n"); - print_bias_help(); - printf("\n"); - printf("Drives:\n"); - printf(" push-pull:\tdrive the line both high and low\n"); - printf(" open-drain:\tdrive the line low or go high impedance\n"); - printf(" open-source:\tdrive the line high or go high impedance\n"); - printf("\n"); - printf("Modes:\n"); - printf(" exit:\t\tset values and exit immediately\n"); - printf(" wait:\t\tset values and wait for user to press ENTER\n"); - printf(" time:\t\tset values and sleep for a specified amount of time\n"); - printf(" signal:\tset values and wait for SIGINT or SIGTERM\n"); - printf("\n"); - printf("Note: the state of a GPIO line controlled over the character device reverts to default\n"); - printf("when the last process referencing the file descriptor representing the device file exits.\n"); - printf("This means that it's wrong to run gpioset, have it exit and expect the line to continue\n"); - printf("being driven high or low. It may happen if given pin is floating but it must be interpreted\n"); - printf("as undefined behavior.\n"); -} - -struct callback_data { - /* Replace with a union once we have more modes using callback data. */ - struct timeval tv; - bool daemonize; -}; - -static void maybe_daemonize(bool daemonize) -{ - int rv; - - if (daemonize) { - rv = daemon(0, 0); - if (rv < 0) - die("unable to daemonize: %s", strerror(errno)); - } -} - -static void wait_enter(void *data UNUSED) -{ - getchar(); -} - -static void wait_time(void *data) -{ - struct callback_data *cbdata = data; - - maybe_daemonize(cbdata->daemonize); - select(0, NULL, NULL, NULL, &cbdata->tv); -} - -static void wait_signal(void *data) -{ - struct callback_data *cbdata = data; - struct pollfd pfd; - int sigfd, rv; - - sigfd = make_signalfd(); - - memset(&pfd, 0, sizeof(pfd)); - pfd.fd = sigfd; - pfd.events = POLLIN | POLLPRI; - - maybe_daemonize(cbdata->daemonize); - - for (;;) { - rv = poll(&pfd, 1, 1000 /* one second */); - if (rv < 0) - die("error polling for signals: %s", strerror(errno)); - else if (rv > 0) - break; - } - - /* - * Don't bother reading siginfo - it's enough to know that we - * received any signal. - */ - close(sigfd); -} - -enum { - MODE_EXIT = 0, - MODE_WAIT, - MODE_TIME, - MODE_SIGNAL, -}; - -struct mode_mapping { - int id; - const char *name; - void (*callback)(void *); -}; - -static const struct mode_mapping modes[] = { - [MODE_EXIT] = { - .id = MODE_EXIT, - .name = "exit", - .callback = NULL, - }, - [MODE_WAIT] = { - .id = MODE_WAIT, - .name = "wait", - .callback = wait_enter, - }, - [MODE_TIME] = { - .id = MODE_TIME, - .name = "time", - .callback = wait_time, - }, - [MODE_SIGNAL] = { - .id = MODE_SIGNAL, - .name = "signal", - .callback = wait_signal, - }, -}; - -static const struct mode_mapping *parse_mode(const char *mode) -{ - size_t i; - - for (i = 0; i < ARRAY_SIZE(modes); i++) - if (strcmp(mode, modes[i].name) == 0) - return &modes[i]; - - return NULL; -} - -static int parse_drive(const char *option) -{ - if (strcmp(option, "open-drain") == 0) - return GPIOD_LINE_DRIVE_OPEN_DRAIN; - if (strcmp(option, "open-source") == 0) - return GPIOD_LINE_DRIVE_OPEN_SOURCE; - if (strcmp(option, "push-pull") != 0) - die("invalid drive: %s", option); - return 0; -} - -int main(int argc, char **argv) -{ - const struct mode_mapping *mode = &modes[MODE_EXIT]; - int ret, optc, opti, bias = 0, drive = 0, *values; - struct gpiod_line_settings *settings; - struct gpiod_request_config *req_cfg; - struct gpiod_line_request *request; - struct gpiod_line_config *line_cfg; - struct callback_data cbdata; - struct gpiod_chip *chip; - bool active_low = false; - unsigned int *offsets; - size_t i, num_lines; - char *device, *end; - - memset(&cbdata, 0, sizeof(cbdata)); - - for (;;) { - optc = getopt_long(argc, argv, shortopts, longopts, &opti); - if (optc < 0) - break; - - switch (optc) { - case 'h': - print_help(); - return EXIT_SUCCESS; - case 'v': - print_version(); - return EXIT_SUCCESS; - case 'l': - active_low = true; - break; - case 'B': - bias = parse_bias(optarg); - break; - case 'D': - drive = parse_drive(optarg); - break; - case 'm': - mode = parse_mode(optarg); - if (!mode) - die("invalid mode: %s", optarg); - break; - case 's': - cbdata.tv.tv_sec = strtoul(optarg, &end, 10); - if (*end != '\0') - die("invalid time value in seconds: %s", optarg); - break; - case 'u': - cbdata.tv.tv_usec = strtoul(optarg, &end, 10); - if (*end != '\0') - die("invalid time value in microseconds: %s", - optarg); - break; - case 'b': - cbdata.daemonize = true; - break; - case '?': - die("try %s --help", get_progname()); - default: - abort(); - } - } - - argc -= optind; - argv += optind; - - if (mode->id != MODE_TIME && (cbdata.tv.tv_sec || cbdata.tv.tv_usec)) - die("can't specify wait time in this mode"); - - if (mode->id != MODE_SIGNAL && - mode->id != MODE_TIME && - cbdata.daemonize) - die("can't daemonize in this mode"); - - if (argc < 1) - die("gpiochip must be specified"); - - if (argc < 2) - die("at least one GPIO line offset to value mapping must be specified"); - - device = argv[0]; - - num_lines = argc - 1; - - offsets = calloc(num_lines, sizeof(*offsets)); - values = calloc(num_lines, sizeof(*values)); - if (!offsets) - die("out of memory"); - - for (i = 0; i < num_lines; i++) { - ret = sscanf(argv[i + 1], "%u=%d", &offsets[i], &values[i]); - if (ret != 2) - die("invalid offset<->value mapping: %s", argv[i + 1]); - - if (values[i] != 0 && values[i] != 1) - die("value must be 0 or 1: %s", argv[i + 1]); - - if (offsets[i] > INT_MAX) - die("invalid offset: %s", argv[i + 1]); - } - - if (has_duplicate_offsets(num_lines, offsets)) - die("offsets must be unique"); - - chip = chip_open_lookup(device); - if (!chip) - die_perror("unable to open %s", device); - - settings = gpiod_line_settings_new(); - if (!settings) - die_perror("unable to allocate line settings"); - - if (bias) - gpiod_line_settings_set_bias(settings, bias); - if (drive) - gpiod_line_settings_set_drive(settings, drive); - if (active_low) - gpiod_line_settings_set_active_low(settings, active_low); - gpiod_line_settings_set_direction(settings, - GPIOD_LINE_DIRECTION_OUTPUT); - - req_cfg = gpiod_request_config_new(); - if (!req_cfg) - die_perror("unable to allocate the request config structure"); - - gpiod_request_config_set_consumer(req_cfg, "gpioset"); - - line_cfg = gpiod_line_config_new(); - if (!line_cfg) - die_perror("unable to allocate the line config structure"); - - for (i = 0; i < num_lines; i++) { - gpiod_line_settings_set_output_value(settings, values[i]); - - ret = gpiod_line_config_add_line_settings(line_cfg, &offsets[i], - 1, settings); - if (ret) - die_perror("unable to add line settings"); - } - - request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); - if (!request) - die_perror("unable to request lines"); - - if (mode->callback) - mode->callback(&cbdata); - - gpiod_line_request_release(request); - gpiod_request_config_free(req_cfg); - gpiod_line_config_free(line_cfg); - gpiod_line_settings_free(settings); - gpiod_chip_close(chip); - free(offsets); - - return EXIT_SUCCESS; -} diff --git a/tools/tools-common.c b/tools/tools-common.c index 8957293..ea7dfa8 100644 --- a/tools/tools-common.c +++ b/tools/tools-common.c @@ -79,26 +79,6 @@ void print_bias_help(void) printf(" pull-down:\tenable pull-down\n"); } -int make_signalfd(void) -{ - sigset_t sigmask; - int sigfd, rv; - - sigemptyset(&sigmask); - sigaddset(&sigmask, SIGTERM); - sigaddset(&sigmask, SIGINT); - - rv = sigprocmask(SIG_BLOCK, &sigmask, NULL); - if (rv < 0) - die("error masking signals: %s", strerror(errno)); - - sigfd = signalfd(-1, &sigmask, 0); - if (sigfd < 0) - die("error creating signalfd: %s", strerror(errno)); - - return sigfd; -} - int chip_dir_filter(const struct dirent *entry) { bool is_chip; @@ -114,38 +94,6 @@ int chip_dir_filter(const struct dirent *entry) return !!is_chip; } -struct gpiod_chip *chip_open_by_name(const char *name) -{ - struct gpiod_chip *chip; - char *path; - int ret; - - ret = asprintf(&path, "/dev/%s", name); - if (ret < 0) - return NULL; - - chip = gpiod_chip_open(path); - free(path); - - return chip; -} - -static struct gpiod_chip *chip_open_by_number(unsigned int num) -{ - struct gpiod_chip *chip; - char *path; - int ret; - - ret = asprintf(&path, "/dev/gpiochip%u", num); - if (!ret) - return NULL; - - chip = gpiod_chip_open(path); - free(path); - - return chip; -} - static bool isuint(const char *str) { for (; *str && isdigit(*str); str++) @@ -153,32 +101,3 @@ static bool isuint(const char *str) return *str == '\0'; } - -struct gpiod_chip *chip_open_lookup(const char *device) -{ - struct gpiod_chip *chip; - - if (isuint(device)) { - chip = chip_open_by_number(strtoul(device, NULL, 10)); - } else { - if (strncmp(device, "/dev/", 5)) - chip = chip_open_by_name(device); - else - chip = gpiod_chip_open(device); - } - - return chip; -} - -bool has_duplicate_offsets(size_t num_offsets, unsigned int *offsets) -{ - size_t i, j; - - for (i = 0; i < num_offsets; i++) { - for (j = i + 1; j < num_offsets; j++) - if (offsets[i] == offsets[j]) - return true; - } - - return false; -} From patchwork Mon Nov 14 04:00:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 624650 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 E4C3AC433FE for ; Mon, 14 Nov 2022 04:01:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235468AbiKNEBn (ORCPT ); Sun, 13 Nov 2022 23:01:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235714AbiKNEBm (ORCPT ); Sun, 13 Nov 2022 23:01:42 -0500 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CA4612AF0 for ; Sun, 13 Nov 2022 20:01:39 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id i3so9877905pfc.11 for ; Sun, 13 Nov 2022 20:01:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.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=Yg3v9V7X8jcr9yCC9WHqypCO2HDyRav4bS4yT9oOC0U=; b=aYaKl5LlGq0Ju7GsEnxAGPHOLpPWbfIdue0W1tse9wRGLjqjonsSxSIYmyuJ5gdJH2 NnRobQhcyU/iHQ6T+DhPA1NCwDYrRGLTk+l0p2VNUUk23cu0YvIoiNAGHw1FLSNlv5q2 7VG2mVjA+xw6eETAx52W4omUlZN2I0FNDuc0jLFTLU5DNPEg6a1CRnUlo2vD6/Zhg9VV FKyhEVU60j/nn95z2cVhlRNBke4HY/kF/H21lnkwV57NqxV40cLB1RteCX3GdfO3Ott4 n93Rux2uwd0chKfSi/BQzD2CSYmzvwO6JWuijWe4MWt/dVf6X7egyd8olzTTrY6jpJ8w P3BA== 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=Yg3v9V7X8jcr9yCC9WHqypCO2HDyRav4bS4yT9oOC0U=; b=iOzylsOVT1wNp5nf8Oqe0VrSXsDfYsfZBFf1HJtiOfTKwaDStFPJ+S5aNKuBaJHA5Q mEiSnBShilAuMeLKJ/nNYnCLEYlHffn1Ewt4wxovW2MI3YSlPl/urlFU9bxFeXk/aVrM QBEgBRBsRdoHQSQbrNta5U5m63abfoF/p21zzSx3hNwO3IhSqkf9okX1WrwWfKh0/V5C 7Jzu1xVmoA3DC7F8UppXHAslnas2k5/xWxYPTSFlVKRYgjSTasXQ0Vkt5LiE0RHVkt49 L0Ue4pHAggGp2YGN4rszvMYwjqTpmZt99fYwqpa/D0A5xM3gb6Z7qR70SlwNRWiQsPw8 rVOw== X-Gm-Message-State: ANoB5plNKdJn0q+MWxrw9LMK4uPyie+Ry7uYnIbLq791pg+SjCe443EW cxxFV/FNCfxJ/+6zl3PcTU4yA619XMg= X-Google-Smtp-Source: AA0mqf4L6aR174FsHilhr5HiBXp/4MiwOHyGx/WGZq3CquVRgh5+JtVt/ov+qpC8ZCMCglSLW1qYoQ== X-Received: by 2002:a62:b612:0:b0:538:17f5:64e8 with SMTP id j18-20020a62b612000000b0053817f564e8mr12386042pff.11.1668398497054; Sun, 13 Nov 2022 20:01:37 -0800 (PST) Received: from sol.home.arpa (14-200-229-209.tpgi.com.au. [14.200.229.209]) by smtp.gmail.com with ESMTPSA id f11-20020a170902684b00b001801aec1f6bsm6018636pln.141.2022.11.13.20.01.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:01:36 -0800 (PST) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod v2][PATCH v4 2/5] tools: line name focussed rework Date: Mon, 14 Nov 2022 12:00:59 +0800 Message-Id: <20221114040102.66031-3-warthog618@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221114040102.66031-1-warthog618@gmail.com> References: <20221114040102.66031-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Rework the tool suite to support identifying lines by name and to support operating on the GPIO lines available to the user at once, rather than on one particular GPIO chip. All tools, other than gpiodetect, now provide the name to (chip,offset) mapping that was previously only performed by gpiofind. As names are not guaranteed to be unique, a --strict option is provided for all tools to either abort the operation or report all lines with the matching name, as appropriate. By default the tools operate on the first line found with a matching name. Selection of line by (chip,offset) is still supported with a --chip option, though it restricts the scope of the operation to an individual chip. When the --chip option is specified, the lines are assumed to be identified by offset where they parse as an integer, else by name. To cater for the unusual case where a line name parses as an integer, but is different from the offset, the --by-name option forces the lines to be identified by name. The updated tools are intentionally NOT backwardly compatible with the previous tools. Using old command lines with the updated tools will almost certainly fail, though migrating old command lines is generally as simple as adding a '-c' before the chip. In addition the individual tools are modified as follows: gpiodetect: Add the option to select individual chips. gpioinfo: Change the focus from chips to lines, so the scope can be an individual line, a subset of lines, all lines on a particular chip, or all the lines available to the user. For line scope a single line summary is output for each line. For chip scope the existing format displaying a summary of the chip and each of its lines is retained. Line attributes are consolidated into a list format, and are extended to cover all attributes supported by uAPI v2. gpioget: The default output format is becomes line=value, as per the input for gpioset, and the value is reported as active or inactive, rather than 0 or 1. The previous format is available using the --numeric option. Add an optional hold period between requesting a line and reading the value to allow the line to settle once the requested configuration has been applied (e.g. bias). gpiomon: Consolidate the edge options into a single option. Add a debounce period option. Add options to report event times as UTC or localtime. Add format specifiers for GPIO chip path, line name, stringified event type, and event time as a datetime. Rearrange default output format to place fields with more predicable widths to the left, and to separate major field groups with tabs. Lines are identified consistent with the command line. gpioset: Add a hold period option that specifies the minimum period the line value must be held for. This applies to all set options. Support line values specified as active/inactive, on/off and true/false, as well as 1/0. Add a toggle option that specifies a time sequence over which the requested lines should be toggled. If the sequence is 0 terminated then gpioset exits when the sequence completes, else it repeats the sequence. This allows for anything from simple blinkers to bit bashing from the command line. e.g. gpioset -t 500ms LED=on Add an interactive option to provide a shell-like interface to allow manual or scripted manipulation of requested lines. A basic command set allows lines to be get, set, or toggled, and to insert sleeps between operations. Remove the --mode, --sec, and --usec options. The combination of hold period and interactive mode provide functionality equivalent to the old --mode options. By default gpioset now holds the line indefinitely, rather than exiting immediately. The old exit behaviour can be emulated with a "-t 0" option. Signed-off-by: Kent Gibson --- configure.ac | 24 +- tools/Makefile.am | 8 +- tools/gpiodetect.c | 113 +++++- tools/gpioget.c | 225 ++++++++++- tools/gpioinfo.c | 250 +++++++++++- tools/gpiomon.c | 468 +++++++++++++++++++++- tools/gpioset.c | 915 ++++++++++++++++++++++++++++++++++++++++++- tools/tools-common.c | 693 +++++++++++++++++++++++++++++++- tools/tools-common.h | 98 ++++- 9 files changed, 2745 insertions(+), 49 deletions(-) diff --git a/configure.ac b/configure.ac index 048b2ac..23d9060 100644 --- a/configure.ac +++ b/configure.ac @@ -103,18 +103,20 @@ AM_CONDITIONAL([WITH_TOOLS], [test "x$with_tools" = xtrue]) AC_DEFUN([FUNC_NOT_FOUND_TOOLS], [ERR_NOT_FOUND([$1()], [tools])]) -AC_DEFUN([HEADER_NOT_FOUND_TOOLS], - [ERR_NOT_FOUND([$1 header], [tools])]) - -if test "x$with_tools" = xtrue -then - # These are only needed to build tools - AC_CHECK_FUNC([basename], [], [FUNC_NOT_FOUND_TOOLS([basename])]) +AC_ARG_ENABLE([gpioset-interactive], + [AS_HELP_STRING([--enable-gpioset-interactive], + [enable gpioset interactive mode [default=no]])], + [if test "x$enableval" = xyes; then with_gpioset_interactive=true; fi], + [with_gpioset_interactive=false]) +AM_CONDITIONAL([WITH_GPIOSET_INTERACTIVE], + [test "x$with_gpioset_interactive" = xtrue]) + +AS_IF([test "x$with_tools" = xtrue], + [# These are only needed to build tools AC_CHECK_FUNC([daemon], [], [FUNC_NOT_FOUND_TOOLS([daemon])]) - AC_CHECK_FUNC([signalfd], [], [FUNC_NOT_FOUND_TOOLS([signalfd])]) - AC_CHECK_FUNC([setlinebuf], [], [FUNC_NOT_FOUND_TOOLS([setlinebuf])]) - AC_CHECK_HEADERS([sys/signalfd.h], [], [HEADER_NOT_FOUND_TOOLS([sys/signalfd.h])]) -fi + AS_IF([test "x$with_gpioset_interactive" = xtrue], + [PKG_CHECK_MODULES([LIBEDIT], [libedit >= 3.1])]) + ]) AC_ARG_ENABLE([tests], [AS_HELP_STRING([--enable-tests],[enable libgpiod tests [default=no]])], diff --git a/tools/Makefile.am b/tools/Makefile.am index fc074b9..3fafec6 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -4,10 +4,16 @@ AM_CFLAGS = -I$(top_srcdir)/include/ -include $(top_builddir)/config.h AM_CFLAGS += -Wall -Wextra -g -std=gnu89 +if WITH_GPIOSET_INTERACTIVE + +AM_CFLAGS += -DGPIOSET_INTERACTIVE + +endif + noinst_LTLIBRARIES = libtools-common.la libtools_common_la_SOURCES = tools-common.c tools-common.h -LDADD = libtools-common.la $(top_builddir)/lib/libgpiod.la +LDADD = libtools-common.la $(top_builddir)/lib/libgpiod.la $(LIBEDIT_LIBS) bin_PROGRAMS = gpiodetect gpioinfo gpioget gpioset gpiomon diff --git a/tools/gpiodetect.c b/tools/gpiodetect.c index 30bde32..3c1ba8b 100644 --- a/tools/gpiodetect.c +++ b/tools/gpiodetect.c @@ -1,8 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski +// SPDX-FileCopyrightText: 2022 Kent Gibson -#include -#include #include #include #include @@ -11,3 +10,113 @@ #include "tools-common.h" +static void print_help(void) +{ + printf("Usage: %s [OPTIONS] [chip]...\n", get_progname()); + printf("\n"); + printf("List GPIO chips, print their labels and number of GPIO lines.\n"); + printf("\n"); + printf("Chips may be identified by number, name, or path.\n"); + printf("e.g. '0', 'gpiochip0', and '/dev/gpiochip0' all refer to the same chip.\n"); + printf("\n"); + printf("If no chips are specified then all chips are listed.\n"); + printf("\n"); + printf("Options:\n"); + printf(" -h, --help\t\tdisplay this help and exit\n"); + printf(" -v, --version\t\toutput version information and exit\n"); +} + +static int parse_config(int argc, char **argv) +{ + int optc, opti; + const char *const shortopts = "+hv"; + static const struct option longopts[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'v' }, + { GETOPT_NULL_LONGOPT }, + }; + + for (;;) { + optc = getopt_long(argc, argv, shortopts, longopts, &opti); + if (optc < 0) + break; + + switch (optc) { + case 'h': + print_help(); + exit(EXIT_SUCCESS); + case 'v': + print_version(); + exit(EXIT_SUCCESS); + case '?': + die("try %s --help", get_progname()); + default: + abort(); + } + } + return optind; +} + +static int print_chip_info(const char *path) +{ + struct gpiod_chip *chip; + struct gpiod_chip_info *info; + + chip = gpiod_chip_open(path); + if (!chip) { + print_perror("unable to open chip '%s'", path); + return 1; + } + + info = gpiod_chip_get_info(chip); + if (!info) + die_perror("unable to read info for '%s'", path); + + printf("%s [%s] (%zu lines)\n", + gpiod_chip_info_get_name(info), + gpiod_chip_info_get_label(info), + gpiod_chip_info_get_num_lines(info)); + + gpiod_chip_info_free(info); + gpiod_chip_close(chip); + return 0; +} + +int main(int argc, char **argv) +{ + int num_chips, i; + char **paths; + char *path; + int ret = EXIT_SUCCESS; + + i = parse_config(argc, argv); + argc -= i; + argv += i; + + if (argc == 0) { + num_chips = all_chip_paths(&paths); + for (i = 0; i < num_chips; i++) { + if (print_chip_info(paths[i])) + ret = EXIT_FAILURE; + + free(paths[i]); + } + free(paths); + } + + for (i = 0; i < argc; i++) { + if (chip_path_lookup(argv[i], &path)) { + if (print_chip_info(path)) + ret = EXIT_FAILURE; + + free(path); + } else { + print_error( + "cannot find GPIO chip character device '%s'", + argv[i]); + ret = EXIT_FAILURE; + } + } + + return ret; +} diff --git a/tools/gpioget.c b/tools/gpioget.c index 1b3e666..497f273 100644 --- a/tools/gpioget.c +++ b/tools/gpioget.c @@ -1,12 +1,235 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski +// SPDX-FileCopyrightText: 2022 Kent Gibson #include #include -#include #include #include #include +#include #include "tools-common.h" +static void print_help(void) +{ + printf("Usage: %s [OPTIONS] ...\n", get_progname()); + printf("\n"); + printf("Read values of GPIO lines.\n"); + printf("\n"); + printf("Lines are specified by name, or optionally by offset if the chip option\n"); + printf("is provided.\n"); + printf("\n"); + printf("Options:\n"); + printf(" -a, --as-is\t\tleave the line direction unchanged, not forced to input\n"); + print_bias_help(); + printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n"); + printf(" -c, --chip \trestrict scope to a particular chip\n"); + printf(" -C, --consumer \tconsumer name applied to requested lines (default is 'gpioget')\n"); + printf(" -h, --help\t\tdisplay this help and exit\n"); + printf(" -l, --active-low\ttreat the line as active low\n"); + printf(" -p, --hold-period \n"); + printf("\t\t\twait between requesting the lines and reading the values\n"); + printf(" --numeric\t\tdisplay line values as '0' (inactive) or '1' (active)\n"); + printf(" -s, --strict\t\tabort if requested line names are not unique\n"); + printf(" --unquoted\tdon't quote line names\n"); + printf(" -v, --version\t\toutput version information and exit\n"); + print_chip_help(); + print_period_help(); +} +struct config { + bool active_low; + bool by_name; + bool numeric; + bool strict; + bool unquoted; + int bias; + int direction; + unsigned int hold_period_us; + const char *chip_id; + const char *consumer; +}; + +static int parse_config(int argc, char **argv, struct config *cfg) +{ + int opti, optc; + const char *const shortopts = "+ab:c:C:hlp:sv"; + static const struct option longopts[] = { + { "active-low", no_argument, NULL, 'l' }, + { "as-is", no_argument, NULL, 'a' }, + { "bias", required_argument, NULL, 'b' }, + { "by-name", no_argument, NULL, 'B' }, + { "chip", required_argument, NULL, 'c' }, + { "consumer", required_argument, NULL, 'C' }, + { "help", no_argument, NULL, 'h' }, + { "hold-period", required_argument, NULL, 'p' }, + { "numeric", no_argument, NULL, 'N' }, + { "strict", no_argument, NULL, 's' }, + { "unquoted", no_argument, NULL, 'Q' }, + { "version", no_argument, NULL, 'v' }, + { GETOPT_NULL_LONGOPT }, + }; + + memset(cfg, 0, sizeof(*cfg)); + cfg->direction = GPIOD_LINE_DIRECTION_INPUT; + cfg->consumer = "gpioget"; + + for (;;) { + optc = getopt_long(argc, argv, shortopts, longopts, &opti); + if (optc < 0) + break; + + switch (optc) { + case 'a': + cfg->direction = GPIOD_LINE_DIRECTION_AS_IS; + break; + case 'b': + cfg->bias = parse_bias_or_die(optarg); + break; + case 'B': + cfg->by_name = true; + break; + case 'c': + cfg->chip_id = optarg; + break; + case 'C': + cfg->consumer = optarg; + break; + case 'l': + cfg->active_low = true; + break; + case 'N': + cfg->numeric = true; + break; + case 'p': + cfg->hold_period_us = parse_period_or_die(optarg); + break; + case 'Q': + cfg->unquoted = true; + break; + case 's': + cfg->strict = true; + break; + case 'h': + print_help(); + exit(EXIT_SUCCESS); + case 'v': + print_version(); + exit(EXIT_SUCCESS); + case '?': + die("try %s --help", get_progname()); + case 0: + break; + default: + abort(); + } + } + + return optind; +} + +int main(int argc, char **argv) +{ + int i, num_lines, ret, *values; + struct gpiod_line_settings *settings; + struct gpiod_request_config *req_cfg; + struct gpiod_line_request *request; + struct gpiod_line_config *line_cfg; + struct gpiod_chip *chip; + unsigned int *offsets; + struct line_resolver *resolver; + struct resolved_line *line; + struct config cfg; + const char *fmt; + + i = parse_config(argc, argv, &cfg); + argc -= i; + argv += i; + + if (argc < 1) + die("at least one GPIO line must be specified"); + + resolver = resolve_lines(argc, argv, cfg.chip_id, cfg.strict, + cfg.by_name); + validate_resolution(resolver, cfg.chip_id); + + offsets = calloc(resolver->num_lines, sizeof(*offsets)); + values = calloc(resolver->num_lines, sizeof(*values)); + if (!offsets || !values) + die("out of memory"); + + settings = gpiod_line_settings_new(); + if (!settings) + die_perror("unable to allocate line settings"); + + gpiod_line_settings_set_direction(settings, cfg.direction); + + if (cfg.bias) + gpiod_line_settings_set_bias(settings, cfg.bias); + + if (cfg.active_low) + gpiod_line_settings_set_active_low(settings, true); + + req_cfg = gpiod_request_config_new(); + if (!req_cfg) + die_perror("unable to allocate the request config structure"); + + line_cfg = gpiod_line_config_new(); + if (!line_cfg) + die_perror("unable to allocate the line config structure"); + + gpiod_request_config_set_consumer(req_cfg, cfg.consumer); + for (i = 0; i < resolver->num_chips; i++) { + chip = gpiod_chip_open(resolver->chips[i].path); + if (!chip) + die_perror("unable to open chip '%s'", + resolver->chips[i].path); + + num_lines = get_line_offsets_and_values(resolver, i, offsets, + NULL); + + gpiod_line_config_reset(line_cfg); + ret = gpiod_line_config_add_line_settings(line_cfg, offsets, + num_lines, settings); + if (ret) + die_perror("unable to add line settings"); + + request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); + if (!request) + die_perror("unable to request lines"); + + if (cfg.hold_period_us) + usleep(cfg.hold_period_us); + + ret = gpiod_line_request_get_values(request, values); + if (ret) + die_perror("unable to read GPIO line values"); + + set_line_values(resolver, i, values); + + gpiod_line_request_release(request); + gpiod_chip_close(chip); + } + fmt = cfg.unquoted ? "%s=%s" : "\"%s\"=%s"; + for (i = 0; i < resolver->num_lines; i++) { + line = &resolver->lines[i]; + if (cfg.numeric) + printf("%d", line->value); + else + printf(fmt, line->id, + line->value ? "active" : "inactive"); + + if (i != resolver->num_lines - 1) + printf(" "); + } + printf("\n"); + + free_line_resolver(resolver); + gpiod_request_config_free(req_cfg); + gpiod_line_config_free(line_cfg); + gpiod_line_settings_free(settings); + free(offsets); + free(values); + + return EXIT_SUCCESS; +} diff --git a/tools/gpioinfo.c b/tools/gpioinfo.c index ae368fa..b79690c 100644 --- a/tools/gpioinfo.c +++ b/tools/gpioinfo.c @@ -1,8 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski +// SPDX-FileCopyrightText: 2022 Kent Gibson -#include -#include #include #include #include @@ -12,3 +11,250 @@ #include "tools-common.h" +static void print_help(void) +{ + printf("Usage: %s [OPTIONS] [line]...\n", get_progname()); + printf("\n"); + printf("Print information about GPIO lines.\n"); + printf("\n"); + printf("Lines are specified by name, or optionally by offset if the chip option\n"); + printf("is provided.\n"); + printf("\n"); + printf("If no lines are specified then all lines are displayed.\n"); + printf("\n"); + printf("Options:\n"); + printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n"); + printf(" -c, --chip \trestrict scope to a particular chip\n"); + printf(" -h, --help\t\tdisplay this help and exit\n"); + printf(" -s, --strict\t\tcheck all lines - don't assume line names are unique\n"); + printf(" --unquoted\tdon't quote line or consumer names\n"); + printf(" -v, --version\t\toutput version information and exit\n"); + print_chip_help(); +} + +struct config { + bool by_name; + bool strict; + bool unquoted_strings; + const char *chip_id; +}; + +static int parse_config(int argc, char **argv, struct config *cfg) +{ + int opti, optc; + const char *const shortopts = "+c:hsv"; + static const struct option longopts[] = { + { "by-name", no_argument, NULL, 'B' }, + { "chip", required_argument, NULL, 'c' }, + { "help", no_argument, NULL, 'h' }, + { "strict", no_argument, NULL, 's' }, + { "unquoted", no_argument, NULL, 'Q' }, + { "version", no_argument, NULL, 'v' }, + { GETOPT_NULL_LONGOPT }, + }; + + memset(cfg, 0, sizeof(*cfg)); + + for (;;) { + optc = getopt_long(argc, argv, shortopts, longopts, &opti); + if (optc < 0) + break; + + switch (optc) { + case 'B': + cfg->by_name = true; + break; + case 'c': + cfg->chip_id = optarg; + break; + case 's': + cfg->strict = true; + break; + case 'h': + print_help(); + exit(EXIT_SUCCESS); + case 'Q': + cfg->unquoted_strings = true; + break; + case 'v': + print_version(); + exit(EXIT_SUCCESS); + case '?': + die("try %s --help", get_progname()); + case 0: + break; + default: + abort(); + } + } + + return optind; +} + + +/* + * minimal version similar to tools-common that indicates if a line should be + * printed rather than storing details into the resolver. + * Does not die on non-unique lines. + */ +static bool resolve_line(struct line_resolver *resolver, + struct gpiod_line_info *info, int chip_num) +{ + struct resolved_line *line; + const char *name; + int i; + unsigned int offset; + bool resolved = false; + + offset = gpiod_line_info_get_offset(info); + for (i = 0; i < resolver->num_lines; i++) { + line = &resolver->lines[i]; + /* already resolved by offset? */ + if (line->resolved && + (line->offset == offset) && + (line->chip_num == chip_num)) { + resolved = true; + } + if (line->resolved && !resolver->strict) + continue; + + /* else resolve by name */ + name = gpiod_line_info_get_name(info); + if (name && (strcmp(line->id, name) == 0)) { + line->resolved = true; + line->offset = offset; + line->chip_num = chip_num; + resolved = true; + } + } + + return resolved; +} + +static void print_line_info(struct gpiod_line_info *info, bool unquoted_strings) +{ + const char *name; + char quoted_name[17]; + int len; + + name = gpiod_line_info_get_name(info); + if (!name) { + name = "unnamed"; + unquoted_strings = true; + } + if (unquoted_strings) { + printf("%-16s\t", name); + } else { + len = strlen(name); + if (len <= 14) { + quoted_name[0] = '"'; + memcpy("ed_name[1], name, len); + quoted_name[len + 1] = '"'; + quoted_name[len + 2] = '\0'; + printf("%-16s\t", quoted_name); + } else { + printf("\"%s\"\t", name); + } + } + print_line_attributes(info, unquoted_strings); +} + +/* + * based on resolve_lines, but prints lines immediately rather than collecting + * details in the resolver. + */ +static void list_lines(struct line_resolver *resolver, struct gpiod_chip *chip, + int chip_num, struct config *cfg) +{ + struct gpiod_chip_info *chip_info; + struct gpiod_line_info *info; + int offset, num_lines; + + chip_info = gpiod_chip_get_info(chip); + if (!chip_info) + die_perror("unable to read info from chip %s", + gpiod_chip_get_path(chip)); + + num_lines = gpiod_chip_info_get_num_lines(chip_info); + + if ((chip_num == 0) && (cfg->chip_id && !cfg->by_name)) + resolve_lines_by_offset(resolver, num_lines); + + for (offset = 0; + ((offset < num_lines) && + !(resolver->num_lines && resolve_done(resolver))); + offset++) { + info = gpiod_chip_get_line_info(chip, offset); + if (!info) + die_perror("unable to read info for line %d from %s", + offset, gpiod_chip_info_get_name(chip_info)); + + if (resolver->num_lines && + !resolve_line(resolver, info, chip_num)) + continue; + + if (resolver->num_lines) { + printf("%s %u", gpiod_chip_info_get_name(chip_info), + offset); + } else { + if (offset == 0) + printf("%s - %u lines:\n", + gpiod_chip_info_get_name(chip_info), + num_lines); + + printf("\tline %3u:", offset); + } + fputc('\t', stdout); + print_line_info(info, cfg->unquoted_strings); + fputc('\n', stdout); + gpiod_line_info_free(info); + resolver->num_found++; + } + gpiod_chip_info_free(chip_info); +} + +int main(int argc, char **argv) +{ + struct gpiod_chip *chip; + struct line_resolver *resolver = NULL; + struct config cfg; + char **paths; + int num_chips, i, ret = EXIT_SUCCESS; + + i = parse_config(argc, argv, &cfg); + argc -= i; + argv += i; + + if (!cfg.chip_id) + cfg.by_name = true; + + num_chips = chip_paths(cfg.chip_id, &paths); + if (cfg.chip_id && (num_chips == 0)) + die("cannot find GPIO chip character device '%s'", cfg.chip_id); + + resolver = resolver_init(argc, argv, num_chips, cfg.strict, + cfg.by_name); + + for (i = 0; i < num_chips; i++) { + chip = gpiod_chip_open(paths[i]); + if (chip) { + list_lines(resolver, chip, i, &cfg); + gpiod_chip_close(chip); + } else { + print_perror("unable to open chip '%s'", paths[i]); + + if (cfg.chip_id) + return EXIT_FAILURE; + + ret = EXIT_FAILURE; + } + free(paths[i]); + } + free(paths); + + validate_resolution(resolver, cfg.chip_id); + if (argc && resolver->num_found != argc) + ret = EXIT_FAILURE; + free(resolver); + return ret; +} diff --git a/tools/gpiomon.c b/tools/gpiomon.c index 6fa19b6..d5470fc 100644 --- a/tools/gpiomon.c +++ b/tools/gpiomon.c @@ -1,19 +1,479 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski +// SPDX-FileCopyrightText: 2022 Kent Gibson -#include #include #include #include -#include #include -#include #include #include #include -#include #include "tools-common.h" #define EVENT_BUF_SIZE 32 +static void print_help(void) +{ + printf("Usage: %s [OPTIONS] ...\n", get_progname()); + printf("\n"); + printf("Wait for events on GPIO lines and print them to standard output.\n"); + printf("\n"); + printf("Lines are specified by name, or optionally by offset if the chip option\n"); + printf("is provided.\n"); + printf("\n"); + printf("Options:\n"); + printf(" --banner\t\tdisplay a banner on successful startup\n"); + print_bias_help(); + printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n"); + printf(" -c, --chip \trestrict scope to a particular chip\n"); + printf(" -C, --consumer \tconsumer name applied to requested lines (default is 'gpiomon')\n"); + printf(" -e, --edges \tspecify the edges to monitor\n"); + printf("\t\t\tPossible values: 'falling', 'rising', 'both'.\n"); + printf("\t\t\t(default is 'both')\n"); + printf(" -E, --event-clock \n"); + printf("\t\t\tspecify the source clock for event timestamps\n"); + printf("\t\t\tPossible values: 'monotonic', 'realtime', 'hte'.\n"); + printf("\t\t\t(default is 'monotonic')\n"); + printf("\t\t\tBy default 'realtime' is formatted as UTC, others as raw u64.\n"); + printf(" -h, --help\t\tdisplay this help and exit\n"); + printf(" -F, --format \tspecify a custom output format\n"); + printf(" -l, --active-low\ttreat the line as active low, flipping the sense of\n"); + printf("\t\t\trising and falling edges\n"); + printf(" --localtime\tformat event timestamps as local time\n"); + printf(" -n, --num-events \n"); + printf("\t\t\texit after processing num events\n"); + printf(" -p, --debounce-period \n"); + printf("\t\t\tdebounce the line(s) with the specified period\n"); + printf(" -q, --quiet\t\tdon't generate any output\n"); + printf(" -s, --strict\t\tabort if requested line names are not unique\n"); + printf(" --unquoted\tdon't quote line or consumer names\n"); + printf(" --utc\t\tformat event timestamps as UTC (default for 'realtime')\n"); + printf(" -v, --version\t\toutput version information and exit\n"); + print_chip_help(); + print_period_help(); + printf("\n"); + printf("Format specifiers:\n"); + printf(" %%o GPIO line offset\n"); + printf(" %%l GPIO line name\n"); + printf(" %%c GPIO chip name\n"); + printf(" %%e numeric edge event type ('1' - rising or '2' - falling)\n"); + printf(" %%E edge event type ('rising' or 'falling')\n"); + printf(" %%S event timestamp as seconds\n"); + printf(" %%U event timestamp as UTC\n"); + printf(" %%L event timestamp as local time\n"); +} + +static int parse_edges_or_die(const char *option) +{ + if (strcmp(option, "rising") == 0) + return GPIOD_LINE_EDGE_RISING; + if (strcmp(option, "falling") == 0) + return GPIOD_LINE_EDGE_FALLING; + if (strcmp(option, "both") != 0) + die("invalid edges: %s", option); + return GPIOD_LINE_EDGE_BOTH; +} + +static int parse_event_clock_or_die(const char *option) +{ + if (strcmp(option, "realtime") == 0) + return GPIOD_LINE_EVENT_CLOCK_REALTIME; + if (strcmp(option, "hte") != 0) + return GPIOD_LINE_EVENT_CLOCK_HTE; + if (strcmp(option, "monotonic") != 0) + die("invalid event clock: %s", option); + return GPIOD_LINE_EVENT_CLOCK_MONOTONIC; +} + +struct config { + bool active_low; + bool banner; + bool by_name; + bool quiet; + bool strict; + bool unquoted; + int bias; + int edges; + int events_wanted; + unsigned int debounce_period_us; + const char *chip_id; + const char *consumer; + const char *fmt; + int event_clock; + int timestamp_fmt; +}; + +static int parse_config(int argc, char **argv, struct config *cfg) +{ + int opti, optc; + const char *const shortopts = "+b:c:C:e:E:hF:ln:p:qshv"; + const struct option longopts[] = { + { "active-low", no_argument, NULL, 'l' }, + { "banner", no_argument, NULL, '-'}, + { "bias", required_argument, NULL, 'b' }, + { "by-name", no_argument, NULL, 'B'}, + { "chip", required_argument, NULL, 'c' }, + { "consumer", required_argument, NULL, 'C' }, + { "debounce-period", required_argument, NULL, 'p' }, + { "edges", required_argument, NULL, 'e' }, + { "event-clock", required_argument, NULL, 'E' }, + { "format", required_argument, NULL, 'F' }, + { "help", no_argument, NULL, 'h' }, + { "localtime", no_argument, &cfg->timestamp_fmt, 2 }, + { "num-events", required_argument, NULL, 'n' }, + { "quiet", no_argument, NULL, 'q' }, + { "silent", no_argument, NULL, 'q' }, + { "strict", no_argument, NULL, 's' }, + { "unquoted", no_argument, NULL, 'Q' }, + { "utc", no_argument, &cfg->timestamp_fmt, 1 }, + { "version", no_argument, NULL, 'v' }, + { GETOPT_NULL_LONGOPT }, + }; + + memset(cfg, 0, sizeof(*cfg)); + cfg->edges = GPIOD_LINE_EDGE_BOTH; + cfg->consumer = "gpiomon"; + + for (;;) { + optc = getopt_long(argc, argv, shortopts, longopts, &opti); + if (optc < 0) + break; + + switch (optc) { + case '-': + cfg->banner = true; + break; + case 'b': + cfg->bias = parse_bias_or_die(optarg); + break; + case 'B': + cfg->by_name = true; + break; + case 'c': + cfg->chip_id = optarg; + break; + case 'C': + cfg->consumer = optarg; + break; + case 'e': + cfg->edges = parse_edges_or_die(optarg); + break; + case 'E': + cfg->event_clock = parse_event_clock_or_die(optarg); + break; + case 'F': + cfg->fmt = optarg; + break; + case 'l': + cfg->active_low = true; + break; + case 'n': + cfg->events_wanted = parse_uint_or_die(optarg); + break; + case 'p': + cfg->debounce_period_us = parse_period_or_die(optarg); + break; + case 'q': + cfg->quiet = true; + break; + case 's': + cfg->strict = true; + break; + case 'h': + print_help(); + exit(EXIT_SUCCESS); + case 'v': + print_version(); + exit(EXIT_SUCCESS); + case '?': + die("try %s --help", get_progname()); + case 0: + break; + default: + abort(); + } + } + + /* setup default clock/format combinations, where not overridden */ + if (cfg->event_clock == 0) { + if (cfg->timestamp_fmt) + cfg->event_clock = GPIOD_LINE_EVENT_CLOCK_REALTIME; + else + cfg->event_clock = GPIOD_LINE_EVENT_CLOCK_MONOTONIC; + } else if ((cfg->event_clock == GPIOD_LINE_EVENT_CLOCK_REALTIME) && + (cfg->timestamp_fmt == 0)) { + cfg->timestamp_fmt = 1; + } + + return optind; +} + +static void print_banner(int num_lines, char **lines) +{ + int i; + + if (num_lines > 1) { + printf("Monitoring lines "); + + for (i = 0; i < num_lines - 1; i++) + printf("'%s', ", lines[i]); + + printf("and '%s'...\n", lines[i]); + } else { + printf("Monitoring line '%s'...\n", lines[0]); + } +} + +static void event_print_formatted(struct gpiod_edge_event *event, + struct line_resolver *resolver, int chip_num, + struct config *cfg) +{ + const char *lname, *prev, *curr; + char fmt; + uint64_t evtime; + int evtype; + unsigned int offset; + + offset = gpiod_edge_event_get_line_offset(event); + evtime = gpiod_edge_event_get_timestamp_ns(event); + evtype = gpiod_edge_event_get_event_type(event); + + for (prev = curr = cfg->fmt;;) { + curr = strchr(curr, '%'); + if (!curr) { + fputs(prev, stdout); + break; + } + + if (prev != curr) + fwrite(prev, curr - prev, 1, stdout); + + fmt = *(curr + 1); + + switch (fmt) { + case 'c': + fputs(get_chip_name(resolver, chip_num), stdout); + break; + case 'e': + printf("%d", evtype); + break; + case 'E': + if (evtype == GPIOD_EDGE_EVENT_RISING_EDGE) + fputs("rising", stdout); + else + fputs("falling", stdout); + break; + case 'l': + lname = get_line_name(resolver, chip_num, offset); + if (!lname) + lname = "unnamed"; + fputs(lname, stdout); + break; + case 'L': + print_event_time(evtime, 2); + break; + case 'o': + printf("%u", offset); + break; + case 'S': + print_event_time(evtime, 0); + break; + case 'U': + print_event_time(evtime, 1); + break; + case '%': + fputc('%', stdout); + break; + case '\0': + fputc('%', stdout); + goto end; + default: + fwrite(curr, 2, 1, stdout); + break; + } + + curr += 2; + prev = curr; + } + +end: + fputc('\n', stdout); +} + +static void event_print_human_readable(struct gpiod_edge_event *event, + struct line_resolver *resolver, + int chip_num, struct config *cfg) +{ + unsigned int offset; + uint64_t evtime; + + offset = gpiod_edge_event_get_line_offset(event); + evtime = gpiod_edge_event_get_timestamp_ns(event); + + print_event_time(evtime, cfg->timestamp_fmt); + + if (gpiod_edge_event_get_event_type(event) == + GPIOD_EDGE_EVENT_RISING_EDGE) + fputs("\trising\t", stdout); + else + fputs("\tfalling\t", stdout); + + print_line_id(resolver, chip_num, offset, cfg->chip_id, + cfg->unquoted); + fputc('\n', stdout); +} + +static void event_print(struct gpiod_edge_event *event, + struct line_resolver *resolver, int chip_num, + struct config *cfg) +{ + if (cfg->quiet) + return; + + if (cfg->fmt) + event_print_formatted(event, resolver, chip_num, cfg); + else + event_print_human_readable(event, resolver, chip_num, cfg); +} + +int main(int argc, char **argv) +{ + int num_lines, events_done = 0; + struct gpiod_edge_event_buffer *event_buffer; + int ret, i, j; + struct gpiod_line_settings *settings; + struct gpiod_request_config *req_cfg; + struct gpiod_line_request **requests; + struct pollfd *pollfds; + struct gpiod_line_config *line_cfg; + unsigned int *offsets; + struct gpiod_edge_event *event; + struct gpiod_chip *chip; + struct line_resolver *resolver; + struct config cfg; + + i = parse_config(argc, argv, &cfg); + argc -= i; + argv += i; + + if (argc < 1) + die("at least one GPIO line must be specified"); + + if (argc > 64) + die("too many lines given"); + + settings = gpiod_line_settings_new(); + if (!settings) + die_perror("unable to allocate line settings"); + + if (cfg.bias) + gpiod_line_settings_set_bias(settings, cfg.bias); + + if (cfg.active_low) + gpiod_line_settings_set_active_low(settings, true); + + if (cfg.debounce_period_us) + gpiod_line_settings_set_debounce_period_us(settings, + cfg.debounce_period_us); + + gpiod_line_settings_set_event_clock(settings, cfg.event_clock); + gpiod_line_settings_set_edge_detection(settings, cfg.edges); + + line_cfg = gpiod_line_config_new(); + if (!line_cfg) + die_perror("unable to allocate the line config structure"); + + req_cfg = gpiod_request_config_new(); + if (!req_cfg) + die_perror("unable to allocate the request config structure"); + + gpiod_request_config_set_consumer(req_cfg, cfg.consumer); + + event_buffer = gpiod_edge_event_buffer_new(EVENT_BUF_SIZE); + if (!event_buffer) + die_perror("unable to allocate the line event buffer"); + + resolver = resolve_lines(argc, argv, cfg.chip_id, cfg.strict, + cfg.by_name); + validate_resolution(resolver, cfg.chip_id); + requests = calloc(resolver->num_chips, sizeof(*requests)); + pollfds = calloc(resolver->num_chips, sizeof(*pollfds)); + offsets = calloc(resolver->num_lines, sizeof(*offsets)); + if (!requests || !pollfds || !offsets) + die("out of memory"); + + for (i = 0; i < resolver->num_chips; i++) { + num_lines = get_line_offsets_and_values(resolver, i, offsets, + NULL); + gpiod_line_config_reset(line_cfg); + ret = gpiod_line_config_add_line_settings(line_cfg, offsets, + num_lines, settings); + if (ret) + die_perror("unable to add line settings"); + + chip = gpiod_chip_open(resolver->chips[i].path); + if (!chip) + die_perror("unable to open chip '%s'", + resolver->chips[i].path); + + requests[i] = gpiod_chip_request_lines(chip, req_cfg, line_cfg); + if (!requests[i]) + die_perror("unable to request lines on chip %s", + resolver->chips[i].path); + + pollfds[i].fd = gpiod_line_request_get_fd(requests[i]); + pollfds[i].events = POLLIN; + gpiod_chip_close(chip); + } + gpiod_request_config_free(req_cfg); + gpiod_line_config_free(line_cfg); + gpiod_line_settings_free(settings); + + if (cfg.banner) + print_banner(argc, argv); + + for (;;) { + fflush(stdout); + + if (poll(pollfds, resolver->num_chips, -1) < 0) + die_perror("error polling for events"); + + for (i = 0; i < resolver->num_chips; i++) { + if (pollfds[i].revents == 0) + continue; + + ret = gpiod_line_request_read_edge_event(requests[i], + event_buffer, EVENT_BUF_SIZE); + if (ret < 0) + die_perror("error reading line events"); + + for (j = 0; j < ret; j++) { + event = gpiod_edge_event_buffer_get_event( + event_buffer, j); + if (!event) + die_perror("unable to retrieve event from buffer"); + + event_print(event, resolver, i, &cfg); + + events_done++; + + if (cfg.events_wanted && + events_done >= cfg.events_wanted) + goto done; + } + } + } +done: + for (i = 0; i < resolver->num_chips; i++) + gpiod_line_request_release(requests[i]); + + free(requests); + free_line_resolver(resolver); + gpiod_edge_event_buffer_free(event_buffer); + free(offsets); + + return EXIT_SUCCESS; +} + diff --git a/tools/gpioset.c b/tools/gpioset.c index 0e3a1e4..e53aa67 100644 --- a/tools/gpioset.c +++ b/tools/gpioset.c @@ -1,7 +1,8 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski +// SPDX-FileCopyrightText: 2022 Kent Gibson -#include +#include #include #include #include @@ -9,8 +10,918 @@ #include #include #include -#include #include +#if GPIOSET_INTERACTIVE +#include +#endif #include "tools-common.h" +static void print_help(void) +{ + printf("Usage: %s [OPTIONS] ...\n", get_progname()); + printf("\n"); + printf("Set values of GPIO lines.\n"); + printf("\n"); + printf("Lines are specified by name, or optionally by offset if the chip option\n"); + printf("is provided.\n"); + printf("Values may be '1' or '0', or equivalently 'active'/'inactive' or 'on'/'off'.\n"); + printf("\n"); + printf("The line output state is maintained until the process exits, but after that\n"); + printf("is not guaranteed.\n"); + printf("\n"); + printf("Options:\n"); + printf(" --banner\t\tdisplay a banner on successful startup\n"); + print_bias_help(); + printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n"); + printf(" -c, --chip \trestrict scope to a particular chip\n"); + printf(" -C, --consumer \tconsumer name applied to requested lines (default is 'gpioset')\n"); + printf(" -d, --drive \tspecify the line drive mode\n"); + printf("\t\t\tPossible values: 'push-pull', 'open-drain', 'open-source'.\n"); + printf("\t\t\t(default is 'push-pull')\n"); + printf(" -h, --help\t\tdisplay this help and exit\n"); +#ifdef GPIOSET_INTERACTIVE + printf(" -i, --interactive\tset the lines then wait for additional set commands\n"); + printf("\t\t\tUse the 'help' command at the interactive prompt to get help\n"); + printf("\t\t\tfor the supported commands.\n"); +#endif + printf(" -l, --active-low\ttreat the line as active low\n"); + printf(" -p, --hold-period \n"); + printf("\t\t\tthe minimum time period to hold lines at the requested values\n"); + printf(" -s, --strict\t\tabort if requested line names are not unique\n"); + printf(" -t, --toggle [,period]...\n"); + printf("\t\t\ttoggle the line(s) after the specified period(s)\n"); + printf("\t\t\tIf the last period is non-zero then the sequence repeats.\n"); + printf(" --unquoted\tdon't quote line names\n"); + printf(" -v, --version\t\toutput version information and exit\n"); + printf(" -z, --daemonize\tset values then detach from the controlling terminal\n"); + print_chip_help(); + print_period_help(); + printf("\n"); + printf("*Note*\n"); + printf(" The state of a GPIO line controlled over the character device reverts to default\n"); + printf(" when the last process referencing the file descriptor representing the device file exits.\n"); + printf(" This means that it's wrong to run gpioset, have it exit and expect the line to continue\n"); + printf(" being driven high or low. It may happen if given pin is floating but it must be interpreted\n"); + printf(" as undefined behavior.\n"); +} + +static int parse_drive_or_die(const char *option) +{ + if (strcmp(option, "open-drain") == 0) + return GPIOD_LINE_DRIVE_OPEN_DRAIN; + if (strcmp(option, "open-source") == 0) + return GPIOD_LINE_DRIVE_OPEN_SOURCE; + if (strcmp(option, "push-pull") != 0) + die("invalid drive: %s", option); + return 0; +} + +static int parse_periods_or_die(char *option, unsigned int **periods) +{ + int i, num_periods = 1; + unsigned int *pp; + char *end; + + for (i = 0; option[i] != '\0'; i++) + if (option[i] == ',') + num_periods++; + + pp = calloc(num_periods, sizeof(*pp)); + if (pp == NULL) + die("out of memory"); + + for (i = 0; i < num_periods - 1; i++) { + for (end = option; *end != ','; end++) + ; + + *end = '\0'; + pp[i] = parse_period_or_die(option); + option = end + 1; + } + pp[i] = parse_period_or_die(option); + *periods = pp; + + return num_periods; +} + + +struct config { + bool active_low; + bool banner; + bool by_name; + bool daemonize; + bool interactive; + bool strict; + bool unquoted; + int bias; + int drive; + int toggles; + unsigned int *toggle_periods; + unsigned int hold_period_us; + const char *chip_id; + const char *consumer; +}; + +static int parse_config(int argc, char **argv, struct config *cfg) +{ + int opti, optc; +#ifdef GPIOSET_INTERACTIVE + const char *const shortopts = "+b:c:C:d:hilp:st:vz"; +#else + const char *const shortopts = "+b:c:C:d:hlp:st:vz"; +#endif + static const struct option longopts[] = { + { "active-low", no_argument, NULL, 'l' }, + { "banner", no_argument, NULL, '-'}, + { "bias", required_argument, NULL, 'b' }, + { "by-name", no_argument, NULL, 'B' }, + { "chip", required_argument, NULL, 'c' }, + { "consumer", required_argument, NULL, 'C' }, + { "daemonize", no_argument, NULL, 'z' }, + { "drive", required_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "hold-period", required_argument, NULL, 'p' }, +#ifdef GPIOSET_INTERACTIVE + { "interactive", no_argument, NULL, 'i' }, +#endif + { "strict", no_argument, NULL, 's' }, + { "toggle", required_argument, NULL, 't' }, + { "unquoted", no_argument, NULL, 'Q' }, + { "version", no_argument, NULL, 'v' }, + { GETOPT_NULL_LONGOPT }, + }; + + memset(cfg, 0, sizeof(*cfg)); + cfg->consumer = "gpioset"; + for (;;) { + optc = getopt_long(argc, argv, shortopts, longopts, &opti); + if (optc < 0) + break; + + switch (optc) { + case '-': + cfg->banner = true; + break; + case 'b': + cfg->bias = parse_bias_or_die(optarg); + break; + case 'B': + cfg->by_name = true; + break; + case 'c': + cfg->chip_id = optarg; + break; + case 'C': + cfg->consumer = optarg; + break; + case 'd': + cfg->drive = parse_drive_or_die(optarg); + break; +#ifdef GPIOSET_INTERACTIVE + case 'i': + cfg->interactive = true; + break; +#endif + case 'l': + cfg->active_low = true; + break; + case 'p': + cfg->hold_period_us = parse_period_or_die(optarg); + break; + case 'Q': + cfg->unquoted = true; + break; + case 's': + cfg->strict = true; + break; + case 't': + cfg->toggles = parse_periods_or_die(optarg, + &cfg->toggle_periods); + break; + case 'z': + cfg->daemonize = true; + break; + case 'h': + print_help(); + exit(EXIT_SUCCESS); + case 'v': + print_version(); + exit(EXIT_SUCCESS); + case '?': + die("try %s --help", get_progname()); + case 0: + break; + default: + abort(); + } + } + +#ifdef GPIOSET_INTERACTIVE + if (cfg->daemonize && cfg->interactive) + die("can't combine daemonize with interactive"); + + if (cfg->toggles && cfg->interactive) + die("can't combine interactive with toggle"); +#endif + + return optind; +} + +static int parse_value(const char *option) +{ + if (strcmp(option, "0") == 0) + return 0; + if (strcmp(option, "1") == 0) + return 1; + if (strcmp(option, "inactive") == 0) + return 0; + if (strcmp(option, "active") == 0) + return 1; + if (strcmp(option, "off") == 0) + return 0; + if (strcmp(option, "on") == 0) + return 1; + if (strcmp(option, "false") == 0) + return 0; + if (strcmp(option, "true") == 0) + return 1; + return -1; +} + +/* parse num_lines line id and values from lvs into lines and values */ +static bool parse_line_values(int num_lines, char **lvs, char **lines, + int *values, bool interactive) +{ + int i; + char *value; + + for (i = 0; i < num_lines; i++) { + value = strchr(lvs[i], '='); + if (!value) { + if (interactive) + printf("invalid line value: '%s'\n", lvs[i]); + else + print_error("invalid line value: '%s'", lvs[i]); + + return false; + } + *value = '\0'; + value++; + values[i] = parse_value(value); + if (values[i] < 0) { + if (interactive) + printf("invalid line value: '%s'\n", value); + else + print_error("invalid line value: '%s'", value); + + return false; + } + lines[i] = lvs[i]; + } + return true; +} + +/* + * parse num_lines line id and values from lvs into lines and values, + * or die trying. + */ +static void parse_line_values_or_die(int num_lines, char **lvs, char **lines, + int *values) +{ + if (!parse_line_values(num_lines, lvs, lines, values, false)) + exit(EXIT_FAILURE); +} + +static void print_banner(int num_lines, char **lines) +{ + int i; + + if (num_lines > 1) { + printf("Setting lines "); + + for (i = 0; i < num_lines - 1; i++) + printf("'%s', ", lines[i]); + + printf("and '%s'...\n", lines[i]); + } else { + printf("Setting line '%s'...\n", lines[0]); + } + fflush(stdout); +} + +static void wait_fd(int fd) +{ + struct pollfd pfd; + + pfd.fd = fd; + pfd.events = POLLERR; + + if (poll(&pfd, 1, -1) < 0) + die_perror("error waiting on request"); +} + +/* + * Apply values from the resolver to the requests. + * offset and values are scratch pads for working. + */ +static void apply_values(struct gpiod_line_request **requests, + struct line_resolver *resolver, + unsigned int *offsets, int *values) +{ + int i; + + for (i = 0; i < resolver->num_chips; i++) { + get_line_offsets_and_values(resolver, i, offsets, values); + if (gpiod_line_request_set_values(requests[i], values)) + print_perror("unable to set values on '%s'", + get_chip_name(resolver, i)); + } +} + +/* toggle the values of all lines in the resolver */ +static void toggle_all_lines(struct line_resolver *resolver) +{ + int i; + + for (i = 0; i < resolver->num_lines; i++) + resolver->lines[i].value = !resolver->lines[i].value; +} + +/* + * toggle the resolved lines as specified by the toggle_periods, + * and apply the values to the requests. + * offset and values are scratch pads for working. + */ +static void toggle_sequence(int toggles, unsigned int *toggle_periods, + struct gpiod_line_request **requests, + struct line_resolver *resolver, + unsigned int *offsets, int *values) +{ + int i = 0; + + if ((toggles == 1) && (toggle_periods[0] == 0)) + return; + + for (;;) { + usleep(toggle_periods[i]); + toggle_all_lines(resolver); + apply_values(requests, resolver, offsets, values); + + i++; + if ((i == toggles - 1) && (toggle_periods[i] == 0)) + return; + + if (i == toggles) + i = 0; + } +} + + +#ifdef GPIOSET_INTERACTIVE + +/* + * set the values in the resolver for the line values specified by + * the remaining parameters. + */ +static void set_line_values_subset(struct line_resolver *resolver, + int num_lines, char **lines, int *values) +{ + int l, i; + + for (l = 0; l < num_lines; l++) + for (i = 0; i < resolver->num_lines; i++) + if (strcmp(lines[l], resolver->lines[i].id) == 0) { + resolver->lines[i].value = values[l]; + break; + } +} + +static void print_all_line_values(struct line_resolver *resolver, bool unquoted) +{ + int i; + char *fmt = unquoted ? "%s=%s " : "\"%s\"=%s "; + + for (i = 0; i < resolver->num_lines; i++) { + if (i == resolver->num_lines - 1) + fmt = unquoted ? "%s=%s\n" : "\"%s\"=%s\n"; + + printf(fmt, resolver->lines[i].id, + resolver->lines[i].value ? "active" : "inactive"); + } +} + +/* + * print the resovler line values for a subset of lines, + * specified by num_lines and lines. + */ +static void print_line_values(struct line_resolver *resolver, int num_lines, + char **lines, bool unquoted) +{ + int i, j; + char *fmt = unquoted ? "%s=%s " : "\"%s\"=%s "; + struct resolved_line *line; + + for (i = 0; i < num_lines; i++) { + if (i == num_lines - 1) + fmt = unquoted ? "%s=%s\n" : "\"%s\"=%s\n"; + + for (j = 0; j < resolver->num_lines; j++) { + line = &resolver->lines[j]; + if (strcmp(lines[i], line->id) == 0) { + printf(fmt, line->id, + line->value ? "active" : "inactive"); + break; + } + } + } +} + +/* + * toggle a subset of lines, specified by num_lines and lines, + * in the resolver. + */ +static void toggle_lines(struct line_resolver *resolver, int num_lines, + char **lines) +{ + int i, j; + struct resolved_line *line; + + for (i = 0; i < num_lines; i++) + for (j = 0; j < resolver->num_lines; j++) { + line = &resolver->lines[j]; + if (strcmp(lines[i], line->id) == 0) { + line->value = !line->value; + break; + } + } +} + +/* + * check that a set of lines, specified by num_lines and lines, + * are all resolved lines. + */ +static bool valid_lines(struct line_resolver *resolver, int num_lines, + char **lines) +{ + bool ret = true; + int i, l; + bool found; + + for (l = 0; l < num_lines; l++) { + found = false; + for (i = 0; i < resolver->num_lines; i++) { + if (strcmp(lines[l], resolver->lines[i].id) == 0) { + found = true; + break; + } + } + if (!found) { + printf("unknown line: '%s'\n", lines[l]); + ret = false; + } + } + return ret; +} + +static void print_interactive_help(void) +{ + printf("COMMANDS:\n\n"); + printf(" exit\n"); + printf(" Exit the program\n"); + printf(" get [line]...\n"); + printf(" Display the output values of the given requested lines\n\n"); + printf(" If no lines are specified then all requested lines are displayed\n\n"); + printf(" help\n"); + printf(" Print this help\n\n"); + printf(" set ...\n"); + printf(" Update the output values of the given requested lines\n\n"); + printf(" sleep \n"); + printf(" Sleep for the specified period\n\n"); + printf(" toggle [line]...\n"); + printf(" Toggle the output values of the given requested lines\n\n"); + printf(" If no lines are specified then all requested lines are toggled\n\n"); +} + +/* + * split a line into words, returning the each of the words and the count. + * + * max_words specifies the max number of words that may be returned in words. + */ +static int split_words(char *line, int max_words, char **words) +{ + int num_words = 0; + bool in_word = false; + + while (*line != '\0') { + if (!in_word && !isspace(*line)) { + in_word = true; + + /* count all words, but only store max_words */ + if (num_words < max_words) + words[num_words] = line; + + } else if (isspace(*line)) { + if (in_word) { + num_words++; + in_word = false; + } + *line = '\0'; + } + line++; + } + if (in_word) + num_words++; + + return num_words; +} + +/* check if a line is specified somewhere in the rl_line_buffer */ +static bool in_line_buffer(const char *id) +{ + int len = strlen(id); + char *match = rl_line_buffer; + + while ((match = strstr(rl_line_buffer, id))) { + if ((match > rl_line_buffer && isspace(match[-1])) && + (isspace(match[len]) || (match[len] == '='))) + return true; + + match += len; + } + + return false; +} + +/* context for complete_line_id, so it can provide valid line ids */ +static struct line_resolver *completion_context; + +/* tab completion helper for line ids */ +static char *complete_line_id(const char *text, int state) +{ + static int idx, len; + const char *id; + + if (!state) { + idx = 0; + len = strlen(text); + } + while (idx < completion_context->num_lines) { + id = completion_context->lines[idx].id; + idx++; + if ((strncmp(id, text, len) == 0) && + (!in_line_buffer(id))) + return strdup(id); + } + return NULL; +} + +/* tab completion helper for line values (just the value component) */ +static char *complete_value(const char *text, int state) +{ + static const char * const values[] = { + "1", "0", "active", "inactive", "on", "off", "true", "false", + NULL + }; + static int idx, len; + const char *value; + + if (!state) { + idx = 0; + len = strlen(text); + } + while ((value = values[idx])) { + idx++; + if (strncmp(value, text, len) == 0) + return strdup(value); + } + return NULL; +} + +/* tab completion help for interactive commands */ +static char *complete_command(const char *text, int state) +{ + static const char * const commands[] = { + "get", "set", "toggle", "sleep", "help", "exit", NULL + }; + static int idx, len; + const char *cmd; + + if (!state) { + idx = 0; + len = strlen(text); + } + + while ((cmd = commands[idx])) { + idx++; + if (strncmp(cmd, text, len) == 0) + return strdup(cmd); + } + return NULL; +} + +/* tab completion for interactive command lines */ +static char **tab_completion(const char *text, int start, int end) +{ + char **matches = NULL; + int cmd_start, cmd_end, len; + + rl_attempted_completion_over = true; + rl_completion_type = '@'; + + for (cmd_start = 0; + isspace(rl_line_buffer[cmd_start]) && cmd_start < end; + cmd_start++) + ; + + if (cmd_start == start) + matches = rl_completion_matches(text, complete_command); + + for (cmd_end = cmd_start + 1; + !isspace(rl_line_buffer[cmd_end]) && cmd_end < end; + cmd_end++) + ; + + len = cmd_end - cmd_start; + if (len == 3 && strncmp("set", &rl_line_buffer[cmd_start], 3) == 0) { + if (rl_line_buffer[start - 1] == '=') { + matches = rl_completion_matches(text, complete_value); + } else { + rl_completion_append_character = '='; + matches = rl_completion_matches(text, complete_line_id); + } + } + + if ((len == 3 && strncmp("get", &rl_line_buffer[cmd_start], 3) == 0) || + (len == 6 && strncmp("toggle", &rl_line_buffer[cmd_start], 6) == 0)) + matches = rl_completion_matches(text, complete_line_id); + + return matches; +} + +static void interact(struct gpiod_line_request **requests, + struct line_resolver *resolver, + char **lines, unsigned int *offsets, int *values, + bool unquoted) +{ + char *line; + int num_words, num_lines, max_words; + char **words; + int period_us, i; + char *line_buf; + bool done; + + stifle_history(20); + rl_attempted_completion_function = tab_completion; + completion_context = resolver; + + max_words = resolver->num_lines + 1; + words = calloc(max_words, sizeof(*words)); + if (!words) + die("out of memory"); + + for (done = false; !done;) { + /* + * manually print the prompt, as libedit doesn't if stdout + * is not a tty. And fflush to ensure the prompt and any + * output buffered from the previous command is sent. + */ + printf("gpioset> "); + fflush(stdout); + + line = readline(NULL); + if (!line || line[0] == '\0') + continue; + + for (i = strlen(line) - 1; (i > 0) && isspace(line[i]); i--) + line[i] = '\0'; + + line_buf = strdup(line); + num_words = split_words(line_buf, max_words, words); + if (num_words > max_words) { + printf("too many command parameters provided\n"); + goto cmd_done; + } + num_lines = num_words - 1; + if (strcmp(words[0], "get") == 0) { + if (num_lines == 0) + print_all_line_values(resolver, unquoted); + else if (valid_lines(resolver, num_lines, &words[1])) + print_line_values(resolver, num_lines, + &words[1], unquoted); + goto cmd_ok; + } + if (strcmp(words[0], "set") == 0) { + if (num_lines == 0) + printf("at least one GPIO line value must be specified\n"); + else if (parse_line_values(num_lines, &words[1], lines, + values, true) && + valid_lines(resolver, num_lines, lines)) { + set_line_values_subset(resolver, num_lines, + lines, values); + apply_values(requests, resolver, offsets, + values); + } + goto cmd_ok; + } + if (strcmp(words[0], "toggle") == 0) { + if (num_lines == 0) + toggle_all_lines(resolver); + else if (valid_lines(resolver, num_lines, &words[1])) + toggle_lines(resolver, num_lines, &words[1]); + + apply_values(requests, resolver, offsets, values); + goto cmd_ok; + } + if (strcmp(words[0], "sleep") == 0) { + if (num_lines == 0) { + printf("a period must be specified\n"); + goto cmd_ok; + } + if (num_lines > 1) { + printf("only one period can be specified\n"); + goto cmd_ok; + } + period_us = parse_period(words[1]); + if (period_us < 0) { + printf("invalid period: '%s'\n", words[1]); + goto cmd_ok; + } + usleep(period_us); + goto cmd_ok; + } + + if (strcmp(words[0], "exit") == 0) { + done = true; + goto cmd_done; + } + + if (strcmp(words[0], "help") == 0) { + print_interactive_help(); + goto cmd_done; + } + + printf("unknown command: '%s'\n", words[0]); + printf("Try the 'help' command\n") + ; + +cmd_ok: + for (i = 0; isspace(line[i]); i++) + ; + + if ((history_length) == 0 || + (strcmp(history_list()[history_length - 1]->line, + &line[i]) != 0)) + add_history(&line[i]); + +cmd_done: + free(line); + free(line_buf); + } + free(words); +} + +#endif /* GPIOSET_INTERACTIVE */ + +int main(int argc, char **argv) +{ + int i, j, num_lines, ret, *values; + struct gpiod_line_settings *settings; + struct gpiod_request_config *req_cfg; + struct gpiod_line_request **requests; + struct gpiod_line_config *line_cfg; + struct gpiod_chip *chip; + unsigned int *offsets; + struct line_resolver *resolver; + char **lines; + struct config cfg; + + i = parse_config(argc, argv, &cfg); + argc -= i; + argv += i; + + if (argc < 1) + die("at least one GPIO line value must be specified"); + + num_lines = argc; + + lines = calloc(num_lines, sizeof(*lines)); + values = calloc(num_lines, sizeof(*values)); + if (!lines || !values) + die("out of memory"); + + parse_line_values_or_die(argc, argv, lines, values); + + settings = gpiod_line_settings_new(); + if (!settings) + die_perror("unable to allocate line settings"); + + if (cfg.bias) + gpiod_line_settings_set_bias(settings, cfg.bias); + + if (cfg.drive) + gpiod_line_settings_set_drive(settings, cfg.drive); + + if (cfg.active_low) + gpiod_line_settings_set_active_low(settings, true); + + gpiod_line_settings_set_direction(settings, + GPIOD_LINE_DIRECTION_OUTPUT); + + req_cfg = gpiod_request_config_new(); + if (!req_cfg) + die_perror("unable to allocate the request config structure"); + + gpiod_request_config_set_consumer(req_cfg, cfg.consumer); + resolver = resolve_lines(num_lines, lines, cfg.chip_id, cfg.strict, + cfg.by_name); + validate_resolution(resolver, cfg.chip_id); + for (i = 0; i < num_lines; i++) + resolver->lines[i].value = values[i]; + + requests = calloc(resolver->num_chips, sizeof(*requests)); + offsets = calloc(num_lines, sizeof(*offsets)); + if (!requests || !offsets) + die("out of memory"); + + line_cfg = gpiod_line_config_new(); + if (!line_cfg) + die_perror("unable to allocate the line config structure"); + + for (i = 0; i < resolver->num_chips; i++) { + num_lines = get_line_offsets_and_values(resolver, i, + offsets, 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"); + } + + chip = gpiod_chip_open(resolver->chips[i].path); + if (!chip) + die_perror("unable to open chip '%s'", + resolver->chips[i].path); + + requests[i] = gpiod_chip_request_lines(chip, req_cfg, line_cfg); + if (!requests[i]) + die_perror("unable to request lines on chip '%s'", + resolver->chips[i].path); + + gpiod_chip_close(chip); + } + + gpiod_request_config_free(req_cfg); + gpiod_line_config_free(line_cfg); + gpiod_line_settings_free(settings); + + if (cfg.banner) + print_banner(argc, lines); + + if (cfg.daemonize) + if (daemon(0, 0) < 0) + die_perror("unable to daemonize"); + + if (cfg.toggles) { + for (i = 0; i < cfg.toggles; i++) + if ((cfg.hold_period_us > cfg.toggle_periods[i]) && + ((i != cfg.toggles - 1) || + cfg.toggle_periods[i] != 0)) + cfg.toggle_periods[i] = cfg.hold_period_us; + + toggle_sequence(cfg.toggles, cfg.toggle_periods, requests, + resolver, offsets, values); + free(cfg.toggle_periods); + } + + if (cfg.hold_period_us) + usleep(cfg.hold_period_us); + +#ifdef GPIOSET_INTERACTIVE + if (cfg.interactive) + interact(requests, resolver, lines, offsets, values, + cfg.unquoted); + else if (!cfg.toggles) + wait_fd(gpiod_line_request_get_fd(requests[0])); +#else + if (!cfg.toggles) + wait_fd(gpiod_line_request_get_fd(requests[0])); +#endif + + for (i = 0; i < resolver->num_chips; i++) + gpiod_line_request_release(requests[i]); + + free(requests); + free_line_resolver(resolver); + free(lines); + free(values); + free(offsets); + + return EXIT_SUCCESS; +} + diff --git a/tools/tools-common.c b/tools/tools-common.c index ea7dfa8..b6f4335 100644 --- a/tools/tools-common.c +++ b/tools/tools-common.c @@ -1,18 +1,22 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski +// SPDX-FileCopyrightText: 2022 Kent Gibson /* Common code for GPIO tools. */ #include +#include #include #include +#include #include -#include +#include #include #include #include #include -#include +#include +#include #include "tools-common.h" @@ -21,6 +25,28 @@ const char *get_progname(void) return program_invocation_name; } +void print_error(const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + fprintf(stderr, "%s: ", program_invocation_name); + vfprintf(stderr, fmt, va); + fprintf(stderr, "\n"); + va_end(va); +} + +void print_perror(const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + fprintf(stderr, "%s: ", program_invocation_name); + vfprintf(stderr, fmt, va); + fprintf(stderr, ": %s\n", strerror(errno)); + va_end(va); +} + void die(const char *fmt, ...) { va_list va; @@ -57,41 +83,305 @@ void print_version(void) printf("There is NO WARRANTY, to the extent permitted by law.\n"); } -int parse_bias(const char *option) +int parse_bias_or_die(const char *option) { if (strcmp(option, "pull-down") == 0) return GPIOD_LINE_BIAS_PULL_DOWN; if (strcmp(option, "pull-up") == 0) return GPIOD_LINE_BIAS_PULL_UP; - if (strcmp(option, "disable") == 0) - return GPIOD_LINE_BIAS_DISABLED; - if (strcmp(option, "as-is") != 0) + if (strcmp(option, "disabled") != 0) die("invalid bias: %s", option); - return 0; + return GPIOD_LINE_BIAS_DISABLED; +} + +int parse_period(const char *option) +{ + unsigned long p, m = 0; + char *end; + + p = strtoul(option, &end, 10); + switch (*end) { + case 'u': + m = 1; + end++; + break; + case 'm': + m = 1000; + end++; + break; + case 's': + m = 1000000; + break; + case '\0': + break; + default: + return -1; + } + + if (m) { + if (*end != 's') + return -1; + + end++; + } else { + m = 1000; + } + + p *= m; + if (*end != '\0' || p > INT_MAX) + return -1; + + return p; +} + +unsigned int parse_period_or_die(const char *option) +{ + int period = parse_period(option); + + if (period < 0) + die("invalid period: %s", option); + + return period; +} + +int parse_uint(const char *option) +{ + unsigned long o; + char *end; + + o = strtoul(option, &end, 10); + if (*end == '\0' && o <= INT_MAX) + return o; + + return -1; +} + +unsigned int parse_uint_or_die(const char *option) +{ + int i = parse_uint(option); + + if (i < 0) + die("invalid number: '%s'", option); + + return i; } void print_bias_help(void) { - printf("Biases:\n"); - printf(" as-is:\tleave bias unchanged\n"); - printf(" disable:\tdisable bias\n"); - printf(" pull-up:\tenable pull-up\n"); - printf(" pull-down:\tenable pull-down\n"); + printf(" -b, --bias \tspecify the line bias\n"); + printf("\t\t\tPossible values: 'pull-down', 'pull-up', 'disabled'.\n"); + printf("\t\t\t(default is to leave bias unchanged)\n"); +} + +void print_chip_help(void) +{ + printf("\nChips:\n"); + printf(" A GPIO chip may be identified by number, name, or path.\n"); + printf(" e.g. '0', 'gpiochip0', and '/dev/gpiochip0' all refer to the same chip.\n"); } -int chip_dir_filter(const struct dirent *entry) +void print_period_help(void) +{ + printf("\nPeriods:\n"); + printf(" Periods are taken as milliseconds unless units are specified. e.g. 10us.\n"); + printf(" Supported units are 's', 'ms', and 'us'.\n"); +} + +#define TIME_BUFFER_SIZE 20 + +/* + * format: + * 0: raw seconds + * 1: utc time + * 2: local time + */ +void print_event_time(uint64_t evtime, int format) +{ + time_t evtsec; + struct tm t; + char tbuf[TIME_BUFFER_SIZE]; + char *tz; + + if (format) { + evtsec = evtime / 1000000000; + if (format == 2) { + localtime_r(&evtsec, &t); + tz = ""; + } else { + gmtime_r(&evtsec, &t); + tz = "Z"; + } + strftime(tbuf, TIME_BUFFER_SIZE, "%FT%T", &t); + printf("%s.%09"PRIu64"%s", tbuf, evtime % 1000000000, tz); + } else { + printf("%"PRIu64".%09"PRIu64, + evtime / 1000000000, evtime % 1000000000); + } +} + +static void print_bias(struct gpiod_line_info *info) +{ + const char *name; + + switch (gpiod_line_info_get_bias(info)) { + case GPIOD_LINE_BIAS_PULL_UP: + name = "pull-up"; + break; + case GPIOD_LINE_BIAS_PULL_DOWN: + name = "pull-down"; + break; + case GPIOD_LINE_BIAS_DISABLED: + name = "disabled"; + break; + default: + return; + } + printf(" bias=%s", name); +} + +static void print_drive(struct gpiod_line_info *info) +{ + const char *name; + + switch (gpiod_line_info_get_drive(info)) { + case GPIOD_LINE_DRIVE_OPEN_DRAIN: + name = "open-drain"; + break; + case GPIOD_LINE_DRIVE_OPEN_SOURCE: + name = "open-source"; + break; + default: + return; + } + printf(" drive=%s", name); +} + +static void print_edge_detection(struct gpiod_line_info *info) +{ + const char *name; + + switch (gpiod_line_info_get_edge_detection(info)) { + case GPIOD_LINE_EDGE_BOTH: + name = "both"; + break; + case GPIOD_LINE_EDGE_RISING: + name = "rising"; + break; + case GPIOD_LINE_EDGE_FALLING: + name = "falling"; + break; + default: + return; + } + printf(" edges=%s", name); +} + +static void print_event_clock(struct gpiod_line_info *info) +{ + const char *name; + + switch (gpiod_line_info_get_event_clock(info)) { + case GPIOD_LINE_EVENT_CLOCK_REALTIME: + name = "realtime"; + break; + case GPIOD_LINE_EVENT_CLOCK_HTE: + name = "hte"; + break; + default: + return; + } + printf(" event-clock=%s", name); +} + +static void print_debounce(struct gpiod_line_info *info) +{ + unsigned long debounce; + const char *units = "us"; + + debounce = gpiod_line_info_get_debounce_period_us(info); + if (!debounce) + return; + if (debounce % 1000000 == 0) { + debounce /= 1000000; + units = "s"; + } else if (debounce % 1000 == 0) { + debounce /= 1000; + units = "ms"; + } + printf(" debounce-period=%lu%s", debounce, units); +} + +static void print_consumer(struct gpiod_line_info *info, bool unquoted) +{ + const char *consumer; + const char *fmt; + + if (!gpiod_line_info_is_used(info)) + return; + + consumer = gpiod_line_info_get_consumer(info); + if (!consumer) + consumer = "kernel"; + + fmt = unquoted ? " consumer=%s" : " consumer=\"%s\""; + + printf(fmt, consumer); +} + +void print_line_attributes(struct gpiod_line_info *info, bool unquoted_strings) +{ + int direction; + + direction = gpiod_line_info_get_direction(info); + + printf("%s", direction == GPIOD_LINE_DIRECTION_INPUT ? + "input" : "output"); + + if (gpiod_line_info_is_active_low(info)) + printf(" active-low"); + + print_drive(info); + print_bias(info); + print_edge_detection(info); + print_event_clock(info); + print_debounce(info); + print_consumer(info, unquoted_strings); +} + +void print_line_id(struct line_resolver *resolver, int chip_num, + unsigned int offset, const char *chip_id, bool unquoted) +{ + const char *lname; + const char *fmt; + + lname = get_line_name(resolver, chip_num, offset); + if (!lname) { + printf("%s %u", get_chip_name(resolver, chip_num), offset); + return; + } + if (chip_id) + printf("%s %u ", get_chip_name(resolver, chip_num), offset); + + fmt = unquoted ? "%s" : "\"%s\""; + printf(fmt, lname); +} + +static int chip_dir_filter(const struct dirent *entry) { - bool is_chip; char *path; - int ret; + int ret = 0; + struct stat sb; - ret = asprintf(&path, "/dev/%s", entry->d_name); - if (ret < 0) + if (asprintf(&path, "/dev/%s", entry->d_name) < 0) return 0; - is_chip = gpiod_is_gpiochip_device(path); + if ((lstat(path, &sb) == 0) && + (!S_ISLNK(sb.st_mode)) && + gpiod_is_gpiochip_device(path)) + ret = 1; + free(path); - return !!is_chip; + return ret; } static bool isuint(const char *str) @@ -101,3 +391,368 @@ static bool isuint(const char *str) return *str == '\0'; } + +bool chip_path_lookup(const char *id, char **path_ptr) +{ + char *path; + + if (isuint(id)) { + /* by number */ + if (asprintf(&path, "/dev/gpiochip%s", id) < 0) + return false; + } else if (strchr(id, '/')) { + /* by path */ + if (asprintf(&path, "%s", id) < 0) + return false; + } else { + /* by device name */ + if (asprintf(&path, "/dev/%s", id) < 0) + return false; + } + + if (!gpiod_is_gpiochip_device(path)) { + free(path); + return false; + } + + *path_ptr = path; + + return true; +} + +int chip_paths(const char *id, char ***paths_ptr) +{ + char *path; + char **paths; + + if (id == NULL) + return all_chip_paths(paths_ptr); + + if (!chip_path_lookup(id, &path)) + return 0; + + paths = malloc(sizeof(*paths)); + if (paths == NULL) + die("out of memory"); + + paths[0] = path; + *paths_ptr = paths; + + return 1; +} + +int all_chip_paths(char ***paths_ptr) +{ + int i, j, num_chips, ret = 0; + struct dirent **entries; + char **paths; + + num_chips = scandir("/dev/", &entries, chip_dir_filter, alphasort); + if (num_chips < 0) + die_perror("unable to scan /dev"); + + paths = calloc(num_chips, sizeof(*paths)); + if (paths == NULL) + die("out of memory"); + + for (i = 0; i < num_chips; i++) { + if (asprintf(&paths[i], "/dev/%s", entries[i]->d_name) < 0) { + for (j = 0; j < i; j++) + free(paths[j]); + + free(paths); + return 0; + } + } + + *paths_ptr = paths; + ret = num_chips; + + for (i = 0; i < num_chips; i++) + free(entries[i]); + + free(entries); + return ret; +} + +static bool resolve_line(struct line_resolver *resolver, + struct gpiod_line_info *info, + int chip_num) +{ + struct resolved_line *line; + const char *name; + int i; + unsigned int offset; + bool resolved = false; + + offset = gpiod_line_info_get_offset(info); + for (i = 0; i < resolver->num_lines; i++) { + line = &resolver->lines[i]; + /* already resolved by offset? */ + if (line->resolved && + (line->offset == offset) && + (line->chip_num == chip_num)) { + line->info = info; + resolver->num_found++; + resolved = true; + } + if (line->resolved && !resolver->strict) + continue; + + /* else resolve by name */ + name = gpiod_line_info_get_name(info); + if (name && (strcmp(line->id, name) == 0)) { + if (resolver->strict && line->resolved) + die("line '%s' is not unique", line->id); + line->offset = offset; + line->info = info; + line->chip_num = resolver->num_chips; + line->resolved = true; + resolver->num_found++; + resolved = true; + } + } + + return resolved; +} + +/* + * check for lines that can be identified by offset + * + * This only applies to the first chip, as otherwise the lines must be + * identified by name. + */ +bool resolve_lines_by_offset(struct line_resolver *resolver, + unsigned int num_lines) +{ + int i; + struct resolved_line *line; + bool used = false; + + for (i = 0; i < resolver->num_lines; i++) { + line = &resolver->lines[i]; + if ((line->id_as_offset != -1) && + (line->id_as_offset < (int)num_lines)) { + line->chip_num = 0; + line->offset = line->id_as_offset; + line->resolved = true; + used = true; + } + } + return used; +} + + +bool resolve_done(struct line_resolver *resolver) +{ + return (!resolver->strict && + resolver->num_found >= resolver->num_lines); +} + +struct line_resolver *resolver_init(int num_lines, char **lines, int num_chips, + bool strict, bool by_name) +{ + struct line_resolver *resolver; + struct resolved_line *line; + size_t resolver_size; + int i; + + resolver_size = sizeof(*resolver) + num_lines * sizeof(*line); + resolver = malloc(resolver_size); + if (resolver == NULL) + die("out of memory"); + + memset(resolver, 0, resolver_size); + + resolver->chips = calloc(num_chips, sizeof(struct resolved_chip)); + if (resolver->chips == NULL) + die("out of memory"); + memset(resolver->chips, 0, num_chips * sizeof(struct resolved_chip)); + + resolver->num_lines = num_lines; + resolver->strict = strict; + for (i = 0; i < num_lines; i++) { + line = &resolver->lines[i]; + line->id = lines[i]; + line->id_as_offset = by_name ? -1 : parse_uint(lines[i]); + line->chip_num = -1; + } + return resolver; +} + +struct line_resolver *resolve_lines(int num_lines, char **lines, + const char *chip_id, bool strict, bool by_name) +{ + struct line_resolver *resolver; + struct gpiod_chip *chip; + struct gpiod_chip_info *chip_info; + struct gpiod_line_info *line_info; + char **paths; + int num_chips, i, offset; + bool chip_used; + + if (chip_id == NULL) + by_name = true; + + num_chips = chip_paths(chip_id, &paths); + if (chip_id && (num_chips == 0)) + die("cannot find GPIO chip character device '%s'", chip_id); + + resolver = resolver_init(num_lines, lines, num_chips, strict, by_name); + + for (i = 0; (i < num_chips) && !resolve_done(resolver); i++) { + chip_used = false; + chip = gpiod_chip_open(paths[i]); + if (!chip) { + if ((errno == EACCES) && (chip_id == NULL)) { + free(paths[i]); + continue; + } + + die_perror("unable to open chip '%s'", paths[i]); + } + + chip_info = gpiod_chip_get_info(chip); + if (!chip_info) + die_perror("unable to get info for '%s'", paths[i]); + + num_lines = gpiod_chip_info_get_num_lines(chip_info); + + if (i == 0 && chip_id && !by_name) + chip_used = resolve_lines_by_offset(resolver, num_lines); + + for (offset = 0; + (offset < num_lines) && !resolve_done(resolver); + offset++) { + line_info = gpiod_chip_get_line_info(chip, offset); + if (!line_info) + die_perror("unable to read the info for line %d from %s", + offset, + gpiod_chip_info_get_name(chip_info)); + + if (resolve_line(resolver, line_info, i)) + chip_used = true; + else + gpiod_line_info_free(line_info); + + } + + gpiod_chip_close(chip); + + if (chip_used) { + resolver->chips[resolver->num_chips].info = chip_info; + resolver->chips[resolver->num_chips].path = paths[i]; + resolver->num_chips++; + } else { + gpiod_chip_info_free(chip_info); + free(paths[i]); + } + } + free(paths); + + return resolver; +} + +void validate_resolution(struct line_resolver *resolver, const char *chip_id) +{ + struct resolved_line *line, *prev; + int i, j; + bool valid = true; + + for (i = 0 ; i < resolver->num_lines; i++) { + line = &resolver->lines[i]; + if (line->resolved) { + for (j = 0; j < i; j++) { + prev = &resolver->lines[j]; + if (prev->resolved && + (prev->chip_num == line->chip_num) && + (prev->offset == line->offset)) { + print_error("lines '%s' and '%s' are the same line", + prev->id, line->id); + valid = false; + break; + } + } + continue; + } + valid = false; + if (chip_id && line->id_as_offset != -1) + print_error("offset %s is out of range on chip '%s'", + line->id, chip_id); + else + print_error("cannot find line '%s'", line->id); + } + if (!valid) + exit(EXIT_FAILURE); +} + +void free_line_resolver(struct line_resolver *resolver) +{ + int i; + + if (!resolver) + return; + + for (i = 0; i < resolver->num_lines; i++) + gpiod_line_info_free(resolver->lines[i].info); + + for (i = 0; i < resolver->num_chips; i++) { + gpiod_chip_info_free(resolver->chips[i].info); + free(resolver->chips[i].path); + } + + free(resolver->chips); + free(resolver); +} + +int get_line_offsets_and_values(struct line_resolver *resolver, + int chip_num, unsigned int *offsets, int *values) +{ + struct resolved_line *line; + int i, num_lines = 0; + + for (i = 0; i < resolver->num_lines; i++) { + line = &resolver->lines[i]; + if (line->chip_num == chip_num) { + offsets[num_lines] = line->offset; + if (values) + values[num_lines] = line->value; + + num_lines++; + } + } + return num_lines; +} + +const char *get_chip_name(struct line_resolver *resolver, int chip_num) +{ + return gpiod_chip_info_get_name(resolver->chips[chip_num].info); +} + +const char *get_line_name(struct line_resolver *resolver, + int chip_num, unsigned int offset) +{ + struct resolved_line *line; + int i; + + for (i = 0; i < resolver->num_lines; i++) { + line = &resolver->lines[i]; + if (line->info && (line->offset == offset) && + (line->chip_num == chip_num)) + return gpiod_line_info_get_name(resolver->lines[i].info); + } + + return 0; +} + +void set_line_values(struct line_resolver *resolver, int chip_num, int *values) +{ + int i, j; + + for (i = 0, j = 0; i < resolver->num_lines; i++) { + if (resolver->lines[i].chip_num == chip_num) { + resolver->lines[i].value = values[j]; + j++; + } + } +} diff --git a/tools/tools-common.h b/tools/tools-common.h index cb61d54..aa697de 100644 --- a/tools/tools-common.h +++ b/tools/tools-common.h @@ -1,10 +1,10 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ /* SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski */ +/* SPDX-FileCopyrightText: 2022 Kent Gibson */ #ifndef __GPIOD_TOOLS_COMMON_H__ #define __GPIOD_TOOLS_COMMON_H__ -#include #include /* @@ -21,16 +21,100 @@ #define GETOPT_NULL_LONGOPT NULL, 0, NULL, 0 +struct resolved_line { + /* from the command line */ + const char *id; + + /* + * id parsed as int, if that is an option, or -1 if line must be + * resolved by name + */ + int id_as_offset; + + /* line has been located on a chip */ + bool resolved; + + /* remaining fields only valid once resolved... */ + + /* info for the line */ + struct gpiod_line_info *info; + + /* num of relevant chip in line_resolver */ + int chip_num; + + /* offset of line on chip */ + uint offset; + + /* line value for gpioget/set */ + int value; +}; + +struct resolved_chip { + /* info of the relevant chips */ + struct gpiod_chip_info *info; + + /* path to the chip */ + char *path; +}; + +/* a resolver from requested line names/offsets to lines on the system */ +struct line_resolver { + /* + * number of chips the lines span, and number of entries in chips + */ + int num_chips; + + /* number of lines in lines */ + int num_lines; + + /* number of lines found */ + int num_found; + + /* perform exhaustive search to check line names are unique */ + bool strict; + + /* details of the relevant chips */ + struct resolved_chip *chips; + + /* descriptors for the requested lines */ + struct resolved_line lines[]; +}; + const char *get_progname(void); +void print_error(const char *fmt, ...) PRINTF(1, 2); +void print_perror(const char *fmt, ...) PRINTF(1, 2); void die(const char *fmt, ...) NORETURN PRINTF(1, 2); void die_perror(const char *fmt, ...) NORETURN PRINTF(1, 2); void print_version(void); -int parse_bias(const char *option); +int parse_bias_or_die(const char *option); +int parse_period(const char *option); +unsigned int parse_period_or_die(const char *option); +int parse_uint(const char *option); +unsigned int parse_uint_or_die(const char *option); void print_bias_help(void); -int make_signalfd(void); -int chip_dir_filter(const struct dirent *entry); -struct gpiod_chip *chip_open_by_name(const char *name); -struct gpiod_chip *chip_open_lookup(const char *device); -bool has_duplicate_offsets(size_t num_offsets, unsigned int *offsets); +void print_chip_help(void); +void print_period_help(void); +void print_event_time(uint64_t evtime, int format); +void print_line_attributes(struct gpiod_line_info *info, bool unquoted_strings); +void print_line_id(struct line_resolver *resolver, int chip_num, + unsigned int offset, const char *chip_id, bool unquoted); +bool chip_path_lookup(const char *id, char **path_ptr); +int chip_paths(const char *id, char ***paths_ptr); +int all_chip_paths(char ***paths_ptr); +struct line_resolver *resolve_lines(int num_lines, char **lines, + const char *chip_id, bool strict, bool by_name); +struct line_resolver *resolver_init(int num_lines, char **lines, int num_chips, + bool strict, bool by_name); +bool resolve_lines_by_offset(struct line_resolver *resolver, + unsigned int num_lines); +bool resolve_done(struct line_resolver *resolver); +void validate_resolution(struct line_resolver *resolver, const char *chip_id); +void free_line_resolver(struct line_resolver *resolver); +int get_line_offsets_and_values(struct line_resolver *resolver, + int chip_num, unsigned int *offsets, int *values); +const char *get_chip_name(struct line_resolver *resolver, int chip_num); +const char *get_line_name(struct line_resolver *resolver, int chip_num, + unsigned int offset); +void set_line_values(struct line_resolver *resolver, int chip_num, int *values); #endif /* __GPIOD_TOOLS_COMMON_H__ */ From patchwork Mon Nov 14 04:01:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 624967 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 640B5C433FE for ; Mon, 14 Nov 2022 04:01:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235768AbiKNEBv (ORCPT ); Sun, 13 Nov 2022 23:01:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235767AbiKNEBt (ORCPT ); Sun, 13 Nov 2022 23:01:49 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC8F912AFD for ; Sun, 13 Nov 2022 20:01:45 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id p21so8971452plr.7 for ; Sun, 13 Nov 2022 20:01:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.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=JVWJuURWZxKg8HrXGHKliKEhtW5c6phjRBF6o/pHKOY=; b=lEbHGesv6S+zB2QIexGieSGIG+EPspmpDiQNHGM0g7ta81YECzQ7InU22BxF1VR464 6PL/Q4W5Wmg0YKmF67YmmbKX+fgzsxHFRjD0I/TS0/xnTBAeW1f6OYkK03eYwAuDBTP1 4TeLqb0x1txIh0YBBHA+C8/ZK/5d6hpz2ejM0gL1YQ1Nh4S/4C93YetqCzixdejErsxw UcYgeO14VkwDAzZB0E3l3sY0E5I1kEAl8JZRkD9LB2ligUHq3DyB0cc19G//uBCFv9p/ xkLOK3Kn0L/9vTKVGl6F3CKn0VkkwGeO+fwycpigH3keJzhrx8hcN11RTwq4o281VpIM XYYQ== 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=JVWJuURWZxKg8HrXGHKliKEhtW5c6phjRBF6o/pHKOY=; b=hKvMS54Tt1Rkx7p+SKfVgaJ8U6ovlik5Bcj3ncnj3rqwswsWDB/i774roE50TqT2DX 6Dplqg+H5nf7XM+44ilCXPQUrVVd+ryLdtJF16DpVMduAJC7Sz4JPjEJw+f2T1Ge8FEK MpFIG5jyQYvXCVQAcv/AlueQqlxzL4qRM1omUNUv8s2nt0XZv15Yw4Ny6Jcp3Usk3kOe HafFY9uaKA0g5AQ7A/VraIj9SMuHwTMLMLQbEr7kAUbqvJHANpun1VMwWg4bqHAMhZcM bznoqFI8k/woBBTLo89hCEzT/d9q+9vLnc+ZxdQFE/DcdeRn9Jiel9IyT4XDaEDL1Irb ygzw== X-Gm-Message-State: ANoB5pkh1aoD9tYV8p0Bpx8OjqDlHLDWXbWCTT2HsgOwF2u7OOMGqsSe RLV1n926WUMM4mV3uraD6bvl2CG2/4M= X-Google-Smtp-Source: AA0mqf7BZ1xByFoZn31HEIP6ObuSxqtnbNGgF8FrD5YpozcvOHSS8AuajoQ3AG7qQRWDCee6NZ04mw== X-Received: by 2002:a17:902:8641:b0:183:6efe:65eb with SMTP id y1-20020a170902864100b001836efe65ebmr12420712plt.34.1668398503972; Sun, 13 Nov 2022 20:01:43 -0800 (PST) Received: from sol.home.arpa (14-200-229-209.tpgi.com.au. [14.200.229.209]) by smtp.gmail.com with ESMTPSA id f11-20020a170902684b00b001801aec1f6bsm6018636pln.141.2022.11.13.20.01.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:01:43 -0800 (PST) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod v2][PATCH v4 3/5] tools: tests for line name focussed rework Date: Mon, 14 Nov 2022 12:01:00 +0800 Message-Id: <20221114040102.66031-4-warthog618@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221114040102.66031-1-warthog618@gmail.com> References: <20221114040102.66031-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Rework the tools tests and expand to cover new functionality. Signed-off-by: Kent Gibson --- tools/gpio-tools-test | 3 - tools/gpio-tools-test.bats | 2369 ++++++++++++++++++++++++++++++++++-- 2 files changed, 2284 insertions(+), 88 deletions(-) diff --git a/tools/gpio-tools-test b/tools/gpio-tools-test index 234f9bd..1a012dc 100755 --- a/tools/gpio-tools-test +++ b/tools/gpio-tools-test @@ -37,9 +37,6 @@ check_prog() { # Check all required non-coreutils tools check_prog bats check_prog modprobe -check_prog rmmod -check_prog udevadm -check_prog timeout # Check if we're running a kernel at the required version or later check_kernel $MIN_KERNEL_VERSION diff --git a/tools/gpio-tools-test.bats b/tools/gpio-tools-test.bats index d200df1..78b0959 100755 --- a/tools/gpio-tools-test.bats +++ b/tools/gpio-tools-test.bats @@ -1,18 +1,24 @@ #!/usr/bin/env bats # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski +# SPDX-FileCopyrightText: 2022 Kent Gibson # Simple test harness for the gpio-tools. -# Where output from coprocesses is stored -COPROC_OUTPUT=$BATS_TMPDIR/gpio-tools-test-output +# Where output from the dut is stored +DUT_OUTPUT=$BATS_TMPDIR/gpio-tools-test-output # Save the PID of coprocess - otherwise we won't be able to wait for it # once it exits as the COPROC_PID will be cleared. -COPROC_SAVED_PID="" +DUT_PID="" -GPIOSIM_CHIPS="" -GPIOSIM_CONFIGFS="/sys/kernel/config/gpio-sim/" +# mappings from local name to system chip name, path, dev name +# -g required for the associative arrays, cos BATS... +declare -g -A GPIOSIM_CHIP_NAME +declare -g -A GPIOSIM_CHIP_PATH +declare -g -A GPIOSIM_DEV_NAME +GPIOSIM_CONFIGFS="/sys/kernel/config/gpio-sim" GPIOSIM_SYSFS="/sys/devices/platform/" +GPIOSIM_APP_NAME="gpio-tools-test" # Run the command in $* and return 0 if the command failed. The way we do it # here is a workaround for the way bats handles failing processes. @@ -23,10 +29,7 @@ assert_fail() { # Check if the string in $2 matches against the pattern in $1. regex_matches() { - local PATTERN=$1 - local STRING=$2 - - [[ $STRING =~ $PATTERN ]] + [[ $2 =~ $1 ]] || (echo "Mismatched: \"$2\"" && false) } # Iterate over all lines in the output of the last command invoked with bats' @@ -38,31 +41,39 @@ output_contains_line() { do test "$line" = "$LINE" && return 0 done - + echo "Mismatched:" + echo "$output" return 1 } +output_is() { + test "$output" = "$1" || (echo "Mismatched: \"$output\"" && false) +} + +num_lines_is() { + test ${#lines[@]} -eq $1 || (echo "Num lines is: ${#lines[@]}" && false) +} + +status_is() { + test "$status" -eq "$1" +} + # Same as above but match against the regex pattern in $1. output_regex_match() { - local PATTERN=$1 - for line in "${lines[@]}" do - regex_matches "$PATTERN" "$line" && return 0 + [[ "$line" =~ $1 ]] && return 0 done - + echo "Mismatched:" + echo "$output" return 1 } -random_name() { - cat /proc/sys/kernel/random/uuid -} - gpiosim_chip() { local VAR=$1 - local NAME=$(random_name) + local NAME=${GPIOSIM_APP_NAME}-$$-${VAR} local DEVPATH=$GPIOSIM_CONFIGFS/$NAME - local BANKPATH=$DEVPATH/$NAME + local BANKPATH=$DEVPATH/bank0 mkdir -p $BANKPATH @@ -87,75 +98,60 @@ gpiosim_chip() { echo 1 > $DEVPATH/live - GPIOSIM_CHIPS="$VAR:$NAME $GPIOSIM_CHIPS" + local chip_name=$(<$BANKPATH/chip_name) + GPIOSIM_CHIP_NAME[$1]=$chip_name + GPIOSIM_CHIP_PATH[$1]="/dev/$chip_name" + GPIOSIM_DEV_NAME[$1]=$(<$DEVPATH/dev_name) } -gpiosim_chip_map_name() { - local VAR=$1 - - for CHIP in $GPIOSIM_CHIPS - do - KEY=$(echo $CHIP | cut -d":" -f1) - VAL=$(echo $CHIP | cut -d":" -f2) - - if [ "$KEY" = "$VAR" ] - then - echo $VAL - fi - done +gpiosim_chip_number() { + local NAME=${GPIOSIM_CHIP_NAME[$1]} + echo ${NAME#"gpiochip"} } -gpiosim_chip_name() { - local VAR=$1 - local NAME=$(gpiosim_chip_map_name $VAR) - - cat $GPIOSIM_CONFIGFS/$NAME/$NAME/chip_name +gpiosim_chip_symlink() { + GPIOSIM_CHIP_LINK="$2/${GPIOSIM_APP_NAME}-$$-lnk" + ln -s ${GPIOSIM_CHIP_PATH[$1]} "$GPIOSIM_CHIP_LINK" } -gpiosim_dev_name() { - local VAR=$1 - local NAME=$(gpiosim_chip_map_name $VAR) - - cat $GPIOSIM_CONFIGFS/$NAME/dev_name +gpiosim_chip_symlink_cleanup() { + if [ -n "$GPIOSIM_CHIP_LINK" ] + then + rm "$GPIOSIM_CHIP_LINK" + fi + unset GPIOSIM_CHIP_LINK } gpiosim_set_pull() { - local VAR=$1 local OFFSET=$2 local PULL=$3 - local DEVNAME=$(gpiosim_dev_name $VAR) - local CHIPNAME=$(gpiosim_chip_name $VAR) + local DEVNAME=${GPIOSIM_DEV_NAME[$1]} + local CHIPNAME=${GPIOSIM_CHIP_NAME[$1]} echo $PULL > $GPIOSIM_SYSFS/$DEVNAME/$CHIPNAME/sim_gpio$OFFSET/pull } gpiosim_check_value() { - local VAR=$1 local OFFSET=$2 local EXPECTED=$3 - local DEVNAME=$(gpiosim_dev_name $VAR) - local CHIPNAME=$(gpiosim_chip_name $VAR) - - VAL=$(cat $GPIOSIM_SYSFS/$DEVNAME/$CHIPNAME/sim_gpio$OFFSET/value) - if [ "$VAL" = "$EXPECTED" ] - then - return 0 - fi + local DEVNAME=${GPIOSIM_DEV_NAME[$1]} + local CHIPNAME=${GPIOSIM_CHIP_NAME[$1]} - return 1 + VAL=$(<$GPIOSIM_SYSFS/$DEVNAME/$CHIPNAME/sim_gpio$OFFSET/value) + [ "$VAL" = "$EXPECTED" ] } gpiosim_cleanup() { - for CHIP in $GPIOSIM_CHIPS + for CHIP in ${!GPIOSIM_CHIP_NAME[@]} do - local NAME=$(echo $CHIP | cut -d":" -f2) + local NAME=${GPIOSIM_APP_NAME}-$$-$CHIP local DEVPATH=$GPIOSIM_CONFIGFS/$NAME - local BANKPATH=$DEVPATH/$NAME + local BANKPATH=$DEVPATH/bank0 echo 0 > $DEVPATH/live - ls $BANKPATH/line* 2> /dev/null + ls $BANKPATH/line* > /dev/null 2>&1 if [ "$?" = "0" ] then for LINE in $(find $BANKPATH/ | egrep "line[0-9]+$") @@ -169,7 +165,11 @@ gpiosim_cleanup() { rmdir $DEVPATH done - GPIOSIM_CHIPS="" + gpiosim_chip_symlink_cleanup + + GPIOSIM_CHIP_NAME=() + GPIOSIM_CHIP_PATH=() + GPIOSIM_DEV_NAME=() } run_tool() { @@ -178,45 +178,2244 @@ run_tool() { run timeout 10s $BATS_TEST_DIRNAME/"$@" } -coproc_run_tool() { - rm -f $BR_PROC_OUTPUT - coproc timeout 10s $BATS_TEST_DIRNAME/"$@" > $COPROC_OUTPUT 2> $COPROC_OUTPUT - COPROC_SAVED_PID=$COPROC_PID - # FIXME We're giving the background process some time to get up, but really this - # should be more reliable... +dut_run() { + coproc timeout 10s $BATS_TEST_DIRNAME/"$@" 2>&1 + DUT_PID=$COPROC_PID + read -t1 -n1 -u ${COPROC[0]} DUT_FIRST_CHAR +} + +dut_run_redirect() { + coproc timeout 10s $BATS_TEST_DIRNAME/"$@" > $DUT_OUTPUT 2>&1 + DUT_PID=$COPROC_PID + # give the process time to spin up + # FIXME - find a better solution sleep 0.2 } -coproc_tool_stdin_write() { +dut_read_redirect() { + output=$(<$DUT_OUTPUT) + local ORIG_IFS="$IFS" + IFS=$'\n' lines=($output) + IFS="$ORIG_IFS" +} + +dut_read() { + local LINE + lines=() + while read -t 0.2 -u ${COPROC[0]} LINE; + do + if [ -n "$DUT_FIRST_CHAR" ] + then + LINE=${DUT_FIRST_CHAR}${LINE} + unset DUT_FIRST_CHAR + fi + lines+=("$LINE") + done + output="${lines[@]}" +} + +dut_readable() { + read -t 0 -u ${COPROC[0]} LINE +} + +dut_flush() { + local JUNK + lines=() + output= + unset DUT_FIRST_CHAR + while read -t 0 -u ${COPROC[0]} JUNK; + do + read -t 0.1 -u ${COPROC[0]} JUNK || true + done +} + +# check the next line of output matches the regex +dut_regex_match() { + PATTERN=$1 + + read -t 0.2 -u ${COPROC[0]} LINE || (echo Timeout && false) + if [ -n "$DUT_FIRST_CHAR" ] + then + LINE=${DUT_FIRST_CHAR}${LINE} + unset DUT_FIRST_CHAR + fi + [[ $LINE =~ $PATTERN ]] || (echo "Mismatched: \"$LINE\"" && false) +} + +dut_write() { echo $* >&${COPROC[1]} } -coproc_tool_kill() { +dut_kill() { SIGNUM=$1 - kill $SIGNUM $COPROC_SAVED_PID + kill $SIGNUM $DUT_PID } -coproc_tool_wait() { +dut_wait() { status="0" # A workaround for the way bats handles command failures. - wait $COPROC_SAVED_PID || export status=$? + wait $DUT_PID || export status=$? test "$status" -ne 0 || export status="0" - output=$(cat $COPROC_OUTPUT) - local ORIG_IFS="$IFS" - IFS=$'\n' lines=($output) - IFS="$ORIG_IFS" - rm -f $COPROC_OUTPUT + unset DUT_PID } -teardown() { - if [ -n "$BG_PROC_PID" ] - then - kill -9 $BG_PROC_PID - run wait $BG_PROC_PID - BG_PROC_PID="" - fi +dut_cleanup() { + if [ -n "$DUT_PID" ] + then + kill -SIGTERM $DUT_PID + wait $DUT_PID || false + fi + rm -f $DUT_OUTPUT +} +teardown() { + dut_cleanup gpiosim_cleanup } +request_release_line() { + $BATS_TEST_DIRNAME/gpioget -c $* >/dev/null +} + +# +# gpiodetect test cases +# + +@test "gpiodetect: all chips" { + gpiosim_chip sim0 num_lines=4 + gpiosim_chip sim1 num_lines=8 + gpiosim_chip sim2 num_lines=16 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + local sim2=${GPIOSIM_CHIP_NAME[sim2]} + local sim0dev=${GPIOSIM_DEV_NAME[sim0]} + local sim1dev=${GPIOSIM_DEV_NAME[sim1]} + local sim2dev=${GPIOSIM_DEV_NAME[sim2]} + + run_tool gpiodetect + + output_contains_line "$sim0 [${sim0dev}-node0] (4 lines)" + output_contains_line "$sim1 [${sim1dev}-node0] (8 lines)" + output_contains_line "$sim2 [${sim2dev}-node0] (16 lines)" + status_is 0 + + # ignoring symlinks + local initial_output=$output + gpiosim_chip_symlink sim1 /dev + + run_tool gpiodetect + + output_is "$initial_output" + status_is 0 +} + +@test "gpiodetect: a chip" { + gpiosim_chip sim0 num_lines=4 + gpiosim_chip sim1 num_lines=8 + gpiosim_chip sim2 num_lines=16 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + local sim2=${GPIOSIM_CHIP_NAME[sim2]} + local sim0dev=${GPIOSIM_DEV_NAME[sim0]} + local sim1dev=${GPIOSIM_DEV_NAME[sim1]} + local sim2dev=${GPIOSIM_DEV_NAME[sim2]} + + # by name + run_tool gpiodetect $sim0 + + output_contains_line "$sim0 [${sim0dev}-node0] (4 lines)" + num_lines_is 1 + status_is 0 + + # by path + run_tool gpiodetect ${GPIOSIM_CHIP_PATH[sim1]} + + output_contains_line "$sim1 [${sim1dev}-node0] (8 lines)" + num_lines_is 1 + status_is 0 + + # by number + run_tool gpiodetect $(gpiosim_chip_number sim2) + + output_contains_line "$sim2 [${sim2dev}-node0] (16 lines)" + num_lines_is 1 + status_is 0 + + # by symlink + gpiosim_chip_symlink sim2 . + run_tool gpiodetect $GPIOSIM_CHIP_LINK + + output_contains_line "$sim2 [${sim2dev}-node0] (16 lines)" + num_lines_is 1 + status_is 0 +} + +@test "gpiodetect: multiple chips" { + gpiosim_chip sim0 num_lines=4 + gpiosim_chip sim1 num_lines=8 + gpiosim_chip sim2 num_lines=16 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + local sim2=${GPIOSIM_CHIP_NAME[sim2]} + local sim0dev=${GPIOSIM_DEV_NAME[sim0]} + local sim1dev=${GPIOSIM_DEV_NAME[sim1]} + local sim2dev=${GPIOSIM_DEV_NAME[sim2]} + + run_tool gpiodetect $sim0 $sim1 $sim2 + + output_contains_line "$sim0 [${sim0dev}-node0] (4 lines)" + output_contains_line "$sim1 [${sim1dev}-node0] (8 lines)" + output_contains_line "$sim2 [${sim2dev}-node0] (16 lines)" + num_lines_is 3 + status_is 0 +} + +@test "gpiodetect: with nonexistent chip" { + run_tool gpiodetect nonexistent-chip + + status_is 1 + output_regex_match \ +".*cannot find GPIO chip character device 'nonexistent-chip'" +} + +# +# gpioinfo test cases +# + +@test "gpioinfo: all chips" { + gpiosim_chip sim0 num_lines=4 + gpiosim_chip sim1 num_lines=8 + + run_tool gpioinfo + + echo "$output" + output_contains_line "${GPIOSIM_CHIP_NAME[sim0]} - 4 lines:" + output_contains_line "${GPIOSIM_CHIP_NAME[sim1]} - 8 lines:" + output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+7:\\s+unnamed\\s+input" + status_is 0 + + # ignoring symlinks + local initial_output=$output + gpiosim_chip_symlink sim1 /dev + + run_tool gpioinfo + + output_is "$initial_output" + status_is 0 +} + +@test "gpioinfo: all chips with some used lines" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + gpiosim_chip sim1 num_lines=8 line_name=3:baz line_name=4:xyz + + dut_run gpioset --banner --active-low foo=1 baz=0 + + run_tool gpioinfo + + output_contains_line "${GPIOSIM_CHIP_NAME[sim0]} - 4 lines:" + output_contains_line "${GPIOSIM_CHIP_NAME[sim1]} - 8 lines:" + output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+input" + output_regex_match \ +"\\s+line\\s+1:\\s+\"foo\"\\s+output active-low consumer=\"gpioset\"" + output_regex_match \ +"\\s+line\\s+3:\\s+\"baz\"\\s+output active-low consumer=\"gpioset\"" + status_is 0 +} + +@test "gpioinfo: a chip" { + gpiosim_chip sim0 num_lines=8 + gpiosim_chip sim1 num_lines=4 + + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + + # by name + run_tool gpioinfo --chip $sim1 + + output_contains_line "$sim1 - 4 lines:" + output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+1:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+2:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+3:\\s+unnamed\\s+input" + num_lines_is 5 + status_is 0 + + # by path + run_tool gpioinfo --chip $sim1 + + output_contains_line "$sim1 - 4 lines:" + output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+1:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+2:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+3:\\s+unnamed\\s+input" + num_lines_is 5 + status_is 0 + + # by number + run_tool gpioinfo --chip $sim1 + + output_contains_line "$sim1 - 4 lines:" + output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+1:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+2:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+3:\\s+unnamed\\s+input" + num_lines_is 5 + status_is 0 + + # by symlink + gpiosim_chip_symlink sim1 . + run_tool gpioinfo --chip $GPIOSIM_CHIP_LINK + + output_contains_line "$sim1 - 4 lines:" + output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+1:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+2:\\s+unnamed\\s+input" + output_regex_match "\\s+line\\s+3:\\s+unnamed\\s+input" + num_lines_is 5 + status_is 0 +} + +@test "gpioinfo: a line" { + gpiosim_chip sim0 num_lines=8 line_name=5:bar + gpiosim_chip sim1 num_lines=4 line_name=2:bar + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + + # by offset + run_tool gpioinfo --chip $sim1 2 + + output_regex_match "$sim1 2\\s+\"bar\"\\s+input" + num_lines_is 1 + status_is 0 + + # by name + run_tool gpioinfo bar + + output_regex_match "$sim0 5\\s+\"bar\"\\s+input" + num_lines_is 1 + status_is 0 + + # by chip and name + run_tool gpioinfo --chip $sim1 2 + + output_regex_match "$sim1 2\\s+\"bar\"\\s+input" + num_lines_is 1 + status_is 0 +} + +@test "gpioinfo: first matching named line" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz line_name=7:foobar + gpiosim_chip sim2 num_lines=16 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioinfo foobar + + output_regex_match "$sim0 3\\s+\"foobar\"\\s+input" + num_lines_is 1 + status_is 0 +} + +@test "gpioinfo: multiple lines" { + gpiosim_chip sim0 num_lines=8 line_name=5:bar + gpiosim_chip sim1 num_lines=4 line_name=2:baz + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + + # by offset + run_tool gpioinfo --chip $sim1 1 2 + + output_regex_match "$sim1 1\\s+unnamed\\s+input" + output_regex_match "$sim1 2\\s+\"baz\"\\s+input" + num_lines_is 2 + status_is 0 + + # by name + run_tool gpioinfo bar baz + + output_regex_match "$sim0 5\\s+\"bar\"\\s+input" + output_regex_match "$sim1 2\\s+\"baz\"\\s+input" + num_lines_is 2 + status_is 0 + + # by name and offset + run_tool gpioinfo --chip $sim0 bar 3 + + output_regex_match "$sim0 5\\s+\"bar\"\\s+input" + output_regex_match "$sim0 3\\s+unnamed\\s+input" + num_lines_is 2 + status_is 0 +} + +@test "gpioinfo: line attribute menagerie" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo + gpiosim_chip sim1 num_lines=8 line_name=3:baz + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + + dut_run gpioset --banner --active-low --bias=pull-up --drive=open-source foo=1 baz=0 + + run_tool gpioinfo foo baz + + output_regex_match \ +"$sim0 1\\s+\"foo\"\\s+output active-low drive=open-source bias=pull-up consumer=\"gpioset\"" + output_regex_match \ +"$sim1 3\\s+\"baz\"\\s+output active-low drive=open-source bias=pull-up consumer=\"gpioset\"" + num_lines_is 2 + status_is 0 + + dut_kill + dut_wait + + dut_run gpioset --banner --bias=pull-down --drive=open-drain foo=1 baz=0 + + run_tool gpioinfo foo baz + + output_regex_match \ +"$sim0 1\\s+\"foo\"\\s+output drive=open-drain bias=pull-down consumer=\"gpioset\"" + output_regex_match \ +"$sim1 3\\s+\"baz\"\\s+output drive=open-drain bias=pull-down consumer=\"gpioset\"" + num_lines_is 2 + status_is 0 + + dut_kill + dut_wait + + dut_run gpiomon --banner --bias=disabled --utc -p 10ms foo baz + + run_tool gpioinfo foo baz + + output_regex_match \ +"$sim0 1\\s+\"foo\"\\s+input bias=disabled edges=both event-clock=realtime debounce-period=10ms consumer=\"gpiomon\"" + output_regex_match \ +"$sim1 3\\s+\"baz\"\\s+input bias=disabled edges=both event-clock=realtime debounce-period=10ms consumer=\"gpiomon\"" + num_lines_is 2 + status_is 0 + + dut_kill + dut_wait + + dut_run gpiomon --banner --edges=rising --localtime foo baz + + run_tool gpioinfo foo baz + + output_regex_match \ +"$sim0 1\\s+\"foo\"\\s+input edges=rising event-clock=realtime consumer=\"gpiomon\"" + output_regex_match \ +"$sim1 3\\s+\"baz\"\\s+input edges=rising event-clock=realtime consumer=\"gpiomon\"" + num_lines_is 2 + status_is 0 + + dut_kill + dut_wait + + dut_run gpiomon --banner --edges=falling foo baz + + run_tool gpioinfo foo baz + + output_regex_match \ +"$sim0 1\\s+\"foo\"\\s+input edges=falling consumer=\"gpiomon\"" + output_regex_match \ +"$sim1 3\\s+\"baz\"\\s+input edges=falling consumer=\"gpiomon\"" + num_lines_is 2 + status_is 0 +} + +@test "gpioinfo: with same line twice" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # by offset + run_tool gpioinfo --chip $sim0 1 1 + + output_regex_match "$sim0 1\\s+\"foo\"\\s+input" + output_regex_match ".*lines '1' and '1' are the same line" + num_lines_is 2 + status_is 1 + + # by name + run_tool gpioinfo foo foo + + output_regex_match "$sim0 1\\s+\"foo\"\\s+input" + output_regex_match ".*lines 'foo' and 'foo' are the same line" + num_lines_is 2 + status_is 1 + + # by name and offset + run_tool gpioinfo --chip $sim0 foo 1 + + output_regex_match "$sim0 1\\s+\"foo\"\\s+input" + output_regex_match ".*lines 'foo' and '1' are the same line" + num_lines_is 2 + status_is 1 +} + +@test "gpioinfo: all lines matching name" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz line_name=7:foobar + gpiosim_chip sim2 num_lines=16 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + + run_tool gpioinfo --strict foobar + + output_regex_match "$sim0 3\\s+\"foobar\"\\s+input" + output_regex_match "$sim1 2\\s+\"foobar\"\\s+input" + output_regex_match "$sim1 7\\s+\"foobar\"\\s+input" + num_lines_is 3 + status_is 1 +} + +@test "gpioinfo: with lines strictly by name" { + # not suggesting this setup makes any sense + # - just test that we can deal with it + gpiosim_chip sim0 num_lines=8 line_name=1:6 line_name=6:1 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # first by offset (to show offsets match first) + run_tool gpioinfo --chip $sim0 1 6 + + output_regex_match "$sim0 1\\s+\"6\"\\s+input" + output_regex_match "$sim0 6\\s+\"1\"\\s+input" + num_lines_is 2 + status_is 0 + + # then strictly by name + run_tool gpioinfo --by-name --chip $sim0 1 + + output_regex_match "$sim0 6\\s+\"1\"\\s+input" + num_lines_is 1 + status_is 0 +} + +@test "gpioinfo: with nonexistent chip" { + run_tool gpioinfo --chip nonexistent-chip + + output_regex_match \ +".*cannot find GPIO chip character device 'nonexistent-chip'" + status_is 1 +} + +@test "gpioinfo: with nonexistent line" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpioinfo nonexistent-line + + output_regex_match ".*cannot find line 'nonexistent-line'" + status_is 1 + + run_tool gpioinfo --chip ${GPIOSIM_CHIP_NAME[sim0]} nonexistent-line + + output_regex_match ".*cannot find line 'nonexistent-line'" + status_is 1 +} + +@test "gpioinfo: with offset out of range" { + gpiosim_chip sim0 num_lines=4 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioinfo --chip $sim0 0 1 2 3 4 5 + + output_regex_match "$sim0 0\\s+unnamed\\s+input" + output_regex_match "$sim0 1\\s+unnamed\\s+input" + output_regex_match "$sim0 2\\s+unnamed\\s+input" + output_regex_match "$sim0 3\\s+unnamed\\s+input" + output_regex_match ".*offset 4 is out of range on chip '$sim0'" + output_regex_match ".*offset 5 is out of range on chip '$sim0'" + num_lines_is 6 + status_is 1 +} + +# +# gpioget test cases +# + +@test "gpioget: by name" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + gpiosim_set_pull sim0 1 pull-up + + run_tool gpioget foo + + output_is "\"foo\"=active" + status_is 0 + + run_tool gpioget --unquoted foo + + output_is "foo=active" + status_is 0 +} + +@test "gpioget: by offset" { + gpiosim_chip sim0 num_lines=8 + + gpiosim_set_pull sim0 1 pull-up + + run_tool gpioget --chip ${GPIOSIM_CHIP_NAME[sim0]} 1 + + output_is "\"1\"=active" + status_is 0 + + run_tool gpioget --unquoted --chip ${GPIOSIM_CHIP_NAME[sim0]} 1 + + output_is "1=active" + status_is 0 +} + +@test "gpioget: by symlink" { + gpiosim_chip sim0 num_lines=8 + gpiosim_chip_symlink sim0 . + + gpiosim_set_pull sim0 1 pull-up + + run_tool gpioget --chip $GPIOSIM_CHIP_LINK 1 + + output_is "\"1\"=active" + status_is 0 +} + +@test "gpioget: by chip and name" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + gpiosim_chip sim1 num_lines=8 line_name=3:foo + + gpiosim_set_pull sim1 3 pull-up + + run_tool gpioget --chip ${GPIOSIM_CHIP_NAME[sim1]} foo + + output_is "\"foo\"=active" + status_is 0 + + run_tool gpioget --unquoted --chip ${GPIOSIM_CHIP_NAME[sim1]} foo + + output_is "foo=active" + status_is 0 +} + +@test "gpioget: first matching named line" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar line_name=7:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz + gpiosim_chip sim2 num_lines=16 + + gpiosim_set_pull sim0 3 pull-up + + run_tool gpioget foobar + + output_is "\"foobar\"=active" + status_is 0 +} + +@test "gpioget: multiple lines" { + gpiosim_chip sim0 num_lines=8 + + gpiosim_set_pull sim0 2 pull-up + gpiosim_set_pull sim0 3 pull-up + gpiosim_set_pull sim0 5 pull-up + gpiosim_set_pull sim0 7 pull-up + + run_tool gpioget --unquoted --chip ${GPIOSIM_CHIP_NAME[sim0]} 0 1 2 3 4 5 6 7 + + output_is \ +"0=inactive 1=inactive 2=active 3=active 4=inactive 5=active 6=inactive 7=active" + status_is 0 +} + +@test "gpioget: multiple lines by name and offset" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=6:bar + gpiosim_chip sim1 num_lines=8 line_name=1:baz line_name=3:bar + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 1 pull-up + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 6 pull-up + + run_tool gpioget --chip $sim0 0 foo 4 bar + + output_is "\"0\"=inactive \"foo\"=active \"4\"=active \"bar\"=active" + status_is 0 +} + +@test "gpioget: multiple lines across multiple chips" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=4:xyz + + gpiosim_set_pull sim0 1 pull-up + gpiosim_set_pull sim1 4 pull-up + + run_tool gpioget baz bar foo xyz + + output_is "\"baz\"=inactive \"bar\"=inactive \"foo\"=active \"xyz\"=active" + status_is 0 +} + +@test "gpioget: with numeric values" { + gpiosim_chip sim0 num_lines=8 + + gpiosim_set_pull sim0 2 pull-up + gpiosim_set_pull sim0 3 pull-up + gpiosim_set_pull sim0 5 pull-up + gpiosim_set_pull sim0 7 pull-up + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioget --numeric --chip $sim0 0 1 2 3 4 5 6 7 + + output_is "0 0 1 1 0 1 0 1" + status_is 0 +} + +@test "gpioget: with active-low" { + gpiosim_chip sim0 num_lines=8 + + gpiosim_set_pull sim0 2 pull-up + gpiosim_set_pull sim0 3 pull-up + gpiosim_set_pull sim0 5 pull-up + gpiosim_set_pull sim0 7 pull-up + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioget --active-low --unquoted --chip $sim0 0 1 2 3 4 5 6 7 + + output_is \ +"0=active 1=active 2=inactive 3=inactive 4=active 5=inactive 6=active 7=inactive" + status_is 0 +} + +@test "gpioget: with consumer" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + gpiosim_chip sim1 num_lines=8 line_name=3:baz line_name=4:xyz + + dut_run gpionotify --banner -F "%l %E %C" foo baz + + run_tool gpioget --consumer gpio-tools-tests foo baz + status_is 0 + + dut_read + output_regex_match "foo requested gpio-tools-tests" + output_regex_match "baz requested gpio-tools-tests" +} + +@test "gpioget: with pull-up" { + gpiosim_chip sim0 num_lines=8 + + gpiosim_set_pull sim0 2 pull-up + gpiosim_set_pull sim0 3 pull-up + gpiosim_set_pull sim0 5 pull-up + gpiosim_set_pull sim0 7 pull-up + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioget --bias=pull-up --unquoted --chip $sim0 0 1 2 3 4 5 6 7 + + output_is \ +"0=active 1=active 2=active 3=active 4=active 5=active 6=active 7=active" + status_is 0 +} + +@test "gpioget: with pull-down" { + gpiosim_chip sim0 num_lines=8 + + gpiosim_set_pull sim0 2 pull-up + gpiosim_set_pull sim0 3 pull-up + gpiosim_set_pull sim0 5 pull-up + gpiosim_set_pull sim0 7 pull-up + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioget --bias=pull-down --unquoted --chip $sim0 0 1 2 3 4 5 6 7 + + output_is \ +"0=inactive 1=inactive 2=inactive 3=inactive 4=inactive 5=inactive 6=inactive 7=inactive" + status_is 0 +} + +@test "gpioget: with direction as-is" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # flip to output + run_tool gpioset -t0 foo=1 + + status_is 0 + + run_tool gpioinfo foo + output_regex_match "$sim0 1\\s+\"foo\"\\s+output" + status_is 0 + + run_tool gpioget --as-is foo + # note gpio-sim reverts line to its pull when released + output_is "\"foo\"=inactive" + status_is 0 + + run_tool gpioinfo foo + output_regex_match "$sim0 1\\s+\"foo\"\\s+output" + status_is 0 + + # whereas the default behaviour forces to input + run_tool gpioget foo + # note gpio-sim reverts line to its pull when released + # (defaults to pull-down) + output_is "\"foo\"=inactive" + status_is 0 + + run_tool gpioinfo foo + output_regex_match "$sim0 1\\s+\"foo\"\\s+input" + status_is 0 +} + +@test "gpioget: with hold-period" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + # only test parsing - testing the hold-period itself is tricky + run_tool gpioget --hold-period=100ms foo + output_is "\"foo\"=inactive" + status_is 0 +} + +@test "gpioget: with strict named line check" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz line_name=7:foobar + gpiosim_chip sim2 num_lines=16 + + run_tool gpioget --strict foobar + + output_regex_match ".*line 'foobar' is not unique" + status_is 1 +} + +@test "gpioget: with lines by offset" { + # not suggesting this setup makes any sense + # - just test that we can deal with it + gpiosim_chip sim0 num_lines=8 line_name=1:6 line_name=6:1 + + gpiosim_set_pull sim0 1 pull-up + gpiosim_set_pull sim0 6 pull-down + + run_tool gpioget --chip ${GPIOSIM_CHIP_NAME[sim0]} 1 6 + + output_is "\"1\"=active \"6\"=inactive" + status_is 0 + + run_tool gpioget --unquoted --chip ${GPIOSIM_CHIP_NAME[sim0]} 1 6 + + output_is "1=active 6=inactive" + status_is 0 +} + +@test "gpioget: with lines strictly by name" { + # not suggesting this setup makes any sense + # - just test that we can deal with it + gpiosim_chip sim0 num_lines=8 line_name=1:6 line_name=6:1 + + gpiosim_set_pull sim0 1 pull-up + gpiosim_set_pull sim0 6 pull-down + + run_tool gpioget --by-name --chip ${GPIOSIM_CHIP_NAME[sim0]} 1 6 + + output_is "\"1\"=inactive \"6\"=active" + status_is 0 + + run_tool gpioget --by-name --unquoted --chip ${GPIOSIM_CHIP_NAME[sim0]} 1 6 + + output_is "1=inactive 6=active" + status_is 0 +} + +@test "gpioget: with no arguments" { + run_tool gpioget + + output_regex_match ".*at least one GPIO line must be specified" + status_is 1 +} + +@test "gpioget: with chip but no line specified" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpioget --chip ${GPIOSIM_CHIP_NAME[sim0]} + + output_regex_match ".*at least one GPIO line must be specified" + status_is 1 +} + +@test "gpioget: with offset out of range" { + gpiosim_chip sim0 num_lines=4 + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioget --chip $sim0 0 1 2 3 4 5 + + output_regex_match ".*offset 4 is out of range on chip '$sim0'" + output_regex_match ".*offset 5 is out of range on chip '$sim0'" + status_is 1 +} + +@test "gpioget: with nonexistent line" { + run_tool gpioget nonexistent-line + + output_regex_match ".*cannot find line 'nonexistent-line'" + status_is 1 +} + +@test "gpioget: with same line twice" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # by offset + run_tool gpioget --chip $sim0 0 0 + + output_regex_match ".*lines '0' and '0' are the same line" + status_is 1 + + # by name + run_tool gpioget foo foo + + output_regex_match ".*lines 'foo' and 'foo' are the same line" + status_is 1 + + # by chip and name + run_tool gpioget --chip $sim0 foo foo + + output_regex_match ".*lines 'foo' and 'foo' are the same line" + status_is 1 + + # by name and offset + run_tool gpioget --chip $sim0 foo 1 + + output_regex_match ".*lines 'foo' and '1' are the same line" + status_is 1 + + # by offset and name + run_tool gpioget --chip $sim0 1 foo + + output_regex_match ".*lines '1' and 'foo' are the same line" + status_is 1 +} + +@test "gpioget: with invalid bias" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpioget --bias=bad --chip ${GPIOSIM_CHIP_NAME[sim0]} 0 1 + + output_regex_match ".*invalid bias.*" + status_is 1 +} + +@test "gpioget: with invalid hold-period" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpioget --hold-period=bad --chip ${GPIOSIM_CHIP_NAME[sim0]} 0 + + output_regex_match ".*invalid period.*" + status_is 1 +} + +# +# gpioset test cases +# + +@test "gpioset: by name" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + dut_run gpioset --banner foo=1 + + gpiosim_check_value sim0 1 1 +} + +@test "gpioset: by offset" { + gpiosim_chip sim0 num_lines=8 + + dut_run gpioset --banner --chip ${GPIOSIM_CHIP_NAME[sim0]} 1=1 + + gpiosim_check_value sim0 1 1 +} + +@test "gpioset: by symlink" { + gpiosim_chip sim0 num_lines=8 + gpiosim_chip_symlink sim0 . + + dut_run gpioset --banner --chip $GPIOSIM_CHIP_LINK 1=1 + + gpiosim_check_value sim0 1 1 +} + +@test "gpioset: by chip and name" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + gpiosim_chip sim1 num_lines=8 line_name=3:foo + + dut_run gpioset --banner --chip ${GPIOSIM_CHIP_NAME[sim1]} foo=1 + + gpiosim_check_value sim1 3 1 +} + +@test "gpioset: first matching named line" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz line_name=7:foobar + gpiosim_chip sim2 num_lines=16 + + dut_run gpioset --banner foobar=1 + + gpiosim_check_value sim0 3 1 +} + +@test "gpioset: multiple lines" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpioset --banner --chip $sim0 0=0 1=0 2=1 3=1 4=1 5=1 6=0 7=1 + + gpiosim_check_value sim0 0 0 + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim0 3 1 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 5 1 + gpiosim_check_value sim0 6 0 + gpiosim_check_value sim0 7 1 +} + +@test "gpioset: multiple lines by name and offset" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + + dut_run gpioset --banner --chip ${GPIOSIM_CHIP_NAME[sim0]} 0=1 foo=1 bar=1 3=1 + + gpiosim_check_value sim0 0 1 + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim0 3 1 +} + + +@test "gpioset: multiple lines across multiple chips" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=4:xyz + + dut_run gpioset --banner foo=1 bar=1 baz=1 xyz=1 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim1 0 1 + gpiosim_check_value sim1 4 1 +} + +@test "gpioset: with active-low" { + gpiosim_chip sim0 num_lines=8 + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpioset --banner --active-low -c $sim0 \ + 0=0 1=0 2=1 3=1 4=1 5=1 6=0 7=1 + + gpiosim_check_value sim0 0 1 + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 2 0 + gpiosim_check_value sim0 3 0 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 5 0 + gpiosim_check_value sim0 6 1 + gpiosim_check_value sim0 7 0 +} + +@test "gpioset: with consumer" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + gpiosim_chip sim1 num_lines=8 line_name=3:baz line_name=4:xyz + + dut_run gpioset --banner --consumer gpio-tools-tests foo=1 baz=0 + + run_tool gpioinfo + + output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+input" + output_regex_match \ +"\\s+line\\s+1:\\s+\"foo\"\\s+output consumer=\"gpio-tools-tests\"" + output_regex_match \ +"\\s+line\\s+3:\\s+\"baz\"\\s+output consumer=\"gpio-tools-tests\"" + status_is 0 +} + +@test "gpioset: with push-pull" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpioset --banner --drive=push-pull --chip $sim0 \ + 0=0 1=0 2=1 3=1 4=1 5=1 6=0 7=1 + + gpiosim_check_value sim0 0 0 + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim0 3 1 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 5 1 + gpiosim_check_value sim0 6 0 + gpiosim_check_value sim0 7 1 +} + +@test "gpioset: with open-drain" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 2 pull-up + gpiosim_set_pull sim0 3 pull-up + gpiosim_set_pull sim0 5 pull-up + gpiosim_set_pull sim0 7 pull-up + + dut_run gpioset --banner --drive=open-drain --chip $sim0 \ + 0=0 1=0 2=1 3=1 4=1 5=1 6=0 7=1 + + gpiosim_check_value sim0 0 0 + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim0 3 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 5 1 + gpiosim_check_value sim0 6 0 + gpiosim_check_value sim0 7 1 +} + +@test "gpioset: with open-source" { + gpiosim_chip sim0 num_lines=8 + + gpiosim_set_pull sim0 2 pull-up + gpiosim_set_pull sim0 3 pull-up + gpiosim_set_pull sim0 5 pull-up + gpiosim_set_pull sim0 7 pull-up + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpioset --banner --drive=open-source --chip $sim0 \ + 0=0 1=0 2=1 3=0 4=1 5=1 6=0 7=1 + + gpiosim_check_value sim0 0 0 + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim0 3 1 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 5 1 + gpiosim_check_value sim0 6 0 + gpiosim_check_value sim0 7 1 +} + +@test "gpioset: with pull-up" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpioset --banner --bias=pull-up --drive=open-drain \ + --chip $sim0 0=0 1=0 2=1 3=0 4=1 5=1 6=0 7=1 + + gpiosim_check_value sim0 0 0 + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim0 3 0 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 5 1 + gpiosim_check_value sim0 6 0 + gpiosim_check_value sim0 7 1 +} + +@test "gpioset: with pull-down" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpioset --banner --bias=pull-down --drive=open-source \ + --chip $sim0 0=0 1=0 2=1 3=0 4=1 5=1 6=0 7=1 + + gpiosim_check_value sim0 0 0 + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim0 3 0 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 5 1 + gpiosim_check_value sim0 6 0 + gpiosim_check_value sim0 7 1 +} + +@test "gpioset: with value variants" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 0 pull-up + gpiosim_set_pull sim0 1 pull-down + gpiosim_set_pull sim0 2 pull-down + gpiosim_set_pull sim0 3 pull-up + gpiosim_set_pull sim0 4 pull-down + gpiosim_set_pull sim0 5 pull-up + gpiosim_set_pull sim0 6 pull-up + gpiosim_set_pull sim0 7 pull-down + + dut_run gpioset --banner --chip $sim0 0=0 1=1 2=active \ + 3=inactive 4=on 5=off 6=false 7=true + + gpiosim_check_value sim0 0 0 + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim0 3 0 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 5 0 + gpiosim_check_value sim0 6 0 + gpiosim_check_value sim0 7 1 +} + +@test "gpioset: with hold-period" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 5 pull-up + + dut_run gpioset --banner --hold-period=1200ms -t0 --chip $sim0 0=1 5=0 7=1 + + gpiosim_check_value sim0 0 1 + gpiosim_check_value sim0 5 0 + gpiosim_check_value sim0 7 1 + + dut_wait + + status_is 0 +} + +@test "gpioset: interactive exit" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpioset --interactive --chip $sim0 1=0 2=1 5=1 6=0 7=1 + + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 2 1 + gpiosim_check_value sim0 5 1 + gpiosim_check_value sim0 6 0 + gpiosim_check_value sim0 7 1 + + dut_write "exit" + dut_wait + + status_is 0 +} + +@test "gpioset: interactive help" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=4:bar \ + line_name=7:baz + + dut_run gpioset --interactive foo=1 bar=0 baz=0 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 7 0 + + dut_write "help" + + dut_read + output_regex_match "COMMANDS:.*" + output_regex_match ".*get \[line\]\.\.\..*" + output_regex_match ".*set \.\.\..*" + output_regex_match ".*toggle \[line\]\.\.\..*" + output_regex_match ".*sleep .*" +} + +@test "gpioset: interactive get" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=4:bar \ + line_name=7:baz + + dut_run gpioset --interactive foo=1 bar=0 baz=0 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 7 0 + + dut_write "get" + + dut_read + output_regex_match "\"foo\"=active \"bar\"=inactive \"baz\"=inactive" + + dut_write "get bar" + + dut_read + output_regex_match "\"bar\"=inactive" +} + +@test "gpioset: interactive get unquoted" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=4:bar \ + line_name=7:baz + + dut_run gpioset --interactive --unquoted foo=1 bar=0 baz=0 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 7 0 + + dut_write "get" + + dut_read + output_regex_match "foo=active bar=inactive baz=inactive" + + dut_write "get bar" + + dut_read + output_regex_match "bar=inactive" +} + +@test "gpioset: interactive set" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=4:bar \ + line_name=7:baz + + dut_run gpioset --interactive foo=1 bar=0 baz=0 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 7 0 + + dut_write "set bar=active" + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 7 0 + dut_write "get" + dut_read + output_regex_match "\"foo\"=active \"bar\"=active \"baz\"=inactive" +} + +@test "gpioset: interactive toggle" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=4:bar \ + line_name=7:baz + + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 7 pull-up + + dut_run gpioset -i foo=1 bar=0 baz=0 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 7 0 + + dut_write "toggle" + + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 7 1 + dut_write "get" + dut_read + output_regex_match "\"foo\"=inactive\\s+\"bar\"=active\\s+\"baz\"=active\\s*" + + dut_write "toggle baz" + + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 7 0 + dut_write "get" + dut_read + output_regex_match "\"foo\"=inactive\\s+\"bar\"=active\\s+\"baz\"=inactive\\s*" +} + +@test "gpioset: interactive sleep" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=4:bar \ + line_name=7:baz + + dut_run gpioset --interactive foo=1 bar=0 baz=0 + + dut_write "sleep 500ms" + dut_flush + + assert_fail dut_readable + + sleep 1 + + # prompt, but not a full line... + dut_readable +} + +@test "gpioset: toggle (continuous)" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=4:bar \ + line_name=7:baz + + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 7 pull-up + + dut_run gpioset --banner --toggle 1s foo=1 bar=0 baz=0 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 7 0 + + sleep 1 + + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 7 1 + + sleep 1 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 7 0 +} + +@test "gpioset: toggle (terminated)" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=4:bar \ + line_name=7:baz + + gpiosim_set_pull sim0 4 pull-up + + # hold-period to allow test to sample before gpioset exits + dut_run gpioset --banner --toggle 1s,0 -p 600ms foo=1 bar=0 baz=1 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 7 1 + + sleep 1 + + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 4 1 + gpiosim_check_value sim0 7 0 + + dut_wait + + status_is 0 + + # using --toggle 0 to exit + # hold-period to allow test to sample before gpioset exits + dut_run gpioset --banner -t0 -p 600ms foo=1 bar=0 baz=1 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 4 0 + gpiosim_check_value sim0 7 1 + + dut_wait + + status_is 0 +} + +@test "gpioset: with invalid toggle period" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo line_name=4:bar \ + line_name=7:baz + + run_tool gpioset --toggle 1ns foo=1 bar=0 baz=0 + + output_regex_match ".*invalid period.*" + status_is 1 +} + +@test "gpioset: with strict named line check" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz line_name=7:foobar + gpiosim_chip sim2 num_lines=16 + + run_tool gpioset --strict foobar=active + + output_regex_match ".*line 'foobar' is not unique" + status_is 1 +} + +@test "gpioset: with lines by offset" { + # not suggesting this setup makes any sense + # - just test that we can deal with it + gpiosim_chip sim0 num_lines=8 line_name=1:6 line_name=6:1 + + gpiosim_set_pull sim0 1 pull-down + gpiosim_set_pull sim0 6 pull-up + + dut_run gpioset --banner --chip ${GPIOSIM_CHIP_NAME[sim0]} 6=1 1=0 + + gpiosim_check_value sim0 1 0 + gpiosim_check_value sim0 6 1 +} + +@test "gpioset: with lines strictly by name" { + # not suggesting this setup makes any sense + # - just test that we can deal with it + gpiosim_chip sim0 num_lines=8 line_name=1:6 line_name=6:1 + + gpiosim_set_pull sim0 1 pull-down + gpiosim_set_pull sim0 6 pull-up + + dut_run gpioset --banner --by-name --chip ${GPIOSIM_CHIP_NAME[sim0]} 6=1 1=0 + + gpiosim_check_value sim0 1 1 + gpiosim_check_value sim0 6 0 +} + +@test "gpioset: interactive after SIGINT" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + dut_run gpioset -i foo=1 + + dut_kill -SIGINT + dut_wait + + status_is 130 +} + +@test "gpioset: interactive after SIGTERM" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + dut_run gpioset -i foo=1 + + dut_kill -SIGTERM + dut_wait + + status_is 143 +} + +@test "gpioset: with no arguments" { + run_tool gpioset + + status_is 1 + output_regex_match ".*at least one GPIO line value must be specified" +} + +@test "gpioset: with chip but no line specified" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpioset --chip ${GPIOSIM_CHIP_NAME[sim0]} + + output_regex_match ".*at least one GPIO line value must be specified" + status_is 1 +} + +@test "gpioset: with offset out of range" { + gpiosim_chip sim0 num_lines=4 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioset --chip $sim0 0=1 1=1 2=1 3=1 4=1 5=1 + + output_regex_match ".*offset 4 is out of range on chip '$sim0'" + output_regex_match ".*offset 5 is out of range on chip '$sim0'" + status_is 1 +} + +@test "gpioset: with invalid hold-period" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioset --hold-period=bad --chip $sim0 0=1 + + output_regex_match ".*invalid period.*" + status_is 1 +} + +@test "gpioset: with invalid value" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # by name + run_tool gpioset --chip $sim0 0=c + + output_regex_match ".*invalid line value.*" + status_is 1 + + # by value + run_tool gpioset --chip $sim0 0=3 + + output_regex_match ".*invalid line value.*" + status_is 1 +} + +@test "gpioset: with invalid offset" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpioset --chip ${GPIOSIM_CHIP_NAME[sim0]} 4000000000=0 + + output_regex_match ".*cannot find line '4000000000'" + status_is 1 +} + +@test "gpioset: with invalid bias" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpioset --bias=bad --chip ${GPIOSIM_CHIP_NAME[sim0]} 0=1 1=1 + + output_regex_match ".*invalid bias.*" + status_is 1 +} + +@test "gpioset: with invalid drive" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpioset --drive=bad --chip ${GPIOSIM_CHIP_NAME[sim0]} 0=1 1=1 + + output_regex_match ".*invalid drive.*" + status_is 1 +} + +@test "gpioset: with daemonize and interactive" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioset --interactive --daemonize --chip $sim0 0=1 + + output_regex_match ".*can't combine daemonize with interactive" + status_is 1 +} + +@test "gpioset: with interactive and toggle" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpioset --interactive --toggle 1s --chip $sim0 0=1 + + output_regex_match ".*can't combine interactive with toggle" + status_is 1 +} + +@test "gpioset: with nonexistent line" { + run_tool gpioset nonexistent-line=0 + + output_regex_match ".*cannot find line 'nonexistent-line'" + status_is 1 +} + +@test "gpioset: with same line twice" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # by offset + run_tool gpioset --chip $sim0 0=1 0=1 + + output_regex_match ".*lines '0' and '0' are the same line" + status_is 1 + + # by name + run_tool gpioset --chip $sim0 foo=1 foo=1 + + output_regex_match ".*lines 'foo' and 'foo' are the same line" + status_is 1 + + # by name and offset + run_tool gpioset --chip $sim0 foo=1 1=1 + + output_regex_match ".*lines 'foo' and '1' are the same line" + status_is 1 + + # by offset and name + run_tool gpioset --chip $sim0 1=1 foo=1 + + output_regex_match ".*lines '1' and 'foo' are the same line" + status_is 1 +} + +# +# gpiomon test cases +# + +@test "gpiomon: by name" { + gpiosim_chip sim0 num_lines=8 line_name=4:foo + + gpiosim_set_pull sim0 4 pull-up + + dut_run gpiomon --banner --edges=rising foo + dut_flush + + gpiosim_set_pull sim0 4 pull-down + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 4 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+\"foo\"" + assert_fail dut_readable +} + +@test "gpiomon: by offset" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 4 pull-up + + dut_run gpiomon --banner --edges=rising --chip $sim0 4 + dut_regex_match "Monitoring line .*" + + gpiosim_set_pull sim0 4 pull-down + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 4 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 4" + assert_fail dut_readable +} + +@test "gpiomon: by symlink" { + gpiosim_chip sim0 num_lines=8 + gpiosim_chip_symlink sim0 . + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 4 pull-up + + dut_run gpiomon --banner --edges=rising --chip $GPIOSIM_CHIP_LINK 4 + dut_regex_match "Monitoring line .*" + + gpiosim_set_pull sim0 4 pull-down + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 4 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0\\s+4" + assert_fail dut_readable +} + + +@test "gpiomon: by chip and name" { + gpiosim_chip sim0 num_lines=8 line_name=0:foo + gpiosim_chip sim1 num_lines=8 line_name=2:foo + + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + + gpiosim_set_pull sim1 0 pull-up + + dut_run gpiomon --banner --edges=rising --chip $sim1 foo + dut_regex_match "Monitoring line .*" + + gpiosim_set_pull sim1 2 pull-down + gpiosim_set_pull sim1 2 pull-up + gpiosim_set_pull sim1 2 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim1 2 \"foo\"" + assert_fail dut_readable +} + +@test "gpiomon: first matching named line" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz line_name=7:foobar + gpiosim_chip sim2 num_lines=16 + + dut_run gpiomon --banner foobar + dut_regex_match "Monitoring line .*" + + gpiosim_set_pull sim0 3 pull-up + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+\"foobar\"" + assert_fail dut_readable +} + +@test "gpiomon: rising edge" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 4 pull-up + + dut_run gpiomon --banner --edges=rising --chip $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-down + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 4 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 4" + assert_fail dut_readable +} + +@test "gpiomon: falling edge" { + gpiosim_chip sim0 num_lines=8 + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 4 pull-down + + dut_run gpiomon --banner --edges=falling --chip $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 4 pull-down + gpiosim_set_pull sim0 4 pull-up + dut_regex_match "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 4" + assert_fail dut_readable +} + +@test "gpiomon: both edges" { + gpiosim_chip sim0 num_lines=8 + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner --edges=both --chip $sim0 4 + dut_regex_match "Monitoring line .*" + + gpiosim_set_pull sim0 4 pull-up + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 4" + + gpiosim_set_pull sim0 4 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 4" +} + +@test "gpiomon: with pull-up" { + gpiosim_chip sim0 num_lines=8 + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 4 pull-down + + dut_run gpiomon --banner --bias=pull-up --chip $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 4" + + assert_fail dut_readable +} + +@test "gpiomon: with pull-down" { + gpiosim_chip sim0 num_lines=8 + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 4 pull-up + + dut_run gpiomon --banner --bias=pull-down --chip $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 4" + + assert_fail dut_readable +} + +@test "gpiomon: with active-low" { + gpiosim_chip sim0 num_lines=8 + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 4 pull-up + + dut_run gpiomon --banner --active-low --chip $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 4" + + gpiosim_set_pull sim0 4 pull-up + dut_regex_match "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 4" + + assert_fail dut_readable +} + +@test "gpiomon: with consumer" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + gpiosim_chip sim1 num_lines=8 line_name=3:baz line_name=4:xyz + + dut_run gpiomon --banner --consumer gpio-tools-tests foo baz + + run_tool gpioinfo + + output_regex_match "\\s+line\\s+0:\\s+unnamed\\s+input" + output_regex_match \ +"\\s+line\\s+1:\\s+\"foo\"\\s+input edges=both consumer=\"gpio-tools-tests\"" + output_regex_match \ +"\\s+line\\s+3:\\s+\"baz\"\\s+input edges=both consumer=\"gpio-tools-tests\"" + status_is 0 +} + +@test "gpiomon: with quiet mode" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner --edges=rising --quiet --chip $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + assert_fail dut_readable +} + +@test "gpiomon: with num-events" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # redirect, as gpiomon exits after 4 events + dut_run_redirect gpiomon --num-events=4 --chip $sim0 4 + + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 4 pull-down + gpiosim_set_pull sim0 4 pull-up + gpiosim_set_pull sim0 4 pull-down + + dut_wait + status_is 0 + dut_read_redirect + + regex_matches "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 4" "${lines[0]}" + regex_matches "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 4" "${lines[1]}" + regex_matches "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 4" "${lines[2]}" + regex_matches "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 4" "${lines[3]}" + num_lines_is 4 +} + +@test "gpiomon: multiple lines" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner --format=%o --chip $sim0 1 3 2 5 4 + dut_regex_match "Monitoring lines .*" + + gpiosim_set_pull sim0 2 pull-up + dut_regex_match "2" + gpiosim_set_pull sim0 3 pull-up + dut_regex_match "3" + gpiosim_set_pull sim0 4 pull-up + dut_regex_match "4" + + assert_fail dut_readable +} + +@test "gpiomon: multiple lines by name and offset" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner --format=%o --chip $sim0 foo bar 3 + dut_regex_match "Monitoring lines .*" + + gpiosim_set_pull sim0 2 pull-up + dut_regex_match "2" + gpiosim_set_pull sim0 3 pull-up + dut_regex_match "3" + gpiosim_set_pull sim0 1 pull-up + dut_regex_match "1" + + assert_fail dut_readable +} + +@test "gpiomon: multiple lines across multiple chips" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=4:xyz + + dut_run gpiomon --banner --format=%l foo bar baz + dut_regex_match "Monitoring lines .*" + + gpiosim_set_pull sim0 2 pull-up + dut_regex_match "bar" + gpiosim_set_pull sim1 0 pull-up + dut_regex_match "baz" + gpiosim_set_pull sim0 1 pull-up + dut_regex_match "foo" + + assert_fail dut_readable +} + +@test "gpiomon: exit after SIGINT" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner --chip $sim0 4 + dut_regex_match "Monitoring line .*" + + dut_kill -SIGINT + dut_wait + + status_is 130 +} + +@test "gpiomon: exit after SIGTERM" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner --chip $sim0 4 + dut_regex_match "Monitoring line .*" + + dut_kill -SIGTERM + dut_wait + + status_is 143 +} + +@test "gpiomon: with nonexistent line" { + run_tool gpiomon nonexistent-line + + status_is 1 + output_regex_match ".*cannot find line 'nonexistent-line'" +} + +@test "gpiomon: with same line twice" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # by offset + run_tool gpiomon --chip $sim0 0 0 + + output_regex_match ".*lines '0' and '0' are the same line" + status_is 1 + + # by name + run_tool gpiomon foo foo + + output_regex_match ".*lines 'foo' and 'foo' are the same line" + status_is 1 + + # by name and offset + run_tool gpiomon --chip $sim0 1 foo + + output_regex_match ".*lines '1' and 'foo' are the same line" + status_is 1 +} + +@test "gpiomon: with strict named line check" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz line_name=7:foobar + gpiosim_chip sim2 num_lines=16 + + run_tool gpiomon --strict foobar + + output_regex_match ".*line 'foobar' is not unique" + status_is 1 +} +@test "gpiomon: with lines by offset" { + # not suggesting this setup makes any sense + # - just test that we can deal with it + gpiosim_chip sim0 num_lines=8 line_name=1:6 line_name=6:1 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 1 pull-up + + dut_run gpiomon --banner --chip $sim0 6 1 + dut_flush + + gpiosim_set_pull sim0 1 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 1" + + gpiosim_set_pull sim0 1 pull-up + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 1" + + gpiosim_set_pull sim0 6 pull-up + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 6" + + gpiosim_set_pull sim0 6 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 6" + + assert_fail dut_readable +} + +@test "gpiomon: with lines strictly by name" { + # not suggesting this setup makes sense + # - just test that we can deal with it + gpiosim_chip sim0 num_lines=8 line_name=1:42 line_name=6:13 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 1 pull-up + + dut_run gpiomon --banner --by-name --chip $sim0 42 13 + dut_flush + + gpiosim_set_pull sim0 1 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 1" + + gpiosim_set_pull sim0 1 pull-up + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 1" + + gpiosim_set_pull sim0 6 pull-up + dut_regex_match "[0-9]+\.[0-9]+\\s+rising\\s+$sim0 6" + + gpiosim_set_pull sim0 6 pull-down + dut_regex_match "[0-9]+\.[0-9]+\\s+falling\\s+$sim0 6" + + assert_fail dut_readable +} + +@test "gpiomon: with no arguments" { + run_tool gpiomon + + output_regex_match ".*at least one GPIO line must be specified" + status_is 1 +} + +@test "gpiomon: with no line specified" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpiomon --chip ${GPIOSIM_CHIP_NAME[sim0]} + + output_regex_match ".*at least one GPIO line must be specified" + status_is 1 +} + +@test "gpiomon: with offset out of range" { + gpiosim_chip sim0 num_lines=4 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpiomon --chip $sim0 5 + + output_regex_match ".*offset 5 is out of range on chip '$sim0'" + status_is 1 +} + +@test "gpiomon: with invalid bias" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpiomon --bias=bad -c $sim0 0 1 + + output_regex_match ".*invalid bias.*" + status_is 1 +} + +@test "gpiomon: with custom format (event type + offset)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=%e %o" -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_read + output_is "1 4" +} + +@test "gpiomon: with custom format (event type + offset joined)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=%e%o" -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_read + output_is "14" +} + +@test "gpiomon: with custom format (edge, chip and line)" { + gpiosim_chip sim0 num_lines=8 line_name=4:baz + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=%e %o %E %c %l" -c $sim0 baz + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_regex_match "1 4 rising $sim0 baz" +} + +@test "gpiomon: with custom format (seconds timestamp)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=%e %o %S" -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_regex_match "1 4 [0-9]+\\.[0-9]+" +} + +@test "gpiomon: with custom format (UTC timestamp)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=%U %e %o " --event-clock=realtime \ + -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_regex_match \ +"[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\\.[0-9]+Z 1 4" +} + +@test "gpiomon: with custom format (localtime timestamp)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=%L %e %o" --event-clock=realtime \ + -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_regex_match \ +"[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\\.[0-9]+ 1 4" +} + +@test "gpiomon: with custom format (double percent sign)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=start%%end" -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_read + output_is "start%end" +} + +@test "gpiomon: with custom format (double percent sign + event type specifier)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=%%e" -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_read + output_is "%e" +} + +@test "gpiomon: with custom format (single percent sign)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=%" -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_read + output_is "%" +} + +@test "gpiomon: with custom format (single percent sign between other characters)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=foo % bar" -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_read + output_is "foo % bar" +} + +@test "gpiomon: with custom format (unknown specifier)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpiomon --banner "--format=%x" -c $sim0 4 + dut_flush + + gpiosim_set_pull sim0 4 pull-up + dut_read + output_is "%x" +} From patchwork Mon Nov 14 04:01:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 624649 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 AC5A8C4332F for ; Mon, 14 Nov 2022 04:01:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235771AbiKNEBw (ORCPT ); Sun, 13 Nov 2022 23:01:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235714AbiKNEBv (ORCPT ); Sun, 13 Nov 2022 23:01:51 -0500 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A61B713CD3 for ; Sun, 13 Nov 2022 20:01:49 -0800 (PST) Received: by mail-pf1-x42d.google.com with SMTP id v28so9883469pfi.12 for ; Sun, 13 Nov 2022 20:01:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.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=maEdBsz2kLehmf10cGacNvNHGSNaGfa7oMa5GR8cWD0=; b=jAYkByFN1QAzAk8jByBfYnF7b59D39I0OdCQucUsk/5TbR4QlWeAkIIiKv90rV8gEQ AsFiUodh6RZbY6Qbx2rAqZSirZ7Am6Ts3b3Tlo8R4pJdBGnwY88ApAN4zHyGxeR+N3S7 OjJwwv3G7j6YpBWGOpkRJcRZsMEGC/Ln5BODRYuPIICM/GcopppP8iEmCeLA4hVhk/+0 OzrvzuJDOj8ZzQm9oFjk8bZn4cfBmBj8P2INdeS7yNryxozuKduuYvN6x9C0PdHzmWWp HB7E6v0UxC3oliv5y+5Ug4R6vx1DB8LFqKSVqmrOL3QRyQJMKtdATOd9wase2d/BzrHz WJ8Q== 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=maEdBsz2kLehmf10cGacNvNHGSNaGfa7oMa5GR8cWD0=; b=02qY2crYnzROx5CBx8kas+Ok2oay3r9VF3/rej/GZlzth1jV4CMVVHYed6qnLeyNnu /ppyQea3JlIC7Oxi/NuFNgboV2x24QKsAPibbUX4MUSMv2ybDJUHgfGEjZLPNKNyqufY 33Go8I4Y1aYSvVEvVQh99SCpEOkhAMmQWoaJ363/IcQ2WtOuQiNnlVte7wYownDXQqam 0J3d/u4yxirAGkaeeyIynM/dX2l9d7bWzhFMm7PrFVpMZI49HnXed74UBRQmZBcGlQM+ UpH/LNYSbpvlA8N4316ZFc/oNGVrhQ1LDHQg9y+m7/YcDzHSDUWyUdShIl0WZY7PdYIq kMMw== X-Gm-Message-State: ANoB5pkiW9a4yLa0n8pNNin5rUvnWugw1rD6jNMGHB5CR5TeiAWhJQd7 4JebWwAr/ysq9STVvgACyEEcc6VvYKE= X-Google-Smtp-Source: AA0mqf77H0kDVk5Nj2CycFWJ5Xc/c264ucsacuZxURx8YQvKdqgOQSGPQ2BCamjJtj5Rd0ei0KB6yw== X-Received: by 2002:a63:dd4f:0:b0:464:2135:5f88 with SMTP id g15-20020a63dd4f000000b0046421355f88mr10278340pgj.360.1668398508870; Sun, 13 Nov 2022 20:01:48 -0800 (PST) Received: from sol.home.arpa (14-200-229-209.tpgi.com.au. [14.200.229.209]) by smtp.gmail.com with ESMTPSA id f11-20020a170902684b00b001801aec1f6bsm6018636pln.141.2022.11.13.20.01.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:01:48 -0800 (PST) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod v2][PATCH v4 4/5] tools: add gpionotify Date: Mon, 14 Nov 2022 12:01:01 +0800 Message-Id: <20221114040102.66031-5-warthog618@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221114040102.66031-1-warthog618@gmail.com> References: <20221114040102.66031-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a gpionotify tool, based on gpiomon, to report line info change events read from chip file descriptors. Inspired by the gpio-watch tool in the linux kernel, but with gpiomon features such as custom formatted output, filtering events of interest and exiting after a number of events, so more useful for scripting. Default output is minimalist, so just time, event type and line id. Full event details are available using the custom formatted output. Signed-off-by: Kent Gibson --- man/Makefile.am | 2 +- tools/.gitignore | 1 + tools/Makefile.am | 4 +- tools/gpionotify.c | 445 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 450 insertions(+), 2 deletions(-) create mode 100644 tools/gpionotify.c diff --git a/man/Makefile.am b/man/Makefile.am index 8d1d9b3..201a52b 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -3,7 +3,7 @@ if WITH_MANPAGES -dist_man1_MANS = gpiodetect.man gpioinfo.man gpioget.man gpioset.man gpiomon.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/tools/.gitignore b/tools/.gitignore index d6b2f44..dfdbc0d 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -6,3 +6,4 @@ gpioinfo gpioget gpioset gpiomon +gpionotify diff --git a/tools/Makefile.am b/tools/Makefile.am index 3fafec6..b6e8f34 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -15,7 +15,7 @@ libtools_common_la_SOURCES = tools-common.c tools-common.h LDADD = libtools-common.la $(top_builddir)/lib/libgpiod.la $(LIBEDIT_LIBS) -bin_PROGRAMS = gpiodetect gpioinfo gpioget gpioset gpiomon +bin_PROGRAMS = gpiodetect gpioinfo gpioget gpioset gpiomon gpionotify gpiodetect_SOURCES = gpiodetect.c @@ -27,6 +27,8 @@ gpioset_SOURCES = gpioset.c gpiomon_SOURCES = gpiomon.c +gpionotify_SOURCES = gpionotify.c + EXTRA_DIST = gpio-tools-test gpio-tools-test.bats if WITH_TESTS diff --git a/tools/gpionotify.c b/tools/gpionotify.c new file mode 100644 index 0000000..dfa0538 --- /dev/null +++ b/tools/gpionotify.c @@ -0,0 +1,445 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: 2022 Kent Gibson + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tools-common.h" + +static void print_help(void) +{ + printf("Usage: %s [OPTIONS] ...\n", get_progname()); + printf("\n"); + printf("Wait for changes to info on GPIO lines and print them to standard output.\n"); + printf("\n"); + printf("Lines are specified by name, or optionally by offset if the chip option\n"); + printf("is provided.\n"); + printf("\n"); + printf("Options:\n"); + printf(" --banner\t\tdisplay a banner on successful startup\n"); + printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n"); + printf(" -c, --chip \trestrict scope to a particular chip\n"); + printf(" -e, --event \tspecify the events to monitor\n"); + printf("\t\t\tPossible values: 'requested', 'released', 'reconfigured'.\n"); + printf("\t\t\t(default is all events)\n"); + printf(" -h, --help\t\tdisplay this help and exit\n"); + printf(" -F, --format \tspecify a custom output format\n"); + printf(" --localtime\tconvert event timestamps to local time\n"); + printf(" -n, --num-events \n"); + printf("\t\t\texit after processing num events\n"); + printf(" -q, --quiet\t\tdon't generate any output\n"); + printf(" -s, --strict\t\tabort if requested line names are not unique\n"); + printf(" --unquoted\tdon't quote line or consumer names\n"); + printf(" --utc\t\tconvert event timestamps to UTC\n"); + printf(" -v, --version\t\toutput version information and exit\n"); + print_chip_help(); + printf("\n"); + printf("Format specifiers:\n"); + printf(" %%o GPIO line offset\n"); + printf(" %%l GPIO line name\n"); + printf(" %%c GPIO chip name\n"); + printf(" %%e numeric info event type ('1' - requested, '2' - released or '3' - reconfigured)\n"); + printf(" %%E info event type ('requested', 'released' or 'reconfigured')\n"); + printf(" %%a line attributes\n"); + printf(" %%C consumer\n"); + printf(" %%S event timestamp as seconds\n"); + printf(" %%U event timestamp as UTC\n"); + printf(" %%L event timestamp as local time\n"); +} + +static int parse_event_type_or_die(const char *option) +{ + if (strcmp(option, "requested") == 0) + return GPIOD_INFO_EVENT_LINE_REQUESTED; + if (strcmp(option, "released") == 0) + return GPIOD_INFO_EVENT_LINE_RELEASED; + if (strcmp(option, "reconfigured") != 0) + die("invalid edge: %s", option); + return GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED; +} + +struct config { + bool banner; + bool by_name; + bool quiet; + bool strict; + bool unquoted; + int event_type; + int events_wanted; + const char *chip_id; + const char *fmt; + int timestamp_fmt; +}; + +static int parse_config(int argc, char **argv, struct config *cfg) +{ + int opti, optc; + const char *const shortopts = "+c:e:hF:n:qshv"; + const struct option longopts[] = { + { "banner", no_argument, NULL, '-'}, + { "by-name", no_argument, NULL, 'B'}, + { "chip", required_argument, NULL, 'c' }, + { "event", required_argument, NULL, 'e' }, + { "format", required_argument, NULL, 'F' }, + { "help", no_argument, NULL, 'h' }, + { "localtime", no_argument, &cfg->timestamp_fmt, 2 }, + { "num-events", required_argument, NULL, 'n' }, + { "quiet", no_argument, NULL, 'q' }, + { "silent", no_argument, NULL, 'q' }, + { "strict", no_argument, NULL, 's' }, + { "unquoted", no_argument, NULL, 'Q' }, + { "utc", no_argument, &cfg->timestamp_fmt, 1 }, + { "version", no_argument, NULL, 'v' }, + { GETOPT_NULL_LONGOPT }, + }; + + memset(cfg, 0, sizeof(*cfg)); + + for (;;) { + optc = getopt_long(argc, argv, shortopts, longopts, &opti); + if (optc < 0) + break; + + switch (optc) { + case '-': + cfg->banner = true; + break; + case 'B': + cfg->by_name = true; + break; + case 'c': + cfg->chip_id = optarg; + break; + case 'e': + cfg->event_type = parse_event_type_or_die(optarg); + break; + case 'F': + cfg->fmt = optarg; + break; + case 'n': + cfg->events_wanted = parse_uint_or_die(optarg); + break; + case 'q': + cfg->quiet = true; + break; + case 'Q': + cfg->unquoted = true; + break; + case 's': + cfg->strict = true; + break; + case 'h': + print_help(); + exit(EXIT_SUCCESS); + case 'v': + print_version(); + exit(EXIT_SUCCESS); + case '?': + die("try %s --help", get_progname()); + case 0: + break; + default: + abort(); + } + } + + return optind; +} + +static void print_banner(int num_lines, char **lines) +{ + int i; + + if (num_lines > 1) { + printf("Watching lines "); + + for (i = 0; i < num_lines - 1; i++) + printf("'%s', ", lines[i]); + + printf("and '%s'...\n", lines[i]); + } else { + printf("Watching line '%s'...\n", lines[0]); + } +} + +static void print_event_type(int evtype) +{ + switch (evtype) { + case GPIOD_INFO_EVENT_LINE_REQUESTED: + fputs("requested", stdout); + break; + case GPIOD_INFO_EVENT_LINE_RELEASED: + fputs("released", stdout); + break; + case GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED: + fputs("reconfigured", stdout); + break; + default: + fputs("unknown", stdout); + break; + } +} + +/* + * A convenience function to map clock monotonic to realtime, as uAPI only + * supports CLOCK_MONOTONIC. + * + * Samples the realtime clock on either side of a monotonic sample and averages + * the realtime samples to estimate the offset between the two clocks. + * Any time shifts between the two realtime samples will result in the + * monotonic time being mapped to the average of the before and after, so + * half way between the old and new times. + * + * Any CPU suspension between the event being generated and converted will + * result in the returned time being shifted by the period of suspension. + */ +static uint64_t monotonic_to_realtime(uint64_t evtime) +{ + struct timespec ts; + uint64_t before, after, mono; + + clock_gettime(CLOCK_REALTIME, &ts); + before = ts.tv_nsec + ((uint64_t)ts.tv_sec) * 1000000000; + + clock_gettime(CLOCK_MONOTONIC, &ts); + mono = ts.tv_nsec + ((uint64_t)ts.tv_sec) * 1000000000; + + clock_gettime(CLOCK_REALTIME, &ts); + after = ts.tv_nsec + ((uint64_t)ts.tv_sec) * 1000000000; + + evtime += (after/2 - mono + before/2); + return evtime; +} + +static void event_print_formatted(struct gpiod_info_event *event, + struct line_resolver *resolver, int chip_num, + struct config *cfg) +{ + struct gpiod_line_info *info; + const char *lname, *prev, *curr, *consumer; + char fmt; + uint64_t evtime; + int evtype; + unsigned int offset; + + info = gpiod_info_event_get_line_info(event); + evtime = gpiod_info_event_get_timestamp_ns(event); + evtype = gpiod_info_event_get_event_type(event); + offset = gpiod_line_info_get_offset(info); + + for (prev = curr = cfg->fmt;;) { + curr = strchr(curr, '%'); + if (!curr) { + fputs(prev, stdout); + break; + } + + if (prev != curr) + fwrite(prev, curr - prev, 1, stdout); + + fmt = *(curr + 1); + + switch (fmt) { + case 'a': + print_line_attributes(info, cfg->unquoted); + break; + case 'c': + fputs(get_chip_name(resolver, chip_num), stdout); + break; + case 'C': + if (!gpiod_line_info_is_used(info)) { + consumer = "unused"; + } else { + consumer = gpiod_line_info_get_consumer(info); + if (!consumer) + consumer = "kernel"; + } + fputs(consumer, stdout); + break; + case 'e': + printf("%d", evtype); + break; + case 'E': + print_event_type(evtype); + break; + case 'l': + lname = gpiod_line_info_get_name(info); + if (!lname) + lname = "unnamed"; + fputs(lname, stdout); + break; + case 'L': + print_event_time(monotonic_to_realtime(evtime), 2); + break; + case 'o': + printf("%u", offset); + break; + case 'S': + print_event_time(evtime, 0); + break; + case 'U': + print_event_time(monotonic_to_realtime(evtime), 1); + break; + case '%': + fputc('%', stdout); + break; + case '\0': + fputc('%', stdout); + goto end; + default: + fwrite(curr, 2, 1, stdout); + break; + } + + curr += 2; + prev = curr; + } + +end: + fputc('\n', stdout); +} + +static void event_print_human_readable(struct gpiod_info_event *event, + struct line_resolver *resolver, int chip_num, + struct config *cfg) +{ + struct gpiod_line_info *info; + uint64_t evtime; + int evtype; + unsigned int offset; + char *evname; + + info = gpiod_info_event_get_line_info(event); + evtime = gpiod_info_event_get_timestamp_ns(event); + evtype = gpiod_info_event_get_event_type(event); + offset = gpiod_line_info_get_offset(info); + + switch (evtype) { + case GPIOD_INFO_EVENT_LINE_REQUESTED: + evname = "requested"; + break; + case GPIOD_INFO_EVENT_LINE_RELEASED: + evname = "released"; + break; + case GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED: + evname = "reconfigured"; + break; + default: + evname = "unknown"; + } + + if (cfg->timestamp_fmt) + evtime = monotonic_to_realtime(evtime); + + print_event_time(evtime, cfg->timestamp_fmt); + printf("\t%s\t", evname); + print_line_id(resolver, chip_num, offset, cfg->chip_id, cfg->unquoted); + fputc('\n', stdout); +} + +static void event_print(struct gpiod_info_event *event, + struct line_resolver *resolver, int chip_num, + struct config *cfg) +{ + if (cfg->quiet) + return; + + if (cfg->fmt) + event_print_formatted(event, resolver, chip_num, cfg); + else + event_print_human_readable(event, resolver, chip_num, cfg); +} + +int main(int argc, char **argv) +{ + int i, j, events_done = 0, evtype; + struct gpiod_chip **chips; + struct pollfd *pollfds; + struct gpiod_chip *chip; + struct line_resolver *resolver; + struct gpiod_info_event *event; + struct config cfg; + + i = parse_config(argc, argv, &cfg); + argc -= optind; + argv += optind; + + if (argc < 1) + die("at least one GPIO line must be specified"); + + if (argc > 64) + die("too many lines given"); + + resolver = resolve_lines(argc, argv, cfg.chip_id, cfg.strict, + cfg.by_name); + validate_resolution(resolver, cfg.chip_id); + chips = calloc(resolver->num_chips, sizeof(*chips)); + pollfds = calloc(resolver->num_chips, sizeof(*pollfds)); + if (!pollfds) + die("out of memory"); + + for (i = 0; i < resolver->num_chips; i++) { + chip = gpiod_chip_open(resolver->chips[i].path); + if (!chip) + die_perror("unable to open chip '%s'", + resolver->chips[i].path); + + for (j = 0; j < resolver->num_lines; j++) + if ((resolver->lines[j].chip_num == i) && + !gpiod_chip_watch_line_info( + chip, resolver->lines[j].offset)) + die_perror("unable to watch line on chip '%s'", + resolver->chips[i].path); + + chips[i] = chip; + pollfds[i].fd = gpiod_chip_get_fd(chip); + pollfds[i].events = POLLIN; + } + + if (cfg.banner) + print_banner(argc, argv); + + for (;;) { + fflush(stdout); + + if (poll(pollfds, resolver->num_chips, -1) < 0) + die_perror("error polling for events"); + + for (i = 0; i < resolver->num_chips; i++) { + if (pollfds[i].revents == 0) + continue; + + event = gpiod_chip_read_info_event(chips[i]); + if (!event) + die_perror("unable to retrieve chip event"); + + if (cfg.event_type) { + evtype = gpiod_info_event_get_event_type(event); + if (evtype != cfg.event_type) + continue; + } + + event_print(event, resolver, i, &cfg); + + events_done++; + + if (cfg.events_wanted && + events_done >= cfg.events_wanted) + goto done; + } + } +done: + for (i = 0; i < resolver->num_chips; i++) + gpiod_chip_close(chips[i]); + + free(chips); + free_line_resolver(resolver); + + return EXIT_SUCCESS; +} From patchwork Mon Nov 14 04:01:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 624966 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 5CC4BC433FE for ; Mon, 14 Nov 2022 04:01:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235714AbiKNEB6 (ORCPT ); Sun, 13 Nov 2022 23:01:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235773AbiKNEB5 (ORCPT ); Sun, 13 Nov 2022 23:01:57 -0500 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E3F012D39 for ; Sun, 13 Nov 2022 20:01:55 -0800 (PST) Received: by mail-pj1-x1030.google.com with SMTP id m6-20020a17090a5a4600b00212f8dffec9so9514605pji.0 for ; Sun, 13 Nov 2022 20:01:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.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=NbK+IngWpViM1296w06W1SejMrRpWzBlWnD5Rov/MPA=; b=pmSt3IvQ7+EZev/YEErhcv5XsaB9ugQFhJOfkxuSEcbXohVhbs3Nq8zzbMzM3/YeIY C0sGRCsJPWZgyzJRP1E4kuHmX4a/j4wetCtwaVLN2+6fifCzi4r60lIsHL4MikiNbGoa t5eUqa3MupfBSnIy0H+7qcI7RbpIr9/luCCKSM+DDVYcc4bwXR53GmNmcM+loqFGpm+R UeIF2U/5IH0fS0hOgfpuOq4UoiCZrjiBlu87sdeBWIAEnd1sQ8+R4U2AX/XY2Qqe4ISy b0zzjCxHTX407l7UTzOXqdNwc6PxUHvoF4fJX67s1wM3zl7ofxL6bUjKjBDET02WuyO1 r90A== 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=NbK+IngWpViM1296w06W1SejMrRpWzBlWnD5Rov/MPA=; b=lkj8cYqaaJjXvSqWSyTLJIhYsuvO2bnSDXAiJqNgkSSkp2L72y4NNgwysgiSxEleou 6HltVHYZPW8sE1m+J+gPijAyDl1qZSSrxDPZrubNHH9Aj0n5fax64v09lh8h592dXEkw Xd6RpQNcJib5wusljzBKNW/g4pLwZgmfGQNIxGpw0fREoOPHfy7T5igpwJaCdNTANB2H 1byQcJ8C7Ezy51v+y3rLsWzCUzpnEzhLgB8neJOLlkpeBf6S0UiyA5M/Ryg96Iw0I+2Z nVpUiiRo8V4c9p+eM8AcetHxNpD7wq2i7G1ZMq3zHnbmqqxHvp5jvWuByrjW0whzFbfG sifw== X-Gm-Message-State: ANoB5pk5WosExbCiyT8OVGVDR5m2PXzZK89BqBVcAQOdv8WPx2XDNsov 9rLZR68qAH4dqg9tJS3m0YfMnCaxYhw= X-Google-Smtp-Source: AA0mqf4iF2zPySxp6mmxrEEkSf1UzHCEEjHCJ0jqCzja9QXQI/wp7wi3JWqgdVebfkbv8K77bD1uDw== X-Received: by 2002:a17:902:be12:b0:186:a97d:6bcc with SMTP id r18-20020a170902be1200b00186a97d6bccmr11881001pls.121.1668398514473; Sun, 13 Nov 2022 20:01:54 -0800 (PST) Received: from sol.home.arpa (14-200-229-209.tpgi.com.au. [14.200.229.209]) by smtp.gmail.com with ESMTPSA id f11-20020a170902684b00b001801aec1f6bsm6018636pln.141.2022.11.13.20.01.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:01:54 -0800 (PST) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod v2][PATCH v4 5/5] tools: gpionotify tests Date: Mon, 14 Nov 2022 12:01:02 +0800 Message-Id: <20221114040102.66031-6-warthog618@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221114040102.66031-1-warthog618@gmail.com> References: <20221114040102.66031-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Extend the tool test suite to cover the gpionotify tool. Signed-off-by: Kent Gibson --- tools/gpio-tools-test.bats | 503 +++++++++++++++++++++++++++++++++++++ 1 file changed, 503 insertions(+) diff --git a/tools/gpio-tools-test.bats b/tools/gpio-tools-test.bats index 78b0959..d73cf9b 100755 --- a/tools/gpio-tools-test.bats +++ b/tools/gpio-tools-test.bats @@ -2419,3 +2419,506 @@ request_release_line() { dut_read output_is "%x" } + +# +# gpionotify test cases +# + +@test "gpionotify: by name" { + gpiosim_chip sim0 num_lines=8 line_name=4:foo + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner foo + dut_regex_match "Watching line .*" + + request_release_line $sim0 4 + + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+\"foo\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+\"foo\"" + # tools currently have no way to generate a reconfig event +} + +@test "gpionotify: by offset" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --chip $sim0 4 + dut_regex_match "Watching line .*" + + request_release_line $sim0 4 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 4" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 4" + + assert_fail dut_readable +} + +@test "gpionotify: by symlink" { + gpiosim_chip sim0 num_lines=8 + gpiosim_chip_symlink sim0 . + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --chip $GPIOSIM_CHIP_LINK 4 + dut_regex_match "Watching line .*" + + request_release_line $sim0 4 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0\\s+4" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0\\s+4" + + assert_fail dut_readable +} + +@test "gpionotify: by chip and name" { + gpiosim_chip sim0 num_lines=8 line_name=4:foo + gpiosim_chip sim1 num_lines=8 line_name=2:foo + + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + + dut_run gpionotify --banner --chip $sim1 foo + dut_regex_match "Watching line .*" + + request_release_line $sim1 2 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim1 2 \"foo\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim1 2 \"foo\"" + + assert_fail dut_readable +} + +@test "gpionotify: first matching named line" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz line_name=7:foobar + gpiosim_chip sim2 num_lines=16 + + dut_run gpionotify --banner foobar + dut_regex_match "Watching line .*" + + request_release_line ${GPIOSIM_CHIP_NAME[sim0]} 3 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+\"foobar\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+\"foobar\"" + + assert_fail dut_readable +} + +@test "gpionotify: with requested" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 4 pull-up + + dut_run gpionotify --banner --event=requested --chip $sim0 4 + dut_flush + + request_release_line ${GPIOSIM_CHIP_NAME[sim0]} 4 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 4" + assert_fail dut_readable +} + +@test "gpionotify: with released" { + gpiosim_chip sim0 num_lines=8 + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + gpiosim_set_pull sim0 4 pull-down + + dut_run gpionotify --banner --event=released --chip $sim0 4 + dut_flush + + request_release_line ${GPIOSIM_CHIP_NAME[sim0]} 4 + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 4" + assert_fail dut_readable +} + +@test "gpionotify: with quiet mode" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --quiet --chip $sim0 4 + dut_flush + + request_release_line ${GPIOSIM_CHIP_NAME[sim0]} 4 + assert_fail dut_readable +} + +@test "gpionotify: with num-events" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # redirect, as gpionotify exits after 4 events + dut_run_redirect gpionotify --num-events=4 --chip $sim0 3 4 + + + request_release_line ${GPIOSIM_CHIP_NAME[sim0]} 4 + request_release_line ${GPIOSIM_CHIP_NAME[sim0]} 3 + + dut_wait + status_is 0 + dut_read_redirect + + regex_matches "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 4" "${lines[0]}" + regex_matches "[0-9]+\.[0-9]+\\s+released\\s+$sim0 4" "${lines[1]}" + regex_matches "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 3" "${lines[2]}" + regex_matches "[0-9]+\.[0-9]+\\s+released\\s+$sim0 3" "${lines[3]}" + num_lines_is 4 +} + +@test "gpionotify: multiple lines" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --chip $sim0 1 2 3 4 5 + dut_regex_match "Watching lines .*" + + request_release_line $sim0 2 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 2" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 2" + + request_release_line $sim0 3 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 3" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 3" + + request_release_line $sim0 4 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 4" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 4" + + assert_fail dut_readable +} + +@test "gpionotify: multiple lines by name and offset" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --chip $sim0 bar foo 3 + dut_regex_match "Watching lines .*" + + request_release_line $sim0 2 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 2\\s+\"bar\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 2\\s+\"bar\"" + + request_release_line $sim0 1 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 1\\s+\"foo\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 1\\s+\"foo\"" + + request_release_line $sim0 3 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 3" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 3" + + assert_fail dut_readable +} + +@test "gpionotify: multiple lines across multiple chips" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=4:xyz + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + local sim1=${GPIOSIM_CHIP_NAME[sim1]} + + dut_run gpionotify --banner baz bar foo xyz + dut_regex_match "Watching lines .*" + + request_release_line $sim0 2 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+\"bar\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+\"bar\"" + + request_release_line $sim0 1 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+\"foo\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+\"foo\"" + + request_release_line $sim1 4 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+\"xyz\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+\"xyz\"" + + request_release_line $sim1 0 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+\"baz\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+\"baz\"" + + assert_fail dut_readable +} + +@test "gpionotify: exit after SIGINT" { + gpiosim_chip sim0 num_lines=8 + + dut_run gpionotify --banner --chip ${GPIOSIM_CHIP_NAME[sim0]} 4 + dut_regex_match "Watching line .*" + + dut_kill -SIGINT + dut_wait + + status_is 130 +} + +@test "gpionotify: exit after SIGTERM" { + gpiosim_chip sim0 num_lines=8 + + dut_run gpionotify --banner --chip ${GPIOSIM_CHIP_NAME[sim0]} 4 + dut_regex_match "Watching line .*" + + dut_kill -SIGTERM + dut_wait + + status_is 143 +} + +@test "gpionotify: with nonexistent line" { + run_tool gpionotify nonexistent-line + + status_is 1 + output_regex_match ".*cannot find line 'nonexistent-line'" +} + +@test "gpionotify: with same line twice" { + gpiosim_chip sim0 num_lines=8 line_name=1:foo + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + # by offset + run_tool gpionotify --chip $sim0 0 0 + + output_regex_match ".*lines '0' and '0' are the same line" + num_lines_is 1 + status_is 1 + + # by name + run_tool gpionotify foo foo + + output_regex_match ".*lines 'foo' and 'foo' are the same line" + num_lines_is 1 + status_is 1 + + # by name and offset + run_tool gpionotify --chip $sim0 1 foo + + output_regex_match ".*lines '1' and 'foo' are the same line" + num_lines_is 1 + status_is 1 +} + +@test "gpionotify: with strict named line check" { + gpiosim_chip sim0 num_lines=4 line_name=1:foo line_name=2:bar \ + line_name=3:foobar + gpiosim_chip sim1 num_lines=8 line_name=0:baz line_name=2:foobar \ + line_name=4:xyz line_name=7:foobar + gpiosim_chip sim2 num_lines=16 + + run_tool gpionotify --strict foobar + + output_regex_match ".*line 'foobar' is not unique" + status_is 1 +} + +@test "gpionotify: with lines by offset" { + # not suggesting this setup makes any sense + # - just test that we can deal with it + gpiosim_chip sim0 num_lines=8 line_name=1:6 line_name=6:1 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --chip $sim0 1 + dut_flush + + request_release_line $sim0 1 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 1" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 1" + + request_release_line $sim0 6 + + assert_fail dut_readable +} + +@test "gpionotify: with lines strictly by name" { + # not suggesting this setup makes any sense + # - just test that we can deal with it + gpiosim_chip sim0 num_lines=8 line_name=1:6 line_name=6:1 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --by-name --chip $sim0 1 + dut_flush + + request_release_line $sim0 6 + dut_regex_match "[0-9]+\.[0-9]+\\s+requested\\s+$sim0 6 \"1\"" + dut_regex_match "[0-9]+\.[0-9]+\\s+released\\s+$sim0 6 \"1\"" + + request_release_line $sim0 1 + assert_fail dut_readable +} + +@test "gpionotify: with no arguments" { + run_tool gpionotify + + output_regex_match ".*at least one GPIO line must be specified" + status_is 1 +} + +@test "gpionotify: with no line specified" { + gpiosim_chip sim0 num_lines=8 + + run_tool gpionotify --chip ${GPIOSIM_CHIP_NAME[sim0]} + + output_regex_match ".*at least one GPIO line must be specified" + status_is 1 +} + +@test "gpionotify: with offset out of range" { + gpiosim_chip sim0 num_lines=4 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + run_tool gpionotify --chip $sim0 5 + + output_regex_match ".*offset 5 is out of range on chip '$sim0'" + status_is 1 +} + +@test "gpionotify: with custom format (event type + offset)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=requested "--format=%e %o" -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_read + output_is "1 4" +} + +@test "gpionotify: with custom format (event type + offset joined)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=requested "--format=%e%o" -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_read + output_is "14" +} + +@test "gpionotify: with custom format (event, chip and line)" { + gpiosim_chip sim0 num_lines=8 line_name=4:baz + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=released \ + "--format=%e %o %E %c %l" -c $sim0 baz + dut_flush + + request_release_line $sim0 4 + dut_regex_match "2 4 released $sim0 baz" +} + +@test "gpionotify: with custom format (seconds timestamp)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=requested "--format=%e %o %S" \ + -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_regex_match "1 4 [0-9]+\\.[0-9]+" +} + +@test "gpionotify: with custom format (UTC timestamp)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=released \ + "--format=%U %e %o" -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_regex_match \ +"[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\\.[0-9]+Z 2 4" +} + +@test "gpionotify: with custom format (localtime timestamp)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=released \ + "--format=%L %e %o" -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_regex_match \ +"[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\\.[0-9]+ 2 4" +} + +@test "gpionotify: with custom format (double percent sign)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=requested "--format=start%%end" \ + -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_read + output_is "start%end" +} + +@test "gpionotify: with custom format (double percent sign + event type specifier)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=requested "--format=%%e" -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_read + output_is "%e" +} + +@test "gpionotify: with custom format (single percent sign)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=requested "--format=%" -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_read + output_is "%" +} + +@test "gpionotify: with custom format (single percent sign between other characters)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=requested "--format=foo % bar" -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_read + output_is "foo % bar" +} + +@test "gpionotify: with custom format (unknown specifier)" { + gpiosim_chip sim0 num_lines=8 + + local sim0=${GPIOSIM_CHIP_NAME[sim0]} + + dut_run gpionotify --banner --event=requested "--format=%x" -c $sim0 4 + dut_flush + + request_release_line $sim0 4 + dut_read + output_is "%x" +}