[libcamera-devel,RFC,6/9] libcamera: device_match: Introduce USBDeviceMatch
diff mbox series

Message ID 20230808125228.29043-7-jacopo.mondi@ideasonboard.com
State New
Headers show
Series
  • libcamera: Generalize device match and support USB devices
Related show

Commit Message

Jacopo Mondi Aug. 8, 2023, 12:52 p.m. UTC
Introduce a USB-specific DeviceMatch derived class.

Generalize the DeviceMatch::match() function by making its only
parameter a CameraDevice instance and dynamically cast to the correct
derived class in the overloaded match() functions.

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 include/libcamera/internal/device_match.h | 21 ++++++++++++++++---
 src/libcamera/device_match.cpp            | 25 ++++++++++++++++++++---
 2 files changed, 40 insertions(+), 6 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/device_match.h b/include/libcamera/internal/device_match.h
index 6df7dece1031..e6608cac2f85 100644
--- a/include/libcamera/internal/device_match.h
+++ b/include/libcamera/internal/device_match.h
@@ -12,12 +12,12 @@ 
 
 namespace libcamera {
 
-class MediaDevice;
+class CameraDevice;
 
 class DeviceMatch
 {
 public:
-	virtual bool match(const MediaDevice *device) const = 0;
+	virtual bool match(const CameraDevice *device) const = 0;
 };
 
 class MediaDeviceMatch : public DeviceMatch
@@ -26,11 +26,26 @@  public:
 	void add(const std::string &entity);
 	MediaDeviceMatch(const std::string &driver);
 
-	bool match(const MediaDevice *device) const override;
+	bool match(const CameraDevice *device) const override;
 
 private:
 	std::string driver_;
 	std::vector<std::string> entities_;
 };
 
+class USBDeviceMatch : public DeviceMatch
+{
+public:
+	USBDeviceMatch(const std::string &vid, const std::string &pid)
+		: vid_(vid), pid_(pid)
+	{
+	}
+
+	bool match(const CameraDevice *device) const override;
+
+private:
+	std::string vid_;
+	std::string pid_;
+};
+
 }; /* namespace libcamera */
diff --git a/src/libcamera/device_match.cpp b/src/libcamera/device_match.cpp
index 17937d435c88..8870649cb53b 100644
--- a/src/libcamera/device_match.cpp
+++ b/src/libcamera/device_match.cpp
@@ -7,7 +7,9 @@ 
 
 #include "libcamera/internal/device_match.h"
 
+#include "libcamera/internal/camera_device.h"
 #include "libcamera/internal/media_device.h"
+#include "libcamera/internal/usb_device.h"
 
 /**
  * \file device_match.h
@@ -84,15 +86,17 @@  void MediaDeviceMatch::add(const std::string &entity)
  *
  * \return True if the media device matches the search pattern, false otherwise
  */
-bool MediaDeviceMatch::match(const MediaDevice *device) const
+bool MediaDeviceMatch::match(const CameraDevice *device) const
 {
-	if (driver_ != device->driver())
+	const MediaDevice *media = static_cast<const MediaDevice *>(device);
+
+	if (driver_ != media->driver())
 		return false;
 
 	for (const std::string &name : entities_) {
 		bool found = false;
 
-		for (const MediaEntity *entity : device->entities()) {
+		for (const MediaEntity *entity : media->entities()) {
 			if (name == entity->name()) {
 				found = true;
 				break;
@@ -106,4 +110,19 @@  bool MediaDeviceMatch::match(const MediaDevice *device) const
 	return true;
 }
 
+/**
+ * \brief Compare a search pattern with a USB device
+ * \param[in] device The USB device
+ *
+ * Matching is performed on the USB device vendorId and productId.
+ *
+ * \return True if the USB device matches the search pattern, false otherwise
+ */
+bool USBDeviceMatch::match(const CameraDevice *device) const
+{
+	const USBDevice *usb = static_cast<const USBDevice *>(device);
+
+	return usb->vid() == vid_ && usb->pid() == pid_;
+}
+
 } /* namespace libcamera */