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 "ManifestHal.h"
18
19 #include <unordered_set>
20
21 #include "MapValueIterator.h"
22 #include "constants-private.h"
23 #include "parse_string.h"
24 #include "utils.h"
25
26 namespace android {
27 namespace vintf {
28
isValid(std::string * error) const29 bool ManifestHal::isValid(std::string* error) const {
30 if (error) {
31 error->clear();
32 }
33
34 bool success = true;
35 std::map<size_t, Version> existing;
36 for (const auto &v : versions) {
37 auto&& [it, inserted] = existing.emplace(v.majorVer, v);
38 if (inserted) {
39 continue;
40 }
41 success = false;
42 if (error) {
43 *error += "Duplicated major version: " + to_string(v) + " vs. " +
44 to_string(it->second) + ".\n";
45 }
46 }
47
48 std::string transportArchError;
49 if (!transportArch.isValid(&transportArchError)) {
50 success = false;
51 if (error) *error += transportArchError + "\n";
52 }
53 return success;
54 }
55
operator ==(const ManifestHal & other) const56 bool ManifestHal::operator==(const ManifestHal &other) const {
57 // ignore fileName().
58 if (format != other.format)
59 return false;
60 if (name != other.name)
61 return false;
62 if (versions != other.versions)
63 return false;
64 if (!(transportArch == other.transportArch)) return false;
65 if (isOverride() != other.isOverride()) return false;
66 if (updatableViaApex() != other.updatableViaApex()) return false;
67 if (mManifestInstances != other.mManifestInstances) return false;
68 return true;
69 }
70
forEachInstance(const std::function<bool (const ManifestInstance &)> & func) const71 bool ManifestHal::forEachInstance(const std::function<bool(const ManifestInstance&)>& func) const {
72 for (const auto& manifestInstance : mManifestInstances) {
73 // For AIDL HALs, <version> tag is mangled with <fqname>. Note that if there's no
74 // <version> tag, libvintf will create one by default, so each <fqname> is executed
75 // at least once.
76 if (format == HalFormat::AIDL) {
77 for (const auto& v : versions) {
78 if (!func(manifestInstance.withVersion(v))) {
79 return false;
80 }
81 }
82 } else {
83 if (!func(manifestInstance)) {
84 return false;
85 }
86 }
87 }
88
89 return true;
90 }
91
isDisabledHal() const92 bool ManifestHal::isDisabledHal() const {
93 if (!isOverride()) return false;
94 bool hasInstance = false;
95 forEachInstance([&hasInstance](const auto&) {
96 hasInstance = true;
97 return false; // has at least one instance, stop here.
98 });
99 return !hasInstance;
100 }
101
appendAllVersions(std::set<Version> * ret) const102 void ManifestHal::appendAllVersions(std::set<Version>* ret) const {
103 ret->insert(versions.begin(), versions.end());
104 forEachInstance([&](const auto& e) {
105 ret->insert(e.version());
106 return true;
107 });
108 }
109
verifyInstance(const FqInstance & fqInstance,std::string * error) const110 bool ManifestHal::verifyInstance(const FqInstance& fqInstance, std::string* error) const {
111 if (fqInstance.hasPackage() && fqInstance.getPackage() != this->getName()) {
112 if (error) {
113 *error = "Should not add \"" + fqInstance.string() + "\" to a HAL with name " +
114 this->getName();
115 }
116 return false;
117 }
118 if (!fqInstance.hasVersion()) {
119 if (error) *error = "Should specify version: \"" + fqInstance.string() + "\"";
120 return false;
121 }
122 if (!fqInstance.hasInterface() && format != HalFormat::NATIVE) {
123 if (error) *error = "Should specify interface: \"" + fqInstance.string() + "\"";
124 return false;
125 }
126 if (!fqInstance.hasInstance()) {
127 if (error) *error = "Should specify instance: \"" + fqInstance.string() + "\"";
128 return false;
129 }
130 return true;
131 }
132
insertInstances(const std::set<FqInstance> & fqInstances,bool allowDupMajorVersion,std::string * error)133 bool ManifestHal::insertInstances(const std::set<FqInstance>& fqInstances,
134 bool allowDupMajorVersion, std::string* error) {
135 for (const FqInstance& e : fqInstances) {
136 if (!insertInstance(e, allowDupMajorVersion, error)) {
137 return false;
138 }
139 }
140 return true;
141 }
142
insertInstance(const FqInstance & e,bool allowDupMajorVersion,std::string * error)143 bool ManifestHal::insertInstance(const FqInstance& e, bool allowDupMajorVersion,
144 std::string* error) {
145 if (!verifyInstance(e, error)) {
146 return false;
147 }
148
149 size_t minorVer = e.getMinorVersion();
150 for (auto it = mManifestInstances.begin(); it != mManifestInstances.end();) {
151 if (it->version().majorVer == e.getMajorVersion() && it->interface() == e.getInterface() &&
152 it->instance() == e.getInstance()) {
153 if (!allowDupMajorVersion) {
154 if (format == HalFormat::AIDL) {
155 *error = "Duplicated HAL version: " + to_string(it->version().minorVer) +
156 " vs " + to_string(e.getMinorVersion());
157 } else {
158 *error = "Duplicated major version: " + to_string(it->version()) + " vs " +
159 to_string(Version(e.getMajorVersion(), e.getMinorVersion()));
160 }
161 return false;
162 }
163 minorVer = std::max(minorVer, it->version().minorVer);
164 it = mManifestInstances.erase(it);
165 } else {
166 ++it;
167 }
168 }
169
170 FqInstance toAdd;
171 if (!toAdd.setTo(this->getName(), e.getMajorVersion(), minorVer, e.getInterface(),
172 e.getInstance())) {
173 if (error) {
174 *error = "Cannot create FqInstance with package='" + this->getName() + "', version='" +
175 to_string(Version(e.getMajorVersion(), minorVer)) + "', interface='" +
176 e.getInterface() + "', instance='" + e.getInstance() + "'";
177 }
178 return false;
179 }
180
181 mManifestInstances.emplace(std::move(toAdd), this->transportArch, this->format,
182 this->updatableViaApex());
183 return true;
184 }
185
186 } // namespace vintf
187 } // namespace android
188