summaryrefslogtreecommitdiff
path: root/test/serialization/control_serialization.cpp
blob: e23383d13bd687a395701ea7b1deba7caa748d80 (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
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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * control_serialization.cpp - Serialize and deserialize controls
 */

#include <iostream>

#include <libcamera/camera.h>
#include <libcamera/control_ids.h>
#include <libcamera/controls.h>

#include "libcamera/internal/byte_stream_buffer.h"
#include "libcamera/internal/control_serializer.h"

#include "serialization_test.h"
#include "test.h"

using namespace std;
using namespace libcamera;

class ControlSerializationTest : public SerializationTest
{
protected:
	int init() override
	{
		return status_;
	}

	int run() override
	{
		ControlSerializer serializer;
		ControlSerializer deserializer;

		std::vector<uint8_t> infoData;
		std::vector<uint8_t> listData;

		size_t size;
		int ret;

		/* Create a control list with three controls. */
		const ControlInfoMap &infoMap = camera_->controls();
		ControlList list(infoMap);

		list.set(controls::Brightness, 0.5f);
		list.set(controls::Contrast, 1.2f);
		list.set(controls::Saturation, 0.2f);

		/*
		 * Serialize the control list, this should fail as the control
		 * info map hasn't been serialized.
		 */
		size = serializer.binarySize(list);
		listData.resize(size);
		ByteStreamBuffer buffer(listData.data(), listData.size());

		ret = serializer.serialize(list, buffer);
		if (!ret) {
			cerr << "List serialization without info map should have failed"
			     << endl;
			return TestFail;
		}

		if (buffer.overflow() || buffer.offset()) {
			cerr << "Failed list serialization modified the buffer"
			     << endl;
			return TestFail;
		}

		/* Serialize the control info map. */
		size = serializer.binarySize(infoMap);
		infoData.resize(size);
		buffer = ByteStreamBuffer(infoData.data(), infoData.size());

		ret = serializer.serialize(infoMap, buffer);
		if (ret < 0) {
			cerr << "Failed to serialize ControlInfoMap" << endl;
			return TestFail;
		}

		if (buffer.overflow()) {
			cerr << "Overflow when serializing ControlInfoMap" << endl;
			return TestFail;
		}

		/* Serialize the control list, this should now succeed. */
		size = serializer.binarySize(list);
		listData.resize(size);
		buffer = ByteStreamBuffer(listData.data(), listData.size());

		ret = serializer.serialize(list, buffer);
		if (ret) {
			cerr << "Failed to serialize ControlList" << endl;
			return TestFail;
		}

		if (buffer.overflow()) {
			cerr << "Overflow when serializing ControlList" << endl;
			return TestFail;
		}

		/*
		 * Deserialize the control list, this should fail as the control
		 * info map hasn't been deserialized.
		 */
		buffer = ByteStreamBuffer(const_cast<const uint8_t *>(listData.data()),
					  listData.size());

		ControlList newList = deserializer.deserialize<ControlList>(buffer);
		if (!newList.empty()) {
			cerr << "List deserialization without info map should have failed"
			     << endl;
			return TestFail;
		}

		if (buffer.overflow()) {
			cerr << "Failed list deserialization modified the buffer"
			     << endl;
			return TestFail;
		}

		/* Deserialize the control info map and verify the contents. */
		buffer = ByteStreamBuffer(const_cast<const uint8_t *>(infoData.data()),
					  infoData.size());

		ControlInfoMap newInfoMap = deserializer.deserialize<ControlInfoMap>(buffer);
		if (newInfoMap.empty()) {
			cerr << "Failed to deserialize ControlInfoMap" << endl;
			return TestFail;
		}

		if (buffer.overflow()) {
			cerr << "Overflow when deserializing ControlInfoMap" << endl;
			return TestFail;
		}

		if (!equals(infoMap, newInfoMap)) {
			cerr << "Deserialized map doesn't match original" << endl;
			return TestFail;
		}

		/* Deserialize the control list and verify the contents. */
		buffer = ByteStreamBuffer(const_cast<const uint8_t *>(listData.data()),
					  listData.size());

		newList = deserializer.deserialize<ControlList>(buffer);
		if (newList.empty()) {
			cerr << "Failed to deserialize ControlList" << endl;
			return TestFail;
		}

		if (buffer.overflow()) {
			cerr << "Overflow when deserializing ControlList" << endl;
			return TestFail;
		}

		if (!equals(list, newList)) {
			cerr << "Deserialized list doesn't match original" << endl;
			return TestFail;
		}

		return TestPass;
	}
};

TEST_REGISTER(ControlSerializationTest)
id='n646' href='#n646'>646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2018, Google Inc.
 *
 * log.cpp - Logging infrastructure
 */

#include "libcamera/internal/log.h"

#if HAVE_BACKTRACE
#include <execinfo.h>
#endif
#include <fstream>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unordered_set>

#include <libcamera/logging.h>

#include "libcamera/internal/thread.h"
#include "libcamera/internal/utils.h"

/**
 * \file log.h
 * \brief Logging infrastructure
 *
 * libcamera includes a logging infrastructure used through the library that
 * allows inspection of internal operation in a user-configurable way. The log
 * messages are grouped in categories that represent areas of libcamera, and
 * output of messages for each category can be controlled by independent log
 * levels.
 *
 * The levels are configurable through the LIBCAMERA_LOG_LEVELS environment
 * variable that contains a comma-separated list of 'category:level' pairs.
 *
 * The category names are strings and can include a wildcard ('*') character at
 * the end to match multiple categories.
 *
 * The level are either numeric values, or strings containing the log level
 * name. The available log levels are DEBUG, INFO, WARN, ERROR and FATAL. Log
 * message with a level higher than or equal to the configured log level for
 * their category are output to the log, while other messages are silently
 * discarded.
 *
 * By default log messages are output to stderr. They can be redirected to a log
 * file by setting the LIBCAMERA_LOG_FILE environment variable to the name of
 * the file. The file must be writable and is truncated if it exists. If any
 * error occurs when opening the file, the file is ignored and the log is output
 * to stderr.
 */

/**
 * \file logging.h
 * \brief Logging management
 *
 * API to change the logging output destination and log levels programatically.
 */

namespace libcamera {

static int log_severity_to_syslog(LogSeverity severity)
{
	switch (severity) {
	case LogDebug:
		return LOG_DEBUG;
	case LogInfo:
		return LOG_INFO;
	case LogWarning:
		return LOG_WARNING;
	case LogError:
		return LOG_ERR;
	case LogFatal:
		return LOG_ALERT;
	default:
		return LOG_NOTICE;
	}
}

static const char *log_severity_name(LogSeverity severity)
{
	static const char *const names[] = {
		"DEBUG",
		" INFO",
		" WARN",
		"ERROR",
		"FATAL",
	};

	if (static_cast<unsigned int>(severity) < ARRAY_SIZE(names))
		return names[severity];
	else
		return "UNKWN";
}

/**
 * \brief Log output
 *
 * The LogOutput class models a log output destination
 */
class LogOutput
{
public:
	LogOutput(const char *path);
	LogOutput(std::ostream *stream);
	LogOutput();
	~LogOutput();

	bool isValid() const;
	void write(const LogMessage &msg);
	void write(const std::string &msg);

private:
	void writeSyslog(LogSeverity severity, const std::string &msg);
	void writeStream(const std::string &msg);

	std::ostream *stream_;
	LoggingTarget target_;
};

/**
 * \brief Construct a log output based on a file
 * \param[in] path Full path to log file
 */
LogOutput::LogOutput(const char *path)
	: target_(LoggingTargetFile)
{
	stream_ = new std::ofstream(path);
}

/**
 * \brief Construct a log output based on a stream
 * \param[in] stream Stream to send log output to
 */
LogOutput::LogOutput(std::ostream *stream)
	: stream_(stream), target_(LoggingTargetStream)
{
}

/**
 * \brief Construct a log output to syslog
 */
LogOutput::LogOutput()
	: stream_(nullptr), target_(LoggingTargetSyslog)
{
	openlog("libcamera", LOG_PID, 0);
}

LogOutput::~LogOutput()
{
	switch (target_) {
	case LoggingTargetFile:
		delete stream_;
		break;
	case LoggingTargetSyslog:
		closelog();
		break;
	default:
		break;
	}
}

/**
 * \brief Check if the log output is valid
 * \return True if the log output is valid
 */
bool LogOutput::isValid() const
{
	switch (target_) {
	case LoggingTargetFile:
		return stream_->good();
	case LoggingTargetStream:
		return stream_ != nullptr;
	default:
		return true;
	}
}

/**
 * \brief Write message to log output
 * \param[in] msg Message to write
 */
void LogOutput::write(const LogMessage &msg)
{
	std::string str;

	switch (target_) {
	case LoggingTargetSyslog:
		str = std::string(log_severity_name(msg.severity())) + " "
		    + msg.category().name() + " " + msg.fileInfo() + " "
		    + msg.msg();
		writeSyslog(msg.severity(), str);
		break;
	case LoggingTargetStream:
	case LoggingTargetFile:
		str = "[" + utils::time_point_to_string(msg.timestamp()) + "] ["
		    + std::to_string(Thread::currentId()) + "] "
		    + log_severity_name(msg.severity()) + " "
		    + msg.category().name() + " " + msg.fileInfo() + " "
		    + msg.msg();
		writeStream(str);
		break;
	default:
		break;
	}
}

/**
 * \brief Write string to log output
 * \param[in] str String to write
 */
void LogOutput::write(const std::string &str)
{
	switch (target_) {
	case LoggingTargetSyslog:
		writeSyslog(LogDebug, str);
		break;
	case LoggingTargetStream:
	case LoggingTargetFile:
		writeStream(str);
		break;
	default:
		break;
	}
}

void LogOutput::writeSyslog(LogSeverity severity, const std::string &str)
{
	syslog(log_severity_to_syslog(severity), "%s", str.c_str());
}

void LogOutput::writeStream(const std::string &str)
{
	stream_->write(str.c_str(), str.size());
	stream_->flush();
}

/**
 * \brief Message logger
 *
 * The Logger class handles log configuration.
 */
class Logger
{
public:
	static Logger *instance();

	void write(const LogMessage &msg);
	void backtrace();