/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2019, Google Inc. * * event.cpp - Event test */ #include #include #include #include #include #include #include "libcamera/internal/thread.h" #include "test.h" using namespace std; using namespace libcamera; class EventTest : public Test { protected: void readReady(EventNotifier *notifier) { size_ = read(notifier->fd(), data_, sizeof(data_)); notified_ = true; } int init() { return pipe(pipefd_); } int run() { EventDispatcher *dispatcher = Thread::current()->eventDispatcher(); std::string data("H2G2"); Timer timeout; ssize_t ret; EventNotifier readNotifier(pipefd_[0], EventNotifier::Read); readNotifier.activated.connect(this, &EventTest::readReady); /* Test read notification with data. */ memset(data_, 0, sizeof(data_)); size_ = 0; ret = write(pipefd_[1], data.data(), data.size()); if (ret < 0) { cout << "Pipe write failed" << endl; return TestFail; } timeout.start(100); dispatcher->processEvents(); timeout.stop(); if (static_cast(size_) != data.size()) { cout << "Event notifier read ready test failed" << endl; return TestFail; } /* Test read notification without data. */ notified_ = false; timeout.start(100); dispatcher->processEvents(); timeout.stop(); if (notified_) { cout << "Event notifier read no ready test failed" << endl; return TestFail; } /* Test read notifier disabling. */ notified_ = false; readNotifier.setEnabled(false); ret = write(pipefd_[1], data.data(), data.size()); if (ret < 0) { cout << "Pipe write failed" << endl; return TestFail; } timeout.start(100); dispatcher->processEvents(); timeout.stop(); if (notified_) { cout << "Event notifier read disabling failed" << endl; return TestFail; } /* Test read notifier enabling. */ notified_ = false; readNotifier.setEnabled(true); timeout.start(100); dispatcher->processEvents(); timeout.stop(); if (!notified_) { cout << "Event notifier read enabling test failed" << endl; return TestFail; } return TestPass; } void cleanup() { close(pipefd_[0]); close(pipefd_[1]); } private: int pipefd_[2]; bool notified_; char data_[16]; ssize_t size_; }; TEST_REGISTER(EventTest) d>
blob: 8c7fd1d2d4445db320488b01df67e34bb6baf234 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * camera-sensor.cpp - Camera sensor tests
 */

#include <algorithm>
#include <iostream>

#include <linux/media-bus-format.h>

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

#include "test.h"

using namespace std;
using namespace libcamera;

class CameraSensorTest : public Test
{
protected:
	int init()
	{
		enumerator_ = DeviceEnumerator::create();
		if (!enumerator_) {
			cerr << "Failed to create device enumerator" << endl;
			return TestFail;
		}

		if (enumerator_->enumerate()) {
			cerr << "Failed to enumerate media devices" << endl;
			return TestFail;
		}

		DeviceMatch dm("vimc");
		media_ = enumerator_->search(dm);
		if (!media_) {
			cerr << "Unable to find \'vimc\' media device node" << endl;
			return TestSkip;
		}

		MediaEntity *entity = media_->getEntityByName("Sensor A");
		if (!entity) {
			cerr << "Unable to find media entity 'Sensor A'" << endl;
			return TestFail;
		}

		sensor_ = new CameraSensor(entity);
		if (sensor_->init() < 0) {
			cerr << "Unable to initialise camera sensor" << endl;
			return TestFail;
		}

		return TestPass;
	}

	int run()
	{
		if (sensor_->model() != "Sensor A") {
			cerr << "Incorrect sensor model '" << sensor_->model()
			     << "'" << endl;
			return TestFail;
		}

		const std::vector<unsigned int> &codes = sensor_->mbusCodes();
		auto iter = std::find(codes.begin(), codes.end(),
				      MEDIA_BUS_FMT_ARGB8888_1X32);
		if (iter == codes.end()) {
			cerr << "Sensor doesn't support ARGB8888_1X32" << endl;
			return TestFail;
		}

		const std::vector<Size> &sizes = sensor_->sizes();
		auto iter2 = std::find(sizes.begin(), sizes.end(),
				       Size(4096, 2160));
		if (iter2 == sizes.end()) {
			cerr << "Sensor doesn't support 4096x2160" << endl;
			return TestFail;
		}

		const Size &resolution = sensor_->resolution();
		if (resolution != Size(4096, 2160)) {
			cerr << "Incorrect sensor resolution "
			     << resolution.toString() << endl;
			return TestFail;
		}

		/* Use an invalid format and make sure it's not selected. */
		V4L2SubdeviceFormat format = sensor_->getFormat({ 0xdeadbeef,
								  MEDIA_BUS_FMT_SBGGR10_1X10,
								  MEDIA_BUS_FMT_BGR888_1X24 },
								Size(1024, 768));
		if (format.mbus_code != MEDIA_BUS_FMT_SBGGR10_1X10 ||
		    format.size != Size(4096, 2160)) {
			cerr << "Failed to get a suitable format, expected 4096x2160-0x"
			     << utils::hex(MEDIA_BUS_FMT_SBGGR10_1X10)
			     << ", got " << format.toString() << endl;
			return TestFail;
		}

		return TestPass;
	}

	void cleanup()
	{
		delete sensor_;
	}

private:
	std::unique_ptr<DeviceEnumerator> enumerator_;
	std::shared_ptr<MediaDevice> media_;
	CameraSensor *sensor_;
};

TEST_REGISTER(CameraSensorTest)