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