• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 package com.android.bluetooth.gatt;
18 
19 import android.bluetooth.BluetoothUuid;
20 import android.bluetooth.le.ScanFilter;
21 import android.os.ParcelUuid;
22 
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.Set;
27 import java.util.UUID;
28 
29 /**
30  * Helper class used to manage advertisement package filters.
31  *
32  * @hide
33  */
34 /* package */class ScanFilterQueue {
35     public static final int TYPE_DEVICE_ADDRESS = 0;
36     public static final int TYPE_SERVICE_DATA_CHANGED = 1;
37     public static final int TYPE_SERVICE_UUID = 2;
38     public static final int TYPE_SOLICIT_UUID = 3;
39     public static final int TYPE_LOCAL_NAME = 4;
40     public static final int TYPE_MANUFACTURER_DATA = 5;
41     public static final int TYPE_SERVICE_DATA = 6;
42     public static final int TYPE_ADVERTISING_DATA_TYPE = 8;
43 
44     // Max length is 31 - 3(flags) - 2 (one byte for length and one byte for type).
45     private static final int MAX_LEN_PER_FIELD = 26;
46 
47     // Values defined in bluedroid.
48     private static final byte DEVICE_TYPE_ALL = 2;
49 
50     class Entry {
51         public byte type;
52         public String address;
53         public byte addr_type;
54         public byte[] irk;
55         public UUID uuid;
56         public UUID uuid_mask;
57         public String name;
58         public int company;
59         public int company_mask;
60         public int ad_type;
61         public byte[] data;
62         public byte[] data_mask;
63     }
64 
65     private Set<Entry> mEntries = new HashSet<Entry>();
66 
addDeviceAddress(String address, byte type, byte[] irk)67     void addDeviceAddress(String address, byte type, byte[] irk) {
68         Entry entry = new Entry();
69         entry.type = TYPE_DEVICE_ADDRESS;
70         entry.address = address;
71         entry.addr_type = type;
72         entry.irk = irk;
73         mEntries.add(entry);
74     }
75 
addServiceChanged()76     void addServiceChanged() {
77         Entry entry = new Entry();
78         entry.type = TYPE_SERVICE_DATA_CHANGED;
79         mEntries.add(entry);
80     }
81 
addUuid(UUID uuid)82     void addUuid(UUID uuid) {
83         Entry entry = new Entry();
84         entry.type = TYPE_SERVICE_UUID;
85         entry.uuid = uuid;
86         entry.uuid_mask = new UUID(0, 0);
87         mEntries.add(entry);
88     }
89 
addUuid(UUID uuid, UUID uuidMask)90     void addUuid(UUID uuid, UUID uuidMask) {
91         Entry entry = new Entry();
92         entry.type = TYPE_SERVICE_UUID;
93         entry.uuid = uuid;
94         entry.uuid_mask = uuidMask;
95         mEntries.add(entry);
96     }
97 
addSolicitUuid(UUID uuid)98     void addSolicitUuid(UUID uuid) {
99         Entry entry = new Entry();
100         entry.type = TYPE_SOLICIT_UUID;
101         entry.uuid = uuid;
102         entry.uuid_mask = new UUID(0, 0);
103         mEntries.add(entry);
104     }
105 
addSolicitUuid(UUID uuid, UUID uuidMask)106     void addSolicitUuid(UUID uuid, UUID uuidMask) {
107         Entry entry = new Entry();
108         entry.type = TYPE_SOLICIT_UUID;
109         entry.uuid = uuid;
110         entry.uuid_mask = uuidMask;
111         mEntries.add(entry);
112     }
113 
addName(String name)114     void addName(String name) {
115         Entry entry = new Entry();
116         entry.type = TYPE_LOCAL_NAME;
117         entry.name = name;
118         mEntries.add(entry);
119     }
120 
addManufacturerData(int company, byte[] data)121     void addManufacturerData(int company, byte[] data) {
122         Entry entry = new Entry();
123         entry.type = TYPE_MANUFACTURER_DATA;
124         entry.company = company;
125         entry.company_mask = 0xFFFF;
126         entry.data = data;
127         entry.data_mask = new byte[data.length];
128         Arrays.fill(entry.data_mask, (byte) 0xFF);
129         mEntries.add(entry);
130     }
131 
addManufacturerData(int company, int companyMask, byte[] data, byte[] dataMask)132     void addManufacturerData(int company, int companyMask, byte[] data, byte[] dataMask) {
133         Entry entry = new Entry();
134         entry.type = TYPE_MANUFACTURER_DATA;
135         entry.company = company;
136         entry.company_mask = companyMask;
137         entry.data = data;
138         entry.data_mask = dataMask;
139         mEntries.add(entry);
140     }
141 
addServiceData(byte[] data, byte[] dataMask)142     void addServiceData(byte[] data, byte[] dataMask) {
143         Entry entry = new Entry();
144         entry.type = TYPE_SERVICE_DATA;
145         entry.data = data;
146         entry.data_mask = dataMask;
147         mEntries.add(entry);
148     }
149 
addAdvertisingDataType(int adType, byte[] data, byte[] dataMask)150     void addAdvertisingDataType(int adType, byte[] data, byte[] dataMask) {
151         Entry entry = new Entry();
152         entry.type = TYPE_ADVERTISING_DATA_TYPE;
153         entry.ad_type = adType;
154         entry.data = data;
155         entry.data_mask = dataMask;
156         mEntries.add(entry);
157     }
158 
pop()159     Entry pop() {
160         if (mEntries.isEmpty()) {
161             return null;
162         }
163         Iterator<Entry> iterator = mEntries.iterator();
164         Entry entry = iterator.next();
165         iterator.remove();
166         return entry;
167     }
168 
169     /**
170      * Compute feature selection based on the filters presented.
171      */
getFeatureSelection()172     int getFeatureSelection() {
173         int selc = 0;
174         for (Entry entry : mEntries) {
175             selc |= (1 << entry.type);
176         }
177         return selc;
178     }
179 
toArray()180     ScanFilterQueue.Entry[] toArray() {
181         return mEntries.toArray(new ScanFilterQueue.Entry[mEntries.size()]);
182     }
183 
184     /**
185      * Add ScanFilter to scan filter queue.
186      */
addScanFilter(ScanFilter filter)187     void addScanFilter(ScanFilter filter) {
188         if (filter == null) {
189             return;
190         }
191         if (filter.getDeviceName() != null) {
192             addName(filter.getDeviceName());
193         }
194         if (filter.getDeviceAddress() != null) {
195             /*
196              * Pass the addres type here.  This address type will be used for the resolving address,
197              * however, the host stack will force the type to 0x02 for the APCF filter in
198              * btm_ble_adv_filter.cc#BTM_LE_PF_addr_filter(...)
199              */
200             addDeviceAddress(filter.getDeviceAddress(), (byte) filter.getAddressType(),
201                     filter.getIrk());
202         }
203         if (filter.getServiceUuid() != null) {
204             if (filter.getServiceUuidMask() == null) {
205                 addUuid(filter.getServiceUuid().getUuid());
206             } else {
207                 addUuid(filter.getServiceUuid().getUuid(), filter.getServiceUuidMask().getUuid());
208             }
209         }
210         if (filter.getServiceSolicitationUuid() != null) {
211             if (filter.getServiceSolicitationUuidMask() == null) {
212                 addSolicitUuid(filter.getServiceSolicitationUuid().getUuid());
213             } else {
214                 addSolicitUuid(filter.getServiceSolicitationUuid().getUuid(),
215                         filter.getServiceSolicitationUuidMask().getUuid());
216             }
217         }
218         if (filter.getManufacturerData() != null) {
219             if (filter.getManufacturerDataMask() == null) {
220                 addManufacturerData(filter.getManufacturerId(), filter.getManufacturerData());
221             } else {
222                 addManufacturerData(filter.getManufacturerId(), 0xFFFF,
223                         filter.getManufacturerData(), filter.getManufacturerDataMask());
224             }
225         }
226         if (filter.getServiceDataUuid() != null && filter.getServiceData() != null) {
227             ParcelUuid serviceDataUuid = filter.getServiceDataUuid();
228             byte[] serviceData = filter.getServiceData();
229             byte[] serviceDataMask = filter.getServiceDataMask();
230             if (serviceDataMask == null) {
231                 serviceDataMask = new byte[serviceData.length];
232                 Arrays.fill(serviceDataMask, (byte) 0xFF);
233             }
234             serviceData = concate(serviceDataUuid, serviceData);
235             serviceDataMask = concate(serviceDataUuid, serviceDataMask);
236             if (serviceData != null && serviceDataMask != null) {
237                 addServiceData(serviceData, serviceDataMask);
238             }
239         }
240         if (filter.getAdvertisingDataType() > 0) {
241             addAdvertisingDataType(filter.getAdvertisingDataType(),
242                     filter.getAdvertisingData(), filter.getAdvertisingDataMask());
243         }
244     }
245 
concate(ParcelUuid serviceDataUuid, byte[] serviceData)246     private byte[] concate(ParcelUuid serviceDataUuid, byte[] serviceData) {
247         byte[] uuid = BluetoothUuid.uuidToBytes(serviceDataUuid);
248 
249         int dataLen = uuid.length + (serviceData == null ? 0 : serviceData.length);
250         // If data is too long, don't add it to hardware scan filter.
251         if (dataLen > MAX_LEN_PER_FIELD) {
252             return null;
253         }
254         byte[] concated = new byte[dataLen];
255         System.arraycopy(uuid, 0, concated, 0, uuid.length);
256         if (serviceData != null) {
257             System.arraycopy(serviceData, 0, concated, uuid.length, serviceData.length);
258         }
259         return concated;
260     }
261 }
262