blob: da9a1afd91cdea320c45ac088bf22ba930167f39 [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 Coenen04b91c02017-01-19 14:14:21 +010097 hidl_handle(hidl_handle &&other);
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 Coenen04b91c02017-01-19 14:14:21 +0100104 hidl_handle &operator=(hidl_handle &&other);
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 Huber6b9cc692017-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.
136 hidl_string(hidl_string &&);
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.
149 hidl_string &operator=(hidl_string &&other);
150 // cast to std::string.
151 operator std::string() const;
152 // cast to C-style string. Caller is responsible
153 // to maintain this hidl_string alive.
154 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700155
Martijn Coenen72110162016-08-19 14:28:25 +0200156 void clear();
157
158 // Reference an external char array. Ownership is _not_ transferred.
159 // Caller is responsible for ensuring that underlying memory is valid
160 // for the lifetime of this hidl_string.
161 void setToExternal(const char *data, size_t size);
162
Andreas Huberebfeb362016-08-25 13:39:05 -0700163 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
164 static const size_t kOffsetOfBuffer;
165
Martijn Coenen72110162016-08-19 14:28:25 +0200166private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100167 details::hidl_pointer<const char> mBuffer;
168 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700169 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200170
Yifan Hong602b85a2016-10-24 13:40:01 -0700171 // copy from data with size. Assume that my memory is freed
172 // (through clear(), for example)
173 void copyFrom(const char *data, size_t size);
174 // move from another hidl_string
175 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200176};
177
Steven Moreland551396a2017-02-13 18:33:20 -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; \
181 } \
182 inline bool operator OP(const hidl_string &hs, const char *s) { \
183 return strcmp(hs.c_str(), s) OP 0; \
184 } \
185 inline bool operator OP(const char *s, const hidl_string &hs) { \
186 return strcmp(hs.c_str(), s) OP 0; \
187 }
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
Martijn Coenen30791002016-12-01 15:40:46 +0100198// hidl_memory is a structure that can be used to transfer
199// pieces of shared memory between processes. The assumption
200// of this object is that the memory remains accessible as
201// long as the file descriptors in the enclosed mHandle
202// - as well as all of its cross-process dups() - remain opened.
203struct hidl_memory {
204
Martijn Coenen04b91c02017-01-19 14:14:21 +0100205 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100206 }
207
208 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100209 * Creates a hidl_memory object, but doesn't take ownership of
210 * the passed in native_handle_t; callers are responsible for
211 * making sure the handle remains valid while this object is
212 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100213 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100214 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
215 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100216 mSize(size),
217 mName(name)
218 {}
219
220 // copy constructor
221 hidl_memory(const hidl_memory& other) {
222 *this = other;
223 }
224
225 // copy assignment
226 hidl_memory &operator=(const hidl_memory &other) {
227 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100228 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100229 mSize = other.mSize;
230 mName = other.mName;
231 }
232
233 return *this;
234 }
235
Hridya Valsaraju01268892017-02-27 08:48:38 -0800236 // move constructor
237 hidl_memory(hidl_memory&& other) {
238 *this = std::move(other);
239 }
240
241 // move assignment
242 hidl_memory &operator=(hidl_memory &&other) {
243 if (this != &other) {
244 mHandle = std::move(other.mHandle);
245 mSize = other.mSize;
246 mName = std::move(other.mName);
247 other.mSize = 0;
248 }
249
250 return *this;
251 }
252
Martijn Coenen30791002016-12-01 15:40:46 +0100253
254 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100255 }
256
257 const native_handle_t* handle() const {
258 return mHandle;
259 }
260
261 const hidl_string &name() const {
262 return mName;
263 }
264
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800265 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100266 return mSize;
267 }
268
269 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
270 static const size_t kOffsetOfHandle;
271 // offsetof(hidl_memory, mName) exposed since mHandle is private.
272 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800273
Martijn Coenen30791002016-12-01 15:40:46 +0100274private:
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800275 hidl_handle mHandle __attribute__ ((aligned(8)));
276 uint64_t mSize __attribute__ ((aligned(8)));
277 hidl_string mName __attribute__ ((aligned(8)));
Martijn Coenen30791002016-12-01 15:40:46 +0100278};
279
Andreas Huber20dce082016-09-22 19:39:13 -0700280////////////////////////////////////////////////////////////////////////////////
281
Martijn Coenen72110162016-08-19 14:28:25 +0200282template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800283struct hidl_vec {
Martijn Coenen72110162016-08-19 14:28:25 +0200284 hidl_vec()
285 : mBuffer(NULL),
286 mSize(0),
287 mOwnsBuffer(true) {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800288 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Martijn Coenen72110162016-08-19 14:28:25 +0200289 }
290
Yifan Hong602b85a2016-10-24 13:40:01 -0700291 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200292 *this = other;
293 }
294
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100295 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800296 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100297 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700298 }
299
Steven Morelandb69926a2016-11-15 10:02:57 -0800300 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800301 : mOwnsBuffer(true) {
302 if (list.size() > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800303 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800304 }
305 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800306 mBuffer = new T[mSize];
307
Steven Morelandb69926a2016-11-15 10:02:57 -0800308 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800309 for (auto it = list.begin(); it != list.end(); ++it) {
310 mBuffer[idx++] = *it;
311 }
312 }
313
Yifan Hong602b85a2016-10-24 13:40:01 -0700314 hidl_vec(const std::vector<T> &other) : hidl_vec() {
315 *this = other;
316 }
317
Martijn Coenen72110162016-08-19 14:28:25 +0200318 ~hidl_vec() {
319 if (mOwnsBuffer) {
320 delete[] mBuffer;
321 }
322 mBuffer = NULL;
323 }
324
Alexey Polyudove2299012016-10-19 09:52:00 -0700325 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200326 // caller's responsibility to ensure that the underlying memory stays
327 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700328 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200329 if (mOwnsBuffer) {
330 delete [] mBuffer;
331 }
332 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100333 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800334 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100335 }
336 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700337 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200338 }
339
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700340 T *data() {
341 return mBuffer;
342 }
343
344 const T *data() const {
345 return mBuffer;
346 }
347
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700348 T *releaseData() {
349 if (!mOwnsBuffer && mSize > 0) {
350 resize(mSize);
351 }
352 mOwnsBuffer = false;
353 return mBuffer;
354 }
355
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700356 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100357 if (mOwnsBuffer) {
358 delete[] mBuffer;
359 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700360 mBuffer = other.mBuffer;
361 mSize = other.mSize;
362 mOwnsBuffer = other.mOwnsBuffer;
363 other.mOwnsBuffer = false;
364 return *this;
365 }
366
Martijn Coenen72110162016-08-19 14:28:25 +0200367 hidl_vec &operator=(const hidl_vec &other) {
368 if (this != &other) {
369 if (mOwnsBuffer) {
370 delete[] mBuffer;
371 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700372 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200373 }
374
375 return *this;
376 }
377
Yifan Hong602b85a2016-10-24 13:40:01 -0700378 // copy from an std::vector.
379 hidl_vec &operator=(const std::vector<T> &other) {
380 if (mOwnsBuffer) {
381 delete[] mBuffer;
382 }
383 copyFrom(other, other.size());
384 return *this;
385 }
386
387 // cast to an std::vector.
388 operator std::vector<T>() const {
389 std::vector<T> v(mSize);
390 for (size_t i = 0; i < mSize; ++i) {
391 v[i] = mBuffer[i];
392 }
393 return v;
394 }
395
Yifan Hong9fcbb362016-12-20 16:46:41 -0800396 // equality check, assuming that T::operator== is defined.
397 bool operator==(const hidl_vec &other) const {
398 if (mSize != other.size()) {
399 return false;
400 }
401 for (size_t i = 0; i < mSize; ++i) {
402 if (!(mBuffer[i] == other.mBuffer[i])) {
403 return false;
404 }
405 }
406 return true;
407 }
408
409 // inequality check, assuming that T::operator== is defined.
410 inline bool operator!=(const hidl_vec &other) const {
411 return !((*this) == other);
412 }
413
Martijn Coenen72110162016-08-19 14:28:25 +0200414 size_t size() const {
415 return mSize;
416 }
417
418 T &operator[](size_t index) {
419 return mBuffer[index];
420 }
421
422 const T &operator[](size_t index) const {
423 return mBuffer[index];
424 }
425
426 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100427 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800428 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100429 }
Martijn Coenen72110162016-08-19 14:28:25 +0200430 T *newBuffer = new T[size];
431
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100432 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200433 newBuffer[i] = mBuffer[i];
434 }
435
436 if (mOwnsBuffer) {
437 delete[] mBuffer;
438 }
439 mBuffer = newBuffer;
440
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100441 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200442 mOwnsBuffer = true;
443 }
444
Yifan Hong089ae132016-11-11 11:32:52 -0800445 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
446 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800447
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800448private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800449 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800450 template<bool is_const>
451 class iter : public std::iterator<
452 std::random_access_iterator_tag, /* Category */
453 T,
454 ptrdiff_t, /* Distance */
455 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
456 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800457 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800458 using traits = std::iterator_traits<iter>;
459 using ptr_type = typename traits::pointer;
460 using ref_type = typename traits::reference;
461 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800462 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800463 iter(ptr_type ptr) : mPtr(ptr) { }
464 inline iter &operator++() { mPtr++; return *this; }
465 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
466 inline iter &operator--() { mPtr--; return *this; }
467 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
468 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
469 inline iter operator+(diff_type n) const { return mPtr + n; }
470 inline iter operator-(diff_type n) const { return mPtr - n; }
471 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
472 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
473 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
474 inline ref_type operator*() const { return *mPtr; }
475 inline ptr_type operator->() const { return mPtr; }
476 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
477 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
478 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
479 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
480 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
481 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
482 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800483 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800484 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800485 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800486public:
487 using iterator = iter<false /* is_const */>;
488 using const_iterator = iter<true /* is_const */>;
489
Scott Randolphbb840f72016-11-21 14:39:26 -0800490 iterator begin() { return data(); }
491 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800492 const_iterator begin() const { return data(); }
493 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800494
Martijn Coenen72110162016-08-19 14:28:25 +0200495private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100496 details::hidl_pointer<T> mBuffer;
497 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200498 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700499
500 // copy from an array-like object, assuming my resources are freed.
501 template <typename Array>
502 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800503 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700504 mOwnsBuffer = true;
505 if (mSize > 0) {
506 mBuffer = new T[size];
507 for (size_t i = 0; i < size; ++i) {
508 mBuffer[i] = data[i];
509 }
510 } else {
511 mBuffer = NULL;
512 }
513 }
Martijn Coenen72110162016-08-19 14:28:25 +0200514};
515
Yifan Hong089ae132016-11-11 11:32:52 -0800516template <typename T>
517const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
518
Andreas Huber20dce082016-09-22 19:39:13 -0700519////////////////////////////////////////////////////////////////////////////////
520
521namespace details {
522
523 template<size_t SIZE1, size_t... SIZES>
524 struct product {
525 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
526 };
527
528 template<size_t SIZE1>
529 struct product<SIZE1> {
530 static constexpr size_t value = SIZE1;
531 };
532
533 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800534 struct std_array {
535 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
536 };
537
538 template<typename T, size_t SIZE1>
539 struct std_array<T, SIZE1> {
540 using type = std::array<T, SIZE1>;
541 };
542
543 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700544 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800545
546 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
547
Andreas Huber20dce082016-09-22 19:39:13 -0700548 explicit accessor(T *base)
549 : mBase(base) {
550 }
551
552 accessor<T, SIZES...> operator[](size_t index) {
553 return accessor<T, SIZES...>(
554 &mBase[index * product<SIZES...>::value]);
555 }
556
Yifan Hong44ab6232016-11-22 16:28:24 -0800557 accessor &operator=(const std_array_type &other) {
558 for (size_t i = 0; i < SIZE1; ++i) {
559 (*this)[i] = other[i];
560 }
561 return *this;
562 }
563
Andreas Huber20dce082016-09-22 19:39:13 -0700564 private:
565 T *mBase;
566 };
567
568 template<typename T, size_t SIZE1>
569 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800570
571 using std_array_type = typename std_array<T, SIZE1>::type;
572
Andreas Huber20dce082016-09-22 19:39:13 -0700573 explicit accessor(T *base)
574 : mBase(base) {
575 }
576
577 T &operator[](size_t index) {
578 return mBase[index];
579 }
580
Yifan Hong44ab6232016-11-22 16:28:24 -0800581 accessor &operator=(const std_array_type &other) {
582 for (size_t i = 0; i < SIZE1; ++i) {
583 (*this)[i] = other[i];
584 }
585 return *this;
586 }
587
Andreas Huber20dce082016-09-22 19:39:13 -0700588 private:
589 T *mBase;
590 };
591
592 template<typename T, size_t SIZE1, size_t... SIZES>
593 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800594
595 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
596
Andreas Huber20dce082016-09-22 19:39:13 -0700597 explicit const_accessor(const T *base)
598 : mBase(base) {
599 }
600
Yifan Hongbe7a6882017-01-05 17:30:17 -0800601 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700602 return const_accessor<T, SIZES...>(
603 &mBase[index * product<SIZES...>::value]);
604 }
605
Yifan Hong44ab6232016-11-22 16:28:24 -0800606 operator std_array_type() {
607 std_array_type array;
608 for (size_t i = 0; i < SIZE1; ++i) {
609 array[i] = (*this)[i];
610 }
611 return array;
612 }
613
Andreas Huber20dce082016-09-22 19:39:13 -0700614 private:
615 const T *mBase;
616 };
617
618 template<typename T, size_t SIZE1>
619 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800620
621 using std_array_type = typename std_array<T, SIZE1>::type;
622
Andreas Huber20dce082016-09-22 19:39:13 -0700623 explicit const_accessor(const T *base)
624 : mBase(base) {
625 }
626
627 const T &operator[](size_t index) const {
628 return mBase[index];
629 }
630
Yifan Hong44ab6232016-11-22 16:28:24 -0800631 operator std_array_type() {
632 std_array_type array;
633 for (size_t i = 0; i < SIZE1; ++i) {
634 array[i] = (*this)[i];
635 }
636 return array;
637 }
638
Andreas Huber20dce082016-09-22 19:39:13 -0700639 private:
640 const T *mBase;
641 };
642
643} // namespace details
644
645////////////////////////////////////////////////////////////////////////////////
646
Yifan Hong44ab6232016-11-22 16:28:24 -0800647// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700648template<typename T, size_t SIZE1, size_t... SIZES>
649struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800650
651 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
652
Andreas Huber20dce082016-09-22 19:39:13 -0700653 hidl_array() = default;
654
Yifan Hong44ab6232016-11-22 16:28:24 -0800655 // Copies the data from source, using T::operator=(const T &).
656 hidl_array(const T *source) {
657 for (size_t i = 0; i < elementCount(); ++i) {
658 mBuffer[i] = source[i];
659 }
660 }
661
662 // Copies the data from the given std::array, using T::operator=(const T &).
663 hidl_array(const std_array_type &array) {
664 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
665 modifier = array;
666 }
667
Andreas Huber20dce082016-09-22 19:39:13 -0700668 T *data() { return mBuffer; }
669 const T *data() const { return mBuffer; }
670
671 details::accessor<T, SIZES...> operator[](size_t index) {
672 return details::accessor<T, SIZES...>(
673 &mBuffer[index * details::product<SIZES...>::value]);
674 }
675
676 details::const_accessor<T, SIZES...> operator[](size_t index) const {
677 return details::const_accessor<T, SIZES...>(
678 &mBuffer[index * details::product<SIZES...>::value]);
679 }
680
Yifan Hong9fcbb362016-12-20 16:46:41 -0800681 // equality check, assuming that T::operator== is defined.
682 bool operator==(const hidl_array &other) const {
683 for (size_t i = 0; i < elementCount(); ++i) {
684 if (!(mBuffer[i] == other.mBuffer[i])) {
685 return false;
686 }
687 }
688 return true;
689 }
690
691 inline bool operator!=(const hidl_array &other) const {
692 return !((*this) == other);
693 }
694
Andreas Huber00a985c2016-09-28 14:24:53 -0700695 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
696
697 static constexpr size_tuple_type size() {
698 return std::make_tuple(SIZE1, SIZES...);
699 }
700
Yifan Hong44ab6232016-11-22 16:28:24 -0800701 static constexpr size_t elementCount() {
702 return details::product<SIZE1, SIZES...>::value;
703 }
704
705 operator std_array_type() const {
706 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
707 }
708
Andreas Huber20dce082016-09-22 19:39:13 -0700709private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800710 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700711};
712
Yifan Hong44ab6232016-11-22 16:28:24 -0800713// An 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>
715struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800716
717 using std_array_type = typename details::std_array<T, SIZE1>::type;
718
Andreas Huber20dce082016-09-22 19:39:13 -0700719 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800720
721 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800722 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800723 for (size_t i = 0; i < elementCount(); ++i) {
724 mBuffer[i] = source[i];
725 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800726 }
Andreas Huber20dce082016-09-22 19:39:13 -0700727
Yifan Hong44ab6232016-11-22 16:28:24 -0800728 // Copies the data from the given std::array, using T::operator=(const T &).
729 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
730
Andreas Huber20dce082016-09-22 19:39:13 -0700731 T *data() { return mBuffer; }
732 const T *data() const { return mBuffer; }
733
734 T &operator[](size_t index) {
735 return mBuffer[index];
736 }
737
738 const T &operator[](size_t index) const {
739 return mBuffer[index];
740 }
741
Yifan Hong9fcbb362016-12-20 16:46:41 -0800742 // equality check, assuming that T::operator== is defined.
743 bool operator==(const hidl_array &other) const {
744 for (size_t i = 0; i < elementCount(); ++i) {
745 if (!(mBuffer[i] == other.mBuffer[i])) {
746 return false;
747 }
748 }
749 return true;
750 }
751
752 inline bool operator!=(const hidl_array &other) const {
753 return !((*this) == other);
754 }
755
Andreas Huber00a985c2016-09-28 14:24:53 -0700756 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800757 static constexpr size_t elementCount() { return SIZE1; }
758
759 // Copies the data to an std::array, using T::operator=(T).
760 operator std_array_type() const {
761 std_array_type array;
762 for (size_t i = 0; i < SIZE1; ++i) {
763 array[i] = mBuffer[i];
764 }
765 return array;
766 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700767
Andreas Huber20dce082016-09-22 19:39:13 -0700768private:
769 T mBuffer[SIZE1];
770};
771
Martijn Coenen72110162016-08-19 14:28:25 +0200772// ----------------------------------------------------------------------
773// Version functions
774struct hidl_version {
775public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800776 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200777
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700778 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200779 return (mMajor == other.get_major() && mMinor == other.get_minor());
780 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200781
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800782 bool operator<(const hidl_version& other) const {
783 return (mMajor < other.get_major() ||
784 (mMajor == other.get_major() && mMinor < other.get_minor()));
785 }
786
787 bool operator>(const hidl_version& other) const {
788 return other < *this;
789 }
790
791 bool operator<=(const hidl_version& other) const {
792 return !(*this > other);
793 }
794
795 bool operator>=(const hidl_version& other) const {
796 return !(*this < other);
797 }
798
Martijn Coenen097a7672016-09-08 16:56:41 +0200799 constexpr uint16_t get_major() const { return mMajor; }
800 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200801
Martijn Coenen72110162016-08-19 14:28:25 +0200802private:
803 uint16_t mMajor;
804 uint16_t mMinor;
805};
806
807inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
808 return hidl_version(major,minor);
809}
810
Yifan Hongbe7a6882017-01-05 17:30:17 -0800811///////////////////// toString functions
812
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800813std::string toString(const void *t);
Yifan Hongbe7a6882017-01-05 17:30:17 -0800814
815// toString alias for numeric types
816template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
817inline std::string toString(T t) {
818 return std::to_string(t);
819}
820
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800821namespace details {
822
Yifan Hongbe7a6882017-01-05 17:30:17 -0800823template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
824inline std::string toHexString(T t, bool prefix = true) {
825 std::ostringstream os;
826 if (prefix) { os << std::showbase; }
827 os << std::hex << t;
828 return os.str();
829}
830
831template<>
832inline std::string toHexString(uint8_t t, bool prefix) {
833 return toHexString(static_cast<int32_t>(t), prefix);
834}
835
836template<>
837inline std::string toHexString(int8_t t, bool prefix) {
838 return toHexString(static_cast<int32_t>(t), prefix);
839}
840
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800841template<typename Array>
842std::string arrayToString(const Array &a, size_t size);
843
844template<size_t SIZE1>
845std::string arraySizeToString() {
846 return std::string{"["} + toString(SIZE1) + "]";
847}
848
849template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
850std::string arraySizeToString() {
851 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
852}
853
854template<typename T, size_t SIZE1>
855std::string toString(details::const_accessor<T, SIZE1> a) {
856 return arrayToString(a, SIZE1);
857}
858
859template<typename Array>
860std::string arrayToString(const Array &a, size_t size) {
861 using android::hardware::toString;
862 std::string os;
863 os += "{";
864 for (size_t i = 0; i < size; ++i) {
865 if (i > 0) {
866 os += ", ";
867 }
868 os += toString(a[i]);
869 }
870 os += "}";
871 return os;
872}
873
874template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
875std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
876 return arrayToString(a, SIZE1);
877}
878
879} //namespace details
880
881inline std::string toString(const void *t) {
882 return details::toHexString(reinterpret_cast<uintptr_t>(t));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800883}
884
885// debug string dump. There will be quotes around the string!
886inline std::string toString(const hidl_string &hs) {
887 return std::string{"\""} + hs.c_str() + "\"";
888}
889
890// debug string dump
891inline std::string toString(const hidl_handle &hs) {
892 return toString(hs.getNativeHandle());
893}
894
895inline std::string toString(const hidl_memory &mem) {
896 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
897 + toString(mem.size())
898 + ", .handle = " + toString(mem.handle()) + "}";
899}
900
901inline std::string toString(const sp<hidl_death_recipient> &dr) {
902 return std::string{"death_recipient@"} + toString(dr.get());
903}
904
Yifan Hongbe7a6882017-01-05 17:30:17 -0800905// debug string dump, assuming that toString(T) is defined.
906template<typename T>
907std::string toString(const hidl_vec<T> &a) {
908 std::string os;
909 os += "[" + toString(a.size()) + "]";
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800910 os += details::arrayToString(a, a.size());
Yifan Hongbe7a6882017-01-05 17:30:17 -0800911 return os;
912}
913
Yifan Hongbe7a6882017-01-05 17:30:17 -0800914template<typename T, size_t SIZE1>
915std::string toString(const hidl_array<T, SIZE1> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800916 return details::arraySizeToString<SIZE1>()
917 + details::toString(details::const_accessor<T, SIZE1>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800918}
919
920template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
921std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800922 return details::arraySizeToString<SIZE1, SIZE2, SIZES...>()
923 + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800924}
925
Martijn Coenen72110162016-08-19 14:28:25 +0200926} // namespace hardware
927} // namespace android
928
Martijn Coenenc28f1152016-08-22 14:06:56 +0200929
Martijn Coenen72110162016-08-19 14:28:25 +0200930#endif // ANDROID_HIDL_SUPPORT_H