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> |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 21 | #include <array> |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 22 | #include <dirent.h> |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 23 | #include <dlfcn.h> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 24 | #include <iterator> |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 25 | #include <cutils/native_handle.h> |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 27 | #include <functional> |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 28 | #include <hidl/HidlInternal.h> |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 29 | #include <hidl/Status.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 30 | #include <map> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 31 | #include <stddef.h> |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 32 | #include <tuple> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 33 | #include <type_traits> |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 34 | #include <utils/Errors.h> |
| 35 | #include <utils/RefBase.h> |
| 36 | #include <utils/StrongPointer.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 37 | #include <vector> |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 38 | |
| 39 | namespace android { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 40 | |
| 41 | // this file is included by all hidl interface, so we must forward declare the IMemory type. |
| 42 | namespace hidl { |
| 43 | namespace memory { |
| 44 | namespace V1_0 { |
| 45 | struct IMemory; |
| 46 | }; // namespace V1_0 |
| 47 | }; // namespace manager |
| 48 | }; // namespace hidl |
| 49 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 50 | namespace hardware { |
| 51 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 52 | // hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer, |
| 53 | // so that it can safely be transferred between 32-bit and 64-bit processes. |
| 54 | struct hidl_handle { |
| 55 | hidl_handle() { |
| 56 | mHandle = nullptr; |
| 57 | } |
| 58 | ~hidl_handle() { |
| 59 | } |
| 60 | |
| 61 | // copy constructors. |
| 62 | hidl_handle(const native_handle_t *handle) { |
| 63 | mHandle = handle; |
| 64 | } |
| 65 | |
| 66 | hidl_handle(const hidl_handle &other) { |
| 67 | mHandle = other.mHandle; |
| 68 | } |
| 69 | |
| 70 | // move constructor. |
| 71 | hidl_handle(hidl_handle &&other) { |
| 72 | *this = std::move(other); |
| 73 | } |
| 74 | |
| 75 | // assingment operators |
| 76 | hidl_handle &operator=(const hidl_handle &other) { |
| 77 | mHandle = other.mHandle; |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | hidl_handle &operator=(const native_handle_t *native_handle) { |
| 82 | mHandle = native_handle; |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | hidl_handle &operator=(hidl_handle &&other) { |
| 87 | mHandle = other.mHandle; |
| 88 | other.mHandle = nullptr; |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | const native_handle_t* operator->() const { |
| 93 | return mHandle; |
| 94 | } |
| 95 | // implicit conversion to const native_handle_t* |
| 96 | operator const native_handle_t *() const { |
| 97 | return mHandle; |
| 98 | } |
| 99 | // explicit conversion |
| 100 | const native_handle_t *getNativeHandle() const { |
| 101 | return mHandle; |
| 102 | } |
| 103 | private: |
| 104 | details::hidl_pointer<const native_handle_t> mHandle; |
| 105 | }; |
| 106 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 107 | struct hidl_string { |
| 108 | hidl_string(); |
| 109 | ~hidl_string(); |
| 110 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 111 | // copy constructor. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 112 | hidl_string(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 113 | // copy from a C-style string. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 114 | hidl_string(const char *); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 115 | // copy from an std::string. |
| 116 | hidl_string(const std::string &); |
| 117 | |
| 118 | // move constructor. |
| 119 | hidl_string(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 120 | |
| 121 | const char *c_str() const; |
| 122 | size_t size() const; |
| 123 | bool empty() const; |
| 124 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 125 | // copy assignment operator. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 126 | hidl_string &operator=(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 127 | // copy from a C-style string. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 128 | hidl_string &operator=(const char *s); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 129 | // copy from an std::string. |
| 130 | hidl_string &operator=(const std::string &); |
| 131 | // move assignment operator. |
| 132 | hidl_string &operator=(hidl_string &&other); |
| 133 | // cast to std::string. |
| 134 | operator std::string() const; |
| 135 | // cast to C-style string. Caller is responsible |
| 136 | // to maintain this hidl_string alive. |
| 137 | operator const char *() const; |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 138 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 139 | void clear(); |
| 140 | |
| 141 | // Reference an external char array. Ownership is _not_ transferred. |
| 142 | // Caller is responsible for ensuring that underlying memory is valid |
| 143 | // for the lifetime of this hidl_string. |
| 144 | void setToExternal(const char *data, size_t size); |
| 145 | |
Andreas Huber | ebfeb36 | 2016-08-25 13:39:05 -0700 | [diff] [blame] | 146 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 147 | static const size_t kOffsetOfBuffer; |
| 148 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 149 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 150 | details::hidl_pointer<const char> mBuffer; |
| 151 | uint32_t mSize; // NOT including the terminating '\0'. |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 152 | bool mOwnsBuffer; // if true then mBuffer is a mutable char * |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 153 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 154 | // copy from data with size. Assume that my memory is freed |
| 155 | // (through clear(), for example) |
| 156 | void copyFrom(const char *data, size_t size); |
| 157 | // move from another hidl_string |
| 158 | void moveFrom(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 159 | }; |
| 160 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 161 | inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) { |
| 162 | return strcmp(hs1.c_str(), hs2.c_str()) == 0; |
| 163 | } |
| 164 | |
| 165 | inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) { |
| 166 | return !(hs1 == hs2); |
| 167 | } |
| 168 | |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 169 | inline bool operator==(const hidl_string &hs, const char *s) { |
| 170 | return strcmp(hs.c_str(), s) == 0; |
| 171 | } |
| 172 | |
| 173 | inline bool operator!=(const hidl_string &hs, const char *s) { |
| 174 | return !(hs == s); |
| 175 | } |
| 176 | |
| 177 | inline bool operator==(const char *s, const hidl_string &hs) { |
| 178 | return strcmp(hs.c_str(), s) == 0; |
| 179 | } |
| 180 | |
| 181 | inline bool operator!=(const char *s, const hidl_string &hs) { |
| 182 | return !(s == hs); |
| 183 | } |
| 184 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 185 | // hidl_memory is a structure that can be used to transfer |
| 186 | // pieces of shared memory between processes. The assumption |
| 187 | // of this object is that the memory remains accessible as |
| 188 | // long as the file descriptors in the enclosed mHandle |
| 189 | // - as well as all of its cross-process dups() - remain opened. |
| 190 | struct hidl_memory { |
| 191 | |
| 192 | hidl_memory() : mOwnsHandle(false), mHandle(nullptr), mSize(0), mName("") { |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Creates a hidl_memory object and takes ownership of the handle. |
| 197 | */ |
| 198 | hidl_memory(const hidl_string &name, const hidl_handle &handle, size_t size) |
| 199 | : mOwnsHandle(true), |
| 200 | mHandle(handle), |
| 201 | mSize(size), |
| 202 | mName(name) |
| 203 | {} |
| 204 | |
| 205 | // copy constructor |
| 206 | hidl_memory(const hidl_memory& other) { |
| 207 | *this = other; |
| 208 | } |
| 209 | |
| 210 | // copy assignment |
| 211 | hidl_memory &operator=(const hidl_memory &other) { |
| 212 | if (this != &other) { |
| 213 | mOwnsHandle = true; |
| 214 | mHandle = native_handle_clone(other.mHandle); |
| 215 | mSize = other.mSize; |
| 216 | mName = other.mName; |
| 217 | } |
| 218 | |
| 219 | return *this; |
| 220 | } |
| 221 | |
| 222 | // TODO move constructor/move assignment |
| 223 | |
| 224 | ~hidl_memory() { |
| 225 | // TODO if we had previously mapped from this object, unmap |
| 226 | if (mOwnsHandle) { |
| 227 | native_handle_close(mHandle); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | const native_handle_t* handle() const { |
| 232 | return mHandle; |
| 233 | } |
| 234 | |
| 235 | const hidl_string &name() const { |
| 236 | return mName; |
| 237 | } |
| 238 | |
| 239 | size_t size() const { |
| 240 | return mSize; |
| 241 | } |
| 242 | |
| 243 | // offsetof(hidl_memory, mHandle) exposed since mHandle is private. |
| 244 | static const size_t kOffsetOfHandle; |
| 245 | // offsetof(hidl_memory, mName) exposed since mHandle is private. |
| 246 | static const size_t kOffsetOfName; |
| 247 | private: |
| 248 | bool mOwnsHandle; |
| 249 | hidl_handle mHandle; |
| 250 | size_t mSize; |
| 251 | hidl_string mName; |
| 252 | }; |
| 253 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 254 | //////////////////////////////////////////////////////////////////////////////// |
| 255 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 256 | template<typename T> |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 257 | struct hidl_vec : private details::hidl_log_base { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 258 | hidl_vec() |
| 259 | : mBuffer(NULL), |
| 260 | mSize(0), |
| 261 | mOwnsBuffer(true) { |
| 262 | } |
| 263 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 264 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 265 | *this = other; |
| 266 | } |
| 267 | |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 268 | hidl_vec(hidl_vec<T> &&other) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 269 | : mOwnsBuffer(false) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 270 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 273 | hidl_vec(const std::initializer_list<T> list) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 274 | : mSize(list.size()), |
| 275 | mOwnsBuffer(true) { |
| 276 | mBuffer = new T[mSize]; |
| 277 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 278 | size_t idx = 0; |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 279 | for (auto it = list.begin(); it != list.end(); ++it) { |
| 280 | mBuffer[idx++] = *it; |
| 281 | } |
| 282 | } |
| 283 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 284 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 285 | *this = other; |
| 286 | } |
| 287 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 288 | ~hidl_vec() { |
| 289 | if (mOwnsBuffer) { |
| 290 | delete[] mBuffer; |
| 291 | } |
| 292 | mBuffer = NULL; |
| 293 | } |
| 294 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 295 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 296 | // caller's responsibility to ensure that the underlying memory stays |
| 297 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 298 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 299 | if (mOwnsBuffer) { |
| 300 | delete [] mBuffer; |
| 301 | } |
| 302 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 303 | if (size > UINT32_MAX) { |
| 304 | logAlwaysFatal("external vector size exceeds 2^32 elements."); |
| 305 | } |
| 306 | mSize = static_cast<uint32_t>(size); |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 307 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 310 | T *data() { |
| 311 | return mBuffer; |
| 312 | } |
| 313 | |
| 314 | const T *data() const { |
| 315 | return mBuffer; |
| 316 | } |
| 317 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 318 | T *releaseData() { |
| 319 | if (!mOwnsBuffer && mSize > 0) { |
| 320 | resize(mSize); |
| 321 | } |
| 322 | mOwnsBuffer = false; |
| 323 | return mBuffer; |
| 324 | } |
| 325 | |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 326 | hidl_vec &operator=(hidl_vec &&other) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 327 | if (mOwnsBuffer) { |
| 328 | delete[] mBuffer; |
| 329 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 330 | mBuffer = other.mBuffer; |
| 331 | mSize = other.mSize; |
| 332 | mOwnsBuffer = other.mOwnsBuffer; |
| 333 | other.mOwnsBuffer = false; |
| 334 | return *this; |
| 335 | } |
| 336 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 337 | hidl_vec &operator=(const hidl_vec &other) { |
| 338 | if (this != &other) { |
| 339 | if (mOwnsBuffer) { |
| 340 | delete[] mBuffer; |
| 341 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 342 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | return *this; |
| 346 | } |
| 347 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 348 | // copy from an std::vector. |
| 349 | hidl_vec &operator=(const std::vector<T> &other) { |
| 350 | if (mOwnsBuffer) { |
| 351 | delete[] mBuffer; |
| 352 | } |
| 353 | copyFrom(other, other.size()); |
| 354 | return *this; |
| 355 | } |
| 356 | |
| 357 | // cast to an std::vector. |
| 358 | operator std::vector<T>() const { |
| 359 | std::vector<T> v(mSize); |
| 360 | for (size_t i = 0; i < mSize; ++i) { |
| 361 | v[i] = mBuffer[i]; |
| 362 | } |
| 363 | return v; |
| 364 | } |
| 365 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 366 | size_t size() const { |
| 367 | return mSize; |
| 368 | } |
| 369 | |
| 370 | T &operator[](size_t index) { |
| 371 | return mBuffer[index]; |
| 372 | } |
| 373 | |
| 374 | const T &operator[](size_t index) const { |
| 375 | return mBuffer[index]; |
| 376 | } |
| 377 | |
| 378 | void resize(size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 379 | if (size > UINT32_MAX) { |
| 380 | logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
| 381 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 382 | T *newBuffer = new T[size]; |
| 383 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 384 | for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 385 | newBuffer[i] = mBuffer[i]; |
| 386 | } |
| 387 | |
| 388 | if (mOwnsBuffer) { |
| 389 | delete[] mBuffer; |
| 390 | } |
| 391 | mBuffer = newBuffer; |
| 392 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 393 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 394 | mOwnsBuffer = true; |
| 395 | } |
| 396 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 397 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 398 | static const size_t kOffsetOfBuffer; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 399 | |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 400 | private: |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 401 | // Define std interator interface for walking the array contents |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 402 | template<bool is_const> |
| 403 | class iter : public std::iterator< |
| 404 | std::random_access_iterator_tag, /* Category */ |
| 405 | T, |
| 406 | ptrdiff_t, /* Distance */ |
| 407 | typename std::conditional<is_const, const T *, T *>::type /* Pointer */, |
| 408 | typename std::conditional<is_const, const T &, T &>::type /* Reference */> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 409 | { |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 410 | using traits = std::iterator_traits<iter>; |
| 411 | using ptr_type = typename traits::pointer; |
| 412 | using ref_type = typename traits::reference; |
| 413 | using diff_type = typename traits::difference_type; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 414 | public: |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 415 | iter(ptr_type ptr) : mPtr(ptr) { } |
| 416 | inline iter &operator++() { mPtr++; return *this; } |
| 417 | inline iter operator++(int) { iter i = *this; mPtr++; return i; } |
| 418 | inline iter &operator--() { mPtr--; return *this; } |
| 419 | inline iter operator--(int) { iter i = *this; mPtr--; return i; } |
| 420 | inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; } |
| 421 | inline iter operator+(diff_type n) const { return mPtr + n; } |
| 422 | inline iter operator-(diff_type n) const { return mPtr - n; } |
| 423 | inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; } |
| 424 | inline iter &operator+=(diff_type n) { mPtr += n; return *this; } |
| 425 | inline iter &operator-=(diff_type n) { mPtr -= n; return *this; } |
| 426 | inline ref_type operator*() const { return *mPtr; } |
| 427 | inline ptr_type operator->() const { return mPtr; } |
| 428 | inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; } |
| 429 | inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; } |
| 430 | inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; } |
| 431 | inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; } |
| 432 | inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; } |
| 433 | inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; } |
| 434 | inline ref_type operator[](size_t n) const { return mPtr[n]; } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 435 | private: |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 436 | ptr_type mPtr; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 437 | }; |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 438 | public: |
| 439 | using iterator = iter<false /* is_const */>; |
| 440 | using const_iterator = iter<true /* is_const */>; |
| 441 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 442 | iterator begin() { return data(); } |
| 443 | iterator end() { return data()+mSize; } |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 444 | const_iterator begin() const { return data(); } |
| 445 | const_iterator end() const { return data()+mSize; } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 446 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 447 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 448 | details::hidl_pointer<T> mBuffer; |
| 449 | uint32_t mSize; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 450 | bool mOwnsBuffer; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 451 | |
| 452 | // copy from an array-like object, assuming my resources are freed. |
| 453 | template <typename Array> |
| 454 | void copyFrom(const Array &data, size_t size) { |
Chia-I Wu | 4b48edc | 2016-12-07 22:31:17 +0800 | [diff] [blame] | 455 | mSize = static_cast<uint32_t>(size); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 456 | mOwnsBuffer = true; |
| 457 | if (mSize > 0) { |
| 458 | mBuffer = new T[size]; |
| 459 | for (size_t i = 0; i < size; ++i) { |
| 460 | mBuffer[i] = data[i]; |
| 461 | } |
| 462 | } else { |
| 463 | mBuffer = NULL; |
| 464 | } |
| 465 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 466 | }; |
| 467 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 468 | template <typename T> |
| 469 | const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer); |
| 470 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 471 | //////////////////////////////////////////////////////////////////////////////// |
| 472 | |
| 473 | namespace details { |
| 474 | |
| 475 | template<size_t SIZE1, size_t... SIZES> |
| 476 | struct product { |
| 477 | static constexpr size_t value = SIZE1 * product<SIZES...>::value; |
| 478 | }; |
| 479 | |
| 480 | template<size_t SIZE1> |
| 481 | struct product<SIZE1> { |
| 482 | static constexpr size_t value = SIZE1; |
| 483 | }; |
| 484 | |
| 485 | template<typename T, size_t SIZE1, size_t... SIZES> |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 486 | struct std_array { |
| 487 | using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>; |
| 488 | }; |
| 489 | |
| 490 | template<typename T, size_t SIZE1> |
| 491 | struct std_array<T, SIZE1> { |
| 492 | using type = std::array<T, SIZE1>; |
| 493 | }; |
| 494 | |
| 495 | template<typename T, size_t SIZE1, size_t... SIZES> |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 496 | struct accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 497 | |
| 498 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 499 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 500 | explicit accessor(T *base) |
| 501 | : mBase(base) { |
| 502 | } |
| 503 | |
| 504 | accessor<T, SIZES...> operator[](size_t index) { |
| 505 | return accessor<T, SIZES...>( |
| 506 | &mBase[index * product<SIZES...>::value]); |
| 507 | } |
| 508 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 509 | accessor &operator=(const std_array_type &other) { |
| 510 | for (size_t i = 0; i < SIZE1; ++i) { |
| 511 | (*this)[i] = other[i]; |
| 512 | } |
| 513 | return *this; |
| 514 | } |
| 515 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 516 | private: |
| 517 | T *mBase; |
| 518 | }; |
| 519 | |
| 520 | template<typename T, size_t SIZE1> |
| 521 | struct accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 522 | |
| 523 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 524 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 525 | explicit accessor(T *base) |
| 526 | : mBase(base) { |
| 527 | } |
| 528 | |
| 529 | T &operator[](size_t index) { |
| 530 | return mBase[index]; |
| 531 | } |
| 532 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 533 | accessor &operator=(const std_array_type &other) { |
| 534 | for (size_t i = 0; i < SIZE1; ++i) { |
| 535 | (*this)[i] = other[i]; |
| 536 | } |
| 537 | return *this; |
| 538 | } |
| 539 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 540 | private: |
| 541 | T *mBase; |
| 542 | }; |
| 543 | |
| 544 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 545 | struct const_accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 546 | |
| 547 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 548 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 549 | explicit const_accessor(const T *base) |
| 550 | : mBase(base) { |
| 551 | } |
| 552 | |
| 553 | const_accessor<T, SIZES...> operator[](size_t index) { |
| 554 | return const_accessor<T, SIZES...>( |
| 555 | &mBase[index * product<SIZES...>::value]); |
| 556 | } |
| 557 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 558 | operator std_array_type() { |
| 559 | std_array_type array; |
| 560 | for (size_t i = 0; i < SIZE1; ++i) { |
| 561 | array[i] = (*this)[i]; |
| 562 | } |
| 563 | return array; |
| 564 | } |
| 565 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 566 | private: |
| 567 | const T *mBase; |
| 568 | }; |
| 569 | |
| 570 | template<typename T, size_t SIZE1> |
| 571 | struct const_accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 572 | |
| 573 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 574 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 575 | explicit const_accessor(const T *base) |
| 576 | : mBase(base) { |
| 577 | } |
| 578 | |
| 579 | const T &operator[](size_t index) const { |
| 580 | return mBase[index]; |
| 581 | } |
| 582 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 583 | operator std_array_type() { |
| 584 | std_array_type array; |
| 585 | for (size_t i = 0; i < SIZE1; ++i) { |
| 586 | array[i] = (*this)[i]; |
| 587 | } |
| 588 | return array; |
| 589 | } |
| 590 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 591 | private: |
| 592 | const T *mBase; |
| 593 | }; |
| 594 | |
| 595 | } // namespace details |
| 596 | |
| 597 | //////////////////////////////////////////////////////////////////////////////// |
| 598 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 599 | // A multidimensional array of T's. Assumes that T::operator=(const T &) is defined. |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 600 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 601 | struct hidl_array { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 602 | |
| 603 | using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type; |
| 604 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 605 | hidl_array() = default; |
| 606 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 607 | // Copies the data from source, using T::operator=(const T &). |
| 608 | hidl_array(const T *source) { |
| 609 | for (size_t i = 0; i < elementCount(); ++i) { |
| 610 | mBuffer[i] = source[i]; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 615 | hidl_array(const std_array_type &array) { |
| 616 | details::accessor<T, SIZE1, SIZES...> modifier(mBuffer); |
| 617 | modifier = array; |
| 618 | } |
| 619 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 620 | T *data() { return mBuffer; } |
| 621 | const T *data() const { return mBuffer; } |
| 622 | |
| 623 | details::accessor<T, SIZES...> operator[](size_t index) { |
| 624 | return details::accessor<T, SIZES...>( |
| 625 | &mBuffer[index * details::product<SIZES...>::value]); |
| 626 | } |
| 627 | |
| 628 | details::const_accessor<T, SIZES...> operator[](size_t index) const { |
| 629 | return details::const_accessor<T, SIZES...>( |
| 630 | &mBuffer[index * details::product<SIZES...>::value]); |
| 631 | } |
| 632 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 633 | using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>; |
| 634 | |
| 635 | static constexpr size_tuple_type size() { |
| 636 | return std::make_tuple(SIZE1, SIZES...); |
| 637 | } |
| 638 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 639 | static constexpr size_t elementCount() { |
| 640 | return details::product<SIZE1, SIZES...>::value; |
| 641 | } |
| 642 | |
| 643 | operator std_array_type() const { |
| 644 | return details::const_accessor<T, SIZE1, SIZES...>(mBuffer); |
| 645 | } |
| 646 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 647 | private: |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 648 | T mBuffer[elementCount()]; |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 649 | }; |
| 650 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 651 | // An array of T's. Assumes that T::operator=(const T &) is defined. |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 652 | template<typename T, size_t SIZE1> |
| 653 | struct hidl_array<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 654 | |
| 655 | using std_array_type = typename details::std_array<T, SIZE1>::type; |
| 656 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 657 | hidl_array() = default; |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 658 | |
| 659 | // Copies the data from source, using T::operator=(const T &). |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 660 | hidl_array(const T *source) { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 661 | for (size_t i = 0; i < elementCount(); ++i) { |
| 662 | mBuffer[i] = source[i]; |
| 663 | } |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 664 | } |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 665 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 666 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 667 | hidl_array(const std_array_type &array) : hidl_array(array.data()) {} |
| 668 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 669 | T *data() { return mBuffer; } |
| 670 | const T *data() const { return mBuffer; } |
| 671 | |
| 672 | T &operator[](size_t index) { |
| 673 | return mBuffer[index]; |
| 674 | } |
| 675 | |
| 676 | const T &operator[](size_t index) const { |
| 677 | return mBuffer[index]; |
| 678 | } |
| 679 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 680 | static constexpr size_t size() { return SIZE1; } |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 681 | static constexpr size_t elementCount() { return SIZE1; } |
| 682 | |
| 683 | // Copies the data to an std::array, using T::operator=(T). |
| 684 | operator std_array_type() const { |
| 685 | std_array_type array; |
| 686 | for (size_t i = 0; i < SIZE1; ++i) { |
| 687 | array[i] = mBuffer[i]; |
| 688 | } |
| 689 | return array; |
| 690 | } |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 691 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 692 | private: |
| 693 | T mBuffer[SIZE1]; |
| 694 | }; |
| 695 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 696 | // ---------------------------------------------------------------------- |
| 697 | // Version functions |
| 698 | struct hidl_version { |
| 699 | public: |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 700 | 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] | 701 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 702 | bool operator==(const hidl_version& other) const { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 703 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 704 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 705 | |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame^] | 706 | bool operator<(const hidl_version& other) const { |
| 707 | return (mMajor < other.get_major() || |
| 708 | (mMajor == other.get_major() && mMinor < other.get_minor())); |
| 709 | } |
| 710 | |
| 711 | bool operator>(const hidl_version& other) const { |
| 712 | return other < *this; |
| 713 | } |
| 714 | |
| 715 | bool operator<=(const hidl_version& other) const { |
| 716 | return !(*this > other); |
| 717 | } |
| 718 | |
| 719 | bool operator>=(const hidl_version& other) const { |
| 720 | return !(*this < other); |
| 721 | } |
| 722 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 723 | constexpr uint16_t get_major() const { return mMajor; } |
| 724 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 725 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 726 | private: |
| 727 | uint16_t mMajor; |
| 728 | uint16_t mMinor; |
| 729 | }; |
| 730 | |
| 731 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 732 | return hidl_version(major,minor); |
| 733 | } |
| 734 | |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 735 | #if defined(__LP64__) |
| 736 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/" |
| 737 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/" |
| 738 | #define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/" |
| 739 | #else |
| 740 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/" |
| 741 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/" |
| 742 | #define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/" |
| 743 | #endif |
| 744 | |
Steven Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 745 | #define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 746 | static ::android::sp<I##INTERFACE> getService( \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 747 | const std::string &serviceName, bool getStub=false); \ |
Steven Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 748 | ::android::status_t registerAsService(const std::string &serviceName); \ |
| 749 | static bool registerForNotifications( \ |
| 750 | const std::string &serviceName, \ |
| 751 | const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \ |
| 752 | ¬ification); \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 753 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 754 | // ---------------------------------------------------------------------- |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 755 | // Class that provides Hidl instrumentation utilities. |
| 756 | struct HidlInstrumentor { |
| 757 | // Event that triggers the instrumentation. e.g. enter of an API call on |
| 758 | // the server/client side, exit of an API call on the server/client side |
| 759 | // etc. |
| 760 | enum InstrumentationEvent { |
| 761 | SERVER_API_ENTRY = 0, |
| 762 | SERVER_API_EXIT, |
| 763 | CLIENT_API_ENTRY, |
| 764 | CLIENT_API_EXIT, |
| 765 | SYNC_CALLBACK_ENTRY, |
| 766 | SYNC_CALLBACK_EXIT, |
| 767 | ASYNC_CALLBACK_ENTRY, |
| 768 | ASYNC_CALLBACK_EXIT, |
Steven Moreland | cefbd6e | 2016-11-01 10:14:30 -0700 | [diff] [blame] | 769 | PASSTHROUGH_ENTRY, |
| 770 | PASSTHROUGH_EXIT, |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 771 | }; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 772 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 773 | // Signature of the instrumentation callback function. |
| 774 | using InstrumentationCallback = std::function<void( |
| 775 | const InstrumentationEvent event, |
| 776 | const char *package, |
| 777 | const char *version, |
| 778 | const char *interface, |
| 779 | const char *method, |
| 780 | std::vector<void *> *args)>; |
| 781 | |
| 782 | explicit HidlInstrumentor(const std::string &prefix); |
| 783 | virtual ~HidlInstrumentor(); |
| 784 | |
| 785 | protected: |
| 786 | // Function that lookup and dynamically loads the hidl instrumentation |
| 787 | // libraries and registers the instrumentation callback functions. |
| 788 | // |
| 789 | // The instrumentation libraries should be stored under any of the following |
| 790 | // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and |
| 791 | // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should |
| 792 | // follow pattern: ^profilerPrefix(.*).profiler.so$ |
| 793 | // |
| 794 | // Each instrumentation library is expected to implement the instrumentation |
| 795 | // function called HIDL_INSTRUMENTATION_FUNCTION. |
| 796 | // |
| 797 | // A no-op for user build. |
| 798 | void registerInstrumentationCallbacks( |
| 799 | const std::string &profilerPrefix, |
| 800 | std::vector<InstrumentationCallback> *instrumentationCallbacks); |
| 801 | |
| 802 | // Utility function to determine whether a give file is a instrumentation |
| 803 | // library (i.e. the file name follow the expected pattern). |
| 804 | bool isInstrumentationLib( |
| 805 | const std::string &profilerPrefix, |
| 806 | const dirent *file); |
| 807 | // A list of registered instrumentation callbacks. |
| 808 | std::vector<InstrumentationCallback> mInstrumentationCallbacks; |
| 809 | // Flag whether to enable instrumentation. |
| 810 | bool mEnableInstrumentation; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 811 | }; |
| 812 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 813 | } // namespace hardware |
| 814 | } // namespace android |
| 815 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 816 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 817 | #endif // ANDROID_HIDL_SUPPORT_H |