1/*
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 "CompatibilityMatrix.h"
18
19#include "parse_string.h"
20#include "utils.h"
21
22namespace android {
23namespace vintf {
24
25constexpr Version CompatibilityMatrix::kVersion;
26
27bool CompatibilityMatrix::add(MatrixHal &&hal) {
28    return HalGroup<MatrixHal>::add(std::move(hal));
29}
30
31bool CompatibilityMatrix::add(MatrixKernel &&kernel) {
32    if (mType != SchemaType::FRAMEWORK) {
33        return false;
34    }
35    framework.mKernels.push_back(std::move(kernel));
36    return true;
37}
38
39SchemaType CompatibilityMatrix::type() const {
40    return mType;
41}
42
43
44status_t CompatibilityMatrix::fetchAllInformation(const std::string &path) {
45    return details::fetchAllInformation(path, gCompatibilityMatrixConverter, this);
46}
47
48std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName,
49                                                  const Version& version) const {
50    using std::literals::string_literals::operator""s;
51    auto range = getXmlFiles(xmlFileName);
52    for (auto it = range.first; it != range.second; ++it) {
53        const MatrixXmlFile& matrixXmlFile = it->second;
54        if (matrixXmlFile.versionRange().contains(version)) {
55            if (!matrixXmlFile.overriddenPath().empty()) {
56                return matrixXmlFile.overriddenPath();
57            }
58            return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" +
59                   xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) +
60                   "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." +
61                   to_string(matrixXmlFile.format());
62        }
63    }
64    return "";
65}
66
67bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) {
68    return lft.mType == rgt.mType && lft.mHals == rgt.mHals && lft.mXmlFiles == rgt.mXmlFiles &&
69           (lft.mType != SchemaType::DEVICE || (lft.device.mVndk == rgt.device.mVndk)) &&
70           (lft.mType != SchemaType::FRAMEWORK ||
71            (lft.framework.mKernels == rgt.framework.mKernels &&
72             lft.framework.mSepolicy == rgt.framework.mSepolicy &&
73             lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion));
74}
75
76} // namespace vintf
77} // namespace android
78