xref: /aosp_15_r20/system/libvintf/ManifestHal.cpp (revision 70a7ec852fcefd15a4fb57f8f183a8b1c3aacb08)
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     if (accessor().has_value() && accessor().value().empty()) {
54         success = false;
55         if (error) *error += "Accessor requires a non-empty value.\n";
56     }
57     return success;
58 }
59 
operator ==(const ManifestHal & other) const60 bool ManifestHal::operator==(const ManifestHal &other) const {
61     // ignore fileName().
62     if (format != other.format)
63         return false;
64     if (name != other.name)
65         return false;
66     if (versions != other.versions)
67         return false;
68     if (!(transportArch == other.transportArch)) return false;
69     if (isOverride() != other.isOverride()) return false;
70     if (updatableViaApex() != other.updatableViaApex()) return false;
71     if (updatableViaSystem() != other.updatableViaSystem()) return false;
72     if (mManifestInstances != other.mManifestInstances) return false;
73     if (getExclusiveTo() != other.getExclusiveTo()) return false;
74     return accessor() == other.accessor();
75 }
76 
forEachInstance(const std::function<bool (const ManifestInstance &)> & func) const77 bool ManifestHal::forEachInstance(const std::function<bool(const ManifestInstance&)>& func) const {
78     for (const auto& manifestInstance : mManifestInstances) {
79         // For AIDL HALs, <version> tag is mangled with <fqname>. Note that if there's no
80         // <version> tag, libvintf will create one by default, so each <fqname> is executed
81         // at least once.
82         if (format == HalFormat::AIDL) {
83             for (const auto& v : versions) {
84                 if (!func(manifestInstance.withVersion(v))) {
85                     return false;
86                 }
87             }
88         } else {
89             if (!func(manifestInstance)) {
90                 return false;
91             }
92         }
93     }
94 
95     return true;
96 }
97 
isDisabledHal() const98 bool ManifestHal::isDisabledHal() const {
99     if (!isOverride()) return false;
100     bool hasInstance = false;
101     forEachInstance([&hasInstance](const auto&) {
102         hasInstance = true;
103         return false;  // has at least one instance, stop here.
104     });
105     return !hasInstance;
106 }
107 
appendAllVersions(std::set<Version> * ret) const108 void ManifestHal::appendAllVersions(std::set<Version>* ret) const {
109     ret->insert(versions.begin(), versions.end());
110     forEachInstance([&](const auto& e) {
111         ret->insert(e.version());
112         return true;
113     });
114 }
115 
verifyInstance(const FqInstance & fqInstance,std::string * error) const116 bool ManifestHal::verifyInstance(const FqInstance& fqInstance, std::string* error) const {
117     if (fqInstance.hasPackage() && fqInstance.getPackage() != this->getName()) {
118         if (error) {
119             *error = "Should not add \"" + fqInstance.string() + "\" to a HAL with name " +
120                      this->getName();
121         }
122         return false;
123     }
124     if (!fqInstance.hasVersion()) {
125         if (error) *error = "Should specify version: \"" + fqInstance.string() + "\"";
126         return false;
127     }
128     if (!fqInstance.hasInterface() && format != HalFormat::NATIVE) {
129         if (error) *error = "Should specify interface: \"" + fqInstance.string() + "\"";
130         return false;
131     }
132     if (!fqInstance.hasInstance()) {
133         if (error) *error = "Should specify instance: \"" + fqInstance.string() + "\"";
134         return false;
135     }
136     return true;
137 }
138 
insertInstances(const std::set<FqInstance> & fqInstances,bool allowDupMajorVersion,std::string * error)139 bool ManifestHal::insertInstances(const std::set<FqInstance>& fqInstances,
140                                   bool allowDupMajorVersion, std::string* error) {
141     for (const FqInstance& e : fqInstances) {
142         if (!insertInstance(e, allowDupMajorVersion, error)) {
143             return false;
144         }
145     }
146     return true;
147 }
148 
insertInstance(const FqInstance & e,bool allowDupMajorVersion,std::string * error)149 bool ManifestHal::insertInstance(const FqInstance& e, bool allowDupMajorVersion,
150                                  std::string* error) {
151     if (!verifyInstance(e, error)) {
152         return false;
153     }
154 
155     size_t minorVer = e.getMinorVersion();
156     for (auto it = mManifestInstances.begin(); it != mManifestInstances.end();) {
157         if (it->version().majorVer == e.getMajorVersion() && it->interface() == e.getInterface() &&
158             it->instance() == e.getInstance()) {
159             if (!allowDupMajorVersion) {
160                 if (format == HalFormat::AIDL) {
161                     *error = "Duplicated HAL version: " + to_string(it->version().minorVer) +
162                              " vs " + to_string(e.getMinorVersion());
163                 } else {
164                     *error = "Duplicated major version: " + to_string(it->version()) + " vs " +
165                              to_string(Version(e.getMajorVersion(), e.getMinorVersion()));
166                 }
167                 return false;
168             }
169             minorVer = std::max(minorVer, it->version().minorVer);
170             it = mManifestInstances.erase(it);
171         } else {
172             ++it;
173         }
174     }
175 
176     FqInstance toAdd;
177     if (!toAdd.setTo(this->getName(), e.getMajorVersion(), minorVer, e.getInterface(),
178                      e.getInstance())) {
179         if (error) {
180             *error = "Cannot create FqInstance with package='" + this->getName() + "', version='" +
181                      to_string(Version(e.getMajorVersion(), minorVer)) + "', interface='" +
182                      e.getInterface() + "', instance='" + e.getInstance() + "'";
183         }
184         return false;
185     }
186 
187     mManifestInstances.emplace(std::move(toAdd), this->transportArch, this->format,
188                                this->updatableViaApex(), this->getExclusiveTo(), this->accessor(),
189                                this->updatableViaSystem());
190     return true;
191 }
192 
193 } // namespace vintf
194 } // namespace android
195