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