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