summaryrefslogtreecommitdiff
path: root/test/ipa/meson.build
AgeCommit message (Collapse)Author
2019-11-20test: ipa: Add IPA wrappers testLaurent Pinchart
Wrap an IPAInterface in an IPAInterfaceWrapper in an IPAContextWrapper, and verify that the translation between C and C++ APIs work correctly. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2019-10-08test: ipa: Add test for the IPA InterfaceJacopo Mondi
Implement a basic test for IPA Interface using the VIMC dummy IPA module. The test implements an out-of-band feedback channel between the test and the dummy IPA module to verify the success of the IPA interactions. Test the only available operation defined by the IPA interface by receiving a confirmation code on the fifo communication channel. Tested-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
2019-10-08test: ipa: Rename the ipa_test to ipa_module_testJacopo Mondi
Update the IPA test to match the test class name in prevision of adding more IPA tests. Also update the description comment in the test unit, as the old name for the test was still used. Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
2019-07-04test: Allow self-contained tests to run in parallelNiklas Söderlund
The IPA, IPC and Stream tests are self-contained so they can run in parallel. Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2019-06-05libcamera: test: remove test IPA and use dummy IPA insteadPaul Elder
Use the dummy IPA for testing/sample IPA instead of the earlier test IPA. Remove the test IPA, and update tests and meson accordingly. Signed-off-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2019-06-05libcamera: ipa_interface: add headerPaul Elder
Define an IPAInterface class which will contain an IPA implementation. The methods that the IPAInterface exposes form the interface to the IPA implementation, hence the name. IPA module shared objects will implement this class. This also means that IPA module shared objects must be implemented in C++, so remove the C test IPA module. Signed-off-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2019-05-23meson: Create and use a dependency for libcamera and its headersLaurent Pinchart
Instead of manually adding the libcamera library and include path to every target that requires it, declare a dependency that groups the headers as source, the library and the include path, and use it through the project. This simplifies handling of the dependency. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2019-05-23meson: Fix coding style in meson.build filesLaurent Pinchart
Consistently go for 4 spaces indentation, and always put a space between the colon in argument lists, as per the examples from the meson documentation. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2019-05-21test: ipa: add IPAModule testsPaul Elder
Add tests to test the the IPAModule class, for loading the IPA module info from IPA module .so shared objects, with modules written in both C and C++. Signed-off-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
'>219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2021, Google Inc.
 *
 * camera_hal_config.cpp - Camera HAL configuration file manager
 */
#include "camera_hal_config.h"

#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
#include <experimental/filesystem>
namespace std {
namespace filesystem = std::experimental::filesystem;
}
#else
#include <filesystem>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <yaml.h>

#include <hardware/camera3.h>

#include <libcamera/base/log.h>

using namespace libcamera;

LOG_DEFINE_CATEGORY(HALConfig)

class CameraHalConfig::Private : public Extensible::Private
{
	LIBCAMERA_DECLARE_PUBLIC(CameraHalConfig)

public:
	Private();

	int parseConfigFile(FILE *fh, std::map<std::string, CameraConfigData> *cameras);

private:
	std::string parseValue();
	std::string parseKey();
	int parseValueBlock();
	int parseCameraLocation(CameraConfigData *cameraConfigData,
				const std::string &location);
	int parseCameraConfigData(const std::string &cameraId);
	int parseCameras();
	int parseEntry();

	yaml_parser_t parser_;
	std::map<std::string, CameraConfigData> *cameras_;
};

CameraHalConfig::Private::Private()
{
}

std::string CameraHalConfig::Private::parseValue()
{
	yaml_token_t token;

	/* Make sure the token type is a value and get its content. */
	yaml_parser_scan(&parser_, &token);
	if (token.type != YAML_VALUE_TOKEN) {
		yaml_token_delete(&token);
		return "";
	}
	yaml_token_delete(&token);

	yaml_parser_scan(&parser_, &token);
	if (token.type != YAML_SCALAR_TOKEN) {
		yaml_token_delete(&token);
		return "";
	}

	std::string value(reinterpret_cast<char *>(token.data.scalar.value),
			  token.data.scalar.length);
	yaml_token_delete(&token);

	return value;
}

