blob: f5d9e71f69e2f13f687043bbec7154908c1efd10 [file] [log] [blame]
Martijn Coenen72110162016-08-19 14:28:25 +02001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HIDL_SUPPORT_H
18#define ANDROID_HIDL_SUPPORT_H
19
Iliyan Malchev692070a2016-09-12 16:30:44 -070020#include <algorithm>
Yifan Hong44ab6232016-11-22 16:28:24 -080021#include <array>
Scott Randolphbb840f72016-11-21 14:39:26 -080022#include <iterator>
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010023#include <cutils/native_handle.h>
Steven Moreland337c3ae2016-11-22 13:37:32 -080024#include <hidl/HidlInternal.h>
Yifan Honga3c31842016-10-21 10:33:14 -070025#include <hidl/Status.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080026#include <map>
Yifan Hongbe7a6882017-01-05 17:30:17 -080027#include <sstream>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080028#include <stddef.h>
Andreas Huber00a985c2016-09-28 14:24:53 -070029#include <tuple>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080030#include <type_traits>
Iliyan Malchev692070a2016-09-12 16:30:44 -070031#include <utils/Errors.h>
32#include <utils/RefBase.h>
33#include <utils/StrongPointer.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080034#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020035
36namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010037
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010038// this file is included by all hidl interface, so we must forward declare the
39// IMemory and IBase types.
Martijn Coenen30791002016-12-01 15:40:46 +010040namespace hidl {
41namespace memory {
42namespace V1_0 {
43 struct IMemory;
44}; // namespace V1_0
45}; // namespace manager
46}; // namespace hidl
47
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010048namespace hidl {
49namespace base {
50namespace V1_0 {
51 struct IBase;
52}; // namespace V1_0
53}; // namespace base
54}; // namespace hidl
55
Martijn Coenen72110162016-08-19 14:28:25 +020056namespace hardware {
57
Yifan Hong24332ef2017-03-07 16:22:19 -080058namespace details {
59// Return true on userdebug / eng builds and false on user builds.
60bool debuggable();
61} // namespace details
62
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010063// hidl_death_recipient is a callback interfaced that can be used with
64// linkToDeath() / unlinkToDeath()
65struct hidl_death_recipient : public virtual RefBase {
66 virtual void serviceDied(uint64_t cookie,
67 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
68};
69
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010070// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
71// so that it can safely be transferred between 32-bit and 64-bit processes.
Martijn Coenen04b91c02017-01-19 14:14:21 +010072// The ownership semantics for this are:
73// 1) The conversion constructor and assignment operator taking a const native_handle_t*
74// do not take ownership of the handle; this is because these operations are usually
75// just done for IPC, and cloning by default is a waste of resources. If you want
76// a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/);
77// 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership;
78// that is because it's not intuitive that this class encapsulates a native_handle_t
79// which needs cloning to be valid; in particular, this allows constructs like this:
80// hidl_handle copy;
81// foo->someHidlCall([&](auto incoming_handle) {
82// copy = incoming_handle;
83// });
84// // copy and its enclosed file descriptors will remain valid here.
85// 3) The move constructor does what you would expect; it only owns the handle if the
86// original did.
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010087struct hidl_handle {
Martijn Coenen04b91c02017-01-19 14:14:21 +010088 hidl_handle();
89 ~hidl_handle();
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010090
Martijn Coenen04b91c02017-01-19 14:14:21 +010091 hidl_handle(const native_handle_t *handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010092
Martijn Coenen04b91c02017-01-19 14:14:21 +010093 // copy constructor.
94 hidl_handle(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010095
96 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -070097 hidl_handle(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010098
Steven Moreland6ffdc2a2017-01-23 12:34:33 -080099 // assignment operators
Martijn Coenen04b91c02017-01-19 14:14:21 +0100100 hidl_handle &operator=(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100101
Martijn Coenen04b91c02017-01-19 14:14:21 +0100102 hidl_handle &operator=(const native_handle_t *native_handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100103
Martijn Coenene8c36e12017-05-30 16:25:07 -0700104 hidl_handle &operator=(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100105
Martijn Coenen04b91c02017-01-19 14:14:21 +0100106 void setTo(native_handle_t* handle, bool shouldOwn = false);
107
108 const native_handle_t* operator->() const;
109
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100110 // implicit conversion to const native_handle_t*
Martijn Coenen04b91c02017-01-19 14:14:21 +0100111 operator const native_handle_t *() const;
112
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100113 // explicit conversion
Martijn Coenen04b91c02017-01-19 14:14:21 +0100114 const native_handle_t *getNativeHandle() const;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100115private:
Martijn Coenen04b91c02017-01-19 14:14:21 +0100116 void freeHandle();
117
Andreas Huber6688d602017-03-30 10:58:29 -0700118 details::hidl_pointer<const native_handle_t> mHandle __attribute__ ((aligned(8)));
119 bool mOwnsHandle __attribute ((aligned(8)));
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100120};
121
Martijn Coenen72110162016-08-19 14:28:25 +0200122struct hidl_string {
123 hidl_string();
124 ~hidl_string();
125
Yifan Hong602b85a2016-10-24 13:40:01 -0700126 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200127 hidl_string(const hidl_string &);
Steven Morelanda21d84f2017-02-16 09:23:45 -0800128 // copy from a C-style string. nullptr will create an empty string
Steven Morelande03c0872016-10-24 10:43:50 -0700129 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800130 // copy the first length characters from a C-style string.
131 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700132 // copy from an std::string.
133 hidl_string(const std::string &);
134
135 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700136 hidl_string(hidl_string &&) noexcept;
Martijn Coenen72110162016-08-19 14:28:25 +0200137
138 const char *c_str() const;
139 size_t size() const;
140 bool empty() const;
141
Yifan Hong602b85a2016-10-24 13:40:01 -0700142 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700143 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700144 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200145 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700146 // copy from an std::string.
147 hidl_string &operator=(const std::string &);
148 // move assignment operator.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700149 hidl_string &operator=(hidl_string &&other) noexcept;
Yifan Hong602b85a2016-10-24 13:40:01 -0700150 // cast to std::string.
151 operator std::string() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700152
Martijn Coenen72110162016-08-19 14:28:25 +0200153 void clear();
154
155 // Reference an external char array. Ownership is _not_ transferred.
156 // Caller is responsible for ensuring that underlying memory is valid
157 // for the lifetime of this hidl_string.
158 void setToExternal(const char *data, size_t size);
159
Andreas Huberebfeb362016-08-25 13:39:05 -0700160 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
161 static const size_t kOffsetOfBuffer;
162
Martijn Coenen72110162016-08-19 14:28:25 +0200163private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100164 details::hidl_pointer<const char> mBuffer;
165 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700166 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200167
Yifan Hong602b85a2016-10-24 13:40:01 -0700168 // copy from data with size. Assume that my memory is freed
169 // (through clear(), for example)
170 void copyFrom(const char *data, size_t size);
171 // move from another hidl_string
172 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200173};
174
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700175// Use NOLINT to suppress missing parentheses warnings around OP.
Janis Danisevskis62a38df2017-11-22 13:47:33 -0800176#define HIDL_STRING_OPERATOR(OP) \
177 inline bool operator OP(const hidl_string& hs1, const hidl_string& hs2) { \
178 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; /* NOLINT */ \
179 } \
180 inline bool operator OP(const hidl_string& hs, const char* s) { \
181 return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \
182 } \
183 inline bool operator OP(const char* s, const hidl_string& hs) { \
184 return strcmp(s, hs.c_str()) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800185 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800186
Steven Moreland551396a2017-02-13 18:33:20 -0800187HIDL_STRING_OPERATOR(==)
188HIDL_STRING_OPERATOR(!=)
189HIDL_STRING_OPERATOR(<)
190HIDL_STRING_OPERATOR(<=)
191HIDL_STRING_OPERATOR(>)
192HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800193
Steven Moreland551396a2017-02-13 18:33:20 -0800194#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700195
Scott Randolph0c84ab42017-04-03 14:07:14 -0700196// Send our content to the output stream
197std::ostream& operator<<(std::ostream& os, const hidl_string& str);
198
199
Martijn Coenen30791002016-12-01 15:40:46 +0100200// hidl_memory is a structure that can be used to transfer
201// pieces of shared memory between processes. The assumption
202// of this object is that the memory remains accessible as
203// long as the file descriptors in the enclosed mHandle
204// - as well as all of its cross-process dups() - remain opened.
205struct hidl_memory {
206
Martijn Coenen04b91c02017-01-19 14:14:21 +0100207 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100208 }
209
210 /**
Steven Moreland3f8c4162017-10-11 13:47:00 -0700211 * Creates a hidl_memory object whose handle has the same lifetime
212 * as the handle moved into it.
213 */
214 hidl_memory(const hidl_string& name, hidl_handle&& handle, size_t size)
215 : mHandle(std::move(handle)), mSize(size), mName(name) {}
216
217 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100218 * Creates a hidl_memory object, but doesn't take ownership of
219 * the passed in native_handle_t; callers are responsible for
220 * making sure the handle remains valid while this object is
221 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100222 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100223 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
224 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100225 mSize(size),
226 mName(name)
227 {}
228
229 // copy constructor
230 hidl_memory(const hidl_memory& other) {
231 *this = other;
232 }
233
234 // copy assignment
235 hidl_memory &operator=(const hidl_memory &other) {
236 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100237 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100238 mSize = other.mSize;
239 mName = other.mName;
240 }
241
242 return *this;
243 }
244
Hridya Valsaraju01268892017-02-27 08:48:38 -0800245 // move constructor
Martijn Coenene8c36e12017-05-30 16:25:07 -0700246 hidl_memory(hidl_memory&& other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800247 *this = std::move(other);
248 }
249
250 // move assignment
Martijn Coenene8c36e12017-05-30 16:25:07 -0700251 hidl_memory &operator=(hidl_memory &&other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800252 if (this != &other) {
253 mHandle = std::move(other.mHandle);
254 mSize = other.mSize;
255 mName = std::move(other.mName);
256 other.mSize = 0;
257 }
258
259 return *this;
260 }
261
Martijn Coenen30791002016-12-01 15:40:46 +0100262
263 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100264 }
265
266 const native_handle_t* handle() const {
267 return mHandle;
268 }
269
270 const hidl_string &name() const {
271 return mName;
272 }
273
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800274 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100275 return mSize;
276 }
277
Howard Chen9bc35c42017-10-13 14:21:46 +0800278 // @return true if it's valid
279 inline bool valid() const { return handle() != nullptr; }
280
Martijn Coenen30791002016-12-01 15:40:46 +0100281 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
282 static const size_t kOffsetOfHandle;
283 // offsetof(hidl_memory, mName) exposed since mHandle is private.
284 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800285
Martijn Coenen30791002016-12-01 15:40:46 +0100286private:
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800287 hidl_handle mHandle __attribute__ ((aligned(8)));
288 uint64_t mSize __attribute__ ((aligned(8)));
289 hidl_string mName __attribute__ ((aligned(8)));
Martijn Coenen30791002016-12-01 15:40:46 +0100290};
291
Howard Chen9bc35c42017-10-13 14:21:46 +0800292// HidlMemory is a wrapper class to support sp<> for hidl_memory. It also
293// provides factory methods to create an instance from hidl_memory or
294// from a opened file descriptor. The number of factory methods can be increase
295// to support other type of hidl_memory without break the ABI.
296class HidlMemory : public virtual hidl_memory, public virtual ::android::RefBase {
297public:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800298 static sp<HidlMemory> getInstance(const hidl_memory& mem);
299
Howard Chen9bc35c42017-10-13 14:21:46 +0800300 static sp<HidlMemory> getInstance(hidl_memory&& mem);
301
302 static sp<HidlMemory> getInstance(const hidl_string& name, hidl_handle&& handle, uint64_t size);
303 // @param fd, shall be opened and points to the resource.
304 // @note this method takes the ownership of the fd and will close it in
305 // destructor
Howard Chen8e42f5a2017-11-09 13:58:28 +0800306 // @return nullptr in failure with the fd closed
Howard Chen9bc35c42017-10-13 14:21:46 +0800307 static sp<HidlMemory> getInstance(const hidl_string& name, int fd, uint64_t size);
308
Howard Chen280af0a2017-10-20 13:58:36 +0800309 virtual ~HidlMemory();
Howard Chen8e42f5a2017-11-09 13:58:28 +0800310
Howard Chen9bc35c42017-10-13 14:21:46 +0800311protected:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800312 HidlMemory();
313 HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size);
Howard Chen9bc35c42017-10-13 14:21:46 +0800314};
Andreas Huber20dce082016-09-22 19:39:13 -0700315////////////////////////////////////////////////////////////////////////////////
316
Martijn Coenen72110162016-08-19 14:28:25 +0200317template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800318struct hidl_vec {
Steven Morelandf2658722019-04-18 12:54:56 -0700319 hidl_vec() {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800320 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Steven Morelandf2658722019-04-18 12:54:56 -0700321
322 memset(this, 0, sizeof(*this));
323 // mSize is 0
324 // mBuffer is nullptr
325
326 // this is for consistency with the original implementation
327 mOwnsBuffer = true;
Martijn Coenen72110162016-08-19 14:28:25 +0200328 }
329
Steven Morelandaa661fc2017-10-06 09:40:18 -0700330 // Note, does not initialize primitive types.
Steven Moreland96e9de02017-09-29 14:11:20 -0700331 hidl_vec(size_t size) : hidl_vec() { resize(size); }
332
Yifan Hong602b85a2016-10-24 13:40:01 -0700333 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200334 *this = other;
335 }
336
Steven Morelandf2658722019-04-18 12:54:56 -0700337 hidl_vec(hidl_vec<T> &&other) noexcept : hidl_vec() {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100338 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700339 }
340
Steven Morelandf2658722019-04-18 12:54:56 -0700341 hidl_vec(const std::initializer_list<T> list) : hidl_vec() {
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800342 if (list.size() > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800343 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800344 }
345 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800346 mBuffer = new T[mSize];
Steven Morelandf2658722019-04-18 12:54:56 -0700347 mOwnsBuffer = true;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800348
Steven Morelandb69926a2016-11-15 10:02:57 -0800349 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800350 for (auto it = list.begin(); it != list.end(); ++it) {
351 mBuffer[idx++] = *it;
352 }
353 }
354
Yifan Hong602b85a2016-10-24 13:40:01 -0700355 hidl_vec(const std::vector<T> &other) : hidl_vec() {
356 *this = other;
357 }
358
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700359 template <typename InputIterator,
360 typename = typename std::enable_if<std::is_convertible<
361 typename std::iterator_traits<InputIterator>::iterator_category,
362 std::input_iterator_tag>::value>::type>
Steven Morelandf2658722019-04-18 12:54:56 -0700363 hidl_vec(InputIterator first, InputIterator last) : hidl_vec() {
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700364 auto size = std::distance(first, last);
365 if (size > static_cast<int64_t>(UINT32_MAX)) {
366 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
367 }
368 if (size < 0) {
369 details::logAlwaysFatal("size can't be negative.");
370 }
371 mSize = static_cast<uint32_t>(size);
372 mBuffer = new T[mSize];
Steven Morelandf2658722019-04-18 12:54:56 -0700373 mOwnsBuffer = true;
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700374
375 size_t idx = 0;
376 for (; first != last; ++first) {
377 mBuffer[idx++] = static_cast<T>(*first);
378 }
379 }
380
Martijn Coenen72110162016-08-19 14:28:25 +0200381 ~hidl_vec() {
382 if (mOwnsBuffer) {
383 delete[] mBuffer;
384 }
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700385 mBuffer = nullptr;
Martijn Coenen72110162016-08-19 14:28:25 +0200386 }
387
Alexey Polyudove2299012016-10-19 09:52:00 -0700388 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200389 // caller's responsibility to ensure that the underlying memory stays
390 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700391 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200392 if (mOwnsBuffer) {
393 delete [] mBuffer;
394 }
395 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100396 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800397 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100398 }
399 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700400 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200401 }
402
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700403 T *data() {
404 return mBuffer;
405 }
406
407 const T *data() const {
408 return mBuffer;
409 }
410
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700411 T *releaseData() {
412 if (!mOwnsBuffer && mSize > 0) {
413 resize(mSize);
414 }
415 mOwnsBuffer = false;
416 return mBuffer;
417 }
418
Martijn Coenene8c36e12017-05-30 16:25:07 -0700419 hidl_vec &operator=(hidl_vec &&other) noexcept {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100420 if (mOwnsBuffer) {
421 delete[] mBuffer;
422 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700423 mBuffer = other.mBuffer;
424 mSize = other.mSize;
425 mOwnsBuffer = other.mOwnsBuffer;
426 other.mOwnsBuffer = false;
427 return *this;
428 }
429
Martijn Coenen72110162016-08-19 14:28:25 +0200430 hidl_vec &operator=(const hidl_vec &other) {
431 if (this != &other) {
432 if (mOwnsBuffer) {
433 delete[] mBuffer;
434 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700435 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200436 }
437
438 return *this;
439 }
440
Yifan Hong602b85a2016-10-24 13:40:01 -0700441 // copy from an std::vector.
442 hidl_vec &operator=(const std::vector<T> &other) {
443 if (mOwnsBuffer) {
444 delete[] mBuffer;
445 }
446 copyFrom(other, other.size());
447 return *this;
448 }
449
450 // cast to an std::vector.
451 operator std::vector<T>() const {
452 std::vector<T> v(mSize);
453 for (size_t i = 0; i < mSize; ++i) {
454 v[i] = mBuffer[i];
455 }
456 return v;
457 }
458
Yifan Hong9fcbb362016-12-20 16:46:41 -0800459 // equality check, assuming that T::operator== is defined.
460 bool operator==(const hidl_vec &other) const {
461 if (mSize != other.size()) {
462 return false;
463 }
464 for (size_t i = 0; i < mSize; ++i) {
465 if (!(mBuffer[i] == other.mBuffer[i])) {
466 return false;
467 }
468 }
469 return true;
470 }
471
472 // inequality check, assuming that T::operator== is defined.
473 inline bool operator!=(const hidl_vec &other) const {
474 return !((*this) == other);
475 }
476
Martijn Coenen72110162016-08-19 14:28:25 +0200477 size_t size() const {
478 return mSize;
479 }
480
481 T &operator[](size_t index) {
482 return mBuffer[index];
483 }
484
485 const T &operator[](size_t index) const {
486 return mBuffer[index];
487 }
488
Steven Morelandaa661fc2017-10-06 09:40:18 -0700489 // Does not initialize primitive types if new size > old size.
Martijn Coenen72110162016-08-19 14:28:25 +0200490 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100491 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800492 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100493 }
Martijn Coenen72110162016-08-19 14:28:25 +0200494 T *newBuffer = new T[size];
495
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100496 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200497 newBuffer[i] = mBuffer[i];
498 }
499
500 if (mOwnsBuffer) {
501 delete[] mBuffer;
502 }
503 mBuffer = newBuffer;
504
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100505 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200506 mOwnsBuffer = true;
507 }
508
Yifan Hong089ae132016-11-11 11:32:52 -0800509 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
510 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800511
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800512private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800513 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800514 template<bool is_const>
515 class iter : public std::iterator<
516 std::random_access_iterator_tag, /* Category */
517 T,
518 ptrdiff_t, /* Distance */
519 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
520 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800521 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800522 using traits = std::iterator_traits<iter>;
523 using ptr_type = typename traits::pointer;
524 using ref_type = typename traits::reference;
525 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800526 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800527 iter(ptr_type ptr) : mPtr(ptr) { }
528 inline iter &operator++() { mPtr++; return *this; }
529 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
530 inline iter &operator--() { mPtr--; return *this; }
531 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
532 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
533 inline iter operator+(diff_type n) const { return mPtr + n; }
534 inline iter operator-(diff_type n) const { return mPtr - n; }
535 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
536 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
537 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
538 inline ref_type operator*() const { return *mPtr; }
539 inline ptr_type operator->() const { return mPtr; }
540 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
541 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
542 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
543 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
544 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
545 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
546 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800547 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800548 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800549 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800550public:
551 using iterator = iter<false /* is_const */>;
552 using const_iterator = iter<true /* is_const */>;
553
Scott Randolphbb840f72016-11-21 14:39:26 -0800554 iterator begin() { return data(); }
555 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800556 const_iterator begin() const { return data(); }
557 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800558
Martijn Coenen72110162016-08-19 14:28:25 +0200559private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100560 details::hidl_pointer<T> mBuffer;
561 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200562 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700563
564 // copy from an array-like object, assuming my resources are freed.
565 template <typename Array>
566 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800567 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700568 mOwnsBuffer = true;
569 if (mSize > 0) {
570 mBuffer = new T[size];
571 for (size_t i = 0; i < size; ++i) {
572 mBuffer[i] = data[i];
573 }
574 } else {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700575 mBuffer = nullptr;
Yifan Hong602b85a2016-10-24 13:40:01 -0700576 }
577 }
Martijn Coenen72110162016-08-19 14:28:25 +0200578};
579
Yifan Hong089ae132016-11-11 11:32:52 -0800580template <typename T>
581const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
582
Andreas Huber20dce082016-09-22 19:39:13 -0700583////////////////////////////////////////////////////////////////////////////////
584
585namespace details {
586
587 template<size_t SIZE1, size_t... SIZES>
588 struct product {
589 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
590 };
591
592 template<size_t SIZE1>
593 struct product<SIZE1> {
594 static constexpr size_t value = SIZE1;
595 };
596
597 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800598 struct std_array {
599 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
600 };
601
602 template<typename T, size_t SIZE1>
603 struct std_array<T, SIZE1> {
604 using type = std::array<T, SIZE1>;
605 };
606
607 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700608 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800609
610 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
611
Andreas Huber20dce082016-09-22 19:39:13 -0700612 explicit accessor(T *base)
613 : mBase(base) {
614 }
615
616 accessor<T, SIZES...> operator[](size_t index) {
617 return accessor<T, SIZES...>(
618 &mBase[index * product<SIZES...>::value]);
619 }
620
Yifan Hong44ab6232016-11-22 16:28:24 -0800621 accessor &operator=(const std_array_type &other) {
622 for (size_t i = 0; i < SIZE1; ++i) {
623 (*this)[i] = other[i];
624 }
625 return *this;
626 }
627
Andreas Huber20dce082016-09-22 19:39:13 -0700628 private:
629 T *mBase;
630 };
631
632 template<typename T, size_t SIZE1>
633 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800634
635 using std_array_type = typename std_array<T, SIZE1>::type;
636
Andreas Huber20dce082016-09-22 19:39:13 -0700637 explicit accessor(T *base)
638 : mBase(base) {
639 }
640
641 T &operator[](size_t index) {
642 return mBase[index];
643 }
644
Yifan Hong44ab6232016-11-22 16:28:24 -0800645 accessor &operator=(const std_array_type &other) {
646 for (size_t i = 0; i < SIZE1; ++i) {
647 (*this)[i] = other[i];
648 }
649 return *this;
650 }
651
Andreas Huber20dce082016-09-22 19:39:13 -0700652 private:
653 T *mBase;
654 };
655
656 template<typename T, size_t SIZE1, size_t... SIZES>
657 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800658
659 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
660
Andreas Huber20dce082016-09-22 19:39:13 -0700661 explicit const_accessor(const T *base)
662 : mBase(base) {
663 }
664
Yifan Hongbe7a6882017-01-05 17:30:17 -0800665 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700666 return const_accessor<T, SIZES...>(
667 &mBase[index * product<SIZES...>::value]);
668 }
669
Yifan Hong44ab6232016-11-22 16:28:24 -0800670 operator std_array_type() {
671 std_array_type array;
672 for (size_t i = 0; i < SIZE1; ++i) {
673 array[i] = (*this)[i];
674 }
675 return array;
676 }
677
Andreas Huber20dce082016-09-22 19:39:13 -0700678 private:
679 const T *mBase;
680 };
681
682 template<typename T, size_t SIZE1>
683 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800684
685 using std_array_type = typename std_array<T, SIZE1>::type;
686
Andreas Huber20dce082016-09-22 19:39:13 -0700687 explicit const_accessor(const T *base)
688 : mBase(base) {
689 }
690
691 const T &operator[](size_t index) const {
692 return mBase[index];
693 }
694
Yifan Hong44ab6232016-11-22 16:28:24 -0800695 operator std_array_type() {
696 std_array_type array;
697 for (size_t i = 0; i < SIZE1; ++i) {
698 array[i] = (*this)[i];
699 }
700 return array;
701 }
702
Andreas Huber20dce082016-09-22 19:39:13 -0700703 private:
704 const T *mBase;
705 };
706
707} // namespace details
708
709////////////////////////////////////////////////////////////////////////////////
710
Yifan Hong44ab6232016-11-22 16:28:24 -0800711// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700712template<typename T, size_t SIZE1, size_t... SIZES>
713struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800714
715 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
716
Andreas Huber20dce082016-09-22 19:39:13 -0700717 hidl_array() = default;
718
Yifan Hong44ab6232016-11-22 16:28:24 -0800719 // Copies the data from source, using T::operator=(const T &).
720 hidl_array(const T *source) {
721 for (size_t i = 0; i < elementCount(); ++i) {
722 mBuffer[i] = source[i];
723 }
724 }
725
726 // Copies the data from the given std::array, using T::operator=(const T &).
727 hidl_array(const std_array_type &array) {
728 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
729 modifier = array;
730 }
731
Andreas Huber20dce082016-09-22 19:39:13 -0700732 T *data() { return mBuffer; }
733 const T *data() const { return mBuffer; }
734
735 details::accessor<T, SIZES...> operator[](size_t index) {
736 return details::accessor<T, SIZES...>(
737 &mBuffer[index * details::product<SIZES...>::value]);
738 }
739
740 details::const_accessor<T, SIZES...> operator[](size_t index) const {
741 return details::const_accessor<T, SIZES...>(
742 &mBuffer[index * details::product<SIZES...>::value]);
743 }
744
Yifan Hong9fcbb362016-12-20 16:46:41 -0800745 // equality check, assuming that T::operator== is defined.
746 bool operator==(const hidl_array &other) const {
747 for (size_t i = 0; i < elementCount(); ++i) {
748 if (!(mBuffer[i] == other.mBuffer[i])) {
749 return false;
750 }
751 }
752 return true;
753 }
754
755 inline bool operator!=(const hidl_array &other) const {
756 return !((*this) == other);
757 }
758
Andreas Huber00a985c2016-09-28 14:24:53 -0700759 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
760
761 static constexpr size_tuple_type size() {
762 return std::make_tuple(SIZE1, SIZES...);
763 }
764
Yifan Hong44ab6232016-11-22 16:28:24 -0800765 static constexpr size_t elementCount() {
766 return details::product<SIZE1, SIZES...>::value;
767 }
768
769 operator std_array_type() const {
770 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
771 }
772
Andreas Huber20dce082016-09-22 19:39:13 -0700773private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800774 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700775};
776
Yifan Hong44ab6232016-11-22 16:28:24 -0800777// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700778template<typename T, size_t SIZE1>
779struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800780
781 using std_array_type = typename details::std_array<T, SIZE1>::type;
782
Andreas Huber20dce082016-09-22 19:39:13 -0700783 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800784
785 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800786 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800787 for (size_t i = 0; i < elementCount(); ++i) {
788 mBuffer[i] = source[i];
789 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800790 }
Andreas Huber20dce082016-09-22 19:39:13 -0700791
Yifan Hong44ab6232016-11-22 16:28:24 -0800792 // Copies the data from the given std::array, using T::operator=(const T &).
793 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
794
Andreas Huber20dce082016-09-22 19:39:13 -0700795 T *data() { return mBuffer; }
796 const T *data() const { return mBuffer; }
797
798 T &operator[](size_t index) {
799 return mBuffer[index];
800 }
801
802 const T &operator[](size_t index) const {
803 return mBuffer[index];
804 }
805
Yifan Hong9fcbb362016-12-20 16:46:41 -0800806 // equality check, assuming that T::operator== is defined.
807 bool operator==(const hidl_array &other) const {
808 for (size_t i = 0; i < elementCount(); ++i) {
809 if (!(mBuffer[i] == other.mBuffer[i])) {
810 return false;
811 }
812 }
813 return true;
814 }
815
816 inline bool operator!=(const hidl_array &other) const {
817 return !((*this) == other);
818 }
819
Andreas Huber00a985c2016-09-28 14:24:53 -0700820 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800821 static constexpr size_t elementCount() { return SIZE1; }
822
823 // Copies the data to an std::array, using T::operator=(T).
824 operator std_array_type() const {
825 std_array_type array;
826 for (size_t i = 0; i < SIZE1; ++i) {
827 array[i] = mBuffer[i];
828 }
829 return array;
830 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700831
Andreas Huber20dce082016-09-22 19:39:13 -0700832private:
833 T mBuffer[SIZE1];
834};
835
Martijn Coenen72110162016-08-19 14:28:25 +0200836// ----------------------------------------------------------------------
837// Version functions
838struct hidl_version {
839public:
Steven Morelandf2658722019-04-18 12:54:56 -0700840 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {
841 static_assert(sizeof(*this) == 4, "wrong size");
842 }
Martijn Coenen72110162016-08-19 14:28:25 +0200843
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700844 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200845 return (mMajor == other.get_major() && mMinor == other.get_minor());
846 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200847
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800848 bool operator<(const hidl_version& other) const {
849 return (mMajor < other.get_major() ||
850 (mMajor == other.get_major() && mMinor < other.get_minor()));
851 }
852
853 bool operator>(const hidl_version& other) const {
854 return other < *this;
855 }
856
857 bool operator<=(const hidl_version& other) const {
858 return !(*this > other);
859 }
860
861 bool operator>=(const hidl_version& other) const {
862 return !(*this < other);
863 }
864
Martijn Coenen097a7672016-09-08 16:56:41 +0200865 constexpr uint16_t get_major() const { return mMajor; }
866 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200867
Martijn Coenen72110162016-08-19 14:28:25 +0200868private:
869 uint16_t mMajor;
870 uint16_t mMinor;
871};
872
873inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
874 return hidl_version(major,minor);
875}
876
Yifan Hongbe7a6882017-01-05 17:30:17 -0800877///////////////////// toString functions
878
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800879std::string toString(const void *t);
Yifan Hongbe7a6882017-01-05 17:30:17 -0800880
881// toString alias for numeric types
882template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
883inline std::string toString(T t) {
884 return std::to_string(t);
885}
886
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800887namespace details {
888
Yifan Hongbe7a6882017-01-05 17:30:17 -0800889template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
890inline std::string toHexString(T t, bool prefix = true) {
891 std::ostringstream os;
892 if (prefix) { os << std::showbase; }
893 os << std::hex << t;
894 return os.str();
895}
896
897template<>
898inline std::string toHexString(uint8_t t, bool prefix) {
899 return toHexString(static_cast<int32_t>(t), prefix);
900}
901
902template<>
903inline std::string toHexString(int8_t t, bool prefix) {
904 return toHexString(static_cast<int32_t>(t), prefix);
905}
906
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800907template<typename Array>
908std::string arrayToString(const Array &a, size_t size);
909
910template<size_t SIZE1>
911std::string arraySizeToString() {
912 return std::string{"["} + toString(SIZE1) + "]";
913}
914
915template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
916std::string arraySizeToString() {
917 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
918}
919
920template<typename T, size_t SIZE1>
921std::string toString(details::const_accessor<T, SIZE1> a) {
922 return arrayToString(a, SIZE1);
923}
924
925template<typename Array>
926std::string arrayToString(const Array &a, size_t size) {
927 using android::hardware::toString;
928 std::string os;
929 os += "{";
930 for (size_t i = 0; i < size; ++i) {
931 if (i > 0) {
932 os += ", ";
933 }
934 os += toString(a[i]);
935 }
936 os += "}";
937 return os;
938}
939
940template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
941std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
942 return arrayToString(a, SIZE1);
943}
944
945} //namespace details
946
947inline std::string toString(const void *t) {
948 return details::toHexString(reinterpret_cast<uintptr_t>(t));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800949}
950
951// debug string dump. There will be quotes around the string!
952inline std::string toString(const hidl_string &hs) {
953 return std::string{"\""} + hs.c_str() + "\"";
954}
955
956// debug string dump
957inline std::string toString(const hidl_handle &hs) {
958 return toString(hs.getNativeHandle());
959}
960
961inline std::string toString(const hidl_memory &mem) {
962 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
963 + toString(mem.size())
964 + ", .handle = " + toString(mem.handle()) + "}";
965}
966
967inline std::string toString(const sp<hidl_death_recipient> &dr) {
968 return std::string{"death_recipient@"} + toString(dr.get());
969}
970
Yifan Hongbe7a6882017-01-05 17:30:17 -0800971// debug string dump, assuming that toString(T) is defined.
972template<typename T>
973std::string toString(const hidl_vec<T> &a) {
974 std::string os;
975 os += "[" + toString(a.size()) + "]";
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800976 os += details::arrayToString(a, a.size());
Yifan Hongbe7a6882017-01-05 17:30:17 -0800977 return os;
978}
979
Yifan Hongbe7a6882017-01-05 17:30:17 -0800980template<typename T, size_t SIZE1>
981std::string toString(const hidl_array<T, SIZE1> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800982 return details::arraySizeToString<SIZE1>()
983 + details::toString(details::const_accessor<T, SIZE1>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800984}
985
986template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
987std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800988 return details::arraySizeToString<SIZE1, SIZE2, SIZES...>()
989 + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800990}
991
Steven Morelandabce0c72017-11-17 15:37:09 -0800992/**
993 * Every HIDL generated enum generates an implementation of this function.
994 * E.x.: for(const auto v : hidl_enum_iterator<Enum>) { ... }
995 */
996template <typename>
997struct hidl_enum_iterator;
998
Steven Morelanda6752122017-12-28 13:35:20 -0800999/**
1000 * Bitfields in HIDL are the underlying type of the enumeration.
1001 */
1002template <typename Enum>
1003using hidl_bitfield = typename std::underlying_type<Enum>::type;
1004
Martijn Coenen72110162016-08-19 14:28:25 +02001005} // namespace hardware
1006} // namespace android
1007
Martijn Coenenc28f1152016-08-22 14:06:56 +02001008
Martijn Coenen72110162016-08-19 14:28:25 +02001009#endif // ANDROID_HIDL_SUPPORT_H