Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 1 | /* |
| 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 Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 21 | #include <dirent.h> |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 22 | #include <dlfcn.h> |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 24 | #include <functional> |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 25 | #include <hidl/Status.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 26 | #include <map> |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 27 | #include <tuple> |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 28 | #include <utils/Errors.h> |
| 29 | #include <utils/RefBase.h> |
| 30 | #include <utils/StrongPointer.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame^] | 31 | #include <vector> |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace hardware { |
| 35 | |
| 36 | struct hidl_string { |
| 37 | hidl_string(); |
| 38 | ~hidl_string(); |
| 39 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 40 | // copy constructor. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 41 | hidl_string(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 42 | // copy from a C-style string. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 43 | hidl_string(const char *); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 44 | // copy from an std::string. |
| 45 | hidl_string(const std::string &); |
| 46 | |
| 47 | // move constructor. |
| 48 | hidl_string(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 49 | |
| 50 | const char *c_str() const; |
| 51 | size_t size() const; |
| 52 | bool empty() const; |
| 53 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 54 | // copy assignment operator. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 55 | hidl_string &operator=(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 56 | // copy from a C-style string. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 57 | hidl_string &operator=(const char *s); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 58 | // 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 Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 67 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 68 | 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 Huber | ebfeb36 | 2016-08-25 13:39:05 -0700 | [diff] [blame] | 75 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 76 | static const size_t kOffsetOfBuffer; |
| 77 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 78 | private: |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 79 | const char *mBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 80 | size_t mSize; // NOT including the terminating '\0'. |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 81 | bool mOwnsBuffer; // if true then mBuffer is a mutable char * |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 82 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 83 | // 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 Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 88 | }; |
| 89 | |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 90 | inline bool operator==(const hidl_string &hs, const char *s) { |
| 91 | return strcmp(hs.c_str(), s) == 0; |
| 92 | } |
| 93 | |
| 94 | inline bool operator!=(const hidl_string &hs, const char *s) { |
| 95 | return !(hs == s); |
| 96 | } |
| 97 | |
| 98 | inline bool operator==(const char *s, const hidl_string &hs) { |
| 99 | return strcmp(hs.c_str(), s) == 0; |
| 100 | } |
| 101 | |
| 102 | inline bool operator!=(const char *s, const hidl_string &hs) { |
| 103 | return !(s == hs); |
| 104 | } |
| 105 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 106 | //////////////////////////////////////////////////////////////////////////////// |
| 107 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 108 | template<typename T> |
| 109 | struct hidl_vec { |
| 110 | hidl_vec() |
| 111 | : mBuffer(NULL), |
| 112 | mSize(0), |
| 113 | mOwnsBuffer(true) { |
| 114 | } |
| 115 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 116 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 117 | *this = other; |
| 118 | } |
| 119 | |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 120 | hidl_vec(hidl_vec<T> &&other) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 121 | : mOwnsBuffer(false) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 122 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 125 | hidl_vec(const std::initializer_list<T> list) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 126 | : mSize(list.size()), |
| 127 | mOwnsBuffer(true) { |
| 128 | mBuffer = new T[mSize]; |
| 129 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 130 | size_t idx = 0; |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 131 | for (auto it = list.begin(); it != list.end(); ++it) { |
| 132 | mBuffer[idx++] = *it; |
| 133 | } |
| 134 | } |
| 135 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 136 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 137 | *this = other; |
| 138 | } |
| 139 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 140 | ~hidl_vec() { |
| 141 | if (mOwnsBuffer) { |
| 142 | delete[] mBuffer; |
| 143 | } |
| 144 | mBuffer = NULL; |
| 145 | } |
| 146 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 147 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 148 | // caller's responsibility to ensure that the underlying memory stays |
| 149 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 150 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 151 | if (mOwnsBuffer) { |
| 152 | delete [] mBuffer; |
| 153 | } |
| 154 | mBuffer = data; |
| 155 | mSize = size; |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 156 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 159 | T *data() { |
| 160 | return mBuffer; |
| 161 | } |
| 162 | |
| 163 | const T *data() const { |
| 164 | return mBuffer; |
| 165 | } |
| 166 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 167 | T *releaseData() { |
| 168 | if (!mOwnsBuffer && mSize > 0) { |
| 169 | resize(mSize); |
| 170 | } |
| 171 | mOwnsBuffer = false; |
| 172 | return mBuffer; |
| 173 | } |
| 174 | |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 175 | hidl_vec &operator=(hidl_vec &&other) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 176 | if (mOwnsBuffer) { |
| 177 | delete[] mBuffer; |
| 178 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 179 | mBuffer = other.mBuffer; |
| 180 | mSize = other.mSize; |
| 181 | mOwnsBuffer = other.mOwnsBuffer; |
| 182 | other.mOwnsBuffer = false; |
| 183 | return *this; |
| 184 | } |
| 185 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 186 | hidl_vec &operator=(const hidl_vec &other) { |
| 187 | if (this != &other) { |
| 188 | if (mOwnsBuffer) { |
| 189 | delete[] mBuffer; |
| 190 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 191 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | return *this; |
| 195 | } |
| 196 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 197 | // 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 Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 215 | 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 Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 243 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 244 | static const size_t kOffsetOfBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 245 | private: |
| 246 | T *mBuffer; |
| 247 | size_t mSize; |
| 248 | bool mOwnsBuffer; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 249 | |
| 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 Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 264 | }; |
| 265 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 266 | template <typename T> |
| 267 | const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer); |
| 268 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 269 | //////////////////////////////////////////////////////////////////////////////// |
| 270 | |
| 271 | namespace 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 | |
| 345 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 346 | struct 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 Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 362 | 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 Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 368 | private: |
| 369 | T mBuffer[details::product<SIZE1, SIZES...>::value]; |
| 370 | }; |
| 371 | |
| 372 | template<typename T, size_t SIZE1> |
| 373 | struct 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 Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 387 | static constexpr size_t size() { return SIZE1; } |
| 388 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 389 | private: |
| 390 | T mBuffer[SIZE1]; |
| 391 | }; |
| 392 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 393 | // ---------------------------------------------------------------------- |
| 394 | // Version functions |
| 395 | struct hidl_version { |
| 396 | public: |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 397 | constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {} |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 398 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 399 | bool operator==(const hidl_version& other) const { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 400 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 401 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 402 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 403 | constexpr uint16_t get_major() const { return mMajor; } |
| 404 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 405 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 406 | private: |
| 407 | uint16_t mMajor; |
| 408 | uint16_t mMinor; |
| 409 | }; |
| 410 | |
| 411 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 412 | return hidl_version(major,minor); |
| 413 | } |
| 414 | |
Yifan Hong | c1a60ef | 2016-11-08 18:53:16 -0800 | [diff] [blame] | 415 | struct IBase : virtual public RefBase { |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 416 | 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 Moreland | 5a68232 | 2016-11-11 12:32:50 -0800 | [diff] [blame] | 421 | static const char* descriptor; |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 422 | }; |
| 423 | |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 424 | #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 Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 434 | #define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 435 | static ::android::sp<I##INTERFACE> getService( \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 436 | const std::string &serviceName, bool getStub=false); \ |
Steven Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 437 | ::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 | ¬ification); \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 442 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 443 | // ---------------------------------------------------------------------- |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 444 | // Class that provides Hidl instrumentation utilities. |
| 445 | struct 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 Moreland | cefbd6e | 2016-11-01 10:14:30 -0700 | [diff] [blame] | 458 | PASSTHROUGH_ENTRY, |
| 459 | PASSTHROUGH_EXIT, |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 460 | }; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 461 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 462 | // 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 Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 500 | }; |
| 501 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 502 | } // namespace hardware |
| 503 | } // namespace android |
| 504 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 505 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 506 | #endif // ANDROID_HIDL_SUPPORT_H |
| 507 | |