• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <google/protobuf/descriptor.h>
21 #include <stdint.h>
22 
23 #include <map>
24 #include <set>
25 #include <vector>
26 
27 #include "frameworks/proto_logging/stats/atom_field_options.pb.h"
28 
29 namespace android {
30 namespace stats_log_api_gen {
31 
32 using google::protobuf::Descriptor;
33 using google::protobuf::FieldDescriptor;
34 using google::protobuf::OneofDescriptor;
35 using std::map;
36 using std::set;
37 using std::shared_ptr;
38 using std::string;
39 using std::vector;
40 
41 const int PULL_ATOM_START_ID = 10000;
42 
43 const int FIRST_UID_IN_CHAIN_ID = 0;
44 
45 /**
46  * The types of oneof atoms.
47  *
48  * `OneofDescriptor::name()` returns the name of the oneof.
49  */
50 const char ONEOF_PUSHED_ATOM_NAME[] = "pushed";
51 const char ONEOF_PULLED_ATOM_NAME[] = "pulled";
52 
53 enum AtomType { ATOM_TYPE_PUSHED, ATOM_TYPE_PULLED };
54 
55 enum AnnotationId : uint8_t {
56     ANNOTATION_ID_IS_UID = 1,
57     ANNOTATION_ID_TRUNCATE_TIMESTAMP = 2,
58     ANNOTATION_ID_PRIMARY_FIELD = 3,
59     ANNOTATION_ID_EXCLUSIVE_STATE = 4,
60     ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID = 5,
61     ANNOTATION_ID_DEFAULT_STATE = 6,
62     ANNOTATION_ID_TRIGGER_STATE_RESET = 7,
63     ANNOTATION_ID_STATE_NESTED = 8,
64     ANNOTATION_ID_RESTRICTION_CATEGORY = 9,
65     ANNOTATION_ID_FIELD_RESTRICTION_PERIPHERAL_DEVICE_INFO = 10,
66     ANNOTATION_ID_FIELD_RESTRICTION_APP_USAGE = 11,
67     ANNOTATION_ID_FIELD_RESTRICTION_APP_ACTIVITY = 12,
68     ANNOTATION_ID_FIELD_RESTRICTION_HEALTH_CONNECT = 13,
69     ANNOTATION_ID_FIELD_RESTRICTION_ACCESSIBILITY = 14,
70     ANNOTATION_ID_FIELD_RESTRICTION_SYSTEM_SEARCH = 15,
71     ANNOTATION_ID_FIELD_RESTRICTION_USER_ENGAGEMENT = 16,
72     ANNOTATION_ID_FIELD_RESTRICTION_AMBIENT_SENSING = 17,
73     ANNOTATION_ID_FIELD_RESTRICTION_DEMOGRAPHIC_CLASSIFICATION = 18,
74 };
75 
76 const int ATOM_ID_FIELD_NUMBER = -1;
77 
78 const char DEFAULT_MODULE_NAME[] = "DEFAULT";
79 
80 /**
81  * The types for atom parameters.
82  */
83 typedef enum {
84     JAVA_TYPE_UNKNOWN_OR_INVALID = 0,
85 
86     JAVA_TYPE_ATTRIBUTION_CHAIN = 1,
87     JAVA_TYPE_BOOLEAN = 2,
88     JAVA_TYPE_INT = 3,
89     JAVA_TYPE_LONG = 4,
90     JAVA_TYPE_FLOAT = 5,
91     JAVA_TYPE_DOUBLE = 6,
92     JAVA_TYPE_STRING = 7,
93     JAVA_TYPE_ENUM = 8,
94     JAVA_TYPE_BOOLEAN_ARRAY = 10,
95     JAVA_TYPE_INT_ARRAY = 11,
96     JAVA_TYPE_LONG_ARRAY = 12,
97     JAVA_TYPE_FLOAT_ARRAY = 13,
98     JAVA_TYPE_DOUBLE_ARRAY = 14,
99     JAVA_TYPE_STRING_ARRAY = 15,
100     JAVA_TYPE_ENUM_ARRAY = 16,
101 
102     JAVA_TYPE_OBJECT = -1,
103     JAVA_TYPE_BYTE_ARRAY = -2,
104 } java_type_t;
105 
106 enum AnnotationType {
107     ANNOTATION_TYPE_UNKNOWN = 0,
108     ANNOTATION_TYPE_INT = 1,
109     ANNOTATION_TYPE_BOOL = 2,
110 };
111 
112 union AnnotationValue {
113     int intValue;
114     bool boolValue;
115 
AnnotationValue(const int value)116     explicit AnnotationValue(const int value) : intValue(value) {
117     }
AnnotationValue(const bool value)118     explicit AnnotationValue(const bool value) : boolValue(value) {
119     }
120 };
121 
122 struct Annotation {
123     const AnnotationId annotationId;
124     const int atomId;
125     AnnotationType type;
126     AnnotationValue value;
127 
AnnotationAnnotation128     inline Annotation(AnnotationId annotationId, int atomId, AnnotationType type,
129                       AnnotationValue value)
130         : annotationId(annotationId), atomId(atomId), type(type), value(value) {
131     }
~AnnotationAnnotation132     inline ~Annotation() {
133     }
134 
135     inline bool operator<(const Annotation& that) const {
136         return atomId == that.atomId ? annotationId < that.annotationId : atomId < that.atomId;
137     }
138 };
139 
140 struct SharedComparator {
141     template <typename T>
operatorSharedComparator142     inline bool operator()(const shared_ptr<T>& lhs, const shared_ptr<T>& rhs) const {
143         return (*lhs) < (*rhs);
144     }
145 };
146 
147 using AnnotationSet = set<shared_ptr<Annotation>, SharedComparator>;
148 
149 using FieldNumberToAnnotations = map<int, AnnotationSet>;
150 
151 /**
152  * The name and type for an atom field.
153  */
154 struct AtomField {
155     string name;
156     java_type_t javaType;
157 
158     // If the field is of type enum, the following map contains the list of enum
159     // values.
160     map<int /* numeric value */, string /* value name */> enumValues;
161     // If the field is of type enum, the following field contains enum type name
162     string enumTypeName;
163 
AtomFieldAtomField164     inline AtomField() : name(), javaType(JAVA_TYPE_UNKNOWN_OR_INVALID) {
165     }
AtomFieldAtomField166     inline AtomField(const AtomField& that)
167         : name(that.name),
168           javaType(that.javaType),
169           enumValues(that.enumValues),
170           enumTypeName(that.enumTypeName) {
171     }
172 
AtomFieldAtomField173     inline AtomField(string n, java_type_t jt) : name(n), javaType(jt) {
174     }
~AtomFieldAtomField175     inline ~AtomField() {
176     }
177 };
178 
179 /**
180  * The name and code for an atom.
181  */
182 struct AtomDecl {
183     int code;
184     string name;
185 
186     string message;
187     vector<AtomField> fields;
188 
189     AtomType atomType;
190 
191     FieldNumberToAnnotations fieldNumberToAnnotations;
192 
193     vector<int> primaryFields;
194     int exclusiveField = 0;
195     int defaultState = INT_MAX;
196     int triggerStateReset = INT_MAX;
197     bool nested = true;
198     bool restricted = false;
199 
200     AtomDecl();
201     AtomDecl(const AtomDecl& that);
202     AtomDecl(int code, const string& name, const string& message, AtomType atomType);
203     ~AtomDecl();
204 
205     inline bool operator<(const AtomDecl& that) const {
206         return (code == that.code) ? (name < that.name) : (code < that.code);
207     }
208 };
209 
210 using AtomDeclSet = set<shared_ptr<AtomDecl>, SharedComparator>;
211 
212 // Maps a field number to a set of atoms that have annotation(s) for their field with that field
213 // number.
214 using FieldNumberToAtomDeclSet = map<int, AtomDeclSet>;
215 
216 using SignatureInfoMap = map<vector<java_type_t>, FieldNumberToAtomDeclSet>;
217 
218 struct Atoms {
219     SignatureInfoMap signatureInfoMap;
220     SignatureInfoMap pulledAtomsSignatureInfoMap;
221     AtomDeclSet decls;
222     AtomDeclSet non_chained_decls;
223     SignatureInfoMap nonChainedSignatureInfoMap;
224 };
225 
226 /**
227  * Gather the information about the atoms.  Returns the number of errors.
228  */
229 int collate_atoms(const Descriptor* descriptor, const string& moduleName, Atoms* atoms);
230 int collate_atom(const Descriptor* atom, AtomDecl* atomDecl, vector<java_type_t>* signature);
231 
232 }  // namespace stats_log_api_gen
233 }  // namespace android
234 
235 #endif  // ANDROID_STATS_LOG_API_GEN_COLLATION_H
236