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 | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame^] | 78 | // Add all package@other_version::interface/instance as an optional instance. |
| 79 | // If package@this_version::interface/instance is in this (that is, some instance |
| 80 | // with the same package and interface and instance exists), then other_version is |
| 81 | // considered a possible replacement to this_version. |
| 82 | // See LibVintfTest.AddOptionalHal* tests for details. |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 83 | bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 84 | if (other == nullptr || other->level() <= level()) { |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | for (auto& pair : other->mHals) { |
| 89 | const std::string& name = pair.first; |
| 90 | MatrixHal& halToAdd = pair.second; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 91 | |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame^] | 92 | bool added = false; |
| 93 | for (auto* existingHal : getHals(name)) { |
| 94 | if (existingHal->containsInstances(halToAdd)) { |
| 95 | existingHal->insertVersionRanges(halToAdd); |
| 96 | added = true; |
| 97 | // Do not break here; try other <hal> with the same name as well. |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 98 | } |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame^] | 99 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 100 | |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame^] | 101 | if (!added) { |
| 102 | halToAdd.setOptional(true); |
| 103 | if (!add(std::move(halToAdd))) { |
| 104 | if (error) { |
| 105 | *error = "Cannot add HAL " + name + " for unknown reason."; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 106 | } |
| 107 | return false; |
| 108 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
Yifan Hong | d4b92fe | 2017-12-20 15:29:03 -0800 | [diff] [blame] | 114 | bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 115 | if (other == nullptr || other->level() <= level()) { |
| 116 | return true; |
| 117 | } |
| 118 | for (auto& pair : other->mXmlFiles) { |
| 119 | const std::string& name = pair.first; |
| 120 | MatrixXmlFile& xmlFileToAdd = pair.second; |
| 121 | |
| 122 | xmlFileToAdd.mOptional = true; |
| 123 | if (!addXmlFile(std::move(xmlFileToAdd))) { |
| 124 | if (error) { |
| 125 | *error = "Cannot add XML File " + name + " for unknown reason."; |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 133 | bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) { |
Yifan Hong | 2027a49 | 2017-12-11 15:21:19 -0800 | [diff] [blame] | 134 | return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals && |
| 135 | lft.mXmlFiles == rgt.mXmlFiles && |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 136 | (lft.mType != SchemaType::DEVICE || |
| 137 | ( |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 138 | #pragma clang diagnostic push |
| 139 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 140 | lft.device.mVndk == rgt.device.mVndk && |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 141 | #pragma clang diagnostic pop |
Yifan Hong | a28729e | 2018-01-17 13:40:35 -0800 | [diff] [blame] | 142 | lft.device.mVendorNdk == rgt.device.mVendorNdk && |
| 143 | lft.device.mSystemSdk == rgt.device.mSystemSdk)) && |
Yifan Hong | d485790 | 2017-06-13 14:13:56 -0700 | [diff] [blame] | 144 | (lft.mType != SchemaType::FRAMEWORK || |
| 145 | (lft.framework.mKernels == rgt.framework.mKernels && |
| 146 | lft.framework.mSepolicy == rgt.framework.mSepolicy && |
| 147 | lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion)); |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 150 | // Find compatibility_matrix.empty.xml (which has unspecified level) and use it |
| 151 | // as a base matrix. |
| 152 | CompatibilityMatrix* CompatibilityMatrix::findOrInsertBaseMatrix( |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 153 | std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 154 | bool multipleFound = false; |
| 155 | CompatibilityMatrix* matrix = nullptr; |
| 156 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 157 | if (e.object.level() == Level::UNSPECIFIED) { |
| 158 | if (!e.object.mHals.empty()) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 159 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 160 | *error = "Error: File \"" + e.name + "\" should not contain " + "HAL elements."; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 161 | } |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 165 | if (!e.object.mXmlFiles.empty()) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 166 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 167 | *error = |
| 168 | "Error: File \"" + e.name + "\" should not contain " + "XML File elements."; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 169 | } |
| 170 | return nullptr; |
| 171 | } |
| 172 | |
| 173 | if (matrix != nullptr) { |
| 174 | multipleFound = true; |
| 175 | } |
| 176 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 177 | matrix = &e.object; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 178 | // continue to detect multiple files with "unspecified" levels |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (multipleFound) { |
| 183 | if (error) { |
| 184 | *error = |
| 185 | "Error: multiple framework compatibility matrix files have " |
| 186 | "unspecified level; there should only be one such file.\n"; |
| 187 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 188 | if (e.object.level() == Level::UNSPECIFIED) { |
| 189 | *error += " " + e.name + "\n"; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | return nullptr; |
| 194 | } |
| 195 | |
| 196 | if (matrix == nullptr) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 197 | matrix = &matrices->emplace(matrices->end())->object; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 198 | matrix->mType = SchemaType::FRAMEWORK; |
| 199 | matrix->mLevel = Level::UNSPECIFIED; |
| 200 | } |
| 201 | |
| 202 | return matrix; |
| 203 | } |
| 204 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 205 | CompatibilityMatrix* CompatibilityMatrix::combine(Level deviceLevel, |
| 206 | std::vector<Named<CompatibilityMatrix>>* matrices, |
| 207 | std::string* error) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 208 | if (deviceLevel == Level::UNSPECIFIED) { |
| 209 | if (error) { |
| 210 | *error = "Error: device level is unspecified."; |
| 211 | } |
| 212 | return nullptr; |
| 213 | } |
| 214 | |
| 215 | CompatibilityMatrix* matrix = findOrInsertBaseMatrix(matrices, error); |
| 216 | if (matrix == nullptr) { |
| 217 | return nullptr; |
| 218 | } |
| 219 | |
| 220 | matrix->mLevel = deviceLevel; |
| 221 | |
| 222 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 223 | if (&e.object != matrix && e.object.level() == deviceLevel) { |
| 224 | if (!matrix->addAllHals(&e.object, error)) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 225 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 226 | *error = "File \"" + e.name + "\" cannot be added: HAL " + *error + |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 227 | " has a conflict."; |
| 228 | } |
| 229 | return nullptr; |
| 230 | } |
| 231 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 232 | if (!matrix->addAllXmlFiles(&e.object, error)) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 233 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 234 | *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error + |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 235 | " has a conflict."; |
| 236 | } |
| 237 | return nullptr; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 243 | if (&e.object != matrix && e.object.level() != Level::UNSPECIFIED && |
| 244 | e.object.level() > deviceLevel) { |
| 245 | if (!matrix->addAllHalsAsOptional(&e.object, error)) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 246 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 247 | *error = "File \"" + e.name + "\" cannot be added: " + *error + |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 248 | ". See <hal> with the same name " + |
| 249 | "in previously parsed files or previously declared in this file."; |
| 250 | } |
| 251 | return nullptr; |
| 252 | } |
| 253 | |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 254 | if (!matrix->addAllXmlFilesAsOptional(&e.object, error)) { |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 255 | if (error) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 256 | *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error + |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 257 | " has a conflict."; |
| 258 | } |
| 259 | return nullptr; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Yifan Hong | f7e8b1b | 2018-01-23 15:30:35 -0800 | [diff] [blame] | 264 | for (auto& e : *matrices) { |
| 265 | if (&e.object != matrix && e.object.level() == deviceLevel && |
| 266 | e.object.type() == SchemaType::FRAMEWORK) { |
| 267 | for (MatrixKernel& kernel : e.object.framework.mKernels) { |
| 268 | KernelVersion ver = kernel.minLts(); |
| 269 | if (!matrix->add(std::move(kernel))) { |
| 270 | if (error) { |
| 271 | *error = "Cannot add kernel version " + to_string(ver) + |
| 272 | " from FCM version " + to_string(deviceLevel); |
| 273 | } |
| 274 | return nullptr; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 280 | return matrix; |
| 281 | } |
| 282 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 283 | bool CompatibilityMatrix::forEachInstanceOfVersion( |
| 284 | const std::string& package, const Version& expectVersion, |
| 285 | const std::function<bool(const MatrixInstance&)>& func) const { |
| 286 | for (const MatrixHal* hal : getHals(package)) { |
| 287 | bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) { |
| 288 | if (matrixInstance.versionRange().contains(expectVersion)) { |
| 289 | return func(matrixInstance); |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 290 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 291 | return true; |
| 292 | }); |
| 293 | if (!cont) return false; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 294 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 295 | return true; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 298 | } // namespace vintf |
| 299 | } // namespace android |