blob: 97885453e46877c4079480c121456dc4e0920b60 [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 Hong2a90ffe2018-03-05 17:45:34 -080019#include "MapValueIterator.h"
20
Yifan Hong676447a2016-11-15 12:57:23 -080021namespace android {
22namespace vintf {
23
24bool MatrixHal::operator==(const MatrixHal &other) const {
25 if (format != other.format)
26 return false;
27 if (name != other.name)
28 return false;
29 if (versionRanges != other.versionRanges)
30 return false;
Yifan Hong43e2aae2017-05-17 18:36:08 -070031 if (interfaces != other.interfaces)
32 return false;
Yifan Hong676447a2016-11-15 12:57:23 -080033 // do not compare optional
34 return true;
35}
36
Zhuoyao Zhangc7e75bd2017-10-29 17:20:19 -070037bool MatrixHal::containsVersion(const Version& version) const {
38 for (VersionRange vRange : versionRanges) {
39 if (vRange.contains(version)) return true;
40 }
41 return false;
42}
43
44std::set<std::string> MatrixHal::getInstances(const std::string& interfaceName) const {
45 std::set<std::string> ret;
46 auto it = interfaces.find(interfaceName);
47 if (it != interfaces.end()) {
48 ret.insert(it->second.instances.begin(), it->second.instances.end());
49 }
50 return ret;
51}
52
Yifan Hongdbe9db32017-12-11 19:06:11 -080053bool MatrixHal::containsInstances(const MatrixHal& other) const {
54 for (const auto& pair : other.interfaces) {
55 const std::string& interfaceName = pair.first;
56 auto thisIt = interfaces.find(interfaceName);
57 if (thisIt == interfaces.end()) {
58 return false;
59 }
60
61 const std::set<std::string>& thisInstances = thisIt->second.instances;
62 const std::set<std::string>& otherInstances = pair.second.instances;
63 if (!std::includes(thisInstances.begin(), thisInstances.end(), otherInstances.begin(),
64 otherInstances.end())) {
65 return false;
66 }
67 }
68 return true;
69}
70
Yifan Hong2a90ffe2018-03-05 17:45:34 -080071bool MatrixHal::forEachInstance(const std::function<bool(const MatrixInstance&)>& func) const {
72 for (const auto& vr : versionRanges) {
Yifan Hong075b3632018-03-14 16:03:11 -070073 if (!forEachInstance(vr, func)) {
74 return false;
75 }
76 }
77 return true;
78}
79
80bool MatrixHal::forEachInstance(const VersionRange& vr,
81 const std::function<bool(const MatrixInstance&)>& func) const {
82 for (const auto& intf : iterateValues(interfaces)) {
83 for (const auto& instance : intf.instances) {
84 // TODO(b/73556059): Store MatrixInstance as well to avoid creating temps
85 FqInstance fqInstance;
86 if (fqInstance.setTo(getName(), vr.majorVer, vr.minMinor, intf.name, instance)) {
87 if (!func(MatrixInstance(std::move(fqInstance), VersionRange(vr), optional))) {
88 return false;
Yifan Hong2a90ffe2018-03-05 17:45:34 -080089 }
90 }
91 }
92 }
93 return true;
94}
95
Yifan Hong075b3632018-03-14 16:03:11 -070096bool MatrixHal::isCompatible(const std::set<FqInstance>& providedInstances,
97 const std::set<Version>& providedVersions) const {
98 // <version>'s are related by OR.
99 return std::any_of(versionRanges.begin(), versionRanges.end(), [&](const VersionRange& vr) {
100 return isCompatible(vr, providedInstances, providedVersions);
101 });
102}
103
104bool MatrixHal::isCompatible(const VersionRange& vr, const std::set<FqInstance>& providedInstances,
105 const std::set<Version>& providedVersions) const {
106 bool hasAnyInstance = false;
107 bool versionUnsatisfied = false;
108
109 // Look at each interface/instance, and ensure that they are in providedInstances.
110 forEachInstance(vr, [&](const MatrixInstance& matrixInstance) {
111 hasAnyInstance = true;
112
113 versionUnsatisfied |=
114 !std::any_of(providedInstances.begin(), providedInstances.end(),
115 [&](const FqInstance& providedInstance) {
116 return matrixInstance.isSatisfiedBy(providedInstance);
117 });
118
119 return !versionUnsatisfied; // if any interface/instance is unsatisfied, break
120 });
121
122 if (hasAnyInstance) {
123 return !versionUnsatisfied;
124 }
125
126 // In some cases (e.g. tests and native HALs), compatibility matrix doesn't specify
127 // any instances. Check versions only.
128 return std::any_of(
129 providedVersions.begin(), providedVersions.end(),
130 [&](const auto& providedVersion) { return vr.supportedBy(providedVersion); });
131}
132
Yifan Hong676447a2016-11-15 12:57:23 -0800133} // namespace vintf
134} // namespace android