blob: 320f97f2d475c54cafcba473955d2ff74d188f63 [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 Hongff1a25c2020-03-17 14:09:10 -070028#include <hidl/metadata.h>
Yifan Hong60217032018-01-08 16:19:42 -080029
Yifan Hong12e23c22018-11-05 14:53:30 -080030#include "CompatibilityMatrix.h"
Yifan Hong12e23c22018-11-05 14:53:30 -080031#include "parse_string.h"
32#include "parse_xml.h"
33#include "utils.h"
34
Yifan Hong60217032018-01-08 16:19:42 -080035using std::placeholders::_1;
36using std::placeholders::_2;
37
Yifan Hong3daec812017-02-27 18:49:11 -080038namespace android {
39namespace vintf {
40
Yifan Hong270b5652018-01-18 18:46:01 -080041using namespace details;
42
Yifan Hong9f78c182018-07-12 14:45:52 -070043#ifdef LIBVINTF_TARGET
44static constexpr bool kIsTarget = true;
45#else
46static constexpr bool kIsTarget = false;
47#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070048
Yifan Hong3daec812017-02-27 18:49:11 -080049template <typename T, typename F>
Yifan Hongfc73edf2017-08-29 11:39:07 -070050static std::shared_ptr<const T> Get(
51 LockedSharedPtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070052 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080053 const F &fetchAllInformation) {
54 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080055 if (skipCache || !ptr->fetchedOnce) {
Yifan Hong3daec812017-02-27 18:49:11 -080056 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080057 std::string error;
58 if (fetchAllInformation(ptr->object.get(), &error) != OK) {
59 LOG(WARNING) << error;
Yifan Hong3daec812017-02-27 18:49:11 -080060 ptr->object = nullptr; // frees the old object
61 }
Yifan Hong7219ba12018-01-08 16:23:49 -080062 ptr->fetchedOnce = true;
Yifan Hong3daec812017-02-27 18:49:11 -080063 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070064 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080065}
66
Yifan Hong9f78c182018-07-12 14:45:52 -070067static std::unique_ptr<FileSystem> createDefaultFileSystem() {
68 std::unique_ptr<FileSystem> fileSystem;
69 if (kIsTarget) {
70 fileSystem = std::make_unique<details::FileSystemImpl>();
71 } else {
72 fileSystem = std::make_unique<details::FileSystemNoOp>();
73 }
74 return fileSystem;
75}
76
77static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
78 std::unique_ptr<PropertyFetcher> propertyFetcher;
79 if (kIsTarget) {
80 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
81 } else {
82 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
83 }
84 return propertyFetcher;
85}
86
Yifan Hong9f78c182018-07-12 14:45:52 -070087std::shared_ptr<VintfObject> VintfObject::GetInstance() {
Steven Moreland954c4642020-02-26 17:03:37 -080088 static details::LockedSharedPtr<VintfObject> sInstance{};
Yifan Hong9f78c182018-07-12 14:45:52 -070089 std::unique_lock<std::mutex> lock(sInstance.mutex);
90 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -080091 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -070092 }
93 return sInstance.object;
94}
95
Yifan Hongfc73edf2017-08-29 11:39:07 -070096std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -070097 return GetInstance()->getDeviceHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -080098}
99
Yifan Hong9f78c182018-07-12 14:45:52 -0700100std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
101 return Get(&mDeviceManifest, skipCache,
102 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
103}
104
Yifan Hongfc73edf2017-08-29 11:39:07 -0700105std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700106 return GetInstance()->getFrameworkHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800107}
108
Yifan Hong9f78c182018-07-12 14:45:52 -0700109std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
110 return Get(&mFrameworkManifest, skipCache,
111 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
112}
Yifan Hong2272bf82017-04-28 14:37:56 -0700113
Yifan Hongfc73edf2017-08-29 11:39:07 -0700114std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700115 return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
Yifan Hong2272bf82017-04-28 14:37:56 -0700116}
117
Yifan Hong9f78c182018-07-12 14:45:52 -0700118std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
119 bool skipCache) {
120 return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
121}
122
Yifan Hongfc73edf2017-08-29 11:39:07 -0700123std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700124 return GetInstance()->getFrameworkCompatibilityMatrix(skipCache);
125}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800126
Yifan Hong9f78c182018-07-12 14:45:52 -0700127std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix(
128 bool skipCache) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800129 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700130 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800131
Yifan Hong9f78c182018-07-12 14:45:52 -0700132 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800133
134 auto combined =
Yifan Hong9f78c182018-07-12 14:45:52 -0700135 Get(&mCombinedFrameworkMatrix, skipCache,
136 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800137 if (combined != nullptr) {
138 return combined;
139 }
140
Yifan Hong9f78c182018-07-12 14:45:52 -0700141 return Get(&mFrameworkMatrix, skipCache,
Yifan Hong12e23c22018-11-05 14:53:30 -0800142 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700143 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700144}
145
Yifan Hong9f78c182018-07-12 14:45:52 -0700146status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800147 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
148 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800149 std::vector<Named<CompatibilityMatrix>> matrixFragments;
150 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
151 if (matrixFragmentsStatus != OK) {
152 return matrixFragmentsStatus;
153 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800154 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800155 if (error && error->empty()) {
156 *error = "Cannot get framework matrix for each FCM version for unknown error.";
157 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800158 return NAME_NOT_FOUND;
159 }
160
161 Level deviceLevel = Level::UNSPECIFIED;
162
163 if (deviceManifest != nullptr) {
164 deviceLevel = deviceManifest->level();
165 }
166
167 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800168 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800169 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800170 if (shippingApi != 0u) {
171 deviceLevel = details::convertFromApiLevel(shippingApi);
172 }
173 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800174
175 if (deviceLevel == Level::UNSPECIFIED) {
176 // Cannot infer FCM version. Combine all matrices by assuming
177 // Shipping FCM Version == min(all supported FCM Versions in the framework)
178 for (auto&& pair : matrixFragments) {
179 Level fragmentLevel = pair.object.level();
180 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
181 deviceLevel = fragmentLevel;
182 }
183 }
184 }
185
186 if (deviceLevel == Level::UNSPECIFIED) {
187 // None of the fragments specify any FCM version. Should never happen except
188 // for inconsistent builds.
189 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800190 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800191 " declare FCM version.";
192 }
193 return NAME_NOT_FOUND;
194 }
195
Yifan Honge7837b12018-10-11 10:38:57 -0700196 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800197 if (combined == nullptr) {
198 return BAD_VALUE;
199 }
200 *out = std::move(*combined);
201 return OK;
202}
203
Steven Morelandeedf2d42018-04-04 16:36:27 -0700204// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700205status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700206 std::string* error) {
207 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800208 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700209 // if the directory isn't there, that's okay
210 if (err == NAME_NOT_FOUND) return OK;
211 if (err != OK) return err;
212
213 for (const std::string& file : fileNames) {
214 // Only adds HALs because all other things are added by libvintf
215 // itself for now.
216 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700217 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700218 if (err != OK) return err;
219
Yifan Hong4c6962d2019-01-30 15:50:46 -0800220 if (!manifest->addAll(&fragmentManifest, error)) {
221 if (error) {
222 error->insert(0, "Cannot add manifest fragment " + directory + file + ":");
223 }
224 return UNKNOWN_ERROR;
225 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700226 }
227
228 return OK;
229}
230
Yifan Hong5a93bf22018-01-17 18:22:32 -0800231// Priority for loading vendor manifest:
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800232// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
233// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700234// 3. ODM manifest (optional) + odm fragments
235// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800236// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700237// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
238// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700239status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800240 HalManifest vendorManifest;
241 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800242 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
243 return vendorStatus;
244 }
245
Steven Morelandeedf2d42018-04-04 16:36:27 -0700246 if (vendorStatus == OK) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800247 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700248 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700249 if (fragmentStatus != OK) {
250 return fragmentStatus;
251 }
252 }
253
Yifan Hong5a93bf22018-01-17 18:22:32 -0800254 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700255 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800256 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
257 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800258 }
259
Yifan Hong5a93bf22018-01-17 18:22:32 -0800260 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800261 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800262 if (!out->addAll(&odmManifest, error)) {
263 if (error) {
264 error->insert(0, "Cannot add ODM manifest :");
265 }
266 return UNKNOWN_ERROR;
267 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800268 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700269 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800270 }
271
Yifan Hong12a11ac2018-01-19 13:58:32 -0800272 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800273 if (odmStatus == OK) {
274 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700275 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800276 }
277
278 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800279 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800280}
281
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800282// Priority:
283// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
284// 2. /vendor/etc/vintf/manifest.xml
285// where:
286// {vendorSku} is the value of ro.boot.product.vendor.sku
287status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
288 status_t status;
289
290 std::string vendorSku;
291 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
292
293 if (!vendorSku.empty()) {
294 status =
295 fetchOneHalManifest(kVendorVintfDir + "manifest_" + vendorSku + ".xml", out, error);
296 if (status == OK || status != NAME_NOT_FOUND) {
297 return status;
298 }
299 }
300
301 status = fetchOneHalManifest(kVendorManifest, out, error);
302 if (status == OK || status != NAME_NOT_FOUND) {
303 return status;
304 }
305
306 return NAME_NOT_FOUND;
307}
308
Yifan Hong12a11ac2018-01-19 13:58:32 -0800309// "out" is written to iff return status is OK.
310// Priority:
311// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
312// 2. /odm/etc/vintf/manifest.xml
313// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
314// 4. /odm/etc/manifest.xml
315// where:
316// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700317status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800318 status_t status;
319
Yifan Hong12a11ac2018-01-19 13:58:32 -0800320 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800321 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800322
323 if (!productModel.empty()) {
324 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700325 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800326 if (status == OK || status != NAME_NOT_FOUND) {
327 return status;
328 }
329 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800330
Yifan Hong9f78c182018-07-12 14:45:52 -0700331 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800332 if (status == OK || status != NAME_NOT_FOUND) {
333 return status;
334 }
335
Yifan Hong12a11ac2018-01-19 13:58:32 -0800336 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700337 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800338 error);
339 if (status == OK || status != NAME_NOT_FOUND) {
340 return status;
341 }
342 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800343
Yifan Hong9f78c182018-07-12 14:45:52 -0700344 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800345 if (status == OK || status != NAME_NOT_FOUND) {
346 return status;
347 }
348
349 return NAME_NOT_FOUND;
350}
351
352// Fetch one manifest.xml file. "out" is written to iff return status is OK.
353// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700354status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800355 std::string* error) {
356 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800357 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800358 if (status == OK) {
359 *out = std::move(ret);
360 }
361 return status;
362}
363
Yifan Hong9f78c182018-07-12 14:45:52 -0700364status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800365 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800366 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800367 *out = std::move(etcMatrix);
368 return OK;
369 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800370 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800371}
372
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700373// Priority:
374// 1. /system/etc/vintf/manifest.xml
375// + /system/etc/vintf/manifest/*.xml if they exist
376// + /product/etc/vintf/manifest.xml if it exists
377// + /product/etc/vintf/manifest/*.xml if they exist
378// 2. (deprecated) /system/manifest.xml
Yifan Hong9f78c182018-07-12 14:45:52 -0700379status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700380 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
381 if (systemEtcStatus == OK) {
382 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
383 if (dirStatus != OK) {
384 return dirStatus;
385 }
386
387 HalManifest productManifest;
388 auto productStatus = fetchOneHalManifest(kProductManifest, &productManifest, error);
389 if (productStatus != OK && productStatus != NAME_NOT_FOUND) {
390 return productStatus;
391 }
392 if (productStatus == OK) {
393 if (!out->addAll(&productManifest, error)) {
394 if (error) {
395 error->insert(0, "Cannot add " + kProductManifest + ":");
396 }
397 return UNKNOWN_ERROR;
398 }
399 }
400
401 return addDirectoryManifests(kProductManifestFragmentDir, out, error);
402 } else {
403 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
404 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800405 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700406
Yifan Hong12e23c22018-11-05 14:53:30 -0800407 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800408}
409
Yifan Hong9f8f6562018-11-06 16:26:20 -0800410static void appendLine(std::string* error, const std::string& message) {
411 if (error != nullptr) {
412 if (!error->empty()) *error += "\n";
413 *error += message;
414 }
415}
416
Yifan Hong73bde592019-01-22 13:30:23 -0800417status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
418 std::string* error) {
419 std::string content;
420 status_t status = getFileSystem()->fetch(path, &content, error);
421 if (status != OK) {
422 return status;
423 }
424 if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
425 if (error) {
426 error->insert(0, "Cannot parse " + path + ": ");
427 }
428 return BAD_VALUE;
429 }
430 out->name = path;
431 return OK;
432}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800433
Yifan Hong73bde592019-01-22 13:30:23 -0800434status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
435 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800436 std::vector<std::string> dirs = {
437 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800438 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800439 kProductVintfDir,
440 };
441 for (const auto& dir : dirs) {
442 std::vector<std::string> fileNames;
443 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
444 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800445 continue;
446 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800447 if (listStatus != OK) {
448 return listStatus;
449 }
450 for (const std::string& fileName : fileNames) {
451 std::string path = dir + fileName;
452 Named<CompatibilityMatrix> namedMatrix;
453 std::string matrixError;
454 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
455 if (matrixStatus != OK) {
456 // Manifests and matrices share the same dir. Client may not have enough
457 // permissions to read system manifests, or may not be able to parse it.
458 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
459 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
460 continue;
461 }
462 results->emplace_back(std::move(namedMatrix));
463 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800464
Yifan Hongb02d87d2019-12-19 11:15:27 -0800465 if (dir == kSystemVintfDir && results->empty()) {
466 if (error) {
467 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
468 }
469 return NAME_NOT_FOUND;
470 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800471 }
472
Yifan Hong73bde592019-01-22 13:30:23 -0800473 if (results->empty()) {
474 if (error) {
475 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800476 "No framework matrices can be fetched or parsed. "
477 "The following directories are searched:\n " +
478 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800479 }
480 return NAME_NOT_FOUND;
481 }
482 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800483}
484
Yifan Hong1fb004e2017-09-26 15:04:44 -0700485std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
486 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700487 return GetInstance()->getRuntimeInfo(skipCache, flags);
488}
489std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
490 RuntimeInfo::FetchFlags flags) {
491 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700492
493 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700494 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700495 }
496
Yifan Hong9f78c182018-07-12 14:45:52 -0700497 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800498 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700499 }
500
Yifan Hongf3247982019-12-12 12:11:36 -0800501 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
502 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
503 auto manifest = getDeviceHalManifest();
504 if (!manifest) {
505 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
506 return nullptr;
507 }
508 Level level = Level::UNSPECIFIED;
509 if (manifest->kernel().has_value()) {
510 level = manifest->kernel()->level();
511 }
512 mDeviceRuntimeInfo.object->setKernelLevel(level);
513 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
514 }
515
Yifan Hong9f78c182018-07-12 14:45:52 -0700516 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700517 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700518 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700519 return nullptr;
520 }
521
Yifan Hong9f78c182018-07-12 14:45:52 -0700522 mDeviceRuntimeInfo.fetchedFlags |= flags;
523 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800524}
525
Yifan Hong12e23c22018-11-05 14:53:30 -0800526int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
527 status_t status = OK;
528 // null checks for files and runtime info
529 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700530 appendLine(error, "No framework manifest file from device or from update package");
531 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700532 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800533 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700534 appendLine(error, "No device manifest file from device or from update package");
535 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700536 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800537 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700538 appendLine(error, "No framework matrix file from device or from update package");
539 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700540 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800541 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700542 appendLine(error, "No device matrix file from device or from update package");
543 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700544 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800545
Yifan Hong072f12d2018-08-08 13:04:51 -0700546 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800547 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700548 appendLine(error, "No runtime info from device");
549 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800550 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700551 }
Yifan Hong878556e2018-07-13 13:45:30 -0700552 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700553
554 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800555 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800556 if (error) {
557 error->insert(0,
558 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700559 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800560 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700561 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800562 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800563 if (error) {
564 error->insert(0,
565 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700566 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800567 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700568 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800569
Yifan Hong072f12d2018-08-08 13:04:51 -0700570 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800571 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800572 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800573 if (error) {
574 error->insert(0,
575 "Runtime info and framework compatibility matrix are incompatible: ");
576 }
577 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700578 }
579 }
580
581 return COMPATIBLE;
582}
583
Yifan Hong9f78c182018-07-12 14:45:52 -0700584namespace details {
585
Yifan Hong270b5652018-01-18 18:46:01 -0800586const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800587const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800588const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800589const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800590const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800591
592const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800593const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800594const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800595const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800596const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700597const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800598
Steven Morelandeedf2d42018-04-04 16:36:27 -0700599const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
600const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
601const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700602const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700603
Yifan Hong270b5652018-01-18 18:46:01 -0800604const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
605const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800606const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800607const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
608const std::string kOdmLegacyVintfDir = "/odm/etc/";
609const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
610
Yifan Hong69c1b112018-02-27 17:06:00 -0800611std::vector<std::string> dumpFileList() {
612 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800613 // clang-format off
614 kSystemVintfDir,
615 kVendorVintfDir,
616 kOdmVintfDir,
617 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800618 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800619 kOdmLegacyVintfDir,
620 kVendorLegacyManifest,
621 kVendorLegacyMatrix,
622 kSystemLegacyManifest,
623 kSystemLegacyMatrix,
624 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800625 };
626}
627
Yifan Hong9f78c182018-07-12 14:45:52 -0700628} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700629
Yifan Hong9f78c182018-07-12 14:45:52 -0700630bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800631 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700632 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700633 bool isDeprecated = false;
634 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700635 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700636 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800637 }
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700638 return !isDeprecated; // continue if no deprecated instance is found.
639 });
640 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800641}
642
Yifan Honga8a8fa92018-03-20 14:42:43 -0700643// Let oldMatrixInstance = package@x.y-w::interface with instancePattern.
644// If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return
645// true iff:
646// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
647// 2. package@x.z::interface/servedInstance is in targetMatrix but
648// servedInstance is not in listInstances(package@x.z::interface)
Yifan Hong9f78c182018-07-12 14:45:52 -0700649bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800650 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700651 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700652 const std::string& package = oldMatrixInstance.package();
653 const Version& version = oldMatrixInstance.versionRange().minVer();
654 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700655
Yifan Honga8a8fa92018-03-20 14:42:43 -0700656 std::vector<std::string> instanceHint;
657 if (!oldMatrixInstance.isRegex()) {
658 instanceHint.push_back(oldMatrixInstance.exactInstance());
659 }
660
661 auto list = listInstances(package, version, interface, instanceHint);
662 for (const auto& pair : list) {
663 const std::string& servedInstance = pair.first;
664 Version servedVersion = pair.second;
665 if (!oldMatrixInstance.matchInstance(servedInstance)) {
666 continue;
667 }
668
Yifan Hongf73ba512018-01-17 15:52:30 -0800669 // Find any package@x.? in target matrix, and check if instance is in target matrix.
Yifan Hong217f47e2018-03-15 12:34:05 -0700670 bool foundInstance = false;
671 Version targetMatrixMinVer;
Yifan Hong0b1916f2019-09-10 19:04:52 -0700672 targetMatrix.forEachHidlInstanceOfPackage(package, [&](const auto& targetMatrixInstance) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700673 if (targetMatrixInstance.versionRange().majorVer == version.majorVer &&
674 targetMatrixInstance.interface() == interface &&
Yifan Honga8a8fa92018-03-20 14:42:43 -0700675 targetMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700676 targetMatrixMinVer = targetMatrixInstance.versionRange().minVer();
677 foundInstance = true;
678 }
679 return !foundInstance; // continue if not found
680 });
681 if (!foundInstance) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800682 if (error) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700683 *error = toFQNameString(package, servedVersion, interface, servedInstance) +
Steven Morelanda1b984c2018-03-09 13:09:15 -0800684 " is deprecated in compatibility matrix at FCM Version " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800685 to_string(targetMatrix.level()) + "; it should not be served.";
686 }
687 return true;
688 }
689
Yifan Hongf73ba512018-01-17 15:52:30 -0800690 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
Yifan Honga8a8fa92018-03-20 14:42:43 -0700691 bool targetVersionServed = false;
692 for (const auto& newPair :
693 listInstances(package, targetMatrixMinVer, interface, instanceHint)) {
694 if (newPair.first == servedInstance) {
695 targetVersionServed = true;
696 break;
697 }
698 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800699
700 if (!targetVersionServed) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800701 appendLine(error, toFQNameString(package, servedVersion, interface, servedInstance) +
702 " is deprecated; requires at least " +
703 to_string(targetMatrixMinVer));
Yifan Hongf73ba512018-01-17 15:52:30 -0800704 return true;
705 }
706 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700707
Yifan Hongf73ba512018-01-17 15:52:30 -0800708 return false;
709}
710
Yifan Honga8a8fa92018-03-20 14:42:43 -0700711int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700712 return GetInstance()->checkDeprecation(listInstances, error);
713}
714int32_t VintfObject::checkDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800715 std::vector<Named<CompatibilityMatrix>> matrixFragments;
716 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
717 if (matrixFragmentsStatus != OK) {
718 return matrixFragmentsStatus;
719 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800720 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800721 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800722 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800723 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800724 return NAME_NOT_FOUND;
725 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700726 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800727 if (deviceManifest == nullptr) {
728 if (error) *error = "No device manifest.";
729 return NAME_NOT_FOUND;
730 }
731 Level deviceLevel = deviceManifest->level();
732 if (deviceLevel == Level::UNSPECIFIED) {
733 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
734 return BAD_VALUE;
735 }
736
737 const CompatibilityMatrix* targetMatrix = nullptr;
738 for (const auto& namedMatrix : matrixFragments) {
739 if (namedMatrix.object.level() == deviceLevel) {
740 targetMatrix = &namedMatrix.object;
741 }
742 }
743 if (targetMatrix == nullptr) {
744 if (error)
745 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
746 return NAME_NOT_FOUND;
747 }
748
749 bool hasDeprecatedHals = false;
750 for (const auto& namedMatrix : matrixFragments) {
751 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
752 if (namedMatrix.object.level() >= deviceLevel) continue;
753
754 const auto& oldMatrix = namedMatrix.object;
755 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700756 hasDeprecatedHals |= IsHalDeprecated(hal, *targetMatrix, listInstances, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800757 }
758 }
759
760 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
761}
762
763int32_t VintfObject::CheckDeprecation(std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700764 return GetInstance()->checkDeprecation(error);
765}
766int32_t VintfObject::checkDeprecation(std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800767 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700768 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700769 ListInstances inManifest =
770 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
771 const std::vector<std::string>& /* hintInstances */) {
772 std::vector<std::pair<std::string, Version>> ret;
773 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700774 HalFormat::HIDL, package, version, interface,
775 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700776 ret.push_back(
777 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
778 return true;
779 });
780 return ret;
781 };
Yifan Hong9f78c182018-07-12 14:45:52 -0700782 return checkDeprecation(inManifest, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800783}
Yifan Hong3daec812017-02-27 18:49:11 -0800784
Yifan Hong378c4082019-12-23 13:10:57 -0800785Level VintfObject::getKernelLevel(std::string* error) {
786 auto manifest = getDeviceHalManifest();
787 if (!manifest) {
788 if (error) *error = "Cannot retrieve device manifest.";
789 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700790 }
Yifan Hong378c4082019-12-23 13:10:57 -0800791 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
792 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700793 }
Yifan Hong378c4082019-12-23 13:10:57 -0800794 if (error) *error = "Device manifest does not specify kernel FCM version.";
795 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700796}
797
Yifan Hong9f78c182018-07-12 14:45:52 -0700798const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
799 return mFileSystem;
800}
801
Yifan Hong9f78c182018-07-12 14:45:52 -0700802const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
803 return mPropertyFetcher;
804}
805
Yifan Hongd038b2b2018-11-27 13:57:56 -0800806const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700807 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700808}
809
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700810android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
811 std::vector<Named<CompatibilityMatrix>> matrixFragments;
812 std::string error;
813 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
814 if (status != OK) {
815 return android::base::Error(-status)
816 << "Cannot get all framework matrix fragments: " << error;
817 }
818 for (const auto& namedMatrix : matrixFragments) {
819 // Returns true if product matrix exists.
820 if (android::base::StartsWith(namedMatrix.name, kProductVintfDir)) {
821 return true;
822 }
Yifan Hong238c6dc2020-03-12 22:56:16 -0700823 // Returns true if system_ext matrix exists.
824 if (android::base::StartsWith(namedMatrix.name, kSystemExtVintfDir)) {
825 return true;
826 }
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700827 // Returns true if device system matrix exists.
828 if (android::base::StartsWith(namedMatrix.name, kSystemVintfDir) &&
829 namedMatrix.object.level() == Level::UNSPECIFIED &&
830 !namedMatrix.object.getHals().empty()) {
831 return true;
832 }
833 }
834 return false;
835}
836
Yifan Hongff1a25c2020-03-17 14:09:10 -0700837android::base::Result<void> VintfObject::checkUnusedHals(
838 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700839 auto matrix = getFrameworkCompatibilityMatrix();
840 if (matrix == nullptr) {
841 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
842 }
843 auto manifest = getDeviceHalManifest();
844 if (manifest == nullptr) {
845 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
846 }
Yifan Hongff1a25c2020-03-17 14:09:10 -0700847 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700848 if (!unused.empty()) {
849 return android::base::Error()
850 << "The following instances are in the device manifest but "
851 << "not specified in framework compatibility matrix: \n"
852 << " " << android::base::Join(unused, "\n ") << "\n"
853 << "Suggested fix:\n"
854 << "1. Check for any typos in device manifest or framework compatibility "
855 << "matrices with FCM version >= " << matrix->level() << ".\n"
856 << "2. Add them to any framework compatibility matrix with FCM "
857 << "version >= " << matrix->level() << " where applicable.\n"
858 << "3. Add them to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
859 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
860 }
861 return {};
862}
863
Yifan Hong78f5b572018-11-27 14:05:03 -0800864// make_unique does not work because VintfObject constructor is private.
865VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
866
867VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
868 mObject->mFileSystem = std::move(e);
869 return *this;
870}
871
872VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
873 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
874 mObject->mRuntimeInfoFactory = std::move(e);
875 return *this;
876}
877
878VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
879 std::unique_ptr<PropertyFetcher>&& e) {
880 mObject->mPropertyFetcher = std::move(e);
881 return *this;
882}
883
884std::unique_ptr<VintfObject> VintfObject::Builder::build() {
885 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
886 if (!mObject->mRuntimeInfoFactory)
887 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
888 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
889 return std::move(mObject);
890}
891
Yifan Hong3daec812017-02-27 18:49:11 -0800892} // namespace vintf
893} // namespace android