Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_HIDL_SUPPORT_H |
| 18 | #define ANDROID_HIDL_SUPPORT_H |
| 19 | |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 21 | #include <array> |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 22 | #include <dirent.h> |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 23 | #include <dlfcn.h> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 24 | #include <iterator> |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 25 | #include <cutils/native_handle.h> |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 27 | #include <functional> |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 28 | #include <hidl/HidlInternal.h> |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 29 | #include <hidl/Status.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 30 | #include <map> |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame^] | 31 | #include <sstream> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 32 | #include <stddef.h> |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 33 | #include <tuple> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 34 | #include <type_traits> |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 35 | #include <utils/Errors.h> |
| 36 | #include <utils/RefBase.h> |
| 37 | #include <utils/StrongPointer.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 38 | #include <vector> |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 39 | |
| 40 | namespace android { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 41 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 42 | // this file is included by all hidl interface, so we must forward declare the |
| 43 | // IMemory and IBase types. |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 44 | namespace hidl { |
| 45 | namespace memory { |
| 46 | namespace V1_0 { |
| 47 | struct IMemory; |
| 48 | }; // namespace V1_0 |
| 49 | }; // namespace manager |
| 50 | }; // namespace hidl |
| 51 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 52 | namespace hidl { |
| 53 | namespace base { |
| 54 | namespace V1_0 { |
| 55 | struct IBase; |
| 56 | }; // namespace V1_0 |
| 57 | }; // namespace base |
| 58 | }; // namespace hidl |
| 59 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 60 | namespace hardware { |
| 61 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 62 | // hidl_death_recipient is a callback interfaced that can be used with |
| 63 | // linkToDeath() / unlinkToDeath() |
| 64 | struct hidl_death_recipient : public virtual RefBase { |
| 65 | virtual void serviceDied(uint64_t cookie, |
| 66 | const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0; |
| 67 | }; |
| 68 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 69 | // hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer, |
| 70 | // so that it can safely be transferred between 32-bit and 64-bit processes. |
| 71 | struct hidl_handle { |
| 72 | hidl_handle() { |
| 73 | mHandle = nullptr; |
| 74 | } |
| 75 | ~hidl_handle() { |
| 76 | } |
| 77 | |
| 78 | // copy constructors. |
| 79 | hidl_handle(const native_handle_t *handle) { |
| 80 | mHandle = handle; |
| 81 | } |
| 82 | |
| 83 | hidl_handle(const hidl_handle &other) { |
| 84 | mHandle = other.mHandle; |
| 85 | } |
| 86 | |
| 87 | // move constructor. |
| 88 | hidl_handle(hidl_handle &&other) { |
| 89 | *this = std::move(other); |
| 90 | } |
| 91 | |
| 92 | // assingment operators |
| 93 | hidl_handle &operator=(const hidl_handle &other) { |
| 94 | mHandle = other.mHandle; |
| 95 | return *this; |
| 96 | } |
| 97 | |
| 98 | hidl_handle &operator=(const native_handle_t *native_handle) { |
| 99 | mHandle = native_handle; |
| 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | hidl_handle &operator=(hidl_handle &&other) { |
| 104 | mHandle = other.mHandle; |
| 105 | other.mHandle = nullptr; |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | const native_handle_t* operator->() const { |
| 110 | return mHandle; |
| 111 | } |
| 112 | // implicit conversion to const native_handle_t* |
| 113 | operator const native_handle_t *() const { |
| 114 | return mHandle; |
| 115 | } |
| 116 | // explicit conversion |
| 117 | const native_handle_t *getNativeHandle() const { |
| 118 | return mHandle; |
| 119 | } |
| 120 | private: |
| 121 | details::hidl_pointer<const native_handle_t> mHandle; |
| 122 | }; |
| 123 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 124 | struct hidl_string { |
| 125 | hidl_string(); |
| 126 | ~hidl_string(); |
| 127 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 128 | // copy constructor. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 129 | hidl_string(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 130 | // copy from a C-style string. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 131 | hidl_string(const char *); |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 132 | // copy the first length characters from a C-style string. |
| 133 | hidl_string(const char *, size_t length); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 134 | // copy from an std::string. |
| 135 | hidl_string(const std::string &); |
| 136 | |
| 137 | // move constructor. |
| 138 | hidl_string(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 139 | |
| 140 | const char *c_str() const; |
| 141 | size_t size() const; |
| 142 | bool empty() const; |
| 143 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 144 | // copy assignment operator. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 145 | hidl_string &operator=(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 146 | // copy from a C-style string. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 147 | hidl_string &operator=(const char *s); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 148 | // copy from an std::string. |
| 149 | hidl_string &operator=(const std::string &); |
| 150 | // move assignment operator. |
| 151 | hidl_string &operator=(hidl_string &&other); |
| 152 | // cast to std::string. |
| 153 | operator std::string() const; |
| 154 | // cast to C-style string. Caller is responsible |
| 155 | // to maintain this hidl_string alive. |
| 156 | operator const char *() const; |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 157 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 158 | void clear(); |
| 159 | |
| 160 | // Reference an external char array. Ownership is _not_ transferred. |
| 161 | // Caller is responsible for ensuring that underlying memory is valid |
| 162 | // for the lifetime of this hidl_string. |
| 163 | void setToExternal(const char *data, size_t size); |
| 164 | |
Andreas Huber | ebfeb36 | 2016-08-25 13:39:05 -0700 | [diff] [blame] | 165 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 166 | static const size_t kOffsetOfBuffer; |
| 167 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 168 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 169 | details::hidl_pointer<const char> mBuffer; |
| 170 | uint32_t mSize; // NOT including the terminating '\0'. |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 171 | bool mOwnsBuffer; // if true then mBuffer is a mutable char * |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 172 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 173 | // copy from data with size. Assume that my memory is freed |
| 174 | // (through clear(), for example) |
| 175 | void copyFrom(const char *data, size_t size); |
| 176 | // move from another hidl_string |
| 177 | void moveFrom(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 178 | }; |
| 179 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 180 | inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) { |
| 181 | return strcmp(hs1.c_str(), hs2.c_str()) == 0; |
| 182 | } |
| 183 | |
| 184 | inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) { |
| 185 | return !(hs1 == hs2); |
| 186 | } |
| 187 | |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 188 | inline bool operator==(const hidl_string &hs, const char *s) { |
| 189 | return strcmp(hs.c_str(), s) == 0; |
| 190 | } |
| 191 | |
| 192 | inline bool operator!=(const hidl_string &hs, const char *s) { |
| 193 | return !(hs == s); |
| 194 | } |
| 195 | |
| 196 | inline bool operator==(const char *s, const hidl_string &hs) { |
| 197 | return strcmp(hs.c_str(), s) == 0; |
| 198 | } |
| 199 | |
| 200 | inline bool operator!=(const char *s, const hidl_string &hs) { |
| 201 | return !(s == hs); |
| 202 | } |
| 203 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 204 | // hidl_memory is a structure that can be used to transfer |
| 205 | // pieces of shared memory between processes. The assumption |
| 206 | // of this object is that the memory remains accessible as |
| 207 | // long as the file descriptors in the enclosed mHandle |
| 208 | // - as well as all of its cross-process dups() - remain opened. |
| 209 | struct hidl_memory { |
| 210 | |
| 211 | hidl_memory() : mOwnsHandle(false), mHandle(nullptr), mSize(0), mName("") { |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Creates a hidl_memory object and takes ownership of the handle. |
| 216 | */ |
| 217 | hidl_memory(const hidl_string &name, const hidl_handle &handle, size_t size) |
| 218 | : mOwnsHandle(true), |
| 219 | mHandle(handle), |
| 220 | mSize(size), |
| 221 | mName(name) |
| 222 | {} |
| 223 | |
| 224 | // copy constructor |
| 225 | hidl_memory(const hidl_memory& other) { |
| 226 | *this = other; |
| 227 | } |
| 228 | |
| 229 | // copy assignment |
| 230 | hidl_memory &operator=(const hidl_memory &other) { |
| 231 | if (this != &other) { |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 232 | cleanup(); |
| 233 | |
| 234 | if (other.mHandle == nullptr) { |
| 235 | mHandle = nullptr; |
| 236 | mOwnsHandle = false; |
| 237 | } else { |
| 238 | mOwnsHandle = true; |
| 239 | mHandle = native_handle_clone(other.mHandle); |
| 240 | } |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 241 | mSize = other.mSize; |
| 242 | mName = other.mName; |
| 243 | } |
| 244 | |
| 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | // TODO move constructor/move assignment |
| 249 | |
| 250 | ~hidl_memory() { |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 251 | cleanup(); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | const native_handle_t* handle() const { |
| 255 | return mHandle; |
| 256 | } |
| 257 | |
| 258 | const hidl_string &name() const { |
| 259 | return mName; |
| 260 | } |
| 261 | |
| 262 | size_t size() const { |
| 263 | return mSize; |
| 264 | } |
| 265 | |
| 266 | // offsetof(hidl_memory, mHandle) exposed since mHandle is private. |
| 267 | static const size_t kOffsetOfHandle; |
| 268 | // offsetof(hidl_memory, mName) exposed since mHandle is private. |
| 269 | static const size_t kOffsetOfName; |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 270 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 271 | private: |
| 272 | bool mOwnsHandle; |
| 273 | hidl_handle mHandle; |
| 274 | size_t mSize; |
| 275 | hidl_string mName; |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 276 | |
| 277 | void cleanup() { |
| 278 | // TODO(b/33812533): native_handle_delete |
| 279 | if (mOwnsHandle && mHandle != nullptr) { |
| 280 | native_handle_close(mHandle); |
| 281 | } |
| 282 | } |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 283 | }; |
| 284 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 285 | //////////////////////////////////////////////////////////////////////////////// |
| 286 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 287 | template<typename T> |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 288 | struct hidl_vec : private details::hidl_log_base { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 289 | hidl_vec() |
| 290 | : mBuffer(NULL), |
| 291 | mSize(0), |
| 292 | mOwnsBuffer(true) { |
| 293 | } |
| 294 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 295 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 296 | *this = other; |
| 297 | } |
| 298 | |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 299 | hidl_vec(hidl_vec<T> &&other) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 300 | : mOwnsBuffer(false) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 301 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 304 | hidl_vec(const std::initializer_list<T> list) |
Yifan Hong | ca1d1bf | 2016-12-19 14:26:19 -0800 | [diff] [blame] | 305 | : mOwnsBuffer(true) { |
| 306 | if (list.size() > UINT32_MAX) { |
| 307 | logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
| 308 | } |
| 309 | mSize = static_cast<uint32_t>(list.size()); |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 310 | mBuffer = new T[mSize]; |
| 311 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 312 | size_t idx = 0; |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 313 | for (auto it = list.begin(); it != list.end(); ++it) { |
| 314 | mBuffer[idx++] = *it; |
| 315 | } |
| 316 | } |
| 317 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 318 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 319 | *this = other; |
| 320 | } |
| 321 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 322 | ~hidl_vec() { |
| 323 | if (mOwnsBuffer) { |
| 324 | delete[] mBuffer; |
| 325 | } |
| 326 | mBuffer = NULL; |
| 327 | } |
| 328 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 329 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 330 | // caller's responsibility to ensure that the underlying memory stays |
| 331 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 332 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 333 | if (mOwnsBuffer) { |
| 334 | delete [] mBuffer; |
| 335 | } |
| 336 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 337 | if (size > UINT32_MAX) { |
| 338 | logAlwaysFatal("external vector size exceeds 2^32 elements."); |
| 339 | } |
| 340 | mSize = static_cast<uint32_t>(size); |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 341 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 344 | T *data() { |
| 345 | return mBuffer; |
| 346 | } |
| 347 | |
| 348 | const T *data() const { |
| 349 | return mBuffer; |
| 350 | } |
| 351 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 352 | T *releaseData() { |
| 353 | if (!mOwnsBuffer && mSize > 0) { |
| 354 | resize(mSize); |
| 355 | } |
| 356 | mOwnsBuffer = false; |
| 357 | return mBuffer; |
| 358 | } |
| 359 | |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 360 | hidl_vec &operator=(hidl_vec &&other) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 361 | if (mOwnsBuffer) { |
| 362 | delete[] mBuffer; |
| 363 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 364 | mBuffer = other.mBuffer; |
| 365 | mSize = other.mSize; |
| 366 | mOwnsBuffer = other.mOwnsBuffer; |
| 367 | other.mOwnsBuffer = false; |
| 368 | return *this; |
| 369 | } |
| 370 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 371 | hidl_vec &operator=(const hidl_vec &other) { |
| 372 | if (this != &other) { |
| 373 | if (mOwnsBuffer) { |
| 374 | delete[] mBuffer; |
| 375 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 376 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | return *this; |
| 380 | } |
| 381 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 382 | // copy from an std::vector. |
| 383 | hidl_vec &operator=(const std::vector<T> &other) { |
| 384 | if (mOwnsBuffer) { |
| 385 | delete[] mBuffer; |
| 386 | } |
| 387 | copyFrom(other, other.size()); |
| 388 | return *this; |
| 389 | } |
| 390 | |
| 391 | // cast to an std::vector. |
| 392 | operator std::vector<T>() const { |
| 393 | std::vector<T> v(mSize); |
| 394 | for (size_t i = 0; i < mSize; ++i) { |
| 395 | v[i] = mBuffer[i]; |
| 396 | } |
| 397 | return v; |
| 398 | } |
| 399 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 400 | // equality check, assuming that T::operator== is defined. |
| 401 | bool operator==(const hidl_vec &other) const { |
| 402 | if (mSize != other.size()) { |
| 403 | return false; |
| 404 | } |
| 405 | for (size_t i = 0; i < mSize; ++i) { |
| 406 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | // inequality check, assuming that T::operator== is defined. |
| 414 | inline bool operator!=(const hidl_vec &other) const { |
| 415 | return !((*this) == other); |
| 416 | } |
| 417 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 418 | size_t size() const { |
| 419 | return mSize; |
| 420 | } |
| 421 | |
| 422 | T &operator[](size_t index) { |
| 423 | return mBuffer[index]; |
| 424 | } |
| 425 | |
| 426 | const T &operator[](size_t index) const { |
| 427 | return mBuffer[index]; |
| 428 | } |
| 429 | |
| 430 | void resize(size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 431 | if (size > UINT32_MAX) { |
| 432 | logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
| 433 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 434 | T *newBuffer = new T[size]; |
| 435 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 436 | 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] | 437 | newBuffer[i] = mBuffer[i]; |
| 438 | } |
| 439 | |
| 440 | if (mOwnsBuffer) { |
| 441 | delete[] mBuffer; |
| 442 | } |
| 443 | mBuffer = newBuffer; |
| 444 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 445 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 446 | mOwnsBuffer = true; |
| 447 | } |
| 448 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 449 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 450 | static const size_t kOffsetOfBuffer; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 451 | |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 452 | private: |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 453 | // Define std interator interface for walking the array contents |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 454 | template<bool is_const> |
| 455 | class iter : public std::iterator< |
| 456 | std::random_access_iterator_tag, /* Category */ |
| 457 | T, |
| 458 | ptrdiff_t, /* Distance */ |
| 459 | typename std::conditional<is_const, const T *, T *>::type /* Pointer */, |
| 460 | typename std::conditional<is_const, const T &, T &>::type /* Reference */> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 461 | { |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 462 | using traits = std::iterator_traits<iter>; |
| 463 | using ptr_type = typename traits::pointer; |
| 464 | using ref_type = typename traits::reference; |
| 465 | using diff_type = typename traits::difference_type; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 466 | public: |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 467 | iter(ptr_type ptr) : mPtr(ptr) { } |
| 468 | inline iter &operator++() { mPtr++; return *this; } |
| 469 | inline iter operator++(int) { iter i = *this; mPtr++; return i; } |
| 470 | inline iter &operator--() { mPtr--; return *this; } |
| 471 | inline iter operator--(int) { iter i = *this; mPtr--; return i; } |
| 472 | inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; } |
| 473 | inline iter operator+(diff_type n) const { return mPtr + n; } |
| 474 | inline iter operator-(diff_type n) const { return mPtr - n; } |
| 475 | inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; } |
| 476 | inline iter &operator+=(diff_type n) { mPtr += n; return *this; } |
| 477 | inline iter &operator-=(diff_type n) { mPtr -= n; return *this; } |
| 478 | inline ref_type operator*() const { return *mPtr; } |
| 479 | inline ptr_type operator->() const { return mPtr; } |
| 480 | inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; } |
| 481 | inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; } |
| 482 | inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; } |
| 483 | inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; } |
| 484 | inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; } |
| 485 | inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; } |
| 486 | inline ref_type operator[](size_t n) const { return mPtr[n]; } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 487 | private: |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 488 | ptr_type mPtr; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 489 | }; |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 490 | public: |
| 491 | using iterator = iter<false /* is_const */>; |
| 492 | using const_iterator = iter<true /* is_const */>; |
| 493 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 494 | iterator begin() { return data(); } |
| 495 | iterator end() { return data()+mSize; } |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 496 | const_iterator begin() const { return data(); } |
| 497 | const_iterator end() const { return data()+mSize; } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 498 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 499 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 500 | details::hidl_pointer<T> mBuffer; |
| 501 | uint32_t mSize; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 502 | bool mOwnsBuffer; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 503 | |
| 504 | // copy from an array-like object, assuming my resources are freed. |
| 505 | template <typename Array> |
| 506 | void copyFrom(const Array &data, size_t size) { |
Chia-I Wu | 4b48edc | 2016-12-07 22:31:17 +0800 | [diff] [blame] | 507 | mSize = static_cast<uint32_t>(size); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 508 | mOwnsBuffer = true; |
| 509 | if (mSize > 0) { |
| 510 | mBuffer = new T[size]; |
| 511 | for (size_t i = 0; i < size; ++i) { |
| 512 | mBuffer[i] = data[i]; |
| 513 | } |
| 514 | } else { |
| 515 | mBuffer = NULL; |
| 516 | } |
| 517 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 518 | }; |
| 519 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 520 | template <typename T> |
| 521 | const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer); |
| 522 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 523 | //////////////////////////////////////////////////////////////////////////////// |
| 524 | |
| 525 | namespace details { |
| 526 | |
| 527 | template<size_t SIZE1, size_t... SIZES> |
| 528 | struct product { |
| 529 | static constexpr size_t value = SIZE1 * product<SIZES...>::value; |
| 530 | }; |
| 531 | |
| 532 | template<size_t SIZE1> |
| 533 | struct product<SIZE1> { |
| 534 | static constexpr size_t value = SIZE1; |
| 535 | }; |
| 536 | |
| 537 | template<typename T, size_t SIZE1, size_t... SIZES> |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 538 | struct std_array { |
| 539 | using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>; |
| 540 | }; |
| 541 | |
| 542 | template<typename T, size_t SIZE1> |
| 543 | struct std_array<T, SIZE1> { |
| 544 | using type = std::array<T, SIZE1>; |
| 545 | }; |
| 546 | |
| 547 | template<typename T, size_t SIZE1, size_t... SIZES> |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 548 | struct accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 549 | |
| 550 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 551 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 552 | explicit accessor(T *base) |
| 553 | : mBase(base) { |
| 554 | } |
| 555 | |
| 556 | accessor<T, SIZES...> operator[](size_t index) { |
| 557 | return accessor<T, SIZES...>( |
| 558 | &mBase[index * product<SIZES...>::value]); |
| 559 | } |
| 560 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 561 | accessor &operator=(const std_array_type &other) { |
| 562 | for (size_t i = 0; i < SIZE1; ++i) { |
| 563 | (*this)[i] = other[i]; |
| 564 | } |
| 565 | return *this; |
| 566 | } |
| 567 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 568 | private: |
| 569 | T *mBase; |
| 570 | }; |
| 571 | |
| 572 | template<typename T, size_t SIZE1> |
| 573 | struct accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 574 | |
| 575 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 576 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 577 | explicit accessor(T *base) |
| 578 | : mBase(base) { |
| 579 | } |
| 580 | |
| 581 | T &operator[](size_t index) { |
| 582 | return mBase[index]; |
| 583 | } |
| 584 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 585 | accessor &operator=(const std_array_type &other) { |
| 586 | for (size_t i = 0; i < SIZE1; ++i) { |
| 587 | (*this)[i] = other[i]; |
| 588 | } |
| 589 | return *this; |
| 590 | } |
| 591 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 592 | private: |
| 593 | T *mBase; |
| 594 | }; |
| 595 | |
| 596 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 597 | struct const_accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 598 | |
| 599 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 600 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 601 | explicit const_accessor(const T *base) |
| 602 | : mBase(base) { |
| 603 | } |
| 604 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame^] | 605 | const_accessor<T, SIZES...> operator[](size_t index) const { |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 606 | return const_accessor<T, SIZES...>( |
| 607 | &mBase[index * product<SIZES...>::value]); |
| 608 | } |
| 609 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 610 | operator std_array_type() { |
| 611 | std_array_type array; |
| 612 | for (size_t i = 0; i < SIZE1; ++i) { |
| 613 | array[i] = (*this)[i]; |
| 614 | } |
| 615 | return array; |
| 616 | } |
| 617 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 618 | private: |
| 619 | const T *mBase; |
| 620 | }; |
| 621 | |
| 622 | template<typename T, size_t SIZE1> |
| 623 | struct const_accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 624 | |
| 625 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 626 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 627 | explicit const_accessor(const T *base) |
| 628 | : mBase(base) { |
| 629 | } |
| 630 | |
| 631 | const T &operator[](size_t index) const { |
| 632 | return mBase[index]; |
| 633 | } |
| 634 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 635 | operator std_array_type() { |
| 636 | std_array_type array; |
| 637 | for (size_t i = 0; i < SIZE1; ++i) { |
| 638 | array[i] = (*this)[i]; |
| 639 | } |
| 640 | return array; |
| 641 | } |
| 642 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 643 | private: |
| 644 | const T *mBase; |
| 645 | }; |
| 646 | |
| 647 | } // namespace details |
| 648 | |
| 649 | //////////////////////////////////////////////////////////////////////////////// |
| 650 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 651 | // A multidimensional array of T's. Assumes that T::operator=(const T &) is defined. |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 652 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 653 | struct hidl_array { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 654 | |
| 655 | using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type; |
| 656 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 657 | hidl_array() = default; |
| 658 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 659 | // Copies the data from source, using T::operator=(const T &). |
| 660 | hidl_array(const T *source) { |
| 661 | for (size_t i = 0; i < elementCount(); ++i) { |
| 662 | mBuffer[i] = source[i]; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 667 | hidl_array(const std_array_type &array) { |
| 668 | details::accessor<T, SIZE1, SIZES...> modifier(mBuffer); |
| 669 | modifier = array; |
| 670 | } |
| 671 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 672 | T *data() { return mBuffer; } |
| 673 | const T *data() const { return mBuffer; } |
| 674 | |
| 675 | details::accessor<T, SIZES...> operator[](size_t index) { |
| 676 | return details::accessor<T, SIZES...>( |
| 677 | &mBuffer[index * details::product<SIZES...>::value]); |
| 678 | } |
| 679 | |
| 680 | details::const_accessor<T, SIZES...> operator[](size_t index) const { |
| 681 | return details::const_accessor<T, SIZES...>( |
| 682 | &mBuffer[index * details::product<SIZES...>::value]); |
| 683 | } |
| 684 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 685 | // equality check, assuming that T::operator== is defined. |
| 686 | bool operator==(const hidl_array &other) const { |
| 687 | for (size_t i = 0; i < elementCount(); ++i) { |
| 688 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 689 | return false; |
| 690 | } |
| 691 | } |
| 692 | return true; |
| 693 | } |
| 694 | |
| 695 | inline bool operator!=(const hidl_array &other) const { |
| 696 | return !((*this) == other); |
| 697 | } |
| 698 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 699 | using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>; |
| 700 | |
| 701 | static constexpr size_tuple_type size() { |
| 702 | return std::make_tuple(SIZE1, SIZES...); |
| 703 | } |
| 704 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 705 | static constexpr size_t elementCount() { |
| 706 | return details::product<SIZE1, SIZES...>::value; |
| 707 | } |
| 708 | |
| 709 | operator std_array_type() const { |
| 710 | return details::const_accessor<T, SIZE1, SIZES...>(mBuffer); |
| 711 | } |
| 712 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 713 | private: |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 714 | T mBuffer[elementCount()]; |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 715 | }; |
| 716 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 717 | // An array of T's. Assumes that T::operator=(const T &) is defined. |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 718 | template<typename T, size_t SIZE1> |
| 719 | struct hidl_array<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 720 | |
| 721 | using std_array_type = typename details::std_array<T, SIZE1>::type; |
| 722 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 723 | hidl_array() = default; |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 724 | |
| 725 | // Copies the data from source, using T::operator=(const T &). |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 726 | hidl_array(const T *source) { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 727 | for (size_t i = 0; i < elementCount(); ++i) { |
| 728 | mBuffer[i] = source[i]; |
| 729 | } |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 730 | } |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 731 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 732 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 733 | hidl_array(const std_array_type &array) : hidl_array(array.data()) {} |
| 734 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 735 | T *data() { return mBuffer; } |
| 736 | const T *data() const { return mBuffer; } |
| 737 | |
| 738 | T &operator[](size_t index) { |
| 739 | return mBuffer[index]; |
| 740 | } |
| 741 | |
| 742 | const T &operator[](size_t index) const { |
| 743 | return mBuffer[index]; |
| 744 | } |
| 745 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 746 | // equality check, assuming that T::operator== is defined. |
| 747 | bool operator==(const hidl_array &other) const { |
| 748 | for (size_t i = 0; i < elementCount(); ++i) { |
| 749 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 750 | return false; |
| 751 | } |
| 752 | } |
| 753 | return true; |
| 754 | } |
| 755 | |
| 756 | inline bool operator!=(const hidl_array &other) const { |
| 757 | return !((*this) == other); |
| 758 | } |
| 759 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 760 | static constexpr size_t size() { return SIZE1; } |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 761 | static constexpr size_t elementCount() { return SIZE1; } |
| 762 | |
| 763 | // Copies the data to an std::array, using T::operator=(T). |
| 764 | operator std_array_type() const { |
| 765 | std_array_type array; |
| 766 | for (size_t i = 0; i < SIZE1; ++i) { |
| 767 | array[i] = mBuffer[i]; |
| 768 | } |
| 769 | return array; |
| 770 | } |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 771 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 772 | private: |
| 773 | T mBuffer[SIZE1]; |
| 774 | }; |
| 775 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 776 | // ---------------------------------------------------------------------- |
| 777 | // Version functions |
| 778 | struct hidl_version { |
| 779 | public: |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 780 | 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] | 781 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 782 | bool operator==(const hidl_version& other) const { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 783 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 784 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 785 | |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 786 | bool operator<(const hidl_version& other) const { |
| 787 | return (mMajor < other.get_major() || |
| 788 | (mMajor == other.get_major() && mMinor < other.get_minor())); |
| 789 | } |
| 790 | |
| 791 | bool operator>(const hidl_version& other) const { |
| 792 | return other < *this; |
| 793 | } |
| 794 | |
| 795 | bool operator<=(const hidl_version& other) const { |
| 796 | return !(*this > other); |
| 797 | } |
| 798 | |
| 799 | bool operator>=(const hidl_version& other) const { |
| 800 | return !(*this < other); |
| 801 | } |
| 802 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 803 | constexpr uint16_t get_major() const { return mMajor; } |
| 804 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 805 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 806 | private: |
| 807 | uint16_t mMajor; |
| 808 | uint16_t mMinor; |
| 809 | }; |
| 810 | |
| 811 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 812 | return hidl_version(major,minor); |
| 813 | } |
| 814 | |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 815 | #if defined(__LP64__) |
| 816 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/" |
| 817 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/" |
| 818 | #define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/" |
| 819 | #else |
| 820 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/" |
| 821 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/" |
| 822 | #define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/" |
| 823 | #endif |
| 824 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 825 | // ---------------------------------------------------------------------- |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 826 | // Class that provides Hidl instrumentation utilities. |
| 827 | struct HidlInstrumentor { |
| 828 | // Event that triggers the instrumentation. e.g. enter of an API call on |
| 829 | // the server/client side, exit of an API call on the server/client side |
| 830 | // etc. |
| 831 | enum InstrumentationEvent { |
| 832 | SERVER_API_ENTRY = 0, |
| 833 | SERVER_API_EXIT, |
| 834 | CLIENT_API_ENTRY, |
| 835 | CLIENT_API_EXIT, |
| 836 | SYNC_CALLBACK_ENTRY, |
| 837 | SYNC_CALLBACK_EXIT, |
| 838 | ASYNC_CALLBACK_ENTRY, |
| 839 | ASYNC_CALLBACK_EXIT, |
Steven Moreland | cefbd6e | 2016-11-01 10:14:30 -0700 | [diff] [blame] | 840 | PASSTHROUGH_ENTRY, |
| 841 | PASSTHROUGH_EXIT, |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 842 | }; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 843 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 844 | // Signature of the instrumentation callback function. |
| 845 | using InstrumentationCallback = std::function<void( |
| 846 | const InstrumentationEvent event, |
| 847 | const char *package, |
| 848 | const char *version, |
| 849 | const char *interface, |
| 850 | const char *method, |
| 851 | std::vector<void *> *args)>; |
| 852 | |
| 853 | explicit HidlInstrumentor(const std::string &prefix); |
| 854 | virtual ~HidlInstrumentor(); |
| 855 | |
| 856 | protected: |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 857 | // Set mEnableInstrumentation based on system property |
| 858 | // hal.instrumentation.enable, register/de-register instrumentation |
| 859 | // callbacks if mEnableInstrumentation is true/false. |
| 860 | void configureInstrumentation(bool log=true); |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 861 | // Function that lookup and dynamically loads the hidl instrumentation |
| 862 | // libraries and registers the instrumentation callback functions. |
| 863 | // |
| 864 | // The instrumentation libraries should be stored under any of the following |
| 865 | // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and |
| 866 | // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should |
| 867 | // follow pattern: ^profilerPrefix(.*).profiler.so$ |
| 868 | // |
| 869 | // Each instrumentation library is expected to implement the instrumentation |
| 870 | // function called HIDL_INSTRUMENTATION_FUNCTION. |
| 871 | // |
| 872 | // A no-op for user build. |
| 873 | void registerInstrumentationCallbacks( |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 874 | std::vector<InstrumentationCallback> *instrumentationCallbacks); |
| 875 | |
| 876 | // Utility function to determine whether a give file is a instrumentation |
| 877 | // library (i.e. the file name follow the expected pattern). |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 878 | bool isInstrumentationLib(const dirent *file); |
| 879 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 880 | // A list of registered instrumentation callbacks. |
| 881 | std::vector<InstrumentationCallback> mInstrumentationCallbacks; |
| 882 | // Flag whether to enable instrumentation. |
| 883 | bool mEnableInstrumentation; |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 884 | // Prefix to lookup the instrumentation libraries. |
| 885 | std::string mInstrumentationLibPrefix; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 886 | }; |
| 887 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame^] | 888 | ///////////////////// toString functions |
| 889 | |
| 890 | namespace details { |
| 891 | |
| 892 | // toString alias for numeric types |
| 893 | template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> |
| 894 | inline std::string toString(T t) { |
| 895 | return std::to_string(t); |
| 896 | } |
| 897 | |
| 898 | template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> |
| 899 | inline std::string toHexString(T t, bool prefix = true) { |
| 900 | std::ostringstream os; |
| 901 | if (prefix) { os << std::showbase; } |
| 902 | os << std::hex << t; |
| 903 | return os.str(); |
| 904 | } |
| 905 | |
| 906 | template<> |
| 907 | inline std::string toHexString(uint8_t t, bool prefix) { |
| 908 | return toHexString(static_cast<int32_t>(t), prefix); |
| 909 | } |
| 910 | |
| 911 | template<> |
| 912 | inline std::string toHexString(int8_t t, bool prefix) { |
| 913 | return toHexString(static_cast<int32_t>(t), prefix); |
| 914 | } |
| 915 | |
| 916 | inline std::string toString(const void *t, bool prefix = true) { |
| 917 | return toHexString(reinterpret_cast<uintptr_t>(t), prefix); |
| 918 | } |
| 919 | |
| 920 | // debug string dump. There will be quotes around the string! |
| 921 | inline std::string toString(const hidl_string &hs) { |
| 922 | return std::string{"\""} + hs.c_str() + "\""; |
| 923 | } |
| 924 | |
| 925 | // debug string dump |
| 926 | inline std::string toString(const hidl_handle &hs) { |
| 927 | return toString(hs.getNativeHandle()); |
| 928 | } |
| 929 | |
| 930 | inline std::string toString(const hidl_memory &mem) { |
| 931 | return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = " |
| 932 | + toString(mem.size()) |
| 933 | + ", .handle = " + toString(mem.handle()) + "}"; |
| 934 | } |
| 935 | |
| 936 | inline std::string toString(const sp<hidl_death_recipient> &dr) { |
| 937 | return std::string{"death_recipient@"} + toString(dr.get()); |
| 938 | } |
| 939 | |
| 940 | template<typename Array> |
| 941 | std::string arrayToString(const Array &a, size_t size); |
| 942 | |
| 943 | // debug string dump, assuming that toString(T) is defined. |
| 944 | template<typename T> |
| 945 | std::string toString(const hidl_vec<T> &a) { |
| 946 | std::string os; |
| 947 | os += "[" + toString(a.size()) + "]"; |
| 948 | os += arrayToString(a, a.size()); |
| 949 | return os; |
| 950 | } |
| 951 | |
| 952 | template<size_t SIZE1> |
| 953 | std::string arraySizeToString() { |
| 954 | return std::string{"["} + toString(SIZE1) + "]"; |
| 955 | } |
| 956 | |
| 957 | template<typename T, size_t SIZE1> |
| 958 | std::string toString(const_accessor<T, SIZE1> a) { |
| 959 | return arrayToString(a, SIZE1); |
| 960 | } |
| 961 | |
| 962 | template<typename T, size_t SIZE1> |
| 963 | std::string toString(const hidl_array<T, SIZE1> &a) { |
| 964 | return arraySizeToString<SIZE1>() |
| 965 | + toString(const_accessor<T, SIZE1>(a.data())); |
| 966 | } |
| 967 | |
| 968 | template<size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 969 | std::string arraySizeToString() { |
| 970 | return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>(); |
| 971 | } |
| 972 | |
| 973 | |
| 974 | template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 975 | std::string toString(const_accessor<T, SIZE1, SIZE2, SIZES...> a) { |
| 976 | return arrayToString(a, SIZE1); |
| 977 | } |
| 978 | |
| 979 | template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 980 | std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) { |
| 981 | return arraySizeToString<SIZE1, SIZE2, SIZES...>() |
| 982 | + toString(const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data())); |
| 983 | } |
| 984 | |
| 985 | template<typename Array> |
| 986 | std::string arrayToString(const Array &a, size_t size) { |
| 987 | std::string os; |
| 988 | os += "{"; |
| 989 | for (size_t i = 0; i < size; ++i) { |
| 990 | if (i > 0) { |
| 991 | os += ", "; |
| 992 | } |
| 993 | os += toString(a[i]); |
| 994 | } |
| 995 | os += "}"; |
| 996 | return os; |
| 997 | } |
| 998 | |
| 999 | } // namespace details |
| 1000 | |
| 1001 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 1002 | } // namespace hardware |
| 1003 | } // namespace android |
| 1004 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 1005 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 1006 | #endif // ANDROID_HIDL_SUPPORT_H |