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> |
| 24 | #include <hidl-util/FQName.h> |
Yifan Hong | 64a41d5 | 2017-02-27 18:52:44 -0800 | [diff] [blame] | 25 | #include <vintf/VintfObject.h> |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 26 | #include <vintf/parse_string.h> |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 27 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace hardware { |
| 30 | |
Yifan Hong | 64a41d5 | 2017-02-27 18:52:44 -0800 | [diff] [blame] | 31 | vintf::Transport getTransportFromManifest( |
Yifan Hong | 37b3620 | 2017-02-28 16:04:22 -0800 | [diff] [blame] | 32 | const FQName &fqName, const std::string &instanceName, |
Yifan Hong | 64a41d5 | 2017-02-27 18:52:44 -0800 | [diff] [blame] | 33 | const vintf::HalManifest *vm) { |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 34 | if (vm == nullptr) { |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 35 | return vintf::Transport::EMPTY; |
| 36 | } |
Yifan Hong | 1152a2a | 2017-03-13 15:26:01 -0700 | [diff] [blame] | 37 | return vm->getTransport(fqName.package(), |
Yifan Hong | 37b3620 | 2017-02-28 16:04:22 -0800 | [diff] [blame] | 38 | vintf::Version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()}, |
| 39 | fqName.name(), instanceName); |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Yifan Hong | 37b3620 | 2017-02-28 16:04:22 -0800 | [diff] [blame] | 42 | vintf::Transport getTransport(const std::string &interfaceName, const std::string &instanceName) { |
| 43 | FQName fqName(interfaceName); |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 44 | if (!fqName.isValid()) { |
Yifan Hong | 37b3620 | 2017-02-28 16:04:22 -0800 | [diff] [blame] | 45 | LOG(ERROR) << "getTransport: " << interfaceName << " is not a valid fully-qualified name."; |
Steven Moreland | 6d8960d | 2017-02-23 13:19:32 -0800 | [diff] [blame] | 46 | return vintf::Transport::EMPTY; |
| 47 | } |
| 48 | if (!fqName.hasVersion()) { |
| 49 | LOG(ERROR) << "getTransport: " << fqName.string() |
| 50 | << " does not specify a version. Using default transport."; |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 51 | return vintf::Transport::EMPTY; |
| 52 | } |
Yifan Hong | 37b3620 | 2017-02-28 16:04:22 -0800 | [diff] [blame] | 53 | if (fqName.name().empty()) { |
| 54 | LOG(ERROR) << "getTransport: " << fqName.string() |
| 55 | << " does not specify an interface name. Using default transport."; |
| 56 | return vintf::Transport::EMPTY; |
| 57 | } |
Yifan Hong | 1152a2a | 2017-03-13 15:26:01 -0700 | [diff] [blame] | 58 | |
| 59 | vintf::Transport tr = getTransportFromManifest(fqName, instanceName, |
| 60 | vintf::VintfObject::GetFrameworkHalManifest()); |
| 61 | if (tr != vintf::Transport::EMPTY) { |
| 62 | return tr; |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 63 | } |
Yifan Hong | 1152a2a | 2017-03-13 15:26:01 -0700 | [diff] [blame] | 64 | tr = getTransportFromManifest(fqName, instanceName, |
Yifan Hong | 64a41d5 | 2017-02-27 18:52:44 -0800 | [diff] [blame] | 65 | vintf::VintfObject::GetDeviceHalManifest()); |
Yifan Hong | 1152a2a | 2017-03-13 15:26:01 -0700 | [diff] [blame] | 66 | if (tr != vintf::Transport::EMPTY) { |
| 67 | return tr; |
| 68 | } |
| 69 | |
| 70 | LOG(WARNING) << "getTransportFromManifest: Cannot find entry " |
| 71 | << fqName.string() |
| 72 | << " in either framework or device manifest, using default transport."; |
| 73 | return vintf::Transport::EMPTY; |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Yifan Hong | 24332ef | 2017-03-07 16:22:19 -0800 | [diff] [blame] | 76 | namespace details { |
| 77 | bool debuggable() { |
| 78 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 79 | return true; |
| 80 | #else |
| 81 | return false; |
| 82 | #endif |
| 83 | } |
| 84 | } // namespace details |
| 85 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 86 | hidl_handle::hidl_handle() { |
| 87 | mHandle = nullptr; |
| 88 | mOwnsHandle = false; |
| 89 | } |
| 90 | |
| 91 | hidl_handle::~hidl_handle() { |
| 92 | freeHandle(); |
| 93 | } |
| 94 | |
| 95 | hidl_handle::hidl_handle(const native_handle_t *handle) { |
| 96 | mHandle = handle; |
| 97 | mOwnsHandle = false; |
| 98 | } |
| 99 | |
| 100 | // copy constructor. |
| 101 | hidl_handle::hidl_handle(const hidl_handle &other) { |
| 102 | mOwnsHandle = false; |
| 103 | *this = other; |
| 104 | } |
| 105 | |
| 106 | // move constructor. |
| 107 | hidl_handle::hidl_handle(hidl_handle &&other) { |
| 108 | mOwnsHandle = false; |
| 109 | *this = std::move(other); |
| 110 | } |
| 111 | |
| 112 | // assignment operators |
| 113 | hidl_handle &hidl_handle::operator=(const hidl_handle &other) { |
| 114 | if (this == &other) { |
| 115 | return *this; |
| 116 | } |
| 117 | freeHandle(); |
| 118 | if (other.mHandle != nullptr) { |
| 119 | mHandle = native_handle_clone(other.mHandle); |
| 120 | if (mHandle == nullptr) { |
| 121 | LOG(FATAL) << "Failed to clone native_handle in hidl_handle."; |
| 122 | } |
| 123 | mOwnsHandle = true; |
| 124 | } else { |
| 125 | mHandle = nullptr; |
| 126 | mOwnsHandle = false; |
| 127 | } |
| 128 | return *this; |
| 129 | } |
| 130 | |
| 131 | hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) { |
| 132 | freeHandle(); |
| 133 | mHandle = native_handle; |
| 134 | mOwnsHandle = false; |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | hidl_handle &hidl_handle::operator=(hidl_handle &&other) { |
| 139 | if (this != &other) { |
| 140 | freeHandle(); |
| 141 | mHandle = other.mHandle; |
| 142 | mOwnsHandle = other.mOwnsHandle; |
| 143 | other.mHandle = nullptr; |
| 144 | other.mOwnsHandle = false; |
| 145 | } |
| 146 | return *this; |
| 147 | } |
| 148 | |
| 149 | void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) { |
Scott Randolph | ca37c0e | 2017-02-15 16:38:46 -0800 | [diff] [blame] | 150 | freeHandle(); |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 151 | mHandle = handle; |
| 152 | mOwnsHandle = shouldOwn; |
| 153 | } |
| 154 | |
| 155 | const native_handle_t* hidl_handle::operator->() const { |
| 156 | return mHandle; |
| 157 | } |
| 158 | |
| 159 | // implicit conversion to const native_handle_t* |
| 160 | hidl_handle::operator const native_handle_t *() const { |
| 161 | return mHandle; |
| 162 | } |
| 163 | |
| 164 | // explicit conversion |
| 165 | const native_handle_t *hidl_handle::getNativeHandle() const { |
| 166 | return mHandle; |
| 167 | } |
| 168 | |
| 169 | void hidl_handle::freeHandle() { |
| 170 | if (mOwnsHandle && mHandle != nullptr) { |
| 171 | // This can only be true if: |
| 172 | // 1. Somebody called setTo() with shouldOwn=true, so we know the handle |
| 173 | // wasn't const to begin with. |
| 174 | // 2. Copy/assignment from another hidl_handle, in which case we have |
| 175 | // cloned the handle. |
| 176 | // 3. Move constructor from another hidl_handle, in which case the original |
| 177 | // hidl_handle must have been non-const as well. |
| 178 | native_handle_t *handle = const_cast<native_handle_t*>( |
| 179 | static_cast<const native_handle_t*>(mHandle)); |
| 180 | native_handle_close(handle); |
| 181 | native_handle_delete(handle); |
| 182 | mHandle = nullptr; |
| 183 | } |
| 184 | } |
| 185 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 186 | static const char *const kEmptyString = ""; |
| 187 | |
| 188 | hidl_string::hidl_string() |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 189 | : mBuffer(kEmptyString), |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 190 | mSize(0), |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 191 | mOwnsBuffer(false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | hidl_string::~hidl_string() { |
| 195 | clear(); |
| 196 | } |
| 197 | |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 198 | hidl_string::hidl_string(const char *s) : hidl_string() { |
Steven Moreland | a21d84f | 2017-02-16 09:23:45 -0800 | [diff] [blame] | 199 | if (s == nullptr) { |
| 200 | return; |
| 201 | } |
| 202 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 203 | copyFrom(s, strlen(s)); |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 206 | hidl_string::hidl_string(const char *s, size_t length) : hidl_string() { |
| 207 | copyFrom(s, length); |
| 208 | } |
| 209 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 210 | hidl_string::hidl_string(const hidl_string &other): hidl_string() { |
| 211 | copyFrom(other.c_str(), other.size()); |
| 212 | } |
| 213 | |
| 214 | hidl_string::hidl_string(const std::string &s) : hidl_string() { |
| 215 | copyFrom(s.c_str(), s.size()); |
| 216 | } |
| 217 | |
| 218 | hidl_string::hidl_string(hidl_string &&other): hidl_string() { |
| 219 | moveFrom(std::forward<hidl_string>(other)); |
| 220 | } |
| 221 | |
| 222 | hidl_string &hidl_string::operator=(hidl_string &&other) { |
| 223 | if (this != &other) { |
| 224 | clear(); |
| 225 | moveFrom(std::forward<hidl_string>(other)); |
| 226 | } |
| 227 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | hidl_string &hidl_string::operator=(const hidl_string &other) { |
| 231 | if (this != &other) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 232 | clear(); |
| 233 | copyFrom(other.c_str(), other.size()); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return *this; |
| 237 | } |
| 238 | |
| 239 | hidl_string &hidl_string::operator=(const char *s) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 240 | clear(); |
Steven Moreland | 153f87a | 2017-02-28 09:42:26 -0800 | [diff] [blame] | 241 | |
| 242 | if (s == nullptr) { |
| 243 | return *this; |
| 244 | } |
| 245 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 246 | copyFrom(s, strlen(s)); |
| 247 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 250 | hidl_string &hidl_string::operator=(const std::string &s) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 251 | clear(); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 252 | copyFrom(s.c_str(), s.size()); |
| 253 | return *this; |
| 254 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 255 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 256 | hidl_string::operator std::string() const { |
| 257 | return std::string(mBuffer, mSize); |
| 258 | } |
| 259 | |
Scott Randolph | 0c84ab4 | 2017-04-03 14:07:14 -0700 | [diff] [blame^] | 260 | std::ostream& operator<<(std::ostream& os, const hidl_string& str) { |
| 261 | os << str.c_str(); |
| 262 | return os; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void hidl_string::copyFrom(const char *data, size_t size) { |
| 266 | // assume my resources are freed. |
| 267 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 268 | if (size > UINT32_MAX) { |
| 269 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 270 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 271 | char *buf = (char *)malloc(size + 1); |
| 272 | memcpy(buf, data, size); |
| 273 | buf[size] = '\0'; |
| 274 | mBuffer = buf; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 275 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 276 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 277 | mOwnsBuffer = true; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 278 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 279 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 280 | void hidl_string::moveFrom(hidl_string &&other) { |
| 281 | // assume my resources are freed. |
| 282 | |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 283 | mBuffer = std::move(other.mBuffer); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 284 | mSize = other.mSize; |
| 285 | mOwnsBuffer = other.mOwnsBuffer; |
| 286 | |
| 287 | other.mOwnsBuffer = false; |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 288 | other.clear(); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void hidl_string::clear() { |
| 292 | if (mOwnsBuffer && (mBuffer != kEmptyString)) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 293 | free(const_cast<char *>(static_cast<const char *>(mBuffer))); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 296 | mBuffer = kEmptyString; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 297 | mSize = 0; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 298 | mOwnsBuffer = false; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void hidl_string::setToExternal(const char *data, size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 302 | if (size > UINT32_MAX) { |
| 303 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 304 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 305 | clear(); |
| 306 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 307 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 308 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 309 | mOwnsBuffer = false; |
| 310 | } |
| 311 | |
| 312 | const char *hidl_string::c_str() const { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 313 | return mBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | size_t hidl_string::size() const { |
| 317 | return mSize; |
| 318 | } |
| 319 | |
| 320 | bool hidl_string::empty() const { |
| 321 | return mSize == 0; |
| 322 | } |
| 323 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 324 | } // namespace hardware |
| 325 | } // namespace android |
| 326 | |
| 327 | |