summaryrefslogtreecommitdiff
path: root/src/apps/cam/file_sink.h
AgeCommit message (Collapse)Author
2024-05-08libcamera: Drop file name from header comment blocksLaurent Pinchart
Source files in libcamera start by a comment block header, which includes the file name and a one-line description of the file contents. While the latter is useful to get a quick overview of the file contents at a glance, the former is mostly a source of inconvenience. The name in the comments can easily get out of sync with the file name when files are renamed, and copy & paste during development have often lead to incorrect names being used to start with. Readers of the source code are expected to know which file they're looking it. Drop the file name from the header comment block. The change was generated with the following script: ---------------------------------------- dirs="include/libcamera src test utils" declare -rA patterns=( ['c']=' \* ' ['cpp']=' \* ' ['h']=' \* ' ['py']='# ' ['sh']='# ' ) for ext in ${!patterns[@]} ; do files=$(for dir in $dirs ; do find $dir -name "*.${ext}" ; done) pattern=${patterns[${ext}]} for file in $files ; do name=$(basename ${file}) sed -i "s/^\(${pattern}\)${name} - /\1/" "$file" done done ---------------------------------------- This misses several files that are out of sync with the comment block header. Those will be addressed separately and manually. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
2022-10-24apps: cam: Fix compilation error with clang when libtiff-4 is not foundLaurent Pinchart
When libtiff-4 is not found, the private camera_ member of the FileSink class is set but never used. This causes a compilation error with clang: In file included from ../../src/apps/cam/file_sink.cpp:19: ../../src/apps/cam/file_sink.h:39:27: error: private field 'camera_' is not used [-Werror,-Wunused-private-field] const libcamera::Camera *camera_; Fix by making the camera_ member field conditional on HAVE_TIFF. Fixes: 6404b163bcbb ("cam: file_sink: Add support for DNG output") Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2022-10-20Move test applications to src/apps/Laurent Pinchart
The cam and qcam test application share code, currently through a crude hack that references the cam source files directly from the qcam meson.build file. To prepare for the introduction of hosting that code in a static library, move all applications to src/apps/. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
ref='#n127'>127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/* 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();
}