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_VINTF_OBJECT_H_ 18 #define ANDROID_VINTF_VINTF_OBJECT_H_ 19 20 #include <memory> 21 22 #include "CompatibilityMatrix.h" 23 #include "DisabledChecks.h" 24 #include "HalManifest.h" 25 #include "Named.h" 26 #include "RuntimeInfo.h" 27 28 namespace android { 29 namespace vintf { 30 /* 31 * The top level class for libvintf. 32 * An overall diagram of the public API: 33 * VintfObject 34 * + GetDeviceHalManfiest 35 * | + getTransport 36 * | + checkCompatibility 37 * + GetFrameworkHalManifest 38 * | + getTransport 39 * | + checkCompatibility 40 * + GetRuntimeInfo 41 * + checkCompatibility 42 * 43 * Each of the function gathers all information and encapsulate it into the object. 44 * If no error, it return the same singleton object in the future, and the HAL manifest 45 * file won't be touched again. 46 * If any error, nullptr is returned, and Get will try to parse the HAL manifest 47 * again when it is called again. 48 * All these operations are thread-safe. 49 * If skipCache, always skip the cache in memory and read the files / get runtime information 50 * again from the device. 51 */ 52 class VintfObject { 53 public: 54 /* 55 * Return the API that access the device-side HAL manifest stored 56 * in /vendor/manifest.xml. 57 */ 58 static std::shared_ptr<const HalManifest> GetDeviceHalManifest(bool skipCache = false); 59 60 /* 61 * Return the API that access the framework-side HAL manifest stored 62 * in /system/manfiest.xml. 63 */ 64 static std::shared_ptr<const HalManifest> GetFrameworkHalManifest(bool skipCache = false); 65 66 /* 67 * Return the API that access the device-side compatibility matrix stored 68 * in /vendor/compatibility_matrix.xml. 69 */ 70 static std::shared_ptr<const CompatibilityMatrix> GetDeviceCompatibilityMatrix( 71 bool skipCache = false); 72 73 /* 74 * Return the API that access the framework-side compatibility matrix stored 75 * in /system/compatibility_matrix.xml. 76 */ 77 static std::shared_ptr<const CompatibilityMatrix> GetFrameworkCompatibilityMatrix( 78 bool skipCache = false); 79 80 /* 81 * Return the API that access device runtime info. 82 * 83 * {skipCache == true, flags == ALL}: re-fetch everything 84 * {skipCache == false, flags == ALL}: fetch everything if not previously fetched 85 * {skipCache == true, flags == selected info}: re-fetch selected information 86 * if not previously fetched. 87 * {skipCache == false, flags == selected info}: fetch selected information 88 * if not previously fetched. 89 * 90 * @param skipCache do not fetch if previously fetched 91 * @param flags bitwise-or of RuntimeInfo::FetchFlag 92 */ 93 static std::shared_ptr<const RuntimeInfo> GetRuntimeInfo( 94 bool skipCache = false, RuntimeInfo::FetchFlags flags = RuntimeInfo::FetchFlag::ALL); 95 96 /** 97 * Check compatibility, given a set of manifests / matrices in packageInfo. 98 * They will be checked against the manifests / matrices on the device. 99 * 100 * @param packageInfo a list of XMLs of HalManifest / 101 * CompatibilityMatrix objects. 102 * @param error error message 103 * @param disabledChecks flags to disable certain checks. See DisabledChecks. 104 * 105 * @return = 0 if success (compatible) 106 * > 0 if incompatible 107 * < 0 if any error (mount partition fails, illformed XML, etc.) 108 */ 109 static int32_t CheckCompatibility(const std::vector<std::string>& packageInfo, 110 std::string* error = nullptr, 111 DisabledChecks disabledChecks = ENABLE_ALL_CHECKS); 112 113 /** 114 * A std::function that abstracts a list of "provided" instance names. Given package, version 115 * and interface, the function returns a list of instance names that matches. 116 * This function can represent a manifest, an IServiceManager, etc. 117 * If the source is passthrough service manager, a list of instance names cannot be provided. 118 * Instead, the function should call getService on each of the "hintInstances", and 119 * return those instances for which getService does not return a nullptr. This means that for 120 * passthrough HALs, the deprecation on <regex-instance>s cannot be enforced; only <instance>s 121 * can be enforced. 122 */ 123 using ListInstances = std::function<std::vector<std::pair<std::string, Version>>( 124 const std::string& package, Version version, const std::string& interface, 125 const std::vector<std::string>& hintInstances)>; 126 /** 127 * Check deprecation on framework matrices with a provided predicate. 128 * 129 * @param listInstances predicate that takes parameter in this format: 130 * android.hardware.foo@1.0::IFoo 131 * and returns {{"default", version}...} if HAL is in use, where version = 132 * first version in interfaceChain where package + major version matches. 133 * 134 * @return = 0 if success (no deprecated HALs) 135 * > 0 if there is at least one deprecated HAL 136 * < 0 if any error (mount partition fails, illformed XML, etc.) 137 */ 138 static int32_t CheckDeprecation(const ListInstances& listInstances, 139 std::string* error = nullptr); 140 141 /** 142 * Check deprecation on existing VINTF metadata. Use Device Manifest as the 143 * predicate to check if a HAL is in use. 144 * 145 * @return = 0 if success (no deprecated HALs) 146 * > 0 if there is at least one deprecated HAL 147 * < 0 if any error (mount partition fails, illformed XML, etc.) 148 */ 149 static int32_t CheckDeprecation(std::string* error = nullptr); 150 151 private: 152 static status_t GetCombinedFrameworkMatrix( 153 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out, 154 std::string* error = nullptr); 155 static std::vector<Named<CompatibilityMatrix>> GetAllFrameworkMatrixLevels( 156 std::string* error = nullptr); 157 static status_t FetchDeviceHalManifest(HalManifest* out, std::string* error = nullptr); 158 static status_t FetchDeviceMatrix(CompatibilityMatrix* out, std::string* error = nullptr); 159 static status_t FetchOdmHalManifest(HalManifest* out, std::string* error = nullptr); 160 static status_t FetchOneHalManifest(const std::string& path, HalManifest* out, 161 std::string* error = nullptr); 162 static status_t FetchFrameworkHalManifest(HalManifest* out, std::string* error = nullptr); 163 164 static bool isHalDeprecated(const MatrixHal& oldMatrixHal, 165 const CompatibilityMatrix& targetMatrix, 166 const ListInstances& listInstances, std::string* error); 167 static bool isInstanceDeprecated(const MatrixInstance& oldMatrixInstance, 168 const CompatibilityMatrix& targetMatrix, 169 const ListInstances& listInstances, std::string* error); 170 }; 171 172 enum : int32_t { 173 COMPATIBLE = 0, 174 INCOMPATIBLE = 1, 175 176 NO_DEPRECATED_HALS = 0, 177 DEPRECATED = 1, 178 }; 179 180 // exposed for testing and VintfObjectRecovery. 181 namespace details { 182 class PartitionMounter; 183 int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount, 184 const PartitionMounter& partitionMounter, std::string* error, 185 DisabledChecks disabledChecks = ENABLE_ALL_CHECKS); 186 187 extern const std::string kSystemVintfDir; 188 extern const std::string kVendorVintfDir; 189 extern const std::string kOdmVintfDir; 190 extern const std::string kOdmLegacyVintfDir; 191 extern const std::string kOdmLegacyManifest; 192 extern const std::string kVendorManifest; 193 extern const std::string kSystemManifest; 194 extern const std::string kVendorMatrix; 195 extern const std::string kOdmManifest; 196 extern const std::string kVendorLegacyManifest; 197 extern const std::string kVendorLegacyMatrix; 198 extern const std::string kSystemLegacyManifest; 199 extern const std::string kSystemLegacyMatrix; 200 201 // Convenience function to dump all files and directories that could be read 202 // by calling Get(Framework|Device)(HalManifest|CompatibilityMatrix). The list 203 // include files that may not actually be read when the four functions are called 204 // because some files have a higher priority than others. The list does NOT 205 // include "files" (including kernel interfaces) that are read when GetRuntimeInfo 206 // is called. 207 std::vector<std::string> dumpFileList(); 208 209 } // namespace details 210 211 } // namespace vintf 212 } // namespace android 213 214 #endif // ANDROID_VINTF_VINTF_OBJECT_H_ 215