std::string CameraHalConfig::Private::parseKey()
{
	yaml_token_t token;

	/* Make sure the token type is a key and get its value. */
	yaml_parser_scan(&parser_, &token);
	if (token.type != YAML_SCALAR_TOKEN) {
		yaml_token_delete(&token);
		return "";
	}

	std::string value(reinterpret_cast<char *>(token.data.scalar.value),
			  token.data.scalar.length);
	yaml_token_delete(&token);

	return value;
}

int CameraHalConfig::Private::parseValueBlock()
{
	yaml_token_t token;

	/* Make sure the next token are VALUE and BLOCK_MAPPING_START. */
	yaml_parser_scan(&parser_, &token);
	if (token.type != YAML_VALUE_TOKEN) {
		yaml_token_delete(&token);
		return -EINVAL;
	}
	yaml_token_delete(&token);

	yaml_parser_scan(&parser_, &token);
	if (token.type != YAML_BLOCK_MAPPING_START_TOKEN) {
		yaml_token_delete(&token);
		return -EINVAL;
	}
	yaml_token_delete(&token);

	return 0;
}

int CameraHalConfig::Private::parseCameraLocation(CameraConfigData *cameraConfigData,
						  const std::string &location)
{
	if (location == "front")
		cameraConfigData->facing = CAMERA_FACING_FRONT;
	else if (location == "back")
		cameraConfigData->facing = CAMERA_FACING_BACK;
	else
		return -EINVAL;

	return 0;
}

int CameraHalConfig::Private::parseCameraConfigData(const std::string &cameraId)
{
	int ret = parseValueBlock();
	if (ret)
		return ret;

	/*
	 * Parse the camera properties and store them in a cameraConfigData
	 * instance.
	 *
	 * Add a safety counter to make sure we don't loop indefinitely in case
	 * the configuration file is malformed.
	 */
	CameraConfigData cameraConfigData;
	unsigned int sentinel = 100;
	bool blockEnd = false;
	yaml_token_t token;

	do {
		yaml_parser_scan(&parser_, &token);
		switch (token.type) {
		case YAML_KEY_TOKEN: {
			yaml_token_delete(&token);

			/*
			 * Parse the camera property key and make sure it is
			 * valid.
			 */
			std::string key = parseKey();
			std::string value = parseValue();
			if (key.empty() || value.empty())
				return -EINVAL;

			if (key == "location") {
				ret = parseCameraLocation(&cameraConfigData, value);
				if (ret) {
					LOG(HALConfig, Error)
						<< "Unknown location: " << value;
					return -EINVAL;
				}
			} else if (key == "rotation") {
				ret = std::stoi(value);
				if (ret < 0 || ret >= 360) {
					LOG(HALConfig, Error)
						<< "Unknown rotation: " << value;
					return -EINVAL;
				}
				cameraConfigData.rotation = ret;
			} else {
				LOG(HALConfig, Error)
					<< "Unknown key: " << key;
				return -EINVAL;
			}
			break;
		}

		case YAML_BLOCK_END_TOKEN:
			blockEnd = true;
			[[fallthrough]];
		default:
			yaml_token_delete(&token);
			break;
		}

		--sentinel;
	} while (!blockEnd && sentinel);
	if (!sentinel)
		return -EINVAL;

	(*cameras_)[cameraId] = cameraConfigData;

	return 0;
}

int CameraHalConfig::Private::parseCameras()
{
	int ret = parseValueBlock();
	if (ret) {
		LOG(HALConfig, Error) << "Configuration file is not valid";
		return ret;
	}

	/*
	 * Parse the camera properties.
	 *
	 * Each camera properties block is a list of properties associated
	 * with the ID (as assembled by CameraSensor::generateId()) of the
	 * camera they refer to.
	 *
	 * cameras:
	 *   "camera0 id":
	 *     key: value
	 *     key: value
	 *     ...
	 *
	 *   "camera1 id":
	 *     key: value
	 *     key: value
	 *     ...
	 */
	bool blockEnd = false;
	yaml_token_t token;
	do {
		yaml_parser_scan(&parser_, &token);
		switch (token.type) {
		case YAML_KEY_TOKEN: {
			yaml_token_delete(&token);

			/* Parse the camera ID as key of the property list. */
			std::string cameraId = parseKey();
			if (cameraId.empty())
				return -EINVAL;

			ret = parseCameraConfigData(cameraId);
			if (ret)
				return -EINVAL;
			break;
		}
		case YAML_BLOCK_END_TOKEN:
			blockEnd = true;
			[[fallthrough]];
		default:
			yaml_token_delete(&token);
			break;
		}
	} while (!blockEnd);

	return 0;
}

