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 | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 24 | #include <hidl/Status.h> |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 25 | #include <hwbinder/Parcel.h> |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 26 | #include <tuple> |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 27 | #include <utils/Errors.h> |
| 28 | #include <utils/RefBase.h> |
| 29 | #include <utils/StrongPointer.h> |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | |
| 34 | struct hidl_string { |
| 35 | hidl_string(); |
| 36 | ~hidl_string(); |
| 37 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 38 | // copy constructor. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 39 | hidl_string(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 40 | // copy from a C-style string. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 41 | hidl_string(const char *); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 42 | // copy from an std::string. |
| 43 | hidl_string(const std::string &); |
| 44 | |
| 45 | // move constructor. |
| 46 | hidl_string(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 47 | |
| 48 | const char *c_str() const; |
| 49 | size_t size() const; |
| 50 | bool empty() const; |
| 51 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 52 | // copy assignment operator. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 53 | hidl_string &operator=(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 54 | // copy from a C-style string. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 55 | hidl_string &operator=(const char *s); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 56 | // copy from an std::string. |
| 57 | hidl_string &operator=(const std::string &); |
| 58 | // move assignment operator. |
| 59 | hidl_string &operator=(hidl_string &&other); |
| 60 | // cast to std::string. |
| 61 | operator std::string() const; |
| 62 | // cast to C-style string. Caller is responsible |
| 63 | // to maintain this hidl_string alive. |
| 64 | operator const char *() const; |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 65 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 66 | void clear(); |
| 67 | |
| 68 | // Reference an external char array. Ownership is _not_ transferred. |
| 69 | // Caller is responsible for ensuring that underlying memory is valid |
| 70 | // for the lifetime of this hidl_string. |
| 71 | void setToExternal(const char *data, size_t size); |
| 72 | |
| 73 | status_t readEmbeddedFromParcel( |
| 74 | const Parcel &parcel, size_t parentHandle, size_t parentOffset); |
| 75 | |
| 76 | status_t writeEmbeddedToParcel( |
| 77 | Parcel *parcel, size_t parentHandle, size_t parentOffset) const; |
| 78 | |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 79 | inline bool operator==(const char *s) const { |
| 80 | return strcmp(mBuffer, s) == 0; |
| 81 | } |
| 82 | |
| 83 | inline bool operator!=(const char *s) const { |
| 84 | return !(operator==(s)); |
| 85 | } |
| 86 | |
Andreas Huber | ebfeb36 | 2016-08-25 13:39:05 -0700 | [diff] [blame] | 87 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 88 | static const size_t kOffsetOfBuffer; |
| 89 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 90 | private: |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 91 | const char *mBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 92 | size_t mSize; // NOT including the terminating '\0'. |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 93 | bool mOwnsBuffer; // if true then mBuffer is a mutable char * |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 94 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 95 | // copy from data with size. Assume that my memory is freed |
| 96 | // (through clear(), for example) |
| 97 | void copyFrom(const char *data, size_t size); |
| 98 | // move from another hidl_string |
| 99 | void moveFrom(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 102 | //////////////////////////////////////////////////////////////////////////////// |
| 103 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 104 | template<typename T> |
| 105 | struct hidl_vec { |
| 106 | hidl_vec() |
| 107 | : mBuffer(NULL), |
| 108 | mSize(0), |
| 109 | mOwnsBuffer(true) { |
| 110 | } |
| 111 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 112 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 113 | *this = other; |
| 114 | } |
| 115 | |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame^] | 116 | hidl_vec(hidl_vec<T> &&other) |
| 117 | : mOwnsBuffer(false) { |
| 118 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 121 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 122 | *this = other; |
| 123 | } |
| 124 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 125 | ~hidl_vec() { |
| 126 | if (mOwnsBuffer) { |
| 127 | delete[] mBuffer; |
| 128 | } |
| 129 | mBuffer = NULL; |
| 130 | } |
| 131 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 132 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 133 | // caller's responsibility to ensure that the underlying memory stays |
| 134 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 135 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 136 | if (mOwnsBuffer) { |
| 137 | delete [] mBuffer; |
| 138 | } |
| 139 | mBuffer = data; |
| 140 | mSize = size; |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 141 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 144 | T *data() { |
| 145 | return mBuffer; |
| 146 | } |
| 147 | |
| 148 | const T *data() const { |
| 149 | return mBuffer; |
| 150 | } |
| 151 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 152 | T *releaseData() { |
| 153 | if (!mOwnsBuffer && mSize > 0) { |
| 154 | resize(mSize); |
| 155 | } |
| 156 | mOwnsBuffer = false; |
| 157 | return mBuffer; |
| 158 | } |
| 159 | |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 160 | hidl_vec &operator=(hidl_vec &&other) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame^] | 161 | if (mOwnsBuffer) { |
| 162 | delete[] mBuffer; |
| 163 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 164 | mBuffer = other.mBuffer; |
| 165 | mSize = other.mSize; |
| 166 | mOwnsBuffer = other.mOwnsBuffer; |
| 167 | other.mOwnsBuffer = false; |
| 168 | return *this; |
| 169 | } |
| 170 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 171 | hidl_vec &operator=(const hidl_vec &other) { |
| 172 | if (this != &other) { |
| 173 | if (mOwnsBuffer) { |
| 174 | delete[] mBuffer; |
| 175 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 176 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return *this; |
| 180 | } |
| 181 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 182 | // copy from an std::vector. |
| 183 | hidl_vec &operator=(const std::vector<T> &other) { |
| 184 | if (mOwnsBuffer) { |
| 185 | delete[] mBuffer; |
| 186 | } |
| 187 | copyFrom(other, other.size()); |
| 188 | return *this; |
| 189 | } |
| 190 | |
| 191 | // cast to an std::vector. |
| 192 | operator std::vector<T>() const { |
| 193 | std::vector<T> v(mSize); |
| 194 | for (size_t i = 0; i < mSize; ++i) { |
| 195 | v[i] = mBuffer[i]; |
| 196 | } |
| 197 | return v; |
| 198 | } |
| 199 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 200 | size_t size() const { |
| 201 | return mSize; |
| 202 | } |
| 203 | |
| 204 | T &operator[](size_t index) { |
| 205 | return mBuffer[index]; |
| 206 | } |
| 207 | |
| 208 | const T &operator[](size_t index) const { |
| 209 | return mBuffer[index]; |
| 210 | } |
| 211 | |
| 212 | void resize(size_t size) { |
| 213 | T *newBuffer = new T[size]; |
| 214 | |
| 215 | for (size_t i = 0; i < std::min(size, mSize); ++i) { |
| 216 | newBuffer[i] = mBuffer[i]; |
| 217 | } |
| 218 | |
| 219 | if (mOwnsBuffer) { |
| 220 | delete[] mBuffer; |
| 221 | } |
| 222 | mBuffer = newBuffer; |
| 223 | |
| 224 | mSize = size; |
| 225 | mOwnsBuffer = true; |
| 226 | } |
| 227 | |
| 228 | status_t readEmbeddedFromParcel( |
| 229 | const Parcel &parcel, |
| 230 | size_t parentHandle, |
| 231 | size_t parentOffset, |
| 232 | size_t *handle); |
| 233 | |
| 234 | status_t writeEmbeddedToParcel( |
| 235 | Parcel *parcel, |
| 236 | size_t parentHandle, |
| 237 | size_t parentOffset, |
| 238 | size_t *handle) const; |
| 239 | |
Yifan Hong | b1d8d4c | 2016-08-23 17:39:06 -0700 | [diff] [blame] | 240 | status_t findInParcel(const Parcel &parcel, size_t *handle) const { |
| 241 | return parcel.quickFindBuffer(mBuffer, handle); |
| 242 | } |
| 243 | |
| 244 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 245 | private: |
| 246 | T *mBuffer; |
| 247 | size_t mSize; |
| 248 | bool mOwnsBuffer; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 249 | |
| 250 | // copy from an array-like object, assuming my resources are freed. |
| 251 | template <typename Array> |
| 252 | void copyFrom(const Array &data, size_t size) { |
| 253 | mSize = size; |
| 254 | mOwnsBuffer = true; |
| 255 | if (mSize > 0) { |
| 256 | mBuffer = new T[size]; |
| 257 | for (size_t i = 0; i < size; ++i) { |
| 258 | mBuffer[i] = data[i]; |
| 259 | } |
| 260 | } else { |
| 261 | mBuffer = NULL; |
| 262 | } |
| 263 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 264 | }; |
| 265 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 266 | //////////////////////////////////////////////////////////////////////////////// |
| 267 | |
| 268 | namespace details { |
| 269 | |
| 270 | template<size_t SIZE1, size_t... SIZES> |
| 271 | struct product { |
| 272 | static constexpr size_t value = SIZE1 * product<SIZES...>::value; |
| 273 | }; |
| 274 | |
| 275 | template<size_t SIZE1> |
| 276 | struct product<SIZE1> { |
| 277 | static constexpr size_t value = SIZE1; |
| 278 | }; |
| 279 | |
| 280 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 281 | struct accessor { |
| 282 | explicit accessor(T *base) |
| 283 | : mBase(base) { |
| 284 | } |
| 285 | |
| 286 | accessor<T, SIZES...> operator[](size_t index) { |
| 287 | return accessor<T, SIZES...>( |
| 288 | &mBase[index * product<SIZES...>::value]); |
| 289 | } |
| 290 | |
| 291 | private: |
| 292 | T *mBase; |
| 293 | }; |
| 294 | |
| 295 | template<typename T, size_t SIZE1> |
| 296 | struct accessor<T, SIZE1> { |
| 297 | explicit accessor(T *base) |
| 298 | : mBase(base) { |
| 299 | } |
| 300 | |
| 301 | T &operator[](size_t index) { |
| 302 | return mBase[index]; |
| 303 | } |
| 304 | |
| 305 | private: |
| 306 | T *mBase; |
| 307 | }; |
| 308 | |
| 309 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 310 | struct const_accessor { |
| 311 | explicit const_accessor(const T *base) |
| 312 | : mBase(base) { |
| 313 | } |
| 314 | |
| 315 | const_accessor<T, SIZES...> operator[](size_t index) { |
| 316 | return const_accessor<T, SIZES...>( |
| 317 | &mBase[index * product<SIZES...>::value]); |
| 318 | } |
| 319 | |
| 320 | private: |
| 321 | const T *mBase; |
| 322 | }; |
| 323 | |
| 324 | template<typename T, size_t SIZE1> |
| 325 | struct const_accessor<T, SIZE1> { |
| 326 | explicit const_accessor(const T *base) |
| 327 | : mBase(base) { |
| 328 | } |
| 329 | |
| 330 | const T &operator[](size_t index) const { |
| 331 | return mBase[index]; |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | const T *mBase; |
| 336 | }; |
| 337 | |
| 338 | } // namespace details |
| 339 | |
| 340 | //////////////////////////////////////////////////////////////////////////////// |
| 341 | |
| 342 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 343 | struct hidl_array { |
| 344 | hidl_array() = default; |
| 345 | |
| 346 | T *data() { return mBuffer; } |
| 347 | const T *data() const { return mBuffer; } |
| 348 | |
| 349 | details::accessor<T, SIZES...> operator[](size_t index) { |
| 350 | return details::accessor<T, SIZES...>( |
| 351 | &mBuffer[index * details::product<SIZES...>::value]); |
| 352 | } |
| 353 | |
| 354 | details::const_accessor<T, SIZES...> operator[](size_t index) const { |
| 355 | return details::const_accessor<T, SIZES...>( |
| 356 | &mBuffer[index * details::product<SIZES...>::value]); |
| 357 | } |
| 358 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 359 | using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>; |
| 360 | |
| 361 | static constexpr size_tuple_type size() { |
| 362 | return std::make_tuple(SIZE1, SIZES...); |
| 363 | } |
| 364 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 365 | private: |
| 366 | T mBuffer[details::product<SIZE1, SIZES...>::value]; |
| 367 | }; |
| 368 | |
| 369 | template<typename T, size_t SIZE1> |
| 370 | struct hidl_array<T, SIZE1> { |
| 371 | hidl_array() = default; |
| 372 | |
| 373 | T *data() { return mBuffer; } |
| 374 | const T *data() const { return mBuffer; } |
| 375 | |
| 376 | T &operator[](size_t index) { |
| 377 | return mBuffer[index]; |
| 378 | } |
| 379 | |
| 380 | const T &operator[](size_t index) const { |
| 381 | return mBuffer[index]; |
| 382 | } |
| 383 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 384 | static constexpr size_t size() { return SIZE1; } |
| 385 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 386 | private: |
| 387 | T mBuffer[SIZE1]; |
| 388 | }; |
| 389 | |
| 390 | //////////////////////////////////////////////////////////////////////////////// |
| 391 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 392 | template<typename T> |
| 393 | status_t hidl_vec<T>::readEmbeddedFromParcel( |
| 394 | const Parcel &parcel, |
| 395 | size_t parentHandle, |
| 396 | size_t parentOffset, |
| 397 | size_t *handle) { |
| 398 | const void *ptr = parcel.readEmbeddedBuffer( |
| 399 | handle, |
| 400 | parentHandle, |
| 401 | parentOffset + offsetof(hidl_vec<T>, mBuffer)); |
| 402 | |
| 403 | return ptr != NULL ? OK : UNKNOWN_ERROR; |
| 404 | } |
| 405 | |
| 406 | template<typename T> |
| 407 | status_t hidl_vec<T>::writeEmbeddedToParcel( |
| 408 | Parcel *parcel, |
| 409 | size_t parentHandle, |
| 410 | size_t parentOffset, |
| 411 | size_t *handle) const { |
| 412 | return parcel->writeEmbeddedBuffer( |
| 413 | mBuffer, |
| 414 | sizeof(T) * mSize, |
| 415 | handle, |
| 416 | parentHandle, |
| 417 | parentOffset + offsetof(hidl_vec<T>, mBuffer)); |
| 418 | } |
| 419 | |
Yifan Hong | b1d8d4c | 2016-08-23 17:39:06 -0700 | [diff] [blame] | 420 | ///////////////////////////// pointers for HIDL |
| 421 | |
| 422 | template <typename T> |
| 423 | static status_t readEmbeddedReferenceFromParcel( |
| 424 | T const* * /* bufptr */, |
| 425 | const Parcel & parcel, |
| 426 | size_t parentHandle, |
| 427 | size_t parentOffset, |
| 428 | size_t *handle, |
| 429 | bool *shouldResolveRefInBuffer |
| 430 | ) { |
| 431 | // *bufptr is ignored because, if I am embedded in some |
| 432 | // other buffer, the kernel should have fixed me up already. |
| 433 | bool isPreviouslyWritten; |
| 434 | status_t result = parcel.readEmbeddedReference( |
| 435 | nullptr, // ignored, not written to bufptr. |
| 436 | handle, |
| 437 | parentHandle, |
| 438 | parentOffset, |
| 439 | &isPreviouslyWritten); |
| 440 | // tell caller to run T::readEmbeddedToParcel and |
| 441 | // T::readEmbeddedReferenceToParcel if necessary. |
| 442 | // It is not called here because we don't know if these two are valid methods. |
| 443 | *shouldResolveRefInBuffer = !isPreviouslyWritten; |
| 444 | return result; |
| 445 | } |
| 446 | |
| 447 | template <typename T> |
| 448 | static status_t writeEmbeddedReferenceToParcel( |
| 449 | T const* buf, |
| 450 | Parcel *parcel, size_t parentHandle, size_t parentOffset, |
| 451 | size_t *handle, |
| 452 | bool *shouldResolveRefInBuffer |
| 453 | ) { |
| 454 | |
| 455 | if(buf == nullptr) { |
| 456 | *shouldResolveRefInBuffer = false; |
| 457 | return parcel->writeEmbeddedNullReference(handle, parentHandle, parentOffset); |
| 458 | } |
| 459 | |
| 460 | // find whether the buffer exists |
| 461 | size_t childHandle, childOffset; |
| 462 | status_t result; |
| 463 | bool found; |
| 464 | |
| 465 | result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset); |
| 466 | |
| 467 | // tell caller to run T::writeEmbeddedToParcel and |
| 468 | // T::writeEmbeddedReferenceToParcel if necessary. |
| 469 | // It is not called here because we don't know if these two are valid methods. |
| 470 | *shouldResolveRefInBuffer = !found; |
| 471 | |
| 472 | if(result != OK) { |
| 473 | return result; // bad pointers and length given |
| 474 | } |
| 475 | if(!found) { // did not find it. |
| 476 | return parcel->writeEmbeddedBuffer(buf, sizeof(T), handle, |
| 477 | parentHandle, parentOffset); |
| 478 | } |
| 479 | // found the buffer. easy case. |
| 480 | return parcel->writeEmbeddedReference( |
| 481 | handle, |
| 482 | childHandle, |
| 483 | childOffset, |
| 484 | parentHandle, |
| 485 | parentOffset); |
| 486 | } |
| 487 | |
| 488 | template <typename T> |
| 489 | static status_t readReferenceFromParcel( |
| 490 | T const* *bufptr, |
| 491 | const Parcel & parcel, |
| 492 | size_t *handle, |
| 493 | bool *shouldResolveRefInBuffer |
| 494 | ) { |
| 495 | bool isPreviouslyWritten; |
| 496 | status_t result = parcel.readReference(reinterpret_cast<void const* *>(bufptr), |
| 497 | handle, &isPreviouslyWritten); |
| 498 | // tell caller to run T::readEmbeddedToParcel and |
| 499 | // T::readEmbeddedReferenceToParcel if necessary. |
| 500 | // It is not called here because we don't know if these two are valid methods. |
| 501 | *shouldResolveRefInBuffer = !isPreviouslyWritten; |
| 502 | return result; |
| 503 | } |
| 504 | |
| 505 | template <typename T> |
| 506 | static status_t writeReferenceToParcel( |
| 507 | T const *buf, |
| 508 | Parcel * parcel, |
| 509 | size_t *handle, |
| 510 | bool *shouldResolveRefInBuffer |
| 511 | ) { |
| 512 | |
| 513 | if(buf == nullptr) { |
| 514 | *shouldResolveRefInBuffer = false; |
| 515 | return parcel->writeNullReference(handle); |
| 516 | } |
| 517 | |
| 518 | // find whether the buffer exists |
| 519 | size_t childHandle, childOffset; |
| 520 | status_t result; |
| 521 | bool found; |
| 522 | |
| 523 | result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset); |
| 524 | |
| 525 | // tell caller to run T::writeEmbeddedToParcel and |
| 526 | // T::writeEmbeddedReferenceToParcel if necessary. |
| 527 | // It is not called here because we don't know if these two are valid methods. |
| 528 | *shouldResolveRefInBuffer = !found; |
| 529 | |
| 530 | if(result != OK) { |
| 531 | return result; // bad pointers and length given |
| 532 | } |
| 533 | if(!found) { // did not find it. |
| 534 | return parcel->writeBuffer(buf, sizeof(T), handle); |
| 535 | } |
| 536 | // found the buffer. easy case. |
| 537 | return parcel->writeReference(handle, |
| 538 | childHandle, childOffset); |
| 539 | } |
| 540 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 541 | // ---------------------------------------------------------------------- |
| 542 | // Version functions |
| 543 | struct hidl_version { |
| 544 | public: |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 545 | 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] | 546 | |
| 547 | bool operator==(const hidl_version& other) { |
| 548 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 549 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 550 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 551 | constexpr uint16_t get_major() const { return mMajor; } |
| 552 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 553 | |
| 554 | android::status_t writeToParcel(android::hardware::Parcel& parcel) const { |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 555 | return parcel.writeUint32(static_cast<uint32_t>(mMajor) << 16 | mMinor); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static hidl_version* readFromParcel(const android::hardware::Parcel& parcel) { |
| 559 | uint32_t version; |
| 560 | android::status_t status = parcel.readUint32(&version); |
| 561 | if (status != OK) { |
| 562 | return nullptr; |
| 563 | } else { |
| 564 | return new hidl_version(version >> 16, version & 0xFFFF); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | private: |
| 569 | uint16_t mMajor; |
| 570 | uint16_t mMinor; |
| 571 | }; |
| 572 | |
| 573 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 574 | return hidl_version(major,minor); |
| 575 | } |
| 576 | |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 577 | struct IHidlInterfaceBase : virtual public RefBase { |
| 578 | virtual bool isRemote() const = 0; |
| 579 | // HIDL reserved methods follow. |
| 580 | virtual ::android::hardware::Return<void> interfaceChain( |
| 581 | std::function<void(const hidl_vec<hidl_string>&)> _hidl_cb) = 0; |
| 582 | // descriptor for HIDL reserved methods. |
| 583 | static const ::android::String16 descriptor; |
| 584 | }; |
| 585 | |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 586 | #if defined(__LP64__) |
| 587 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/" |
| 588 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/" |
| 589 | #define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/" |
| 590 | #else |
| 591 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/" |
| 592 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/" |
| 593 | #define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/" |
| 594 | #endif |
| 595 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 596 | #define DECLARE_REGISTER_AND_GET_SERVICE(INTERFACE) \ |
| 597 | static ::android::sp<I##INTERFACE> getService( \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 598 | const std::string &serviceName, bool getStub=false); \ |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 599 | status_t registerAsService( \ |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 600 | const std::string &serviceName); \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 601 | |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 602 | #define IMPLEMENT_REGISTER_AND_GET_SERVICE(INTERFACE, LIB) \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 603 | ::android::sp<I##INTERFACE> I##INTERFACE::getService( \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 604 | const std::string &serviceName, bool getStub) \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 605 | { \ |
| 606 | sp<I##INTERFACE> iface; \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 607 | const struct timespec DELAY {1,0}; \ |
| 608 | unsigned retries = 3; \ |
Martijn Coenen | 270cb50 | 2016-10-25 00:19:56 +0200 | [diff] [blame] | 609 | const sp<IServiceManager> sm = defaultServiceManager(); \ |
| 610 | if (sm != nullptr && !getStub) { \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 611 | do { \ |
Martijn Coenen | 270cb50 | 2016-10-25 00:19:56 +0200 | [diff] [blame] | 612 | sp<IBinder> binderIface = \ |
| 613 | sm->checkService(String16(serviceName.c_str()), \ |
| 614 | I##INTERFACE::version); \ |
| 615 | iface = IHw##INTERFACE::asInterface(binderIface); \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 616 | if (iface != nullptr) { \ |
| 617 | return iface; \ |
| 618 | } \ |
| 619 | TEMP_FAILURE_RETRY(nanosleep(&DELAY, nullptr)); \ |
| 620 | } while (retries--); \ |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 621 | } \ |
| 622 | int dlMode = RTLD_LAZY; \ |
| 623 | void *handle = dlopen(HAL_LIBRARY_PATH_ODM LIB, dlMode); \ |
| 624 | if (handle == nullptr) { \ |
| 625 | handle = dlopen(HAL_LIBRARY_PATH_VENDOR LIB, dlMode); \ |
| 626 | } \ |
| 627 | if (handle == nullptr) { \ |
| 628 | handle = dlopen(HAL_LIBRARY_PATH_SYSTEM LIB, dlMode); \ |
| 629 | } \ |
| 630 | if (handle == nullptr) { \ |
| 631 | return iface; \ |
| 632 | } \ |
| 633 | I##INTERFACE* (*generator)(const char* name); \ |
| 634 | *(void **)(&generator) = dlsym(handle, "HIDL_FETCH_I"#INTERFACE); \ |
| 635 | if (generator) { \ |
Martijn Coenen | f7a651d | 2016-09-08 13:38:12 +0200 | [diff] [blame] | 636 | iface = (*generator)(serviceName.c_str()); \ |
Steven Moreland | 7391bc3 | 2016-09-14 10:31:48 -0700 | [diff] [blame] | 637 | if (iface != nullptr) { \ |
| 638 | iface = new Bs##INTERFACE(iface); \ |
| 639 | } \ |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 640 | } \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 641 | return iface; \ |
| 642 | } \ |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 643 | status_t I##INTERFACE::registerAsService( \ |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 644 | const std::string &serviceName) \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 645 | { \ |
| 646 | sp<Bn##INTERFACE> binderIface = new Bn##INTERFACE(this); \ |
| 647 | const sp<IServiceManager> sm = defaultServiceManager(); \ |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 648 | return sm->addService(String16(serviceName.c_str()), binderIface, \ |
| 649 | I##INTERFACE::version); \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 652 | // ---------------------------------------------------------------------- |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 653 | // Class that provides Hidl instrumentation utilities. |
| 654 | struct HidlInstrumentor { |
| 655 | // Event that triggers the instrumentation. e.g. enter of an API call on |
| 656 | // the server/client side, exit of an API call on the server/client side |
| 657 | // etc. |
| 658 | enum InstrumentationEvent { |
| 659 | SERVER_API_ENTRY = 0, |
| 660 | SERVER_API_EXIT, |
| 661 | CLIENT_API_ENTRY, |
| 662 | CLIENT_API_EXIT, |
| 663 | SYNC_CALLBACK_ENTRY, |
| 664 | SYNC_CALLBACK_EXIT, |
| 665 | ASYNC_CALLBACK_ENTRY, |
| 666 | ASYNC_CALLBACK_EXIT, |
| 667 | }; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 668 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 669 | // Signature of the instrumentation callback function. |
| 670 | using InstrumentationCallback = std::function<void( |
| 671 | const InstrumentationEvent event, |
| 672 | const char *package, |
| 673 | const char *version, |
| 674 | const char *interface, |
| 675 | const char *method, |
| 676 | std::vector<void *> *args)>; |
| 677 | |
| 678 | explicit HidlInstrumentor(const std::string &prefix); |
| 679 | virtual ~HidlInstrumentor(); |
| 680 | |
| 681 | protected: |
| 682 | // Function that lookup and dynamically loads the hidl instrumentation |
| 683 | // libraries and registers the instrumentation callback functions. |
| 684 | // |
| 685 | // The instrumentation libraries should be stored under any of the following |
| 686 | // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and |
| 687 | // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should |
| 688 | // follow pattern: ^profilerPrefix(.*).profiler.so$ |
| 689 | // |
| 690 | // Each instrumentation library is expected to implement the instrumentation |
| 691 | // function called HIDL_INSTRUMENTATION_FUNCTION. |
| 692 | // |
| 693 | // A no-op for user build. |
| 694 | void registerInstrumentationCallbacks( |
| 695 | const std::string &profilerPrefix, |
| 696 | std::vector<InstrumentationCallback> *instrumentationCallbacks); |
| 697 | |
| 698 | // Utility function to determine whether a give file is a instrumentation |
| 699 | // library (i.e. the file name follow the expected pattern). |
| 700 | bool isInstrumentationLib( |
| 701 | const std::string &profilerPrefix, |
| 702 | const dirent *file); |
| 703 | // A list of registered instrumentation callbacks. |
| 704 | std::vector<InstrumentationCallback> mInstrumentationCallbacks; |
| 705 | // Flag whether to enable instrumentation. |
| 706 | bool mEnableInstrumentation; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 707 | }; |
| 708 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 709 | } // namespace hardware |
| 710 | } // namespace android |
| 711 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 712 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 713 | #endif // ANDROID_HIDL_SUPPORT_H |
| 714 | |