Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 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 | #include "VintfObject.h" |
| 18 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 19 | #include "CompatibilityMatrix.h" |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 20 | #include "parse_string.h" |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 21 | #include "parse_xml.h" |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 22 | #include "utils.h" |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 23 | |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 24 | #include <dirent.h> |
| 25 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 26 | #include <functional> |
| 27 | #include <memory> |
| 28 | #include <mutex> |
| 29 | |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 30 | #include <android-base/logging.h> |
| 31 | |
| 32 | using std::placeholders::_1; |
| 33 | using std::placeholders::_2; |
| 34 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace vintf { |
| 37 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 38 | using namespace details; |
| 39 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 40 | template <typename T> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 41 | struct LockedSharedPtr { |
| 42 | std::shared_ptr<T> object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 43 | std::mutex mutex; |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 44 | bool fetchedOnce = false; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 47 | struct LockedRuntimeInfoCache { |
| 48 | std::shared_ptr<RuntimeInfo> object; |
| 49 | std::mutex mutex; |
| 50 | RuntimeInfo::FetchFlags fetchedFlags = RuntimeInfo::FetchFlag::NONE; |
| 51 | }; |
| 52 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 53 | template <typename T, typename F> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 54 | static std::shared_ptr<const T> Get( |
| 55 | LockedSharedPtr<T> *ptr, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 56 | bool skipCache, |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 57 | const F &fetchAllInformation) { |
| 58 | std::unique_lock<std::mutex> _lock(ptr->mutex); |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 59 | if (skipCache || !ptr->fetchedOnce) { |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 60 | ptr->object = std::make_unique<T>(); |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 61 | std::string error; |
| 62 | if (fetchAllInformation(ptr->object.get(), &error) != OK) { |
| 63 | LOG(WARNING) << error; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 64 | ptr->object = nullptr; // frees the old object |
| 65 | } |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 66 | ptr->fetchedOnce = true; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 67 | } |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 68 | return ptr->object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 72 | std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) { |
Steven Moreland | 648a001 | 2017-10-19 21:23:41 -0700 | [diff] [blame] | 73 | static LockedSharedPtr<HalManifest> gVendorManifest; |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 74 | return Get(&gVendorManifest, skipCache, &VintfObject::FetchDeviceHalManifest); |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 78 | std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) { |
| 79 | static LockedSharedPtr<HalManifest> gFrameworkManifest; |
Yifan Hong | de6d00e | 2018-02-27 17:11:28 -0800 | [diff] [blame] | 80 | return Get(&gFrameworkManifest, skipCache, &VintfObject::FetchFrameworkHalManifest); |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 83 | |
| 84 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 85 | std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) { |
| 86 | static LockedSharedPtr<CompatibilityMatrix> gDeviceMatrix; |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 87 | return Get(&gDeviceMatrix, skipCache, &VintfObject::FetchDeviceMatrix); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 91 | std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) { |
| 92 | static LockedSharedPtr<CompatibilityMatrix> gFrameworkMatrix; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 93 | static LockedSharedPtr<CompatibilityMatrix> gCombinedFrameworkMatrix; |
| 94 | static std::mutex gFrameworkCompatibilityMatrixMutex; |
| 95 | |
| 96 | // To avoid deadlock, get device manifest before any locks. |
| 97 | auto deviceManifest = GetDeviceHalManifest(); |
| 98 | |
| 99 | std::unique_lock<std::mutex> _lock(gFrameworkCompatibilityMatrixMutex); |
| 100 | |
| 101 | auto combined = |
| 102 | Get(&gCombinedFrameworkMatrix, skipCache, |
| 103 | std::bind(&VintfObject::GetCombinedFrameworkMatrix, deviceManifest, _1, _2)); |
| 104 | if (combined != nullptr) { |
| 105 | return combined; |
| 106 | } |
| 107 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 108 | return Get(&gFrameworkMatrix, skipCache, |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 109 | std::bind(&CompatibilityMatrix::fetchAllInformation, _1, kSystemLegacyMatrix, _2)); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 112 | status_t VintfObject::GetCombinedFrameworkMatrix( |
| 113 | const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out, |
| 114 | std::string* error) { |
| 115 | auto matrixFragments = GetAllFrameworkMatrixLevels(error); |
| 116 | if (matrixFragments.empty()) { |
| 117 | return NAME_NOT_FOUND; |
| 118 | } |
| 119 | |
| 120 | Level deviceLevel = Level::UNSPECIFIED; |
| 121 | |
| 122 | if (deviceManifest != nullptr) { |
| 123 | deviceLevel = deviceManifest->level(); |
| 124 | } |
| 125 | |
| 126 | // TODO(b/70628538): Do not infer from Shipping API level. |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 127 | if (deviceLevel == Level::UNSPECIFIED) { |
Yifan Hong | d14640a | 2018-02-27 18:35:39 -0800 | [diff] [blame] | 128 | auto shippingApi = getPropertyFetcher().getUintProperty("ro.product.first_api_level", 0u); |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 129 | if (shippingApi != 0u) { |
| 130 | deviceLevel = details::convertFromApiLevel(shippingApi); |
| 131 | } |
| 132 | } |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 133 | |
| 134 | if (deviceLevel == Level::UNSPECIFIED) { |
| 135 | // Cannot infer FCM version. Combine all matrices by assuming |
| 136 | // Shipping FCM Version == min(all supported FCM Versions in the framework) |
| 137 | for (auto&& pair : matrixFragments) { |
| 138 | Level fragmentLevel = pair.object.level(); |
| 139 | if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) { |
| 140 | deviceLevel = fragmentLevel; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (deviceLevel == Level::UNSPECIFIED) { |
| 146 | // None of the fragments specify any FCM version. Should never happen except |
| 147 | // for inconsistent builds. |
| 148 | if (error) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 149 | *error = "No framework compatibility matrix files under " + kSystemVintfDir + |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 150 | " declare FCM version."; |
| 151 | } |
| 152 | return NAME_NOT_FOUND; |
| 153 | } |
| 154 | |
| 155 | CompatibilityMatrix* combined = |
| 156 | CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error); |
| 157 | if (combined == nullptr) { |
| 158 | return BAD_VALUE; |
| 159 | } |
| 160 | *out = std::move(*combined); |
| 161 | return OK; |
| 162 | } |
| 163 | |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame^] | 164 | // Load and combine all of the manifests in a directory |
| 165 | status_t VintfObject::AddDirectoryManifests(const std::string& directory, HalManifest* manifest, |
| 166 | std::string* error) { |
| 167 | std::vector<std::string> fileNames; |
| 168 | status_t err = details::gFetcher->listFiles(directory, &fileNames, error); |
| 169 | // if the directory isn't there, that's okay |
| 170 | if (err == NAME_NOT_FOUND) return OK; |
| 171 | if (err != OK) return err; |
| 172 | |
| 173 | for (const std::string& file : fileNames) { |
| 174 | // Only adds HALs because all other things are added by libvintf |
| 175 | // itself for now. |
| 176 | HalManifest fragmentManifest; |
| 177 | err = FetchOneHalManifest(directory + file, &fragmentManifest, error); |
| 178 | if (err != OK) return err; |
| 179 | |
| 180 | manifest->addAllHals(&fragmentManifest); |
| 181 | } |
| 182 | |
| 183 | return OK; |
| 184 | } |
| 185 | |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 186 | // Priority for loading vendor manifest: |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame^] | 187 | // 1. /vendor/etc/vintf/manifest.xml + device fragments + ODM manifest (optional) + odm fragments |
| 188 | // 2. /vendor/etc/vintf/manifest.xml + device fragments |
| 189 | // 3. ODM manifest (optional) + odm fragments |
| 190 | // 4. /vendor/manifest.xml (legacy, no fragments) |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 191 | // where: |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 192 | // A + B means adding <hal> tags from B to A (so that <hal>s from B can override A) |
| 193 | status_t VintfObject::FetchDeviceHalManifest(HalManifest* out, std::string* error) { |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 194 | status_t vendorStatus = FetchOneHalManifest(kVendorManifest, out, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 195 | if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) { |
| 196 | return vendorStatus; |
| 197 | } |
| 198 | |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame^] | 199 | if (vendorStatus == OK) { |
| 200 | status_t fragmentStatus = AddDirectoryManifests(kVendorManifestFragmentDir, out, error); |
| 201 | if (fragmentStatus != OK) { |
| 202 | return fragmentStatus; |
| 203 | } |
| 204 | } |
| 205 | |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 206 | HalManifest odmManifest; |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 207 | status_t odmStatus = FetchOdmHalManifest(&odmManifest, error); |
| 208 | if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) { |
| 209 | return odmStatus; |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 212 | if (vendorStatus == OK) { |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 213 | if (odmStatus == OK) { |
| 214 | out->addAllHals(&odmManifest); |
| 215 | } |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame^] | 216 | return AddDirectoryManifests(kOdmManifestFragmentDir, out, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 219 | // vendorStatus != OK, "out" is not changed. |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 220 | if (odmStatus == OK) { |
| 221 | *out = std::move(odmManifest); |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame^] | 222 | return AddDirectoryManifests(kOdmManifestFragmentDir, out, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Use legacy /vendor/manifest.xml |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 226 | return out->fetchAllInformation(kVendorLegacyManifest, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 229 | // "out" is written to iff return status is OK. |
| 230 | // Priority: |
| 231 | // 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml |
| 232 | // 2. /odm/etc/vintf/manifest.xml |
| 233 | // 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml |
| 234 | // 4. /odm/etc/manifest.xml |
| 235 | // where: |
| 236 | // {sku} is the value of ro.boot.product.hardware.sku |
| 237 | status_t VintfObject::FetchOdmHalManifest(HalManifest* out, std::string* error) { |
| 238 | status_t status; |
| 239 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 240 | std::string productModel; |
Yifan Hong | d14640a | 2018-02-27 18:35:39 -0800 | [diff] [blame] | 241 | productModel = getPropertyFetcher().getProperty("ro.boot.product.hardware.sku", ""); |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 242 | |
| 243 | if (!productModel.empty()) { |
| 244 | status = |
| 245 | FetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error); |
| 246 | if (status == OK || status != NAME_NOT_FOUND) { |
| 247 | return status; |
| 248 | } |
| 249 | } |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 250 | |
| 251 | status = FetchOneHalManifest(kOdmManifest, out, error); |
| 252 | if (status == OK || status != NAME_NOT_FOUND) { |
| 253 | return status; |
| 254 | } |
| 255 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 256 | if (!productModel.empty()) { |
| 257 | status = FetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out, |
| 258 | error); |
| 259 | if (status == OK || status != NAME_NOT_FOUND) { |
| 260 | return status; |
| 261 | } |
| 262 | } |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 263 | |
| 264 | status = FetchOneHalManifest(kOdmLegacyManifest, out, error); |
| 265 | if (status == OK || status != NAME_NOT_FOUND) { |
| 266 | return status; |
| 267 | } |
| 268 | |
| 269 | return NAME_NOT_FOUND; |
| 270 | } |
| 271 | |
| 272 | // Fetch one manifest.xml file. "out" is written to iff return status is OK. |
| 273 | // Returns NAME_NOT_FOUND if file is missing. |
| 274 | status_t VintfObject::FetchOneHalManifest(const std::string& path, HalManifest* out, |
| 275 | std::string* error) { |
| 276 | HalManifest ret; |
| 277 | status_t status = ret.fetchAllInformation(path, error); |
| 278 | if (status == OK) { |
| 279 | *out = std::move(ret); |
| 280 | } |
| 281 | return status; |
| 282 | } |
| 283 | |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 284 | status_t VintfObject::FetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) { |
| 285 | CompatibilityMatrix etcMatrix; |
| 286 | if (etcMatrix.fetchAllInformation(kVendorMatrix, error) == OK) { |
| 287 | *out = std::move(etcMatrix); |
| 288 | return OK; |
| 289 | } |
| 290 | return out->fetchAllInformation(kVendorLegacyMatrix, error); |
| 291 | } |
| 292 | |
Yifan Hong | de6d00e | 2018-02-27 17:11:28 -0800 | [diff] [blame] | 293 | status_t VintfObject::FetchFrameworkHalManifest(HalManifest* out, std::string* error) { |
| 294 | HalManifest etcManifest; |
| 295 | if (etcManifest.fetchAllInformation(kSystemManifest, error) == OK) { |
| 296 | *out = std::move(etcManifest); |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame^] | 297 | return AddDirectoryManifests(kSystemManifestFragmentDir, out, error); |
Yifan Hong | de6d00e | 2018-02-27 17:11:28 -0800 | [diff] [blame] | 298 | } |
| 299 | return out->fetchAllInformation(kSystemLegacyManifest, error); |
| 300 | } |
| 301 | |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 302 | std::vector<Named<CompatibilityMatrix>> VintfObject::GetAllFrameworkMatrixLevels( |
| 303 | std::string* error) { |
| 304 | std::vector<std::string> fileNames; |
| 305 | std::vector<Named<CompatibilityMatrix>> results; |
| 306 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 307 | if (details::gFetcher->listFiles(kSystemVintfDir, &fileNames, error) != OK) { |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 308 | return {}; |
| 309 | } |
| 310 | for (const std::string& fileName : fileNames) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 311 | std::string path = kSystemVintfDir + fileName; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 312 | |
| 313 | std::string content; |
| 314 | std::string fetchError; |
| 315 | status_t status = details::gFetcher->fetch(path, content, &fetchError); |
| 316 | if (status != OK) { |
| 317 | if (error) { |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 318 | *error += "Framework Matrix: Ignore file " + path + ": " + fetchError + "\n"; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 319 | } |
| 320 | continue; |
| 321 | } |
| 322 | |
| 323 | auto it = results.emplace(results.end()); |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 324 | if (!gCompatibilityMatrixConverter(&it->object, content, error)) { |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 325 | if (error) { |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 326 | *error += "Framework Matrix: Ignore file " + path + ": " + *error + "\n"; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 327 | } |
| 328 | results.erase(it); |
| 329 | continue; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (results.empty()) { |
| 334 | if (error) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 335 | *error = "No framework matrices under " + kSystemVintfDir + |
| 336 | " can be fetched or parsed.\n" + *error; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 337 | } |
| 338 | } else { |
| 339 | if (error && !error->empty()) { |
| 340 | LOG(WARNING) << *error; |
| 341 | *error = ""; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | return results; |
| 346 | } |
| 347 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 348 | // static |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 349 | std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache, |
| 350 | RuntimeInfo::FetchFlags flags) { |
| 351 | static LockedRuntimeInfoCache gDeviceRuntimeInfo; |
| 352 | std::unique_lock<std::mutex> _lock(gDeviceRuntimeInfo.mutex); |
| 353 | |
| 354 | if (!skipCache) { |
| 355 | flags &= (~gDeviceRuntimeInfo.fetchedFlags); |
| 356 | } |
| 357 | |
| 358 | if (gDeviceRuntimeInfo.object == nullptr) { |
Yifan Hong | 29bb2d4 | 2017-09-27 13:28:00 -0700 | [diff] [blame] | 359 | gDeviceRuntimeInfo.object = details::gRuntimeInfoFactory->make_shared(); |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | status_t status = gDeviceRuntimeInfo.object->fetchAllInformation(flags); |
| 363 | if (status != OK) { |
| 364 | gDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched" |
| 365 | return nullptr; |
| 366 | } |
| 367 | |
| 368 | gDeviceRuntimeInfo.fetchedFlags |= flags; |
| 369 | return gDeviceRuntimeInfo.object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 372 | namespace details { |
| 373 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 374 | enum class ParseStatus { |
| 375 | OK, |
| 376 | PARSE_ERROR, |
| 377 | DUPLICATED_FWK_ENTRY, |
| 378 | DUPLICATED_DEV_ENTRY, |
| 379 | }; |
| 380 | |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 381 | static std::string toString(ParseStatus status) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 382 | switch(status) { |
| 383 | case ParseStatus::OK: return "OK"; |
| 384 | case ParseStatus::PARSE_ERROR: return "parse error"; |
| 385 | case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework"; |
| 386 | case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device"; |
| 387 | } |
| 388 | return ""; |
| 389 | } |
| 390 | |
| 391 | template<typename T> |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 392 | static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse, |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 393 | std::shared_ptr<T> *fwk, std::shared_ptr<T> *dev) { |
| 394 | std::shared_ptr<T> ret = std::make_shared<T>(); |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 395 | if (!parse(ret.get(), xml, nullptr /* error */)) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 396 | return ParseStatus::PARSE_ERROR; |
| 397 | } |
| 398 | if (ret->type() == SchemaType::FRAMEWORK) { |
| 399 | if (fwk->get() != nullptr) { |
| 400 | return ParseStatus::DUPLICATED_FWK_ENTRY; |
| 401 | } |
| 402 | *fwk = std::move(ret); |
| 403 | } else if (ret->type() == SchemaType::DEVICE) { |
| 404 | if (dev->get() != nullptr) { |
| 405 | return ParseStatus::DUPLICATED_DEV_ENTRY; |
| 406 | } |
| 407 | *dev = std::move(ret); |
| 408 | } |
| 409 | return ParseStatus::OK; |
| 410 | } |
| 411 | |
| 412 | template<typename T, typename GetFunction> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 413 | static status_t getMissing(const std::shared_ptr<T>& pkg, bool mount, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 414 | std::function<status_t(void)> mountFunction, |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 415 | std::shared_ptr<const T>* updated, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 416 | GetFunction getFunction) { |
| 417 | if (pkg != nullptr) { |
| 418 | *updated = pkg; |
| 419 | } else { |
| 420 | if (mount) { |
| 421 | (void)mountFunction(); // ignore mount errors |
| 422 | } |
| 423 | *updated = getFunction(); |
| 424 | } |
| 425 | return OK; |
| 426 | } |
| 427 | |
| 428 | #define ADD_MESSAGE(__error__) \ |
| 429 | if (error != nullptr) { \ |
| 430 | *error += (__error__); \ |
| 431 | } \ |
| 432 | |
| 433 | struct PackageInfo { |
| 434 | struct Pair { |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 435 | std::shared_ptr<HalManifest> manifest; |
| 436 | std::shared_ptr<CompatibilityMatrix> matrix; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 437 | }; |
| 438 | Pair dev; |
| 439 | Pair fwk; |
| 440 | }; |
| 441 | |
| 442 | struct UpdatedInfo { |
| 443 | struct Pair { |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 444 | std::shared_ptr<const HalManifest> manifest; |
| 445 | std::shared_ptr<const CompatibilityMatrix> matrix; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 446 | }; |
| 447 | Pair dev; |
| 448 | Pair fwk; |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 449 | std::shared_ptr<const RuntimeInfo> runtimeInfo; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 450 | }; |
| 451 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 452 | // Checks given compatibility info against info on the device. If no |
| 453 | // compatability info is given then the device info will be checked against |
| 454 | // itself. |
Yifan Hong | db6423e | 2017-09-11 14:38:46 -0700 | [diff] [blame] | 455 | int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount, |
| 456 | const PartitionMounter& mounter, std::string* error, |
| 457 | DisabledChecks disabledChecks) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 458 | status_t status; |
| 459 | ParseStatus parseStatus; |
| 460 | PackageInfo pkg; // All information from package. |
| 461 | UpdatedInfo updated; // All files and runtime info after the update. |
| 462 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 463 | // parse all information from package |
| 464 | for (const auto &xml : xmls) { |
| 465 | parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest); |
| 466 | if (parseStatus == ParseStatus::OK) { |
| 467 | continue; // work on next one |
| 468 | } |
| 469 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
| 470 | ADD_MESSAGE(toString(parseStatus) + " manifest"); |
| 471 | return ALREADY_EXISTS; |
| 472 | } |
| 473 | parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix); |
| 474 | if (parseStatus == ParseStatus::OK) { |
| 475 | continue; // work on next one |
| 476 | } |
| 477 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
| 478 | ADD_MESSAGE(toString(parseStatus) + " matrix"); |
| 479 | return ALREADY_EXISTS; |
| 480 | } |
| 481 | ADD_MESSAGE(toString(parseStatus)); // parse error |
| 482 | return BAD_VALUE; |
| 483 | } |
| 484 | |
| 485 | // get missing info from device |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 486 | // use functions instead of std::bind because std::bind doesn't work well with mock objects |
| 487 | auto mountSystem = [&mounter] { return mounter.mountSystem(); }; |
| 488 | auto mountVendor = [&mounter] { return mounter.mountVendor(); }; |
| 489 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 490 | pkg.fwk.manifest, mount, mountSystem, &updated.fwk.manifest, |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 491 | std::bind(VintfObject::GetFrameworkHalManifest, true /* skipCache */))) != OK) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 492 | return status; |
| 493 | } |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 494 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 495 | pkg.dev.manifest, mount, mountVendor, &updated.dev.manifest, |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 496 | std::bind(VintfObject::GetDeviceHalManifest, true /* skipCache */))) != OK) { |
| 497 | return status; |
| 498 | } |
| 499 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 500 | pkg.fwk.matrix, mount, mountSystem, &updated.fwk.matrix, |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 501 | std::bind(VintfObject::GetFrameworkCompatibilityMatrix, true /* skipCache */))) != |
| 502 | OK) { |
| 503 | return status; |
| 504 | } |
| 505 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 506 | pkg.dev.matrix, mount, mountVendor, &updated.dev.matrix, |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 507 | std::bind(VintfObject::GetDeviceCompatibilityMatrix, true /* skipCache */))) != OK) { |
| 508 | return status; |
| 509 | } |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 510 | |
| 511 | if (mount) { |
| 512 | (void)mounter.umountSystem(); // ignore errors |
| 513 | (void)mounter.umountVendor(); // ignore errors |
| 514 | } |
| 515 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 516 | if ((disabledChecks & DISABLE_RUNTIME_INFO) == 0) { |
| 517 | updated.runtimeInfo = VintfObject::GetRuntimeInfo(true /* skipCache */); |
| 518 | } |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 519 | |
| 520 | // null checks for files and runtime info after the update |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 521 | if (updated.fwk.manifest == nullptr) { |
| 522 | ADD_MESSAGE("No framework manifest file from device or from update package"); |
| 523 | return NO_INIT; |
| 524 | } |
| 525 | if (updated.dev.manifest == nullptr) { |
| 526 | ADD_MESSAGE("No device manifest file from device or from update package"); |
| 527 | return NO_INIT; |
| 528 | } |
| 529 | if (updated.fwk.matrix == nullptr) { |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 530 | ADD_MESSAGE("No framework matrix file from device or from update package"); |
| 531 | return NO_INIT; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 532 | } |
| 533 | if (updated.dev.matrix == nullptr) { |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 534 | ADD_MESSAGE("No device matrix file from device or from update package"); |
| 535 | return NO_INIT; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 536 | } |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 537 | |
| 538 | if ((disabledChecks & DISABLE_RUNTIME_INFO) == 0) { |
| 539 | if (updated.runtimeInfo == nullptr) { |
| 540 | ADD_MESSAGE("No runtime info from device"); |
| 541 | return NO_INIT; |
| 542 | } |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // compatiblity check. |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 546 | if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) { |
| 547 | if (error) { |
| 548 | error->insert(0, |
| 549 | "Device manifest and framework compatibility matrix are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 550 | } |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 551 | return INCOMPATIBLE; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 552 | } |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 553 | if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) { |
| 554 | if (error) { |
| 555 | error->insert(0, |
| 556 | "Framework manifest and device compatibility matrix are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 557 | } |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 558 | return INCOMPATIBLE; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 559 | } |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 560 | |
| 561 | if ((disabledChecks & DISABLE_RUNTIME_INFO) == 0) { |
| 562 | if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) { |
| 563 | if (error) { |
| 564 | error->insert(0, |
| 565 | "Runtime info and framework compatibility matrix are incompatible: "); |
| 566 | } |
| 567 | return INCOMPATIBLE; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | return COMPATIBLE; |
| 572 | } |
| 573 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 574 | const std::string kSystemVintfDir = "/system/etc/vintf/"; |
Yifan Hong | ccbea05 | 2018-01-18 18:48:46 -0800 | [diff] [blame] | 575 | const std::string kVendorVintfDir = "/vendor/etc/vintf/"; |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 576 | const std::string kOdmVintfDir = "/odm/etc/vintf/"; |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 577 | |
| 578 | const std::string kVendorManifest = kVendorVintfDir + "manifest.xml"; |
Yifan Hong | a5ddddf | 2018-01-18 18:50:13 -0800 | [diff] [blame] | 579 | const std::string kSystemManifest = kSystemVintfDir + "manifest.xml"; |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 580 | const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml"; |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 581 | const std::string kOdmManifest = kOdmVintfDir + "manifest.xml"; |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 582 | |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame^] | 583 | const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/"; |
| 584 | const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/"; |
| 585 | const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/"; |
| 586 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 587 | const std::string kVendorLegacyManifest = "/vendor/manifest.xml"; |
| 588 | const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml"; |
Yifan Hong | de6d00e | 2018-02-27 17:11:28 -0800 | [diff] [blame] | 589 | const std::string kSystemLegacyManifest = "/system/manifest.xml"; |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 590 | const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml"; |
| 591 | const std::string kOdmLegacyVintfDir = "/odm/etc/"; |
| 592 | const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml"; |
| 593 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 594 | std::vector<std::string> dumpFileList() { |
| 595 | return { |
| 596 | kSystemVintfDir, kVendorVintfDir, kOdmVintfDir, kOdmLegacyVintfDir, |
| 597 | |
| 598 | kVendorLegacyManifest, kVendorLegacyMatrix, kSystemLegacyManifest, kSystemLegacyMatrix, |
| 599 | }; |
| 600 | } |
| 601 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 602 | } // namespace details |
| 603 | |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame] | 604 | // static |
Yifan Hong | db6423e | 2017-09-11 14:38:46 -0700 | [diff] [blame] | 605 | int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error, |
| 606 | DisabledChecks disabledChecks) { |
| 607 | return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error, |
| 608 | disabledChecks); |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 611 | bool VintfObject::isHalDeprecated(const MatrixHal& oldMatrixHal, |
| 612 | const CompatibilityMatrix& targetMatrix, |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 613 | const ListInstances& listInstances, std::string* error) { |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 614 | bool isDeprecated = false; |
| 615 | oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) { |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 616 | if (isInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) { |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 617 | isDeprecated = true; |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 618 | } |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 619 | return !isDeprecated; // continue if no deprecated instance is found. |
| 620 | }); |
| 621 | return isDeprecated; |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 624 | // Let oldMatrixInstance = package@x.y-w::interface with instancePattern. |
| 625 | // If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return |
| 626 | // true iff: |
| 627 | // 1. package@x.?::interface/servedInstance is not in targetMatrix; OR |
| 628 | // 2. package@x.z::interface/servedInstance is in targetMatrix but |
| 629 | // servedInstance is not in listInstances(package@x.z::interface) |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 630 | bool VintfObject::isInstanceDeprecated(const MatrixInstance& oldMatrixInstance, |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 631 | const CompatibilityMatrix& targetMatrix, |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 632 | const ListInstances& listInstances, std::string* error) { |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 633 | const std::string& package = oldMatrixInstance.package(); |
| 634 | const Version& version = oldMatrixInstance.versionRange().minVer(); |
| 635 | const std::string& interface = oldMatrixInstance.interface(); |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 636 | |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 637 | std::vector<std::string> instanceHint; |
| 638 | if (!oldMatrixInstance.isRegex()) { |
| 639 | instanceHint.push_back(oldMatrixInstance.exactInstance()); |
| 640 | } |
| 641 | |
| 642 | auto list = listInstances(package, version, interface, instanceHint); |
| 643 | for (const auto& pair : list) { |
| 644 | const std::string& servedInstance = pair.first; |
| 645 | Version servedVersion = pair.second; |
| 646 | if (!oldMatrixInstance.matchInstance(servedInstance)) { |
| 647 | continue; |
| 648 | } |
| 649 | |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 650 | // Find any package@x.? in target matrix, and check if instance is in target matrix. |
Yifan Hong | 217f47e | 2018-03-15 12:34:05 -0700 | [diff] [blame] | 651 | bool foundInstance = false; |
| 652 | Version targetMatrixMinVer; |
| 653 | targetMatrix.forEachInstanceOfPackage(package, [&](const auto& targetMatrixInstance) { |
| 654 | if (targetMatrixInstance.versionRange().majorVer == version.majorVer && |
| 655 | targetMatrixInstance.interface() == interface && |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 656 | targetMatrixInstance.matchInstance(servedInstance)) { |
Yifan Hong | 217f47e | 2018-03-15 12:34:05 -0700 | [diff] [blame] | 657 | targetMatrixMinVer = targetMatrixInstance.versionRange().minVer(); |
| 658 | foundInstance = true; |
| 659 | } |
| 660 | return !foundInstance; // continue if not found |
| 661 | }); |
| 662 | if (!foundInstance) { |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 663 | if (error) { |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 664 | *error = toFQNameString(package, servedVersion, interface, servedInstance) + |
Steven Moreland | a1b984c | 2018-03-09 13:09:15 -0800 | [diff] [blame] | 665 | " is deprecated in compatibility matrix at FCM Version " + |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 666 | to_string(targetMatrix.level()) + "; it should not be served."; |
| 667 | } |
| 668 | return true; |
| 669 | } |
| 670 | |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 671 | // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served. |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 672 | bool targetVersionServed = false; |
| 673 | for (const auto& newPair : |
| 674 | listInstances(package, targetMatrixMinVer, interface, instanceHint)) { |
| 675 | if (newPair.first == servedInstance) { |
| 676 | targetVersionServed = true; |
| 677 | break; |
| 678 | } |
| 679 | } |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 680 | |
| 681 | if (!targetVersionServed) { |
| 682 | if (error) { |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 683 | *error += toFQNameString(package, servedVersion, interface, servedInstance) + |
| 684 | " is deprecated; requires at least " + to_string(targetMatrixMinVer) + |
| 685 | "\n"; |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 686 | } |
| 687 | return true; |
| 688 | } |
| 689 | } |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 690 | |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 691 | return false; |
| 692 | } |
| 693 | |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 694 | int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) { |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 695 | auto matrixFragments = GetAllFrameworkMatrixLevels(error); |
| 696 | if (matrixFragments.empty()) { |
| 697 | if (error && error->empty()) |
| 698 | *error = "Cannot get framework matrix for each FCM version for unknown error."; |
| 699 | return NAME_NOT_FOUND; |
| 700 | } |
| 701 | auto deviceManifest = GetDeviceHalManifest(); |
| 702 | if (deviceManifest == nullptr) { |
| 703 | if (error) *error = "No device manifest."; |
| 704 | return NAME_NOT_FOUND; |
| 705 | } |
| 706 | Level deviceLevel = deviceManifest->level(); |
| 707 | if (deviceLevel == Level::UNSPECIFIED) { |
| 708 | if (error) *error = "Device manifest does not specify Shipping FCM Version."; |
| 709 | return BAD_VALUE; |
| 710 | } |
| 711 | |
| 712 | const CompatibilityMatrix* targetMatrix = nullptr; |
| 713 | for (const auto& namedMatrix : matrixFragments) { |
| 714 | if (namedMatrix.object.level() == deviceLevel) { |
| 715 | targetMatrix = &namedMatrix.object; |
| 716 | } |
| 717 | } |
| 718 | if (targetMatrix == nullptr) { |
| 719 | if (error) |
| 720 | *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + "."; |
| 721 | return NAME_NOT_FOUND; |
| 722 | } |
| 723 | |
| 724 | bool hasDeprecatedHals = false; |
| 725 | for (const auto& namedMatrix : matrixFragments) { |
| 726 | if (namedMatrix.object.level() == Level::UNSPECIFIED) continue; |
| 727 | if (namedMatrix.object.level() >= deviceLevel) continue; |
| 728 | |
| 729 | const auto& oldMatrix = namedMatrix.object; |
| 730 | for (const MatrixHal& hal : oldMatrix.getHals()) { |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 731 | hasDeprecatedHals |= isHalDeprecated(hal, *targetMatrix, listInstances, error); |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
| 735 | return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS; |
| 736 | } |
| 737 | |
| 738 | int32_t VintfObject::CheckDeprecation(std::string* error) { |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 739 | using namespace std::placeholders; |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 740 | auto deviceManifest = GetDeviceHalManifest(); |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 741 | ListInstances inManifest = |
| 742 | [&deviceManifest](const std::string& package, Version version, const std::string& interface, |
| 743 | const std::vector<std::string>& /* hintInstances */) { |
| 744 | std::vector<std::pair<std::string, Version>> ret; |
| 745 | deviceManifest->forEachInstanceOfInterface( |
| 746 | package, version, interface, [&ret](const ManifestInstance& manifestInstance) { |
| 747 | ret.push_back( |
| 748 | std::make_pair(manifestInstance.instance(), manifestInstance.version())); |
| 749 | return true; |
| 750 | }); |
| 751 | return ret; |
| 752 | }; |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 753 | return CheckDeprecation(inManifest, error); |
| 754 | } |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 755 | |
| 756 | } // namespace vintf |
| 757 | } // namespace android |