summaryrefslogtreecommitdiff
path: root/test/event-dispatcher.cpp
blob: e8818dcab4ad5ee827fa9fca8d24facd1e9d7430 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * event-dispatcher.cpp - Event dispatcher test
 */

#include <iostream>
#include <signal.h>
#include <sys/time.h>

#include <libcamera/camera_manager.h>
#include <libcamera/event_dispatcher.h>
#include <libcamera/timer.h>

#include "test.h"

using namespace std;
using namespace libcamera;

static EventDispatcher *dispatcher;
static bool interrupt;

class EventDispatcherTest : public Test
{
protected:
	static void sigAlarmHandler(int)
	{
		cout << "SIGALARM received" << endl;
		if (interrupt)
			dispatcher->interrupt();
	}

	int init()
	{
		dispatcher = CameraManager::instance()->eventDispatcher();

		struct sigaction sa = {};
		sa.sa_handler = &sigAlarmHandler;
		sigaction(SIGALRM, &sa, nullptr);

		return 0;
	}

	int run()
	{
		Timer timer;

		/* Event processing interruption by signal. */
		struct timespec start;
		clock_gettime(CLOCK_MONOTONIC, &start);

		timer.start(1000);

		struct itimerval itimer = {};
		itimer.it_value.tv_usec = 500000;
		interrupt = false;
		setitimer(ITIMER_REAL, &itimer, nullptr);

		dispatcher->processEvents();

		struct timespec stop;
		clock_gettime(CLOCK_MONOTONIC, &stop);
		int duration = (stop.tv_sec - start.tv_sec) * 1000;
		duration += (stop.tv_nsec - start.tv_nsec) / 1000000;

		if (abs(duration - 1000) > 50) {
			cout << "Event processing restart test failed" << endl;
			return TestFail;
		}

		/* Event processing interruption. */
		timer.start(1000);
		dispatcher->interrupt();

		dispatcher->processEvents();

		if (!timer.isRunning()) {
			cout << "Event processing immediate interruption failed" << endl;
			return TestFail;
		}

		timer.start(1000);
		itimer.it_value.tv_usec = 500000;
		interrupt = true;
		setitimer(ITIMER_REAL, &itimer, nullptr);

		dispatcher->processEvents();

		if (!timer.isRunning()) {
			cout << "Event processing delayed interruption failed" << endl;
			return TestFail;
		}

		return TestPass;
	}

	void cleanup()
	{
	}
};

TEST_REGISTER(EventDispatcherTest)
lt; "[ "; for (unsigned int j = 0; j < Cols; j++) { out << (*this)[i][j]; out << ((j + 1 < Cols) ? ", " : " "); } out << ((i + 1 < Rows) ? "], " : "]"); } out << " }"; return out.str(); } Span<const T, Cols> operator[](size_t i) const { return Span<const T, Cols>{ &data_.data()[i * Cols], Cols }; } Span<T, Cols> operator[](size_t i) { return Span<T, Cols>{ &data_.data()[i * Cols], Cols }; } #ifndef __DOXYGEN__ template<typename U, std::enable_if_t<std::is_arithmetic_v<U>>> #else template<typename U> #endif /* __DOXYGEN__ */ Matrix<T, Rows, Cols> &operator*=(U d) { for (unsigned int i = 0; i < Rows * Cols; i++) data_[i] *= d; return *this; } private: std::array<T, Rows * Cols> data_; }; #ifndef __DOXYGEN__ template<typename T, typename U, unsigned int Rows, unsigned int Cols, std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr> #else template<typename T, typename U, unsigned int Rows, unsigned int Cols> #endif /* __DOXYGEN__ */ Matrix<U, Rows, Cols> operator*(T d, const Matrix<U, Rows, Cols> &m) { Matrix<U, Rows, Cols> result; for (unsigned int i = 0; i < Rows; i++) { for (unsigned int j = 0; j < Cols; j++) result[i][j] = d * m[i][j]; } return result; } #ifndef __DOXYGEN__ template<typename T, typename U, unsigned int Rows, unsigned int Cols, std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr> #else template<typename T, typename U, unsigned int Rows, unsigned int Cols> #endif /* __DOXYGEN__ */ Matrix<U, Rows, Cols> operator*(const Matrix<U, Rows, Cols> &m, T d) { return d * m; } #ifndef __DOXYGEN__ template<typename T, unsigned int R1, unsigned int C1, unsigned int R2, unsigned int C2, std::enable_if_t<C1 == R2> * = nullptr> #else template<typename T, unsigned int R1, unsigned int C1, unsigned int R2, unsigned in C2> #endif /* __DOXYGEN__ */ Matrix<T, R1, C2> operator*(const Matrix<T, R1, C1> &m1, const Matrix<T, R2, C2> &m2) { Matrix<T, R1, C2> result; for (unsigned int i = 0; i < R1; i++) { for (unsigned int j = 0; j < C2; j++) { T sum = 0; for (unsigned int k = 0; k < C1; k++) sum += m1[i][k] * m2[k][j]; result[i][j] = sum; } } return result; } template<typename T, unsigned int Rows, unsigned int Cols> Matrix<T, Rows, Cols> operator+(const Matrix<T, Rows, Cols> &m1, const Matrix<T, Rows, Cols> &m2) { Matrix<T, Rows, Cols> result; for (unsigned int i = 0; i < Rows; i++) { for (unsigned int j = 0; j < Cols; j++) result[i][j] = m1[i][j] + m2[i][j]; } return result; } #ifndef __DOXYGEN__ bool matrixValidateYaml(const YamlObject &obj, unsigned int size); #endif /* __DOXYGEN__ */ } /* namespace ipa */ #ifndef __DOXYGEN__ template<typename T, unsigned int Rows, unsigned int Cols> std::ostream &operator<<(std::ostream &out, const ipa::Matrix<T, Rows, Cols> &m) { out << m.toString(); return out; } template<typename T, unsigned int Rows, unsigned int Cols> struct YamlObject::Getter<ipa::Matrix<T, Rows, Cols>> { std::optional<ipa::Matrix<T, Rows, Cols>> get(const YamlObject &obj) const { if (!ipa::matrixValidateYaml(obj, Rows * Cols)) return std::nullopt; ipa::Matrix<T, Rows, Cols> matrix; T *data = &matrix[0][0]; unsigned int i = 0; for (const YamlObject &entry : obj.asList()) { const auto value = entry.get<T>(); if (!value) return std::nullopt; data[i++] = *value; } return matrix; } }; #endif /* __DOXYGEN__ */ } /* namespace libcamera */