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> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 23 | #include <iterator> |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame^] | 24 | #include <cutils/native_handle.h> |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 25 | #include <cutils/properties.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 26 | #include <functional> |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 27 | #include <hidl/Status.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 28 | #include <map> |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 29 | #include <tuple> |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 30 | #include <utils/Errors.h> |
| 31 | #include <utils/RefBase.h> |
| 32 | #include <utils/StrongPointer.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 33 | #include <vector> |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | namespace hardware { |
| 37 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 38 | namespace details { |
| 39 | |
| 40 | // hidl_log_base is a base class that templatized |
| 41 | // classes implemented in a header can inherit from, |
| 42 | // to avoid creating dependencies on liblog. |
| 43 | struct hidl_log_base { |
| 44 | void logAlwaysFatal(const char *message); |
| 45 | }; |
| 46 | |
| 47 | // HIDL client/server code should *NOT* use this class. |
| 48 | // |
| 49 | // hidl_pointer wraps a pointer without taking ownership, |
| 50 | // and stores it in a union with a uint64_t. This ensures |
| 51 | // that we always have enough space to store a pointer, |
| 52 | // regardless of whether we're running in a 32-bit or 64-bit |
| 53 | // process. |
| 54 | template<typename T> |
| 55 | struct hidl_pointer { |
| 56 | hidl_pointer() |
| 57 | : mPointer(nullptr) { |
| 58 | } |
| 59 | hidl_pointer(T* ptr) |
| 60 | : mPointer(ptr) { |
| 61 | } |
| 62 | hidl_pointer(const hidl_pointer<T>& other) { |
| 63 | mPointer = other.mPointer; |
| 64 | } |
| 65 | hidl_pointer(hidl_pointer<T>&& other) { |
| 66 | *this = std::move(other); |
| 67 | } |
| 68 | |
| 69 | hidl_pointer &operator=(const hidl_pointer<T>& other) { |
| 70 | mPointer = other.mPointer; |
| 71 | return *this; |
| 72 | } |
| 73 | hidl_pointer &operator=(hidl_pointer<T>&& other) { |
| 74 | mPointer = other.mPointer; |
| 75 | other.mPointer = nullptr; |
| 76 | return *this; |
| 77 | } |
| 78 | hidl_pointer &operator=(T* ptr) { |
| 79 | mPointer = ptr; |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | operator T*() const { |
| 84 | return mPointer; |
| 85 | } |
| 86 | explicit operator void*() const { // requires explicit cast to avoid ambiguity |
| 87 | return mPointer; |
| 88 | } |
| 89 | T& operator*() const { |
| 90 | return *mPointer; |
| 91 | } |
| 92 | T* operator->() const { |
| 93 | return mPointer; |
| 94 | } |
| 95 | T &operator[](size_t index) { |
| 96 | return mPointer[index]; |
| 97 | } |
| 98 | const T &operator[](size_t index) const { |
| 99 | return mPointer[index]; |
| 100 | } |
| 101 | private: |
| 102 | union { |
| 103 | T* mPointer; |
| 104 | uint64_t _pad; |
| 105 | }; |
| 106 | }; |
| 107 | } // namespace details |
| 108 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame^] | 109 | |
| 110 | // hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer, |
| 111 | // so that it can safely be transferred between 32-bit and 64-bit processes. |
| 112 | struct hidl_handle { |
| 113 | hidl_handle() { |
| 114 | mHandle = nullptr; |
| 115 | } |
| 116 | ~hidl_handle() { |
| 117 | } |
| 118 | |
| 119 | // copy constructors. |
| 120 | hidl_handle(const native_handle_t *handle) { |
| 121 | mHandle = handle; |
| 122 | } |
| 123 | |
| 124 | hidl_handle(const hidl_handle &other) { |
| 125 | mHandle = other.mHandle; |
| 126 | } |
| 127 | |
| 128 | // move constructor. |
| 129 | hidl_handle(hidl_handle &&other) { |
| 130 | *this = std::move(other); |
| 131 | } |
| 132 | |
| 133 | // assingment operators |
| 134 | hidl_handle &operator=(const hidl_handle &other) { |
| 135 | mHandle = other.mHandle; |
| 136 | return *this; |
| 137 | } |
| 138 | |
| 139 | hidl_handle &operator=(const native_handle_t *native_handle) { |
| 140 | mHandle = native_handle; |
| 141 | return *this; |
| 142 | } |
| 143 | |
| 144 | hidl_handle &operator=(hidl_handle &&other) { |
| 145 | mHandle = other.mHandle; |
| 146 | other.mHandle = nullptr; |
| 147 | return *this; |
| 148 | } |
| 149 | |
| 150 | const native_handle_t* operator->() const { |
| 151 | return mHandle; |
| 152 | } |
| 153 | // implicit conversion to const native_handle_t* |
| 154 | operator const native_handle_t *() const { |
| 155 | return mHandle; |
| 156 | } |
| 157 | // explicit conversion |
| 158 | const native_handle_t *getNativeHandle() const { |
| 159 | return mHandle; |
| 160 | } |
| 161 | private: |
| 162 | details::hidl_pointer<const native_handle_t> mHandle; |
| 163 | }; |
| 164 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 165 | struct hidl_string { |
| 166 | hidl_string(); |
| 167 | ~hidl_string(); |
| 168 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 169 | // copy constructor. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 170 | hidl_string(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 171 | // copy from a C-style string. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 172 | hidl_string(const char *); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 173 | // copy from an std::string. |
| 174 | hidl_string(const std::string &); |
| 175 | |
| 176 | // move constructor. |
| 177 | hidl_string(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 178 | |
| 179 | const char *c_str() const; |
| 180 | size_t size() const; |
| 181 | bool empty() const; |
| 182 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 183 | // copy assignment operator. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 184 | hidl_string &operator=(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 185 | // copy from a C-style string. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 186 | hidl_string &operator=(const char *s); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 187 | // copy from an std::string. |
| 188 | hidl_string &operator=(const std::string &); |
| 189 | // move assignment operator. |
| 190 | hidl_string &operator=(hidl_string &&other); |
| 191 | // cast to std::string. |
| 192 | operator std::string() const; |
| 193 | // cast to C-style string. Caller is responsible |
| 194 | // to maintain this hidl_string alive. |
| 195 | operator const char *() const; |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 196 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 197 | void clear(); |
| 198 | |
| 199 | // Reference an external char array. Ownership is _not_ transferred. |
| 200 | // Caller is responsible for ensuring that underlying memory is valid |
| 201 | // for the lifetime of this hidl_string. |
| 202 | void setToExternal(const char *data, size_t size); |
| 203 | |
Andreas Huber | ebfeb36 | 2016-08-25 13:39:05 -0700 | [diff] [blame] | 204 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 205 | static const size_t kOffsetOfBuffer; |
| 206 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 207 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 208 | details::hidl_pointer<const char> mBuffer; |
| 209 | uint32_t mSize; // NOT including the terminating '\0'. |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 210 | bool mOwnsBuffer; // if true then mBuffer is a mutable char * |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 211 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 212 | // copy from data with size. Assume that my memory is freed |
| 213 | // (through clear(), for example) |
| 214 | void copyFrom(const char *data, size_t size); |
| 215 | // move from another hidl_string |
| 216 | void moveFrom(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 217 | }; |
| 218 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 219 | inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) { |
| 220 | return strcmp(hs1.c_str(), hs2.c_str()) == 0; |
| 221 | } |
| 222 | |
| 223 | inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) { |
| 224 | return !(hs1 == hs2); |
| 225 | } |
| 226 | |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 227 | inline bool operator==(const hidl_string &hs, const char *s) { |
| 228 | return strcmp(hs.c_str(), s) == 0; |
| 229 | } |
| 230 | |
| 231 | inline bool operator!=(const hidl_string &hs, const char *s) { |
| 232 | return !(hs == s); |
| 233 | } |
| 234 | |
| 235 | inline bool operator==(const char *s, const hidl_string &hs) { |
| 236 | return strcmp(hs.c_str(), s) == 0; |
| 237 | } |
| 238 | |
| 239 | inline bool operator!=(const char *s, const hidl_string &hs) { |
| 240 | return !(s == hs); |
| 241 | } |
| 242 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 243 | //////////////////////////////////////////////////////////////////////////////// |
| 244 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 245 | template<typename T> |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 246 | struct hidl_vec : private details::hidl_log_base { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 247 | hidl_vec() |
| 248 | : mBuffer(NULL), |
| 249 | mSize(0), |
| 250 | mOwnsBuffer(true) { |
| 251 | } |
| 252 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 253 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 254 | *this = other; |
| 255 | } |
| 256 | |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 257 | hidl_vec(hidl_vec<T> &&other) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 258 | : mOwnsBuffer(false) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 259 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 262 | hidl_vec(const std::initializer_list<T> list) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 263 | : mSize(list.size()), |
| 264 | mOwnsBuffer(true) { |
| 265 | mBuffer = new T[mSize]; |
| 266 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 267 | size_t idx = 0; |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 268 | for (auto it = list.begin(); it != list.end(); ++it) { |
| 269 | mBuffer[idx++] = *it; |
| 270 | } |
| 271 | } |
| 272 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 273 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 274 | *this = other; |
| 275 | } |
| 276 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 277 | ~hidl_vec() { |
| 278 | if (mOwnsBuffer) { |
| 279 | delete[] mBuffer; |
| 280 | } |
| 281 | mBuffer = NULL; |
| 282 | } |
| 283 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 284 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 285 | // caller's responsibility to ensure that the underlying memory stays |
| 286 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 287 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 288 | if (mOwnsBuffer) { |
| 289 | delete [] mBuffer; |
| 290 | } |
| 291 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 292 | if (size > UINT32_MAX) { |
| 293 | logAlwaysFatal("external vector size exceeds 2^32 elements."); |
| 294 | } |
| 295 | mSize = static_cast<uint32_t>(size); |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 296 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 299 | T *data() { |
| 300 | return mBuffer; |
| 301 | } |
| 302 | |
| 303 | const T *data() const { |
| 304 | return mBuffer; |
| 305 | } |
| 306 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 307 | T *releaseData() { |
| 308 | if (!mOwnsBuffer && mSize > 0) { |
| 309 | resize(mSize); |
| 310 | } |
| 311 | mOwnsBuffer = false; |
| 312 | return mBuffer; |
| 313 | } |
| 314 | |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 315 | hidl_vec &operator=(hidl_vec &&other) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 316 | if (mOwnsBuffer) { |
| 317 | delete[] mBuffer; |
| 318 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 319 | mBuffer = other.mBuffer; |
| 320 | mSize = other.mSize; |
| 321 | mOwnsBuffer = other.mOwnsBuffer; |
| 322 | other.mOwnsBuffer = false; |
| 323 | return *this; |
| 324 | } |
| 325 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 326 | hidl_vec &operator=(const hidl_vec &other) { |
| 327 | if (this != &other) { |
| 328 | if (mOwnsBuffer) { |
| 329 | delete[] mBuffer; |
| 330 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 331 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | return *this; |
| 335 | } |
| 336 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 337 | // copy from an std::vector. |
| 338 | hidl_vec &operator=(const std::vector<T> &other) { |
| 339 | if (mOwnsBuffer) { |
| 340 | delete[] mBuffer; |
| 341 | } |
| 342 | copyFrom(other, other.size()); |
| 343 | return *this; |
| 344 | } |
| 345 | |
| 346 | // cast to an std::vector. |
| 347 | operator std::vector<T>() const { |
| 348 | std::vector<T> v(mSize); |
| 349 | for (size_t i = 0; i < mSize; ++i) { |
| 350 | v[i] = mBuffer[i]; |
| 351 | } |
| 352 | return v; |
| 353 | } |
| 354 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 355 | size_t size() const { |
| 356 | return mSize; |
| 357 | } |
| 358 | |
| 359 | T &operator[](size_t index) { |
| 360 | return mBuffer[index]; |
| 361 | } |
| 362 | |
| 363 | const T &operator[](size_t index) const { |
| 364 | return mBuffer[index]; |
| 365 | } |
| 366 | |
| 367 | void resize(size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 368 | if (size > UINT32_MAX) { |
| 369 | logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
| 370 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 371 | T *newBuffer = new T[size]; |
| 372 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 373 | 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] | 374 | newBuffer[i] = mBuffer[i]; |
| 375 | } |
| 376 | |
| 377 | if (mOwnsBuffer) { |
| 378 | delete[] mBuffer; |
| 379 | } |
| 380 | mBuffer = newBuffer; |
| 381 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 382 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 383 | mOwnsBuffer = true; |
| 384 | } |
| 385 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 386 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 387 | static const size_t kOffsetOfBuffer; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 388 | |
| 389 | // Define std interator interface for walking the array contents |
| 390 | // TODO: it might be nice to implement a full featured random access iterator... |
| 391 | class iterator : public std::iterator<std::bidirectional_iterator_tag, T> |
| 392 | { |
| 393 | public: |
| 394 | iterator(T* ptr) : mPtr(ptr) { } |
| 395 | iterator operator++() { iterator i = *this; mPtr++; return i; } |
| 396 | iterator operator++(int) { mPtr++; return *this; } |
| 397 | iterator operator--() { iterator i = *this; mPtr--; return i; } |
| 398 | iterator operator--(int) { mPtr--; return *this; } |
| 399 | T& operator*() { return *mPtr; } |
| 400 | T* operator->() { return mPtr; } |
| 401 | bool operator==(const iterator& rhs) const { return mPtr == rhs.mPtr; } |
| 402 | bool operator!=(const iterator& rhs) const { return mPtr != rhs.mPtr; } |
| 403 | private: |
| 404 | T* mPtr; |
| 405 | }; |
| 406 | iterator begin() { return data(); } |
| 407 | iterator end() { return data()+mSize; } |
| 408 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 409 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 410 | details::hidl_pointer<T> mBuffer; |
| 411 | uint32_t mSize; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 412 | bool mOwnsBuffer; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 413 | |
| 414 | // copy from an array-like object, assuming my resources are freed. |
| 415 | template <typename Array> |
| 416 | void copyFrom(const Array &data, size_t size) { |
| 417 | mSize = size; |
| 418 | mOwnsBuffer = true; |
| 419 | if (mSize > 0) { |
| 420 | mBuffer = new T[size]; |
| 421 | for (size_t i = 0; i < size; ++i) { |
| 422 | mBuffer[i] = data[i]; |
| 423 | } |
| 424 | } else { |
| 425 | mBuffer = NULL; |
| 426 | } |
| 427 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 428 | }; |
| 429 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 430 | template <typename T> |
| 431 | const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer); |
| 432 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 433 | //////////////////////////////////////////////////////////////////////////////// |
| 434 | |
| 435 | namespace details { |
| 436 | |
| 437 | template<size_t SIZE1, size_t... SIZES> |
| 438 | struct product { |
| 439 | static constexpr size_t value = SIZE1 * product<SIZES...>::value; |
| 440 | }; |
| 441 | |
| 442 | template<size_t SIZE1> |
| 443 | struct product<SIZE1> { |
| 444 | static constexpr size_t value = SIZE1; |
| 445 | }; |
| 446 | |
| 447 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 448 | struct accessor { |
| 449 | explicit accessor(T *base) |
| 450 | : mBase(base) { |
| 451 | } |
| 452 | |
| 453 | accessor<T, SIZES...> operator[](size_t index) { |
| 454 | return accessor<T, SIZES...>( |
| 455 | &mBase[index * product<SIZES...>::value]); |
| 456 | } |
| 457 | |
| 458 | private: |
| 459 | T *mBase; |
| 460 | }; |
| 461 | |
| 462 | template<typename T, size_t SIZE1> |
| 463 | struct accessor<T, SIZE1> { |
| 464 | explicit accessor(T *base) |
| 465 | : mBase(base) { |
| 466 | } |
| 467 | |
| 468 | T &operator[](size_t index) { |
| 469 | return mBase[index]; |
| 470 | } |
| 471 | |
| 472 | private: |
| 473 | T *mBase; |
| 474 | }; |
| 475 | |
| 476 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 477 | struct const_accessor { |
| 478 | explicit const_accessor(const T *base) |
| 479 | : mBase(base) { |
| 480 | } |
| 481 | |
| 482 | const_accessor<T, SIZES...> operator[](size_t index) { |
| 483 | return const_accessor<T, SIZES...>( |
| 484 | &mBase[index * product<SIZES...>::value]); |
| 485 | } |
| 486 | |
| 487 | private: |
| 488 | const T *mBase; |
| 489 | }; |
| 490 | |
| 491 | template<typename T, size_t SIZE1> |
| 492 | struct const_accessor<T, SIZE1> { |
| 493 | explicit const_accessor(const T *base) |
| 494 | : mBase(base) { |
| 495 | } |
| 496 | |
| 497 | const T &operator[](size_t index) const { |
| 498 | return mBase[index]; |
| 499 | } |
| 500 | |
| 501 | private: |
| 502 | const T *mBase; |
| 503 | }; |
| 504 | |
| 505 | } // namespace details |
| 506 | |
| 507 | //////////////////////////////////////////////////////////////////////////////// |
| 508 | |
| 509 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 510 | struct hidl_array { |
| 511 | hidl_array() = default; |
| 512 | |
| 513 | T *data() { return mBuffer; } |
| 514 | const T *data() const { return mBuffer; } |
| 515 | |
| 516 | details::accessor<T, SIZES...> operator[](size_t index) { |
| 517 | return details::accessor<T, SIZES...>( |
| 518 | &mBuffer[index * details::product<SIZES...>::value]); |
| 519 | } |
| 520 | |
| 521 | details::const_accessor<T, SIZES...> operator[](size_t index) const { |
| 522 | return details::const_accessor<T, SIZES...>( |
| 523 | &mBuffer[index * details::product<SIZES...>::value]); |
| 524 | } |
| 525 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 526 | using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>; |
| 527 | |
| 528 | static constexpr size_tuple_type size() { |
| 529 | return std::make_tuple(SIZE1, SIZES...); |
| 530 | } |
| 531 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 532 | private: |
| 533 | T mBuffer[details::product<SIZE1, SIZES...>::value]; |
| 534 | }; |
| 535 | |
| 536 | template<typename T, size_t SIZE1> |
| 537 | struct hidl_array<T, SIZE1> { |
| 538 | hidl_array() = default; |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 539 | hidl_array(const T *source) { |
| 540 | memcpy(mBuffer, source, SIZE1 * sizeof(T)); |
| 541 | } |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 542 | |
| 543 | T *data() { return mBuffer; } |
| 544 | const T *data() const { return mBuffer; } |
| 545 | |
| 546 | T &operator[](size_t index) { |
| 547 | return mBuffer[index]; |
| 548 | } |
| 549 | |
| 550 | const T &operator[](size_t index) const { |
| 551 | return mBuffer[index]; |
| 552 | } |
| 553 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 554 | static constexpr size_t size() { return SIZE1; } |
| 555 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 556 | private: |
| 557 | T mBuffer[SIZE1]; |
| 558 | }; |
| 559 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 560 | // ---------------------------------------------------------------------- |
| 561 | // Version functions |
| 562 | struct hidl_version { |
| 563 | public: |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 564 | 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] | 565 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 566 | bool operator==(const hidl_version& other) const { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 567 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 568 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 569 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 570 | constexpr uint16_t get_major() const { return mMajor; } |
| 571 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 572 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 573 | private: |
| 574 | uint16_t mMajor; |
| 575 | uint16_t mMinor; |
| 576 | }; |
| 577 | |
| 578 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 579 | return hidl_version(major,minor); |
| 580 | } |
| 581 | |
Yifan Hong | c1a60ef | 2016-11-08 18:53:16 -0800 | [diff] [blame] | 582 | struct IBase : virtual public RefBase { |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 583 | virtual bool isRemote() const = 0; |
| 584 | // HIDL reserved methods follow. |
| 585 | virtual ::android::hardware::Return<void> interfaceChain( |
| 586 | std::function<void(const hidl_vec<hidl_string>&)> _hidl_cb) = 0; |
Martijn Coenen | 70822ea | 2016-11-16 15:47:09 +0100 | [diff] [blame] | 587 | // This method notifies the interface that one or more system properties have changed. |
| 588 | // The default implementation calls report_sysprop_change() in libcutils, which in turn |
| 589 | // calls a set of registered callbacks (eg to update trace tags). |
| 590 | virtual ::android::hardware::Return<void> notifySyspropsChanged() = 0; |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 591 | // descriptor for HIDL reserved methods. |
Steven Moreland | 5a68232 | 2016-11-11 12:32:50 -0800 | [diff] [blame] | 592 | static const char* descriptor; |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 593 | }; |
| 594 | |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 595 | #if defined(__LP64__) |
| 596 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/" |
| 597 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/" |
| 598 | #define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/" |
| 599 | #else |
| 600 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/" |
| 601 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/" |
| 602 | #define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/" |
| 603 | #endif |
| 604 | |
Steven Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 605 | #define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 606 | static ::android::sp<I##INTERFACE> getService( \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 607 | const std::string &serviceName, bool getStub=false); \ |
Steven Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 608 | ::android::status_t registerAsService(const std::string &serviceName); \ |
| 609 | static bool registerForNotifications( \ |
| 610 | const std::string &serviceName, \ |
| 611 | const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \ |
| 612 | ¬ification); \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 613 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 614 | // ---------------------------------------------------------------------- |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 615 | // Class that provides Hidl instrumentation utilities. |
| 616 | struct HidlInstrumentor { |
| 617 | // Event that triggers the instrumentation. e.g. enter of an API call on |
| 618 | // the server/client side, exit of an API call on the server/client side |
| 619 | // etc. |
| 620 | enum InstrumentationEvent { |
| 621 | SERVER_API_ENTRY = 0, |
| 622 | SERVER_API_EXIT, |
| 623 | CLIENT_API_ENTRY, |
| 624 | CLIENT_API_EXIT, |
| 625 | SYNC_CALLBACK_ENTRY, |
| 626 | SYNC_CALLBACK_EXIT, |
| 627 | ASYNC_CALLBACK_ENTRY, |
| 628 | ASYNC_CALLBACK_EXIT, |
Steven Moreland | cefbd6e | 2016-11-01 10:14:30 -0700 | [diff] [blame] | 629 | PASSTHROUGH_ENTRY, |
| 630 | PASSTHROUGH_EXIT, |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 631 | }; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 632 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 633 | // Signature of the instrumentation callback function. |
| 634 | using InstrumentationCallback = std::function<void( |
| 635 | const InstrumentationEvent event, |
| 636 | const char *package, |
| 637 | const char *version, |
| 638 | const char *interface, |
| 639 | const char *method, |
| 640 | std::vector<void *> *args)>; |
| 641 | |
| 642 | explicit HidlInstrumentor(const std::string &prefix); |
| 643 | virtual ~HidlInstrumentor(); |
| 644 | |
| 645 | protected: |
| 646 | // Function that lookup and dynamically loads the hidl instrumentation |
| 647 | // libraries and registers the instrumentation callback functions. |
| 648 | // |
| 649 | // The instrumentation libraries should be stored under any of the following |
| 650 | // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and |
| 651 | // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should |
| 652 | // follow pattern: ^profilerPrefix(.*).profiler.so$ |
| 653 | // |
| 654 | // Each instrumentation library is expected to implement the instrumentation |
| 655 | // function called HIDL_INSTRUMENTATION_FUNCTION. |
| 656 | // |
| 657 | // A no-op for user build. |
| 658 | void registerInstrumentationCallbacks( |
| 659 | const std::string &profilerPrefix, |
| 660 | std::vector<InstrumentationCallback> *instrumentationCallbacks); |
| 661 | |
| 662 | // Utility function to determine whether a give file is a instrumentation |
| 663 | // library (i.e. the file name follow the expected pattern). |
| 664 | bool isInstrumentationLib( |
| 665 | const std::string &profilerPrefix, |
| 666 | const dirent *file); |
| 667 | // A list of registered instrumentation callbacks. |
| 668 | std::vector<InstrumentationCallback> mInstrumentationCallbacks; |
| 669 | // Flag whether to enable instrumentation. |
| 670 | bool mEnableInstrumentation; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 671 | }; |
| 672 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 673 | } // namespace hardware |
| 674 | } // namespace android |
| 675 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 676 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 677 | #endif // ANDROID_HIDL_SUPPORT_H |
| 678 | |