blob: 394350e1961e57ce2a5b9f782dba4011a8f03c53 [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
223 if (err == NAME_NOT_FOUND) return OK;
224 if (err != OK) return err;
225
226 for (const std::string& file : fileNames) {
227 // Only adds HALs because all other things are added by libvintf
228 // itself for now.
229 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700230 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700231 if (err != OK) return err;
232
Yifan Hong4c6962d2019-01-30 15:50:46 -0800233 if (!manifest->addAll(&fragmentManifest, error)) {
234 if (error) {
Yifan Hong9f9a3192020-04-13 16:39:45 -0700235 error->insert(0, "Cannot add manifest fragment " + directory + file + ": ");
Yifan Hong4c6962d2019-01-30 15:50:46 -0800236 }
237 return UNKNOWN_ERROR;
238 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700239 }
240
241 return OK;
242}
243
Yifan Hong5a93bf22018-01-17 18:22:32 -0800244// Priority for loading vendor manifest:
Roopesh Natarajad629d382020-02-26 09:51:38 -0800245// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
246// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700247// 3. ODM manifest (optional) + odm fragments
248// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800249// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700250// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
251// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700252status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800253 HalManifest vendorManifest;
254 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800255 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
256 return vendorStatus;
257 }
258
Steven Morelandeedf2d42018-04-04 16:36:27 -0700259 if (vendorStatus == OK) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800260 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700261 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700262 if (fragmentStatus != OK) {
263 return fragmentStatus;
264 }
265 }
266
Yifan Hong5a93bf22018-01-17 18:22:32 -0800267 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700268 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800269 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
270 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800271 }
272
Yifan Hong5a93bf22018-01-17 18:22:32 -0800273 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800274 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800275 if (!out->addAll(&odmManifest, error)) {
276 if (error) {
277 error->insert(0, "Cannot add ODM manifest :");
278 }
279 return UNKNOWN_ERROR;
280 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800281 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700282 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800283 }
284
Yifan Hong12a11ac2018-01-19 13:58:32 -0800285 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800286 if (odmStatus == OK) {
287 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700288 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800289 }
290
291 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800292 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800293}
294
Roopesh Natarajad629d382020-02-26 09:51:38 -0800295// Priority:
296// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
297// 2. /vendor/etc/vintf/manifest.xml
298// where:
299// {vendorSku} is the value of ro.boot.product.vendor.sku
300status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
301 status_t status;
302
303 std::string vendorSku;
304 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
305
306 if (!vendorSku.empty()) {
307 status =
Yifan Hongc7a9a292021-02-03 17:27:58 -0800308 fetchOneHalManifest(kVendorVintfDir + "manifest_"s + vendorSku + ".xml", out, error);
Roopesh Natarajad629d382020-02-26 09:51:38 -0800309 if (status == OK || status != NAME_NOT_FOUND) {
310 return status;
311 }
312 }
313
314 status = fetchOneHalManifest(kVendorManifest, out, error);
315 if (status == OK || status != NAME_NOT_FOUND) {
316 return status;
317 }
318
319 return NAME_NOT_FOUND;
320}
321
Yifan Hong12a11ac2018-01-19 13:58:32 -0800322// "out" is written to iff return status is OK.
323// Priority:
324// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
325// 2. /odm/etc/vintf/manifest.xml
326// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
327// 4. /odm/etc/manifest.xml
328// where:
329// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700330status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800331 status_t status;
332
Yifan Hong12a11ac2018-01-19 13:58:32 -0800333 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800334 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800335
336 if (!productModel.empty()) {
337 status =
Yifan Hongc7a9a292021-02-03 17:27:58 -0800338 fetchOneHalManifest(kOdmVintfDir + "manifest_"s + productModel + ".xml", 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
Yifan Hong9f78c182018-07-12 14:45:52 -0700344 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800345 if (status == OK || status != NAME_NOT_FOUND) {
346 return status;
347 }
348
Yifan Hong12a11ac2018-01-19 13:58:32 -0800349 if (!productModel.empty()) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800350 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_"s + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800351 error);
352 if (status == OK || status != NAME_NOT_FOUND) {
353 return status;
354 }
355 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800356
Yifan Hong9f78c182018-07-12 14:45:52 -0700357 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800358 if (status == OK || status != NAME_NOT_FOUND) {
359 return status;
360 }
361
362 return NAME_NOT_FOUND;
363}
364
365// Fetch one manifest.xml file. "out" is written to iff return status is OK.
366// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700367status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800368 std::string* error) {
369 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800370 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800371 if (status == OK) {
372 *out = std::move(ret);
373 }
374 return status;
375}
376
Yifan Hong9f78c182018-07-12 14:45:52 -0700377status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800378 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800379 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800380 *out = std::move(etcMatrix);
381 return OK;
382 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800383 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800384}
385
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700386// Priority:
387// 1. /system/etc/vintf/manifest.xml
388// + /system/etc/vintf/manifest/*.xml if they exist
389// + /product/etc/vintf/manifest.xml if it exists
390// + /product/etc/vintf/manifest/*.xml if they exist
391// 2. (deprecated) /system/manifest.xml
Yifan Hongc735be92020-10-12 16:25:50 -0700392status_t VintfObject::fetchUnfilteredFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700393 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
394 if (systemEtcStatus == OK) {
395 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
396 if (dirStatus != OK) {
397 return dirStatus;
398 }
399
Yifan Hongc7a9a292021-02-03 17:27:58 -0800400 std::vector<std::pair<const char*, const char*>> extensions{
Yifan Hong659feac2020-03-19 18:09:54 -0700401 {kProductManifest, kProductManifestFragmentDir},
402 {kSystemExtManifest, kSystemExtManifestFragmentDir},
403 };
404 for (auto&& [manifestPath, frags] : extensions) {
405 HalManifest halManifest;
406 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
407 if (status != OK && status != NAME_NOT_FOUND) {
408 return status;
409 }
410 if (status == OK) {
411 if (!out->addAll(&halManifest, error)) {
412 if (error) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800413 error->insert(0, "Cannot add "s + manifestPath + ":");
Yifan Hong659feac2020-03-19 18:09:54 -0700414 }
415 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700416 }
Yifan Hong659feac2020-03-19 18:09:54 -0700417 }
418
419 auto fragmentStatus = addDirectoryManifests(frags, out, error);
420 if (fragmentStatus != OK) {
421 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700422 }
423 }
Yifan Hongc735be92020-10-12 16:25:50 -0700424
Yifan Hong659feac2020-03-19 18:09:54 -0700425 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700426 } else {
427 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
428 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800429 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700430
Yifan Hong12e23c22018-11-05 14:53:30 -0800431 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800432}
433
Yifan Hongc735be92020-10-12 16:25:50 -0700434status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
435 status_t status = fetchUnfilteredFrameworkHalManifest(out, error);
436 if (status != OK) {
437 return status;
438 }
439 filterHalsByDeviceManifestLevel(out);
440 return OK;
441}
442
443void VintfObject::filterHalsByDeviceManifestLevel(HalManifest* out) {
444 auto deviceManifest = getDeviceHalManifest();
445 if (deviceManifest == nullptr) {
446 LOG(WARNING) << "Cannot fetch device manifest to determine target FCM version to "
447 "filter framework manifest HALs.";
448 return;
449 }
450 Level deviceManifestLevel = deviceManifest->level();
451 if (deviceManifestLevel == Level::UNSPECIFIED) {
452 LOG(WARNING)
453 << "Not filtering framework manifest HALs because target FCM version is unspecified.";
454 return;
455 }
456 out->removeHalsIf([deviceManifestLevel](const ManifestHal& hal) {
457 if (hal.getMaxLevel() == Level::UNSPECIFIED) {
458 return false;
459 }
460 return hal.getMaxLevel() < deviceManifestLevel;
461 });
462}
463
Yifan Hong9f8f6562018-11-06 16:26:20 -0800464static void appendLine(std::string* error, const std::string& message) {
465 if (error != nullptr) {
466 if (!error->empty()) *error += "\n";
467 *error += message;
468 }
469}
470
Yifan Honga83d0e42020-04-13 13:07:31 -0700471status_t VintfObject::getOneMatrix(const std::string& path, CompatibilityMatrix* out,
Yifan Hong73bde592019-01-22 13:30:23 -0800472 std::string* error) {
473 std::string content;
474 status_t status = getFileSystem()->fetch(path, &content, error);
475 if (status != OK) {
476 return status;
477 }
Yifan Hong25e2a772021-04-16 18:34:28 -0700478 if (!fromXml(out, content, error)) {
Yifan Hong73bde592019-01-22 13:30:23 -0800479 if (error) {
480 error->insert(0, "Cannot parse " + path + ": ");
481 }
482 return BAD_VALUE;
483 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700484 out->setFileName(path);
Yifan Hong73bde592019-01-22 13:30:23 -0800485 return OK;
486}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800487
Yifan Honga83d0e42020-04-13 13:07:31 -0700488status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix>* results,
Yifan Hong73bde592019-01-22 13:30:23 -0800489 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800490 std::vector<std::string> dirs = {
491 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800492 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800493 kProductVintfDir,
494 };
495 for (const auto& dir : dirs) {
496 std::vector<std::string> fileNames;
497 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
498 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800499 continue;
500 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800501 if (listStatus != OK) {
502 return listStatus;
503 }
504 for (const std::string& fileName : fileNames) {
505 std::string path = dir + fileName;
Yifan Honga83d0e42020-04-13 13:07:31 -0700506 CompatibilityMatrix namedMatrix;
Yifan Hongb02d87d2019-12-19 11:15:27 -0800507 std::string matrixError;
508 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
509 if (matrixStatus != OK) {
510 // Manifests and matrices share the same dir. Client may not have enough
511 // permissions to read system manifests, or may not be able to parse it.
512 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
513 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
514 continue;
515 }
516 results->emplace_back(std::move(namedMatrix));
517 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800518
Yifan Hongb02d87d2019-12-19 11:15:27 -0800519 if (dir == kSystemVintfDir && results->empty()) {
520 if (error) {
521 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
522 }
523 return NAME_NOT_FOUND;
524 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800525 }
526
Yifan Hong73bde592019-01-22 13:30:23 -0800527 if (results->empty()) {
528 if (error) {
529 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800530 "No framework matrices can be fetched or parsed. "
531 "The following directories are searched:\n " +
532 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800533 }
534 return NAME_NOT_FOUND;
535 }
536 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800537}
538
Yifan Honga192fa52020-07-21 12:09:20 -0700539std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(RuntimeInfo::FetchFlags flags) {
540 return GetInstance()->getRuntimeInfo(flags);
Yifan Hong9f78c182018-07-12 14:45:52 -0700541}
Yifan Honga192fa52020-07-21 12:09:20 -0700542std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700543 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700544
Yifan Honga192fa52020-07-21 12:09:20 -0700545 // Skip fetching information that has already been fetched previously.
546 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700547
Yifan Hong9f78c182018-07-12 14:45:52 -0700548 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800549 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700550 }
551
Yifan Hong9f78c182018-07-12 14:45:52 -0700552 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700553 if (status != OK) {
Yifan Hong7becb062020-11-13 15:54:58 -0800554 // If only kernel FCM is needed, ignore errors when fetching RuntimeInfo because RuntimeInfo
555 // is not available on host. On host, the kernel level can still be inferred from device
556 // manifest.
557 // If other information is needed, flag the error by returning nullptr.
558 auto allExceptKernelFcm = RuntimeInfo::FetchFlag::ALL & ~RuntimeInfo::FetchFlag::KERNEL_FCM;
559 bool needDeviceRuntimeInfo = flags & allExceptKernelFcm;
560 if (needDeviceRuntimeInfo) {
561 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
562 return nullptr;
563 }
564 }
565
566 // To support devices without GKI, RuntimeInfo::fetchAllInformation does not report errors
567 // if kernel level cannot be retrieved. If so, fetch kernel FCM version from device HAL
568 // manifest and store it in RuntimeInfo too.
569 if (flags & RuntimeInfo::FetchFlag::KERNEL_FCM) {
570 Level deviceManifestKernelLevel = Level::UNSPECIFIED;
571 auto manifest = getDeviceHalManifest();
572 if (manifest) {
573 deviceManifestKernelLevel = manifest->inferredKernelLevel();
574 }
575 if (deviceManifestKernelLevel != Level::UNSPECIFIED) {
576 Level kernelLevel = mDeviceRuntimeInfo.object->kernelLevel();
577 if (kernelLevel == Level::UNSPECIFIED) {
578 mDeviceRuntimeInfo.object->setKernelLevel(deviceManifestKernelLevel);
579 } else if (kernelLevel != deviceManifestKernelLevel) {
580 LOG(WARNING) << "uname() reports kernel level " << kernelLevel
581 << " but device manifest sets kernel level "
582 << deviceManifestKernelLevel << ". Using kernel level " << kernelLevel;
583 }
584 }
Yifan Hong1fb004e2017-09-26 15:04:44 -0700585 }
586
Yifan Hong9f78c182018-07-12 14:45:52 -0700587 mDeviceRuntimeInfo.fetchedFlags |= flags;
588 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800589}
590
Yifan Hong12e23c22018-11-05 14:53:30 -0800591int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
592 status_t status = OK;
593 // null checks for files and runtime info
594 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700595 appendLine(error, "No framework manifest file from device or from update package");
596 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700597 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800598 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700599 appendLine(error, "No device manifest file from device or from update package");
600 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700601 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800602 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700603 appendLine(error, "No framework matrix 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 (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700607 appendLine(error, "No device matrix file from device or from update package");
608 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700609 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800610
Yifan Hong072f12d2018-08-08 13:04:51 -0700611 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800612 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700613 appendLine(error, "No runtime info from device");
614 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800615 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700616 }
Yifan Hong878556e2018-07-13 13:45:30 -0700617 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700618
619 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800620 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800621 if (error) {
622 error->insert(0,
623 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700624 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800625 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700626 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800627 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800628 if (error) {
629 error->insert(0,
630 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700631 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800632 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700633 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800634
Yifan Hong072f12d2018-08-08 13:04:51 -0700635 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800636 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800637 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800638 if (error) {
639 error->insert(0,
640 "Runtime info and framework compatibility matrix are incompatible: ");
641 }
642 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700643 }
644 }
645
646 return COMPATIBLE;
647}
648
Yifan Hong9f78c182018-07-12 14:45:52 -0700649namespace details {
650
Yifan Hong69c1b112018-02-27 17:06:00 -0800651std::vector<std::string> dumpFileList() {
652 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800653 // clang-format off
654 kSystemVintfDir,
655 kVendorVintfDir,
656 kOdmVintfDir,
657 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800658 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800659 kOdmLegacyVintfDir,
660 kVendorLegacyManifest,
661 kVendorLegacyMatrix,
662 kSystemLegacyManifest,
663 kSystemLegacyMatrix,
664 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800665 };
666}
667
Yifan Hong9f78c182018-07-12 14:45:52 -0700668} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700669
Yifan Hong9f78c182018-07-12 14:45:52 -0700670bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800671 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700672 const ListInstances& listInstances,
673 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700674 bool isDeprecated = false;
675 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700676 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
677 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700678 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800679 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700680 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700681 });
682 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800683}
684
Yifan Hong03ea4282020-03-17 17:58:35 -0700685// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
686// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
687// matches instancePattern, return true iff for all child interfaces (from
688// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700689bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800690 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700691 const ListInstances& listInstances,
692 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700693 const std::string& package = oldMatrixInstance.package();
694 const Version& version = oldMatrixInstance.versionRange().minVer();
695 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700696
Yifan Honga8a8fa92018-03-20 14:42:43 -0700697 std::vector<std::string> instanceHint;
698 if (!oldMatrixInstance.isRegex()) {
699 instanceHint.push_back(oldMatrixInstance.exactInstance());
700 }
701
Yifan Hong03ea4282020-03-17 17:58:35 -0700702 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700703 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hong03ea4282020-03-17 17:58:35 -0700704
Yifan Honga8a8fa92018-03-20 14:42:43 -0700705 for (const auto& pair : list) {
706 const std::string& servedInstance = pair.first;
707 Version servedVersion = pair.second;
Yifan Hong03ea4282020-03-17 17:58:35 -0700708 std::string servedFqInstanceString =
709 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700710 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700711 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700712 continue;
713 }
714
Yifan Hong03ea4282020-03-17 17:58:35 -0700715 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
716 servedInstance, listInstances, childrenMap);
717 if (!inheritance.has_value()) {
718 accumulatedErrors.push_back(inheritance.error().message());
719 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800720 }
721
Yifan Hong03ea4282020-03-17 17:58:35 -0700722 std::vector<std::string> errors;
723 for (const auto& fqInstance : *inheritance) {
724 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
725 fqInstance, listInstances);
726 if (result.ok()) {
727 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700728 break;
729 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700730 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700731 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800732
Yifan Hong03ea4282020-03-17 17:58:35 -0700733 if (errors.empty()) {
734 continue;
735 }
736 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
737 }
738
739 if (accumulatedErrors.empty()) {
740 return false;
741 }
742 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
743 return true;
744}
745
746// Check if fqInstance is listed in |listInstances|.
747bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
748 const FqInstance& fqInstance) {
749 auto list =
750 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
751 {fqInstance.getInstance()} /* instanceHint*/);
752 return std::any_of(list.begin(), list.end(),
753 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
754}
755
756// Return a list of FqInstance, where each element:
757// - is listed in |listInstances|; AND
758// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
759android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
760 const std::string& package, const Version& version, const std::string& interface,
761 const std::string& instance, const ListInstances& listInstances,
762 const ChildrenMap& childrenMap) {
763 FqInstance fqInstance;
764 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
765 return android::base::Error() << toFQNameString(package, version, interface, instance)
766 << " is not a valid FqInstance";
767 }
768
769 if (!IsInstanceListed(listInstances, fqInstance)) {
770 return {};
771 }
772
773 const FQName& fqName = fqInstance.getFqName();
774
775 std::vector<FqInstance> ret;
776 ret.push_back(fqInstance);
777
778 auto childRange = childrenMap.equal_range(fqName.string());
779 for (auto it = childRange.first; it != childRange.second; ++it) {
780 const auto& childFqNameString = it->second;
781 FQName childFqName;
782 if (!childFqName.setTo(childFqNameString)) {
783 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
784 }
785 FqInstance childFqInstance;
786 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
787 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
788 << fqInstance.getInstance() << " as FqInstance";
789 continue;
790 }
791 if (!IsInstanceListed(listInstances, childFqInstance)) {
792 continue;
793 }
794 ret.push_back(childFqInstance);
795 }
796 return ret;
797}
798
799// Check if |fqInstance| is in |targetMatrix|; essentially equal to
800// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
801// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
802// 2. package@x.z::interface/servedInstance is in targetMatrix but
803// servedInstance is not in listInstances(package@x.z::interface)
804android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
805 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
806 const ListInstances& listInstances) {
807 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
808 bool foundInstance = false;
809 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
810 targetMatrix.forEachInstanceOfPackage(
811 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
812 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
813 targetMatrixInstance.interface() == fqInstance.getInterface() &&
814 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
815 targetMatrixMinVer =
816 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
817 foundInstance = true;
818 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800819 return true;
Yifan Hong03ea4282020-03-17 17:58:35 -0700820 });
821 if (!foundInstance) {
822 return android::base::Error()
823 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
824 << targetMatrix.level() << "; it should not be served.";
825 }
826
827 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
828 bool targetVersionServed = false;
829 for (const auto& newPair :
830 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
831 {fqInstance.getInstance()} /* instanceHint */)) {
832 if (newPair.first == fqInstance.getInstance()) {
833 targetVersionServed = true;
834 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800835 }
836 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700837
Yifan Hong03ea4282020-03-17 17:58:35 -0700838 if (!targetVersionServed) {
839 return android::base::Error()
840 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
841 }
842 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800843}
844
Yifan Hong03ea4282020-03-17 17:58:35 -0700845int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
846 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
847 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700848 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800849 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
850 if (matrixFragmentsStatus != OK) {
851 return matrixFragmentsStatus;
852 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800853 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800854 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800855 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800856 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800857 return NAME_NOT_FOUND;
858 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700859 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800860 if (deviceManifest == nullptr) {
861 if (error) *error = "No device manifest.";
862 return NAME_NOT_FOUND;
863 }
864 Level deviceLevel = deviceManifest->level();
865 if (deviceLevel == Level::UNSPECIFIED) {
866 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
867 return BAD_VALUE;
868 }
Yifan Hong507815b2021-05-21 16:48:37 -0700869 std::string kernelLevelError;
870 Level kernelLevel = getKernelLevel(&kernelLevelError);
871 if (kernelLevel == Level::UNSPECIFIED) {
872 LOG(WARNING) << kernelLevelError;
873 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800874
Daniel Normanedf80402021-03-29 16:49:22 -0700875 std::vector<CompatibilityMatrix> targetMatrices;
876 // Partition matrixFragments into two groups, where the second group
877 // contains all matrices whose level == deviceLevel.
878 auto targetMatricesPartition = std::partition(
879 matrixFragments.begin(), matrixFragments.end(),
880 [&](const CompatibilityMatrix& matrix) { return matrix.level() != deviceLevel; });
881 // Move these matrices into the targetMatrices vector...
882 std::move(targetMatricesPartition, matrixFragments.end(), std::back_inserter(targetMatrices));
883 if (targetMatrices.empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800884 if (error)
885 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
886 return NAME_NOT_FOUND;
887 }
Daniel Normanedf80402021-03-29 16:49:22 -0700888 // so that they can be combined into one matrix for deprecation checking.
Yifan Hong507815b2021-05-21 16:48:37 -0700889 auto targetMatrix =
890 CompatibilityMatrix::combine(deviceLevel, kernelLevel, &targetMatrices, error);
Daniel Normanedf80402021-03-29 16:49:22 -0700891 if (targetMatrix == nullptr) {
892 return BAD_VALUE;
893 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800894
Yifan Hong03ea4282020-03-17 17:58:35 -0700895 std::multimap<std::string, std::string> childrenMap;
896 for (const auto& child : hidlMetadata) {
897 for (const auto& parent : child.inherited) {
898 childrenMap.emplace(parent, child.name);
899 }
900 }
901
902 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
903 // Matrices with unspecified level are considered "current".
904 bool isDeprecated = false;
Daniel Normanedf80402021-03-29 16:49:22 -0700905 for (auto it = matrixFragments.begin(); it < targetMatricesPartition; ++it) {
906 const auto& namedMatrix = *it;
Yifan Honga83d0e42020-04-13 13:07:31 -0700907 if (namedMatrix.level() == Level::UNSPECIFIED) continue;
Daniel Normanedf80402021-03-29 16:49:22 -0700908 if (namedMatrix.level() > deviceLevel) continue;
Yifan Honga83d0e42020-04-13 13:07:31 -0700909 for (const MatrixHal& hal : namedMatrix.getHals()) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700910 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
911 isDeprecated = true;
912 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800913 }
914 }
915
Yifan Hong03ea4282020-03-17 17:58:35 -0700916 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800917}
918
Yifan Hong03ea4282020-03-17 17:58:35 -0700919int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
920 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800921 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700922 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700923 ListInstances inManifest =
924 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
925 const std::vector<std::string>& /* hintInstances */) {
926 std::vector<std::pair<std::string, Version>> ret;
927 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700928 HalFormat::HIDL, package, version, interface,
929 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700930 ret.push_back(
931 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
932 return true;
933 });
934 return ret;
935 };
Yifan Hong03ea4282020-03-17 17:58:35 -0700936 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800937}
Yifan Hong3daec812017-02-27 18:49:11 -0800938
Yifan Hong378c4082019-12-23 13:10:57 -0800939Level VintfObject::getKernelLevel(std::string* error) {
Yifan Hong2d644112020-11-12 16:11:24 -0800940 auto runtimeInfo = getRuntimeInfo(RuntimeInfo::FetchFlag::KERNEL_FCM);
941 if (!runtimeInfo) {
942 if (error) *error = "Cannot retrieve runtime info with kernel level.";
Yifan Hong378c4082019-12-23 13:10:57 -0800943 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700944 }
Yifan Hong2d644112020-11-12 16:11:24 -0800945 if (runtimeInfo->kernelLevel() != Level::UNSPECIFIED) {
946 return runtimeInfo->kernelLevel();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700947 }
Yifan Hong2d644112020-11-12 16:11:24 -0800948 if (error) {
949 *error = "Both device manifest and kernel release do not specify kernel FCM version.";
950 }
Yifan Hong378c4082019-12-23 13:10:57 -0800951 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700952}
953
Yifan Hong9f78c182018-07-12 14:45:52 -0700954const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
955 return mFileSystem;
956}
957
Yifan Hong9f78c182018-07-12 14:45:52 -0700958const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
959 return mPropertyFetcher;
960}
961
Yifan Hongd038b2b2018-11-27 13:57:56 -0800962const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700963 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700964}
965
Yifan Hong6b860852020-03-19 23:12:07 +0000966android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
Yifan Honga83d0e42020-04-13 13:07:31 -0700967 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong6b860852020-03-19 23:12:07 +0000968 std::string error;
969 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
970 if (status != OK) {
971 return android::base::Error(-status)
972 << "Cannot get all framework matrix fragments: " << error;
973 }
974 for (const auto& namedMatrix : matrixFragments) {
975 // Returns true if product matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700976 if (android::base::StartsWith(namedMatrix.fileName(), kProductVintfDir)) {
Yifan Hong6b860852020-03-19 23:12:07 +0000977 return true;
978 }
Yifan Hongf6ff4272020-03-12 22:56:16 -0700979 // Returns true if system_ext matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700980 if (android::base::StartsWith(namedMatrix.fileName(), kSystemExtVintfDir)) {
Yifan Hongf6ff4272020-03-12 22:56:16 -0700981 return true;
982 }
Yifan Hong6b860852020-03-19 23:12:07 +0000983 // Returns true if device system matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700984 if (android::base::StartsWith(namedMatrix.fileName(), kSystemVintfDir) &&
985 namedMatrix.level() == Level::UNSPECIFIED && !namedMatrix.getHals().empty()) {
Yifan Hong6b860852020-03-19 23:12:07 +0000986 return true;
987 }
988 }
989 return false;
990}
991
Yifan Hongd7924ff2020-03-17 14:09:10 -0700992android::base::Result<void> VintfObject::checkUnusedHals(
993 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6b860852020-03-19 23:12:07 +0000994 auto matrix = getFrameworkCompatibilityMatrix();
995 if (matrix == nullptr) {
996 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
997 }
998 auto manifest = getDeviceHalManifest();
999 if (manifest == nullptr) {
1000 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
1001 }
Yifan Hongd7924ff2020-03-17 14:09:10 -07001002 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6b860852020-03-19 23:12:07 +00001003 if (!unused.empty()) {
1004 return android::base::Error()
1005 << "The following instances are in the device manifest but "
1006 << "not specified in framework compatibility matrix: \n"
1007 << " " << android::base::Join(unused, "\n ") << "\n"
1008 << "Suggested fix:\n"
Yifan Hong5a220b12020-04-07 15:22:24 -07001009 << "1. Update deprecated HALs to the latest version.\n"
1010 << "2. Check for any typos in device manifest or framework compatibility "
Yifan Hong6b860852020-03-19 23:12:07 +00001011 << "matrices with FCM version >= " << matrix->level() << ".\n"
Yifan Hong5a220b12020-04-07 15:22:24 -07001012 << "3. For new platform HALs, add them to any framework compatibility matrix "
1013 << "with FCM version >= " << matrix->level() << " where applicable.\n"
1014 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
Yifan Hong6b860852020-03-19 23:12:07 +00001015 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
1016 }
1017 return {};
1018}
1019
Yifan Hongd7043972020-03-30 13:27:46 -07001020namespace {
1021
1022// Insert |name| into |ret| if shouldCheck(name).
1023void InsertIf(const std::string& name, const std::function<bool(const std::string&)>& shouldCheck,
1024 std::set<std::string>* ret) {
1025 if (shouldCheck(name)) ret->insert(name);
1026}
1027
1028std::string StripHidlInterface(const std::string& fqNameString) {
1029 FQName fqName;
1030 if (!fqName.setTo(fqNameString)) {
1031 return "";
1032 }
1033 return fqName.getPackageAndVersion().string();
1034}
1035
1036std::string StripAidlType(const std::string& type) {
1037 auto items = android::base::Split(type, ".");
1038 if (items.empty()) {
1039 return "";
1040 }
1041 items.erase(items.end() - 1);
1042 return android::base::Join(items, ".");
1043}
1044
Yifan Hong8c61c752021-02-01 19:21:03 -08001045// android.hardware.foo@1.0
1046std::set<std::string> HidlMetadataToPackagesAndVersions(
Yifan Hongd7043972020-03-30 13:27:46 -07001047 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1048 const std::function<bool(const std::string&)>& shouldCheck) {
1049 std::set<std::string> ret;
1050 for (const auto& item : hidlMetadata) {
1051 InsertIf(StripHidlInterface(item.name), shouldCheck, &ret);
1052 }
1053 return ret;
1054}
1055
Yifan Hong8c61c752021-02-01 19:21:03 -08001056// android.hardware.foo
1057std::set<std::string> AidlMetadataToPackages(
Yifan Hongd7043972020-03-30 13:27:46 -07001058 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1059 const std::function<bool(const std::string&)>& shouldCheck) {
1060 std::set<std::string> ret;
1061 for (const auto& item : aidlMetadata) {
1062 for (const auto& type : item.types) {
1063 InsertIf(StripAidlType(type), shouldCheck, &ret);
1064 }
1065 }
1066 return ret;
1067}
1068
Yifan Hong9db8ab12021-02-01 19:21:30 -08001069// android.hardware.foo@1.0::IFoo.
1070// Note that UDTs are not filtered out, so there might be non-interface types.
1071std::set<std::string> HidlMetadataToNames(const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
1072 std::set<std::string> ret;
1073 for (const auto& item : hidlMetadata) {
1074 ret.insert(item.name);
1075 }
1076 return ret;
1077}
1078
1079// android.hardware.foo.IFoo
1080// Note that UDTs are not filtered out, so there might be non-interface types.
1081std::set<std::string> AidlMetadataToNames(const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1082 std::set<std::string> ret;
1083 for (const auto& item : aidlMetadata) {
1084 for (const auto& type : item.types) {
1085 ret.insert(type);
1086 }
1087 }
1088 return ret;
1089}
1090
Yifan Hongd7043972020-03-30 13:27:46 -07001091} // anonymous namespace
1092
Yifan Hong8c61c752021-02-01 19:21:03 -08001093android::base::Result<std::vector<CompatibilityMatrix>> VintfObject::getAllFrameworkMatrixLevels() {
Yifan Hongd7043972020-03-30 13:27:46 -07001094 // Get all framework matrix fragments instead of the combined framework compatibility matrix
1095 // because the latter may omit interfaces from the latest FCM if device target-level is not
1096 // the latest.
1097 std::vector<CompatibilityMatrix> matrixFragments;
1098 std::string error;
1099 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, &error);
1100 if (matrixFragmentsStatus != OK) {
1101 return android::base::Error(-matrixFragmentsStatus)
1102 << "Unable to get all framework matrix fragments: " << error;
1103 }
1104 if (matrixFragments.empty()) {
1105 if (error.empty()) {
1106 error = "Cannot get framework matrix for each FCM version for unknown error.";
1107 }
1108 return android::base::Error(-NAME_NOT_FOUND) << error;
1109 }
Yifan Hong8c61c752021-02-01 19:21:03 -08001110 return matrixFragments;
1111}
1112
1113android::base::Result<void> VintfObject::checkMissingHalsInMatrices(
1114 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1115 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1116 std::function<bool(const std::string&)> shouldCheck) {
1117 if (!shouldCheck) {
1118 shouldCheck = [](const auto&) { return true; };
1119 }
1120
1121 auto matrixFragments = getAllFrameworkMatrixLevels();
1122 if (!matrixFragments.ok()) return matrixFragments.error();
Yifan Hongd7043972020-03-30 13:27:46 -07001123
1124 // Filter aidlMetadata and hidlMetadata with shouldCheck.
Yifan Hong8c61c752021-02-01 19:21:03 -08001125 auto allAidlPackages = AidlMetadataToPackages(aidlMetadata, shouldCheck);
1126 auto allHidlPackagesAndVersions = HidlMetadataToPackagesAndVersions(hidlMetadata, shouldCheck);
Yifan Hongd7043972020-03-30 13:27:46 -07001127
1128 // Filter out instances in allAidlMetadata and allHidlMetadata that are in the matrices.
1129 std::vector<std::string> errors;
Yifan Hong8c61c752021-02-01 19:21:03 -08001130 for (const auto& matrix : matrixFragments.value()) {
Yifan Hongd7043972020-03-30 13:27:46 -07001131 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1132 switch (matrixInstance.format()) {
1133 case HalFormat::AIDL: {
Yifan Hong8c61c752021-02-01 19:21:03 -08001134 allAidlPackages.erase(matrixInstance.package());
Yifan Hongd7043972020-03-30 13:27:46 -07001135 return true; // continue to next instance
1136 }
1137 case HalFormat::HIDL: {
1138 for (Version v = matrixInstance.versionRange().minVer();
1139 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
Yifan Hong8c61c752021-02-01 19:21:03 -08001140 allHidlPackagesAndVersions.erase(
1141 toFQNameString(matrixInstance.package(), v));
Yifan Hongd7043972020-03-30 13:27:46 -07001142 }
1143 return true; // continue to next instance
1144 }
1145 default: {
1146 if (shouldCheck(matrixInstance.package())) {
1147 errors.push_back("HAL package " + matrixInstance.package() +
1148 " is not allowed to have format " +
1149 to_string(matrixInstance.format()) + ".");
1150 }
1151 return true; // continue to next instance
1152 }
1153 }
1154 });
1155 }
1156
Yifan Hong8c61c752021-02-01 19:21:03 -08001157 if (!allHidlPackagesAndVersions.empty()) {
Yifan Hongd7043972020-03-30 13:27:46 -07001158 errors.push_back(
1159 "The following HIDL packages are not found in any compatibility matrix fragments:\t\n" +
Yifan Hong8c61c752021-02-01 19:21:03 -08001160 android::base::Join(allHidlPackagesAndVersions, "\t\n"));
Yifan Hongd7043972020-03-30 13:27:46 -07001161 }
Yifan Hong8c61c752021-02-01 19:21:03 -08001162 if (!allAidlPackages.empty()) {
Yifan Hongd7043972020-03-30 13:27:46 -07001163 errors.push_back(
1164 "The following AIDL packages are not found in any compatibility matrix fragments:\t\n" +
Yifan Hong8c61c752021-02-01 19:21:03 -08001165 android::base::Join(allAidlPackages, "\t\n"));
Yifan Hongd7043972020-03-30 13:27:46 -07001166 }
1167
1168 if (!errors.empty()) {
1169 return android::base::Error() << android::base::Join(errors, "\n");
1170 }
1171
1172 return {};
1173}
1174
Yifan Hong9db8ab12021-02-01 19:21:30 -08001175android::base::Result<void> VintfObject::checkMatrixHalsHasDefinition(
1176 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1177 const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1178 auto matrixFragments = getAllFrameworkMatrixLevels();
1179 if (!matrixFragments.ok()) return matrixFragments.error();
1180
1181 auto allAidlNames = AidlMetadataToNames(aidlMetadata);
1182 auto allHidlNames = HidlMetadataToNames(hidlMetadata);
1183 std::set<std::string> badAidlInterfaces;
1184 std::set<std::string> badHidlInterfaces;
1185
1186 std::vector<std::string> errors;
1187 for (const auto& matrix : matrixFragments.value()) {
1188 if (matrix.level() == Level::UNSPECIFIED) {
1189 LOG(INFO) << "Skip checkMatrixHalsHasDefinition() on " << matrix.fileName()
1190 << " with no level.";
1191 continue;
1192 }
1193
1194 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1195 switch (matrixInstance.format()) {
1196 case HalFormat::AIDL: {
1197 auto matrixInterface =
1198 toAidlFqnameString(matrixInstance.package(), matrixInstance.interface());
1199 if (allAidlNames.find(matrixInterface) == allAidlNames.end()) {
1200 errors.push_back(
1201 "AIDL interface " + matrixInterface + " is referenced in " +
1202 matrix.fileName() +
1203 ", but there is no corresponding .aidl definition associated with an "
1204 "aidl_interface module in this build. Typo?");
1205 }
1206 return true; // continue to next instance
1207 }
1208 case HalFormat::HIDL: {
1209 for (Version v = matrixInstance.versionRange().minVer();
1210 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1211 auto matrixInterface = matrixInstance.interfaceDescription(v);
1212 if (allHidlNames.find(matrixInterface) == allHidlNames.end()) {
1213 errors.push_back(
1214 "HIDL interface " + matrixInterface + " is referenced in " +
1215 matrix.fileName() +
1216 ", but there is no corresponding .hal definition associated with "
1217 "a hidl_interface module in this build. Typo?");
1218 }
1219 }
1220 return true; // continue to next instance
1221 }
1222 default: {
1223 // We do not have data for native HALs.
1224 return true; // continue to next instance
1225 }
1226 }
1227 });
Yifan Hong3daec812017-02-27 18:49:11 -08001228 }
1229
1230 if (!errors.empty()) {
1231 return android::base::Error() << android::base::Join(errors, "\n");
1232 }
1233
1234 return {};
1235}
1236
1237// make_unique does not work because VintfObject constructor is private.
1238VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
1239
1240VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
1241 mObject->mFileSystem = std::move(e);
1242 return *this;
1243}
1244
1245VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
1246 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
1247 mObject->mRuntimeInfoFactory = std::move(e);
1248 return *this;
1249}
1250
1251VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
1252 std::unique_ptr<PropertyFetcher>&& e) {
1253 mObject->mPropertyFetcher = std::move(e);
1254 return *this;
1255}
1256
1257std::unique_ptr<VintfObject> VintfObject::Builder::build() {
1258 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1259 if (!mObject->mRuntimeInfoFactory)
1260 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1261 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1262 return std::move(mObject);
1263}
1264
Yifan Hongd7043972020-03-30 13:27:46 -07001265} // namespace vintf
1266} // namespace android