1 /*
2  * Copyright (C) 2019 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 <fstream>
18 #include <iostream>
19 
20 #include <json/json.h>
21 
main(int argc,char ** argv)22 int main(int argc, char** argv) {
23     if (argc != 2) {
24         std::cerr << "Usage: hidl_metadata_parser *.json" << std::endl;
25         return EXIT_FAILURE;
26     }
27     const std::string path = argv[1];
28 
29     Json::Value root;
30     Json::CharReaderBuilder builder;
31 
32     std::ifstream stream(path);
33     std::string errorMessage;
34     if (!Json::parseFromStream(builder, stream, &root, &errorMessage)) {
35         std::cerr << "Failed to read interface inheritance hierarchy file: " << path << std::endl
36                   << errorMessage << std::endl;
37         return EXIT_FAILURE;
38     }
39 
40     std::cout << "#include <hidl/metadata.h>" << std::endl;
41     std::cout << "namespace android {" << std::endl;
42     std::cout << "std::vector<HidlInterfaceMetadata> HidlInterfaceMetadata::all() {" << std::endl;
43     std::cout << "return std::vector<HidlInterfaceMetadata>{" << std::endl;
44     for (const Json::Value& entry : root) {
45         std::cout << "HidlInterfaceMetadata{" << std::endl;
46         // HIDL interface characters guaranteed to be accepted in C++ string
47         std::cout << "std::string(\"" << entry["interface"].asString() << "\")," << std::endl;
48         std::cout << "std::vector<std::string>{" << std::endl;
49         for (const Json::Value& intf : entry["inheritedInterfaces"]) {
50             std::cout << "std::string(\"" << intf.asString() << "\")," << std::endl;
51         }
52         std::cout << "}," << std::endl;
53         std::cout << "}," << std::endl;
54     }
55     std::cout << "};" << std::endl;
56     std::cout << "}" << std::endl;
57     std::cout << "}  // namespace android" << std::endl;
58     return EXIT_SUCCESS;
59 }
60