1 /*
2  * Copyright (C) 2018 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 #ifndef LIBVINTF_TARGET
18 #define LOG_TAG "libvintf"
19 #include <android-base/logging.h>
20 #endif
21 
22 #include "ManifestInstance.h"
23 
24 #include <utility>
25 
26 #include <android-base/logging.h>
27 
28 #include "parse_string.h"
29 
30 namespace android {
31 namespace vintf {
32 
33 ManifestInstance::ManifestInstance() = default;
34 
35 ManifestInstance::ManifestInstance(const ManifestInstance&) = default;
36 
37 ManifestInstance::ManifestInstance(ManifestInstance&&) noexcept = default;
38 
39 ManifestInstance& ManifestInstance::operator=(const ManifestInstance&) = default;
40 
41 ManifestInstance& ManifestInstance::operator=(ManifestInstance&&) noexcept = default;
42 
43 ManifestInstance::ManifestInstance(FqInstance&& fqInstance, TransportArch&& ta, HalFormat fmt,
44                                    std::optional<std::string>&& updatableViaApex)
45     : mFqInstance(std::move(fqInstance)),
46       mTransportArch(std::move(ta)),
47       mHalFormat(fmt),
48       mUpdatableViaApex(std::move(updatableViaApex)) {}
49 
50 ManifestInstance::ManifestInstance(const FqInstance& fqInstance, const TransportArch& ta,
51                                    HalFormat fmt,
52                                    const std::optional<std::string>& updatableViaApex)
53     : mFqInstance(fqInstance),
54       mTransportArch(ta),
55       mHalFormat(fmt),
56       mUpdatableViaApex(updatableViaApex) {}
57 
58 const std::string& ManifestInstance::package() const {
59     return mFqInstance.getPackage();
60 }
61 
62 Version ManifestInstance::version() const {
63     return mFqInstance.getVersion();
64 }
65 
66 const std::string& ManifestInstance::interface() const {
67     return mFqInstance.getInterface();
68 }
69 
70 const std::string& ManifestInstance::instance() const {
71     return mFqInstance.getInstance();
72 }
73 
74 Transport ManifestInstance::transport() const {
75     return mTransportArch.transport;
76 }
77 
78 Arch ManifestInstance::arch() const {
79     return mTransportArch.arch;
80 }
81 
82 HalFormat ManifestInstance::format() const {
83     return mHalFormat;
84 }
85 
86 const std::optional<std::string>& ManifestInstance::updatableViaApex() const {
87     return mUpdatableViaApex;
88 }
89 
90 const FqInstance& ManifestInstance::getFqInstance() const {
91     return mFqInstance;
92 }
93 
94 bool ManifestInstance::operator==(const ManifestInstance& other) const {
95     return mFqInstance == other.mFqInstance && mTransportArch == other.mTransportArch &&
96            mHalFormat == other.mHalFormat && mUpdatableViaApex == other.mUpdatableViaApex;
97 }
98 bool ManifestInstance::operator<(const ManifestInstance& other) const {
99     if (mFqInstance < other.mFqInstance) return true;
100     if (other.mFqInstance < mFqInstance) return false;
101     if (mTransportArch < other.mTransportArch) return true;
102     if (other.mTransportArch < mTransportArch) return false;
103     if (mHalFormat < other.mHalFormat) return true;
104     if (other.mHalFormat < mHalFormat) return false;
105     return mUpdatableViaApex < other.mUpdatableViaApex;
106 }
107 
108 std::string ManifestInstance::getSimpleFqInstance() const {
109     FqInstance e;
110     bool success = false;
111     switch (format()) {
112         case HalFormat::AIDL: {
113             // Hide fake version when printing to manifest XML <fqname> tag.
114             success = e.setTo(interface(), instance());
115         } break;
116         case HalFormat::HIDL:
117             [[fallthrough]];
118         case HalFormat::NATIVE: {
119             success = e.setTo(version().majorVer, version().minorVer, interface(), instance());
120         } break;
121     }
122 #ifndef LIBVINTF_TARGET
123     CHECK(success) << "Cannot get simple fqinstnance from '" << mFqInstance.string() << "'";
124 #endif
125     return success ? e.string() : "";
126 }
127 
128 std::string ManifestInstance::description() const {
129     switch (format()) {
130         case HalFormat::AIDL: {
131             return toAidlFqnameString(package(), interface(), instance()) + " (@" +
132                    aidlVersionToString(version()) + ")";
133         } break;
134         case HalFormat::HIDL:
135             [[fallthrough]];
136         case HalFormat::NATIVE: {
137             return getFqInstance().string();
138         } break;
139     }
140 }
141 
142 std::string ManifestInstance::descriptionWithoutPackage() const {
143     switch (format()) {
144         case HalFormat::AIDL: {
145             return toFQNameString(interface(), instance()) + " (@" +
146                    aidlVersionToString(version()) + ")";
147         } break;
148         case HalFormat::HIDL:
149             [[fallthrough]];
150         case HalFormat::NATIVE: {
151             return getSimpleFqInstance();
152         } break;
153     }
154 }
155 
156 ManifestInstance ManifestInstance::withVersion(const Version& v) const {
157     FqInstance fqInstance;
158     CHECK(fqInstance.setTo(getFqInstance().getPackage(), v.majorVer, v.minorVer,
159                            getFqInstance().getInterface(), getFqInstance().getInstance()));
160     return ManifestInstance(std::move(fqInstance), mTransportArch, format(), mUpdatableViaApex);
161 }
162 
163 }  // namespace vintf
164 }  // namespace android
165