From patchwork Thu Mar 31 01:11:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 555740 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 863B0C433F5 for ; Thu, 31 Mar 2022 01:12:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352235AbiCaBOE (ORCPT ); Wed, 30 Mar 2022 21:14:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352244AbiCaBOA (ORCPT ); Wed, 30 Mar 2022 21:14:00 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D67466600 for ; Wed, 30 Mar 2022 18:12:13 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id a16so5126056plh.13 for ; Wed, 30 Mar 2022 18:12:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zGj/WGDVyhyJE2QGKE7k5onT9lcIVUucYPJ4LgmEC74=; b=MmZ2ffWylJ6/vndlcW68jlGi9pnjwkpDkhAEJy23RSwVvWj6zF7ZI23Qxz0uIWcHZ8 Tdx6zvYqGo3vUL8sMctymRTmQklXzJZkLOW9HSvmW4YqGVrE7DlkMqCQOzi2kg6xM1zf jd6MBsRp1Jwhrfy7S6GvempPrPoWLWq/DWus+2/rDMPKvhJhemPMOjeSjf2CsxjfWjP0 gAX9bZnjlu5uC9gEljmYr5reTGZjDpIk5h31qnd/POydlGil426iQqbstlgaSjI9jjnn r1BXxcHO9KE8z1vtM88ACP6eVmQ/tsmdKtiks/R1kZyI7nrKpky2ErKxLt95eKuT7CIC Q6bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zGj/WGDVyhyJE2QGKE7k5onT9lcIVUucYPJ4LgmEC74=; b=Q9oS65PuOUPJ/e85X0dqMMxzzfnOm/MhCTxD3tYcy4UxtQV9joHmJ2oYH+Wx/u8raD vUz8vksQ1jxjfMiwu4ELj/TYkprVc4RmtlI3hrVHtBIj5Il6uad4zzddTDJ7/4MyxBH5 ZCIKPLX719m30zpkrFZS3d3YOuE10PBTFGNCc3Mti6Krl/MvGLaQsT/zHQiXLQn5T0ha FpaWC5V2I5LghrUBGoQM5NLkUtqCZy05bjT3aRhFJVkU9v6hRhLZNVaJn7ExhSthxnoB jFwWzkv42RjeKgGv9ZNpF9SdxysjUorQYUe89z24qNjSqxXB89munhNP0wQ2Ayxmgg3e 2JWg== X-Gm-Message-State: AOAM530gSveAtrbOi6CM1NBOVM35uQ10oKkun00aps4hXWJeOVsjTRPm 0pGrTtPxBF84aS90lONapz/VADSnWWidLg== X-Google-Smtp-Source: ABdhPJzSf6CAOt3uyE57ZIP90PBERDhwDgWpCc7L9qa1+cbyH7lSU0kd1ZBJoignifQYuAGTUwmtFQ== X-Received: by 2002:a17:90b:4ac9:b0:1c9:a345:6b9a with SMTP id mh9-20020a17090b4ac900b001c9a3456b9amr3008465pjb.118.1648689132773; Wed, 30 Mar 2022 18:12:12 -0700 (PDT) Received: from sol.home.arpa ([118.209.204.33]) by smtp.gmail.com with ESMTPSA id e6-20020a63aa06000000b00380c8bed5a6sm21232176pgf.46.2022.03.30.18.12.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 18:12:11 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod v2][PATCH 1/4] core: add gpiod_request_lines Date: Thu, 31 Mar 2022 09:11:38 +0800 Message-Id: <20220331011141.53489-2-warthog618@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220331011141.53489-1-warthog618@gmail.com> References: <20220331011141.53489-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a wrapper around gpiod_chip_open() and gpiod_chip_request_lines() for the common use case of only requesting a single set of lines from a chip. The chip itself is only required for the request so hide it from the user. Signed-off-by: Kent Gibson --- include/gpiod.h | 15 ++++++++ lib/line-request.c | 17 +++++++++ tests/tests-line-request.c | 73 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/include/gpiod.h b/include/gpiod.h index 456c2b8..8f64d7f 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -50,6 +50,21 @@ struct gpiod_info_event; struct gpiod_edge_event; struct gpiod_edge_event_buffer; +/** + * @brief Request a set of lines for exclusive usage. + * @param path GPIO chip path. + * @param req_cfg Request config object. + * @param line_cfg Line config object. + * @return New line request object or NULL if an error occurred. The request + * must be released by the caller using ::gpiod_line_request_release. + * @note Line configuration overrides for lines that are not requested are + * silently ignored. + */ +struct gpiod_line_request * +gpiod_request_lines(const char *path, + struct gpiod_request_config *req_cfg, + struct gpiod_line_config *line_cfg); + /** * @defgroup chips GPIO chips * @{ diff --git a/lib/line-request.c b/lib/line-request.c index d578a9e..639a66f 100644 --- a/lib/line-request.c +++ b/lib/line-request.c @@ -16,6 +16,23 @@ struct gpiod_line_request { int fd; }; +GPIOD_API struct gpiod_line_request * +gpiod_request_lines(const char *path, + struct gpiod_request_config *req_cfg, + struct gpiod_line_config *line_cfg) +{ + struct gpiod_chip *chip; + struct gpiod_line_request *request; + + chip = gpiod_chip_open(path); + if (chip == NULL) + return NULL; + + request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); + gpiod_chip_close(chip); + return request; +} + struct gpiod_line_request * gpiod_line_request_from_uapi(struct gpio_v2_line_request *uapi_req) { diff --git a/tests/tests-line-request.c b/tests/tests-line-request.c index 531e466..bf7ebb1 100644 --- a/tests/tests-line-request.c +++ b/tests/tests-line-request.c @@ -332,6 +332,79 @@ GPIOD_TEST_CASE(request_survives_parent_chip) gpiod_test_return_if_failed(); } +GPIOD_TEST_CASE(request_chipless) +{ + static const guint offset = 0; + + g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); + g_autoptr(struct_gpiod_request_config) req_cfg = NULL; + g_autoptr(struct_gpiod_line_config) line_cfg = NULL; + g_autoptr(struct_gpiod_line_request) request = NULL; + gint ret; + + req_cfg = gpiod_test_create_request_config_or_fail(); + line_cfg = gpiod_test_create_line_config_or_fail(); + + gpiod_request_config_set_offsets(req_cfg, 1, &offset); + gpiod_line_config_set_direction_default(line_cfg, + GPIOD_LINE_DIRECTION_OUTPUT); + gpiod_line_config_set_output_value_default(line_cfg, + GPIOD_LINE_VALUE_ACTIVE); + + request = gpiod_request_lines(g_gpiosim_chip_get_dev_path(sim), + req_cfg, line_cfg); + g_assert_nonnull(request); + + g_assert_cmpint(gpiod_line_request_get_value(request, offset), ==, + GPIOD_LINE_VALUE_ACTIVE); + + ret = gpiod_line_request_set_value(request, offset, + GPIOD_LINE_VALUE_ACTIVE); + g_assert_cmpint(ret, ==, 0); + gpiod_test_return_if_failed(); + + ret = gpiod_line_request_set_value(request, offset, + GPIOD_LINE_VALUE_ACTIVE); + g_assert_cmpint(ret, ==, 0); + gpiod_test_return_if_failed(); +} + +GPIOD_TEST_CASE(request_chipless_bad_device) +{ + static const guint offset = 0; + + g_autoptr(struct_gpiod_request_config) req_cfg = NULL; + g_autoptr(struct_gpiod_line_config) line_cfg = NULL; + g_autoptr(struct_gpiod_line_request) request = NULL; + gint ret; + + req_cfg = gpiod_test_create_request_config_or_fail(); + line_cfg = gpiod_test_create_line_config_or_fail(); + + gpiod_request_config_set_offsets(req_cfg, 1, &offset); + gpiod_line_config_set_direction_default(line_cfg, + GPIOD_LINE_DIRECTION_OUTPUT); + gpiod_line_config_set_output_value_default(line_cfg, + GPIOD_LINE_VALUE_ACTIVE); + + request = gpiod_request_lines("/dev/nonexistent", + req_cfg, line_cfg); + g_assert_null(request); + gpiod_test_expect_errno(ENOENT); + + request = gpiod_request_lines("/tmp", + req_cfg, line_cfg); + g_assert_null(request); + gpiod_test_expect_errno(ENOTTY); + + + request = gpiod_request_lines("/dev/null", + req_cfg, line_cfg); + g_assert_null(request); + gpiod_test_expect_errno(ENODEV); +} + + GPIOD_TEST_CASE(request_with_overridden_direction) { static const guint offsets[] = { 0, 1, 2, 3 }; From patchwork Thu Mar 31 01:11:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 555393 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 07B66C433F5 for ; Thu, 31 Mar 2022 01:12:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352298AbiCaBOY (ORCPT ); Wed, 30 Mar 2022 21:14:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352296AbiCaBOW (ORCPT ); Wed, 30 Mar 2022 21:14:22 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBECE66C8D for ; Wed, 30 Mar 2022 18:12:24 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id mj15-20020a17090b368f00b001c637aa358eso1300860pjb.0 for ; Wed, 30 Mar 2022 18:12:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AefIuC4rm+GeraSOrBP2sRV8khqpEosEd9Wd5Xqzs9Q=; b=NKigok4mojsmjnkOLMqs9pRj22pZcUjnz6rvdjD76bV01+0cxSAFHKz8L0gc9FnVIq wuOU0+2l2Bp8Uoo2yqeoCLOKJB3IV1Ek98jSPthkZ/pRyQy5rUFfKnMeqzMBDAToDXiD l37yPTscb5CRoV2G3pWMV3LzASP+eltmcpoKwAKbll1DBr76kXAd0z7siPt6ffMNCGUD PpURYqL3zkW3AfAVXbtUsRtTNPkREoYlNNEkRK9/moqZKPFOZSNMbmqW4w4y+GOZrlZ/ 6ld6MbNltOL+NxC2WSrGGMR8IWk2o3ETEgX99CFJabYnLbY8S1lU7ZA0Y1fHsiCPxt5V jGyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AefIuC4rm+GeraSOrBP2sRV8khqpEosEd9Wd5Xqzs9Q=; b=PhnsniiC9Jhw13qRAWp1+8jXdFsgqJEzYvAIqf+G/CUK2FGDUCPYkedsR749keO+sF 0i4lBWfMTwa+GV+sxZqDR56s4eugKsMnf47k0x2fC9RtO/alkS1QAD13Ahjl8ZhU/qgK +f8yOlCCM3YMTVm9VmKUt0QjlFbKgYXkNIFS8pf7mYYImeLuUAc//igQNwzlFPu9iFg7 Lx7xKnKrxUZcf5+5IYkZVA+pJJqq/FBHdRml4AWi5cjST5nGWt3LhZMHxI9oWJIKrlyk fGsYit8SLWCHHQC+tMC/EzQohyus7094l8KOE8MsaRXzm51KBjvGPT+QZepTaESQTwS/ ChLA== X-Gm-Message-State: AOAM530y/NCvLNrslhAUAqEex+V5w7L8Gvpec4IdelZsbaGxt6uljAD3 zT6lXgPdj642saYvd2gB3hlo5b4MWNwSHA== X-Google-Smtp-Source: ABdhPJxWivlx7I5pewcoBkZyqHSKoCB74D5qs12gd+P08p+U5X2D9eMsP6ibhWWhOcWtkfWCUZLPTQ== X-Received: by 2002:a17:90a:560a:b0:1bc:72e7:3c13 with SMTP id r10-20020a17090a560a00b001bc72e73c13mr3052601pjf.246.1648689144023; Wed, 30 Mar 2022 18:12:24 -0700 (PDT) Received: from sol.home.arpa ([118.209.204.33]) by smtp.gmail.com with ESMTPSA id e6-20020a63aa06000000b00380c8bed5a6sm21232176pgf.46.2022.03.30.18.12.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 18:12:23 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod v2][PATCH 2/4] tools: rename inexistent to nonexistent Date: Thu, 31 Mar 2022 09:11:39 +0800 Message-Id: <20220331011141.53489-3-warthog618@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220331011141.53489-1-warthog618@gmail.com> References: <20220331011141.53489-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org The use of inexistent is virtually nonexistent, so switch to the more usual form. Signed-off-by: Kent Gibson --- tools/gpio-tools-test.bats | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/gpio-tools-test.bats b/tools/gpio-tools-test.bats index 69ad786..32c3861 100755 --- a/tools/gpio-tools-test.bats +++ b/tools/gpio-tools-test.bats @@ -309,8 +309,8 @@ teardown() { 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 "gpioinfo: nonexistent chip" { + run_tool gpioinfo "nonexistent" test "$status" -eq 1 } From patchwork Thu Mar 31 01:11:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 555739 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 18784C4332F for ; Thu, 31 Mar 2022 01:13:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352367AbiCaBOp (ORCPT ); Wed, 30 Mar 2022 21:14:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352316AbiCaBOW (ORCPT ); Wed, 30 Mar 2022 21:14:22 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 068A566FB2 for ; Wed, 30 Mar 2022 18:12:31 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id j8so11855652pll.11 for ; Wed, 30 Mar 2022 18:12:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uJeaChm6PkPCsmLkHBJJfG3eD3SiBAc7Q5XyPGm7aOc=; b=BNpAjIZa3cNBQedz83DwBLBCWdw8TsW4VblZRoiBJAZq7cpoNoLYSRPM3vxBBjAp/3 nDmMLlXQ0HyN2WInpjrqIwkXYIcQKNlAcIm984fdUaM2alPG7Ladjes+GS1NNn42JUME 4YLbfoRatJ7y11Xi8kW4sxID4NKN1dmmYDV+IWbnCuS5pEzHQLlrbU7G1UXD6QL4bLWd LGtFRtT/3VXfbiqN3453qfswvaSL5jXTCxdyaKsybtIShEkCf773jRP7e+BsX3iedTrV PEfTz4fKSCny0Fqe6O6iN8Rb5Y1TwJdZbNaHKd5xwzJdTTJNaZelMNtMLJqZ83Cqfjg0 DqDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uJeaChm6PkPCsmLkHBJJfG3eD3SiBAc7Q5XyPGm7aOc=; b=prtV/UKwxNdQjy+DH/cwsWZOkR6CXF1f7pGJ+IyjaZOjg0mrqwggzK9hFXCGiU7502 qgZoKvSzG6HPK4DvJKckcX0XwAZEIGLOovGPFVGsC+Z3y7mQxEY7ueeLstBWsLdXcbsq FOA/PuT9gidpBX6Dd50M58ykcfpQ/4kj4HbGn7RGh/6tkRNXDCVb+4k+k+cJSMvwj9sx xpRQwyZgIxkK2Cv8PIwXBdOc3m5+6QWbzi0a3a1Iia6KMT6cmJvmlpvzqcS/3wTl0HsR +JA8N/u6ubgkBEVbyhZIANW4l6A2xZ16yhpVodjcxi73CsyDoIl3zrwoQH6dCIQY9Xov m1Bg== X-Gm-Message-State: AOAM531FScCI3eNpc9I1BaMza5ZuhcOswLPZFPuApVl1+GSiTRtqmnHv TFSPJg0PBJv9j5++m9jRaX04q9C0YdHJNQ== X-Google-Smtp-Source: ABdhPJwPt1v1LnsM2qTYfRHBHz3aN1ET4ojsDWiasSeuhYfSqUkxA/jviA3NQppRVg4MiSEOBOfzZQ== X-Received: by 2002:a17:902:bf04:b0:149:c5a5:5323 with SMTP id bi4-20020a170902bf0400b00149c5a55323mr2437799plb.97.1648689150933; Wed, 30 Mar 2022 18:12:30 -0700 (PDT) Received: from sol.home.arpa ([118.209.204.33]) by smtp.gmail.com with ESMTPSA id e6-20020a63aa06000000b00380c8bed5a6sm21232176pgf.46.2022.03.30.18.12.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 18:12:30 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod v2][PATCH 3/4] tools: migrate to gpiod_request_lines Date: Thu, 31 Mar 2022 09:11:40 +0800 Message-Id: <20220331011141.53489-4-warthog618@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220331011141.53489-1-warthog618@gmail.com> References: <20220331011141.53489-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Switch to gpiod_request_lines() for tools that only use the chip to request lines. Signed-off-by: Kent Gibson --- tools/gpiodetect.c | 2 +- tools/gpiofind.c | 2 +- tools/gpioget.c | 11 +++++----- tools/gpioinfo.c | 4 ++-- tools/gpiomon.c | 11 +++++----- tools/gpioset.c | 11 +++++----- tools/tools-common.c | 50 +++++++++++++++++--------------------------- tools/tools-common.h | 5 +++-- 8 files changed, 41 insertions(+), 55 deletions(-) diff --git a/tools/gpiodetect.c b/tools/gpiodetect.c index 8f6e8b3..2fada05 100644 --- a/tools/gpiodetect.c +++ b/tools/gpiodetect.c @@ -67,7 +67,7 @@ int main(int argc, char **argv) die_perror("unable to scan /dev"); for (i = 0; i < num_chips; i++) { - chip = chip_open_by_name(entries[i]->d_name); + chip = chip_open(chip_path_from_name(entries[i]->d_name)); if (!chip) die_perror("unable to open %s", entries[i]->d_name); diff --git a/tools/gpiofind.c b/tools/gpiofind.c index 36eba86..17e1702 100644 --- a/tools/gpiofind.c +++ b/tools/gpiofind.c @@ -67,7 +67,7 @@ int main(int argc, char **argv) die_perror("unable to scan /dev"); for (i = 0; i < num_chips; i++) { - chip = chip_open_by_name(entries[i]->d_name); + chip = chip_open(chip_path_from_name(entries[i]->d_name)); if (!chip) { if (errno == EACCES) continue; diff --git a/tools/gpioget.c b/tools/gpioget.c index 2cf5eae..f4c5d46 100644 --- a/tools/gpioget.c +++ b/tools/gpioget.c @@ -46,11 +46,10 @@ int main(int argc, char **argv) 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, *values; size_t i, num_lines; - char *device, *end; + char *device, *end, *path; for (;;) { optc = getopt_long(argc, argv, shortopts, longopts, &opti); @@ -103,8 +102,8 @@ int main(int argc, char **argv) die("invalid GPIO offset: %s", argv[i + 1]); } - chip = chip_open_lookup(device); - if (!chip) + path = chip_path_lookup(device); + if (!path) die_perror("unable to open %s", device); line_cfg = gpiod_line_config_new(); @@ -126,7 +125,7 @@ int main(int argc, char **argv) gpiod_request_config_set_consumer(req_cfg, "gpioget"); gpiod_request_config_set_offsets(req_cfg, num_lines, offsets); - request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); + request = gpiod_request_lines(path, req_cfg, line_cfg); if (!request) die_perror("unable to request lines"); @@ -144,7 +143,7 @@ int main(int argc, char **argv) gpiod_line_request_release(request); gpiod_request_config_free(req_cfg); gpiod_line_config_free(line_cfg); - gpiod_chip_close(chip); + free(path); free(offsets); free(values); diff --git a/tools/gpioinfo.c b/tools/gpioinfo.c index fbe2a13..f0f9926 100644 --- a/tools/gpioinfo.c +++ b/tools/gpioinfo.c @@ -227,7 +227,7 @@ int main(int argc, char **argv) die_perror("unable to scan /dev"); for (i = 0; i < num_chips; i++) { - chip = chip_open_by_name(entries[i]->d_name); + chip = chip_open(chip_path_from_name(entries[i]->d_name)); if (!chip) die_perror("unable to open %s", entries[i]->d_name); @@ -240,7 +240,7 @@ int main(int argc, char **argv) free(entries); } else { for (i = 0; i < argc; i++) { - chip = chip_open_lookup(argv[i]); + chip = chip_open(chip_path_lookup(argv[i])); if (!chip) die_perror("looking up chip %s", argv[i]); diff --git a/tools/gpiomon.c b/tools/gpiomon.c index f6a0dba..e461458 100644 --- a/tools/gpiomon.c +++ b/tools/gpiomon.c @@ -164,9 +164,8 @@ int main(int argc, char **argv) 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; + char *end, *path; /* * FIXME: use signalfd once the API has been converted to using a single file @@ -250,8 +249,8 @@ int main(int argc, char **argv) num_lines++; } - chip = chip_open_lookup(argv[0]); - if (!chip) + path = chip_path_lookup(argv[0]); + if (!path) die_perror("unable to open %s", argv[0]); line_cfg = gpiod_line_config_new(); @@ -271,7 +270,7 @@ int main(int argc, char **argv) gpiod_request_config_set_consumer(req_cfg, "gpiomon"); gpiod_request_config_set_offsets(req_cfg, num_lines, offsets); - request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); + request = gpiod_request_lines(path, req_cfg, line_cfg); if (!request) die_perror("unable to request lines"); @@ -314,7 +313,7 @@ done: gpiod_line_request_release(request); gpiod_request_config_free(req_cfg); gpiod_line_config_free(line_cfg); - gpiod_chip_close(chip); + free(path); return EXIT_SUCCESS; } diff --git a/tools/gpioset.c b/tools/gpioset.c index 1c11470..7497eab 100644 --- a/tools/gpioset.c +++ b/tools/gpioset.c @@ -195,11 +195,10 @@ int main(int argc, char **argv) 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, *values; size_t i, num_lines; - char *device, *end; + char *device, *end, *path; memset(&cbdata, 0, sizeof(cbdata)); @@ -288,8 +287,8 @@ int main(int argc, char **argv) die("invalid offset: %s", argv[i + 1]); } - chip = chip_open_lookup(device); - if (!chip) + path = chip_path_lookup(device); + if (!path) die_perror("unable to open %s", device); line_cfg = gpiod_line_config_new(); @@ -314,7 +313,7 @@ int main(int argc, char **argv) gpiod_request_config_set_consumer(req_cfg, "gpioset"); gpiod_request_config_set_offsets(req_cfg, num_lines, offsets); - request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); + request = gpiod_request_lines(path, req_cfg, line_cfg); if (!request) die_perror("unable to request lines"); @@ -324,7 +323,7 @@ int main(int argc, char **argv) gpiod_line_request_release(request); gpiod_request_config_free(req_cfg); gpiod_line_config_free(line_cfg); - gpiod_chip_close(chip); + free(path); free(offsets); return EXIT_SUCCESS; diff --git a/tools/tools-common.c b/tools/tools-common.c index f5fd50c..8d4ca12 100644 --- a/tools/tools-common.c +++ b/tools/tools-common.c @@ -114,36 +114,24 @@ int chip_dir_filter(const struct dirent *entry) return !!is_chip; } -struct gpiod_chip *chip_open_by_name(const char *name) +char *chip_path_from_name(const char *name) { - struct gpiod_chip *chip; char *path; - int ret; - ret = asprintf(&path, "/dev/%s", name); - if (ret < 0) + if (asprintf(&path, "/dev/%s", name) < 0) return NULL; - chip = gpiod_chip_open(path); - free(path); - - return chip; + return path; } -static struct gpiod_chip *chip_open_by_number(unsigned int num) +static char *chip_path_from_number(unsigned int num) { - struct gpiod_chip *chip; char *path; - int ret; - ret = asprintf(&path, "/dev/gpiochip%u", num); - if (!ret) + if (asprintf(&path, "/dev/gpiochip%u", num) < 0) return NULL; - chip = gpiod_chip_open(path); - free(path); - - return chip; + return path; } static bool isuint(const char *str) @@ -154,18 +142,18 @@ static bool isuint(const char *str) return *str == '\0'; } -struct gpiod_chip *chip_open_lookup(const char *device) +char *chip_path_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; + if (isuint(device)) + return chip_path_from_number(strtoul(device, NULL, 10)); + if (strncmp(device, "/dev/", 5)) + return chip_path_from_name(device); + return strdup(device); +} + +struct gpiod_chip *chip_open(const char *path) +{ + if (path == NULL) + return NULL; + return gpiod_chip_open(path); } diff --git a/tools/tools-common.h b/tools/tools-common.h index f059440..d0b3a83 100644 --- a/tools/tools-common.h +++ b/tools/tools-common.h @@ -29,7 +29,8 @@ int parse_bias(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); +struct gpiod_chip *chip_open(const char *path); +char *chip_path_from_name(const char *name); +char *chip_path_lookup(const char *device); #endif /* __GPIOD_TOOLS_COMMON_H__ */ From patchwork Thu Mar 31 01:11:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 555392 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 13F51C433EF for ; Thu, 31 Mar 2022 01:13:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352377AbiCaBOu (ORCPT ); Wed, 30 Mar 2022 21:14:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352335AbiCaBOY (ORCPT ); Wed, 30 Mar 2022 21:14:24 -0400 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C99CB6661C for ; Wed, 30 Mar 2022 18:12:37 -0700 (PDT) Received: by mail-pl1-x635.google.com with SMTP id y6so19500506plg.2 for ; Wed, 30 Mar 2022 18:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NVLhKDY5NVzC+PQtKfgyX0Lx7D2H4nIz7XR+RS3dwz4=; b=fwpyD+87B6ffq736RIc9xqkMFk9F28PE+xTna7mfdK8kv6wsqUTCJMj1vfBSMJQ9Xl SCyOWOKgq3hy/8aIEDIsSYqdmolCL8mCo07/kHtCr8AuLUKxnQJCpevr1LFDn6+7KSC/ 6dQSkcp09Arzp3gMr2u8Mv6jxquv1jhwb/inBx1fGF+K7g75Bt4JjwCwJ/O2nTJcTqbw xJMpLwtY3ttOb0MFyZZu5NMaBUWpTQpNh4vLfl1R2mIs9rhyRvDe8SUGkMj6ql8kX/bV 2QSrkY+dobGetB2x5XNgLXmsY0Lmu44gE7lrwOyCH86Ek97qx2C32M6eituTNPSDVfLz EQZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NVLhKDY5NVzC+PQtKfgyX0Lx7D2H4nIz7XR+RS3dwz4=; b=l7mIEgY/uNOBW9/uqNbdZtYUf+R/ZfF7LD6n5mAu3lid9ak1kl0wKUmzcSFnLQPXIA B6fB+x5QJJLijr3PnCP1rWL4vnt3+JEdkUJxiu4K5eaBjVRMDgOPsU53SByUWcp7Ik60 N/mPrbwCXfx6KlRWueAwGygq3q/o+3U5jN9akuBW2OPq9Wogg81TgN/2Imvwo9nfmDQc axA4GuVn4OMt7FbabPx+FX6In2UzWkI2KTdyLRPLAGlRetS/JIQjli8hCmU1+Y1ojxuZ 0Tr+decvJRlRVLjria2VKMjp/cpE9R8TEgHTMXwlEYB/mB4TpJ3RkgXxqvrqhfD3avoK FHOQ== X-Gm-Message-State: AOAM530kfIY64tEVqV/92i7a04kfubWgXJMNysCsHkp3Fu6LqD9g653m rXzEzOC8ED1tAxMTFn2p1jcHObtwJ7XWew== X-Google-Smtp-Source: ABdhPJySulKFxtpAsoxAiQUy4/QdNWjA4QAC4NSeLFUFC0Qf3cEbA8O1qjN7tXJBWDCwGXYpScIluA== X-Received: by 2002:a17:90a:e7d2:b0:1c7:b410:ccfc with SMTP id kb18-20020a17090ae7d200b001c7b410ccfcmr2886900pjb.209.1648689157024; Wed, 30 Mar 2022 18:12:37 -0700 (PDT) Received: from sol.home.arpa ([118.209.204.33]) by smtp.gmail.com with ESMTPSA id e6-20020a63aa06000000b00380c8bed5a6sm21232176pgf.46.2022.03.30.18.12.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 18:12:36 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod v2][PATCH 4/4] tools: minimize object lifetimes Date: Thu, 31 Mar 2022 09:11:41 +0800 Message-Id: <20220331011141.53489-5-warthog618@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220331011141.53489-1-warthog618@gmail.com> References: <20220331011141.53489-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org The tools double as examples of API usage, so keep object lifetimes to a minimum to highlight where transient objects are no longer required and may be discarded. Signed-off-by: Kent Gibson --- tools/gpioget.c | 16 ++++++++++------ tools/gpiomon.c | 7 ++++--- tools/gpioset.c | 9 +++++---- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/tools/gpioget.c b/tools/gpioget.c index f4c5d46..f789198 100644 --- a/tools/gpioget.c +++ b/tools/gpioget.c @@ -92,8 +92,7 @@ int main(int argc, char **argv) num_lines = argc - 1; offsets = calloc(num_lines, sizeof(*offsets)); - values = calloc(num_lines, sizeof(*values)); - if (!offsets || ! values) + if (!offsets) die("out of memory"); for (i = 0; i < num_lines; i++) { @@ -124,11 +123,20 @@ int main(int argc, char **argv) gpiod_request_config_set_consumer(req_cfg, "gpioget"); gpiod_request_config_set_offsets(req_cfg, num_lines, offsets); + free(offsets); request = gpiod_request_lines(path, req_cfg, line_cfg); if (!request) die_perror("unable to request lines"); + free(path); + gpiod_request_config_free(req_cfg); + gpiod_line_config_free(line_cfg); + + values = calloc(num_lines, sizeof(*values)); + if (!values) + die("out of memory"); + ret = gpiod_line_request_get_values(request, values); if (ret) die_perror("unable to read GPIO line values"); @@ -141,10 +149,6 @@ int main(int argc, char **argv) printf("\n"); gpiod_line_request_release(request); - gpiod_request_config_free(req_cfg); - gpiod_line_config_free(line_cfg); - free(path); - free(offsets); free(values); return EXIT_SUCCESS; diff --git a/tools/gpiomon.c b/tools/gpiomon.c index e461458..34de2b2 100644 --- a/tools/gpiomon.c +++ b/tools/gpiomon.c @@ -274,6 +274,10 @@ int main(int argc, char **argv) if (!request) die_perror("unable to request lines"); + free(path); + gpiod_request_config_free(req_cfg); + gpiod_line_config_free(line_cfg); + event_buffer = gpiod_edge_event_buffer_new(EVENT_BUF_SIZE); if (!event_buffer) die_perror("unable to allocate the line event buffer"); @@ -311,9 +315,6 @@ int main(int argc, char **argv) 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); - free(path); return EXIT_SUCCESS; } diff --git a/tools/gpioset.c b/tools/gpioset.c index 7497eab..f28f8b6 100644 --- a/tools/gpioset.c +++ b/tools/gpioset.c @@ -312,19 +312,20 @@ int main(int argc, char **argv) gpiod_request_config_set_consumer(req_cfg, "gpioset"); gpiod_request_config_set_offsets(req_cfg, num_lines, offsets); + free(offsets); request = gpiod_request_lines(path, req_cfg, line_cfg); if (!request) die_perror("unable to request lines"); + free(path); + gpiod_request_config_free(req_cfg); + gpiod_line_config_free(line_cfg); + if (mode->callback) mode->callback(&cbdata); gpiod_line_request_release(request); - gpiod_request_config_free(req_cfg); - gpiod_line_config_free(line_cfg); - free(path); - free(offsets); return EXIT_SUCCESS; }