Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include "CompatibilityMatrix.h" |
| 18 | |
Yifan Hong | b6c7f49 | 2018-02-27 14:07:57 -0800 | [diff] [blame] | 19 | #include <iostream> |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame^] | 22 | #include <android-base/logging.h> |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 23 | #include <android-base/strings.h> |
| 24 | |
Yifan Hong | bbfff30 | 2017-06-06 17:10:13 -0700 | [diff] [blame] | 25 | #include "parse_string.h" |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 26 | #include "parse_xml.h" |
Yifan Hong | b6c7f49 | 2018-02-27 14:07:57 -0800 | [diff] [blame] | 27 | #include "utils.h" |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 28 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace vintf { |
| 31 | |
Yifan Hong | 4c6962d | 2019-01-30 15:50:46 -0800 | [diff] [blame] | 32 | using details::mergeField; |
| 33 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 34 | bool CompatibilityMatrix::addKernel(MatrixKernel&& kernel, std::string* error) { |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 35 | if (mType != SchemaType::FRAMEWORK) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 36 | if (error) { |
| 37 | *error = "Cannot add <kernel> to a " + to_string(mType) + " compatibility matrix."; |
| 38 | } |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 39 | return false; |
| 40 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 41 | |
| 42 | auto it = framework.mKernels.begin(); |
| 43 | for (; it != framework.mKernels.end(); ++it) { |
| 44 | if (it->minLts() == kernel.minLts()) { |
| 45 | break; |
| 46 | } |
| 47 | if (it->minLts().version == kernel.minLts().version && |
| 48 | it->minLts().majorRev == kernel.minLts().majorRev) { |
| 49 | if (error) { |
| 50 | *error = "Kernel version mismatch; cannot add " + to_string(kernel.minLts()) + |
| 51 | " because " + to_string(it->minLts()) + " was added."; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | bool seenVersion = it != framework.mKernels.end(); |
| 58 | |
| 59 | if (seenVersion) { |
| 60 | // If no conditions, must be the first among the same minLts |
| 61 | // because O libvintf only checks the first <kernel> tag that version matches. |
| 62 | if (kernel.conditions().empty()) { |
| 63 | // Found first <kernel> with the same minLts. |
| 64 | // Append config if it does not have <condition>s, else error. |
| 65 | if (it->conditions().empty()) { |
| 66 | const auto& configs = kernel.configs(); |
| 67 | it->mConfigs.insert(it->mConfigs.end(), configs.begin(), configs.end()); |
| 68 | } else { |
| 69 | if (error) { |
| 70 | *error = |
| 71 | "Base compatibility matrix has <condition> for the first <kernel> " |
| 72 | "with minlts " + |
| 73 | to_string(kernel.minLts()) + " for unknown reason."; |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | } else { |
| 80 | // First <kernel> of a minLts must not have <condition>'s for backwards compatibility |
| 81 | // with O libvintf. |
| 82 | if (!kernel.conditions().empty()) { |
| 83 | framework.mKernels.push_back(MatrixKernel(KernelVersion{kernel.minLts()}, {})); |
| 84 | } |
| 85 | } |
| 86 | |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 87 | framework.mKernels.push_back(std::move(kernel)); |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | |
Yifan Hong | 398f4c7 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 91 | SchemaType CompatibilityMatrix::type() const { |
| 92 | return mType; |
| 93 | } |
| 94 | |
Yifan Hong | 2027a49 | 2017-12-11 15:21:19 -0800 | [diff] [blame] | 95 | Level CompatibilityMatrix::level() const { |
| 96 | return mLevel; |
| 97 | } |
| 98 | |
Yifan Hong | db127cb | 2017-09-19 13:36:21 -0700 | [diff] [blame] | 99 | Version CompatibilityMatrix::getMinimumMetaVersion() const { |
| 100 | // TODO(b/62801658): this needs to depend on whether there are 1.1 requirements |
| 101 | // (e.g. required <xmlfile> entry) |
| 102 | return {1, 0}; |
| 103 | } |
Yifan Hong | 1e5a054 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 104 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 105 | status_t CompatibilityMatrix::fetchAllInformation(const FileSystem* fileSystem, |
| 106 | const std::string& path, std::string* error) { |
| 107 | return details::fetchAllInformation(fileSystem, path, gCompatibilityMatrixConverter, this, |
| 108 | error); |
Yifan Hong | 1e5a054 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Yifan Hong | bbfff30 | 2017-06-06 17:10:13 -0700 | [diff] [blame] | 111 | std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName, |
| 112 | const Version& version) const { |
| 113 | using std::literals::string_literals::operator""s; |
| 114 | auto range = getXmlFiles(xmlFileName); |
| 115 | for (auto it = range.first; it != range.second; ++it) { |
| 116 | const MatrixXmlFile& matrixXmlFile = it->second; |
| 117 | if (matrixXmlFile.versionRange().contains(version)) { |
| 118 | if (!matrixXmlFile.overriddenPath().empty()) { |
| 119 | return matrixXmlFile.overriddenPath(); |
| 120 | } |
| 121 | return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" + |
| 122 | xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) + |
| 123 | "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." + |
| 124 | to_string(matrixXmlFile.format()); |
| 125 | } |
| 126 | } |
| 127 | return ""; |
| 128 | } |
| 129 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 130 | // Split existingHal into a HAL that contains only interface/instance and a HAL |
| 131 | // that does not contain it. Return the HAL that contains only interface/instance. |
| 132 | // - Return nullptr if existingHal does not contain interface/instance |
| 133 | // - Return existingHal if existingHal contains only interface/instance |
| 134 | // - Remove interface/instance from existingHal, and return a new MatrixHal (that is added |
| 135 | // to "this") that contains only interface/instance. |
| 136 | MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface, |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 137 | const std::string& instanceOrPattern, bool isRegex) { |
| 138 | bool found = false; |
| 139 | bool foundOthers = false; |
| 140 | existingHal->forEachInstance([&](const auto& matrixInstance) { |
| 141 | bool interfaceMatch = matrixInstance.interface() == interface; |
| 142 | bool instanceMatch = false; |
| 143 | if (matrixInstance.isRegex() && isRegex) { |
| 144 | instanceMatch = (matrixInstance.regexPattern() == instanceOrPattern); |
| 145 | } else if (!matrixInstance.isRegex() && !isRegex) { |
| 146 | instanceMatch = (matrixInstance.exactInstance() == instanceOrPattern); |
| 147 | } |
| 148 | |
| 149 | bool match = interfaceMatch && instanceMatch; |
| 150 | |
| 151 | found |= match; |
| 152 | foundOthers |= (!match); |
| 153 | |
| 154 | return !found || !foundOthers; |
| 155 | }); |
| 156 | |
| 157 | if (!found) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 158 | return nullptr; |
| 159 | } |
| 160 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 161 | if (!foundOthers) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 162 | return existingHal; |
| 163 | } |
| 164 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 165 | existingHal->removeInstance(interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 166 | MatrixHal copy = *existingHal; |
| 167 | copy.clearInstances(); |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 168 | copy.insertInstance(interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 169 | |
| 170 | return addInternal(std::move(copy)); |
| 171 | } |
| 172 | |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 173 | // Add all package@other_version::interface/instance as an optional instance. |
| 174 | // If package@this_version::interface/instance is in this (that is, some instance |
| 175 | // with the same package and interface and instance exists), then other_version is |
| 176 | // considered a possible replacement to this_version. |
| 177 | // See LibVintfTest.AddOptionalHal* tests for details. |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 178 | bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 179 | if (other == nullptr || other->level() <= level()) { |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | for (auto& pair : other->mHals) { |
| 184 | const std::string& name = pair.first; |
| 185 | MatrixHal& halToAdd = pair.second; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 186 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 187 | std::set<std::pair<std::string, std::string>> insertedInstances; |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 188 | std::set<std::pair<std::string, std::string>> insertedRegex; |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 189 | auto existingHals = getHals(name); |
| 190 | |
| 191 | halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges, |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 192 | const std::string& interface, |
| 193 | const std::string& instanceOrPattern, bool isRegex) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 194 | for (auto* existingHal : existingHals) { |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 195 | MatrixHal* splitInstance = |
| 196 | this->splitInstance(existingHal, interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 197 | if (splitInstance != nullptr) { |
| 198 | splitInstance->insertVersionRanges(versionRanges); |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 199 | if (isRegex) { |
| 200 | insertedRegex.insert(std::make_pair(interface, instanceOrPattern)); |
| 201 | } else { |
| 202 | insertedInstances.insert(std::make_pair(interface, instanceOrPattern)); |
| 203 | } |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 204 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 205 | } |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 206 | return true; |
| 207 | }); |
| 208 | |
| 209 | // Add the remaining instances. |
| 210 | for (const auto& pair : insertedInstances) { |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 211 | halToAdd.removeInstance(pair.first, pair.second, false /* isRegex */); |
| 212 | } |
| 213 | for (const auto& pair : insertedRegex) { |
| 214 | halToAdd.removeInstance(pair.first, pair.second, true /* isRegex */); |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 215 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 216 | |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 217 | if (halToAdd.instancesCount() > 0) { |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 218 | halToAdd.setOptional(true); |
| 219 | if (!add(std::move(halToAdd))) { |
| 220 | if (error) { |
| 221 | *error = "Cannot add HAL " + name + " for unknown reason."; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 222 | } |
| 223 | return false; |
| 224 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
Yifan Hong | d4b92fe | 2017-12-20 15:29:03 -0800 | [diff] [blame] | 230 | bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 231 | if (other == nullptr || other->level() <= level()) { |
| 232 | return true; |
| 233 | } |
| 234 | for (auto& pair : other->mXmlFiles) { |
| 235 | const std::string& name = pair.first; |
| 236 | MatrixXmlFile& xmlFileToAdd = pair.second; |
| 237 | |
| 238 | xmlFileToAdd.mOptional = true; |
| 239 | if (!addXmlFile(std::move(xmlFileToAdd))) { |
| 240 | if (error) { |
| 241 | *error = "Cannot add XML File " + name + " for unknown reason."; |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 249 | // Merge Kernel. |
| 250 | // Add <kernel> from exact "level", then optionally add <kernel> from high levels to low levels. |
| 251 | // For example, (each letter is a kernel version x.y.z) |
| 252 | // 1.xml: A1, B1 |
| 253 | // 2.xml: B2, C2, D2 |
| 254 | // 3.xml: D3, E3 |
| 255 | // Then the combined 1.xml should have |
| 256 | // A1, B1 (from 1.xml, required), C2, D2, E3 (optional, use earliest possible). |
| 257 | bool CompatibilityMatrix::addAllKernels(CompatibilityMatrix* other, std::string* error) { |
| 258 | for (MatrixKernel& kernel : other->framework.mKernels) { |
| 259 | KernelVersion ver = kernel.minLts(); |
| 260 | if (!addKernel(std::move(kernel), error)) { |
| 261 | if (error) { |
| 262 | *error = "Cannot add kernel version " + to_string(ver) + ": " + *error; |
| 263 | } |
| 264 | return false; |
| 265 | } |
| 266 | } |
| 267 | return true; |
| 268 | } |
| 269 | |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 270 | bool CompatibilityMatrix::addAllKernelsAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 271 | if (other == nullptr || other->level() <= level()) { |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | for (MatrixKernel& kernelToAdd : other->framework.mKernels) { |
| 276 | bool exists = |
| 277 | std::any_of(this->framework.mKernels.begin(), this->framework.mKernels.end(), |
| 278 | [&kernelToAdd](const MatrixKernel& existing) { |
| 279 | return kernelToAdd.minLts().version == existing.minLts().version && |
| 280 | kernelToAdd.minLts().majorRev == existing.minLts().majorRev; |
| 281 | }); |
| 282 | |
| 283 | if (exists) { |
| 284 | // Shouldn't retroactively add requirements to minLts(), so ignore this. |
| 285 | // This happens even when kernelToAdd.conditions() != existing.conditions(). |
| 286 | continue; |
| 287 | } |
| 288 | |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame^] | 289 | (void)kernelToAdd.setSourceMatrixLevel(other->level()); |
| 290 | |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 291 | KernelVersion minLts = kernelToAdd.minLts(); |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 292 | if (!addKernel(std::move(kernelToAdd), error)) { |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 293 | if (error) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 294 | *error = "Cannot add " + to_string(minLts) + ": " + *error; |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 302 | bool CompatibilityMatrix::addSepolicy(CompatibilityMatrix* other, std::string* error) { |
| 303 | bool success = mergeField(&this->framework.mSepolicy, &other->framework.mSepolicy); |
| 304 | if (!success && error) *error = "<sepolicy> is already defined"; |
| 305 | return success; |
| 306 | } |
| 307 | |
| 308 | bool CompatibilityMatrix::addAvbMetaVersion(CompatibilityMatrix* other, std::string* error) { |
| 309 | bool success = mergeField(&this->framework.mAvbMetaVersion, &other->framework.mAvbMetaVersion); |
| 310 | if (!success && error) *error = "<avb><vbmeta-version> is already defined"; |
| 311 | return success; |
| 312 | } |
| 313 | |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 314 | bool CompatibilityMatrix::addVndk(CompatibilityMatrix* other, std::string* error) { |
| 315 | #pragma clang diagnostic push |
| 316 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 317 | bool success = mergeField(&this->device.mVndk, &other->device.mVndk); |
| 318 | #pragma clang diagnostic pop |
| 319 | if (!success && error) *error = "<vndk> is already defined"; |
| 320 | return success; |
| 321 | } |
| 322 | |
| 323 | bool CompatibilityMatrix::addVendorNdk(CompatibilityMatrix* other, std::string* error) { |
| 324 | bool success = mergeField(&this->device.mVendorNdk, &other->device.mVendorNdk); |
| 325 | if (!success && error) *error = "<vendor-ndk> is already defined"; |
| 326 | return success; |
| 327 | } |
| 328 | |
| 329 | bool CompatibilityMatrix::addSystemSdk(CompatibilityMatrix* other, std::string* /* error */) { |
| 330 | this->device.mSystemSdk.addAll(&other->device.mSystemSdk); |
| 331 | return true; |
| 332 | } |
| 333 | |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 334 | bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) { |
Yifan Hong | 2027a49 | 2017-12-11 15:21:19 -0800 | [diff] [blame] | 335 | return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals && |
| 336 | lft.mXmlFiles == rgt.mXmlFiles && |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 337 | (lft.mType != SchemaType::DEVICE || |
| 338 | ( |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 339 | #pragma clang diagnostic push |
| 340 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 341 | lft.device.mVndk == rgt.device.mVndk && |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 342 | #pragma clang diagnostic pop |
Yifan Hong | a28729e | 2018-01-17 13:40:35 -0800 | [diff] [blame] | 343 | lft.device.mVendorNdk == rgt.device.mVendorNdk && |
| 344 | lft.device.mSystemSdk == rgt.device.mSystemSdk)) && |
Yifan Hong | d485790 | 2017-06-13 14:13:56 -0700 | [diff] [blame] | 345 | (lft.mType != SchemaType::FRAMEWORK || |
| 346 | (lft.framework.mKernels == rgt.framework.mKernels && |
| 347 | lft.framework.mSepolicy == rgt.framework.mSepolicy && |
| 348 | lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion)); |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 351 | std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combine( |
| 352 | Level deviceLevel, std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) { |
| 353 | // Check type. |
| 354 | for (const auto& e : *matrices) { |
| 355 | if (e.object.type() != SchemaType::FRAMEWORK) { |
| 356 | if (error) { |
| 357 | *error = "File \"" + e.name + "\" is not a framework compatibility matrix."; |
| 358 | return nullptr; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Matrices with unspecified (empty) level are auto-filled with deviceLevel. |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 364 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 365 | if (e.object.level() == Level::UNSPECIFIED) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 366 | e.object.mLevel = deviceLevel; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 370 | // Add from low to high FCM version so that optional <kernel> requirements are added correctly. |
| 371 | // See comment in addAllAsOptional. |
| 372 | std::sort(matrices->begin(), matrices->end(), |
| 373 | [](const auto& x, const auto& y) { return x.object.level() < y.object.level(); }); |
| 374 | |
| 375 | auto baseMatrix = std::make_unique<CompatibilityMatrix>(); |
| 376 | baseMatrix->mLevel = deviceLevel; |
| 377 | baseMatrix->mType = SchemaType::FRAMEWORK; |
| 378 | |
| 379 | std::vector<std::string> parsedFiles; |
| 380 | for (auto& e : *matrices) { |
| 381 | if (e.object.level() < deviceLevel) { |
| 382 | continue; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 383 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 384 | |
| 385 | bool success = false; |
| 386 | if (e.object.level() == deviceLevel) { |
| 387 | success = baseMatrix->addAll(&e, error); |
| 388 | } else { |
| 389 | success = baseMatrix->addAllAsOptional(&e, error); |
| 390 | } |
| 391 | if (!success) { |
| 392 | if (error) { |
| 393 | *error = "Conflict when merging \"" + e.name + "\": " + *error + "\n" + |
| 394 | "Previous files:\n" + base::Join(parsedFiles, "\n"); |
| 395 | } |
| 396 | return nullptr; |
| 397 | } |
| 398 | parsedFiles.push_back(e.name); |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 399 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 400 | |
| 401 | return baseMatrix; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 404 | std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combineDeviceMatrices( |
| 405 | std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) { |
| 406 | auto baseMatrix = std::make_unique<CompatibilityMatrix>(); |
| 407 | baseMatrix->mType = SchemaType::DEVICE; |
| 408 | |
| 409 | std::vector<std::string> parsedFiles; |
| 410 | for (auto& e : *matrices) { |
| 411 | bool success = baseMatrix->addAll(&e, error); |
| 412 | if (!success) { |
| 413 | if (error) { |
| 414 | *error = "Conflict when merging \"" + e.name + "\": " + *error + "\n" + |
| 415 | "Previous files:\n" + base::Join(parsedFiles, "\n"); |
| 416 | } |
| 417 | return nullptr; |
| 418 | } |
| 419 | parsedFiles.push_back(e.name); |
| 420 | } |
| 421 | return baseMatrix; |
| 422 | } |
| 423 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 424 | bool CompatibilityMatrix::addAll(Named<CompatibilityMatrix>* inputMatrix, std::string* error) { |
| 425 | if (!addAllHals(&inputMatrix->object, error) || !addAllXmlFiles(&inputMatrix->object, error) || |
| 426 | !addAllKernels(&inputMatrix->object, error) || !addSepolicy(&inputMatrix->object, error) || |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 427 | !addAvbMetaVersion(&inputMatrix->object, error) || !addVndk(&inputMatrix->object, error) || |
| 428 | !addVendorNdk(&inputMatrix->object, error) || !addSystemSdk(&inputMatrix->object, error)) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 429 | if (error) { |
| 430 | *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error + "."; |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 431 | } |
Yifan Hong | 5390e3b | 2019-01-31 14:13:00 -0800 | [diff] [blame] | 432 | return false; |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 433 | } |
| 434 | return true; |
| 435 | } |
| 436 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 437 | bool CompatibilityMatrix::addAllAsOptional(Named<CompatibilityMatrix>* inputMatrix, |
| 438 | std::string* error) { |
| 439 | if (!addAllHalsAsOptional(&inputMatrix->object, error) || |
| 440 | !addAllXmlFilesAsOptional(&inputMatrix->object, error) || |
| 441 | !addAllKernelsAsOptional(&inputMatrix->object, error)) { |
| 442 | if (error) { |
| 443 | *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 444 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 445 | return false; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 446 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 447 | // ignore <sepolicy> requirement from higher level |
| 448 | // ignore <avb> requirement from higher level |
| 449 | return true; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 452 | bool CompatibilityMatrix::forEachInstanceOfVersion( |
| 453 | const std::string& package, const Version& expectVersion, |
| 454 | const std::function<bool(const MatrixInstance&)>& func) const { |
| 455 | for (const MatrixHal* hal : getHals(package)) { |
| 456 | bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) { |
| 457 | if (matrixInstance.versionRange().contains(expectVersion)) { |
| 458 | return func(matrixInstance); |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 459 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 460 | return true; |
| 461 | }); |
| 462 | if (!cont) return false; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 463 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 464 | return true; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Yifan Hong | def7e7f | 2018-03-20 13:27:36 -0700 | [diff] [blame] | 467 | bool CompatibilityMatrix::matchInstance(const std::string& halName, const Version& version, |
| 468 | const std::string& interfaceName, |
| 469 | const std::string& instance) const { |
| 470 | bool found = false; |
| 471 | (void)forEachInstanceOfInterface(halName, version, interfaceName, |
| 472 | [&found, &instance](const auto& e) { |
| 473 | found |= (e.matchInstance(instance)); |
| 474 | return !found; // if not found, continue |
| 475 | }); |
| 476 | return found; |
| 477 | } |
| 478 | |
Yifan Hong | 52b7fae | 2018-05-22 16:21:31 -0700 | [diff] [blame] | 479 | std::string CompatibilityMatrix::getVendorNdkVersion() const { |
| 480 | return type() == SchemaType::DEVICE ? device.mVendorNdk.version() : ""; |
| 481 | } |
| 482 | |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame^] | 483 | Level CompatibilityMatrix::getSourceMatrixLevel(const MatrixKernel* matrixKernel) const { |
| 484 | CHECK(std::find_if(framework.mKernels.begin(), framework.mKernels.end(), |
| 485 | [matrixKernel](const auto& e) { return &e == matrixKernel; }) != |
| 486 | framework.mKernels.end()); |
| 487 | Level ret = matrixKernel->getSourceMatrixLevel(); |
| 488 | if (ret != Level::UNSPECIFIED) return ret; |
| 489 | return level(); |
| 490 | } |
| 491 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 492 | } // namespace vintf |
| 493 | } // namespace android |