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 #pragma once 18 19 #include <string> 20 #include <vector> 21 22 namespace android::vintf::details { 23 24 struct FQName { 25 __attribute__((warn_unused_result)) static bool parse(const std::string& s, FQName* into); 26 27 explicit FQName(); 28 29 FQName(const std::string& package, const std::string& version, const std::string& name = ""); 30 31 // Returns false if string isn't a valid FQName object. 32 __attribute__((warn_unused_result)) bool setTo(const std::string& s); 33 __attribute__((warn_unused_result)) bool setTo(const std::string& package, size_t majorVer, 34 size_t minorVer, const std::string& name = ""); 35 36 const std::string& package() const; 37 // Return version in the form "1.0" for HIDL interfaces with major.minor versions, 38 // "1" for AIDL versions that have minor version set with `kFakeAidlMajorVersion` as 39 // the major version, and an empty string if there is no version set. 40 std::string version() const; 41 // Same as version, but keeps the kFakeAidlMajorVersion in the string. 42 // This is still required for all of the parsing/assembling but not desired 43 // for logging and errors. 44 std::string parsedVersion() const; 45 // Return true only if version is present. 46 bool hasVersion() const; 47 // Return pair of (major, minor) version. Defaults to 0, 0. 48 std::pair<size_t, size_t> getVersion() const; 49 50 // The next method return the name part of the FQName, that is, the 51 // part after the version field. For example: 52 // 53 // package android.hardware.tests.foo@1.0; 54 // interface IFoo { 55 // struct bar { 56 // struct baz { 57 // ... 58 // }; 59 // }; 60 // }; 61 // 62 // package android.hardware.tests.bar@1.0; 63 // import android.hardware.tests.foo@1.0; 64 // interface { 65 // struct boo { 66 // IFoo.bar.baz base; 67 // }; 68 // } 69 // 70 // The FQName for base is android.hardware.tests.foo@1.0::IFoo.bar.baz; so 71 // FQName::name() will return "IFoo.bar.baz". 72 const std::string& name() const; 73 74 // Interface names start with 'I' 75 bool isInterfaceName() const; 76 77 std::string string() const; 78 // Same as string, but keeps the kFakeAidlMajorVersion in the string. 79 // This is still required for all of the parsing/assembling but not desired 80 // for logging and errors. 81 std::string parsedString() const; 82 83 bool operator<(const FQName& other) const; 84 bool operator==(const FQName& other) const; 85 bool operator!=(const FQName& other) const; 86 87 // Must be called on an interface 88 // android.hardware.foo@1.0::IBar 89 // -> IBar 90 const std::string& getInterfaceName() const; 91 92 // android.hardware.foo@1.0::Abc.Type:VALUE 93 // -> android.hardware.foo@1.0 94 FQName getPackageAndVersion() const; 95 96 // If this is android.hardware@1.0::IFoo 97 // package = "and" -> false 98 // package = "android" -> true 99 // package = "android.hardware@1.0" -> false 100 bool inPackage(const std::string& package) const; 101 102 // return major and minor version if they exist, else abort program. 103 // Existence of version can be checked via hasVersion(). 104 size_t getPackageMajorVersion() const; 105 size_t getPackageMinorVersion() const; 106 107 private: 108 bool mIsIdentifier; 109 std::string mPackage; 110 // mMajor == 0 means empty. 111 size_t mMajor = 0; 112 size_t mMinor = 0; 113 std::string mName; 114 115 void clear(); 116 117 __attribute__((warn_unused_result)) bool setVersion(const std::string& v); 118 __attribute__((warn_unused_result)) bool parseVersion(const std::string& majorStr, 119 const std::string& minorStr); 120 __attribute__((warn_unused_result)) static bool parseVersion(const std::string& majorStr, 121 const std::string& minorStr, 122 size_t* majorVer, 123 size_t* minorVer); 124 __attribute__((warn_unused_result)) static bool parseVersion(const std::string& v, 125 size_t* majorVer, 126 size_t* minorVer); 127 static void clearVersion(size_t* majorVer, size_t* minorVer); 128 129 void clearVersion(); 130 131 bool isIdentifier() const; 132 // Return version in the form "@1.0" if it is present, otherwise empty string. 133 std::string atVersion() const; 134 // Same as atVersion, but keeps the kFakeAidlMajorVersion in the string. 135 // This is still required for all of the parsing/assembling but not desired 136 // for logging and errors. 137 std::string parsedAtVersion() const; 138 139 std::vector<std::string> getPackageComponents() const; 140 }; 141 142 } // namespace android::vintf::details 143