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 | */ |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 16 | #define LOG_TAG "HidlSupport" |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 17 | |
| 18 | #include <hidl/HidlSupport.h> |
| 19 | |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 20 | #include <unordered_map> |
| 21 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 23 | #include <android-base/parseint.h> |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 24 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | |
Yifan Hong | 24332ef | 2017-03-07 16:22:19 -0800 | [diff] [blame] | 28 | namespace details { |
| 29 | bool debuggable() { |
| 30 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 31 | return true; |
| 32 | #else |
| 33 | return false; |
| 34 | #endif |
| 35 | } |
| 36 | } // namespace details |
| 37 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 38 | hidl_handle::hidl_handle() { |
| 39 | mHandle = nullptr; |
| 40 | mOwnsHandle = false; |
| 41 | } |
| 42 | |
| 43 | hidl_handle::~hidl_handle() { |
| 44 | freeHandle(); |
| 45 | } |
| 46 | |
| 47 | hidl_handle::hidl_handle(const native_handle_t *handle) { |
| 48 | mHandle = handle; |
| 49 | mOwnsHandle = false; |
| 50 | } |
| 51 | |
| 52 | // copy constructor. |
| 53 | hidl_handle::hidl_handle(const hidl_handle &other) { |
| 54 | mOwnsHandle = false; |
| 55 | *this = other; |
| 56 | } |
| 57 | |
| 58 | // move constructor. |
| 59 | hidl_handle::hidl_handle(hidl_handle &&other) { |
| 60 | mOwnsHandle = false; |
| 61 | *this = std::move(other); |
| 62 | } |
| 63 | |
| 64 | // assignment operators |
| 65 | hidl_handle &hidl_handle::operator=(const hidl_handle &other) { |
| 66 | if (this == &other) { |
| 67 | return *this; |
| 68 | } |
| 69 | freeHandle(); |
| 70 | if (other.mHandle != nullptr) { |
| 71 | mHandle = native_handle_clone(other.mHandle); |
| 72 | if (mHandle == nullptr) { |
| 73 | LOG(FATAL) << "Failed to clone native_handle in hidl_handle."; |
| 74 | } |
| 75 | mOwnsHandle = true; |
| 76 | } else { |
| 77 | mHandle = nullptr; |
| 78 | mOwnsHandle = false; |
| 79 | } |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) { |
| 84 | freeHandle(); |
| 85 | mHandle = native_handle; |
| 86 | mOwnsHandle = false; |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | hidl_handle &hidl_handle::operator=(hidl_handle &&other) { |
| 91 | if (this != &other) { |
| 92 | freeHandle(); |
| 93 | mHandle = other.mHandle; |
| 94 | mOwnsHandle = other.mOwnsHandle; |
| 95 | other.mHandle = nullptr; |
| 96 | other.mOwnsHandle = false; |
| 97 | } |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) { |
| 102 | mHandle = handle; |
| 103 | mOwnsHandle = shouldOwn; |
| 104 | } |
| 105 | |
| 106 | const native_handle_t* hidl_handle::operator->() const { |
| 107 | return mHandle; |
| 108 | } |
| 109 | |
| 110 | // implicit conversion to const native_handle_t* |
| 111 | hidl_handle::operator const native_handle_t *() const { |
| 112 | return mHandle; |
| 113 | } |
| 114 | |
| 115 | // explicit conversion |
| 116 | const native_handle_t *hidl_handle::getNativeHandle() const { |
| 117 | return mHandle; |
| 118 | } |
| 119 | |
| 120 | void hidl_handle::freeHandle() { |
| 121 | if (mOwnsHandle && mHandle != nullptr) { |
| 122 | // This can only be true if: |
| 123 | // 1. Somebody called setTo() with shouldOwn=true, so we know the handle |
| 124 | // wasn't const to begin with. |
| 125 | // 2. Copy/assignment from another hidl_handle, in which case we have |
| 126 | // cloned the handle. |
| 127 | // 3. Move constructor from another hidl_handle, in which case the original |
| 128 | // hidl_handle must have been non-const as well. |
| 129 | native_handle_t *handle = const_cast<native_handle_t*>( |
| 130 | static_cast<const native_handle_t*>(mHandle)); |
| 131 | native_handle_close(handle); |
| 132 | native_handle_delete(handle); |
| 133 | mHandle = nullptr; |
| 134 | } |
| 135 | } |
| 136 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 137 | static const char *const kEmptyString = ""; |
| 138 | |
| 139 | hidl_string::hidl_string() |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 140 | : mBuffer(kEmptyString), |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 141 | mSize(0), |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 142 | mOwnsBuffer(false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | hidl_string::~hidl_string() { |
| 146 | clear(); |
| 147 | } |
| 148 | |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 149 | hidl_string::hidl_string(const char *s) : hidl_string() { |
Steven Moreland | a21d84f | 2017-02-16 09:23:45 -0800 | [diff] [blame] | 150 | if (s == nullptr) { |
| 151 | return; |
| 152 | } |
| 153 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 154 | copyFrom(s, strlen(s)); |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 157 | hidl_string::hidl_string(const char *s, size_t length) : hidl_string() { |
| 158 | copyFrom(s, length); |
| 159 | } |
| 160 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 161 | hidl_string::hidl_string(const hidl_string &other): hidl_string() { |
| 162 | copyFrom(other.c_str(), other.size()); |
| 163 | } |
| 164 | |
| 165 | hidl_string::hidl_string(const std::string &s) : hidl_string() { |
| 166 | copyFrom(s.c_str(), s.size()); |
| 167 | } |
| 168 | |
| 169 | hidl_string::hidl_string(hidl_string &&other): hidl_string() { |
| 170 | moveFrom(std::forward<hidl_string>(other)); |
| 171 | } |
| 172 | |
| 173 | hidl_string &hidl_string::operator=(hidl_string &&other) { |
| 174 | if (this != &other) { |
| 175 | clear(); |
| 176 | moveFrom(std::forward<hidl_string>(other)); |
| 177 | } |
| 178 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | hidl_string &hidl_string::operator=(const hidl_string &other) { |
| 182 | if (this != &other) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 183 | clear(); |
| 184 | copyFrom(other.c_str(), other.size()); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return *this; |
| 188 | } |
| 189 | |
| 190 | hidl_string &hidl_string::operator=(const char *s) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 191 | clear(); |
Steven Moreland | 153f87a | 2017-02-28 09:42:26 -0800 | [diff] [blame] | 192 | |
| 193 | if (s == nullptr) { |
| 194 | return *this; |
| 195 | } |
| 196 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 197 | copyFrom(s, strlen(s)); |
| 198 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 201 | hidl_string &hidl_string::operator=(const std::string &s) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 202 | clear(); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 203 | copyFrom(s.c_str(), s.size()); |
| 204 | return *this; |
| 205 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 206 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 207 | hidl_string::operator std::string() const { |
| 208 | return std::string(mBuffer, mSize); |
| 209 | } |
| 210 | |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame^] | 211 | std::ostream& operator<<(std::ostream& os, const hidl_string& str) { |
| 212 | os << str.c_str(); |
| 213 | return os; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void hidl_string::copyFrom(const char *data, size_t size) { |
| 217 | // assume my resources are freed. |
| 218 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 219 | if (size > UINT32_MAX) { |
| 220 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 221 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 222 | char *buf = (char *)malloc(size + 1); |
| 223 | memcpy(buf, data, size); |
| 224 | buf[size] = '\0'; |
| 225 | mBuffer = buf; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 226 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 227 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 228 | mOwnsBuffer = true; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 229 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 230 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 231 | void hidl_string::moveFrom(hidl_string &&other) { |
| 232 | // assume my resources are freed. |
| 233 | |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 234 | mBuffer = std::move(other.mBuffer); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 235 | mSize = other.mSize; |
| 236 | mOwnsBuffer = other.mOwnsBuffer; |
| 237 | |
| 238 | other.mOwnsBuffer = false; |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 239 | other.clear(); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void hidl_string::clear() { |
| 243 | if (mOwnsBuffer && (mBuffer != kEmptyString)) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 244 | free(const_cast<char *>(static_cast<const char *>(mBuffer))); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 247 | mBuffer = kEmptyString; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 248 | mSize = 0; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 249 | mOwnsBuffer = false; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void hidl_string::setToExternal(const char *data, size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 253 | if (size > UINT32_MAX) { |
| 254 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 255 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 256 | clear(); |
| 257 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 258 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 259 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 260 | mOwnsBuffer = false; |
| 261 | } |
| 262 | |
| 263 | const char *hidl_string::c_str() const { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 264 | return mBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | size_t hidl_string::size() const { |
| 268 | return mSize; |
| 269 | } |
| 270 | |
| 271 | bool hidl_string::empty() const { |
| 272 | return mSize == 0; |
| 273 | } |
| 274 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 275 | } // namespace hardware |
| 276 | } // namespace android |
| 277 | |
| 278 | |