Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Onorato | 7669012 | 2016-12-20 08:18:32 -0800 | [diff] [blame] | 18 | #include <frameworks/base/core/proto/android/os/incident.pb.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 19 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 20 | #include <map> |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame^] | 21 | #include <string> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 22 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame^] | 23 | using namespace android; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 24 | using namespace android::os; |
| 25 | using namespace google::protobuf; |
| 26 | using namespace google::protobuf::io; |
| 27 | using namespace google::protobuf::internal; |
| 28 | using namespace std; |
| 29 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame^] | 30 | static void generateHead(const char* header) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 31 | printf("// Auto generated file. Do not modify\n"); |
| 32 | printf("\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame^] | 33 | printf("#include \"%s.h\"\n", header); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 34 | printf("\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame^] | 35 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 36 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame^] | 37 | // ================================================================================ |
| 38 | static bool generateIncidentSectionsCpp() |
| 39 | { |
| 40 | generateHead("incident_sections"); |
| 41 | |
| 42 | map<string,FieldDescriptor const*> sections; |
| 43 | int N; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 44 | 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 Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame^] | 70 | return true; |
| 71 | } |
| 72 | |
| 73 | // ================================================================================ |
| 74 | static 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 | |
| 88 | static 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 | // ================================================================================ |
| 124 | int 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 138 | } |