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