summaryrefslogtreecommitdiff
path: root/test/ipc/unixsocket_ipc.cpp
blob: 3ee6017e042a4262730d1823afe34791ce92f41d (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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2021, Raspberry Pi (Trading) Limited
 *
 * cam_helper_imx290.cpp - camera helper for imx290 sensor
 */

#include <math.h>

#include "cam_helper.hpp"

using namespace RPiController;

class CamHelperImx290 : public CamHelper
{
public:
	CamHelperImx290();
	uint32_t GainCode(double gain) const override;
	double Gain(uint32_t gain_code) const override;
	void GetDelays(int &exposure_delay, int &gain_delay,
		       int &vblank_delay) const override;
	unsigned int HideFramesModeSwitch() const override;

private:
	/*
	 * Smallest difference between the frame length and integration time,
	 * in units of lines.
	 */
	static constexpr int frameIntegrationDiff = 2;
};

CamHelperImx290::CamHelperImx290()
	: CamHelper({}, frameIntegrationDiff)
{
}

uint32_t CamHelperImx290::GainCode(double gain) const
{
	int code = 66.6667 * l/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * unixsocket_ipc.cpp - Unix socket IPC test
 */

#include <algorithm>
#include <fcntl.h>
#include <iostream>
#include <limits.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

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

#include "libcamera/internal/ipa_data_serializer.h"
#include "libcamera/internal/ipc_pipe.h"
#include "libcamera/internal/ipc_pipe_unixsocket.h"
#include "libcamera/internal/process.h"

#include "test.h"

using namespace std;
using namespace libcamera;

enum {
	CmdExit = 0,
	CmdGetSync = 1,
	CmdSetAsync = 2,
};

const int32_t kInitialValue = 1337;
const int32_t kChangedValue = 9001;

class UnixSocketTestIPCSlave
{
public:
	UnixSocketTestIPCSlave()
		: value_(kInitialValue), exitCode_(EXIT_FAILURE), exit_(false)
	{
		dispatcher_ = Thread::current()->eventDispatcher();
		ipc_.readyRead.connect(this, &UnixSocketTestIPCSlave::readyRead);
	}

	int run(UniqueFD fd)
	{
		if (ipc_.bind(std::move(fd))) {
			cerr << "Failed to connect to IPC channel" << endl;
			return EXIT_FAILURE;
		}

		while (!exit_)
			dispatcher_->processEvents();

		ipc_.close();

		return exitCode_;
	}

private:
	void readyRead()
	{
		IPCUnixSocket::Payload message;
		int ret;

		ret = ipc_.receive(&message);
		if (ret) {
			cerr << "Receive message failed: " << ret << endl;
			return;
		}

		IPCMessage ipcMessage(message);
		uint32_t cmd = ipcMessage.header().cmd;

		switch (cmd) {
		case CmdExit: {
			exit_ = true;
			break;
		}

		case CmdGetSync: {
			IPCMessage::Header header = { cmd, ipcMessage.header().cookie };
			IPCMessage response(header);

			vector<uint8_t> buf;
			tie(buf, ignore) = IPADataSerializer<int32_t>::serialize(value_);
			response.data().insert(response.data().end(), buf.begin(), buf.end());

			ret = ipc_.send(response.payload());
			if (ret < 0) {
				cerr << "Reply failed" << endl;
				stop(ret);
			}
			break;
		}

		case CmdSetAsync: {
			value_ = IPADataSerializer<int32_t>::deserialize(ipcMessage.data());
			break;
		}
		}
	}

	void stop(int code)
	{
		exitCode_ = code;
		exit_ = true;
	}

	int32_t value_;

	IPCUnixSocket ipc_;
	EventDispatcher *dispatcher_;
	int exitCode_;
	bool exit_;
};

class UnixSocketTestIPC : public Test
{
protected:
	int init()
	{
		return 0;
	}

	int setValue(int32_t val)
	{
		IPCMessage msg(CmdSetAsync);
		tie(msg.data(), ignore) = IPADataSerializer<int32_t>::serialize(val);

		int ret = ipc_->sendAsync(msg);
		if (ret < 0) {
			cerr << "Failed to call set value" << endl;
			return ret;
		}

		return 0;
	}

	int getValue()
	{
		IPCMessage msg(CmdGetSync);
		IPCMessage buf;

		int ret = ipc_->sendSync(msg, &buf);
		if (ret < 0) {
			cerr << "Failed to call get value" << endl;
			return ret;
		}

		return IPADataSerializer<int32_t>::deserialize(buf.data());
	}

	int exit()
	{
		IPCMessage msg(CmdExit);

		int ret = ipc_->sendAsync(msg);
		if (ret < 0) {
			cerr << "Failed to call exit" << endl;
			return ret;
		}

		return 0;
	}

	int run()
	{
		ipc_ = std::make_unique<IPCPipeUnixSocket>("", self().c_str());
		if (!ipc_->isConnected()) {
			cerr << "Failed to create IPCPipe" << endl;
			return TestFail;
		}

		int ret = getValue();
		if (ret != kInitialValue) {
			cerr << "Wrong initial value, expected "
			     << kInitialValue << ", got " << ret << endl;
			return TestFail;
		}

		ret = setValue(kChangedValue);
		if (ret < 0) {
			cerr << "Failed to set value: " << strerror(-ret) << endl;
			return TestFail;
		}

		ret = getValue();
		if (ret != kChangedValue) {
			cerr << "Wrong set value, expected " << kChangedValue
			     << ", got " << ret << endl;
			return TestFail;
		}

		ret = exit();
		if (ret < 0) {
			cerr << "Failed to exit: " << strerror(-ret) << endl;
			return TestFail;
		}

		return TestPass;
	}

private:
	ProcessManager processManager_;

	unique_ptr<IPCPipeUnixSocket> ipc_;
};

/*
 * Can't use TEST_REGISTER() as single binary needs to act as both client and
 * server
 */
int main(int argc, char **argv)
{
	/* IPCPipeUnixSocket passes IPA module path in argv[1] */
	if (argc == 3) {
		UniqueFD ipcfd = UniqueFD(std::stoi(argv[2]));
		UnixSocketTestIPCSlave slave;
		return slave.run(std::move(ipcfd));
	}

	UnixSocketTestIPC test;
	test.setArgs(argc, argv);
	return test.execute();
}