• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <optional>
18 #include <set>
19 
20 #include <android-base/logging.h>
21 #include <gflags/gflags.h>
22 #include <vintf/FileSystem.h>
23 #include <vintf/parse_string.h>
24 #include <vintf/parse_xml.h>
25 
26 namespace android {
27 namespace vintf {
28 
29 namespace {
30 
31 template <typename T>
readObject(const std::string & path)32 std::optional<T> readObject(const std::string& path) {
33     std::string xml;
34     std::string error;
35     status_t err = details::FileSystemImpl().fetch(path, &xml, &error);
36     if (err != OK) {
37         LOG(ERROR) << "Cannot read '" << path << "': " << error;
38         return std::nullopt;
39     }
40     auto ret = std::make_optional<T>();
41     if (!fromXml(&ret.value(), xml, &error)) {
42         LOG(ERROR) << "Cannot parse '" << path << "': " << error;
43         return std::nullopt;
44     }
45     return ret;
46 }
47 
48 template <typename F>
getDescription(const CompatibilityMatrix & mat,F descriptionFn)49 std::set<std::string> getDescription(const CompatibilityMatrix& mat, F descriptionFn) {
50     std::set<std::string> set;
51     mat.forEachInstance([&set, descriptionFn](const auto& matrixInstance) {
52         for (auto minorVer = matrixInstance.versionRange().minMinor;
53              minorVer >= matrixInstance.versionRange().minMinor &&
54              minorVer <= matrixInstance.versionRange().maxMinor;
55              ++minorVer) {
56             Version version{matrixInstance.versionRange().majorVer, minorVer};
57             std::string s = std::invoke(descriptionFn, matrixInstance, version);
58             set.insert(s);
59         }
60         return true;  // continue
61     });
62     return set;
63 }
64 
65 }  // namespace
66 
GetDescription(Level level)67 std::string GetDescription(Level level) {
68     switch (level) {
69         case Level::LEGACY:
70             return "Level legacy";
71         case Level::O:
72             return "Android 8.0 (O)";
73         case Level::O_MR1:
74             return "Android 8.1 (O-MR1)";
75         case Level::P:
76             return "Android 9 (P)";
77         case Level::Q:
78             return "Android 10 (Q)";
79         case Level::R:
80             return "Android 11 (R)";
81         case Level::S:
82             return "Android 12 (S)";
83         case Level::T:
84             return "Android 13 (T)";
85         case Level::U:
86             return "Android 14 (U)";
87         case Level::V:
88             return "Android 15 (V)";
89         case Level::B:
90             return "Android 16 (B)";
91         case Level::C:
92             return "Android 17 (C)";
93         case Level::UNSPECIFIED:
94             return "Level unspecified";
95         default:
96             return "Level " + to_string(level);
97     }
98 }
99 
100 }  // namespace vintf
101 }  // namespace android
102 
103 DEFINE_string(input, "", "Input compatibility matrix file");
ValidateInput(const char *,const std::string & value)104 static bool ValidateInput(const char* /* flagname */, const std::string& value) {
105     return !value.empty();
106 }
107 DEFINE_validator(input, &ValidateInput);
108 
109 DEFINE_bool(level, false, "Write level (FCM version) of the compatibility matrix.");
110 DEFINE_bool(level_name, false, "Write level name (FCM version) of the compatibility matrix.");
111 DEFINE_bool(interfaces, false, "Write strings like \"android.hardware.foo@1.0::IFoo\".");
112 DEFINE_bool(instances, false, "Write strings like \"android.hardware.foo@1.0::IFoo/default\".");
113 
main(int argc,char ** argv)114 int main(int argc, char** argv) {
115     using namespace android::vintf;
116 
117     gflags::ParseCommandLineFlags(&argc, &argv, true /* remove flags */);
118 
119     auto mat = readObject<CompatibilityMatrix>(FLAGS_input);
120     if (!mat) {
121         return 1;
122     }
123 
124     bool written = false;
125 
126     if (FLAGS_level) {
127         if (mat->level() == Level::UNSPECIFIED) {
128             LOG(WARNING) << "FCM version is unspecified.";
129         }
130         std::cout << mat->level() << std::endl;
131 
132         written = true;
133     }
134 
135     if (FLAGS_level_name) {
136         if (mat->level() == Level::UNSPECIFIED) {
137             LOG(WARNING) << "FCM version is unspecified.";
138         }
139         std::cout << GetDescription(mat->level()) << std::endl;
140 
141         written = true;
142     }
143 
144     if (FLAGS_interfaces) {
145         auto interfaces = getDescription(*mat, &MatrixInstance::interfaceDescription);
146         if (interfaces.empty()) {
147             LOG(WARNING) << "No interfaces are found.";
148         }
149 
150         for (const auto& interface : interfaces) {
151             std::cout << interface << std::endl;
152         }
153 
154         written = true;
155     }
156 
157     if (FLAGS_instances) {
158         auto instances = getDescription(*mat, &MatrixInstance::description);
159         if (instances.empty()) {
160             LOG(WARNING) << "No instances are found.";
161         }
162 
163         for (const auto& instance : instances) {
164             std::cout << instance << std::endl;
165         }
166 
167         written = true;
168     }
169 
170     if (!written) {
171         LOG(ERROR) << "No output format is set.";
172         return 1;
173     }
174 
175     return 0;
176 }
177