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_MATRIX_HAL_H
18 #define ANDROID_VINTF_MATRIX_HAL_H
19 
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include "HalFormat.h"
26 #include "HalInterface.h"
27 #include "MatrixInstance.h"
28 #include "VersionRange.h"
29 
30 namespace android {
31 namespace vintf {
32 
33 // A HAL entry to a compatibility matrix
34 struct MatrixHal {
35     using InstanceType = MatrixInstance;
36 
37     bool operator==(const MatrixHal &other) const;
38     // Check whether the MatrixHal contains the given version.
39     bool containsVersion(const Version& version) const;
40 
41     HalFormat format = HalFormat::HIDL;
42     std::string name;
43     std::vector<VersionRange> versionRanges;
44     bool optional = false;
45     std::map<std::string, HalInterface> interfaces;
46 
getNameMatrixHal47     inline const std::string& getName() const { return name; }
48 
49     // Assumes isValid().
50     bool forEachInstance(const std::function<bool(const MatrixInstance&)>& func) const;
51 
52    private:
53     friend struct HalManifest;
54     friend struct CompatibilityMatrix;
55     friend struct MatrixHalConverter;
56 
57     // Whether this hal is a valid one. Note that an empty MatrixHal
58     // (constructed via MatrixHal()) is valid.
59     [[nodiscard]] bool isValid(std::string* error = nullptr) const;
60 
61     friend std::string expandInstances(const MatrixHal& req, const VersionRange& vr, bool brace);
62     friend std::vector<std::string> expandInstances(const MatrixHal& req);
63 
64     // Loop over interface/instance for a specific VersionRange.
65     // Assumes isValid().
66     bool forEachInstance(const VersionRange& vr,
67                          const std::function<bool(const MatrixInstance&)>& func) const;
68     // Loop over interface/instance. VersionRange is supplied to the function as a vector.
69     // Assumes isValid().
70     bool forEachInstance(
71         const std::function<bool(const std::vector<VersionRange>&, const std::string&,
72                                  const std::string& instanceOrPattern, bool isRegex)>& func) const;
73 
74     bool isCompatible(const std::set<FqInstance>& providedInstances,
75                       const std::set<Version>& providedVersions) const;
76     bool isCompatible(const VersionRange& vr, const std::set<FqInstance>& providedInstances,
77                       const std::set<Version>& providedVersions) const;
78 
79     void setOptional(bool o);
80     void insertVersionRanges(const std::vector<VersionRange>& other);
81     // Return size of all interface/instance pairs.
82     size_t instancesCount() const;
83     void insertInstance(const std::string& interface, const std::string& instance, bool isRegex);
84     // Remove a specific interface/instances. Return true if removed, false otherwise.
85     bool removeInstance(const std::string& interface, const std::string& instance, bool isRegex);
86     // Remove all <interface> tags.
87     void clearInstances();
88 };
89 
90 } // namespace vintf
91 } // namespace android
92 
93 #endif // ANDROID_VINTF_MATRIX_HAL_H
94