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