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 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame^] | 36 | namespace details { |
| 37 | |
| 38 | // hidl_log_base is a base class that templatized |
| 39 | // classes implemented in a header can inherit from, |
| 40 | // to avoid creating dependencies on liblog. |
| 41 | struct hidl_log_base { |
| 42 | void logAlwaysFatal(const char *message); |
| 43 | }; |
| 44 | |
| 45 | // HIDL client/server code should *NOT* use this class. |
| 46 | // |
| 47 | // hidl_pointer wraps a pointer without taking ownership, |
| 48 | // and stores it in a union with a uint64_t. This ensures |
| 49 | // that we always have enough space to store a pointer, |
| 50 | // regardless of whether we're running in a 32-bit or 64-bit |
| 51 | // process. |
| 52 | template<typename T> |
| 53 | struct hidl_pointer { |
| 54 | hidl_pointer() |
| 55 | : mPointer(nullptr) { |
| 56 | } |
| 57 | hidl_pointer(T* ptr) |
| 58 | : mPointer(ptr) { |
| 59 | } |
| 60 | hidl_pointer(const hidl_pointer<T>& other) { |
| 61 | mPointer = other.mPointer; |
| 62 | } |
| 63 | hidl_pointer(hidl_pointer<T>&& other) { |
| 64 | *this = std::move(other); |
| 65 | } |
| 66 | |
| 67 | hidl_pointer &operator=(const hidl_pointer<T>& other) { |
| 68 | mPointer = other.mPointer; |
| 69 | return *this; |
| 70 | } |
| 71 | hidl_pointer &operator=(hidl_pointer<T>&& other) { |
| 72 | mPointer = other.mPointer; |
| 73 | other.mPointer = nullptr; |
| 74 | return *this; |
| 75 | } |
| 76 | hidl_pointer &operator=(T* ptr) { |
| 77 | mPointer = ptr; |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | operator T*() const { |
| 82 | return mPointer; |
| 83 | } |
| 84 | explicit operator void*() const { // requires explicit cast to avoid ambiguity |
| 85 | return mPointer; |
| 86 | } |
| 87 | T& operator*() const { |
| 88 | return *mPointer; |
| 89 | } |
| 90 | T* operator->() const { |
| 91 | return mPointer; |
| 92 | } |
| 93 | T &operator[](size_t index) { |
| 94 | return mPointer[index]; |
| 95 | } |
| 96 | const T &operator[](size_t index) const { |
| 97 | return mPointer[index]; |
| 98 | } |
| 99 | private: |
| 100 | union { |
| 101 | T* mPointer; |
| 102 | uint64_t _pad; |
| 103 | }; |
| 104 | }; |
| 105 | } // namespace details |
| 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 | |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 161 | inline bool operator==(const hidl_string &hs, const char *s) { |
| 162 | return strcmp(hs.c_str(), s) == 0; |
| 163 | } |
| 164 | |
| 165 | inline bool operator!=(const hidl_string &hs, const char *s) { |
| 166 | return !(hs == s); |
| 167 | } |
| 168 | |
| 169 | inline bool operator==(const char *s, const hidl_string &hs) { |
| 170 | return strcmp(hs.c_str(), s) == 0; |
| 171 | } |
| 172 | |
| 173 | inline bool operator!=(const char *s, const hidl_string &hs) { |
| 174 | return !(s == hs); |
| 175 | } |
| 176 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 177 | //////////////////////////////////////////////////////////////////////////////// |
| 178 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 179 | template<typename T> |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame^] | 180 | struct hidl_vec : private details::hidl_log_base { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 181 | hidl_vec() |
| 182 | : mBuffer(NULL), |
| 183 | mSize(0), |
| 184 | mOwnsBuffer(true) { |
| 185 | } |
| 186 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 187 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 188 | *this = other; |
| 189 | } |
| 190 | |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 191 | hidl_vec(hidl_vec<T> &&other) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 192 | : mOwnsBuffer(false) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 193 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 196 | hidl_vec(const std::initializer_list<T> list) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 197 | : mSize(list.size()), |
| 198 | mOwnsBuffer(true) { |
| 199 | mBuffer = new T[mSize]; |
| 200 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 201 | size_t idx = 0; |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 202 | for (auto it = list.begin(); it != list.end(); ++it) { |
| 203 | mBuffer[idx++] = *it; |
| 204 | } |
| 205 | } |
| 206 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 207 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 208 | *this = other; |
| 209 | } |
| 210 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 211 | ~hidl_vec() { |
| 212 | if (mOwnsBuffer) { |
| 213 | delete[] mBuffer; |
| 214 | } |
| 215 | mBuffer = NULL; |
| 216 | } |
| 217 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 218 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 219 | // caller's responsibility to ensure that the underlying memory stays |
| 220 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 221 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 222 | if (mOwnsBuffer) { |
| 223 | delete [] mBuffer; |
| 224 | } |
| 225 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame^] | 226 | if (size > UINT32_MAX) { |
| 227 | logAlwaysFatal("external vector size exceeds 2^32 elements."); |
| 228 | } |
| 229 | mSize = static_cast<uint32_t>(size); |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 230 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 233 | T *data() { |
| 234 | return mBuffer; |
| 235 | } |
| 236 | |
| 237 | const T *data() const { |
| 238 | return mBuffer; |
| 239 | } |
| 240 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 241 | T *releaseData() { |
| 242 | if (!mOwnsBuffer && mSize > 0) { |
| 243 | resize(mSize); |
| 244 | } |
| 245 | mOwnsBuffer = false; |
| 246 | return mBuffer; |
| 247 | } |
| 248 | |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 249 | hidl_vec &operator=(hidl_vec &&other) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 250 | if (mOwnsBuffer) { |
| 251 | delete[] mBuffer; |
| 252 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 253 | mBuffer = other.mBuffer; |
| 254 | mSize = other.mSize; |
| 255 | mOwnsBuffer = other.mOwnsBuffer; |
| 256 | other.mOwnsBuffer = false; |
| 257 | return *this; |
| 258 | } |
| 259 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 260 | hidl_vec &operator=(const hidl_vec &other) { |
| 261 | if (this != &other) { |
| 262 | if (mOwnsBuffer) { |
| 263 | delete[] mBuffer; |
| 264 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 265 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | return *this; |
| 269 | } |
| 270 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 271 | // copy from an std::vector. |
| 272 | hidl_vec &operator=(const std::vector<T> &other) { |
| 273 | if (mOwnsBuffer) { |
| 274 | delete[] mBuffer; |
| 275 | } |
| 276 | copyFrom(other, other.size()); |
| 277 | return *this; |
| 278 | } |
| 279 | |
| 280 | // cast to an std::vector. |
| 281 | operator std::vector<T>() const { |
| 282 | std::vector<T> v(mSize); |
| 283 | for (size_t i = 0; i < mSize; ++i) { |
| 284 | v[i] = mBuffer[i]; |
| 285 | } |
| 286 | return v; |
| 287 | } |
| 288 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 289 | size_t size() const { |
| 290 | return mSize; |
| 291 | } |
| 292 | |
| 293 | T &operator[](size_t index) { |
| 294 | return mBuffer[index]; |
| 295 | } |
| 296 | |
| 297 | const T &operator[](size_t index) const { |
| 298 | return mBuffer[index]; |
| 299 | } |
| 300 | |
| 301 | void resize(size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame^] | 302 | if (size > UINT32_MAX) { |
| 303 | logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
| 304 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 305 | T *newBuffer = new T[size]; |
| 306 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame^] | 307 | 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] | 308 | newBuffer[i] = mBuffer[i]; |
| 309 | } |
| 310 | |
| 311 | if (mOwnsBuffer) { |
| 312 | delete[] mBuffer; |
| 313 | } |
| 314 | mBuffer = newBuffer; |
| 315 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame^] | 316 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 317 | mOwnsBuffer = true; |
| 318 | } |
| 319 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 320 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 321 | static const size_t kOffsetOfBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 322 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame^] | 323 | details::hidl_pointer<T> mBuffer; |
| 324 | uint32_t mSize; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 325 | bool mOwnsBuffer; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 326 | |
| 327 | // copy from an array-like object, assuming my resources are freed. |
| 328 | template <typename Array> |
| 329 | void copyFrom(const Array &data, size_t size) { |
| 330 | mSize = size; |
| 331 | mOwnsBuffer = true; |
| 332 | if (mSize > 0) { |
| 333 | mBuffer = new T[size]; |
| 334 | for (size_t i = 0; i < size; ++i) { |
| 335 | mBuffer[i] = data[i]; |
| 336 | } |
| 337 | } else { |
| 338 | mBuffer = NULL; |
| 339 | } |
| 340 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 341 | }; |
| 342 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 343 | template <typename T> |
| 344 | const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer); |
| 345 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 346 | //////////////////////////////////////////////////////////////////////////////// |
| 347 | |
| 348 | namespace details { |
| 349 | |
| 350 | template<size_t SIZE1, size_t... SIZES> |
| 351 | struct product { |
| 352 | static constexpr size_t value = SIZE1 * product<SIZES...>::value; |
| 353 | }; |
| 354 | |
| 355 | template<size_t SIZE1> |
| 356 | struct product<SIZE1> { |
| 357 | static constexpr size_t value = SIZE1; |
| 358 | }; |
| 359 | |
| 360 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 361 | struct accessor { |
| 362 | explicit accessor(T *base) |
| 363 | : mBase(base) { |
| 364 | } |
| 365 | |
| 366 | accessor<T, SIZES...> operator[](size_t index) { |
| 367 | return accessor<T, SIZES...>( |
| 368 | &mBase[index * product<SIZES...>::value]); |
| 369 | } |
| 370 | |
| 371 | private: |
| 372 | T *mBase; |
| 373 | }; |
| 374 | |
| 375 | template<typename T, size_t SIZE1> |
| 376 | struct accessor<T, SIZE1> { |
| 377 | explicit accessor(T *base) |
| 378 | : mBase(base) { |
| 379 | } |
| 380 | |
| 381 | T &operator[](size_t index) { |
| 382 | return mBase[index]; |
| 383 | } |
| 384 | |
| 385 | private: |
| 386 | T *mBase; |
| 387 | }; |
| 388 | |
| 389 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 390 | struct const_accessor { |
| 391 | explicit const_accessor(const T *base) |
| 392 | : mBase(base) { |
| 393 | } |
| 394 | |
| 395 | const_accessor<T, SIZES...> operator[](size_t index) { |
| 396 | return const_accessor<T, SIZES...>( |
| 397 | &mBase[index * product<SIZES...>::value]); |
| 398 | } |
| 399 | |
| 400 | private: |
| 401 | const T *mBase; |
| 402 | }; |
| 403 | |
| 404 | template<typename T, size_t SIZE1> |
| 405 | struct const_accessor<T, SIZE1> { |
| 406 | explicit const_accessor(const T *base) |
| 407 | : mBase(base) { |
| 408 | } |
| 409 | |
| 410 | const T &operator[](size_t index) const { |
| 411 | return mBase[index]; |
| 412 | } |
| 413 | |
| 414 | private: |
| 415 | const T *mBase; |
| 416 | }; |
| 417 | |
| 418 | } // namespace details |
| 419 | |
| 420 | //////////////////////////////////////////////////////////////////////////////// |
| 421 | |
| 422 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 423 | struct hidl_array { |
| 424 | hidl_array() = default; |
| 425 | |
| 426 | T *data() { return mBuffer; } |
| 427 | const T *data() const { return mBuffer; } |
| 428 | |
| 429 | details::accessor<T, SIZES...> operator[](size_t index) { |
| 430 | return details::accessor<T, SIZES...>( |
| 431 | &mBuffer[index * details::product<SIZES...>::value]); |
| 432 | } |
| 433 | |
| 434 | details::const_accessor<T, SIZES...> operator[](size_t index) const { |
| 435 | return details::const_accessor<T, SIZES...>( |
| 436 | &mBuffer[index * details::product<SIZES...>::value]); |
| 437 | } |
| 438 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 439 | using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>; |
| 440 | |
| 441 | static constexpr size_tuple_type size() { |
| 442 | return std::make_tuple(SIZE1, SIZES...); |
| 443 | } |
| 444 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 445 | private: |
| 446 | T mBuffer[details::product<SIZE1, SIZES...>::value]; |
| 447 | }; |
| 448 | |
| 449 | template<typename T, size_t SIZE1> |
| 450 | struct hidl_array<T, SIZE1> { |
| 451 | hidl_array() = default; |
| 452 | |
| 453 | T *data() { return mBuffer; } |
| 454 | const T *data() const { return mBuffer; } |
| 455 | |
| 456 | T &operator[](size_t index) { |
| 457 | return mBuffer[index]; |
| 458 | } |
| 459 | |
| 460 | const T &operator[](size_t index) const { |
| 461 | return mBuffer[index]; |
| 462 | } |
| 463 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 464 | static constexpr size_t size() { return SIZE1; } |
| 465 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 466 | private: |
| 467 | T mBuffer[SIZE1]; |
| 468 | }; |
| 469 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 470 | // ---------------------------------------------------------------------- |
| 471 | // Version functions |
| 472 | struct hidl_version { |
| 473 | public: |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 474 | 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] | 475 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 476 | bool operator==(const hidl_version& other) const { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 477 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 478 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 479 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 480 | constexpr uint16_t get_major() const { return mMajor; } |
| 481 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 482 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 483 | private: |
| 484 | uint16_t mMajor; |
| 485 | uint16_t mMinor; |
| 486 | }; |
| 487 | |
| 488 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 489 | return hidl_version(major,minor); |
| 490 | } |
| 491 | |
Yifan Hong | c1a60ef | 2016-11-08 18:53:16 -0800 | [diff] [blame] | 492 | struct IBase : virtual public RefBase { |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 493 | virtual bool isRemote() const = 0; |
| 494 | // HIDL reserved methods follow. |
| 495 | virtual ::android::hardware::Return<void> interfaceChain( |
| 496 | std::function<void(const hidl_vec<hidl_string>&)> _hidl_cb) = 0; |
| 497 | // descriptor for HIDL reserved methods. |
Steven Moreland | 5a68232 | 2016-11-11 12:32:50 -0800 | [diff] [blame] | 498 | static const char* descriptor; |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 499 | }; |
| 500 | |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 501 | #if defined(__LP64__) |
| 502 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/" |
| 503 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/" |
| 504 | #define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/" |
| 505 | #else |
| 506 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/" |
| 507 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/" |
| 508 | #define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/" |
| 509 | #endif |
| 510 | |
Steven Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 511 | #define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 512 | static ::android::sp<I##INTERFACE> getService( \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 513 | const std::string &serviceName, bool getStub=false); \ |
Steven Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 514 | ::android::status_t registerAsService(const std::string &serviceName); \ |
| 515 | static bool registerForNotifications( \ |
| 516 | const std::string &serviceName, \ |
| 517 | const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \ |
| 518 | ¬ification); \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 519 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 520 | // ---------------------------------------------------------------------- |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 521 | // Class that provides Hidl instrumentation utilities. |
| 522 | struct HidlInstrumentor { |
| 523 | // Event that triggers the instrumentation. e.g. enter of an API call on |
| 524 | // the server/client side, exit of an API call on the server/client side |
| 525 | // etc. |
| 526 | enum InstrumentationEvent { |
| 527 | SERVER_API_ENTRY = 0, |
| 528 | SERVER_API_EXIT, |
| 529 | CLIENT_API_ENTRY, |
| 530 | CLIENT_API_EXIT, |
| 531 | SYNC_CALLBACK_ENTRY, |
| 532 | SYNC_CALLBACK_EXIT, |
| 533 | ASYNC_CALLBACK_ENTRY, |
| 534 | ASYNC_CALLBACK_EXIT, |
Steven Moreland | cefbd6e | 2016-11-01 10:14:30 -0700 | [diff] [blame] | 535 | PASSTHROUGH_ENTRY, |
| 536 | PASSTHROUGH_EXIT, |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 537 | }; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 538 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 539 | // Signature of the instrumentation callback function. |
| 540 | using InstrumentationCallback = std::function<void( |
| 541 | const InstrumentationEvent event, |
| 542 | const char *package, |
| 543 | const char *version, |
| 544 | const char *interface, |
| 545 | const char *method, |
| 546 | std::vector<void *> *args)>; |
| 547 | |
| 548 | explicit HidlInstrumentor(const std::string &prefix); |
| 549 | virtual ~HidlInstrumentor(); |
| 550 | |
| 551 | protected: |
| 552 | // Function that lookup and dynamically loads the hidl instrumentation |
| 553 | // libraries and registers the instrumentation callback functions. |
| 554 | // |
| 555 | // The instrumentation libraries should be stored under any of the following |
| 556 | // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and |
| 557 | // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should |
| 558 | // follow pattern: ^profilerPrefix(.*).profiler.so$ |
| 559 | // |
| 560 | // Each instrumentation library is expected to implement the instrumentation |
| 561 | // function called HIDL_INSTRUMENTATION_FUNCTION. |
| 562 | // |
| 563 | // A no-op for user build. |
| 564 | void registerInstrumentationCallbacks( |
| 565 | const std::string &profilerPrefix, |
| 566 | std::vector<InstrumentationCallback> *instrumentationCallbacks); |
| 567 | |
| 568 | // Utility function to determine whether a give file is a instrumentation |
| 569 | // library (i.e. the file name follow the expected pattern). |
| 570 | bool isInstrumentationLib( |
| 571 | const std::string &profilerPrefix, |
| 572 | const dirent *file); |
| 573 | // A list of registered instrumentation callbacks. |
| 574 | std::vector<InstrumentationCallback> mInstrumentationCallbacks; |
| 575 | // Flag whether to enable instrumentation. |
| 576 | bool mEnableInstrumentation; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 577 | }; |
| 578 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 579 | } // namespace hardware |
| 580 | } // namespace android |
| 581 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 582 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 583 | #endif // ANDROID_HIDL_SUPPORT_H |
| 584 | |