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 | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 55 | status_t CompatibilityMatrix::fetchAllInformation(const std::string& path, std::string* error) { |
| 56 | return details::fetchAllInformation(path, gCompatibilityMatrixConverter, this, error); |
Yifan Hong | 1e5a054 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Yifan Hong | bbfff30 | 2017-06-06 17:10:13 -0700 | [diff] [blame] | 59 | std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName, |
| 60 | const Version& version) const { |
| 61 | using std::literals::string_literals::operator""s; |
| 62 | auto range = getXmlFiles(xmlFileName); |
| 63 | for (auto it = range.first; it != range.second; ++it) { |
| 64 | const MatrixXmlFile& matrixXmlFile = it->second; |
| 65 | if (matrixXmlFile.versionRange().contains(version)) { |
| 66 | if (!matrixXmlFile.overriddenPath().empty()) { |
| 67 | return matrixXmlFile.overriddenPath(); |
| 68 | } |
| 69 | return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" + |
| 70 | xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) + |
| 71 | "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." + |
| 72 | to_string(matrixXmlFile.format()); |
| 73 | } |
| 74 | } |
| 75 | return ""; |
| 76 | } |
| 77 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 78 | // Split existingHal into a HAL that contains only interface/instance and a HAL |
| 79 | // that does not contain it. Return the HAL that contains only interface/instance. |
| 80 | // - Return nullptr if existingHal does not contain interface/instance |
| 81 | // - Return existingHal if existingHal contains only interface/instance |
| 82 | // - Remove interface/instance from existingHal, and return a new MatrixHal (that is added |
| 83 | // to "this") that contains only interface/instance. |
| 84 | MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface, |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame^] | 85 | const std::string& instanceOrPattern, bool isRegex) { |
| 86 | bool found = false; |
| 87 | bool foundOthers = false; |
| 88 | existingHal->forEachInstance([&](const auto& matrixInstance) { |
| 89 | bool interfaceMatch = matrixInstance.interface() == interface; |
| 90 | bool instanceMatch = false; |
| 91 | if (matrixInstance.isRegex() && isRegex) { |
| 92 | instanceMatch = (matrixInstance.regexPattern() == instanceOrPattern); |
| 93 | } else if (!matrixInstance.isRegex() && !isRegex) { |
| 94 | instanceMatch = (matrixInstance.exactInstance() == instanceOrPattern); |
| 95 | } |
| 96 | |
| 97 | bool match = interfaceMatch && instanceMatch; |
| 98 | |
| 99 | found |= match; |
| 100 | foundOthers |= (!match); |
| 101 | |
| 102 | return !found || !foundOthers; |
| 103 | }); |
| 104 | |
| 105 | if (!found) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 106 | return nullptr; |
| 107 | } |
| 108 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame^] | 109 | if (!foundOthers) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 110 | return existingHal; |
| 111 | } |
| 112 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame^] | 113 | existingHal->removeInstance(interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 114 | MatrixHal copy = *existingHal; |
| 115 | copy.clearInstances(); |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame^] | 116 | copy.insertInstance(interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 117 | |
| 118 | return addInternal(std::move(copy)); |
| 119 | } |
| 120 | |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 121 | // Add all package@other_version::interface/instance as an optional instance. |
| 122 | // If package@this_version::interface/instance is in this (that is, some instance |
| 123 | // with the same package and interface and instance exists), then other_version is |
| 124 | // considered a possible replacement to this_version. |
| 125 | // See LibVintfTest.AddOptionalHal* tests for details. |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 126 | bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 127 | if (other == nullptr || other->level() <= level()) { |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | for (auto& pair : other->mHals) { |
| 132 | const std::string& name = pair.first; |
| 133 | MatrixHal& halToAdd = pair.second; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 134 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 135 | std::set<std::pair<std::string, std::string>> insertedInstances; |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame^] | 136 | std::set<std::pair<std::string, std::string>> insertedRegex; |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 137 | auto existingHals = getHals(name); |
| 138 | |
| 139 | halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges, |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame^] | 140 | const std::string& interface, |
| 141 | const std::string& instanceOrPattern, bool isRegex) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 142 | for (auto* existingHal : existingHals) { |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame^] | 143 | MatrixHal* splitInstance = |
| 144 | this->splitInstance(existingHal, interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 145 | if (splitInstance != nullptr) { |
| 146 | splitInstance->insertVersionRanges(versionRanges); |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame^] | 147 | if (isRegex) { |
| 148 | insertedRegex.insert(std::make_pair(interface, instanceOrPattern)); |
| 149 | } else { |
| 150 | insertedInstances.insert(std::make_pair(interface, instanceOrPattern)); |
| 151 | } |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 152 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 153 | } |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 154 | return true; |
| 155 | }); |
| 156 | |
| 157 | // Add the remaining instances. |
| 158 | for (const auto& pair : insertedInstances) { |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame^] | 159 | halToAdd.removeInstance(pair.first, pair.second, false /* isRegex */); |
| 160 | } |
| 161 | for (const auto& pair : insertedRegex) { |
| 162 | halToAdd.removeInstance(pair.first, pair.second, true /* isRegex */); |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 163 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 164 | |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 165 | if (halToAdd.instancesCount() > 0) { |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 166 | halToAdd.setOptional(true); |
| 167 | if (!add(std::move(halToAdd))) { |
| 168 | if (error) { |
| 169 | *error = "Cannot add HAL " + name + " for unknown reason."; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 170 | } |
| 171 | return false; |
| 172 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | return true; |
| 176 | } |
| 177 | |
Yifan Hong | d4b92fe | 2017-12-20 15:29:03 -0800 | [diff] [blame] | 178 | bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 179 | if (other == nullptr || other->level() <= level()) { |
| 180 | return true; |
| 181 | } |
| 182 | for (auto& pair : other->mXmlFiles) { |
| 183 | const std::string& name = pair.first; |
| 184 | MatrixXmlFile& xmlFileToAdd = pair.second; |
| 185 | |
| 186 | xmlFileToAdd.mOptional = true; |
| 187 | if (!addXmlFile(std::move(xmlFileToAdd))) { |
| 188 | if (error) { |
| 189 | *error = "Cannot add XML File " + name + " for unknown reason."; |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 197 | bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) { |
Yifan Hong | 2027a49 | 2017-12-11 15:21:19 -0800 | [diff] [blame] | 198 | return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals && |
| 199 | lft.mXmlFiles == rgt.mXmlFiles && |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 200 | (lft.mType != SchemaType::DEVICE || |
| 201 | ( |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 202 | #pragma clang diagnostic push |
| 203 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 204 | lft.device.mVndk == rgt.device.mVndk && |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 205 | #pragma clang diagnostic pop |
Yifan Hong | a28729e | 2018-01-17 13:40:35 -0800 | [diff] [blame] | 206 | lft.device.mVendorNdk == rgt.device.mVendorNdk && |
| 207 | lft.device.mSystemSdk == rgt.device.mSystemSdk)) && |
Yifan Hong | d485790 | 2017-06-13 14:13:56 -0700 | [diff] [blame] | 208 | (lft.mType != SchemaType::FRAMEWORK || |
| 209 | (lft.framework.mKernels == rgt.framework.mKernels && |
| 210 | lft.framework.mSepolicy == rgt.framework.mSepolicy && |
| 211 | lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion)); |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 214 | // Find compatibility_matrix.empty.xml (which has unspecified level) and use it |
| 215 | // as a base matrix. |
| 216 | CompatibilityMatrix* CompatibilityMatrix::findOrInsertBaseMatrix( |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 217 | std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 218 | bool multipleFound = false; |
| 219 | CompatibilityMatrix* matrix = nullptr; |
| 220 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 221 | if (e.object.level() == Level::UNSPECIFIED) { |
| 222 | if (!e.object.mHals.empty()) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 223 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 224 | *error = "Error: File \"" + e.name + "\" should not contain " + "HAL elements."; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 225 | } |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 229 | if (!e.object.mXmlFiles.empty()) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 230 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 231 | *error = |
| 232 | "Error: File \"" + e.name + "\" should not contain " + "XML File elements."; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 233 | } |
| 234 | return nullptr; |
| 235 | } |
| 236 | |
| 237 | if (matrix != nullptr) { |
| 238 | multipleFound = true; |
| 239 | } |
| 240 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 241 | matrix = &e.object; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 242 | // continue to detect multiple files with "unspecified" levels |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (multipleFound) { |
| 247 | if (error) { |
| 248 | *error = |
| 249 | "Error: multiple framework compatibility matrix files have " |
| 250 | "unspecified level; there should only be one such file.\n"; |
| 251 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 252 | if (e.object.level() == Level::UNSPECIFIED) { |
| 253 | *error += " " + e.name + "\n"; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
| 257 | return nullptr; |
| 258 | } |
| 259 | |
| 260 | if (matrix == nullptr) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 261 | matrix = &matrices->emplace(matrices->end())->object; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 262 | matrix->mType = SchemaType::FRAMEWORK; |
| 263 | matrix->mLevel = Level::UNSPECIFIED; |
| 264 | } |
| 265 | |
| 266 | return matrix; |
| 267 | } |
| 268 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 269 | CompatibilityMatrix* CompatibilityMatrix::combine(Level deviceLevel, |
| 270 | std::vector<Named<CompatibilityMatrix>>* matrices, |
| 271 | std::string* error) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 272 | if (deviceLevel == Level::UNSPECIFIED) { |
| 273 | if (error) { |
| 274 | *error = "Error: device level is unspecified."; |
| 275 | } |
| 276 | return nullptr; |
| 277 | } |
| 278 | |
| 279 | CompatibilityMatrix* matrix = findOrInsertBaseMatrix(matrices, error); |
| 280 | if (matrix == nullptr) { |
| 281 | return nullptr; |
| 282 | } |
| 283 | |
| 284 | matrix->mLevel = deviceLevel; |
| 285 | |
| 286 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 287 | if (&e.object != matrix && e.object.level() == deviceLevel) { |
| 288 | if (!matrix->addAllHals(&e.object, error)) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 289 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 290 | *error = "File \"" + e.name + "\" cannot be added: HAL " + *error + |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 291 | " has a conflict."; |
| 292 | } |
| 293 | return nullptr; |
| 294 | } |
| 295 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 296 | if (!matrix->addAllXmlFiles(&e.object, error)) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 297 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 298 | *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error + |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 299 | " has a conflict."; |
| 300 | } |
| 301 | return nullptr; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 307 | if (&e.object != matrix && e.object.level() != Level::UNSPECIFIED && |
| 308 | e.object.level() > deviceLevel) { |
| 309 | if (!matrix->addAllHalsAsOptional(&e.object, error)) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 310 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 311 | *error = "File \"" + e.name + "\" cannot be added: " + *error + |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 312 | ". See <hal> with the same name " + |
| 313 | "in previously parsed files or previously declared in this file."; |
| 314 | } |
| 315 | return nullptr; |
| 316 | } |
| 317 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 318 | if (!matrix->addAllXmlFilesAsOptional(&e.object, error)) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 319 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 320 | *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error + |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 321 | " has a conflict."; |
| 322 | } |
| 323 | return nullptr; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
Yifan Hong | f7e8b1b | 2018-01-23 15:30:35 -0800 | [diff] [blame] | 328 | for (auto& e : *matrices) { |
| 329 | if (&e.object != matrix && e.object.level() == deviceLevel && |
| 330 | e.object.type() == SchemaType::FRAMEWORK) { |
| 331 | for (MatrixKernel& kernel : e.object.framework.mKernels) { |
| 332 | KernelVersion ver = kernel.minLts(); |
| 333 | if (!matrix->add(std::move(kernel))) { |
| 334 | if (error) { |
| 335 | *error = "Cannot add kernel version " + to_string(ver) + |
| 336 | " from FCM version " + to_string(deviceLevel); |
| 337 | } |
| 338 | return nullptr; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 344 | return matrix; |
| 345 | } |
| 346 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 347 | bool CompatibilityMatrix::forEachInstanceOfVersion( |
| 348 | const std::string& package, const Version& expectVersion, |
| 349 | const std::function<bool(const MatrixInstance&)>& func) const { |
| 350 | for (const MatrixHal* hal : getHals(package)) { |
| 351 | bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) { |
| 352 | if (matrixInstance.versionRange().contains(expectVersion)) { |
| 353 | return func(matrixInstance); |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 354 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 355 | return true; |
| 356 | }); |
| 357 | if (!cont) return false; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 358 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 359 | return true; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Yifan Hong | def7e7f | 2018-03-20 13:27:36 -0700 | [diff] [blame] | 362 | bool CompatibilityMatrix::matchInstance(const std::string& halName, const Version& version, |
| 363 | const std::string& interfaceName, |
| 364 | const std::string& instance) const { |
| 365 | bool found = false; |
| 366 | (void)forEachInstanceOfInterface(halName, version, interfaceName, |
| 367 | [&found, &instance](const auto& e) { |
| 368 | found |= (e.matchInstance(instance)); |
| 369 | return !found; // if not found, continue |
| 370 | }); |
| 371 | return found; |
| 372 | } |
| 373 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 374 | } // namespace vintf |
| 375 | } // namespace android |