• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
CompatibilityMatrixCompatibilityMatrix47     CompatibilityMatrix() : mType(SchemaType::FRAMEWORK) {}
48 
49     SchemaType type() const;
50     Level level() const;
51     Version getMinimumMetaVersion() const;
52 
53     // If the corresponding <xmlfile> with the given version exists, for the first match,
54     // - Return the overridden <path> if it is present,
55     // - otherwise the default value: /{system,vendor}/etc/<name>_V<major>_<minor-max>.<format>
56     // Otherwise if the <xmlfile> entry does not exist, "" is returned.
57     // For example, if the matrix says ["audio@1.0-5" -> "foo.xml", "audio@1.3-7" -> bar.xml]
58     // getXmlSchemaPath("audio", 1.0) -> foo.xml
59     // getXmlSchemaPath("audio", 1.5) -> foo.xml
60     // getXmlSchemaPath("audio", 1.7) -> bar.xml
61     // (Normally, version ranges do not overlap, and the only match is returned.)
62     std::string getXmlSchemaPath(const std::string& xmlFileName, const Version& version) const;
63 
64     bool forEachInstanceOfVersion(
65         const std::string& package, const Version& expectVersion,
66         const std::function<bool(const MatrixInstance&)>& func) const override;
67 
68     std::string getVendorNdkVersion() const;
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     // Similar to addAllHalsAsOptional but on <kernel> entries.
107     bool addAllKernelsAsOptional(CompatibilityMatrix* other, std::string* error);
108 
109     // Combine a set of framework compatibility matrices. For each CompatibilityMatrix in matrices
110     // (in the order of level(), where UNSPECIFIED (empty) is treated as deviceLevel)
111     // - If level() < deviceLevel, ignore
112     // - If level() == UNSPECIFIED or level() == deviceLevel,
113     //   - Add as hard requirements. See combineSameFcmVersion
114     // - If level() > deviceLevel,
115     //   - all <hal> versions and <xmlfile>s are added as optional.
116     //   - <kernel minlts="x.y.z"> is added only if x.y does not exist in a file
117     //     with lower level()
118     //   - <sepolicy>, <avb><vbmeta-version> is ignored
119     // Return the combined matrix, nullptr if any error (e.g. conflict of information).
120     static std::unique_ptr<CompatibilityMatrix> combine(
121         Level deviceLevel, std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error);
122 
123     // Combine a set of device compatibility matrices.
124     static std::unique_ptr<CompatibilityMatrix> combineDeviceMatrices(
125         std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error);
126 
127     status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
128                                  std::string* error = nullptr);
129 
130     MatrixHal* splitInstance(MatrixHal* existingHal, const std::string& interface,
131                              const std::string& instance, bool isRegex);
132 
133     // Return whether instance is in "this"; that is, instance is in any <instance> tag or
134     // matches any <regex-instance> tag.
135     bool matchInstance(const std::string& halName, const Version& version,
136                        const std::string& interfaceName, const std::string& instance) const;
137 
138     friend struct HalManifest;
139     friend struct RuntimeInfo;
140     friend struct CompatibilityMatrixConverter;
141     friend struct LibVintfTest;
142     friend struct FrameworkCompatibilityMatrixCombineTest;
143     friend struct DeviceCompatibilityMatrixCombineTest;
144     friend class VintfObject;
145     friend class AssembleVintfImpl;
146     friend bool operator==(const CompatibilityMatrix &, const CompatibilityMatrix &);
147 
148     SchemaType mType;
149     Level mLevel = Level::UNSPECIFIED;
150 
151     // entries only for framework compatibility matrix.
152     struct {
153         std::vector<MatrixKernel> mKernels;
154         Sepolicy mSepolicy;
155         Version mAvbMetaVersion;
156     } framework;
157 
158     // entries only for device compatibility matrix.
159     struct {
160 #pragma clang diagnostic push
161 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
162         Vndk mVndk;
163 #pragma clang diagnostic pop
164 
165         VendorNdk mVendorNdk;
166         SystemSdk mSystemSdk;
167     } device;
168 };
169 
170 } // namespace vintf
171 } // namespace android
172 
173 #endif // ANDROID_VINTF_COMPATIBILITY_MATRIX_H
174