blob: b9e4ffa7ec7489c1175a0f1f5f4ec04ea6a09c27 [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>
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070022#include <dirent.h>
Scott Randolphbb840f72016-11-21 14:39:26 -080023#include <iterator>
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010024#include <cutils/native_handle.h>
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -070025#include <cutils/properties.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080026#include <functional>
Steven Moreland337c3ae2016-11-22 13:37:32 -080027#include <hidl/HidlInternal.h>
Yifan Honga3c31842016-10-21 10:33:14 -070028#include <hidl/Status.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080029#include <map>
Yifan Hongbe7a6882017-01-05 17:30:17 -080030#include <sstream>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080031#include <stddef.h>
Andreas Huber00a985c2016-09-28 14:24:53 -070032#include <tuple>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080033#include <type_traits>
Iliyan Malchev692070a2016-09-12 16:30:44 -070034#include <utils/Errors.h>
35#include <utils/RefBase.h>
36#include <utils/StrongPointer.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080037#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020038
39namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010040
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010041// this file is included by all hidl interface, so we must forward declare the
42// IMemory and IBase types.
Martijn Coenen30791002016-12-01 15:40:46 +010043namespace hidl {
44namespace memory {
45namespace V1_0 {
46 struct IMemory;
47}; // namespace V1_0
48}; // namespace manager
49}; // namespace hidl
50
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010051namespace hidl {
52namespace base {
53namespace V1_0 {
54 struct IBase;
55}; // namespace V1_0
56}; // namespace base
57}; // namespace hidl
58
Martijn Coenen72110162016-08-19 14:28:25 +020059namespace hardware {
60
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010061// hidl_death_recipient is a callback interfaced that can be used with
62// linkToDeath() / unlinkToDeath()
63struct hidl_death_recipient : public virtual RefBase {
64 virtual void serviceDied(uint64_t cookie,
65 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
66};
67
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010068// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
69// so that it can safely be transferred between 32-bit and 64-bit processes.
70struct hidl_handle {
71 hidl_handle() {
72 mHandle = nullptr;
73 }
74 ~hidl_handle() {
75 }
76
77 // copy constructors.
78 hidl_handle(const native_handle_t *handle) {
79 mHandle = handle;
80 }
81
82 hidl_handle(const hidl_handle &other) {
83 mHandle = other.mHandle;
84 }
85
86 // move constructor.
87 hidl_handle(hidl_handle &&other) {
88 *this = std::move(other);
89 }
90
Steven Moreland6ffdc2a2017-01-23 12:34:33 -080091 // assignment operators
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010092 hidl_handle &operator=(const hidl_handle &other) {
93 mHandle = other.mHandle;
94 return *this;
95 }
96
97 hidl_handle &operator=(const native_handle_t *native_handle) {
98 mHandle = native_handle;
99 return *this;
100 }
101
102 hidl_handle &operator=(hidl_handle &&other) {
103 mHandle = other.mHandle;
104 other.mHandle = nullptr;
105 return *this;
106 }
107
108 const native_handle_t* operator->() const {
109 return mHandle;
110 }
111 // implicit conversion to const native_handle_t*
112 operator const native_handle_t *() const {
113 return mHandle;
114 }
115 // explicit conversion
116 const native_handle_t *getNativeHandle() const {
117 return mHandle;
118 }
119private:
120 details::hidl_pointer<const native_handle_t> mHandle;
121};
122
Martijn Coenen72110162016-08-19 14:28:25 +0200123struct hidl_string {
124 hidl_string();
125 ~hidl_string();
126
Yifan Hong602b85a2016-10-24 13:40:01 -0700127 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200128 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700129 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -0700130 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800131 // copy the first length characters from a C-style string.
132 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700133 // copy from an std::string.
134 hidl_string(const std::string &);
135
136 // move constructor.
137 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200138
139 const char *c_str() const;
140 size_t size() const;
141 bool empty() const;
142
Yifan Hong602b85a2016-10-24 13:40:01 -0700143 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700144 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700145 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200146 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700147 // copy from an std::string.
148 hidl_string &operator=(const std::string &);
149 // move assignment operator.
150 hidl_string &operator=(hidl_string &&other);
151 // cast to std::string.
152 operator std::string() const;
153 // cast to C-style string. Caller is responsible
154 // to maintain this hidl_string alive.
155 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700156
Steven Morelanda2a81842017-01-20 14:48:15 -0800157 bool operator< (const hidl_string &rhs) const;
158
Martijn Coenen72110162016-08-19 14:28:25 +0200159 void clear();
160
161 // Reference an external char array. Ownership is _not_ transferred.
162 // Caller is responsible for ensuring that underlying memory is valid
163 // for the lifetime of this hidl_string.
164 void setToExternal(const char *data, size_t size);
165
Andreas Huberebfeb362016-08-25 13:39:05 -0700166 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
167 static const size_t kOffsetOfBuffer;
168
Martijn Coenen72110162016-08-19 14:28:25 +0200169private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100170 details::hidl_pointer<const char> mBuffer;
171 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700172 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200173
Yifan Hong602b85a2016-10-24 13:40:01 -0700174 // copy from data with size. Assume that my memory is freed
175 // (through clear(), for example)
176 void copyFrom(const char *data, size_t size);
177 // move from another hidl_string
178 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200179};
180
Scott Randolphbb840f72016-11-21 14:39:26 -0800181inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
182 return strcmp(hs1.c_str(), hs2.c_str()) == 0;
183}
184
185inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
186 return !(hs1 == hs2);
187}
188
Yifan Hong5708fb42016-10-26 17:50:29 -0700189inline bool operator==(const hidl_string &hs, const char *s) {
190 return strcmp(hs.c_str(), s) == 0;
191}
192
193inline bool operator!=(const hidl_string &hs, const char *s) {
194 return !(hs == s);
195}
196
197inline bool operator==(const char *s, const hidl_string &hs) {
198 return strcmp(hs.c_str(), s) == 0;
199}
200
201inline bool operator!=(const char *s, const hidl_string &hs) {
202 return !(s == hs);
203}
204
Martijn Coenen30791002016-12-01 15:40:46 +0100205// hidl_memory is a structure that can be used to transfer
206// pieces of shared memory between processes. The assumption
207// of this object is that the memory remains accessible as
208// long as the file descriptors in the enclosed mHandle
209// - as well as all of its cross-process dups() - remain opened.
210struct hidl_memory {
211
212 hidl_memory() : mOwnsHandle(false), mHandle(nullptr), mSize(0), mName("") {
213 }
214
215 /**
216 * Creates a hidl_memory object and takes ownership of the handle.
217 */
218 hidl_memory(const hidl_string &name, const hidl_handle &handle, size_t size)
219 : mOwnsHandle(true),
220 mHandle(handle),
221 mSize(size),
222 mName(name)
223 {}
224
225 // copy constructor
226 hidl_memory(const hidl_memory& other) {
227 *this = other;
228 }
229
230 // copy assignment
231 hidl_memory &operator=(const hidl_memory &other) {
232 if (this != &other) {
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800233 cleanup();
234
235 if (other.mHandle == nullptr) {
236 mHandle = nullptr;
237 mOwnsHandle = false;
238 } else {
239 mOwnsHandle = true;
240 mHandle = native_handle_clone(other.mHandle);
241 }
Martijn Coenen30791002016-12-01 15:40:46 +0100242 mSize = other.mSize;
243 mName = other.mName;
244 }
245
246 return *this;
247 }
248
249 // TODO move constructor/move assignment
250
251 ~hidl_memory() {
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800252 cleanup();
Martijn Coenen30791002016-12-01 15:40:46 +0100253 }
254
255 const native_handle_t* handle() const {
256 return mHandle;
257 }
258
259 const hidl_string &name() const {
260 return mName;
261 }
262
263 size_t size() const {
264 return mSize;
265 }
266
267 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
268 static const size_t kOffsetOfHandle;
269 // offsetof(hidl_memory, mName) exposed since mHandle is private.
270 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800271
Martijn Coenen30791002016-12-01 15:40:46 +0100272private:
273 bool mOwnsHandle;
274 hidl_handle mHandle;
275 size_t mSize;
276 hidl_string mName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800277
278 void cleanup() {
279 // TODO(b/33812533): native_handle_delete
280 if (mOwnsHandle && mHandle != nullptr) {
281 native_handle_close(mHandle);
282 }
283 }
Martijn Coenen30791002016-12-01 15:40:46 +0100284};
285
Andreas Huber20dce082016-09-22 19:39:13 -0700286////////////////////////////////////////////////////////////////////////////////
287
Martijn Coenen72110162016-08-19 14:28:25 +0200288template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100289struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200290 hidl_vec()
291 : mBuffer(NULL),
292 mSize(0),
293 mOwnsBuffer(true) {
294 }
295
Yifan Hong602b85a2016-10-24 13:40:01 -0700296 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200297 *this = other;
298 }
299
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100300 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800301 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100302 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700303 }
304
Steven Morelandb69926a2016-11-15 10:02:57 -0800305 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800306 : mOwnsBuffer(true) {
307 if (list.size() > UINT32_MAX) {
308 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
309 }
310 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800311 mBuffer = new T[mSize];
312
Steven Morelandb69926a2016-11-15 10:02:57 -0800313 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800314 for (auto it = list.begin(); it != list.end(); ++it) {
315 mBuffer[idx++] = *it;
316 }
317 }
318
Yifan Hong602b85a2016-10-24 13:40:01 -0700319 hidl_vec(const std::vector<T> &other) : hidl_vec() {
320 *this = other;
321 }
322
Martijn Coenen72110162016-08-19 14:28:25 +0200323 ~hidl_vec() {
324 if (mOwnsBuffer) {
325 delete[] mBuffer;
326 }
327 mBuffer = NULL;
328 }
329
Alexey Polyudove2299012016-10-19 09:52:00 -0700330 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200331 // caller's responsibility to ensure that the underlying memory stays
332 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700333 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200334 if (mOwnsBuffer) {
335 delete [] mBuffer;
336 }
337 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100338 if (size > UINT32_MAX) {
339 logAlwaysFatal("external vector size exceeds 2^32 elements.");
340 }
341 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700342 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200343 }
344
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700345 T *data() {
346 return mBuffer;
347 }
348
349 const T *data() const {
350 return mBuffer;
351 }
352
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700353 T *releaseData() {
354 if (!mOwnsBuffer && mSize > 0) {
355 resize(mSize);
356 }
357 mOwnsBuffer = false;
358 return mBuffer;
359 }
360
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700361 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100362 if (mOwnsBuffer) {
363 delete[] mBuffer;
364 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700365 mBuffer = other.mBuffer;
366 mSize = other.mSize;
367 mOwnsBuffer = other.mOwnsBuffer;
368 other.mOwnsBuffer = false;
369 return *this;
370 }
371
Martijn Coenen72110162016-08-19 14:28:25 +0200372 hidl_vec &operator=(const hidl_vec &other) {
373 if (this != &other) {
374 if (mOwnsBuffer) {
375 delete[] mBuffer;
376 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700377 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200378 }
379
380 return *this;
381 }
382
Yifan Hong602b85a2016-10-24 13:40:01 -0700383 // copy from an std::vector.
384 hidl_vec &operator=(const std::vector<T> &other) {
385 if (mOwnsBuffer) {
386 delete[] mBuffer;
387 }
388 copyFrom(other, other.size());
389 return *this;
390 }
391
392 // cast to an std::vector.
393 operator std::vector<T>() const {
394 std::vector<T> v(mSize);
395 for (size_t i = 0; i < mSize; ++i) {
396 v[i] = mBuffer[i];
397 }
398 return v;
399 }
400
Yifan Hong9fcbb362016-12-20 16:46:41 -0800401 // equality check, assuming that T::operator== is defined.
402 bool operator==(const hidl_vec &other) const {
403 if (mSize != other.size()) {
404 return false;
405 }
406 for (size_t i = 0; i < mSize; ++i) {
407 if (!(mBuffer[i] == other.mBuffer[i])) {
408 return false;
409 }
410 }
411 return true;
412 }
413
414 // inequality check, assuming that T::operator== is defined.
415 inline bool operator!=(const hidl_vec &other) const {
416 return !((*this) == other);
417 }
418
Martijn Coenen72110162016-08-19 14:28:25 +0200419 size_t size() const {
420 return mSize;
421 }
422
423 T &operator[](size_t index) {
424 return mBuffer[index];
425 }
426
427 const T &operator[](size_t index) const {
428 return mBuffer[index];
429 }
430
431 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100432 if (size > UINT32_MAX) {
433 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
434 }
Martijn Coenen72110162016-08-19 14:28:25 +0200435 T *newBuffer = new T[size];
436
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100437 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200438 newBuffer[i] = mBuffer[i];
439 }
440
441 if (mOwnsBuffer) {
442 delete[] mBuffer;
443 }
444 mBuffer = newBuffer;
445
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100446 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200447 mOwnsBuffer = true;
448 }
449
Yifan Hong089ae132016-11-11 11:32:52 -0800450 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
451 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800452
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800453private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800454 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800455 template<bool is_const>
456 class iter : public std::iterator<
457 std::random_access_iterator_tag, /* Category */
458 T,
459 ptrdiff_t, /* Distance */
460 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
461 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800462 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800463 using traits = std::iterator_traits<iter>;
464 using ptr_type = typename traits::pointer;
465 using ref_type = typename traits::reference;
466 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800467 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800468 iter(ptr_type ptr) : mPtr(ptr) { }
469 inline iter &operator++() { mPtr++; return *this; }
470 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
471 inline iter &operator--() { mPtr--; return *this; }
472 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
473 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
474 inline iter operator+(diff_type n) const { return mPtr + n; }
475 inline iter operator-(diff_type n) const { return mPtr - n; }
476 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
477 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
478 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
479 inline ref_type operator*() const { return *mPtr; }
480 inline ptr_type operator->() const { return mPtr; }
481 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
482 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.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 ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800488 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800489 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800490 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800491public:
492 using iterator = iter<false /* is_const */>;
493 using const_iterator = iter<true /* is_const */>;
494
Scott Randolphbb840f72016-11-21 14:39:26 -0800495 iterator begin() { return data(); }
496 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800497 const_iterator begin() const { return data(); }
498 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800499
Martijn Coenen72110162016-08-19 14:28:25 +0200500private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100501 details::hidl_pointer<T> mBuffer;
502 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200503 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700504
505 // copy from an array-like object, assuming my resources are freed.
506 template <typename Array>
507 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800508 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700509 mOwnsBuffer = true;
510 if (mSize > 0) {
511 mBuffer = new T[size];
512 for (size_t i = 0; i < size; ++i) {
513 mBuffer[i] = data[i];
514 }
515 } else {
516 mBuffer = NULL;
517 }
518 }
Martijn Coenen72110162016-08-19 14:28:25 +0200519};
520
Yifan Hong089ae132016-11-11 11:32:52 -0800521template <typename T>
522const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
523
Andreas Huber20dce082016-09-22 19:39:13 -0700524////////////////////////////////////////////////////////////////////////////////
525
526namespace details {
527
528 template<size_t SIZE1, size_t... SIZES>
529 struct product {
530 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
531 };
532
533 template<size_t SIZE1>
534 struct product<SIZE1> {
535 static constexpr size_t value = SIZE1;
536 };
537
538 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800539 struct std_array {
540 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
541 };
542
543 template<typename T, size_t SIZE1>
544 struct std_array<T, SIZE1> {
545 using type = std::array<T, SIZE1>;
546 };
547
548 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700549 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800550
551 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
552
Andreas Huber20dce082016-09-22 19:39:13 -0700553 explicit accessor(T *base)
554 : mBase(base) {
555 }
556
557 accessor<T, SIZES...> operator[](size_t index) {
558 return accessor<T, SIZES...>(
559 &mBase[index * product<SIZES...>::value]);
560 }
561
Yifan Hong44ab6232016-11-22 16:28:24 -0800562 accessor &operator=(const std_array_type &other) {
563 for (size_t i = 0; i < SIZE1; ++i) {
564 (*this)[i] = other[i];
565 }
566 return *this;
567 }
568
Andreas Huber20dce082016-09-22 19:39:13 -0700569 private:
570 T *mBase;
571 };
572
573 template<typename T, size_t SIZE1>
574 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800575
576 using std_array_type = typename std_array<T, SIZE1>::type;
577
Andreas Huber20dce082016-09-22 19:39:13 -0700578 explicit accessor(T *base)
579 : mBase(base) {
580 }
581
582 T &operator[](size_t index) {
583 return mBase[index];
584 }
585
Yifan Hong44ab6232016-11-22 16:28:24 -0800586 accessor &operator=(const std_array_type &other) {
587 for (size_t i = 0; i < SIZE1; ++i) {
588 (*this)[i] = other[i];
589 }
590 return *this;
591 }
592
Andreas Huber20dce082016-09-22 19:39:13 -0700593 private:
594 T *mBase;
595 };
596
597 template<typename T, size_t SIZE1, size_t... SIZES>
598 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800599
600 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
601
Andreas Huber20dce082016-09-22 19:39:13 -0700602 explicit const_accessor(const T *base)
603 : mBase(base) {
604 }
605
Yifan Hongbe7a6882017-01-05 17:30:17 -0800606 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700607 return const_accessor<T, SIZES...>(
608 &mBase[index * product<SIZES...>::value]);
609 }
610
Yifan Hong44ab6232016-11-22 16:28:24 -0800611 operator std_array_type() {
612 std_array_type array;
613 for (size_t i = 0; i < SIZE1; ++i) {
614 array[i] = (*this)[i];
615 }
616 return array;
617 }
618
Andreas Huber20dce082016-09-22 19:39:13 -0700619 private:
620 const T *mBase;
621 };
622
623 template<typename T, size_t SIZE1>
624 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800625
626 using std_array_type = typename std_array<T, SIZE1>::type;
627
Andreas Huber20dce082016-09-22 19:39:13 -0700628 explicit const_accessor(const T *base)
629 : mBase(base) {
630 }
631
632 const T &operator[](size_t index) const {
633 return mBase[index];
634 }
635
Yifan Hong44ab6232016-11-22 16:28:24 -0800636 operator std_array_type() {
637 std_array_type array;
638 for (size_t i = 0; i < SIZE1; ++i) {
639 array[i] = (*this)[i];
640 }
641 return array;
642 }
643
Andreas Huber20dce082016-09-22 19:39:13 -0700644 private:
645 const T *mBase;
646 };
647
648} // namespace details
649
650////////////////////////////////////////////////////////////////////////////////
651
Yifan Hong44ab6232016-11-22 16:28:24 -0800652// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700653template<typename T, size_t SIZE1, size_t... SIZES>
654struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800655
656 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
657
Andreas Huber20dce082016-09-22 19:39:13 -0700658 hidl_array() = default;
659
Yifan Hong44ab6232016-11-22 16:28:24 -0800660 // Copies the data from source, using T::operator=(const T &).
661 hidl_array(const T *source) {
662 for (size_t i = 0; i < elementCount(); ++i) {
663 mBuffer[i] = source[i];
664 }
665 }
666
667 // Copies the data from the given std::array, using T::operator=(const T &).
668 hidl_array(const std_array_type &array) {
669 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
670 modifier = array;
671 }
672
Andreas Huber20dce082016-09-22 19:39:13 -0700673 T *data() { return mBuffer; }
674 const T *data() const { return mBuffer; }
675
676 details::accessor<T, SIZES...> operator[](size_t index) {
677 return details::accessor<T, SIZES...>(
678 &mBuffer[index * details::product<SIZES...>::value]);
679 }
680
681 details::const_accessor<T, SIZES...> operator[](size_t index) const {
682 return details::const_accessor<T, SIZES...>(
683 &mBuffer[index * details::product<SIZES...>::value]);
684 }
685
Yifan Hong9fcbb362016-12-20 16:46:41 -0800686 // equality check, assuming that T::operator== is defined.
687 bool operator==(const hidl_array &other) const {
688 for (size_t i = 0; i < elementCount(); ++i) {
689 if (!(mBuffer[i] == other.mBuffer[i])) {
690 return false;
691 }
692 }
693 return true;
694 }
695
696 inline bool operator!=(const hidl_array &other) const {
697 return !((*this) == other);
698 }
699
Andreas Huber00a985c2016-09-28 14:24:53 -0700700 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
701
702 static constexpr size_tuple_type size() {
703 return std::make_tuple(SIZE1, SIZES...);
704 }
705
Yifan Hong44ab6232016-11-22 16:28:24 -0800706 static constexpr size_t elementCount() {
707 return details::product<SIZE1, SIZES...>::value;
708 }
709
710 operator std_array_type() const {
711 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
712 }
713
Andreas Huber20dce082016-09-22 19:39:13 -0700714private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800715 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700716};
717
Yifan Hong44ab6232016-11-22 16:28:24 -0800718// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700719template<typename T, size_t SIZE1>
720struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800721
722 using std_array_type = typename details::std_array<T, SIZE1>::type;
723
Andreas Huber20dce082016-09-22 19:39:13 -0700724 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800725
726 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800727 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800728 for (size_t i = 0; i < elementCount(); ++i) {
729 mBuffer[i] = source[i];
730 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800731 }
Andreas Huber20dce082016-09-22 19:39:13 -0700732
Yifan Hong44ab6232016-11-22 16:28:24 -0800733 // Copies the data from the given std::array, using T::operator=(const T &).
734 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
735
Andreas Huber20dce082016-09-22 19:39:13 -0700736 T *data() { return mBuffer; }
737 const T *data() const { return mBuffer; }
738
739 T &operator[](size_t index) {
740 return mBuffer[index];
741 }
742
743 const T &operator[](size_t index) const {
744 return mBuffer[index];
745 }
746
Yifan Hong9fcbb362016-12-20 16:46:41 -0800747 // equality check, assuming that T::operator== is defined.
748 bool operator==(const hidl_array &other) const {
749 for (size_t i = 0; i < elementCount(); ++i) {
750 if (!(mBuffer[i] == other.mBuffer[i])) {
751 return false;
752 }
753 }
754 return true;
755 }
756
757 inline bool operator!=(const hidl_array &other) const {
758 return !((*this) == other);
759 }
760
Andreas Huber00a985c2016-09-28 14:24:53 -0700761 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800762 static constexpr size_t elementCount() { return SIZE1; }
763
764 // Copies the data to an std::array, using T::operator=(T).
765 operator std_array_type() const {
766 std_array_type array;
767 for (size_t i = 0; i < SIZE1; ++i) {
768 array[i] = mBuffer[i];
769 }
770 return array;
771 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700772
Andreas Huber20dce082016-09-22 19:39:13 -0700773private:
774 T mBuffer[SIZE1];
775};
776
Martijn Coenen72110162016-08-19 14:28:25 +0200777// ----------------------------------------------------------------------
778// Version functions
779struct hidl_version {
780public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800781 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200782
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700783 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200784 return (mMajor == other.get_major() && mMinor == other.get_minor());
785 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200786
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800787 bool operator<(const hidl_version& other) const {
788 return (mMajor < other.get_major() ||
789 (mMajor == other.get_major() && mMinor < other.get_minor()));
790 }
791
792 bool operator>(const hidl_version& other) const {
793 return other < *this;
794 }
795
796 bool operator<=(const hidl_version& other) const {
797 return !(*this > other);
798 }
799
800 bool operator>=(const hidl_version& other) const {
801 return !(*this < other);
802 }
803
Martijn Coenen097a7672016-09-08 16:56:41 +0200804 constexpr uint16_t get_major() const { return mMajor; }
805 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200806
Martijn Coenen72110162016-08-19 14:28:25 +0200807private:
808 uint16_t mMajor;
809 uint16_t mMinor;
810};
811
812inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
813 return hidl_version(major,minor);
814}
815
Steven Morelandbdf26662016-09-02 11:03:15 -0700816#if defined(__LP64__)
817#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
818#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
819#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
820#else
821#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
822#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
823#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
824#endif
825
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700826// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700827// Class that provides Hidl instrumentation utilities.
828struct HidlInstrumentor {
829 // Event that triggers the instrumentation. e.g. enter of an API call on
830 // the server/client side, exit of an API call on the server/client side
831 // etc.
832 enum InstrumentationEvent {
833 SERVER_API_ENTRY = 0,
834 SERVER_API_EXIT,
835 CLIENT_API_ENTRY,
836 CLIENT_API_EXIT,
837 SYNC_CALLBACK_ENTRY,
838 SYNC_CALLBACK_EXIT,
839 ASYNC_CALLBACK_ENTRY,
840 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700841 PASSTHROUGH_ENTRY,
842 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700843 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700844
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700845 // Signature of the instrumentation callback function.
846 using InstrumentationCallback = std::function<void(
847 const InstrumentationEvent event,
848 const char *package,
849 const char *version,
850 const char *interface,
851 const char *method,
852 std::vector<void *> *args)>;
853
854 explicit HidlInstrumentor(const std::string &prefix);
855 virtual ~HidlInstrumentor();
856
857 protected:
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800858 // Set mEnableInstrumentation based on system property
859 // hal.instrumentation.enable, register/de-register instrumentation
860 // callbacks if mEnableInstrumentation is true/false.
861 void configureInstrumentation(bool log=true);
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700862 // Function that lookup and dynamically loads the hidl instrumentation
863 // libraries and registers the instrumentation callback functions.
864 //
865 // The instrumentation libraries should be stored under any of the following
866 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
867 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
868 // follow pattern: ^profilerPrefix(.*).profiler.so$
869 //
870 // Each instrumentation library is expected to implement the instrumentation
871 // function called HIDL_INSTRUMENTATION_FUNCTION.
872 //
873 // A no-op for user build.
874 void registerInstrumentationCallbacks(
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700875 std::vector<InstrumentationCallback> *instrumentationCallbacks);
876
877 // Utility function to determine whether a give file is a instrumentation
878 // library (i.e. the file name follow the expected pattern).
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800879 bool isInstrumentationLib(const dirent *file);
880
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700881 // A list of registered instrumentation callbacks.
882 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
883 // Flag whether to enable instrumentation.
884 bool mEnableInstrumentation;
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800885 // Prefix to lookup the instrumentation libraries.
886 std::string mInstrumentationLibPrefix;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700887};
888
Yifan Hongbe7a6882017-01-05 17:30:17 -0800889///////////////////// toString functions
890
891namespace details {
892
893// toString alias for numeric types
894template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
895inline std::string toString(T t) {
896 return std::to_string(t);
897}
898
899template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
900inline std::string toHexString(T t, bool prefix = true) {
901 std::ostringstream os;
902 if (prefix) { os << std::showbase; }
903 os << std::hex << t;
904 return os.str();
905}
906
907template<>
908inline std::string toHexString(uint8_t t, bool prefix) {
909 return toHexString(static_cast<int32_t>(t), prefix);
910}
911
912template<>
913inline std::string toHexString(int8_t t, bool prefix) {
914 return toHexString(static_cast<int32_t>(t), prefix);
915}
916
917inline std::string toString(const void *t, bool prefix = true) {
918 return toHexString(reinterpret_cast<uintptr_t>(t), prefix);
919}
920
921// debug string dump. There will be quotes around the string!
922inline std::string toString(const hidl_string &hs) {
923 return std::string{"\""} + hs.c_str() + "\"";
924}
925
926// debug string dump
927inline std::string toString(const hidl_handle &hs) {
928 return toString(hs.getNativeHandle());
929}
930
931inline std::string toString(const hidl_memory &mem) {
932 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
933 + toString(mem.size())
934 + ", .handle = " + toString(mem.handle()) + "}";
935}
936
937inline std::string toString(const sp<hidl_death_recipient> &dr) {
938 return std::string{"death_recipient@"} + toString(dr.get());
939}
940
941template<typename Array>
942std::string arrayToString(const Array &a, size_t size);
943
944// debug string dump, assuming that toString(T) is defined.
945template<typename T>
946std::string toString(const hidl_vec<T> &a) {
947 std::string os;
948 os += "[" + toString(a.size()) + "]";
949 os += arrayToString(a, a.size());
950 return os;
951}
952
953template<size_t SIZE1>
954std::string arraySizeToString() {
955 return std::string{"["} + toString(SIZE1) + "]";
956}
957
958template<typename T, size_t SIZE1>
959std::string toString(const_accessor<T, SIZE1> a) {
960 return arrayToString(a, SIZE1);
961}
962
963template<typename T, size_t SIZE1>
964std::string toString(const hidl_array<T, SIZE1> &a) {
965 return arraySizeToString<SIZE1>()
966 + toString(const_accessor<T, SIZE1>(a.data()));
967}
968
969template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
970std::string arraySizeToString() {
971 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
972}
973
974
975template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
976std::string toString(const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
977 return arrayToString(a, SIZE1);
978}
979
980template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
981std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
982 return arraySizeToString<SIZE1, SIZE2, SIZES...>()
983 + toString(const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
984}
985
986template<typename Array>
987std::string arrayToString(const Array &a, size_t size) {
988 std::string os;
989 os += "{";
990 for (size_t i = 0; i < size; ++i) {
991 if (i > 0) {
992 os += ", ";
993 }
994 os += toString(a[i]);
995 }
996 os += "}";
997 return os;
998}
999
1000} // namespace details
1001
1002
Martijn Coenen72110162016-08-19 14:28:25 +02001003} // namespace hardware
1004} // namespace android
1005
Martijn Coenenc28f1152016-08-22 14:06:56 +02001006
Martijn Coenen72110162016-08-19 14:28:25 +02001007#endif // ANDROID_HIDL_SUPPORT_H