blob: 1ef6edc0959871c436a1d3ed7f68a753fe565936 [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 Hong143cfe62017-04-13 20:18:01 -070019#include "CompatibilityMatrix.h"
20#include "parse_xml.h"
21
Yifan Hong3daec812017-02-27 18:49:11 -080022#include <functional>
23#include <memory>
24#include <mutex>
25
26namespace android {
27namespace vintf {
28
29template <typename T>
30struct LockedUniquePtr {
31 std::unique_ptr<T> object;
32 std::mutex mutex;
33};
34
35static LockedUniquePtr<HalManifest> gDeviceManifest;
36static LockedUniquePtr<HalManifest> gFrameworkManifest;
Yifan Hong2272bf82017-04-28 14:37:56 -070037static LockedUniquePtr<CompatibilityMatrix> gDeviceMatrix;
38static LockedUniquePtr<CompatibilityMatrix> gFrameworkMatrix;
Yifan Hong3daec812017-02-27 18:49:11 -080039static LockedUniquePtr<RuntimeInfo> gDeviceRuntimeInfo;
40
41template <typename T, typename F>
42static const T *Get(
43 LockedUniquePtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070044 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080045 const F &fetchAllInformation) {
46 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong143cfe62017-04-13 20:18:01 -070047 if (skipCache || ptr->object == nullptr) {
Yifan Hong3daec812017-02-27 18:49:11 -080048 ptr->object = std::make_unique<T>();
49 if (fetchAllInformation(ptr->object.get()) != OK) {
50 ptr->object = nullptr; // frees the old object
51 }
52 }
53 return ptr->object.get();
54}
55
56// static
Yifan Hong143cfe62017-04-13 20:18:01 -070057const HalManifest *VintfObject::GetDeviceHalManifest(bool skipCache) {
58 return Get(&gDeviceManifest, skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080059 std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
60 "/vendor/manifest.xml"));
61}
62
63// static
Yifan Hong143cfe62017-04-13 20:18:01 -070064const HalManifest *VintfObject::GetFrameworkHalManifest(bool skipCache) {
65 return Get(&gFrameworkManifest, skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080066 std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
67 "/system/manifest.xml"));
68}
69
Yifan Hong2272bf82017-04-28 14:37:56 -070070
71// static
72const CompatibilityMatrix *VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
73 return Get(&gDeviceMatrix, skipCache,
74 std::bind(&CompatibilityMatrix::fetchAllInformation, std::placeholders::_1,
75 "/vendor/compatibility_matrix.xml"));
76}
77
78// static
79const CompatibilityMatrix *VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
80 return Get(&gFrameworkMatrix, skipCache,
81 std::bind(&CompatibilityMatrix::fetchAllInformation, std::placeholders::_1,
82 "/system/compatibility_matrix.xml"));
83}
84
Yifan Hong3daec812017-02-27 18:49:11 -080085// static
Yifan Hong143cfe62017-04-13 20:18:01 -070086const RuntimeInfo *VintfObject::GetRuntimeInfo(bool skipCache) {
87 return Get(&gDeviceRuntimeInfo, skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080088 std::bind(&RuntimeInfo::fetchAllInformation, std::placeholders::_1));
89}
90
Yifan Hong143cfe62017-04-13 20:18:01 -070091namespace details {
92
Yifan Hong9532bd22017-04-14 15:30:52 -070093static status_t fakeMountUmount() {
Yifan Hong143cfe62017-04-13 20:18:01 -070094 return OK;
95}
96
97enum class ParseStatus {
98 OK,
99 PARSE_ERROR,
100 DUPLICATED_FWK_ENTRY,
101 DUPLICATED_DEV_ENTRY,
102};
103
Yifan Hong9532bd22017-04-14 15:30:52 -0700104static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700105 switch(status) {
106 case ParseStatus::OK: return "OK";
107 case ParseStatus::PARSE_ERROR: return "parse error";
108 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
109 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
110 }
111 return "";
112}
113
114template<typename T>
Yifan Hong9532bd22017-04-14 15:30:52 -0700115static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse,
Yifan Hong143cfe62017-04-13 20:18:01 -0700116 std::unique_ptr<T> *fwk, std::unique_ptr<T> *dev) {
117 std::unique_ptr<T> ret = std::make_unique<T>();
118 if (!parse(ret.get(), xml)) {
119 return ParseStatus::PARSE_ERROR;
120 }
121 if (ret->type() == SchemaType::FRAMEWORK) {
122 if (fwk->get() != nullptr) {
123 return ParseStatus::DUPLICATED_FWK_ENTRY;
124 }
125 *fwk = std::move(ret);
126 } else if (ret->type() == SchemaType::DEVICE) {
127 if (dev->get() != nullptr) {
128 return ParseStatus::DUPLICATED_DEV_ENTRY;
129 }
130 *dev = std::move(ret);
131 }
132 return ParseStatus::OK;
133}
134
135template<typename T, typename GetFunction>
136static status_t getMissing(const T *pkg, bool mount,
137 std::function<status_t(void)> mountFunction,
Yifan Hong9532bd22017-04-14 15:30:52 -0700138 std::function<status_t(void)> umountFunction,
Yifan Hong143cfe62017-04-13 20:18:01 -0700139 const T **updated,
140 GetFunction getFunction) {
141 if (pkg != nullptr) {
142 *updated = pkg;
143 } else {
144 if (mount) {
145 (void)mountFunction(); // ignore mount errors
146 }
147 *updated = getFunction();
Yifan Hong9532bd22017-04-14 15:30:52 -0700148 if (mount) {
149 (void)umountFunction(); // ignore umount errors
150 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700151 }
152 return OK;
153}
154
155#define ADD_MESSAGE(__error__) \
156 if (error != nullptr) { \
157 *error += (__error__); \
158 } \
159
160struct PackageInfo {
161 struct Pair {
162 std::unique_ptr<HalManifest> manifest;
163 std::unique_ptr<CompatibilityMatrix> matrix;
164 };
165 Pair dev;
166 Pair fwk;
167};
168
169struct UpdatedInfo {
170 struct Pair {
171 const HalManifest *manifest;
172 const CompatibilityMatrix *matrix;
173 };
174 Pair dev;
175 Pair fwk;
176 const RuntimeInfo *runtimeInfo;
177};
178
179// Parse all information from package;
180// Get missing information from the device;
181// Do compatibility check.
182int32_t checkCompatibility(const std::vector<std::string> &xmls, bool mount,
183 std::function<status_t(void)> mountSystem,
Yifan Hong9532bd22017-04-14 15:30:52 -0700184 std::function<status_t(void)> umountSystem,
Yifan Hong143cfe62017-04-13 20:18:01 -0700185 std::function<status_t(void)> mountVendor,
Yifan Hong9532bd22017-04-14 15:30:52 -0700186 std::function<status_t(void)> umountVendor,
Yifan Hong143cfe62017-04-13 20:18:01 -0700187 std::function<const HalManifest *(bool)> GetFrameworkHalManifest,
188 std::function<const HalManifest *(bool)> GetDeviceHalManifest,
189 std::function<const RuntimeInfo *(bool)> GetRuntimeInfo,
190 std::string *error) {
191
192 status_t status;
193 ParseStatus parseStatus;
194 PackageInfo pkg; // All information from package.
195 UpdatedInfo updated; // All files and runtime info after the update.
196
197 if (xmls.empty()) {
198 ADD_MESSAGE("nothing to update");
199 return BAD_VALUE;
200 }
201
202 // parse all information from package
203 for (const auto &xml : xmls) {
204 parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest);
205 if (parseStatus == ParseStatus::OK) {
206 continue; // work on next one
207 }
208 if (parseStatus != ParseStatus::PARSE_ERROR) {
209 ADD_MESSAGE(toString(parseStatus) + " manifest");
210 return ALREADY_EXISTS;
211 }
212 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix);
213 if (parseStatus == ParseStatus::OK) {
214 continue; // work on next one
215 }
216 if (parseStatus != ParseStatus::PARSE_ERROR) {
217 ADD_MESSAGE(toString(parseStatus) + " matrix");
218 return ALREADY_EXISTS;
219 }
220 ADD_MESSAGE(toString(parseStatus)); // parse error
221 return BAD_VALUE;
222 }
223
224 // get missing info from device
Yifan Hong9532bd22017-04-14 15:30:52 -0700225 if ((status = getMissing(pkg.fwk.manifest.get(), mount, mountSystem, umountSystem,
226 &updated.fwk.manifest,
Yifan Hong143cfe62017-04-13 20:18:01 -0700227 std::bind(GetFrameworkHalManifest, true /* skipCache */))) != OK) {
228 return status;
229 }
Yifan Hong9532bd22017-04-14 15:30:52 -0700230 if ((status = getMissing(pkg.dev.manifest.get(), mount, mountVendor, umountVendor,
231 &updated.dev.manifest,
Yifan Hong143cfe62017-04-13 20:18:01 -0700232 std::bind(GetDeviceHalManifest, true /* skipCache */))) != OK) {
233 return status;
234 }
235 updated.runtimeInfo = GetRuntimeInfo(true /* skipCache */);
236 // TODO(b/37321309) get matrices from the device as well.
237 updated.fwk.matrix = pkg.fwk.matrix.get();
238 updated.dev.matrix = pkg.dev.matrix.get();
239
240 // null checks for files and runtime info after the update
241 // TODO(b/37321309) if a compat mat is missing, it is not matched and considered compatible.
242 if (updated.fwk.manifest == nullptr) {
243 ADD_MESSAGE("No framework manifest file from device or from update package");
244 return NO_INIT;
245 }
246 if (updated.dev.manifest == nullptr) {
247 ADD_MESSAGE("No device manifest file from device or from update package");
248 return NO_INIT;
249 }
250 if (updated.fwk.matrix == nullptr) {
251 ADD_MESSAGE("No framework matrix, skipping;");
252 // TODO(b/37321309) consider missing matricies as errors.
253 }
254 if (updated.dev.matrix == nullptr) {
255 ADD_MESSAGE("No device matrix, skipping;");
256 // TODO(b/37321309) consider missing matricies as errors.
257 }
258 if (updated.runtimeInfo == nullptr) {
259 ADD_MESSAGE("No runtime info from device");
260 return NO_INIT;
261 }
262
263 // compatiblity check.
264 // TODO(b/37321309) outer if checks can be removed if we consider missing matrices as errors.
265 if (updated.dev.manifest && updated.fwk.matrix) {
266 if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) {
267 error->insert(0, "Device manifest and framework compatibility matrix "
268 "are incompatible: ");
269 return INCOMPATIBLE;
270 }
271 }
272 if (updated.fwk.manifest && updated.dev.matrix) {
273 if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) {
274 error->insert(0, "Framework manifest and device compatibility matrix "
275 "are incompatible: ");
276 return INCOMPATIBLE;
277 }
278 }
279 if (updated.runtimeInfo && updated.fwk.matrix) {
280 if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error)) {
281 error->insert(0, "Runtime info and framework compatibility matrix "
282 "are incompatible: ");
283 return INCOMPATIBLE;
284 }
285 }
286
287 return COMPATIBLE;
288}
289
290} // namespace details
291
Yifan Hongfbbf0472017-04-07 18:14:18 -0700292// static
293int32_t VintfObject::CheckCompatibility(
Yifan Hong9532bd22017-04-14 15:30:52 -0700294 const std::vector<std::string> &xmls, std::string *error) {
295 return details::checkCompatibility(xmls, false /* mount */,
296 &details::fakeMountUmount, &details::fakeMountUmount,
297 &details::fakeMountUmount, &details::fakeMountUmount,
Yifan Hong143cfe62017-04-13 20:18:01 -0700298 &VintfObject::GetFrameworkHalManifest,
299 &VintfObject::GetDeviceHalManifest,
300 &VintfObject::GetRuntimeInfo,
301 error);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700302}
303
Yifan Hong3daec812017-02-27 18:49:11 -0800304
305} // namespace vintf
306} // namespace android