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