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