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