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