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 | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame^] | 19 | #include <utility> |
| 20 | |
Yifan Hong | bbfff30 | 2017-06-06 17:10:13 -0700 | [diff] [blame] | 21 | #include "parse_string.h" |
Yifan Hong | 1e5a054 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 22 | #include "utils.h" |
| 23 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace vintf { |
| 26 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 27 | bool CompatibilityMatrix::add(MatrixHal &&hal) { |
Yifan Hong | 0fd7aef | 2017-05-24 14:37:19 -0700 | [diff] [blame] | 28 | return HalGroup<MatrixHal>::add(std::move(hal)); |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool CompatibilityMatrix::add(MatrixKernel &&kernel) { |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 32 | if (mType != SchemaType::FRAMEWORK) { |
| 33 | return false; |
| 34 | } |
| 35 | framework.mKernels.push_back(std::move(kernel)); |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 36 | return true; |
| 37 | } |
| 38 | |
Yifan Hong | 398f4c7 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 39 | SchemaType CompatibilityMatrix::type() const { |
| 40 | return mType; |
| 41 | } |
| 42 | |
Yifan Hong | 2027a49 | 2017-12-11 15:21:19 -0800 | [diff] [blame] | 43 | Level CompatibilityMatrix::level() const { |
| 44 | return mLevel; |
| 45 | } |
| 46 | |
Yifan Hong | db127cb | 2017-09-19 13:36:21 -0700 | [diff] [blame] | 47 | Version CompatibilityMatrix::getMinimumMetaVersion() const { |
| 48 | // TODO(b/62801658): this needs to depend on whether there are 1.1 requirements |
| 49 | // (e.g. required <xmlfile> entry) |
| 50 | return {1, 0}; |
| 51 | } |
Yifan Hong | 1e5a054 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 52 | |
| 53 | status_t CompatibilityMatrix::fetchAllInformation(const std::string &path) { |
| 54 | return details::fetchAllInformation(path, gCompatibilityMatrixConverter, this); |
| 55 | } |
| 56 | |
Yifan Hong | bbfff30 | 2017-06-06 17:10:13 -0700 | [diff] [blame] | 57 | std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName, |
| 58 | const Version& version) const { |
| 59 | using std::literals::string_literals::operator""s; |
| 60 | auto range = getXmlFiles(xmlFileName); |
| 61 | for (auto it = range.first; it != range.second; ++it) { |
| 62 | const MatrixXmlFile& matrixXmlFile = it->second; |
| 63 | if (matrixXmlFile.versionRange().contains(version)) { |
| 64 | if (!matrixXmlFile.overriddenPath().empty()) { |
| 65 | return matrixXmlFile.overriddenPath(); |
| 66 | } |
| 67 | return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" + |
| 68 | xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) + |
| 69 | "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." + |
| 70 | to_string(matrixXmlFile.format()); |
| 71 | } |
| 72 | } |
| 73 | return ""; |
| 74 | } |
| 75 | |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame^] | 76 | static VersionRange* findRangeWithMajorVersion(std::vector<VersionRange>& versionRanges, |
| 77 | size_t majorVer) { |
| 78 | for (VersionRange& vr : versionRanges) { |
| 79 | if (vr.majorVer == majorVer) { |
| 80 | return &vr; |
| 81 | } |
| 82 | } |
| 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | std::pair<MatrixHal*, VersionRange*> CompatibilityMatrix::getHalWithMajorVersion( |
| 87 | const std::string& name, size_t majorVer) { |
| 88 | for (MatrixHal* hal : getHals(name)) { |
| 89 | VersionRange* vr = findRangeWithMajorVersion(hal->versionRanges, majorVer); |
| 90 | if (vr != nullptr) { |
| 91 | return {hal, vr}; |
| 92 | } |
| 93 | } |
| 94 | return {nullptr, nullptr}; |
| 95 | } |
| 96 | |
| 97 | bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 98 | if (other == nullptr || other->level() <= level()) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | for (auto& pair : other->mHals) { |
| 103 | const std::string& name = pair.first; |
| 104 | MatrixHal& halToAdd = pair.second; |
| 105 | for (const VersionRange& vr : halToAdd.versionRanges) { |
| 106 | MatrixHal* existingHal; |
| 107 | VersionRange* existingVr; |
| 108 | std::tie(existingHal, existingVr) = getHalWithMajorVersion(name, vr.majorVer); |
| 109 | |
| 110 | if (existingHal == nullptr) { |
| 111 | halToAdd.optional = true; |
| 112 | add(std::move(halToAdd)); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if (!existingHal->optional && !existingHal->containsInstances(halToAdd)) { |
| 117 | if (error != nullptr) { |
| 118 | *error = "HAL " + name + "@" + to_string(vr.minVer()) + " is a required " + |
| 119 | "HAL, but fully qualified instance names don't match (at FCM " |
| 120 | "Version " + |
| 121 | std::to_string(level()) + " and " + std::to_string(other->level()) + |
| 122 | ")"; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | existingVr->maxMinor = std::max(existingVr->maxMinor, vr.maxMinor); |
| 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 | d485790 | 2017-06-13 14:13:56 -0700 | [diff] [blame] | 136 | (lft.mType != SchemaType::DEVICE || (lft.device.mVndk == rgt.device.mVndk)) && |
| 137 | (lft.mType != SchemaType::FRAMEWORK || |
| 138 | (lft.framework.mKernels == rgt.framework.mKernels && |
| 139 | lft.framework.mSepolicy == rgt.framework.mSepolicy && |
| 140 | lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion)); |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 143 | } // namespace vintf |
| 144 | } // namespace android |