/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2019, Google Inc. * * threads.cpp - Threads test */ #include #include #include #include "thread.h" #include "test.h" using namespace std; using namespace libcamera; class InstrumentedThread : public Thread { public: InstrumentedThread(unsigned int iterations) : iterations_(iterations) { } protected: void run() { for (unsigned int i = 0; i < iterations_; ++i) { this_thread::sleep_for(chrono::milliseconds(50)); } } private: unsigned int iterations_; }; class ThreadTest : public Test { protected: int init() { return 0; } int run() { /* Test Thread() retrieval for the main thread. */ Thread *thread = Thread::current(); if (!thread) { cout << "Thread::current() failed to main thread" << endl; return TestFail; } if (!thread->isRunning()) { cout << "Main thread is not running" << endl; return TestFail; } /* Test starting the main thread, the test shall not crash. */ thread->start(); /* Test the running state of a custom thread. */ thread = new Thread(); thread->start(); if (!thread->isRunning()) { cout << "Thread is not running after being started" << endl; return TestFail; } thread->exit(0); thread->wait(); if (thread->isRunning()) { cout << "Thread is still running after finishing" << endl; return TestFail; } delete thread; return TestPass; } void cleanup() { } }; TEST_REGISTER(ThreadTest) libcamera/vivid.git/tree/src/lc-compliance/main.cpp?h=vivid-pre-a07968bed276&id=b2ddc9b11815976ec0bdf741fded993b8bc9ef4d'>treecommitdiff
blob: 54cee54aa9788a8812ce7a277627b7f16309867a (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * main.cpp - lc-compliance - The libcamera compliance tool
 */

#include <iomanip>
#include <iostream>
#include <string.h>

#include <libcamera/libcamera.h>

#include "../cam/options.h"
#include "tests.h"

using namespace libcamera;

enum {
	OptCamera = 'c',
	OptHelp = 'h',
};

class Harness
{
public:
	Harness(const OptionsParser::Options &options);
	~Harness();

	int exec();

private:
	int init();
	void listCameras();

	OptionsParser::Options options_;
	std::unique_ptr<CameraManager> cm_;
	std::shared_ptr<Camera> camera_;
};

Harness::Harness(const OptionsParser::Options &options)
	: options_(options)
{
	cm_ = std::make_unique<CameraManager>();
}

Harness::~Harness()
{
	if (camera_) {
		camera_->release();
		camera_.reset();
	}

	cm_->stop();
}

int Harness::exec()
{
	int ret = init();
	if (ret)
		return ret;

	std::vector<Results> results;

	results.push_back(testSingleStream(camera_));

	for (const Results &result : results) {
		ret = result.summary();
		if (ret)
			return ret;
	}

	return 0;
}

int Harness::init()
{
	int ret = cm_->start();
	if (ret) {
		std::cout << "Failed to start camera manager: "
			  << strerror(-ret) << std::endl;
		return ret;
	}

	if (!options_.isSet(OptCamera)) {
		std::cout << "No camera specified, available cameras:" << std::endl;
		listCameras();
		return -ENODEV;
	}

	const std::string &cameraId = options_[OptCamera];
	camera_ = cm_->get(cameraId);
	if (!camera_) {
		std::cout << "Camera " << cameraId << " not found, available cameras:" << std::endl;
		listCameras();
		return -ENODEV;
	}

	if (camera_->acquire()) {
		std::cout << "Failed to acquire camera" << std::endl;
		return -EINVAL;
	}

	std::cout << "Using camera " << cameraId << std::endl;

	return 0;
}

void Harness::listCameras()
{
	for (const std::shared_ptr<Camera> &cam : cm_->cameras())
		std::cout << "- " << cam.get()->id() << std::endl;
}

static int parseOptions(int argc, char **argv, OptionsParser::Options *options)
{
	OptionsParser parser;
	parser.addOption(OptCamera, OptionString,
			 "Specify which camera to operate on, by id", "camera",
			 ArgumentRequired, "camera");
	parser.addOption(OptHelp, OptionNone, "Display this help message",
			 "help");

	*options = parser.parse(argc, argv);
	if (!options->valid())
		return -EINVAL;

	if (options->isSet(OptHelp)) {
		parser.usage();
		return -EINTR;
	}

	return 0;
}

int main(int argc, char **argv)
{
	OptionsParser::Options options;
	int ret = parseOptions(argc, argv, &options);
	if (ret == -EINTR)
		return EXIT_SUCCESS;
	if (ret < 0)
		return EXIT_FAILURE;

	Harness harness(options);

	return harness.exec() ? EXIT_FAILURE : EXIT_SUCCESS;
}