blob: f397ef30d395215e03bd8a913d1a5b13f7975b96 [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 Hongb02d87d2019-12-19 11:15:27 -080026#include <android-base/strings.h>
Yifan Hong60217032018-01-08 16:19:42 -080027
Yifan Hong12e23c22018-11-05 14:53:30 -080028#include "CompatibilityMatrix.h"
Yifan Hong12e23c22018-11-05 14:53:30 -080029#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 -070085std::shared_ptr<VintfObject> VintfObject::GetInstance() {
Steven Morelandc16ff2b2020-02-26 17:03:37 -080086 static details::LockedSharedPtr<VintfObject> sInstance{};
Yifan Hong9f78c182018-07-12 14:45:52 -070087 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:
Roopesh Natarajad629d382020-02-26 09:51:38 -0800230// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
231// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700232// 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) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800238 HalManifest vendorManifest;
239 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800240 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
241 return vendorStatus;
242 }
243
Steven Morelandeedf2d42018-04-04 16:36:27 -0700244 if (vendorStatus == OK) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800245 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700246 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700247 if (fragmentStatus != OK) {
248 return fragmentStatus;
249 }
250 }
251
Yifan Hong5a93bf22018-01-17 18:22:32 -0800252 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700253 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800254 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
255 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800256 }
257
Yifan Hong5a93bf22018-01-17 18:22:32 -0800258 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800259 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800260 if (!out->addAll(&odmManifest, error)) {
261 if (error) {
262 error->insert(0, "Cannot add ODM manifest :");
263 }
264 return UNKNOWN_ERROR;
265 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800266 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700267 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800268 }
269
Yifan Hong12a11ac2018-01-19 13:58:32 -0800270 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800271 if (odmStatus == OK) {
272 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700273 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800274 }
275
276 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800277 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800278}
279
Roopesh Natarajad629d382020-02-26 09:51:38 -0800280// Priority:
281// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
282// 2. /vendor/etc/vintf/manifest.xml
283// where:
284// {vendorSku} is the value of ro.boot.product.vendor.sku
285status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
286 status_t status;
287
288 std::string vendorSku;
289 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
290
291 if (!vendorSku.empty()) {
292 status =
293 fetchOneHalManifest(kVendorVintfDir + "manifest_" + vendorSku + ".xml", out, error);
294 if (status == OK || status != NAME_NOT_FOUND) {
295 return status;
296 }
297 }
298
299 status = fetchOneHalManifest(kVendorManifest, out, error);
300 if (status == OK || status != NAME_NOT_FOUND) {
301 return status;
302 }
303
304 return NAME_NOT_FOUND;
305}
306
Yifan Hong12a11ac2018-01-19 13:58:32 -0800307// "out" is written to iff return status is OK.
308// Priority:
309// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
310// 2. /odm/etc/vintf/manifest.xml
311// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
312// 4. /odm/etc/manifest.xml
313// where:
314// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700315status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800316 status_t status;
317
Yifan Hong12a11ac2018-01-19 13:58:32 -0800318 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800319 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800320
321 if (!productModel.empty()) {
322 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700323 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800324 if (status == OK || status != NAME_NOT_FOUND) {
325 return status;
326 }
327 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800328
Yifan Hong9f78c182018-07-12 14:45:52 -0700329 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800330 if (status == OK || status != NAME_NOT_FOUND) {
331 return status;
332 }
333
Yifan Hong12a11ac2018-01-19 13:58:32 -0800334 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700335 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800336 error);
337 if (status == OK || status != NAME_NOT_FOUND) {
338 return status;
339 }
340 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800341
Yifan Hong9f78c182018-07-12 14:45:52 -0700342 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800343 if (status == OK || status != NAME_NOT_FOUND) {
344 return status;
345 }
346
347 return NAME_NOT_FOUND;
348}
349
350// Fetch one manifest.xml file. "out" is written to iff return status is OK.
351// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700352status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800353 std::string* error) {
354 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800355 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800356 if (status == OK) {
357 *out = std::move(ret);
358 }
359 return status;
360}
361
Yifan Hong9f78c182018-07-12 14:45:52 -0700362status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800363 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800364 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800365 *out = std::move(etcMatrix);
366 return OK;
367 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800368 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800369}
370
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700371// Priority:
372// 1. /system/etc/vintf/manifest.xml
373// + /system/etc/vintf/manifest/*.xml if they exist
374// + /product/etc/vintf/manifest.xml if it exists
375// + /product/etc/vintf/manifest/*.xml if they exist
376// 2. (deprecated) /system/manifest.xml
Yifan Hong9f78c182018-07-12 14:45:52 -0700377status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700378 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
379 if (systemEtcStatus == OK) {
380 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
381 if (dirStatus != OK) {
382 return dirStatus;
383 }
384
Yifan Hong659feac2020-03-19 18:09:54 -0700385 std::vector<std::pair<const std::string&, const std::string&>> extensions{
386 {kProductManifest, kProductManifestFragmentDir},
387 {kSystemExtManifest, kSystemExtManifestFragmentDir},
388 };
389 for (auto&& [manifestPath, frags] : extensions) {
390 HalManifest halManifest;
391 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
392 if (status != OK && status != NAME_NOT_FOUND) {
393 return status;
394 }
395 if (status == OK) {
396 if (!out->addAll(&halManifest, error)) {
397 if (error) {
398 error->insert(0, "Cannot add " + manifestPath + ":");
399 }
400 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700401 }
Yifan Hong659feac2020-03-19 18:09:54 -0700402 }
403
404 auto fragmentStatus = addDirectoryManifests(frags, out, error);
405 if (fragmentStatus != OK) {
406 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700407 }
408 }
Yifan Hong659feac2020-03-19 18:09:54 -0700409 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700410 } else {
411 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
412 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800413 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700414
Yifan Hong12e23c22018-11-05 14:53:30 -0800415 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800416}
417
Yifan Hong9f8f6562018-11-06 16:26:20 -0800418static void appendLine(std::string* error, const std::string& message) {
419 if (error != nullptr) {
420 if (!error->empty()) *error += "\n";
421 *error += message;
422 }
423}
424
Yifan Hong73bde592019-01-22 13:30:23 -0800425status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
426 std::string* error) {
427 std::string content;
428 status_t status = getFileSystem()->fetch(path, &content, error);
429 if (status != OK) {
430 return status;
431 }
432 if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
433 if (error) {
434 error->insert(0, "Cannot parse " + path + ": ");
435 }
436 return BAD_VALUE;
437 }
438 out->name = path;
439 return OK;
440}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800441
Yifan Hong73bde592019-01-22 13:30:23 -0800442status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
443 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800444 std::vector<std::string> dirs = {
445 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800446 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800447 kProductVintfDir,
448 };
449 for (const auto& dir : dirs) {
450 std::vector<std::string> fileNames;
451 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
452 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800453 continue;
454 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800455 if (listStatus != OK) {
456 return listStatus;
457 }
458 for (const std::string& fileName : fileNames) {
459 std::string path = dir + fileName;
460 Named<CompatibilityMatrix> namedMatrix;
461 std::string matrixError;
462 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
463 if (matrixStatus != OK) {
464 // Manifests and matrices share the same dir. Client may not have enough
465 // permissions to read system manifests, or may not be able to parse it.
466 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
467 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
468 continue;
469 }
470 results->emplace_back(std::move(namedMatrix));
471 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800472
Yifan Hongb02d87d2019-12-19 11:15:27 -0800473 if (dir == kSystemVintfDir && results->empty()) {
474 if (error) {
475 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
476 }
477 return NAME_NOT_FOUND;
478 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800479 }
480
Yifan Hong73bde592019-01-22 13:30:23 -0800481 if (results->empty()) {
482 if (error) {
483 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800484 "No framework matrices can be fetched or parsed. "
485 "The following directories are searched:\n " +
486 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800487 }
488 return NAME_NOT_FOUND;
489 }
490 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800491}
492
Yifan Hong1fb004e2017-09-26 15:04:44 -0700493std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
494 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700495 return GetInstance()->getRuntimeInfo(skipCache, flags);
496}
497std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
498 RuntimeInfo::FetchFlags flags) {
499 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700500
501 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700502 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700503 }
504
Yifan Hong9f78c182018-07-12 14:45:52 -0700505 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800506 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700507 }
508
Yifan Hongf3247982019-12-12 12:11:36 -0800509 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
510 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
511 auto manifest = getDeviceHalManifest();
512 if (!manifest) {
513 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
514 return nullptr;
515 }
516 Level level = Level::UNSPECIFIED;
517 if (manifest->kernel().has_value()) {
518 level = manifest->kernel()->level();
519 }
520 mDeviceRuntimeInfo.object->setKernelLevel(level);
521 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
522 }
523
Yifan Hong9f78c182018-07-12 14:45:52 -0700524 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700525 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700526 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700527 return nullptr;
528 }
529
Yifan Hong9f78c182018-07-12 14:45:52 -0700530 mDeviceRuntimeInfo.fetchedFlags |= flags;
531 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800532}
533
Yifan Hong12e23c22018-11-05 14:53:30 -0800534int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
535 status_t status = OK;
536 // null checks for files and runtime info
537 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700538 appendLine(error, "No framework manifest 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 (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700542 appendLine(error, "No device manifest file from device or from update package");
543 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700544 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800545 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700546 appendLine(error, "No framework matrix file from device or from update package");
547 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700548 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800549 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700550 appendLine(error, "No device matrix file from device or from update package");
551 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700552 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800553
Yifan Hong072f12d2018-08-08 13:04:51 -0700554 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800555 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700556 appendLine(error, "No runtime info from device");
557 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800558 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700559 }
Yifan Hong878556e2018-07-13 13:45:30 -0700560 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700561
562 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800563 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800564 if (error) {
565 error->insert(0,
566 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700567 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800568 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700569 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800570 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800571 if (error) {
572 error->insert(0,
573 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700574 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800575 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700576 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800577
Yifan Hong072f12d2018-08-08 13:04:51 -0700578 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800579 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800580 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800581 if (error) {
582 error->insert(0,
583 "Runtime info and framework compatibility matrix are incompatible: ");
584 }
585 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700586 }
587 }
588
589 return COMPATIBLE;
590}
591
Yifan Hong9f78c182018-07-12 14:45:52 -0700592namespace details {
593
Yifan Hong270b5652018-01-18 18:46:01 -0800594const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800595const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800596const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800597const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800598const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800599
600const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800601const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800602const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800603const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800604const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700605const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hong659feac2020-03-19 18:09:54 -0700606const std::string kSystemExtManifest = kSystemExtVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800607
Steven Morelandeedf2d42018-04-04 16:36:27 -0700608const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
609const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
610const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700611const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Yifan Hong659feac2020-03-19 18:09:54 -0700612const std::string kSystemExtManifestFragmentDir = kSystemExtVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700613
Yifan Hong270b5652018-01-18 18:46:01 -0800614const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
615const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800616const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800617const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
618const std::string kOdmLegacyVintfDir = "/odm/etc/";
619const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
620
Yifan Hong69c1b112018-02-27 17:06:00 -0800621std::vector<std::string> dumpFileList() {
622 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800623 // clang-format off
624 kSystemVintfDir,
625 kVendorVintfDir,
626 kOdmVintfDir,
627 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800628 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800629 kOdmLegacyVintfDir,
630 kVendorLegacyManifest,
631 kVendorLegacyMatrix,
632 kSystemLegacyManifest,
633 kSystemLegacyMatrix,
634 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800635 };
636}
637
Yifan Hong9f78c182018-07-12 14:45:52 -0700638} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700639
Yifan Hong9f78c182018-07-12 14:45:52 -0700640bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800641 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700642 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700643 bool isDeprecated = false;
644 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700645 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700646 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800647 }
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700648 return !isDeprecated; // continue if no deprecated instance is found.
649 });
650 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800651}
652
Yifan Honga8a8fa92018-03-20 14:42:43 -0700653// Let oldMatrixInstance = package@x.y-w::interface with instancePattern.
654// If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return
655// true iff:
656// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
657// 2. package@x.z::interface/servedInstance is in targetMatrix but
658// servedInstance is not in listInstances(package@x.z::interface)
Yifan Hong9f78c182018-07-12 14:45:52 -0700659bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800660 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700661 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700662 const std::string& package = oldMatrixInstance.package();
663 const Version& version = oldMatrixInstance.versionRange().minVer();
664 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700665
Yifan Honga8a8fa92018-03-20 14:42:43 -0700666 std::vector<std::string> instanceHint;
667 if (!oldMatrixInstance.isRegex()) {
668 instanceHint.push_back(oldMatrixInstance.exactInstance());
669 }
670
671 auto list = listInstances(package, version, interface, instanceHint);
672 for (const auto& pair : list) {
673 const std::string& servedInstance = pair.first;
674 Version servedVersion = pair.second;
675 if (!oldMatrixInstance.matchInstance(servedInstance)) {
676 continue;
677 }
678
Yifan Hongf73ba512018-01-17 15:52:30 -0800679 // Find any package@x.? in target matrix, and check if instance is in target matrix.
Yifan Hong217f47e2018-03-15 12:34:05 -0700680 bool foundInstance = false;
681 Version targetMatrixMinVer;
Yifan Hong0b1916f2019-09-10 19:04:52 -0700682 targetMatrix.forEachHidlInstanceOfPackage(package, [&](const auto& targetMatrixInstance) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700683 if (targetMatrixInstance.versionRange().majorVer == version.majorVer &&
684 targetMatrixInstance.interface() == interface &&
Yifan Honga8a8fa92018-03-20 14:42:43 -0700685 targetMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700686 targetMatrixMinVer = targetMatrixInstance.versionRange().minVer();
687 foundInstance = true;
688 }
689 return !foundInstance; // continue if not found
690 });
691 if (!foundInstance) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800692 if (error) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700693 *error = toFQNameString(package, servedVersion, interface, servedInstance) +
Steven Morelanda1b984c2018-03-09 13:09:15 -0800694 " is deprecated in compatibility matrix at FCM Version " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800695 to_string(targetMatrix.level()) + "; it should not be served.";
696 }
697 return true;
698 }
699
Yifan Hongf73ba512018-01-17 15:52:30 -0800700 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
Yifan Honga8a8fa92018-03-20 14:42:43 -0700701 bool targetVersionServed = false;
702 for (const auto& newPair :
703 listInstances(package, targetMatrixMinVer, interface, instanceHint)) {
704 if (newPair.first == servedInstance) {
705 targetVersionServed = true;
706 break;
707 }
708 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800709
710 if (!targetVersionServed) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800711 appendLine(error, toFQNameString(package, servedVersion, interface, servedInstance) +
712 " is deprecated; requires at least " +
713 to_string(targetMatrixMinVer));
Yifan Hongf73ba512018-01-17 15:52:30 -0800714 return true;
715 }
716 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700717
Yifan Hongf73ba512018-01-17 15:52:30 -0800718 return false;
719}
720
Yifan Honga8a8fa92018-03-20 14:42:43 -0700721int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700722 return GetInstance()->checkDeprecation(listInstances, error);
723}
724int32_t VintfObject::checkDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800725 std::vector<Named<CompatibilityMatrix>> matrixFragments;
726 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
727 if (matrixFragmentsStatus != OK) {
728 return matrixFragmentsStatus;
729 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800730 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800731 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800732 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800733 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800734 return NAME_NOT_FOUND;
735 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700736 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800737 if (deviceManifest == nullptr) {
738 if (error) *error = "No device manifest.";
739 return NAME_NOT_FOUND;
740 }
741 Level deviceLevel = deviceManifest->level();
742 if (deviceLevel == Level::UNSPECIFIED) {
743 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
744 return BAD_VALUE;
745 }
746
747 const CompatibilityMatrix* targetMatrix = nullptr;
748 for (const auto& namedMatrix : matrixFragments) {
749 if (namedMatrix.object.level() == deviceLevel) {
750 targetMatrix = &namedMatrix.object;
751 }
752 }
753 if (targetMatrix == nullptr) {
754 if (error)
755 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
756 return NAME_NOT_FOUND;
757 }
758
759 bool hasDeprecatedHals = false;
760 for (const auto& namedMatrix : matrixFragments) {
761 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
762 if (namedMatrix.object.level() >= deviceLevel) continue;
763
764 const auto& oldMatrix = namedMatrix.object;
765 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700766 hasDeprecatedHals |= IsHalDeprecated(hal, *targetMatrix, listInstances, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800767 }
768 }
769
770 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
771}
772
773int32_t VintfObject::CheckDeprecation(std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700774 return GetInstance()->checkDeprecation(error);
775}
776int32_t VintfObject::checkDeprecation(std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800777 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700778 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700779 ListInstances inManifest =
780 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
781 const std::vector<std::string>& /* hintInstances */) {
782 std::vector<std::pair<std::string, Version>> ret;
783 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700784 HalFormat::HIDL, package, version, interface,
785 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700786 ret.push_back(
787 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
788 return true;
789 });
790 return ret;
791 };
Yifan Hong9f78c182018-07-12 14:45:52 -0700792 return checkDeprecation(inManifest, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800793}
Yifan Hong3daec812017-02-27 18:49:11 -0800794
Yifan Hong378c4082019-12-23 13:10:57 -0800795Level VintfObject::getKernelLevel(std::string* error) {
796 auto manifest = getDeviceHalManifest();
797 if (!manifest) {
798 if (error) *error = "Cannot retrieve device manifest.";
799 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700800 }
Yifan Hong378c4082019-12-23 13:10:57 -0800801 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
802 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700803 }
Yifan Hong378c4082019-12-23 13:10:57 -0800804 if (error) *error = "Device manifest does not specify kernel FCM version.";
805 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700806}
807
Yifan Hong9f78c182018-07-12 14:45:52 -0700808const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
809 return mFileSystem;
810}
811
Yifan Hong9f78c182018-07-12 14:45:52 -0700812const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
813 return mPropertyFetcher;
814}
815
Yifan Hongd038b2b2018-11-27 13:57:56 -0800816const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700817 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700818}
819
Yifan Hong78f5b572018-11-27 14:05:03 -0800820// make_unique does not work because VintfObject constructor is private.
821VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
822
823VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
824 mObject->mFileSystem = std::move(e);
825 return *this;
826}
827
828VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
829 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
830 mObject->mRuntimeInfoFactory = std::move(e);
831 return *this;
832}
833
834VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
835 std::unique_ptr<PropertyFetcher>&& e) {
836 mObject->mPropertyFetcher = std::move(e);
837 return *this;
838}
839
840std::unique_ptr<VintfObject> VintfObject::Builder::build() {
841 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
842 if (!mObject->mRuntimeInfoFactory)
843 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
844 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
845 return std::move(mObject);
846}
847
Yifan Hong3daec812017-02-27 18:49:11 -0800848} // namespace vintf
849} // namespace android