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