14 #include <type_traits> 36 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
37 void appendPOD(std::vector<uint8_t> &vec, T val)
39 constexpr
size_t byteWidth =
sizeof(val);
40 vec.resize(vec.size() + byteWidth);
41 memcpy(&*(vec.end() - byteWidth), &val, byteWidth);
45 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
46 T readPOD(std::vector<uint8_t>::const_iterator it,
size_t pos,
47 std::vector<uint8_t>::const_iterator end)
52 memcpy(&ret, &(*(it + pos)),
sizeof(ret));
58 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
59 T readPOD(std::vector<uint8_t> &vec,
size_t pos)
61 return readPOD<T>(vec.cbegin(), pos, vec.end());
70 static std::tuple<std::vector<uint8_t>, std::vector<SharedFD>>
73 static T
deserialize(
const std::vector<uint8_t> &data,
75 static T
deserialize(std::vector<uint8_t>::const_iterator dataBegin,
76 std::vector<uint8_t>::const_iterator dataEnd,
79 static T
deserialize(
const std::vector<uint8_t> &data,
80 const std::vector<SharedFD> &fds,
82 static T
deserialize(std::vector<uint8_t>::const_iterator dataBegin,
83 std::vector<uint8_t>::const_iterator dataEnd,
84 std::vector<SharedFD>::const_iterator fdsBegin,
85 std::vector<SharedFD>::const_iterator fdsEnd,
108 static std::tuple<std::vector<uint8_t>, std::vector<SharedFD>>
111 std::vector<uint8_t> dataVec;
112 std::vector<SharedFD> fdsVec;
115 uint32_t vecLen = data.size();
116 appendPOD<uint32_t>(dataVec, vecLen);
119 for (
auto const &it : data) {
120 std::vector<uint8_t> dvec;
121 std::vector<SharedFD> fvec;
123 std::tie(dvec, fvec) =
126 appendPOD<uint32_t>(dataVec, dvec.size());
127 appendPOD<uint32_t>(dataVec, fvec.size());
129 dataVec.insert(dataVec.end(), dvec.begin(), dvec.end());
130 fdsVec.insert(fdsVec.end(), fvec.begin(), fvec.end());
133 return { dataVec, fdsVec };
138 return deserialize(data.cbegin(), data.cend(), cs);
141 static std::vector<V>
deserialize(std::vector<uint8_t>::const_iterator dataBegin,
142 std::vector<uint8_t>::const_iterator dataEnd,
145 std::vector<SharedFD> fds;
146 return deserialize(dataBegin, dataEnd, fds.cbegin(), fds.cend(), cs);
149 static std::vector<V>
deserialize(std::vector<uint8_t> &data, std::vector<SharedFD> &fds,
152 return deserialize(data.cbegin(), data.cend(), fds.cbegin(), fds.cend(), cs);
155 static std::vector<V>
deserialize(std::vector<uint8_t>::const_iterator dataBegin,
156 std::vector<uint8_t>::const_iterator dataEnd,
157 std::vector<SharedFD>::const_iterator fdsBegin,
158 [[maybe_unused]] std::vector<SharedFD>::const_iterator fdsEnd,
161 uint32_t vecLen = readPOD<uint32_t>(dataBegin, 0, dataEnd);
162 std::vector<V> ret(vecLen);
164 std::vector<uint8_t>::const_iterator dataIter = dataBegin + 4;
165 std::vector<SharedFD>::const_iterator fdIter = fdsBegin;
166 for (uint32_t i = 0; i < vecLen; i++) {
167 uint32_t sizeofData = readPOD<uint32_t>(dataIter, 0, dataEnd);
168 uint32_t sizeofFds = readPOD<uint32_t>(dataIter, 4, dataEnd);
172 dataIter + sizeofData,
177 dataIter += sizeofData;
201 template<
typename K,
typename V>
205 static std::tuple<std::vector<uint8_t>, std::vector<SharedFD>>
208 std::vector<uint8_t> dataVec;
209 std::vector<SharedFD> fdsVec;
212 uint32_t mapLen = data.size();
213 appendPOD<uint32_t>(dataVec, mapLen);
216 for (
auto const &it : data) {
217 std::vector<uint8_t> dvec;
218 std::vector<SharedFD> fvec;
220 std::tie(dvec, fvec) =
223 appendPOD<uint32_t>(dataVec, dvec.size());
224 appendPOD<uint32_t>(dataVec, fvec.size());
226 dataVec.insert(dataVec.end(), dvec.begin(), dvec.end());
227 fdsVec.insert(fdsVec.end(), fvec.begin(), fvec.end());
229 std::tie(dvec, fvec) =
232 appendPOD<uint32_t>(dataVec, dvec.size());
233 appendPOD<uint32_t>(dataVec, fvec.size());
235 dataVec.insert(dataVec.end(), dvec.begin(), dvec.end());
236 fdsVec.insert(fdsVec.end(), fvec.begin(), fvec.end());
239 return { dataVec, fdsVec };
244 return deserialize(data.cbegin(), data.cend(), cs);
247 static std::map<K, V>
deserialize(std::vector<uint8_t>::const_iterator dataBegin,
248 std::vector<uint8_t>::const_iterator dataEnd,
251 std::vector<SharedFD> fds;
252 return deserialize(dataBegin, dataEnd, fds.cbegin(), fds.cend(), cs);
255 static std::map<K, V>
deserialize(std::vector<uint8_t> &data, std::vector<SharedFD> &fds,
258 return deserialize(data.cbegin(), data.cend(), fds.cbegin(), fds.cend(), cs);
261 static std::map<K, V>
deserialize(std::vector<uint8_t>::const_iterator dataBegin,
262 std::vector<uint8_t>::const_iterator dataEnd,
263 std::vector<SharedFD>::const_iterator fdsBegin,
264 [[maybe_unused]] std::vector<SharedFD>::const_iterator fdsEnd,
269 uint32_t mapLen = readPOD<uint32_t>(dataBegin, 0, dataEnd);
271 std::vector<uint8_t>::const_iterator dataIter = dataBegin + 4;
272 std::vector<SharedFD>::const_iterator fdIter = fdsBegin;
273 for (uint32_t i = 0; i < mapLen; i++) {
274 uint32_t sizeofData = readPOD<uint32_t>(dataIter, 0, dataEnd);
275 uint32_t sizeofFds = readPOD<uint32_t>(dataIter, 4, dataEnd);
279 dataIter + sizeofData,
284 dataIter += sizeofData;
286 sizeofData = readPOD<uint32_t>(dataIter, 0, dataEnd);
287 sizeofFds = readPOD<uint32_t>(dataIter, 4, dataEnd);
291 dataIter + sizeofData,
295 ret.insert({ key, value });
297 dataIter += sizeofData;
310 static std::tuple<std::vector<uint8_t>, std::vector<SharedFD>>
313 std::vector<uint8_t> dataVec;
315 appendPOD<uint32_t>(dataVec,
static_cast<typename
Flags<E>::Type>(data));
317 return { dataVec, {} };
327 std::vector<uint8_t>::const_iterator dataEnd,
330 return Flags<E>{
static_cast<E
>(readPOD<uint32_t>(dataBegin, 0, dataEnd)) };
334 [[maybe_unused]] std::vector<SharedFD> &fds,
341 std::vector<uint8_t>::const_iterator dataEnd,
342 [[maybe_unused]] std::vector<SharedFD>::const_iterator fdsBegin,
343 [[maybe_unused]] std::vector<SharedFD>::const_iterator fdsEnd,
Managed memory container for serialized data.
Camera control identifiers.
Image Processing Algorithm interface.
Top-level libcamera namespace.
Definition: backtrace.h:17
#define ASSERT(condition)
Abort program execution if assertion fails.
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Serializer and deserializer for control-related classes.
Definition: control_serializer.h:20
std::underlying_type_t< E > Type
The underlying data type of the enum.
Definition: flags.h:21
static T deserialize(const std::vector< uint8_t > &data, ControlSerializer *cs=nullptr)
Deserialize byte vector into an object.
IPA Data Serializer.
Definition: ipa_data_serializer.h:67
static std::tuple< std::vector< uint8_t >, std::vector< SharedFD > > serialize(const T &data, ControlSerializer *cs=nullptr)
Serialize an object into byte vector and fd vector.
Type-safe container for enum-based bitfields.
Definition: flags.h:15
Data structures related to geometric objects.
Serialization and deserialization helpers for controls.