1 /*
2  * Copyright (C) 2022 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 CHRE_UTIL_NANOAPP_BLE_H_
18 #define CHRE_UTIL_NANOAPP_BLE_H_
19 
20 #include <inttypes.h>
21 #include <cstdint>
22 
23 #include "chre_api/chre.h"
24 
25 namespace chre {
26 
27 namespace ble_constants {
28 
29 /**
30  * The minimum threshold for RSSI. Used to filter out RSSI values below this.
31  */
32 constexpr int8_t kRssiThreshold = -128;
33 
34 /**
35  * The length of the UUID data at the beginning of the data in the BLE packet.
36  */
37 constexpr uint16_t kGoogleUuidDataLength = 2;
38 
39 /** The mask to get the UUID from the data in the BLE packet. */
40 constexpr uint8_t kGoogleUuidMask[kGoogleUuidDataLength] = {0xFF, 0xFF};
41 
42 /** The Google Eddystone BLE beacon UUID. */
43 constexpr uint8_t kGoogleEddystoneUuid[kGoogleUuidDataLength] = {0xAA, 0xFE};
44 
45 /** The Google Nearby Fastpair BLE beacon UUID. */
46 constexpr uint8_t kGoogleNearbyFastpairUuid[kGoogleUuidDataLength] = {0x2C,
47                                                                       0xFE};
48 /** Length of Google manufacturer data filter. */
49 constexpr uint16_t kGoogleManufactureDataLength = 4;
50 
51 /** The Google manufacturer ID followed by some data. */
52 constexpr uint8_t kGoogleManufactureData[kGoogleManufactureDataLength] = {
53     0xE0, 0x00, 0xAA, 0xFE};
54 
55 /** Manufacturer data filter mask. */
56 constexpr uint8_t kGoogleManufactureDataMask[kGoogleManufactureDataLength] = {
57     0xFF, 0xFF, 0xFF, 0xFF};
58 
59 /** The number of generic filters (equal to the number of known beacons). */
60 constexpr uint8_t kNumScanFilters = 2;
61 
62 /** The number of manufacturer data filters. */
63 constexpr uint8_t kNumManufacturerDataFilters = 1;
64 
65 }  // namespace ble_constants
66 
67 /**
68  * Create a BLE generic filter object.
69  *
70  * @param type                              the filter type.
71  * @param len                               the filter length.
72  * @param data                              the filter data.
73  * @param mask                              the filter mask.
74  * @return                                  the filter.
75  */
76 chreBleGenericFilter createBleGenericFilter(uint8_t type, uint8_t len,
77                                             const uint8_t *data,
78                                             const uint8_t *mask);
79 
80 /**
81  * Creates a chreBleScanFilter that filters for the Google eddystone UUID,
82  * the Google nearby fastpair UUID, and a RSSI threshold of kRssiThreshold.
83  *
84  * @param filter                            (out) the output filter.
85  * @param genericFilters                    (out) the output generic filters
86  * array.
87  * @param numGenericFilters                 the size of the generic filters
88  * array. must be >= kNumScanFilters.
89  *
90  * @return true                             the operation was successful
91  * @return false                            the operation was not successful
92  */
93 bool createBleScanFilterForKnownBeacons(struct chreBleScanFilter &filter,
94                                         chreBleGenericFilter *genericFilters,
95                                         uint8_t numGenericFilters);
96 
97 /**
98  * Similar to createBleScanFilterForKnownBeacons but creates a
99  * chreBleScanFilterV1_9 instead of a chreBleScanFilter. The
100  * broadcasterAddressFilters are set to empty.
101  */
102 bool createBleScanFilterForKnownBeaconsV1_9(
103     struct chreBleScanFilterV1_9 &filter, chreBleGenericFilter *genericFilters,
104     uint8_t numGenericFilters);
105 
106 /**
107  * Creates a chreBleScanFilterV1_9 that filters for advertisements with the
108  * manufacturer data in kGoogleManufactureData.
109  *
110  * @param numGenericFilters         The size of the generic filters array. Must
111  * be >= kNumManufacturerDataFilters.
112  * @param genericFilters            (out) The output generic filters.
113  * @param filter                    (out) The output filter.
114  *
115  * @return true if the filter was created successfully.
116  */
117 bool createBleManufacturerDataFilter(uint8_t numGenericFilters,
118                                      chreBleGenericFilter *genericFilters,
119                                      struct chreBleScanFilterV1_9 &filter);
120 
121 }  // namespace chre
122 
123 #endif  // CHRE_UTIL_NANOAPP_BLE_H_
124