blob: 0a2b49c3e993174d3d6e5ec6066b1247253b4bdd [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 Hong03ea4282020-03-17 17:58:35 -070021#include <algorithm>
Yifan Hong3daec812017-02-27 18:49:11 -080022#include <functional>
23#include <memory>
24#include <mutex>
25
Yifan Hongd7043972020-03-30 13:27:46 -070026#include <aidl/metadata.h>
Yifan Hong60217032018-01-08 16:19:42 -080027#include <android-base/logging.h>
Yifan Hong6b860852020-03-19 23:12:07 +000028#include <android-base/result.h>
Yifan Hongb02d87d2019-12-19 11:15:27 -080029#include <android-base/strings.h>
Yifan Hongd7924ff2020-03-17 14:09:10 -070030#include <hidl/metadata.h>
Yifan Hong60217032018-01-08 16:19:42 -080031
Yifan Hong12e23c22018-11-05 14:53:30 -080032#include "CompatibilityMatrix.h"
Yifan Hongd7043972020-03-30 13:27:46 -070033#include "constants-private.h"
Yifan Hong12e23c22018-11-05 14:53:30 -080034#include "parse_string.h"
35#include "parse_xml.h"
36#include "utils.h"
37
Yifan Hong60217032018-01-08 16:19:42 -080038using std::placeholders::_1;
39using std::placeholders::_2;
Yifan Hongc7a9a292021-02-03 17:27:58 -080040using std::string_literals::operator""s;
Yifan Hong60217032018-01-08 16:19:42 -080041
Yifan Hong3daec812017-02-27 18:49:11 -080042namespace android {
43namespace vintf {
44
Yifan Hong270b5652018-01-18 18:46:01 -080045using namespace details;
46
Yifan Hong9f78c182018-07-12 14:45:52 -070047#ifdef LIBVINTF_TARGET
48static constexpr bool kIsTarget = true;
49#else
50static constexpr bool kIsTarget = false;
51#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070052
Yifan Hong3daec812017-02-27 18:49:11 -080053template <typename T, typename F>
Yifan Hong44f611c2020-07-21 11:57:57 -070054static std::shared_ptr<const T> Get(const char* id, LockedSharedPtr<T>* ptr,
Steven Morelandec0721d2020-04-30 15:48:35 -070055 const F& fetchAllInformation) {
Yifan Hong3daec812017-02-27 18:49:11 -080056 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong44f611c2020-07-21 11:57:57 -070057 if (!ptr->fetchedOnce) {
Steven Morelandec0721d2020-04-30 15:48:35 -070058 LOG(INFO) << id << ": Reading VINTF information.";
Yifan Hong3daec812017-02-27 18:49:11 -080059 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080060 std::string error;
Steven Moreland609d7ff2020-03-27 15:48:40 -070061 status_t status = fetchAllInformation(ptr->object.get(), &error);
Steven Morelandec0721d2020-04-30 15:48:35 -070062 if (status == OK) {
Steven Moreland2cc413f2020-04-30 16:42:56 -070063 ptr->fetchedOnce = true;
Steven Morelandec0721d2020-04-30 15:48:35 -070064 LOG(INFO) << id << ": Successfully processed VINTF information";
65 } else {
66 // Doubled because a malformed error std::string might cause us to
67 // lose the status.
68 LOG(ERROR) << id << ": status from fetching VINTF information: " << status;
69 LOG(ERROR) << id << ": " << status << " VINTF parse error: " << error;
Yifan Hongd7043972020-03-30 13:27:46 -070070 ptr->object = nullptr; // frees the old object
Yifan Hong3daec812017-02-27 18:49:11 -080071 }
72 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070073 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080074}
75
Yifan Hong9f78c182018-07-12 14:45:52 -070076static std::unique_ptr<FileSystem> createDefaultFileSystem() {
77 std::unique_ptr<FileSystem> fileSystem;
78 if (kIsTarget) {
79 fileSystem = std::make_unique<details::FileSystemImpl>();
80 } else {
81 fileSystem = std::make_unique<details::FileSystemNoOp>();
82 }
83 return fileSystem;
84}
85
86static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
87 std::unique_ptr<PropertyFetcher> propertyFetcher;
88 if (kIsTarget) {
89 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
90 } else {
91 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
92 }
93 return propertyFetcher;
94}
95
Yifan Hong9f78c182018-07-12 14:45:52 -070096std::shared_ptr<VintfObject> VintfObject::GetInstance() {
Steven Morelandc16ff2b2020-02-26 17:03:37 -080097 static details::LockedSharedPtr<VintfObject> sInstance{};
Yifan Hong9f78c182018-07-12 14:45:52 -070098 std::unique_lock<std::mutex> lock(sInstance.mutex);
99 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -0800100 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -0700101 }
102 return sInstance.object;
103}
104
Yifan Hong44f611c2020-07-21 11:57:57 -0700105std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest() {
106 return GetInstance()->getDeviceHalManifest();
Yifan Hong3daec812017-02-27 18:49:11 -0800107}
108
Yifan Hong44f611c2020-07-21 11:57:57 -0700109std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest() {
110 return Get(__func__, &mDeviceManifest,
Yifan Hong9f78c182018-07-12 14:45:52 -0700111 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
112}
113
Yifan Hong44f611c2020-07-21 11:57:57 -0700114std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest() {
115 return GetInstance()->getFrameworkHalManifest();
Yifan Hong3daec812017-02-27 18:49:11 -0800116}
117
Yifan Hong44f611c2020-07-21 11:57:57 -0700118std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest() {
119 return Get(__func__, &mFrameworkManifest,
Yifan Hong9f78c182018-07-12 14:45:52 -0700120 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
121}
Yifan Hong2272bf82017-04-28 14:37:56 -0700122
Yifan Hong44f611c2020-07-21 11:57:57 -0700123std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix() {
124 return GetInstance()->getDeviceCompatibilityMatrix();
Yifan Hong2272bf82017-04-28 14:37:56 -0700125}
126
Yifan Hong44f611c2020-07-21 11:57:57 -0700127std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix() {
128 return Get(__func__, &mDeviceMatrix, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
Yifan Hong9f78c182018-07-12 14:45:52 -0700129}
130
Yifan Hong44f611c2020-07-21 11:57:57 -0700131std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix() {
132 return GetInstance()->getFrameworkCompatibilityMatrix();
Yifan Hong9f78c182018-07-12 14:45:52 -0700133}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800134
Yifan Hong44f611c2020-07-21 11:57:57 -0700135std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix() {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800136 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700137 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800138
Yifan Hong9f78c182018-07-12 14:45:52 -0700139 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800140
141 auto combined =
Yifan Hong44f611c2020-07-21 11:57:57 -0700142 Get(__func__, &mCombinedFrameworkMatrix,
Yifan Hong9f78c182018-07-12 14:45:52 -0700143 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800144 if (combined != nullptr) {
145 return combined;
146 }
147
Yifan Hong44f611c2020-07-21 11:57:57 -0700148 return Get(__func__, &mFrameworkMatrix,
Yifan Hong12e23c22018-11-05 14:53:30 -0800149 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700150 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700151}
152
Yifan Hong9f78c182018-07-12 14:45:52 -0700153status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800154 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
155 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700156 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800157 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
158 if (matrixFragmentsStatus != OK) {
159 return matrixFragmentsStatus;
160 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800161 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800162 if (error && error->empty()) {
163 *error = "Cannot get framework matrix for each FCM version for unknown error.";
164 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800165 return NAME_NOT_FOUND;
166 }
167
168 Level deviceLevel = Level::UNSPECIFIED;
169
170 if (deviceManifest != nullptr) {
171 deviceLevel = deviceManifest->level();
172 }
173
174 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800175 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800176 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800177 if (shippingApi != 0u) {
178 deviceLevel = details::convertFromApiLevel(shippingApi);
179 }
180 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800181
182 if (deviceLevel == Level::UNSPECIFIED) {
183 // Cannot infer FCM version. Combine all matrices by assuming
184 // Shipping FCM Version == min(all supported FCM Versions in the framework)
Yifan Honga83d0e42020-04-13 13:07:31 -0700185 for (auto&& fragment : matrixFragments) {
186 Level fragmentLevel = fragment.level();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800187 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
188 deviceLevel = fragmentLevel;
189 }
190 }
191 }
192
193 if (deviceLevel == Level::UNSPECIFIED) {
194 // None of the fragments specify any FCM version. Should never happen except
195 // for inconsistent builds.
196 if (error) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800197 *error = "No framework compatibility matrix files under "s + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800198 " declare FCM version.";
199 }
200 return NAME_NOT_FOUND;
201 }
202
Yifan Honge7837b12018-10-11 10:38:57 -0700203 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800204 if (combined == nullptr) {
205 return BAD_VALUE;
206 }
207 *out = std::move(*combined);
208 return OK;
209}
210
Steven Morelandeedf2d42018-04-04 16:36:27 -0700211// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700212status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700213 std::string* error) {
214 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800215 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700216 // if the directory isn't there, that's okay
217 if (err == NAME_NOT_FOUND) return OK;
218 if (err != OK) return err;
219
220 for (const std::string& file : fileNames) {
221 // Only adds HALs because all other things are added by libvintf
222 // itself for now.
223 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700224 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700225 if (err != OK) return err;
226
Yifan Hong4c6962d2019-01-30 15:50:46 -0800227 if (!manifest->addAll(&fragmentManifest, error)) {
228 if (error) {
Yifan Hong9f9a3192020-04-13 16:39:45 -0700229 error->insert(0, "Cannot add manifest fragment " + directory + file + ": ");
Yifan Hong4c6962d2019-01-30 15:50:46 -0800230 }
231 return UNKNOWN_ERROR;
232 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700233 }
234
235 return OK;
236}
237
Yifan Hong5a93bf22018-01-17 18:22:32 -0800238// Priority for loading vendor manifest:
Roopesh Natarajad629d382020-02-26 09:51:38 -0800239// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
240// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700241// 3. ODM manifest (optional) + odm fragments
242// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800243// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700244// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
245// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700246status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800247 HalManifest vendorManifest;
248 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800249 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
250 return vendorStatus;
251 }
252
Steven Morelandeedf2d42018-04-04 16:36:27 -0700253 if (vendorStatus == OK) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800254 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700255 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700256 if (fragmentStatus != OK) {
257 return fragmentStatus;
258 }
259 }
260
Yifan Hong5a93bf22018-01-17 18:22:32 -0800261 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700262 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800263 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
264 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800265 }
266
Yifan Hong5a93bf22018-01-17 18:22:32 -0800267 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800268 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800269 if (!out->addAll(&odmManifest, error)) {
270 if (error) {
271 error->insert(0, "Cannot add ODM manifest :");
272 }
273 return UNKNOWN_ERROR;
274 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800275 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700276 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800277 }
278
Yifan Hong12a11ac2018-01-19 13:58:32 -0800279 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800280 if (odmStatus == OK) {
281 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700282 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800283 }
284
285 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800286 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800287}
288
Roopesh Natarajad629d382020-02-26 09:51:38 -0800289// Priority:
290// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
291// 2. /vendor/etc/vintf/manifest.xml
292// where:
293// {vendorSku} is the value of ro.boot.product.vendor.sku
294status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
295 status_t status;
296
297 std::string vendorSku;
298 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
299
300 if (!vendorSku.empty()) {
301 status =
Yifan Hongc7a9a292021-02-03 17:27:58 -0800302 fetchOneHalManifest(kVendorVintfDir + "manifest_"s + vendorSku + ".xml", out, error);
Roopesh Natarajad629d382020-02-26 09:51:38 -0800303 if (status == OK || status != NAME_NOT_FOUND) {
304 return status;
305 }
306 }
307
308 status = fetchOneHalManifest(kVendorManifest, out, error);
309 if (status == OK || status != NAME_NOT_FOUND) {
310 return status;
311 }
312
313 return NAME_NOT_FOUND;
314}
315
Yifan Hong12a11ac2018-01-19 13:58:32 -0800316// "out" is written to iff return status is OK.
317// Priority:
318// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
319// 2. /odm/etc/vintf/manifest.xml
320// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
321// 4. /odm/etc/manifest.xml
322// where:
323// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700324status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800325 status_t status;
326
Yifan Hong12a11ac2018-01-19 13:58:32 -0800327 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800328 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800329
330 if (!productModel.empty()) {
331 status =
Yifan Hongc7a9a292021-02-03 17:27:58 -0800332 fetchOneHalManifest(kOdmVintfDir + "manifest_"s + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800333 if (status == OK || status != NAME_NOT_FOUND) {
334 return status;
335 }
336 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800337
Yifan Hong9f78c182018-07-12 14:45:52 -0700338 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800339 if (status == OK || status != NAME_NOT_FOUND) {
340 return status;
341 }
342
Yifan Hong12a11ac2018-01-19 13:58:32 -0800343 if (!productModel.empty()) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800344 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_"s + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800345 error);
346 if (status == OK || status != NAME_NOT_FOUND) {
347 return status;
348 }
349 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800350
Yifan Hong9f78c182018-07-12 14:45:52 -0700351 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800352 if (status == OK || status != NAME_NOT_FOUND) {
353 return status;
354 }
355
356 return NAME_NOT_FOUND;
357}
358
359// Fetch one manifest.xml file. "out" is written to iff return status is OK.
360// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700361status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800362 std::string* error) {
363 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800364 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800365 if (status == OK) {
366 *out = std::move(ret);
367 }
368 return status;
369}
370
Yifan Hong9f78c182018-07-12 14:45:52 -0700371status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800372 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800373 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800374 *out = std::move(etcMatrix);
375 return OK;
376 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800377 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800378}
379
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700380// Priority:
381// 1. /system/etc/vintf/manifest.xml
382// + /system/etc/vintf/manifest/*.xml if they exist
383// + /product/etc/vintf/manifest.xml if it exists
384// + /product/etc/vintf/manifest/*.xml if they exist
385// 2. (deprecated) /system/manifest.xml
Yifan Hongc735be92020-10-12 16:25:50 -0700386status_t VintfObject::fetchUnfilteredFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700387 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
388 if (systemEtcStatus == OK) {
389 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
390 if (dirStatus != OK) {
391 return dirStatus;
392 }
393
Yifan Hongc7a9a292021-02-03 17:27:58 -0800394 std::vector<std::pair<const char*, const char*>> extensions{
Yifan Hong659feac2020-03-19 18:09:54 -0700395 {kProductManifest, kProductManifestFragmentDir},
396 {kSystemExtManifest, kSystemExtManifestFragmentDir},
397 };
398 for (auto&& [manifestPath, frags] : extensions) {
399 HalManifest halManifest;
400 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
401 if (status != OK && status != NAME_NOT_FOUND) {
402 return status;
403 }
404 if (status == OK) {
405 if (!out->addAll(&halManifest, error)) {
406 if (error) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800407 error->insert(0, "Cannot add "s + manifestPath + ":");
Yifan Hong659feac2020-03-19 18:09:54 -0700408 }
409 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700410 }
Yifan Hong659feac2020-03-19 18:09:54 -0700411 }
412
413 auto fragmentStatus = addDirectoryManifests(frags, out, error);
414 if (fragmentStatus != OK) {
415 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700416 }
417 }
Yifan Hongc735be92020-10-12 16:25:50 -0700418
Yifan Hong659feac2020-03-19 18:09:54 -0700419 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700420 } else {
421 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
422 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800423 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700424
Yifan Hong12e23c22018-11-05 14:53:30 -0800425 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800426}
427
Yifan Hongc735be92020-10-12 16:25:50 -0700428status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
429 status_t status = fetchUnfilteredFrameworkHalManifest(out, error);
430 if (status != OK) {
431 return status;
432 }
433 filterHalsByDeviceManifestLevel(out);
434 return OK;
435}
436
437void VintfObject::filterHalsByDeviceManifestLevel(HalManifest* out) {
438 auto deviceManifest = getDeviceHalManifest();
439 if (deviceManifest == nullptr) {
440 LOG(WARNING) << "Cannot fetch device manifest to determine target FCM version to "
441 "filter framework manifest HALs.";
442 return;
443 }
444 Level deviceManifestLevel = deviceManifest->level();
445 if (deviceManifestLevel == Level::UNSPECIFIED) {
446 LOG(WARNING)
447 << "Not filtering framework manifest HALs because target FCM version is unspecified.";
448 return;
449 }
450 out->removeHalsIf([deviceManifestLevel](const ManifestHal& hal) {
451 if (hal.getMaxLevel() == Level::UNSPECIFIED) {
452 return false;
453 }
454 return hal.getMaxLevel() < deviceManifestLevel;
455 });
456}
457
Yifan Hong9f8f6562018-11-06 16:26:20 -0800458static void appendLine(std::string* error, const std::string& message) {
459 if (error != nullptr) {
460 if (!error->empty()) *error += "\n";
461 *error += message;
462 }
463}
464
Yifan Honga83d0e42020-04-13 13:07:31 -0700465status_t VintfObject::getOneMatrix(const std::string& path, CompatibilityMatrix* out,
Yifan Hong73bde592019-01-22 13:30:23 -0800466 std::string* error) {
467 std::string content;
468 status_t status = getFileSystem()->fetch(path, &content, error);
469 if (status != OK) {
470 return status;
471 }
Yifan Hong25e2a772021-04-16 18:34:28 -0700472 if (!fromXml(out, content, error)) {
Yifan Hong73bde592019-01-22 13:30:23 -0800473 if (error) {
474 error->insert(0, "Cannot parse " + path + ": ");
475 }
476 return BAD_VALUE;
477 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700478 out->setFileName(path);
Yifan Hong73bde592019-01-22 13:30:23 -0800479 return OK;
480}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800481
Yifan Honga83d0e42020-04-13 13:07:31 -0700482status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix>* results,
Yifan Hong73bde592019-01-22 13:30:23 -0800483 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800484 std::vector<std::string> dirs = {
485 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800486 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800487 kProductVintfDir,
488 };
489 for (const auto& dir : dirs) {
490 std::vector<std::string> fileNames;
491 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
492 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800493 continue;
494 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800495 if (listStatus != OK) {
496 return listStatus;
497 }
498 for (const std::string& fileName : fileNames) {
499 std::string path = dir + fileName;
Yifan Honga83d0e42020-04-13 13:07:31 -0700500 CompatibilityMatrix namedMatrix;
Yifan Hongb02d87d2019-12-19 11:15:27 -0800501 std::string matrixError;
502 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
503 if (matrixStatus != OK) {
504 // Manifests and matrices share the same dir. Client may not have enough
505 // permissions to read system manifests, or may not be able to parse it.
506 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
507 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
508 continue;
509 }
510 results->emplace_back(std::move(namedMatrix));
511 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800512
Yifan Hongb02d87d2019-12-19 11:15:27 -0800513 if (dir == kSystemVintfDir && results->empty()) {
514 if (error) {
515 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
516 }
517 return NAME_NOT_FOUND;
518 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800519 }
520
Yifan Hong73bde592019-01-22 13:30:23 -0800521 if (results->empty()) {
522 if (error) {
523 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800524 "No framework matrices can be fetched or parsed. "
525 "The following directories are searched:\n " +
526 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800527 }
528 return NAME_NOT_FOUND;
529 }
530 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800531}
532
Yifan Honga192fa52020-07-21 12:09:20 -0700533std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(RuntimeInfo::FetchFlags flags) {
534 return GetInstance()->getRuntimeInfo(flags);
Yifan Hong9f78c182018-07-12 14:45:52 -0700535}
Yifan Honga192fa52020-07-21 12:09:20 -0700536std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700537 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700538
Yifan Honga192fa52020-07-21 12:09:20 -0700539 // Skip fetching information that has already been fetched previously.
540 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700541
Yifan Hong9f78c182018-07-12 14:45:52 -0700542 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800543 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700544 }
545
Yifan Hong9f78c182018-07-12 14:45:52 -0700546 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700547 if (status != OK) {
Yifan Hong7becb062020-11-13 15:54:58 -0800548 // If only kernel FCM is needed, ignore errors when fetching RuntimeInfo because RuntimeInfo
549 // is not available on host. On host, the kernel level can still be inferred from device
550 // manifest.
551 // If other information is needed, flag the error by returning nullptr.
552 auto allExceptKernelFcm = RuntimeInfo::FetchFlag::ALL & ~RuntimeInfo::FetchFlag::KERNEL_FCM;
553 bool needDeviceRuntimeInfo = flags & allExceptKernelFcm;
554 if (needDeviceRuntimeInfo) {
555 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
556 return nullptr;
557 }
558 }
559
560 // To support devices without GKI, RuntimeInfo::fetchAllInformation does not report errors
561 // if kernel level cannot be retrieved. If so, fetch kernel FCM version from device HAL
562 // manifest and store it in RuntimeInfo too.
563 if (flags & RuntimeInfo::FetchFlag::KERNEL_FCM) {
564 Level deviceManifestKernelLevel = Level::UNSPECIFIED;
565 auto manifest = getDeviceHalManifest();
566 if (manifest) {
567 deviceManifestKernelLevel = manifest->inferredKernelLevel();
568 }
569 if (deviceManifestKernelLevel != Level::UNSPECIFIED) {
570 Level kernelLevel = mDeviceRuntimeInfo.object->kernelLevel();
571 if (kernelLevel == Level::UNSPECIFIED) {
572 mDeviceRuntimeInfo.object->setKernelLevel(deviceManifestKernelLevel);
573 } else if (kernelLevel != deviceManifestKernelLevel) {
574 LOG(WARNING) << "uname() reports kernel level " << kernelLevel
575 << " but device manifest sets kernel level "
576 << deviceManifestKernelLevel << ". Using kernel level " << kernelLevel;
577 }
578 }
Yifan Hong1fb004e2017-09-26 15:04:44 -0700579 }
580
Yifan Hong9f78c182018-07-12 14:45:52 -0700581 mDeviceRuntimeInfo.fetchedFlags |= flags;
582 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800583}
584
Yifan Hong12e23c22018-11-05 14:53:30 -0800585int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
586 status_t status = OK;
587 // null checks for files and runtime info
588 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700589 appendLine(error, "No framework manifest file from device or from update package");
590 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700591 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800592 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700593 appendLine(error, "No device manifest file from device or from update package");
594 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700595 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800596 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700597 appendLine(error, "No framework matrix file from device or from update package");
598 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700599 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800600 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700601 appendLine(error, "No device matrix file from device or from update package");
602 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700603 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800604
Yifan Hong072f12d2018-08-08 13:04:51 -0700605 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800606 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700607 appendLine(error, "No runtime info from device");
608 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800609 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700610 }
Yifan Hong878556e2018-07-13 13:45:30 -0700611 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700612
613 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800614 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800615 if (error) {
616 error->insert(0,
617 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700618 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800619 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700620 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800621 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800622 if (error) {
623 error->insert(0,
624 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700625 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800626 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700627 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800628
Yifan Hong072f12d2018-08-08 13:04:51 -0700629 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800630 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800631 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800632 if (error) {
633 error->insert(0,
634 "Runtime info and framework compatibility matrix are incompatible: ");
635 }
636 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700637 }
638 }
639
640 return COMPATIBLE;
641}
642
Yifan Hong9f78c182018-07-12 14:45:52 -0700643namespace details {
644
Yifan Hong69c1b112018-02-27 17:06:00 -0800645std::vector<std::string> dumpFileList() {
646 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800647 // clang-format off
648 kSystemVintfDir,
649 kVendorVintfDir,
650 kOdmVintfDir,
651 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800652 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800653 kOdmLegacyVintfDir,
654 kVendorLegacyManifest,
655 kVendorLegacyMatrix,
656 kSystemLegacyManifest,
657 kSystemLegacyMatrix,
658 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800659 };
660}
661
Yifan Hong9f78c182018-07-12 14:45:52 -0700662} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700663
Yifan Hong9f78c182018-07-12 14:45:52 -0700664bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800665 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700666 const ListInstances& listInstances,
667 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700668 bool isDeprecated = false;
669 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700670 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
671 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700672 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800673 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700674 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700675 });
676 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800677}
678
Yifan Hong03ea4282020-03-17 17:58:35 -0700679// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
680// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
681// matches instancePattern, return true iff for all child interfaces (from
682// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700683bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800684 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700685 const ListInstances& listInstances,
686 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700687 const std::string& package = oldMatrixInstance.package();
688 const Version& version = oldMatrixInstance.versionRange().minVer();
689 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700690
Yifan Honga8a8fa92018-03-20 14:42:43 -0700691 std::vector<std::string> instanceHint;
692 if (!oldMatrixInstance.isRegex()) {
693 instanceHint.push_back(oldMatrixInstance.exactInstance());
694 }
695
Yifan Hong03ea4282020-03-17 17:58:35 -0700696 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700697 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hong03ea4282020-03-17 17:58:35 -0700698
Yifan Honga8a8fa92018-03-20 14:42:43 -0700699 for (const auto& pair : list) {
700 const std::string& servedInstance = pair.first;
701 Version servedVersion = pair.second;
Yifan Hong03ea4282020-03-17 17:58:35 -0700702 std::string servedFqInstanceString =
703 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700704 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700705 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700706 continue;
707 }
708
Yifan Hong03ea4282020-03-17 17:58:35 -0700709 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
710 servedInstance, listInstances, childrenMap);
711 if (!inheritance.has_value()) {
712 accumulatedErrors.push_back(inheritance.error().message());
713 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800714 }
715
Yifan Hong03ea4282020-03-17 17:58:35 -0700716 std::vector<std::string> errors;
717 for (const auto& fqInstance : *inheritance) {
718 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
719 fqInstance, listInstances);
720 if (result.ok()) {
721 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700722 break;
723 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700724 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700725 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800726
Yifan Hong03ea4282020-03-17 17:58:35 -0700727 if (errors.empty()) {
728 continue;
729 }
730 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
731 }
732
733 if (accumulatedErrors.empty()) {
734 return false;
735 }
736 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
737 return true;
738}
739
740// Check if fqInstance is listed in |listInstances|.
741bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
742 const FqInstance& fqInstance) {
743 auto list =
744 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
745 {fqInstance.getInstance()} /* instanceHint*/);
746 return std::any_of(list.begin(), list.end(),
747 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
748}
749
750// Return a list of FqInstance, where each element:
751// - is listed in |listInstances|; AND
752// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
753android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
754 const std::string& package, const Version& version, const std::string& interface,
755 const std::string& instance, const ListInstances& listInstances,
756 const ChildrenMap& childrenMap) {
757 FqInstance fqInstance;
758 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
759 return android::base::Error() << toFQNameString(package, version, interface, instance)
760 << " is not a valid FqInstance";
761 }
762
763 if (!IsInstanceListed(listInstances, fqInstance)) {
764 return {};
765 }
766
767 const FQName& fqName = fqInstance.getFqName();
768
769 std::vector<FqInstance> ret;
770 ret.push_back(fqInstance);
771
772 auto childRange = childrenMap.equal_range(fqName.string());
773 for (auto it = childRange.first; it != childRange.second; ++it) {
774 const auto& childFqNameString = it->second;
775 FQName childFqName;
776 if (!childFqName.setTo(childFqNameString)) {
777 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
778 }
779 FqInstance childFqInstance;
780 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
781 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
782 << fqInstance.getInstance() << " as FqInstance";
783 continue;
784 }
785 if (!IsInstanceListed(listInstances, childFqInstance)) {
786 continue;
787 }
788 ret.push_back(childFqInstance);
789 }
790 return ret;
791}
792
793// Check if |fqInstance| is in |targetMatrix|; essentially equal to
794// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
795// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
796// 2. package@x.z::interface/servedInstance is in targetMatrix but
797// servedInstance is not in listInstances(package@x.z::interface)
798android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
799 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
800 const ListInstances& listInstances) {
801 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
802 bool foundInstance = false;
803 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
804 targetMatrix.forEachInstanceOfPackage(
805 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
806 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
807 targetMatrixInstance.interface() == fqInstance.getInterface() &&
808 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
809 targetMatrixMinVer =
810 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
811 foundInstance = true;
812 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800813 return true;
Yifan Hong03ea4282020-03-17 17:58:35 -0700814 });
815 if (!foundInstance) {
816 return android::base::Error()
817 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
818 << targetMatrix.level() << "; it should not be served.";
819 }
820
821 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
822 bool targetVersionServed = false;
823 for (const auto& newPair :
824 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
825 {fqInstance.getInstance()} /* instanceHint */)) {
826 if (newPair.first == fqInstance.getInstance()) {
827 targetVersionServed = true;
828 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800829 }
830 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700831
Yifan Hong03ea4282020-03-17 17:58:35 -0700832 if (!targetVersionServed) {
833 return android::base::Error()
834 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
835 }
836 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800837}
838
Yifan Hong03ea4282020-03-17 17:58:35 -0700839int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
840 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
841 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700842 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800843 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
844 if (matrixFragmentsStatus != OK) {
845 return matrixFragmentsStatus;
846 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800847 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800848 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800849 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800850 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800851 return NAME_NOT_FOUND;
852 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700853 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800854 if (deviceManifest == nullptr) {
855 if (error) *error = "No device manifest.";
856 return NAME_NOT_FOUND;
857 }
858 Level deviceLevel = deviceManifest->level();
859 if (deviceLevel == Level::UNSPECIFIED) {
860 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
861 return BAD_VALUE;
862 }
863
Daniel Normanedf80402021-03-29 16:49:22 -0700864 std::vector<CompatibilityMatrix> targetMatrices;
865 // Partition matrixFragments into two groups, where the second group
866 // contains all matrices whose level == deviceLevel.
867 auto targetMatricesPartition = std::partition(
868 matrixFragments.begin(), matrixFragments.end(),
869 [&](const CompatibilityMatrix& matrix) { return matrix.level() != deviceLevel; });
870 // Move these matrices into the targetMatrices vector...
871 std::move(targetMatricesPartition, matrixFragments.end(), std::back_inserter(targetMatrices));
872 if (targetMatrices.empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800873 if (error)
874 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
875 return NAME_NOT_FOUND;
876 }
Daniel Normanedf80402021-03-29 16:49:22 -0700877 // so that they can be combined into one matrix for deprecation checking.
878 auto targetMatrix = CompatibilityMatrix::combine(deviceLevel, &targetMatrices, error);
879 if (targetMatrix == nullptr) {
880 return BAD_VALUE;
881 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800882
Yifan Hong03ea4282020-03-17 17:58:35 -0700883 std::multimap<std::string, std::string> childrenMap;
884 for (const auto& child : hidlMetadata) {
885 for (const auto& parent : child.inherited) {
886 childrenMap.emplace(parent, child.name);
887 }
888 }
889
890 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
891 // Matrices with unspecified level are considered "current".
892 bool isDeprecated = false;
Daniel Normanedf80402021-03-29 16:49:22 -0700893 for (auto it = matrixFragments.begin(); it < targetMatricesPartition; ++it) {
894 const auto& namedMatrix = *it;
Yifan Honga83d0e42020-04-13 13:07:31 -0700895 if (namedMatrix.level() == Level::UNSPECIFIED) continue;
Daniel Normanedf80402021-03-29 16:49:22 -0700896 if (namedMatrix.level() > deviceLevel) continue;
Yifan Honga83d0e42020-04-13 13:07:31 -0700897 for (const MatrixHal& hal : namedMatrix.getHals()) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700898 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
899 isDeprecated = true;
900 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800901 }
902 }
903
Yifan Hong03ea4282020-03-17 17:58:35 -0700904 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800905}
906
Yifan Hong03ea4282020-03-17 17:58:35 -0700907int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
908 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800909 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700910 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700911 ListInstances inManifest =
912 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
913 const std::vector<std::string>& /* hintInstances */) {
914 std::vector<std::pair<std::string, Version>> ret;
915 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700916 HalFormat::HIDL, package, version, interface,
917 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700918 ret.push_back(
919 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
920 return true;
921 });
922 return ret;
923 };
Yifan Hong03ea4282020-03-17 17:58:35 -0700924 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800925}
Yifan Hong3daec812017-02-27 18:49:11 -0800926
Yifan Hong378c4082019-12-23 13:10:57 -0800927Level VintfObject::getKernelLevel(std::string* error) {
Yifan Hong2d644112020-11-12 16:11:24 -0800928 auto runtimeInfo = getRuntimeInfo(RuntimeInfo::FetchFlag::KERNEL_FCM);
929 if (!runtimeInfo) {
930 if (error) *error = "Cannot retrieve runtime info with kernel level.";
Yifan Hong378c4082019-12-23 13:10:57 -0800931 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700932 }
Yifan Hong2d644112020-11-12 16:11:24 -0800933 if (runtimeInfo->kernelLevel() != Level::UNSPECIFIED) {
934 return runtimeInfo->kernelLevel();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700935 }
Yifan Hong2d644112020-11-12 16:11:24 -0800936 if (error) {
937 *error = "Both device manifest and kernel release do not specify kernel FCM version.";
938 }
Yifan Hong378c4082019-12-23 13:10:57 -0800939 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700940}
941
Yifan Hong9f78c182018-07-12 14:45:52 -0700942const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
943 return mFileSystem;
944}
945
Yifan Hong9f78c182018-07-12 14:45:52 -0700946const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
947 return mPropertyFetcher;
948}
949
Yifan Hongd038b2b2018-11-27 13:57:56 -0800950const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700951 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700952}
953
Yifan Hong6b860852020-03-19 23:12:07 +0000954android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
Yifan Honga83d0e42020-04-13 13:07:31 -0700955 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong6b860852020-03-19 23:12:07 +0000956 std::string error;
957 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
958 if (status != OK) {
959 return android::base::Error(-status)
960 << "Cannot get all framework matrix fragments: " << error;
961 }
962 for (const auto& namedMatrix : matrixFragments) {
963 // Returns true if product matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700964 if (android::base::StartsWith(namedMatrix.fileName(), kProductVintfDir)) {
Yifan Hong6b860852020-03-19 23:12:07 +0000965 return true;
966 }
Yifan Hongf6ff4272020-03-12 22:56:16 -0700967 // Returns true if system_ext matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700968 if (android::base::StartsWith(namedMatrix.fileName(), kSystemExtVintfDir)) {
Yifan Hongf6ff4272020-03-12 22:56:16 -0700969 return true;
970 }
Yifan Hong6b860852020-03-19 23:12:07 +0000971 // Returns true if device system matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700972 if (android::base::StartsWith(namedMatrix.fileName(), kSystemVintfDir) &&
973 namedMatrix.level() == Level::UNSPECIFIED && !namedMatrix.getHals().empty()) {
Yifan Hong6b860852020-03-19 23:12:07 +0000974 return true;
975 }
976 }
977 return false;
978}
979
Yifan Hongd7924ff2020-03-17 14:09:10 -0700980android::base::Result<void> VintfObject::checkUnusedHals(
981 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6b860852020-03-19 23:12:07 +0000982 auto matrix = getFrameworkCompatibilityMatrix();
983 if (matrix == nullptr) {
984 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
985 }
986 auto manifest = getDeviceHalManifest();
987 if (manifest == nullptr) {
988 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
989 }
Yifan Hongd7924ff2020-03-17 14:09:10 -0700990 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6b860852020-03-19 23:12:07 +0000991 if (!unused.empty()) {
992 return android::base::Error()
993 << "The following instances are in the device manifest but "
994 << "not specified in framework compatibility matrix: \n"
995 << " " << android::base::Join(unused, "\n ") << "\n"
996 << "Suggested fix:\n"
Yifan Hong5a220b12020-04-07 15:22:24 -0700997 << "1. Update deprecated HALs to the latest version.\n"
998 << "2. Check for any typos in device manifest or framework compatibility "
Yifan Hong6b860852020-03-19 23:12:07 +0000999 << "matrices with FCM version >= " << matrix->level() << ".\n"
Yifan Hong5a220b12020-04-07 15:22:24 -07001000 << "3. For new platform HALs, add them to any framework compatibility matrix "
1001 << "with FCM version >= " << matrix->level() << " where applicable.\n"
1002 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
Yifan Hong6b860852020-03-19 23:12:07 +00001003 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
1004 }
1005 return {};
1006}
1007
Yifan Hongd7043972020-03-30 13:27:46 -07001008namespace {
1009
1010// Insert |name| into |ret| if shouldCheck(name).
1011void InsertIf(const std::string& name, const std::function<bool(const std::string&)>& shouldCheck,
1012 std::set<std::string>* ret) {
1013 if (shouldCheck(name)) ret->insert(name);
1014}
1015
1016std::string StripHidlInterface(const std::string& fqNameString) {
1017 FQName fqName;
1018 if (!fqName.setTo(fqNameString)) {
1019 return "";
1020 }
1021 return fqName.getPackageAndVersion().string();
1022}
1023
1024std::string StripAidlType(const std::string& type) {
1025 auto items = android::base::Split(type, ".");
1026 if (items.empty()) {
1027 return "";
1028 }
1029 items.erase(items.end() - 1);
1030 return android::base::Join(items, ".");
1031}
1032
Yifan Hong8c61c752021-02-01 19:21:03 -08001033// android.hardware.foo@1.0
1034std::set<std::string> HidlMetadataToPackagesAndVersions(
Yifan Hongd7043972020-03-30 13:27:46 -07001035 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1036 const std::function<bool(const std::string&)>& shouldCheck) {
1037 std::set<std::string> ret;
1038 for (const auto& item : hidlMetadata) {
1039 InsertIf(StripHidlInterface(item.name), shouldCheck, &ret);
1040 }
1041 return ret;
1042}
1043
Yifan Hong8c61c752021-02-01 19:21:03 -08001044// android.hardware.foo
1045std::set<std::string> AidlMetadataToPackages(
Yifan Hongd7043972020-03-30 13:27:46 -07001046 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1047 const std::function<bool(const std::string&)>& shouldCheck) {
1048 std::set<std::string> ret;
1049 for (const auto& item : aidlMetadata) {
1050 for (const auto& type : item.types) {
1051 InsertIf(StripAidlType(type), shouldCheck, &ret);
1052 }
1053 }
1054 return ret;
1055}
1056
Yifan Hong9db8ab12021-02-01 19:21:30 -08001057// android.hardware.foo@1.0::IFoo.
1058// Note that UDTs are not filtered out, so there might be non-interface types.
1059std::set<std::string> HidlMetadataToNames(const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
1060 std::set<std::string> ret;
1061 for (const auto& item : hidlMetadata) {
1062 ret.insert(item.name);
1063 }
1064 return ret;
1065}
1066
1067// android.hardware.foo.IFoo
1068// Note that UDTs are not filtered out, so there might be non-interface types.
1069std::set<std::string> AidlMetadataToNames(const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1070 std::set<std::string> ret;
1071 for (const auto& item : aidlMetadata) {
1072 for (const auto& type : item.types) {
1073 ret.insert(type);
1074 }
1075 }
1076 return ret;
1077}
1078
Yifan Hongd7043972020-03-30 13:27:46 -07001079} // anonymous namespace
1080
Yifan Hong8c61c752021-02-01 19:21:03 -08001081android::base::Result<std::vector<CompatibilityMatrix>> VintfObject::getAllFrameworkMatrixLevels() {
Yifan Hongd7043972020-03-30 13:27:46 -07001082 // Get all framework matrix fragments instead of the combined framework compatibility matrix
1083 // because the latter may omit interfaces from the latest FCM if device target-level is not
1084 // the latest.
1085 std::vector<CompatibilityMatrix> matrixFragments;
1086 std::string error;
1087 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, &error);
1088 if (matrixFragmentsStatus != OK) {
1089 return android::base::Error(-matrixFragmentsStatus)
1090 << "Unable to get all framework matrix fragments: " << error;
1091 }
1092 if (matrixFragments.empty()) {
1093 if (error.empty()) {
1094 error = "Cannot get framework matrix for each FCM version for unknown error.";
1095 }
1096 return android::base::Error(-NAME_NOT_FOUND) << error;
1097 }
Yifan Hong8c61c752021-02-01 19:21:03 -08001098 return matrixFragments;
1099}
1100
1101android::base::Result<void> VintfObject::checkMissingHalsInMatrices(
1102 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1103 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1104 std::function<bool(const std::string&)> shouldCheck) {
1105 if (!shouldCheck) {
1106 shouldCheck = [](const auto&) { return true; };
1107 }
1108
1109 auto matrixFragments = getAllFrameworkMatrixLevels();
1110 if (!matrixFragments.ok()) return matrixFragments.error();
Yifan Hongd7043972020-03-30 13:27:46 -07001111
1112 // Filter aidlMetadata and hidlMetadata with shouldCheck.
Yifan Hong8c61c752021-02-01 19:21:03 -08001113 auto allAidlPackages = AidlMetadataToPackages(aidlMetadata, shouldCheck);
1114 auto allHidlPackagesAndVersions = HidlMetadataToPackagesAndVersions(hidlMetadata, shouldCheck);
Yifan Hongd7043972020-03-30 13:27:46 -07001115
1116 // Filter out instances in allAidlMetadata and allHidlMetadata that are in the matrices.
1117 std::vector<std::string> errors;
Yifan Hong8c61c752021-02-01 19:21:03 -08001118 for (const auto& matrix : matrixFragments.value()) {
Yifan Hongd7043972020-03-30 13:27:46 -07001119 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1120 switch (matrixInstance.format()) {
1121 case HalFormat::AIDL: {
Yifan Hong8c61c752021-02-01 19:21:03 -08001122 allAidlPackages.erase(matrixInstance.package());
Yifan Hongd7043972020-03-30 13:27:46 -07001123 return true; // continue to next instance
1124 }
1125 case HalFormat::HIDL: {
1126 for (Version v = matrixInstance.versionRange().minVer();
1127 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
Yifan Hong8c61c752021-02-01 19:21:03 -08001128 allHidlPackagesAndVersions.erase(
1129 toFQNameString(matrixInstance.package(), v));
Yifan Hongd7043972020-03-30 13:27:46 -07001130 }
1131 return true; // continue to next instance
1132 }
1133 default: {
1134 if (shouldCheck(matrixInstance.package())) {
1135 errors.push_back("HAL package " + matrixInstance.package() +
1136 " is not allowed to have format " +
1137 to_string(matrixInstance.format()) + ".");
1138 }
1139 return true; // continue to next instance
1140 }
1141 }
1142 });
1143 }
1144
Yifan Hong8c61c752021-02-01 19:21:03 -08001145 if (!allHidlPackagesAndVersions.empty()) {
Yifan Hongd7043972020-03-30 13:27:46 -07001146 errors.push_back(
1147 "The following HIDL packages are not found in any compatibility matrix fragments:\t\n" +
Yifan Hong8c61c752021-02-01 19:21:03 -08001148 android::base::Join(allHidlPackagesAndVersions, "\t\n"));
Yifan Hongd7043972020-03-30 13:27:46 -07001149 }
Yifan Hong8c61c752021-02-01 19:21:03 -08001150 if (!allAidlPackages.empty()) {
Yifan Hongd7043972020-03-30 13:27:46 -07001151 errors.push_back(
1152 "The following AIDL packages are not found in any compatibility matrix fragments:\t\n" +
Yifan Hong8c61c752021-02-01 19:21:03 -08001153 android::base::Join(allAidlPackages, "\t\n"));
Yifan Hongd7043972020-03-30 13:27:46 -07001154 }
1155
1156 if (!errors.empty()) {
1157 return android::base::Error() << android::base::Join(errors, "\n");
1158 }
1159
1160 return {};
1161}
1162
Yifan Hong9db8ab12021-02-01 19:21:30 -08001163android::base::Result<void> VintfObject::checkMatrixHalsHasDefinition(
1164 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1165 const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1166 auto matrixFragments = getAllFrameworkMatrixLevels();
1167 if (!matrixFragments.ok()) return matrixFragments.error();
1168
1169 auto allAidlNames = AidlMetadataToNames(aidlMetadata);
1170 auto allHidlNames = HidlMetadataToNames(hidlMetadata);
1171 std::set<std::string> badAidlInterfaces;
1172 std::set<std::string> badHidlInterfaces;
1173
1174 std::vector<std::string> errors;
1175 for (const auto& matrix : matrixFragments.value()) {
1176 if (matrix.level() == Level::UNSPECIFIED) {
1177 LOG(INFO) << "Skip checkMatrixHalsHasDefinition() on " << matrix.fileName()
1178 << " with no level.";
1179 continue;
1180 }
1181
1182 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1183 switch (matrixInstance.format()) {
1184 case HalFormat::AIDL: {
1185 auto matrixInterface =
1186 toAidlFqnameString(matrixInstance.package(), matrixInstance.interface());
1187 if (allAidlNames.find(matrixInterface) == allAidlNames.end()) {
1188 errors.push_back(
1189 "AIDL interface " + matrixInterface + " is referenced in " +
1190 matrix.fileName() +
1191 ", but there is no corresponding .aidl definition associated with an "
1192 "aidl_interface module in this build. Typo?");
1193 }
1194 return true; // continue to next instance
1195 }
1196 case HalFormat::HIDL: {
1197 for (Version v = matrixInstance.versionRange().minVer();
1198 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1199 auto matrixInterface = matrixInstance.interfaceDescription(v);
1200 if (allHidlNames.find(matrixInterface) == allHidlNames.end()) {
1201 errors.push_back(
1202 "HIDL interface " + matrixInterface + " is referenced in " +
1203 matrix.fileName() +
1204 ", but there is no corresponding .hal definition associated with "
1205 "a hidl_interface module in this build. Typo?");
1206 }
1207 }
1208 return true; // continue to next instance
1209 }
1210 default: {
1211 // We do not have data for native HALs.
1212 return true; // continue to next instance
1213 }
1214 }
1215 });
Yifan Hong3daec812017-02-27 18:49:11 -08001216 }
1217
1218 if (!errors.empty()) {
1219 return android::base::Error() << android::base::Join(errors, "\n");
1220 }
1221
1222 return {};
1223}
1224
1225// make_unique does not work because VintfObject constructor is private.
1226VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
1227
1228VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
1229 mObject->mFileSystem = std::move(e);
1230 return *this;
1231}
1232
1233VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
1234 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
1235 mObject->mRuntimeInfoFactory = std::move(e);
1236 return *this;
1237}
1238
1239VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
1240 std::unique_ptr<PropertyFetcher>&& e) {
1241 mObject->mPropertyFetcher = std::move(e);
1242 return *this;
1243}
1244
1245std::unique_ptr<VintfObject> VintfObject::Builder::build() {
1246 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1247 if (!mObject->mRuntimeInfoFactory)
1248 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1249 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1250 return std::move(mObject);
1251}
1252
Yifan Hongd7043972020-03-30 13:27:46 -07001253} // namespace vintf
1254} // namespace android