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 <map>
22 #include <string>
23 #include <utils/Errors.h>
24 #include <vector>
25 
26 #include "ManifestHal.h"
27 #include "MapValueIterator.h"
28 #include "SchemaType.h"
29 #include "Version.h"
30 #include "Vndk.h"
31 
32 namespace android {
33 namespace vintf {
34 
35 struct MatrixHal;
36 struct CompatibilityMatrix;
37 
38 // A HalManifest is reported by the hardware and query-able from
39 // framework code. This is the API for the framework.
40 struct HalManifest {
41 public:
42 
43     // manifest.version
44     constexpr static Version kVersion{1, 0};
45 
46     // Construct a device HAL manifest.
HalManifestHalManifest47     HalManifest() : mType(SchemaType::DEVICE) {}
48 
49     // Given a component name (e.g. "android.hardware.camera"),
50     // return getHal(name)->transport if the component exist and v exactly matches
51     // one of the versions in that component, else EMPTY
52     Transport getTransport(const std::string &name, const Version &v,
53             const std::string &interfaceName, const std::string &instanceName) const;
54 
55     // Given a component name (e.g. "android.hardware.camera"),
56     // return a list of version numbers that are supported by the hardware.
57     // If the component is not found, empty list is returned.
58     // If multiple matches, return a concatenation of version entries
59     // (dupes removed)
60     std::set<Version> getSupportedVersions(const std::string &name) const;
61 
62     // Given a component name (e.g. "android.hardware.camera") and an interface
63     // name, return all instance names for that interface.
64     // * If the component ("android.hardware.camera") does not exist, return empty list
65     // * If the component ("android.hardware.camera") does exist,
66     //    * If the interface (ICamera) does not exist, return empty list
67     //    * Else return the list hal.interface.instance
68     std::set<std::string> getInstances(
69             const std::string &halName, const std::string &interfaceName) const;
70 
71     // Convenience method for checking if instanceName is in getInstances(halName, interfaceName)
72     bool hasInstance(const std::string &halName,
73             const std::string &interfaceName, const std::string &instanceName) const;
74 
75     // Return a list of component names that does NOT conform to
76     // the given compatibility matrix. It contains components that are optional
77     // for the framework if includeOptional = true.
78     // Note: only HAL entries are checked. To check other entries as well, use
79     // checkCompatibility.
80     std::vector<std::string> checkIncompatibility(const CompatibilityMatrix &mat,
81             bool includeOptional = true) const;
82 
83     // Check compatibility against a compatibility matrix. Considered compatible if
84     // - framework manifest vs. device compat-mat
85     //     - checkIncompatibility for HALs returns only optional HALs
86     //     - one of manifest.vndk match compat-mat.vndk
87     // - device manifest vs. framework compat-mat
88     //     - checkIncompatibility for HALs returns only optional HALs
89     //     - manifest.sepolicy.version match one of compat-mat.sepolicy.sepolicy-version
90     bool checkCompatibility(const CompatibilityMatrix &mat, std::string *error = nullptr) const;
91 
92     // Generate a compatibility matrix such that checkCompatibility will return true.
93     CompatibilityMatrix generateCompatibleMatrix() const;
94 
95     // Add an hal to this manifest so that a HalManifest can be constructed programatically.
96     bool add(ManifestHal &&hal);
97 
98     // Returns all component names.
99     std::set<std::string> getHalNames() const;
100 
101     // Returns all component names and versions, e.g.
102     // "android.hardware.camera.device@1.0", "android.hardware.camera.device@3.2",
103     // "android.hardware.nfc@1.0"]
104     std::set<std::string> getHalNamesAndVersions() const;
105 
106     // Given a component name (e.g. "android.hardware.camera"),
107     // return a list of interface names of that component.
108     // If the component is not found, empty list is returned.
109     std::set<std::string> getInterfaceNames(const std::string &name) const;
110 
111     // Type of the manifest. FRAMEWORK or DEVICE.
112     SchemaType type() const;
113 
114     // Get all hals with the name
115     std::vector<const ManifestHal *> getHals(const std::string &name) const;
116     std::vector<ManifestHal *> getHals(const std::string &name);
117 
118     // device.mSepolicyVersion. Assume type == device.
119     // Abort if type != device.
120     const Version &sepolicyVersion() const;
121 
122     // framework.mVndks. Assume type == framework.
123     // Abort if type != framework.
124     const std::vector<Vndk> &vndks() const;
125 
126 private:
127     friend struct HalManifestConverter;
128     friend class VintfObject;
129     friend class AssembleVintf;
130     friend struct LibVintfTest;
131     friend std::string dump(const HalManifest &vm);
132     friend bool operator==(const HalManifest &lft, const HalManifest &rgt);
133 
134     // Check before add()
135     bool shouldAdd(const ManifestHal& toAdd) const;
136 
137     // Return an iterable to all ManifestHal objects. Call it as follows:
138     // for (const ManifestHal &e : vm.getHals()) { }
139     ConstMultiMapValueIterable<std::string, ManifestHal> getHals() const;
140 
141     // Get any HAL component based on the component name. Return any one
142     // if multiple. Return nullptr if the component does not exist. This is only
143     // for creating HalManifest objects programatically.
144     // The component name looks like:
145     // android.hardware.foo
146     ManifestHal *getAnyHal(const std::string &name);
147 
148     status_t fetchAllInformation(const std::string &path);
149 
150     // Check if all instances in matrixHal is supported in this manifest.
151     bool isCompatible(const MatrixHal& matrixHal) const;
152 
153     SchemaType mType;
154 
155     // sorted map from component name to the component.
156     // The component name looks like: android.hardware.foo
157     std::multimap<std::string, ManifestHal> mHals;
158 
159     // entries for device hal manifest only
160     struct {
161         Version mSepolicyVersion;
162     } device;
163 
164     // entries for framework hal manifest only
165     struct {
166         std::vector<Vndk> mVndks;
167     } framework;
168 };
169 
170 
171 } // namespace vintf
172 } // namespace android
173 
174 #endif // ANDROID_VINTF_HAL_MANIFEST_H
175