1 /*
2  * Copyright (C) 2021 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 <vintf/VintfObjectRecovery.h>
18 
19 #include "VintfObjectUtils.h"
20 #include "constants-private.h"
21 #include "utils.h"
22 
23 using std::placeholders::_1;
24 using std::placeholders::_2;
25 
26 namespace android::vintf {
27 
28 using details::Get;
29 using details::kSystemManifest;
30 using details::kSystemManifestFragmentDir;
31 
GetInstance()32 std::shared_ptr<VintfObjectRecovery> VintfObjectRecovery::GetInstance() {
33     static details::LockedSharedPtr<VintfObjectRecovery> sInstance{};
34     std::unique_lock<std::mutex> lock(sInstance.mutex);
35     if (sInstance.object == nullptr) {
36         std::unique_ptr<VintfObjectRecovery> uptr =
37             VintfObjectRecovery::Builder().build<VintfObjectRecovery>();
38         sInstance.object = std::shared_ptr<VintfObjectRecovery>(uptr.release());
39     }
40     return sInstance.object;
41 }
42 
getRecoveryHalManifest()43 std::shared_ptr<const HalManifest> VintfObjectRecovery::getRecoveryHalManifest() {
44     return Get(__func__, &mRecoveryManifest,
45                std::bind(&VintfObjectRecovery::fetchRecoveryHalManifest, this, _1, _2));
46 }
47 
48 // All manifests are installed under /system/etc/vintf.
49 // There may be mixed framework and device manifests under that directory. Treat them all
50 // as device manifest fragments.
51 // Priority:
52 // 1. /system/etc/vintf/manifest.xml
53 //    + /system/etc/vintf/manifest/*.xml if they exist
fetchRecoveryHalManifest(HalManifest * out,std::string * error)54 status_t VintfObjectRecovery::fetchRecoveryHalManifest(HalManifest* out, std::string* error) {
55     HalManifest manifest;
56     status_t systemEtcStatus = fetchOneHalManifest(kSystemManifest, &manifest, error);
57     if (systemEtcStatus != OK && systemEtcStatus != NAME_NOT_FOUND) {
58         return systemEtcStatus;
59     }
60     // Merge |manifest| to |out| only if the main manifest is found.
61     if (systemEtcStatus == OK) {
62         *out = std::move(manifest);
63     }
64     out->setType(SchemaType::DEVICE);
65     status_t fragmentStatus =
66         addDirectoryManifests(kSystemManifestFragmentDir, out, true /* forceSchemaType */, error);
67     if (fragmentStatus != OK) {
68         return fragmentStatus;
69     }
70     return OK;
71 }
72 
Builder()73 VintfObjectRecovery::Builder::Builder()
74     : VintfObjectBuilder(std::unique_ptr<VintfObject>(new VintfObjectRecovery())) {}
75 
76 }  // namespace android::vintf
77