libcamera: media_device: Get entity by regexp

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(P) 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>
Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
This commit is contained in:
Daniel Scally
2025-06-25 16:02:28 +01:00
committed by Jacopo Mondi
parent 8d068b7470
commit 1e92c4cc0d
2 changed files with 28 additions and 0 deletions

View File

@@ -15,6 +15,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>
@@ -47,6 +48,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);

View File

@@ -342,6 +342,32 @@ 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 matching the regex \a name, or nullptr if no such entity
* is found or multiple entities match on \a name
*/
MediaEntity *MediaDevice::getEntityByName(const std::regex &name) const
{
MediaEntity *entity = nullptr;
for (MediaEntity *e : entities_) {
if (!std::regex_search(e->name(), name))
continue;
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