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
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
|
/* 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"
#include <stdlib.h>
#include <string>
#include <libcamera/base/file.h>
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
#include "libcamera/internal/yaml_parser.h"
#include <hardware/camera3.h>
using namespace libcamera;
LOG_DEFINE_CATEGORY(HALConfig)
class CameraHalConfig::Private : public Extensible::Private
{
LIBCAMERA_DECLARE_PUBLIC(CameraHalConfig)
public:
Private();
int parseConfigFile(File &file, std::map<std::string, CameraConfigData> *cameras);
private:
int parseCameraConfigData(const std::string &cameraId, const YamlObject &);
int parseLocation(const YamlObject &, CameraConfigData &cameraConfigData);
int parseRotation(const YamlObject &, CameraConfigData &cameraConfigData);
std::map<std::string, CameraConfigData> *cameras_;
};
CameraHalConfig::Private::Private()
{
}
int CameraHalConfig::Private::parseConfigFile(File &file,
std::map<std::string, CameraConfigData> *cameras)
{
/*
* Parse the HAL 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":
* location: value
* rotation: value
* ...
*
* "camera1 id":
* location: value
* rotation: value
* ...
*/
cameras_ = cameras;
std::unique_ptr<YamlObject> root = YamlParser::parse(file);
if (!root) {
LOG(HALConfig, Error) << "Failed to parse " << file.fileName()
<< ": not a valid yaml file";
return -EINVAL;
}
if (!root->isDictionary()) {
LOG(HALConfig, Error) << "Failed to parse " << file.fileName()
<< ": not a dictionary";
return -EINVAL;
}
/* Parse property "cameras" */
if (!root->contains("cameras")) {
LOG(HALConfig, Error) << "Failed to parse " << file.fileName()
<< ": missing 'cameras' property";
return -EINVAL;
}
const YamlObject &yamlObjectCameras = (*root)["cameras"];
if (!yamlObjectCameras.isDictionary()) {
LOG(HALConfig, Error) << "Failed to parse " << file.fileName()
<< ": malformed 'cameras' dictionary";
return -EINVAL;
}
for (const auto &[cameraId, configData] : yamlObjectCameras.asDict()) {
if (parseCameraConfigData(cameraId, configData)) {
LOG(HALConfig, Error) << "Failed to parse " << file.fileName()
<< ": invalid camera entry: "
<< cameraId;
return -EINVAL;
}
}
return 0;
}
int CameraHalConfig::Private::parseCameraConfigData(const std::string &cameraId,
const YamlObject &cameraObject)
{
if (!cameraObject.isDictionary())
return -EINVAL;
CameraConfigData &cameraConfigData = (*cameras_)[cameraId];
/* Parse property "location" */
if (parseLocation(cameraObject, cameraConfigData))
return -EINVAL;
/* Parse property "rotation" */
if (parseRotation(cameraObject, cameraConfigData))
return -EINVAL;
return 0;
}
int CameraHalConfig::Private::parseLocation(const YamlObject &cameraObject,
CameraConfigData &cameraConfigData)
{
if (!cameraObject.contains("location"))
return -EINVAL;
std::string location = cameraObject["location"].get<std::string>("");
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::parseRotation(const YamlObject &cameraObject,
CameraConfigData &cameraConfigData)
{
if (!cameraObject.contains("rotation"))
return -EINVAL;
int32_t rotation = cameraObject["rotation"].get<int32_t>(-1);
if (rotation < 0 || rotation >= 360) {
LOG(HALConfig, Error)
<< "Unknown rotation: " << rotation;
return -EINVAL;
}
cameraConfigData.rotation = rotation;
return 0;
}
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()
{
const char *configPath = utils::secure_getenv("LIBCAMERA_HAL_CONFIG_PATH");
if (!configPath)
configPath = LIBCAMERA_SYSCONF_DIR;
std::string filePath(configPath);
filePath += "/camera_hal.yaml";
File file(filePath);
if (!file.exists()) {
LOG(HALConfig, Debug)
<< "Configuration file " << filePath << " not found";
return -ENOENT;
}
if (!file.open(File::OpenModeFlag::ReadOnly)) {
int ret = file.error();
LOG(HALConfig, Error) << "Failed to open configuration file "
<< filePath << ": " << strerror(-ret);
return ret;
}
exists_ = true;
int ret = _d()->parseConfigFile(file, &cameras_);
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;
}
|