summaryrefslogtreecommitdiff
path: root/test/v4l2_subdevice/v4l2_subdevice_test.h
blob: 00c6399d3b8a6aed6a4f3fee7fe79f6281c7679f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * v4l2_subdevice_test.h - VIMC-based V4L2 subdevice test
 */

#ifndef __LIBCAMERA_V4L2_SUBDEVICE_TEST_H__
#define __LIBCAMERA_V4L2_SUBDEVICE_TEST_H__

#include <libcamera/buffer.h>

#include "libcamera/internal/device_enumerator.h"
#include "libcamera/internal/media_device.h"
#include "libcamera/internal/v4l2_subdevice.h"

#include "test.h"

using namespace libcamera;

class V4L2SubdeviceTest : public Test
{
public:
	V4L2SubdeviceTest()
		: scaler_(nullptr)
	{
	}

protected:
	int init() override;
	void cleanup() override;

	std::unique_ptr<DeviceEnumerator> enumerator_;
	std::shared_ptr<MediaDevice> media_;
	V4L2Subdevice *scaler_;
};

#endif /* __LIBCAMERA_V4L2_SUBDEVICE_TEST_H__ */
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2020, Umang Jain <email@uajain.com>
 *
 * hotplug-cameras.cpp - Test cameraAdded/cameraRemoved signals in CameraManager
 */

#include <dirent.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <unistd.h>

#include <libcamera/camera.h>
#include <libcamera/camera_manager.h>

#include <libcamera/base/event_dispatcher.h>
#include <libcamera/base/file.h>
#include <libcamera/base/thread.h>
#include <libcamera/base/timer.h>

#include "test.h"

using namespace libcamera;

class HotplugTest : public Test
{
protected:
	void cameraAddedHandler([[maybe_unused]] std::shared_ptr<Camera> cam)
	{
		cameraAdded_ = true;
	}

	void cameraRemovedHandler([[maybe_unused]] std::shared_ptr<Camera> cam)
	{
		cameraRemoved_ = true;
	}

	int init()
	{
		if (!File::exists("/sys/module/uvcvideo")) {
			std::cout << "uvcvideo driver is not loaded, skipping" << std::endl;
			return TestSkip;
		}

		if (geteuid() != 0) {
			std::cout << "This test requires root permissions, skipping" << std::endl;
			return TestSkip;
		}

		cm_ = new CameraManager();
		if (cm_->start()) {
			std::cout << "Failed to start camera manager" << std::endl;
			return TestFail;
		}

		cameraAdded_ = false;
		cameraRemoved_ = false;

		cm_->cameraAdded.connect(this, &HotplugTest::cameraAddedHandler);
		cm_->cameraRemoved.connect(this, &HotplugTest::cameraRemovedHandler);

		return 0;
	}

	int run()
	{
		DIR *dir;
		struct dirent *dirent;
		std::string uvcDeviceDir;

		dir = opendir(uvcDriverDir_.c_str());
		/* Find a UVC device directory, which we can bind/unbind. */
		while ((dirent = readdir(dir)) != nullptr) {
			if (!File::exists(uvcDriverDir_ + dirent->d_name + "/video4linux"))
				continue;

			uvcDeviceDir = dirent->d_name;
			break;
		}
		closedir(dir);

		/* If no UVC device found, skip the test. */
		if (uvcDeviceDir.empty())
			return TestSkip;

		/* Unbind a camera and process events. */
		std::ofstream(uvcDriverDir_ + "unbind", std::ios::binary)
			<< uvcDeviceDir;
		Timer timer;
		timer.start(1000);
		while (timer.isRunning() && !cameraRemoved_)
			Thread::current()->eventDispatcher()->processEvents();
		if (!cameraRemoved_) {
			std::cout << "Camera unplug not detected" << std::endl;
			return TestFail;
		}

		/* Bind the camera again and process events. */
		std::ofstream(uvcDriverDir_ + "bind", std::ios::binary)
			<< uvcDeviceDir;
		timer.start(1000);
		while (timer.isRunning() && !cameraAdded_)
			Thread::current()->eventDispatcher()->processEvents();
		if (!cameraAdded_) {
			std::cout << "Camera plug not detected" << std::endl;
			return TestFail;
		}

		return TestPass;
	}

	void cleanup()
	{
		cm_->stop();
		delete cm_;
	}

private:
	CameraManager *cm_;
	static const std::string uvcDriverDir_;
	bool cameraRemoved_;
	bool cameraAdded_;
};

const std::string HotplugTest::uvcDriverDir_ = "/sys/bus/usb/drivers/uvcvideo/";

TEST_REGISTER(HotplugTest)