[libcamera-devel,RFC,9/9,HACK] libcamera: pipeline: Add usb-test pipeline
diff mbox series

Message ID 20230808125228.29043-10-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
Add a test pipeline to match on a USBDevice. This is a proof of concept
as the pipeline handler matches on my mouse VID/PID pair.

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 meson_options.txt                            |  1 +
 src/libcamera/pipeline/usb-test/meson.build  |  5 ++
 src/libcamera/pipeline/usb-test/usb_test.cpp | 93 ++++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100644 src/libcamera/pipeline/usb-test/meson.build
 create mode 100644 src/libcamera/pipeline/usb-test/usb_test.cpp

Patch
diff mbox series

diff --git a/meson_options.txt b/meson_options.txt
index fad928af4b62..e706484be109 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -46,6 +46,7 @@  option('pipelines',
             'rkisp1',
             'rpi/vc4',
             'simple',
+            'usb-test',
             'uvcvideo',
             'vimc'
         ],
diff --git a/src/libcamera/pipeline/usb-test/meson.build b/src/libcamera/pipeline/usb-test/meson.build
new file mode 100644
index 000000000000..bafff18e7b0b
--- /dev/null
+++ b/src/libcamera/pipeline/usb-test/meson.build
@@ -0,0 +1,5 @@ 
+# SPDX-License-Identifier: CC0-1.0
+
+libcamera_sources += files([
+    'usb_test.cpp',
+])
diff --git a/src/libcamera/pipeline/usb-test/usb_test.cpp b/src/libcamera/pipeline/usb-test/usb_test.cpp
new file mode 100644
index 000000000000..5df1a137f81f
--- /dev/null
+++ b/src/libcamera/pipeline/usb-test/usb_test.cpp
@@ -0,0 +1,93 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2023, Ideas On Board Oy
+ *
+ * usb_test.cpp - Test USB device matching
+ */
+
+#include <libcamera/base/log.h>
+
+#include <libcamera/camera.h>
+#include <libcamera/stream.h>
+
+#include "libcamera/internal/camera.h"
+#include "libcamera/internal/device_enumerator.h"
+#include "libcamera/internal/pipeline_handler.h"
+#include "libcamera/internal/usb_device.h"
+
+namespace libcamera {
+
+LOG_DEFINE_CATEGORY(USBTest)
+
+class USBCameraData : public Camera::Private
+{
+public:
+	USBCameraData(PipelineHandler *pipe)
+		: Camera::Private(pipe)
+	{
+	}
+
+	Stream stream_;
+};
+
+class PipelineHandlerUSB : public PipelineHandler
+{
+public:
+	PipelineHandlerUSB(CameraManager *manager)
+		: PipelineHandler(manager)
+	{
+	}
+
+	std::unique_ptr<CameraConfiguration> generateConfiguration([[maybe_unused]] Camera *camera,
+								   [[maybe_unused]] Span<const StreamRole> roles) override
+	{
+		return {};
+	}
+	int configure([[maybe_unused]] Camera *camera, [[maybe_unused]] CameraConfiguration *config) override
+	{
+		return 0;
+	}
+
+	int exportFrameBuffers([[maybe_unused]] Camera *camera, [[maybe_unused]] Stream *stream,
+			       [[maybe_unused]] std::vector<std::unique_ptr<FrameBuffer>> *buffers) override
+	{
+		return 0;
+	}
+
+	int start([[maybe_unused]] Camera *camera, [[maybe_unused]] const ControlList *controls) override
+	{
+		return 0;
+	}
+	void stopDevice([[maybe_unused]] Camera *camera) override
+	{
+	}
+
+	int queueRequestDevice([[maybe_unused]] Camera *camera, [[maybe_unused]] Request *request) override
+	{
+		return 0;
+	}
+
+	bool match(DeviceEnumerator *enumerator) override;
+};
+
+bool PipelineHandlerUSB::match(DeviceEnumerator *enumerator)
+{
+	USBDeviceMatch dm("046d", "c52b");
+	USBDevice *usbDev = acquireUSBDevice(enumerator, dm);
+	if (!usbDev)
+		return false;
+
+	std::unique_ptr<USBCameraData> data = std::make_unique<USBCameraData>(this);
+	std::string id = usbDev->vid() + "/" + usbDev->pid();
+	std::set<Stream *> streams{ &data->stream_ };
+
+	std::shared_ptr<Camera> camera =
+		Camera::create(std::move(data), id, streams);
+	registerCamera(std::move(camera));
+
+	return true;
+}
+
+REGISTER_PIPELINE_HANDLER(PipelineHandlerUSB)
+
+} /* namespace libcamera */