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