blob: 1f4d185b61034955d5f01ca1856bd3807902fc6c [file] [log] [blame]
Yifan Hong3daec812017-02-27 18:49:11 -08001/*
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 Hongd52bf3e2018-01-11 16:56:51 -080019#include <dirent.h>
20
Yifan Hong3daec812017-02-27 18:49:11 -080021#include <functional>
22#include <memory>
23#include <mutex>
24
Yifan Hong60217032018-01-08 16:19:42 -080025#include <android-base/logging.h>
26
Yifan Hong12e23c22018-11-05 14:53:30 -080027#include "CompatibilityMatrix.h"
28#include "VintfObjectAfterUpdate.h"
29#include "parse_string.h"
30#include "parse_xml.h"
31#include "utils.h"
32
Yifan Hong60217032018-01-08 16:19:42 -080033using std::placeholders::_1;
34using std::placeholders::_2;
35
Yifan Hong3daec812017-02-27 18:49:11 -080036namespace android {
37namespace vintf {
38
Yifan Hong270b5652018-01-18 18:46:01 -080039using namespace details;
40
Yifan Hong9f78c182018-07-12 14:45:52 -070041#ifdef LIBVINTF_TARGET
42static constexpr bool kIsTarget = true;
43#else
44static constexpr bool kIsTarget = false;
45#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070046
Yifan Hong3daec812017-02-27 18:49:11 -080047template <typename T, typename F>
Yifan Hongfc73edf2017-08-29 11:39:07 -070048static std::shared_ptr<const T> Get(
49 LockedSharedPtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070050 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080051 const F &fetchAllInformation) {
52 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080053 if (skipCache || !ptr->fetchedOnce) {
Yifan Hong3daec812017-02-27 18:49:11 -080054 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080055 std::string error;
56 if (fetchAllInformation(ptr->object.get(), &error) != OK) {
57 LOG(WARNING) << error;
Yifan Hong3daec812017-02-27 18:49:11 -080058 ptr->object = nullptr; // frees the old object
59 }
Yifan Hong7219ba12018-01-08 16:23:49 -080060 ptr->fetchedOnce = true;
Yifan Hong3daec812017-02-27 18:49:11 -080061 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070062 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080063}
64
Yifan Hong9f78c182018-07-12 14:45:52 -070065static std::unique_ptr<FileSystem> createDefaultFileSystem() {
66 std::unique_ptr<FileSystem> fileSystem;
67 if (kIsTarget) {
68 fileSystem = std::make_unique<details::FileSystemImpl>();
69 } else {
70 fileSystem = std::make_unique<details::FileSystemNoOp>();
71 }
72 return fileSystem;
73}
74
75static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
76 std::unique_ptr<PropertyFetcher> propertyFetcher;
77 if (kIsTarget) {
78 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
79 } else {
80 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
81 }
82 return propertyFetcher;
83}
84
Yifan Hong9f78c182018-07-12 14:45:52 -070085details::LockedSharedPtr<VintfObject> VintfObject::sInstance{};
86std::shared_ptr<VintfObject> VintfObject::GetInstance() {
87 std::unique_lock<std::mutex> lock(sInstance.mutex);
88 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -080089 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -070090 }
91 return sInstance.object;
92}
93
Yifan Hongfc73edf2017-08-29 11:39:07 -070094std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -070095 return GetInstance()->getDeviceHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -080096}
97
Yifan Hong9f78c182018-07-12 14:45:52 -070098std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
99 return Get(&mDeviceManifest, skipCache,
100 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
101}
102
Yifan Hongfc73edf2017-08-29 11:39:07 -0700103std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700104 return GetInstance()->getFrameworkHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800105}
106
Yifan Hong9f78c182018-07-12 14:45:52 -0700107std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
108 return Get(&mFrameworkManifest, skipCache,
109 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
110}
Yifan Hong2272bf82017-04-28 14:37:56 -0700111
Yifan Hongfc73edf2017-08-29 11:39:07 -0700112std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700113 return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
Yifan Hong2272bf82017-04-28 14:37:56 -0700114}
115
Yifan Hong9f78c182018-07-12 14:45:52 -0700116std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
117 bool skipCache) {
118 return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
119}
120
Yifan Hongfc73edf2017-08-29 11:39:07 -0700121std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700122 return GetInstance()->getFrameworkCompatibilityMatrix(skipCache);
123}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800124
Yifan Hong9f78c182018-07-12 14:45:52 -0700125std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix(
126 bool skipCache) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800127 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700128 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800129
Yifan Hong9f78c182018-07-12 14:45:52 -0700130 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800131
132 auto combined =
Yifan Hong9f78c182018-07-12 14:45:52 -0700133 Get(&mCombinedFrameworkMatrix, skipCache,
134 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800135 if (combined != nullptr) {
136 return combined;
137 }
138
Yifan Hong9f78c182018-07-12 14:45:52 -0700139 return Get(&mFrameworkMatrix, skipCache,
Yifan Hong12e23c22018-11-05 14:53:30 -0800140 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700141 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700142}
143
Yifan Hong9f78c182018-07-12 14:45:52 -0700144status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800145 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
146 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800147 std::vector<Named<CompatibilityMatrix>> matrixFragments;
148 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
149 if (matrixFragmentsStatus != OK) {
150 return matrixFragmentsStatus;
151 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800152 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800153 if (error && error->empty()) {
154 *error = "Cannot get framework matrix for each FCM version for unknown error.";
155 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800156 return NAME_NOT_FOUND;
157 }
158
159 Level deviceLevel = Level::UNSPECIFIED;
160
161 if (deviceManifest != nullptr) {
162 deviceLevel = deviceManifest->level();
163 }
164
165 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800166 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800167 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800168 if (shippingApi != 0u) {
169 deviceLevel = details::convertFromApiLevel(shippingApi);
170 }
171 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800172
173 if (deviceLevel == Level::UNSPECIFIED) {
174 // Cannot infer FCM version. Combine all matrices by assuming
175 // Shipping FCM Version == min(all supported FCM Versions in the framework)
176 for (auto&& pair : matrixFragments) {
177 Level fragmentLevel = pair.object.level();
178 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
179 deviceLevel = fragmentLevel;
180 }
181 }
182 }
183
184 if (deviceLevel == Level::UNSPECIFIED) {
185 // None of the fragments specify any FCM version. Should never happen except
186 // for inconsistent builds.
187 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800188 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800189 " declare FCM version.";
190 }
191 return NAME_NOT_FOUND;
192 }
193
Yifan Honge7837b12018-10-11 10:38:57 -0700194 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800195 if (combined == nullptr) {
196 return BAD_VALUE;
197 }
198 *out = std::move(*combined);
199 return OK;
200}
201
Steven Morelandeedf2d42018-04-04 16:36:27 -0700202// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700203status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700204 std::string* error) {
205 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800206 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700207 // if the directory isn't there, that's okay
208 if (err == NAME_NOT_FOUND) return OK;
209 if (err != OK) return err;
210
211 for (const std::string& file : fileNames) {
212 // Only adds HALs because all other things are added by libvintf
213 // itself for now.
214 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700215 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700216 if (err != OK) return err;
217
Yifan Hong4c6962d2019-01-30 15:50:46 -0800218 if (!manifest->addAll(&fragmentManifest, error)) {
219 if (error) {
220 error->insert(0, "Cannot add manifest fragment " + directory + file + ":");
221 }
222 return UNKNOWN_ERROR;
223 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700224 }
225
226 return OK;
227}
228
Yifan Hong5a93bf22018-01-17 18:22:32 -0800229// Priority for loading vendor manifest:
Steven Morelandeedf2d42018-04-04 16:36:27 -0700230// 1. /vendor/etc/vintf/manifest.xml + device fragments + ODM manifest (optional) + odm fragments
231// 2. /vendor/etc/vintf/manifest.xml + device fragments
232// 3. ODM manifest (optional) + odm fragments
233// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800234// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700235// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
236// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700237status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
238 status_t vendorStatus = fetchOneHalManifest(kVendorManifest, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800239 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
240 return vendorStatus;
241 }
242
Steven Morelandeedf2d42018-04-04 16:36:27 -0700243 if (vendorStatus == OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700244 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700245 if (fragmentStatus != OK) {
246 return fragmentStatus;
247 }
248 }
249
Yifan Hong5a93bf22018-01-17 18:22:32 -0800250 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700251 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800252 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
253 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800254 }
255
Yifan Hong5a93bf22018-01-17 18:22:32 -0800256 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800257 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800258 if (!out->addAll(&odmManifest, error)) {
259 if (error) {
260 error->insert(0, "Cannot add ODM manifest :");
261 }
262 return UNKNOWN_ERROR;
263 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800264 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700265 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800266 }
267
Yifan Hong12a11ac2018-01-19 13:58:32 -0800268 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800269 if (odmStatus == OK) {
270 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700271 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800272 }
273
274 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800275 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800276}
277
Yifan Hong12a11ac2018-01-19 13:58:32 -0800278// "out" is written to iff return status is OK.
279// Priority:
280// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
281// 2. /odm/etc/vintf/manifest.xml
282// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
283// 4. /odm/etc/manifest.xml
284// where:
285// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700286status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800287 status_t status;
288
Yifan Hong12a11ac2018-01-19 13:58:32 -0800289 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800290 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800291
292 if (!productModel.empty()) {
293 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700294 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800295 if (status == OK || status != NAME_NOT_FOUND) {
296 return status;
297 }
298 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800299
Yifan Hong9f78c182018-07-12 14:45:52 -0700300 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800301 if (status == OK || status != NAME_NOT_FOUND) {
302 return status;
303 }
304
Yifan Hong12a11ac2018-01-19 13:58:32 -0800305 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700306 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800307 error);
308 if (status == OK || status != NAME_NOT_FOUND) {
309 return status;
310 }
311 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800312
Yifan Hong9f78c182018-07-12 14:45:52 -0700313 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800314 if (status == OK || status != NAME_NOT_FOUND) {
315 return status;
316 }
317
318 return NAME_NOT_FOUND;
319}
320
321// Fetch one manifest.xml file. "out" is written to iff return status is OK.
322// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700323status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800324 std::string* error) {
325 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800326 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800327 if (status == OK) {
328 *out = std::move(ret);
329 }
330 return status;
331}
332
Yifan Hong9f78c182018-07-12 14:45:52 -0700333status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800334 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800335 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800336 *out = std::move(etcMatrix);
337 return OK;
338 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800339 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800340}
341
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700342// Priority:
343// 1. /system/etc/vintf/manifest.xml
344// + /system/etc/vintf/manifest/*.xml if they exist
345// + /product/etc/vintf/manifest.xml if it exists
346// + /product/etc/vintf/manifest/*.xml if they exist
347// 2. (deprecated) /system/manifest.xml
Yifan Hong9f78c182018-07-12 14:45:52 -0700348status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700349 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
350 if (systemEtcStatus == OK) {
351 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
352 if (dirStatus != OK) {
353 return dirStatus;
354 }
355
356 HalManifest productManifest;
357 auto productStatus = fetchOneHalManifest(kProductManifest, &productManifest, error);
358 if (productStatus != OK && productStatus != NAME_NOT_FOUND) {
359 return productStatus;
360 }
361 if (productStatus == OK) {
362 if (!out->addAll(&productManifest, error)) {
363 if (error) {
364 error->insert(0, "Cannot add " + kProductManifest + ":");
365 }
366 return UNKNOWN_ERROR;
367 }
368 }
369
370 return addDirectoryManifests(kProductManifestFragmentDir, out, error);
371 } else {
372 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
373 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800374 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700375
Yifan Hong12e23c22018-11-05 14:53:30 -0800376 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800377}
378
Yifan Hong9f8f6562018-11-06 16:26:20 -0800379static void appendLine(std::string* error, const std::string& message) {
380 if (error != nullptr) {
381 if (!error->empty()) *error += "\n";
382 *error += message;
383 }
384}
385
Yifan Hong73bde592019-01-22 13:30:23 -0800386status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
387 std::string* error) {
388 std::string content;
389 status_t status = getFileSystem()->fetch(path, &content, error);
390 if (status != OK) {
391 return status;
392 }
393 if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
394 if (error) {
395 error->insert(0, "Cannot parse " + path + ": ");
396 }
397 return BAD_VALUE;
398 }
399 out->name = path;
400 return OK;
401}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800402
Yifan Hong73bde592019-01-22 13:30:23 -0800403status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
404 std::string* error) {
405 std::vector<std::string> fileNames;
406
407 status_t listStatus = getFileSystem()->listFiles(kSystemVintfDir, &fileNames, error);
408 if (listStatus != OK) {
409 return listStatus;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800410 }
411 for (const std::string& fileName : fileNames) {
Yifan Hong270b5652018-01-18 18:46:01 -0800412 std::string path = kSystemVintfDir + fileName;
Yifan Hong73bde592019-01-22 13:30:23 -0800413 Named<CompatibilityMatrix> namedMatrix;
414 std::string matrixError;
415 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
416 if (matrixStatus != OK) {
417 // System manifests and matrices share the same dir. Client may not have enough
418 // permissions to read system manifests, or may not be able to parse it.
419 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
420 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800421 continue;
422 }
Yifan Hong73bde592019-01-22 13:30:23 -0800423 results->emplace_back(std::move(namedMatrix));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800424 }
425
Yifan Hong73bde592019-01-22 13:30:23 -0800426 Named<CompatibilityMatrix> productMatrix;
427 std::string productError;
428 status_t productStatus = getOneMatrix(kProductMatrix, &productMatrix, &productError);
429 if (productStatus == OK) {
430 results->emplace_back(std::move(productMatrix));
431 } else if (productStatus == NAME_NOT_FOUND) {
432 LOG(DEBUG) << "Framework Matrix: missing " << kProductMatrix;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800433 } else {
Yifan Hong73bde592019-01-22 13:30:23 -0800434 if (error) *error = std::move(productError);
435 return productStatus;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800436 }
437
Yifan Hong73bde592019-01-22 13:30:23 -0800438 if (results->empty()) {
439 if (error) {
440 *error =
441 "No framework matrices under " + kSystemVintfDir + " can be fetched or parsed.\n";
442 }
443 return NAME_NOT_FOUND;
444 }
445 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800446}
447
Yifan Hong1fb004e2017-09-26 15:04:44 -0700448std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
449 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700450 return GetInstance()->getRuntimeInfo(skipCache, flags);
451}
452std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
453 RuntimeInfo::FetchFlags flags) {
454 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700455
456 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700457 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700458 }
459
Yifan Hong9f78c182018-07-12 14:45:52 -0700460 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800461 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700462 }
463
Yifan Hongf3247982019-12-12 12:11:36 -0800464 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
465 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
466 auto manifest = getDeviceHalManifest();
467 if (!manifest) {
468 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
469 return nullptr;
470 }
471 Level level = Level::UNSPECIFIED;
472 if (manifest->kernel().has_value()) {
473 level = manifest->kernel()->level();
474 }
475 mDeviceRuntimeInfo.object->setKernelLevel(level);
476 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
477 }
478
Yifan Hong9f78c182018-07-12 14:45:52 -0700479 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700480 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700481 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700482 return nullptr;
483 }
484
Yifan Hong9f78c182018-07-12 14:45:52 -0700485 mDeviceRuntimeInfo.fetchedFlags |= flags;
486 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800487}
488
Yifan Hong143cfe62017-04-13 20:18:01 -0700489namespace details {
490
Yifan Hong143cfe62017-04-13 20:18:01 -0700491enum class ParseStatus {
492 OK,
493 PARSE_ERROR,
494 DUPLICATED_FWK_ENTRY,
495 DUPLICATED_DEV_ENTRY,
496};
497
Yifan Hong9532bd22017-04-14 15:30:52 -0700498static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700499 switch(status) {
500 case ParseStatus::OK: return "OK";
501 case ParseStatus::PARSE_ERROR: return "parse error";
502 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
503 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
504 }
505 return "";
506}
507
Yifan Hong12e23c22018-11-05 14:53:30 -0800508template <typename T>
509static ParseStatus tryParse(const std::string& xml, const XmlConverter<T>& parse,
510 VintfObjectAfterUpdate* afterUpdate) {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700511 std::shared_ptr<T> ret = std::make_shared<T>();
Yifan Hong94757062018-02-09 16:36:31 -0800512 if (!parse(ret.get(), xml, nullptr /* error */)) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700513 return ParseStatus::PARSE_ERROR;
514 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800515 if (!afterUpdate->set(ret)) {
516 if (ret->type() == SchemaType::FRAMEWORK) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700517 return ParseStatus::DUPLICATED_FWK_ENTRY;
Yifan Hong12e23c22018-11-05 14:53:30 -0800518 } else if (ret->type() == SchemaType::DEVICE) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700519 return ParseStatus::DUPLICATED_DEV_ENTRY;
520 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800521 LOG(FATAL) << "unknown SchemaType: "
522 << static_cast<std::underlying_type_t<SchemaType>>(ret->type());
Yifan Hong143cfe62017-04-13 20:18:01 -0700523 }
524 return ParseStatus::OK;
525}
526
Yifan Hong9f78c182018-07-12 14:45:52 -0700527} // namespace details
528
Yifan Hong12e23c22018-11-05 14:53:30 -0800529// Simulate applying xmls to VintfObject, then checkCompatibility as usual.
Yifan Hongaf3713e2018-11-05 13:59:18 -0800530int32_t VintfObject::checkCompatibility(const std::vector<std::string>& xmls, std::string* error,
531 CheckFlags::Type flags) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800532 VintfObjectAfterUpdate afterUpdate(this);
533 ParseStatus parseStatus = ParseStatus::OK;
Yifan Hong143cfe62017-04-13 20:18:01 -0700534
Yifan Hong143cfe62017-04-13 20:18:01 -0700535 // parse all information from package
536 for (const auto &xml : xmls) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800537 parseStatus = tryParse(xml, gHalManifestConverter, &afterUpdate);
Yifan Hong143cfe62017-04-13 20:18:01 -0700538 if (parseStatus == ParseStatus::OK) {
539 continue; // work on next one
540 }
541 if (parseStatus != ParseStatus::PARSE_ERROR) {
Yifan Hong878556e2018-07-13 13:45:30 -0700542 appendLine(error, toString(parseStatus) + " manifest");
Yifan Hong143cfe62017-04-13 20:18:01 -0700543 return ALREADY_EXISTS;
544 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800545 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &afterUpdate);
Yifan Hong143cfe62017-04-13 20:18:01 -0700546 if (parseStatus == ParseStatus::OK) {
547 continue; // work on next one
548 }
549 if (parseStatus != ParseStatus::PARSE_ERROR) {
Yifan Hong878556e2018-07-13 13:45:30 -0700550 appendLine(error, toString(parseStatus) + " matrix");
Yifan Hong143cfe62017-04-13 20:18:01 -0700551 return ALREADY_EXISTS;
552 }
Yifan Hong878556e2018-07-13 13:45:30 -0700553 appendLine(error, toString(parseStatus)); // parse error
Yifan Hong143cfe62017-04-13 20:18:01 -0700554 return BAD_VALUE;
555 }
556
Yifan Hong12e23c22018-11-05 14:53:30 -0800557 return afterUpdate.checkCompatibility(error, flags);
558}
Yifan Hong8640cd12017-05-17 12:02:28 -0700559
Yifan Hong12e23c22018-11-05 14:53:30 -0800560int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
561 status_t status = OK;
562 // null checks for files and runtime info
563 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700564 appendLine(error, "No framework manifest file from device or from update package");
565 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700566 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800567 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700568 appendLine(error, "No device manifest file from device or from update package");
569 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700570 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800571 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700572 appendLine(error, "No framework matrix file from device or from update package");
573 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700574 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800575 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700576 appendLine(error, "No device matrix file from device or from update package");
577 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700578 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800579
Yifan Hong072f12d2018-08-08 13:04:51 -0700580 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800581 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700582 appendLine(error, "No runtime info from device");
583 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800584 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700585 }
Yifan Hong878556e2018-07-13 13:45:30 -0700586 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700587
588 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800589 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800590 if (error) {
591 error->insert(0,
592 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700593 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800594 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700595 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800596 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800597 if (error) {
598 error->insert(0,
599 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700600 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800601 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700602 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800603
Yifan Hong40b7e622019-01-29 12:27:42 -0800604 CheckFlags::Type runtimeInfoCheckFlags = flags;
605 if (!!getDeviceHalManifest()->kernel()) {
606 // Use kernel from incoming OTA package, but not on the device.
607 runtimeInfoCheckFlags = runtimeInfoCheckFlags.disableKernel();
608 }
609
Yifan Hong072f12d2018-08-08 13:04:51 -0700610 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800611 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong40b7e622019-01-29 12:27:42 -0800612 runtimeInfoCheckFlags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800613 if (error) {
614 error->insert(0,
615 "Runtime info and framework compatibility matrix are incompatible: ");
616 }
617 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700618 }
619 }
620
621 return COMPATIBLE;
622}
623
Yifan Hong9f78c182018-07-12 14:45:52 -0700624namespace details {
625
Yifan Hong270b5652018-01-18 18:46:01 -0800626const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800627const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800628const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800629const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800630
631const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800632const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800633const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800634const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800635const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700636const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800637
Steven Morelandeedf2d42018-04-04 16:36:27 -0700638const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
639const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
640const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700641const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700642
Yifan Hong270b5652018-01-18 18:46:01 -0800643const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
644const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800645const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800646const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
647const std::string kOdmLegacyVintfDir = "/odm/etc/";
648const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
649
Yifan Hong69c1b112018-02-27 17:06:00 -0800650std::vector<std::string> dumpFileList() {
651 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800652 // clang-format off
653 kSystemVintfDir,
654 kVendorVintfDir,
655 kOdmVintfDir,
656 kProductVintfDir,
657 kOdmLegacyVintfDir,
658 kVendorLegacyManifest,
659 kVendorLegacyMatrix,
660 kSystemLegacyManifest,
661 kSystemLegacyMatrix,
662 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800663 };
664}
665
Yifan Hong9f78c182018-07-12 14:45:52 -0700666} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700667
Yifan Hongdb6423e2017-09-11 14:38:46 -0700668int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
Yifan Hong072f12d2018-08-08 13:04:51 -0700669 CheckFlags::Type flags) {
670 return GetInstance()->checkCompatibility(xmls, error, flags);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700671}
672
Yifan Hong9f78c182018-07-12 14:45:52 -0700673bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800674 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700675 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700676 bool isDeprecated = false;
677 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700678 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700679 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800680 }
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700681 return !isDeprecated; // continue if no deprecated instance is found.
682 });
683 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800684}
685
Yifan Honga8a8fa92018-03-20 14:42:43 -0700686// Let oldMatrixInstance = package@x.y-w::interface with instancePattern.
687// If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return
688// true iff:
689// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
690// 2. package@x.z::interface/servedInstance is in targetMatrix but
691// servedInstance is not in listInstances(package@x.z::interface)
Yifan Hong9f78c182018-07-12 14:45:52 -0700692bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800693 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700694 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700695 const std::string& package = oldMatrixInstance.package();
696 const Version& version = oldMatrixInstance.versionRange().minVer();
697 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700698
Yifan Honga8a8fa92018-03-20 14:42:43 -0700699 std::vector<std::string> instanceHint;
700 if (!oldMatrixInstance.isRegex()) {
701 instanceHint.push_back(oldMatrixInstance.exactInstance());
702 }
703
704 auto list = listInstances(package, version, interface, instanceHint);
705 for (const auto& pair : list) {
706 const std::string& servedInstance = pair.first;
707 Version servedVersion = pair.second;
708 if (!oldMatrixInstance.matchInstance(servedInstance)) {
709 continue;
710 }
711
Yifan Hongf73ba512018-01-17 15:52:30 -0800712 // Find any package@x.? in target matrix, and check if instance is in target matrix.
Yifan Hong217f47e2018-03-15 12:34:05 -0700713 bool foundInstance = false;
714 Version targetMatrixMinVer;
Yifan Hong0b1916f2019-09-10 19:04:52 -0700715 targetMatrix.forEachHidlInstanceOfPackage(package, [&](const auto& targetMatrixInstance) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700716 if (targetMatrixInstance.versionRange().majorVer == version.majorVer &&
717 targetMatrixInstance.interface() == interface &&
Yifan Honga8a8fa92018-03-20 14:42:43 -0700718 targetMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700719 targetMatrixMinVer = targetMatrixInstance.versionRange().minVer();
720 foundInstance = true;
721 }
722 return !foundInstance; // continue if not found
723 });
724 if (!foundInstance) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800725 if (error) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700726 *error = toFQNameString(package, servedVersion, interface, servedInstance) +
Steven Morelanda1b984c2018-03-09 13:09:15 -0800727 " is deprecated in compatibility matrix at FCM Version " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800728 to_string(targetMatrix.level()) + "; it should not be served.";
729 }
730 return true;
731 }
732
Yifan Hongf73ba512018-01-17 15:52:30 -0800733 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
Yifan Honga8a8fa92018-03-20 14:42:43 -0700734 bool targetVersionServed = false;
735 for (const auto& newPair :
736 listInstances(package, targetMatrixMinVer, interface, instanceHint)) {
737 if (newPair.first == servedInstance) {
738 targetVersionServed = true;
739 break;
740 }
741 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800742
743 if (!targetVersionServed) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800744 appendLine(error, toFQNameString(package, servedVersion, interface, servedInstance) +
745 " is deprecated; requires at least " +
746 to_string(targetMatrixMinVer));
Yifan Hongf73ba512018-01-17 15:52:30 -0800747 return true;
748 }
749 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700750
Yifan Hongf73ba512018-01-17 15:52:30 -0800751 return false;
752}
753
Yifan Honga8a8fa92018-03-20 14:42:43 -0700754int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700755 return GetInstance()->checkDeprecation(listInstances, error);
756}
757int32_t VintfObject::checkDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800758 std::vector<Named<CompatibilityMatrix>> matrixFragments;
759 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
760 if (matrixFragmentsStatus != OK) {
761 return matrixFragmentsStatus;
762 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800763 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800764 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800765 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800766 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800767 return NAME_NOT_FOUND;
768 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700769 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800770 if (deviceManifest == nullptr) {
771 if (error) *error = "No device manifest.";
772 return NAME_NOT_FOUND;
773 }
774 Level deviceLevel = deviceManifest->level();
775 if (deviceLevel == Level::UNSPECIFIED) {
776 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
777 return BAD_VALUE;
778 }
779
780 const CompatibilityMatrix* targetMatrix = nullptr;
781 for (const auto& namedMatrix : matrixFragments) {
782 if (namedMatrix.object.level() == deviceLevel) {
783 targetMatrix = &namedMatrix.object;
784 }
785 }
786 if (targetMatrix == nullptr) {
787 if (error)
788 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
789 return NAME_NOT_FOUND;
790 }
791
792 bool hasDeprecatedHals = false;
793 for (const auto& namedMatrix : matrixFragments) {
794 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
795 if (namedMatrix.object.level() >= deviceLevel) continue;
796
797 const auto& oldMatrix = namedMatrix.object;
798 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700799 hasDeprecatedHals |= IsHalDeprecated(hal, *targetMatrix, listInstances, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800800 }
801 }
802
803 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
804}
805
806int32_t VintfObject::CheckDeprecation(std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700807 return GetInstance()->checkDeprecation(error);
808}
809int32_t VintfObject::checkDeprecation(std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800810 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700811 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700812 ListInstances inManifest =
813 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
814 const std::vector<std::string>& /* hintInstances */) {
815 std::vector<std::pair<std::string, Version>> ret;
816 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700817 HalFormat::HIDL, package, version, interface,
818 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700819 ret.push_back(
820 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
821 return true;
822 });
823 return ret;
824 };
Yifan Hong9f78c182018-07-12 14:45:52 -0700825 return checkDeprecation(inManifest, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800826}
Yifan Hong3daec812017-02-27 18:49:11 -0800827
Yifan Hong1e8febd2019-08-07 16:17:19 -0700828std::optional<KernelRequirement> VintfObject::getCompatibleKernelRequirement(std::string* error) {
829 auto matrix = getFrameworkCompatibilityMatrix();
830 if (!matrix) {
831 if (error) *error = "Cannot retrieve framework compatibility matrix";
832 return std::nullopt;
833 }
834 auto runtime_info = getRuntimeInfo();
835 if (!runtime_info) {
836 if (error) *error = "Cannot retrieve runtime information";
837 return std::nullopt;
838 }
839 auto reqs =
840 runtime_info->mKernel.getMatchedKernelRequirements(matrix->framework.mKernels, error);
841 if (reqs.empty()) {
842 if (error) error->insert(0, "Cannot find any matched kernel requirements: ");
843 return std::nullopt;
844 }
845 for (const MatrixKernel* matrixKernel : reqs) {
846 if (matrixKernel->conditions().empty()) {
847 return KernelRequirement(matrixKernel->minLts(),
848 matrix->getSourceMatrixLevel(matrixKernel));
849 }
850 }
851 KernelRequirement ret(reqs[0]->minLts(), matrix->getSourceMatrixLevel(reqs[0]));
852 LOG(ERROR) << "Cannot find kernel requirement with empty conditions; "
853 << "this should not happen. Returning (" << ret.minLts() << ", " << ret.level()
854 << ") as a heuristic.";
855 return ret;
856}
857
Yifan Hong9f78c182018-07-12 14:45:52 -0700858const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
859 return mFileSystem;
860}
861
Yifan Hong9f78c182018-07-12 14:45:52 -0700862const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
863 return mPropertyFetcher;
864}
865
Yifan Hongd038b2b2018-11-27 13:57:56 -0800866const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700867 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700868}
869
Yifan Hong78f5b572018-11-27 14:05:03 -0800870// make_unique does not work because VintfObject constructor is private.
871VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
872
873VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
874 mObject->mFileSystem = std::move(e);
875 return *this;
876}
877
878VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
879 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
880 mObject->mRuntimeInfoFactory = std::move(e);
881 return *this;
882}
883
884VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
885 std::unique_ptr<PropertyFetcher>&& e) {
886 mObject->mPropertyFetcher = std::move(e);
887 return *this;
888}
889
890std::unique_ptr<VintfObject> VintfObject::Builder::build() {
891 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
892 if (!mObject->mRuntimeInfoFactory)
893 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
894 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
895 return std::move(mObject);
896}
897
Yifan Hong3daec812017-02-27 18:49:11 -0800898} // namespace vintf
899} // namespace android