blob: 44a500a56af44710c7cb665177762e73471e693c [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)) {
62 if (!func(MatrixInstance(std::move(fqInstance), VersionRange(vr), optional))) {
63 return false;
Yifan Hong2a90ffe2018-03-05 17:45:34 -080064 }
65 }
66 }
67 }
68 return true;
69}
70
Yifan Honge7e45532018-03-16 18:11:49 -070071bool MatrixHal::forEachInstance(
72 const std::function<bool(const std::vector<VersionRange>&, const std::string&,
73 const std::string&)>& func) const {
74 for (const auto& intf : iterateValues(interfaces)) {
75 for (const auto& instance : intf.instances) {
76 if (!func(versionRanges, intf.name, instance)) {
77 return false;
78 }
79 }
80 }
81 return true;
82}
83
Yifan Hong075b3632018-03-14 16:03:11 -070084bool MatrixHal::isCompatible(const std::set<FqInstance>& providedInstances,
85 const std::set<Version>& providedVersions) const {
86 // <version>'s are related by OR.
87 return std::any_of(versionRanges.begin(), versionRanges.end(), [&](const VersionRange& vr) {
88 return isCompatible(vr, providedInstances, providedVersions);
89 });
90}
91
92bool MatrixHal::isCompatible(const VersionRange& vr, const std::set<FqInstance>& providedInstances,
93 const std::set<Version>& providedVersions) const {
94 bool hasAnyInstance = false;
95 bool versionUnsatisfied = false;
96
97 // Look at each interface/instance, and ensure that they are in providedInstances.
98 forEachInstance(vr, [&](const MatrixInstance& matrixInstance) {
99 hasAnyInstance = true;
100
101 versionUnsatisfied |=
102 !std::any_of(providedInstances.begin(), providedInstances.end(),
103 [&](const FqInstance& providedInstance) {
104 return matrixInstance.isSatisfiedBy(providedInstance);
105 });
106
107 return !versionUnsatisfied; // if any interface/instance is unsatisfied, break
108 });
109
110 if (hasAnyInstance) {
111 return !versionUnsatisfied;
112 }
113
114 // In some cases (e.g. tests and native HALs), compatibility matrix doesn't specify
115 // any instances. Check versions only.
116 return std::any_of(
117 providedVersions.begin(), providedVersions.end(),
118 [&](const auto& providedVersion) { return vr.supportedBy(providedVersion); });
119}
120
Yifan Hong7967d7b2018-03-15 17:08:58 -0700121void MatrixHal::setOptional(bool o) {
122 this->optional = o;
123}
124
Yifan Honge7e45532018-03-16 18:11:49 -0700125void MatrixHal::insertVersionRanges(const std::vector<VersionRange>& other) {
126 for (const VersionRange& otherVr : other) {
Yifan Hong7967d7b2018-03-15 17:08:58 -0700127 auto existingVr = std::find_if(this->versionRanges.begin(), this->versionRanges.end(),
128 [&](const auto& e) { return e.overlaps(otherVr); });
129
130 if (existingVr == this->versionRanges.end()) {
131 this->versionRanges.push_back(otherVr);
132 } else {
Yifan Hong5da19c92018-03-15 17:11:43 -0700133 existingVr->minMinor = std::min(existingVr->minMinor, otherVr.minMinor);
Yifan Hong7967d7b2018-03-15 17:08:58 -0700134 existingVr->maxMinor = std::max(existingVr->maxMinor, otherVr.maxMinor);
135 }
136 }
137}
138
Yifan Honge7e45532018-03-16 18:11:49 -0700139void MatrixHal::insertInstance(const std::string& interface, const std::string& instance) {
140 auto it = interfaces.find(interface);
141 if (it == interfaces.end())
142 it = interfaces.emplace(interface, HalInterface{interface, {}}).first;
143 it->second.instances.insert(instance);
144}
145
146bool MatrixHal::hasAnyInstance() const {
147 bool found = false;
148 forEachInstance([&](const auto&) {
149 found = true;
150 return false; // break if any instance
151 });
152 return found;
153}
154
155bool MatrixHal::hasInstance(const std::string& interface, const std::string& instance) const {
156 bool found = false;
157 forEachInstance([&](const auto& matrixInstance) {
158 found |= matrixInstance.interface() == interface && matrixInstance.instance() == instance;
159 return !found; // continue if not match
160 });
161 return found;
162}
163
164bool MatrixHal::hasOnlyInstance(const std::string& interface, const std::string& instance) const {
165 bool found = false;
166 bool foundOthers = false;
167
168 forEachInstance([&](const auto& matrixInstance) {
169 bool match =
170 matrixInstance.interface() == interface && matrixInstance.instance() == instance;
171
172 found |= match;
173 foundOthers |= (!match);
174
175 return !foundOthers;
176 });
177
178 return found && !foundOthers;
179}
180
181bool MatrixHal::removeInstance(const std::string& interface, const std::string& instance) {
182 auto it = interfaces.find(interface);
183 if (it == interfaces.end()) return false;
184 it->second.instances.erase(instance);
185 if (it->second.instances.empty()) interfaces.erase(it);
186 return true;
187}
188
189void MatrixHal::clearInstances() {
190 this->interfaces.clear();
191}
192
Yifan Hong676447a2016-11-15 12:57:23 -0800193} // namespace vintf
194} // namespace android