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