blob: 674031ac599fbbe6aa41484dbeb29a65b057cc70 [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;
37static LockedUniquePtr<RuntimeInfo> gDeviceRuntimeInfo;
38
39template <typename T, typename F>
40static const T *Get(
41 LockedUniquePtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070042 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080043 const F &fetchAllInformation) {
44 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong143cfe62017-04-13 20:18:01 -070045 if (skipCache || ptr->object == nullptr) {
Yifan Hong3daec812017-02-27 18:49:11 -080046 ptr->object = std::make_unique<T>();
47 if (fetchAllInformation(ptr->object.get()) != OK) {
48 ptr->object = nullptr; // frees the old object
49 }
50 }
51 return ptr->object.get();
52}
53
54// static
Yifan Hong143cfe62017-04-13 20:18:01 -070055const HalManifest *VintfObject::GetDeviceHalManifest(bool skipCache) {
56 return Get(&gDeviceManifest, skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080057 std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
58 "/vendor/manifest.xml"));
59}
60
61// static
Yifan Hong143cfe62017-04-13 20:18:01 -070062const HalManifest *VintfObject::GetFrameworkHalManifest(bool skipCache) {
63 return Get(&gFrameworkManifest, skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080064 std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
65 "/system/manifest.xml"));
66}
67
68// static
Yifan Hong143cfe62017-04-13 20:18:01 -070069const RuntimeInfo *VintfObject::GetRuntimeInfo(bool skipCache) {
70 return Get(&gDeviceRuntimeInfo, skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080071 std::bind(&RuntimeInfo::fetchAllInformation, std::placeholders::_1));
72}
73
Yifan Hong143cfe62017-04-13 20:18:01 -070074namespace details {
75
Yifan Hong9532bd22017-04-14 15:30:52 -070076static status_t fakeMountUmount() {
Yifan Hong143cfe62017-04-13 20:18:01 -070077 return OK;
78}
79
80enum class ParseStatus {
81 OK,
82 PARSE_ERROR,
83 DUPLICATED_FWK_ENTRY,
84 DUPLICATED_DEV_ENTRY,
85};
86
Yifan Hong9532bd22017-04-14 15:30:52 -070087static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -070088 switch(status) {
89 case ParseStatus::OK: return "OK";
90 case ParseStatus::PARSE_ERROR: return "parse error";
91 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
92 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
93 }
94 return "";
95}
96
97template<typename T>
Yifan Hong9532bd22017-04-14 15:30:52 -070098static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse,
Yifan Hong143cfe62017-04-13 20:18:01 -070099 std::unique_ptr<T> *fwk, std::unique_ptr<T> *dev) {
100 std::unique_ptr<T> ret = std::make_unique<T>();
101 if (!parse(ret.get(), xml)) {
102 return ParseStatus::PARSE_ERROR;
103 }
104 if (ret->type() == SchemaType::FRAMEWORK) {
105 if (fwk->get() != nullptr) {
106 return ParseStatus::DUPLICATED_FWK_ENTRY;
107 }
108 *fwk = std::move(ret);
109 } else if (ret->type() == SchemaType::DEVICE) {
110 if (dev->get() != nullptr) {
111 return ParseStatus::DUPLICATED_DEV_ENTRY;
112 }
113 *dev = std::move(ret);
114 }
115 return ParseStatus::OK;
116}
117
118template<typename T, typename GetFunction>
119static status_t getMissing(const T *pkg, bool mount,
120 std::function<status_t(void)> mountFunction,
Yifan Hong9532bd22017-04-14 15:30:52 -0700121 std::function<status_t(void)> umountFunction,
Yifan Hong143cfe62017-04-13 20:18:01 -0700122 const T **updated,
123 GetFunction getFunction) {
124 if (pkg != nullptr) {
125 *updated = pkg;
126 } else {
127 if (mount) {
128 (void)mountFunction(); // ignore mount errors
129 }
130 *updated = getFunction();
Yifan Hong9532bd22017-04-14 15:30:52 -0700131 if (mount) {
132 (void)umountFunction(); // ignore umount errors
133 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700134 }
135 return OK;
136}
137
138#define ADD_MESSAGE(__error__) \
139 if (error != nullptr) { \
140 *error += (__error__); \
141 } \
142
143struct PackageInfo {
144 struct Pair {
145 std::unique_ptr<HalManifest> manifest;
146 std::unique_ptr<CompatibilityMatrix> matrix;
147 };
148 Pair dev;
149 Pair fwk;
150};
151
152struct UpdatedInfo {
153 struct Pair {
154 const HalManifest *manifest;
155 const CompatibilityMatrix *matrix;
156 };
157 Pair dev;
158 Pair fwk;
159 const RuntimeInfo *runtimeInfo;
160};
161
162// Parse all information from package;
163// Get missing information from the device;
164// Do compatibility check.
165int32_t checkCompatibility(const std::vector<std::string> &xmls, bool mount,
166 std::function<status_t(void)> mountSystem,
Yifan Hong9532bd22017-04-14 15:30:52 -0700167 std::function<status_t(void)> umountSystem,
Yifan Hong143cfe62017-04-13 20:18:01 -0700168 std::function<status_t(void)> mountVendor,
Yifan Hong9532bd22017-04-14 15:30:52 -0700169 std::function<status_t(void)> umountVendor,
Yifan Hong143cfe62017-04-13 20:18:01 -0700170 std::function<const HalManifest *(bool)> GetFrameworkHalManifest,
171 std::function<const HalManifest *(bool)> GetDeviceHalManifest,
172 std::function<const RuntimeInfo *(bool)> GetRuntimeInfo,
173 std::string *error) {
174
175 status_t status;
176 ParseStatus parseStatus;
177 PackageInfo pkg; // All information from package.
178 UpdatedInfo updated; // All files and runtime info after the update.
179
180 if (xmls.empty()) {
181 ADD_MESSAGE("nothing to update");
182 return BAD_VALUE;
183 }
184
185 // parse all information from package
186 for (const auto &xml : xmls) {
187 parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest);
188 if (parseStatus == ParseStatus::OK) {
189 continue; // work on next one
190 }
191 if (parseStatus != ParseStatus::PARSE_ERROR) {
192 ADD_MESSAGE(toString(parseStatus) + " manifest");
193 return ALREADY_EXISTS;
194 }
195 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix);
196 if (parseStatus == ParseStatus::OK) {
197 continue; // work on next one
198 }
199 if (parseStatus != ParseStatus::PARSE_ERROR) {
200 ADD_MESSAGE(toString(parseStatus) + " matrix");
201 return ALREADY_EXISTS;
202 }
203 ADD_MESSAGE(toString(parseStatus)); // parse error
204 return BAD_VALUE;
205 }
206
207 // get missing info from device
Yifan Hong9532bd22017-04-14 15:30:52 -0700208 if ((status = getMissing(pkg.fwk.manifest.get(), mount, mountSystem, umountSystem,
209 &updated.fwk.manifest,
Yifan Hong143cfe62017-04-13 20:18:01 -0700210 std::bind(GetFrameworkHalManifest, true /* skipCache */))) != OK) {
211 return status;
212 }
Yifan Hong9532bd22017-04-14 15:30:52 -0700213 if ((status = getMissing(pkg.dev.manifest.get(), mount, mountVendor, umountVendor,
214 &updated.dev.manifest,
Yifan Hong143cfe62017-04-13 20:18:01 -0700215 std::bind(GetDeviceHalManifest, true /* skipCache */))) != OK) {
216 return status;
217 }
218 updated.runtimeInfo = GetRuntimeInfo(true /* skipCache */);
219 // TODO(b/37321309) get matrices from the device as well.
220 updated.fwk.matrix = pkg.fwk.matrix.get();
221 updated.dev.matrix = pkg.dev.matrix.get();
222
223 // null checks for files and runtime info after the update
224 // TODO(b/37321309) if a compat mat is missing, it is not matched and considered compatible.
225 if (updated.fwk.manifest == nullptr) {
226 ADD_MESSAGE("No framework manifest file from device or from update package");
227 return NO_INIT;
228 }
229 if (updated.dev.manifest == nullptr) {
230 ADD_MESSAGE("No device manifest file from device or from update package");
231 return NO_INIT;
232 }
233 if (updated.fwk.matrix == nullptr) {
234 ADD_MESSAGE("No framework matrix, skipping;");
235 // TODO(b/37321309) consider missing matricies as errors.
236 }
237 if (updated.dev.matrix == nullptr) {
238 ADD_MESSAGE("No device matrix, skipping;");
239 // TODO(b/37321309) consider missing matricies as errors.
240 }
241 if (updated.runtimeInfo == nullptr) {
242 ADD_MESSAGE("No runtime info from device");
243 return NO_INIT;
244 }
245
246 // compatiblity check.
247 // TODO(b/37321309) outer if checks can be removed if we consider missing matrices as errors.
248 if (updated.dev.manifest && updated.fwk.matrix) {
249 if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) {
250 error->insert(0, "Device manifest and framework compatibility matrix "
251 "are incompatible: ");
252 return INCOMPATIBLE;
253 }
254 }
255 if (updated.fwk.manifest && updated.dev.matrix) {
256 if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) {
257 error->insert(0, "Framework manifest and device compatibility matrix "
258 "are incompatible: ");
259 return INCOMPATIBLE;
260 }
261 }
262 if (updated.runtimeInfo && updated.fwk.matrix) {
263 if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error)) {
264 error->insert(0, "Runtime info and framework compatibility matrix "
265 "are incompatible: ");
266 return INCOMPATIBLE;
267 }
268 }
269
270 return COMPATIBLE;
271}
272
273} // namespace details
274
Yifan Hongfbbf0472017-04-07 18:14:18 -0700275// static
276int32_t VintfObject::CheckCompatibility(
Yifan Hong9532bd22017-04-14 15:30:52 -0700277 const std::vector<std::string> &xmls, std::string *error) {
278 return details::checkCompatibility(xmls, false /* mount */,
279 &details::fakeMountUmount, &details::fakeMountUmount,
280 &details::fakeMountUmount, &details::fakeMountUmount,
Yifan Hong143cfe62017-04-13 20:18:01 -0700281 &VintfObject::GetFrameworkHalManifest,
282 &VintfObject::GetDeviceHalManifest,
283 &VintfObject::GetRuntimeInfo,
284 error);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700285}
286
Yifan Hong3daec812017-02-27 18:49:11 -0800287
288} // namespace vintf
289} // namespace android