blob: d1221fe8aafedf8fb5c01b62f885d031c9126893 [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.
Steven Moreland57c6fcb2018-03-06 11:31:33 -0800158 //
159 // size == strlen(data)
Martijn Coenen72110162016-08-19 14:28:25 +0200160 void setToExternal(const char *data, size_t size);
161
Andreas Huberebfeb362016-08-25 13:39:05 -0700162 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
163 static const size_t kOffsetOfBuffer;
164
Martijn Coenen72110162016-08-19 14:28:25 +0200165private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100166 details::hidl_pointer<const char> mBuffer;
167 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700168 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200169
Yifan Hong602b85a2016-10-24 13:40:01 -0700170 // copy from data with size. Assume that my memory is freed
171 // (through clear(), for example)
172 void copyFrom(const char *data, size_t size);
173 // move from another hidl_string
174 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200175};
176
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700177// Use NOLINT to suppress missing parentheses warnings around OP.
Janis Danisevskis62a38df2017-11-22 13:47:33 -0800178#define HIDL_STRING_OPERATOR(OP) \
179 inline bool operator OP(const hidl_string& hs1, const hidl_string& hs2) { \
180 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; /* NOLINT */ \
181 } \
182 inline bool operator OP(const hidl_string& hs, const char* s) { \
183 return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \
184 } \
185 inline bool operator OP(const char* s, const hidl_string& hs) { \
186 return strcmp(s, hs.c_str()) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800187 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800188
Steven Moreland551396a2017-02-13 18:33:20 -0800189HIDL_STRING_OPERATOR(==)
190HIDL_STRING_OPERATOR(!=)
191HIDL_STRING_OPERATOR(<)
192HIDL_STRING_OPERATOR(<=)
193HIDL_STRING_OPERATOR(>)
194HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800195
Steven Moreland551396a2017-02-13 18:33:20 -0800196#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700197
Scott Randolph0c84ab42017-04-03 14:07:14 -0700198// Send our content to the output stream
199std::ostream& operator<<(std::ostream& os, const hidl_string& str);
200
201
Martijn Coenen30791002016-12-01 15:40:46 +0100202// hidl_memory is a structure that can be used to transfer
203// pieces of shared memory between processes. The assumption
204// of this object is that the memory remains accessible as
205// long as the file descriptors in the enclosed mHandle
206// - as well as all of its cross-process dups() - remain opened.
207struct hidl_memory {
208
Martijn Coenen04b91c02017-01-19 14:14:21 +0100209 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100210 }
211
212 /**
Steven Moreland3f8c4162017-10-11 13:47:00 -0700213 * Creates a hidl_memory object whose handle has the same lifetime
214 * as the handle moved into it.
215 */
216 hidl_memory(const hidl_string& name, hidl_handle&& handle, size_t size)
217 : mHandle(std::move(handle)), mSize(size), mName(name) {}
218
219 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100220 * Creates a hidl_memory object, but doesn't take ownership of
221 * the passed in native_handle_t; callers are responsible for
222 * making sure the handle remains valid while this object is
223 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100224 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100225 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
226 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100227 mSize(size),
228 mName(name)
229 {}
230
231 // copy constructor
232 hidl_memory(const hidl_memory& other) {
233 *this = other;
234 }
235
236 // copy assignment
237 hidl_memory &operator=(const hidl_memory &other) {
238 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100239 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100240 mSize = other.mSize;
241 mName = other.mName;
242 }
243
244 return *this;
245 }
246
Hridya Valsaraju01268892017-02-27 08:48:38 -0800247 // move constructor
Martijn Coenene8c36e12017-05-30 16:25:07 -0700248 hidl_memory(hidl_memory&& other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800249 *this = std::move(other);
250 }
251
252 // move assignment
Martijn Coenene8c36e12017-05-30 16:25:07 -0700253 hidl_memory &operator=(hidl_memory &&other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800254 if (this != &other) {
255 mHandle = std::move(other.mHandle);
256 mSize = other.mSize;
257 mName = std::move(other.mName);
258 other.mSize = 0;
259 }
260
261 return *this;
262 }
263
Martijn Coenen30791002016-12-01 15:40:46 +0100264
265 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100266 }
267
268 const native_handle_t* handle() const {
269 return mHandle;
270 }
271
272 const hidl_string &name() const {
273 return mName;
274 }
275
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800276 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100277 return mSize;
278 }
279
Howard Chen9bc35c42017-10-13 14:21:46 +0800280 // @return true if it's valid
281 inline bool valid() const { return handle() != nullptr; }
282
Martijn Coenen30791002016-12-01 15:40:46 +0100283 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
284 static const size_t kOffsetOfHandle;
285 // offsetof(hidl_memory, mName) exposed since mHandle is private.
286 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800287
Martijn Coenen30791002016-12-01 15:40:46 +0100288private:
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800289 hidl_handle mHandle __attribute__ ((aligned(8)));
290 uint64_t mSize __attribute__ ((aligned(8)));
291 hidl_string mName __attribute__ ((aligned(8)));
Martijn Coenen30791002016-12-01 15:40:46 +0100292};
293
Howard Chen9bc35c42017-10-13 14:21:46 +0800294// HidlMemory is a wrapper class to support sp<> for hidl_memory. It also
295// provides factory methods to create an instance from hidl_memory or
296// from a opened file descriptor. The number of factory methods can be increase
297// to support other type of hidl_memory without break the ABI.
298class HidlMemory : public virtual hidl_memory, public virtual ::android::RefBase {
299public:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800300 static sp<HidlMemory> getInstance(const hidl_memory& mem);
301
Howard Chen9bc35c42017-10-13 14:21:46 +0800302 static sp<HidlMemory> getInstance(hidl_memory&& mem);
303
304 static sp<HidlMemory> getInstance(const hidl_string& name, hidl_handle&& handle, uint64_t size);
305 // @param fd, shall be opened and points to the resource.
306 // @note this method takes the ownership of the fd and will close it in
307 // destructor
Howard Chen8e42f5a2017-11-09 13:58:28 +0800308 // @return nullptr in failure with the fd closed
Howard Chen9bc35c42017-10-13 14:21:46 +0800309 static sp<HidlMemory> getInstance(const hidl_string& name, int fd, uint64_t size);
310
Howard Chen280af0a2017-10-20 13:58:36 +0800311 virtual ~HidlMemory();
Howard Chen8e42f5a2017-11-09 13:58:28 +0800312
Howard Chen9bc35c42017-10-13 14:21:46 +0800313protected:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800314 HidlMemory();
315 HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size);
Howard Chen9bc35c42017-10-13 14:21:46 +0800316};
Andreas Huber20dce082016-09-22 19:39:13 -0700317////////////////////////////////////////////////////////////////////////////////
318
Martijn Coenen72110162016-08-19 14:28:25 +0200319template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800320struct hidl_vec {
Martijn Coenen72110162016-08-19 14:28:25 +0200321 hidl_vec()
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700322 : mBuffer(nullptr),
Martijn Coenen72110162016-08-19 14:28:25 +0200323 mSize(0),
324 mOwnsBuffer(true) {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800325 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Martijn Coenen72110162016-08-19 14:28:25 +0200326 }
327
Steven Morelandaa661fc2017-10-06 09:40:18 -0700328 // Note, does not initialize primitive types.
Steven Moreland96e9de02017-09-29 14:11:20 -0700329 hidl_vec(size_t size) : hidl_vec() { resize(size); }
330
Yifan Hong602b85a2016-10-24 13:40:01 -0700331 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200332 *this = other;
333 }
334
Martijn Coenene8c36e12017-05-30 16:25:07 -0700335 hidl_vec(hidl_vec<T> &&other) noexcept
Steven Moreland9fbfe472016-11-14 16:49:17 -0800336 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100337 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700338 }
339
Steven Morelandb69926a2016-11-15 10:02:57 -0800340 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800341 : mOwnsBuffer(true) {
342 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];
347
Steven Morelandb69926a2016-11-15 10:02:57 -0800348 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800349 for (auto it = list.begin(); it != list.end(); ++it) {
350 mBuffer[idx++] = *it;
351 }
352 }
353
Yifan Hong602b85a2016-10-24 13:40:01 -0700354 hidl_vec(const std::vector<T> &other) : hidl_vec() {
355 *this = other;
356 }
357
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700358 template <typename InputIterator,
359 typename = typename std::enable_if<std::is_convertible<
360 typename std::iterator_traits<InputIterator>::iterator_category,
361 std::input_iterator_tag>::value>::type>
362 hidl_vec(InputIterator first, InputIterator last) : mOwnsBuffer(true) {
363 auto size = std::distance(first, last);
364 if (size > static_cast<int64_t>(UINT32_MAX)) {
365 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
366 }
367 if (size < 0) {
368 details::logAlwaysFatal("size can't be negative.");
369 }
370 mSize = static_cast<uint32_t>(size);
371 mBuffer = new T[mSize];
372
373 size_t idx = 0;
374 for (; first != last; ++first) {
375 mBuffer[idx++] = static_cast<T>(*first);
376 }
377 }
378
Martijn Coenen72110162016-08-19 14:28:25 +0200379 ~hidl_vec() {
380 if (mOwnsBuffer) {
381 delete[] mBuffer;
382 }
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700383 mBuffer = nullptr;
Martijn Coenen72110162016-08-19 14:28:25 +0200384 }
385
Alexey Polyudove2299012016-10-19 09:52:00 -0700386 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200387 // caller's responsibility to ensure that the underlying memory stays
388 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700389 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200390 if (mOwnsBuffer) {
391 delete [] mBuffer;
392 }
393 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100394 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800395 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100396 }
397 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700398 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200399 }
400
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700401 T *data() {
402 return mBuffer;
403 }
404
405 const T *data() const {
406 return mBuffer;
407 }
408
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700409 T *releaseData() {
410 if (!mOwnsBuffer && mSize > 0) {
411 resize(mSize);
412 }
413 mOwnsBuffer = false;
414 return mBuffer;
415 }
416
Martijn Coenene8c36e12017-05-30 16:25:07 -0700417 hidl_vec &operator=(hidl_vec &&other) noexcept {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100418 if (mOwnsBuffer) {
419 delete[] mBuffer;
420 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700421 mBuffer = other.mBuffer;
422 mSize = other.mSize;
423 mOwnsBuffer = other.mOwnsBuffer;
424 other.mOwnsBuffer = false;
425 return *this;
426 }
427
Martijn Coenen72110162016-08-19 14:28:25 +0200428 hidl_vec &operator=(const hidl_vec &other) {
429 if (this != &other) {
430 if (mOwnsBuffer) {
431 delete[] mBuffer;
432 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700433 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200434 }
435
436 return *this;
437 }
438
Yifan Hong602b85a2016-10-24 13:40:01 -0700439 // copy from an std::vector.
440 hidl_vec &operator=(const std::vector<T> &other) {
441 if (mOwnsBuffer) {
442 delete[] mBuffer;
443 }
444 copyFrom(other, other.size());
445 return *this;
446 }
447
448 // cast to an std::vector.
449 operator std::vector<T>() const {
450 std::vector<T> v(mSize);
451 for (size_t i = 0; i < mSize; ++i) {
452 v[i] = mBuffer[i];
453 }
454 return v;
455 }
456
Yifan Hong9fcbb362016-12-20 16:46:41 -0800457 // equality check, assuming that T::operator== is defined.
458 bool operator==(const hidl_vec &other) const {
459 if (mSize != other.size()) {
460 return false;
461 }
462 for (size_t i = 0; i < mSize; ++i) {
463 if (!(mBuffer[i] == other.mBuffer[i])) {
464 return false;
465 }
466 }
467 return true;
468 }
469
470 // inequality check, assuming that T::operator== is defined.
471 inline bool operator!=(const hidl_vec &other) const {
472 return !((*this) == other);
473 }
474
Martijn Coenen72110162016-08-19 14:28:25 +0200475 size_t size() const {
476 return mSize;
477 }
478
479 T &operator[](size_t index) {
480 return mBuffer[index];
481 }
482
483 const T &operator[](size_t index) const {
484 return mBuffer[index];
485 }
486
Steven Morelandaa661fc2017-10-06 09:40:18 -0700487 // Does not initialize primitive types if new size > old size.
Martijn Coenen72110162016-08-19 14:28:25 +0200488 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100489 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800490 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100491 }
Martijn Coenen72110162016-08-19 14:28:25 +0200492 T *newBuffer = new T[size];
493
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100494 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200495 newBuffer[i] = mBuffer[i];
496 }
497
498 if (mOwnsBuffer) {
499 delete[] mBuffer;
500 }
501 mBuffer = newBuffer;
502
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100503 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200504 mOwnsBuffer = true;
505 }
506
Yifan Hong089ae132016-11-11 11:32:52 -0800507 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
508 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800509
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800510private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800511 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800512 template<bool is_const>
513 class iter : public std::iterator<
514 std::random_access_iterator_tag, /* Category */
515 T,
516 ptrdiff_t, /* Distance */
517 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
518 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800519 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800520 using traits = std::iterator_traits<iter>;
521 using ptr_type = typename traits::pointer;
522 using ref_type = typename traits::reference;
523 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800524 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800525 iter(ptr_type ptr) : mPtr(ptr) { }
526 inline iter &operator++() { mPtr++; return *this; }
527 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
528 inline iter &operator--() { mPtr--; return *this; }
529 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
530 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
531 inline iter operator+(diff_type n) const { return mPtr + n; }
532 inline iter operator-(diff_type n) const { return mPtr - n; }
533 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
534 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
535 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
536 inline ref_type operator*() const { return *mPtr; }
537 inline ptr_type operator->() const { return mPtr; }
538 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
539 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.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 ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800545 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800546 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800547 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800548public:
549 using iterator = iter<false /* is_const */>;
550 using const_iterator = iter<true /* is_const */>;
551
Scott Randolphbb840f72016-11-21 14:39:26 -0800552 iterator begin() { return data(); }
553 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800554 const_iterator begin() const { return data(); }
555 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800556
Martijn Coenen72110162016-08-19 14:28:25 +0200557private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100558 details::hidl_pointer<T> mBuffer;
559 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200560 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700561
562 // copy from an array-like object, assuming my resources are freed.
563 template <typename Array>
564 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800565 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700566 mOwnsBuffer = true;
567 if (mSize > 0) {
568 mBuffer = new T[size];
569 for (size_t i = 0; i < size; ++i) {
570 mBuffer[i] = data[i];
571 }
572 } else {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700573 mBuffer = nullptr;
Yifan Hong602b85a2016-10-24 13:40:01 -0700574 }
575 }
Martijn Coenen72110162016-08-19 14:28:25 +0200576};
577
Yifan Hong089ae132016-11-11 11:32:52 -0800578template <typename T>
579const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
580
Andreas Huber20dce082016-09-22 19:39:13 -0700581////////////////////////////////////////////////////////////////////////////////
582
583namespace details {
584
585 template<size_t SIZE1, size_t... SIZES>
586 struct product {
587 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
588 };
589
590 template<size_t SIZE1>
591 struct product<SIZE1> {
592 static constexpr size_t value = SIZE1;
593 };
594
595 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800596 struct std_array {
597 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
598 };
599
600 template<typename T, size_t SIZE1>
601 struct std_array<T, SIZE1> {
602 using type = std::array<T, SIZE1>;
603 };
604
605 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700606 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800607
608 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
609
Andreas Huber20dce082016-09-22 19:39:13 -0700610 explicit accessor(T *base)
611 : mBase(base) {
612 }
613
614 accessor<T, SIZES...> operator[](size_t index) {
615 return accessor<T, SIZES...>(
616 &mBase[index * product<SIZES...>::value]);
617 }
618
Yifan Hong44ab6232016-11-22 16:28:24 -0800619 accessor &operator=(const std_array_type &other) {
620 for (size_t i = 0; i < SIZE1; ++i) {
621 (*this)[i] = other[i];
622 }
623 return *this;
624 }
625
Andreas Huber20dce082016-09-22 19:39:13 -0700626 private:
627 T *mBase;
628 };
629
630 template<typename T, size_t SIZE1>
631 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800632
633 using std_array_type = typename std_array<T, SIZE1>::type;
634
Andreas Huber20dce082016-09-22 19:39:13 -0700635 explicit accessor(T *base)
636 : mBase(base) {
637 }
638
639 T &operator[](size_t index) {
640 return mBase[index];
641 }
642
Yifan Hong44ab6232016-11-22 16:28:24 -0800643 accessor &operator=(const std_array_type &other) {
644 for (size_t i = 0; i < SIZE1; ++i) {
645 (*this)[i] = other[i];
646 }
647 return *this;
648 }
649
Andreas Huber20dce082016-09-22 19:39:13 -0700650 private:
651 T *mBase;
652 };
653
654 template<typename T, size_t SIZE1, size_t... SIZES>
655 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800656
657 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
658
Andreas Huber20dce082016-09-22 19:39:13 -0700659 explicit const_accessor(const T *base)
660 : mBase(base) {
661 }
662
Yifan Hongbe7a6882017-01-05 17:30:17 -0800663 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700664 return const_accessor<T, SIZES...>(
665 &mBase[index * product<SIZES...>::value]);
666 }
667
Yifan Hong44ab6232016-11-22 16:28:24 -0800668 operator std_array_type() {
669 std_array_type array;
670 for (size_t i = 0; i < SIZE1; ++i) {
671 array[i] = (*this)[i];
672 }
673 return array;
674 }
675
Andreas Huber20dce082016-09-22 19:39:13 -0700676 private:
677 const T *mBase;
678 };
679
680 template<typename T, size_t SIZE1>
681 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800682
683 using std_array_type = typename std_array<T, SIZE1>::type;
684
Andreas Huber20dce082016-09-22 19:39:13 -0700685 explicit const_accessor(const T *base)
686 : mBase(base) {
687 }
688
689 const T &operator[](size_t index) const {
690 return mBase[index];
691 }
692
Yifan Hong44ab6232016-11-22 16:28:24 -0800693 operator std_array_type() {
694 std_array_type array;
695 for (size_t i = 0; i < SIZE1; ++i) {
696 array[i] = (*this)[i];
697 }
698 return array;
699 }
700
Andreas Huber20dce082016-09-22 19:39:13 -0700701 private:
702 const T *mBase;
703 };
704
705} // namespace details
706
707////////////////////////////////////////////////////////////////////////////////
708
Yifan Hong44ab6232016-11-22 16:28:24 -0800709// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700710template<typename T, size_t SIZE1, size_t... SIZES>
711struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800712
713 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
714
Andreas Huber20dce082016-09-22 19:39:13 -0700715 hidl_array() = default;
716
Yifan Hong44ab6232016-11-22 16:28:24 -0800717 // Copies the data from source, using T::operator=(const T &).
718 hidl_array(const T *source) {
719 for (size_t i = 0; i < elementCount(); ++i) {
720 mBuffer[i] = source[i];
721 }
722 }
723
724 // Copies the data from the given std::array, using T::operator=(const T &).
725 hidl_array(const std_array_type &array) {
726 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
727 modifier = array;
728 }
729
Andreas Huber20dce082016-09-22 19:39:13 -0700730 T *data() { return mBuffer; }
731 const T *data() const { return mBuffer; }
732
733 details::accessor<T, SIZES...> operator[](size_t index) {
734 return details::accessor<T, SIZES...>(
735 &mBuffer[index * details::product<SIZES...>::value]);
736 }
737
738 details::const_accessor<T, SIZES...> operator[](size_t index) const {
739 return details::const_accessor<T, SIZES...>(
740 &mBuffer[index * details::product<SIZES...>::value]);
741 }
742
Yifan Hong9fcbb362016-12-20 16:46:41 -0800743 // equality check, assuming that T::operator== is defined.
744 bool operator==(const hidl_array &other) const {
745 for (size_t i = 0; i < elementCount(); ++i) {
746 if (!(mBuffer[i] == other.mBuffer[i])) {
747 return false;
748 }
749 }
750 return true;
751 }
752
753 inline bool operator!=(const hidl_array &other) const {
754 return !((*this) == other);
755 }
756
Andreas Huber00a985c2016-09-28 14:24:53 -0700757 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
758
759 static constexpr size_tuple_type size() {
760 return std::make_tuple(SIZE1, SIZES...);
761 }
762
Yifan Hong44ab6232016-11-22 16:28:24 -0800763 static constexpr size_t elementCount() {
764 return details::product<SIZE1, SIZES...>::value;
765 }
766
767 operator std_array_type() const {
768 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
769 }
770
Andreas Huber20dce082016-09-22 19:39:13 -0700771private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800772 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700773};
774
Yifan Hong44ab6232016-11-22 16:28:24 -0800775// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700776template<typename T, size_t SIZE1>
777struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800778
779 using std_array_type = typename details::std_array<T, SIZE1>::type;
780
Andreas Huber20dce082016-09-22 19:39:13 -0700781 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800782
783 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800784 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800785 for (size_t i = 0; i < elementCount(); ++i) {
786 mBuffer[i] = source[i];
787 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800788 }
Andreas Huber20dce082016-09-22 19:39:13 -0700789
Yifan Hong44ab6232016-11-22 16:28:24 -0800790 // Copies the data from the given std::array, using T::operator=(const T &).
791 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
792
Andreas Huber20dce082016-09-22 19:39:13 -0700793 T *data() { return mBuffer; }
794 const T *data() const { return mBuffer; }
795
796 T &operator[](size_t index) {
797 return mBuffer[index];
798 }
799
800 const T &operator[](size_t index) const {
801 return mBuffer[index];
802 }
803
Yifan Hong9fcbb362016-12-20 16:46:41 -0800804 // equality check, assuming that T::operator== is defined.
805 bool operator==(const hidl_array &other) const {
806 for (size_t i = 0; i < elementCount(); ++i) {
807 if (!(mBuffer[i] == other.mBuffer[i])) {
808 return false;
809 }
810 }
811 return true;
812 }
813
814 inline bool operator!=(const hidl_array &other) const {
815 return !((*this) == other);
816 }
817
Andreas Huber00a985c2016-09-28 14:24:53 -0700818 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800819 static constexpr size_t elementCount() { return SIZE1; }
820
821 // Copies the data to an std::array, using T::operator=(T).
822 operator std_array_type() const {
823 std_array_type array;
824 for (size_t i = 0; i < SIZE1; ++i) {
825 array[i] = mBuffer[i];
826 }
827 return array;
828 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700829
Andreas Huber20dce082016-09-22 19:39:13 -0700830private:
831 T mBuffer[SIZE1];
832};
833
Martijn Coenen72110162016-08-19 14:28:25 +0200834// ----------------------------------------------------------------------
835// Version functions
836struct hidl_version {
837public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800838 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200839
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700840 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200841 return (mMajor == other.get_major() && mMinor == other.get_minor());
842 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200843
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800844 bool operator<(const hidl_version& other) const {
845 return (mMajor < other.get_major() ||
846 (mMajor == other.get_major() && mMinor < other.get_minor()));
847 }
848
849 bool operator>(const hidl_version& other) const {
850 return other < *this;
851 }
852
853 bool operator<=(const hidl_version& other) const {
854 return !(*this > other);
855 }
856
857 bool operator>=(const hidl_version& other) const {
858 return !(*this < other);
859 }
860
Martijn Coenen097a7672016-09-08 16:56:41 +0200861 constexpr uint16_t get_major() const { return mMajor; }
862 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200863
Martijn Coenen72110162016-08-19 14:28:25 +0200864private:
865 uint16_t mMajor;
866 uint16_t mMinor;
867};
868
869inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
870 return hidl_version(major,minor);
871}
872
Yifan Hongbe7a6882017-01-05 17:30:17 -0800873///////////////////// toString functions
874
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800875std::string toString(const void *t);
Yifan Hongbe7a6882017-01-05 17:30:17 -0800876
877// toString alias for numeric types
878template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
879inline std::string toString(T t) {
880 return std::to_string(t);
881}
882
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800883namespace details {
884
Yifan Hongbe7a6882017-01-05 17:30:17 -0800885template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
886inline std::string toHexString(T t, bool prefix = true) {
887 std::ostringstream os;
888 if (prefix) { os << std::showbase; }
889 os << std::hex << t;
890 return os.str();
891}
892
893template<>
894inline std::string toHexString(uint8_t t, bool prefix) {
895 return toHexString(static_cast<int32_t>(t), prefix);
896}
897
898template<>
899inline std::string toHexString(int8_t t, bool prefix) {
900 return toHexString(static_cast<int32_t>(t), prefix);
901}
902
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800903template<typename Array>
904std::string arrayToString(const Array &a, size_t size);
905
906template<size_t SIZE1>
907std::string arraySizeToString() {
908 return std::string{"["} + toString(SIZE1) + "]";
909}
910
911template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
912std::string arraySizeToString() {
913 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
914}
915
916template<typename T, size_t SIZE1>
917std::string toString(details::const_accessor<T, SIZE1> a) {
918 return arrayToString(a, SIZE1);
919}
920
921template<typename Array>
922std::string arrayToString(const Array &a, size_t size) {
923 using android::hardware::toString;
924 std::string os;
925 os += "{";
926 for (size_t i = 0; i < size; ++i) {
927 if (i > 0) {
928 os += ", ";
929 }
930 os += toString(a[i]);
931 }
932 os += "}";
933 return os;
934}
935
936template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
937std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
938 return arrayToString(a, SIZE1);
939}
940
941} //namespace details
942
943inline std::string toString(const void *t) {
944 return details::toHexString(reinterpret_cast<uintptr_t>(t));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800945}
946
947// debug string dump. There will be quotes around the string!
948inline std::string toString(const hidl_string &hs) {
949 return std::string{"\""} + hs.c_str() + "\"";
950}
951
952// debug string dump
953inline std::string toString(const hidl_handle &hs) {
954 return toString(hs.getNativeHandle());
955}
956
957inline std::string toString(const hidl_memory &mem) {
958 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
959 + toString(mem.size())
960 + ", .handle = " + toString(mem.handle()) + "}";
961}
962
963inline std::string toString(const sp<hidl_death_recipient> &dr) {
964 return std::string{"death_recipient@"} + toString(dr.get());
965}
966
Yifan Hongbe7a6882017-01-05 17:30:17 -0800967// debug string dump, assuming that toString(T) is defined.
968template<typename T>
969std::string toString(const hidl_vec<T> &a) {
970 std::string os;
971 os += "[" + toString(a.size()) + "]";
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800972 os += details::arrayToString(a, a.size());
Yifan Hongbe7a6882017-01-05 17:30:17 -0800973 return os;
974}
975
Yifan Hongbe7a6882017-01-05 17:30:17 -0800976template<typename T, size_t SIZE1>
977std::string toString(const hidl_array<T, SIZE1> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800978 return details::arraySizeToString<SIZE1>()
979 + details::toString(details::const_accessor<T, SIZE1>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800980}
981
982template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
983std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800984 return details::arraySizeToString<SIZE1, SIZE2, SIZES...>()
985 + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800986}
987
Steven Morelandabce0c72017-11-17 15:37:09 -0800988/**
989 * Every HIDL generated enum generates an implementation of this function.
990 * E.x.: for(const auto v : hidl_enum_iterator<Enum>) { ... }
991 */
992template <typename>
993struct hidl_enum_iterator;
994
Steven Morelanda6752122017-12-28 13:35:20 -0800995/**
996 * Bitfields in HIDL are the underlying type of the enumeration.
997 */
998template <typename Enum>
999using hidl_bitfield = typename std::underlying_type<Enum>::type;
1000
Martijn Coenen72110162016-08-19 14:28:25 +02001001} // namespace hardware
1002} // namespace android
1003
Martijn Coenenc28f1152016-08-22 14:06:56 +02001004
Martijn Coenen72110162016-08-19 14:28:25 +02001005#endif // ANDROID_HIDL_SUPPORT_H