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 "MatrixHal.h" |
| 18 | |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 21 | #include "MapValueIterator.h" |
| 22 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace vintf { |
| 25 | |
| 26 | bool MatrixHal::operator==(const MatrixHal &other) const { |
| 27 | if (format != other.format) |
| 28 | return false; |
| 29 | if (name != other.name) |
| 30 | return false; |
| 31 | if (versionRanges != other.versionRanges) |
| 32 | return false; |
Yifan Hong | 43e2aae | 2017-05-17 18:36:08 -0700 | [diff] [blame] | 33 | if (interfaces != other.interfaces) |
| 34 | return false; |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 35 | // do not compare optional |
| 36 | return true; |
| 37 | } |
| 38 | |
Zhuoyao Zhang | c7e75bd | 2017-10-29 17:20:19 -0700 | [diff] [blame] | 39 | bool MatrixHal::containsVersion(const Version& version) const { |
| 40 | for (VersionRange vRange : versionRanges) { |
| 41 | if (vRange.contains(version)) return true; |
| 42 | } |
| 43 | return false; |
| 44 | } |
| 45 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 46 | bool MatrixHal::forEachInstance(const std::function<bool(const MatrixInstance&)>& func) const { |
| 47 | for (const auto& vr : versionRanges) { |
Yifan Hong | 075b363 | 2018-03-14 16:03:11 -0700 | [diff] [blame] | 48 | if (!forEachInstance(vr, func)) { |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | bool MatrixHal::forEachInstance(const VersionRange& vr, |
| 56 | const std::function<bool(const MatrixInstance&)>& func) const { |
| 57 | for (const auto& intf : iterateValues(interfaces)) { |
| 58 | for (const auto& instance : intf.instances) { |
| 59 | // TODO(b/73556059): Store MatrixInstance as well to avoid creating temps |
| 60 | FqInstance fqInstance; |
| 61 | if (fqInstance.setTo(getName(), vr.majorVer, vr.minMinor, intf.name, instance)) { |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame^] | 62 | if (!func(MatrixInstance(std::move(fqInstance), VersionRange(vr), optional, |
| 63 | false /* isRegex */))) { |
Yifan Hong | 075b363 | 2018-03-14 16:03:11 -0700 | [diff] [blame] | 64 | return false; |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 72 | bool MatrixHal::forEachInstance( |
| 73 | const std::function<bool(const std::vector<VersionRange>&, const std::string&, |
| 74 | const std::string&)>& func) const { |
| 75 | for (const auto& intf : iterateValues(interfaces)) { |
| 76 | for (const auto& instance : intf.instances) { |
| 77 | if (!func(versionRanges, intf.name, instance)) { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
Yifan Hong | 075b363 | 2018-03-14 16:03:11 -0700 | [diff] [blame] | 85 | bool MatrixHal::isCompatible(const std::set<FqInstance>& providedInstances, |
| 86 | const std::set<Version>& providedVersions) const { |
| 87 | // <version>'s are related by OR. |
| 88 | return std::any_of(versionRanges.begin(), versionRanges.end(), [&](const VersionRange& vr) { |
| 89 | return isCompatible(vr, providedInstances, providedVersions); |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | bool MatrixHal::isCompatible(const VersionRange& vr, const std::set<FqInstance>& providedInstances, |
| 94 | const std::set<Version>& providedVersions) const { |
| 95 | bool hasAnyInstance = false; |
| 96 | bool versionUnsatisfied = false; |
| 97 | |
| 98 | // Look at each interface/instance, and ensure that they are in providedInstances. |
| 99 | forEachInstance(vr, [&](const MatrixInstance& matrixInstance) { |
| 100 | hasAnyInstance = true; |
| 101 | |
| 102 | versionUnsatisfied |= |
| 103 | !std::any_of(providedInstances.begin(), providedInstances.end(), |
| 104 | [&](const FqInstance& providedInstance) { |
| 105 | return matrixInstance.isSatisfiedBy(providedInstance); |
| 106 | }); |
| 107 | |
| 108 | return !versionUnsatisfied; // if any interface/instance is unsatisfied, break |
| 109 | }); |
| 110 | |
| 111 | if (hasAnyInstance) { |
| 112 | return !versionUnsatisfied; |
| 113 | } |
| 114 | |
| 115 | // In some cases (e.g. tests and native HALs), compatibility matrix doesn't specify |
| 116 | // any instances. Check versions only. |
| 117 | return std::any_of( |
| 118 | providedVersions.begin(), providedVersions.end(), |
| 119 | [&](const auto& providedVersion) { return vr.supportedBy(providedVersion); }); |
| 120 | } |
| 121 | |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 122 | void MatrixHal::setOptional(bool o) { |
| 123 | this->optional = o; |
| 124 | } |
| 125 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 126 | void MatrixHal::insertVersionRanges(const std::vector<VersionRange>& other) { |
| 127 | for (const VersionRange& otherVr : other) { |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 128 | auto existingVr = std::find_if(this->versionRanges.begin(), this->versionRanges.end(), |
| 129 | [&](const auto& e) { return e.overlaps(otherVr); }); |
| 130 | |
| 131 | if (existingVr == this->versionRanges.end()) { |
| 132 | this->versionRanges.push_back(otherVr); |
| 133 | } else { |
Yifan Hong | 5da19c9 | 2018-03-15 17:11:43 -0700 | [diff] [blame] | 134 | existingVr->minMinor = std::min(existingVr->minMinor, otherVr.minMinor); |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 135 | existingVr->maxMinor = std::max(existingVr->maxMinor, otherVr.maxMinor); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 140 | void MatrixHal::insertInstance(const std::string& interface, const std::string& instance) { |
| 141 | auto it = interfaces.find(interface); |
| 142 | if (it == interfaces.end()) |
| 143 | it = interfaces.emplace(interface, HalInterface{interface, {}}).first; |
| 144 | it->second.instances.insert(instance); |
| 145 | } |
| 146 | |
| 147 | bool MatrixHal::hasAnyInstance() const { |
| 148 | bool found = false; |
| 149 | forEachInstance([&](const auto&) { |
| 150 | found = true; |
| 151 | return false; // break if any instance |
| 152 | }); |
| 153 | return found; |
| 154 | } |
| 155 | |
| 156 | bool MatrixHal::hasInstance(const std::string& interface, const std::string& instance) const { |
| 157 | bool found = false; |
| 158 | forEachInstance([&](const auto& matrixInstance) { |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame^] | 159 | found |= matrixInstance.interface() == interface && !matrixInstance.isRegex() && |
| 160 | matrixInstance.matchInstance(instance); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 161 | return !found; // continue if not match |
| 162 | }); |
| 163 | return found; |
| 164 | } |
| 165 | |
| 166 | bool MatrixHal::hasOnlyInstance(const std::string& interface, const std::string& instance) const { |
| 167 | bool found = false; |
| 168 | bool foundOthers = false; |
| 169 | |
| 170 | forEachInstance([&](const auto& matrixInstance) { |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame^] | 171 | bool match = matrixInstance.interface() == interface && !matrixInstance.isRegex() && |
| 172 | matrixInstance.matchInstance(instance); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 173 | |
| 174 | found |= match; |
| 175 | foundOthers |= (!match); |
| 176 | |
| 177 | return !foundOthers; |
| 178 | }); |
| 179 | |
| 180 | return found && !foundOthers; |
| 181 | } |
| 182 | |
| 183 | bool MatrixHal::removeInstance(const std::string& interface, const std::string& instance) { |
| 184 | auto it = interfaces.find(interface); |
| 185 | if (it == interfaces.end()) return false; |
| 186 | it->second.instances.erase(instance); |
| 187 | if (it->second.instances.empty()) interfaces.erase(it); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | void MatrixHal::clearInstances() { |
| 192 | this->interfaces.clear(); |
| 193 | } |
| 194 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 195 | } // namespace vintf |
| 196 | } // namespace android |