1 /*
2  * Copyright (C) 2017, 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 #ifndef ANDROID_STATS_LOG_API_GEN_COLLATION_H
18 #define ANDROID_STATS_LOG_API_GEN_COLLATION_H
19 
20 
21 #include <google/protobuf/descriptor.h>
22 
23 #include <set>
24 #include <vector>
25 #include <map>
26 
27 namespace android {
28 namespace stats_log_api_gen {
29 
30 using std::map;
31 using std::set;
32 using std::string;
33 using std::vector;
34 using google::protobuf::Descriptor;
35 using google::protobuf::FieldDescriptor;
36 
37 const int PULL_ATOM_START_ID = 10000;
38 
39 /**
40  * The types for atom parameters.
41  */
42 typedef enum {
43   JAVA_TYPE_UNKNOWN = 0,
44 
45   JAVA_TYPE_ATTRIBUTION_CHAIN = 1,
46   JAVA_TYPE_BOOLEAN = 2,
47   JAVA_TYPE_INT = 3,
48   JAVA_TYPE_LONG = 4,
49   JAVA_TYPE_FLOAT = 5,
50   JAVA_TYPE_DOUBLE = 6,
51   JAVA_TYPE_STRING = 7,
52   JAVA_TYPE_ENUM = 8,
53   JAVA_TYPE_KEY_VALUE_PAIR = 9,
54 
55   JAVA_TYPE_OBJECT = -1,
56   JAVA_TYPE_BYTE_ARRAY = -2,
57 } java_type_t;
58 
59 /**
60  * The name and type for an atom field.
61  */
62 struct AtomField {
63     string name;
64     java_type_t javaType;
65 
66     // If the field is of type enum, the following map contains the list of enum values.
67     map<int /* numeric value */, string /* value name */> enumValues;
68 
AtomFieldAtomField69     inline AtomField() :name(), javaType(JAVA_TYPE_UNKNOWN) {}
AtomFieldAtomField70     inline AtomField(const AtomField& that) :name(that.name),
71                                              javaType(that.javaType),
72                                              enumValues(that.enumValues) {}
AtomFieldAtomField73     inline AtomField(string n, java_type_t jt) :name(n), javaType(jt) {}
~AtomFieldAtomField74     inline ~AtomField() {}
75 };
76 
77 /**
78  * The name and code for an atom.
79  */
80 struct AtomDecl {
81     int code;
82     string name;
83 
84     string message;
85     vector<AtomField> fields;
86 
87     vector<int> primaryFields;
88     int exclusiveField = 0;
89 
90     int uidField = 0;
91 
92     bool whitelisted = false;
93 
94     vector<int> binaryFields;
95 
96     bool hasModule = false;
97     string moduleName;
98 
99     AtomDecl();
100     AtomDecl(const AtomDecl& that);
101     AtomDecl(int code, const string& name, const string& message);
102     ~AtomDecl();
103 
104     inline bool operator<(const AtomDecl& that) const {
105         return (code == that.code) ? (name < that.name) : (code < that.code);
106     }
107 };
108 
109 struct Atoms {
110     map<vector<java_type_t>, set<string>> signatures_to_modules;
111     set<AtomDecl> decls;
112     set<AtomDecl> non_chained_decls;
113     map<vector<java_type_t>, set<string>> non_chained_signatures_to_modules;
114     int maxPushedAtomId;
115 };
116 
117 /**
118  * Gather the information about the atoms.  Returns the number of errors.
119  */
120 int collate_atoms(const Descriptor* descriptor, Atoms* atoms);
121 int collate_atom(const Descriptor *atom, AtomDecl *atomDecl, vector<java_type_t> *signature);
122 
123 }  // namespace stats_log_api_gen
124 }  // namespace android
125 
126 
127 #endif // ANDROID_STATS_LOG_API_GEN_COLLATION_H
128