int CameraHalConfig::Private::parseEntry()
{
	int ret = -EINVAL;

	/*
	 * Parse each key we find in the file.
	 *
	 * The 'cameras' keys maps to a list of (lists) of camera properties.
	 */

	std::string key = parseKey();
	if (key.empty())
		return ret;

	if (key == "cameras")
		ret = parseCameras();
	else
		LOG(HALConfig, Error) << "Unknown key: " << key;

	return ret;
}

int CameraHalConfig::Private::parseConfigFile(FILE *fh,
					      std::map<std::string, CameraConfigData> *cameras)
{
	cameras_ = cameras;

	int ret = yaml_parser_initialize(&parser_);
	if (!ret) {
		LOG(HALConfig, Error) << "Failed to initialize yaml parser";
		return -EINVAL;
	}
	yaml_parser_set_input_file(&parser_, fh);

	yaml_token_t token;
	yaml_parser_scan(&parser_, &token);
	if (token.type != YAML_STREAM_START_TOKEN) {
		LOG(HALConfig, Error) << "Configuration file is not valid";
		yaml_token_delete(&token);
		yaml_parser_delete(&parser_);
		return -EINVAL;
	}
	yaml_token_delete(&token);

	yaml_parser_scan(&parser_, &token);
	if (token.type != YAML_BLOCK_MAPPING_START_TOKEN) {
		LOG(HALConfig, Error) << "Configuration file is not valid";
		yaml_token_delete(&token);
		yaml_parser_delete(&parser_);
		return -EINVAL;
	}
	yaml_token_delete(&token);

	/* Parse the file and parse each single key one by one. */
	do {
		yaml_parser_scan(&parser_, &token);
		switch (token.type) {
		case YAML_KEY_TOKEN:
			yaml_token_delete(&token);
			ret = parseEntry();
			break;

		case YAML_STREAM_END_TOKEN:
			ret = -ENOENT;
			[[fallthrough]];
		default:
			yaml_token_delete(&token);
			break;
		}
	} while (ret >= 0);
	yaml_parser_delete(&parser_);

	if (ret && ret != -ENOENT)
		LOG(HALConfig, Error) << "Configuration file is not valid";

	return ret == -ENOENT ? 0 : ret;
}

CameraHalConfig::CameraHalConfig()
	: Extensible(std::make_unique<Private>()), exists_(false), valid_(false)
{
	parseConfigurationFile();
}

/*
 * Open the HAL configuration file and validate its content.
 * Return 0 on success, a negative error code otherwise
 * retval -ENOENT The configuration file is not available
 * retval -EINVAL The configuration file is available but not valid
 */
int CameraHalConfig::parseConfigurationFile()
{
	std::filesystem::path filePath = LIBCAMERA_SYSCONF_DIR;
	filePath /= "camera_hal.yaml";
	if (!std::filesystem::is_regular_file(filePath)) {
		LOG(HALConfig, Debug)
			<< "Configuration file: \"" << filePath << "\" not found";
		return -ENOENT;
	}

	FILE *fh = fopen(filePath.c_str(), "r");
	if (!fh) {
		int ret = -errno;
		LOG(HALConfig, Error) << "Failed to open configuration file "
				      << filePath << ": " << strerror(-ret);
		return ret;
	}

	exists_ = true;

	int ret = _d()->parseConfigFile(fh, &cameras_);
	fclose(fh);
	if (ret)
		return -EINVAL;

	valid_ = true;

	for (const auto &c : cameras_) {
		const std::string &cameraId = c.first;
		const CameraConfigData &camera = c.second;
		LOG(HALConfig, Debug) << "'" << cameraId << "' "
				      << "(" << camera.facing << ")["
				      << camera.rotation << "]";
	}

	return 0;
}

const CameraConfigData *CameraHalConfig::cameraConfigData(const std::string &cameraId) const
{
	const auto &it = cameras_.find(cameraId);
	if (it == cameras_.end()) {
		LOG(HALConfig, Error)
			<< "Camera '" << cameraId
			<< "' not described in the HAL configuration file";
		return nullptr;
	}

	return &it->second;
}