xref: /aosp_15_r20/system/libvintf/include/vintf/CompatibilityMatrix.h (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 #ifndef ANDROID_VINTF_COMPATIBILITY_MATRIX_H
18 #define ANDROID_VINTF_COMPATIBILITY_MATRIX_H
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include <utils/Errors.h>
25 
26 #include "FileSystem.h"
27 #include "HalGroup.h"
28 #include "Level.h"
29 #include "MapValueIterator.h"
30 #include "MatrixHal.h"
31 #include "MatrixInstance.h"
32 #include "MatrixKernel.h"
33 #include "SchemaType.h"
34 #include "Sepolicy.h"
35 #include "SystemSdk.h"
36 #include "VendorNdk.h"
37 #include "Vndk.h"
38 #include "WithFileName.h"
39 #include "XmlFileGroup.h"
40 
41 namespace android {
42 namespace vintf {
43 
44 namespace details {
45 class CheckVintfUtils;
46 }  // namespace details
47 
48 // Compatibility matrix defines what hardware does the framework requires.
49 struct CompatibilityMatrix : public HalGroup<MatrixHal>,
50                              public XmlFileGroup<MatrixXmlFile>,
51                              public WithFileName {
52     // Create a framework compatibility matrix.
CompatibilityMatrixCompatibilityMatrix53     CompatibilityMatrix() : mType(SchemaType::FRAMEWORK) {}
54 
55     SchemaType type() const;
56     Level level() const;
57 
58     // If the corresponding <xmlfile> with the given version exists, for the first match,
59     // - Return the overridden <path> if it is present,
60     // - otherwise the default value: /{system,vendor}/etc/<name>_V<major>_<minor-max>.<format>
61     // Otherwise if the <xmlfile> entry does not exist, "" is returned.
62     // For example, if the matrix says ["[email protected]" -> "foo.xml", "[email protected]" -> bar.xml]
63     // getXmlSchemaPath("audio", 1.0) -> foo.xml
64     // getXmlSchemaPath("audio", 1.5) -> foo.xml
65     // getXmlSchemaPath("audio", 1.7) -> bar.xml
66     // (Normally, version ranges do not overlap, and the only match is returned.)
67     std::string getXmlSchemaPath(const std::string& xmlFileName, const Version& version) const;
68 
69     std::string getVendorNdkVersion() const;
70 
71     std::vector<SepolicyVersionRange> getSepolicyVersions() const;
72 
73     bool add(MatrixHal&&, std::string* error = nullptr);
74     // Move all hals from another CompatibilityMatrix to this.
75     bool addAllHals(CompatibilityMatrix* other, std::string* error = nullptr);
76 
77    protected:
78     bool forEachInstanceOfVersion(
79         HalFormat format, ExclusiveTo exclusiveTo, const std::string& package,
80         const Version& expectVersion,
81         const std::function<bool(const MatrixInstance&)>& func) const override;
82 
83    private:
84     // Add everything in inputMatrix to "this" as requirements.
85     bool addAll(CompatibilityMatrix* inputMatrix, std::string* error);
86 
87     // Add all <kernel> from other to "this". Error if there is a conflict.
88     bool addAllKernels(CompatibilityMatrix* other, std::string* error);
89 
90     // Add a <kernel> tag to "this". Error if there is a conflict.
91     bool addKernel(MatrixKernel&& kernel, std::string* error);
92 
93     // Merge <sepolicy> with other's <sepolicy>. Error if there is a conflict.
94     bool addSepolicy(CompatibilityMatrix* other, std::string* error);
95 
96     // Merge <avb><vbmeta-version> with other's <avb><vbmeta-version>. Error if there is a conflict.
97     bool addAvbMetaVersion(CompatibilityMatrix* other, std::string* error);
98 
99     // Merge <vndk> with other's <vndk>. Error if there is a conflict.
100     bool addVndk(CompatibilityMatrix* other, std::string* error);
101 
102     // Merge <vendor-ndk> with other's <vendor-ndk>. Error if there is a conflict.
103     bool addVendorNdk(CompatibilityMatrix* other, std::string* error);
104 
105     // Merge <system-sdk> with other's <system-sdk>.
106     bool addSystemSdk(CompatibilityMatrix* other, std::string* error);
107 
108     // Add everything in inputMatrix to "this" as optional.
109     bool addAllAsOptional(CompatibilityMatrix* inputMatrix, std::string* error);
110 
111     // Add all HALs as optional HALs from "other". This function moves MatrixHal objects
112     // from "other".
113     // Require other->level() > this->level(), otherwise do nothing.
114     bool addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error);
115 
116     // Similar to addAllHalsAsOptional but on <xmlfile> entries.
117     bool addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error);
118 
119     // Combine a set of framework compatibility matrices. For each CompatibilityMatrix in matrices
120     // (in the order of level(), where UNSPECIFIED (empty) is treated as deviceLevel)
121     // - If level() < deviceLevel:
122     //    - If kernelLevel is UNSPECIFIED, ignore
123     //    - If kernelLevel is not UNSPECIFIED and level() < kernelLevel, ignore
124     //    - If kernelLevel is not UNSPECIFIED and level() >= kernelLevel, add kernel()
125     // - If level() == UNSPECIFIED or level() == deviceLevel,
126     //   - Add as hard requirements. See combineSameFcmVersion
127     // - If level() > deviceLevel,
128     //   - all <hal> versions and <xmlfile>s are added as optional.
129     //   - <kernel minlts="x.y.z"> is added only if x.y does not exist in a file
130     //     with lower level()
131     //   - <sepolicy>, <avb><vbmeta-version> is ignored
132     // Return the combined matrix, nullptr if any error (e.g. conflict of information).
133     static std::unique_ptr<CompatibilityMatrix> combine(Level deviceLevel, Level kernelLevel,
134                                                         std::vector<CompatibilityMatrix>* matrices,
135                                                         std::string* error);
136 
137     // Combine a set of device compatibility matrices.
138     static std::unique_ptr<CompatibilityMatrix> combineDeviceMatrices(
139         std::vector<CompatibilityMatrix>* matrices, std::string* error);
140 
141     status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
142                                  std::string* error = nullptr);
143 
144     MatrixHal* splitInstance(MatrixHal* existingHal, const std::string& interface,
145                              const std::string& instance, bool isRegex);
146 
147     // Return whether instance is in "this"; that is, instance is in any <instance> tag or
148     // matches any <regex-instance> tag.
149     bool matchInstance(HalFormat format, ExclusiveTo exclusiveTo, const std::string& halName,
150                        const Version& version, const std::string& interfaceName,
151                        const std::string& instance) const;
152 
153     // Return the minlts of the latest <kernel>, or empty value if any error (e.g. this is not an
154     // FCM, or there are no <kernel> tags).
155     [[nodiscard]] KernelVersion getLatestKernelMinLts() const;
156 
157     friend struct HalManifest;
158     friend struct RuntimeInfo;
159     friend struct CompatibilityMatrixConverter;
160     friend struct LibVintfTest;
161     friend struct FrameworkCompatibilityMatrixCombineTest;
162     friend struct DeviceCompatibilityMatrixCombineTest;
163     friend class VintfObject;
164     friend class AssembleVintfImpl;
165     friend class KernelInfo;
166     friend class details::CheckVintfUtils;
167     friend bool operator==(const CompatibilityMatrix &, const CompatibilityMatrix &);
168 
169     SchemaType mType;
170     Level mLevel = Level::UNSPECIFIED;
171 
172     // entries only for framework compatibility matrix.
173     struct {
174         std::vector<MatrixKernel> mKernels;
175         Sepolicy mSepolicy;
176         Version mAvbMetaVersion;
177     } framework;
178 
179     // entries only for device compatibility matrix.
180     struct {
181 #pragma clang diagnostic push
182 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
183         Vndk mVndk;
184 #pragma clang diagnostic pop
185 
186         VendorNdk mVendorNdk;
187         SystemSdk mSystemSdk;
188     } device;
189 };
190 
191 } // namespace vintf
192 } // namespace android
193 
194 #endif // ANDROID_VINTF_COMPATIBILITY_MATRIX_H
195