blob: 0137eed965c21412c7db4131a18677a9a2e010a2 [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 Huber6688d602017-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;
Steven Morelande03c0872016-10-24 10:43:50 -0700160
Martijn Coenen72110162016-08-19 14:28:25 +0200161 void clear();
162
163 // Reference an external char array. Ownership is _not_ transferred.
164 // Caller is responsible for ensuring that underlying memory is valid
165 // for the lifetime of this hidl_string.
166 void setToExternal(const char *data, size_t size);
167
Andreas Huberebfeb362016-08-25 13:39:05 -0700168 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
169 static const size_t kOffsetOfBuffer;
170
Martijn Coenen72110162016-08-19 14:28:25 +0200171private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100172 details::hidl_pointer<const char> mBuffer;
173 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700174 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200175
Yifan Hong602b85a2016-10-24 13:40:01 -0700176 // copy from data with size. Assume that my memory is freed
177 // (through clear(), for example)
178 void copyFrom(const char *data, size_t size);
179 // move from another hidl_string
180 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200181};
182
Steven Moreland551396a2017-02-13 18:33:20 -0800183#define HIDL_STRING_OPERATOR(OP) \
184 inline bool operator OP(const hidl_string &hs1, const hidl_string &hs2) { \
185 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; \
186 } \
187 inline bool operator OP(const hidl_string &hs, const char *s) { \
188 return strcmp(hs.c_str(), s) OP 0; \
189 } \
190 inline bool operator OP(const char *s, const hidl_string &hs) { \
191 return strcmp(hs.c_str(), s) OP 0; \
192 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800193
Steven Moreland551396a2017-02-13 18:33:20 -0800194HIDL_STRING_OPERATOR(==)
195HIDL_STRING_OPERATOR(!=)
196HIDL_STRING_OPERATOR(<)
197HIDL_STRING_OPERATOR(<=)
198HIDL_STRING_OPERATOR(>)
199HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800200
Steven Moreland551396a2017-02-13 18:33:20 -0800201#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700202
Scott Randolph0c84ab42017-04-03 14:07:14 -0700203// Send our content to the output stream
204std::ostream& operator<<(std::ostream& os, const hidl_string& str);
205
206
Martijn Coenen30791002016-12-01 15:40:46 +0100207// hidl_memory is a structure that can be used to transfer
208// pieces of shared memory between processes. The assumption
209// of this object is that the memory remains accessible as
210// long as the file descriptors in the enclosed mHandle
211// - as well as all of its cross-process dups() - remain opened.
212struct hidl_memory {
213
Martijn Coenen04b91c02017-01-19 14:14:21 +0100214 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100215 }
216
217 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100218 * Creates a hidl_memory object, but doesn't take ownership of
219 * the passed in native_handle_t; callers are responsible for
220 * making sure the handle remains valid while this object is
221 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100222 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100223 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
224 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100225 mSize(size),
226 mName(name)
227 {}
228
229 // copy constructor
230 hidl_memory(const hidl_memory& other) {
231 *this = other;
232 }
233
234 // copy assignment
235 hidl_memory &operator=(const hidl_memory &other) {
236 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100237 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100238 mSize = other.mSize;
239 mName = other.mName;
240 }
241
242 return *this;
243 }
244
Hridya Valsaraju01268892017-02-27 08:48:38 -0800245 // move constructor
246 hidl_memory(hidl_memory&& other) {
247 *this = std::move(other);
248 }
249
250 // move assignment
251 hidl_memory &operator=(hidl_memory &&other) {
252 if (this != &other) {
253 mHandle = std::move(other.mHandle);
254 mSize = other.mSize;
255 mName = std::move(other.mName);
256 other.mSize = 0;
257 }
258
259 return *this;
260 }
261
Martijn Coenen30791002016-12-01 15:40:46 +0100262
263 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100264 }
265
266 const native_handle_t* handle() const {
267 return mHandle;
268 }
269
270 const hidl_string &name() const {
271 return mName;
272 }
273
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800274 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100275 return mSize;
276 }
277
278 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
279 static const size_t kOffsetOfHandle;
280 // offsetof(hidl_memory, mName) exposed since mHandle is private.
281 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800282
Martijn Coenen30791002016-12-01 15:40:46 +0100283private:
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800284 hidl_handle mHandle __attribute__ ((aligned(8)));
285 uint64_t mSize __attribute__ ((aligned(8)));
286 hidl_string mName __attribute__ ((aligned(8)));
Martijn Coenen30791002016-12-01 15:40:46 +0100287};
288
Andreas Huber20dce082016-09-22 19:39:13 -0700289////////////////////////////////////////////////////////////////////////////////
290
Martijn Coenen72110162016-08-19 14:28:25 +0200291template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800292struct hidl_vec {
Martijn Coenen72110162016-08-19 14:28:25 +0200293 hidl_vec()
294 : mBuffer(NULL),
295 mSize(0),
296 mOwnsBuffer(true) {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800297 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Martijn Coenen72110162016-08-19 14:28:25 +0200298 }
299
Yifan Hong602b85a2016-10-24 13:40:01 -0700300 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200301 *this = other;
302 }
303
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100304 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800305 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100306 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700307 }
308
Steven Morelandb69926a2016-11-15 10:02:57 -0800309 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800310 : mOwnsBuffer(true) {
311 if (list.size() > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800312 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800313 }
314 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800315 mBuffer = new T[mSize];
316
Steven Morelandb69926a2016-11-15 10:02:57 -0800317 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800318 for (auto it = list.begin(); it != list.end(); ++it) {
319 mBuffer[idx++] = *it;
320 }
321 }
322
Yifan Hong602b85a2016-10-24 13:40:01 -0700323 hidl_vec(const std::vector<T> &other) : hidl_vec() {
324 *this = other;
325 }
326
Martijn Coenen72110162016-08-19 14:28:25 +0200327 ~hidl_vec() {
328 if (mOwnsBuffer) {
329 delete[] mBuffer;
330 }
331 mBuffer = NULL;
332 }
333
Alexey Polyudove2299012016-10-19 09:52:00 -0700334 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200335 // caller's responsibility to ensure that the underlying memory stays
336 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700337 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200338 if (mOwnsBuffer) {
339 delete [] mBuffer;
340 }
341 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100342 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800343 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100344 }
345 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700346 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200347 }
348
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700349 T *data() {
350 return mBuffer;
351 }
352
353 const T *data() const {
354 return mBuffer;
355 }
356
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700357 T *releaseData() {
358 if (!mOwnsBuffer && mSize > 0) {
359 resize(mSize);
360 }
361 mOwnsBuffer = false;
362 return mBuffer;
363 }
364
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700365 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100366 if (mOwnsBuffer) {
367 delete[] mBuffer;
368 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700369 mBuffer = other.mBuffer;
370 mSize = other.mSize;
371 mOwnsBuffer = other.mOwnsBuffer;
372 other.mOwnsBuffer = false;
373 return *this;
374 }
375
Martijn Coenen72110162016-08-19 14:28:25 +0200376 hidl_vec &operator=(const hidl_vec &other) {
377 if (this != &other) {
378 if (mOwnsBuffer) {
379 delete[] mBuffer;
380 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700381 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200382 }
383
384 return *this;
385 }
386
Yifan Hong602b85a2016-10-24 13:40:01 -0700387 // copy from an std::vector.
388 hidl_vec &operator=(const std::vector<T> &other) {
389 if (mOwnsBuffer) {
390 delete[] mBuffer;
391 }
392 copyFrom(other, other.size());
393 return *this;
394 }
395
396 // cast to an std::vector.
397 operator std::vector<T>() const {
398 std::vector<T> v(mSize);
399 for (size_t i = 0; i < mSize; ++i) {
400 v[i] = mBuffer[i];
401 }
402 return v;
403 }
404
Yifan Hong9fcbb362016-12-20 16:46:41 -0800405 // equality check, assuming that T::operator== is defined.
406 bool operator==(const hidl_vec &other) const {
407 if (mSize != other.size()) {
408 return false;
409 }
410 for (size_t i = 0; i < mSize; ++i) {
411 if (!(mBuffer[i] == other.mBuffer[i])) {
412 return false;
413 }
414 }
415 return true;
416 }
417
418 // inequality check, assuming that T::operator== is defined.
419 inline bool operator!=(const hidl_vec &other) const {
420 return !((*this) == other);
421 }
422
Martijn Coenen72110162016-08-19 14:28:25 +0200423 size_t size() const {
424 return mSize;
425 }
426
427 T &operator[](size_t index) {
428 return mBuffer[index];
429 }
430
431 const T &operator[](size_t index) const {
432 return mBuffer[index];
433 }
434
435 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100436 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800437 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100438 }
Martijn Coenen72110162016-08-19 14:28:25 +0200439 T *newBuffer = new T[size];
440
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100441 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200442 newBuffer[i] = mBuffer[i];
443 }
444
445 if (mOwnsBuffer) {
446 delete[] mBuffer;
447 }
448 mBuffer = newBuffer;
449
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100450 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200451 mOwnsBuffer = true;
452 }
453
Yifan Hong089ae132016-11-11 11:32:52 -0800454 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
455 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800456
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800457private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800458 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800459 template<bool is_const>
460 class iter : public std::iterator<
461 std::random_access_iterator_tag, /* Category */
462 T,
463 ptrdiff_t, /* Distance */
464 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
465 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800466 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800467 using traits = std::iterator_traits<iter>;
468 using ptr_type = typename traits::pointer;
469 using ref_type = typename traits::reference;
470 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800471 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800472 iter(ptr_type ptr) : mPtr(ptr) { }
473 inline iter &operator++() { mPtr++; return *this; }
474 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
475 inline iter &operator--() { mPtr--; return *this; }
476 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
477 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
478 inline iter operator+(diff_type n) const { return mPtr + n; }
479 inline iter operator-(diff_type n) const { return mPtr - n; }
480 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
481 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
482 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
483 inline ref_type operator*() const { return *mPtr; }
484 inline ptr_type operator->() const { return 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 bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
491 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800492 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800493 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800494 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800495public:
496 using iterator = iter<false /* is_const */>;
497 using const_iterator = iter<true /* is_const */>;
498
Scott Randolphbb840f72016-11-21 14:39:26 -0800499 iterator begin() { return data(); }
500 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800501 const_iterator begin() const { return data(); }
502 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800503
Martijn Coenen72110162016-08-19 14:28:25 +0200504private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100505 details::hidl_pointer<T> mBuffer;
506 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200507 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700508
509 // copy from an array-like object, assuming my resources are freed.
510 template <typename Array>
511 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800512 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700513 mOwnsBuffer = true;
514 if (mSize > 0) {
515 mBuffer = new T[size];
516 for (size_t i = 0; i < size; ++i) {
517 mBuffer[i] = data[i];
518 }
519 } else {
520 mBuffer = NULL;
521 }
522 }
Martijn Coenen72110162016-08-19 14:28:25 +0200523};
524
Yifan Hong089ae132016-11-11 11:32:52 -0800525template <typename T>
526const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
527
Andreas Huber20dce082016-09-22 19:39:13 -0700528////////////////////////////////////////////////////////////////////////////////
529
530namespace details {
531
532 template<size_t SIZE1, size_t... SIZES>
533 struct product {
534 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
535 };
536
537 template<size_t SIZE1>
538 struct product<SIZE1> {
539 static constexpr size_t value = SIZE1;
540 };
541
542 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800543 struct std_array {
544 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
545 };
546
547 template<typename T, size_t SIZE1>
548 struct std_array<T, SIZE1> {
549 using type = std::array<T, SIZE1>;
550 };
551
552 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700553 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800554
555 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
556
Andreas Huber20dce082016-09-22 19:39:13 -0700557 explicit accessor(T *base)
558 : mBase(base) {
559 }
560
561 accessor<T, SIZES...> operator[](size_t index) {
562 return accessor<T, SIZES...>(
563 &mBase[index * product<SIZES...>::value]);
564 }
565
Yifan Hong44ab6232016-11-22 16:28:24 -0800566 accessor &operator=(const std_array_type &other) {
567 for (size_t i = 0; i < SIZE1; ++i) {
568 (*this)[i] = other[i];
569 }
570 return *this;
571 }
572
Andreas Huber20dce082016-09-22 19:39:13 -0700573 private:
574 T *mBase;
575 };
576
577 template<typename T, size_t SIZE1>
578 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800579
580 using std_array_type = typename std_array<T, SIZE1>::type;
581
Andreas Huber20dce082016-09-22 19:39:13 -0700582 explicit accessor(T *base)
583 : mBase(base) {
584 }
585
586 T &operator[](size_t index) {
587 return mBase[index];
588 }
589
Yifan Hong44ab6232016-11-22 16:28:24 -0800590 accessor &operator=(const std_array_type &other) {
591 for (size_t i = 0; i < SIZE1; ++i) {
592 (*this)[i] = other[i];
593 }
594 return *this;
595 }
596
Andreas Huber20dce082016-09-22 19:39:13 -0700597 private:
598 T *mBase;
599 };
600
601 template<typename T, size_t SIZE1, size_t... SIZES>
602 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800603
604 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
605
Andreas Huber20dce082016-09-22 19:39:13 -0700606 explicit const_accessor(const T *base)
607 : mBase(base) {
608 }
609
Yifan Hongbe7a6882017-01-05 17:30:17 -0800610 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700611 return const_accessor<T, SIZES...>(
612 &mBase[index * product<SIZES...>::value]);
613 }
614
Yifan Hong44ab6232016-11-22 16:28:24 -0800615 operator std_array_type() {
616 std_array_type array;
617 for (size_t i = 0; i < SIZE1; ++i) {
618 array[i] = (*this)[i];
619 }
620 return array;
621 }
622
Andreas Huber20dce082016-09-22 19:39:13 -0700623 private:
624 const T *mBase;
625 };
626
627 template<typename T, size_t SIZE1>
628 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800629
630 using std_array_type = typename std_array<T, SIZE1>::type;
631
Andreas Huber20dce082016-09-22 19:39:13 -0700632 explicit const_accessor(const T *base)
633 : mBase(base) {
634 }
635
636 const T &operator[](size_t index) const {
637 return mBase[index];
638 }
639
Yifan Hong44ab6232016-11-22 16:28:24 -0800640 operator std_array_type() {
641 std_array_type array;
642 for (size_t i = 0; i < SIZE1; ++i) {
643 array[i] = (*this)[i];
644 }
645 return array;
646 }
647
Andreas Huber20dce082016-09-22 19:39:13 -0700648 private:
649 const T *mBase;
650 };
651
652} // namespace details
653
654////////////////////////////////////////////////////////////////////////////////
655
Yifan Hong44ab6232016-11-22 16:28:24 -0800656// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700657template<typename T, size_t SIZE1, size_t... SIZES>
658struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800659
660 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
661
Andreas Huber20dce082016-09-22 19:39:13 -0700662 hidl_array() = default;
663
Yifan Hong44ab6232016-11-22 16:28:24 -0800664 // Copies the data from source, using T::operator=(const T &).
665 hidl_array(const T *source) {
666 for (size_t i = 0; i < elementCount(); ++i) {
667 mBuffer[i] = source[i];
668 }
669 }
670
671 // Copies the data from the given std::array, using T::operator=(const T &).
672 hidl_array(const std_array_type &array) {
673 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
674 modifier = array;
675 }
676
Andreas Huber20dce082016-09-22 19:39:13 -0700677 T *data() { return mBuffer; }
678 const T *data() const { return mBuffer; }
679
680 details::accessor<T, SIZES...> operator[](size_t index) {
681 return details::accessor<T, SIZES...>(
682 &mBuffer[index * details::product<SIZES...>::value]);
683 }
684
685 details::const_accessor<T, SIZES...> operator[](size_t index) const {
686 return details::const_accessor<T, SIZES...>(
687 &mBuffer[index * details::product<SIZES...>::value]);
688 }
689
Yifan Hong9fcbb362016-12-20 16:46:41 -0800690 // equality check, assuming that T::operator== is defined.
691 bool operator==(const hidl_array &other) const {
692 for (size_t i = 0; i < elementCount(); ++i) {
693 if (!(mBuffer[i] == other.mBuffer[i])) {
694 return false;
695 }
696 }
697 return true;
698 }
699
700 inline bool operator!=(const hidl_array &other) const {
701 return !((*this) == other);
702 }
703
Andreas Huber00a985c2016-09-28 14:24:53 -0700704 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
705
706 static constexpr size_tuple_type size() {
707 return std::make_tuple(SIZE1, SIZES...);
708 }
709
Yifan Hong44ab6232016-11-22 16:28:24 -0800710 static constexpr size_t elementCount() {
711 return details::product<SIZE1, SIZES...>::value;
712 }
713
714 operator std_array_type() const {
715 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
716 }
717
Andreas Huber20dce082016-09-22 19:39:13 -0700718private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800719 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700720};
721
Yifan Hong44ab6232016-11-22 16:28:24 -0800722// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700723template<typename T, size_t SIZE1>
724struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800725
726 using std_array_type = typename details::std_array<T, SIZE1>::type;
727
Andreas Huber20dce082016-09-22 19:39:13 -0700728 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800729
730 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800731 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800732 for (size_t i = 0; i < elementCount(); ++i) {
733 mBuffer[i] = source[i];
734 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800735 }
Andreas Huber20dce082016-09-22 19:39:13 -0700736
Yifan Hong44ab6232016-11-22 16:28:24 -0800737 // Copies the data from the given std::array, using T::operator=(const T &).
738 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
739
Andreas Huber20dce082016-09-22 19:39:13 -0700740 T *data() { return mBuffer; }
741 const T *data() const { return mBuffer; }
742
743 T &operator[](size_t index) {
744 return mBuffer[index];
745 }
746
747 const T &operator[](size_t index) const {
748 return mBuffer[index];
749 }
750
Yifan Hong9fcbb362016-12-20 16:46:41 -0800751 // equality check, assuming that T::operator== is defined.
752 bool operator==(const hidl_array &other) const {
753 for (size_t i = 0; i < elementCount(); ++i) {
754 if (!(mBuffer[i] == other.mBuffer[i])) {
755 return false;
756 }
757 }
758 return true;
759 }
760
761 inline bool operator!=(const hidl_array &other) const {
762 return !((*this) == other);
763 }
764
Andreas Huber00a985c2016-09-28 14:24:53 -0700765 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800766 static constexpr size_t elementCount() { return SIZE1; }
767
768 // Copies the data to an std::array, using T::operator=(T).
769 operator std_array_type() const {
770 std_array_type array;
771 for (size_t i = 0; i < SIZE1; ++i) {
772 array[i] = mBuffer[i];
773 }
774 return array;
775 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700776
Andreas Huber20dce082016-09-22 19:39:13 -0700777private:
778 T mBuffer[SIZE1];
779};
780
Martijn Coenen72110162016-08-19 14:28:25 +0200781// ----------------------------------------------------------------------
782// Version functions
783struct hidl_version {
784public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800785 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200786
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700787 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200788 return (mMajor == other.get_major() && mMinor == other.get_minor());
789 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200790
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800791 bool operator<(const hidl_version& other) const {
792 return (mMajor < other.get_major() ||
793 (mMajor == other.get_major() && mMinor < other.get_minor()));
794 }
795
796 bool operator>(const hidl_version& other) const {
797 return other < *this;
798 }
799
800 bool operator<=(const hidl_version& other) const {
801 return !(*this > other);
802 }
803
804 bool operator>=(const hidl_version& other) const {
805 return !(*this < other);
806 }
807
Martijn Coenen097a7672016-09-08 16:56:41 +0200808 constexpr uint16_t get_major() const { return mMajor; }
809 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200810
Martijn Coenen72110162016-08-19 14:28:25 +0200811private:
812 uint16_t mMajor;
813 uint16_t mMinor;
814};
815
816inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
817 return hidl_version(major,minor);
818}
819
Yifan Hongbe7a6882017-01-05 17:30:17 -0800820///////////////////// toString functions
821
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800822std::string toString(const void *t);
Yifan Hongbe7a6882017-01-05 17:30:17 -0800823
824// toString alias for numeric types
825template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
826inline std::string toString(T t) {
827 return std::to_string(t);
828}
829
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800830namespace details {
831
Yifan Hongbe7a6882017-01-05 17:30:17 -0800832template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
833inline std::string toHexString(T t, bool prefix = true) {
834 std::ostringstream os;
835 if (prefix) { os << std::showbase; }
836 os << std::hex << t;
837 return os.str();
838}
839
840template<>
841inline std::string toHexString(uint8_t t, bool prefix) {
842 return toHexString(static_cast<int32_t>(t), prefix);
843}
844
845template<>
846inline std::string toHexString(int8_t t, bool prefix) {
847 return toHexString(static_cast<int32_t>(t), prefix);
848}
849
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800850template<typename Array>
851std::string arrayToString(const Array &a, size_t size);
852
853template<size_t SIZE1>
854std::string arraySizeToString() {
855 return std::string{"["} + toString(SIZE1) + "]";
856}
857
858template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
859std::string arraySizeToString() {
860 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
861}
862
863template<typename T, size_t SIZE1>
864std::string toString(details::const_accessor<T, SIZE1> a) {
865 return arrayToString(a, SIZE1);
866}
867
868template<typename Array>
869std::string arrayToString(const Array &a, size_t size) {
870 using android::hardware::toString;
871 std::string os;
872 os += "{";
873 for (size_t i = 0; i < size; ++i) {
874 if (i > 0) {
875 os += ", ";
876 }
877 os += toString(a[i]);
878 }
879 os += "}";
880 return os;
881}
882
883template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
884std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
885 return arrayToString(a, SIZE1);
886}
887
888} //namespace details
889
890inline std::string toString(const void *t) {
891 return details::toHexString(reinterpret_cast<uintptr_t>(t));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800892}
893
894// debug string dump. There will be quotes around the string!
895inline std::string toString(const hidl_string &hs) {
896 return std::string{"\""} + hs.c_str() + "\"";
897}
898
899// debug string dump
900inline std::string toString(const hidl_handle &hs) {
901 return toString(hs.getNativeHandle());
902}
903
904inline std::string toString(const hidl_memory &mem) {
905 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
906 + toString(mem.size())
907 + ", .handle = " + toString(mem.handle()) + "}";
908}
909
910inline std::string toString(const sp<hidl_death_recipient> &dr) {
911 return std::string{"death_recipient@"} + toString(dr.get());
912}
913
Yifan Hongbe7a6882017-01-05 17:30:17 -0800914// debug string dump, assuming that toString(T) is defined.
915template<typename T>
916std::string toString(const hidl_vec<T> &a) {
917 std::string os;
918 os += "[" + toString(a.size()) + "]";
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800919 os += details::arrayToString(a, a.size());
Yifan Hongbe7a6882017-01-05 17:30:17 -0800920 return os;
921}
922
Yifan Hongbe7a6882017-01-05 17:30:17 -0800923template<typename T, size_t SIZE1>
924std::string toString(const hidl_array<T, SIZE1> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800925 return details::arraySizeToString<SIZE1>()
926 + details::toString(details::const_accessor<T, SIZE1>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800927}
928
929template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
930std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800931 return details::arraySizeToString<SIZE1, SIZE2, SIZES...>()
932 + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800933}
934
Martijn Coenen72110162016-08-19 14:28:25 +0200935} // namespace hardware
936} // namespace android
937
Martijn Coenenc28f1152016-08-22 14:06:56 +0200938
Martijn Coenen72110162016-08-19 14:28:25 +0200939#endif // ANDROID_HIDL_SUPPORT_H