blob: 8e5c4f934c07d76dcc9bd15b01bebd2ab4dda761 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 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
Joe Onorato76690122016-12-20 08:18:32 -080018#include <frameworks/base/core/proto/android/os/incident.pb.h>
Joe Onorato1754d742016-11-21 17:51:35 -080019
Joe Onorato1754d742016-11-21 17:51:35 -080020#include <map>
Yi Jinf8601842017-08-15 22:01:41 -070021#include <string>
Joe Onorato1754d742016-11-21 17:51:35 -080022
Yi Jinf8601842017-08-15 22:01:41 -070023using namespace android;
Joe Onorato1754d742016-11-21 17:51:35 -080024using namespace android::os;
25using namespace google::protobuf;
26using namespace google::protobuf::io;
27using namespace google::protobuf::internal;
28using namespace std;
29
Yi Jinf8601842017-08-15 22:01:41 -070030static void generateHead(const char* header) {
Joe Onorato1754d742016-11-21 17:51:35 -080031 printf("// Auto generated file. Do not modify\n");
32 printf("\n");
Yi Jinf8601842017-08-15 22:01:41 -070033 printf("#include \"%s.h\"\n", header);
Joe Onorato1754d742016-11-21 17:51:35 -080034 printf("\n");
Yi Jinf8601842017-08-15 22:01:41 -070035}
Joe Onorato1754d742016-11-21 17:51:35 -080036
Yi Jinf8601842017-08-15 22:01:41 -070037// ================================================================================
38static bool generateIncidentSectionsCpp()
39{
40 generateHead("incident_sections");
41
42 map<string,FieldDescriptor const*> sections;
43 int N;
Joe Onorato1754d742016-11-21 17:51:35 -080044 Descriptor const* descriptor = IncidentProto::descriptor();
45 N = descriptor->field_count();
46 for (int i=0; i<N; i++) {
47 const FieldDescriptor* field = descriptor->field(i);
48 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
49 sections[field->name()] = field;
50 }
51 }
52
53 printf("IncidentSection const INCIDENT_SECTIONS[] = {\n");
54 N = sections.size();
55 int i = 0;
56 for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin();
57 it != sections.end(); it++, i++) {
58 const FieldDescriptor* field = it->second;
59 printf(" { %d, \"%s\" }", field->number(), field->name().c_str());
60 if (i != N-1) {
61 printf(",\n");
62 } else {
63 printf("\n");
64 }
65 }
66 printf("};\n");
67
68 printf("const int INCIDENT_SECTION_COUNT = %d;\n", N);
69
Yi Jinf8601842017-08-15 22:01:41 -070070 return true;
71}
72
73// ================================================================================
74static void splitAndPrint(const string& args) {
75 size_t base = 0;
76 size_t found;
77 while (true) {
78 found = args.find_first_of(" ", base);
79 if (found != base) {
80 string arg = args.substr(base, found - base);
81 printf(" \"%s\",", arg.c_str());
82 }
83 if (found == args.npos) break;
84 base = found + 1;
85 }
86}
87
88static bool generateSectionListCpp() {
89 generateHead("section_list");
90
91 printf("const Section* SECTION_LIST[] = {\n");
92 Descriptor const* descriptor = IncidentProto::descriptor();
93 for (int i=0; i<descriptor->field_count(); i++) {
94 const FieldDescriptor* field = descriptor->field(i);
95
96 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
97 continue;
98 }
99 const SectionFlags s = field->options().GetExtension(section);
100 switch (s.type()) {
101 case SECTION_NONE:
102 continue;
103 case SECTION_FILE:
104 printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
105 break;
106 case SECTION_COMMAND:
107 printf(" new CommandSection(%d,", field->number());
108 splitAndPrint(s.args());
109 printf(" NULL),\n");
110 break;
111 case SECTION_DUMPSYS:
112 printf(" new DumpsysSection(%d,", field->number());
113 splitAndPrint(s.args());
114 printf(" NULL),\n");
115 break;
116 }
117 }
118 printf(" NULL\n");
119 printf("};\n");
120 return true;
121}
122
123// ================================================================================
124int main(int argc, char const *argv[])
125{
126 if (argc != 2) return 1;
127 const char* module = argv[1];
128
129 if (strcmp(module, "incident") == 0) {
130 return !generateIncidentSectionsCpp();
131 }
132 if (strcmp(module, "incidentd") == 0 ) {
133 return !generateSectionListCpp();
134 }
135
136 // return failure if not called by the whitelisted modules
137 return 1;
Joe Onorato1754d742016-11-21 17:51:35 -0800138}