blob: 1a4fd3ce52d891ae96e77e0f727678250c08c4e9 [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
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
91 // assingment operators
92 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 *);
Yifan Hong602b85a2016-10-24 13:40:01 -0700131 // copy from an std::string.
132 hidl_string(const std::string &);
133
134 // move constructor.
135 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200136
137 const char *c_str() const;
138 size_t size() const;
139 bool empty() const;
140
Yifan Hong602b85a2016-10-24 13:40:01 -0700141 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700142 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700143 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200144 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700145 // copy from an std::string.
146 hidl_string &operator=(const std::string &);
147 // move assignment operator.
148 hidl_string &operator=(hidl_string &&other);
149 // cast to std::string.
150 operator std::string() const;
151 // cast to C-style string. Caller is responsible
152 // to maintain this hidl_string alive.
153 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700154
Martijn Coenen72110162016-08-19 14:28:25 +0200155 void clear();
156
157 // Reference an external char array. Ownership is _not_ transferred.
158 // Caller is responsible for ensuring that underlying memory is valid
159 // for the lifetime of this hidl_string.
160 void setToExternal(const char *data, size_t size);
161
Andreas Huberebfeb362016-08-25 13:39:05 -0700162 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
163 static const size_t kOffsetOfBuffer;
164
Martijn Coenen72110162016-08-19 14:28:25 +0200165private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100166 details::hidl_pointer<const char> mBuffer;
167 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700168 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200169
Yifan Hong602b85a2016-10-24 13:40:01 -0700170 // copy from data with size. Assume that my memory is freed
171 // (through clear(), for example)
172 void copyFrom(const char *data, size_t size);
173 // move from another hidl_string
174 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200175};
176
Scott Randolphbb840f72016-11-21 14:39:26 -0800177inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
178 return strcmp(hs1.c_str(), hs2.c_str()) == 0;
179}
180
181inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
182 return !(hs1 == hs2);
183}
184
Yifan Hong5708fb42016-10-26 17:50:29 -0700185inline bool operator==(const hidl_string &hs, const char *s) {
186 return strcmp(hs.c_str(), s) == 0;
187}
188
189inline bool operator!=(const hidl_string &hs, const char *s) {
190 return !(hs == s);
191}
192
193inline bool operator==(const char *s, const hidl_string &hs) {
194 return strcmp(hs.c_str(), s) == 0;
195}
196
197inline bool operator!=(const char *s, const hidl_string &hs) {
198 return !(s == hs);
199}
200
Martijn Coenen30791002016-12-01 15:40:46 +0100201// hidl_memory is a structure that can be used to transfer
202// pieces of shared memory between processes. The assumption
203// of this object is that the memory remains accessible as
204// long as the file descriptors in the enclosed mHandle
205// - as well as all of its cross-process dups() - remain opened.
206struct hidl_memory {
207
208 hidl_memory() : mOwnsHandle(false), mHandle(nullptr), mSize(0), mName("") {
209 }
210
211 /**
212 * Creates a hidl_memory object and takes ownership of the handle.
213 */
214 hidl_memory(const hidl_string &name, const hidl_handle &handle, size_t size)
215 : mOwnsHandle(true),
216 mHandle(handle),
217 mSize(size),
218 mName(name)
219 {}
220
221 // copy constructor
222 hidl_memory(const hidl_memory& other) {
223 *this = other;
224 }
225
226 // copy assignment
227 hidl_memory &operator=(const hidl_memory &other) {
228 if (this != &other) {
229 mOwnsHandle = true;
230 mHandle = native_handle_clone(other.mHandle);
231 mSize = other.mSize;
232 mName = other.mName;
233 }
234
235 return *this;
236 }
237
238 // TODO move constructor/move assignment
239
240 ~hidl_memory() {
241 // TODO if we had previously mapped from this object, unmap
242 if (mOwnsHandle) {
243 native_handle_close(mHandle);
244 }
245 }
246
247 const native_handle_t* handle() const {
248 return mHandle;
249 }
250
251 const hidl_string &name() const {
252 return mName;
253 }
254
255 size_t size() const {
256 return mSize;
257 }
258
259 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
260 static const size_t kOffsetOfHandle;
261 // offsetof(hidl_memory, mName) exposed since mHandle is private.
262 static const size_t kOffsetOfName;
263private:
264 bool mOwnsHandle;
265 hidl_handle mHandle;
266 size_t mSize;
267 hidl_string mName;
268};
269
Andreas Huber20dce082016-09-22 19:39:13 -0700270////////////////////////////////////////////////////////////////////////////////
271
Martijn Coenen72110162016-08-19 14:28:25 +0200272template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100273struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200274 hidl_vec()
275 : mBuffer(NULL),
276 mSize(0),
277 mOwnsBuffer(true) {
278 }
279
Yifan Hong602b85a2016-10-24 13:40:01 -0700280 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200281 *this = other;
282 }
283
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100284 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800285 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100286 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700287 }
288
Steven Morelandb69926a2016-11-15 10:02:57 -0800289 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800290 : mOwnsBuffer(true) {
291 if (list.size() > UINT32_MAX) {
292 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
293 }
294 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800295 mBuffer = new T[mSize];
296
Steven Morelandb69926a2016-11-15 10:02:57 -0800297 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800298 for (auto it = list.begin(); it != list.end(); ++it) {
299 mBuffer[idx++] = *it;
300 }
301 }
302
Yifan Hong602b85a2016-10-24 13:40:01 -0700303 hidl_vec(const std::vector<T> &other) : hidl_vec() {
304 *this = other;
305 }
306
Martijn Coenen72110162016-08-19 14:28:25 +0200307 ~hidl_vec() {
308 if (mOwnsBuffer) {
309 delete[] mBuffer;
310 }
311 mBuffer = NULL;
312 }
313
Alexey Polyudove2299012016-10-19 09:52:00 -0700314 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200315 // caller's responsibility to ensure that the underlying memory stays
316 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700317 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200318 if (mOwnsBuffer) {
319 delete [] mBuffer;
320 }
321 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100322 if (size > UINT32_MAX) {
323 logAlwaysFatal("external vector size exceeds 2^32 elements.");
324 }
325 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700326 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200327 }
328
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700329 T *data() {
330 return mBuffer;
331 }
332
333 const T *data() const {
334 return mBuffer;
335 }
336
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700337 T *releaseData() {
338 if (!mOwnsBuffer && mSize > 0) {
339 resize(mSize);
340 }
341 mOwnsBuffer = false;
342 return mBuffer;
343 }
344
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700345 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100346 if (mOwnsBuffer) {
347 delete[] mBuffer;
348 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700349 mBuffer = other.mBuffer;
350 mSize = other.mSize;
351 mOwnsBuffer = other.mOwnsBuffer;
352 other.mOwnsBuffer = false;
353 return *this;
354 }
355
Martijn Coenen72110162016-08-19 14:28:25 +0200356 hidl_vec &operator=(const hidl_vec &other) {
357 if (this != &other) {
358 if (mOwnsBuffer) {
359 delete[] mBuffer;
360 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700361 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200362 }
363
364 return *this;
365 }
366
Yifan Hong602b85a2016-10-24 13:40:01 -0700367 // copy from an std::vector.
368 hidl_vec &operator=(const std::vector<T> &other) {
369 if (mOwnsBuffer) {
370 delete[] mBuffer;
371 }
372 copyFrom(other, other.size());
373 return *this;
374 }
375
376 // cast to an std::vector.
377 operator std::vector<T>() const {
378 std::vector<T> v(mSize);
379 for (size_t i = 0; i < mSize; ++i) {
380 v[i] = mBuffer[i];
381 }
382 return v;
383 }
384
Martijn Coenen72110162016-08-19 14:28:25 +0200385 size_t size() const {
386 return mSize;
387 }
388
389 T &operator[](size_t index) {
390 return mBuffer[index];
391 }
392
393 const T &operator[](size_t index) const {
394 return mBuffer[index];
395 }
396
397 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100398 if (size > UINT32_MAX) {
399 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
400 }
Martijn Coenen72110162016-08-19 14:28:25 +0200401 T *newBuffer = new T[size];
402
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100403 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200404 newBuffer[i] = mBuffer[i];
405 }
406
407 if (mOwnsBuffer) {
408 delete[] mBuffer;
409 }
410 mBuffer = newBuffer;
411
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100412 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200413 mOwnsBuffer = true;
414 }
415
Yifan Hong089ae132016-11-11 11:32:52 -0800416 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
417 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800418
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800419private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800420 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800421 template<bool is_const>
422 class iter : public std::iterator<
423 std::random_access_iterator_tag, /* Category */
424 T,
425 ptrdiff_t, /* Distance */
426 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
427 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800428 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800429 using traits = std::iterator_traits<iter>;
430 using ptr_type = typename traits::pointer;
431 using ref_type = typename traits::reference;
432 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800433 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800434 iter(ptr_type ptr) : mPtr(ptr) { }
435 inline iter &operator++() { mPtr++; return *this; }
436 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
437 inline iter &operator--() { mPtr--; return *this; }
438 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
439 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
440 inline iter operator+(diff_type n) const { return mPtr + n; }
441 inline iter operator-(diff_type n) const { return mPtr - n; }
442 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
443 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
444 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
445 inline ref_type operator*() const { return *mPtr; }
446 inline ptr_type operator->() const { return mPtr; }
447 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
448 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
449 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
450 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
451 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
452 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
453 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800454 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800455 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800456 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800457public:
458 using iterator = iter<false /* is_const */>;
459 using const_iterator = iter<true /* is_const */>;
460
Scott Randolphbb840f72016-11-21 14:39:26 -0800461 iterator begin() { return data(); }
462 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800463 const_iterator begin() const { return data(); }
464 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800465
Martijn Coenen72110162016-08-19 14:28:25 +0200466private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100467 details::hidl_pointer<T> mBuffer;
468 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200469 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700470
471 // copy from an array-like object, assuming my resources are freed.
472 template <typename Array>
473 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800474 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700475 mOwnsBuffer = true;
476 if (mSize > 0) {
477 mBuffer = new T[size];
478 for (size_t i = 0; i < size; ++i) {
479 mBuffer[i] = data[i];
480 }
481 } else {
482 mBuffer = NULL;
483 }
484 }
Martijn Coenen72110162016-08-19 14:28:25 +0200485};
486
Yifan Hong089ae132016-11-11 11:32:52 -0800487template <typename T>
488const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
489
Andreas Huber20dce082016-09-22 19:39:13 -0700490////////////////////////////////////////////////////////////////////////////////
491
492namespace details {
493
494 template<size_t SIZE1, size_t... SIZES>
495 struct product {
496 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
497 };
498
499 template<size_t SIZE1>
500 struct product<SIZE1> {
501 static constexpr size_t value = SIZE1;
502 };
503
504 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800505 struct std_array {
506 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
507 };
508
509 template<typename T, size_t SIZE1>
510 struct std_array<T, SIZE1> {
511 using type = std::array<T, SIZE1>;
512 };
513
514 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700515 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800516
517 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
518
Andreas Huber20dce082016-09-22 19:39:13 -0700519 explicit accessor(T *base)
520 : mBase(base) {
521 }
522
523 accessor<T, SIZES...> operator[](size_t index) {
524 return accessor<T, SIZES...>(
525 &mBase[index * product<SIZES...>::value]);
526 }
527
Yifan Hong44ab6232016-11-22 16:28:24 -0800528 accessor &operator=(const std_array_type &other) {
529 for (size_t i = 0; i < SIZE1; ++i) {
530 (*this)[i] = other[i];
531 }
532 return *this;
533 }
534
Andreas Huber20dce082016-09-22 19:39:13 -0700535 private:
536 T *mBase;
537 };
538
539 template<typename T, size_t SIZE1>
540 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800541
542 using std_array_type = typename std_array<T, SIZE1>::type;
543
Andreas Huber20dce082016-09-22 19:39:13 -0700544 explicit accessor(T *base)
545 : mBase(base) {
546 }
547
548 T &operator[](size_t index) {
549 return mBase[index];
550 }
551
Yifan Hong44ab6232016-11-22 16:28:24 -0800552 accessor &operator=(const std_array_type &other) {
553 for (size_t i = 0; i < SIZE1; ++i) {
554 (*this)[i] = other[i];
555 }
556 return *this;
557 }
558
Andreas Huber20dce082016-09-22 19:39:13 -0700559 private:
560 T *mBase;
561 };
562
563 template<typename T, size_t SIZE1, size_t... SIZES>
564 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800565
566 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
567
Andreas Huber20dce082016-09-22 19:39:13 -0700568 explicit const_accessor(const T *base)
569 : mBase(base) {
570 }
571
572 const_accessor<T, SIZES...> operator[](size_t index) {
573 return const_accessor<T, SIZES...>(
574 &mBase[index * product<SIZES...>::value]);
575 }
576
Yifan Hong44ab6232016-11-22 16:28:24 -0800577 operator std_array_type() {
578 std_array_type array;
579 for (size_t i = 0; i < SIZE1; ++i) {
580 array[i] = (*this)[i];
581 }
582 return array;
583 }
584
Andreas Huber20dce082016-09-22 19:39:13 -0700585 private:
586 const T *mBase;
587 };
588
589 template<typename T, size_t SIZE1>
590 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800591
592 using std_array_type = typename std_array<T, SIZE1>::type;
593
Andreas Huber20dce082016-09-22 19:39:13 -0700594 explicit const_accessor(const T *base)
595 : mBase(base) {
596 }
597
598 const T &operator[](size_t index) const {
599 return mBase[index];
600 }
601
Yifan Hong44ab6232016-11-22 16:28:24 -0800602 operator std_array_type() {
603 std_array_type array;
604 for (size_t i = 0; i < SIZE1; ++i) {
605 array[i] = (*this)[i];
606 }
607 return array;
608 }
609
Andreas Huber20dce082016-09-22 19:39:13 -0700610 private:
611 const T *mBase;
612 };
613
614} // namespace details
615
616////////////////////////////////////////////////////////////////////////////////
617
Yifan Hong44ab6232016-11-22 16:28:24 -0800618// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700619template<typename T, size_t SIZE1, size_t... SIZES>
620struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800621
622 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
623
Andreas Huber20dce082016-09-22 19:39:13 -0700624 hidl_array() = default;
625
Yifan Hong44ab6232016-11-22 16:28:24 -0800626 // Copies the data from source, using T::operator=(const T &).
627 hidl_array(const T *source) {
628 for (size_t i = 0; i < elementCount(); ++i) {
629 mBuffer[i] = source[i];
630 }
631 }
632
633 // Copies the data from the given std::array, using T::operator=(const T &).
634 hidl_array(const std_array_type &array) {
635 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
636 modifier = array;
637 }
638
Andreas Huber20dce082016-09-22 19:39:13 -0700639 T *data() { return mBuffer; }
640 const T *data() const { return mBuffer; }
641
642 details::accessor<T, SIZES...> operator[](size_t index) {
643 return details::accessor<T, SIZES...>(
644 &mBuffer[index * details::product<SIZES...>::value]);
645 }
646
647 details::const_accessor<T, SIZES...> operator[](size_t index) const {
648 return details::const_accessor<T, SIZES...>(
649 &mBuffer[index * details::product<SIZES...>::value]);
650 }
651
Andreas Huber00a985c2016-09-28 14:24:53 -0700652 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
653
654 static constexpr size_tuple_type size() {
655 return std::make_tuple(SIZE1, SIZES...);
656 }
657
Yifan Hong44ab6232016-11-22 16:28:24 -0800658 static constexpr size_t elementCount() {
659 return details::product<SIZE1, SIZES...>::value;
660 }
661
662 operator std_array_type() const {
663 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
664 }
665
Andreas Huber20dce082016-09-22 19:39:13 -0700666private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800667 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700668};
669
Yifan Hong44ab6232016-11-22 16:28:24 -0800670// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700671template<typename T, size_t SIZE1>
672struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800673
674 using std_array_type = typename details::std_array<T, SIZE1>::type;
675
Andreas Huber20dce082016-09-22 19:39:13 -0700676 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800677
678 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800679 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800680 for (size_t i = 0; i < elementCount(); ++i) {
681 mBuffer[i] = source[i];
682 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800683 }
Andreas Huber20dce082016-09-22 19:39:13 -0700684
Yifan Hong44ab6232016-11-22 16:28:24 -0800685 // Copies the data from the given std::array, using T::operator=(const T &).
686 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
687
Andreas Huber20dce082016-09-22 19:39:13 -0700688 T *data() { return mBuffer; }
689 const T *data() const { return mBuffer; }
690
691 T &operator[](size_t index) {
692 return mBuffer[index];
693 }
694
695 const T &operator[](size_t index) const {
696 return mBuffer[index];
697 }
698
Andreas Huber00a985c2016-09-28 14:24:53 -0700699 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800700 static constexpr size_t elementCount() { return SIZE1; }
701
702 // Copies the data to an std::array, using T::operator=(T).
703 operator std_array_type() const {
704 std_array_type array;
705 for (size_t i = 0; i < SIZE1; ++i) {
706 array[i] = mBuffer[i];
707 }
708 return array;
709 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700710
Andreas Huber20dce082016-09-22 19:39:13 -0700711private:
712 T mBuffer[SIZE1];
713};
714
Martijn Coenen72110162016-08-19 14:28:25 +0200715// ----------------------------------------------------------------------
716// Version functions
717struct hidl_version {
718public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800719 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200720
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700721 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200722 return (mMajor == other.get_major() && mMinor == other.get_minor());
723 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200724
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800725 bool operator<(const hidl_version& other) const {
726 return (mMajor < other.get_major() ||
727 (mMajor == other.get_major() && mMinor < other.get_minor()));
728 }
729
730 bool operator>(const hidl_version& other) const {
731 return other < *this;
732 }
733
734 bool operator<=(const hidl_version& other) const {
735 return !(*this > other);
736 }
737
738 bool operator>=(const hidl_version& other) const {
739 return !(*this < other);
740 }
741
Martijn Coenen097a7672016-09-08 16:56:41 +0200742 constexpr uint16_t get_major() const { return mMajor; }
743 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200744
Martijn Coenen72110162016-08-19 14:28:25 +0200745private:
746 uint16_t mMajor;
747 uint16_t mMinor;
748};
749
750inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
751 return hidl_version(major,minor);
752}
753
Steven Morelandbdf26662016-09-02 11:03:15 -0700754#if defined(__LP64__)
755#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
756#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
757#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
758#else
759#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
760#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
761#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
762#endif
763
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700764// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700765// Class that provides Hidl instrumentation utilities.
766struct HidlInstrumentor {
767 // Event that triggers the instrumentation. e.g. enter of an API call on
768 // the server/client side, exit of an API call on the server/client side
769 // etc.
770 enum InstrumentationEvent {
771 SERVER_API_ENTRY = 0,
772 SERVER_API_EXIT,
773 CLIENT_API_ENTRY,
774 CLIENT_API_EXIT,
775 SYNC_CALLBACK_ENTRY,
776 SYNC_CALLBACK_EXIT,
777 ASYNC_CALLBACK_ENTRY,
778 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700779 PASSTHROUGH_ENTRY,
780 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700781 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700782
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700783 // Signature of the instrumentation callback function.
784 using InstrumentationCallback = std::function<void(
785 const InstrumentationEvent event,
786 const char *package,
787 const char *version,
788 const char *interface,
789 const char *method,
790 std::vector<void *> *args)>;
791
792 explicit HidlInstrumentor(const std::string &prefix);
793 virtual ~HidlInstrumentor();
794
795 protected:
796 // Function that lookup and dynamically loads the hidl instrumentation
797 // libraries and registers the instrumentation callback functions.
798 //
799 // The instrumentation libraries should be stored under any of the following
800 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
801 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
802 // follow pattern: ^profilerPrefix(.*).profiler.so$
803 //
804 // Each instrumentation library is expected to implement the instrumentation
805 // function called HIDL_INSTRUMENTATION_FUNCTION.
806 //
807 // A no-op for user build.
808 void registerInstrumentationCallbacks(
809 const std::string &profilerPrefix,
810 std::vector<InstrumentationCallback> *instrumentationCallbacks);
811
812 // Utility function to determine whether a give file is a instrumentation
813 // library (i.e. the file name follow the expected pattern).
814 bool isInstrumentationLib(
815 const std::string &profilerPrefix,
816 const dirent *file);
817 // A list of registered instrumentation callbacks.
818 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
819 // Flag whether to enable instrumentation.
820 bool mEnableInstrumentation;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700821};
822
Martijn Coenen72110162016-08-19 14:28:25 +0200823} // namespace hardware
824} // namespace android
825
Martijn Coenenc28f1152016-08-22 14:06:56 +0200826
Martijn Coenen72110162016-08-19 14:28:25 +0200827#endif // ANDROID_HIDL_SUPPORT_H