• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "chre/util/nanoapp/ble.h"
18 #include "chre_api/chre.h"
19 
20 namespace chre {
21 
22 using ble_constants::kGoogleEddystoneUuid;
23 using ble_constants::kGoogleManufactureData;
24 using ble_constants::kGoogleManufactureDataLength;
25 using ble_constants::kGoogleManufactureDataMask;
26 using ble_constants::kGoogleNearbyFastpairUuid;
27 using ble_constants::kGoogleUuidDataLength;
28 using ble_constants::kGoogleUuidMask;
29 using ble_constants::kNumManufacturerDataFilters;
30 using ble_constants::kNumScanFilters;
31 using ble_constants::kRssiThreshold;
32 
createBleGenericFilter(uint8_t type,uint8_t len,const uint8_t * data,const uint8_t * mask)33 chreBleGenericFilter createBleGenericFilter(uint8_t type, uint8_t len,
34                                             const uint8_t *data,
35                                             const uint8_t *mask) {
36   chreBleGenericFilter filter;
37   memset(&filter, 0, sizeof(filter));
38   filter.type = type;
39   filter.len = len;
40   memcpy(filter.data, data, sizeof(uint8_t) * len);
41   memcpy(filter.dataMask, mask, sizeof(uint8_t) * len);
42   return filter;
43 }
44 
createBleScanFilterForKnownBeacons(struct chreBleScanFilter & filter,chreBleGenericFilter * genericFilters,uint8_t numGenericFilters)45 bool createBleScanFilterForKnownBeacons(struct chreBleScanFilter &filter,
46                                         chreBleGenericFilter *genericFilters,
47                                         uint8_t numGenericFilters) {
48   if (numGenericFilters < kNumScanFilters) {
49     return false;
50   }
51   memset(&filter, 0, sizeof(filter));
52 
53   genericFilters[0] = createBleGenericFilter(
54       CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE, kGoogleUuidDataLength,
55       kGoogleEddystoneUuid, kGoogleUuidMask);
56   genericFilters[1] = createBleGenericFilter(
57       CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE, kGoogleUuidDataLength,
58       kGoogleNearbyFastpairUuid, kGoogleUuidMask);
59 
60   filter.rssiThreshold = kRssiThreshold;
61   filter.scanFilterCount = kNumScanFilters;
62   filter.scanFilters = genericFilters;
63   return true;
64 }
65 
createBleScanFilterForKnownBeaconsV1_9(struct chreBleScanFilterV1_9 & filter,chreBleGenericFilter * genericFilters,uint8_t numGenericFilters)66 bool createBleScanFilterForKnownBeaconsV1_9(
67     struct chreBleScanFilterV1_9 &filter, chreBleGenericFilter *genericFilters,
68     uint8_t numGenericFilters) {
69   if (numGenericFilters < kNumScanFilters) {
70     return false;
71   }
72   memset(&filter, 0, sizeof(filter));
73 
74   genericFilters[0] = createBleGenericFilter(
75       CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE, kGoogleUuidDataLength,
76       kGoogleEddystoneUuid, kGoogleUuidMask);
77   genericFilters[1] = createBleGenericFilter(
78       CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE, kGoogleUuidDataLength,
79       kGoogleNearbyFastpairUuid, kGoogleUuidMask);
80 
81   filter.rssiThreshold = kRssiThreshold;
82   filter.genericFilterCount = kNumScanFilters;
83   filter.genericFilters = genericFilters;
84 
85   filter.broadcasterAddressFilterCount = 0;
86   filter.broadcasterAddressFilters = nullptr;
87   return true;
88 }
89 
createBleManufacturerDataFilter(uint8_t numGenericFilters,chreBleGenericFilter * genericFilters,struct chreBleScanFilterV1_9 & filter)90 bool createBleManufacturerDataFilter(uint8_t numGenericFilters,
91                                      chreBleGenericFilter *genericFilters,
92                                      struct chreBleScanFilterV1_9 &filter) {
93   if (numGenericFilters < kNumManufacturerDataFilters) {
94     return false;
95   }
96   memset(&filter, 0, sizeof(filter));
97   filter.rssiThreshold = kRssiThreshold;
98 
99   genericFilters[0] = createBleGenericFilter(
100       CHRE_BLE_AD_TYPE_MANUFACTURER_DATA, kGoogleManufactureDataLength,
101       kGoogleManufactureData, kGoogleManufactureDataMask);
102 
103   filter.genericFilterCount = kNumManufacturerDataFilters;
104   filter.genericFilters = genericFilters;
105 
106   filter.broadcasterAddressFilterCount = 0;
107   filter.broadcasterAddressFilters = nullptr;
108   return true;
109 }
110 
111 }  // namespace chre
112