blob: 62e0cd8d821b4ffb0633cb4ab965ba62f74b60e6 [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
46std::set<std::string> MatrixHal::getInstances(const std::string& interfaceName) const {
47 std::set<std::string> ret;
48 auto it = interfaces.find(interfaceName);
49 if (it != interfaces.end()) {
50 ret.insert(it->second.instances.begin(), it->second.instances.end());
51 }
52 return ret;
53}
54
Yifan Hong2a90ffe2018-03-05 17:45:34 -080055bool MatrixHal::forEachInstance(const std::function<bool(const MatrixInstance&)>& func) const {
56 for (const auto& vr : versionRanges) {
Yifan Hong075b3632018-03-14 16:03:11 -070057 if (!forEachInstance(vr, func)) {
58 return false;
59 }
60 }
61 return true;
62}
63
64bool MatrixHal::forEachInstance(const VersionRange& vr,
65 const std::function<bool(const MatrixInstance&)>& func) const {
66 for (const auto& intf : iterateValues(interfaces)) {
67 for (const auto& instance : intf.instances) {
68 // TODO(b/73556059): Store MatrixInstance as well to avoid creating temps
69 FqInstance fqInstance;
70 if (fqInstance.setTo(getName(), vr.majorVer, vr.minMinor, intf.name, instance)) {
71 if (!func(MatrixInstance(std::move(fqInstance), VersionRange(vr), optional))) {
72 return false;
Yifan Hong2a90ffe2018-03-05 17:45:34 -080073 }
74 }
75 }
76 }
77 return true;
78}
79
Yifan Honge7e45532018-03-16 18:11:49 -070080bool MatrixHal::forEachInstance(
81 const std::function<bool(const std::vector<VersionRange>&, const std::string&,
82 const std::string&)>& func) const {
83 for (const auto& intf : iterateValues(interfaces)) {
84 for (const auto& instance : intf.instances) {
85 if (!func(versionRanges, intf.name, instance)) {
86 return false;
87 }
88 }
89 }
90 return true;
91}
92
Yifan Hong075b3632018-03-14 16:03:11 -070093bool MatrixHal::isCompatible(const std::set<FqInstance>& providedInstances,
94 const std::set<Version>& providedVersions) const {
95 // <version>'s are related by OR.
96 return std::any_of(versionRanges.begin(), versionRanges.end(), [&](const VersionRange& vr) {
97 return isCompatible(vr, providedInstances, providedVersions);
98 });
99}
100
101bool MatrixHal::isCompatible(const VersionRange& vr, const std::set<FqInstance>& providedInstances,
102 const std::set<Version>& providedVersions) const {
103 bool hasAnyInstance = false;
104 bool versionUnsatisfied = false;
105
106 // Look at each interface/instance, and ensure that they are in providedInstances.
107 forEachInstance(vr, [&](const MatrixInstance& matrixInstance) {
108 hasAnyInstance = true;
109
110 versionUnsatisfied |=
111 !std::any_of(providedInstances.begin(), providedInstances.end(),
112 [&](const FqInstance& providedInstance) {
113 return matrixInstance.isSatisfiedBy(providedInstance);
114 });
115
116 return !versionUnsatisfied; // if any interface/instance is unsatisfied, break
117 });
118
119 if (hasAnyInstance) {
120 return !versionUnsatisfied;
121 }
122
123 // In some cases (e.g. tests and native HALs), compatibility matrix doesn't specify
124 // any instances. Check versions only.
125 return std::any_of(
126 providedVersions.begin(), providedVersions.end(),
127 [&](const auto& providedVersion) { return vr.supportedBy(providedVersion); });
128}
129
Yifan Hong7967d7b2018-03-15 17:08:58 -0700130void MatrixHal::setOptional(bool o) {
131 this->optional = o;
132}
133
Yifan Honge7e45532018-03-16 18:11:49 -0700134void MatrixHal::insertVersionRanges(const std::vector<VersionRange>& other) {
135 for (const VersionRange& otherVr : other) {
Yifan Hong7967d7b2018-03-15 17:08:58 -0700136 auto existingVr = std::find_if(this->versionRanges.begin(), this->versionRanges.end(),
137 [&](const auto& e) { return e.overlaps(otherVr); });
138
139 if (existingVr == this->versionRanges.end()) {
140 this->versionRanges.push_back(otherVr);
141 } else {
Yifan Hong5da19c92018-03-15 17:11:43 -0700142 existingVr->minMinor = std::min(existingVr->minMinor, otherVr.minMinor);
Yifan Hong7967d7b2018-03-15 17:08:58 -0700143 existingVr->maxMinor = std::max(existingVr->maxMinor, otherVr.maxMinor);
144 }
145 }
146}
147
Yifan Honge7e45532018-03-16 18:11:49 -0700148void MatrixHal::insertInstance(const std::string& interface, const std::string& instance) {
149 auto it = interfaces.find(interface);
150 if (it == interfaces.end())
151 it = interfaces.emplace(interface, HalInterface{interface, {}}).first;
152 it->second.instances.insert(instance);
153}
154
155bool MatrixHal::hasAnyInstance() const {
156 bool found = false;
157 forEachInstance([&](const auto&) {
158 found = true;
159 return false; // break if any instance
160 });
161 return found;
162}
163
164bool MatrixHal::hasInstance(const std::string& interface, const std::string& instance) const {
165 bool found = false;
166 forEachInstance([&](const auto& matrixInstance) {
167 found |= matrixInstance.interface() == interface && matrixInstance.instance() == instance;
168 return !found; // continue if not match
169 });
170 return found;
171}
172
173bool MatrixHal::hasOnlyInstance(const std::string& interface, const std::string& instance) const {
174 bool found = false;
175 bool foundOthers = false;
176
177 forEachInstance([&](const auto& matrixInstance) {
178 bool match =
179 matrixInstance.interface() == interface && matrixInstance.instance() == instance;
180
181 found |= match;
182 foundOthers |= (!match);
183
184 return !foundOthers;
185 });
186
187 return found && !foundOthers;
188}
189
190bool MatrixHal::removeInstance(const std::string& interface, const std::string& instance) {
191 auto it = interfaces.find(interface);
192 if (it == interfaces.end()) return false;
193 it->second.instances.erase(instance);
194 if (it->second.instances.empty()) interfaces.erase(it);
195 return true;
196}
197
198void MatrixHal::clearInstances() {
199 this->interfaces.clear();
200}
201
Yifan Hong676447a2016-11-15 12:57:23 -0800202} // namespace vintf
203} // namespace android