blob: f9f7f323d9a8e456d2a9bfedfebf97a7d05dcde2 [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>
Steven Moreland337c3ae2016-11-22 13:37:32 -080027#include <hidl/HidlInternal.h>
Yifan Honga3c31842016-10-21 10:33:14 -070028#include <hidl/Status.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080029#include <map>
Andreas Huber00a985c2016-09-28 14:24:53 -070030#include <tuple>
Iliyan Malchev692070a2016-09-12 16:30:44 -070031#include <utils/Errors.h>
32#include <utils/RefBase.h>
33#include <utils/StrongPointer.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080034#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020035
36namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010037
38// this file is included by all hidl interface, so we must forward declare the IMemory type.
39namespace hidl {
40namespace memory {
41namespace V1_0 {
42 struct IMemory;
43}; // namespace V1_0
44}; // namespace manager
45}; // namespace hidl
46
Martijn Coenen72110162016-08-19 14:28:25 +020047namespace hardware {
48
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010049// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
50// so that it can safely be transferred between 32-bit and 64-bit processes.
51struct hidl_handle {
52 hidl_handle() {
53 mHandle = nullptr;
54 }
55 ~hidl_handle() {
56 }
57
58 // copy constructors.
59 hidl_handle(const native_handle_t *handle) {
60 mHandle = handle;
61 }
62
63 hidl_handle(const hidl_handle &other) {
64 mHandle = other.mHandle;
65 }
66
67 // move constructor.
68 hidl_handle(hidl_handle &&other) {
69 *this = std::move(other);
70 }
71
72 // assingment operators
73 hidl_handle &operator=(const hidl_handle &other) {
74 mHandle = other.mHandle;
75 return *this;
76 }
77
78 hidl_handle &operator=(const native_handle_t *native_handle) {
79 mHandle = native_handle;
80 return *this;
81 }
82
83 hidl_handle &operator=(hidl_handle &&other) {
84 mHandle = other.mHandle;
85 other.mHandle = nullptr;
86 return *this;
87 }
88
89 const native_handle_t* operator->() const {
90 return mHandle;
91 }
92 // implicit conversion to const native_handle_t*
93 operator const native_handle_t *() const {
94 return mHandle;
95 }
96 // explicit conversion
97 const native_handle_t *getNativeHandle() const {
98 return mHandle;
99 }
100private:
101 details::hidl_pointer<const native_handle_t> mHandle;
102};
103
Martijn Coenen72110162016-08-19 14:28:25 +0200104struct hidl_string {
105 hidl_string();
106 ~hidl_string();
107
Yifan Hong602b85a2016-10-24 13:40:01 -0700108 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200109 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700110 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -0700111 hidl_string(const char *);
Yifan Hong602b85a2016-10-24 13:40:01 -0700112 // copy from an std::string.
113 hidl_string(const std::string &);
114
115 // move constructor.
116 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200117
118 const char *c_str() const;
119 size_t size() const;
120 bool empty() const;
121
Yifan Hong602b85a2016-10-24 13:40:01 -0700122 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700123 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700124 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200125 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700126 // copy from an std::string.
127 hidl_string &operator=(const std::string &);
128 // move assignment operator.
129 hidl_string &operator=(hidl_string &&other);
130 // cast to std::string.
131 operator std::string() const;
132 // cast to C-style string. Caller is responsible
133 // to maintain this hidl_string alive.
134 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700135
Martijn Coenen72110162016-08-19 14:28:25 +0200136 void clear();
137
138 // Reference an external char array. Ownership is _not_ transferred.
139 // Caller is responsible for ensuring that underlying memory is valid
140 // for the lifetime of this hidl_string.
141 void setToExternal(const char *data, size_t size);
142
Andreas Huberebfeb362016-08-25 13:39:05 -0700143 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
144 static const size_t kOffsetOfBuffer;
145
Martijn Coenen72110162016-08-19 14:28:25 +0200146private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100147 details::hidl_pointer<const char> mBuffer;
148 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700149 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200150
Yifan Hong602b85a2016-10-24 13:40:01 -0700151 // copy from data with size. Assume that my memory is freed
152 // (through clear(), for example)
153 void copyFrom(const char *data, size_t size);
154 // move from another hidl_string
155 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200156};
157
Scott Randolphbb840f72016-11-21 14:39:26 -0800158inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
159 return strcmp(hs1.c_str(), hs2.c_str()) == 0;
160}
161
162inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
163 return !(hs1 == hs2);
164}
165
Yifan Hong5708fb42016-10-26 17:50:29 -0700166inline bool operator==(const hidl_string &hs, const char *s) {
167 return strcmp(hs.c_str(), s) == 0;
168}
169
170inline bool operator!=(const hidl_string &hs, const char *s) {
171 return !(hs == s);
172}
173
174inline bool operator==(const char *s, const hidl_string &hs) {
175 return strcmp(hs.c_str(), s) == 0;
176}
177
178inline bool operator!=(const char *s, const hidl_string &hs) {
179 return !(s == hs);
180}
181
Martijn Coenen30791002016-12-01 15:40:46 +0100182// hidl_memory is a structure that can be used to transfer
183// pieces of shared memory between processes. The assumption
184// of this object is that the memory remains accessible as
185// long as the file descriptors in the enclosed mHandle
186// - as well as all of its cross-process dups() - remain opened.
187struct hidl_memory {
188
189 hidl_memory() : mOwnsHandle(false), mHandle(nullptr), mSize(0), mName("") {
190 }
191
192 /**
193 * Creates a hidl_memory object and takes ownership of the handle.
194 */
195 hidl_memory(const hidl_string &name, const hidl_handle &handle, size_t size)
196 : mOwnsHandle(true),
197 mHandle(handle),
198 mSize(size),
199 mName(name)
200 {}
201
202 // copy constructor
203 hidl_memory(const hidl_memory& other) {
204 *this = other;
205 }
206
207 // copy assignment
208 hidl_memory &operator=(const hidl_memory &other) {
209 if (this != &other) {
210 mOwnsHandle = true;
211 mHandle = native_handle_clone(other.mHandle);
212 mSize = other.mSize;
213 mName = other.mName;
214 }
215
216 return *this;
217 }
218
219 // TODO move constructor/move assignment
220
221 ~hidl_memory() {
222 // TODO if we had previously mapped from this object, unmap
223 if (mOwnsHandle) {
224 native_handle_close(mHandle);
225 }
226 }
227
228 const native_handle_t* handle() const {
229 return mHandle;
230 }
231
232 const hidl_string &name() const {
233 return mName;
234 }
235
236 size_t size() const {
237 return mSize;
238 }
239
240 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
241 static const size_t kOffsetOfHandle;
242 // offsetof(hidl_memory, mName) exposed since mHandle is private.
243 static const size_t kOffsetOfName;
244private:
245 bool mOwnsHandle;
246 hidl_handle mHandle;
247 size_t mSize;
248 hidl_string mName;
249};
250
Andreas Huber20dce082016-09-22 19:39:13 -0700251////////////////////////////////////////////////////////////////////////////////
252
Martijn Coenen72110162016-08-19 14:28:25 +0200253template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100254struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200255 hidl_vec()
256 : mBuffer(NULL),
257 mSize(0),
258 mOwnsBuffer(true) {
259 }
260
Yifan Hong602b85a2016-10-24 13:40:01 -0700261 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200262 *this = other;
263 }
264
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100265 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800266 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100267 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700268 }
269
Steven Morelandb69926a2016-11-15 10:02:57 -0800270 hidl_vec(const std::initializer_list<T> list)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800271 : mSize(list.size()),
272 mOwnsBuffer(true) {
273 mBuffer = new T[mSize];
274
Steven Morelandb69926a2016-11-15 10:02:57 -0800275 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800276 for (auto it = list.begin(); it != list.end(); ++it) {
277 mBuffer[idx++] = *it;
278 }
279 }
280
Yifan Hong602b85a2016-10-24 13:40:01 -0700281 hidl_vec(const std::vector<T> &other) : hidl_vec() {
282 *this = other;
283 }
284
Martijn Coenen72110162016-08-19 14:28:25 +0200285 ~hidl_vec() {
286 if (mOwnsBuffer) {
287 delete[] mBuffer;
288 }
289 mBuffer = NULL;
290 }
291
Alexey Polyudove2299012016-10-19 09:52:00 -0700292 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200293 // caller's responsibility to ensure that the underlying memory stays
294 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700295 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200296 if (mOwnsBuffer) {
297 delete [] mBuffer;
298 }
299 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100300 if (size > UINT32_MAX) {
301 logAlwaysFatal("external vector size exceeds 2^32 elements.");
302 }
303 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700304 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200305 }
306
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700307 T *data() {
308 return mBuffer;
309 }
310
311 const T *data() const {
312 return mBuffer;
313 }
314
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700315 T *releaseData() {
316 if (!mOwnsBuffer && mSize > 0) {
317 resize(mSize);
318 }
319 mOwnsBuffer = false;
320 return mBuffer;
321 }
322
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700323 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100324 if (mOwnsBuffer) {
325 delete[] mBuffer;
326 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700327 mBuffer = other.mBuffer;
328 mSize = other.mSize;
329 mOwnsBuffer = other.mOwnsBuffer;
330 other.mOwnsBuffer = false;
331 return *this;
332 }
333
Martijn Coenen72110162016-08-19 14:28:25 +0200334 hidl_vec &operator=(const hidl_vec &other) {
335 if (this != &other) {
336 if (mOwnsBuffer) {
337 delete[] mBuffer;
338 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700339 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200340 }
341
342 return *this;
343 }
344
Yifan Hong602b85a2016-10-24 13:40:01 -0700345 // copy from an std::vector.
346 hidl_vec &operator=(const std::vector<T> &other) {
347 if (mOwnsBuffer) {
348 delete[] mBuffer;
349 }
350 copyFrom(other, other.size());
351 return *this;
352 }
353
354 // cast to an std::vector.
355 operator std::vector<T>() const {
356 std::vector<T> v(mSize);
357 for (size_t i = 0; i < mSize; ++i) {
358 v[i] = mBuffer[i];
359 }
360 return v;
361 }
362
Martijn Coenen72110162016-08-19 14:28:25 +0200363 size_t size() const {
364 return mSize;
365 }
366
367 T &operator[](size_t index) {
368 return mBuffer[index];
369 }
370
371 const T &operator[](size_t index) const {
372 return mBuffer[index];
373 }
374
375 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100376 if (size > UINT32_MAX) {
377 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
378 }
Martijn Coenen72110162016-08-19 14:28:25 +0200379 T *newBuffer = new T[size];
380
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100381 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200382 newBuffer[i] = mBuffer[i];
383 }
384
385 if (mOwnsBuffer) {
386 delete[] mBuffer;
387 }
388 mBuffer = newBuffer;
389
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100390 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200391 mOwnsBuffer = true;
392 }
393
Yifan Hong089ae132016-11-11 11:32:52 -0800394 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
395 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800396
397 // Define std interator interface for walking the array contents
398 // TODO: it might be nice to implement a full featured random access iterator...
399 class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
400 {
401 public:
402 iterator(T* ptr) : mPtr(ptr) { }
403 iterator operator++() { iterator i = *this; mPtr++; return i; }
404 iterator operator++(int) { mPtr++; return *this; }
405 iterator operator--() { iterator i = *this; mPtr--; return i; }
406 iterator operator--(int) { mPtr--; return *this; }
407 T& operator*() { return *mPtr; }
408 T* operator->() { return mPtr; }
409 bool operator==(const iterator& rhs) const { return mPtr == rhs.mPtr; }
410 bool operator!=(const iterator& rhs) const { return mPtr != rhs.mPtr; }
411 private:
412 T* mPtr;
413 };
414 iterator begin() { return data(); }
415 iterator end() { return data()+mSize; }
416
Martijn Coenen72110162016-08-19 14:28:25 +0200417private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100418 details::hidl_pointer<T> mBuffer;
419 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200420 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700421
422 // copy from an array-like object, assuming my resources are freed.
423 template <typename Array>
424 void copyFrom(const Array &data, size_t size) {
425 mSize = size;
426 mOwnsBuffer = true;
427 if (mSize > 0) {
428 mBuffer = new T[size];
429 for (size_t i = 0; i < size; ++i) {
430 mBuffer[i] = data[i];
431 }
432 } else {
433 mBuffer = NULL;
434 }
435 }
Martijn Coenen72110162016-08-19 14:28:25 +0200436};
437
Yifan Hong089ae132016-11-11 11:32:52 -0800438template <typename T>
439const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
440
Andreas Huber20dce082016-09-22 19:39:13 -0700441////////////////////////////////////////////////////////////////////////////////
442
443namespace details {
444
445 template<size_t SIZE1, size_t... SIZES>
446 struct product {
447 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
448 };
449
450 template<size_t SIZE1>
451 struct product<SIZE1> {
452 static constexpr size_t value = SIZE1;
453 };
454
455 template<typename T, size_t SIZE1, size_t... SIZES>
456 struct accessor {
457 explicit accessor(T *base)
458 : mBase(base) {
459 }
460
461 accessor<T, SIZES...> operator[](size_t index) {
462 return accessor<T, SIZES...>(
463 &mBase[index * product<SIZES...>::value]);
464 }
465
466 private:
467 T *mBase;
468 };
469
470 template<typename T, size_t SIZE1>
471 struct accessor<T, SIZE1> {
472 explicit accessor(T *base)
473 : mBase(base) {
474 }
475
476 T &operator[](size_t index) {
477 return mBase[index];
478 }
479
480 private:
481 T *mBase;
482 };
483
484 template<typename T, size_t SIZE1, size_t... SIZES>
485 struct const_accessor {
486 explicit const_accessor(const T *base)
487 : mBase(base) {
488 }
489
490 const_accessor<T, SIZES...> operator[](size_t index) {
491 return const_accessor<T, SIZES...>(
492 &mBase[index * product<SIZES...>::value]);
493 }
494
495 private:
496 const T *mBase;
497 };
498
499 template<typename T, size_t SIZE1>
500 struct const_accessor<T, SIZE1> {
501 explicit const_accessor(const T *base)
502 : mBase(base) {
503 }
504
505 const T &operator[](size_t index) const {
506 return mBase[index];
507 }
508
509 private:
510 const T *mBase;
511 };
512
513} // namespace details
514
515////////////////////////////////////////////////////////////////////////////////
516
517template<typename T, size_t SIZE1, size_t... SIZES>
518struct hidl_array {
519 hidl_array() = default;
520
521 T *data() { return mBuffer; }
522 const T *data() const { return mBuffer; }
523
524 details::accessor<T, SIZES...> operator[](size_t index) {
525 return details::accessor<T, SIZES...>(
526 &mBuffer[index * details::product<SIZES...>::value]);
527 }
528
529 details::const_accessor<T, SIZES...> operator[](size_t index) const {
530 return details::const_accessor<T, SIZES...>(
531 &mBuffer[index * details::product<SIZES...>::value]);
532 }
533
Andreas Huber00a985c2016-09-28 14:24:53 -0700534 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
535
536 static constexpr size_tuple_type size() {
537 return std::make_tuple(SIZE1, SIZES...);
538 }
539
Andreas Huber20dce082016-09-22 19:39:13 -0700540private:
541 T mBuffer[details::product<SIZE1, SIZES...>::value];
542};
543
544template<typename T, size_t SIZE1>
545struct hidl_array<T, SIZE1> {
546 hidl_array() = default;
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800547 hidl_array(const T *source) {
548 memcpy(mBuffer, source, SIZE1 * sizeof(T));
549 }
Andreas Huber20dce082016-09-22 19:39:13 -0700550
551 T *data() { return mBuffer; }
552 const T *data() const { return mBuffer; }
553
554 T &operator[](size_t index) {
555 return mBuffer[index];
556 }
557
558 const T &operator[](size_t index) const {
559 return mBuffer[index];
560 }
561
Andreas Huber00a985c2016-09-28 14:24:53 -0700562 static constexpr size_t size() { return SIZE1; }
563
Andreas Huber20dce082016-09-22 19:39:13 -0700564private:
565 T mBuffer[SIZE1];
566};
567
Martijn Coenen72110162016-08-19 14:28:25 +0200568// ----------------------------------------------------------------------
569// Version functions
570struct hidl_version {
571public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800572 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200573
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700574 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200575 return (mMajor == other.get_major() && mMinor == other.get_minor());
576 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200577
Martijn Coenen097a7672016-09-08 16:56:41 +0200578 constexpr uint16_t get_major() const { return mMajor; }
579 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200580
Martijn Coenen72110162016-08-19 14:28:25 +0200581private:
582 uint16_t mMajor;
583 uint16_t mMinor;
584};
585
586inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
587 return hidl_version(major,minor);
588}
589
Yifan Hongc1a60ef2016-11-08 18:53:16 -0800590struct IBase : virtual public RefBase {
Yifan Honga3c31842016-10-21 10:33:14 -0700591 virtual bool isRemote() const = 0;
592 // HIDL reserved methods follow.
593 virtual ::android::hardware::Return<void> interfaceChain(
594 std::function<void(const hidl_vec<hidl_string>&)> _hidl_cb) = 0;
Martijn Coenen70822ea2016-11-16 15:47:09 +0100595 // This method notifies the interface that one or more system properties have changed.
596 // The default implementation calls report_sysprop_change() in libcutils, which in turn
597 // calls a set of registered callbacks (eg to update trace tags).
598 virtual ::android::hardware::Return<void> notifySyspropsChanged() = 0;
Yifan Honga3c31842016-10-21 10:33:14 -0700599 // descriptor for HIDL reserved methods.
Steven Moreland5a682322016-11-11 12:32:50 -0800600 static const char* descriptor;
Yifan Honga3c31842016-10-21 10:33:14 -0700601};
602
Steven Morelandbdf26662016-09-02 11:03:15 -0700603#if defined(__LP64__)
604#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
605#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
606#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
607#else
608#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
609#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
610#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
611#endif
612
Steven Moreland40ede2c2016-11-09 13:51:53 -0800613#define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200614 static ::android::sp<I##INTERFACE> getService( \
Iliyan Malchevb42ce262016-09-26 00:09:35 -0700615 const std::string &serviceName, bool getStub=false); \
Steven Moreland40ede2c2016-11-09 13:51:53 -0800616 ::android::status_t registerAsService(const std::string &serviceName); \
617 static bool registerForNotifications( \
618 const std::string &serviceName, \
619 const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \
620 &notification); \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200621
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700622// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700623// Class that provides Hidl instrumentation utilities.
624struct HidlInstrumentor {
625 // Event that triggers the instrumentation. e.g. enter of an API call on
626 // the server/client side, exit of an API call on the server/client side
627 // etc.
628 enum InstrumentationEvent {
629 SERVER_API_ENTRY = 0,
630 SERVER_API_EXIT,
631 CLIENT_API_ENTRY,
632 CLIENT_API_EXIT,
633 SYNC_CALLBACK_ENTRY,
634 SYNC_CALLBACK_EXIT,
635 ASYNC_CALLBACK_ENTRY,
636 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700637 PASSTHROUGH_ENTRY,
638 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700639 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700640
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700641 // Signature of the instrumentation callback function.
642 using InstrumentationCallback = std::function<void(
643 const InstrumentationEvent event,
644 const char *package,
645 const char *version,
646 const char *interface,
647 const char *method,
648 std::vector<void *> *args)>;
649
650 explicit HidlInstrumentor(const std::string &prefix);
651 virtual ~HidlInstrumentor();
652
653 protected:
654 // Function that lookup and dynamically loads the hidl instrumentation
655 // libraries and registers the instrumentation callback functions.
656 //
657 // The instrumentation libraries should be stored under any of the following
658 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
659 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
660 // follow pattern: ^profilerPrefix(.*).profiler.so$
661 //
662 // Each instrumentation library is expected to implement the instrumentation
663 // function called HIDL_INSTRUMENTATION_FUNCTION.
664 //
665 // A no-op for user build.
666 void registerInstrumentationCallbacks(
667 const std::string &profilerPrefix,
668 std::vector<InstrumentationCallback> *instrumentationCallbacks);
669
670 // Utility function to determine whether a give file is a instrumentation
671 // library (i.e. the file name follow the expected pattern).
672 bool isInstrumentationLib(
673 const std::string &profilerPrefix,
674 const dirent *file);
675 // A list of registered instrumentation callbacks.
676 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
677 // Flag whether to enable instrumentation.
678 bool mEnableInstrumentation;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700679};
680
Martijn Coenen72110162016-08-19 14:28:25 +0200681} // namespace hardware
682} // namespace android
683
Martijn Coenenc28f1152016-08-22 14:06:56 +0200684
Martijn Coenen72110162016-08-19 14:28:25 +0200685#endif // ANDROID_HIDL_SUPPORT_H
686