[libcamera-devel,RFC,4/9] libcamera: Introduce CameraDevice and USBDevice
diff mbox series

Message ID 20230808125228.29043-5-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 CameraDevice base class from which the MediaDevice and
USBDevice derive from.

This allows to generalize the PipelineHandler base class implementation
to use a generic 'CameraDevice' type in the later patches.

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 include/libcamera/internal/camera_device.h | 31 +++++++++++++
 include/libcamera/internal/media_device.h  | 13 +++---
 include/libcamera/internal/meson.build     |  2 +
 include/libcamera/internal/usb_device.h    | 52 ++++++++++++++++++++++
 src/libcamera/camera_device.cpp            | 22 +++++++++
 src/libcamera/media_device.cpp             |  2 +-
 src/libcamera/meson.build                  |  1 +
 7 files changed, 115 insertions(+), 8 deletions(-)
 create mode 100644 include/libcamera/internal/camera_device.h
 create mode 100644 include/libcamera/internal/usb_device.h
 create mode 100644 src/libcamera/camera_device.cpp

Patch
diff mbox series

diff --git a/include/libcamera/internal/camera_device.h b/include/libcamera/internal/camera_device.h
new file mode 100644
index 000000000000..421d77e8d6cf
--- /dev/null
+++ b/include/libcamera/internal/camera_device.h
@@ -0,0 +1,31 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2023, Ideas On Board Oy
+ *
+ * camera_device.h - Base class for camera devices
+ */
+
+#pragma once
+
+namespace libcamera {
+
+class CameraDevice
+{
+public:
+	CameraDevice()
+		: acquired_(false)
+	{
+	}
+
+	virtual bool acquire() = 0;
+	virtual void release() = 0;
+	bool busy() const { return acquired_; }
+
+	virtual bool lock() = 0;
+	virtual void unlock() = 0;
+
+protected:
+	bool acquired_;
+};
+
+} /* namespace libcamera */
diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h
index eb8cfde48e20..2c748ae66545 100644
--- a/include/libcamera/internal/media_device.h
+++ b/include/libcamera/internal/media_device.h
@@ -18,22 +18,22 @@ 
 #include <libcamera/base/signal.h>
 #include <libcamera/base/unique_fd.h>
 
+#include "libcamera/internal/camera_device.h"
 #include "libcamera/internal/media_object.h"
 
 namespace libcamera {
 
-class MediaDevice : protected Loggable
+class MediaDevice : protected Loggable, public CameraDevice
 {
 public:
 	MediaDevice(const std::string &deviceNode);
 	~MediaDevice();
 
-	bool acquire();
-	void release();
-	bool busy() const { return acquired_; }
+	bool acquire() override;
+	void release() override;
 
-	bool lock();
-	void unlock();
+	bool lock() override;
+	void unlock() override;
 
 	int populate();
 	bool isValid() const { return valid_; }
@@ -85,7 +85,6 @@  private:
 
 	UniqueFD fd_;
 	bool valid_;
-	bool acquired_;
 
 	std::map<unsigned int, MediaObject *> objects_;
 	std::vector<MediaEntity *> entities_;
diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build
index 169e06557106..c683283da37d 100644
--- a/include/libcamera/internal/meson.build
+++ b/include/libcamera/internal/meson.build
@@ -14,6 +14,7 @@  libcamera_internal_headers = files([
     'byte_stream_buffer.h',
     'camera.h',
     'camera_controls.h',
+    'camera_device.h',
     'camera_lens.h',
     'camera_manager.h',
     'camera_sensor.h',
@@ -41,6 +42,7 @@  libcamera_internal_headers = files([
     'request.h',
     'source_paths.h',
     'sysfs.h',
+    'usb_device.h',
     'v4l2_device.h',
     'v4l2_pixelformat.h',
     'v4l2_subdevice.h',
diff --git a/include/libcamera/internal/usb_device.h b/include/libcamera/internal/usb_device.h
new file mode 100644
index 000000000000..7fe0f7c4d6a9
--- /dev/null
+++ b/include/libcamera/internal/usb_device.h
@@ -0,0 +1,52 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2023, Ideas On Board Oy
+ *
+ * usb_device.h - Description of a USB device
+ */
+
+#pragma once
+
+#include "libcamera/internal/camera_device.h"
+
+namespace libcamera {
+
+class USBDevice : public CameraDevice
+{
+public:
+	USBDevice(const char *vid, const char *pid)
+		: CameraDevice(), vid_(vid), pid_(pid)
+	{
+	}
+
+	/* \todo Implement acquire/release and lock/unlock */
+	bool acquire() override
+	{
+		/* This only works within the same process!! */
+		if (acquired_)
+			return false;
+
+		acquired_ = true;
+		return true;
+	}
+	void release() override
+	{
+	}
+
+	bool lock() override
+	{
+		return true;
+	}
+	void unlock() override
+	{
+	}
+
+	const std::string &vid() const { return vid_; };
+	const std::string &pid() const { return pid_; };
+
+private:
+	std::string vid_;
+	std::string pid_;
+};
+
+} /* namespace libcamera */
diff --git a/src/libcamera/camera_device.cpp b/src/libcamera/camera_device.cpp
new file mode 100644
index 000000000000..a0ceba0db5af
--- /dev/null
+++ b/src/libcamera/camera_device.cpp
@@ -0,0 +1,22 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2023, Ideas On Board Oy
+ *
+ * camera_device.cpp - Base class for camera devices
+ */
+
+#include "libcamera/internal/camera_device.h"
+
+/**
+ * \file camera_device.h
+ * \brief Base class for camera devices
+ */
+
+namespace libcamera {
+
+/**
+ * \class CameraDevice
+ * \brief Base class for camera devices
+ */
+
+} /* namespace libcamera */
diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp
index 2949816b4a64..f9c2dbf289f1 100644
--- a/src/libcamera/media_device.cpp
+++ b/src/libcamera/media_device.cpp
@@ -63,7 +63,7 @@  LOG_DEFINE_CATEGORY(MediaDevice)
  * populate() before the media graph can be queried.
  */
 MediaDevice::MediaDevice(const std::string &deviceNode)
-	: deviceNode_(deviceNode), valid_(false), acquired_(false)
+	: CameraDevice(), deviceNode_(deviceNode), valid_(false)
 {
 }
 
diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
index d5562afc50c7..3a70946a6f78 100644
--- a/src/libcamera/meson.build
+++ b/src/libcamera/meson.build
@@ -5,6 +5,7 @@  libcamera_sources = files([
     'byte_stream_buffer.cpp',
     'camera.cpp',
     'camera_controls.cpp',
+    'camera_device.cpp',
     'camera_lens.cpp',
     'camera_manager.cpp',
     'camera_sensor.cpp',