[v2,3/4] libcamera: media_device: Allow for a regex to match entity name
diff mbox series

Message ID 20250717124853.2317191-4-dan.scally@ideasonboard.com
State New
Headers show
Series
  • Use regular expressions for entity name matching
Related show

Commit Message

Dan Scally July 17, 2025, 12:48 p.m. UTC
Some kernel drivers give their entities names that will differ from
implementation to implementation; for example the drivers for the
Camera Receiver Unit and CSI-2 receiver in the RZ/V2H SoC give their
entities names that include their memory address, in the format
"csi-16000400.csi2". Passing that entity name to
MediaDevice::getEntityByName() is too inflexible given it would only
then work if that specific CSI-2 receiver were the one being used.

Add an overload for MediaDevice::getEntityByName() that accepts a
std::basic_regex instead of a string, and use std::regex_search()
instead of a direct string comparison to find a matching entity. This
allows us to search for entites using regex patterns like
"csi-[0-9a-f]{8}.csi2".

Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
---
Changes in v2:

	- Add an overload for ::getEntityByName() instead of
	replacing the existing functionality. The new overload
	takes a regex, but will return a nullptr in the event
	that the given regex matches multiple entities.

 include/libcamera/internal/media_device.h |  2 ++
 src/libcamera/media_device.cpp            | 25 +++++++++++++++++++++++
 2 files changed, 27 insertions(+)

Comments

Stefan Klug Aug. 18, 2025, 10:40 a.m. UTC | #1
Hi Dan,

Thank you for the patch.

Quoting Daniel Scally (2025-07-17 14:48:52)
> Some kernel drivers give their entities names that will differ from
> implementation to implementation; for example the drivers for the
> Camera Receiver Unit and CSI-2 receiver in the RZ/V2H SoC give their
> entities names that include their memory address, in the format
> "csi-16000400.csi2". Passing that entity name to
> MediaDevice::getEntityByName() is too inflexible given it would only
> then work if that specific CSI-2 receiver were the one being used.
> 
> Add an overload for MediaDevice::getEntityByName() that accepts a
> std::basic_regex instead of a string, and use std::regex_search()
> instead of a direct string comparison to find a matching entity. This
> allows us to search for entites using regex patterns like
> "csi-[0-9a-f]{8}.csi2".
> 
> Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
> ---
> Changes in v2:
> 
>         - Add an overload for ::getEntityByName() instead of
>         replacing the existing functionality. The new overload
>         takes a regex, but will return a nullptr in the event
>         that the given regex matches multiple entities.
> 
>  include/libcamera/internal/media_device.h |  2 ++
>  src/libcamera/media_device.cpp            | 25 +++++++++++++++++++++++
>  2 files changed, 27 insertions(+)
> 
> diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h
> index b3a48b98..3f28793f 100644
> --- a/include/libcamera/internal/media_device.h
> +++ b/include/libcamera/internal/media_device.h
> @@ -14,6 +14,7 @@
>  #include <linux/media.h>
>  
>  #include <libcamera/base/log.h>
> +#include <libcamera/base/regex.h>
>  #include <libcamera/base/signal.h>
>  #include <libcamera/base/unique_fd.h>
>  
> @@ -45,6 +46,7 @@ public:
>  
>         const std::vector<MediaEntity *> &entities() const { return entities_; }
>         MediaEntity *getEntityByName(const std::string &name) const;
> +       MediaEntity *getEntityByName(const std::regex &name) const;
>  
>         MediaLink *link(const std::string &sourceName, unsigned int sourceIdx,
>                         const std::string &sinkName, unsigned int sinkIdx);
> diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp
> index 353f34a8..a0c0df86 100644
> --- a/src/libcamera/media_device.cpp
> +++ b/src/libcamera/media_device.cpp
> @@ -341,6 +341,31 @@ MediaEntity *MediaDevice::getEntityByName(const std::string &name) const
>         return nullptr;
>  }
>  
> +/**
> + * \brief Return the MediaEntity with name matching the regex \a name
> + * \param[in] name A regex to match the entity name
> + * \return The entity with name matching the regex \a name, or nullptr if no
> + * such entity is found
> + */
> +MediaEntity *MediaDevice::getEntityByName(const std::regex &name) const
> +{
> +       MediaEntity *entity = nullptr;
> +
> +       for (MediaEntity *e : entities_) {
> +               if (std::regex_search(e->name(), name)) {

I far as my planning goes I'll need a
MediaEntinty::matchesName(std::regex) function. But that can also be
added on top. So

Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>

Regards,
Stefan

> +                       if (entity) {
> +                               LOG(MediaDevice, Error)
> +                                       << "Multiple entities match given regex";
> +                               return nullptr;
> +                       }
> +
> +                       entity = e;
> +               }
> +       }
> +
> +       return entity;
> +}
> +
>  /**
>   * \brief Retrieve the MediaLink connecting two pads, identified by entity
>   * names and pad indexes
> -- 
> 2.34.1
>

Patch
diff mbox series

diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h
index b3a48b98..3f28793f 100644
--- a/include/libcamera/internal/media_device.h
+++ b/include/libcamera/internal/media_device.h
@@ -14,6 +14,7 @@ 
 #include <linux/media.h>
 
 #include <libcamera/base/log.h>
+#include <libcamera/base/regex.h>
 #include <libcamera/base/signal.h>
 #include <libcamera/base/unique_fd.h>
 
@@ -45,6 +46,7 @@  public:
 
 	const std::vector<MediaEntity *> &entities() const { return entities_; }
 	MediaEntity *getEntityByName(const std::string &name) const;
+	MediaEntity *getEntityByName(const std::regex &name) const;
 
 	MediaLink *link(const std::string &sourceName, unsigned int sourceIdx,
 			const std::string &sinkName, unsigned int sinkIdx);
diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp
index 353f34a8..a0c0df86 100644
--- a/src/libcamera/media_device.cpp
+++ b/src/libcamera/media_device.cpp
@@ -341,6 +341,31 @@  MediaEntity *MediaDevice::getEntityByName(const std::string &name) const
 	return nullptr;
 }
 
+/**
+ * \brief Return the MediaEntity with name matching the regex \a name
+ * \param[in] name A regex to match the entity name
+ * \return The entity with name matching the regex \a name, or nullptr if no
+ * such entity is found
+ */
+MediaEntity *MediaDevice::getEntityByName(const std::regex &name) const
+{
+	MediaEntity *entity = nullptr;
+
+	for (MediaEntity *e : entities_) {
+		if (std::regex_search(e->name(), name)) {
+			if (entity) {
+				LOG(MediaDevice, Error)
+					<< "Multiple entities match given regex";
+				return nullptr;
+			}
+
+			entity = e;
+		}
+	}
+
+	return entity;
+}
+
 /**
  * \brief Retrieve the MediaLink connecting two pads, identified by entity
  * names and pad indexes