blob: c9460a0c1ddeffb8eda941d66d8a1835a37299b0 [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 Hong507815b2021-05-21 16:48:37 -0700139 std::string error;
140 auto kernelLevel = getKernelLevel(&error);
141 if (kernelLevel == Level::UNSPECIFIED) {
142 LOG(WARNING) << "getKernelLevel: " << error;
143 }
144
Yifan Hong9f78c182018-07-12 14:45:52 -0700145 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800146
Yifan Hong507815b2021-05-21 16:48:37 -0700147 auto combined = Get(__func__, &mCombinedFrameworkMatrix,
148 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest,
149 kernelLevel, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800150 if (combined != nullptr) {
151 return combined;
152 }
153
Yifan Hong44f611c2020-07-21 11:57:57 -0700154 return Get(__func__, &mFrameworkMatrix,
Yifan Hong12e23c22018-11-05 14:53:30 -0800155 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700156 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700157}
158
Yifan Hong9f78c182018-07-12 14:45:52 -0700159status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hong507815b2021-05-21 16:48:37 -0700160 const std::shared_ptr<const HalManifest>& deviceManifest, Level kernelLevel,
161 CompatibilityMatrix* out, std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700162 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800163 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
164 if (matrixFragmentsStatus != OK) {
165 return matrixFragmentsStatus;
166 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800167 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800168 if (error && error->empty()) {
169 *error = "Cannot get framework matrix for each FCM version for unknown error.";
170 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800171 return NAME_NOT_FOUND;
172 }
173
174 Level deviceLevel = Level::UNSPECIFIED;
175
176 if (deviceManifest != nullptr) {
177 deviceLevel = deviceManifest->level();
178 }
179
180 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800181 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800182 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800183 if (shippingApi != 0u) {
184 deviceLevel = details::convertFromApiLevel(shippingApi);
185 }
186 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800187
188 if (deviceLevel == Level::UNSPECIFIED) {
189 // Cannot infer FCM version. Combine all matrices by assuming
190 // Shipping FCM Version == min(all supported FCM Versions in the framework)
Yifan Honga83d0e42020-04-13 13:07:31 -0700191 for (auto&& fragment : matrixFragments) {
192 Level fragmentLevel = fragment.level();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800193 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
194 deviceLevel = fragmentLevel;
195 }
196 }
197 }
198
199 if (deviceLevel == Level::UNSPECIFIED) {
200 // None of the fragments specify any FCM version. Should never happen except
201 // for inconsistent builds.
202 if (error) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800203 *error = "No framework compatibility matrix files under "s + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800204 " declare FCM version.";
205 }
206 return NAME_NOT_FOUND;
207 }
208
Yifan Hong507815b2021-05-21 16:48:37 -0700209 auto combined = CompatibilityMatrix::combine(deviceLevel, kernelLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800210 if (combined == nullptr) {
211 return BAD_VALUE;
212 }
213 *out = std::move(*combined);
214 return OK;
215}
216
Steven Morelandeedf2d42018-04-04 16:36:27 -0700217// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700218status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700219 std::string* error) {
220 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800221 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700222 // if the directory isn't there, that's okay
Xin Li12896432021-08-18 08:29:29 +0000223 if (err == NAME_NOT_FOUND) {
224 if (error) {
225 error->clear();
226 }
227 return OK;
228 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700229 if (err != OK) return err;
230
231 for (const std::string& file : fileNames) {
232 // Only adds HALs because all other things are added by libvintf
233 // itself for now.
234 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700235 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700236 if (err != OK) return err;
237
Yifan Hong4c6962d2019-01-30 15:50:46 -0800238 if (!manifest->addAll(&fragmentManifest, error)) {
239 if (error) {
Yifan Hong9f9a3192020-04-13 16:39:45 -0700240 error->insert(0, "Cannot add manifest fragment " + directory + file + ": ");
Yifan Hong4c6962d2019-01-30 15:50:46 -0800241 }
242 return UNKNOWN_ERROR;
243 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700244 }
245
246 return OK;
247}
248
Yifan Hong5a93bf22018-01-17 18:22:32 -0800249// Priority for loading vendor manifest:
Roopesh Natarajad629d382020-02-26 09:51:38 -0800250// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
251// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700252// 3. ODM manifest (optional) + odm fragments
253// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800254// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700255// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
256// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700257status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800258 HalManifest vendorManifest;
259 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800260 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
261 return vendorStatus;
262 }
263
Steven Morelandeedf2d42018-04-04 16:36:27 -0700264 if (vendorStatus == OK) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800265 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700266 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700267 if (fragmentStatus != OK) {
268 return fragmentStatus;
269 }
270 }
271
Yifan Hong5a93bf22018-01-17 18:22:32 -0800272 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700273 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800274 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
275 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800276 }
277
Yifan Hong5a93bf22018-01-17 18:22:32 -0800278 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800279 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800280 if (!out->addAll(&odmManifest, error)) {
281 if (error) {
282 error->insert(0, "Cannot add ODM manifest :");
283 }
284 return UNKNOWN_ERROR;
285 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800286 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700287 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800288 }
289
Yifan Hong12a11ac2018-01-19 13:58:32 -0800290 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800291 if (odmStatus == OK) {
292 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700293 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800294 }
295
296 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800297 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800298}
299
Roopesh Natarajad629d382020-02-26 09:51:38 -0800300// Priority:
301// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
302// 2. /vendor/etc/vintf/manifest.xml
303// where:
304// {vendorSku} is the value of ro.boot.product.vendor.sku
305status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
306 status_t status;
307
308 std::string vendorSku;
309 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
310
311 if (!vendorSku.empty()) {
312 status =
Yifan Hongc7a9a292021-02-03 17:27:58 -0800313 fetchOneHalManifest(kVendorVintfDir + "manifest_"s + vendorSku + ".xml", out, error);
Roopesh Natarajad629d382020-02-26 09:51:38 -0800314 if (status == OK || status != NAME_NOT_FOUND) {
315 return status;
316 }
317 }
318
319 status = fetchOneHalManifest(kVendorManifest, out, error);
320 if (status == OK || status != NAME_NOT_FOUND) {
321 return status;
322 }
323
324 return NAME_NOT_FOUND;
325}
326
Yifan Hong12a11ac2018-01-19 13:58:32 -0800327// "out" is written to iff return status is OK.
328// Priority:
329// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
330// 2. /odm/etc/vintf/manifest.xml
331// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
332// 4. /odm/etc/manifest.xml
333// where:
334// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700335status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800336 status_t status;
337
Yifan Hong12a11ac2018-01-19 13:58:32 -0800338 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800339 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800340
341 if (!productModel.empty()) {
342 status =
Yifan Hongc7a9a292021-02-03 17:27:58 -0800343 fetchOneHalManifest(kOdmVintfDir + "manifest_"s + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800344 if (status == OK || status != NAME_NOT_FOUND) {
345 return status;
346 }
347 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800348
Yifan Hong9f78c182018-07-12 14:45:52 -0700349 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800350 if (status == OK || status != NAME_NOT_FOUND) {
351 return status;
352 }
353
Yifan Hong12a11ac2018-01-19 13:58:32 -0800354 if (!productModel.empty()) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800355 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_"s + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800356 error);
357 if (status == OK || status != NAME_NOT_FOUND) {
358 return status;
359 }
360 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800361
Yifan Hong9f78c182018-07-12 14:45:52 -0700362 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800363 if (status == OK || status != NAME_NOT_FOUND) {
364 return status;
365 }
366
367 return NAME_NOT_FOUND;
368}
369
370// Fetch one manifest.xml file. "out" is written to iff return status is OK.
371// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700372status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800373 std::string* error) {
374 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800375 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800376 if (status == OK) {
377 *out = std::move(ret);
378 }
379 return status;
380}
381
Yifan Hong9f78c182018-07-12 14:45:52 -0700382status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800383 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800384 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800385 *out = std::move(etcMatrix);
386 return OK;
387 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800388 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800389}
390
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700391// Priority:
392// 1. /system/etc/vintf/manifest.xml
393// + /system/etc/vintf/manifest/*.xml if they exist
394// + /product/etc/vintf/manifest.xml if it exists
395// + /product/etc/vintf/manifest/*.xml if they exist
396// 2. (deprecated) /system/manifest.xml
Yifan Hongc735be92020-10-12 16:25:50 -0700397status_t VintfObject::fetchUnfilteredFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700398 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
399 if (systemEtcStatus == OK) {
400 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
401 if (dirStatus != OK) {
402 return dirStatus;
403 }
404
Yifan Hongc7a9a292021-02-03 17:27:58 -0800405 std::vector<std::pair<const char*, const char*>> extensions{
Yifan Hong659feac2020-03-19 18:09:54 -0700406 {kProductManifest, kProductManifestFragmentDir},
407 {kSystemExtManifest, kSystemExtManifestFragmentDir},
408 };
409 for (auto&& [manifestPath, frags] : extensions) {
410 HalManifest halManifest;
411 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
412 if (status != OK && status != NAME_NOT_FOUND) {
413 return status;
414 }
415 if (status == OK) {
416 if (!out->addAll(&halManifest, error)) {
417 if (error) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800418 error->insert(0, "Cannot add "s + manifestPath + ":");
Yifan Hong659feac2020-03-19 18:09:54 -0700419 }
420 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700421 }
Yifan Hong659feac2020-03-19 18:09:54 -0700422 }
423
424 auto fragmentStatus = addDirectoryManifests(frags, out, error);
425 if (fragmentStatus != OK) {
426 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700427 }
428 }
Yifan Hongc735be92020-10-12 16:25:50 -0700429
Yifan Hong659feac2020-03-19 18:09:54 -0700430 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700431 } else {
432 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
433 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800434 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700435
Yifan Hong12e23c22018-11-05 14:53:30 -0800436 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800437}
438
Yifan Hongc735be92020-10-12 16:25:50 -0700439status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
440 status_t status = fetchUnfilteredFrameworkHalManifest(out, error);
441 if (status != OK) {
442 return status;
443 }
444 filterHalsByDeviceManifestLevel(out);
445 return OK;
446}
447
448void VintfObject::filterHalsByDeviceManifestLevel(HalManifest* out) {
449 auto deviceManifest = getDeviceHalManifest();
450 if (deviceManifest == nullptr) {
451 LOG(WARNING) << "Cannot fetch device manifest to determine target FCM version to "
452 "filter framework manifest HALs.";
453 return;
454 }
455 Level deviceManifestLevel = deviceManifest->level();
456 if (deviceManifestLevel == Level::UNSPECIFIED) {
457 LOG(WARNING)
458 << "Not filtering framework manifest HALs because target FCM version is unspecified.";
459 return;
460 }
461 out->removeHalsIf([deviceManifestLevel](const ManifestHal& hal) {
462 if (hal.getMaxLevel() == Level::UNSPECIFIED) {
463 return false;
464 }
465 return hal.getMaxLevel() < deviceManifestLevel;
466 });
467}
468
Yifan Hong9f8f6562018-11-06 16:26:20 -0800469static void appendLine(std::string* error, const std::string& message) {
470 if (error != nullptr) {
471 if (!error->empty()) *error += "\n";
472 *error += message;
473 }
474}
475
Yifan Honga83d0e42020-04-13 13:07:31 -0700476status_t VintfObject::getOneMatrix(const std::string& path, CompatibilityMatrix* out,
Yifan Hong73bde592019-01-22 13:30:23 -0800477 std::string* error) {
478 std::string content;
479 status_t status = getFileSystem()->fetch(path, &content, error);
480 if (status != OK) {
481 return status;
482 }
Yifan Hong25e2a772021-04-16 18:34:28 -0700483 if (!fromXml(out, content, error)) {
Yifan Hong73bde592019-01-22 13:30:23 -0800484 if (error) {
485 error->insert(0, "Cannot parse " + path + ": ");
486 }
487 return BAD_VALUE;
488 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700489 out->setFileName(path);
Yifan Hong73bde592019-01-22 13:30:23 -0800490 return OK;
491}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800492
Yifan Honga83d0e42020-04-13 13:07:31 -0700493status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix>* results,
Yifan Hong73bde592019-01-22 13:30:23 -0800494 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800495 std::vector<std::string> dirs = {
496 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800497 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800498 kProductVintfDir,
499 };
500 for (const auto& dir : dirs) {
501 std::vector<std::string> fileNames;
502 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
503 if (listStatus == NAME_NOT_FOUND) {
Xin Li12896432021-08-18 08:29:29 +0000504 if (error) {
505 error->clear();
506 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800507 continue;
508 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800509 if (listStatus != OK) {
510 return listStatus;
511 }
512 for (const std::string& fileName : fileNames) {
513 std::string path = dir + fileName;
Yifan Honga83d0e42020-04-13 13:07:31 -0700514 CompatibilityMatrix namedMatrix;
Yifan Hongb02d87d2019-12-19 11:15:27 -0800515 std::string matrixError;
516 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
517 if (matrixStatus != OK) {
518 // Manifests and matrices share the same dir. Client may not have enough
519 // permissions to read system manifests, or may not be able to parse it.
520 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
521 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
522 continue;
523 }
524 results->emplace_back(std::move(namedMatrix));
525 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800526
Yifan Hongb02d87d2019-12-19 11:15:27 -0800527 if (dir == kSystemVintfDir && results->empty()) {
528 if (error) {
529 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
530 }
531 return NAME_NOT_FOUND;
532 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800533 }
534
Yifan Hong73bde592019-01-22 13:30:23 -0800535 if (results->empty()) {
536 if (error) {
537 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800538 "No framework matrices can be fetched or parsed. "
539 "The following directories are searched:\n " +
540 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800541 }
542 return NAME_NOT_FOUND;
543 }
544 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800545}
546
Yifan Honga192fa52020-07-21 12:09:20 -0700547std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(RuntimeInfo::FetchFlags flags) {
548 return GetInstance()->getRuntimeInfo(flags);
Yifan Hong9f78c182018-07-12 14:45:52 -0700549}
Yifan Honga192fa52020-07-21 12:09:20 -0700550std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700551 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700552
Yifan Honga192fa52020-07-21 12:09:20 -0700553 // Skip fetching information that has already been fetched previously.
554 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700555
Yifan Hong9f78c182018-07-12 14:45:52 -0700556 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800557 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700558 }
559
Yifan Hong9f78c182018-07-12 14:45:52 -0700560 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700561 if (status != OK) {
Yifan Hong7becb062020-11-13 15:54:58 -0800562 // If only kernel FCM is needed, ignore errors when fetching RuntimeInfo because RuntimeInfo
563 // is not available on host. On host, the kernel level can still be inferred from device
564 // manifest.
565 // If other information is needed, flag the error by returning nullptr.
566 auto allExceptKernelFcm = RuntimeInfo::FetchFlag::ALL & ~RuntimeInfo::FetchFlag::KERNEL_FCM;
567 bool needDeviceRuntimeInfo = flags & allExceptKernelFcm;
568 if (needDeviceRuntimeInfo) {
569 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
570 return nullptr;
571 }
572 }
573
574 // To support devices without GKI, RuntimeInfo::fetchAllInformation does not report errors
575 // if kernel level cannot be retrieved. If so, fetch kernel FCM version from device HAL
576 // manifest and store it in RuntimeInfo too.
577 if (flags & RuntimeInfo::FetchFlag::KERNEL_FCM) {
578 Level deviceManifestKernelLevel = Level::UNSPECIFIED;
579 auto manifest = getDeviceHalManifest();
580 if (manifest) {
581 deviceManifestKernelLevel = manifest->inferredKernelLevel();
582 }
583 if (deviceManifestKernelLevel != Level::UNSPECIFIED) {
584 Level kernelLevel = mDeviceRuntimeInfo.object->kernelLevel();
585 if (kernelLevel == Level::UNSPECIFIED) {
586 mDeviceRuntimeInfo.object->setKernelLevel(deviceManifestKernelLevel);
587 } else if (kernelLevel != deviceManifestKernelLevel) {
588 LOG(WARNING) << "uname() reports kernel level " << kernelLevel
589 << " but device manifest sets kernel level "
590 << deviceManifestKernelLevel << ". Using kernel level " << kernelLevel;
591 }
592 }
Yifan Hong1fb004e2017-09-26 15:04:44 -0700593 }
594
Yifan Hong9f78c182018-07-12 14:45:52 -0700595 mDeviceRuntimeInfo.fetchedFlags |= flags;
596 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800597}
598
Yifan Hong12e23c22018-11-05 14:53:30 -0800599int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
600 status_t status = OK;
601 // null checks for files and runtime info
602 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700603 appendLine(error, "No framework manifest file from device or from update package");
604 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700605 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800606 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700607 appendLine(error, "No device manifest file from device or from update package");
608 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700609 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800610 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700611 appendLine(error, "No framework matrix file from device or from update package");
612 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700613 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800614 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700615 appendLine(error, "No device matrix file from device or from update package");
616 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700617 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800618
Yifan Hong072f12d2018-08-08 13:04:51 -0700619 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800620 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700621 appendLine(error, "No runtime info from device");
622 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800623 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700624 }
Yifan Hong878556e2018-07-13 13:45:30 -0700625 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700626
627 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800628 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800629 if (error) {
630 error->insert(0,
631 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700632 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800633 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700634 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800635 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800636 if (error) {
637 error->insert(0,
638 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700639 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800640 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700641 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800642
Yifan Hong072f12d2018-08-08 13:04:51 -0700643 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800644 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800645 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800646 if (error) {
647 error->insert(0,
648 "Runtime info and framework compatibility matrix are incompatible: ");
649 }
650 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700651 }
652 }
653
654 return COMPATIBLE;
655}
656
Yifan Hong9f78c182018-07-12 14:45:52 -0700657namespace details {
658
Yifan Hong69c1b112018-02-27 17:06:00 -0800659std::vector<std::string> dumpFileList() {
660 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800661 // clang-format off
662 kSystemVintfDir,
663 kVendorVintfDir,
664 kOdmVintfDir,
665 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800666 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800667 kOdmLegacyVintfDir,
668 kVendorLegacyManifest,
669 kVendorLegacyMatrix,
670 kSystemLegacyManifest,
671 kSystemLegacyMatrix,
672 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800673 };
674}
675
Yifan Hong9f78c182018-07-12 14:45:52 -0700676} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700677
Yifan Hong9f78c182018-07-12 14:45:52 -0700678bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800679 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700680 const ListInstances& listInstances,
681 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700682 bool isDeprecated = false;
683 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700684 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
685 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700686 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800687 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700688 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700689 });
690 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800691}
692
Yifan Hong03ea4282020-03-17 17:58:35 -0700693// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
694// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
695// matches instancePattern, return true iff for all child interfaces (from
696// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700697bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800698 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700699 const ListInstances& listInstances,
700 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700701 const std::string& package = oldMatrixInstance.package();
702 const Version& version = oldMatrixInstance.versionRange().minVer();
703 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700704
Yifan Honga8a8fa92018-03-20 14:42:43 -0700705 std::vector<std::string> instanceHint;
706 if (!oldMatrixInstance.isRegex()) {
707 instanceHint.push_back(oldMatrixInstance.exactInstance());
708 }
709
Yifan Hong03ea4282020-03-17 17:58:35 -0700710 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700711 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hong03ea4282020-03-17 17:58:35 -0700712
Yifan Honga8a8fa92018-03-20 14:42:43 -0700713 for (const auto& pair : list) {
714 const std::string& servedInstance = pair.first;
715 Version servedVersion = pair.second;
Yifan Hong03ea4282020-03-17 17:58:35 -0700716 std::string servedFqInstanceString =
717 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700718 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700719 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700720 continue;
721 }
722
Yifan Hong03ea4282020-03-17 17:58:35 -0700723 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
724 servedInstance, listInstances, childrenMap);
725 if (!inheritance.has_value()) {
726 accumulatedErrors.push_back(inheritance.error().message());
727 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800728 }
729
Yifan Hong03ea4282020-03-17 17:58:35 -0700730 std::vector<std::string> errors;
731 for (const auto& fqInstance : *inheritance) {
732 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
733 fqInstance, listInstances);
734 if (result.ok()) {
735 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700736 break;
737 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700738 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700739 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800740
Yifan Hong03ea4282020-03-17 17:58:35 -0700741 if (errors.empty()) {
742 continue;
743 }
744 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
745 }
746
747 if (accumulatedErrors.empty()) {
748 return false;
749 }
750 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
751 return true;
752}
753
754// Check if fqInstance is listed in |listInstances|.
755bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
756 const FqInstance& fqInstance) {
757 auto list =
758 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
759 {fqInstance.getInstance()} /* instanceHint*/);
760 return std::any_of(list.begin(), list.end(),
761 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
762}
763
764// Return a list of FqInstance, where each element:
765// - is listed in |listInstances|; AND
766// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
767android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
768 const std::string& package, const Version& version, const std::string& interface,
769 const std::string& instance, const ListInstances& listInstances,
770 const ChildrenMap& childrenMap) {
771 FqInstance fqInstance;
772 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
773 return android::base::Error() << toFQNameString(package, version, interface, instance)
774 << " is not a valid FqInstance";
775 }
776
777 if (!IsInstanceListed(listInstances, fqInstance)) {
778 return {};
779 }
780
781 const FQName& fqName = fqInstance.getFqName();
782
783 std::vector<FqInstance> ret;
784 ret.push_back(fqInstance);
785
786 auto childRange = childrenMap.equal_range(fqName.string());
787 for (auto it = childRange.first; it != childRange.second; ++it) {
788 const auto& childFqNameString = it->second;
789 FQName childFqName;
790 if (!childFqName.setTo(childFqNameString)) {
791 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
792 }
793 FqInstance childFqInstance;
794 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
795 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
796 << fqInstance.getInstance() << " as FqInstance";
797 continue;
798 }
799 if (!IsInstanceListed(listInstances, childFqInstance)) {
800 continue;
801 }
802 ret.push_back(childFqInstance);
803 }
804 return ret;
805}
806
807// Check if |fqInstance| is in |targetMatrix|; essentially equal to
808// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
809// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
810// 2. package@x.z::interface/servedInstance is in targetMatrix but
811// servedInstance is not in listInstances(package@x.z::interface)
812android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
813 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
814 const ListInstances& listInstances) {
815 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
816 bool foundInstance = false;
817 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
818 targetMatrix.forEachInstanceOfPackage(
819 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
820 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
821 targetMatrixInstance.interface() == fqInstance.getInterface() &&
822 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
823 targetMatrixMinVer =
824 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
825 foundInstance = true;
826 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800827 return true;
Yifan Hong03ea4282020-03-17 17:58:35 -0700828 });
829 if (!foundInstance) {
830 return android::base::Error()
831 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
832 << targetMatrix.level() << "; it should not be served.";
833 }
834
835 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
836 bool targetVersionServed = false;
837 for (const auto& newPair :
838 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
839 {fqInstance.getInstance()} /* instanceHint */)) {
840 if (newPair.first == fqInstance.getInstance()) {
841 targetVersionServed = true;
842 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800843 }
844 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700845
Yifan Hong03ea4282020-03-17 17:58:35 -0700846 if (!targetVersionServed) {
847 return android::base::Error()
848 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
849 }
850 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800851}
852
Yifan Hong03ea4282020-03-17 17:58:35 -0700853int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
854 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
855 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700856 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800857 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
858 if (matrixFragmentsStatus != OK) {
859 return matrixFragmentsStatus;
860 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800861 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800862 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800863 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800864 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800865 return NAME_NOT_FOUND;
866 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700867 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800868 if (deviceManifest == nullptr) {
869 if (error) *error = "No device manifest.";
870 return NAME_NOT_FOUND;
871 }
872 Level deviceLevel = deviceManifest->level();
873 if (deviceLevel == Level::UNSPECIFIED) {
874 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
875 return BAD_VALUE;
876 }
Yifan Hong507815b2021-05-21 16:48:37 -0700877 std::string kernelLevelError;
878 Level kernelLevel = getKernelLevel(&kernelLevelError);
879 if (kernelLevel == Level::UNSPECIFIED) {
880 LOG(WARNING) << kernelLevelError;
881 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800882
Daniel Normanedf80402021-03-29 16:49:22 -0700883 std::vector<CompatibilityMatrix> targetMatrices;
884 // Partition matrixFragments into two groups, where the second group
885 // contains all matrices whose level == deviceLevel.
886 auto targetMatricesPartition = std::partition(
887 matrixFragments.begin(), matrixFragments.end(),
888 [&](const CompatibilityMatrix& matrix) { return matrix.level() != deviceLevel; });
889 // Move these matrices into the targetMatrices vector...
890 std::move(targetMatricesPartition, matrixFragments.end(), std::back_inserter(targetMatrices));
891 if (targetMatrices.empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800892 if (error)
893 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
894 return NAME_NOT_FOUND;
895 }
Daniel Normanedf80402021-03-29 16:49:22 -0700896 // so that they can be combined into one matrix for deprecation checking.
Yifan Hong507815b2021-05-21 16:48:37 -0700897 auto targetMatrix =
898 CompatibilityMatrix::combine(deviceLevel, kernelLevel, &targetMatrices, error);
Daniel Normanedf80402021-03-29 16:49:22 -0700899 if (targetMatrix == nullptr) {
900 return BAD_VALUE;
901 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800902
Yifan Hong03ea4282020-03-17 17:58:35 -0700903 std::multimap<std::string, std::string> childrenMap;
904 for (const auto& child : hidlMetadata) {
905 for (const auto& parent : child.inherited) {
906 childrenMap.emplace(parent, child.name);
907 }
908 }
909
910 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
911 // Matrices with unspecified level are considered "current".
912 bool isDeprecated = false;
Daniel Normanedf80402021-03-29 16:49:22 -0700913 for (auto it = matrixFragments.begin(); it < targetMatricesPartition; ++it) {
914 const auto& namedMatrix = *it;
Yifan Honga83d0e42020-04-13 13:07:31 -0700915 if (namedMatrix.level() == Level::UNSPECIFIED) continue;
Daniel Normanedf80402021-03-29 16:49:22 -0700916 if (namedMatrix.level() > deviceLevel) continue;
Yifan Honga83d0e42020-04-13 13:07:31 -0700917 for (const MatrixHal& hal : namedMatrix.getHals()) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700918 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
919 isDeprecated = true;
920 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800921 }
922 }
923
Yifan Hong03ea4282020-03-17 17:58:35 -0700924 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800925}
926
Yifan Hong03ea4282020-03-17 17:58:35 -0700927int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
928 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800929 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700930 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700931 ListInstances inManifest =
932 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
933 const std::vector<std::string>& /* hintInstances */) {
934 std::vector<std::pair<std::string, Version>> ret;
935 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700936 HalFormat::HIDL, package, version, interface,
937 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700938 ret.push_back(
939 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
940 return true;
941 });
942 return ret;
943 };
Yifan Hong03ea4282020-03-17 17:58:35 -0700944 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800945}
Yifan Hong3daec812017-02-27 18:49:11 -0800946
Yifan Hong378c4082019-12-23 13:10:57 -0800947Level VintfObject::getKernelLevel(std::string* error) {
Yifan Hong2d644112020-11-12 16:11:24 -0800948 auto runtimeInfo = getRuntimeInfo(RuntimeInfo::FetchFlag::KERNEL_FCM);
949 if (!runtimeInfo) {
950 if (error) *error = "Cannot retrieve runtime info with kernel level.";
Yifan Hong378c4082019-12-23 13:10:57 -0800951 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700952 }
Yifan Hong2d644112020-11-12 16:11:24 -0800953 if (runtimeInfo->kernelLevel() != Level::UNSPECIFIED) {
954 return runtimeInfo->kernelLevel();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700955 }
Yifan Hong2d644112020-11-12 16:11:24 -0800956 if (error) {
957 *error = "Both device manifest and kernel release do not specify kernel FCM version.";
958 }
Yifan Hong378c4082019-12-23 13:10:57 -0800959 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700960}
961
Yifan Hong9f78c182018-07-12 14:45:52 -0700962const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
963 return mFileSystem;
964}
965
Yifan Hong9f78c182018-07-12 14:45:52 -0700966const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
967 return mPropertyFetcher;
968}
969
Yifan Hongd038b2b2018-11-27 13:57:56 -0800970const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700971 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700972}
973
Yifan Hong6b860852020-03-19 23:12:07 +0000974android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
Yifan Honga83d0e42020-04-13 13:07:31 -0700975 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong6b860852020-03-19 23:12:07 +0000976 std::string error;
977 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
978 if (status != OK) {
979 return android::base::Error(-status)
980 << "Cannot get all framework matrix fragments: " << error;
981 }
982 for (const auto& namedMatrix : matrixFragments) {
983 // Returns true if product matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700984 if (android::base::StartsWith(namedMatrix.fileName(), kProductVintfDir)) {
Yifan Hong6b860852020-03-19 23:12:07 +0000985 return true;
986 }
Yifan Hongf6ff4272020-03-12 22:56:16 -0700987 // Returns true if system_ext matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700988 if (android::base::StartsWith(namedMatrix.fileName(), kSystemExtVintfDir)) {
Yifan Hongf6ff4272020-03-12 22:56:16 -0700989 return true;
990 }
Yifan Hong6b860852020-03-19 23:12:07 +0000991 // Returns true if device system matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700992 if (android::base::StartsWith(namedMatrix.fileName(), kSystemVintfDir) &&
993 namedMatrix.level() == Level::UNSPECIFIED && !namedMatrix.getHals().empty()) {
Yifan Hong6b860852020-03-19 23:12:07 +0000994 return true;
995 }
996 }
997 return false;
998}
999
Yifan Hongd7924ff2020-03-17 14:09:10 -07001000android::base::Result<void> VintfObject::checkUnusedHals(
1001 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6b860852020-03-19 23:12:07 +00001002 auto matrix = getFrameworkCompatibilityMatrix();
1003 if (matrix == nullptr) {
1004 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
1005 }
1006 auto manifest = getDeviceHalManifest();
1007 if (manifest == nullptr) {
1008 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
1009 }
Yifan Hongd7924ff2020-03-17 14:09:10 -07001010 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6b860852020-03-19 23:12:07 +00001011 if (!unused.empty()) {
1012 return android::base::Error()
1013 << "The following instances are in the device manifest but "
1014 << "not specified in framework compatibility matrix: \n"
1015 << " " << android::base::Join(unused, "\n ") << "\n"
1016 << "Suggested fix:\n"
Yifan Hong5a220b12020-04-07 15:22:24 -07001017 << "1. Update deprecated HALs to the latest version.\n"
1018 << "2. Check for any typos in device manifest or framework compatibility "
Yifan Hong6b860852020-03-19 23:12:07 +00001019 << "matrices with FCM version >= " << matrix->level() << ".\n"
Yifan Hong5a220b12020-04-07 15:22:24 -07001020 << "3. For new platform HALs, add them to any framework compatibility matrix "
1021 << "with FCM version >= " << matrix->level() << " where applicable.\n"
1022 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
Yifan Hong6b860852020-03-19 23:12:07 +00001023 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
1024 }
1025 return {};
1026}
1027
Yifan Hongd7043972020-03-30 13:27:46 -07001028namespace {
1029
1030// Insert |name| into |ret| if shouldCheck(name).
1031void InsertIf(const std::string& name, const std::function<bool(const std::string&)>& shouldCheck,
1032 std::set<std::string>* ret) {
1033 if (shouldCheck(name)) ret->insert(name);
1034}
1035
1036std::string StripHidlInterface(const std::string& fqNameString) {
1037 FQName fqName;
1038 if (!fqName.setTo(fqNameString)) {
1039 return "";
1040 }
1041 return fqName.getPackageAndVersion().string();
1042}
1043
1044std::string StripAidlType(const std::string& type) {
1045 auto items = android::base::Split(type, ".");
1046 if (items.empty()) {
1047 return "";
1048 }
1049 items.erase(items.end() - 1);
1050 return android::base::Join(items, ".");
1051}
1052
Yifan Hong8c61c752021-02-01 19:21:03 -08001053// android.hardware.foo@1.0
1054std::set<std::string> HidlMetadataToPackagesAndVersions(
Yifan Hongd7043972020-03-30 13:27:46 -07001055 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1056 const std::function<bool(const std::string&)>& shouldCheck) {
1057 std::set<std::string> ret;
1058 for (const auto& item : hidlMetadata) {
1059 InsertIf(StripHidlInterface(item.name), shouldCheck, &ret);
1060 }
1061 return ret;
1062}
1063
Yifan Hong8c61c752021-02-01 19:21:03 -08001064// android.hardware.foo
1065std::set<std::string> AidlMetadataToPackages(
Yifan Hongd7043972020-03-30 13:27:46 -07001066 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1067 const std::function<bool(const std::string&)>& shouldCheck) {
1068 std::set<std::string> ret;
1069 for (const auto& item : aidlMetadata) {
1070 for (const auto& type : item.types) {
1071 InsertIf(StripAidlType(type), shouldCheck, &ret);
1072 }
1073 }
1074 return ret;
1075}
1076
Yifan Hong9db8ab12021-02-01 19:21:30 -08001077// android.hardware.foo@1.0::IFoo.
1078// Note that UDTs are not filtered out, so there might be non-interface types.
1079std::set<std::string> HidlMetadataToNames(const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
1080 std::set<std::string> ret;
1081 for (const auto& item : hidlMetadata) {
1082 ret.insert(item.name);
1083 }
1084 return ret;
1085}
1086
1087// android.hardware.foo.IFoo
1088// Note that UDTs are not filtered out, so there might be non-interface types.
1089std::set<std::string> AidlMetadataToNames(const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1090 std::set<std::string> ret;
1091 for (const auto& item : aidlMetadata) {
1092 for (const auto& type : item.types) {
1093 ret.insert(type);
1094 }
1095 }
1096 return ret;
1097}
1098
Yifan Hongd7043972020-03-30 13:27:46 -07001099} // anonymous namespace
1100
Yifan Hong8c61c752021-02-01 19:21:03 -08001101android::base::Result<std::vector<CompatibilityMatrix>> VintfObject::getAllFrameworkMatrixLevels() {
Yifan Hongd7043972020-03-30 13:27:46 -07001102 // Get all framework matrix fragments instead of the combined framework compatibility matrix
1103 // because the latter may omit interfaces from the latest FCM if device target-level is not
1104 // the latest.
1105 std::vector<CompatibilityMatrix> matrixFragments;
1106 std::string error;
1107 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, &error);
1108 if (matrixFragmentsStatus != OK) {
1109 return android::base::Error(-matrixFragmentsStatus)
1110 << "Unable to get all framework matrix fragments: " << error;
1111 }
1112 if (matrixFragments.empty()) {
1113 if (error.empty()) {
1114 error = "Cannot get framework matrix for each FCM version for unknown error.";
1115 }
1116 return android::base::Error(-NAME_NOT_FOUND) << error;
1117 }
Yifan Hong8c61c752021-02-01 19:21:03 -08001118 return matrixFragments;
1119}
1120
1121android::base::Result<void> VintfObject::checkMissingHalsInMatrices(
1122 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1123 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1124 std::function<bool(const std::string&)> shouldCheck) {
1125 if (!shouldCheck) {
1126 shouldCheck = [](const auto&) { return true; };
1127 }
1128
1129 auto matrixFragments = getAllFrameworkMatrixLevels();
1130 if (!matrixFragments.ok()) return matrixFragments.error();
Yifan Hongd7043972020-03-30 13:27:46 -07001131
1132 // Filter aidlMetadata and hidlMetadata with shouldCheck.
Yifan Hong8c61c752021-02-01 19:21:03 -08001133 auto allAidlPackages = AidlMetadataToPackages(aidlMetadata, shouldCheck);
1134 auto allHidlPackagesAndVersions = HidlMetadataToPackagesAndVersions(hidlMetadata, shouldCheck);
Yifan Hongd7043972020-03-30 13:27:46 -07001135
1136 // Filter out instances in allAidlMetadata and allHidlMetadata that are in the matrices.
1137 std::vector<std::string> errors;
Yifan Hong8c61c752021-02-01 19:21:03 -08001138 for (const auto& matrix : matrixFragments.value()) {
Yifan Hongd7043972020-03-30 13:27:46 -07001139 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1140 switch (matrixInstance.format()) {
1141 case HalFormat::AIDL: {
Yifan Hong8c61c752021-02-01 19:21:03 -08001142 allAidlPackages.erase(matrixInstance.package());
Yifan Hongd7043972020-03-30 13:27:46 -07001143 return true; // continue to next instance
1144 }
1145 case HalFormat::HIDL: {
1146 for (Version v = matrixInstance.versionRange().minVer();
1147 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
Yifan Hong8c61c752021-02-01 19:21:03 -08001148 allHidlPackagesAndVersions.erase(
1149 toFQNameString(matrixInstance.package(), v));
Yifan Hongd7043972020-03-30 13:27:46 -07001150 }
1151 return true; // continue to next instance
1152 }
1153 default: {
1154 if (shouldCheck(matrixInstance.package())) {
1155 errors.push_back("HAL package " + matrixInstance.package() +
1156 " is not allowed to have format " +
1157 to_string(matrixInstance.format()) + ".");
1158 }
1159 return true; // continue to next instance
1160 }
1161 }
1162 });
1163 }
1164
Yifan Hong8c61c752021-02-01 19:21:03 -08001165 if (!allHidlPackagesAndVersions.empty()) {
Yifan Hongd7043972020-03-30 13:27:46 -07001166 errors.push_back(
1167 "The following HIDL packages are not found in any compatibility matrix fragments:\t\n" +
Yifan Hong8c61c752021-02-01 19:21:03 -08001168 android::base::Join(allHidlPackagesAndVersions, "\t\n"));
Yifan Hongd7043972020-03-30 13:27:46 -07001169 }
Yifan Hong8c61c752021-02-01 19:21:03 -08001170 if (!allAidlPackages.empty()) {
Yifan Hongd7043972020-03-30 13:27:46 -07001171 errors.push_back(
1172 "The following AIDL packages are not found in any compatibility matrix fragments:\t\n" +
Yifan Hong8c61c752021-02-01 19:21:03 -08001173 android::base::Join(allAidlPackages, "\t\n"));
Yifan Hongd7043972020-03-30 13:27:46 -07001174 }
1175
1176 if (!errors.empty()) {
1177 return android::base::Error() << android::base::Join(errors, "\n");
1178 }
1179
1180 return {};
1181}
1182
Yifan Hong9db8ab12021-02-01 19:21:30 -08001183android::base::Result<void> VintfObject::checkMatrixHalsHasDefinition(
1184 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1185 const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1186 auto matrixFragments = getAllFrameworkMatrixLevels();
1187 if (!matrixFragments.ok()) return matrixFragments.error();
1188
1189 auto allAidlNames = AidlMetadataToNames(aidlMetadata);
1190 auto allHidlNames = HidlMetadataToNames(hidlMetadata);
1191 std::set<std::string> badAidlInterfaces;
1192 std::set<std::string> badHidlInterfaces;
1193
1194 std::vector<std::string> errors;
1195 for (const auto& matrix : matrixFragments.value()) {
1196 if (matrix.level() == Level::UNSPECIFIED) {
1197 LOG(INFO) << "Skip checkMatrixHalsHasDefinition() on " << matrix.fileName()
1198 << " with no level.";
1199 continue;
1200 }
1201
1202 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1203 switch (matrixInstance.format()) {
1204 case HalFormat::AIDL: {
1205 auto matrixInterface =
1206 toAidlFqnameString(matrixInstance.package(), matrixInstance.interface());
1207 if (allAidlNames.find(matrixInterface) == allAidlNames.end()) {
1208 errors.push_back(
1209 "AIDL interface " + matrixInterface + " is referenced in " +
1210 matrix.fileName() +
1211 ", but there is no corresponding .aidl definition associated with an "
1212 "aidl_interface module in this build. Typo?");
1213 }
1214 return true; // continue to next instance
1215 }
1216 case HalFormat::HIDL: {
1217 for (Version v = matrixInstance.versionRange().minVer();
1218 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1219 auto matrixInterface = matrixInstance.interfaceDescription(v);
1220 if (allHidlNames.find(matrixInterface) == allHidlNames.end()) {
1221 errors.push_back(
1222 "HIDL interface " + matrixInterface + " is referenced in " +
1223 matrix.fileName() +
1224 ", but there is no corresponding .hal definition associated with "
1225 "a hidl_interface module in this build. Typo?");
1226 }
1227 }
1228 return true; // continue to next instance
1229 }
1230 default: {
1231 // We do not have data for native HALs.
1232 return true; // continue to next instance
1233 }
1234 }
1235 });
Yifan Hong3daec812017-02-27 18:49:11 -08001236 }
1237
1238 if (!errors.empty()) {
1239 return android::base::Error() << android::base::Join(errors, "\n");
1240 }
1241
1242 return {};
1243}
1244
1245// make_unique does not work because VintfObject constructor is private.
Yifan Hong9d4c59c2021-11-29 18:37:42 -08001246VintfObject::Builder::Builder()
1247 : VintfObjectBuilder(std::unique_ptr<VintfObject>(new VintfObject())) {}
Yifan Hong3daec812017-02-27 18:49:11 -08001248
Yifan Hong9d4c59c2021-11-29 18:37:42 -08001249namespace details {
1250
1251VintfObjectBuilder::~VintfObjectBuilder() {}
1252
1253VintfObjectBuilder& VintfObjectBuilder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
Yifan Hong3daec812017-02-27 18:49:11 -08001254 mObject->mFileSystem = std::move(e);
1255 return *this;
1256}
1257
Yifan Hong9d4c59c2021-11-29 18:37:42 -08001258VintfObjectBuilder& VintfObjectBuilder::setRuntimeInfoFactory(
Yifan Hong3daec812017-02-27 18:49:11 -08001259 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
1260 mObject->mRuntimeInfoFactory = std::move(e);
1261 return *this;
1262}
1263
Yifan Hong9d4c59c2021-11-29 18:37:42 -08001264VintfObjectBuilder& VintfObjectBuilder::setPropertyFetcher(std::unique_ptr<PropertyFetcher>&& e) {
Yifan Hong3daec812017-02-27 18:49:11 -08001265 mObject->mPropertyFetcher = std::move(e);
1266 return *this;
1267}
1268
Yifan Hong9d4c59c2021-11-29 18:37:42 -08001269std::unique_ptr<VintfObject> VintfObjectBuilder::buildInternal() {
Yifan Hong3daec812017-02-27 18:49:11 -08001270 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1271 if (!mObject->mRuntimeInfoFactory)
1272 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1273 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1274 return std::move(mObject);
1275}
1276
Yifan Hong9d4c59c2021-11-29 18:37:42 -08001277} // namespace details
1278
Yifan Hongd7043972020-03-30 13:27:46 -07001279} // namespace vintf
1280} // namespace android