blob: 4f47622d38f2a632e9a9c0b8962049229b4a8ce5 [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>
Yifan Hong6e32a5f2020-03-12 16:06:27 -070026#include <android-base/result.h>
Yifan Hongb02d87d2019-12-19 11:15:27 -080027#include <android-base/strings.h>
Yifan Hong60217032018-01-08 16:19:42 -080028
Yifan Hong12e23c22018-11-05 14:53:30 -080029#include "CompatibilityMatrix.h"
Yifan Hong12e23c22018-11-05 14:53:30 -080030#include "parse_string.h"
31#include "parse_xml.h"
32#include "utils.h"
33
Yifan Hong60217032018-01-08 16:19:42 -080034using std::placeholders::_1;
35using std::placeholders::_2;
36
Yifan Hong3daec812017-02-27 18:49:11 -080037namespace android {
38namespace vintf {
39
Yifan Hong270b5652018-01-18 18:46:01 -080040using namespace details;
41
Yifan Hong9f78c182018-07-12 14:45:52 -070042#ifdef LIBVINTF_TARGET
43static constexpr bool kIsTarget = true;
44#else
45static constexpr bool kIsTarget = false;
46#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070047
Yifan Hong3daec812017-02-27 18:49:11 -080048template <typename T, typename F>
Yifan Hongfc73edf2017-08-29 11:39:07 -070049static std::shared_ptr<const T> Get(
50 LockedSharedPtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070051 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080052 const F &fetchAllInformation) {
53 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080054 if (skipCache || !ptr->fetchedOnce) {
Yifan Hong3daec812017-02-27 18:49:11 -080055 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080056 std::string error;
57 if (fetchAllInformation(ptr->object.get(), &error) != OK) {
58 LOG(WARNING) << error;
Yifan Hong3daec812017-02-27 18:49:11 -080059 ptr->object = nullptr; // frees the old object
60 }
Yifan Hong7219ba12018-01-08 16:23:49 -080061 ptr->fetchedOnce = true;
Yifan Hong3daec812017-02-27 18:49:11 -080062 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070063 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080064}
65
Yifan Hong9f78c182018-07-12 14:45:52 -070066static std::unique_ptr<FileSystem> createDefaultFileSystem() {
67 std::unique_ptr<FileSystem> fileSystem;
68 if (kIsTarget) {
69 fileSystem = std::make_unique<details::FileSystemImpl>();
70 } else {
71 fileSystem = std::make_unique<details::FileSystemNoOp>();
72 }
73 return fileSystem;
74}
75
76static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
77 std::unique_ptr<PropertyFetcher> propertyFetcher;
78 if (kIsTarget) {
79 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
80 } else {
81 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
82 }
83 return propertyFetcher;
84}
85
Yifan Hong9f78c182018-07-12 14:45:52 -070086std::shared_ptr<VintfObject> VintfObject::GetInstance() {
Steven Moreland954c4642020-02-26 17:03:37 -080087 static details::LockedSharedPtr<VintfObject> sInstance{};
Yifan Hong9f78c182018-07-12 14:45:52 -070088 std::unique_lock<std::mutex> lock(sInstance.mutex);
89 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -080090 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -070091 }
92 return sInstance.object;
93}
94
Yifan Hongfc73edf2017-08-29 11:39:07 -070095std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -070096 return GetInstance()->getDeviceHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -080097}
98
Yifan Hong9f78c182018-07-12 14:45:52 -070099std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
100 return Get(&mDeviceManifest, skipCache,
101 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
102}
103
Yifan Hongfc73edf2017-08-29 11:39:07 -0700104std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700105 return GetInstance()->getFrameworkHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800106}
107
Yifan Hong9f78c182018-07-12 14:45:52 -0700108std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
109 return Get(&mFrameworkManifest, skipCache,
110 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
111}
Yifan Hong2272bf82017-04-28 14:37:56 -0700112
Yifan Hongfc73edf2017-08-29 11:39:07 -0700113std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700114 return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
Yifan Hong2272bf82017-04-28 14:37:56 -0700115}
116
Yifan Hong9f78c182018-07-12 14:45:52 -0700117std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
118 bool skipCache) {
119 return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
120}
121
Yifan Hongfc73edf2017-08-29 11:39:07 -0700122std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700123 return GetInstance()->getFrameworkCompatibilityMatrix(skipCache);
124}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800125
Yifan Hong9f78c182018-07-12 14:45:52 -0700126std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix(
127 bool skipCache) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800128 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700129 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800130
Yifan Hong9f78c182018-07-12 14:45:52 -0700131 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800132
133 auto combined =
Yifan Hong9f78c182018-07-12 14:45:52 -0700134 Get(&mCombinedFrameworkMatrix, skipCache,
135 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800136 if (combined != nullptr) {
137 return combined;
138 }
139
Yifan Hong9f78c182018-07-12 14:45:52 -0700140 return Get(&mFrameworkMatrix, skipCache,
Yifan Hong12e23c22018-11-05 14:53:30 -0800141 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700142 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700143}
144
Yifan Hong9f78c182018-07-12 14:45:52 -0700145status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800146 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
147 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800148 std::vector<Named<CompatibilityMatrix>> matrixFragments;
149 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
150 if (matrixFragmentsStatus != OK) {
151 return matrixFragmentsStatus;
152 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800153 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800154 if (error && error->empty()) {
155 *error = "Cannot get framework matrix for each FCM version for unknown error.";
156 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800157 return NAME_NOT_FOUND;
158 }
159
160 Level deviceLevel = Level::UNSPECIFIED;
161
162 if (deviceManifest != nullptr) {
163 deviceLevel = deviceManifest->level();
164 }
165
166 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800167 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800168 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800169 if (shippingApi != 0u) {
170 deviceLevel = details::convertFromApiLevel(shippingApi);
171 }
172 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800173
174 if (deviceLevel == Level::UNSPECIFIED) {
175 // Cannot infer FCM version. Combine all matrices by assuming
176 // Shipping FCM Version == min(all supported FCM Versions in the framework)
177 for (auto&& pair : matrixFragments) {
178 Level fragmentLevel = pair.object.level();
179 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
180 deviceLevel = fragmentLevel;
181 }
182 }
183 }
184
185 if (deviceLevel == Level::UNSPECIFIED) {
186 // None of the fragments specify any FCM version. Should never happen except
187 // for inconsistent builds.
188 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800189 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800190 " declare FCM version.";
191 }
192 return NAME_NOT_FOUND;
193 }
194
Yifan Honge7837b12018-10-11 10:38:57 -0700195 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800196 if (combined == nullptr) {
197 return BAD_VALUE;
198 }
199 *out = std::move(*combined);
200 return OK;
201}
202
Steven Morelandeedf2d42018-04-04 16:36:27 -0700203// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700204status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700205 std::string* error) {
206 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800207 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700208 // if the directory isn't there, that's okay
209 if (err == NAME_NOT_FOUND) return OK;
210 if (err != OK) return err;
211
212 for (const std::string& file : fileNames) {
213 // Only adds HALs because all other things are added by libvintf
214 // itself for now.
215 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700216 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700217 if (err != OK) return err;
218
Yifan Hong4c6962d2019-01-30 15:50:46 -0800219 if (!manifest->addAll(&fragmentManifest, error)) {
220 if (error) {
221 error->insert(0, "Cannot add manifest fragment " + directory + file + ":");
222 }
223 return UNKNOWN_ERROR;
224 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700225 }
226
227 return OK;
228}
229
Yifan Hong5a93bf22018-01-17 18:22:32 -0800230// Priority for loading vendor manifest:
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800231// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
232// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700233// 3. ODM manifest (optional) + odm fragments
234// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800235// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700236// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
237// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700238status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800239 HalManifest vendorManifest;
240 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800241 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
242 return vendorStatus;
243 }
244
Steven Morelandeedf2d42018-04-04 16:36:27 -0700245 if (vendorStatus == OK) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800246 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700247 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700248 if (fragmentStatus != OK) {
249 return fragmentStatus;
250 }
251 }
252
Yifan Hong5a93bf22018-01-17 18:22:32 -0800253 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700254 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800255 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
256 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800257 }
258
Yifan Hong5a93bf22018-01-17 18:22:32 -0800259 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800260 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800261 if (!out->addAll(&odmManifest, error)) {
262 if (error) {
263 error->insert(0, "Cannot add ODM manifest :");
264 }
265 return UNKNOWN_ERROR;
266 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800267 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700268 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800269 }
270
Yifan Hong12a11ac2018-01-19 13:58:32 -0800271 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800272 if (odmStatus == OK) {
273 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700274 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800275 }
276
277 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800278 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800279}
280
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800281// Priority:
282// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
283// 2. /vendor/etc/vintf/manifest.xml
284// where:
285// {vendorSku} is the value of ro.boot.product.vendor.sku
286status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
287 status_t status;
288
289 std::string vendorSku;
290 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
291
292 if (!vendorSku.empty()) {
293 status =
294 fetchOneHalManifest(kVendorVintfDir + "manifest_" + vendorSku + ".xml", out, error);
295 if (status == OK || status != NAME_NOT_FOUND) {
296 return status;
297 }
298 }
299
300 status = fetchOneHalManifest(kVendorManifest, out, error);
301 if (status == OK || status != NAME_NOT_FOUND) {
302 return status;
303 }
304
305 return NAME_NOT_FOUND;
306}
307
Yifan Hong12a11ac2018-01-19 13:58:32 -0800308// "out" is written to iff return status is OK.
309// Priority:
310// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
311// 2. /odm/etc/vintf/manifest.xml
312// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
313// 4. /odm/etc/manifest.xml
314// where:
315// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700316status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800317 status_t status;
318
Yifan Hong12a11ac2018-01-19 13:58:32 -0800319 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800320 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800321
322 if (!productModel.empty()) {
323 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700324 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800325 if (status == OK || status != NAME_NOT_FOUND) {
326 return status;
327 }
328 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800329
Yifan Hong9f78c182018-07-12 14:45:52 -0700330 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800331 if (status == OK || status != NAME_NOT_FOUND) {
332 return status;
333 }
334
Yifan Hong12a11ac2018-01-19 13:58:32 -0800335 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700336 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800337 error);
338 if (status == OK || status != NAME_NOT_FOUND) {
339 return status;
340 }
341 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800342
Yifan Hong9f78c182018-07-12 14:45:52 -0700343 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800344 if (status == OK || status != NAME_NOT_FOUND) {
345 return status;
346 }
347
348 return NAME_NOT_FOUND;
349}
350
351// Fetch one manifest.xml file. "out" is written to iff return status is OK.
352// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700353status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800354 std::string* error) {
355 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800356 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800357 if (status == OK) {
358 *out = std::move(ret);
359 }
360 return status;
361}
362
Yifan Hong9f78c182018-07-12 14:45:52 -0700363status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800364 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800365 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800366 *out = std::move(etcMatrix);
367 return OK;
368 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800369 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800370}
371
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700372// Priority:
373// 1. /system/etc/vintf/manifest.xml
374// + /system/etc/vintf/manifest/*.xml if they exist
375// + /product/etc/vintf/manifest.xml if it exists
376// + /product/etc/vintf/manifest/*.xml if they exist
377// 2. (deprecated) /system/manifest.xml
Yifan Hong9f78c182018-07-12 14:45:52 -0700378status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700379 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
380 if (systemEtcStatus == OK) {
381 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
382 if (dirStatus != OK) {
383 return dirStatus;
384 }
385
386 HalManifest productManifest;
387 auto productStatus = fetchOneHalManifest(kProductManifest, &productManifest, error);
388 if (productStatus != OK && productStatus != NAME_NOT_FOUND) {
389 return productStatus;
390 }
391 if (productStatus == OK) {
392 if (!out->addAll(&productManifest, error)) {
393 if (error) {
394 error->insert(0, "Cannot add " + kProductManifest + ":");
395 }
396 return UNKNOWN_ERROR;
397 }
398 }
399
400 return addDirectoryManifests(kProductManifestFragmentDir, out, error);
401 } else {
402 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
403 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800404 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700405
Yifan Hong12e23c22018-11-05 14:53:30 -0800406 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800407}
408
Yifan Hong9f8f6562018-11-06 16:26:20 -0800409static void appendLine(std::string* error, const std::string& message) {
410 if (error != nullptr) {
411 if (!error->empty()) *error += "\n";
412 *error += message;
413 }
414}
415
Yifan Hong73bde592019-01-22 13:30:23 -0800416status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
417 std::string* error) {
418 std::string content;
419 status_t status = getFileSystem()->fetch(path, &content, error);
420 if (status != OK) {
421 return status;
422 }
423 if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
424 if (error) {
425 error->insert(0, "Cannot parse " + path + ": ");
426 }
427 return BAD_VALUE;
428 }
429 out->name = path;
430 return OK;
431}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800432
Yifan Hong73bde592019-01-22 13:30:23 -0800433status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
434 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800435 std::vector<std::string> dirs = {
436 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800437 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800438 kProductVintfDir,
439 };
440 for (const auto& dir : dirs) {
441 std::vector<std::string> fileNames;
442 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
443 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800444 continue;
445 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800446 if (listStatus != OK) {
447 return listStatus;
448 }
449 for (const std::string& fileName : fileNames) {
450 std::string path = dir + fileName;
451 Named<CompatibilityMatrix> namedMatrix;
452 std::string matrixError;
453 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
454 if (matrixStatus != OK) {
455 // Manifests and matrices share the same dir. Client may not have enough
456 // permissions to read system manifests, or may not be able to parse it.
457 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
458 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
459 continue;
460 }
461 results->emplace_back(std::move(namedMatrix));
462 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800463
Yifan Hongb02d87d2019-12-19 11:15:27 -0800464 if (dir == kSystemVintfDir && results->empty()) {
465 if (error) {
466 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
467 }
468 return NAME_NOT_FOUND;
469 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800470 }
471
Yifan Hong73bde592019-01-22 13:30:23 -0800472 if (results->empty()) {
473 if (error) {
474 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800475 "No framework matrices can be fetched or parsed. "
476 "The following directories are searched:\n " +
477 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800478 }
479 return NAME_NOT_FOUND;
480 }
481 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800482}
483
Yifan Hong1fb004e2017-09-26 15:04:44 -0700484std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
485 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700486 return GetInstance()->getRuntimeInfo(skipCache, flags);
487}
488std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
489 RuntimeInfo::FetchFlags flags) {
490 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700491
492 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700493 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700494 }
495
Yifan Hong9f78c182018-07-12 14:45:52 -0700496 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800497 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700498 }
499
Yifan Hongf3247982019-12-12 12:11:36 -0800500 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
501 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
502 auto manifest = getDeviceHalManifest();
503 if (!manifest) {
504 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
505 return nullptr;
506 }
507 Level level = Level::UNSPECIFIED;
508 if (manifest->kernel().has_value()) {
509 level = manifest->kernel()->level();
510 }
511 mDeviceRuntimeInfo.object->setKernelLevel(level);
512 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
513 }
514
Yifan Hong9f78c182018-07-12 14:45:52 -0700515 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700516 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700517 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700518 return nullptr;
519 }
520
Yifan Hong9f78c182018-07-12 14:45:52 -0700521 mDeviceRuntimeInfo.fetchedFlags |= flags;
522 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800523}
524
Yifan Hong12e23c22018-11-05 14:53:30 -0800525int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
526 status_t status = OK;
527 // null checks for files and runtime info
528 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700529 appendLine(error, "No framework manifest file from device or from update package");
530 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700531 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800532 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700533 appendLine(error, "No device manifest file from device or from update package");
534 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700535 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800536 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700537 appendLine(error, "No framework matrix file from device or from update package");
538 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700539 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800540 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700541 appendLine(error, "No device matrix file from device or from update package");
542 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700543 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800544
Yifan Hong072f12d2018-08-08 13:04:51 -0700545 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800546 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700547 appendLine(error, "No runtime info from device");
548 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800549 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700550 }
Yifan Hong878556e2018-07-13 13:45:30 -0700551 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700552
553 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800554 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800555 if (error) {
556 error->insert(0,
557 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700558 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800559 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700560 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800561 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800562 if (error) {
563 error->insert(0,
564 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700565 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800566 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700567 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800568
Yifan Hong072f12d2018-08-08 13:04:51 -0700569 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800570 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800571 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800572 if (error) {
573 error->insert(0,
574 "Runtime info and framework compatibility matrix are incompatible: ");
575 }
576 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700577 }
578 }
579
580 return COMPATIBLE;
581}
582
Yifan Hong9f78c182018-07-12 14:45:52 -0700583namespace details {
584
Yifan Hong270b5652018-01-18 18:46:01 -0800585const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800586const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800587const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800588const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800589const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800590
591const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800592const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800593const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800594const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800595const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700596const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800597
Steven Morelandeedf2d42018-04-04 16:36:27 -0700598const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
599const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
600const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700601const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700602
Yifan Hong270b5652018-01-18 18:46:01 -0800603const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
604const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800605const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800606const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
607const std::string kOdmLegacyVintfDir = "/odm/etc/";
608const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
609
Yifan Hong69c1b112018-02-27 17:06:00 -0800610std::vector<std::string> dumpFileList() {
611 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800612 // clang-format off
613 kSystemVintfDir,
614 kVendorVintfDir,
615 kOdmVintfDir,
616 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800617 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800618 kOdmLegacyVintfDir,
619 kVendorLegacyManifest,
620 kVendorLegacyMatrix,
621 kSystemLegacyManifest,
622 kSystemLegacyMatrix,
623 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800624 };
625}
626
Yifan Hong9f78c182018-07-12 14:45:52 -0700627} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700628
Yifan Hong9f78c182018-07-12 14:45:52 -0700629bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800630 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700631 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700632 bool isDeprecated = false;
633 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700634 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700635 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800636 }
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700637 return !isDeprecated; // continue if no deprecated instance is found.
638 });
639 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800640}
641
Yifan Honga8a8fa92018-03-20 14:42:43 -0700642// Let oldMatrixInstance = package@x.y-w::interface with instancePattern.
643// If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return
644// true iff:
645// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
646// 2. package@x.z::interface/servedInstance is in targetMatrix but
647// servedInstance is not in listInstances(package@x.z::interface)
Yifan Hong9f78c182018-07-12 14:45:52 -0700648bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800649 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700650 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700651 const std::string& package = oldMatrixInstance.package();
652 const Version& version = oldMatrixInstance.versionRange().minVer();
653 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700654
Yifan Honga8a8fa92018-03-20 14:42:43 -0700655 std::vector<std::string> instanceHint;
656 if (!oldMatrixInstance.isRegex()) {
657 instanceHint.push_back(oldMatrixInstance.exactInstance());
658 }
659
660 auto list = listInstances(package, version, interface, instanceHint);
661 for (const auto& pair : list) {
662 const std::string& servedInstance = pair.first;
663 Version servedVersion = pair.second;
664 if (!oldMatrixInstance.matchInstance(servedInstance)) {
665 continue;
666 }
667
Yifan Hongf73ba512018-01-17 15:52:30 -0800668 // Find any package@x.? in target matrix, and check if instance is in target matrix.
Yifan Hong217f47e2018-03-15 12:34:05 -0700669 bool foundInstance = false;
670 Version targetMatrixMinVer;
Yifan Hong0b1916f2019-09-10 19:04:52 -0700671 targetMatrix.forEachHidlInstanceOfPackage(package, [&](const auto& targetMatrixInstance) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700672 if (targetMatrixInstance.versionRange().majorVer == version.majorVer &&
673 targetMatrixInstance.interface() == interface &&
Yifan Honga8a8fa92018-03-20 14:42:43 -0700674 targetMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700675 targetMatrixMinVer = targetMatrixInstance.versionRange().minVer();
676 foundInstance = true;
677 }
678 return !foundInstance; // continue if not found
679 });
680 if (!foundInstance) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800681 if (error) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700682 *error = toFQNameString(package, servedVersion, interface, servedInstance) +
Steven Morelanda1b984c2018-03-09 13:09:15 -0800683 " is deprecated in compatibility matrix at FCM Version " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800684 to_string(targetMatrix.level()) + "; it should not be served.";
685 }
686 return true;
687 }
688
Yifan Hongf73ba512018-01-17 15:52:30 -0800689 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
Yifan Honga8a8fa92018-03-20 14:42:43 -0700690 bool targetVersionServed = false;
691 for (const auto& newPair :
692 listInstances(package, targetMatrixMinVer, interface, instanceHint)) {
693 if (newPair.first == servedInstance) {
694 targetVersionServed = true;
695 break;
696 }
697 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800698
699 if (!targetVersionServed) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800700 appendLine(error, toFQNameString(package, servedVersion, interface, servedInstance) +
701 " is deprecated; requires at least " +
702 to_string(targetMatrixMinVer));
Yifan Hongf73ba512018-01-17 15:52:30 -0800703 return true;
704 }
705 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700706
Yifan Hongf73ba512018-01-17 15:52:30 -0800707 return false;
708}
709
Yifan Honga8a8fa92018-03-20 14:42:43 -0700710int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700711 return GetInstance()->checkDeprecation(listInstances, error);
712}
713int32_t VintfObject::checkDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800714 std::vector<Named<CompatibilityMatrix>> matrixFragments;
715 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
716 if (matrixFragmentsStatus != OK) {
717 return matrixFragmentsStatus;
718 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800719 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800720 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800721 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800722 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800723 return NAME_NOT_FOUND;
724 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700725 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800726 if (deviceManifest == nullptr) {
727 if (error) *error = "No device manifest.";
728 return NAME_NOT_FOUND;
729 }
730 Level deviceLevel = deviceManifest->level();
731 if (deviceLevel == Level::UNSPECIFIED) {
732 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
733 return BAD_VALUE;
734 }
735
736 const CompatibilityMatrix* targetMatrix = nullptr;
737 for (const auto& namedMatrix : matrixFragments) {
738 if (namedMatrix.object.level() == deviceLevel) {
739 targetMatrix = &namedMatrix.object;
740 }
741 }
742 if (targetMatrix == nullptr) {
743 if (error)
744 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
745 return NAME_NOT_FOUND;
746 }
747
748 bool hasDeprecatedHals = false;
749 for (const auto& namedMatrix : matrixFragments) {
750 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
751 if (namedMatrix.object.level() >= deviceLevel) continue;
752
753 const auto& oldMatrix = namedMatrix.object;
754 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700755 hasDeprecatedHals |= IsHalDeprecated(hal, *targetMatrix, listInstances, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800756 }
757 }
758
759 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
760}
761
762int32_t VintfObject::CheckDeprecation(std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700763 return GetInstance()->checkDeprecation(error);
764}
765int32_t VintfObject::checkDeprecation(std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800766 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700767 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700768 ListInstances inManifest =
769 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
770 const std::vector<std::string>& /* hintInstances */) {
771 std::vector<std::pair<std::string, Version>> ret;
772 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700773 HalFormat::HIDL, package, version, interface,
774 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700775 ret.push_back(
776 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
777 return true;
778 });
779 return ret;
780 };
Yifan Hong9f78c182018-07-12 14:45:52 -0700781 return checkDeprecation(inManifest, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800782}
Yifan Hong3daec812017-02-27 18:49:11 -0800783
Yifan Hong378c4082019-12-23 13:10:57 -0800784Level VintfObject::getKernelLevel(std::string* error) {
785 auto manifest = getDeviceHalManifest();
786 if (!manifest) {
787 if (error) *error = "Cannot retrieve device manifest.";
788 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700789 }
Yifan Hong378c4082019-12-23 13:10:57 -0800790 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
791 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700792 }
Yifan Hong378c4082019-12-23 13:10:57 -0800793 if (error) *error = "Device manifest does not specify kernel FCM version.";
794 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700795}
796
Yifan Hong9f78c182018-07-12 14:45:52 -0700797const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
798 return mFileSystem;
799}
800
Yifan Hong9f78c182018-07-12 14:45:52 -0700801const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
802 return mPropertyFetcher;
803}
804
Yifan Hongd038b2b2018-11-27 13:57:56 -0800805const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700806 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700807}
808
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700809android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
810 std::vector<Named<CompatibilityMatrix>> matrixFragments;
811 std::string error;
812 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
813 if (status != OK) {
814 return android::base::Error(-status)
815 << "Cannot get all framework matrix fragments: " << error;
816 }
817 for (const auto& namedMatrix : matrixFragments) {
818 // Returns true if product matrix exists.
819 if (android::base::StartsWith(namedMatrix.name, kProductVintfDir)) {
820 return true;
821 }
Yifan Hong238c6dc2020-03-12 22:56:16 -0700822 // Returns true if system_ext matrix exists.
823 if (android::base::StartsWith(namedMatrix.name, kSystemExtVintfDir)) {
824 return true;
825 }
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700826 // Returns true if device system matrix exists.
827 if (android::base::StartsWith(namedMatrix.name, kSystemVintfDir) &&
828 namedMatrix.object.level() == Level::UNSPECIFIED &&
829 !namedMatrix.object.getHals().empty()) {
830 return true;
831 }
832 }
833 return false;
834}
835
836android::base::Result<void> VintfObject::checkUnusedHals() {
837 auto matrix = getFrameworkCompatibilityMatrix();
838 if (matrix == nullptr) {
839 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
840 }
841 auto manifest = getDeviceHalManifest();
842 if (manifest == nullptr) {
843 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
844 }
845 auto unused = manifest->checkUnusedHals(*matrix);
846 if (!unused.empty()) {
847 return android::base::Error()
848 << "The following instances are in the device manifest but "
849 << "not specified in framework compatibility matrix: \n"
850 << " " << android::base::Join(unused, "\n ") << "\n"
851 << "Suggested fix:\n"
852 << "1. Check for any typos in device manifest or framework compatibility "
853 << "matrices with FCM version >= " << matrix->level() << ".\n"
854 << "2. Add them to any framework compatibility matrix with FCM "
855 << "version >= " << matrix->level() << " where applicable.\n"
856 << "3. Add them to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
857 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
858 }
859 return {};
860}
861
Yifan Hong78f5b572018-11-27 14:05:03 -0800862// make_unique does not work because VintfObject constructor is private.
863VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
864
865VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
866 mObject->mFileSystem = std::move(e);
867 return *this;
868}
869
870VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
871 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
872 mObject->mRuntimeInfoFactory = std::move(e);
873 return *this;
874}
875
876VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
877 std::unique_ptr<PropertyFetcher>&& e) {
878 mObject->mPropertyFetcher = std::move(e);
879 return *this;
880}
881
882std::unique_ptr<VintfObject> VintfObject::Builder::build() {
883 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
884 if (!mObject->mRuntimeInfoFactory)
885 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
886 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
887 return std::move(mObject);
888}
889
Yifan Hong3daec812017-02-27 18:49:11 -0800890} // namespace vintf
891} // namespace android