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