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_MANIFEST_HAL_H
19 #define ANDROID_VINTF_MANIFEST_HAL_H
20 
21 #include <map>
22 #include <optional>
23 #include <set>
24 #include <string>
25 #include <vector>
26 
27 #include <vintf/FqInstance.h>
28 #include <vintf/HalFormat.h>
29 #include <vintf/HalInterface.h>
30 #include <vintf/Level.h>
31 #include <vintf/ManifestInstance.h>
32 #include <vintf/TransportArch.h>
33 #include <vintf/Version.h>
34 #include <vintf/WithFileName.h>
35 
36 namespace android {
37 namespace vintf {
38 
39 // A component of HalManifest.
40 struct ManifestHal : public WithFileName {
41     using InstanceType = ManifestInstance;
42 
43     ManifestHal() = default;
44 
45     bool operator==(const ManifestHal &other) const;
46 
47     HalFormat format = HalFormat::HIDL;
48     std::string name;
49     std::vector<Version> versions;
50     TransportArch transportArch;
51 
transportManifestHal52     inline Transport transport() const {
53         return transportArch.transport;
54     }
55 
archManifestHal56     inline Arch arch() const { return transportArch.arch; }
ipManifestHal57     inline std::optional<std::string> ip() const { return transportArch.ip; }
portManifestHal58     inline std::optional<uint64_t> port() const { return transportArch.port; }
59 
getNameManifestHal60     inline const std::string& getName() const { return name; }
61 
62     // Assume isValid().
63     bool forEachInstance(const std::function<bool(const ManifestInstance&)>& func) const;
64 
isOverrideManifestHal65     bool isOverride() const { return mIsOverride; }
updatableViaApexManifestHal66     const std::optional<std::string>& updatableViaApex() const { return mUpdatableViaApex; }
67 
68     // When true, the existence of this <hal> tag means the component does NOT
69     // exist on the device. This is useful for ODM manifests to specify that
70     // a HAL is disabled on certain products.
71     bool isDisabledHal() const;
72 
getMaxLevelManifestHal73     Level getMaxLevel() const { return mMaxLevel; }
getMinLevelManifestHal74     Level getMinLevel() const { return mMinLevel; }
75 
76    private:
77     friend struct LibVintfTest;
78     friend struct ManifestHalConverter;
79     friend struct HalManifest;
80     friend bool parse(const std::string &s, ManifestHal *hal);
81 
82     // Whether this hal is a valid one. Note that an empty ManifestHal
83     // (constructed via ManifestHal()) is valid.
84     bool isValid(std::string* error = nullptr) const;
85 
86     // Return all versions mentioned by <version>s and <fqname>s.
87     void appendAllVersions(std::set<Version>* ret) const;
88 
89     // insert instances to mManifestInstances.
90     // Existing instances will be ignored.
91     // Pre: all instances to be inserted must satisfy
92     // !hasPackage() && hasVersion() && hasInterface() && hasInstance()
93     bool insertInstance(const FqInstance& fqInstance, bool allowDupMajorVersion,
94                         std::string* error = nullptr);
95     bool insertInstances(const std::set<FqInstance>& fqInstances, bool allowDupMajorVersion,
96                          std::string* error = nullptr);
97 
98     // Verify instance before inserting.
99     bool verifyInstance(const FqInstance& fqInstance, std::string* error = nullptr) const;
100 
101     bool mIsOverride = false;
102     std::optional<std::string> mUpdatableViaApex;
103     // All instances specified with <fqname> and <version> x <interface> x <instance>
104     std::set<ManifestInstance> mManifestInstances;
105 
106     // Max level of this HAL (inclusive). Only valid for framework manifest HALs.
107     // If set, HALs with max-level < target FCM version in device manifest is
108     // disabled.
109     Level mMaxLevel = Level::UNSPECIFIED;
110     // Min level of this HAL (inclusive). Only valid for framework manifest HALs.
111     // If set, HALs with max-level > target FCM version in device manifest is
112     // disabled.
113     Level mMinLevel = Level::UNSPECIFIED;
114 };
115 
116 } // namespace vintf
117 } // namespace android
118 
119 #endif // ANDROID_VINTF_MANIFEST_HAL_H
120