blob: 8c939984127f498d239338a2886341057cd6ea01 [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 {
Pirama Arumuga Nainared4c5d02018-04-10 15:46:23 -070043
44struct IMemory;
45
46} // namespace V1_0
47} // namespace memory
48} // namespace hidl
Martijn Coenen30791002016-12-01 15:40:46 +010049
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010050namespace hidl {
51namespace base {
52namespace V1_0 {
Pirama Arumuga Nainared4c5d02018-04-10 15:46:23 -070053
54struct IBase;
55
56} // namespace V1_0
57} // namespace base
58} // namespace hidl
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010059
Martijn Coenen72110162016-08-19 14:28:25 +020060namespace hardware {
61
Yifan Hong24332ef2017-03-07 16:22:19 -080062namespace details {
63// Return true on userdebug / eng builds and false on user builds.
64bool debuggable();
65} // namespace details
66
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010067// hidl_death_recipient is a callback interfaced that can be used with
68// linkToDeath() / unlinkToDeath()
69struct hidl_death_recipient : public virtual RefBase {
70 virtual void serviceDied(uint64_t cookie,
71 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
72};
73
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010074// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
75// so that it can safely be transferred between 32-bit and 64-bit processes.
Martijn Coenen04b91c02017-01-19 14:14:21 +010076// The ownership semantics for this are:
77// 1) The conversion constructor and assignment operator taking a const native_handle_t*
78// do not take ownership of the handle; this is because these operations are usually
79// just done for IPC, and cloning by default is a waste of resources. If you want
80// a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/);
81// 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership;
82// that is because it's not intuitive that this class encapsulates a native_handle_t
83// which needs cloning to be valid; in particular, this allows constructs like this:
84// hidl_handle copy;
85// foo->someHidlCall([&](auto incoming_handle) {
86// copy = incoming_handle;
87// });
88// // copy and its enclosed file descriptors will remain valid here.
89// 3) The move constructor does what you would expect; it only owns the handle if the
90// original did.
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010091struct hidl_handle {
Martijn Coenen04b91c02017-01-19 14:14:21 +010092 hidl_handle();
93 ~hidl_handle();
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010094
Martijn Coenen04b91c02017-01-19 14:14:21 +010095 hidl_handle(const native_handle_t *handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010096
Martijn Coenen04b91c02017-01-19 14:14:21 +010097 // copy constructor.
98 hidl_handle(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010099
100 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700101 hidl_handle(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100102
Steven Moreland6ffdc2a2017-01-23 12:34:33 -0800103 // assignment operators
Martijn Coenen04b91c02017-01-19 14:14:21 +0100104 hidl_handle &operator=(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100105
Martijn Coenen04b91c02017-01-19 14:14:21 +0100106 hidl_handle &operator=(const native_handle_t *native_handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100107
Martijn Coenene8c36e12017-05-30 16:25:07 -0700108 hidl_handle &operator=(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100109
Martijn Coenen04b91c02017-01-19 14:14:21 +0100110 void setTo(native_handle_t* handle, bool shouldOwn = false);
111
112 const native_handle_t* operator->() const;
113
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100114 // implicit conversion to const native_handle_t*
Martijn Coenen04b91c02017-01-19 14:14:21 +0100115 operator const native_handle_t *() const;
116
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100117 // explicit conversion
Martijn Coenen04b91c02017-01-19 14:14:21 +0100118 const native_handle_t *getNativeHandle() const;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100119private:
Martijn Coenen04b91c02017-01-19 14:14:21 +0100120 void freeHandle();
121
Andreas Huber6688d602017-03-30 10:58:29 -0700122 details::hidl_pointer<const native_handle_t> mHandle __attribute__ ((aligned(8)));
123 bool mOwnsHandle __attribute ((aligned(8)));
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100124};
125
Martijn Coenen72110162016-08-19 14:28:25 +0200126struct hidl_string {
127 hidl_string();
128 ~hidl_string();
129
Yifan Hong602b85a2016-10-24 13:40:01 -0700130 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200131 hidl_string(const hidl_string &);
Steven Morelanda21d84f2017-02-16 09:23:45 -0800132 // copy from a C-style string. nullptr will create an empty string
Steven Morelande03c0872016-10-24 10:43:50 -0700133 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800134 // copy the first length characters from a C-style string.
135 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700136 // copy from an std::string.
137 hidl_string(const std::string &);
138
139 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700140 hidl_string(hidl_string &&) noexcept;
Martijn Coenen72110162016-08-19 14:28:25 +0200141
142 const char *c_str() const;
143 size_t size() const;
144 bool empty() const;
145
Yifan Hong602b85a2016-10-24 13:40:01 -0700146 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700147 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700148 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200149 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700150 // copy from an std::string.
151 hidl_string &operator=(const std::string &);
152 // move assignment operator.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700153 hidl_string &operator=(hidl_string &&other) noexcept;
Yifan Hong602b85a2016-10-24 13:40:01 -0700154 // cast to std::string.
155 operator std::string() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700156
Martijn Coenen72110162016-08-19 14:28:25 +0200157 void clear();
158
159 // Reference an external char array. Ownership is _not_ transferred.
160 // Caller is responsible for ensuring that underlying memory is valid
161 // for the lifetime of this hidl_string.
Steven Moreland57c6fcb2018-03-06 11:31:33 -0800162 //
163 // size == strlen(data)
Martijn Coenen72110162016-08-19 14:28:25 +0200164 void setToExternal(const char *data, size_t size);
165
Andreas Huberebfeb362016-08-25 13:39:05 -0700166 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
167 static const size_t kOffsetOfBuffer;
168
Martijn Coenen72110162016-08-19 14:28:25 +0200169private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100170 details::hidl_pointer<const char> mBuffer;
171 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700172 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200173
Yifan Hong602b85a2016-10-24 13:40:01 -0700174 // copy from data with size. Assume that my memory is freed
175 // (through clear(), for example)
176 void copyFrom(const char *data, size_t size);
177 // move from another hidl_string
178 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200179};
180
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700181// Use NOLINT to suppress missing parentheses warnings around OP.
Janis Danisevskis62a38df2017-11-22 13:47:33 -0800182#define HIDL_STRING_OPERATOR(OP) \
183 inline bool operator OP(const hidl_string& hs1, const hidl_string& hs2) { \
184 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; /* NOLINT */ \
185 } \
186 inline bool operator OP(const hidl_string& hs, const char* s) { \
187 return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \
188 } \
189 inline bool operator OP(const char* s, const hidl_string& hs) { \
190 return strcmp(s, hs.c_str()) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800191 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800192
Steven Moreland551396a2017-02-13 18:33:20 -0800193HIDL_STRING_OPERATOR(==)
194HIDL_STRING_OPERATOR(!=)
195HIDL_STRING_OPERATOR(<)
196HIDL_STRING_OPERATOR(<=)
197HIDL_STRING_OPERATOR(>)
198HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800199
Steven Moreland551396a2017-02-13 18:33:20 -0800200#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700201
Scott Randolph0c84ab42017-04-03 14:07:14 -0700202// Send our content to the output stream
203std::ostream& operator<<(std::ostream& os, const hidl_string& str);
204
205
Martijn Coenen30791002016-12-01 15:40:46 +0100206// hidl_memory is a structure that can be used to transfer
207// pieces of shared memory between processes. The assumption
208// of this object is that the memory remains accessible as
209// long as the file descriptors in the enclosed mHandle
210// - as well as all of its cross-process dups() - remain opened.
211struct hidl_memory {
212
Martijn Coenen04b91c02017-01-19 14:14:21 +0100213 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100214 }
215
216 /**
Steven Moreland3f8c4162017-10-11 13:47:00 -0700217 * Creates a hidl_memory object whose handle has the same lifetime
218 * as the handle moved into it.
219 */
220 hidl_memory(const hidl_string& name, hidl_handle&& handle, size_t size)
221 : mHandle(std::move(handle)), mSize(size), mName(name) {}
222
223 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100224 * Creates a hidl_memory object, but doesn't take ownership of
225 * the passed in native_handle_t; callers are responsible for
226 * making sure the handle remains valid while this object is
227 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100228 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100229 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
230 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100231 mSize(size),
232 mName(name)
233 {}
234
235 // copy constructor
236 hidl_memory(const hidl_memory& other) {
237 *this = other;
238 }
239
240 // copy assignment
241 hidl_memory &operator=(const hidl_memory &other) {
242 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100243 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100244 mSize = other.mSize;
245 mName = other.mName;
246 }
247
248 return *this;
249 }
250
Hridya Valsaraju01268892017-02-27 08:48:38 -0800251 // move constructor
Martijn Coenene8c36e12017-05-30 16:25:07 -0700252 hidl_memory(hidl_memory&& other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800253 *this = std::move(other);
254 }
255
256 // move assignment
Martijn Coenene8c36e12017-05-30 16:25:07 -0700257 hidl_memory &operator=(hidl_memory &&other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800258 if (this != &other) {
259 mHandle = std::move(other.mHandle);
260 mSize = other.mSize;
261 mName = std::move(other.mName);
262 other.mSize = 0;
263 }
264
265 return *this;
266 }
267
Martijn Coenen30791002016-12-01 15:40:46 +0100268
269 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100270 }
271
272 const native_handle_t* handle() const {
273 return mHandle;
274 }
275
276 const hidl_string &name() const {
277 return mName;
278 }
279
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800280 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100281 return mSize;
282 }
283
Howard Chen9bc35c42017-10-13 14:21:46 +0800284 // @return true if it's valid
285 inline bool valid() const { return handle() != nullptr; }
286
Martijn Coenen30791002016-12-01 15:40:46 +0100287 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
288 static const size_t kOffsetOfHandle;
289 // offsetof(hidl_memory, mName) exposed since mHandle is private.
290 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800291
Martijn Coenen30791002016-12-01 15:40:46 +0100292private:
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800293 hidl_handle mHandle __attribute__ ((aligned(8)));
294 uint64_t mSize __attribute__ ((aligned(8)));
295 hidl_string mName __attribute__ ((aligned(8)));
Martijn Coenen30791002016-12-01 15:40:46 +0100296};
297
Howard Chen9bc35c42017-10-13 14:21:46 +0800298// HidlMemory is a wrapper class to support sp<> for hidl_memory. It also
299// provides factory methods to create an instance from hidl_memory or
300// from a opened file descriptor. The number of factory methods can be increase
301// to support other type of hidl_memory without break the ABI.
302class HidlMemory : public virtual hidl_memory, public virtual ::android::RefBase {
303public:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800304 static sp<HidlMemory> getInstance(const hidl_memory& mem);
305
Howard Chen9bc35c42017-10-13 14:21:46 +0800306 static sp<HidlMemory> getInstance(hidl_memory&& mem);
307
308 static sp<HidlMemory> getInstance(const hidl_string& name, hidl_handle&& handle, uint64_t size);
309 // @param fd, shall be opened and points to the resource.
310 // @note this method takes the ownership of the fd and will close it in
311 // destructor
Howard Chen8e42f5a2017-11-09 13:58:28 +0800312 // @return nullptr in failure with the fd closed
Howard Chen9bc35c42017-10-13 14:21:46 +0800313 static sp<HidlMemory> getInstance(const hidl_string& name, int fd, uint64_t size);
314
Howard Chen280af0a2017-10-20 13:58:36 +0800315 virtual ~HidlMemory();
Howard Chen8e42f5a2017-11-09 13:58:28 +0800316
Howard Chen9bc35c42017-10-13 14:21:46 +0800317protected:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800318 HidlMemory();
319 HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size);
Howard Chen9bc35c42017-10-13 14:21:46 +0800320};
Andreas Huber20dce082016-09-22 19:39:13 -0700321////////////////////////////////////////////////////////////////////////////////
322
Martijn Coenen72110162016-08-19 14:28:25 +0200323template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800324struct hidl_vec {
Martijn Coenen72110162016-08-19 14:28:25 +0200325 hidl_vec()
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700326 : mBuffer(nullptr),
Martijn Coenen72110162016-08-19 14:28:25 +0200327 mSize(0),
328 mOwnsBuffer(true) {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800329 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Martijn Coenen72110162016-08-19 14:28:25 +0200330 }
331
Steven Morelandaa661fc2017-10-06 09:40:18 -0700332 // Note, does not initialize primitive types.
Steven Moreland96e9de02017-09-29 14:11:20 -0700333 hidl_vec(size_t size) : hidl_vec() { resize(size); }
334
Yifan Hong602b85a2016-10-24 13:40:01 -0700335 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200336 *this = other;
337 }
338
Martijn Coenene8c36e12017-05-30 16:25:07 -0700339 hidl_vec(hidl_vec<T> &&other) noexcept
Steven Moreland9fbfe472016-11-14 16:49:17 -0800340 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100341 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700342 }
343
Steven Morelandb69926a2016-11-15 10:02:57 -0800344 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800345 : mOwnsBuffer(true) {
346 if (list.size() > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800347 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800348 }
349 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800350 mBuffer = new T[mSize];
351
Steven Morelandb69926a2016-11-15 10:02:57 -0800352 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800353 for (auto it = list.begin(); it != list.end(); ++it) {
354 mBuffer[idx++] = *it;
355 }
356 }
357
Yifan Hong602b85a2016-10-24 13:40:01 -0700358 hidl_vec(const std::vector<T> &other) : hidl_vec() {
359 *this = other;
360 }
361
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700362 template <typename InputIterator,
363 typename = typename std::enable_if<std::is_convertible<
364 typename std::iterator_traits<InputIterator>::iterator_category,
365 std::input_iterator_tag>::value>::type>
366 hidl_vec(InputIterator first, InputIterator last) : mOwnsBuffer(true) {
367 auto size = std::distance(first, last);
368 if (size > static_cast<int64_t>(UINT32_MAX)) {
369 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
370 }
371 if (size < 0) {
372 details::logAlwaysFatal("size can't be negative.");
373 }
374 mSize = static_cast<uint32_t>(size);
375 mBuffer = new T[mSize];
376
377 size_t idx = 0;
378 for (; first != last; ++first) {
379 mBuffer[idx++] = static_cast<T>(*first);
380 }
381 }
382
Martijn Coenen72110162016-08-19 14:28:25 +0200383 ~hidl_vec() {
384 if (mOwnsBuffer) {
385 delete[] mBuffer;
386 }
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700387 mBuffer = nullptr;
Martijn Coenen72110162016-08-19 14:28:25 +0200388 }
389
Alexey Polyudove2299012016-10-19 09:52:00 -0700390 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200391 // caller's responsibility to ensure that the underlying memory stays
392 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700393 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200394 if (mOwnsBuffer) {
395 delete [] mBuffer;
396 }
397 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100398 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800399 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100400 }
401 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700402 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200403 }
404
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700405 T *data() {
406 return mBuffer;
407 }
408
409 const T *data() const {
410 return mBuffer;
411 }
412
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700413 T *releaseData() {
414 if (!mOwnsBuffer && mSize > 0) {
415 resize(mSize);
416 }
417 mOwnsBuffer = false;
418 return mBuffer;
419 }
420
Martijn Coenene8c36e12017-05-30 16:25:07 -0700421 hidl_vec &operator=(hidl_vec &&other) noexcept {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100422 if (mOwnsBuffer) {
423 delete[] mBuffer;
424 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700425 mBuffer = other.mBuffer;
426 mSize = other.mSize;
427 mOwnsBuffer = other.mOwnsBuffer;
428 other.mOwnsBuffer = false;
429 return *this;
430 }
431
Martijn Coenen72110162016-08-19 14:28:25 +0200432 hidl_vec &operator=(const hidl_vec &other) {
433 if (this != &other) {
434 if (mOwnsBuffer) {
435 delete[] mBuffer;
436 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700437 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200438 }
439
440 return *this;
441 }
442
Yifan Hong602b85a2016-10-24 13:40:01 -0700443 // copy from an std::vector.
444 hidl_vec &operator=(const std::vector<T> &other) {
445 if (mOwnsBuffer) {
446 delete[] mBuffer;
447 }
448 copyFrom(other, other.size());
449 return *this;
450 }
451
452 // cast to an std::vector.
453 operator std::vector<T>() const {
454 std::vector<T> v(mSize);
455 for (size_t i = 0; i < mSize; ++i) {
456 v[i] = mBuffer[i];
457 }
458 return v;
459 }
460
Yifan Hong9fcbb362016-12-20 16:46:41 -0800461 // equality check, assuming that T::operator== is defined.
462 bool operator==(const hidl_vec &other) const {
463 if (mSize != other.size()) {
464 return false;
465 }
466 for (size_t i = 0; i < mSize; ++i) {
467 if (!(mBuffer[i] == other.mBuffer[i])) {
468 return false;
469 }
470 }
471 return true;
472 }
473
474 // inequality check, assuming that T::operator== is defined.
475 inline bool operator!=(const hidl_vec &other) const {
476 return !((*this) == other);
477 }
478
Martijn Coenen72110162016-08-19 14:28:25 +0200479 size_t size() const {
480 return mSize;
481 }
482
483 T &operator[](size_t index) {
484 return mBuffer[index];
485 }
486
487 const T &operator[](size_t index) const {
488 return mBuffer[index];
489 }
490
Steven Morelandaa661fc2017-10-06 09:40:18 -0700491 // Does not initialize primitive types if new size > old size.
Martijn Coenen72110162016-08-19 14:28:25 +0200492 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100493 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800494 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100495 }
Martijn Coenen72110162016-08-19 14:28:25 +0200496 T *newBuffer = new T[size];
497
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100498 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200499 newBuffer[i] = mBuffer[i];
500 }
501
502 if (mOwnsBuffer) {
503 delete[] mBuffer;
504 }
505 mBuffer = newBuffer;
506
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100507 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200508 mOwnsBuffer = true;
509 }
510
Yifan Hong089ae132016-11-11 11:32:52 -0800511 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
512 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800513
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800514private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800515 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800516 template<bool is_const>
517 class iter : public std::iterator<
518 std::random_access_iterator_tag, /* Category */
519 T,
520 ptrdiff_t, /* Distance */
521 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
522 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800523 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800524 using traits = std::iterator_traits<iter>;
525 using ptr_type = typename traits::pointer;
526 using ref_type = typename traits::reference;
527 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800528 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800529 iter(ptr_type ptr) : mPtr(ptr) { }
530 inline iter &operator++() { mPtr++; return *this; }
531 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
532 inline iter &operator--() { mPtr--; return *this; }
533 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
534 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
535 inline iter operator+(diff_type n) const { return mPtr + n; }
536 inline iter operator-(diff_type n) const { return mPtr - n; }
537 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
538 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
539 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
540 inline ref_type operator*() const { return *mPtr; }
541 inline ptr_type operator->() const { return 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 bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
547 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
548 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800549 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800550 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800551 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800552public:
553 using iterator = iter<false /* is_const */>;
554 using const_iterator = iter<true /* is_const */>;
555
Scott Randolphbb840f72016-11-21 14:39:26 -0800556 iterator begin() { return data(); }
557 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800558 const_iterator begin() const { return data(); }
559 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800560
Martijn Coenen72110162016-08-19 14:28:25 +0200561private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100562 details::hidl_pointer<T> mBuffer;
563 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200564 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700565
566 // copy from an array-like object, assuming my resources are freed.
567 template <typename Array>
568 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800569 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700570 mOwnsBuffer = true;
571 if (mSize > 0) {
572 mBuffer = new T[size];
573 for (size_t i = 0; i < size; ++i) {
574 mBuffer[i] = data[i];
575 }
576 } else {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700577 mBuffer = nullptr;
Yifan Hong602b85a2016-10-24 13:40:01 -0700578 }
579 }
Martijn Coenen72110162016-08-19 14:28:25 +0200580};
581
Yifan Hong089ae132016-11-11 11:32:52 -0800582template <typename T>
583const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
584
Andreas Huber20dce082016-09-22 19:39:13 -0700585////////////////////////////////////////////////////////////////////////////////
586
587namespace details {
588
589 template<size_t SIZE1, size_t... SIZES>
590 struct product {
591 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
592 };
593
594 template<size_t SIZE1>
595 struct product<SIZE1> {
596 static constexpr size_t value = SIZE1;
597 };
598
599 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800600 struct std_array {
601 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
602 };
603
604 template<typename T, size_t SIZE1>
605 struct std_array<T, SIZE1> {
606 using type = std::array<T, SIZE1>;
607 };
608
609 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700610 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800611
612 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
613
Andreas Huber20dce082016-09-22 19:39:13 -0700614 explicit accessor(T *base)
615 : mBase(base) {
616 }
617
618 accessor<T, SIZES...> operator[](size_t index) {
619 return accessor<T, SIZES...>(
620 &mBase[index * product<SIZES...>::value]);
621 }
622
Yifan Hong44ab6232016-11-22 16:28:24 -0800623 accessor &operator=(const std_array_type &other) {
624 for (size_t i = 0; i < SIZE1; ++i) {
625 (*this)[i] = other[i];
626 }
627 return *this;
628 }
629
Andreas Huber20dce082016-09-22 19:39:13 -0700630 private:
631 T *mBase;
632 };
633
634 template<typename T, size_t SIZE1>
635 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800636
637 using std_array_type = typename std_array<T, SIZE1>::type;
638
Andreas Huber20dce082016-09-22 19:39:13 -0700639 explicit accessor(T *base)
640 : mBase(base) {
641 }
642
643 T &operator[](size_t index) {
644 return mBase[index];
645 }
646
Yifan Hong44ab6232016-11-22 16:28:24 -0800647 accessor &operator=(const std_array_type &other) {
648 for (size_t i = 0; i < SIZE1; ++i) {
649 (*this)[i] = other[i];
650 }
651 return *this;
652 }
653
Andreas Huber20dce082016-09-22 19:39:13 -0700654 private:
655 T *mBase;
656 };
657
658 template<typename T, size_t SIZE1, size_t... SIZES>
659 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800660
661 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
662
Andreas Huber20dce082016-09-22 19:39:13 -0700663 explicit const_accessor(const T *base)
664 : mBase(base) {
665 }
666
Yifan Hongbe7a6882017-01-05 17:30:17 -0800667 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700668 return const_accessor<T, SIZES...>(
669 &mBase[index * product<SIZES...>::value]);
670 }
671
Yifan Hong44ab6232016-11-22 16:28:24 -0800672 operator std_array_type() {
673 std_array_type array;
674 for (size_t i = 0; i < SIZE1; ++i) {
675 array[i] = (*this)[i];
676 }
677 return array;
678 }
679
Andreas Huber20dce082016-09-22 19:39:13 -0700680 private:
681 const T *mBase;
682 };
683
684 template<typename T, size_t SIZE1>
685 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800686
687 using std_array_type = typename std_array<T, SIZE1>::type;
688
Andreas Huber20dce082016-09-22 19:39:13 -0700689 explicit const_accessor(const T *base)
690 : mBase(base) {
691 }
692
693 const T &operator[](size_t index) const {
694 return mBase[index];
695 }
696
Yifan Hong44ab6232016-11-22 16:28:24 -0800697 operator std_array_type() {
698 std_array_type array;
699 for (size_t i = 0; i < SIZE1; ++i) {
700 array[i] = (*this)[i];
701 }
702 return array;
703 }
704
Andreas Huber20dce082016-09-22 19:39:13 -0700705 private:
706 const T *mBase;
707 };
708
709} // namespace details
710
711////////////////////////////////////////////////////////////////////////////////
712
Yifan Hong44ab6232016-11-22 16:28:24 -0800713// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700714template<typename T, size_t SIZE1, size_t... SIZES>
715struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800716
717 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
718
Andreas Huber20dce082016-09-22 19:39:13 -0700719 hidl_array() = default;
720
Yifan Hong44ab6232016-11-22 16:28:24 -0800721 // Copies the data from source, using T::operator=(const T &).
722 hidl_array(const T *source) {
723 for (size_t i = 0; i < elementCount(); ++i) {
724 mBuffer[i] = source[i];
725 }
726 }
727
728 // Copies the data from the given std::array, using T::operator=(const T &).
729 hidl_array(const std_array_type &array) {
730 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
731 modifier = array;
732 }
733
Andreas Huber20dce082016-09-22 19:39:13 -0700734 T *data() { return mBuffer; }
735 const T *data() const { return mBuffer; }
736
737 details::accessor<T, SIZES...> operator[](size_t index) {
738 return details::accessor<T, SIZES...>(
739 &mBuffer[index * details::product<SIZES...>::value]);
740 }
741
742 details::const_accessor<T, SIZES...> operator[](size_t index) const {
743 return details::const_accessor<T, SIZES...>(
744 &mBuffer[index * details::product<SIZES...>::value]);
745 }
746
Yifan Hong9fcbb362016-12-20 16:46:41 -0800747 // equality check, assuming that T::operator== is defined.
748 bool operator==(const hidl_array &other) const {
749 for (size_t i = 0; i < elementCount(); ++i) {
750 if (!(mBuffer[i] == other.mBuffer[i])) {
751 return false;
752 }
753 }
754 return true;
755 }
756
757 inline bool operator!=(const hidl_array &other) const {
758 return !((*this) == other);
759 }
760
Andreas Huber00a985c2016-09-28 14:24:53 -0700761 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
762
763 static constexpr size_tuple_type size() {
764 return std::make_tuple(SIZE1, SIZES...);
765 }
766
Yifan Hong44ab6232016-11-22 16:28:24 -0800767 static constexpr size_t elementCount() {
768 return details::product<SIZE1, SIZES...>::value;
769 }
770
771 operator std_array_type() const {
772 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
773 }
774
Andreas Huber20dce082016-09-22 19:39:13 -0700775private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800776 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700777};
778
Yifan Hong44ab6232016-11-22 16:28:24 -0800779// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700780template<typename T, size_t SIZE1>
781struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800782
783 using std_array_type = typename details::std_array<T, SIZE1>::type;
784
Andreas Huber20dce082016-09-22 19:39:13 -0700785 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800786
787 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800788 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800789 for (size_t i = 0; i < elementCount(); ++i) {
790 mBuffer[i] = source[i];
791 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800792 }
Andreas Huber20dce082016-09-22 19:39:13 -0700793
Yifan Hong44ab6232016-11-22 16:28:24 -0800794 // Copies the data from the given std::array, using T::operator=(const T &).
795 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
796
Andreas Huber20dce082016-09-22 19:39:13 -0700797 T *data() { return mBuffer; }
798 const T *data() const { return mBuffer; }
799
800 T &operator[](size_t index) {
801 return mBuffer[index];
802 }
803
804 const T &operator[](size_t index) const {
805 return mBuffer[index];
806 }
807
Yifan Hong9fcbb362016-12-20 16:46:41 -0800808 // equality check, assuming that T::operator== is defined.
809 bool operator==(const hidl_array &other) const {
810 for (size_t i = 0; i < elementCount(); ++i) {
811 if (!(mBuffer[i] == other.mBuffer[i])) {
812 return false;
813 }
814 }
815 return true;
816 }
817
818 inline bool operator!=(const hidl_array &other) const {
819 return !((*this) == other);
820 }
821
Andreas Huber00a985c2016-09-28 14:24:53 -0700822 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800823 static constexpr size_t elementCount() { return SIZE1; }
824
825 // Copies the data to an std::array, using T::operator=(T).
826 operator std_array_type() const {
827 std_array_type array;
828 for (size_t i = 0; i < SIZE1; ++i) {
829 array[i] = mBuffer[i];
830 }
831 return array;
832 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700833
Andreas Huber20dce082016-09-22 19:39:13 -0700834private:
835 T mBuffer[SIZE1];
836};
837
Martijn Coenen72110162016-08-19 14:28:25 +0200838// ----------------------------------------------------------------------
839// Version functions
840struct hidl_version {
841public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800842 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
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.
Steven Moreland5f3a8cf2018-04-25 11:57:35 -0700994 * E.x.: for(const auto v : hidl_enum_range<Enum>) { ... }
Steven Morelandabce0c72017-11-17 15:37:09 -0800995 */
Steven Moreland2f2c9632018-05-01 12:24:14 -0700996template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
Steven Moreland5f3a8cf2018-04-25 11:57:35 -0700997struct hidl_enum_range;
998
Steven Moreland2f2c9632018-05-01 12:24:14 -0700999template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
1000struct hidl_enum_iterator {
1001 static_assert(!std::is_enum<T>::value,
1002 "b/78573628: hidl_enum_iterator was renamed to hidl_enum_range because it is not "
1003 "actually an iterator. Please use that type instead.");
1004};
Steven Morelandabce0c72017-11-17 15:37:09 -08001005
Steven Morelanda6752122017-12-28 13:35:20 -08001006/**
1007 * Bitfields in HIDL are the underlying type of the enumeration.
1008 */
1009template <typename Enum>
1010using hidl_bitfield = typename std::underlying_type<Enum>::type;
1011
Martijn Coenen72110162016-08-19 14:28:25 +02001012} // namespace hardware
1013} // namespace android
1014
Martijn Coenenc28f1152016-08-22 14:06:56 +02001015
Martijn Coenen72110162016-08-19 14:28:25 +02001016#endif // ANDROID_HIDL_SUPPORT_H