blob: 77370119db069473f90ec52e8ad20a35047d6f7d [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
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100126 details::hidl_pointer<const native_handle_t> mHandle;
Martijn Coenen04b91c02017-01-19 14:14:21 +0100127 bool mOwnsHandle;
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
273 size_t size() const {
274 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:
Martijn Coenen30791002016-12-01 15:40:46 +0100283 hidl_handle mHandle;
284 size_t mSize;
285 hidl_string mName;
286};
287
Andreas Huber20dce082016-09-22 19:39:13 -0700288////////////////////////////////////////////////////////////////////////////////
289
Martijn Coenen72110162016-08-19 14:28:25 +0200290template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100291struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200292 hidl_vec()
293 : mBuffer(NULL),
294 mSize(0),
295 mOwnsBuffer(true) {
296 }
297
Yifan Hong602b85a2016-10-24 13:40:01 -0700298 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200299 *this = other;
300 }
301
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100302 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800303 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100304 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700305 }
306
Steven Morelandb69926a2016-11-15 10:02:57 -0800307 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800308 : mOwnsBuffer(true) {
309 if (list.size() > UINT32_MAX) {
310 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
311 }
312 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800313 mBuffer = new T[mSize];
314
Steven Morelandb69926a2016-11-15 10:02:57 -0800315 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800316 for (auto it = list.begin(); it != list.end(); ++it) {
317 mBuffer[idx++] = *it;
318 }
319 }
320
Yifan Hong602b85a2016-10-24 13:40:01 -0700321 hidl_vec(const std::vector<T> &other) : hidl_vec() {
322 *this = other;
323 }
324
Martijn Coenen72110162016-08-19 14:28:25 +0200325 ~hidl_vec() {
326 if (mOwnsBuffer) {
327 delete[] mBuffer;
328 }
329 mBuffer = NULL;
330 }
331
Alexey Polyudove2299012016-10-19 09:52:00 -0700332 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200333 // caller's responsibility to ensure that the underlying memory stays
334 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700335 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200336 if (mOwnsBuffer) {
337 delete [] mBuffer;
338 }
339 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100340 if (size > UINT32_MAX) {
341 logAlwaysFatal("external vector size exceeds 2^32 elements.");
342 }
343 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700344 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200345 }
346
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700347 T *data() {
348 return mBuffer;
349 }
350
351 const T *data() const {
352 return mBuffer;
353 }
354
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700355 T *releaseData() {
356 if (!mOwnsBuffer && mSize > 0) {
357 resize(mSize);
358 }
359 mOwnsBuffer = false;
360 return mBuffer;
361 }
362
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700363 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100364 if (mOwnsBuffer) {
365 delete[] mBuffer;
366 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700367 mBuffer = other.mBuffer;
368 mSize = other.mSize;
369 mOwnsBuffer = other.mOwnsBuffer;
370 other.mOwnsBuffer = false;
371 return *this;
372 }
373
Martijn Coenen72110162016-08-19 14:28:25 +0200374 hidl_vec &operator=(const hidl_vec &other) {
375 if (this != &other) {
376 if (mOwnsBuffer) {
377 delete[] mBuffer;
378 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700379 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200380 }
381
382 return *this;
383 }
384
Yifan Hong602b85a2016-10-24 13:40:01 -0700385 // copy from an std::vector.
386 hidl_vec &operator=(const std::vector<T> &other) {
387 if (mOwnsBuffer) {
388 delete[] mBuffer;
389 }
390 copyFrom(other, other.size());
391 return *this;
392 }
393
394 // cast to an std::vector.
395 operator std::vector<T>() const {
396 std::vector<T> v(mSize);
397 for (size_t i = 0; i < mSize; ++i) {
398 v[i] = mBuffer[i];
399 }
400 return v;
401 }
402
Yifan Hong9fcbb362016-12-20 16:46:41 -0800403 // equality check, assuming that T::operator== is defined.
404 bool operator==(const hidl_vec &other) const {
405 if (mSize != other.size()) {
406 return false;
407 }
408 for (size_t i = 0; i < mSize; ++i) {
409 if (!(mBuffer[i] == other.mBuffer[i])) {
410 return false;
411 }
412 }
413 return true;
414 }
415
416 // inequality check, assuming that T::operator== is defined.
417 inline bool operator!=(const hidl_vec &other) const {
418 return !((*this) == other);
419 }
420
Martijn Coenen72110162016-08-19 14:28:25 +0200421 size_t size() const {
422 return mSize;
423 }
424
425 T &operator[](size_t index) {
426 return mBuffer[index];
427 }
428
429 const T &operator[](size_t index) const {
430 return mBuffer[index];
431 }
432
433 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100434 if (size > UINT32_MAX) {
435 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
436 }
Martijn Coenen72110162016-08-19 14:28:25 +0200437 T *newBuffer = new T[size];
438
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100439 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200440 newBuffer[i] = mBuffer[i];
441 }
442
443 if (mOwnsBuffer) {
444 delete[] mBuffer;
445 }
446 mBuffer = newBuffer;
447
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100448 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200449 mOwnsBuffer = true;
450 }
451
Yifan Hong089ae132016-11-11 11:32:52 -0800452 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
453 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800454
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800455private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800456 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800457 template<bool is_const>
458 class iter : public std::iterator<
459 std::random_access_iterator_tag, /* Category */
460 T,
461 ptrdiff_t, /* Distance */
462 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
463 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800464 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800465 using traits = std::iterator_traits<iter>;
466 using ptr_type = typename traits::pointer;
467 using ref_type = typename traits::reference;
468 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800469 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800470 iter(ptr_type ptr) : mPtr(ptr) { }
471 inline iter &operator++() { mPtr++; return *this; }
472 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
473 inline iter &operator--() { mPtr--; return *this; }
474 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
475 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
476 inline iter operator+(diff_type n) const { return mPtr + n; }
477 inline iter operator-(diff_type n) const { return mPtr - n; }
478 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
479 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
480 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
481 inline ref_type operator*() const { return *mPtr; }
482 inline ptr_type operator->() const { return mPtr; }
483 inline bool operator==(const iter &rhs) const { return mPtr == rhs.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 ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800490 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800491 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800492 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800493public:
494 using iterator = iter<false /* is_const */>;
495 using const_iterator = iter<true /* is_const */>;
496
Scott Randolphbb840f72016-11-21 14:39:26 -0800497 iterator begin() { return data(); }
498 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800499 const_iterator begin() const { return data(); }
500 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800501
Martijn Coenen72110162016-08-19 14:28:25 +0200502private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100503 details::hidl_pointer<T> mBuffer;
504 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200505 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700506
507 // copy from an array-like object, assuming my resources are freed.
508 template <typename Array>
509 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800510 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700511 mOwnsBuffer = true;
512 if (mSize > 0) {
513 mBuffer = new T[size];
514 for (size_t i = 0; i < size; ++i) {
515 mBuffer[i] = data[i];
516 }
517 } else {
518 mBuffer = NULL;
519 }
520 }
Martijn Coenen72110162016-08-19 14:28:25 +0200521};
522
Yifan Hong089ae132016-11-11 11:32:52 -0800523template <typename T>
524const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
525
Andreas Huber20dce082016-09-22 19:39:13 -0700526////////////////////////////////////////////////////////////////////////////////
527
528namespace details {
529
530 template<size_t SIZE1, size_t... SIZES>
531 struct product {
532 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
533 };
534
535 template<size_t SIZE1>
536 struct product<SIZE1> {
537 static constexpr size_t value = SIZE1;
538 };
539
540 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800541 struct std_array {
542 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
543 };
544
545 template<typename T, size_t SIZE1>
546 struct std_array<T, SIZE1> {
547 using type = std::array<T, SIZE1>;
548 };
549
550 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700551 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800552
553 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
554
Andreas Huber20dce082016-09-22 19:39:13 -0700555 explicit accessor(T *base)
556 : mBase(base) {
557 }
558
559 accessor<T, SIZES...> operator[](size_t index) {
560 return accessor<T, SIZES...>(
561 &mBase[index * product<SIZES...>::value]);
562 }
563
Yifan Hong44ab6232016-11-22 16:28:24 -0800564 accessor &operator=(const std_array_type &other) {
565 for (size_t i = 0; i < SIZE1; ++i) {
566 (*this)[i] = other[i];
567 }
568 return *this;
569 }
570
Andreas Huber20dce082016-09-22 19:39:13 -0700571 private:
572 T *mBase;
573 };
574
575 template<typename T, size_t SIZE1>
576 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800577
578 using std_array_type = typename std_array<T, SIZE1>::type;
579
Andreas Huber20dce082016-09-22 19:39:13 -0700580 explicit accessor(T *base)
581 : mBase(base) {
582 }
583
584 T &operator[](size_t index) {
585 return mBase[index];
586 }
587
Yifan Hong44ab6232016-11-22 16:28:24 -0800588 accessor &operator=(const std_array_type &other) {
589 for (size_t i = 0; i < SIZE1; ++i) {
590 (*this)[i] = other[i];
591 }
592 return *this;
593 }
594
Andreas Huber20dce082016-09-22 19:39:13 -0700595 private:
596 T *mBase;
597 };
598
599 template<typename T, size_t SIZE1, size_t... SIZES>
600 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800601
602 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
603
Andreas Huber20dce082016-09-22 19:39:13 -0700604 explicit const_accessor(const T *base)
605 : mBase(base) {
606 }
607
Yifan Hongbe7a6882017-01-05 17:30:17 -0800608 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700609 return const_accessor<T, SIZES...>(
610 &mBase[index * product<SIZES...>::value]);
611 }
612
Yifan Hong44ab6232016-11-22 16:28:24 -0800613 operator std_array_type() {
614 std_array_type array;
615 for (size_t i = 0; i < SIZE1; ++i) {
616 array[i] = (*this)[i];
617 }
618 return array;
619 }
620
Andreas Huber20dce082016-09-22 19:39:13 -0700621 private:
622 const T *mBase;
623 };
624
625 template<typename T, size_t SIZE1>
626 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800627
628 using std_array_type = typename std_array<T, SIZE1>::type;
629
Andreas Huber20dce082016-09-22 19:39:13 -0700630 explicit const_accessor(const T *base)
631 : mBase(base) {
632 }
633
634 const T &operator[](size_t index) const {
635 return mBase[index];
636 }
637
Yifan Hong44ab6232016-11-22 16:28:24 -0800638 operator std_array_type() {
639 std_array_type array;
640 for (size_t i = 0; i < SIZE1; ++i) {
641 array[i] = (*this)[i];
642 }
643 return array;
644 }
645
Andreas Huber20dce082016-09-22 19:39:13 -0700646 private:
647 const T *mBase;
648 };
649
650} // namespace details
651
652////////////////////////////////////////////////////////////////////////////////
653
Yifan Hong44ab6232016-11-22 16:28:24 -0800654// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700655template<typename T, size_t SIZE1, size_t... SIZES>
656struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800657
658 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
659
Andreas Huber20dce082016-09-22 19:39:13 -0700660 hidl_array() = default;
661
Yifan Hong44ab6232016-11-22 16:28:24 -0800662 // Copies the data from source, using T::operator=(const T &).
663 hidl_array(const T *source) {
664 for (size_t i = 0; i < elementCount(); ++i) {
665 mBuffer[i] = source[i];
666 }
667 }
668
669 // Copies the data from the given std::array, using T::operator=(const T &).
670 hidl_array(const std_array_type &array) {
671 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
672 modifier = array;
673 }
674
Andreas Huber20dce082016-09-22 19:39:13 -0700675 T *data() { return mBuffer; }
676 const T *data() const { return mBuffer; }
677
678 details::accessor<T, SIZES...> operator[](size_t index) {
679 return details::accessor<T, SIZES...>(
680 &mBuffer[index * details::product<SIZES...>::value]);
681 }
682
683 details::const_accessor<T, SIZES...> operator[](size_t index) const {
684 return details::const_accessor<T, SIZES...>(
685 &mBuffer[index * details::product<SIZES...>::value]);
686 }
687
Yifan Hong9fcbb362016-12-20 16:46:41 -0800688 // equality check, assuming that T::operator== is defined.
689 bool operator==(const hidl_array &other) const {
690 for (size_t i = 0; i < elementCount(); ++i) {
691 if (!(mBuffer[i] == other.mBuffer[i])) {
692 return false;
693 }
694 }
695 return true;
696 }
697
698 inline bool operator!=(const hidl_array &other) const {
699 return !((*this) == other);
700 }
701
Andreas Huber00a985c2016-09-28 14:24:53 -0700702 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
703
704 static constexpr size_tuple_type size() {
705 return std::make_tuple(SIZE1, SIZES...);
706 }
707
Yifan Hong44ab6232016-11-22 16:28:24 -0800708 static constexpr size_t elementCount() {
709 return details::product<SIZE1, SIZES...>::value;
710 }
711
712 operator std_array_type() const {
713 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
714 }
715
Andreas Huber20dce082016-09-22 19:39:13 -0700716private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800717 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700718};
719
Yifan Hong44ab6232016-11-22 16:28:24 -0800720// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700721template<typename T, size_t SIZE1>
722struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800723
724 using std_array_type = typename details::std_array<T, SIZE1>::type;
725
Andreas Huber20dce082016-09-22 19:39:13 -0700726 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800727
728 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800729 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800730 for (size_t i = 0; i < elementCount(); ++i) {
731 mBuffer[i] = source[i];
732 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800733 }
Andreas Huber20dce082016-09-22 19:39:13 -0700734
Yifan Hong44ab6232016-11-22 16:28:24 -0800735 // Copies the data from the given std::array, using T::operator=(const T &).
736 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
737
Andreas Huber20dce082016-09-22 19:39:13 -0700738 T *data() { return mBuffer; }
739 const T *data() const { return mBuffer; }
740
741 T &operator[](size_t index) {
742 return mBuffer[index];
743 }
744
745 const T &operator[](size_t index) const {
746 return mBuffer[index];
747 }
748
Yifan Hong9fcbb362016-12-20 16:46:41 -0800749 // equality check, assuming that T::operator== is defined.
750 bool operator==(const hidl_array &other) const {
751 for (size_t i = 0; i < elementCount(); ++i) {
752 if (!(mBuffer[i] == other.mBuffer[i])) {
753 return false;
754 }
755 }
756 return true;
757 }
758
759 inline bool operator!=(const hidl_array &other) const {
760 return !((*this) == other);
761 }
762
Andreas Huber00a985c2016-09-28 14:24:53 -0700763 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800764 static constexpr size_t elementCount() { return SIZE1; }
765
766 // Copies the data to an std::array, using T::operator=(T).
767 operator std_array_type() const {
768 std_array_type array;
769 for (size_t i = 0; i < SIZE1; ++i) {
770 array[i] = mBuffer[i];
771 }
772 return array;
773 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700774
Andreas Huber20dce082016-09-22 19:39:13 -0700775private:
776 T mBuffer[SIZE1];
777};
778
Martijn Coenen72110162016-08-19 14:28:25 +0200779// ----------------------------------------------------------------------
780// Version functions
781struct hidl_version {
782public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800783 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200784
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700785 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200786 return (mMajor == other.get_major() && mMinor == other.get_minor());
787 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200788
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800789 bool operator<(const hidl_version& other) const {
790 return (mMajor < other.get_major() ||
791 (mMajor == other.get_major() && mMinor < other.get_minor()));
792 }
793
794 bool operator>(const hidl_version& other) const {
795 return other < *this;
796 }
797
798 bool operator<=(const hidl_version& other) const {
799 return !(*this > other);
800 }
801
802 bool operator>=(const hidl_version& other) const {
803 return !(*this < other);
804 }
805
Martijn Coenen097a7672016-09-08 16:56:41 +0200806 constexpr uint16_t get_major() const { return mMajor; }
807 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200808
Martijn Coenen72110162016-08-19 14:28:25 +0200809private:
810 uint16_t mMajor;
811 uint16_t mMinor;
812};
813
814inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
815 return hidl_version(major,minor);
816}
817
Yifan Hongbe7a6882017-01-05 17:30:17 -0800818///////////////////// toString functions
819
820namespace details {
821
822// toString alias for numeric types
823template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
824inline std::string toString(T t) {
825 return std::to_string(t);
826}
827
828template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
829inline std::string toHexString(T t, bool prefix = true) {
830 std::ostringstream os;
831 if (prefix) { os << std::showbase; }
832 os << std::hex << t;
833 return os.str();
834}
835
836template<>
837inline std::string toHexString(uint8_t t, bool prefix) {
838 return toHexString(static_cast<int32_t>(t), prefix);
839}
840
841template<>
842inline std::string toHexString(int8_t t, bool prefix) {
843 return toHexString(static_cast<int32_t>(t), prefix);
844}
845
846inline std::string toString(const void *t, bool prefix = true) {
847 return toHexString(reinterpret_cast<uintptr_t>(t), prefix);
848}
849
850// debug string dump. There will be quotes around the string!
851inline std::string toString(const hidl_string &hs) {
852 return std::string{"\""} + hs.c_str() + "\"";
853}
854
855// debug string dump
856inline std::string toString(const hidl_handle &hs) {
857 return toString(hs.getNativeHandle());
858}
859
860inline std::string toString(const hidl_memory &mem) {
861 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
862 + toString(mem.size())
863 + ", .handle = " + toString(mem.handle()) + "}";
864}
865
866inline std::string toString(const sp<hidl_death_recipient> &dr) {
867 return std::string{"death_recipient@"} + toString(dr.get());
868}
869
870template<typename Array>
871std::string arrayToString(const Array &a, size_t size);
872
873// debug string dump, assuming that toString(T) is defined.
874template<typename T>
875std::string toString(const hidl_vec<T> &a) {
876 std::string os;
877 os += "[" + toString(a.size()) + "]";
878 os += arrayToString(a, a.size());
879 return os;
880}
881
882template<size_t SIZE1>
883std::string arraySizeToString() {
884 return std::string{"["} + toString(SIZE1) + "]";
885}
886
887template<typename T, size_t SIZE1>
888std::string toString(const_accessor<T, SIZE1> a) {
889 return arrayToString(a, SIZE1);
890}
891
892template<typename T, size_t SIZE1>
893std::string toString(const hidl_array<T, SIZE1> &a) {
894 return arraySizeToString<SIZE1>()
895 + toString(const_accessor<T, SIZE1>(a.data()));
896}
897
898template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
899std::string arraySizeToString() {
900 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
901}
902
903
904template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
905std::string toString(const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
906 return arrayToString(a, SIZE1);
907}
908
909template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
910std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
911 return arraySizeToString<SIZE1, SIZE2, SIZES...>()
912 + toString(const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
913}
914
915template<typename Array>
916std::string arrayToString(const Array &a, size_t size) {
917 std::string os;
918 os += "{";
919 for (size_t i = 0; i < size; ++i) {
920 if (i > 0) {
921 os += ", ";
922 }
923 os += toString(a[i]);
924 }
925 os += "}";
926 return os;
927}
928
929} // namespace details
930
931
Martijn Coenen72110162016-08-19 14:28:25 +0200932} // namespace hardware
933} // namespace android
934
Martijn Coenenc28f1152016-08-22 14:06:56 +0200935
Martijn Coenen72110162016-08-19 14:28:25 +0200936#endif // ANDROID_HIDL_SUPPORT_H