blob: 8bdb5feac297a6ffabb69cf32fd9c4fa61acb568 [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 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
41// this file is included by all hidl interface, so we must forward declare the IMemory type.
42namespace hidl {
43namespace memory {
44namespace V1_0 {
45 struct IMemory;
46}; // namespace V1_0
47}; // namespace manager
48}; // namespace hidl
49
Martijn Coenen72110162016-08-19 14:28:25 +020050namespace hardware {
51
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010052// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
53// so that it can safely be transferred between 32-bit and 64-bit processes.
54struct hidl_handle {
55 hidl_handle() {
56 mHandle = nullptr;
57 }
58 ~hidl_handle() {
59 }
60
61 // copy constructors.
62 hidl_handle(const native_handle_t *handle) {
63 mHandle = handle;
64 }
65
66 hidl_handle(const hidl_handle &other) {
67 mHandle = other.mHandle;
68 }
69
70 // move constructor.
71 hidl_handle(hidl_handle &&other) {
72 *this = std::move(other);
73 }
74
75 // assingment operators
76 hidl_handle &operator=(const hidl_handle &other) {
77 mHandle = other.mHandle;
78 return *this;
79 }
80
81 hidl_handle &operator=(const native_handle_t *native_handle) {
82 mHandle = native_handle;
83 return *this;
84 }
85
86 hidl_handle &operator=(hidl_handle &&other) {
87 mHandle = other.mHandle;
88 other.mHandle = nullptr;
89 return *this;
90 }
91
92 const native_handle_t* operator->() const {
93 return mHandle;
94 }
95 // implicit conversion to const native_handle_t*
96 operator const native_handle_t *() const {
97 return mHandle;
98 }
99 // explicit conversion
100 const native_handle_t *getNativeHandle() const {
101 return mHandle;
102 }
103private:
104 details::hidl_pointer<const native_handle_t> mHandle;
105};
106
Martijn Coenen72110162016-08-19 14:28:25 +0200107struct hidl_string {
108 hidl_string();
109 ~hidl_string();
110
Yifan Hong602b85a2016-10-24 13:40:01 -0700111 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200112 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700113 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -0700114 hidl_string(const char *);
Yifan Hong602b85a2016-10-24 13:40:01 -0700115 // copy from an std::string.
116 hidl_string(const std::string &);
117
118 // move constructor.
119 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200120
121 const char *c_str() const;
122 size_t size() const;
123 bool empty() const;
124
Yifan Hong602b85a2016-10-24 13:40:01 -0700125 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700126 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700127 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200128 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700129 // copy from an std::string.
130 hidl_string &operator=(const std::string &);
131 // move assignment operator.
132 hidl_string &operator=(hidl_string &&other);
133 // cast to std::string.
134 operator std::string() const;
135 // cast to C-style string. Caller is responsible
136 // to maintain this hidl_string alive.
137 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700138
Martijn Coenen72110162016-08-19 14:28:25 +0200139 void clear();
140
141 // Reference an external char array. Ownership is _not_ transferred.
142 // Caller is responsible for ensuring that underlying memory is valid
143 // for the lifetime of this hidl_string.
144 void setToExternal(const char *data, size_t size);
145
Andreas Huberebfeb362016-08-25 13:39:05 -0700146 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
147 static const size_t kOffsetOfBuffer;
148
Martijn Coenen72110162016-08-19 14:28:25 +0200149private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100150 details::hidl_pointer<const char> mBuffer;
151 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700152 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200153
Yifan Hong602b85a2016-10-24 13:40:01 -0700154 // copy from data with size. Assume that my memory is freed
155 // (through clear(), for example)
156 void copyFrom(const char *data, size_t size);
157 // move from another hidl_string
158 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200159};
160
Scott Randolphbb840f72016-11-21 14:39:26 -0800161inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
162 return strcmp(hs1.c_str(), hs2.c_str()) == 0;
163}
164
165inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
166 return !(hs1 == hs2);
167}
168
Yifan Hong5708fb42016-10-26 17:50:29 -0700169inline bool operator==(const hidl_string &hs, const char *s) {
170 return strcmp(hs.c_str(), s) == 0;
171}
172
173inline bool operator!=(const hidl_string &hs, const char *s) {
174 return !(hs == s);
175}
176
177inline bool operator==(const char *s, const hidl_string &hs) {
178 return strcmp(hs.c_str(), s) == 0;
179}
180
181inline bool operator!=(const char *s, const hidl_string &hs) {
182 return !(s == hs);
183}
184
Martijn Coenen30791002016-12-01 15:40:46 +0100185// hidl_memory is a structure that can be used to transfer
186// pieces of shared memory between processes. The assumption
187// of this object is that the memory remains accessible as
188// long as the file descriptors in the enclosed mHandle
189// - as well as all of its cross-process dups() - remain opened.
190struct hidl_memory {
191
192 hidl_memory() : mOwnsHandle(false), mHandle(nullptr), mSize(0), mName("") {
193 }
194
195 /**
196 * Creates a hidl_memory object and takes ownership of the handle.
197 */
198 hidl_memory(const hidl_string &name, const hidl_handle &handle, size_t size)
199 : mOwnsHandle(true),
200 mHandle(handle),
201 mSize(size),
202 mName(name)
203 {}
204
205 // copy constructor
206 hidl_memory(const hidl_memory& other) {
207 *this = other;
208 }
209
210 // copy assignment
211 hidl_memory &operator=(const hidl_memory &other) {
212 if (this != &other) {
213 mOwnsHandle = true;
214 mHandle = native_handle_clone(other.mHandle);
215 mSize = other.mSize;
216 mName = other.mName;
217 }
218
219 return *this;
220 }
221
222 // TODO move constructor/move assignment
223
224 ~hidl_memory() {
225 // TODO if we had previously mapped from this object, unmap
226 if (mOwnsHandle) {
227 native_handle_close(mHandle);
228 }
229 }
230
231 const native_handle_t* handle() const {
232 return mHandle;
233 }
234
235 const hidl_string &name() const {
236 return mName;
237 }
238
239 size_t size() const {
240 return mSize;
241 }
242
243 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
244 static const size_t kOffsetOfHandle;
245 // offsetof(hidl_memory, mName) exposed since mHandle is private.
246 static const size_t kOffsetOfName;
247private:
248 bool mOwnsHandle;
249 hidl_handle mHandle;
250 size_t mSize;
251 hidl_string mName;
252};
253
Andreas Huber20dce082016-09-22 19:39:13 -0700254////////////////////////////////////////////////////////////////////////////////
255
Martijn Coenen72110162016-08-19 14:28:25 +0200256template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100257struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200258 hidl_vec()
259 : mBuffer(NULL),
260 mSize(0),
261 mOwnsBuffer(true) {
262 }
263
Yifan Hong602b85a2016-10-24 13:40:01 -0700264 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200265 *this = other;
266 }
267
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100268 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800269 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100270 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700271 }
272
Steven Morelandb69926a2016-11-15 10:02:57 -0800273 hidl_vec(const std::initializer_list<T> list)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800274 : mSize(list.size()),
275 mOwnsBuffer(true) {
276 mBuffer = new T[mSize];
277
Steven Morelandb69926a2016-11-15 10:02:57 -0800278 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800279 for (auto it = list.begin(); it != list.end(); ++it) {
280 mBuffer[idx++] = *it;
281 }
282 }
283
Yifan Hong602b85a2016-10-24 13:40:01 -0700284 hidl_vec(const std::vector<T> &other) : hidl_vec() {
285 *this = other;
286 }
287
Martijn Coenen72110162016-08-19 14:28:25 +0200288 ~hidl_vec() {
289 if (mOwnsBuffer) {
290 delete[] mBuffer;
291 }
292 mBuffer = NULL;
293 }
294
Alexey Polyudove2299012016-10-19 09:52:00 -0700295 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200296 // caller's responsibility to ensure that the underlying memory stays
297 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700298 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200299 if (mOwnsBuffer) {
300 delete [] mBuffer;
301 }
302 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100303 if (size > UINT32_MAX) {
304 logAlwaysFatal("external vector size exceeds 2^32 elements.");
305 }
306 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700307 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200308 }
309
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700310 T *data() {
311 return mBuffer;
312 }
313
314 const T *data() const {
315 return mBuffer;
316 }
317
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700318 T *releaseData() {
319 if (!mOwnsBuffer && mSize > 0) {
320 resize(mSize);
321 }
322 mOwnsBuffer = false;
323 return mBuffer;
324 }
325
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700326 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100327 if (mOwnsBuffer) {
328 delete[] mBuffer;
329 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700330 mBuffer = other.mBuffer;
331 mSize = other.mSize;
332 mOwnsBuffer = other.mOwnsBuffer;
333 other.mOwnsBuffer = false;
334 return *this;
335 }
336
Martijn Coenen72110162016-08-19 14:28:25 +0200337 hidl_vec &operator=(const hidl_vec &other) {
338 if (this != &other) {
339 if (mOwnsBuffer) {
340 delete[] mBuffer;
341 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700342 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200343 }
344
345 return *this;
346 }
347
Yifan Hong602b85a2016-10-24 13:40:01 -0700348 // copy from an std::vector.
349 hidl_vec &operator=(const std::vector<T> &other) {
350 if (mOwnsBuffer) {
351 delete[] mBuffer;
352 }
353 copyFrom(other, other.size());
354 return *this;
355 }
356
357 // cast to an std::vector.
358 operator std::vector<T>() const {
359 std::vector<T> v(mSize);
360 for (size_t i = 0; i < mSize; ++i) {
361 v[i] = mBuffer[i];
362 }
363 return v;
364 }
365
Martijn Coenen72110162016-08-19 14:28:25 +0200366 size_t size() const {
367 return mSize;
368 }
369
370 T &operator[](size_t index) {
371 return mBuffer[index];
372 }
373
374 const T &operator[](size_t index) const {
375 return mBuffer[index];
376 }
377
378 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100379 if (size > UINT32_MAX) {
380 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
381 }
Martijn Coenen72110162016-08-19 14:28:25 +0200382 T *newBuffer = new T[size];
383
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100384 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200385 newBuffer[i] = mBuffer[i];
386 }
387
388 if (mOwnsBuffer) {
389 delete[] mBuffer;
390 }
391 mBuffer = newBuffer;
392
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100393 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200394 mOwnsBuffer = true;
395 }
396
Yifan Hong089ae132016-11-11 11:32:52 -0800397 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
398 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800399
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800400private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800401 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800402 template<bool is_const>
403 class iter : public std::iterator<
404 std::random_access_iterator_tag, /* Category */
405 T,
406 ptrdiff_t, /* Distance */
407 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
408 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800409 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800410 using traits = std::iterator_traits<iter>;
411 using ptr_type = typename traits::pointer;
412 using ref_type = typename traits::reference;
413 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800414 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800415 iter(ptr_type ptr) : mPtr(ptr) { }
416 inline iter &operator++() { mPtr++; return *this; }
417 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
418 inline iter &operator--() { mPtr--; return *this; }
419 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
420 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
421 inline iter operator+(diff_type n) const { return mPtr + n; }
422 inline iter operator-(diff_type n) const { return mPtr - n; }
423 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
424 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
425 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
426 inline ref_type operator*() const { return *mPtr; }
427 inline ptr_type operator->() const { return mPtr; }
428 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
429 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
430 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
431 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
432 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
433 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
434 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800435 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800436 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800437 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800438public:
439 using iterator = iter<false /* is_const */>;
440 using const_iterator = iter<true /* is_const */>;
441
Scott Randolphbb840f72016-11-21 14:39:26 -0800442 iterator begin() { return data(); }
443 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800444 const_iterator begin() const { return data(); }
445 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800446
Martijn Coenen72110162016-08-19 14:28:25 +0200447private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100448 details::hidl_pointer<T> mBuffer;
449 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200450 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700451
452 // copy from an array-like object, assuming my resources are freed.
453 template <typename Array>
454 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800455 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700456 mOwnsBuffer = true;
457 if (mSize > 0) {
458 mBuffer = new T[size];
459 for (size_t i = 0; i < size; ++i) {
460 mBuffer[i] = data[i];
461 }
462 } else {
463 mBuffer = NULL;
464 }
465 }
Martijn Coenen72110162016-08-19 14:28:25 +0200466};
467
Yifan Hong089ae132016-11-11 11:32:52 -0800468template <typename T>
469const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
470
Andreas Huber20dce082016-09-22 19:39:13 -0700471////////////////////////////////////////////////////////////////////////////////
472
473namespace details {
474
475 template<size_t SIZE1, size_t... SIZES>
476 struct product {
477 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
478 };
479
480 template<size_t SIZE1>
481 struct product<SIZE1> {
482 static constexpr size_t value = SIZE1;
483 };
484
485 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800486 struct std_array {
487 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
488 };
489
490 template<typename T, size_t SIZE1>
491 struct std_array<T, SIZE1> {
492 using type = std::array<T, SIZE1>;
493 };
494
495 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700496 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800497
498 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
499
Andreas Huber20dce082016-09-22 19:39:13 -0700500 explicit accessor(T *base)
501 : mBase(base) {
502 }
503
504 accessor<T, SIZES...> operator[](size_t index) {
505 return accessor<T, SIZES...>(
506 &mBase[index * product<SIZES...>::value]);
507 }
508
Yifan Hong44ab6232016-11-22 16:28:24 -0800509 accessor &operator=(const std_array_type &other) {
510 for (size_t i = 0; i < SIZE1; ++i) {
511 (*this)[i] = other[i];
512 }
513 return *this;
514 }
515
Andreas Huber20dce082016-09-22 19:39:13 -0700516 private:
517 T *mBase;
518 };
519
520 template<typename T, size_t SIZE1>
521 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800522
523 using std_array_type = typename std_array<T, SIZE1>::type;
524
Andreas Huber20dce082016-09-22 19:39:13 -0700525 explicit accessor(T *base)
526 : mBase(base) {
527 }
528
529 T &operator[](size_t index) {
530 return mBase[index];
531 }
532
Yifan Hong44ab6232016-11-22 16:28:24 -0800533 accessor &operator=(const std_array_type &other) {
534 for (size_t i = 0; i < SIZE1; ++i) {
535 (*this)[i] = other[i];
536 }
537 return *this;
538 }
539
Andreas Huber20dce082016-09-22 19:39:13 -0700540 private:
541 T *mBase;
542 };
543
544 template<typename T, size_t SIZE1, size_t... SIZES>
545 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800546
547 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
548
Andreas Huber20dce082016-09-22 19:39:13 -0700549 explicit const_accessor(const T *base)
550 : mBase(base) {
551 }
552
553 const_accessor<T, SIZES...> operator[](size_t index) {
554 return const_accessor<T, SIZES...>(
555 &mBase[index * product<SIZES...>::value]);
556 }
557
Yifan Hong44ab6232016-11-22 16:28:24 -0800558 operator std_array_type() {
559 std_array_type array;
560 for (size_t i = 0; i < SIZE1; ++i) {
561 array[i] = (*this)[i];
562 }
563 return array;
564 }
565
Andreas Huber20dce082016-09-22 19:39:13 -0700566 private:
567 const T *mBase;
568 };
569
570 template<typename T, size_t SIZE1>
571 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800572
573 using std_array_type = typename std_array<T, SIZE1>::type;
574
Andreas Huber20dce082016-09-22 19:39:13 -0700575 explicit const_accessor(const T *base)
576 : mBase(base) {
577 }
578
579 const T &operator[](size_t index) const {
580 return mBase[index];
581 }
582
Yifan Hong44ab6232016-11-22 16:28:24 -0800583 operator std_array_type() {
584 std_array_type array;
585 for (size_t i = 0; i < SIZE1; ++i) {
586 array[i] = (*this)[i];
587 }
588 return array;
589 }
590
Andreas Huber20dce082016-09-22 19:39:13 -0700591 private:
592 const T *mBase;
593 };
594
595} // namespace details
596
597////////////////////////////////////////////////////////////////////////////////
598
Yifan Hong44ab6232016-11-22 16:28:24 -0800599// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700600template<typename T, size_t SIZE1, size_t... SIZES>
601struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800602
603 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
604
Andreas Huber20dce082016-09-22 19:39:13 -0700605 hidl_array() = default;
606
Yifan Hong44ab6232016-11-22 16:28:24 -0800607 // Copies the data from source, using T::operator=(const T &).
608 hidl_array(const T *source) {
609 for (size_t i = 0; i < elementCount(); ++i) {
610 mBuffer[i] = source[i];
611 }
612 }
613
614 // Copies the data from the given std::array, using T::operator=(const T &).
615 hidl_array(const std_array_type &array) {
616 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
617 modifier = array;
618 }
619
Andreas Huber20dce082016-09-22 19:39:13 -0700620 T *data() { return mBuffer; }
621 const T *data() const { return mBuffer; }
622
623 details::accessor<T, SIZES...> operator[](size_t index) {
624 return details::accessor<T, SIZES...>(
625 &mBuffer[index * details::product<SIZES...>::value]);
626 }
627
628 details::const_accessor<T, SIZES...> operator[](size_t index) const {
629 return details::const_accessor<T, SIZES...>(
630 &mBuffer[index * details::product<SIZES...>::value]);
631 }
632
Andreas Huber00a985c2016-09-28 14:24:53 -0700633 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
634
635 static constexpr size_tuple_type size() {
636 return std::make_tuple(SIZE1, SIZES...);
637 }
638
Yifan Hong44ab6232016-11-22 16:28:24 -0800639 static constexpr size_t elementCount() {
640 return details::product<SIZE1, SIZES...>::value;
641 }
642
643 operator std_array_type() const {
644 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
645 }
646
Andreas Huber20dce082016-09-22 19:39:13 -0700647private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800648 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700649};
650
Yifan Hong44ab6232016-11-22 16:28:24 -0800651// An 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>
653struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800654
655 using std_array_type = typename details::std_array<T, SIZE1>::type;
656
Andreas Huber20dce082016-09-22 19:39:13 -0700657 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800658
659 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800660 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800661 for (size_t i = 0; i < elementCount(); ++i) {
662 mBuffer[i] = source[i];
663 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800664 }
Andreas Huber20dce082016-09-22 19:39:13 -0700665
Yifan Hong44ab6232016-11-22 16:28:24 -0800666 // Copies the data from the given std::array, using T::operator=(const T &).
667 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
668
Andreas Huber20dce082016-09-22 19:39:13 -0700669 T *data() { return mBuffer; }
670 const T *data() const { return mBuffer; }
671
672 T &operator[](size_t index) {
673 return mBuffer[index];
674 }
675
676 const T &operator[](size_t index) const {
677 return mBuffer[index];
678 }
679
Andreas Huber00a985c2016-09-28 14:24:53 -0700680 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800681 static constexpr size_t elementCount() { return SIZE1; }
682
683 // Copies the data to an std::array, using T::operator=(T).
684 operator std_array_type() const {
685 std_array_type array;
686 for (size_t i = 0; i < SIZE1; ++i) {
687 array[i] = mBuffer[i];
688 }
689 return array;
690 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700691
Andreas Huber20dce082016-09-22 19:39:13 -0700692private:
693 T mBuffer[SIZE1];
694};
695
Martijn Coenen72110162016-08-19 14:28:25 +0200696// ----------------------------------------------------------------------
697// Version functions
698struct hidl_version {
699public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800700 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200701
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700702 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200703 return (mMajor == other.get_major() && mMinor == other.get_minor());
704 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200705
Martijn Coenen097a7672016-09-08 16:56:41 +0200706 constexpr uint16_t get_major() const { return mMajor; }
707 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200708
Martijn Coenen72110162016-08-19 14:28:25 +0200709private:
710 uint16_t mMajor;
711 uint16_t mMinor;
712};
713
714inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
715 return hidl_version(major,minor);
716}
717
Steven Morelandbdf26662016-09-02 11:03:15 -0700718#if defined(__LP64__)
719#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
720#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
721#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
722#else
723#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
724#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
725#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
726#endif
727
Steven Moreland40ede2c2016-11-09 13:51:53 -0800728#define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200729 static ::android::sp<I##INTERFACE> getService( \
Iliyan Malchevb42ce262016-09-26 00:09:35 -0700730 const std::string &serviceName, bool getStub=false); \
Steven Moreland40ede2c2016-11-09 13:51:53 -0800731 ::android::status_t registerAsService(const std::string &serviceName); \
732 static bool registerForNotifications( \
733 const std::string &serviceName, \
734 const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \
735 &notification); \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200736
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700737// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700738// Class that provides Hidl instrumentation utilities.
739struct HidlInstrumentor {
740 // Event that triggers the instrumentation. e.g. enter of an API call on
741 // the server/client side, exit of an API call on the server/client side
742 // etc.
743 enum InstrumentationEvent {
744 SERVER_API_ENTRY = 0,
745 SERVER_API_EXIT,
746 CLIENT_API_ENTRY,
747 CLIENT_API_EXIT,
748 SYNC_CALLBACK_ENTRY,
749 SYNC_CALLBACK_EXIT,
750 ASYNC_CALLBACK_ENTRY,
751 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700752 PASSTHROUGH_ENTRY,
753 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700754 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700755
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700756 // Signature of the instrumentation callback function.
757 using InstrumentationCallback = std::function<void(
758 const InstrumentationEvent event,
759 const char *package,
760 const char *version,
761 const char *interface,
762 const char *method,
763 std::vector<void *> *args)>;
764
765 explicit HidlInstrumentor(const std::string &prefix);
766 virtual ~HidlInstrumentor();
767
768 protected:
769 // Function that lookup and dynamically loads the hidl instrumentation
770 // libraries and registers the instrumentation callback functions.
771 //
772 // The instrumentation libraries should be stored under any of the following
773 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
774 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
775 // follow pattern: ^profilerPrefix(.*).profiler.so$
776 //
777 // Each instrumentation library is expected to implement the instrumentation
778 // function called HIDL_INSTRUMENTATION_FUNCTION.
779 //
780 // A no-op for user build.
781 void registerInstrumentationCallbacks(
782 const std::string &profilerPrefix,
783 std::vector<InstrumentationCallback> *instrumentationCallbacks);
784
785 // Utility function to determine whether a give file is a instrumentation
786 // library (i.e. the file name follow the expected pattern).
787 bool isInstrumentationLib(
788 const std::string &profilerPrefix,
789 const dirent *file);
790 // A list of registered instrumentation callbacks.
791 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
792 // Flag whether to enable instrumentation.
793 bool mEnableInstrumentation;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700794};
795
Martijn Coenen72110162016-08-19 14:28:25 +0200796} // namespace hardware
797} // namespace android
798
Martijn Coenenc28f1152016-08-22 14:06:56 +0200799
Martijn Coenen72110162016-08-19 14:28:25 +0200800#endif // ANDROID_HIDL_SUPPORT_H
801