blob: de5081d05b34fc0fc696ab1f952aeb8ec9e00ad2 [file] [log] [blame]
Martijn Coenen72110162016-08-19 14:28:25 +02001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HIDL_SUPPORT_H
18#define ANDROID_HIDL_SUPPORT_H
19
Iliyan Malchev692070a2016-09-12 16:30:44 -070020#include <algorithm>
Yifan Hong44ab6232016-11-22 16:28:24 -080021#include <array>
Scott Randolphbb840f72016-11-21 14:39:26 -080022#include <iterator>
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010023#include <cutils/native_handle.h>
Steven Moreland337c3ae2016-11-22 13:37:32 -080024#include <hidl/HidlInternal.h>
Yifan Honga3c31842016-10-21 10:33:14 -070025#include <hidl/Status.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080026#include <map>
Yifan Hongbe7a6882017-01-05 17:30:17 -080027#include <sstream>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080028#include <stddef.h>
Andreas Huber00a985c2016-09-28 14:24:53 -070029#include <tuple>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080030#include <type_traits>
Iliyan Malchev692070a2016-09-12 16:30:44 -070031#include <utils/Errors.h>
32#include <utils/RefBase.h>
33#include <utils/StrongPointer.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080034#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020035
36namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010037
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010038// this file is included by all hidl interface, so we must forward declare the
39// IMemory and IBase types.
Martijn Coenen30791002016-12-01 15:40:46 +010040namespace hidl {
41namespace memory {
42namespace V1_0 {
43 struct IMemory;
44}; // namespace V1_0
45}; // namespace manager
46}; // namespace hidl
47
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010048namespace hidl {
49namespace base {
50namespace V1_0 {
51 struct IBase;
52}; // namespace V1_0
53}; // namespace base
54}; // namespace hidl
55
Martijn Coenen72110162016-08-19 14:28:25 +020056namespace hardware {
57
Yifan Hong24332ef2017-03-07 16:22:19 -080058namespace details {
59// Return true on userdebug / eng builds and false on user builds.
60bool debuggable();
61} // namespace details
62
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010063// hidl_death_recipient is a callback interfaced that can be used with
64// linkToDeath() / unlinkToDeath()
65struct hidl_death_recipient : public virtual RefBase {
66 virtual void serviceDied(uint64_t cookie,
67 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
68};
69
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010070// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
71// so that it can safely be transferred between 32-bit and 64-bit processes.
Martijn Coenen04b91c02017-01-19 14:14:21 +010072// The ownership semantics for this are:
73// 1) The conversion constructor and assignment operator taking a const native_handle_t*
74// do not take ownership of the handle; this is because these operations are usually
75// just done for IPC, and cloning by default is a waste of resources. If you want
76// a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/);
77// 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership;
78// that is because it's not intuitive that this class encapsulates a native_handle_t
79// which needs cloning to be valid; in particular, this allows constructs like this:
80// hidl_handle copy;
81// foo->someHidlCall([&](auto incoming_handle) {
82// copy = incoming_handle;
83// });
84// // copy and its enclosed file descriptors will remain valid here.
85// 3) The move constructor does what you would expect; it only owns the handle if the
86// original did.
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010087struct hidl_handle {
Martijn Coenen04b91c02017-01-19 14:14:21 +010088 hidl_handle();
89 ~hidl_handle();
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010090
Martijn Coenen04b91c02017-01-19 14:14:21 +010091 hidl_handle(const native_handle_t *handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010092
Martijn Coenen04b91c02017-01-19 14:14:21 +010093 // copy constructor.
94 hidl_handle(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010095
96 // move constructor.
Martijn Coenen04b91c02017-01-19 14:14:21 +010097 hidl_handle(hidl_handle &&other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010098
Steven Moreland6ffdc2a2017-01-23 12:34:33 -080099 // assignment operators
Martijn Coenen04b91c02017-01-19 14:14:21 +0100100 hidl_handle &operator=(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100101
Martijn Coenen04b91c02017-01-19 14:14:21 +0100102 hidl_handle &operator=(const native_handle_t *native_handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100103
Martijn Coenen04b91c02017-01-19 14:14:21 +0100104 hidl_handle &operator=(hidl_handle &&other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100105
Martijn Coenen04b91c02017-01-19 14:14:21 +0100106 void setTo(native_handle_t* handle, bool shouldOwn = false);
107
108 const native_handle_t* operator->() const;
109
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100110 // implicit conversion to const native_handle_t*
Martijn Coenen04b91c02017-01-19 14:14:21 +0100111 operator const native_handle_t *() const;
112
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100113 // explicit conversion
Martijn Coenen04b91c02017-01-19 14:14:21 +0100114 const native_handle_t *getNativeHandle() const;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100115private:
Martijn Coenen04b91c02017-01-19 14:14:21 +0100116 void freeHandle();
117
Andreas Huber6b9cc692017-03-30 10:58:29 -0700118 details::hidl_pointer<const native_handle_t> mHandle __attribute__ ((aligned(8)));
119 bool mOwnsHandle __attribute ((aligned(8)));
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100120};
121
Martijn Coenen72110162016-08-19 14:28:25 +0200122struct hidl_string {
123 hidl_string();
124 ~hidl_string();
125
Yifan Hong602b85a2016-10-24 13:40:01 -0700126 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200127 hidl_string(const hidl_string &);
Steven Morelanda21d84f2017-02-16 09:23:45 -0800128 // copy from a C-style string. nullptr will create an empty string
Steven Morelande03c0872016-10-24 10:43:50 -0700129 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800130 // copy the first length characters from a C-style string.
131 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700132 // copy from an std::string.
133 hidl_string(const std::string &);
134
135 // move constructor.
136 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200137
138 const char *c_str() const;
139 size_t size() const;
140 bool empty() const;
141
Yifan Hong602b85a2016-10-24 13:40:01 -0700142 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700143 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700144 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200145 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700146 // copy from an std::string.
147 hidl_string &operator=(const std::string &);
148 // move assignment operator.
149 hidl_string &operator=(hidl_string &&other);
150 // cast to std::string.
151 operator std::string() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700152
Martijn Coenen72110162016-08-19 14:28:25 +0200153 void clear();
154
155 // Reference an external char array. Ownership is _not_ transferred.
156 // Caller is responsible for ensuring that underlying memory is valid
157 // for the lifetime of this hidl_string.
158 void setToExternal(const char *data, size_t size);
159
Andreas Huberebfeb362016-08-25 13:39:05 -0700160 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
161 static const size_t kOffsetOfBuffer;
162
Martijn Coenen72110162016-08-19 14:28:25 +0200163private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100164 details::hidl_pointer<const char> mBuffer;
165 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700166 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200167
Yifan Hong602b85a2016-10-24 13:40:01 -0700168 // copy from data with size. Assume that my memory is freed
169 // (through clear(), for example)
170 void copyFrom(const char *data, size_t size);
171 // move from another hidl_string
172 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200173};
174
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700175// Use NOLINT to suppress missing parentheses warnings around OP.
Steven Moreland551396a2017-02-13 18:33:20 -0800176#define HIDL_STRING_OPERATOR(OP) \
177 inline bool operator OP(const hidl_string &hs1, const hidl_string &hs2) { \
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700178 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800179 } \
180 inline bool operator OP(const hidl_string &hs, const char *s) { \
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700181 return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800182 } \
183 inline bool operator OP(const char *s, const hidl_string &hs) { \
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700184 return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800185 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800186
Steven Moreland551396a2017-02-13 18:33:20 -0800187HIDL_STRING_OPERATOR(==)
188HIDL_STRING_OPERATOR(!=)
189HIDL_STRING_OPERATOR(<)
190HIDL_STRING_OPERATOR(<=)
191HIDL_STRING_OPERATOR(>)
192HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800193
Steven Moreland551396a2017-02-13 18:33:20 -0800194#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700195
Scott Randolpheb0c3372017-04-03 14:07:14 -0700196// Send our content to the output stream
197std::ostream& operator<<(std::ostream& os, const hidl_string& str);
198
199
Martijn Coenen30791002016-12-01 15:40:46 +0100200// hidl_memory is a structure that can be used to transfer
201// pieces of shared memory between processes. The assumption
202// of this object is that the memory remains accessible as
203// long as the file descriptors in the enclosed mHandle
204// - as well as all of its cross-process dups() - remain opened.
205struct hidl_memory {
206
Martijn Coenen04b91c02017-01-19 14:14:21 +0100207 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100208 }
209
210 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100211 * Creates a hidl_memory object, but doesn't take ownership of
212 * the passed in native_handle_t; callers are responsible for
213 * making sure the handle remains valid while this object is
214 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100215 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100216 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
217 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100218 mSize(size),
219 mName(name)
220 {}
221
222 // copy constructor
223 hidl_memory(const hidl_memory& other) {
224 *this = other;
225 }
226
227 // copy assignment
228 hidl_memory &operator=(const hidl_memory &other) {
229 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100230 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100231 mSize = other.mSize;
232 mName = other.mName;
233 }
234
235 return *this;
236 }
237
Hridya Valsaraju01268892017-02-27 08:48:38 -0800238 // move constructor
239 hidl_memory(hidl_memory&& other) {
240 *this = std::move(other);
241 }
242
243 // move assignment
244 hidl_memory &operator=(hidl_memory &&other) {
245 if (this != &other) {
246 mHandle = std::move(other.mHandle);
247 mSize = other.mSize;
248 mName = std::move(other.mName);
249 other.mSize = 0;
250 }
251
252 return *this;
253 }
254
Martijn Coenen30791002016-12-01 15:40:46 +0100255
256 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100257 }
258
259 const native_handle_t* handle() const {
260 return mHandle;
261 }
262
263 const hidl_string &name() const {
264 return mName;
265 }
266
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800267 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100268 return mSize;
269 }
270
271 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
272 static const size_t kOffsetOfHandle;
273 // offsetof(hidl_memory, mName) exposed since mHandle is private.
274 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800275
Martijn Coenen30791002016-12-01 15:40:46 +0100276private:
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800277 hidl_handle mHandle __attribute__ ((aligned(8)));
278 uint64_t mSize __attribute__ ((aligned(8)));
279 hidl_string mName __attribute__ ((aligned(8)));
Martijn Coenen30791002016-12-01 15:40:46 +0100280};
281
Andreas Huber20dce082016-09-22 19:39:13 -0700282////////////////////////////////////////////////////////////////////////////////
283
Martijn Coenen72110162016-08-19 14:28:25 +0200284template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800285struct hidl_vec {
Martijn Coenen72110162016-08-19 14:28:25 +0200286 hidl_vec()
287 : mBuffer(NULL),
288 mSize(0),
289 mOwnsBuffer(true) {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800290 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Martijn Coenen72110162016-08-19 14:28:25 +0200291 }
292
Yifan Hong602b85a2016-10-24 13:40:01 -0700293 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200294 *this = other;
295 }
296
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100297 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800298 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100299 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700300 }
301
Steven Morelandb69926a2016-11-15 10:02:57 -0800302 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800303 : mOwnsBuffer(true) {
304 if (list.size() > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800305 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800306 }
307 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800308 mBuffer = new T[mSize];
309
Steven Morelandb69926a2016-11-15 10:02:57 -0800310 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800311 for (auto it = list.begin(); it != list.end(); ++it) {
312 mBuffer[idx++] = *it;
313 }
314 }
315
Yifan Hong602b85a2016-10-24 13:40:01 -0700316 hidl_vec(const std::vector<T> &other) : hidl_vec() {
317 *this = other;
318 }
319
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700320 template <typename InputIterator,
321 typename = typename std::enable_if<std::is_convertible<
322 typename std::iterator_traits<InputIterator>::iterator_category,
323 std::input_iterator_tag>::value>::type>
324 hidl_vec(InputIterator first, InputIterator last) : mOwnsBuffer(true) {
325 auto size = std::distance(first, last);
326 if (size > static_cast<int64_t>(UINT32_MAX)) {
327 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
328 }
329 if (size < 0) {
330 details::logAlwaysFatal("size can't be negative.");
331 }
332 mSize = static_cast<uint32_t>(size);
333 mBuffer = new T[mSize];
334
335 size_t idx = 0;
336 for (; first != last; ++first) {
337 mBuffer[idx++] = static_cast<T>(*first);
338 }
339 }
340
Martijn Coenen72110162016-08-19 14:28:25 +0200341 ~hidl_vec() {
342 if (mOwnsBuffer) {
343 delete[] mBuffer;
344 }
345 mBuffer = NULL;
346 }
347
Alexey Polyudove2299012016-10-19 09:52:00 -0700348 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200349 // caller's responsibility to ensure that the underlying memory stays
350 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700351 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200352 if (mOwnsBuffer) {
353 delete [] mBuffer;
354 }
355 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100356 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800357 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100358 }
359 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700360 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200361 }
362
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700363 T *data() {
364 return mBuffer;
365 }
366
367 const T *data() const {
368 return mBuffer;
369 }
370
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700371 T *releaseData() {
372 if (!mOwnsBuffer && mSize > 0) {
373 resize(mSize);
374 }
375 mOwnsBuffer = false;
376 return mBuffer;
377 }
378
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700379 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100380 if (mOwnsBuffer) {
381 delete[] mBuffer;
382 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700383 mBuffer = other.mBuffer;
384 mSize = other.mSize;
385 mOwnsBuffer = other.mOwnsBuffer;
386 other.mOwnsBuffer = false;
387 return *this;
388 }
389
Martijn Coenen72110162016-08-19 14:28:25 +0200390 hidl_vec &operator=(const hidl_vec &other) {
391 if (this != &other) {
392 if (mOwnsBuffer) {
393 delete[] mBuffer;
394 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700395 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200396 }
397
398 return *this;
399 }
400
Yifan Hong602b85a2016-10-24 13:40:01 -0700401 // copy from an std::vector.
402 hidl_vec &operator=(const std::vector<T> &other) {
403 if (mOwnsBuffer) {
404 delete[] mBuffer;
405 }
406 copyFrom(other, other.size());
407 return *this;
408 }
409
410 // cast to an std::vector.
411 operator std::vector<T>() const {
412 std::vector<T> v(mSize);
413 for (size_t i = 0; i < mSize; ++i) {
414 v[i] = mBuffer[i];
415 }
416 return v;
417 }
418
Yifan Hong9fcbb362016-12-20 16:46:41 -0800419 // equality check, assuming that T::operator== is defined.
420 bool operator==(const hidl_vec &other) const {
421 if (mSize != other.size()) {
422 return false;
423 }
424 for (size_t i = 0; i < mSize; ++i) {
425 if (!(mBuffer[i] == other.mBuffer[i])) {
426 return false;
427 }
428 }
429 return true;
430 }
431
432 // inequality check, assuming that T::operator== is defined.
433 inline bool operator!=(const hidl_vec &other) const {
434 return !((*this) == other);
435 }
436
Martijn Coenen72110162016-08-19 14:28:25 +0200437 size_t size() const {
438 return mSize;
439 }
440
441 T &operator[](size_t index) {
442 return mBuffer[index];
443 }
444
445 const T &operator[](size_t index) const {
446 return mBuffer[index];
447 }
448
449 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100450 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800451 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100452 }
Martijn Coenen72110162016-08-19 14:28:25 +0200453 T *newBuffer = new T[size];
454
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100455 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200456 newBuffer[i] = mBuffer[i];
457 }
458
459 if (mOwnsBuffer) {
460 delete[] mBuffer;
461 }
462 mBuffer = newBuffer;
463
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100464 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200465 mOwnsBuffer = true;
466 }
467
Yifan Hong089ae132016-11-11 11:32:52 -0800468 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
469 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800470
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800471private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800472 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800473 template<bool is_const>
474 class iter : public std::iterator<
475 std::random_access_iterator_tag, /* Category */
476 T,
477 ptrdiff_t, /* Distance */
478 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
479 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800480 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800481 using traits = std::iterator_traits<iter>;
482 using ptr_type = typename traits::pointer;
483 using ref_type = typename traits::reference;
484 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800485 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800486 iter(ptr_type ptr) : mPtr(ptr) { }
487 inline iter &operator++() { mPtr++; return *this; }
488 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
489 inline iter &operator--() { mPtr--; return *this; }
490 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
491 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
492 inline iter operator+(diff_type n) const { return mPtr + n; }
493 inline iter operator-(diff_type n) const { return mPtr - n; }
494 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
495 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
496 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
497 inline ref_type operator*() const { return *mPtr; }
498 inline ptr_type operator->() const { return mPtr; }
499 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
500 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
501 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
502 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
503 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
504 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
505 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800506 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800507 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800508 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800509public:
510 using iterator = iter<false /* is_const */>;
511 using const_iterator = iter<true /* is_const */>;
512
Scott Randolphbb840f72016-11-21 14:39:26 -0800513 iterator begin() { return data(); }
514 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800515 const_iterator begin() const { return data(); }
516 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800517
Martijn Coenen72110162016-08-19 14:28:25 +0200518private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100519 details::hidl_pointer<T> mBuffer;
520 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200521 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700522
523 // copy from an array-like object, assuming my resources are freed.
524 template <typename Array>
525 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800526 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700527 mOwnsBuffer = true;
528 if (mSize > 0) {
529 mBuffer = new T[size];
530 for (size_t i = 0; i < size; ++i) {
531 mBuffer[i] = data[i];
532 }
533 } else {
534 mBuffer = NULL;
535 }
536 }
Martijn Coenen72110162016-08-19 14:28:25 +0200537};
538
Yifan Hong089ae132016-11-11 11:32:52 -0800539template <typename T>
540const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
541
Andreas Huber20dce082016-09-22 19:39:13 -0700542////////////////////////////////////////////////////////////////////////////////
543
544namespace details {
545
546 template<size_t SIZE1, size_t... SIZES>
547 struct product {
548 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
549 };
550
551 template<size_t SIZE1>
552 struct product<SIZE1> {
553 static constexpr size_t value = SIZE1;
554 };
555
556 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800557 struct std_array {
558 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
559 };
560
561 template<typename T, size_t SIZE1>
562 struct std_array<T, SIZE1> {
563 using type = std::array<T, SIZE1>;
564 };
565
566 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700567 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800568
569 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
570
Andreas Huber20dce082016-09-22 19:39:13 -0700571 explicit accessor(T *base)
572 : mBase(base) {
573 }
574
575 accessor<T, SIZES...> operator[](size_t index) {
576 return accessor<T, SIZES...>(
577 &mBase[index * product<SIZES...>::value]);
578 }
579
Yifan Hong44ab6232016-11-22 16:28:24 -0800580 accessor &operator=(const std_array_type &other) {
581 for (size_t i = 0; i < SIZE1; ++i) {
582 (*this)[i] = other[i];
583 }
584 return *this;
585 }
586
Andreas Huber20dce082016-09-22 19:39:13 -0700587 private:
588 T *mBase;
589 };
590
591 template<typename T, size_t SIZE1>
592 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800593
594 using std_array_type = typename std_array<T, SIZE1>::type;
595
Andreas Huber20dce082016-09-22 19:39:13 -0700596 explicit accessor(T *base)
597 : mBase(base) {
598 }
599
600 T &operator[](size_t index) {
601 return mBase[index];
602 }
603
Yifan Hong44ab6232016-11-22 16:28:24 -0800604 accessor &operator=(const std_array_type &other) {
605 for (size_t i = 0; i < SIZE1; ++i) {
606 (*this)[i] = other[i];
607 }
608 return *this;
609 }
610
Andreas Huber20dce082016-09-22 19:39:13 -0700611 private:
612 T *mBase;
613 };
614
615 template<typename T, size_t SIZE1, size_t... SIZES>
616 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800617
618 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
619
Andreas Huber20dce082016-09-22 19:39:13 -0700620 explicit const_accessor(const T *base)
621 : mBase(base) {
622 }
623
Yifan Hongbe7a6882017-01-05 17:30:17 -0800624 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700625 return const_accessor<T, SIZES...>(
626 &mBase[index * product<SIZES...>::value]);
627 }
628
Yifan Hong44ab6232016-11-22 16:28:24 -0800629 operator std_array_type() {
630 std_array_type array;
631 for (size_t i = 0; i < SIZE1; ++i) {
632 array[i] = (*this)[i];
633 }
634 return array;
635 }
636
Andreas Huber20dce082016-09-22 19:39:13 -0700637 private:
638 const T *mBase;
639 };
640
641 template<typename T, size_t SIZE1>
642 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800643
644 using std_array_type = typename std_array<T, SIZE1>::type;
645
Andreas Huber20dce082016-09-22 19:39:13 -0700646 explicit const_accessor(const T *base)
647 : mBase(base) {
648 }
649
650 const T &operator[](size_t index) const {
651 return mBase[index];
652 }
653
Yifan Hong44ab6232016-11-22 16:28:24 -0800654 operator std_array_type() {
655 std_array_type array;
656 for (size_t i = 0; i < SIZE1; ++i) {
657 array[i] = (*this)[i];
658 }
659 return array;
660 }
661
Andreas Huber20dce082016-09-22 19:39:13 -0700662 private:
663 const T *mBase;
664 };
665
666} // namespace details
667
668////////////////////////////////////////////////////////////////////////////////
669
Yifan Hong44ab6232016-11-22 16:28:24 -0800670// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700671template<typename T, size_t SIZE1, size_t... SIZES>
672struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800673
674 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
675
Andreas Huber20dce082016-09-22 19:39:13 -0700676 hidl_array() = default;
677
Yifan Hong44ab6232016-11-22 16:28:24 -0800678 // Copies the data from source, using T::operator=(const T &).
679 hidl_array(const T *source) {
680 for (size_t i = 0; i < elementCount(); ++i) {
681 mBuffer[i] = source[i];
682 }
683 }
684
685 // Copies the data from the given std::array, using T::operator=(const T &).
686 hidl_array(const std_array_type &array) {
687 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
688 modifier = array;
689 }
690
Andreas Huber20dce082016-09-22 19:39:13 -0700691 T *data() { return mBuffer; }
692 const T *data() const { return mBuffer; }
693
694 details::accessor<T, SIZES...> operator[](size_t index) {
695 return details::accessor<T, SIZES...>(
696 &mBuffer[index * details::product<SIZES...>::value]);
697 }
698
699 details::const_accessor<T, SIZES...> operator[](size_t index) const {
700 return details::const_accessor<T, SIZES...>(
701 &mBuffer[index * details::product<SIZES...>::value]);
702 }
703
Yifan Hong9fcbb362016-12-20 16:46:41 -0800704 // equality check, assuming that T::operator== is defined.
705 bool operator==(const hidl_array &other) const {
706 for (size_t i = 0; i < elementCount(); ++i) {
707 if (!(mBuffer[i] == other.mBuffer[i])) {
708 return false;
709 }
710 }
711 return true;
712 }
713
714 inline bool operator!=(const hidl_array &other) const {
715 return !((*this) == other);
716 }
717
Andreas Huber00a985c2016-09-28 14:24:53 -0700718 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
719
720 static constexpr size_tuple_type size() {
721 return std::make_tuple(SIZE1, SIZES...);
722 }
723
Yifan Hong44ab6232016-11-22 16:28:24 -0800724 static constexpr size_t elementCount() {
725 return details::product<SIZE1, SIZES...>::value;
726 }
727
728 operator std_array_type() const {
729 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
730 }
731
Andreas Huber20dce082016-09-22 19:39:13 -0700732private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800733 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700734};
735
Yifan Hong44ab6232016-11-22 16:28:24 -0800736// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700737template<typename T, size_t SIZE1>
738struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800739
740 using std_array_type = typename details::std_array<T, SIZE1>::type;
741
Andreas Huber20dce082016-09-22 19:39:13 -0700742 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800743
744 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800745 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800746 for (size_t i = 0; i < elementCount(); ++i) {
747 mBuffer[i] = source[i];
748 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800749 }
Andreas Huber20dce082016-09-22 19:39:13 -0700750
Yifan Hong44ab6232016-11-22 16:28:24 -0800751 // Copies the data from the given std::array, using T::operator=(const T &).
752 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
753
Andreas Huber20dce082016-09-22 19:39:13 -0700754 T *data() { return mBuffer; }
755 const T *data() const { return mBuffer; }
756
757 T &operator[](size_t index) {
758 return mBuffer[index];
759 }
760
761 const T &operator[](size_t index) const {
762 return mBuffer[index];
763 }
764
Yifan Hong9fcbb362016-12-20 16:46:41 -0800765 // equality check, assuming that T::operator== is defined.
766 bool operator==(const hidl_array &other) const {
767 for (size_t i = 0; i < elementCount(); ++i) {
768 if (!(mBuffer[i] == other.mBuffer[i])) {
769 return false;
770 }
771 }
772 return true;
773 }
774
775 inline bool operator!=(const hidl_array &other) const {
776 return !((*this) == other);
777 }
778
Andreas Huber00a985c2016-09-28 14:24:53 -0700779 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800780 static constexpr size_t elementCount() { return SIZE1; }
781
782 // Copies the data to an std::array, using T::operator=(T).
783 operator std_array_type() const {
784 std_array_type array;
785 for (size_t i = 0; i < SIZE1; ++i) {
786 array[i] = mBuffer[i];
787 }
788 return array;
789 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700790
Andreas Huber20dce082016-09-22 19:39:13 -0700791private:
792 T mBuffer[SIZE1];
793};
794
Martijn Coenen72110162016-08-19 14:28:25 +0200795// ----------------------------------------------------------------------
796// Version functions
797struct hidl_version {
798public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800799 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200800
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700801 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200802 return (mMajor == other.get_major() && mMinor == other.get_minor());
803 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200804
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800805 bool operator<(const hidl_version& other) const {
806 return (mMajor < other.get_major() ||
807 (mMajor == other.get_major() && mMinor < other.get_minor()));
808 }
809
810 bool operator>(const hidl_version& other) const {
811 return other < *this;
812 }
813
814 bool operator<=(const hidl_version& other) const {
815 return !(*this > other);
816 }
817
818 bool operator>=(const hidl_version& other) const {
819 return !(*this < other);
820 }
821
Martijn Coenen097a7672016-09-08 16:56:41 +0200822 constexpr uint16_t get_major() const { return mMajor; }
823 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200824
Martijn Coenen72110162016-08-19 14:28:25 +0200825private:
826 uint16_t mMajor;
827 uint16_t mMinor;
828};
829
830inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
831 return hidl_version(major,minor);
832}
833
Yifan Hongbe7a6882017-01-05 17:30:17 -0800834///////////////////// toString functions
835
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800836std::string toString(const void *t);
Yifan Hongbe7a6882017-01-05 17:30:17 -0800837
838// toString alias for numeric types
839template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
840inline std::string toString(T t) {
841 return std::to_string(t);
842}
843
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800844namespace details {
845
Yifan Hongbe7a6882017-01-05 17:30:17 -0800846template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
847inline std::string toHexString(T t, bool prefix = true) {
848 std::ostringstream os;
849 if (prefix) { os << std::showbase; }
850 os << std::hex << t;
851 return os.str();
852}
853
854template<>
855inline std::string toHexString(uint8_t t, bool prefix) {
856 return toHexString(static_cast<int32_t>(t), prefix);
857}
858
859template<>
860inline std::string toHexString(int8_t t, bool prefix) {
861 return toHexString(static_cast<int32_t>(t), prefix);
862}
863
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800864template<typename Array>
865std::string arrayToString(const Array &a, size_t size);
866
867template<size_t SIZE1>
868std::string arraySizeToString() {
869 return std::string{"["} + toString(SIZE1) + "]";
870}
871
872template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
873std::string arraySizeToString() {
874 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
875}
876
877template<typename T, size_t SIZE1>
878std::string toString(details::const_accessor<T, SIZE1> a) {
879 return arrayToString(a, SIZE1);
880}
881
882template<typename Array>
883std::string arrayToString(const Array &a, size_t size) {
884 using android::hardware::toString;
885 std::string os;
886 os += "{";
887 for (size_t i = 0; i < size; ++i) {
888 if (i > 0) {
889 os += ", ";
890 }
891 os += toString(a[i]);
892 }
893 os += "}";
894 return os;
895}
896
897template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
898std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
899 return arrayToString(a, SIZE1);
900}
901
902} //namespace details
903
904inline std::string toString(const void *t) {
905 return details::toHexString(reinterpret_cast<uintptr_t>(t));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800906}
907
908// debug string dump. There will be quotes around the string!
909inline std::string toString(const hidl_string &hs) {
910 return std::string{"\""} + hs.c_str() + "\"";
911}
912
913// debug string dump
914inline std::string toString(const hidl_handle &hs) {
915 return toString(hs.getNativeHandle());
916}
917
918inline std::string toString(const hidl_memory &mem) {
919 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
920 + toString(mem.size())
921 + ", .handle = " + toString(mem.handle()) + "}";
922}
923
924inline std::string toString(const sp<hidl_death_recipient> &dr) {
925 return std::string{"death_recipient@"} + toString(dr.get());
926}
927
Yifan Hongbe7a6882017-01-05 17:30:17 -0800928// debug string dump, assuming that toString(T) is defined.
929template<typename T>
930std::string toString(const hidl_vec<T> &a) {
931 std::string os;
932 os += "[" + toString(a.size()) + "]";
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800933 os += details::arrayToString(a, a.size());
Yifan Hongbe7a6882017-01-05 17:30:17 -0800934 return os;
935}
936
Yifan Hongbe7a6882017-01-05 17:30:17 -0800937template<typename T, size_t SIZE1>
938std::string toString(const hidl_array<T, SIZE1> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800939 return details::arraySizeToString<SIZE1>()
940 + details::toString(details::const_accessor<T, SIZE1>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800941}
942
943template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
944std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800945 return details::arraySizeToString<SIZE1, SIZE2, SIZES...>()
946 + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800947}
948
Martijn Coenen72110162016-08-19 14:28:25 +0200949} // namespace hardware
950} // namespace android
951
Martijn Coenenc28f1152016-08-22 14:06:56 +0200952
Martijn Coenen72110162016-08-19 14:28:25 +0200953#endif // ANDROID_HIDL_SUPPORT_H