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