blob: 8ca2dbd404323b13478e25f14bed831822d85775 [file] [log] [blame]
Yifan Hong676447a2016-11-15 12:57:23 -08001/*
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 Hongb6c7f492018-02-27 14:07:57 -080019#include <iostream>
Yifan Hongdbe9db32017-12-11 19:06:11 -080020#include <utility>
21
Yifan Hong1e8febd2019-08-07 16:17:19 -070022#include <android-base/logging.h>
Yifan Honge7837b12018-10-11 10:38:57 -070023#include <android-base/strings.h>
24
Yifan Hongbbfff302017-06-06 17:10:13 -070025#include "parse_string.h"
Yifan Hongddae77e2017-12-18 16:57:07 -080026#include "parse_xml.h"
Yifan Hongb6c7f492018-02-27 14:07:57 -080027#include "utils.h"
Yifan Hongddae77e2017-12-18 16:57:07 -080028
Yifan Hong676447a2016-11-15 12:57:23 -080029namespace android {
30namespace vintf {
31
Yifan Hong4c6962d2019-01-30 15:50:46 -080032using details::mergeField;
33
Yifan Hong7992f5c2020-04-13 15:50:27 -070034bool CompatibilityMatrix::add(MatrixHal&& halToAdd, std::string*) {
35 CHECK(addInternal(std::move(halToAdd)) != nullptr);
36 return true;
37}
38
39bool CompatibilityMatrix::addAllHals(CompatibilityMatrix* other, std::string*) {
40 std::string internalError;
41 for (auto& pair : other->mHals) {
42 CHECK(add(std::move(pair.second), &internalError)) << internalError;
43 }
44 other->mHals.clear();
45 return true;
46}
47
Yifan Honge7837b12018-10-11 10:38:57 -070048bool CompatibilityMatrix::addKernel(MatrixKernel&& kernel, std::string* error) {
Yifan Hong7c7d7062017-04-04 16:26:51 -070049 if (mType != SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -070050 if (error) {
51 *error = "Cannot add <kernel> to a " + to_string(mType) + " compatibility matrix.";
52 }
Yifan Hong7c7d7062017-04-04 16:26:51 -070053 return false;
54 }
Yifan Honge7837b12018-10-11 10:38:57 -070055
Yifan Hongb343c5b2019-12-11 18:13:59 -080056 if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) {
57 kernel.setSourceMatrixLevel(level());
58 }
59
Yifan Honge7837b12018-10-11 10:38:57 -070060 auto it = framework.mKernels.begin();
61 for (; it != framework.mKernels.end(); ++it) {
Yifan Hong38c837b2019-12-12 15:22:00 -080062 if (it->getSourceMatrixLevel() != kernel.getSourceMatrixLevel()) {
63 continue;
64 }
Yifan Honge7837b12018-10-11 10:38:57 -070065 if (it->minLts() == kernel.minLts()) {
66 break;
67 }
Yifan Hong89093ed2019-12-12 12:16:01 -080068 if (it->minLts().dropMinor() == kernel.minLts().dropMinor()) {
Yifan Honge7837b12018-10-11 10:38:57 -070069 if (error) {
Yifan Hong38c837b2019-12-12 15:22:00 -080070 *error = "Kernel version mismatch; for level " +
71 to_string(kernel.getSourceMatrixLevel()) + ", cannot add " +
72 to_string(kernel.minLts()) + " because " + to_string(it->minLts()) +
73 " was added.";
Yifan Honge7837b12018-10-11 10:38:57 -070074 }
75 return false;
76 }
77 }
78
79 bool seenVersion = it != framework.mKernels.end();
80
81 if (seenVersion) {
82 // If no conditions, must be the first among the same minLts
83 // because O libvintf only checks the first <kernel> tag that version matches.
84 if (kernel.conditions().empty()) {
85 // Found first <kernel> with the same minLts.
86 // Append config if it does not have <condition>s, else error.
87 if (it->conditions().empty()) {
88 const auto& configs = kernel.configs();
89 it->mConfigs.insert(it->mConfigs.end(), configs.begin(), configs.end());
90 } else {
91 if (error) {
92 *error =
93 "Base compatibility matrix has <condition> for the first <kernel> "
94 "with minlts " +
95 to_string(kernel.minLts()) + " for unknown reason.";
96 }
97 return false;
98 }
99 return true;
100 }
101 } else {
102 // First <kernel> of a minLts must not have <condition>'s for backwards compatibility
103 // with O libvintf.
104 if (!kernel.conditions().empty()) {
105 framework.mKernels.push_back(MatrixKernel(KernelVersion{kernel.minLts()}, {}));
106 }
107 }
108
Yifan Hong7c7d7062017-04-04 16:26:51 -0700109 framework.mKernels.push_back(std::move(kernel));
Yifan Hong676447a2016-11-15 12:57:23 -0800110 return true;
111}
112
Yifan Hong398f4c72017-04-13 20:18:01 -0700113SchemaType CompatibilityMatrix::type() const {
114 return mType;
115}
116
Yifan Hong2027a492017-12-11 15:21:19 -0800117Level CompatibilityMatrix::level() const {
118 return mLevel;
119}
120
Yifan Hong9f78c182018-07-12 14:45:52 -0700121status_t CompatibilityMatrix::fetchAllInformation(const FileSystem* fileSystem,
122 const std::string& path, std::string* error) {
123 return details::fetchAllInformation(fileSystem, path, gCompatibilityMatrixConverter, this,
124 error);
Yifan Hong1e5a0542017-04-28 14:37:56 -0700125}
126
Yifan Hongbbfff302017-06-06 17:10:13 -0700127std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName,
128 const Version& version) const {
129 using std::literals::string_literals::operator""s;
130 auto range = getXmlFiles(xmlFileName);
131 for (auto it = range.first; it != range.second; ++it) {
132 const MatrixXmlFile& matrixXmlFile = it->second;
133 if (matrixXmlFile.versionRange().contains(version)) {
134 if (!matrixXmlFile.overriddenPath().empty()) {
135 return matrixXmlFile.overriddenPath();
136 }
137 return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" +
138 xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) +
139 "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." +
140 to_string(matrixXmlFile.format());
141 }
142 }
143 return "";
144}
145
Yifan Honge7e45532018-03-16 18:11:49 -0700146// Split existingHal into a HAL that contains only interface/instance and a HAL
147// that does not contain it. Return the HAL that contains only interface/instance.
148// - Return nullptr if existingHal does not contain interface/instance
149// - Return existingHal if existingHal contains only interface/instance
150// - Remove interface/instance from existingHal, and return a new MatrixHal (that is added
151// to "this") that contains only interface/instance.
152MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface,
Yifan Hong643a9ef2018-03-21 14:13:55 -0700153 const std::string& instanceOrPattern, bool isRegex) {
154 bool found = false;
155 bool foundOthers = false;
156 existingHal->forEachInstance([&](const auto& matrixInstance) {
157 bool interfaceMatch = matrixInstance.interface() == interface;
158 bool instanceMatch = false;
159 if (matrixInstance.isRegex() && isRegex) {
160 instanceMatch = (matrixInstance.regexPattern() == instanceOrPattern);
161 } else if (!matrixInstance.isRegex() && !isRegex) {
162 instanceMatch = (matrixInstance.exactInstance() == instanceOrPattern);
163 }
164
165 bool match = interfaceMatch && instanceMatch;
166
167 found |= match;
168 foundOthers |= (!match);
169
170 return !found || !foundOthers;
171 });
172
173 if (!found) {
Yifan Honge7e45532018-03-16 18:11:49 -0700174 return nullptr;
175 }
176
Yifan Hong643a9ef2018-03-21 14:13:55 -0700177 if (!foundOthers) {
Yifan Honge7e45532018-03-16 18:11:49 -0700178 return existingHal;
179 }
180
Yifan Hong643a9ef2018-03-21 14:13:55 -0700181 existingHal->removeInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700182 MatrixHal copy = *existingHal;
183 copy.clearInstances();
Yifan Hong643a9ef2018-03-21 14:13:55 -0700184 copy.insertInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700185
186 return addInternal(std::move(copy));
187}
188
Yifan Hong7967d7b2018-03-15 17:08:58 -0700189// Add all package@other_version::interface/instance as an optional instance.
190// If package@this_version::interface/instance is in this (that is, some instance
191// with the same package and interface and instance exists), then other_version is
192// considered a possible replacement to this_version.
193// See LibVintfTest.AddOptionalHal* tests for details.
Yifan Hongdbe9db32017-12-11 19:06:11 -0800194bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) {
195 if (other == nullptr || other->level() <= level()) {
196 return true;
197 }
198
199 for (auto& pair : other->mHals) {
200 const std::string& name = pair.first;
201 MatrixHal& halToAdd = pair.second;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800202
Yifan Honge7e45532018-03-16 18:11:49 -0700203 std::set<std::pair<std::string, std::string>> insertedInstances;
Yifan Hong643a9ef2018-03-21 14:13:55 -0700204 std::set<std::pair<std::string, std::string>> insertedRegex;
Yifan Honge7e45532018-03-16 18:11:49 -0700205 auto existingHals = getHals(name);
206
207 halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges,
Yifan Hong643a9ef2018-03-21 14:13:55 -0700208 const std::string& interface,
209 const std::string& instanceOrPattern, bool isRegex) {
Yifan Honge7e45532018-03-16 18:11:49 -0700210 for (auto* existingHal : existingHals) {
Yifan Hong5bb4fbf2019-09-10 14:47:28 -0700211 // Ignore HALs with different format.
212 if (halToAdd.format != existingHal->format) {
213 continue;
214 }
215
Yifan Hong643a9ef2018-03-21 14:13:55 -0700216 MatrixHal* splitInstance =
217 this->splitInstance(existingHal, interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700218 if (splitInstance != nullptr) {
219 splitInstance->insertVersionRanges(versionRanges);
Yifan Hong643a9ef2018-03-21 14:13:55 -0700220 if (isRegex) {
221 insertedRegex.insert(std::make_pair(interface, instanceOrPattern));
222 } else {
223 insertedInstances.insert(std::make_pair(interface, instanceOrPattern));
224 }
Yifan Honge7e45532018-03-16 18:11:49 -0700225 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800226 }
Yifan Honge7e45532018-03-16 18:11:49 -0700227 return true;
228 });
229
230 // Add the remaining instances.
231 for (const auto& pair : insertedInstances) {
Yifan Hong643a9ef2018-03-21 14:13:55 -0700232 halToAdd.removeInstance(pair.first, pair.second, false /* isRegex */);
233 }
234 for (const auto& pair : insertedRegex) {
235 halToAdd.removeInstance(pair.first, pair.second, true /* isRegex */);
Yifan Hong7967d7b2018-03-15 17:08:58 -0700236 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800237
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700238 if (halToAdd.instancesCount() > 0) {
Yifan Hong7967d7b2018-03-15 17:08:58 -0700239 halToAdd.setOptional(true);
240 if (!add(std::move(halToAdd))) {
241 if (error) {
242 *error = "Cannot add HAL " + name + " for unknown reason.";
Yifan Hongdbe9db32017-12-11 19:06:11 -0800243 }
244 return false;
245 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800246 }
247 }
248 return true;
249}
250
Yifan Hongd4b92fe2017-12-20 15:29:03 -0800251bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) {
252 if (other == nullptr || other->level() <= level()) {
253 return true;
254 }
255 for (auto& pair : other->mXmlFiles) {
256 const std::string& name = pair.first;
257 MatrixXmlFile& xmlFileToAdd = pair.second;
258
259 xmlFileToAdd.mOptional = true;
260 if (!addXmlFile(std::move(xmlFileToAdd))) {
261 if (error) {
262 *error = "Cannot add XML File " + name + " for unknown reason.";
263 }
264 return false;
265 }
266 }
267 return true;
268}
269
Yifan Hong38c837b2019-12-12 15:22:00 -0800270// Merge Kernel. See KernelInfo::getMatchedKernelRequirements for details on compatibility checks.
Yifan Honge7837b12018-10-11 10:38:57 -0700271bool CompatibilityMatrix::addAllKernels(CompatibilityMatrix* other, std::string* error) {
272 for (MatrixKernel& kernel : other->framework.mKernels) {
Yifan Honga39ecfe2019-12-11 17:55:30 -0800273 if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) {
274 kernel.setSourceMatrixLevel(other->level());
275 }
Yifan Honge7837b12018-10-11 10:38:57 -0700276 KernelVersion ver = kernel.minLts();
277 if (!addKernel(std::move(kernel), error)) {
278 if (error) {
279 *error = "Cannot add kernel version " + to_string(ver) + ": " + *error;
280 }
281 return false;
282 }
283 }
284 return true;
285}
286
Yifan Honge7837b12018-10-11 10:38:57 -0700287bool CompatibilityMatrix::addSepolicy(CompatibilityMatrix* other, std::string* error) {
288 bool success = mergeField(&this->framework.mSepolicy, &other->framework.mSepolicy);
289 if (!success && error) *error = "<sepolicy> is already defined";
290 return success;
291}
292
293bool CompatibilityMatrix::addAvbMetaVersion(CompatibilityMatrix* other, std::string* error) {
294 bool success = mergeField(&this->framework.mAvbMetaVersion, &other->framework.mAvbMetaVersion);
295 if (!success && error) *error = "<avb><vbmeta-version> is already defined";
296 return success;
297}
298
Yifan Hong5a51ea52019-04-22 14:54:57 -0700299bool CompatibilityMatrix::addVndk(CompatibilityMatrix* other, std::string* error) {
300#pragma clang diagnostic push
301#pragma clang diagnostic ignored "-Wdeprecated-declarations"
302 bool success = mergeField(&this->device.mVndk, &other->device.mVndk);
303#pragma clang diagnostic pop
304 if (!success && error) *error = "<vndk> is already defined";
305 return success;
306}
307
308bool CompatibilityMatrix::addVendorNdk(CompatibilityMatrix* other, std::string* error) {
309 bool success = mergeField(&this->device.mVendorNdk, &other->device.mVendorNdk);
310 if (!success && error) *error = "<vendor-ndk> is already defined";
311 return success;
312}
313
314bool CompatibilityMatrix::addSystemSdk(CompatibilityMatrix* other, std::string* /* error */) {
315 this->device.mSystemSdk.addAll(&other->device.mSystemSdk);
316 return true;
317}
318
Yifan Hongfb7469c2017-04-05 19:15:21 -0700319bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700320 // ignore fileName().
Yifan Hong2027a492017-12-11 15:21:19 -0800321 return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals &&
322 lft.mXmlFiles == rgt.mXmlFiles &&
Yifan Hongfeb454e2018-01-09 16:16:40 -0800323 (lft.mType != SchemaType::DEVICE ||
324 (
Yifan Hong0f529fa2018-01-10 14:51:59 -0800325#pragma clang diagnostic push
326#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Yifan Hongfeb454e2018-01-09 16:16:40 -0800327 lft.device.mVndk == rgt.device.mVndk &&
Yifan Hong0f529fa2018-01-10 14:51:59 -0800328#pragma clang diagnostic pop
Yifan Honga28729e2018-01-17 13:40:35 -0800329 lft.device.mVendorNdk == rgt.device.mVendorNdk &&
330 lft.device.mSystemSdk == rgt.device.mSystemSdk)) &&
Yifan Hongd4857902017-06-13 14:13:56 -0700331 (lft.mType != SchemaType::FRAMEWORK ||
332 (lft.framework.mKernels == rgt.framework.mKernels &&
333 lft.framework.mSepolicy == rgt.framework.mSepolicy &&
334 lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion));
Yifan Hongfb7469c2017-04-05 19:15:21 -0700335}
336
Yifan Honge7837b12018-10-11 10:38:57 -0700337std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combine(
Yifan Honga83d0e42020-04-13 13:07:31 -0700338 Level deviceLevel, std::vector<CompatibilityMatrix>* matrices, std::string* error) {
Yifan Honge7837b12018-10-11 10:38:57 -0700339 // Check type.
340 for (const auto& e : *matrices) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700341 if (e.type() != SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -0700342 if (error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700343 *error = "File \"" + e.fileName() + "\" is not a framework compatibility matrix.";
Yifan Honge7837b12018-10-11 10:38:57 -0700344 return nullptr;
345 }
346 }
347 }
348
349 // Matrices with unspecified (empty) level are auto-filled with deviceLevel.
Yifan Hongddae77e2017-12-18 16:57:07 -0800350 for (auto& e : *matrices) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700351 if (e.level() == Level::UNSPECIFIED) {
352 e.mLevel = deviceLevel;
Yifan Hongddae77e2017-12-18 16:57:07 -0800353 }
354 }
355
Yifan Honge7837b12018-10-11 10:38:57 -0700356 // Add from low to high FCM version so that optional <kernel> requirements are added correctly.
357 // See comment in addAllAsOptional.
358 std::sort(matrices->begin(), matrices->end(),
Yifan Honga83d0e42020-04-13 13:07:31 -0700359 [](const auto& x, const auto& y) { return x.level() < y.level(); });
Yifan Honge7837b12018-10-11 10:38:57 -0700360
361 auto baseMatrix = std::make_unique<CompatibilityMatrix>();
362 baseMatrix->mLevel = deviceLevel;
363 baseMatrix->mType = SchemaType::FRAMEWORK;
364
365 std::vector<std::string> parsedFiles;
366 for (auto& e : *matrices) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700367 if (e.level() < deviceLevel) {
Yifan Honge7837b12018-10-11 10:38:57 -0700368 continue;
Yifan Hongddae77e2017-12-18 16:57:07 -0800369 }
Yifan Honge7837b12018-10-11 10:38:57 -0700370
371 bool success = false;
Yifan Honga83d0e42020-04-13 13:07:31 -0700372 if (e.level() == deviceLevel) {
Yifan Honge7837b12018-10-11 10:38:57 -0700373 success = baseMatrix->addAll(&e, error);
374 } else {
375 success = baseMatrix->addAllAsOptional(&e, error);
376 }
377 if (!success) {
378 if (error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700379 *error = "Conflict when merging \"" + e.fileName() + "\": " + *error + "\n" +
Yifan Honge7837b12018-10-11 10:38:57 -0700380 "Previous files:\n" + base::Join(parsedFiles, "\n");
381 }
382 return nullptr;
383 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700384 parsedFiles.push_back(e.fileName());
Yifan Hongddae77e2017-12-18 16:57:07 -0800385 }
Yifan Honge7837b12018-10-11 10:38:57 -0700386
387 return baseMatrix;
Yifan Hongddae77e2017-12-18 16:57:07 -0800388}
389
Yifan Hong5a51ea52019-04-22 14:54:57 -0700390std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combineDeviceMatrices(
Yifan Honga83d0e42020-04-13 13:07:31 -0700391 std::vector<CompatibilityMatrix>* matrices, std::string* error) {
Yifan Hong5a51ea52019-04-22 14:54:57 -0700392 auto baseMatrix = std::make_unique<CompatibilityMatrix>();
393 baseMatrix->mType = SchemaType::DEVICE;
394
395 std::vector<std::string> parsedFiles;
396 for (auto& e : *matrices) {
397 bool success = baseMatrix->addAll(&e, error);
398 if (!success) {
399 if (error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700400 *error = "Conflict when merging \"" + e.fileName() + "\": " + *error + "\n" +
Yifan Hong5a51ea52019-04-22 14:54:57 -0700401 "Previous files:\n" + base::Join(parsedFiles, "\n");
402 }
403 return nullptr;
404 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700405 parsedFiles.push_back(e.fileName());
Yifan Hong5a51ea52019-04-22 14:54:57 -0700406 }
407 return baseMatrix;
408}
409
Yifan Honga83d0e42020-04-13 13:07:31 -0700410bool CompatibilityMatrix::addAll(CompatibilityMatrix* inputMatrix, std::string* error) {
411 if (!addAllHals(inputMatrix, error) || !addAllXmlFiles(inputMatrix, error) ||
412 !addAllKernels(inputMatrix, error) || !addSepolicy(inputMatrix, error) ||
413 !addAvbMetaVersion(inputMatrix, error) || !addVndk(inputMatrix, error) ||
414 !addVendorNdk(inputMatrix, error) || !addSystemSdk(inputMatrix, error)) {
Yifan Honge7837b12018-10-11 10:38:57 -0700415 if (error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700416 *error = "File \"" + inputMatrix->fileName() + "\" cannot be added: " + *error + ".";
Yifan Hongd6de7f62018-04-26 18:40:02 -0700417 }
Yifan Hong5390e3b2019-01-31 14:13:00 -0800418 return false;
Yifan Hongd6de7f62018-04-26 18:40:02 -0700419 }
420 return true;
421}
422
Yifan Honga83d0e42020-04-13 13:07:31 -0700423bool CompatibilityMatrix::addAllAsOptional(CompatibilityMatrix* inputMatrix, std::string* error) {
424 if (!addAllHalsAsOptional(inputMatrix, error) ||
425 !addAllXmlFilesAsOptional(inputMatrix, error) || !addAllKernels(inputMatrix, error)) {
Yifan Honge7837b12018-10-11 10:38:57 -0700426 if (error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700427 *error = "File \"" + inputMatrix->fileName() + "\" cannot be added: " + *error;
Yifan Hongddae77e2017-12-18 16:57:07 -0800428 }
Yifan Honge7837b12018-10-11 10:38:57 -0700429 return false;
Yifan Hongddae77e2017-12-18 16:57:07 -0800430 }
Yifan Honge7837b12018-10-11 10:38:57 -0700431 // ignore <sepolicy> requirement from higher level
432 // ignore <avb> requirement from higher level
433 return true;
Yifan Hongddae77e2017-12-18 16:57:07 -0800434}
435
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800436bool CompatibilityMatrix::forEachInstanceOfVersion(
Yifan Hongfc7ad272019-09-10 19:49:15 -0700437 HalFormat format, const std::string& package, const Version& expectVersion,
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800438 const std::function<bool(const MatrixInstance&)>& func) const {
439 for (const MatrixHal* hal : getHals(package)) {
440 bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) {
Yifan Hongfc7ad272019-09-10 19:49:15 -0700441 if (matrixInstance.format() == format &&
442 matrixInstance.versionRange().contains(expectVersion)) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800443 return func(matrixInstance);
Yifan Honge3a92342018-01-25 17:00:16 -0800444 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800445 return true;
446 });
447 if (!cont) return false;
Yifan Honge3a92342018-01-25 17:00:16 -0800448 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800449 return true;
Yifan Honge3a92342018-01-25 17:00:16 -0800450}
451
Yifan Hong1dc5a472019-09-10 19:40:06 -0700452bool CompatibilityMatrix::matchInstance(HalFormat format, const std::string& halName,
453 const Version& version, const std::string& interfaceName,
Yifan Hongdef7e7f2018-03-20 13:27:36 -0700454 const std::string& instance) const {
455 bool found = false;
Yifan Hong1dc5a472019-09-10 19:40:06 -0700456 (void)forEachInstanceOfInterface(format, halName, version, interfaceName,
Yifan Hongdef7e7f2018-03-20 13:27:36 -0700457 [&found, &instance](const auto& e) {
458 found |= (e.matchInstance(instance));
459 return !found; // if not found, continue
460 });
461 return found;
462}
463
Yifan Hong52b7fae2018-05-22 16:21:31 -0700464std::string CompatibilityMatrix::getVendorNdkVersion() const {
465 return type() == SchemaType::DEVICE ? device.mVendorNdk.version() : "";
466}
467
Yifan Hong1e8febd2019-08-07 16:17:19 -0700468Level CompatibilityMatrix::getSourceMatrixLevel(const MatrixKernel* matrixKernel) const {
469 CHECK(std::find_if(framework.mKernels.begin(), framework.mKernels.end(),
470 [matrixKernel](const auto& e) { return &e == matrixKernel; }) !=
471 framework.mKernels.end());
472 Level ret = matrixKernel->getSourceMatrixLevel();
473 if (ret != Level::UNSPECIFIED) return ret;
474 return level();
475}
476
Yifan Hong676447a2016-11-15 12:57:23 -0800477} // namespace vintf
478} // namespace android