blob: 451ca60332eabe812fb1d2012c3da2e622dbd1ff [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 Hongbbfff302017-06-06 17:10:13 -070022#include "parse_string.h"
Yifan Hongddae77e2017-12-18 16:57:07 -080023#include "parse_xml.h"
Yifan Hongb6c7f492018-02-27 14:07:57 -080024#include "utils.h"
Yifan Hongddae77e2017-12-18 16:57:07 -080025
Yifan Hong676447a2016-11-15 12:57:23 -080026namespace android {
27namespace vintf {
28
Yifan Hong676447a2016-11-15 12:57:23 -080029bool CompatibilityMatrix::add(MatrixHal &&hal) {
Yifan Hong0fd7aef2017-05-24 14:37:19 -070030 return HalGroup<MatrixHal>::add(std::move(hal));
Yifan Hong676447a2016-11-15 12:57:23 -080031}
32
33bool CompatibilityMatrix::add(MatrixKernel &&kernel) {
Yifan Hong7c7d7062017-04-04 16:26:51 -070034 if (mType != SchemaType::FRAMEWORK) {
35 return false;
36 }
37 framework.mKernels.push_back(std::move(kernel));
Yifan Hong676447a2016-11-15 12:57:23 -080038 return true;
39}
40
Yifan Hong398f4c72017-04-13 20:18:01 -070041SchemaType CompatibilityMatrix::type() const {
42 return mType;
43}
44
Yifan Hong2027a492017-12-11 15:21:19 -080045Level CompatibilityMatrix::level() const {
46 return mLevel;
47}
48
Yifan Hongdb127cb2017-09-19 13:36:21 -070049Version 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 Hong1e5a0542017-04-28 14:37:56 -070054
Yifan Hong60217032018-01-08 16:19:42 -080055status_t CompatibilityMatrix::fetchAllInformation(const std::string& path, std::string* error) {
56 return details::fetchAllInformation(path, gCompatibilityMatrixConverter, this, error);
Yifan Hong1e5a0542017-04-28 14:37:56 -070057}
58
Yifan Hongbbfff302017-06-06 17:10:13 -070059std::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 Honge7e45532018-03-16 18:11:49 -070078// 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.
84MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface,
Yifan Hong643a9ef2018-03-21 14:13:55 -070085 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 Honge7e45532018-03-16 18:11:49 -0700106 return nullptr;
107 }
108
Yifan Hong643a9ef2018-03-21 14:13:55 -0700109 if (!foundOthers) {
Yifan Honge7e45532018-03-16 18:11:49 -0700110 return existingHal;
111 }
112
Yifan Hong643a9ef2018-03-21 14:13:55 -0700113 existingHal->removeInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700114 MatrixHal copy = *existingHal;
115 copy.clearInstances();
Yifan Hong643a9ef2018-03-21 14:13:55 -0700116 copy.insertInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700117
118 return addInternal(std::move(copy));
119}
120
Yifan Hong7967d7b2018-03-15 17:08:58 -0700121// 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 Hongdbe9db32017-12-11 19:06:11 -0800126bool 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 Hongdbe9db32017-12-11 19:06:11 -0800134
Yifan Honge7e45532018-03-16 18:11:49 -0700135 std::set<std::pair<std::string, std::string>> insertedInstances;
Yifan Hong643a9ef2018-03-21 14:13:55 -0700136 std::set<std::pair<std::string, std::string>> insertedRegex;
Yifan Honge7e45532018-03-16 18:11:49 -0700137 auto existingHals = getHals(name);
138
139 halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges,
Yifan Hong643a9ef2018-03-21 14:13:55 -0700140 const std::string& interface,
141 const std::string& instanceOrPattern, bool isRegex) {
Yifan Honge7e45532018-03-16 18:11:49 -0700142 for (auto* existingHal : existingHals) {
Yifan Hong643a9ef2018-03-21 14:13:55 -0700143 MatrixHal* splitInstance =
144 this->splitInstance(existingHal, interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700145 if (splitInstance != nullptr) {
146 splitInstance->insertVersionRanges(versionRanges);
Yifan Hong643a9ef2018-03-21 14:13:55 -0700147 if (isRegex) {
148 insertedRegex.insert(std::make_pair(interface, instanceOrPattern));
149 } else {
150 insertedInstances.insert(std::make_pair(interface, instanceOrPattern));
151 }
Yifan Honge7e45532018-03-16 18:11:49 -0700152 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800153 }
Yifan Honge7e45532018-03-16 18:11:49 -0700154 return true;
155 });
156
157 // Add the remaining instances.
158 for (const auto& pair : insertedInstances) {
Yifan Hong643a9ef2018-03-21 14:13:55 -0700159 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 Hong7967d7b2018-03-15 17:08:58 -0700163 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800164
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700165 if (halToAdd.instancesCount() > 0) {
Yifan Hong7967d7b2018-03-15 17:08:58 -0700166 halToAdd.setOptional(true);
167 if (!add(std::move(halToAdd))) {
168 if (error) {
169 *error = "Cannot add HAL " + name + " for unknown reason.";
Yifan Hongdbe9db32017-12-11 19:06:11 -0800170 }
171 return false;
172 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800173 }
174 }
175 return true;
176}
177
Yifan Hongd4b92fe2017-12-20 15:29:03 -0800178bool 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 Hongfb7469c2017-04-05 19:15:21 -0700197bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) {
Yifan Hong2027a492017-12-11 15:21:19 -0800198 return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals &&
199 lft.mXmlFiles == rgt.mXmlFiles &&
Yifan Hongfeb454e2018-01-09 16:16:40 -0800200 (lft.mType != SchemaType::DEVICE ||
201 (
Yifan Hong0f529fa2018-01-10 14:51:59 -0800202#pragma clang diagnostic push
203#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Yifan Hongfeb454e2018-01-09 16:16:40 -0800204 lft.device.mVndk == rgt.device.mVndk &&
Yifan Hong0f529fa2018-01-10 14:51:59 -0800205#pragma clang diagnostic pop
Yifan Honga28729e2018-01-17 13:40:35 -0800206 lft.device.mVendorNdk == rgt.device.mVendorNdk &&
207 lft.device.mSystemSdk == rgt.device.mSystemSdk)) &&
Yifan Hongd4857902017-06-13 14:13:56 -0700208 (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 Hongfb7469c2017-04-05 19:15:21 -0700212}
213
Yifan Hongddae77e2017-12-18 16:57:07 -0800214// Find compatibility_matrix.empty.xml (which has unspecified level) and use it
215// as a base matrix.
216CompatibilityMatrix* CompatibilityMatrix::findOrInsertBaseMatrix(
Yifan Hongffcaf992018-01-09 17:08:51 -0800217 std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700218 std::vector<CompatibilityMatrix*> matricesUnspecified;
219 std::vector<CompatibilityMatrix*> matricesEmpty;
Yifan Hongddae77e2017-12-18 16:57:07 -0800220 for (auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800221 if (e.object.level() == Level::UNSPECIFIED) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700222 matricesUnspecified.push_back(&e.object);
223
Yifan Hongffcaf992018-01-09 17:08:51 -0800224 if (!e.object.mHals.empty()) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700225 continue;
Yifan Hongddae77e2017-12-18 16:57:07 -0800226 }
227
Yifan Hongffcaf992018-01-09 17:08:51 -0800228 if (!e.object.mXmlFiles.empty()) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700229 continue;
Yifan Hongddae77e2017-12-18 16:57:07 -0800230 }
231
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700232 matricesEmpty.push_back(&e.object);
Yifan Hongddae77e2017-12-18 16:57:07 -0800233 }
234 }
235
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700236 if (matricesEmpty.size() > 1) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800237 if (error) {
238 *error =
239 "Error: multiple framework compatibility matrix files have "
240 "unspecified level; there should only be one such file.\n";
241 for (auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800242 if (e.object.level() == Level::UNSPECIFIED) {
243 *error += " " + e.name + "\n";
Yifan Hongddae77e2017-12-18 16:57:07 -0800244 }
245 }
246 }
247 return nullptr;
248 }
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700249 if (matricesEmpty.size() == 1) {
250 return matricesEmpty.front();
Yifan Hongddae77e2017-12-18 16:57:07 -0800251 }
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700252 if (!matricesUnspecified.empty()) {
253 return matricesUnspecified.front();
254 }
255 auto matrix = &matrices->emplace(matrices->end())->object;
256 matrix->mType = SchemaType::FRAMEWORK;
257 matrix->mLevel = Level::UNSPECIFIED;
Yifan Hongddae77e2017-12-18 16:57:07 -0800258 return matrix;
259}
260
Yifan Hongffcaf992018-01-09 17:08:51 -0800261CompatibilityMatrix* CompatibilityMatrix::combine(Level deviceLevel,
262 std::vector<Named<CompatibilityMatrix>>* matrices,
263 std::string* error) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800264
265 CompatibilityMatrix* matrix = findOrInsertBaseMatrix(matrices, error);
266 if (matrix == nullptr) {
267 return nullptr;
268 }
269
270 matrix->mLevel = deviceLevel;
271
272 for (auto& e : *matrices) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700273 if (&e.object != matrix &&
274 (e.object.level() == deviceLevel || e.object.level() == Level::UNSPECIFIED)) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800275 if (!matrix->addAllHals(&e.object, error)) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800276 if (error) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800277 *error = "File \"" + e.name + "\" cannot be added: HAL " + *error +
Yifan Hongddae77e2017-12-18 16:57:07 -0800278 " has a conflict.";
279 }
280 return nullptr;
281 }
282
Yifan Hongffcaf992018-01-09 17:08:51 -0800283 if (!matrix->addAllXmlFiles(&e.object, error)) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800284 if (error) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800285 *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error +
Yifan Hongddae77e2017-12-18 16:57:07 -0800286 " has a conflict.";
287 }
288 return nullptr;
289 }
290 }
291 }
292
293 for (auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800294 if (&e.object != matrix && e.object.level() != Level::UNSPECIFIED &&
295 e.object.level() > deviceLevel) {
296 if (!matrix->addAllHalsAsOptional(&e.object, error)) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800297 if (error) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800298 *error = "File \"" + e.name + "\" cannot be added: " + *error +
Yifan Hongddae77e2017-12-18 16:57:07 -0800299 ". See <hal> with the same name " +
300 "in previously parsed files or previously declared in this file.";
301 }
302 return nullptr;
303 }
304
Yifan Hongffcaf992018-01-09 17:08:51 -0800305 if (!matrix->addAllXmlFilesAsOptional(&e.object, error)) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800306 if (error) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800307 *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error +
Yifan Hongddae77e2017-12-18 16:57:07 -0800308 " has a conflict.";
309 }
310 return nullptr;
311 }
312 }
313 }
314
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800315 for (auto& e : *matrices) {
316 if (&e.object != matrix && e.object.level() == deviceLevel &&
317 e.object.type() == SchemaType::FRAMEWORK) {
318 for (MatrixKernel& kernel : e.object.framework.mKernels) {
319 KernelVersion ver = kernel.minLts();
320 if (!matrix->add(std::move(kernel))) {
321 if (error) {
322 *error = "Cannot add kernel version " + to_string(ver) +
323 " from FCM version " + to_string(deviceLevel);
324 }
325 return nullptr;
326 }
327 }
328 }
329 }
330
Yifan Hongddae77e2017-12-18 16:57:07 -0800331 return matrix;
332}
333
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800334bool CompatibilityMatrix::forEachInstanceOfVersion(
335 const std::string& package, const Version& expectVersion,
336 const std::function<bool(const MatrixInstance&)>& func) const {
337 for (const MatrixHal* hal : getHals(package)) {
338 bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) {
339 if (matrixInstance.versionRange().contains(expectVersion)) {
340 return func(matrixInstance);
Yifan Honge3a92342018-01-25 17:00:16 -0800341 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800342 return true;
343 });
344 if (!cont) return false;
Yifan Honge3a92342018-01-25 17:00:16 -0800345 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800346 return true;
Yifan Honge3a92342018-01-25 17:00:16 -0800347}
348
Yifan Hongdef7e7f2018-03-20 13:27:36 -0700349bool CompatibilityMatrix::matchInstance(const std::string& halName, const Version& version,
350 const std::string& interfaceName,
351 const std::string& instance) const {
352 bool found = false;
353 (void)forEachInstanceOfInterface(halName, version, interfaceName,
354 [&found, &instance](const auto& e) {
355 found |= (e.matchInstance(instance));
356 return !found; // if not found, continue
357 });
358 return found;
359}
360
Yifan Hong676447a2016-11-15 12:57:23 -0800361} // namespace vintf
362} // namespace android