blob: 6a1f4e53882577f8461ec1b1958d55bca78a101c [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 {
37namespace hardware {
38
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010039// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
40// so that it can safely be transferred between 32-bit and 64-bit processes.
41struct hidl_handle {
42 hidl_handle() {
43 mHandle = nullptr;
44 }
45 ~hidl_handle() {
46 }
47
48 // copy constructors.
49 hidl_handle(const native_handle_t *handle) {
50 mHandle = handle;
51 }
52
53 hidl_handle(const hidl_handle &other) {
54 mHandle = other.mHandle;
55 }
56
57 // move constructor.
58 hidl_handle(hidl_handle &&other) {
59 *this = std::move(other);
60 }
61
62 // assingment operators
63 hidl_handle &operator=(const hidl_handle &other) {
64 mHandle = other.mHandle;
65 return *this;
66 }
67
68 hidl_handle &operator=(const native_handle_t *native_handle) {
69 mHandle = native_handle;
70 return *this;
71 }
72
73 hidl_handle &operator=(hidl_handle &&other) {
74 mHandle = other.mHandle;
75 other.mHandle = nullptr;
76 return *this;
77 }
78
79 const native_handle_t* operator->() const {
80 return mHandle;
81 }
82 // implicit conversion to const native_handle_t*
83 operator const native_handle_t *() const {
84 return mHandle;
85 }
86 // explicit conversion
87 const native_handle_t *getNativeHandle() const {
88 return mHandle;
89 }
90private:
91 details::hidl_pointer<const native_handle_t> mHandle;
92};
93
Martijn Coenen72110162016-08-19 14:28:25 +020094struct hidl_string {
95 hidl_string();
96 ~hidl_string();
97
Yifan Hong602b85a2016-10-24 13:40:01 -070098 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +020099 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700100 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -0700101 hidl_string(const char *);
Yifan Hong602b85a2016-10-24 13:40:01 -0700102 // copy from an std::string.
103 hidl_string(const std::string &);
104
105 // move constructor.
106 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200107
108 const char *c_str() const;
109 size_t size() const;
110 bool empty() const;
111
Yifan Hong602b85a2016-10-24 13:40:01 -0700112 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700113 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700114 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200115 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700116 // copy from an std::string.
117 hidl_string &operator=(const std::string &);
118 // move assignment operator.
119 hidl_string &operator=(hidl_string &&other);
120 // cast to std::string.
121 operator std::string() const;
122 // cast to C-style string. Caller is responsible
123 // to maintain this hidl_string alive.
124 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700125
Martijn Coenen72110162016-08-19 14:28:25 +0200126 void clear();
127
128 // Reference an external char array. Ownership is _not_ transferred.
129 // Caller is responsible for ensuring that underlying memory is valid
130 // for the lifetime of this hidl_string.
131 void setToExternal(const char *data, size_t size);
132
Andreas Huberebfeb362016-08-25 13:39:05 -0700133 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
134 static const size_t kOffsetOfBuffer;
135
Martijn Coenen72110162016-08-19 14:28:25 +0200136private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100137 details::hidl_pointer<const char> mBuffer;
138 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700139 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200140
Yifan Hong602b85a2016-10-24 13:40:01 -0700141 // copy from data with size. Assume that my memory is freed
142 // (through clear(), for example)
143 void copyFrom(const char *data, size_t size);
144 // move from another hidl_string
145 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200146};
147
Scott Randolphbb840f72016-11-21 14:39:26 -0800148inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
149 return strcmp(hs1.c_str(), hs2.c_str()) == 0;
150}
151
152inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
153 return !(hs1 == hs2);
154}
155
Yifan Hong5708fb42016-10-26 17:50:29 -0700156inline bool operator==(const hidl_string &hs, const char *s) {
157 return strcmp(hs.c_str(), s) == 0;
158}
159
160inline bool operator!=(const hidl_string &hs, const char *s) {
161 return !(hs == s);
162}
163
164inline bool operator==(const char *s, const hidl_string &hs) {
165 return strcmp(hs.c_str(), s) == 0;
166}
167
168inline bool operator!=(const char *s, const hidl_string &hs) {
169 return !(s == hs);
170}
171
Andreas Huber20dce082016-09-22 19:39:13 -0700172////////////////////////////////////////////////////////////////////////////////
173
Martijn Coenen72110162016-08-19 14:28:25 +0200174template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100175struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200176 hidl_vec()
177 : mBuffer(NULL),
178 mSize(0),
179 mOwnsBuffer(true) {
180 }
181
Yifan Hong602b85a2016-10-24 13:40:01 -0700182 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200183 *this = other;
184 }
185
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100186 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800187 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100188 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700189 }
190
Steven Morelandb69926a2016-11-15 10:02:57 -0800191 hidl_vec(const std::initializer_list<T> list)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800192 : mSize(list.size()),
193 mOwnsBuffer(true) {
194 mBuffer = new T[mSize];
195
Steven Morelandb69926a2016-11-15 10:02:57 -0800196 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800197 for (auto it = list.begin(); it != list.end(); ++it) {
198 mBuffer[idx++] = *it;
199 }
200 }
201
Yifan Hong602b85a2016-10-24 13:40:01 -0700202 hidl_vec(const std::vector<T> &other) : hidl_vec() {
203 *this = other;
204 }
205
Martijn Coenen72110162016-08-19 14:28:25 +0200206 ~hidl_vec() {
207 if (mOwnsBuffer) {
208 delete[] mBuffer;
209 }
210 mBuffer = NULL;
211 }
212
Alexey Polyudove2299012016-10-19 09:52:00 -0700213 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200214 // caller's responsibility to ensure that the underlying memory stays
215 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700216 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200217 if (mOwnsBuffer) {
218 delete [] mBuffer;
219 }
220 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100221 if (size > UINT32_MAX) {
222 logAlwaysFatal("external vector size exceeds 2^32 elements.");
223 }
224 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700225 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200226 }
227
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700228 T *data() {
229 return mBuffer;
230 }
231
232 const T *data() const {
233 return mBuffer;
234 }
235
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700236 T *releaseData() {
237 if (!mOwnsBuffer && mSize > 0) {
238 resize(mSize);
239 }
240 mOwnsBuffer = false;
241 return mBuffer;
242 }
243
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700244 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100245 if (mOwnsBuffer) {
246 delete[] mBuffer;
247 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700248 mBuffer = other.mBuffer;
249 mSize = other.mSize;
250 mOwnsBuffer = other.mOwnsBuffer;
251 other.mOwnsBuffer = false;
252 return *this;
253 }
254
Martijn Coenen72110162016-08-19 14:28:25 +0200255 hidl_vec &operator=(const hidl_vec &other) {
256 if (this != &other) {
257 if (mOwnsBuffer) {
258 delete[] mBuffer;
259 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700260 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200261 }
262
263 return *this;
264 }
265
Yifan Hong602b85a2016-10-24 13:40:01 -0700266 // copy from an std::vector.
267 hidl_vec &operator=(const std::vector<T> &other) {
268 if (mOwnsBuffer) {
269 delete[] mBuffer;
270 }
271 copyFrom(other, other.size());
272 return *this;
273 }
274
275 // cast to an std::vector.
276 operator std::vector<T>() const {
277 std::vector<T> v(mSize);
278 for (size_t i = 0; i < mSize; ++i) {
279 v[i] = mBuffer[i];
280 }
281 return v;
282 }
283
Martijn Coenen72110162016-08-19 14:28:25 +0200284 size_t size() const {
285 return mSize;
286 }
287
288 T &operator[](size_t index) {
289 return mBuffer[index];
290 }
291
292 const T &operator[](size_t index) const {
293 return mBuffer[index];
294 }
295
296 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100297 if (size > UINT32_MAX) {
298 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
299 }
Martijn Coenen72110162016-08-19 14:28:25 +0200300 T *newBuffer = new T[size];
301
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100302 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200303 newBuffer[i] = mBuffer[i];
304 }
305
306 if (mOwnsBuffer) {
307 delete[] mBuffer;
308 }
309 mBuffer = newBuffer;
310
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100311 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200312 mOwnsBuffer = true;
313 }
314
Yifan Hong089ae132016-11-11 11:32:52 -0800315 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
316 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800317
318 // Define std interator interface for walking the array contents
319 // TODO: it might be nice to implement a full featured random access iterator...
320 class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
321 {
322 public:
323 iterator(T* ptr) : mPtr(ptr) { }
324 iterator operator++() { iterator i = *this; mPtr++; return i; }
325 iterator operator++(int) { mPtr++; return *this; }
326 iterator operator--() { iterator i = *this; mPtr--; return i; }
327 iterator operator--(int) { mPtr--; return *this; }
328 T& operator*() { return *mPtr; }
329 T* operator->() { return mPtr; }
330 bool operator==(const iterator& rhs) const { return mPtr == rhs.mPtr; }
331 bool operator!=(const iterator& rhs) const { return mPtr != rhs.mPtr; }
332 private:
333 T* mPtr;
334 };
335 iterator begin() { return data(); }
336 iterator end() { return data()+mSize; }
337
Martijn Coenen72110162016-08-19 14:28:25 +0200338private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100339 details::hidl_pointer<T> mBuffer;
340 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200341 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700342
343 // copy from an array-like object, assuming my resources are freed.
344 template <typename Array>
345 void copyFrom(const Array &data, size_t size) {
346 mSize = size;
347 mOwnsBuffer = true;
348 if (mSize > 0) {
349 mBuffer = new T[size];
350 for (size_t i = 0; i < size; ++i) {
351 mBuffer[i] = data[i];
352 }
353 } else {
354 mBuffer = NULL;
355 }
356 }
Martijn Coenen72110162016-08-19 14:28:25 +0200357};
358
Yifan Hong089ae132016-11-11 11:32:52 -0800359template <typename T>
360const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
361
Andreas Huber20dce082016-09-22 19:39:13 -0700362////////////////////////////////////////////////////////////////////////////////
363
364namespace details {
365
366 template<size_t SIZE1, size_t... SIZES>
367 struct product {
368 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
369 };
370
371 template<size_t SIZE1>
372 struct product<SIZE1> {
373 static constexpr size_t value = SIZE1;
374 };
375
376 template<typename T, size_t SIZE1, size_t... SIZES>
377 struct accessor {
378 explicit accessor(T *base)
379 : mBase(base) {
380 }
381
382 accessor<T, SIZES...> operator[](size_t index) {
383 return accessor<T, SIZES...>(
384 &mBase[index * product<SIZES...>::value]);
385 }
386
387 private:
388 T *mBase;
389 };
390
391 template<typename T, size_t SIZE1>
392 struct accessor<T, SIZE1> {
393 explicit accessor(T *base)
394 : mBase(base) {
395 }
396
397 T &operator[](size_t index) {
398 return mBase[index];
399 }
400
401 private:
402 T *mBase;
403 };
404
405 template<typename T, size_t SIZE1, size_t... SIZES>
406 struct const_accessor {
407 explicit const_accessor(const T *base)
408 : mBase(base) {
409 }
410
411 const_accessor<T, SIZES...> operator[](size_t index) {
412 return const_accessor<T, SIZES...>(
413 &mBase[index * product<SIZES...>::value]);
414 }
415
416 private:
417 const T *mBase;
418 };
419
420 template<typename T, size_t SIZE1>
421 struct const_accessor<T, SIZE1> {
422 explicit const_accessor(const T *base)
423 : mBase(base) {
424 }
425
426 const T &operator[](size_t index) const {
427 return mBase[index];
428 }
429
430 private:
431 const T *mBase;
432 };
433
434} // namespace details
435
436////////////////////////////////////////////////////////////////////////////////
437
438template<typename T, size_t SIZE1, size_t... SIZES>
439struct hidl_array {
440 hidl_array() = default;
441
442 T *data() { return mBuffer; }
443 const T *data() const { return mBuffer; }
444
445 details::accessor<T, SIZES...> operator[](size_t index) {
446 return details::accessor<T, SIZES...>(
447 &mBuffer[index * details::product<SIZES...>::value]);
448 }
449
450 details::const_accessor<T, SIZES...> operator[](size_t index) const {
451 return details::const_accessor<T, SIZES...>(
452 &mBuffer[index * details::product<SIZES...>::value]);
453 }
454
Andreas Huber00a985c2016-09-28 14:24:53 -0700455 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
456
457 static constexpr size_tuple_type size() {
458 return std::make_tuple(SIZE1, SIZES...);
459 }
460
Andreas Huber20dce082016-09-22 19:39:13 -0700461private:
462 T mBuffer[details::product<SIZE1, SIZES...>::value];
463};
464
465template<typename T, size_t SIZE1>
466struct hidl_array<T, SIZE1> {
467 hidl_array() = default;
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800468 hidl_array(const T *source) {
469 memcpy(mBuffer, source, SIZE1 * sizeof(T));
470 }
Andreas Huber20dce082016-09-22 19:39:13 -0700471
472 T *data() { return mBuffer; }
473 const T *data() const { return mBuffer; }
474
475 T &operator[](size_t index) {
476 return mBuffer[index];
477 }
478
479 const T &operator[](size_t index) const {
480 return mBuffer[index];
481 }
482
Andreas Huber00a985c2016-09-28 14:24:53 -0700483 static constexpr size_t size() { return SIZE1; }
484
Andreas Huber20dce082016-09-22 19:39:13 -0700485private:
486 T mBuffer[SIZE1];
487};
488
Martijn Coenen72110162016-08-19 14:28:25 +0200489// ----------------------------------------------------------------------
490// Version functions
491struct hidl_version {
492public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800493 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200494
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700495 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200496 return (mMajor == other.get_major() && mMinor == other.get_minor());
497 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200498
Martijn Coenen097a7672016-09-08 16:56:41 +0200499 constexpr uint16_t get_major() const { return mMajor; }
500 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200501
Martijn Coenen72110162016-08-19 14:28:25 +0200502private:
503 uint16_t mMajor;
504 uint16_t mMinor;
505};
506
507inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
508 return hidl_version(major,minor);
509}
510
Yifan Hongc1a60ef2016-11-08 18:53:16 -0800511struct IBase : virtual public RefBase {
Yifan Honga3c31842016-10-21 10:33:14 -0700512 virtual bool isRemote() const = 0;
513 // HIDL reserved methods follow.
514 virtual ::android::hardware::Return<void> interfaceChain(
515 std::function<void(const hidl_vec<hidl_string>&)> _hidl_cb) = 0;
Martijn Coenen70822ea2016-11-16 15:47:09 +0100516 // This method notifies the interface that one or more system properties have changed.
517 // The default implementation calls report_sysprop_change() in libcutils, which in turn
518 // calls a set of registered callbacks (eg to update trace tags).
519 virtual ::android::hardware::Return<void> notifySyspropsChanged() = 0;
Yifan Honga3c31842016-10-21 10:33:14 -0700520 // descriptor for HIDL reserved methods.
Steven Moreland5a682322016-11-11 12:32:50 -0800521 static const char* descriptor;
Yifan Honga3c31842016-10-21 10:33:14 -0700522};
523
Steven Morelandbdf26662016-09-02 11:03:15 -0700524#if defined(__LP64__)
525#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
526#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
527#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
528#else
529#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
530#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
531#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
532#endif
533
Steven Moreland40ede2c2016-11-09 13:51:53 -0800534#define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200535 static ::android::sp<I##INTERFACE> getService( \
Iliyan Malchevb42ce262016-09-26 00:09:35 -0700536 const std::string &serviceName, bool getStub=false); \
Steven Moreland40ede2c2016-11-09 13:51:53 -0800537 ::android::status_t registerAsService(const std::string &serviceName); \
538 static bool registerForNotifications( \
539 const std::string &serviceName, \
540 const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \
541 &notification); \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200542
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700543// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700544// Class that provides Hidl instrumentation utilities.
545struct HidlInstrumentor {
546 // Event that triggers the instrumentation. e.g. enter of an API call on
547 // the server/client side, exit of an API call on the server/client side
548 // etc.
549 enum InstrumentationEvent {
550 SERVER_API_ENTRY = 0,
551 SERVER_API_EXIT,
552 CLIENT_API_ENTRY,
553 CLIENT_API_EXIT,
554 SYNC_CALLBACK_ENTRY,
555 SYNC_CALLBACK_EXIT,
556 ASYNC_CALLBACK_ENTRY,
557 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700558 PASSTHROUGH_ENTRY,
559 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700560 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700561
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700562 // Signature of the instrumentation callback function.
563 using InstrumentationCallback = std::function<void(
564 const InstrumentationEvent event,
565 const char *package,
566 const char *version,
567 const char *interface,
568 const char *method,
569 std::vector<void *> *args)>;
570
571 explicit HidlInstrumentor(const std::string &prefix);
572 virtual ~HidlInstrumentor();
573
574 protected:
575 // Function that lookup and dynamically loads the hidl instrumentation
576 // libraries and registers the instrumentation callback functions.
577 //
578 // The instrumentation libraries should be stored under any of the following
579 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
580 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
581 // follow pattern: ^profilerPrefix(.*).profiler.so$
582 //
583 // Each instrumentation library is expected to implement the instrumentation
584 // function called HIDL_INSTRUMENTATION_FUNCTION.
585 //
586 // A no-op for user build.
587 void registerInstrumentationCallbacks(
588 const std::string &profilerPrefix,
589 std::vector<InstrumentationCallback> *instrumentationCallbacks);
590
591 // Utility function to determine whether a give file is a instrumentation
592 // library (i.e. the file name follow the expected pattern).
593 bool isInstrumentationLib(
594 const std::string &profilerPrefix,
595 const dirent *file);
596 // A list of registered instrumentation callbacks.
597 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
598 // Flag whether to enable instrumentation.
599 bool mEnableInstrumentation;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700600};
601
Martijn Coenen72110162016-08-19 14:28:25 +0200602} // namespace hardware
603} // namespace android
604
Martijn Coenenc28f1152016-08-22 14:06:56 +0200605
Martijn Coenen72110162016-08-19 14:28:25 +0200606#endif // ANDROID_HIDL_SUPPORT_H
607