• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2014  Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #define LOG_TAG "bt_btm_ble"
20 
21 #include "bt_target.h"
22 
23 #include "bt_types.h"
24 #include "bt_utils.h"
25 #include "btm_ble_api.h"
26 #include "btm_int.h"
27 #include "btu.h"
28 #include "device/include/controller.h"
29 #include "hcidefs.h"
30 #include "hcimsgs.h"
31 
32 #include <string.h>
33 #include <algorithm>
34 #include <vector>
35 
36 #include <base/bind.h>
37 #include <base/bind_helpers.h>
38 
39 using base::Bind;
40 using bluetooth::Uuid;
41 
42 #define BTM_BLE_ADV_FILT_META_HDR_LENGTH 3
43 #define BTM_BLE_ADV_FILT_FEAT_SELN_LEN 13
44 #define BTM_BLE_ADV_FILT_TRACK_NUM 2
45 
46 #define BTM_BLE_PF_SELECT_NONE 0
47 
48 /* BLE meta vsc header: 1 bytes of sub_code, 1 byte of PCF action */
49 #define BTM_BLE_META_HDR_LENGTH 3
50 #define BTM_BLE_PF_FEAT_SEL_LEN 18
51 #define BTM_BLE_PCF_ENABLE_LEN 2
52 
53 #define BTM_BLE_META_ADDR_LEN 7
54 #define BTM_BLE_META_UUID_LEN 40
55 
56 #define BTM_BLE_PF_BIT_TO_MASK(x) (uint16_t)(1 << (x))
57 
58 tBTM_BLE_ADV_FILTER_CB btm_ble_adv_filt_cb;
59 tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
60 
61 static uint8_t btm_ble_cs_update_pf_counter(tBTM_BLE_SCAN_COND_OP action,
62                                             uint8_t cond_type,
63                                             tBLE_BD_ADDR* p_bd_addr,
64                                             uint8_t num_available);
65 
66 #define BTM_BLE_SET_SCAN_PF_OPCODE(x, y) (((x) << 4) | (y))
67 #define BTM_BLE_GET_SCAN_PF_SUBCODE(x) ((x) >> 4)
68 #define BTM_BLE_GET_SCAN_PF_ACTION(x) ((x)&0x0f)
69 #define BTM_BLE_INVALID_COUNTER 0xff
70 
71 /* length of each multi adv sub command */
72 #define BTM_BLE_ADV_FILTER_ENB_LEN 3
73 
74 /* length of each batch scan command */
75 #define BTM_BLE_ADV_FILTER_CLEAR_LEN 3
76 #define BTM_BLE_ADV_FILTER_LEN 2
77 
78 #define BTM_BLE_ADV_FILT_CB_EVT_MASK 0xF0
79 #define BTM_BLE_ADV_FILT_SUBCODE_MASK 0x0F
80 
is_filtering_supported()81 bool is_filtering_supported() {
82   return cmn_ble_vsc_cb.filter_support != 0 && cmn_ble_vsc_cb.max_filter != 0;
83 }
84 
85 /*******************************************************************************
86  *
87  * Function         btm_ble_condtype_to_ocf
88  *
89  * Description      Convert cond_type to OCF
90  *
91  * Returns          Returns ocf value
92  *
93  ******************************************************************************/
btm_ble_condtype_to_ocf(uint8_t cond_type)94 uint8_t btm_ble_condtype_to_ocf(uint8_t cond_type) {
95   uint8_t ocf = 0;
96 
97   switch (cond_type) {
98     case BTM_BLE_PF_ADDR_FILTER:
99       ocf = BTM_BLE_META_PF_ADDR;
100       break;
101     case BTM_BLE_PF_SRVC_UUID:
102       ocf = BTM_BLE_META_PF_UUID;
103       break;
104     case BTM_BLE_PF_SRVC_SOL_UUID:
105       ocf = BTM_BLE_META_PF_SOL_UUID;
106       break;
107     case BTM_BLE_PF_LOCAL_NAME:
108       ocf = BTM_BLE_META_PF_LOCAL_NAME;
109       break;
110     case BTM_BLE_PF_MANU_DATA:
111       ocf = BTM_BLE_META_PF_MANU_DATA;
112       break;
113     case BTM_BLE_PF_SRVC_DATA_PATTERN:
114       ocf = BTM_BLE_META_PF_SRVC_DATA;
115       break;
116     case BTM_BLE_PF_TYPE_ALL:
117       ocf = BTM_BLE_META_PF_ALL;
118       break;
119     default:
120       ocf = BTM_BLE_PF_TYPE_MAX;
121       break;
122   }
123   return ocf;
124 }
125 
126 /*******************************************************************************
127  *
128  * Function         btm_ble_ocf_to_condtype
129  *
130  * Description      Convert OCF to cond type
131  *
132  * Returns          Returns condtype value
133  *
134  ******************************************************************************/
btm_ble_ocf_to_condtype(uint8_t ocf)135 uint8_t btm_ble_ocf_to_condtype(uint8_t ocf) {
136   uint8_t cond_type = 0;
137 
138   switch (ocf) {
139     case BTM_BLE_META_PF_FEAT_SEL:
140       cond_type = BTM_BLE_META_PF_FEAT_SEL;
141       break;
142     case BTM_BLE_META_PF_ADDR:
143       cond_type = BTM_BLE_PF_ADDR_FILTER;
144       break;
145     case BTM_BLE_META_PF_UUID:
146       cond_type = BTM_BLE_PF_SRVC_UUID;
147       break;
148     case BTM_BLE_META_PF_SOL_UUID:
149       cond_type = BTM_BLE_PF_SRVC_SOL_UUID;
150       break;
151     case BTM_BLE_META_PF_LOCAL_NAME:
152       cond_type = BTM_BLE_PF_LOCAL_NAME;
153       break;
154     case BTM_BLE_META_PF_MANU_DATA:
155       cond_type = BTM_BLE_PF_MANU_DATA;
156       break;
157     case BTM_BLE_META_PF_SRVC_DATA:
158       cond_type = BTM_BLE_PF_SRVC_DATA_PATTERN;
159       break;
160     case BTM_BLE_META_PF_ALL:
161       cond_type = BTM_BLE_PF_TYPE_ALL;
162       break;
163     default:
164       cond_type = BTM_BLE_PF_TYPE_MAX;
165       break;
166   }
167   return cond_type;
168 }
169 
btm_flt_update_cb(uint8_t expected_ocf,tBTM_BLE_PF_CFG_CBACK cb,uint8_t * p,uint16_t evt_len)170 void btm_flt_update_cb(uint8_t expected_ocf, tBTM_BLE_PF_CFG_CBACK cb,
171                        uint8_t* p, uint16_t evt_len) {
172   if (evt_len != 4) {
173     BTM_TRACE_ERROR("%s: bad length: %d", __func__, evt_len);
174     return;
175   }
176 
177   uint8_t status, op_subcode, action, num_avail;
178   STREAM_TO_UINT8(status, p);
179   STREAM_TO_UINT8(op_subcode, p);
180   STREAM_TO_UINT8(action, p);
181   STREAM_TO_UINT8(num_avail, p);
182 
183   if (expected_ocf != op_subcode) {
184     BTM_TRACE_ERROR("%s: Incorrect opcode: 0x%02x, expected: 0x%02x", __func__,
185                     expected_ocf, op_subcode);
186     return;
187   }
188 
189   if (op_subcode == BTM_BLE_META_PF_FEAT_SEL) {
190     cb.Run(num_avail, action, status);
191     return;
192   }
193 
194   uint8_t cond_type = btm_ble_ocf_to_condtype(expected_ocf);
195   BTM_TRACE_DEBUG("%s: Recd: %d, %d, %d, %d, %d", __func__, op_subcode,
196                   expected_ocf, action, status, num_avail);
197   if (HCI_SUCCESS == status) {
198     if (btm_ble_adv_filt_cb.cur_filter_target.bda.IsEmpty())
199       btm_ble_cs_update_pf_counter(action, cond_type, NULL, num_avail);
200     else
201       btm_ble_cs_update_pf_counter(
202           action, cond_type, &btm_ble_adv_filt_cb.cur_filter_target, num_avail);
203   }
204 
205   /* send ADV PF operation complete */
206   btm_ble_adv_filt_cb.op_type = 0;
207 
208   cb.Run(num_avail, action, status);
209 }
210 
211 /*******************************************************************************
212  *
213  * Function         btm_ble_find_addr_filter_counter
214  *
215  * Description      find the per bd address ADV payload filter counter by
216  *                  BD_ADDR.
217  *
218  * Returns          pointer to the counter if found; NULL otherwise.
219  *
220  ******************************************************************************/
btm_ble_find_addr_filter_counter(tBLE_BD_ADDR * p_le_bda)221 tBTM_BLE_PF_COUNT* btm_ble_find_addr_filter_counter(tBLE_BD_ADDR* p_le_bda) {
222   uint8_t i;
223   tBTM_BLE_PF_COUNT* p_addr_filter =
224       &btm_ble_adv_filt_cb.p_addr_filter_count[1];
225 
226   if (p_le_bda == NULL) return &btm_ble_adv_filt_cb.p_addr_filter_count[0];
227 
228   for (i = 0; i < cmn_ble_vsc_cb.max_filter; i++, p_addr_filter++) {
229     if (p_addr_filter->in_use && p_le_bda->bda == p_addr_filter->bd_addr) {
230       return p_addr_filter;
231     }
232   }
233   return NULL;
234 }
235 
236 /*******************************************************************************
237  *
238  * Function         btm_ble_alloc_addr_filter_counter
239  *
240  * Description      allocate the per device adv payload filter counter.
241  *
242  * Returns          pointer to the counter if allocation succeed; NULL
243  *                  otherwise.
244  *
245  ******************************************************************************/
btm_ble_alloc_addr_filter_counter(const RawAddress & bd_addr)246 tBTM_BLE_PF_COUNT* btm_ble_alloc_addr_filter_counter(
247     const RawAddress& bd_addr) {
248   uint8_t i;
249   tBTM_BLE_PF_COUNT* p_addr_filter =
250       &btm_ble_adv_filt_cb.p_addr_filter_count[1];
251 
252   for (i = 0; i < cmn_ble_vsc_cb.max_filter; i++, p_addr_filter++) {
253     if (p_addr_filter->bd_addr.IsEmpty()) {
254       p_addr_filter->bd_addr = bd_addr;
255       p_addr_filter->in_use = true;
256       return p_addr_filter;
257     }
258   }
259   return NULL;
260 }
261 /*******************************************************************************
262  *
263  * Function         btm_ble_dealloc_addr_filter_counter
264  *
265  * Description      de-allocate the per device adv payload filter counter.
266  *
267  * Returns          true if deallocation succeed; false otherwise.
268  *
269  ******************************************************************************/
btm_ble_dealloc_addr_filter_counter(tBLE_BD_ADDR * p_bd_addr,uint8_t filter_type)270 bool btm_ble_dealloc_addr_filter_counter(tBLE_BD_ADDR* p_bd_addr,
271                                          uint8_t filter_type) {
272   uint8_t i;
273   tBTM_BLE_PF_COUNT* p_addr_filter =
274       &btm_ble_adv_filt_cb.p_addr_filter_count[1];
275   bool found = false;
276 
277   if (BTM_BLE_PF_TYPE_ALL == filter_type && NULL == p_bd_addr)
278     memset(&btm_ble_adv_filt_cb.p_addr_filter_count[0], 0,
279            sizeof(tBTM_BLE_PF_COUNT));
280 
281   for (i = 0; i < cmn_ble_vsc_cb.max_filter; i++, p_addr_filter++) {
282     if (p_addr_filter->in_use &&
283         (!p_bd_addr || p_bd_addr->bda == p_addr_filter->bd_addr)) {
284       found = true;
285       memset(p_addr_filter, 0, sizeof(tBTM_BLE_PF_COUNT));
286 
287       if (p_bd_addr) break;
288     }
289   }
290   return found;
291 }
292 
293 /**
294  * This function update(add,delete or clear) the adv local name filtering
295  * condition.
296  */
BTM_LE_PF_local_name(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,std::vector<uint8_t> name,tBTM_BLE_PF_CFG_CBACK cb)297 void BTM_LE_PF_local_name(tBTM_BLE_SCAN_COND_OP action,
298                           tBTM_BLE_PF_FILT_INDEX filt_index,
299                           std::vector<uint8_t> name, tBTM_BLE_PF_CFG_CBACK cb) {
300   uint8_t len = BTM_BLE_ADV_FILT_META_HDR_LENGTH;
301 
302   uint8_t len_max = len + BTM_BLE_PF_STR_LEN_MAX;
303   uint8_t param[len_max];
304   memset(param, 0, len_max);
305 
306   uint8_t* p = param;
307   UINT8_TO_STREAM(p, BTM_BLE_META_PF_LOCAL_NAME);
308   UINT8_TO_STREAM(p, action);
309   UINT8_TO_STREAM(p, filt_index);
310 
311   if (action != BTM_BLE_SCAN_COND_CLEAR) {
312     int size = std::min(name.size(), (size_t)BTM_BLE_PF_STR_LEN_MAX);
313     ARRAY_TO_STREAM(p, name.data(), size);
314     len += size;
315   }
316 
317   /* send local name filter */
318   btu_hcif_send_cmd_with_cb(
319       FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param, len,
320       base::Bind(&btm_flt_update_cb, BTM_BLE_META_PF_LOCAL_NAME, cb));
321 
322   memset(&btm_ble_adv_filt_cb.cur_filter_target, 0, sizeof(tBLE_BD_ADDR));
323 }
324 
325 /**
326  * this function update(add/remove) service data change filter.
327  */
BTM_LE_PF_srvc_data(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index)328 void BTM_LE_PF_srvc_data(tBTM_BLE_SCAN_COND_OP action,
329                          tBTM_BLE_PF_FILT_INDEX filt_index) {
330   uint8_t num_avail = (action == BTM_BLE_SCAN_COND_ADD) ? 0 : 1;
331 
332   btm_ble_cs_update_pf_counter(action, BTM_BLE_PF_SRVC_DATA, nullptr,
333                                num_avail);
334 }
335 
336 /**
337  * This function update(add,delete or clear) the adv manufacturer data filtering
338  * condition.
339  */
BTM_LE_PF_manu_data(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,uint16_t company_id,uint16_t company_id_mask,std::vector<uint8_t> data,std::vector<uint8_t> data_mask,tBTM_BLE_PF_CFG_CBACK cb)340 void BTM_LE_PF_manu_data(tBTM_BLE_SCAN_COND_OP action,
341                          tBTM_BLE_PF_FILT_INDEX filt_index, uint16_t company_id,
342                          uint16_t company_id_mask, std::vector<uint8_t> data,
343                          std::vector<uint8_t> data_mask,
344                          tBTM_BLE_PF_CFG_CBACK cb) {
345   uint8_t len = BTM_BLE_ADV_FILT_META_HDR_LENGTH;
346   int len_max = len + BTM_BLE_PF_STR_LEN_MAX + BTM_BLE_PF_STR_LEN_MAX;
347 
348   uint8_t param[len_max];
349   memset(param, 0, len_max);
350 
351   uint8_t* p = param;
352   UINT8_TO_STREAM(p, BTM_BLE_META_PF_MANU_DATA);
353   UINT8_TO_STREAM(p, action);
354   UINT8_TO_STREAM(p, filt_index);
355 
356   if (action != BTM_BLE_SCAN_COND_CLEAR) {
357     uint8_t size = std::min(data.size(), (size_t)(BTM_BLE_PF_STR_LEN_MAX - 2));
358 
359     UINT16_TO_STREAM(p, company_id);
360     if (size > 0 && data_mask.size() != 0) {
361       ARRAY_TO_STREAM(p, data.data(), size);
362       len += size + 2;
363     } else
364       len += 2;
365 
366     if (company_id_mask != 0) {
367       UINT16_TO_STREAM(p, company_id_mask);
368     } else {
369       UINT16_TO_STREAM(p, (uint16_t)0xFFFF);
370     }
371     len += 2;
372 
373     if (size > 0 && data_mask.size() != 0) {
374       ARRAY_TO_STREAM(p, data_mask.data(), size);
375       len += (size);
376     }
377 
378     BTM_TRACE_DEBUG("Manuf data length: %d", len);
379   }
380 
381   btu_hcif_send_cmd_with_cb(
382       FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param, len,
383       base::Bind(&btm_flt_update_cb, BTM_BLE_META_PF_MANU_DATA, cb));
384 
385   memset(&btm_ble_adv_filt_cb.cur_filter_target, 0, sizeof(tBLE_BD_ADDR));
386 }
387 
388 /**
389  * This function update(add,delete or clear) the service data filtering
390  * condition.
391  **/
BTM_LE_PF_srvc_data_pattern(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,std::vector<uint8_t> data,std::vector<uint8_t> data_mask,tBTM_BLE_PF_CFG_CBACK cb)392 void BTM_LE_PF_srvc_data_pattern(tBTM_BLE_SCAN_COND_OP action,
393                                  tBTM_BLE_PF_FILT_INDEX filt_index,
394                                  std::vector<uint8_t> data,
395                                  std::vector<uint8_t> data_mask,
396                                  tBTM_BLE_PF_CFG_CBACK cb) {
397   uint8_t len = BTM_BLE_ADV_FILT_META_HDR_LENGTH;
398   int len_max = len + BTM_BLE_PF_STR_LEN_MAX + BTM_BLE_PF_STR_LEN_MAX;
399 
400   uint8_t param[len_max];
401   memset(param, 0, len_max);
402 
403   uint8_t* p = param;
404   UINT8_TO_STREAM(p, BTM_BLE_META_PF_SRVC_DATA);
405   UINT8_TO_STREAM(p, action);
406   UINT8_TO_STREAM(p, filt_index);
407 
408   if (action != BTM_BLE_SCAN_COND_CLEAR) {
409     uint8_t size = std::min(data.size(), (size_t)(BTM_BLE_PF_STR_LEN_MAX - 2));
410 
411     if (size > 0) {
412       ARRAY_TO_STREAM(p, data.data(), size);
413       len += size;
414       ARRAY_TO_STREAM(p, data_mask.data(), size);
415       len += size;
416     }
417   }
418 
419   btu_hcif_send_cmd_with_cb(
420       FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param, len,
421       base::Bind(&btm_flt_update_cb, BTM_BLE_META_PF_SRVC_DATA, cb));
422 
423   memset(&btm_ble_adv_filt_cb.cur_filter_target, 0, sizeof(tBLE_BD_ADDR));
424 }
425 
426 /*******************************************************************************
427  *
428  * Function         btm_ble_cs_update_pf_counter
429  *
430  * Description      this function is to update the adv data payload filter
431  *                  counter
432  *
433  * Returns          current number of the counter; BTM_BLE_INVALID_COUNTER if
434  *                  counter update failed.
435  *
436  ******************************************************************************/
btm_ble_cs_update_pf_counter(tBTM_BLE_SCAN_COND_OP action,uint8_t cond_type,tBLE_BD_ADDR * p_bd_addr,uint8_t num_available)437 uint8_t btm_ble_cs_update_pf_counter(tBTM_BLE_SCAN_COND_OP action,
438                                      uint8_t cond_type, tBLE_BD_ADDR* p_bd_addr,
439                                      uint8_t num_available) {
440   tBTM_BLE_PF_COUNT* p_addr_filter = NULL;
441   uint8_t* p_counter = NULL;
442 
443   if (cond_type > BTM_BLE_PF_TYPE_ALL) {
444     BTM_TRACE_ERROR("unknown PF filter condition type %d", cond_type);
445     return BTM_BLE_INVALID_COUNTER;
446   }
447 
448   /* for these three types of filter, always generic */
449   if (BTM_BLE_PF_ADDR_FILTER == cond_type ||
450       BTM_BLE_PF_MANU_DATA == cond_type || BTM_BLE_PF_LOCAL_NAME == cond_type ||
451       BTM_BLE_PF_SRVC_DATA_PATTERN == cond_type)
452     p_bd_addr = NULL;
453 
454   if ((p_addr_filter = btm_ble_find_addr_filter_counter(p_bd_addr)) == NULL &&
455       BTM_BLE_SCAN_COND_ADD == action) {
456     p_addr_filter = btm_ble_alloc_addr_filter_counter(p_bd_addr->bda);
457   }
458 
459   if (NULL != p_addr_filter) {
460     /* all filter just cleared */
461     if ((BTM_BLE_PF_TYPE_ALL == cond_type &&
462          BTM_BLE_SCAN_COND_CLEAR == action) ||
463         /* or bd address filter been deleted */
464         (BTM_BLE_PF_ADDR_FILTER == cond_type &&
465          (BTM_BLE_SCAN_COND_DELETE == action ||
466           BTM_BLE_SCAN_COND_CLEAR == action))) {
467       btm_ble_dealloc_addr_filter_counter(p_bd_addr, cond_type);
468     }
469     /* if not feature selection, update new addition/reduction of the filter
470        counter */
471     else if (cond_type != BTM_BLE_PF_TYPE_ALL) {
472       p_counter = p_addr_filter->pf_counter;
473       if (num_available > 0) p_counter[cond_type] += 1;
474 
475       BTM_TRACE_DEBUG("counter = %d, maxfilt = %d, num_avbl=%d",
476                       p_counter[cond_type], cmn_ble_vsc_cb.max_filter,
477                       num_available);
478       return p_counter[cond_type];
479     }
480   } else {
481     BTM_TRACE_ERROR("no matching filter counter found");
482   }
483   /* no matching filter located and updated */
484   return BTM_BLE_INVALID_COUNTER;
485 }
486 
487 /**
488  * This function updates the address filter of adv.
489  */
BTM_LE_PF_addr_filter(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,tBLE_BD_ADDR addr,tBTM_BLE_PF_CFG_CBACK cb)490 void BTM_LE_PF_addr_filter(tBTM_BLE_SCAN_COND_OP action,
491                            tBTM_BLE_PF_FILT_INDEX filt_index, tBLE_BD_ADDR addr,
492                            tBTM_BLE_PF_CFG_CBACK cb) {
493   const uint8_t len = BTM_BLE_ADV_FILT_META_HDR_LENGTH + BTM_BLE_META_ADDR_LEN;
494 
495   uint8_t param[len];
496   memset(param, 0, len);
497 
498   uint8_t* p = param;
499   UINT8_TO_STREAM(p, BTM_BLE_META_PF_ADDR);
500   UINT8_TO_STREAM(p, action);
501   UINT8_TO_STREAM(p, filt_index);
502 
503   if (action != BTM_BLE_SCAN_COND_CLEAR) {
504 #if (BLE_PRIVACY_SPT == TRUE)
505     if (addr.type == BLE_ADDR_PUBLIC_ID) {
506       LOG(INFO) << __func__ << " Filter address " << addr.bda
507                 << " has type PUBLIC_ID, try to get identity address";
508       /* If no matching identity address is found for the input address,
509        * this call will have no effect. */
510       btm_random_pseudo_to_identity_addr(&addr.bda, &addr.type);
511     }
512 #endif
513 
514     LOG(INFO) << __func__
515               << " Adding scan filter with peer address: " << addr.bda;
516 
517     BDADDR_TO_STREAM(p, addr.bda);
518     UINT8_TO_STREAM(p, addr.type);
519   }
520 
521   /* send address filter */
522   btu_hcif_send_cmd_with_cb(
523       FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param, len,
524       base::Bind(&btm_flt_update_cb, BTM_BLE_META_PF_ADDR, cb));
525 
526   memset(&btm_ble_adv_filt_cb.cur_filter_target, 0, sizeof(tBLE_BD_ADDR));
527 }
528 
529 /**
530  * This function updates(adds, deletes or clears) the service UUID filter.
531  */
BTM_LE_PF_uuid_filter(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,tBTM_BLE_PF_COND_TYPE filter_type,const bluetooth::Uuid & uuid,tBTM_BLE_PF_LOGIC_TYPE cond_logic,const bluetooth::Uuid & uuid_mask,tBTM_BLE_PF_CFG_CBACK cb)532 void BTM_LE_PF_uuid_filter(tBTM_BLE_SCAN_COND_OP action,
533                            tBTM_BLE_PF_FILT_INDEX filt_index,
534                            tBTM_BLE_PF_COND_TYPE filter_type,
535                            const bluetooth::Uuid& uuid,
536                            tBTM_BLE_PF_LOGIC_TYPE cond_logic,
537                            const bluetooth::Uuid& uuid_mask,
538                            tBTM_BLE_PF_CFG_CBACK cb) {
539   uint8_t evt_type;
540 
541   if (BTM_BLE_PF_SRVC_UUID == filter_type) {
542     evt_type = BTM_BLE_META_PF_UUID;
543   } else {
544     evt_type = BTM_BLE_META_PF_SOL_UUID;
545   }
546 
547   uint8_t len = BTM_BLE_ADV_FILT_META_HDR_LENGTH;
548   uint8_t max_len = len + BTM_BLE_META_UUID_LEN;
549   uint8_t param[max_len];
550   memset(param, 0, max_len);
551   uint8_t* p = param;
552 
553   UINT8_TO_STREAM(p, evt_type);
554   UINT8_TO_STREAM(p, action);
555   UINT8_TO_STREAM(p, filt_index);
556 
557   uint8_t uuid_len = uuid.GetShortestRepresentationSize();
558   if (action != BTM_BLE_SCAN_COND_CLEAR) {
559     if (uuid_len == Uuid::kNumBytes16) {
560       UINT16_TO_STREAM(p, uuid.As16Bit());
561       len += Uuid::kNumBytes16;
562     } else if (uuid_len == Uuid::kNumBytes32) {
563       UINT32_TO_STREAM(p, uuid.As32Bit());
564       len += Uuid::kNumBytes32;
565     } else if (uuid_len == Uuid::kNumBytes128) {
566       const auto& tmp = uuid.To128BitLE();
567       ARRAY_TO_STREAM(p, tmp.data(), (int)Uuid::kNumBytes128);
568       len += Uuid::kNumBytes128;
569     } else {
570       BTM_TRACE_ERROR("illegal UUID length: %d", uuid_len);
571       cb.Run(0, BTM_BLE_PF_CONFIG, 1 /*BTA_FAILURE*/);
572       return;
573     }
574 
575     if (!uuid_mask.IsEmpty()) {
576       if (uuid_len == Uuid::kNumBytes16) {
577         UINT16_TO_STREAM(p, uuid_mask.As16Bit());
578         len += Uuid::kNumBytes16;
579       } else if (uuid_len == Uuid::kNumBytes32) {
580         UINT32_TO_STREAM(p, uuid_mask.As32Bit());
581         len += Uuid::kNumBytes32;
582       } else if (uuid_len == Uuid::kNumBytes128) {
583         const auto& tmp = uuid_mask.To128BitLE();
584         ARRAY_TO_STREAM(p, tmp.data(), (int)Uuid::kNumBytes128);
585         len += Uuid::kNumBytes128;
586       }
587     } else {
588       memset(p, 0xff, uuid_len);
589       len += uuid_len;
590     }
591   }
592 
593   /* send UUID filter update */
594   btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param, len,
595                             base::Bind(&btm_flt_update_cb, evt_type, cb));
596   memset(&btm_ble_adv_filt_cb.cur_filter_target, 0, sizeof(tBLE_BD_ADDR));
597 }
598 
BTM_LE_PF_set(tBTM_BLE_PF_FILT_INDEX filt_index,std::vector<ApcfCommand> commands,tBTM_BLE_PF_CFG_CBACK cb)599 void BTM_LE_PF_set(tBTM_BLE_PF_FILT_INDEX filt_index,
600                    std::vector<ApcfCommand> commands,
601                    tBTM_BLE_PF_CFG_CBACK cb) {
602   if (!is_filtering_supported()) {
603     cb.Run(0, BTM_BLE_PF_ENABLE, 1 /* BTA_FAILURE */);
604     return;
605   }
606 
607   int action = BTM_BLE_SCAN_COND_ADD;
608   for (const ApcfCommand& cmd : commands) {
609     /* If data is passed, both mask and data have to be the same length */
610     if (cmd.data.size() != cmd.data_mask.size() && cmd.data.size() != 0 &&
611         cmd.data_mask.size() != 0) {
612       LOG(ERROR) << __func__ << " data(" << cmd.data.size() << ") and mask("
613                  << cmd.data_mask.size() << ") are of different size";
614       continue;
615     }
616 
617     switch (cmd.type) {
618       case BTM_BLE_PF_ADDR_FILTER: {
619         tBLE_BD_ADDR target_addr;
620         target_addr.bda = cmd.address;
621         target_addr.type = cmd.addr_type;
622 
623         BTM_LE_PF_addr_filter(action, filt_index, target_addr,
624                               base::DoNothing());
625         break;
626       }
627 
628       case BTM_BLE_PF_SRVC_DATA:
629         BTM_LE_PF_srvc_data(action, filt_index);
630         break;
631 
632       case BTM_BLE_PF_SRVC_UUID:
633       case BTM_BLE_PF_SRVC_SOL_UUID: {
634         BTM_LE_PF_uuid_filter(action, filt_index, cmd.type, cmd.uuid,
635                               BTM_BLE_PF_LOGIC_AND, cmd.uuid_mask,
636                               base::DoNothing());
637         break;
638       }
639 
640       case BTM_BLE_PF_LOCAL_NAME: {
641         BTM_LE_PF_local_name(action, filt_index, cmd.name, base::DoNothing());
642         break;
643       }
644 
645       case BTM_BLE_PF_MANU_DATA: {
646         BTM_LE_PF_manu_data(action, filt_index, cmd.company, cmd.company_mask,
647                             cmd.data, cmd.data_mask, base::DoNothing());
648         break;
649       }
650 
651       case BTM_BLE_PF_SRVC_DATA_PATTERN: {
652         BTM_LE_PF_srvc_data_pattern(action, filt_index, cmd.data, cmd.data_mask,
653                                     base::DoNothing());
654         break;
655       }
656 
657       default:
658         LOG(ERROR) << __func__ << ": Unknown filter type: " << +cmd.type;
659         break;
660     }
661   }
662   cb.Run(0, 0, 0);
663 }
664 
665 /**
666  * all adv payload filter by de-selecting all the adv pf feature bits
667  */
BTM_LE_PF_clear(tBTM_BLE_PF_FILT_INDEX filt_index,tBTM_BLE_PF_CFG_CBACK cb)668 void BTM_LE_PF_clear(tBTM_BLE_PF_FILT_INDEX filt_index,
669                      tBTM_BLE_PF_CFG_CBACK cb) {
670   if (!is_filtering_supported()) {
671     cb.Run(0, BTM_BLE_PF_ENABLE, 1 /* BTA_FAILURE */);
672     return;
673   }
674 
675   /* clear the general filter entry */
676   {
677     tBTM_BLE_PF_CFG_CBACK fDoNothing;
678 
679     /* clear manufactuer data filter */
680     BTM_LE_PF_manu_data(BTM_BLE_SCAN_COND_CLEAR, filt_index, 0, 0, {}, {},
681                         fDoNothing);
682 
683     /* clear local name filter */
684     BTM_LE_PF_local_name(BTM_BLE_SCAN_COND_CLEAR, filt_index, {}, fDoNothing);
685 
686     /* update the counter for service data */
687     BTM_LE_PF_srvc_data(BTM_BLE_SCAN_COND_CLEAR, filt_index);
688 
689     /* clear UUID filter */
690     BTM_LE_PF_uuid_filter(BTM_BLE_SCAN_COND_CLEAR, filt_index,
691                           BTM_BLE_PF_SRVC_UUID, {}, 0, Uuid::kEmpty,
692                           fDoNothing);
693 
694     BTM_LE_PF_uuid_filter(BTM_BLE_SCAN_COND_CLEAR, filt_index,
695                           BTM_BLE_PF_SRVC_SOL_UUID, {}, 0, Uuid::kEmpty,
696                           fDoNothing);
697 
698     /* clear service data filter */
699     BTM_LE_PF_srvc_data_pattern(BTM_BLE_SCAN_COND_CLEAR, filt_index, {}, {},
700                                 fDoNothing);
701   }
702 
703   uint8_t len = BTM_BLE_ADV_FILT_META_HDR_LENGTH + BTM_BLE_PF_FEAT_SEL_LEN;
704   uint8_t param[len];
705   memset(param, 0, len);
706 
707   uint8_t* p = param;
708 
709   /* select feature based on control block settings */
710   UINT8_TO_STREAM(p, BTM_BLE_META_PF_FEAT_SEL);
711   UINT8_TO_STREAM(p, BTM_BLE_SCAN_COND_CLEAR);
712   UINT8_TO_STREAM(p, filt_index);
713   /* set PCF selection */
714   UINT32_TO_STREAM(p, BTM_BLE_PF_SELECT_NONE);
715   /* set logic condition as OR as default */
716   UINT8_TO_STREAM(p, BTM_BLE_PF_LOGIC_OR);
717 
718   btu_hcif_send_cmd_with_cb(
719       FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param, len,
720       base::Bind(&btm_flt_update_cb, BTM_BLE_META_PF_FEAT_SEL, cb));
721 
722   memset(&btm_ble_adv_filt_cb.cur_filter_target, 0, sizeof(tBLE_BD_ADDR));
723 }
724 
725 /*******************************************************************************
726  *
727  * Function         BTM_BleAdvFilterParamSetup
728  *
729  * Description      This function is called to setup the adv data payload filter
730  *                  condition.
731  *
732  * Parameters       action - Type of action to be performed
733  *                       filt_index - Filter index
734  *                       p_filt_params - Filter parameters
735  *                       cb - Callback
736  *
737  ******************************************************************************/
BTM_BleAdvFilterParamSetup(int action,tBTM_BLE_PF_FILT_INDEX filt_index,std::unique_ptr<btgatt_filt_param_setup_t> p_filt_params,tBTM_BLE_PF_PARAM_CB cb)738 void BTM_BleAdvFilterParamSetup(
739     int action, tBTM_BLE_PF_FILT_INDEX filt_index,
740     std::unique_ptr<btgatt_filt_param_setup_t> p_filt_params,
741     tBTM_BLE_PF_PARAM_CB cb) {
742   tBTM_BLE_PF_COUNT* p_bda_filter = NULL;
743   uint8_t len = BTM_BLE_ADV_FILT_META_HDR_LENGTH +
744                 BTM_BLE_ADV_FILT_FEAT_SELN_LEN + BTM_BLE_ADV_FILT_TRACK_NUM;
745   uint8_t param[len], *p;
746 
747   if (!is_filtering_supported()) {
748     cb.Run(0, BTM_BLE_PF_ENABLE, 1 /* BTA_FAILURE */);
749     return;
750   }
751 
752   p = param;
753   memset(param, 0, len);
754   BTM_TRACE_EVENT("%s", __func__);
755 
756   if (BTM_BLE_SCAN_COND_ADD == action) {
757     p_bda_filter = btm_ble_find_addr_filter_counter(nullptr);
758     if (NULL == p_bda_filter) {
759       BTM_TRACE_ERROR("BD Address not found!");
760       cb.Run(0, BTM_BLE_PF_ENABLE, 1 /* BTA_FAILURE */);
761       return;
762     }
763 
764     BTM_TRACE_DEBUG("%s : Feat mask:%d", __func__, p_filt_params->feat_seln);
765     /* select feature based on control block settings */
766     UINT8_TO_STREAM(p, BTM_BLE_META_PF_FEAT_SEL);
767     UINT8_TO_STREAM(p, BTM_BLE_SCAN_COND_ADD);
768 
769     /* Filter index */
770     UINT8_TO_STREAM(p, filt_index);
771 
772     /* set PCF selection */
773     UINT16_TO_STREAM(p, p_filt_params->feat_seln);
774     /* set logic type */
775     UINT16_TO_STREAM(p, p_filt_params->list_logic_type);
776     /* set logic condition */
777     UINT8_TO_STREAM(p, p_filt_params->filt_logic_type);
778     /* set RSSI high threshold */
779     UINT8_TO_STREAM(p, p_filt_params->rssi_high_thres);
780     /* set delivery mode */
781     UINT8_TO_STREAM(p, p_filt_params->dely_mode);
782 
783     if (0x01 == p_filt_params->dely_mode) {
784       /* set onfound timeout */
785       UINT16_TO_STREAM(p, p_filt_params->found_timeout);
786       /* set onfound timeout count*/
787       UINT8_TO_STREAM(p, p_filt_params->found_timeout_cnt);
788       /* set RSSI low threshold */
789       UINT8_TO_STREAM(p, p_filt_params->rssi_low_thres);
790       /* set onlost timeout */
791       UINT16_TO_STREAM(p, p_filt_params->lost_timeout);
792       /* set num_of_track_entries for firmware greater than L-release version */
793       if (cmn_ble_vsc_cb.version_supported > BTM_VSC_CHIP_CAPABILITY_L_VERSION)
794         UINT16_TO_STREAM(p, p_filt_params->num_of_tracking_entries);
795     }
796 
797     if (cmn_ble_vsc_cb.version_supported == BTM_VSC_CHIP_CAPABILITY_L_VERSION)
798       len = BTM_BLE_ADV_FILT_META_HDR_LENGTH + BTM_BLE_ADV_FILT_FEAT_SELN_LEN;
799     else
800       len = BTM_BLE_ADV_FILT_META_HDR_LENGTH + BTM_BLE_ADV_FILT_FEAT_SELN_LEN +
801             BTM_BLE_ADV_FILT_TRACK_NUM;
802 
803     btu_hcif_send_cmd_with_cb(
804         FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param, len,
805         base::Bind(&btm_flt_update_cb, BTM_BLE_META_PF_FEAT_SEL, cb));
806   } else if (BTM_BLE_SCAN_COND_DELETE == action) {
807     /* select feature based on control block settings */
808     UINT8_TO_STREAM(p, BTM_BLE_META_PF_FEAT_SEL);
809     UINT8_TO_STREAM(p, BTM_BLE_SCAN_COND_DELETE);
810     /* Filter index */
811     UINT8_TO_STREAM(p, filt_index);
812 
813     btu_hcif_send_cmd_with_cb(
814         FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param,
815         (uint8_t)(BTM_BLE_ADV_FILT_META_HDR_LENGTH),
816         base::Bind(&btm_flt_update_cb, BTM_BLE_META_PF_FEAT_SEL, cb));
817   } else if (BTM_BLE_SCAN_COND_CLEAR == action) {
818     /* Deallocate all filters here */
819     btm_ble_dealloc_addr_filter_counter(NULL, BTM_BLE_PF_TYPE_ALL);
820 
821     /* select feature based on control block settings */
822     UINT8_TO_STREAM(p, BTM_BLE_META_PF_FEAT_SEL);
823     UINT8_TO_STREAM(p, BTM_BLE_SCAN_COND_CLEAR);
824 
825     btu_hcif_send_cmd_with_cb(
826         FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param,
827         (uint8_t)(BTM_BLE_ADV_FILT_META_HDR_LENGTH - 1),
828         base::Bind(&btm_flt_update_cb, BTM_BLE_META_PF_FEAT_SEL, cb));
829   }
830 }
831 
enable_cmpl_cback(tBTM_BLE_PF_STATUS_CBACK p_stat_cback,uint8_t * p,uint16_t evt_len)832 void enable_cmpl_cback(tBTM_BLE_PF_STATUS_CBACK p_stat_cback, uint8_t* p,
833                        uint16_t evt_len) {
834   uint8_t status, op_subcode, action;
835 
836   if (evt_len != 3) {
837     BTM_TRACE_ERROR("%s: APCF callback length = %d", __func__, evt_len);
838     return;
839   }
840 
841   STREAM_TO_UINT8(status, p);
842   STREAM_TO_UINT8(op_subcode, p);
843   STREAM_TO_UINT8(action, p);
844 
845   if (op_subcode != BTM_BLE_META_PF_ENABLE) {
846     BTM_TRACE_ERROR("%s :bad subcode: 0x%02x", __func__, op_subcode);
847     return;
848   }
849 
850   p_stat_cback.Run(action, status);
851 }
852 
853 /*******************************************************************************
854  *
855  * Function         BTM_BleEnableDisableFilterFeature
856  *
857  * Description      This function is called to enable / disable the APCF feature
858  *
859  * Parameters       enable: enable or disable the filter condition
860  *                  p_stat_cback - Status callback pointer
861  *
862  ******************************************************************************/
BTM_BleEnableDisableFilterFeature(uint8_t enable,tBTM_BLE_PF_STATUS_CBACK p_stat_cback)863 void BTM_BleEnableDisableFilterFeature(uint8_t enable,
864                                        tBTM_BLE_PF_STATUS_CBACK p_stat_cback) {
865   if (!is_filtering_supported()) {
866     if (p_stat_cback) p_stat_cback.Run(BTM_BLE_PF_ENABLE, 1 /* BTA_FAILURE */);
867     return;
868   }
869 
870   uint8_t param[20];
871   memset(param, 0, 20);
872 
873   uint8_t* p = param;
874   UINT8_TO_STREAM(p, BTM_BLE_META_PF_ENABLE);
875   UINT8_TO_STREAM(p, enable);
876 
877   btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_ADV_FILTER_OCF, param,
878                             BTM_BLE_PCF_ENABLE_LEN,
879                             base::Bind(&enable_cmpl_cback, p_stat_cback));
880 }
881 
882 /*******************************************************************************
883  *
884  * Function         btm_ble_adv_filter_init
885  *
886  * Description      This function initializes the adv filter control block
887  *
888  * Parameters
889  *
890  * Returns          status
891  *
892  ******************************************************************************/
btm_ble_adv_filter_init(void)893 void btm_ble_adv_filter_init(void) {
894   memset(&btm_ble_adv_filt_cb, 0, sizeof(tBTM_BLE_ADV_FILTER_CB));
895 
896   BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
897 
898   if (!is_filtering_supported()) return;
899 
900   if (cmn_ble_vsc_cb.max_filter > 0) {
901     btm_ble_adv_filt_cb.p_addr_filter_count = (tBTM_BLE_PF_COUNT*)osi_malloc(
902         sizeof(tBTM_BLE_PF_COUNT) * cmn_ble_vsc_cb.max_filter);
903   }
904 }
905 
906 /*******************************************************************************
907  *
908  * Function         btm_ble_adv_filter_cleanup
909  *
910  * Description      This function de-initializes the adv filter control block
911  *
912  * Parameters
913  *
914  * Returns          status
915  *
916  ******************************************************************************/
btm_ble_adv_filter_cleanup(void)917 void btm_ble_adv_filter_cleanup(void) {
918   osi_free_and_reset((void**)&btm_ble_adv_filt_cb.p_addr_filter_count);
919 }
920