From patchwork Thu Jul 20 14:47:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 704726 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 8FB45EB64DC for ; Thu, 20 Jul 2023 14:48:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230106AbjGTOsC (ORCPT ); Thu, 20 Jul 2023 10:48:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230150AbjGTOsB (ORCPT ); Thu, 20 Jul 2023 10:48:01 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0BD010F5 for ; Thu, 20 Jul 2023 07:48:00 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-3159d75606dso715978f8f.1 for ; Thu, 20 Jul 2023 07:48:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1689864479; x=1690469279; 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=T990pQBICrhmrrL7XRpnEuELQVcO3YU9HP2mAzxkw9s=; b=SIe5dCl3FOE9Dpg0DHlQ49yLGkl93TnoBlVILOpnVVXjKV9BnYRWDvHNKch0aysh8W h+ie7oGdkes+lQfqsbPWcK7xkA19SCt6sxSwofUD7ibEBqn01Eheo6OH+lmHY1BttIVu 6tvDHe7AtcgR/NoEcp0lIEQlsd/0wRAPQWMc88kjRAG1uEelC5GvE7aD1Aa6tSlKCWPx tsIV870OonUPoZslRGzbh6fZikOjGWBkoTIQleDSVC0NwwTzN5l0QkHQPMF3V862EHA8 PbU4UgfW28Qgh71H7QZNYLLrQxn3n0bRH26NUYY1E57LaJXJgSGwF1iy7dgPHHC5/lqW 37fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689864479; x=1690469279; 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=T990pQBICrhmrrL7XRpnEuELQVcO3YU9HP2mAzxkw9s=; b=ixN97oCtQ92+d5zCdgZ35Yd5ELT7BfSkt+T+33btb3xnDPluFozRIKh7AwZpUYMPq4 0PxMp55fQIPQqvqHBdOecD9prGKRJu5Jr7qyFeOjxdSG7PhsRL6KZMXJtNLGLPhvph7O pQXOpuCshEm/jjcC4TJ2egdSWvP0LuJ3IBe4YuvVkFUBztEHZIPZ4P6cTpr3yhGxvd2s eYN23bbUwGw6oeQ1TLUaD8qTZnD8mXtwj88IzlhtSC4hn/SyEINHgR8GkQUN+4DKBTF5 s3q8rcyUTiynd0TzefAT6cZ1mzf/om06VMsfnlinmzSxU5b/hHLYvKk6PYTcfvrel+Fg IX4A== X-Gm-Message-State: ABy/qLbC8uzzsYFKj4gFCLQP61cRidmsyPVnxKmMKJU9K6DhsQWHLzKr LOmr/eD7LmDG3/fuKw8xsjBGaQ== X-Google-Smtp-Source: APBJJlFQisOjt6IswGPmqVEKHz4oEox+lUiESqRw2F0eUZ9URzHy/TygYIMspuNrgzpLmAX7Hq8CiA== X-Received: by 2002:adf:e484:0:b0:314:1d7f:a69 with SMTP id i4-20020adfe484000000b003141d7f0a69mr2121327wrm.67.1689864479280; Thu, 20 Jul 2023 07:47:59 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:bd83:c166:7e2d:3d74]) by smtp.gmail.com with ESMTPSA id r1-20020adfce81000000b0031128382ed0sm1550906wrn.83.2023.07.20.07.47.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 07:47:58 -0700 (PDT) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar , Erik Schilling Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH v2 1/5] core: provide gpiod_line_request_get_chip_name() Date: Thu, 20 Jul 2023 16:47:43 +0200 Message-Id: <20230720144747.73276-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230720144747.73276-1-brgl@bgdev.pl> References: <20230720144747.73276-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski While we can get the list of requested offsets from a line-request object, this information lacks context if we don't provide any data about the GPIO chip the request was made on. Add a helper allowing users to get the name of the parent chip. Signed-off-by: Bartosz Golaszewski --- include/gpiod.h | 9 +++++++++ lib/chip.c | 7 ++++++- lib/internal.h | 3 ++- lib/line-request.c | 20 +++++++++++++++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/gpiod.h b/include/gpiod.h index 3c13783..71ae798 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -1007,6 +1007,15 @@ gpiod_request_config_get_event_buffer_size(struct gpiod_request_config *config); */ void gpiod_line_request_release(struct gpiod_line_request *request); +/** + * @brief Get the name of the chip this request was made on. + * @param request Line request object. + * @return Name the GPIO chip device. The returned pointer is valid for the + * lifetime of the request object and must not be freed by the caller. + */ +const char * +gpiod_line_request_get_chip_name(struct gpiod_line_request *request); + /** * @brief Get the number of lines in the request. * @param request Line request object. diff --git a/lib/chip.c b/lib/chip.c index 7d4d21e..7c05e53 100644 --- a/lib/chip.c +++ b/lib/chip.c @@ -215,6 +215,7 @@ gpiod_chip_request_lines(struct gpiod_chip *chip, { struct gpio_v2_line_request uapi_req; struct gpiod_line_request *request; + struct gpiochip_info info; int ret; assert(chip); @@ -233,11 +234,15 @@ gpiod_chip_request_lines(struct gpiod_chip *chip, if (ret) return NULL; + ret = read_chip_info(chip->fd, &info); + if (ret < 0) + return NULL; + ret = ioctl(chip->fd, GPIO_V2_GET_LINE_IOCTL, &uapi_req); if (ret < 0) return NULL; - request = gpiod_line_request_from_uapi(&uapi_req); + request = gpiod_line_request_from_uapi(&uapi_req, info.name); if (!request) { close(uapi_req.fd); return NULL; diff --git a/lib/internal.h b/lib/internal.h index ef9b17e..61d7aec 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -26,7 +26,8 @@ void gpiod_request_config_to_uapi(struct gpiod_request_config *config, int gpiod_line_config_to_uapi(struct gpiod_line_config *config, struct gpio_v2_line_request *uapi_cfg); struct gpiod_line_request * -gpiod_line_request_from_uapi(struct gpio_v2_line_request *uapi_req); +gpiod_line_request_from_uapi(struct gpio_v2_line_request *uapi_req, + const char *chip_name); int gpiod_edge_event_buffer_read_fd(int fd, struct gpiod_edge_event_buffer *buffer, size_t max_events); diff --git a/lib/line-request.c b/lib/line-request.c index e536355..e867d91 100644 --- a/lib/line-request.c +++ b/lib/line-request.c @@ -13,13 +13,15 @@ #include "internal.h" struct gpiod_line_request { + char *chip_name; unsigned int offsets[GPIO_V2_LINES_MAX]; size_t num_lines; int fd; }; struct gpiod_line_request * -gpiod_line_request_from_uapi(struct gpio_v2_line_request *uapi_req) +gpiod_line_request_from_uapi(struct gpio_v2_line_request *uapi_req, + const char *chip_name) { struct gpiod_line_request *request; @@ -28,6 +30,13 @@ gpiod_line_request_from_uapi(struct gpio_v2_line_request *uapi_req) return NULL; memset(request, 0, sizeof(*request)); + + request->chip_name = strdup(chip_name); + if (!request->chip_name) { + free(request); + return NULL; + } + request->fd = uapi_req->fd; request->num_lines = uapi_req->num_lines; memcpy(request->offsets, uapi_req->offsets, @@ -42,9 +51,18 @@ GPIOD_API void gpiod_line_request_release(struct gpiod_line_request *request) return; close(request->fd); + free(request->chip_name); free(request); } +GPIOD_API const char * +gpiod_line_request_get_chip_name(struct gpiod_line_request *request) +{ + assert(request); + + return request->chip_name; +} + GPIOD_API size_t gpiod_line_request_get_num_requested_lines(struct gpiod_line_request *request) { From patchwork Thu Jul 20 14:47:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 705100 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 DEC67EB64DA for ; Thu, 20 Jul 2023 14:48:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230464AbjGTOsI (ORCPT ); Thu, 20 Jul 2023 10:48:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231151AbjGTOsH (ORCPT ); Thu, 20 Jul 2023 10:48:07 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C70D71722 for ; Thu, 20 Jul 2023 07:48:01 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3fbc54cab6fso7196555e9.0 for ; Thu, 20 Jul 2023 07:48:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1689864480; x=1690469280; 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=dTZVEED0n66JzC14z8Y1dyJsVAMPVbfXgo6SBVqhOmM=; b=5JzdMHLXkyEhZYg7CUm1aleIZQcSL2nkKdFNAwDHqvxdZFgoVzkRTLOj9phUk2L4DK kI17XlXM6MFievJpnpXc+w9csF0WfgzcyyHKcOMvqaUBAGGMcymcavx39cND1I15+BoE ZIsqHxv+Y9XNC0JvYsxRfK1OB/U7lKQtbb37G20ia+JHD6aVNZHqhKMAhEqvHrcrBwbZ hkb1tbLr8zof3wexOhiEYKN6QoPG+sFSMkzqrII9De15nSlzYBkJ2l/ipI5hIzXM4DVp pHr1Ymqaot84VjIQ5cYitpZd5WPUwG67VLG1fZiu3XGqDcVxUE/DkoMQTiBbdSkaAHCK 7wnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689864480; x=1690469280; 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=dTZVEED0n66JzC14z8Y1dyJsVAMPVbfXgo6SBVqhOmM=; b=bF5Tvpkwi7Jooa3NBlQJaWczf7fFkhY0EvT5QkN7UylpHZ8bFIZSrc+arrZlXkwL8D u6ubuW2YtUQhGyPSn39j4VIEXrt2TwoVdGHR4L8oEM5vVt2C/BYnp1HM/sZXGTSk/GPg Y/zHVTpcDWDn3pdCTVkgQzNZi/VYefzMsCuMIhvC+0B2cv/l9BYtPm4wsZB8vGFw4a6j J51z7sRhwdKCsUFNusRbWbU5yj1R/f5LocqSLbQ3tFJnhd8/T7Cr68b08cJdjIcniBRb 3kb0W2kq8UCelxIg2wd+iakP7sjXo7qa5H4/oV3v4B1iKP4St65C0XZGRm8ENDXva14G IDmw== X-Gm-Message-State: ABy/qLZy2/TVg3Iqg30KOc8pbCduIPvLhyahZ70jeff5Gq5EdlE4V3bj ODCXtE+mWBicLvfLdfiigWHwjw== X-Google-Smtp-Source: APBJJlG96IaRTak/hUyKcVaF/fMszw4gQMjAsfQGailp5kkdylkrTs6by3wl71Q7gX9erq6P7MhrNw== X-Received: by 2002:a1c:ed16:0:b0:3fb:dd5d:76b with SMTP id l22-20020a1ced16000000b003fbdd5d076bmr1915049wmh.7.1689864480159; Thu, 20 Jul 2023 07:48:00 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:bd83:c166:7e2d:3d74]) by smtp.gmail.com with ESMTPSA id r1-20020adfce81000000b0031128382ed0sm1550906wrn.83.2023.07.20.07.47.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 07:47:59 -0700 (PDT) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar , Erik Schilling Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH v2 2/5] tests: add a test-case for gpiod_line_request_get_chip_name() Date: Thu, 20 Jul 2023 16:47:44 +0200 Message-Id: <20230720144747.73276-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230720144747.73276-1-brgl@bgdev.pl> References: <20230720144747.73276-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Add a test case for the chip name getter on line-request objects. Signed-off-by: Bartosz Golaszewski --- tests/tests-line-request.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/tests-line-request.c b/tests/tests-line-request.c index 0b985e2..1a58275 100644 --- a/tests/tests-line-request.c +++ b/tests/tests-line-request.c @@ -675,3 +675,24 @@ GPIOD_TEST_CASE(get_requested_offsets_less_and_more) g_assert_cmpuint(retrieved[2], ==, 2); g_assert_cmpuint(retrieved[3], ==, 3); } + +GPIOD_TEST_CASE(get_chip_name) +{ + static const guint offset = 4; + + g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); + g_autoptr(struct_gpiod_chip) chip = NULL; + g_autoptr(struct_gpiod_line_config) line_cfg = NULL; + g_autoptr(struct_gpiod_line_request) request = NULL; + + chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); + line_cfg = gpiod_test_create_line_config_or_fail(); + + gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, + NULL); + + request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); + + g_assert_cmpstr(g_gpiosim_chip_get_name(sim), ==, + gpiod_line_request_get_chip_name(request)); +} From patchwork Thu Jul 20 14:47:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 704725 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 A9151C001DC for ; Thu, 20 Jul 2023 14:48:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231303AbjGTOsF (ORCPT ); Thu, 20 Jul 2023 10:48:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230150AbjGTOsE (ORCPT ); Thu, 20 Jul 2023 10:48:04 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02D7510F5 for ; Thu, 20 Jul 2023 07:48:03 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-3158a5e64b6so748558f8f.0 for ; Thu, 20 Jul 2023 07:48:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1689864481; x=1690469281; 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=/LN3HBqQ/z8x29O7+OLP3+jUwXpgcldLFQfrT/3tFVc=; b=jCqvTaiUlgPgoCB2/54d97Hx86MIy088fPy2SoLP20rfcMgsHHTqB3ug4DXqtioXQC BUx5o7l0O/MBt2SXjSAFXY3sLXaDeSZfap7j/Ov+wSNfZGcVYunOjZciDdPMJEBu+kxI J0KJ4M/wrUwA5ePDXbW4fA0JP1THkoDR/iEjAbvnTmHxZz7971UM1OGuCyNmhIp7yLQO dzBi9McX0BWFTtAwTsDpOuJe2OuCIK6MmCUMpJ6DKxRR6ORW8ob2rh5HUJGziMbtDShG D64cpNXx9VqzSVLclqdeFCWnClYd4JkQaKhQc/VQFsPt5UfyUvof1EXq4vhvd5/ZD8uj YO3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689864481; x=1690469281; 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=/LN3HBqQ/z8x29O7+OLP3+jUwXpgcldLFQfrT/3tFVc=; b=DcOXZ0iwn1NdGjE8LCk3ak7AuSd/gF57f6BZRTTuXfayNtOCqzn1eXdBqU89iZNcX3 EFnJru9Mz4oackoaot1apDuecPTZW94k+UgcE4/XYjN7F0Ams4D3aBm1I6A6nGAJikS7 ACCIDheDElDN4SUPLlIfnBYWnQKm5d9G5KKOzYhZ/Xn9QbgA0I9WCazDh1e5nHlFBqF6 UmF9IS/d++x+gPwL9vRnc4wgZnrQKY0RHCoe1wPpatCpsFkgw+4L4aFZPhIU0YkdwSN3 Rrz5Q5FdutEcd4we5rDVxZsQxynLm+RwpvakR0+MDq5R4cZdbAjLdpmBWP4zaLoIMaGF /aZw== X-Gm-Message-State: ABy/qLb+DSqsD0U9j4Uk+smbtrDfZyWB8UfEziSq7ipWM8kt3JOsMTpP aPPN6Uew43ZUzEKhNKzkhYSj0A== X-Google-Smtp-Source: APBJJlG0bncmHd8kuKrcnn1teeLe7ZSoaKhNNNF1Wt5X9KzE0S/E6zvmon0Fn/c1bLGoL9IhkTGS9A== X-Received: by 2002:adf:d846:0:b0:315:8f4f:81b1 with SMTP id k6-20020adfd846000000b003158f4f81b1mr2116132wrl.50.1689864481222; Thu, 20 Jul 2023 07:48:01 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:bd83:c166:7e2d:3d74]) by smtp.gmail.com with ESMTPSA id r1-20020adfce81000000b0031128382ed0sm1550906wrn.83.2023.07.20.07.48.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 07:48:00 -0700 (PDT) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar , Erik Schilling Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH v2 3/5] bindings: cxx: provide line_request::chip_name() Date: Thu, 20 Jul 2023 16:47:45 +0200 Message-Id: <20230720144747.73276-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230720144747.73276-1-brgl@bgdev.pl> References: <20230720144747.73276-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Provide a wrapper around gpiod_line_request_get_chip_name() for C++ bindings and update the tests. Signed-off-by: Bartosz Golaszewski --- bindings/cxx/gpiodcxx/line-request.hpp | 6 ++++++ bindings/cxx/line-request.cpp | 10 +++++++++- bindings/cxx/tests/tests-line-request.cpp | 6 ++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/bindings/cxx/gpiodcxx/line-request.hpp b/bindings/cxx/gpiodcxx/line-request.hpp index c1e1520..8c1b474 100644 --- a/bindings/cxx/gpiodcxx/line-request.hpp +++ b/bindings/cxx/gpiodcxx/line-request.hpp @@ -75,6 +75,12 @@ public: */ void release(); + /** + * @brief Get the name of the chip this request was made on. + * @return Name to the GPIO chip. + */ + ::std::string chip_name() const; + /** * @brief Get the number of requested lines. * @return Number of lines in this request. diff --git a/bindings/cxx/line-request.cpp b/bindings/cxx/line-request.cpp index b0723c3..e8e0b96 100644 --- a/bindings/cxx/line-request.cpp +++ b/bindings/cxx/line-request.cpp @@ -63,6 +63,13 @@ GPIOD_CXX_API void line_request::release() this->_m_priv->request.reset(); } +GPIOD_CXX_API ::std::string line_request::chip_name() const +{ + this->_m_priv->throw_if_released(); + + return ::gpiod_line_request_get_chip_name(this->_m_priv->request.get()); +} + GPIOD_CXX_API ::std::size_t line_request::num_lines() const { this->_m_priv->throw_if_released(); @@ -222,7 +229,8 @@ GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const line_request if (!request) out << "gpiod::line_request(released)"; else - out << "gpiod::line_request(num_lines=" << request.num_lines() << + out << "gpiod::line_request(chip=\"" << request.chip_name() << + "\", num_lines=" << request.num_lines() << ", line_offsets=" << request.offsets() << ", fd=" << request.fd() << ")"; diff --git a/bindings/cxx/tests/tests-line-request.cpp b/bindings/cxx/tests/tests-line-request.cpp index d1a56ae..9632ae0 100644 --- a/bindings/cxx/tests/tests-line-request.cpp +++ b/bindings/cxx/tests/tests-line-request.cpp @@ -468,14 +468,16 @@ TEST_CASE("line_request stream insertion operator works", "[line-request]") .set_num_lines(4) .build(); - auto request = ::gpiod::chip(sim.dev_path()) + auto chip = ::gpiod::chip(sim.dev_path()); + auto request = chip .prepare_request() .add_line_settings({ 3, 1, 0, 2}, ::gpiod::line_settings()) .do_request(); ::std::stringstream buf, expected; - expected << "gpiod::line_request(num_lines=4, line_offsets=gpiod::offsets(3, 1, 0, 2), fd=" << + expected << "gpiod::line_request(chip=\"" << sim.name() << + "\", num_lines=4, line_offsets=gpiod::offsets(3, 1, 0, 2), fd=" << request.fd() << ")"; SECTION("active request") From patchwork Thu Jul 20 14:47:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 705101 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 6B2B2EB64DD for ; Thu, 20 Jul 2023 14:48:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230150AbjGTOsF (ORCPT ); Thu, 20 Jul 2023 10:48:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230464AbjGTOsE (ORCPT ); Thu, 20 Jul 2023 10:48:04 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9104F171E for ; Thu, 20 Jul 2023 07:48:03 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-3fbc0609cd6so7122425e9.1 for ; Thu, 20 Jul 2023 07:48:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1689864482; x=1690469282; 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=6vLZOYxnnvhagtudLBu9fBVf4KzmQKUtoa3RQs/0e9Q=; b=prD+YoPul8UpF7S30KqtP9J+ySgIoFrvmaSL6BNMxntI59uczUgufY5he3nPEvfOvL Joy0SMYKeN1hK34oZKhoFyGdq7bpuUZzE6qEQe3TxiGd2qYfPDk8NmshqwFl0Ws8ax4L yHtET2GwomZSPoCWA9KyMeDhc++G3nv20UEbyoLkzc7EAZg/05B2zMy4+3DCSzyGnBmD q3v6h9wT6ZuNP3vBEXWlXxxGINpG57XxsU3YZEbvvPL8/aZBqyG2E7NbS8j8cmi2rL3R BKALIm7hnS5sMXI0v5f0eoETmhRk1Uh0/K5A5lt78H2ouaPsZGYljS01HLzkMAHNLyJ6 /JXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689864482; x=1690469282; 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=6vLZOYxnnvhagtudLBu9fBVf4KzmQKUtoa3RQs/0e9Q=; b=K7cEgQP268o+1W6pbTdHJL/i/Oe29uA4HMBKW7dgjHyhifWLLdUtrLmWuo4NIinb5N IFYCU7T22IDIvlRUVuDUViPDOdnxAJK8Yf4ezhWJHrdUQZ5VOdQyeg3M3xOEmWBGH2Vq F5BHOFdIwThcBMAqCrnw7uIYNpsdoCSdtQLDAMbQ3jYxUahXpACPsjSgVSe4YomF1ySt 3lxxoFYetyWAslLdPD9jY4UOIeZ9Upf5nw2H3bnugYNlYYZmByd9MLuCSfizG/PqX9uk LJAhUDd/mD1LgdiJgKkKiHx4prhpLYTaTjQjZORM6GfcF0WM0RCXj5GSSaez+GGIMq/4 5ZHQ== X-Gm-Message-State: ABy/qLa16bRqJVjK/mWrigXVIH43+HwvhTR9T3efqvBjU+7eAHyA4UQY pWa9mv1d0V5xjvxGxXdQF9HnFw== X-Google-Smtp-Source: APBJJlEhGw96fTOcKAzNizZQi/ypWy1Xcyq7AjYpnl3fBqAkZl6GHPNQ/FHhxL2ozOJ2Mj0e9Ywo4Q== X-Received: by 2002:a05:600c:b41:b0:3fc:85c:5ed2 with SMTP id k1-20020a05600c0b4100b003fc085c5ed2mr4327861wmr.11.1689864482087; Thu, 20 Jul 2023 07:48:02 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:bd83:c166:7e2d:3d74]) by smtp.gmail.com with ESMTPSA id r1-20020adfce81000000b0031128382ed0sm1550906wrn.83.2023.07.20.07.48.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 07:48:01 -0700 (PDT) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar , Erik Schilling Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH v2 4/5] bindings: python: provide the chip_name property in line_request Date: Thu, 20 Jul 2023 16:47:46 +0200 Message-Id: <20230720144747.73276-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230720144747.73276-1-brgl@bgdev.pl> References: <20230720144747.73276-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Provide a wrapper around gpiod_line_request_get_chip_name() for Python bindings and update the tests. Signed-off-by: Bartosz Golaszewski --- bindings/python/gpiod/chip.py | 1 + bindings/python/gpiod/ext/request.c | 11 +++++++++++ bindings/python/gpiod/line_request.py | 12 ++++++++++-- bindings/python/tests/tests_line_request.py | 13 ++++++++----- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py index da93370..b3d8e61 100644 --- a/bindings/python/gpiod/chip.py +++ b/bindings/python/gpiod/chip.py @@ -315,6 +315,7 @@ class Chip: req_internal = self._chip.request_lines(line_cfg, consumer, event_buffer_size) request = LineRequest(req_internal) + request._chip_name = req_internal.chip_name request._offsets = req_internal.offsets request._name_map = name_map request._offset_map = offset_map diff --git a/bindings/python/gpiod/ext/request.c b/bindings/python/gpiod/ext/request.c index a32ff8f..5db69fe 100644 --- a/bindings/python/gpiod/ext/request.c +++ b/bindings/python/gpiod/ext/request.c @@ -37,6 +37,13 @@ static void request_finalize(request_object *self) gpiod_edge_event_buffer_free(self->buffer); } +static PyObject * +request_chip_name(request_object *self, void *Py_UNUSED(ignored)) +{ + return PyUnicode_FromString( + gpiod_line_request_get_chip_name(self->request)); +} + static PyObject * request_num_lines(request_object *self, void *Py_UNUSED(ignored)) { @@ -92,6 +99,10 @@ static PyObject *request_fd(request_object *self, void *Py_UNUSED(ignored)) } static PyGetSetDef request_getset[] = { + { + .name = "chip_name", + .get = (getter)request_chip_name, + }, { .name = "num_lines", .get = (getter)request_num_lines, diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py index 096bf18..cde298f 100644 --- a/bindings/python/gpiod/line_request.py +++ b/bindings/python/gpiod/line_request.py @@ -212,10 +212,18 @@ class LineRequest: if not self._req: return "" - return "".format( - self.num_lines, self.offsets, self.fd + return ''.format( + self.chip_name, self.num_lines, self.offsets, self.fd ) + @property + def chip_name(self) -> str: + """ + Name of the chip this request was made on. + """ + self._check_released() + return self._chip_name + @property def num_lines(self) -> int: """ diff --git a/bindings/python/tests/tests_line_request.py b/bindings/python/tests/tests_line_request.py index aa84b9a..f99b93d 100644 --- a/bindings/python/tests/tests_line_request.py +++ b/bindings/python/tests/tests_line_request.py @@ -529,11 +529,14 @@ class LineRequestStringRepresentation(TestCase): del self.sim def test_str(self): - with gpiod.request_lines(self.sim.dev_path, config={(2, 6, 4, 1): None}) as req: - self.assertEqual( - str(req), - "".format(req.fd), - ) + with gpiod.Chip(self.sim.dev_path) as chip: + with chip.request_lines(config={(2, 6, 4, 1): None}) as req: + self.assertEqual( + str(req), + ''.format( + self.sim.name, req.fd + ), + ) def test_str_released(self): req = gpiod.request_lines(self.sim.dev_path, config={(2, 6, 4, 1): None}) From patchwork Thu Jul 20 14:47:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 704724 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 F08F8EB64DD for ; Thu, 20 Jul 2023 14:48:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231320AbjGTOsK (ORCPT ); Thu, 20 Jul 2023 10:48:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231151AbjGTOsJ (ORCPT ); Thu, 20 Jul 2023 10:48:09 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7116A1731 for ; Thu, 20 Jul 2023 07:48:04 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3fd18b1d924so7057995e9.1 for ; Thu, 20 Jul 2023 07:48:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1689864483; x=1690469283; 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=nEeTm4tgLAY/wxS6usZ00FvAJIkc2XjlANbsv7waCck=; b=e1lDVxmlg7Al1vFARIq13fFIwaApdE++FHGu9d+2h1EtCGiwfw3i9gJ9AHTpvXc1ln Z7YJAWRFoZ4M7vUbGQZBjv1RkxHTeosKN/4FF9hyyWr5r5YfUoiy6aoXGap0U1FOFDZc dJLH+1q8rwQ4f9CaQT5gnoz3pUqLtRe/FR1LG77MdWE/2DGGu5/YbJCujRd2BfAq9vtg ziebJPVbEMYhZR8LYHlUSyE9j46N/FySTtiwngSsSlDcT948PA/AD6Z4fFuRAzitDdH9 AE/6wUh8ykdn4dZNnzuIWiCQD4mu1XDJv1xtATNIGlj2xsikyy+kPiMB5fWdUSKi7crS IkRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689864483; x=1690469283; 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=nEeTm4tgLAY/wxS6usZ00FvAJIkc2XjlANbsv7waCck=; b=igT4myh2khQmXccD6dHfOoIx5irC1q9tvx7xzEoBu3C7WUdRnduXNL9dRuvHPpiHD6 SMVgZl5z5R+EmExgpsVQAa9G4cur6nESqiBpOXRBUkNDLgDzmZURBLi/oql6HGz8JIGb 81THrb1erN7hpuNmkqquV+h3KLlHORX9brp29+BBIwry+LJ+bKFogpMJVJVwXF84RjaH RlxBwLp1StXimc8lEYYpeeNznOYRHBroAroS/5eGfwRT9iRkolytAgZpdej7iuWO8z2X KF2i79c4LwsKZQxQNUcGg47PlSk46PtWgYiEXP1oX2QeCRuS+/pjvejZM9JA1KcFZ/aR N/Iw== X-Gm-Message-State: ABy/qLae6w5m7AZZ80R5WdMjJKZmmc9s1HdODvHAxMIoYLz+vJuMM4ku ma83+JRRZCn6liOVq0tgGKrO/A== X-Google-Smtp-Source: APBJJlHyW0fYMf6/3jZ0iIfNbyuS3mCUo8EDv3DLSZrlWHIzwJKCm41KtayPB7Ok0r0X0DpRQDE1IQ== X-Received: by 2002:a05:600c:213:b0:3f6:91c:4e86 with SMTP id 19-20020a05600c021300b003f6091c4e86mr2018313wmi.3.1689864482941; Thu, 20 Jul 2023 07:48:02 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:bd83:c166:7e2d:3d74]) by smtp.gmail.com with ESMTPSA id r1-20020adfce81000000b0031128382ed0sm1550906wrn.83.2023.07.20.07.48.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 07:48:02 -0700 (PDT) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar , Erik Schilling Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH v2 5/5] bindings: rust: provide LineRequest::chip_name() Date: Thu, 20 Jul 2023 16:47:47 +0200 Message-Id: <20230720144747.73276-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230720144747.73276-1-brgl@bgdev.pl> References: <20230720144747.73276-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Provide a wrapper around gpiod_line_request_get_chip_name() for Rust bindings and add a test-case. Signed-off-by: Bartosz Golaszewski Reviewed-by: Kent Gibson Acked-by: Viresh Kumar --- bindings/rust/libgpiod/src/line_request.rs | 16 ++++++++++++++++ bindings/rust/libgpiod/tests/line_request.rs | 14 ++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/bindings/rust/libgpiod/src/line_request.rs b/bindings/rust/libgpiod/src/line_request.rs index 1140aa9..737c06f 100644 --- a/bindings/rust/libgpiod/src/line_request.rs +++ b/bindings/rust/libgpiod/src/line_request.rs @@ -2,6 +2,7 @@ // SPDX-FileCopyrightText: 2022 Linaro Ltd. // SPDX-FileCopyrightText: 2022 Viresh Kumar +use std::ffi::CStr; use std::os::unix::prelude::AsRawFd; use std::time::Duration; @@ -25,6 +26,21 @@ impl Request { Ok(Self { request }) } + /// Get the name of the chip this request was made on. + pub fn chip_name(&self) -> Result<&str> { + // SAFETY: The `gpiod_line_request` is guaranteed to be live as long + // as `&self` + let name = unsafe { gpiod::gpiod_line_request_get_chip_name(self.request) }; + + // SAFETY: The string is guaranteed to be valid, non-null and immutable + // by the C API for the lifetime of the `gpiod_line_request`. The + // `gpiod_line_request` is living as long as `&self`. The string is + // returned read-only with a lifetime of `&self`. + unsafe { CStr::from_ptr(name) } + .to_str() + .map_err(Error::StringNotUtf8) + } + /// Get the number of lines in the request. pub fn num_lines(&self) -> usize { // SAFETY: `gpiod_line_request` is guaranteed to be valid here. diff --git a/bindings/rust/libgpiod/tests/line_request.rs b/bindings/rust/libgpiod/tests/line_request.rs index d49874f..9af5226 100644 --- a/bindings/rust/libgpiod/tests/line_request.rs +++ b/bindings/rust/libgpiod/tests/line_request.rs @@ -59,6 +59,20 @@ mod line_request { mod verify { use super::*; + #[test] + fn chip_name() { + const GPIO: Offset = 2; + let mut config = TestConfig::new(NGPIO).unwrap(); + config.lconfig_add_settings(&[GPIO]); + config.request_lines().unwrap(); + + let arc = config.sim(); + let sim = arc.lock().unwrap(); + let chip_name = sim.chip_name().clone(); + + assert_eq!(config.request().chip_name().unwrap(), chip_name); + } + #[test] fn custom_consumer() { const GPIO: Offset = 2;