blob: a7a3b2df0e9edf693c0053d088e90bd0c6f78eab [file] [log] [blame]
Yifan Hong676447a2016-11-15 12:57:23 -08001/*
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 Hong7967d7b2018-03-15 17:08:58 -070019#include <algorithm>
20
Yifan Hong2a90ffe2018-03-05 17:45:34 -080021#include "MapValueIterator.h"
22
Yifan Hong676447a2016-11-15 12:57:23 -080023namespace android {
24namespace vintf {
25
26bool 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 Hong43e2aae2017-05-17 18:36:08 -070033 if (interfaces != other.interfaces)
34 return false;
Yifan Hong676447a2016-11-15 12:57:23 -080035 // do not compare optional
36 return true;
37}
38
Zhuoyao Zhangc7e75bd2017-10-29 17:20:19 -070039bool 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 Hong2a90ffe2018-03-05 17:45:34 -080046bool MatrixHal::forEachInstance(const std::function<bool(const MatrixInstance&)>& func) const {
47 for (const auto& vr : versionRanges) {
Yifan Hong075b3632018-03-14 16:03:11 -070048 if (!forEachInstance(vr, func)) {
49 return false;
50 }
51 }
52 return true;
53}
54
55bool 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 Hong85103c62018-03-20 15:43:34 -070062 if (!func(MatrixInstance(std::move(fqInstance), VersionRange(vr), optional,
63 false /* isRegex */))) {
Yifan Hong075b3632018-03-14 16:03:11 -070064 return false;
Yifan Hong2a90ffe2018-03-05 17:45:34 -080065 }
66 }
67 }
68 }
69 return true;
70}
71
Yifan Honge7e45532018-03-16 18:11:49 -070072bool 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 Hong075b3632018-03-14 16:03:11 -070085bool 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
93bool 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 Hong7967d7b2018-03-15 17:08:58 -0700122void MatrixHal::setOptional(bool o) {
123 this->optional = o;
124}
125
Yifan Honge7e45532018-03-16 18:11:49 -0700126void MatrixHal::insertVersionRanges(const std::vector<VersionRange>& other) {
127 for (const VersionRange& otherVr : other) {
Yifan Hong7967d7b2018-03-15 17:08:58 -0700128 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 Hong5da19c92018-03-15 17:11:43 -0700134 existingVr->minMinor = std::min(existingVr->minMinor, otherVr.minMinor);
Yifan Hong7967d7b2018-03-15 17:08:58 -0700135 existingVr->maxMinor = std::max(existingVr->maxMinor, otherVr.maxMinor);
136 }
137 }
138}
139
Yifan Honge7e45532018-03-16 18:11:49 -0700140void 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
147bool 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
156bool MatrixHal::hasInstance(const std::string& interface, const std::string& instance) const {
157 bool found = false;
158 forEachInstance([&](const auto& matrixInstance) {
Yifan Hong85103c62018-03-20 15:43:34 -0700159 found |= matrixInstance.interface() == interface && !matrixInstance.isRegex() &&
160 matrixInstance.matchInstance(instance);
Yifan Honge7e45532018-03-16 18:11:49 -0700161 return !found; // continue if not match
162 });
163 return found;
164}
165
166bool 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 Hong85103c62018-03-20 15:43:34 -0700171 bool match = matrixInstance.interface() == interface && !matrixInstance.isRegex() &&
172 matrixInstance.matchInstance(instance);
Yifan Honge7e45532018-03-16 18:11:49 -0700173
174 found |= match;
175 foundOthers |= (!match);
176
177 return !foundOthers;
178 });
179
180 return found && !foundOthers;
181}
182
183bool 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
191void MatrixHal::clearInstances() {
192 this->interfaces.clear();
193}
194
Yifan Hong676447a2016-11-15 12:57:23 -0800195} // namespace vintf
196} // namespace android