1 /*
2 * Copyright (C) 2023 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 <vintf/FqInstance.h>
18
19 #include <sstream>
20
21 namespace android::vintf {
22
23 using details::FQName;
24
25 static const char INSTANCE_SEP = '/';
26
getPackage() const27 const std::string& FqInstance::getPackage() const {
28 return mFqName.package();
29 }
30
hasPackage() const31 bool FqInstance::hasPackage() const {
32 return !getPackage().empty();
33 }
34
getMajorVersion() const35 size_t FqInstance::getMajorVersion() const {
36 return hasVersion() ? mFqName.getPackageMajorVersion() : 0;
37 }
38
getMinorVersion() const39 size_t FqInstance::getMinorVersion() const {
40 return hasVersion() ? mFqName.getPackageMinorVersion() : 0;
41 }
42
getVersion() const43 std::pair<size_t, size_t> FqInstance::getVersion() const {
44 return mFqName.getVersion();
45 }
46
hasVersion() const47 bool FqInstance::hasVersion() const {
48 return mFqName.hasVersion();
49 }
50
getInterface() const51 std::string FqInstance::getInterface() const {
52 return hasInterface() ? mFqName.getInterfaceName() : "";
53 }
54
hasInterface() const55 bool FqInstance::hasInterface() const {
56 return mFqName.isInterfaceName();
57 }
58
getInstance() const59 const std::string& FqInstance::getInstance() const {
60 return mInstance;
61 }
62
hasInstance() const63 bool FqInstance::hasInstance() const {
64 return !mInstance.empty();
65 }
66
getFqNameString() const67 std::string FqInstance::getFqNameString() const {
68 return mFqName.string();
69 }
70
isValid() const71 bool FqInstance::isValid() const {
72 bool hasPkg = hasPackage();
73 bool hasVer = hasVersion();
74 bool hasIntf = hasInterface();
75 bool hasInst = hasInstance();
76
77 // android.hardware.foo@1.0::IFoo/default
78 // android.hardware.foo@1.0/default
79 if (hasPkg && hasVer && hasInst) {
80 return true;
81 }
82
83 // @1.0::IFoo/default
84 // @1.0/default
85 if (!hasPkg && hasVer && hasInst) {
86 return true;
87 }
88
89 // IFoo/default
90 if (!hasPkg && !hasVer && hasIntf && hasInst) {
91 return true;
92 }
93
94 return false;
95 }
96
setTo(const std::string & s)97 bool FqInstance::setTo(const std::string& s) {
98 auto pos = s.find(INSTANCE_SEP);
99 if (!mFqName.setTo(s.substr(0, pos))) return false;
100 mInstance = pos == std::string::npos ? std::string{} : s.substr(pos + 1);
101
102 return isValid();
103 }
104
setTo(const std::string & package,size_t majorVer,size_t minorVer,const std::string & interface,const std::string & instance)105 bool FqInstance::setTo(const std::string& package, size_t majorVer, size_t minorVer,
106 const std::string& interface, const std::string& instance) {
107 if (!mFqName.setTo(package, majorVer, minorVer, interface)) return false;
108 mInstance = instance;
109 return isValid();
110 }
111
setTo(size_t majorVer,size_t minorVer,const std::string & interface,const std::string & instance)112 bool FqInstance::setTo(size_t majorVer, size_t minorVer, const std::string& interface,
113 const std::string& instance) {
114 return setTo("", majorVer, minorVer, interface, instance);
115 }
116
setTo(const std::string & interface,const std::string & instance)117 bool FqInstance::setTo(const std::string& interface, const std::string& instance) {
118 return setTo(0u, 0u, interface, instance);
119 }
120
string() const121 std::string FqInstance::string() const {
122 std::string ret = mFqName.string();
123 if (hasInstance()) ret += INSTANCE_SEP + mInstance;
124 return ret;
125 }
126
operator <(const FqInstance & other) const127 bool FqInstance::operator<(const FqInstance& other) const {
128 return string() < other.string();
129 }
130
operator ==(const FqInstance & other) const131 bool FqInstance::operator==(const FqInstance& other) const {
132 return string() == other.string();
133 }
134
operator !=(const FqInstance & other) const135 bool FqInstance::operator!=(const FqInstance& other) const {
136 return !(*this == other);
137 }
138
inPackage(const std::string & package) const139 bool FqInstance::inPackage(const std::string& package) const {
140 return mFqName.inPackage(package);
141 }
142
143 } // namespace android::vintf
144