xref: /aosp_15_r20/system/chre/util/include/chre/util/nanoapp/ble.h (revision 84e339476a462649f82315436d70fd732297a399)
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 /**
52  * The public address of the known (bonded) BLE advertiser in big endian byte
53  * order. Change this address to the public identity address of the advertiser
54  * in the test.
55  *
56  * Example: To filter on the address (01:02:03:AB:CD:EF), use
57  * {0x01:0x02:0x03:0xAB:0xCD:0xEF}.
58  */
59 constexpr uint8_t kBroadcasterAddress[CHRE_BLE_ADDRESS_LEN] = {
60     0x01, 0x02, 0x03, 0xAB, 0xCD, 0xEF};
61 
62 /** The Google manufacturer ID followed by some data. */
63 constexpr uint8_t kGoogleManufactureData[kGoogleManufactureDataLength] = {
64     0xE0, 0x00, 0xAA, 0xFE};
65 
66 /** Manufacturer data filter mask. */
67 constexpr uint8_t kGoogleManufactureDataMask[kGoogleManufactureDataLength] = {
68     0xFF, 0xFF, 0xFF, 0xFF};
69 
70 /** The number of generic filters (equal to the number of known beacons). */
71 constexpr uint8_t kNumScanFilters = 2;
72 /** The number of manufacturer data filters. */
73 constexpr uint8_t kNumManufacturerDataFilters = 1;
74 
75 /**
76  * The number of broadcaster address filters (equal to the number of known
77  * public advertiser addresses).
78  */
79 constexpr uint8_t kNumBroadcasterFilters = 1;
80 }  // namespace ble_constants
81 
82 /**
83  * Create a BLE generic filter object.
84  *
85  * @param type                              the filter type.
86  * @param len                               the filter length.
87  * @param data                              the filter data.
88  * @param mask                              the filter mask.
89  * @return                                  the filter.
90  */
91 chreBleGenericFilter createBleGenericFilter(uint8_t type, uint8_t len,
92                                             const uint8_t *data,
93                                             const uint8_t *mask);
94 
95 /**
96  * Creates a chreBleScanFilter that filters for the Google eddystone UUID,
97  * the Google nearby fastpair UUID, and a RSSI threshold of kRssiThreshold.
98  *
99  * @param filter                            (out) the output filter.
100  * @param genericFilters                    (out) the output generic filters
101  * array.
102  * @param numGenericFilters                 the size of the generic filters
103  * array. must be >= kNumScanFilters.
104  *
105  * @return true                             the operation was successful
106  * @return false                            the operation was not successful
107  */
108 bool createBleScanFilterForKnownBeacons(struct chreBleScanFilter &filter,
109                                         chreBleGenericFilter *genericFilters,
110                                         uint8_t numGenericFilters);
111 
112 /**
113  * Similar to createBleScanFilterForKnownBeacons but creates a
114  * chreBleScanFilterV1_9 instead of a chreBleScanFilter. The
115  * broadcasterAddressFilters are set to empty.
116  */
117 bool createBleScanFilterForKnownBeaconsV1_9(
118     struct chreBleScanFilterV1_9 &filter, chreBleGenericFilter *genericFilters,
119     uint8_t numGenericFilters);
120 
121 /**
122  * Creates a chreBleScanFilterV1_9 that filters for advertisements with the
123  * manufacturer data in kGoogleManufactureData.
124  *
125  * @param numGenericFilters         The size of the generic filters array. Must
126  * be >= kNumManufacturerDataFilters.
127  * @param genericFilters            (out) The output generic filters.
128  * @param filter                    (out) The output filter.
129  *
130  * @return true if the filter was created successfully.
131  */
132 bool createBleManufacturerDataFilter(uint8_t numGenericFilters,
133                                      chreBleGenericFilter *genericFilters,
134                                      struct chreBleScanFilterV1_9 &filter);
135 
136 /**
137  * Creates a chreBleScanFilter that filters for the Google eddystone UUID,
138  * the Google nearby fastpair UUID, public identity address of a bonded device,
139  * and a RSSI threshold of kRssiThreshold.
140  *
141  * @param filter                   (out) the output filter.
142  * @param broadcasterFilters       (out) the output broadcaster address filters
143  * array.
144  * @param numBroadcasterFilters    the size of the broadcaster address filters
145  * array. must be >= kNumBroadcasterFilters.
146  *
147  * @return true                    the operation was successful
148  * @return false                   the operation was not successful
149  */
150 bool createBleScanFilterForAdvertiser(
151     struct chreBleScanFilterV1_9 &filter,
152     chreBleBroadcasterAddressFilter *broadcasterFilters,
153     uint8_t numBroadcasterFilters);
154 }  // namespace chre
155 
156 #endif  // CHRE_UTIL_NANOAPP_BLE_H_
157