blob: f4a216a795380fe8ff48786401f8e70847cd6d95 [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>
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070021#include <dirent.h>
Steven Morelandbdf26662016-09-02 11:03:15 -070022#include <dlfcn.h>
Scott Randolphbb840f72016-11-21 14:39:26 -080023#include <iterator>
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010024#include <cutils/native_handle.h>
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -070025#include <cutils/properties.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080026#include <functional>
Yifan Honga3c31842016-10-21 10:33:14 -070027#include <hidl/Status.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080028#include <map>
Andreas Huber00a985c2016-09-28 14:24:53 -070029#include <tuple>
Iliyan Malchev692070a2016-09-12 16:30:44 -070030#include <utils/Errors.h>
31#include <utils/RefBase.h>
32#include <utils/StrongPointer.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080033#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020034
35namespace android {
36namespace hardware {
37
Martijn Coenen4ca39a02016-11-11 15:58:51 +010038namespace details {
39
40// hidl_log_base is a base class that templatized
41// classes implemented in a header can inherit from,
42// to avoid creating dependencies on liblog.
43struct hidl_log_base {
44 void logAlwaysFatal(const char *message);
45};
46
47// HIDL client/server code should *NOT* use this class.
48//
49// hidl_pointer wraps a pointer without taking ownership,
50// and stores it in a union with a uint64_t. This ensures
51// that we always have enough space to store a pointer,
52// regardless of whether we're running in a 32-bit or 64-bit
53// process.
54template<typename T>
55struct hidl_pointer {
56 hidl_pointer()
57 : mPointer(nullptr) {
58 }
59 hidl_pointer(T* ptr)
60 : mPointer(ptr) {
61 }
62 hidl_pointer(const hidl_pointer<T>& other) {
63 mPointer = other.mPointer;
64 }
65 hidl_pointer(hidl_pointer<T>&& other) {
66 *this = std::move(other);
67 }
68
69 hidl_pointer &operator=(const hidl_pointer<T>& other) {
70 mPointer = other.mPointer;
71 return *this;
72 }
73 hidl_pointer &operator=(hidl_pointer<T>&& other) {
74 mPointer = other.mPointer;
75 other.mPointer = nullptr;
76 return *this;
77 }
78 hidl_pointer &operator=(T* ptr) {
79 mPointer = ptr;
80 return *this;
81 }
82
83 operator T*() const {
84 return mPointer;
85 }
86 explicit operator void*() const { // requires explicit cast to avoid ambiguity
87 return mPointer;
88 }
89 T& operator*() const {
90 return *mPointer;
91 }
92 T* operator->() const {
93 return mPointer;
94 }
95 T &operator[](size_t index) {
96 return mPointer[index];
97 }
98 const T &operator[](size_t index) const {
99 return mPointer[index];
100 }
101private:
102 union {
103 T* mPointer;
104 uint64_t _pad;
105 };
106};
107} // namespace details
108
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100109
110// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
111// so that it can safely be transferred between 32-bit and 64-bit processes.
112struct hidl_handle {
113 hidl_handle() {
114 mHandle = nullptr;
115 }
116 ~hidl_handle() {
117 }
118
119 // copy constructors.
120 hidl_handle(const native_handle_t *handle) {
121 mHandle = handle;
122 }
123
124 hidl_handle(const hidl_handle &other) {
125 mHandle = other.mHandle;
126 }
127
128 // move constructor.
129 hidl_handle(hidl_handle &&other) {
130 *this = std::move(other);
131 }
132
133 // assingment operators
134 hidl_handle &operator=(const hidl_handle &other) {
135 mHandle = other.mHandle;
136 return *this;
137 }
138
139 hidl_handle &operator=(const native_handle_t *native_handle) {
140 mHandle = native_handle;
141 return *this;
142 }
143
144 hidl_handle &operator=(hidl_handle &&other) {
145 mHandle = other.mHandle;
146 other.mHandle = nullptr;
147 return *this;
148 }
149
150 const native_handle_t* operator->() const {
151 return mHandle;
152 }
153 // implicit conversion to const native_handle_t*
154 operator const native_handle_t *() const {
155 return mHandle;
156 }
157 // explicit conversion
158 const native_handle_t *getNativeHandle() const {
159 return mHandle;
160 }
161private:
162 details::hidl_pointer<const native_handle_t> mHandle;
163};
164
Martijn Coenen72110162016-08-19 14:28:25 +0200165struct hidl_string {
166 hidl_string();
167 ~hidl_string();
168
Yifan Hong602b85a2016-10-24 13:40:01 -0700169 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200170 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700171 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -0700172 hidl_string(const char *);
Yifan Hong602b85a2016-10-24 13:40:01 -0700173 // copy from an std::string.
174 hidl_string(const std::string &);
175
176 // move constructor.
177 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200178
179 const char *c_str() const;
180 size_t size() const;
181 bool empty() const;
182
Yifan Hong602b85a2016-10-24 13:40:01 -0700183 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700184 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700185 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200186 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700187 // copy from an std::string.
188 hidl_string &operator=(const std::string &);
189 // move assignment operator.
190 hidl_string &operator=(hidl_string &&other);
191 // cast to std::string.
192 operator std::string() const;
193 // cast to C-style string. Caller is responsible
194 // to maintain this hidl_string alive.
195 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700196
Martijn Coenen72110162016-08-19 14:28:25 +0200197 void clear();
198
199 // Reference an external char array. Ownership is _not_ transferred.
200 // Caller is responsible for ensuring that underlying memory is valid
201 // for the lifetime of this hidl_string.
202 void setToExternal(const char *data, size_t size);
203
Andreas Huberebfeb362016-08-25 13:39:05 -0700204 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
205 static const size_t kOffsetOfBuffer;
206
Martijn Coenen72110162016-08-19 14:28:25 +0200207private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100208 details::hidl_pointer<const char> mBuffer;
209 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700210 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200211
Yifan Hong602b85a2016-10-24 13:40:01 -0700212 // copy from data with size. Assume that my memory is freed
213 // (through clear(), for example)
214 void copyFrom(const char *data, size_t size);
215 // move from another hidl_string
216 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200217};
218
Scott Randolphbb840f72016-11-21 14:39:26 -0800219inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
220 return strcmp(hs1.c_str(), hs2.c_str()) == 0;
221}
222
223inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
224 return !(hs1 == hs2);
225}
226
Yifan Hong5708fb42016-10-26 17:50:29 -0700227inline bool operator==(const hidl_string &hs, const char *s) {
228 return strcmp(hs.c_str(), s) == 0;
229}
230
231inline bool operator!=(const hidl_string &hs, const char *s) {
232 return !(hs == s);
233}
234
235inline bool operator==(const char *s, const hidl_string &hs) {
236 return strcmp(hs.c_str(), s) == 0;
237}
238
239inline bool operator!=(const char *s, const hidl_string &hs) {
240 return !(s == hs);
241}
242
Andreas Huber20dce082016-09-22 19:39:13 -0700243////////////////////////////////////////////////////////////////////////////////
244
Martijn Coenen72110162016-08-19 14:28:25 +0200245template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100246struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200247 hidl_vec()
248 : mBuffer(NULL),
249 mSize(0),
250 mOwnsBuffer(true) {
251 }
252
Yifan Hong602b85a2016-10-24 13:40:01 -0700253 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200254 *this = other;
255 }
256
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100257 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800258 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100259 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700260 }
261
Steven Morelandb69926a2016-11-15 10:02:57 -0800262 hidl_vec(const std::initializer_list<T> list)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800263 : mSize(list.size()),
264 mOwnsBuffer(true) {
265 mBuffer = new T[mSize];
266
Steven Morelandb69926a2016-11-15 10:02:57 -0800267 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800268 for (auto it = list.begin(); it != list.end(); ++it) {
269 mBuffer[idx++] = *it;
270 }
271 }
272
Yifan Hong602b85a2016-10-24 13:40:01 -0700273 hidl_vec(const std::vector<T> &other) : hidl_vec() {
274 *this = other;
275 }
276
Martijn Coenen72110162016-08-19 14:28:25 +0200277 ~hidl_vec() {
278 if (mOwnsBuffer) {
279 delete[] mBuffer;
280 }
281 mBuffer = NULL;
282 }
283
Alexey Polyudove2299012016-10-19 09:52:00 -0700284 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200285 // caller's responsibility to ensure that the underlying memory stays
286 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700287 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200288 if (mOwnsBuffer) {
289 delete [] mBuffer;
290 }
291 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100292 if (size > UINT32_MAX) {
293 logAlwaysFatal("external vector size exceeds 2^32 elements.");
294 }
295 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700296 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200297 }
298
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700299 T *data() {
300 return mBuffer;
301 }
302
303 const T *data() const {
304 return mBuffer;
305 }
306
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700307 T *releaseData() {
308 if (!mOwnsBuffer && mSize > 0) {
309 resize(mSize);
310 }
311 mOwnsBuffer = false;
312 return mBuffer;
313 }
314
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700315 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100316 if (mOwnsBuffer) {
317 delete[] mBuffer;
318 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700319 mBuffer = other.mBuffer;
320 mSize = other.mSize;
321 mOwnsBuffer = other.mOwnsBuffer;
322 other.mOwnsBuffer = false;
323 return *this;
324 }
325
Martijn Coenen72110162016-08-19 14:28:25 +0200326 hidl_vec &operator=(const hidl_vec &other) {
327 if (this != &other) {
328 if (mOwnsBuffer) {
329 delete[] mBuffer;
330 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700331 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200332 }
333
334 return *this;
335 }
336
Yifan Hong602b85a2016-10-24 13:40:01 -0700337 // copy from an std::vector.
338 hidl_vec &operator=(const std::vector<T> &other) {
339 if (mOwnsBuffer) {
340 delete[] mBuffer;
341 }
342 copyFrom(other, other.size());
343 return *this;
344 }
345
346 // cast to an std::vector.
347 operator std::vector<T>() const {
348 std::vector<T> v(mSize);
349 for (size_t i = 0; i < mSize; ++i) {
350 v[i] = mBuffer[i];
351 }
352 return v;
353 }
354
Martijn Coenen72110162016-08-19 14:28:25 +0200355 size_t size() const {
356 return mSize;
357 }
358
359 T &operator[](size_t index) {
360 return mBuffer[index];
361 }
362
363 const T &operator[](size_t index) const {
364 return mBuffer[index];
365 }
366
367 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100368 if (size > UINT32_MAX) {
369 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
370 }
Martijn Coenen72110162016-08-19 14:28:25 +0200371 T *newBuffer = new T[size];
372
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100373 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200374 newBuffer[i] = mBuffer[i];
375 }
376
377 if (mOwnsBuffer) {
378 delete[] mBuffer;
379 }
380 mBuffer = newBuffer;
381
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100382 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200383 mOwnsBuffer = true;
384 }
385
Yifan Hong089ae132016-11-11 11:32:52 -0800386 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
387 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800388
389 // Define std interator interface for walking the array contents
390 // TODO: it might be nice to implement a full featured random access iterator...
391 class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
392 {
393 public:
394 iterator(T* ptr) : mPtr(ptr) { }
395 iterator operator++() { iterator i = *this; mPtr++; return i; }
396 iterator operator++(int) { mPtr++; return *this; }
397 iterator operator--() { iterator i = *this; mPtr--; return i; }
398 iterator operator--(int) { mPtr--; return *this; }
399 T& operator*() { return *mPtr; }
400 T* operator->() { return mPtr; }
401 bool operator==(const iterator& rhs) const { return mPtr == rhs.mPtr; }
402 bool operator!=(const iterator& rhs) const { return mPtr != rhs.mPtr; }
403 private:
404 T* mPtr;
405 };
406 iterator begin() { return data(); }
407 iterator end() { return data()+mSize; }
408
Martijn Coenen72110162016-08-19 14:28:25 +0200409private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100410 details::hidl_pointer<T> mBuffer;
411 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200412 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700413
414 // copy from an array-like object, assuming my resources are freed.
415 template <typename Array>
416 void copyFrom(const Array &data, size_t size) {
417 mSize = size;
418 mOwnsBuffer = true;
419 if (mSize > 0) {
420 mBuffer = new T[size];
421 for (size_t i = 0; i < size; ++i) {
422 mBuffer[i] = data[i];
423 }
424 } else {
425 mBuffer = NULL;
426 }
427 }
Martijn Coenen72110162016-08-19 14:28:25 +0200428};
429
Yifan Hong089ae132016-11-11 11:32:52 -0800430template <typename T>
431const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
432
Andreas Huber20dce082016-09-22 19:39:13 -0700433////////////////////////////////////////////////////////////////////////////////
434
435namespace details {
436
437 template<size_t SIZE1, size_t... SIZES>
438 struct product {
439 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
440 };
441
442 template<size_t SIZE1>
443 struct product<SIZE1> {
444 static constexpr size_t value = SIZE1;
445 };
446
447 template<typename T, size_t SIZE1, size_t... SIZES>
448 struct accessor {
449 explicit accessor(T *base)
450 : mBase(base) {
451 }
452
453 accessor<T, SIZES...> operator[](size_t index) {
454 return accessor<T, SIZES...>(
455 &mBase[index * product<SIZES...>::value]);
456 }
457
458 private:
459 T *mBase;
460 };
461
462 template<typename T, size_t SIZE1>
463 struct accessor<T, SIZE1> {
464 explicit accessor(T *base)
465 : mBase(base) {
466 }
467
468 T &operator[](size_t index) {
469 return mBase[index];
470 }
471
472 private:
473 T *mBase;
474 };
475
476 template<typename T, size_t SIZE1, size_t... SIZES>
477 struct const_accessor {
478 explicit const_accessor(const T *base)
479 : mBase(base) {
480 }
481
482 const_accessor<T, SIZES...> operator[](size_t index) {
483 return const_accessor<T, SIZES...>(
484 &mBase[index * product<SIZES...>::value]);
485 }
486
487 private:
488 const T *mBase;
489 };
490
491 template<typename T, size_t SIZE1>
492 struct const_accessor<T, SIZE1> {
493 explicit const_accessor(const T *base)
494 : mBase(base) {
495 }
496
497 const T &operator[](size_t index) const {
498 return mBase[index];
499 }
500
501 private:
502 const T *mBase;
503 };
504
505} // namespace details
506
507////////////////////////////////////////////////////////////////////////////////
508
509template<typename T, size_t SIZE1, size_t... SIZES>
510struct hidl_array {
511 hidl_array() = default;
512
513 T *data() { return mBuffer; }
514 const T *data() const { return mBuffer; }
515
516 details::accessor<T, SIZES...> operator[](size_t index) {
517 return details::accessor<T, SIZES...>(
518 &mBuffer[index * details::product<SIZES...>::value]);
519 }
520
521 details::const_accessor<T, SIZES...> operator[](size_t index) const {
522 return details::const_accessor<T, SIZES...>(
523 &mBuffer[index * details::product<SIZES...>::value]);
524 }
525
Andreas Huber00a985c2016-09-28 14:24:53 -0700526 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
527
528 static constexpr size_tuple_type size() {
529 return std::make_tuple(SIZE1, SIZES...);
530 }
531
Andreas Huber20dce082016-09-22 19:39:13 -0700532private:
533 T mBuffer[details::product<SIZE1, SIZES...>::value];
534};
535
536template<typename T, size_t SIZE1>
537struct hidl_array<T, SIZE1> {
538 hidl_array() = default;
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800539 hidl_array(const T *source) {
540 memcpy(mBuffer, source, SIZE1 * sizeof(T));
541 }
Andreas Huber20dce082016-09-22 19:39:13 -0700542
543 T *data() { return mBuffer; }
544 const T *data() const { return mBuffer; }
545
546 T &operator[](size_t index) {
547 return mBuffer[index];
548 }
549
550 const T &operator[](size_t index) const {
551 return mBuffer[index];
552 }
553
Andreas Huber00a985c2016-09-28 14:24:53 -0700554 static constexpr size_t size() { return SIZE1; }
555
Andreas Huber20dce082016-09-22 19:39:13 -0700556private:
557 T mBuffer[SIZE1];
558};
559
Martijn Coenen72110162016-08-19 14:28:25 +0200560// ----------------------------------------------------------------------
561// Version functions
562struct hidl_version {
563public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800564 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200565
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700566 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200567 return (mMajor == other.get_major() && mMinor == other.get_minor());
568 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200569
Martijn Coenen097a7672016-09-08 16:56:41 +0200570 constexpr uint16_t get_major() const { return mMajor; }
571 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200572
Martijn Coenen72110162016-08-19 14:28:25 +0200573private:
574 uint16_t mMajor;
575 uint16_t mMinor;
576};
577
578inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
579 return hidl_version(major,minor);
580}
581
Yifan Hongc1a60ef2016-11-08 18:53:16 -0800582struct IBase : virtual public RefBase {
Yifan Honga3c31842016-10-21 10:33:14 -0700583 virtual bool isRemote() const = 0;
584 // HIDL reserved methods follow.
585 virtual ::android::hardware::Return<void> interfaceChain(
586 std::function<void(const hidl_vec<hidl_string>&)> _hidl_cb) = 0;
Martijn Coenen70822ea2016-11-16 15:47:09 +0100587 // This method notifies the interface that one or more system properties have changed.
588 // The default implementation calls report_sysprop_change() in libcutils, which in turn
589 // calls a set of registered callbacks (eg to update trace tags).
590 virtual ::android::hardware::Return<void> notifySyspropsChanged() = 0;
Yifan Honga3c31842016-10-21 10:33:14 -0700591 // descriptor for HIDL reserved methods.
Steven Moreland5a682322016-11-11 12:32:50 -0800592 static const char* descriptor;
Yifan Honga3c31842016-10-21 10:33:14 -0700593};
594
Steven Morelandbdf26662016-09-02 11:03:15 -0700595#if defined(__LP64__)
596#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
597#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
598#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
599#else
600#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
601#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
602#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
603#endif
604
Steven Moreland40ede2c2016-11-09 13:51:53 -0800605#define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200606 static ::android::sp<I##INTERFACE> getService( \
Iliyan Malchevb42ce262016-09-26 00:09:35 -0700607 const std::string &serviceName, bool getStub=false); \
Steven Moreland40ede2c2016-11-09 13:51:53 -0800608 ::android::status_t registerAsService(const std::string &serviceName); \
609 static bool registerForNotifications( \
610 const std::string &serviceName, \
611 const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \
612 &notification); \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200613
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700614// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700615// Class that provides Hidl instrumentation utilities.
616struct HidlInstrumentor {
617 // Event that triggers the instrumentation. e.g. enter of an API call on
618 // the server/client side, exit of an API call on the server/client side
619 // etc.
620 enum InstrumentationEvent {
621 SERVER_API_ENTRY = 0,
622 SERVER_API_EXIT,
623 CLIENT_API_ENTRY,
624 CLIENT_API_EXIT,
625 SYNC_CALLBACK_ENTRY,
626 SYNC_CALLBACK_EXIT,
627 ASYNC_CALLBACK_ENTRY,
628 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700629 PASSTHROUGH_ENTRY,
630 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700631 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700632
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700633 // Signature of the instrumentation callback function.
634 using InstrumentationCallback = std::function<void(
635 const InstrumentationEvent event,
636 const char *package,
637 const char *version,
638 const char *interface,
639 const char *method,
640 std::vector<void *> *args)>;
641
642 explicit HidlInstrumentor(const std::string &prefix);
643 virtual ~HidlInstrumentor();
644
645 protected:
646 // Function that lookup and dynamically loads the hidl instrumentation
647 // libraries and registers the instrumentation callback functions.
648 //
649 // The instrumentation libraries should be stored under any of the following
650 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
651 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
652 // follow pattern: ^profilerPrefix(.*).profiler.so$
653 //
654 // Each instrumentation library is expected to implement the instrumentation
655 // function called HIDL_INSTRUMENTATION_FUNCTION.
656 //
657 // A no-op for user build.
658 void registerInstrumentationCallbacks(
659 const std::string &profilerPrefix,
660 std::vector<InstrumentationCallback> *instrumentationCallbacks);
661
662 // Utility function to determine whether a give file is a instrumentation
663 // library (i.e. the file name follow the expected pattern).
664 bool isInstrumentationLib(
665 const std::string &profilerPrefix,
666 const dirent *file);
667 // A list of registered instrumentation callbacks.
668 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
669 // Flag whether to enable instrumentation.
670 bool mEnableInstrumentation;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700671};
672
Martijn Coenen72110162016-08-19 14:28:25 +0200673} // namespace hardware
674} // namespace android
675
Martijn Coenenc28f1152016-08-22 14:06:56 +0200676
Martijn Coenen72110162016-08-19 14:28:25 +0200677#endif // ANDROID_HIDL_SUPPORT_H
678