From patchwork Thu Jul 17 12:48:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Scally X-Patchwork-Id: 23835 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 34A78C32C8 for ; Thu, 17 Jul 2025 12:49:09 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 249B268F7F; Thu, 17 Jul 2025 14:49:06 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="mQmdBjjz"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 79F3661517 for ; Thu, 17 Jul 2025 14:49:02 +0200 (CEST) Received: from mail.ideasonboard.com (cpc141996-chfd3-2-0-cust928.12-3.cable.virginm.net [86.13.91.161]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 640631FA4; Thu, 17 Jul 2025 14:48:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1752756508; bh=AVd9Ab55yqR0mQI/Pk2d8gA9rqaX4VV6i7I8u0PGQoM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mQmdBjjzwYDV45YAVoStsF8NymreqXv3Jd/tcUJjuGwvRsUqpP832yU+H5QJG8t9v 1ynrUe22ZDXMa+omt8mOXR6rAefpEB5HrJbJplih+4DNJ9EGa2Sn1dwisKxp/SYAfz i9OnAN+GqAamHWslhEMlYB7nhXYaTSDIEnWgfrCk= From: Daniel Scally To: libcamera-devel@lists.libcamera.org Cc: Daniel Scally Subject: [PATCH v2 2/4] libcamera: device_enumerator: Support regex to match entity names Date: Thu, 17 Jul 2025 13:48:51 +0100 Message-Id: <20250717124853.2317191-3-dan.scally@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250717124853.2317191-1-dan.scally@ideasonboard.com> References: <20250717124853.2317191-1-dan.scally@ideasonboard.com> MIME-Version: 1.0 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" Some entities in a media graph have names that might differ from implementation to implementation; for example the Camera Receiver Unit and CSI-2 receiver on the KakiP board have entities with names that include their address, in the form "csi-16000400.csi2". Passing that entity name to DeviceMatch is too inflexible given it would only work if that specific CSI-2 receiver were the one being used. Add an overload for DeviceMatch::add() such that users can pass in a std::regex instead of a string. Update DeviceMatch::match() to check for entities that are matched by the regular expressions added with the new overload after checking for any exact matches from the vector of strings. This allows us to use regex to match on patterns like "csi-[0-9a-f]{8}.csi2". Signed-off-by: Daniel Scally --- Changes in v2: - Instead of replacing the existing ::add() function and matching process with regex, add an overload for ::add() that takes a regex and incorporate that into ::match() alongside the existing functionality. .../libcamera/internal/device_enumerator.h | 4 +- src/libcamera/device_enumerator.cpp | 37 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h index db3532a9..4e55e172 100644 --- a/include/libcamera/internal/device_enumerator.h +++ b/include/libcamera/internal/device_enumerator.h @@ -11,6 +11,7 @@ #include #include +#include #include namespace libcamera { @@ -23,12 +24,13 @@ public: DeviceMatch(const std::string &driver); void add(const std::string &entity); - + void add(const std::regex &entity); bool match(const MediaDevice *device) const; private: std::string driver_; std::vector entities_; + std::vector entityRegexs_; }; class DeviceEnumerator diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp index ae17862f..191758ca 100644 --- a/src/libcamera/device_enumerator.cpp +++ b/src/libcamera/device_enumerator.cpp @@ -53,7 +53,8 @@ LOG_DEFINE_CATEGORY(DeviceEnumerator) * * A DeviceMatch is created with a specific Linux device driver in mind, * therefore the name of the driver is a required property. One or more Entity - * names can be added as match criteria. + * names (or regular expressions designed to match an entity name) can be added + * as match criteria. * * Pipeline handlers are recommended to add entities to DeviceMatch as * appropriate to ensure that the media device they need can be uniquely @@ -81,6 +82,15 @@ void DeviceMatch::add(const std::string &entity) entities_.push_back(entity); } +/** + * \brief Add a regex to match a media entity name to the search pattern + * \param[in] entity The regex intended to match to an entity in the media graph + */ +void DeviceMatch::add(const std::regex &entity) +{ + entityRegexs_.push_back(entity); +} + /** * \brief Compare a search pattern with a media device * \param[in] device The media device @@ -116,6 +126,31 @@ bool DeviceMatch::match(const MediaDevice *device) const return false; } + for (const std::regex &nameRegex : entityRegexs_) { + bool found = false; + + for (const MediaEntity *entity : device->entities()) { + if (std::regex_search(entity->name(), nameRegex)) { + if (found) { + LOG(DeviceEnumerator, Error) + << "Multiple entities match regex"; + return false; + } + + if (!entity->deviceNode().empty()) { + found = true; + } else { + LOG(DeviceEnumerator, Debug) + << "Skip " << entity->name() + << ": no device node"; + } + } + } + + if (!found) + return false; + } + return true; }