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