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 | 8e0cfc6 | 2017-02-17 10:18:54 -0800 | [diff] [blame] | 25 | #include <vintf/HalManifest.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 { |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 30 | vintf::Transport getTransportForFrameworkPackages(const std::string &name) { |
Steven Moreland | 346cf51 | 2017-02-16 19:23:24 -0800 | [diff] [blame^] | 31 | // TODO(b/34772739): move to framework vintf |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 32 | const static std::unordered_map<std::string, vintf::Transport> sTransports { |
| 33 | {"android.hidl.manager@1.0::IServiceManager", vintf::Transport::HWBINDER}, |
Steven Moreland | 346cf51 | 2017-02-16 19:23:24 -0800 | [diff] [blame^] | 34 | {"android.hidl.allocator@1.0::IAllocator" , vintf::Transport::HWBINDER}, |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 35 | {"android.hidl.memory@1.0::IMapper" , vintf::Transport::PASSTHROUGH}, |
| 36 | {"android.hidl.memory@1.0::IMemory" , vintf::Transport::PASSTHROUGH}, |
| 37 | }; |
| 38 | auto it = sTransports.find(name); |
| 39 | if (it == sTransports.end()) { |
| 40 | LOG(WARNING) << "getTransportForFrameworkPackages: Cannot find entry " |
| 41 | << name << " in the static map."; |
| 42 | return vintf::Transport::EMPTY; |
| 43 | } else { |
| 44 | LOG(INFO) << "getTransportForFrameworkPackages: " << name |
| 45 | << " declares transport method " << to_string(it->second); |
| 46 | } |
| 47 | return it->second; |
| 48 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 49 | |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 50 | vintf::Transport getTransportForHals(const FQName &fqName) { |
| 51 | const std::string package = fqName.package(); |
Yifan Hong | 8e0cfc6 | 2017-02-17 10:18:54 -0800 | [diff] [blame] | 52 | const vintf::HalManifest *vm = vintf::HalManifest::Get(); |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 53 | if (vm == nullptr) { |
Steven Moreland | 0173273 | 2017-02-02 18:42:49 -0800 | [diff] [blame] | 54 | LOG(WARNING) << "getTransportFromManifest: Cannot find vendor interface manifest."; |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 55 | return vintf::Transport::EMPTY; |
| 56 | } |
Yifan Hong | e682a5a | 2017-02-13 18:14:39 +0000 | [diff] [blame] | 57 | if (!fqName.hasVersion()) { |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 58 | LOG(ERROR) << "getTransportFromManifest: " << fqName.string() |
| 59 | << " does not specify a version."; |
| 60 | return vintf::Transport::EMPTY; |
| 61 | } |
Yifan Hong | e682a5a | 2017-02-13 18:14:39 +0000 | [diff] [blame] | 62 | vintf::Transport tr = vm->getTransport(package, |
| 63 | vintf::Version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()}); |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 64 | if (tr == vintf::Transport::EMPTY) { |
| 65 | LOG(WARNING) << "getTransportFromManifest: Cannot find entry " |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 66 | << package << fqName.atVersion() << " in vendor interface manifest."; |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 67 | } else { |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 68 | LOG(INFO) << "getTransportFromManifest: " << package << fqName.atVersion() |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 69 | << " declares transport method " << to_string(tr); |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 70 | } |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 71 | return tr; |
| 72 | } |
| 73 | |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 74 | vintf::Transport getTransport(const std::string &name) { |
| 75 | FQName fqName(name); |
| 76 | if (!fqName.isValid()) { |
| 77 | LOG(WARNING) << name << " is not a valid fully-qualified name."; |
| 78 | return vintf::Transport::EMPTY; |
| 79 | } |
| 80 | if (fqName.inPackage("android.hidl")) { |
| 81 | return getTransportForFrameworkPackages(name); |
| 82 | } |
| 83 | return getTransportForHals(fqName); |
| 84 | } |
| 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) { |
| 150 | mHandle = handle; |
| 151 | mOwnsHandle = shouldOwn; |
| 152 | } |
| 153 | |
| 154 | const native_handle_t* hidl_handle::operator->() const { |
| 155 | return mHandle; |
| 156 | } |
| 157 | |
| 158 | // implicit conversion to const native_handle_t* |
| 159 | hidl_handle::operator const native_handle_t *() const { |
| 160 | return mHandle; |
| 161 | } |
| 162 | |
| 163 | // explicit conversion |
| 164 | const native_handle_t *hidl_handle::getNativeHandle() const { |
| 165 | return mHandle; |
| 166 | } |
| 167 | |
| 168 | void hidl_handle::freeHandle() { |
| 169 | if (mOwnsHandle && mHandle != nullptr) { |
| 170 | // This can only be true if: |
| 171 | // 1. Somebody called setTo() with shouldOwn=true, so we know the handle |
| 172 | // wasn't const to begin with. |
| 173 | // 2. Copy/assignment from another hidl_handle, in which case we have |
| 174 | // cloned the handle. |
| 175 | // 3. Move constructor from another hidl_handle, in which case the original |
| 176 | // hidl_handle must have been non-const as well. |
| 177 | native_handle_t *handle = const_cast<native_handle_t*>( |
| 178 | static_cast<const native_handle_t*>(mHandle)); |
| 179 | native_handle_close(handle); |
| 180 | native_handle_delete(handle); |
| 181 | mHandle = nullptr; |
| 182 | } |
| 183 | } |
| 184 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 185 | static const char *const kEmptyString = ""; |
| 186 | |
| 187 | hidl_string::hidl_string() |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 188 | : mBuffer(kEmptyString), |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 189 | mSize(0), |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 190 | mOwnsBuffer(false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | hidl_string::~hidl_string() { |
| 194 | clear(); |
| 195 | } |
| 196 | |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 197 | hidl_string::hidl_string(const char *s) : hidl_string() { |
Steven Moreland | a21d84f | 2017-02-16 09:23:45 -0800 | [diff] [blame] | 198 | if (s == nullptr) { |
| 199 | return; |
| 200 | } |
| 201 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 202 | copyFrom(s, strlen(s)); |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 205 | hidl_string::hidl_string(const char *s, size_t length) : hidl_string() { |
| 206 | copyFrom(s, length); |
| 207 | } |
| 208 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 209 | hidl_string::hidl_string(const hidl_string &other): hidl_string() { |
| 210 | copyFrom(other.c_str(), other.size()); |
| 211 | } |
| 212 | |
| 213 | hidl_string::hidl_string(const std::string &s) : hidl_string() { |
| 214 | copyFrom(s.c_str(), s.size()); |
| 215 | } |
| 216 | |
| 217 | hidl_string::hidl_string(hidl_string &&other): hidl_string() { |
| 218 | moveFrom(std::forward<hidl_string>(other)); |
| 219 | } |
| 220 | |
| 221 | hidl_string &hidl_string::operator=(hidl_string &&other) { |
| 222 | if (this != &other) { |
| 223 | clear(); |
| 224 | moveFrom(std::forward<hidl_string>(other)); |
| 225 | } |
| 226 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | hidl_string &hidl_string::operator=(const hidl_string &other) { |
| 230 | if (this != &other) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 231 | clear(); |
| 232 | copyFrom(other.c_str(), other.size()); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | return *this; |
| 236 | } |
| 237 | |
| 238 | hidl_string &hidl_string::operator=(const char *s) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 239 | clear(); |
| 240 | copyFrom(s, strlen(s)); |
| 241 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 244 | hidl_string &hidl_string::operator=(const std::string &s) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 245 | clear(); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 246 | copyFrom(s.c_str(), s.size()); |
| 247 | return *this; |
| 248 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 249 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 250 | hidl_string::operator std::string() const { |
| 251 | return std::string(mBuffer, mSize); |
| 252 | } |
| 253 | |
| 254 | hidl_string::operator const char *() const { |
| 255 | return mBuffer; |
| 256 | } |
| 257 | |
| 258 | void hidl_string::copyFrom(const char *data, size_t size) { |
| 259 | // assume my resources are freed. |
| 260 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 261 | if (size > UINT32_MAX) { |
| 262 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 263 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 264 | char *buf = (char *)malloc(size + 1); |
| 265 | memcpy(buf, data, size); |
| 266 | buf[size] = '\0'; |
| 267 | mBuffer = buf; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 268 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 269 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 270 | mOwnsBuffer = true; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 271 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 272 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 273 | void hidl_string::moveFrom(hidl_string &&other) { |
| 274 | // assume my resources are freed. |
| 275 | |
| 276 | mBuffer = other.mBuffer; |
| 277 | mSize = other.mSize; |
| 278 | mOwnsBuffer = other.mOwnsBuffer; |
| 279 | |
| 280 | other.mOwnsBuffer = false; |
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 | |