blob: 900690cf36d60c806a1580136594f02265393e94 [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 Jin0ed9b682017-08-18 14:51:20 -070030static inline void emptyline() {
Joe Onorato1754d742016-11-21 17:51:35 -080031 printf("\n");
Yi Jinf8601842017-08-15 22:01:41 -070032}
Joe Onorato1754d742016-11-21 17:51:35 -080033
Yi Jin0ed9b682017-08-18 14:51:20 -070034static void generateHead(const char* header) {
35 printf("// Auto generated file. Do not modify\n");
36 emptyline();
37 printf("#include \"%s.h\"\n", header);
38 emptyline();
39}
40
Yi Jinf8601842017-08-15 22:01:41 -070041// ================================================================================
Yi Jin0ed9b682017-08-18 14:51:20 -070042static bool generateIncidentSectionsCpp(Descriptor const* descriptor)
Yi Jinf8601842017-08-15 22:01:41 -070043{
44 generateHead("incident_sections");
45
46 map<string,FieldDescriptor const*> sections;
47 int N;
Joe Onorato1754d742016-11-21 17:51:35 -080048 N = descriptor->field_count();
49 for (int i=0; i<N; i++) {
50 const FieldDescriptor* field = descriptor->field(i);
51 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
52 sections[field->name()] = field;
53 }
54 }
55
56 printf("IncidentSection const INCIDENT_SECTIONS[] = {\n");
57 N = sections.size();
58 int i = 0;
59 for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin();
60 it != sections.end(); it++, i++) {
61 const FieldDescriptor* field = it->second;
62 printf(" { %d, \"%s\" }", field->number(), field->name().c_str());
63 if (i != N-1) {
64 printf(",\n");
65 } else {
66 printf("\n");
67 }
68 }
69 printf("};\n");
70
71 printf("const int INCIDENT_SECTION_COUNT = %d;\n", N);
72
Yi Jinf8601842017-08-15 22:01:41 -070073 return true;
74}
75
76// ================================================================================
77static void splitAndPrint(const string& args) {
78 size_t base = 0;
79 size_t found;
80 while (true) {
81 found = args.find_first_of(" ", base);
82 if (found != base) {
83 string arg = args.substr(base, found - base);
84 printf(" \"%s\",", arg.c_str());
85 }
86 if (found == args.npos) break;
87 base = found + 1;
88 }
89}
90
Yunlian Jiang3809bbf2017-09-05 15:50:58 -070091static const std::string replaceAll(const string& field_name, const char oldC, const string& newS) {
Yi Jin0ed9b682017-08-18 14:51:20 -070092 if (field_name.find_first_of(oldC) == field_name.npos) return field_name.c_str();
93 size_t pos = 0, idx = 0;
94 char* res = new char[field_name.size() * newS.size() + 1]; // assign a larger buffer
95 while (pos != field_name.size()) {
96 char cur = field_name[pos++];
97 if (cur != oldC) {
98 res[idx++] = cur;
99 continue;
100 }
101
102 for (size_t i=0; i<newS.size(); i++) {
103 res[idx++] = newS[i];
104 }
105 }
106 res[idx] = '\0';
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700107 std::string result(res);
108 delete [] res;
109 return result;
Yi Jin0ed9b682017-08-18 14:51:20 -0700110}
111
112static inline bool isDefaultDest(const FieldDescriptor* field) {
113 return field->options().GetExtension(privacy).dest() == PrivacyFlags::default_instance().dest();
114}
115
116// Returns true if the descriptor doesn't have any non default privacy flags set, including its submessages
117static bool generatePrivacyFlags(const Descriptor* descriptor, const char* alias, map<string, bool> &msgNames) {
118 bool hasDefaultFlags[descriptor->field_count()];
119 // iterate though its field and generate sub flags first
120 for (int i=0; i<descriptor->field_count(); i++) {
121 hasDefaultFlags[i] = true; // set default to true
122 const FieldDescriptor* field = descriptor->field(i);
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700123 const std::string field_name_str = replaceAll(field->full_name(), '.', "__");
124 const char* field_name = field_name_str.c_str();
Yi Jin0ed9b682017-08-18 14:51:20 -0700125 // check if the same name is already defined
126 if (msgNames.find(field_name) != msgNames.end()) {
127 hasDefaultFlags[i] = msgNames[field_name];
128 continue;
129 };
130
131 PrivacyFlags p = field->options().GetExtension(privacy);
132
133 switch (field->type()) {
134 case FieldDescriptor::TYPE_MESSAGE:
135 if (generatePrivacyFlags(field->message_type(), field_name, msgNames) &&
136 isDefaultDest(field)) break;
137
Yi Jin7e0b4e52017-09-12 20:00:25 -0700138 printf("Privacy %s { %d, %d, %s_LIST, %d, NULL };\n", field_name, field->number(), field->type(), field_name, p.dest());
Yi Jin0ed9b682017-08-18 14:51:20 -0700139 hasDefaultFlags[i] = false;
140 break;
141 case FieldDescriptor::TYPE_STRING:
142 if (isDefaultDest(field) && p.patterns_size() == 0) break;
143
Yi Jin99c248f2017-08-25 18:11:58 -0700144 printf("const char* %s_patterns[] = {\n", field_name);
Yi Jin0ed9b682017-08-18 14:51:20 -0700145 for (int i=0; i<p.patterns_size(); i++) {
146 // the generated string need to escape backslash as well, need to dup it here
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700147 printf(" \"%s\",\n", replaceAll(p.patterns(i), '\\', "\\\\").c_str());
Yi Jin0ed9b682017-08-18 14:51:20 -0700148 }
149 printf(" NULL };\n");
Yi Jin7e0b4e52017-09-12 20:00:25 -0700150 printf("Privacy %s { %d, %d, NULL, %d, %s_patterns };\n", field_name, field->number(), field->type(), p.dest(), field_name);
Yi Jin0ed9b682017-08-18 14:51:20 -0700151 hasDefaultFlags[i] = false;
152 break;
153 default:
154 if (isDefaultDest(field)) break;
Yi Jin7e0b4e52017-09-12 20:00:25 -0700155 printf("Privacy %s { %d, %d, NULL, %d, NULL };\n", field_name, field->number(), field->type(), p.dest());
Yi Jin0ed9b682017-08-18 14:51:20 -0700156 hasDefaultFlags[i] = false;
157 }
158 // add the field name to message map, true means it has default flags
159 msgNames[field_name] = hasDefaultFlags[i];
160 }
161
162 bool allDefaults = true;
163 for (int i=0; i<descriptor->field_count(); i++) {
164 allDefaults &= hasDefaultFlags[i];
165 }
166 if (allDefaults) return true;
167
168 emptyline();
Yi Jin7e0b4e52017-09-12 20:00:25 -0700169
170 bool needConst = strcmp(alias, "PRIVACY_POLICY") == 0;
171 int policyCount = 0;
172
173 printf("%s Privacy* %s_LIST[] = {\n", needConst ? "const" : "", alias);
Yi Jin0ed9b682017-08-18 14:51:20 -0700174 for (int i=0; i<descriptor->field_count(); i++) {
175 const FieldDescriptor* field = descriptor->field(i);
176 if (hasDefaultFlags[i]) continue;
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700177 printf(" &%s,\n", replaceAll(field->full_name(), '.', "__").c_str());
Yi Jin7e0b4e52017-09-12 20:00:25 -0700178 policyCount++;
Yi Jin0ed9b682017-08-18 14:51:20 -0700179 }
Yi Jin7e0b4e52017-09-12 20:00:25 -0700180 if (needConst) {
181 printf("};\n\n");
182 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
183 } else {
184 printf(" NULL };\n");
185 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700186 emptyline();
187 return false;
188}
189
190static bool generateSectionListCpp(Descriptor const* descriptor) {
Yi Jinf8601842017-08-15 22:01:41 -0700191 generateHead("section_list");
192
Yi Jin0ed9b682017-08-18 14:51:20 -0700193 // generates SECTION_LIST
Yi Jinf8601842017-08-15 22:01:41 -0700194 printf("const Section* SECTION_LIST[] = {\n");
Yi Jinf8601842017-08-15 22:01:41 -0700195 for (int i=0; i<descriptor->field_count(); i++) {
196 const FieldDescriptor* field = descriptor->field(i);
197
198 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
199 continue;
200 }
201 const SectionFlags s = field->options().GetExtension(section);
202 switch (s.type()) {
203 case SECTION_NONE:
204 continue;
205 case SECTION_FILE:
206 printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
207 break;
208 case SECTION_COMMAND:
209 printf(" new CommandSection(%d,", field->number());
210 splitAndPrint(s.args());
211 printf(" NULL),\n");
212 break;
213 case SECTION_DUMPSYS:
214 printf(" new DumpsysSection(%d,", field->number());
215 splitAndPrint(s.args());
216 printf(" NULL),\n");
217 break;
218 }
219 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700220 printf(" NULL };\n");
221 emptyline();
222
Yi Jin0ed9b682017-08-18 14:51:20 -0700223 // generates PRIVACY_POLICY
224 map<string, bool> messageNames;
225 if (generatePrivacyFlags(descriptor, "PRIVACY_POLICY", messageNames)) {
226 // if no privacy options set at all, define an empty list
Yi Jin7e0b4e52017-09-12 20:00:25 -0700227 printf("const Privacy* PRIVACY_POLICY_LIST[] = {};\n");
228 printf("const int PRIVACY_POLICY_COUNT = 0;\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700229 }
230
Yi Jinf8601842017-08-15 22:01:41 -0700231 return true;
232}
233
234// ================================================================================
235int main(int argc, char const *argv[])
236{
237 if (argc != 2) return 1;
238 const char* module = argv[1];
239
Yi Jin0ed9b682017-08-18 14:51:20 -0700240 Descriptor const* descriptor = IncidentProto::descriptor();
241
Yi Jinf8601842017-08-15 22:01:41 -0700242 if (strcmp(module, "incident") == 0) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700243 return !generateIncidentSectionsCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700244 }
245 if (strcmp(module, "incidentd") == 0 ) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700246 return !generateSectionListCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700247 }
248
249 // return failure if not called by the whitelisted modules
250 return 1;
Joe Onorato1754d742016-11-21 17:51:35 -0800251}