1 /*
2  * Copyright 2023 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 #pragma once
18 
19 #include <cstdint>
20 
21 namespace rootcanal::apcf {
22 
23 /// Records the filtering parameters for the specified filter_index.
24 /// The associated advertising filters are added to their respective tables.
25 struct Filter {
26   uint8_t filter_index;
27   uint16_t feature_selection;
28   uint16_t list_logic_type;
29   uint8_t filter_logic_type;
30   uint8_t rssi_high_thresh;
31   bluetooth::hci::DeliveryMode delivery_mode;
32   uint16_t onfound_timeout;
33   uint8_t onfound_timeout_cnt;
34   uint8_t rssi_low_thresh;
35   uint16_t onlost_timeout;
36   uint16_t num_of_tracking_entries;
37 };
38 
39 /// Filter for matching the advertiser address.
40 struct BroadcasterAddressFilter {
41   uint8_t filter_index;
42   bluetooth::hci::Address broadcaster_address;
43   bluetooth::hci::ApcfApplicationAddressType application_address_type;
44 };
45 
46 /// Generic filter for GAP data information.
47 /// Used for matching Service UUID, Service Solicitation UUID,
48 /// Local Name, Manufacturer Data, Service Data.
49 struct GapDataFilter {
50   uint8_t filter_index;
51   std::vector<uint8_t> gap_data;
52   std::vector<uint8_t> gap_data_mask;
53 };
54 
55 /// Filter for matching the AD type.
56 struct AdTypeFilter {
57   uint8_t filter_index;
58   uint8_t ad_type;
59   std::vector<uint8_t> ad_data;
60   std::vector<uint8_t> ad_data_mask;
61 };
62 
63 /// State of the APCF scanner.
64 struct ApcfScanner {
65   bool enable{false};
66   std::vector<Filter> filters{};
67   std::vector<BroadcasterAddressFilter> broadcaster_address_filters{};
68   std::vector<GapDataFilter> service_uuid_filters{};
69   std::vector<GapDataFilter> service_solicitation_uuid_filters{};
70   std::vector<GapDataFilter> local_name_filters{};
71   std::vector<GapDataFilter> manufacturer_data_filters{};
72   std::vector<GapDataFilter> service_data_filters{};
73   std::vector<AdTypeFilter> ad_type_filters{};
74 
75   // Return if the APCF filter index is defined in the
76   // list of filters.
77   bool HasFilterIndex(uint8_t apcf_filter_index) const;
78 
79   // Remove the entries associated with the APCF filter index
80   // from all tables.
81   void ClearFilterIndex(uint8_t apcf_filter_index);
82 
83   // Remove all entries in all tables.
84   void Clear();
85 
86   // Apply the requested modification to the selected
87   // filter list.
88   template <typename T>
89   ErrorCode UpdateFilterList(std::vector<T>& filter_list,
90                              size_t max_filter_list_size,
91                              bluetooth::hci::ApcfAction action, T filter);
92 };
93 
94 }  // namespace rootcanal::apcf
95