• 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 
18 #ifndef ANDROID_VINTF_HAL_MANIFEST_H
19 #define ANDROID_VINTF_HAL_MANIFEST_H
20 
21 #include <utils/Errors.h>
22 #include <map>
23 #include <optional>
24 #include <string>
25 #include <vector>
26 
27 #include "FileSystem.h"
28 #include "HalGroup.h"
29 #include "KernelInfo.h"
30 #include "Level.h"
31 #include "ManifestHal.h"
32 #include "ManifestInstance.h"
33 #include "MapValueIterator.h"
34 #include "SchemaType.h"
35 #include "SystemSdk.h"
36 #include "VendorNdk.h"
37 #include "Version.h"
38 #include "Vndk.h"
39 #include "XmlFileGroup.h"
40 
41 namespace android {
42 namespace vintf {
43 
44 struct MatrixHal;
45 struct CompatibilityMatrix;
46 
47 namespace details {
48 using InstancesOfVersion =
49     std::map<std::string /* interface */, std::set<std::string /* instance */>>;
50 using Instances = std::map<Version, InstancesOfVersion>;
51 }  // namespace details
52 
53 // A HalManifest is reported by the hardware and query-able from
54 // framework code. This is the API for the framework.
55 struct HalManifest : public HalGroup<ManifestHal>, public XmlFileGroup<ManifestXmlFile> {
56    public:
57 
58     // Construct a device HAL manifest.
HalManifestHalManifest59     HalManifest() : mType(SchemaType::DEVICE) {}
60 
61     bool add(ManifestHal&& hal) override;
62 
63     // Given a component name (e.g. "android.hardware.camera"),
64     // return getHal(name)->transport if the component exist and v exactly matches
65     // one of the versions in that component, else EMPTY
66     Transport getTransport(const std::string &name, const Version &v,
67             const std::string &interfaceName, const std::string &instanceName) const;
68 
69     // Check compatibility against a compatibility matrix. Considered compatible if
70     // - framework manifest vs. device compat-mat
71     //     - checkIncompatibility for HALs returns only optional HALs
72     //     - one of manifest.vndk match compat-mat.vndk
73     // - device manifest vs. framework compat-mat
74     //     - checkIncompatibility for HALs returns only optional HALs
75     //     - manifest.sepolicy.version match one of compat-mat.sepolicy.sepolicy-version
76     bool checkCompatibility(const CompatibilityMatrix &mat, std::string *error = nullptr) const;
77 
78     // Generate a compatibility matrix such that checkCompatibility will return true.
79     CompatibilityMatrix generateCompatibleMatrix() const;
80 
81     // Returns all component names.
82     std::set<std::string> getHalNames() const;
83 
84     // Returns all component names and versions, e.g.
85     // "android.hardware.camera.device@1.0", "android.hardware.camera.device@3.2",
86     // "android.hardware.nfc@1.0"]
87     std::set<std::string> getHalNamesAndVersions() const;
88 
89     // Type of the manifest. FRAMEWORK or DEVICE.
90     SchemaType type() const;
91     void setType(SchemaType type);
92 
93     // FCM version that it implements.
94     Level level() const;
95 
96     // device.mSepolicyVersion. Assume type == device.
97     // Abort if type != device.
98     const Version &sepolicyVersion() const;
99 
100     // framework.mVendorNdks. Assume type == framework.
101     // Abort if type != framework.
102     const std::vector<VendorNdk>& vendorNdks() const;
103 
104     // If the corresponding <xmlfile> with the given version exists,
105     // - Return the overridden <path> if it is present,
106     // - otherwise the default value: /{system,vendor}/etc/<name>_V<major>_<minor>.xml
107     // Otherwise if the <xmlfile> entry does not exist, "" is returned.
108     std::string getXmlFilePath(const std::string& xmlFileName, const Version& version) const;
109 
110     // Get metaversion of this manifest.
111     Version getMetaVersion() const;
112 
113     bool forEachInstanceOfVersion(
114         const std::string& package, const Version& expectVersion,
115         const std::function<bool(const ManifestInstance&)>& func) const override;
116 
117     // Alternative to forEachInstance if you just need a set of instance names instead.
118     std::set<std::string> getInstances(const std::string& halName, const Version& version,
119                                        const std::string& interfaceName) const;
120 
121     // Return whether instance is in getInstances(...).
122     bool hasInstance(const std::string& halName, const Version& version,
123                      const std::string& interfaceName, const std::string& instance) const;
124 
125     // Insert the given instance. After inserting it, the instance will be available via
126     // forEachInstance* functions. This modifies the manifest.
127     // Return whether this operation is successful.
128     bool insertInstance(const FqInstance& fqInstance, Transport transport, Arch arch, HalFormat fmt,
129                         std::string* error = nullptr);
130 
131     // Get the <kernel> tag. Assumes type() == DEVICE.
132     const std::optional<KernelInfo>& kernel() const;
133 
134     // Add everything from another manifest. If no errors (return true), it is guaranteed
135     // that other->empty() == true after execution.
136     [[nodiscard]] bool addAll(HalManifest* other, std::string* error = nullptr);
137 
138    protected:
139     // Check before add()
140     bool shouldAdd(const ManifestHal& toAdd) const override;
141     bool shouldAddXmlFile(const ManifestXmlFile& toAdd) const override;
142 
143    private:
144     friend struct HalManifestConverter;
145     friend class VintfObject;
146     friend class AssembleVintfImpl;
147     friend struct LibVintfTest;
148     friend std::string dump(const HalManifest &vm);
149     friend bool operator==(const HalManifest &lft, const HalManifest &rgt);
150 
151     status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
152                                  std::string* error = nullptr);
153 
154     details::Instances expandInstances(const std::string& name) const;
155     // Check if all instances in matrixHal is supported in this manifest.
156     bool isCompatible(const details::Instances& instances, const MatrixHal& matrixHal) const;
157 
158     // Return a list of error messages (for each <hal> name) that does NOT conform to
159     // the given compatibility matrix. It does not contain components that are optional.
160     // That is, return empty list iff
161     // (instance in matrix) => (instance in manifest).
162     std::vector<std::string> checkIncompatibleHals(const CompatibilityMatrix& mat) const;
163 
164     void removeHals(const std::string& name, size_t majorVer);
165 
166     // Returns a list of instance names that are in this manifest but
167     // are not specified in the given matrix, whether the HAL is specified as an optional or
168     // required HAL.
169     // That is, return empty list iff
170     // (instance in manifest) => (instance in matrix).
171     std::set<std::string> checkUnusedHals(const CompatibilityMatrix& mat) const;
172 
173     // Check that manifest has no entries.
174     bool empty() const;
175 
176     SchemaType mType;
177     Level mLevel = Level::UNSPECIFIED;
178     // version attribute. Default is 1.0 for manifests created programatically.
179     Version mMetaVersion{1, 0};
180 
181     // entries for device hal manifest only
182     struct {
183         Version mSepolicyVersion;
184         std::optional<KernelInfo> mKernel;
185     } device;
186 
187     // entries for framework hal manifest only
188     struct {
189 #pragma clang diagnostic push
190 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
191         std::vector<Vndk> mVndks;
192 #pragma clang diagnostic pop
193 
194         std::vector<VendorNdk> mVendorNdks;
195         SystemSdk mSystemSdk;
196     } framework;
197 };
198 
199 
200 } // namespace vintf
201 } // namespace android
202 
203 #endif // ANDROID_VINTF_HAL_MANIFEST_H
204