blob: a8f0f5512a57a531a267fddbb2971e4cee924427 [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>
Steven Morelandbdf26662016-09-02 11:03:15 -070023#include <dlfcn.h>
Scott Randolphbb840f72016-11-21 14:39:26 -080024#include <iterator>
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010025#include <cutils/native_handle.h>
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -070026#include <cutils/properties.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080027#include <functional>
Steven Moreland337c3ae2016-11-22 13:37:32 -080028#include <hidl/HidlInternal.h>
Yifan Honga3c31842016-10-21 10:33:14 -070029#include <hidl/Status.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080030#include <map>
Yifan Hongbe7a6882017-01-05 17:30:17 -080031#include <sstream>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080032#include <stddef.h>
Andreas Huber00a985c2016-09-28 14:24:53 -070033#include <tuple>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080034#include <type_traits>
Iliyan Malchev692070a2016-09-12 16:30:44 -070035#include <utils/Errors.h>
36#include <utils/RefBase.h>
37#include <utils/StrongPointer.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080038#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020039
40namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010041
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010042// this file is included by all hidl interface, so we must forward declare the
43// IMemory and IBase types.
Martijn Coenen30791002016-12-01 15:40:46 +010044namespace hidl {
45namespace memory {
46namespace V1_0 {
47 struct IMemory;
48}; // namespace V1_0
49}; // namespace manager
50}; // namespace hidl
51
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010052namespace hidl {
53namespace base {
54namespace V1_0 {
55 struct IBase;
56}; // namespace V1_0
57}; // namespace base
58}; // namespace hidl
59
Martijn Coenen72110162016-08-19 14:28:25 +020060namespace hardware {
61
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010062// hidl_death_recipient is a callback interfaced that can be used with
63// linkToDeath() / unlinkToDeath()
64struct hidl_death_recipient : public virtual RefBase {
65 virtual void serviceDied(uint64_t cookie,
66 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
67};
68
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010069// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
70// so that it can safely be transferred between 32-bit and 64-bit processes.
71struct hidl_handle {
72 hidl_handle() {
73 mHandle = nullptr;
74 }
75 ~hidl_handle() {
76 }
77
78 // copy constructors.
79 hidl_handle(const native_handle_t *handle) {
80 mHandle = handle;
81 }
82
83 hidl_handle(const hidl_handle &other) {
84 mHandle = other.mHandle;
85 }
86
87 // move constructor.
88 hidl_handle(hidl_handle &&other) {
89 *this = std::move(other);
90 }
91
92 // assingment operators
93 hidl_handle &operator=(const hidl_handle &other) {
94 mHandle = other.mHandle;
95 return *this;
96 }
97
98 hidl_handle &operator=(const native_handle_t *native_handle) {
99 mHandle = native_handle;
100 return *this;
101 }
102
103 hidl_handle &operator=(hidl_handle &&other) {
104 mHandle = other.mHandle;
105 other.mHandle = nullptr;
106 return *this;
107 }
108
109 const native_handle_t* operator->() const {
110 return mHandle;
111 }
112 // implicit conversion to const native_handle_t*
113 operator const native_handle_t *() const {
114 return mHandle;
115 }
116 // explicit conversion
117 const native_handle_t *getNativeHandle() const {
118 return mHandle;
119 }
120private:
121 details::hidl_pointer<const native_handle_t> mHandle;
122};
123
Martijn Coenen72110162016-08-19 14:28:25 +0200124struct hidl_string {
125 hidl_string();
126 ~hidl_string();
127
Yifan Hong602b85a2016-10-24 13:40:01 -0700128 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200129 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700130 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -0700131 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800132 // copy the first length characters from a C-style string.
133 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700134 // copy from an std::string.
135 hidl_string(const std::string &);
136
137 // move constructor.
138 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200139
140 const char *c_str() const;
141 size_t size() const;
142 bool empty() const;
143
Yifan Hong602b85a2016-10-24 13:40:01 -0700144 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700145 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700146 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200147 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700148 // copy from an std::string.
149 hidl_string &operator=(const std::string &);
150 // move assignment operator.
151 hidl_string &operator=(hidl_string &&other);
152 // cast to std::string.
153 operator std::string() const;
154 // cast to C-style string. Caller is responsible
155 // to maintain this hidl_string alive.
156 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700157
Martijn Coenen72110162016-08-19 14:28:25 +0200158 void clear();
159
160 // Reference an external char array. Ownership is _not_ transferred.
161 // Caller is responsible for ensuring that underlying memory is valid
162 // for the lifetime of this hidl_string.
163 void setToExternal(const char *data, size_t size);
164
Andreas Huberebfeb362016-08-25 13:39:05 -0700165 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
166 static const size_t kOffsetOfBuffer;
167
Martijn Coenen72110162016-08-19 14:28:25 +0200168private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100169 details::hidl_pointer<const char> mBuffer;
170 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700171 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200172
Yifan Hong602b85a2016-10-24 13:40:01 -0700173 // copy from data with size. Assume that my memory is freed
174 // (through clear(), for example)
175 void copyFrom(const char *data, size_t size);
176 // move from another hidl_string
177 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200178};
179
Scott Randolphbb840f72016-11-21 14:39:26 -0800180inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
181 return strcmp(hs1.c_str(), hs2.c_str()) == 0;
182}
183
184inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
185 return !(hs1 == hs2);
186}
187
Yifan Hong5708fb42016-10-26 17:50:29 -0700188inline bool operator==(const hidl_string &hs, const char *s) {
189 return strcmp(hs.c_str(), s) == 0;
190}
191
192inline bool operator!=(const hidl_string &hs, const char *s) {
193 return !(hs == s);
194}
195
196inline bool operator==(const char *s, const hidl_string &hs) {
197 return strcmp(hs.c_str(), s) == 0;
198}
199
200inline bool operator!=(const char *s, const hidl_string &hs) {
201 return !(s == hs);
202}
203
Martijn Coenen30791002016-12-01 15:40:46 +0100204// hidl_memory is a structure that can be used to transfer
205// pieces of shared memory between processes. The assumption
206// of this object is that the memory remains accessible as
207// long as the file descriptors in the enclosed mHandle
208// - as well as all of its cross-process dups() - remain opened.
209struct hidl_memory {
210
211 hidl_memory() : mOwnsHandle(false), mHandle(nullptr), mSize(0), mName("") {
212 }
213
214 /**
215 * Creates a hidl_memory object and takes ownership of the handle.
216 */
217 hidl_memory(const hidl_string &name, const hidl_handle &handle, size_t size)
218 : mOwnsHandle(true),
219 mHandle(handle),
220 mSize(size),
221 mName(name)
222 {}
223
224 // copy constructor
225 hidl_memory(const hidl_memory& other) {
226 *this = other;
227 }
228
229 // copy assignment
230 hidl_memory &operator=(const hidl_memory &other) {
231 if (this != &other) {
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800232 cleanup();
233
234 if (other.mHandle == nullptr) {
235 mHandle = nullptr;
236 mOwnsHandle = false;
237 } else {
238 mOwnsHandle = true;
239 mHandle = native_handle_clone(other.mHandle);
240 }
Martijn Coenen30791002016-12-01 15:40:46 +0100241 mSize = other.mSize;
242 mName = other.mName;
243 }
244
245 return *this;
246 }
247
248 // TODO move constructor/move assignment
249
250 ~hidl_memory() {
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800251 cleanup();
Martijn Coenen30791002016-12-01 15:40:46 +0100252 }
253
254 const native_handle_t* handle() const {
255 return mHandle;
256 }
257
258 const hidl_string &name() const {
259 return mName;
260 }
261
262 size_t size() const {
263 return mSize;
264 }
265
266 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
267 static const size_t kOffsetOfHandle;
268 // offsetof(hidl_memory, mName) exposed since mHandle is private.
269 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800270
Martijn Coenen30791002016-12-01 15:40:46 +0100271private:
272 bool mOwnsHandle;
273 hidl_handle mHandle;
274 size_t mSize;
275 hidl_string mName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800276
277 void cleanup() {
278 // TODO(b/33812533): native_handle_delete
279 if (mOwnsHandle && mHandle != nullptr) {
280 native_handle_close(mHandle);
281 }
282 }
Martijn Coenen30791002016-12-01 15:40:46 +0100283};
284
Andreas Huber20dce082016-09-22 19:39:13 -0700285////////////////////////////////////////////////////////////////////////////////
286
Martijn Coenen72110162016-08-19 14:28:25 +0200287template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100288struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200289 hidl_vec()
290 : mBuffer(NULL),
291 mSize(0),
292 mOwnsBuffer(true) {
293 }
294
Yifan Hong602b85a2016-10-24 13:40:01 -0700295 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200296 *this = other;
297 }
298
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100299 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800300 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100301 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700302 }
303
Steven Morelandb69926a2016-11-15 10:02:57 -0800304 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800305 : mOwnsBuffer(true) {
306 if (list.size() > UINT32_MAX) {
307 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
308 }
309 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800310 mBuffer = new T[mSize];
311
Steven Morelandb69926a2016-11-15 10:02:57 -0800312 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800313 for (auto it = list.begin(); it != list.end(); ++it) {
314 mBuffer[idx++] = *it;
315 }
316 }
317
Yifan Hong602b85a2016-10-24 13:40:01 -0700318 hidl_vec(const std::vector<T> &other) : hidl_vec() {
319 *this = other;
320 }
321
Martijn Coenen72110162016-08-19 14:28:25 +0200322 ~hidl_vec() {
323 if (mOwnsBuffer) {
324 delete[] mBuffer;
325 }
326 mBuffer = NULL;
327 }
328
Alexey Polyudove2299012016-10-19 09:52:00 -0700329 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200330 // caller's responsibility to ensure that the underlying memory stays
331 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700332 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200333 if (mOwnsBuffer) {
334 delete [] mBuffer;
335 }
336 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100337 if (size > UINT32_MAX) {
338 logAlwaysFatal("external vector size exceeds 2^32 elements.");
339 }
340 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700341 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200342 }
343
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700344 T *data() {
345 return mBuffer;
346 }
347
348 const T *data() const {
349 return mBuffer;
350 }
351
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700352 T *releaseData() {
353 if (!mOwnsBuffer && mSize > 0) {
354 resize(mSize);
355 }
356 mOwnsBuffer = false;
357 return mBuffer;
358 }
359
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700360 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100361 if (mOwnsBuffer) {
362 delete[] mBuffer;
363 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700364 mBuffer = other.mBuffer;
365 mSize = other.mSize;
366 mOwnsBuffer = other.mOwnsBuffer;
367 other.mOwnsBuffer = false;
368 return *this;
369 }
370
Martijn Coenen72110162016-08-19 14:28:25 +0200371 hidl_vec &operator=(const hidl_vec &other) {
372 if (this != &other) {
373 if (mOwnsBuffer) {
374 delete[] mBuffer;
375 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700376 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200377 }
378
379 return *this;
380 }
381
Yifan Hong602b85a2016-10-24 13:40:01 -0700382 // copy from an std::vector.
383 hidl_vec &operator=(const std::vector<T> &other) {
384 if (mOwnsBuffer) {
385 delete[] mBuffer;
386 }
387 copyFrom(other, other.size());
388 return *this;
389 }
390
391 // cast to an std::vector.
392 operator std::vector<T>() const {
393 std::vector<T> v(mSize);
394 for (size_t i = 0; i < mSize; ++i) {
395 v[i] = mBuffer[i];
396 }
397 return v;
398 }
399
Yifan Hong9fcbb362016-12-20 16:46:41 -0800400 // equality check, assuming that T::operator== is defined.
401 bool operator==(const hidl_vec &other) const {
402 if (mSize != other.size()) {
403 return false;
404 }
405 for (size_t i = 0; i < mSize; ++i) {
406 if (!(mBuffer[i] == other.mBuffer[i])) {
407 return false;
408 }
409 }
410 return true;
411 }
412
413 // inequality check, assuming that T::operator== is defined.
414 inline bool operator!=(const hidl_vec &other) const {
415 return !((*this) == other);
416 }
417
Martijn Coenen72110162016-08-19 14:28:25 +0200418 size_t size() const {
419 return mSize;
420 }
421
422 T &operator[](size_t index) {
423 return mBuffer[index];
424 }
425
426 const T &operator[](size_t index) const {
427 return mBuffer[index];
428 }
429
430 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100431 if (size > UINT32_MAX) {
432 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
433 }
Martijn Coenen72110162016-08-19 14:28:25 +0200434 T *newBuffer = new T[size];
435
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100436 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200437 newBuffer[i] = mBuffer[i];
438 }
439
440 if (mOwnsBuffer) {
441 delete[] mBuffer;
442 }
443 mBuffer = newBuffer;
444
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100445 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200446 mOwnsBuffer = true;
447 }
448
Yifan Hong089ae132016-11-11 11:32:52 -0800449 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
450 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800451
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800452private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800453 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800454 template<bool is_const>
455 class iter : public std::iterator<
456 std::random_access_iterator_tag, /* Category */
457 T,
458 ptrdiff_t, /* Distance */
459 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
460 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800461 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800462 using traits = std::iterator_traits<iter>;
463 using ptr_type = typename traits::pointer;
464 using ref_type = typename traits::reference;
465 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800466 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800467 iter(ptr_type ptr) : mPtr(ptr) { }
468 inline iter &operator++() { mPtr++; return *this; }
469 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
470 inline iter &operator--() { mPtr--; return *this; }
471 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
472 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
473 inline iter operator+(diff_type n) const { return mPtr + n; }
474 inline iter operator-(diff_type n) const { return mPtr - n; }
475 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
476 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
477 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
478 inline ref_type operator*() const { return *mPtr; }
479 inline ptr_type operator->() const { return mPtr; }
480 inline bool operator==(const iter &rhs) const { return mPtr == rhs.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 ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800487 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800488 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800489 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800490public:
491 using iterator = iter<false /* is_const */>;
492 using const_iterator = iter<true /* is_const */>;
493
Scott Randolphbb840f72016-11-21 14:39:26 -0800494 iterator begin() { return data(); }
495 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800496 const_iterator begin() const { return data(); }
497 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800498
Martijn Coenen72110162016-08-19 14:28:25 +0200499private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100500 details::hidl_pointer<T> mBuffer;
501 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200502 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700503
504 // copy from an array-like object, assuming my resources are freed.
505 template <typename Array>
506 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800507 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700508 mOwnsBuffer = true;
509 if (mSize > 0) {
510 mBuffer = new T[size];
511 for (size_t i = 0; i < size; ++i) {
512 mBuffer[i] = data[i];
513 }
514 } else {
515 mBuffer = NULL;
516 }
517 }
Martijn Coenen72110162016-08-19 14:28:25 +0200518};
519
Yifan Hong089ae132016-11-11 11:32:52 -0800520template <typename T>
521const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
522
Andreas Huber20dce082016-09-22 19:39:13 -0700523////////////////////////////////////////////////////////////////////////////////
524
525namespace details {
526
527 template<size_t SIZE1, size_t... SIZES>
528 struct product {
529 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
530 };
531
532 template<size_t SIZE1>
533 struct product<SIZE1> {
534 static constexpr size_t value = SIZE1;
535 };
536
537 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800538 struct std_array {
539 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
540 };
541
542 template<typename T, size_t SIZE1>
543 struct std_array<T, SIZE1> {
544 using type = std::array<T, SIZE1>;
545 };
546
547 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700548 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800549
550 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
551
Andreas Huber20dce082016-09-22 19:39:13 -0700552 explicit accessor(T *base)
553 : mBase(base) {
554 }
555
556 accessor<T, SIZES...> operator[](size_t index) {
557 return accessor<T, SIZES...>(
558 &mBase[index * product<SIZES...>::value]);
559 }
560
Yifan Hong44ab6232016-11-22 16:28:24 -0800561 accessor &operator=(const std_array_type &other) {
562 for (size_t i = 0; i < SIZE1; ++i) {
563 (*this)[i] = other[i];
564 }
565 return *this;
566 }
567
Andreas Huber20dce082016-09-22 19:39:13 -0700568 private:
569 T *mBase;
570 };
571
572 template<typename T, size_t SIZE1>
573 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800574
575 using std_array_type = typename std_array<T, SIZE1>::type;
576
Andreas Huber20dce082016-09-22 19:39:13 -0700577 explicit accessor(T *base)
578 : mBase(base) {
579 }
580
581 T &operator[](size_t index) {
582 return mBase[index];
583 }
584
Yifan Hong44ab6232016-11-22 16:28:24 -0800585 accessor &operator=(const std_array_type &other) {
586 for (size_t i = 0; i < SIZE1; ++i) {
587 (*this)[i] = other[i];
588 }
589 return *this;
590 }
591
Andreas Huber20dce082016-09-22 19:39:13 -0700592 private:
593 T *mBase;
594 };
595
596 template<typename T, size_t SIZE1, size_t... SIZES>
597 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800598
599 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
600
Andreas Huber20dce082016-09-22 19:39:13 -0700601 explicit const_accessor(const T *base)
602 : mBase(base) {
603 }
604
Yifan Hongbe7a6882017-01-05 17:30:17 -0800605 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700606 return const_accessor<T, SIZES...>(
607 &mBase[index * product<SIZES...>::value]);
608 }
609
Yifan Hong44ab6232016-11-22 16:28:24 -0800610 operator std_array_type() {
611 std_array_type array;
612 for (size_t i = 0; i < SIZE1; ++i) {
613 array[i] = (*this)[i];
614 }
615 return array;
616 }
617
Andreas Huber20dce082016-09-22 19:39:13 -0700618 private:
619 const T *mBase;
620 };
621
622 template<typename T, size_t SIZE1>
623 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800624
625 using std_array_type = typename std_array<T, SIZE1>::type;
626
Andreas Huber20dce082016-09-22 19:39:13 -0700627 explicit const_accessor(const T *base)
628 : mBase(base) {
629 }
630
631 const T &operator[](size_t index) const {
632 return mBase[index];
633 }
634
Yifan Hong44ab6232016-11-22 16:28:24 -0800635 operator std_array_type() {
636 std_array_type array;
637 for (size_t i = 0; i < SIZE1; ++i) {
638 array[i] = (*this)[i];
639 }
640 return array;
641 }
642
Andreas Huber20dce082016-09-22 19:39:13 -0700643 private:
644 const T *mBase;
645 };
646
647} // namespace details
648
649////////////////////////////////////////////////////////////////////////////////
650
Yifan Hong44ab6232016-11-22 16:28:24 -0800651// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700652template<typename T, size_t SIZE1, size_t... SIZES>
653struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800654
655 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
656
Andreas Huber20dce082016-09-22 19:39:13 -0700657 hidl_array() = default;
658
Yifan Hong44ab6232016-11-22 16:28:24 -0800659 // Copies the data from source, using T::operator=(const T &).
660 hidl_array(const T *source) {
661 for (size_t i = 0; i < elementCount(); ++i) {
662 mBuffer[i] = source[i];
663 }
664 }
665
666 // Copies the data from the given std::array, using T::operator=(const T &).
667 hidl_array(const std_array_type &array) {
668 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
669 modifier = array;
670 }
671
Andreas Huber20dce082016-09-22 19:39:13 -0700672 T *data() { return mBuffer; }
673 const T *data() const { return mBuffer; }
674
675 details::accessor<T, SIZES...> operator[](size_t index) {
676 return details::accessor<T, SIZES...>(
677 &mBuffer[index * details::product<SIZES...>::value]);
678 }
679
680 details::const_accessor<T, SIZES...> operator[](size_t index) const {
681 return details::const_accessor<T, SIZES...>(
682 &mBuffer[index * details::product<SIZES...>::value]);
683 }
684
Yifan Hong9fcbb362016-12-20 16:46:41 -0800685 // equality check, assuming that T::operator== is defined.
686 bool operator==(const hidl_array &other) const {
687 for (size_t i = 0; i < elementCount(); ++i) {
688 if (!(mBuffer[i] == other.mBuffer[i])) {
689 return false;
690 }
691 }
692 return true;
693 }
694
695 inline bool operator!=(const hidl_array &other) const {
696 return !((*this) == other);
697 }
698
Andreas Huber00a985c2016-09-28 14:24:53 -0700699 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
700
701 static constexpr size_tuple_type size() {
702 return std::make_tuple(SIZE1, SIZES...);
703 }
704
Yifan Hong44ab6232016-11-22 16:28:24 -0800705 static constexpr size_t elementCount() {
706 return details::product<SIZE1, SIZES...>::value;
707 }
708
709 operator std_array_type() const {
710 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
711 }
712
Andreas Huber20dce082016-09-22 19:39:13 -0700713private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800714 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700715};
716
Yifan Hong44ab6232016-11-22 16:28:24 -0800717// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700718template<typename T, size_t SIZE1>
719struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800720
721 using std_array_type = typename details::std_array<T, SIZE1>::type;
722
Andreas Huber20dce082016-09-22 19:39:13 -0700723 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800724
725 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800726 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800727 for (size_t i = 0; i < elementCount(); ++i) {
728 mBuffer[i] = source[i];
729 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800730 }
Andreas Huber20dce082016-09-22 19:39:13 -0700731
Yifan Hong44ab6232016-11-22 16:28:24 -0800732 // Copies the data from the given std::array, using T::operator=(const T &).
733 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
734
Andreas Huber20dce082016-09-22 19:39:13 -0700735 T *data() { return mBuffer; }
736 const T *data() const { return mBuffer; }
737
738 T &operator[](size_t index) {
739 return mBuffer[index];
740 }
741
742 const T &operator[](size_t index) const {
743 return mBuffer[index];
744 }
745
Yifan Hong9fcbb362016-12-20 16:46:41 -0800746 // equality check, assuming that T::operator== is defined.
747 bool operator==(const hidl_array &other) const {
748 for (size_t i = 0; i < elementCount(); ++i) {
749 if (!(mBuffer[i] == other.mBuffer[i])) {
750 return false;
751 }
752 }
753 return true;
754 }
755
756 inline bool operator!=(const hidl_array &other) const {
757 return !((*this) == other);
758 }
759
Andreas Huber00a985c2016-09-28 14:24:53 -0700760 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800761 static constexpr size_t elementCount() { return SIZE1; }
762
763 // Copies the data to an std::array, using T::operator=(T).
764 operator std_array_type() const {
765 std_array_type array;
766 for (size_t i = 0; i < SIZE1; ++i) {
767 array[i] = mBuffer[i];
768 }
769 return array;
770 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700771
Andreas Huber20dce082016-09-22 19:39:13 -0700772private:
773 T mBuffer[SIZE1];
774};
775
Martijn Coenen72110162016-08-19 14:28:25 +0200776// ----------------------------------------------------------------------
777// Version functions
778struct hidl_version {
779public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800780 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200781
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700782 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200783 return (mMajor == other.get_major() && mMinor == other.get_minor());
784 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200785
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800786 bool operator<(const hidl_version& other) const {
787 return (mMajor < other.get_major() ||
788 (mMajor == other.get_major() && mMinor < other.get_minor()));
789 }
790
791 bool operator>(const hidl_version& other) const {
792 return other < *this;
793 }
794
795 bool operator<=(const hidl_version& other) const {
796 return !(*this > other);
797 }
798
799 bool operator>=(const hidl_version& other) const {
800 return !(*this < other);
801 }
802
Martijn Coenen097a7672016-09-08 16:56:41 +0200803 constexpr uint16_t get_major() const { return mMajor; }
804 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200805
Martijn Coenen72110162016-08-19 14:28:25 +0200806private:
807 uint16_t mMajor;
808 uint16_t mMinor;
809};
810
811inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
812 return hidl_version(major,minor);
813}
814
Steven Morelandbdf26662016-09-02 11:03:15 -0700815#if defined(__LP64__)
816#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
817#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
818#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
819#else
820#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
821#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
822#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
823#endif
824
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700825// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700826// Class that provides Hidl instrumentation utilities.
827struct HidlInstrumentor {
828 // Event that triggers the instrumentation. e.g. enter of an API call on
829 // the server/client side, exit of an API call on the server/client side
830 // etc.
831 enum InstrumentationEvent {
832 SERVER_API_ENTRY = 0,
833 SERVER_API_EXIT,
834 CLIENT_API_ENTRY,
835 CLIENT_API_EXIT,
836 SYNC_CALLBACK_ENTRY,
837 SYNC_CALLBACK_EXIT,
838 ASYNC_CALLBACK_ENTRY,
839 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700840 PASSTHROUGH_ENTRY,
841 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700842 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700843
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700844 // Signature of the instrumentation callback function.
845 using InstrumentationCallback = std::function<void(
846 const InstrumentationEvent event,
847 const char *package,
848 const char *version,
849 const char *interface,
850 const char *method,
851 std::vector<void *> *args)>;
852
853 explicit HidlInstrumentor(const std::string &prefix);
854 virtual ~HidlInstrumentor();
855
856 protected:
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800857 // Set mEnableInstrumentation based on system property
858 // hal.instrumentation.enable, register/de-register instrumentation
859 // callbacks if mEnableInstrumentation is true/false.
860 void configureInstrumentation(bool log=true);
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700861 // Function that lookup and dynamically loads the hidl instrumentation
862 // libraries and registers the instrumentation callback functions.
863 //
864 // The instrumentation libraries should be stored under any of the following
865 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
866 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
867 // follow pattern: ^profilerPrefix(.*).profiler.so$
868 //
869 // Each instrumentation library is expected to implement the instrumentation
870 // function called HIDL_INSTRUMENTATION_FUNCTION.
871 //
872 // A no-op for user build.
873 void registerInstrumentationCallbacks(
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700874 std::vector<InstrumentationCallback> *instrumentationCallbacks);
875
876 // Utility function to determine whether a give file is a instrumentation
877 // library (i.e. the file name follow the expected pattern).
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800878 bool isInstrumentationLib(const dirent *file);
879
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700880 // A list of registered instrumentation callbacks.
881 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
882 // Flag whether to enable instrumentation.
883 bool mEnableInstrumentation;
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800884 // Prefix to lookup the instrumentation libraries.
885 std::string mInstrumentationLibPrefix;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700886};
887
Yifan Hongbe7a6882017-01-05 17:30:17 -0800888///////////////////// toString functions
889
890namespace details {
891
892// toString alias for numeric types
893template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
894inline std::string toString(T t) {
895 return std::to_string(t);
896}
897
898template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
899inline std::string toHexString(T t, bool prefix = true) {
900 std::ostringstream os;
901 if (prefix) { os << std::showbase; }
902 os << std::hex << t;
903 return os.str();
904}
905
906template<>
907inline std::string toHexString(uint8_t t, bool prefix) {
908 return toHexString(static_cast<int32_t>(t), prefix);
909}
910
911template<>
912inline std::string toHexString(int8_t t, bool prefix) {
913 return toHexString(static_cast<int32_t>(t), prefix);
914}
915
916inline std::string toString(const void *t, bool prefix = true) {
917 return toHexString(reinterpret_cast<uintptr_t>(t), prefix);
918}
919
920// debug string dump. There will be quotes around the string!
921inline std::string toString(const hidl_string &hs) {
922 return std::string{"\""} + hs.c_str() + "\"";
923}
924
925// debug string dump
926inline std::string toString(const hidl_handle &hs) {
927 return toString(hs.getNativeHandle());
928}
929
930inline std::string toString(const hidl_memory &mem) {
931 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
932 + toString(mem.size())
933 + ", .handle = " + toString(mem.handle()) + "}";
934}
935
936inline std::string toString(const sp<hidl_death_recipient> &dr) {
937 return std::string{"death_recipient@"} + toString(dr.get());
938}
939
940template<typename Array>
941std::string arrayToString(const Array &a, size_t size);
942
943// debug string dump, assuming that toString(T) is defined.
944template<typename T>
945std::string toString(const hidl_vec<T> &a) {
946 std::string os;
947 os += "[" + toString(a.size()) + "]";
948 os += arrayToString(a, a.size());
949 return os;
950}
951
952template<size_t SIZE1>
953std::string arraySizeToString() {
954 return std::string{"["} + toString(SIZE1) + "]";
955}
956
957template<typename T, size_t SIZE1>
958std::string toString(const_accessor<T, SIZE1> a) {
959 return arrayToString(a, SIZE1);
960}
961
962template<typename T, size_t SIZE1>
963std::string toString(const hidl_array<T, SIZE1> &a) {
964 return arraySizeToString<SIZE1>()
965 + toString(const_accessor<T, SIZE1>(a.data()));
966}
967
968template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
969std::string arraySizeToString() {
970 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
971}
972
973
974template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
975std::string toString(const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
976 return arrayToString(a, SIZE1);
977}
978
979template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
980std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
981 return arraySizeToString<SIZE1, SIZE2, SIZES...>()
982 + toString(const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
983}
984
985template<typename Array>
986std::string arrayToString(const Array &a, size_t size) {
987 std::string os;
988 os += "{";
989 for (size_t i = 0; i < size; ++i) {
990 if (i > 0) {
991 os += ", ";
992 }
993 os += toString(a[i]);
994 }
995 os += "}";
996 return os;
997}
998
999} // namespace details
1000
1001
Martijn Coenen72110162016-08-19 14:28:25 +02001002} // namespace hardware
1003} // namespace android
1004
Martijn Coenenc28f1152016-08-22 14:06:56 +02001005
Martijn Coenen72110162016-08-19 14:28:25 +02001006#endif // ANDROID_HIDL_SUPPORT_H