Files
external_libcamera/src/libcamera/ipc_pipe_unixsocket.cpp
Laurent Pinchart 626172a16b libcamera: Drop file name from header comment blocks
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>
2024-05-08 22:39:50 +03:00

148 lines
3.2 KiB
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2020, Google Inc.
*
* Image Processing Algorithm IPC module using unix socket
*/
#include "libcamera/internal/ipc_pipe_unixsocket.h"
#include <vector>
#include <libcamera/base/event_dispatcher.h>
#include <libcamera/base/log.h>
#include <libcamera/base/thread.h>
#include <libcamera/base/timer.h>
#include "libcamera/internal/ipc_pipe.h"
#include "libcamera/internal/ipc_unixsocket.h"
#include "libcamera/internal/process.h"
using namespace std::chrono_literals;
namespace libcamera {
LOG_DECLARE_CATEGORY(IPCPipe)
IPCPipeUnixSocket::IPCPipeUnixSocket(const char *ipaModulePath,
const char *ipaProxyWorkerPath)
: IPCPipe()
{
std::vector<int> fds;
std::vector<std::string> args;
args.push_back(ipaModulePath);
socket_ = std::make_unique<IPCUnixSocket>();
UniqueFD fd = socket_->create();
if (!fd.isValid()) {
LOG(IPCPipe, Error) << "Failed to create socket";
return;
}
socket_->readyRead.connect(this, &IPCPipeUnixSocket::readyRead);
args.push_back(std::to_string(fd.get()));
fds.push_back(fd.get());
proc_ = std::make_unique<Process>();
int ret = proc_->start(ipaProxyWorkerPath, args, fds);
if (ret) {
LOG(IPCPipe, Error)
<< "Failed to start proxy worker process";
return;
}
connected_ = true;
}
IPCPipeUnixSocket::~IPCPipeUnixSocket()
{
}
int IPCPipeUnixSocket::sendSync(const IPCMessage &in, IPCMessage *out)
{
IPCUnixSocket::Payload response;
int ret = call(in.payload(), &response, in.header().cookie);
if (ret) {
LOG(IPCPipe, Error) << "Failed to call sync";
return ret;
}
if (out)
*out = IPCMessage(response);
return 0;
}
int IPCPipeUnixSocket::sendAsync(const IPCMessage &data)
{
int ret = socket_->send(data.payload());
if (ret) {
LOG(IPCPipe, Error) << "Failed to call async";
return ret;
}
return 0;
}
void IPCPipeUnixSocket::readyRead()
{
IPCUnixSocket::Payload payload;
int ret = socket_->receive(&payload);
if (ret) {
LOG(IPCPipe, Error) << "Receive message failed" << ret;
return;
}
/* \todo Use span to avoid the double copy when callData is found. */
if (payload.data.size() < sizeof(IPCMessage::Header)) {
LOG(IPCPipe, Error) << "Not enough data received";
return;
}
IPCMessage ipcMessage(payload);
auto callData = callData_.find(ipcMessage.header().cookie);
if (callData != callData_.end()) {
*callData->second.response = std::move(payload);
callData->second.done = true;
return;
}
/* Received unexpected data, this means it's a call from the IPA. */
recv.emit(ipcMessage);
}
int IPCPipeUnixSocket::call(const IPCUnixSocket::Payload &message,
IPCUnixSocket::Payload *response, uint32_t cookie)
{
Timer timeout;
int ret;
const auto result = callData_.insert({ cookie, { response, false } });
const auto &iter = result.first;
ret = socket_->send(message);
if (ret) {
callData_.erase(iter);
return ret;
}
/* \todo Make this less dangerous, see IPCPipe::sendSync() */
timeout.start(2000ms);
while (!iter->second.done) {
if (!timeout.isRunning()) {
LOG(IPCPipe, Error) << "Call timeout!";
callData_.erase(iter);
return -ETIMEDOUT;
}
Thread::current()->eventDispatcher()->processEvents();
}
callData_.erase(iter);
return 0;
}
} /* namespace libcamera */