Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include "KernelInfo.h" |
| 17 | |
| 18 | #include "parse_string.h" |
Yifan Hong | f66094f | 2019-12-11 14:23:08 -0800 | [diff] [blame^] | 19 | #include "utils.h" |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 20 | |
| 21 | namespace android { |
| 22 | namespace vintf { |
| 23 | |
Yifan Hong | f66094f | 2019-12-11 14:23:08 -0800 | [diff] [blame^] | 24 | using details::mergeField; |
| 25 | |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 26 | const KernelVersion& KernelInfo::version() const { |
| 27 | return mVersion; |
| 28 | } |
| 29 | |
| 30 | const std::map<std::string, std::string>& KernelInfo::configs() const { |
| 31 | return mConfigs; |
| 32 | } |
| 33 | |
Yifan Hong | e624844 | 2019-12-11 13:33:00 -0800 | [diff] [blame] | 34 | Level KernelInfo::level() const { |
| 35 | return mLevel; |
| 36 | } |
| 37 | |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 38 | bool KernelInfo::matchKernelConfigs(const std::vector<KernelConfig>& matrixConfigs, |
| 39 | std::string* error) const { |
| 40 | for (const KernelConfig& matrixConfig : matrixConfigs) { |
| 41 | const std::string& key = matrixConfig.first; |
| 42 | auto it = this->mConfigs.find(key); |
| 43 | if (it == this->mConfigs.end()) { |
| 44 | // special case: <value type="tristate">n</value> matches if the config doesn't exist. |
| 45 | if (matrixConfig.second == KernelConfigTypedValue::gMissingConfig) { |
| 46 | continue; |
| 47 | } |
| 48 | if (error != nullptr) { |
| 49 | *error = "Missing config " + key; |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | const std::string& kernelValue = it->second; |
| 54 | if (!matrixConfig.second.matchValue(kernelValue)) { |
| 55 | if (error != nullptr) { |
| 56 | *error = "For config " + key + ", value = " + kernelValue + " but required " + |
| 57 | to_string(matrixConfig.second); |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | bool KernelInfo::matchKernelVersion(const KernelVersion& minLts) const { |
| 66 | return minLts.version == mVersion.version && minLts.majorRev == mVersion.majorRev && |
| 67 | minLts.minorRev <= mVersion.minorRev; |
| 68 | } |
| 69 | |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 70 | std::vector<const MatrixKernel*> KernelInfo::getMatchedKernelRequirements( |
| 71 | const std::vector<MatrixKernel>& kernels, std::string* error) const { |
| 72 | std::vector<const MatrixKernel*> result; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 73 | bool foundMatchedKernelVersion = false; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 74 | for (const MatrixKernel& matrixKernel : kernels) { |
| 75 | if (!matchKernelVersion(matrixKernel.minLts())) { |
| 76 | continue; |
| 77 | } |
| 78 | foundMatchedKernelVersion = true; |
| 79 | // ignore this fragment if not all conditions are met. |
| 80 | if (!matchKernelConfigs(matrixKernel.conditions(), error)) { |
| 81 | continue; |
| 82 | } |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 83 | if (!matchKernelConfigs(matrixKernel.configs(), error)) { |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 84 | return {}; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 85 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 86 | result.push_back(&matrixKernel); |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 87 | } |
| 88 | if (!foundMatchedKernelVersion) { |
| 89 | if (error != nullptr) { |
| 90 | std::stringstream ss; |
| 91 | ss << "Framework is incompatible with kernel version " << version() |
| 92 | << ", compatible kernel versions are"; |
| 93 | for (const MatrixKernel& matrixKernel : kernels) ss << " " << matrixKernel.minLts(); |
| 94 | *error = ss.str(); |
| 95 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 96 | return {}; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 97 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 98 | if (result.empty()) { |
| 99 | // This means matchKernelVersion passes but matchKernelConfigs(conditions) fails. |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 100 | // This should not happen because first <conditions> for each <kernel> must be |
| 101 | // empty. Reject here for inconsistency. |
| 102 | if (error != nullptr) { |
| 103 | error->insert(0, "Framework match kernel version with unmet conditions:"); |
| 104 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 105 | return {}; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 106 | } |
| 107 | if (error != nullptr) { |
| 108 | error->clear(); |
| 109 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 110 | return result; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Yifan Hong | a45f77d | 2018-12-03 16:42:52 -0800 | [diff] [blame] | 113 | bool KernelInfo::operator==(const KernelInfo& other) const { |
| 114 | return mVersion == other.mVersion && mConfigs == other.mConfigs; |
| 115 | } |
| 116 | |
Yifan Hong | f66094f | 2019-12-11 14:23:08 -0800 | [diff] [blame^] | 117 | bool KernelInfo::merge(KernelInfo* other, std::string* error) { |
| 118 | if (!mergeField(&mVersion, &other->mVersion)) { |
| 119 | if (error) { |
| 120 | *error = "Conflicting kernel version: " + to_string(version()) + " vs. " + |
| 121 | to_string(other->version()); |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // Do not allow merging configs. One of them must be empty. |
| 127 | if (!mergeField(&mConfigs, &other->mConfigs)) { |
| 128 | if (error) { |
| 129 | *error = "Found <kernel><config> items in two manifests."; |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if (!mergeField(&mLevel, &other->mLevel, Level::UNSPECIFIED)) { |
| 135 | if (error) { |
| 136 | *error = "Conflicting kernel level: " + to_string(level()) + " vs. " + |
| 137 | to_string(other->level()); |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 144 | } // namespace vintf |
| 145 | } // namespace android |