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