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