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