1 /*
2 * Copyright 2020 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 #pragma once
17
18 #include <base/strings/stringprintf.h>
19
20 #include <string>
21
22 #include "flatbuffers/flatbuffers.h"
23 #include "flatbuffers/idl.h"
24
25 namespace bluetooth {
26 namespace dumpsys {
27 namespace internal {
28
29 constexpr char kPrivacyAttributeKeyword[] = "privacy";
30
31 enum PrivacyLevel {
32 kPrivate = 0,
33 kOpaque = 1,
34 kAnonymized = 2,
35 kAny = 4,
36 kDefaultPrivacyLevel = kPrivate,
37 };
38
39 /**
40 * Remove the field offset from flatbuffer table eliminating ability to
41 * access value.
42 *
43 * @param table Table under consideration for field removeal
44 * @param field_offset Virtual offset of field into table.
45 */
46 void ScrubFromTable(flatbuffers::Table* table, flatbuffers::voffset_t field_offset);
47
48 /**
49 * Overwrite ihe contents of flatbuffer string with the integer value proviced.
50 * The entire size of the string will be set to the value provided.
51 *
52 * @param string Flatbuffer string under consideration for content changing.
53 * @param value Value to overwrite the string contents.
54 */
55 void ReplaceInString(flatbuffers::String* string, int value);
56
57 /**
58 * Overwrite the contents of flatbuffer string with a hashed value.
59 * The portion of the string greater than the hash value will be set to SPACE.
60 * If the string is not large enough for the entire hash value, the hash
61 * value will be truncated to the size of the string.
62 *
63 * @param string Flatbuffer string under consideration for content changing.
64 */
65 void RandomizeInString(flatbuffers::String* string);
66
67 /**
68 * Returns the privacy level name corresponding to the axtual numeric level.
69 *
70 * @param privacy_level PrivacyLevel
71 *
72 * @return Name of privacy level.
73 */
74 const char* PrivacyLevelName(PrivacyLevel privacy_level);
75
76 /**
77 * Returns the privacy level for the given field. If there is no explicitly
78 * privacy level for this field, the default privacy level is returned.
79 *
80 * @param field The reflection field for the schema
81 *
82 * @return Privacy level enumeration value
83 */
84 PrivacyLevel FindFieldPrivacyLevel(const reflection::Field& field);
85
86 /**
87 * Returns the privacy level for given privacy level keyword name.
88 * If the privacy level for this field, the default privacy level is returned.
89 *
90 * @param name The privacy level name.
91 *
92 * @return Privacy level enumeration value.
93 */
94 PrivacyLevel GetPrivacyLevelAttribute(const std::string& name);
95
96 /**
97 * Find a the reflection object that corresponds to the name provided.
98 * Returns nullptr is not found.
99 *
100 * @param objects Vector container of flatbuffer objects
101 * @param name Flatbuffer string name to search
102 *
103 * @return Reflection object if found, nullptr otherwise.
104 */
105 const reflection::Object* FindReflectionObject(
106 const flatbuffers::Vector<flatbuffers::Offset<reflection::Object>>* objects, const flatbuffers::String* name);
107
108 /**
109 * Process and filter the respective data types.
110 *
111 * @param field The reflection field schema.
112 * @param table The mutable table data corresponding to the schema.
113 * @param privacy_level The privacy level in which to filter the data.
114 *
115 * @return true if successfully filtered, false otherwise.
116 */
117 bool FilterTypeBool(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level);
118 bool FilterTypeFloat(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level);
119 bool FilterTypeInteger(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level);
120 bool FilterTypeLong(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level);
121 bool FilterTypeString(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level);
122 bool FilterTypeStruct(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level);
123
124 #define CASE_RETURN_TEXT(code) \
125 case code: \
126 return #code
127
FlatbufferTypeText(const flatbuffers::BaseType & type)128 inline std::string FlatbufferTypeText(const flatbuffers::BaseType& type) {
129 switch (type) {
130 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_NONE);
131 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_BOOL);
132 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_CHAR);
133 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_UCHAR);
134 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_SHORT);
135 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_USHORT);
136 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_INT);
137 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_UINT);
138 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_LONG);
139 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_ULONG);
140 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_FLOAT);
141 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_DOUBLE);
142 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_STRING);
143 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_VECTOR);
144 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_STRUCT);
145 CASE_RETURN_TEXT(flatbuffers::BASE_TYPE_UNION);
146 default:
147 return base::StringPrintf("UNKNOWN[%d]", (int)type);
148 }
149 }
150 #undef CASE_RETURN_TEXT
151
152 } // namespace internal
153 } // namespace dumpsys
154 } // namespace bluetooth
155