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 #ifndef ANDROID_VINTF_PARSE_STRING_H
18 #define ANDROID_VINTF_PARSE_STRING_H
19 
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 
24 #include "CompatibilityMatrix.h"
25 #include "RuntimeInfo.h"
26 #include "HalManifest.h"
27 
28 namespace android {
29 namespace vintf {
30 
31 std::ostream &operator<<(std::ostream &os, HalFormat hf);
32 std::ostream &operator<<(std::ostream &os, Transport tr);
33 std::ostream &operator<<(std::ostream &os, Arch ar);
34 std::ostream &operator<<(std::ostream &os, KernelConfigType il);
35 std::ostream &operator<<(std::ostream &os, Tristate tr);
36 std::ostream &operator<<(std::ostream &os, SchemaType ksv);
37 std::ostream& operator<<(std::ostream& os, XmlSchemaFormat f);
38 std::ostream& operator<<(std::ostream& os, Level l);
39 std::ostream& operator<<(std::ostream& os, KernelSepolicyVersion v);
40 std::ostream &operator<<(std::ostream &os, const Version &ver);
41 std::ostream &operator<<(std::ostream &os, const VersionRange &vr);
42 
43 #pragma clang diagnostic push
44 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
45 std::ostream &operator<<(std::ostream &os, const VndkVersionRange &vr);
46 #pragma clang diagnostic pop
47 
48 std::ostream &operator<<(std::ostream &os, const KernelVersion &ver);
49 std::ostream &operator<<(std::ostream &os, const TransportArch &ta);
50 std::ostream &operator<<(std::ostream &os, const ManifestHal &hal);
51 std::ostream &operator<<(std::ostream &os, const KernelConfigTypedValue &kcv);
52 std::ostream& operator<<(std::ostream& os, const FqInstance& fqInstance);
53 
54 template <typename T>
to_string(const T & obj)55 std::string to_string(const T &obj) {
56     std::ostringstream oss;
57     oss << obj;
58     return oss.str();
59 }
60 
61 bool parse(const std::string &s, HalFormat *hf);
62 bool parse(const std::string &s, Transport *tr);
63 bool parse(const std::string &s, Arch *ar);
64 bool parse(const std::string &s, KernelConfigType *il);
65 bool parse(const std::string &s, KernelConfigKey *key);
66 bool parse(const std::string &s, Tristate *tr);
67 bool parse(const std::string &s, SchemaType *ver);
68 bool parse(const std::string& s, XmlSchemaFormat* ver);
69 bool parse(const std::string& s, Level* l);
70 bool parse(const std::string &s, KernelSepolicyVersion *ksv);
71 bool parse(const std::string &s, Version *ver);
72 bool parse(const std::string &s, VersionRange *vr);
73 
74 #pragma clang diagnostic push
75 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
76 bool parse(const std::string &s, VndkVersionRange *vr);
77 #pragma clang diagnostic pop
78 
79 bool parse(const std::string &s, KernelVersion *ver);
80 // if return true, ta->isValid() must be true.
81 bool parse(const std::string &s, TransportArch *ta);
82 // if return true, hal->isValid() must be true.
83 bool parse(const std::string &s, ManifestHal *hal);
84 bool parse(const std::string& s, FqInstance* fqInstance);
85 
86 bool parseKernelConfigInt(const std::string &s, int64_t *i);
87 bool parseKernelConfigInt(const std::string &s, uint64_t *i);
88 bool parseRange(const std::string &s, KernelConfigRangeValue *range);
89 
90 // Parse the KernelConfigValue in s, assuming type kctv->type, and store it in
91 // kctv->value.
92 bool parseKernelConfigValue(const std::string &s, KernelConfigTypedValue *kctv);
93 
94 // Parse the KernelConfigTypedValue in s (type is guessed) and store it in kctv.
95 // Do not expect quotes in strings.
96 bool parseKernelConfigTypedValue(const std::string& s, KernelConfigTypedValue* kctv);
97 
98 // "100" <=> Version{kFakeAidlMajorVersion, 100}
99 std::string aidlVersionToString(const Version& v);
100 bool parseAidlVersion(const std::string& s, Version* version);
101 
102 // "100" <=> VersionRange{kFakeAidlMajorVersion, 100}
103 // "100-105" <=> VersionRange{kFakeAidlMajorVersion, 100, 105}
104 std::string aidlVersionRangeToString(const VersionRange& vr);
105 bool parseAidlVersionRange(const std::string& s, VersionRange* vr);
106 
107 // A string that describes the whole object, with versions of all
108 // its components. For debugging and testing purposes only. This is not
109 // the XML string.
110 std::string dump(const HalManifest &vm);
111 
112 std::string dump(const RuntimeInfo& ki, bool verbose = true);
113 
114 std::vector<std::string> expandInstances(const MatrixHal& req);
115 
116 std::string toFQNameString(const std::string& package, const Version& version,
117                            const std::string& intf = "", const std::string& instance = "");
118 
119 std::string toFQNameString(const Version& version, const std::string& intf,
120                            const std::string& instance);
121 
122 // android.hardware.foo@1.0-1::IFoo/default.
123 // Note that the format is extended to support a range of versions.
124 std::string toFQNameString(const std::string& package, const VersionRange& range,
125                            const std::string& interface, const std::string& instance);
126 
127 std::string toFQNameString(const VersionRange& range, const std::string& interface,
128                            const std::string& instance);
129 
130 std::string toFQNameString(const std::string& interface, const std::string& instance);
131 
132 std::string toAidlFqnameString(const std::string& package, const std::string& interface,
133                                const std::string& instance = "");
134 
135 } // namespace vintf
136 } // namespace android
137 
138 #endif // ANDROID_VINTF_PARSE_STRING_H
139