blob: c6fa25ec7a05d4b9b61e62289a26cfab8431bc4f [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 Honge7837b12018-10-11 10:38:57 -070034bool CompatibilityMatrix::addKernel(MatrixKernel&& kernel, std::string* error) {
Yifan Hong7c7d7062017-04-04 16:26:51 -070035 if (mType != SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -070036 if (error) {
37 *error = "Cannot add <kernel> to a " + to_string(mType) + " compatibility matrix.";
38 }
Yifan Hong7c7d7062017-04-04 16:26:51 -070039 return false;
40 }
Yifan Honge7837b12018-10-11 10:38:57 -070041
Yifan Hongb343c5b2019-12-11 18:13:59 -080042 if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) {
43 kernel.setSourceMatrixLevel(level());
44 }
45
Yifan Honge7837b12018-10-11 10:38:57 -070046 auto it = framework.mKernels.begin();
47 for (; it != framework.mKernels.end(); ++it) {
48 if (it->minLts() == kernel.minLts()) {
49 break;
50 }
Yifan Hong89093ed2019-12-12 12:16:01 -080051 if (it->minLts().dropMinor() == kernel.minLts().dropMinor()) {
Yifan Honge7837b12018-10-11 10:38:57 -070052 if (error) {
53 *error = "Kernel version mismatch; cannot add " + to_string(kernel.minLts()) +
54 " because " + to_string(it->minLts()) + " was added.";
55 }
56 return false;
57 }
58 }
59
60 bool seenVersion = it != framework.mKernels.end();
61
62 if (seenVersion) {
63 // If no conditions, must be the first among the same minLts
64 // because O libvintf only checks the first <kernel> tag that version matches.
65 if (kernel.conditions().empty()) {
66 // Found first <kernel> with the same minLts.
67 // Append config if it does not have <condition>s, else error.
68 if (it->conditions().empty()) {
69 const auto& configs = kernel.configs();
70 it->mConfigs.insert(it->mConfigs.end(), configs.begin(), configs.end());
71 } else {
72 if (error) {
73 *error =
74 "Base compatibility matrix has <condition> for the first <kernel> "
75 "with minlts " +
76 to_string(kernel.minLts()) + " for unknown reason.";
77 }
78 return false;
79 }
80 return true;
81 }
82 } else {
83 // First <kernel> of a minLts must not have <condition>'s for backwards compatibility
84 // with O libvintf.
85 if (!kernel.conditions().empty()) {
86 framework.mKernels.push_back(MatrixKernel(KernelVersion{kernel.minLts()}, {}));
87 }
88 }
89
Yifan Hong7c7d7062017-04-04 16:26:51 -070090 framework.mKernels.push_back(std::move(kernel));
Yifan Hong676447a2016-11-15 12:57:23 -080091 return true;
92}
93
Yifan Hong398f4c72017-04-13 20:18:01 -070094SchemaType CompatibilityMatrix::type() const {
95 return mType;
96}
97
Yifan Hong2027a492017-12-11 15:21:19 -080098Level CompatibilityMatrix::level() const {
99 return mLevel;
100}
101
Yifan Hong9f78c182018-07-12 14:45:52 -0700102status_t CompatibilityMatrix::fetchAllInformation(const FileSystem* fileSystem,
103 const std::string& path, std::string* error) {
104 return details::fetchAllInformation(fileSystem, path, gCompatibilityMatrixConverter, this,
105 error);
Yifan Hong1e5a0542017-04-28 14:37:56 -0700106}
107
Yifan Hongbbfff302017-06-06 17:10:13 -0700108std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName,
109 const Version& version) const {
110 using std::literals::string_literals::operator""s;
111 auto range = getXmlFiles(xmlFileName);
112 for (auto it = range.first; it != range.second; ++it) {
113 const MatrixXmlFile& matrixXmlFile = it->second;
114 if (matrixXmlFile.versionRange().contains(version)) {
115 if (!matrixXmlFile.overriddenPath().empty()) {
116 return matrixXmlFile.overriddenPath();
117 }
118 return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" +
119 xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) +
120 "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." +
121 to_string(matrixXmlFile.format());
122 }
123 }
124 return "";
125}
126
Yifan Honge7e45532018-03-16 18:11:49 -0700127// Split existingHal into a HAL that contains only interface/instance and a HAL
128// that does not contain it. Return the HAL that contains only interface/instance.
129// - Return nullptr if existingHal does not contain interface/instance
130// - Return existingHal if existingHal contains only interface/instance
131// - Remove interface/instance from existingHal, and return a new MatrixHal (that is added
132// to "this") that contains only interface/instance.
133MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface,
Yifan Hong643a9ef2018-03-21 14:13:55 -0700134 const std::string& instanceOrPattern, bool isRegex) {
135 bool found = false;
136 bool foundOthers = false;
137 existingHal->forEachInstance([&](const auto& matrixInstance) {
138 bool interfaceMatch = matrixInstance.interface() == interface;
139 bool instanceMatch = false;
140 if (matrixInstance.isRegex() && isRegex) {
141 instanceMatch = (matrixInstance.regexPattern() == instanceOrPattern);
142 } else if (!matrixInstance.isRegex() && !isRegex) {
143 instanceMatch = (matrixInstance.exactInstance() == instanceOrPattern);
144 }
145
146 bool match = interfaceMatch && instanceMatch;
147
148 found |= match;
149 foundOthers |= (!match);
150
151 return !found || !foundOthers;
152 });
153
154 if (!found) {
Yifan Honge7e45532018-03-16 18:11:49 -0700155 return nullptr;
156 }
157
Yifan Hong643a9ef2018-03-21 14:13:55 -0700158 if (!foundOthers) {
Yifan Honge7e45532018-03-16 18:11:49 -0700159 return existingHal;
160 }
161
Yifan Hong643a9ef2018-03-21 14:13:55 -0700162 existingHal->removeInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700163 MatrixHal copy = *existingHal;
164 copy.clearInstances();
Yifan Hong643a9ef2018-03-21 14:13:55 -0700165 copy.insertInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700166
167 return addInternal(std::move(copy));
168}
169
Yifan Hong7967d7b2018-03-15 17:08:58 -0700170// Add all package@other_version::interface/instance as an optional instance.
171// If package@this_version::interface/instance is in this (that is, some instance
172// with the same package and interface and instance exists), then other_version is
173// considered a possible replacement to this_version.
174// See LibVintfTest.AddOptionalHal* tests for details.
Yifan Hongdbe9db32017-12-11 19:06:11 -0800175bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) {
176 if (other == nullptr || other->level() <= level()) {
177 return true;
178 }
179
180 for (auto& pair : other->mHals) {
181 const std::string& name = pair.first;
182 MatrixHal& halToAdd = pair.second;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800183
Yifan Honge7e45532018-03-16 18:11:49 -0700184 std::set<std::pair<std::string, std::string>> insertedInstances;
Yifan Hong643a9ef2018-03-21 14:13:55 -0700185 std::set<std::pair<std::string, std::string>> insertedRegex;
Yifan Honge7e45532018-03-16 18:11:49 -0700186 auto existingHals = getHals(name);
187
188 halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges,
Yifan Hong643a9ef2018-03-21 14:13:55 -0700189 const std::string& interface,
190 const std::string& instanceOrPattern, bool isRegex) {
Yifan Honge7e45532018-03-16 18:11:49 -0700191 for (auto* existingHal : existingHals) {
Yifan Hong5bb4fbf2019-09-10 14:47:28 -0700192 // Ignore HALs with different format.
193 if (halToAdd.format != existingHal->format) {
194 continue;
195 }
196
Yifan Hong643a9ef2018-03-21 14:13:55 -0700197 MatrixHal* splitInstance =
198 this->splitInstance(existingHal, interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700199 if (splitInstance != nullptr) {
200 splitInstance->insertVersionRanges(versionRanges);
Yifan Hong643a9ef2018-03-21 14:13:55 -0700201 if (isRegex) {
202 insertedRegex.insert(std::make_pair(interface, instanceOrPattern));
203 } else {
204 insertedInstances.insert(std::make_pair(interface, instanceOrPattern));
205 }
Yifan Honge7e45532018-03-16 18:11:49 -0700206 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800207 }
Yifan Honge7e45532018-03-16 18:11:49 -0700208 return true;
209 });
210
211 // Add the remaining instances.
212 for (const auto& pair : insertedInstances) {
Yifan Hong643a9ef2018-03-21 14:13:55 -0700213 halToAdd.removeInstance(pair.first, pair.second, false /* isRegex */);
214 }
215 for (const auto& pair : insertedRegex) {
216 halToAdd.removeInstance(pair.first, pair.second, true /* isRegex */);
Yifan Hong7967d7b2018-03-15 17:08:58 -0700217 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800218
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700219 if (halToAdd.instancesCount() > 0) {
Yifan Hong7967d7b2018-03-15 17:08:58 -0700220 halToAdd.setOptional(true);
221 if (!add(std::move(halToAdd))) {
222 if (error) {
223 *error = "Cannot add HAL " + name + " for unknown reason.";
Yifan Hongdbe9db32017-12-11 19:06:11 -0800224 }
225 return false;
226 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800227 }
228 }
229 return true;
230}
231
Yifan Hongd4b92fe2017-12-20 15:29:03 -0800232bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) {
233 if (other == nullptr || other->level() <= level()) {
234 return true;
235 }
236 for (auto& pair : other->mXmlFiles) {
237 const std::string& name = pair.first;
238 MatrixXmlFile& xmlFileToAdd = pair.second;
239
240 xmlFileToAdd.mOptional = true;
241 if (!addXmlFile(std::move(xmlFileToAdd))) {
242 if (error) {
243 *error = "Cannot add XML File " + name + " for unknown reason.";
244 }
245 return false;
246 }
247 }
248 return true;
249}
250
Yifan Honge7837b12018-10-11 10:38:57 -0700251// Merge Kernel.
252// Add <kernel> from exact "level", then optionally add <kernel> from high levels to low levels.
253// For example, (each letter is a kernel version x.y.z)
254// 1.xml: A1, B1
255// 2.xml: B2, C2, D2
256// 3.xml: D3, E3
257// Then the combined 1.xml should have
258// A1, B1 (from 1.xml, required), C2, D2, E3 (optional, use earliest possible).
259bool CompatibilityMatrix::addAllKernels(CompatibilityMatrix* other, std::string* error) {
260 for (MatrixKernel& kernel : other->framework.mKernels) {
Yifan Honga39ecfe2019-12-11 17:55:30 -0800261 if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) {
262 kernel.setSourceMatrixLevel(other->level());
263 }
Yifan Honge7837b12018-10-11 10:38:57 -0700264 KernelVersion ver = kernel.minLts();
265 if (!addKernel(std::move(kernel), error)) {
266 if (error) {
267 *error = "Cannot add kernel version " + to_string(ver) + ": " + *error;
268 }
269 return false;
270 }
271 }
272 return true;
273}
274
Yifan Hongd6de7f62018-04-26 18:40:02 -0700275bool CompatibilityMatrix::addAllKernelsAsOptional(CompatibilityMatrix* other, std::string* error) {
276 if (other == nullptr || other->level() <= level()) {
277 return true;
278 }
279
280 for (MatrixKernel& kernelToAdd : other->framework.mKernels) {
281 bool exists =
282 std::any_of(this->framework.mKernels.begin(), this->framework.mKernels.end(),
283 [&kernelToAdd](const MatrixKernel& existing) {
284 return kernelToAdd.minLts().version == existing.minLts().version &&
285 kernelToAdd.minLts().majorRev == existing.minLts().majorRev;
286 });
287
288 if (exists) {
289 // Shouldn't retroactively add requirements to minLts(), so ignore this.
290 // This happens even when kernelToAdd.conditions() != existing.conditions().
291 continue;
292 }
293
Yifan Honga39ecfe2019-12-11 17:55:30 -0800294 if (kernelToAdd.getSourceMatrixLevel() == Level::UNSPECIFIED) {
295 kernelToAdd.setSourceMatrixLevel(other->level());
296 }
Yifan Hong1e8febd2019-08-07 16:17:19 -0700297
Yifan Hongd6de7f62018-04-26 18:40:02 -0700298 KernelVersion minLts = kernelToAdd.minLts();
Yifan Honge7837b12018-10-11 10:38:57 -0700299 if (!addKernel(std::move(kernelToAdd), error)) {
Yifan Hongd6de7f62018-04-26 18:40:02 -0700300 if (error) {
Yifan Honge7837b12018-10-11 10:38:57 -0700301 *error = "Cannot add " + to_string(minLts) + ": " + *error;
Yifan Hongd6de7f62018-04-26 18:40:02 -0700302 }
303 return false;
304 }
305 }
306 return true;
307}
308
Yifan Honge7837b12018-10-11 10:38:57 -0700309bool CompatibilityMatrix::addSepolicy(CompatibilityMatrix* other, std::string* error) {
310 bool success = mergeField(&this->framework.mSepolicy, &other->framework.mSepolicy);
311 if (!success && error) *error = "<sepolicy> is already defined";
312 return success;
313}
314
315bool CompatibilityMatrix::addAvbMetaVersion(CompatibilityMatrix* other, std::string* error) {
316 bool success = mergeField(&this->framework.mAvbMetaVersion, &other->framework.mAvbMetaVersion);
317 if (!success && error) *error = "<avb><vbmeta-version> is already defined";
318 return success;
319}
320
Yifan Hong5a51ea52019-04-22 14:54:57 -0700321bool CompatibilityMatrix::addVndk(CompatibilityMatrix* other, std::string* error) {
322#pragma clang diagnostic push
323#pragma clang diagnostic ignored "-Wdeprecated-declarations"
324 bool success = mergeField(&this->device.mVndk, &other->device.mVndk);
325#pragma clang diagnostic pop
326 if (!success && error) *error = "<vndk> is already defined";
327 return success;
328}
329
330bool CompatibilityMatrix::addVendorNdk(CompatibilityMatrix* other, std::string* error) {
331 bool success = mergeField(&this->device.mVendorNdk, &other->device.mVendorNdk);
332 if (!success && error) *error = "<vendor-ndk> is already defined";
333 return success;
334}
335
336bool CompatibilityMatrix::addSystemSdk(CompatibilityMatrix* other, std::string* /* error */) {
337 this->device.mSystemSdk.addAll(&other->device.mSystemSdk);
338 return true;
339}
340
Yifan Hongfb7469c2017-04-05 19:15:21 -0700341bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) {
Yifan Hong2027a492017-12-11 15:21:19 -0800342 return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals &&
343 lft.mXmlFiles == rgt.mXmlFiles &&
Yifan Hongfeb454e2018-01-09 16:16:40 -0800344 (lft.mType != SchemaType::DEVICE ||
345 (
Yifan Hong0f529fa2018-01-10 14:51:59 -0800346#pragma clang diagnostic push
347#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Yifan Hongfeb454e2018-01-09 16:16:40 -0800348 lft.device.mVndk == rgt.device.mVndk &&
Yifan Hong0f529fa2018-01-10 14:51:59 -0800349#pragma clang diagnostic pop
Yifan Honga28729e2018-01-17 13:40:35 -0800350 lft.device.mVendorNdk == rgt.device.mVendorNdk &&
351 lft.device.mSystemSdk == rgt.device.mSystemSdk)) &&
Yifan Hongd4857902017-06-13 14:13:56 -0700352 (lft.mType != SchemaType::FRAMEWORK ||
353 (lft.framework.mKernels == rgt.framework.mKernels &&
354 lft.framework.mSepolicy == rgt.framework.mSepolicy &&
355 lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion));
Yifan Hongfb7469c2017-04-05 19:15:21 -0700356}
357
Yifan Honge7837b12018-10-11 10:38:57 -0700358std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combine(
359 Level deviceLevel, std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) {
360 // Check type.
361 for (const auto& e : *matrices) {
362 if (e.object.type() != SchemaType::FRAMEWORK) {
363 if (error) {
364 *error = "File \"" + e.name + "\" is not a framework compatibility matrix.";
365 return nullptr;
366 }
367 }
368 }
369
370 // Matrices with unspecified (empty) level are auto-filled with deviceLevel.
Yifan Hongddae77e2017-12-18 16:57:07 -0800371 for (auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800372 if (e.object.level() == Level::UNSPECIFIED) {
Yifan Honge7837b12018-10-11 10:38:57 -0700373 e.object.mLevel = deviceLevel;
Yifan Hongddae77e2017-12-18 16:57:07 -0800374 }
375 }
376
Yifan Honge7837b12018-10-11 10:38:57 -0700377 // Add from low to high FCM version so that optional <kernel> requirements are added correctly.
378 // See comment in addAllAsOptional.
379 std::sort(matrices->begin(), matrices->end(),
380 [](const auto& x, const auto& y) { return x.object.level() < y.object.level(); });
381
382 auto baseMatrix = std::make_unique<CompatibilityMatrix>();
383 baseMatrix->mLevel = deviceLevel;
384 baseMatrix->mType = SchemaType::FRAMEWORK;
385
386 std::vector<std::string> parsedFiles;
387 for (auto& e : *matrices) {
388 if (e.object.level() < deviceLevel) {
389 continue;
Yifan Hongddae77e2017-12-18 16:57:07 -0800390 }
Yifan Honge7837b12018-10-11 10:38:57 -0700391
392 bool success = false;
393 if (e.object.level() == deviceLevel) {
394 success = baseMatrix->addAll(&e, error);
395 } else {
396 success = baseMatrix->addAllAsOptional(&e, error);
397 }
398 if (!success) {
399 if (error) {
400 *error = "Conflict when merging \"" + e.name + "\": " + *error + "\n" +
401 "Previous files:\n" + base::Join(parsedFiles, "\n");
402 }
403 return nullptr;
404 }
405 parsedFiles.push_back(e.name);
Yifan Hongddae77e2017-12-18 16:57:07 -0800406 }
Yifan Honge7837b12018-10-11 10:38:57 -0700407
408 return baseMatrix;
Yifan Hongddae77e2017-12-18 16:57:07 -0800409}
410
Yifan Hong5a51ea52019-04-22 14:54:57 -0700411std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combineDeviceMatrices(
412 std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) {
413 auto baseMatrix = std::make_unique<CompatibilityMatrix>();
414 baseMatrix->mType = SchemaType::DEVICE;
415
416 std::vector<std::string> parsedFiles;
417 for (auto& e : *matrices) {
418 bool success = baseMatrix->addAll(&e, error);
419 if (!success) {
420 if (error) {
421 *error = "Conflict when merging \"" + e.name + "\": " + *error + "\n" +
422 "Previous files:\n" + base::Join(parsedFiles, "\n");
423 }
424 return nullptr;
425 }
426 parsedFiles.push_back(e.name);
427 }
428 return baseMatrix;
429}
430
Yifan Honge7837b12018-10-11 10:38:57 -0700431bool CompatibilityMatrix::addAll(Named<CompatibilityMatrix>* inputMatrix, std::string* error) {
432 if (!addAllHals(&inputMatrix->object, error) || !addAllXmlFiles(&inputMatrix->object, error) ||
433 !addAllKernels(&inputMatrix->object, error) || !addSepolicy(&inputMatrix->object, error) ||
Yifan Hong5a51ea52019-04-22 14:54:57 -0700434 !addAvbMetaVersion(&inputMatrix->object, error) || !addVndk(&inputMatrix->object, error) ||
435 !addVendorNdk(&inputMatrix->object, error) || !addSystemSdk(&inputMatrix->object, error)) {
Yifan Honge7837b12018-10-11 10:38:57 -0700436 if (error) {
437 *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error + ".";
Yifan Hongd6de7f62018-04-26 18:40:02 -0700438 }
Yifan Hong5390e3b2019-01-31 14:13:00 -0800439 return false;
Yifan Hongd6de7f62018-04-26 18:40:02 -0700440 }
441 return true;
442}
443
Yifan Honge7837b12018-10-11 10:38:57 -0700444bool CompatibilityMatrix::addAllAsOptional(Named<CompatibilityMatrix>* inputMatrix,
445 std::string* error) {
446 if (!addAllHalsAsOptional(&inputMatrix->object, error) ||
447 !addAllXmlFilesAsOptional(&inputMatrix->object, error) ||
448 !addAllKernelsAsOptional(&inputMatrix->object, error)) {
449 if (error) {
450 *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error;
Yifan Hongddae77e2017-12-18 16:57:07 -0800451 }
Yifan Honge7837b12018-10-11 10:38:57 -0700452 return false;
Yifan Hongddae77e2017-12-18 16:57:07 -0800453 }
Yifan Honge7837b12018-10-11 10:38:57 -0700454 // ignore <sepolicy> requirement from higher level
455 // ignore <avb> requirement from higher level
456 return true;
Yifan Hongddae77e2017-12-18 16:57:07 -0800457}
458
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800459bool CompatibilityMatrix::forEachInstanceOfVersion(
Yifan Hongfc7ad272019-09-10 19:49:15 -0700460 HalFormat format, const std::string& package, const Version& expectVersion,
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800461 const std::function<bool(const MatrixInstance&)>& func) const {
462 for (const MatrixHal* hal : getHals(package)) {
463 bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) {
Yifan Hongfc7ad272019-09-10 19:49:15 -0700464 if (matrixInstance.format() == format &&
465 matrixInstance.versionRange().contains(expectVersion)) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800466 return func(matrixInstance);
Yifan Honge3a92342018-01-25 17:00:16 -0800467 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800468 return true;
469 });
470 if (!cont) return false;
Yifan Honge3a92342018-01-25 17:00:16 -0800471 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800472 return true;
Yifan Honge3a92342018-01-25 17:00:16 -0800473}
474
Yifan Hong1dc5a472019-09-10 19:40:06 -0700475bool CompatibilityMatrix::matchInstance(HalFormat format, const std::string& halName,
476 const Version& version, const std::string& interfaceName,
Yifan Hongdef7e7f2018-03-20 13:27:36 -0700477 const std::string& instance) const {
478 bool found = false;
Yifan Hong1dc5a472019-09-10 19:40:06 -0700479 (void)forEachInstanceOfInterface(format, halName, version, interfaceName,
Yifan Hongdef7e7f2018-03-20 13:27:36 -0700480 [&found, &instance](const auto& e) {
481 found |= (e.matchInstance(instance));
482 return !found; // if not found, continue
483 });
484 return found;
485}
486
Yifan Hong52b7fae2018-05-22 16:21:31 -0700487std::string CompatibilityMatrix::getVendorNdkVersion() const {
488 return type() == SchemaType::DEVICE ? device.mVendorNdk.version() : "";
489}
490
Yifan Hong1e8febd2019-08-07 16:17:19 -0700491Level CompatibilityMatrix::getSourceMatrixLevel(const MatrixKernel* matrixKernel) const {
492 CHECK(std::find_if(framework.mKernels.begin(), framework.mKernels.end(),
493 [matrixKernel](const auto& e) { return &e == matrixKernel; }) !=
494 framework.mKernels.end());
495 Level ret = matrixKernel->getSourceMatrixLevel();
496 if (ret != Level::UNSPECIFIED) return ret;
497 return level();
498}
499
Yifan Hong676447a2016-11-15 12:57:23 -0800500} // namespace vintf
501} // namespace android