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