blob: a8eae8cab7516404fc7d9d066a7481758af147b4 [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 Hong44c0e572017-01-20 15:41:52 -080034#include <vintf/Transport.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080035#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020036
37namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010038
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010039// this file is included by all hidl interface, so we must forward declare the
40// IMemory and IBase types.
Martijn Coenen30791002016-12-01 15:40:46 +010041namespace hidl {
42namespace memory {
43namespace V1_0 {
44 struct IMemory;
45}; // namespace V1_0
46}; // namespace manager
47}; // namespace hidl
48
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010049namespace hidl {
50namespace base {
51namespace V1_0 {
52 struct IBase;
53}; // namespace V1_0
54}; // namespace base
55}; // namespace hidl
56
Martijn Coenen72110162016-08-19 14:28:25 +020057namespace hardware {
58
Yifan Hong44c0e572017-01-20 15:41:52 -080059// Get transport method from vendor interface manifest.
Yifan Hong37b36202017-02-28 16:04:22 -080060// interfaceName has the format "android.hardware.foo@1.0::IFoo"
61// instanceName is "default", "ashmem", etc.
Yifan Hong20273f92017-01-30 14:13:19 -080062// If it starts with "android.hidl.", a static map is looked up instead.
Yifan Hong37b36202017-02-28 16:04:22 -080063vintf::Transport getTransport(const std::string &interfaceName,
64 const std::string &instanceName);
Yifan Hong44c0e572017-01-20 15:41:52 -080065
Yifan Hong24332ef2017-03-07 16:22:19 -080066namespace details {
67// Return true on userdebug / eng builds and false on user builds.
68bool debuggable();
69} // namespace details
70
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010071// hidl_death_recipient is a callback interfaced that can be used with
72// linkToDeath() / unlinkToDeath()
73struct hidl_death_recipient : public virtual RefBase {
74 virtual void serviceDied(uint64_t cookie,
75 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
76};
77
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010078// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
79// so that it can safely be transferred between 32-bit and 64-bit processes.
Martijn Coenen04b91c02017-01-19 14:14:21 +010080// The ownership semantics for this are:
81// 1) The conversion constructor and assignment operator taking a const native_handle_t*
82// do not take ownership of the handle; this is because these operations are usually
83// just done for IPC, and cloning by default is a waste of resources. If you want
84// a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/);
85// 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership;
86// that is because it's not intuitive that this class encapsulates a native_handle_t
87// which needs cloning to be valid; in particular, this allows constructs like this:
88// hidl_handle copy;
89// foo->someHidlCall([&](auto incoming_handle) {
90// copy = incoming_handle;
91// });
92// // copy and its enclosed file descriptors will remain valid here.
93// 3) The move constructor does what you would expect; it only owns the handle if the
94// original did.
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010095struct hidl_handle {
Martijn Coenen04b91c02017-01-19 14:14:21 +010096 hidl_handle();
97 ~hidl_handle();
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010098
Martijn Coenen04b91c02017-01-19 14:14:21 +010099 hidl_handle(const native_handle_t *handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100100
Martijn Coenen04b91c02017-01-19 14:14:21 +0100101 // copy constructor.
102 hidl_handle(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100103
104 // move constructor.
Martijn Coenen04b91c02017-01-19 14:14:21 +0100105 hidl_handle(hidl_handle &&other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100106
Steven Moreland6ffdc2a2017-01-23 12:34:33 -0800107 // assignment operators
Martijn Coenen04b91c02017-01-19 14:14:21 +0100108 hidl_handle &operator=(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100109
Martijn Coenen04b91c02017-01-19 14:14:21 +0100110 hidl_handle &operator=(const native_handle_t *native_handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100111
Martijn Coenen04b91c02017-01-19 14:14:21 +0100112 hidl_handle &operator=(hidl_handle &&other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100113
Martijn Coenen04b91c02017-01-19 14:14:21 +0100114 void setTo(native_handle_t* handle, bool shouldOwn = false);
115
116 const native_handle_t* operator->() const;
117
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100118 // implicit conversion to const native_handle_t*
Martijn Coenen04b91c02017-01-19 14:14:21 +0100119 operator const native_handle_t *() const;
120
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100121 // explicit conversion
Martijn Coenen04b91c02017-01-19 14:14:21 +0100122 const native_handle_t *getNativeHandle() const;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100123private:
Martijn Coenen04b91c02017-01-19 14:14:21 +0100124 void freeHandle();
125
Andreas Huber6b9cc692017-03-30 10:58:29 -0700126 details::hidl_pointer<const native_handle_t> mHandle __attribute__ ((aligned(8)));
127 bool mOwnsHandle __attribute ((aligned(8)));
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100128};
129
Martijn Coenen72110162016-08-19 14:28:25 +0200130struct hidl_string {
131 hidl_string();
132 ~hidl_string();
133
Yifan Hong602b85a2016-10-24 13:40:01 -0700134 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200135 hidl_string(const hidl_string &);
Steven Morelanda21d84f2017-02-16 09:23:45 -0800136 // copy from a C-style string. nullptr will create an empty string
Steven Morelande03c0872016-10-24 10:43:50 -0700137 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800138 // copy the first length characters from a C-style string.
139 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700140 // copy from an std::string.
141 hidl_string(const std::string &);
142
143 // move constructor.
144 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200145
146 const char *c_str() const;
147 size_t size() const;
148 bool empty() const;
149
Yifan Hong602b85a2016-10-24 13:40:01 -0700150 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700151 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700152 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200153 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700154 // copy from an std::string.
155 hidl_string &operator=(const std::string &);
156 // move assignment operator.
157 hidl_string &operator=(hidl_string &&other);
158 // cast to std::string.
159 operator std::string() const;
160 // cast to C-style string. Caller is responsible
161 // to maintain this hidl_string alive.
162 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700163
Martijn Coenen72110162016-08-19 14:28:25 +0200164 void clear();
165
166 // Reference an external char array. Ownership is _not_ transferred.
167 // Caller is responsible for ensuring that underlying memory is valid
168 // for the lifetime of this hidl_string.
169 void setToExternal(const char *data, size_t size);
170
Andreas Huberebfeb362016-08-25 13:39:05 -0700171 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
172 static const size_t kOffsetOfBuffer;
173
Martijn Coenen72110162016-08-19 14:28:25 +0200174private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100175 details::hidl_pointer<const char> mBuffer;
176 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700177 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200178
Yifan Hong602b85a2016-10-24 13:40:01 -0700179 // copy from data with size. Assume that my memory is freed
180 // (through clear(), for example)
181 void copyFrom(const char *data, size_t size);
182 // move from another hidl_string
183 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200184};
185
Steven Moreland551396a2017-02-13 18:33:20 -0800186#define HIDL_STRING_OPERATOR(OP) \
187 inline bool operator OP(const hidl_string &hs1, const hidl_string &hs2) { \
188 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; \
189 } \
190 inline bool operator OP(const hidl_string &hs, const char *s) { \
191 return strcmp(hs.c_str(), s) OP 0; \
192 } \
193 inline bool operator OP(const char *s, const hidl_string &hs) { \
194 return strcmp(hs.c_str(), s) OP 0; \
195 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800196
Steven Moreland551396a2017-02-13 18:33:20 -0800197HIDL_STRING_OPERATOR(==)
198HIDL_STRING_OPERATOR(!=)
199HIDL_STRING_OPERATOR(<)
200HIDL_STRING_OPERATOR(<=)
201HIDL_STRING_OPERATOR(>)
202HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800203
Steven Moreland551396a2017-02-13 18:33:20 -0800204#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700205
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 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100217 * Creates a hidl_memory object, but doesn't take ownership of
218 * the passed in native_handle_t; callers are responsible for
219 * making sure the handle remains valid while this object is
220 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100221 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100222 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
223 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100224 mSize(size),
225 mName(name)
226 {}
227
228 // copy constructor
229 hidl_memory(const hidl_memory& other) {
230 *this = other;
231 }
232
233 // copy assignment
234 hidl_memory &operator=(const hidl_memory &other) {
235 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100236 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100237 mSize = other.mSize;
238 mName = other.mName;
239 }
240
241 return *this;
242 }
243
Hridya Valsaraju01268892017-02-27 08:48:38 -0800244 // move constructor
245 hidl_memory(hidl_memory&& other) {
246 *this = std::move(other);
247 }
248
249 // move assignment
250 hidl_memory &operator=(hidl_memory &&other) {
251 if (this != &other) {
252 mHandle = std::move(other.mHandle);
253 mSize = other.mSize;
254 mName = std::move(other.mName);
255 other.mSize = 0;
256 }
257
258 return *this;
259 }
260
Martijn Coenen30791002016-12-01 15:40:46 +0100261
262 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100263 }
264
265 const native_handle_t* handle() const {
266 return mHandle;
267 }
268
269 const hidl_string &name() const {
270 return mName;
271 }
272
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800273 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100274 return mSize;
275 }
276
277 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
278 static const size_t kOffsetOfHandle;
279 // offsetof(hidl_memory, mName) exposed since mHandle is private.
280 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800281
Martijn Coenen30791002016-12-01 15:40:46 +0100282private:
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800283 hidl_handle mHandle __attribute__ ((aligned(8)));
284 uint64_t mSize __attribute__ ((aligned(8)));
285 hidl_string mName __attribute__ ((aligned(8)));
Martijn Coenen30791002016-12-01 15:40:46 +0100286};
287
Andreas Huber20dce082016-09-22 19:39:13 -0700288////////////////////////////////////////////////////////////////////////////////
289
Martijn Coenen72110162016-08-19 14:28:25 +0200290template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800291struct hidl_vec {
Martijn Coenen72110162016-08-19 14:28:25 +0200292 hidl_vec()
293 : mBuffer(NULL),
294 mSize(0),
295 mOwnsBuffer(true) {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800296 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Martijn Coenen72110162016-08-19 14:28:25 +0200297 }
298
Yifan Hong602b85a2016-10-24 13:40:01 -0700299 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200300 *this = other;
301 }
302
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100303 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800304 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100305 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700306 }
307
Steven Morelandb69926a2016-11-15 10:02:57 -0800308 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800309 : mOwnsBuffer(true) {
310 if (list.size() > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800311 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800312 }
313 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800314 mBuffer = new T[mSize];
315
Steven Morelandb69926a2016-11-15 10:02:57 -0800316 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800317 for (auto it = list.begin(); it != list.end(); ++it) {
318 mBuffer[idx++] = *it;
319 }
320 }
321
Yifan Hong602b85a2016-10-24 13:40:01 -0700322 hidl_vec(const std::vector<T> &other) : hidl_vec() {
323 *this = other;
324 }
325
Martijn Coenen72110162016-08-19 14:28:25 +0200326 ~hidl_vec() {
327 if (mOwnsBuffer) {
328 delete[] mBuffer;
329 }
330 mBuffer = NULL;
331 }
332
Alexey Polyudove2299012016-10-19 09:52:00 -0700333 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200334 // caller's responsibility to ensure that the underlying memory stays
335 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700336 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200337 if (mOwnsBuffer) {
338 delete [] mBuffer;
339 }
340 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100341 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800342 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100343 }
344 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700345 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200346 }
347
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700348 T *data() {
349 return mBuffer;
350 }
351
352 const T *data() const {
353 return mBuffer;
354 }
355
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700356 T *releaseData() {
357 if (!mOwnsBuffer && mSize > 0) {
358 resize(mSize);
359 }
360 mOwnsBuffer = false;
361 return mBuffer;
362 }
363
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700364 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100365 if (mOwnsBuffer) {
366 delete[] mBuffer;
367 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700368 mBuffer = other.mBuffer;
369 mSize = other.mSize;
370 mOwnsBuffer = other.mOwnsBuffer;
371 other.mOwnsBuffer = false;
372 return *this;
373 }
374
Martijn Coenen72110162016-08-19 14:28:25 +0200375 hidl_vec &operator=(const hidl_vec &other) {
376 if (this != &other) {
377 if (mOwnsBuffer) {
378 delete[] mBuffer;
379 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700380 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200381 }
382
383 return *this;
384 }
385
Yifan Hong602b85a2016-10-24 13:40:01 -0700386 // copy from an std::vector.
387 hidl_vec &operator=(const std::vector<T> &other) {
388 if (mOwnsBuffer) {
389 delete[] mBuffer;
390 }
391 copyFrom(other, other.size());
392 return *this;
393 }
394
395 // cast to an std::vector.
396 operator std::vector<T>() const {
397 std::vector<T> v(mSize);
398 for (size_t i = 0; i < mSize; ++i) {
399 v[i] = mBuffer[i];
400 }
401 return v;
402 }
403
Yifan Hong9fcbb362016-12-20 16:46:41 -0800404 // equality check, assuming that T::operator== is defined.
405 bool operator==(const hidl_vec &other) const {
406 if (mSize != other.size()) {
407 return false;
408 }
409 for (size_t i = 0; i < mSize; ++i) {
410 if (!(mBuffer[i] == other.mBuffer[i])) {
411 return false;
412 }
413 }
414 return true;
415 }
416
417 // inequality check, assuming that T::operator== is defined.
418 inline bool operator!=(const hidl_vec &other) const {
419 return !((*this) == other);
420 }
421
Martijn Coenen72110162016-08-19 14:28:25 +0200422 size_t size() const {
423 return mSize;
424 }
425
426 T &operator[](size_t index) {
427 return mBuffer[index];
428 }
429
430 const T &operator[](size_t index) const {
431 return mBuffer[index];
432 }
433
434 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100435 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800436 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100437 }
Martijn Coenen72110162016-08-19 14:28:25 +0200438 T *newBuffer = new T[size];
439
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100440 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200441 newBuffer[i] = mBuffer[i];
442 }
443
444 if (mOwnsBuffer) {
445 delete[] mBuffer;
446 }
447 mBuffer = newBuffer;
448
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100449 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200450 mOwnsBuffer = true;
451 }
452
Yifan Hong089ae132016-11-11 11:32:52 -0800453 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
454 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800455
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800456private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800457 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800458 template<bool is_const>
459 class iter : public std::iterator<
460 std::random_access_iterator_tag, /* Category */
461 T,
462 ptrdiff_t, /* Distance */
463 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
464 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800465 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800466 using traits = std::iterator_traits<iter>;
467 using ptr_type = typename traits::pointer;
468 using ref_type = typename traits::reference;
469 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800470 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800471 iter(ptr_type ptr) : mPtr(ptr) { }
472 inline iter &operator++() { mPtr++; return *this; }
473 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
474 inline iter &operator--() { mPtr--; return *this; }
475 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
476 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
477 inline iter operator+(diff_type n) const { return mPtr + n; }
478 inline iter operator-(diff_type n) const { return mPtr - n; }
479 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
480 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
481 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
482 inline ref_type operator*() const { return *mPtr; }
483 inline ptr_type operator->() const { return mPtr; }
484 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
485 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
486 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
487 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
488 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
489 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
490 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800491 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800492 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800493 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800494public:
495 using iterator = iter<false /* is_const */>;
496 using const_iterator = iter<true /* is_const */>;
497
Scott Randolphbb840f72016-11-21 14:39:26 -0800498 iterator begin() { return data(); }
499 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800500 const_iterator begin() const { return data(); }
501 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800502
Martijn Coenen72110162016-08-19 14:28:25 +0200503private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100504 details::hidl_pointer<T> mBuffer;
505 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200506 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700507
508 // copy from an array-like object, assuming my resources are freed.
509 template <typename Array>
510 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800511 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700512 mOwnsBuffer = true;
513 if (mSize > 0) {
514 mBuffer = new T[size];
515 for (size_t i = 0; i < size; ++i) {
516 mBuffer[i] = data[i];
517 }
518 } else {
519 mBuffer = NULL;
520 }
521 }
Martijn Coenen72110162016-08-19 14:28:25 +0200522};
523
Yifan Hong089ae132016-11-11 11:32:52 -0800524template <typename T>
525const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
526
Andreas Huber20dce082016-09-22 19:39:13 -0700527////////////////////////////////////////////////////////////////////////////////
528
529namespace details {
530
531 template<size_t SIZE1, size_t... SIZES>
532 struct product {
533 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
534 };
535
536 template<size_t SIZE1>
537 struct product<SIZE1> {
538 static constexpr size_t value = SIZE1;
539 };
540
541 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800542 struct std_array {
543 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
544 };
545
546 template<typename T, size_t SIZE1>
547 struct std_array<T, SIZE1> {
548 using type = std::array<T, SIZE1>;
549 };
550
551 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700552 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800553
554 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
555
Andreas Huber20dce082016-09-22 19:39:13 -0700556 explicit accessor(T *base)
557 : mBase(base) {
558 }
559
560 accessor<T, SIZES...> operator[](size_t index) {
561 return accessor<T, SIZES...>(
562 &mBase[index * product<SIZES...>::value]);
563 }
564
Yifan Hong44ab6232016-11-22 16:28:24 -0800565 accessor &operator=(const std_array_type &other) {
566 for (size_t i = 0; i < SIZE1; ++i) {
567 (*this)[i] = other[i];
568 }
569 return *this;
570 }
571
Andreas Huber20dce082016-09-22 19:39:13 -0700572 private:
573 T *mBase;
574 };
575
576 template<typename T, size_t SIZE1>
577 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800578
579 using std_array_type = typename std_array<T, SIZE1>::type;
580
Andreas Huber20dce082016-09-22 19:39:13 -0700581 explicit accessor(T *base)
582 : mBase(base) {
583 }
584
585 T &operator[](size_t index) {
586 return mBase[index];
587 }
588
Yifan Hong44ab6232016-11-22 16:28:24 -0800589 accessor &operator=(const std_array_type &other) {
590 for (size_t i = 0; i < SIZE1; ++i) {
591 (*this)[i] = other[i];
592 }
593 return *this;
594 }
595
Andreas Huber20dce082016-09-22 19:39:13 -0700596 private:
597 T *mBase;
598 };
599
600 template<typename T, size_t SIZE1, size_t... SIZES>
601 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800602
603 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
604
Andreas Huber20dce082016-09-22 19:39:13 -0700605 explicit const_accessor(const T *base)
606 : mBase(base) {
607 }
608
Yifan Hongbe7a6882017-01-05 17:30:17 -0800609 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700610 return const_accessor<T, SIZES...>(
611 &mBase[index * product<SIZES...>::value]);
612 }
613
Yifan Hong44ab6232016-11-22 16:28:24 -0800614 operator std_array_type() {
615 std_array_type array;
616 for (size_t i = 0; i < SIZE1; ++i) {
617 array[i] = (*this)[i];
618 }
619 return array;
620 }
621
Andreas Huber20dce082016-09-22 19:39:13 -0700622 private:
623 const T *mBase;
624 };
625
626 template<typename T, size_t SIZE1>
627 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800628
629 using std_array_type = typename std_array<T, SIZE1>::type;
630
Andreas Huber20dce082016-09-22 19:39:13 -0700631 explicit const_accessor(const T *base)
632 : mBase(base) {
633 }
634
635 const T &operator[](size_t index) const {
636 return mBase[index];
637 }
638
Yifan Hong44ab6232016-11-22 16:28:24 -0800639 operator std_array_type() {
640 std_array_type array;
641 for (size_t i = 0; i < SIZE1; ++i) {
642 array[i] = (*this)[i];
643 }
644 return array;
645 }
646
Andreas Huber20dce082016-09-22 19:39:13 -0700647 private:
648 const T *mBase;
649 };
650
651} // namespace details
652
653////////////////////////////////////////////////////////////////////////////////
654
Yifan Hong44ab6232016-11-22 16:28:24 -0800655// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700656template<typename T, size_t SIZE1, size_t... SIZES>
657struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800658
659 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
660
Andreas Huber20dce082016-09-22 19:39:13 -0700661 hidl_array() = default;
662
Yifan Hong44ab6232016-11-22 16:28:24 -0800663 // Copies the data from source, using T::operator=(const T &).
664 hidl_array(const T *source) {
665 for (size_t i = 0; i < elementCount(); ++i) {
666 mBuffer[i] = source[i];
667 }
668 }
669
670 // Copies the data from the given std::array, using T::operator=(const T &).
671 hidl_array(const std_array_type &array) {
672 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
673 modifier = array;
674 }
675
Andreas Huber20dce082016-09-22 19:39:13 -0700676 T *data() { return mBuffer; }
677 const T *data() const { return mBuffer; }
678
679 details::accessor<T, SIZES...> operator[](size_t index) {
680 return details::accessor<T, SIZES...>(
681 &mBuffer[index * details::product<SIZES...>::value]);
682 }
683
684 details::const_accessor<T, SIZES...> operator[](size_t index) const {
685 return details::const_accessor<T, SIZES...>(
686 &mBuffer[index * details::product<SIZES...>::value]);
687 }
688
Yifan Hong9fcbb362016-12-20 16:46:41 -0800689 // equality check, assuming that T::operator== is defined.
690 bool operator==(const hidl_array &other) const {
691 for (size_t i = 0; i < elementCount(); ++i) {
692 if (!(mBuffer[i] == other.mBuffer[i])) {
693 return false;
694 }
695 }
696 return true;
697 }
698
699 inline bool operator!=(const hidl_array &other) const {
700 return !((*this) == other);
701 }
702
Andreas Huber00a985c2016-09-28 14:24:53 -0700703 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
704
705 static constexpr size_tuple_type size() {
706 return std::make_tuple(SIZE1, SIZES...);
707 }
708
Yifan Hong44ab6232016-11-22 16:28:24 -0800709 static constexpr size_t elementCount() {
710 return details::product<SIZE1, SIZES...>::value;
711 }
712
713 operator std_array_type() const {
714 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
715 }
716
Andreas Huber20dce082016-09-22 19:39:13 -0700717private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800718 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700719};
720
Yifan Hong44ab6232016-11-22 16:28:24 -0800721// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700722template<typename T, size_t SIZE1>
723struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800724
725 using std_array_type = typename details::std_array<T, SIZE1>::type;
726
Andreas Huber20dce082016-09-22 19:39:13 -0700727 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800728
729 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800730 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800731 for (size_t i = 0; i < elementCount(); ++i) {
732 mBuffer[i] = source[i];
733 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800734 }
Andreas Huber20dce082016-09-22 19:39:13 -0700735
Yifan Hong44ab6232016-11-22 16:28:24 -0800736 // Copies the data from the given std::array, using T::operator=(const T &).
737 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
738
Andreas Huber20dce082016-09-22 19:39:13 -0700739 T *data() { return mBuffer; }
740 const T *data() const { return mBuffer; }
741
742 T &operator[](size_t index) {
743 return mBuffer[index];
744 }
745
746 const T &operator[](size_t index) const {
747 return mBuffer[index];
748 }
749
Yifan Hong9fcbb362016-12-20 16:46:41 -0800750 // equality check, assuming that T::operator== is defined.
751 bool operator==(const hidl_array &other) const {
752 for (size_t i = 0; i < elementCount(); ++i) {
753 if (!(mBuffer[i] == other.mBuffer[i])) {
754 return false;
755 }
756 }
757 return true;
758 }
759
760 inline bool operator!=(const hidl_array &other) const {
761 return !((*this) == other);
762 }
763
Andreas Huber00a985c2016-09-28 14:24:53 -0700764 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800765 static constexpr size_t elementCount() { return SIZE1; }
766
767 // Copies the data to an std::array, using T::operator=(T).
768 operator std_array_type() const {
769 std_array_type array;
770 for (size_t i = 0; i < SIZE1; ++i) {
771 array[i] = mBuffer[i];
772 }
773 return array;
774 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700775
Andreas Huber20dce082016-09-22 19:39:13 -0700776private:
777 T mBuffer[SIZE1];
778};
779
Martijn Coenen72110162016-08-19 14:28:25 +0200780// ----------------------------------------------------------------------
781// Version functions
782struct hidl_version {
783public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800784 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200785
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700786 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200787 return (mMajor == other.get_major() && mMinor == other.get_minor());
788 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200789
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800790 bool operator<(const hidl_version& other) const {
791 return (mMajor < other.get_major() ||
792 (mMajor == other.get_major() && mMinor < other.get_minor()));
793 }
794
795 bool operator>(const hidl_version& other) const {
796 return other < *this;
797 }
798
799 bool operator<=(const hidl_version& other) const {
800 return !(*this > other);
801 }
802
803 bool operator>=(const hidl_version& other) const {
804 return !(*this < other);
805 }
806
Martijn Coenen097a7672016-09-08 16:56:41 +0200807 constexpr uint16_t get_major() const { return mMajor; }
808 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200809
Martijn Coenen72110162016-08-19 14:28:25 +0200810private:
811 uint16_t mMajor;
812 uint16_t mMinor;
813};
814
815inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
816 return hidl_version(major,minor);
817}
818
Yifan Hongbe7a6882017-01-05 17:30:17 -0800819///////////////////// toString functions
820
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800821std::string toString(const void *t);
Yifan Hongbe7a6882017-01-05 17:30:17 -0800822
823// toString alias for numeric types
824template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
825inline std::string toString(T t) {
826 return std::to_string(t);
827}
828
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800829namespace details {
830
Yifan Hongbe7a6882017-01-05 17:30:17 -0800831template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
832inline std::string toHexString(T t, bool prefix = true) {
833 std::ostringstream os;
834 if (prefix) { os << std::showbase; }
835 os << std::hex << t;
836 return os.str();
837}
838
839template<>
840inline std::string toHexString(uint8_t t, bool prefix) {
841 return toHexString(static_cast<int32_t>(t), prefix);
842}
843
844template<>
845inline std::string toHexString(int8_t t, bool prefix) {
846 return toHexString(static_cast<int32_t>(t), prefix);
847}
848
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800849template<typename Array>
850std::string arrayToString(const Array &a, size_t size);
851
852template<size_t SIZE1>
853std::string arraySizeToString() {
854 return std::string{"["} + toString(SIZE1) + "]";
855}
856
857template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
858std::string arraySizeToString() {
859 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
860}
861
862template<typename T, size_t SIZE1>
863std::string toString(details::const_accessor<T, SIZE1> a) {
864 return arrayToString(a, SIZE1);
865}
866
867template<typename Array>
868std::string arrayToString(const Array &a, size_t size) {
869 using android::hardware::toString;
870 std::string os;
871 os += "{";
872 for (size_t i = 0; i < size; ++i) {
873 if (i > 0) {
874 os += ", ";
875 }
876 os += toString(a[i]);
877 }
878 os += "}";
879 return os;
880}
881
882template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
883std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
884 return arrayToString(a, SIZE1);
885}
886
887} //namespace details
888
889inline std::string toString(const void *t) {
890 return details::toHexString(reinterpret_cast<uintptr_t>(t));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800891}
892
893// debug string dump. There will be quotes around the string!
894inline std::string toString(const hidl_string &hs) {
895 return std::string{"\""} + hs.c_str() + "\"";
896}
897
898// debug string dump
899inline std::string toString(const hidl_handle &hs) {
900 return toString(hs.getNativeHandle());
901}
902
903inline std::string toString(const hidl_memory &mem) {
904 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
905 + toString(mem.size())
906 + ", .handle = " + toString(mem.handle()) + "}";
907}
908
909inline std::string toString(const sp<hidl_death_recipient> &dr) {
910 return std::string{"death_recipient@"} + toString(dr.get());
911}
912
Yifan Hongbe7a6882017-01-05 17:30:17 -0800913// debug string dump, assuming that toString(T) is defined.
914template<typename T>
915std::string toString(const hidl_vec<T> &a) {
916 std::string os;
917 os += "[" + toString(a.size()) + "]";
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800918 os += details::arrayToString(a, a.size());
Yifan Hongbe7a6882017-01-05 17:30:17 -0800919 return os;
920}
921
Yifan Hongbe7a6882017-01-05 17:30:17 -0800922template<typename T, size_t SIZE1>
923std::string toString(const hidl_array<T, SIZE1> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800924 return details::arraySizeToString<SIZE1>()
925 + details::toString(details::const_accessor<T, SIZE1>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800926}
927
928template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
929std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800930 return details::arraySizeToString<SIZE1, SIZE2, SIZES...>()
931 + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800932}
933
Martijn Coenen72110162016-08-19 14:28:25 +0200934} // namespace hardware
935} // namespace android
936
Martijn Coenenc28f1152016-08-22 14:06:56 +0200937
Martijn Coenen72110162016-08-19 14:28:25 +0200938#endif // ANDROID_HIDL_SUPPORT_H