blob: 48cfd26dc7e42bc4ea14ee087863bd7b2957ccd2 [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>
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -070023#include <cutils/properties.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080024#include <functional>
Yifan Honga3c31842016-10-21 10:33:14 -070025#include <hidl/Status.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080026#include <map>
Andreas Huber00a985c2016-09-28 14:24:53 -070027#include <tuple>
Iliyan Malchev692070a2016-09-12 16:30:44 -070028#include <utils/Errors.h>
29#include <utils/RefBase.h>
30#include <utils/StrongPointer.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080031#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020032
33namespace android {
34namespace hardware {
35
36struct hidl_string {
37 hidl_string();
38 ~hidl_string();
39
Yifan Hong602b85a2016-10-24 13:40:01 -070040 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +020041 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -070042 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -070043 hidl_string(const char *);
Yifan Hong602b85a2016-10-24 13:40:01 -070044 // copy from an std::string.
45 hidl_string(const std::string &);
46
47 // move constructor.
48 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +020049
50 const char *c_str() const;
51 size_t size() const;
52 bool empty() const;
53
Yifan Hong602b85a2016-10-24 13:40:01 -070054 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -070055 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -070056 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +020057 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -070058 // copy from an std::string.
59 hidl_string &operator=(const std::string &);
60 // move assignment operator.
61 hidl_string &operator=(hidl_string &&other);
62 // cast to std::string.
63 operator std::string() const;
64 // cast to C-style string. Caller is responsible
65 // to maintain this hidl_string alive.
66 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -070067
Martijn Coenen72110162016-08-19 14:28:25 +020068 void clear();
69
70 // Reference an external char array. Ownership is _not_ transferred.
71 // Caller is responsible for ensuring that underlying memory is valid
72 // for the lifetime of this hidl_string.
73 void setToExternal(const char *data, size_t size);
74
Andreas Huberebfeb362016-08-25 13:39:05 -070075 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
76 static const size_t kOffsetOfBuffer;
77
Martijn Coenen72110162016-08-19 14:28:25 +020078private:
Yifan Hong602b85a2016-10-24 13:40:01 -070079 const char *mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +020080 size_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -070081 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +020082
Yifan Hong602b85a2016-10-24 13:40:01 -070083 // copy from data with size. Assume that my memory is freed
84 // (through clear(), for example)
85 void copyFrom(const char *data, size_t size);
86 // move from another hidl_string
87 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +020088};
89
Yifan Hong5708fb42016-10-26 17:50:29 -070090inline bool operator==(const hidl_string &hs, const char *s) {
91 return strcmp(hs.c_str(), s) == 0;
92}
93
94inline bool operator!=(const hidl_string &hs, const char *s) {
95 return !(hs == s);
96}
97
98inline bool operator==(const char *s, const hidl_string &hs) {
99 return strcmp(hs.c_str(), s) == 0;
100}
101
102inline bool operator!=(const char *s, const hidl_string &hs) {
103 return !(s == hs);
104}
105
Andreas Huber20dce082016-09-22 19:39:13 -0700106////////////////////////////////////////////////////////////////////////////////
107
Martijn Coenen72110162016-08-19 14:28:25 +0200108template<typename T>
109struct hidl_vec {
110 hidl_vec()
111 : mBuffer(NULL),
112 mSize(0),
113 mOwnsBuffer(true) {
114 }
115
Yifan Hong602b85a2016-10-24 13:40:01 -0700116 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200117 *this = other;
118 }
119
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100120 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800121 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100122 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700123 }
124
Steven Morelandb69926a2016-11-15 10:02:57 -0800125 hidl_vec(const std::initializer_list<T> list)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800126 : mSize(list.size()),
127 mOwnsBuffer(true) {
128 mBuffer = new T[mSize];
129
Steven Morelandb69926a2016-11-15 10:02:57 -0800130 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800131 for (auto it = list.begin(); it != list.end(); ++it) {
132 mBuffer[idx++] = *it;
133 }
134 }
135
Yifan Hong602b85a2016-10-24 13:40:01 -0700136 hidl_vec(const std::vector<T> &other) : hidl_vec() {
137 *this = other;
138 }
139
Martijn Coenen72110162016-08-19 14:28:25 +0200140 ~hidl_vec() {
141 if (mOwnsBuffer) {
142 delete[] mBuffer;
143 }
144 mBuffer = NULL;
145 }
146
Alexey Polyudove2299012016-10-19 09:52:00 -0700147 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200148 // caller's responsibility to ensure that the underlying memory stays
149 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700150 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200151 if (mOwnsBuffer) {
152 delete [] mBuffer;
153 }
154 mBuffer = data;
155 mSize = size;
Alexey Polyudove2299012016-10-19 09:52:00 -0700156 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200157 }
158
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700159 T *data() {
160 return mBuffer;
161 }
162
163 const T *data() const {
164 return mBuffer;
165 }
166
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700167 T *releaseData() {
168 if (!mOwnsBuffer && mSize > 0) {
169 resize(mSize);
170 }
171 mOwnsBuffer = false;
172 return mBuffer;
173 }
174
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700175 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100176 if (mOwnsBuffer) {
177 delete[] mBuffer;
178 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700179 mBuffer = other.mBuffer;
180 mSize = other.mSize;
181 mOwnsBuffer = other.mOwnsBuffer;
182 other.mOwnsBuffer = false;
183 return *this;
184 }
185
Martijn Coenen72110162016-08-19 14:28:25 +0200186 hidl_vec &operator=(const hidl_vec &other) {
187 if (this != &other) {
188 if (mOwnsBuffer) {
189 delete[] mBuffer;
190 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700191 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200192 }
193
194 return *this;
195 }
196
Yifan Hong602b85a2016-10-24 13:40:01 -0700197 // copy from an std::vector.
198 hidl_vec &operator=(const std::vector<T> &other) {
199 if (mOwnsBuffer) {
200 delete[] mBuffer;
201 }
202 copyFrom(other, other.size());
203 return *this;
204 }
205
206 // cast to an std::vector.
207 operator std::vector<T>() const {
208 std::vector<T> v(mSize);
209 for (size_t i = 0; i < mSize; ++i) {
210 v[i] = mBuffer[i];
211 }
212 return v;
213 }
214
Martijn Coenen72110162016-08-19 14:28:25 +0200215 size_t size() const {
216 return mSize;
217 }
218
219 T &operator[](size_t index) {
220 return mBuffer[index];
221 }
222
223 const T &operator[](size_t index) const {
224 return mBuffer[index];
225 }
226
227 void resize(size_t size) {
228 T *newBuffer = new T[size];
229
230 for (size_t i = 0; i < std::min(size, mSize); ++i) {
231 newBuffer[i] = mBuffer[i];
232 }
233
234 if (mOwnsBuffer) {
235 delete[] mBuffer;
236 }
237 mBuffer = newBuffer;
238
239 mSize = size;
240 mOwnsBuffer = true;
241 }
242
Yifan Hong089ae132016-11-11 11:32:52 -0800243 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
244 static const size_t kOffsetOfBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200245private:
246 T *mBuffer;
247 size_t mSize;
248 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700249
250 // copy from an array-like object, assuming my resources are freed.
251 template <typename Array>
252 void copyFrom(const Array &data, size_t size) {
253 mSize = size;
254 mOwnsBuffer = true;
255 if (mSize > 0) {
256 mBuffer = new T[size];
257 for (size_t i = 0; i < size; ++i) {
258 mBuffer[i] = data[i];
259 }
260 } else {
261 mBuffer = NULL;
262 }
263 }
Martijn Coenen72110162016-08-19 14:28:25 +0200264};
265
Yifan Hong089ae132016-11-11 11:32:52 -0800266template <typename T>
267const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
268
Andreas Huber20dce082016-09-22 19:39:13 -0700269////////////////////////////////////////////////////////////////////////////////
270
271namespace details {
272
273 template<size_t SIZE1, size_t... SIZES>
274 struct product {
275 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
276 };
277
278 template<size_t SIZE1>
279 struct product<SIZE1> {
280 static constexpr size_t value = SIZE1;
281 };
282
283 template<typename T, size_t SIZE1, size_t... SIZES>
284 struct accessor {
285 explicit accessor(T *base)
286 : mBase(base) {
287 }
288
289 accessor<T, SIZES...> operator[](size_t index) {
290 return accessor<T, SIZES...>(
291 &mBase[index * product<SIZES...>::value]);
292 }
293
294 private:
295 T *mBase;
296 };
297
298 template<typename T, size_t SIZE1>
299 struct accessor<T, SIZE1> {
300 explicit accessor(T *base)
301 : mBase(base) {
302 }
303
304 T &operator[](size_t index) {
305 return mBase[index];
306 }
307
308 private:
309 T *mBase;
310 };
311
312 template<typename T, size_t SIZE1, size_t... SIZES>
313 struct const_accessor {
314 explicit const_accessor(const T *base)
315 : mBase(base) {
316 }
317
318 const_accessor<T, SIZES...> operator[](size_t index) {
319 return const_accessor<T, SIZES...>(
320 &mBase[index * product<SIZES...>::value]);
321 }
322
323 private:
324 const T *mBase;
325 };
326
327 template<typename T, size_t SIZE1>
328 struct const_accessor<T, SIZE1> {
329 explicit const_accessor(const T *base)
330 : mBase(base) {
331 }
332
333 const T &operator[](size_t index) const {
334 return mBase[index];
335 }
336
337 private:
338 const T *mBase;
339 };
340
341} // namespace details
342
343////////////////////////////////////////////////////////////////////////////////
344
345template<typename T, size_t SIZE1, size_t... SIZES>
346struct hidl_array {
347 hidl_array() = default;
348
349 T *data() { return mBuffer; }
350 const T *data() const { return mBuffer; }
351
352 details::accessor<T, SIZES...> operator[](size_t index) {
353 return details::accessor<T, SIZES...>(
354 &mBuffer[index * details::product<SIZES...>::value]);
355 }
356
357 details::const_accessor<T, SIZES...> operator[](size_t index) const {
358 return details::const_accessor<T, SIZES...>(
359 &mBuffer[index * details::product<SIZES...>::value]);
360 }
361
Andreas Huber00a985c2016-09-28 14:24:53 -0700362 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
363
364 static constexpr size_tuple_type size() {
365 return std::make_tuple(SIZE1, SIZES...);
366 }
367
Andreas Huber20dce082016-09-22 19:39:13 -0700368private:
369 T mBuffer[details::product<SIZE1, SIZES...>::value];
370};
371
372template<typename T, size_t SIZE1>
373struct hidl_array<T, SIZE1> {
374 hidl_array() = default;
375
376 T *data() { return mBuffer; }
377 const T *data() const { return mBuffer; }
378
379 T &operator[](size_t index) {
380 return mBuffer[index];
381 }
382
383 const T &operator[](size_t index) const {
384 return mBuffer[index];
385 }
386
Andreas Huber00a985c2016-09-28 14:24:53 -0700387 static constexpr size_t size() { return SIZE1; }
388
Andreas Huber20dce082016-09-22 19:39:13 -0700389private:
390 T mBuffer[SIZE1];
391};
392
Martijn Coenen72110162016-08-19 14:28:25 +0200393// ----------------------------------------------------------------------
394// Version functions
395struct hidl_version {
396public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800397 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200398
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700399 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200400 return (mMajor == other.get_major() && mMinor == other.get_minor());
401 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200402
Martijn Coenen097a7672016-09-08 16:56:41 +0200403 constexpr uint16_t get_major() const { return mMajor; }
404 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200405
Martijn Coenen72110162016-08-19 14:28:25 +0200406private:
407 uint16_t mMajor;
408 uint16_t mMinor;
409};
410
411inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
412 return hidl_version(major,minor);
413}
414
Yifan Hongc1a60ef2016-11-08 18:53:16 -0800415struct IBase : virtual public RefBase {
Yifan Honga3c31842016-10-21 10:33:14 -0700416 virtual bool isRemote() const = 0;
417 // HIDL reserved methods follow.
418 virtual ::android::hardware::Return<void> interfaceChain(
419 std::function<void(const hidl_vec<hidl_string>&)> _hidl_cb) = 0;
420 // descriptor for HIDL reserved methods.
Steven Moreland5a682322016-11-11 12:32:50 -0800421 static const char* descriptor;
Yifan Honga3c31842016-10-21 10:33:14 -0700422};
423
Steven Morelandbdf26662016-09-02 11:03:15 -0700424#if defined(__LP64__)
425#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
426#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
427#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
428#else
429#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
430#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
431#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
432#endif
433
Steven Moreland40ede2c2016-11-09 13:51:53 -0800434#define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200435 static ::android::sp<I##INTERFACE> getService( \
Iliyan Malchevb42ce262016-09-26 00:09:35 -0700436 const std::string &serviceName, bool getStub=false); \
Steven Moreland40ede2c2016-11-09 13:51:53 -0800437 ::android::status_t registerAsService(const std::string &serviceName); \
438 static bool registerForNotifications( \
439 const std::string &serviceName, \
440 const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \
441 &notification); \
Martijn Coenenc28f1152016-08-22 14:06:56 +0200442
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700443// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700444// Class that provides Hidl instrumentation utilities.
445struct HidlInstrumentor {
446 // Event that triggers the instrumentation. e.g. enter of an API call on
447 // the server/client side, exit of an API call on the server/client side
448 // etc.
449 enum InstrumentationEvent {
450 SERVER_API_ENTRY = 0,
451 SERVER_API_EXIT,
452 CLIENT_API_ENTRY,
453 CLIENT_API_EXIT,
454 SYNC_CALLBACK_ENTRY,
455 SYNC_CALLBACK_EXIT,
456 ASYNC_CALLBACK_ENTRY,
457 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700458 PASSTHROUGH_ENTRY,
459 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700460 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700461
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700462 // Signature of the instrumentation callback function.
463 using InstrumentationCallback = std::function<void(
464 const InstrumentationEvent event,
465 const char *package,
466 const char *version,
467 const char *interface,
468 const char *method,
469 std::vector<void *> *args)>;
470
471 explicit HidlInstrumentor(const std::string &prefix);
472 virtual ~HidlInstrumentor();
473
474 protected:
475 // Function that lookup and dynamically loads the hidl instrumentation
476 // libraries and registers the instrumentation callback functions.
477 //
478 // The instrumentation libraries should be stored under any of the following
479 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
480 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
481 // follow pattern: ^profilerPrefix(.*).profiler.so$
482 //
483 // Each instrumentation library is expected to implement the instrumentation
484 // function called HIDL_INSTRUMENTATION_FUNCTION.
485 //
486 // A no-op for user build.
487 void registerInstrumentationCallbacks(
488 const std::string &profilerPrefix,
489 std::vector<InstrumentationCallback> *instrumentationCallbacks);
490
491 // Utility function to determine whether a give file is a instrumentation
492 // library (i.e. the file name follow the expected pattern).
493 bool isInstrumentationLib(
494 const std::string &profilerPrefix,
495 const dirent *file);
496 // A list of registered instrumentation callbacks.
497 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
498 // Flag whether to enable instrumentation.
499 bool mEnableInstrumentation;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700500};
501
Martijn Coenen72110162016-08-19 14:28:25 +0200502} // namespace hardware
503} // namespace android
504
Martijn Coenenc28f1152016-08-22 14:06:56 +0200505
Martijn Coenen72110162016-08-19 14:28:25 +0200506#endif // ANDROID_HIDL_SUPPORT_H
507