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
ManifestInstance(FqInstance && fqInstance,TransportArch && ta,HalFormat fmt,std::optional<std::string> && updatableViaApex,ExclusiveTo exclusiveTo,std::optional<std::string> && accessor,bool updatableViaSystem)43 ManifestInstance::ManifestInstance(FqInstance&& fqInstance, TransportArch&& ta, HalFormat fmt,
44 std::optional<std::string>&& updatableViaApex,
45 ExclusiveTo exclusiveTo, std::optional<std::string>&& accessor,
46 bool updatableViaSystem)
47 : mFqInstance(std::move(fqInstance)),
48 mTransportArch(std::move(ta)),
49 mHalFormat(fmt),
50 mUpdatableViaApex(std::move(updatableViaApex)),
51 mExclusiveTo(std::move(exclusiveTo)),
52 mAccessor(std::move(accessor)),
53 mUpdatableViaSystem(std::move(updatableViaSystem)) {}
54
ManifestInstance(const FqInstance & fqInstance,const TransportArch & ta,HalFormat fmt,const std::optional<std::string> & updatableViaApex,ExclusiveTo exclusiveTo,const std::optional<std::string> & accessor,bool updatableViaSystem)55 ManifestInstance::ManifestInstance(const FqInstance& fqInstance, const TransportArch& ta,
56 HalFormat fmt,
57 const std::optional<std::string>& updatableViaApex,
58 ExclusiveTo exclusiveTo,
59 const std::optional<std::string>& accessor,
60 bool updatableViaSystem)
61 : mFqInstance(fqInstance),
62 mTransportArch(ta),
63 mHalFormat(fmt),
64 mUpdatableViaApex(updatableViaApex),
65 mExclusiveTo(exclusiveTo),
66 mAccessor(accessor),
67 mUpdatableViaSystem(updatableViaSystem) {}
68
package() const69 const std::string& ManifestInstance::package() const {
70 return mFqInstance.getPackage();
71 }
72
version() const73 Version ManifestInstance::version() const {
74 return mFqInstance.getVersion();
75 }
76
interface() const77 std::string ManifestInstance::interface() const {
78 return mFqInstance.getInterface();
79 }
80
instance() const81 const std::string& ManifestInstance::instance() const {
82 return mFqInstance.getInstance();
83 }
84
transport() const85 Transport ManifestInstance::transport() const {
86 return mTransportArch.transport;
87 }
88
arch() const89 Arch ManifestInstance::arch() const {
90 return mTransportArch.arch;
91 }
92
ip() const93 const std::optional<std::string> ManifestInstance::ip() const {
94 return mTransportArch.ip;
95 }
96
port() const97 const std::optional<uint64_t> ManifestInstance::port() const {
98 return mTransportArch.port;
99 }
100
format() const101 HalFormat ManifestInstance::format() const {
102 return mHalFormat;
103 }
104
updatableViaApex() const105 const std::optional<std::string>& ManifestInstance::updatableViaApex() const {
106 return mUpdatableViaApex;
107 }
108
exclusiveTo() const109 ExclusiveTo ManifestInstance::exclusiveTo() const {
110 return mExclusiveTo;
111 }
112
accessor() const113 const std::optional<std::string>& ManifestInstance::accessor() const {
114 return mAccessor;
115 }
116
getFqInstance() const117 const FqInstance& ManifestInstance::getFqInstance() const {
118 return mFqInstance;
119 }
120
updatableViaSystem() const121 bool ManifestInstance::updatableViaSystem() const {
122 return mUpdatableViaSystem;
123 }
124
operator ==(const ManifestInstance & other) const125 bool ManifestInstance::operator==(const ManifestInstance& other) const {
126 return mFqInstance == other.mFqInstance && mTransportArch == other.mTransportArch &&
127 mHalFormat == other.mHalFormat && mUpdatableViaApex == other.mUpdatableViaApex &&
128 mUpdatableViaSystem == other.mUpdatableViaSystem && mAccessor == other.mAccessor;
129 }
operator <(const ManifestInstance & other) const130 bool ManifestInstance::operator<(const ManifestInstance& other) const {
131 if (mFqInstance < other.mFqInstance) return true;
132 if (other.mFqInstance < mFqInstance) return false;
133 if (mTransportArch < other.mTransportArch) return true;
134 if (other.mTransportArch < mTransportArch) return false;
135 if (mHalFormat < other.mHalFormat) return true;
136 if (other.mHalFormat < mHalFormat) return false;
137 if (mUpdatableViaApex < other.mUpdatableViaApex) return true;
138 if (other.mUpdatableViaApex < mUpdatableViaApex) return false;
139 if (mUpdatableViaSystem < other.mUpdatableViaSystem) return true;
140 if (other.mUpdatableViaSystem < mUpdatableViaSystem) return false;
141 return mAccessor < other.mAccessor;
142 }
143
getSimpleFqInstance() const144 std::string ManifestInstance::getSimpleFqInstance() const {
145 FqInstance e;
146 bool success = false;
147 switch (format()) {
148 case HalFormat::AIDL: {
149 // Hide fake version when printing to manifest XML <fqname> tag.
150 success = e.setTo(interface(), instance());
151 } break;
152 case HalFormat::HIDL:
153 [[fallthrough]];
154 case HalFormat::NATIVE: {
155 success = e.setTo(version().majorVer, version().minorVer, interface(), instance());
156 } break;
157 }
158 #ifndef LIBVINTF_TARGET
159 CHECK(success) << "Cannot get simple fqinstnance from '" << mFqInstance.string() << "'";
160 #endif
161 return success ? e.string() : "";
162 }
163
description() const164 std::string ManifestInstance::description() const {
165 switch (format()) {
166 case HalFormat::AIDL: {
167 return toAidlFqnameString(package(), interface(), instance()) + " (@" +
168 aidlVersionToString(version()) + ")";
169 } break;
170 case HalFormat::HIDL:
171 [[fallthrough]];
172 case HalFormat::NATIVE: {
173 return getFqInstance().string();
174 } break;
175 }
176 }
177
descriptionWithoutPackage() const178 std::string ManifestInstance::descriptionWithoutPackage() const {
179 switch (format()) {
180 case HalFormat::AIDL: {
181 return toFQNameString(interface(), instance()) + " (@" +
182 aidlVersionToString(version()) + ")";
183 } break;
184 case HalFormat::HIDL:
185 [[fallthrough]];
186 case HalFormat::NATIVE: {
187 return getSimpleFqInstance();
188 } break;
189 }
190 }
191
nameWithVersion() const192 std::string ManifestInstance::nameWithVersion() const {
193 switch (format()) {
194 case HalFormat::HIDL:
195 [[fallthrough]];
196 case HalFormat::NATIVE:
197 return toFQNameString(package(), version());
198 break;
199 case HalFormat::AIDL:
200 return package() + "@" + aidlVersionToString(version());
201 break;
202 }
203 }
204
withVersion(const Version & v) const205 ManifestInstance ManifestInstance::withVersion(const Version& v) const {
206 FqInstance fqInstance;
207 CHECK(fqInstance.setTo(getFqInstance().getPackage(), v.majorVer, v.minorVer,
208 getFqInstance().getInterface(), getFqInstance().getInstance()));
209 return ManifestInstance(std::move(fqInstance), mTransportArch, format(), mUpdatableViaApex,
210 mExclusiveTo, mAccessor, mUpdatableViaSystem);
211 }
212
213 } // namespace vintf
214 } // namespace android
215