From patchwork Thu Sep 11 09:29:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24310 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id E0600C324E for ; Thu, 11 Sep 2025 09:30:11 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9317B69376; Thu, 11 Sep 2025 11:30:11 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="O7muSoGA"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1B0616935A for ; Thu, 11 Sep 2025 11:30:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757583009; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Bw52UuO45FOFbwiN3BUPbA2UWTY2YDEcEO8Nrj9Nmi4=; b=O7muSoGApASjhCtnVxJvWrI+splWzAcBmA+FvfQs1XFZ+3GfkjShb/57W7sZceEeF0jr+i uKkT2LpG9uLtYwiZhxL2DthqtIQIkmg5IcqPO5ND3wWALvgg6HFNJrCP/2LlvYM0uVQwkK /jgQM393q3LqfyilsSbaz9HiFqt2a3Y= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-223-VYKxYmA5Ni2maVBAwqA23Q-1; Thu, 11 Sep 2025 05:30:03 -0400 X-MC-Unique: VYKxYmA5Ni2maVBAwqA23Q-1 X-Mimecast-MFC-AGG-ID: VYKxYmA5Ni2maVBAwqA23Q_1757583002 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A19BA1977538; Thu, 11 Sep 2025 09:30:02 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.44.34.98]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 44C8119560B1; Thu, 11 Sep 2025 09:29:59 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , Kieran Bingham , =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= , Laurent Pinchart , Paul Elder Subject: [PATCH v17 03/12] config: Add configuration retrieval helpers Date: Thu, 11 Sep 2025 11:29:33 +0200 Message-ID: <20250911092945.16517-4-mzamazal@redhat.com> In-Reply-To: <20250911092945.16517-1-mzamazal@redhat.com> References: <20250911092945.16517-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 82JVeA8pAu4Kd0pUHMrke0WZoU-2ZBDkZaheAqjZnRs_1757583002 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Let's add some helpers to make accessing simple configuration values simpler. The helpers are used in the followup patches. GlobalConfiguration::option ensures that no value is returned rather than a value of YamlObject::empty. Signed-off-by: Milan Zamazal Reviewed-by: Paul Elder --- .../libcamera/internal/global_configuration.h | 28 ++++++ src/libcamera/global_configuration.cpp | 99 +++++++++++++++++++ 2 files changed, 127 insertions(+) diff --git a/include/libcamera/internal/global_configuration.h b/include/libcamera/internal/global_configuration.h index f695498c4..8d09517ed 100644 --- a/include/libcamera/internal/global_configuration.h +++ b/include/libcamera/internal/global_configuration.h @@ -8,6 +8,11 @@ #pragma once #include +#include +#include +#include + +#include #include "libcamera/internal/yaml_parser.h" @@ -23,6 +28,29 @@ public: unsigned int version() const; Configuration configuration() const; + template + std::optional option( + const std::initializer_list confPath) const + { + const YamlObject *c = &configuration(); + for (auto part : confPath) { + c = &(*c)[part]; + if (!*c) + return {}; + } + return c->get(); + } + + std::optional> listOption( + const std::initializer_list confPath) const; + std::optional envOption( + const char *const envVariable, + const std::initializer_list confPath) const; + std::optional> envListOption( + const char *const envVariable, + const std::initializer_list confPath, + const std::string delimiter = ":") const; + private: bool loadFile(const std::filesystem::path &fileName); void load(); diff --git a/src/libcamera/global_configuration.cpp b/src/libcamera/global_configuration.cpp index d02668111..592edcf30 100644 --- a/src/libcamera/global_configuration.cpp +++ b/src/libcamera/global_configuration.cpp @@ -8,8 +8,12 @@ #include "libcamera/internal/global_configuration.h" #include +#include +#include +#include #include #include +#include #include #include @@ -43,6 +47,12 @@ LOG_DEFINE_CATEGORY(Configuration) * If the first found configuration file cannot be opened or parsed, an error is * reported and no configuration file is used. This is to prevent libcamera from * using an unintended configuration file. + * + * The configuration can be accessed using the provided helpers. Namely + * GlobalConfiguration::option(), GlobalConfiguration::envOption(), + * GlobalConfiguration::listOption(), and GlobalConfiguration::envListOption() + * to access individual options, or GlobalConfiguration::configuration() to + * access the whole configuration. */ bool GlobalConfiguration::loadFile(const std::filesystem::path &fileName) @@ -110,6 +120,95 @@ GlobalConfiguration::GlobalConfiguration() * the underlying type. */ +/** + * \fn std::optional GlobalConfiguration::option(const std::initializer_list &confPath) const + * \brief Return value of the configuration option identified by \a confPath + * \param[in] confPath Sequence of the YAML section names (excluding + * `configuration') leading to the requested option + * \return The value of the configuration item corresponding to \a confPath if + * it exists in the configuration file, or no value otherwise + */ + +/** + * \brief Return values of the configuration option identified by \a confPath + * \tparam T The type of the retrieved configuration value + * \param[in] confPath Sequence of the YAML section names (excluding + * `configuration') leading to the requested list option, separated by dots + * \return A vector of strings or no value if not found + */ +std::optional> GlobalConfiguration::listOption( + const std::initializer_list confPath) const +{ + const YamlObject *c = &configuration(); + for (auto part : confPath) { + c = &(*c)[part]; + if (!*c) + return {}; + } + return c->getList(); +} + +/** + * \brief Return value of environment variable with a fallback on the configuration file + * \param[in] envVariable Environment variable to get the value from + * \param[in] confPath The sequence of YAML section names to fall back on when + * \a envVariable is unavailable + * + * This helper looks first at the given environment variable and if it is + * defined then it returns its value (even if it is empty). Otherwise it looks + * for \a confPath the same way as in GlobalConfiguration::option. Only string + * values are supported. + * + * \note Support for using environment variables to configure libcamera behavior + * is provided here mostly for backward compatibility reasons. Introducing new + * configuration environment variables is discouraged. + * + * \return The value retrieved from the given environment if it is set, + * otherwise the value from the configuration file if it exists, or no value if + * it does not + */ +std::optional GlobalConfiguration::envOption( + const char *envVariable, + const std::initializer_list confPath) const +{ + const char *envValue = utils::secure_getenv(envVariable); + if (envValue) + return std::optional{ std::string{ envValue } }; + return option(confPath); +} + +/** + * \brief Return values of the configuration option from a file or environment + * \param[in] envVariable Environment variable to get the value from + * \param[in] confPath The same as in GlobalConfiguration::option + * \param[in] delimiter Items separator in the environment variable + * + * This helper looks first at the given environment variable and if it is + * defined (even if it is empty) then it splits its value by semicolons and + * returns the resulting list of strings. Otherwise it looks for \a confPath the + * same way as in GlobalConfiguration::option, value of which must be a list of + * strings. + * + * \note Support for using environment variables to configure libcamera behavior + * is provided here mostly for backward compatibility reasons. Introducing new + * configuration environment variables is discouraged. + * + * \return A vector of strings retrieved from the given environment option or + * configuration file or no value if not found; the vector may be empty + */ +std::optional> GlobalConfiguration::envListOption( + const char *const envVariable, + const std::initializer_list confPath, + const std::string delimiter) const +{ + const char *envValue = utils::secure_getenv(envVariable); + if (envValue) { + auto items = utils::split(envValue, delimiter); + return std::vector(items.begin(), items.end()); + } + return listOption(confPath); +} + /** * \brief Return configuration version *