blob: b706599d8eeb94aab713e7b530b75dd201549b11 [file] [log] [blame]
Yifan Hong3daec812017-02-27 18:49:11 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "VintfObject.h"
18
Yifan Hongd52bf3e2018-01-11 16:56:51 -080019#include <dirent.h>
20
Yifan Hong3daec812017-02-27 18:49:11 -080021#include <functional>
22#include <memory>
23#include <mutex>
24
Yifan Hong60217032018-01-08 16:19:42 -080025#include <android-base/logging.h>
26
Yifan Hong12e23c22018-11-05 14:53:30 -080027#include "CompatibilityMatrix.h"
Yifan Hong12e23c22018-11-05 14:53:30 -080028#include "parse_string.h"
29#include "parse_xml.h"
30#include "utils.h"
31
Yifan Hong60217032018-01-08 16:19:42 -080032using std::placeholders::_1;
33using std::placeholders::_2;
34
Yifan Hong3daec812017-02-27 18:49:11 -080035namespace android {
36namespace vintf {
37
Yifan Hong270b5652018-01-18 18:46:01 -080038using namespace details;
39
Yifan Hong9f78c182018-07-12 14:45:52 -070040#ifdef LIBVINTF_TARGET
41static constexpr bool kIsTarget = true;
42#else
43static constexpr bool kIsTarget = false;
44#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070045
Yifan Hong3daec812017-02-27 18:49:11 -080046template <typename T, typename F>
Yifan Hongfc73edf2017-08-29 11:39:07 -070047static std::shared_ptr<const T> Get(
48 LockedSharedPtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070049 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080050 const F &fetchAllInformation) {
51 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080052 if (skipCache || !ptr->fetchedOnce) {
Yifan Hong3daec812017-02-27 18:49:11 -080053 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080054 std::string error;
55 if (fetchAllInformation(ptr->object.get(), &error) != OK) {
56 LOG(WARNING) << error;
Yifan Hong3daec812017-02-27 18:49:11 -080057 ptr->object = nullptr; // frees the old object
58 }
Yifan Hong7219ba12018-01-08 16:23:49 -080059 ptr->fetchedOnce = true;
Yifan Hong3daec812017-02-27 18:49:11 -080060 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070061 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080062}
63
Yifan Hong9f78c182018-07-12 14:45:52 -070064static std::unique_ptr<FileSystem> createDefaultFileSystem() {
65 std::unique_ptr<FileSystem> fileSystem;
66 if (kIsTarget) {
67 fileSystem = std::make_unique<details::FileSystemImpl>();
68 } else {
69 fileSystem = std::make_unique<details::FileSystemNoOp>();
70 }
71 return fileSystem;
72}
73
74static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
75 std::unique_ptr<PropertyFetcher> propertyFetcher;
76 if (kIsTarget) {
77 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
78 } else {
79 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
80 }
81 return propertyFetcher;
82}
83
Yifan Hong9f78c182018-07-12 14:45:52 -070084details::LockedSharedPtr<VintfObject> VintfObject::sInstance{};
85std::shared_ptr<VintfObject> VintfObject::GetInstance() {
86 std::unique_lock<std::mutex> lock(sInstance.mutex);
87 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -080088 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -070089 }
90 return sInstance.object;
91}
92
Yifan Hongfc73edf2017-08-29 11:39:07 -070093std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -070094 return GetInstance()->getDeviceHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -080095}
96
Yifan Hong9f78c182018-07-12 14:45:52 -070097std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
98 return Get(&mDeviceManifest, skipCache,
99 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
100}
101
Yifan Hongfc73edf2017-08-29 11:39:07 -0700102std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700103 return GetInstance()->getFrameworkHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800104}
105
Yifan Hong9f78c182018-07-12 14:45:52 -0700106std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
107 return Get(&mFrameworkManifest, skipCache,
108 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
109}
Yifan Hong2272bf82017-04-28 14:37:56 -0700110
Yifan Hongfc73edf2017-08-29 11:39:07 -0700111std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700112 return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
Yifan Hong2272bf82017-04-28 14:37:56 -0700113}
114
Yifan Hong9f78c182018-07-12 14:45:52 -0700115std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
116 bool skipCache) {
117 return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
118}
119
Yifan Hongfc73edf2017-08-29 11:39:07 -0700120std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700121 return GetInstance()->getFrameworkCompatibilityMatrix(skipCache);
122}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800123
Yifan Hong9f78c182018-07-12 14:45:52 -0700124std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix(
125 bool skipCache) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800126 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700127 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800128
Yifan Hong9f78c182018-07-12 14:45:52 -0700129 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800130
131 auto combined =
Yifan Hong9f78c182018-07-12 14:45:52 -0700132 Get(&mCombinedFrameworkMatrix, skipCache,
133 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800134 if (combined != nullptr) {
135 return combined;
136 }
137
Yifan Hong9f78c182018-07-12 14:45:52 -0700138 return Get(&mFrameworkMatrix, skipCache,
Yifan Hong12e23c22018-11-05 14:53:30 -0800139 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700140 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700141}
142
Yifan Hong9f78c182018-07-12 14:45:52 -0700143status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800144 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
145 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800146 std::vector<Named<CompatibilityMatrix>> matrixFragments;
147 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
148 if (matrixFragmentsStatus != OK) {
149 return matrixFragmentsStatus;
150 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800151 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800152 if (error && error->empty()) {
153 *error = "Cannot get framework matrix for each FCM version for unknown error.";
154 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800155 return NAME_NOT_FOUND;
156 }
157
158 Level deviceLevel = Level::UNSPECIFIED;
159
160 if (deviceManifest != nullptr) {
161 deviceLevel = deviceManifest->level();
162 }
163
164 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800165 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800166 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800167 if (shippingApi != 0u) {
168 deviceLevel = details::convertFromApiLevel(shippingApi);
169 }
170 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800171
172 if (deviceLevel == Level::UNSPECIFIED) {
173 // Cannot infer FCM version. Combine all matrices by assuming
174 // Shipping FCM Version == min(all supported FCM Versions in the framework)
175 for (auto&& pair : matrixFragments) {
176 Level fragmentLevel = pair.object.level();
177 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
178 deviceLevel = fragmentLevel;
179 }
180 }
181 }
182
183 if (deviceLevel == Level::UNSPECIFIED) {
184 // None of the fragments specify any FCM version. Should never happen except
185 // for inconsistent builds.
186 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800187 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800188 " declare FCM version.";
189 }
190 return NAME_NOT_FOUND;
191 }
192
Yifan Honge7837b12018-10-11 10:38:57 -0700193 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800194 if (combined == nullptr) {
195 return BAD_VALUE;
196 }
197 *out = std::move(*combined);
198 return OK;
199}
200
Steven Morelandeedf2d42018-04-04 16:36:27 -0700201// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700202status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700203 std::string* error) {
204 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800205 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700206 // if the directory isn't there, that's okay
207 if (err == NAME_NOT_FOUND) return OK;
208 if (err != OK) return err;
209
210 for (const std::string& file : fileNames) {
211 // Only adds HALs because all other things are added by libvintf
212 // itself for now.
213 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700214 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700215 if (err != OK) return err;
216
Yifan Hong4c6962d2019-01-30 15:50:46 -0800217 if (!manifest->addAll(&fragmentManifest, error)) {
218 if (error) {
219 error->insert(0, "Cannot add manifest fragment " + directory + file + ":");
220 }
221 return UNKNOWN_ERROR;
222 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700223 }
224
225 return OK;
226}
227
Yifan Hong5a93bf22018-01-17 18:22:32 -0800228// Priority for loading vendor manifest:
Steven Morelandeedf2d42018-04-04 16:36:27 -0700229// 1. /vendor/etc/vintf/manifest.xml + device fragments + ODM manifest (optional) + odm fragments
230// 2. /vendor/etc/vintf/manifest.xml + device fragments
231// 3. ODM manifest (optional) + odm fragments
232// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800233// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700234// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
235// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700236status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
237 status_t vendorStatus = fetchOneHalManifest(kVendorManifest, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800238 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
239 return vendorStatus;
240 }
241
Steven Morelandeedf2d42018-04-04 16:36:27 -0700242 if (vendorStatus == OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700243 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700244 if (fragmentStatus != OK) {
245 return fragmentStatus;
246 }
247 }
248
Yifan Hong5a93bf22018-01-17 18:22:32 -0800249 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700250 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800251 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
252 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800253 }
254
Yifan Hong5a93bf22018-01-17 18:22:32 -0800255 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800256 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800257 if (!out->addAll(&odmManifest, error)) {
258 if (error) {
259 error->insert(0, "Cannot add ODM manifest :");
260 }
261 return UNKNOWN_ERROR;
262 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800263 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700264 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800265 }
266
Yifan Hong12a11ac2018-01-19 13:58:32 -0800267 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800268 if (odmStatus == OK) {
269 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700270 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800271 }
272
273 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800274 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800275}
276
Yifan Hong12a11ac2018-01-19 13:58:32 -0800277// "out" is written to iff return status is OK.
278// Priority:
279// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
280// 2. /odm/etc/vintf/manifest.xml
281// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
282// 4. /odm/etc/manifest.xml
283// where:
284// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700285status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800286 status_t status;
287
Yifan Hong12a11ac2018-01-19 13:58:32 -0800288 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800289 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800290
291 if (!productModel.empty()) {
292 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700293 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800294 if (status == OK || status != NAME_NOT_FOUND) {
295 return status;
296 }
297 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800298
Yifan Hong9f78c182018-07-12 14:45:52 -0700299 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800300 if (status == OK || status != NAME_NOT_FOUND) {
301 return status;
302 }
303
Yifan Hong12a11ac2018-01-19 13:58:32 -0800304 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700305 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800306 error);
307 if (status == OK || status != NAME_NOT_FOUND) {
308 return status;
309 }
310 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800311
Yifan Hong9f78c182018-07-12 14:45:52 -0700312 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800313 if (status == OK || status != NAME_NOT_FOUND) {
314 return status;
315 }
316
317 return NAME_NOT_FOUND;
318}
319
320// Fetch one manifest.xml file. "out" is written to iff return status is OK.
321// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700322status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800323 std::string* error) {
324 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800325 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800326 if (status == OK) {
327 *out = std::move(ret);
328 }
329 return status;
330}
331
Yifan Hong9f78c182018-07-12 14:45:52 -0700332status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800333 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800334 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800335 *out = std::move(etcMatrix);
336 return OK;
337 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800338 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800339}
340
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700341// Priority:
342// 1. /system/etc/vintf/manifest.xml
343// + /system/etc/vintf/manifest/*.xml if they exist
344// + /product/etc/vintf/manifest.xml if it exists
345// + /product/etc/vintf/manifest/*.xml if they exist
346// 2. (deprecated) /system/manifest.xml
Yifan Hong9f78c182018-07-12 14:45:52 -0700347status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700348 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
349 if (systemEtcStatus == OK) {
350 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
351 if (dirStatus != OK) {
352 return dirStatus;
353 }
354
355 HalManifest productManifest;
356 auto productStatus = fetchOneHalManifest(kProductManifest, &productManifest, error);
357 if (productStatus != OK && productStatus != NAME_NOT_FOUND) {
358 return productStatus;
359 }
360 if (productStatus == OK) {
361 if (!out->addAll(&productManifest, error)) {
362 if (error) {
363 error->insert(0, "Cannot add " + kProductManifest + ":");
364 }
365 return UNKNOWN_ERROR;
366 }
367 }
368
369 return addDirectoryManifests(kProductManifestFragmentDir, out, error);
370 } else {
371 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
372 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800373 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700374
Yifan Hong12e23c22018-11-05 14:53:30 -0800375 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800376}
377
Yifan Hong9f8f6562018-11-06 16:26:20 -0800378static void appendLine(std::string* error, const std::string& message) {
379 if (error != nullptr) {
380 if (!error->empty()) *error += "\n";
381 *error += message;
382 }
383}
384
Yifan Hong73bde592019-01-22 13:30:23 -0800385status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
386 std::string* error) {
387 std::string content;
388 status_t status = getFileSystem()->fetch(path, &content, error);
389 if (status != OK) {
390 return status;
391 }
392 if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
393 if (error) {
394 error->insert(0, "Cannot parse " + path + ": ");
395 }
396 return BAD_VALUE;
397 }
398 out->name = path;
399 return OK;
400}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800401
Yifan Hong73bde592019-01-22 13:30:23 -0800402status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
403 std::string* error) {
404 std::vector<std::string> fileNames;
405
406 status_t listStatus = getFileSystem()->listFiles(kSystemVintfDir, &fileNames, error);
407 if (listStatus != OK) {
408 return listStatus;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800409 }
410 for (const std::string& fileName : fileNames) {
Yifan Hong270b5652018-01-18 18:46:01 -0800411 std::string path = kSystemVintfDir + fileName;
Yifan Hong73bde592019-01-22 13:30:23 -0800412 Named<CompatibilityMatrix> namedMatrix;
413 std::string matrixError;
414 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
415 if (matrixStatus != OK) {
416 // System manifests and matrices share the same dir. Client may not have enough
417 // permissions to read system manifests, or may not be able to parse it.
418 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
419 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800420 continue;
421 }
Yifan Hong73bde592019-01-22 13:30:23 -0800422 results->emplace_back(std::move(namedMatrix));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800423 }
424
Yifan Hong73bde592019-01-22 13:30:23 -0800425 Named<CompatibilityMatrix> productMatrix;
426 std::string productError;
427 status_t productStatus = getOneMatrix(kProductMatrix, &productMatrix, &productError);
428 if (productStatus == OK) {
429 results->emplace_back(std::move(productMatrix));
430 } else if (productStatus == NAME_NOT_FOUND) {
431 LOG(DEBUG) << "Framework Matrix: missing " << kProductMatrix;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800432 } else {
Yifan Hong73bde592019-01-22 13:30:23 -0800433 if (error) *error = std::move(productError);
434 return productStatus;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800435 }
436
Yifan Hong73bde592019-01-22 13:30:23 -0800437 if (results->empty()) {
438 if (error) {
439 *error =
440 "No framework matrices under " + kSystemVintfDir + " can be fetched or parsed.\n";
441 }
442 return NAME_NOT_FOUND;
443 }
444 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800445}
446
Yifan Hong1fb004e2017-09-26 15:04:44 -0700447std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
448 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700449 return GetInstance()->getRuntimeInfo(skipCache, flags);
450}
451std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
452 RuntimeInfo::FetchFlags flags) {
453 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700454
455 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700456 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700457 }
458
Yifan Hong9f78c182018-07-12 14:45:52 -0700459 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800460 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700461 }
462
Yifan Hongf3247982019-12-12 12:11:36 -0800463 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
464 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
465 auto manifest = getDeviceHalManifest();
466 if (!manifest) {
467 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
468 return nullptr;
469 }
470 Level level = Level::UNSPECIFIED;
471 if (manifest->kernel().has_value()) {
472 level = manifest->kernel()->level();
473 }
474 mDeviceRuntimeInfo.object->setKernelLevel(level);
475 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
476 }
477
Yifan Hong9f78c182018-07-12 14:45:52 -0700478 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700479 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700480 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700481 return nullptr;
482 }
483
Yifan Hong9f78c182018-07-12 14:45:52 -0700484 mDeviceRuntimeInfo.fetchedFlags |= flags;
485 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800486}
487
Yifan Hong12e23c22018-11-05 14:53:30 -0800488int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
489 status_t status = OK;
490 // null checks for files and runtime info
491 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700492 appendLine(error, "No framework manifest file from device or from update package");
493 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700494 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800495 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700496 appendLine(error, "No device manifest file from device or from update package");
497 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700498 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800499 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700500 appendLine(error, "No framework matrix file from device or from update package");
501 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700502 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800503 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700504 appendLine(error, "No device matrix file from device or from update package");
505 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700506 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800507
Yifan Hong072f12d2018-08-08 13:04:51 -0700508 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800509 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700510 appendLine(error, "No runtime info from device");
511 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800512 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700513 }
Yifan Hong878556e2018-07-13 13:45:30 -0700514 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700515
516 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800517 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800518 if (error) {
519 error->insert(0,
520 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700521 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800522 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700523 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800524 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800525 if (error) {
526 error->insert(0,
527 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700528 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800529 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700530 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800531
Yifan Hong072f12d2018-08-08 13:04:51 -0700532 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800533 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800534 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800535 if (error) {
536 error->insert(0,
537 "Runtime info and framework compatibility matrix are incompatible: ");
538 }
539 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700540 }
541 }
542
543 return COMPATIBLE;
544}
545
Yifan Hong9f78c182018-07-12 14:45:52 -0700546namespace details {
547
Yifan Hong270b5652018-01-18 18:46:01 -0800548const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800549const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800550const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800551const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800552
553const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800554const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800555const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800556const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800557const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700558const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800559
Steven Morelandeedf2d42018-04-04 16:36:27 -0700560const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
561const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
562const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700563const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700564
Yifan Hong270b5652018-01-18 18:46:01 -0800565const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
566const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800567const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800568const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
569const std::string kOdmLegacyVintfDir = "/odm/etc/";
570const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
571
Yifan Hong69c1b112018-02-27 17:06:00 -0800572std::vector<std::string> dumpFileList() {
573 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800574 // clang-format off
575 kSystemVintfDir,
576 kVendorVintfDir,
577 kOdmVintfDir,
578 kProductVintfDir,
579 kOdmLegacyVintfDir,
580 kVendorLegacyManifest,
581 kVendorLegacyMatrix,
582 kSystemLegacyManifest,
583 kSystemLegacyMatrix,
584 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800585 };
586}
587
Yifan Hong9f78c182018-07-12 14:45:52 -0700588} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700589
Yifan Hong9f78c182018-07-12 14:45:52 -0700590bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800591 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700592 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700593 bool isDeprecated = false;
594 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700595 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700596 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800597 }
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700598 return !isDeprecated; // continue if no deprecated instance is found.
599 });
600 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800601}
602
Yifan Honga8a8fa92018-03-20 14:42:43 -0700603// Let oldMatrixInstance = package@x.y-w::interface with instancePattern.
604// If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return
605// true iff:
606// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
607// 2. package@x.z::interface/servedInstance is in targetMatrix but
608// servedInstance is not in listInstances(package@x.z::interface)
Yifan Hong9f78c182018-07-12 14:45:52 -0700609bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800610 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700611 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700612 const std::string& package = oldMatrixInstance.package();
613 const Version& version = oldMatrixInstance.versionRange().minVer();
614 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700615
Yifan Honga8a8fa92018-03-20 14:42:43 -0700616 std::vector<std::string> instanceHint;
617 if (!oldMatrixInstance.isRegex()) {
618 instanceHint.push_back(oldMatrixInstance.exactInstance());
619 }
620
621 auto list = listInstances(package, version, interface, instanceHint);
622 for (const auto& pair : list) {
623 const std::string& servedInstance = pair.first;
624 Version servedVersion = pair.second;
625 if (!oldMatrixInstance.matchInstance(servedInstance)) {
626 continue;
627 }
628
Yifan Hongf73ba512018-01-17 15:52:30 -0800629 // Find any package@x.? in target matrix, and check if instance is in target matrix.
Yifan Hong217f47e2018-03-15 12:34:05 -0700630 bool foundInstance = false;
631 Version targetMatrixMinVer;
Yifan Hong0b1916f2019-09-10 19:04:52 -0700632 targetMatrix.forEachHidlInstanceOfPackage(package, [&](const auto& targetMatrixInstance) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700633 if (targetMatrixInstance.versionRange().majorVer == version.majorVer &&
634 targetMatrixInstance.interface() == interface &&
Yifan Honga8a8fa92018-03-20 14:42:43 -0700635 targetMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700636 targetMatrixMinVer = targetMatrixInstance.versionRange().minVer();
637 foundInstance = true;
638 }
639 return !foundInstance; // continue if not found
640 });
641 if (!foundInstance) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800642 if (error) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700643 *error = toFQNameString(package, servedVersion, interface, servedInstance) +
Steven Morelanda1b984c2018-03-09 13:09:15 -0800644 " is deprecated in compatibility matrix at FCM Version " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800645 to_string(targetMatrix.level()) + "; it should not be served.";
646 }
647 return true;
648 }
649
Yifan Hongf73ba512018-01-17 15:52:30 -0800650 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
Yifan Honga8a8fa92018-03-20 14:42:43 -0700651 bool targetVersionServed = false;
652 for (const auto& newPair :
653 listInstances(package, targetMatrixMinVer, interface, instanceHint)) {
654 if (newPair.first == servedInstance) {
655 targetVersionServed = true;
656 break;
657 }
658 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800659
660 if (!targetVersionServed) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800661 appendLine(error, toFQNameString(package, servedVersion, interface, servedInstance) +
662 " is deprecated; requires at least " +
663 to_string(targetMatrixMinVer));
Yifan Hongf73ba512018-01-17 15:52:30 -0800664 return true;
665 }
666 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700667
Yifan Hongf73ba512018-01-17 15:52:30 -0800668 return false;
669}
670
Yifan Honga8a8fa92018-03-20 14:42:43 -0700671int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700672 return GetInstance()->checkDeprecation(listInstances, error);
673}
674int32_t VintfObject::checkDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800675 std::vector<Named<CompatibilityMatrix>> matrixFragments;
676 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
677 if (matrixFragmentsStatus != OK) {
678 return matrixFragmentsStatus;
679 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800680 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800681 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800682 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800683 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800684 return NAME_NOT_FOUND;
685 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700686 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800687 if (deviceManifest == nullptr) {
688 if (error) *error = "No device manifest.";
689 return NAME_NOT_FOUND;
690 }
691 Level deviceLevel = deviceManifest->level();
692 if (deviceLevel == Level::UNSPECIFIED) {
693 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
694 return BAD_VALUE;
695 }
696
697 const CompatibilityMatrix* targetMatrix = nullptr;
698 for (const auto& namedMatrix : matrixFragments) {
699 if (namedMatrix.object.level() == deviceLevel) {
700 targetMatrix = &namedMatrix.object;
701 }
702 }
703 if (targetMatrix == nullptr) {
704 if (error)
705 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
706 return NAME_NOT_FOUND;
707 }
708
709 bool hasDeprecatedHals = false;
710 for (const auto& namedMatrix : matrixFragments) {
711 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
712 if (namedMatrix.object.level() >= deviceLevel) continue;
713
714 const auto& oldMatrix = namedMatrix.object;
715 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700716 hasDeprecatedHals |= IsHalDeprecated(hal, *targetMatrix, listInstances, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800717 }
718 }
719
720 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
721}
722
723int32_t VintfObject::CheckDeprecation(std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700724 return GetInstance()->checkDeprecation(error);
725}
726int32_t VintfObject::checkDeprecation(std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800727 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700728 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700729 ListInstances inManifest =
730 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
731 const std::vector<std::string>& /* hintInstances */) {
732 std::vector<std::pair<std::string, Version>> ret;
733 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700734 HalFormat::HIDL, package, version, interface,
735 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700736 ret.push_back(
737 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
738 return true;
739 });
740 return ret;
741 };
Yifan Hong9f78c182018-07-12 14:45:52 -0700742 return checkDeprecation(inManifest, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800743}
Yifan Hong3daec812017-02-27 18:49:11 -0800744
Yifan Hong378c4082019-12-23 13:10:57 -0800745Level VintfObject::getKernelLevel(std::string* error) {
746 auto manifest = getDeviceHalManifest();
747 if (!manifest) {
748 if (error) *error = "Cannot retrieve device manifest.";
749 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700750 }
Yifan Hong378c4082019-12-23 13:10:57 -0800751 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
752 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700753 }
Yifan Hong378c4082019-12-23 13:10:57 -0800754 if (error) *error = "Device manifest does not specify kernel FCM version.";
755 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700756}
757
Yifan Hong9f78c182018-07-12 14:45:52 -0700758const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
759 return mFileSystem;
760}
761
Yifan Hong9f78c182018-07-12 14:45:52 -0700762const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
763 return mPropertyFetcher;
764}
765
Yifan Hongd038b2b2018-11-27 13:57:56 -0800766const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700767 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700768}
769
Yifan Hong78f5b572018-11-27 14:05:03 -0800770// make_unique does not work because VintfObject constructor is private.
771VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
772
773VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
774 mObject->mFileSystem = std::move(e);
775 return *this;
776}
777
778VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
779 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
780 mObject->mRuntimeInfoFactory = std::move(e);
781 return *this;
782}
783
784VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
785 std::unique_ptr<PropertyFetcher>&& e) {
786 mObject->mPropertyFetcher = std::move(e);
787 return *this;
788}
789
790std::unique_ptr<VintfObject> VintfObject::Builder::build() {
791 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
792 if (!mObject->mRuntimeInfoFactory)
793 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
794 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
795 return std::move(mObject);
796}
797
Yifan Hong3daec812017-02-27 18:49:11 -0800798} // namespace vintf
799} // namespace android