blob: 0215f0fe5e32a654f50449be8af5c1e5ee23a409 [file] [log] [blame]
Yifan Hong2a90ffe2018-03-05 17:45:34 -08001/*
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#include "MatrixInstance.h"
18
19#include <utility>
20
Yifan Hong643a9ef2018-03-21 14:13:55 -070021#include "Regex.h"
Yifan Hong25e34dc2019-09-11 12:50:24 -070022#include "parse_string.h"
Yifan Hong643a9ef2018-03-21 14:13:55 -070023
Yifan Hong2a90ffe2018-03-05 17:45:34 -080024namespace android {
25namespace vintf {
26
Yifan Hongcf231a92018-03-13 14:19:13 -070027MatrixInstance::MatrixInstance() = default;
28
29MatrixInstance::MatrixInstance(const MatrixInstance&) = default;
30
Chih-Hung Hsiehd3457062018-09-25 13:40:34 -070031MatrixInstance::MatrixInstance(MatrixInstance&&) noexcept = default;
Yifan Hongcf231a92018-03-13 14:19:13 -070032
33MatrixInstance& MatrixInstance::operator=(const MatrixInstance&) = default;
34
Chih-Hung Hsiehd3457062018-09-25 13:40:34 -070035MatrixInstance& MatrixInstance::operator=(MatrixInstance&&) noexcept = default;
Yifan Hongcf231a92018-03-13 14:19:13 -070036
Yifan Hong219000b2019-09-10 19:23:19 -070037MatrixInstance::MatrixInstance(HalFormat format, FqInstance&& fqInstance, VersionRange&& range,
38 bool optional, bool isRegex)
39 : mFormat(format),
40 mFqInstance(std::move(fqInstance)),
Yifan Hong85103c62018-03-20 15:43:34 -070041 mRange(std::move(range)),
42 mOptional(optional),
43 mIsRegex(isRegex) {}
Yifan Hong2a90ffe2018-03-05 17:45:34 -080044
Yifan Hong219000b2019-09-10 19:23:19 -070045MatrixInstance::MatrixInstance(HalFormat format, const FqInstance fqInstance,
46 const VersionRange& range, bool optional, bool isRegex)
47 : mFormat(format),
48 mFqInstance(fqInstance),
49 mRange(range),
50 mOptional(optional),
51 mIsRegex(isRegex) {}
Yifan Hong2a90ffe2018-03-05 17:45:34 -080052
53const std::string& MatrixInstance::package() const {
54 return mFqInstance.getPackage();
55}
56
57const VersionRange& MatrixInstance::versionRange() const {
58 return mRange;
59}
60
61const std::string& MatrixInstance::interface() const {
62 return mFqInstance.getInterface();
63}
64
Yifan Hong219000b2019-09-10 19:23:19 -070065HalFormat MatrixInstance::format() const {
66 return mFormat;
67}
68
Yifan Hong2a90ffe2018-03-05 17:45:34 -080069bool MatrixInstance::optional() const {
70 return mOptional;
71}
72
Yifan Hong075b3632018-03-14 16:03:11 -070073bool MatrixInstance::isSatisfiedBy(const FqInstance& provided) const {
74 return package() == provided.getPackage() &&
75 versionRange().supportedBy(provided.getVersion()) &&
Yifan Hong85103c62018-03-20 15:43:34 -070076 interface() == provided.getInterface() && matchInstance(provided.getInstance());
77}
78
79bool MatrixInstance::matchInstance(const std::string& e) const {
80 if (!isRegex()) {
81 return exactInstance() == e;
82 }
Yifan Hong643a9ef2018-03-21 14:13:55 -070083 details::Regex regex;
84 if (!regex.compile(regexPattern())) {
85 return false;
86 }
87 return regex.matches(e);
Yifan Hong85103c62018-03-20 15:43:34 -070088}
89
90const std::string& MatrixInstance::regexPattern() const {
91 static const std::string kEmptyString;
Yifan Hong643a9ef2018-03-21 14:13:55 -070092 return isRegex() ? mFqInstance.getInstance() : kEmptyString;
Yifan Hong85103c62018-03-20 15:43:34 -070093}
94
95const std::string& MatrixInstance::exactInstance() const {
Yifan Hong643a9ef2018-03-21 14:13:55 -070096 static const std::string kEmptyString;
97 return isRegex() ? kEmptyString : mFqInstance.getInstance();
Yifan Hong85103c62018-03-20 15:43:34 -070098}
99
100bool MatrixInstance::isRegex() const {
101 return mIsRegex;
Yifan Hong075b3632018-03-14 16:03:11 -0700102}
103
Yifan Hong25e34dc2019-09-11 12:50:24 -0700104std::string MatrixInstance::interfaceDescription(Version replaceVersion) const {
105 switch (format()) {
106 case HalFormat::HIDL:
107 [[fallthrough]];
108 case HalFormat::NATIVE: {
109 return toFQNameString(package(), replaceVersion, interface());
110 } break;
111 case HalFormat::AIDL: {
Yifan Hongdf629f52020-12-28 16:55:15 -0800112 return toAidlFqnameString(package(), interface()) + " (@" +
113 aidlVersionToString(replaceVersion) + ")";
Yifan Hong25e34dc2019-09-11 12:50:24 -0700114 } break;
115 }
116}
117
118std::string MatrixInstance::description(Version replaceVersion) const {
119 std::string instanceDescription = isRegex() ? regexPattern() : exactInstance();
120 switch (format()) {
121 case HalFormat::HIDL:
122 [[fallthrough]];
123 case HalFormat::NATIVE: {
124 return toFQNameString(package(), replaceVersion, interface(), instanceDescription);
125 } break;
126 case HalFormat::AIDL: {
Yifan Hongdf629f52020-12-28 16:55:15 -0800127 return toAidlFqnameString(package(), interface(), instanceDescription) + " (@" +
128 aidlVersionToString(replaceVersion) + ")";
Yifan Hong25e34dc2019-09-11 12:50:24 -0700129 } break;
130 }
131}
132
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800133} // namespace vintf
134} // namespace android