1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "btm_int.h"
8 #include "stack/hcimsgs.h"
9 #include "osi/allocator.h"
10 #include "device/controller.h"
11 #include <string.h>
12 #include "l2c_int.h"
13 #if (BLE_50_FEATURE_SUPPORT == TRUE)
14 tBTM_BLE_EXTENDED_CB extend_adv_cb;
15 
16 tBTM_BLE_5_HCI_CBACK ble_5_hci_cb;
17 
18 #define INVALID_VALUE   0XFF
19 extern BOOLEAN BTM_GetLocalResolvablePrivateAddr(BD_ADDR bda);
20 extern void BTM_UpdateAddrInfor(uint8_t addr_type, BD_ADDR bda);
21 extern void BTM_BleSetStaticAddr(BD_ADDR rand_addr);
22 extern uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb);
23 static tBTM_STATUS btm_ble_ext_adv_params_validate(tBTM_BLE_GAP_EXT_ADV_PARAMS *params);
24 static tBTM_STATUS btm_ble_ext_adv_set_data_validate(UINT8 instance, UINT16 len, UINT8 *data);
25 
26 typedef struct {
27     uint16_t ter_con_handle;
28     bool invalid;
29     UINT8 instance;
30     int duration;
31     int max_events;
32     uint8_t retry_count;
33 } tBTM_EXT_ADV_RECORD;
34 
35 tBTM_EXT_ADV_RECORD adv_record[MAX_BLE_ADV_INSTANCE] = {0};
36 extern void btm_ble_inter_set(bool extble_inter);
37 
btm_ble_hci_status_to_str(tHCI_STATUS status)38 static char *btm_ble_hci_status_to_str(tHCI_STATUS status)
39 {
40     switch(status) {
41     case HCI_SUCCESS:
42         return "HCI_SUCCESS";
43     case HCI_ERR_ILLEGAL_COMMAND:
44         return "HCI_ERR_ILLEGAL_COMMAND";
45     case HCI_ERR_NO_CONNECTION:
46         return "HCI_ERR_NO_CONNECTION";
47     case HCI_ERR_HW_FAILURE:
48         return "HCI_ERR_HW_FAILURE";
49     case HCI_ERR_PAGE_TIMEOUT:
50         return "HCI_ERR_PAGE_TIMEOUT";
51     case HCI_ERR_AUTH_FAILURE:
52         return "HCI_ERR_AUTH_FAILURE";
53     case HCI_ERR_KEY_MISSING:
54         return "HCI_ERR_KEY_MISSING";
55     case HCI_ERR_MEMORY_FULL:
56         return "HCI_ERR_MEMORY_FULL";
57     case HCI_ERR_CONNECTION_TOUT:
58         return "HCI_ERR_CONNECTION_TOUT";
59     case HCI_ERR_MAX_NUM_OF_CONNECTIONS:
60         return "HCI_ERR_MAX_NUM_OF_CONNECTIONS";
61     case HCI_ERR_MAX_NUM_OF_SCOS:
62         return "HCI_ERR_MAX_NUM_OF_SCOS";
63     case HCI_ERR_CONNECTION_EXISTS:
64         return "HCI_ERR_CONNECTION_EXISTS";
65     case HCI_ERR_COMMAND_DISALLOWED:
66         return "HCI_ERR_COMMAND_DISALLOWED";
67     case HCI_ERR_HOST_REJECT_RESOURCES:
68         return "HCI_ERR_HOST_REJECT_RESOURCES";
69     case HCI_ERR_HOST_REJECT_SECURITY:
70         return "HCI_ERR_HOST_REJECT_SECURITY";
71     case HCI_ERR_HOST_REJECT_DEVICE:
72         return "HCI_ERR_HOST_REJECT_DEVICE";
73     case HCI_ERR_HOST_TIMEOUT:
74         return "HCI_ERR_HOST_TIMEOUT";
75     case HCI_ERR_UNSUPPORTED_VALUE:
76         return "HCI_ERR_UNSUPPORTED_VALUE";
77     case HCI_ERR_ILLEGAL_PARAMETER_FMT:
78         return "HCI_ERR_ILLEGAL_PARAMETER_FMT";
79     case HCI_ERR_PEER_USER:
80         return "HCI_ERR_PEER_USER";
81     case HCI_ERR_PEER_LOW_RESOURCES:
82         return "HCI_ERR_PEER_LOW_RESOURCES";
83     case HCI_ERR_PEER_POWER_OFF:
84         return "HCI_ERR_PEER_POWER_OFF";
85     case HCI_ERR_CONN_CAUSE_LOCAL_HOST:
86         return "HCI_ERR_CONN_CAUSE_LOCAL_HOST";
87     case HCI_ERR_REPEATED_ATTEMPTS:
88         return "HCI_ERR_REPEATED_ATTEMPTS";
89     case HCI_ERR_PAIRING_NOT_ALLOWED:
90         return "HCI_ERR_PAIRING_NOT_ALLOWED";
91     case HCI_ERR_UNKNOWN_LMP_PDU:
92         return "HCI_ERR_UNKNOWN_LMP_PDU";
93     case HCI_ERR_UNSUPPORTED_REM_FEATURE:
94         return "HCI_ERR_UNSUPPORTED_REM_FEATURE";
95     case HCI_ERR_SCO_OFFSET_REJECTED:
96         return "HCI_ERR_SCO_OFFSET_REJECTED";
97     case HCI_ERR_SCO_INTERVAL_REJECTED:
98         return "HCI_ERR_SCO_INTERVAL_REJECTED";
99     case HCI_ERR_SCO_AIR_MODE:
100         return "HCI_ERR_SCO_AIR_MODE";
101     case HCI_ERR_INVALID_LMP_PARAM:
102         return "HCI_ERR_INVALID_LMP_PARAM";
103     case HCI_ERR_UNSPECIFIED:
104         return "HCI_ERR_UNSPECIFIED";
105     case HCI_ERR_UNSUPPORTED_LMP_PARAMETERS:
106         return "HCI_ERR_UNSUPPORTED_LMP_PARAMETERS";
107     case HCI_ERR_ROLE_CHANGE_NOT_ALLOWED:
108         return "HCI_ERR_ROLE_CHANGE_NOT_ALLOWED";
109     case HCI_ERR_LMP_RESPONSE_TIMEOUT:
110         return "HCI_ERR_LMP_RESPONSE_TIMEOUT";
111     case HCI_ERR_LMP_ERR_TRANS_COLLISION:
112         return "HCI_ERR_LMP_ERR_TRANS_COLLISION";
113     case HCI_ERR_LMP_PDU_NOT_ALLOWED:
114         return "HCI_ERR_LMP_PDU_NOT_ALLOWED";
115     case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
116         return "HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE";
117     case HCI_ERR_UNIT_KEY_USED:
118         return "HCI_ERR_UNIT_KEY_USED";
119     case HCI_ERR_QOS_NOT_SUPPORTED:
120         return "HCI_ERR_QOS_NOT_SUPPORTED";
121     case HCI_ERR_INSTANT_PASSED:
122         return "HCI_ERR_INSTANT_PASSED";
123     case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
124         return "HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED";
125     case HCI_ERR_DIFF_TRANSACTION_COLLISION:
126         return "HCI_ERR_DIFF_TRANSACTION_COLLISION";
127     case HCI_ERR_UNDEFINED_0x2B:
128         return "HCI_ERR_UNDEFINED_0x2B";
129     case HCI_ERR_QOS_UNACCEPTABLE_PARAM:
130         return "HCI_ERR_QOS_UNACCEPTABLE_PARAM";
131     case HCI_ERR_QOS_REJECTED:
132         return "HCI_ERR_QOS_REJECTED";
133     case HCI_ERR_CHAN_CLASSIF_NOT_SUPPORTED:
134         return "HCI_ERR_CHAN_CLASSIF_NOT_SUPPORTED";
135     case HCI_ERR_INSUFFCIENT_SECURITY:
136         return "HCI_ERR_INSUFFCIENT_SECURITY";
137     case HCI_ERR_PARAM_OUT_OF_RANGE:
138         return "HCI_ERR_PARAM_OUT_OF_RANGE";
139     case HCI_ERR_UNDEFINED_0x31:
140         return "HCI_ERR_UNDEFINED_0x31";
141     case HCI_ERR_ROLE_SWITCH_PENDING:
142         return "HCI_ERR_ROLE_SWITCH_PENDING";
143     case HCI_ERR_UNDEFINED_0x33:
144         return "HCI_ERR_UNDEFINED_0x33";
145     case HCI_ERR_RESERVED_SLOT_VIOLATION:
146         return "HCI_ERR_RESERVED_SLOT_VIOLATION";
147     case HCI_ERR_ROLE_SWITCH_FAILED:
148         return "HCI_ERR_ROLE_SWITCH_FAILED";
149     case HCI_ERR_INQ_RSP_DATA_TOO_LARGE:
150         return "HCI_ERR_INQ_RSP_DATA_TOO_LARGE";
151     case HCI_ERR_SIMPLE_PAIRING_NOT_SUPPORTED:
152         return "HCI_ERR_SIMPLE_PAIRING_NOT_SUPPORTED";
153     case HCI_ERR_HOST_BUSY_PAIRING:
154         return "HCI_ERR_HOST_BUSY_PAIRING";
155     case HCI_ERR_REJ_NO_SUITABLE_CHANNEL:
156         return "HCI_ERR_REJ_NO_SUITABLE_CHANNEL";
157     case HCI_ERR_CONTROLLER_BUSY:
158         return "HCI_ERR_CONTROLLER_BUSY";
159     case HCI_ERR_UNACCEPT_CONN_INTERVAL:
160         return "HCI_ERR_UNACCEPT_CONN_INTERVAL";
161     case HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT:
162         return "HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT";
163     case HCI_ERR_CONN_TOUT_DUE_TO_MIC_FAILURE:
164         return "HCI_ERR_CONN_TOUT_DUE_TO_MIC_FAILURE";
165     case HCI_ERR_CONN_FAILED_ESTABLISHMENT:
166         return "HCI_ERR_CONN_FAILED_ESTABLISHMENT";
167     case HCI_ERR_MAC_CONNECTION_FAILED:
168         return "HCI_ERR_MAC_CONNECTION_FAILED";
169     case HCI_ERR_LT_ADDR_ALREADY_IN_USE:
170         return "HCI_ERR_LT_ADDR_ALREADY_IN_USE";
171     case HCI_ERR_LT_ADDR_NOT_ALLOCATED:
172         return "HCI_ERR_LT_ADDR_NOT_ALLOCATED";
173     case HCI_ERR_CLB_NOT_ENABLED:
174         return "HCI_ERR_CLB_NOT_ENABLED";
175     case HCI_ERR_MAX_ERR:
176         return "HCI_ERR_MAX_ERR";
177     case HCI_ERR_ESP_VENDOR_FAIL:
178         return "HCI_ERR_ESP_VENDOR_FAIL";
179     case HCI_HINT_TO_RECREATE_AMP_PHYS_LINK:
180         return "HCI_HINT_TO_RECREATE_AMP_PHYS_LINK";
181     default:
182         return "Invalid HCI status code.";
183     }
184 
185     return NULL;
186 }
187 
btm_ble_extendadvcb_init(void)188 void btm_ble_extendadvcb_init(void)
189 {
190     memset(&extend_adv_cb, 0, sizeof(tBTM_BLE_EXTENDED_CB));
191 }
192 
btm_ble_advrecod_init(void)193 void btm_ble_advrecod_init(void)
194 {
195     memset(&adv_record[0], 0, sizeof(tBTM_EXT_ADV_RECORD)*MAX_BLE_ADV_INSTANCE);
196 }
197 
BTM_BleGapRegisterCallback(tBTM_BLE_5_HCI_CBACK cb)198 void BTM_BleGapRegisterCallback(tBTM_BLE_5_HCI_CBACK cb)
199 {
200     if (cb) {
201         ble_5_hci_cb = cb;
202     } else {
203         BTM_TRACE_ERROR("%s, register fail, the cb function is NULL.", __func__);
204     }
205 }
206 
BTM_ExtBleCallbackTrigger(tBTM_BLE_5_GAP_EVENT event,tBTM_BLE_5_GAP_CB_PARAMS * params)207 void BTM_ExtBleCallbackTrigger(tBTM_BLE_5_GAP_EVENT event, tBTM_BLE_5_GAP_CB_PARAMS *params)
208 {
209     if(params && params->status == BTM_SUCCESS) {
210         btm_ble_inter_set(true);
211     }
212     if (ble_5_hci_cb) {
213         ble_5_hci_cb(event, params);
214     }
215 }
216 
BTM_BleReadPhy(BD_ADDR bd_addr,UINT8 * tx_phy,UINT8 * rx_phy)217 tBTM_STATUS BTM_BleReadPhy(BD_ADDR bd_addr, UINT8 *tx_phy, UINT8 *rx_phy)
218 {
219     tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
220     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
221 
222     if (!tx_phy || !rx_phy || !p_lcb) {
223         cb_params.read_phy.status = BTM_ILLEGAL_VALUE;
224         memcpy(cb_params.read_phy.addr, bd_addr, BD_ADDR_LEN);
225 
226         if (ble_5_hci_cb) {
227             ble_5_hci_cb(BTM_BLE_5_GAP_READ_PHY_COMPLETE_EVT, &cb_params);
228         }
229         BTM_TRACE_ERROR("%s, invalid parameters", __func__);
230         return BTM_ILLEGAL_VALUE;
231     }
232 
233     btsnd_hcic_ble_read_phy(p_lcb->handle);
234 
235     return BTM_SUCCESS;
236 }
237 
BTM_BleSetPreferDefaultPhy(UINT8 tx_phy_mask,UINT8 rx_phy_mask)238 tBTM_STATUS BTM_BleSetPreferDefaultPhy(UINT8 tx_phy_mask, UINT8 rx_phy_mask)
239 {
240     UINT8 all_phys = 0;
241     tBTM_STATUS status = BTM_SUCCESS;
242     tHCI_STATUS err = HCI_SUCCESS;
243     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
244 
245     if ((err = btsnd_hcic_ble_set_prefered_default_phy(all_phys, tx_phy_mask, rx_phy_mask)) != HCI_SUCCESS) {
246         BTM_TRACE_ERROR("%s, fail to send the hci command, the error code = %s(0x%x)",
247                         __func__, btm_ble_hci_status_to_str(err), err);
248         status = BTM_ILLEGAL_VALUE;
249     }
250 
251     cb_params.set_perf_def_phy.status = err;
252 
253     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_PREFERED_DEFAULT_PHY_COMPLETE_EVT, &cb_params);
254 
255     return status;
256 
257 }
258 
BTM_BleSetPreferPhy(BD_ADDR bd_addr,UINT8 all_phys,UINT8 tx_phy_mask,UINT8 rx_phy_mask,UINT16 phy_options)259 tBTM_STATUS BTM_BleSetPreferPhy(BD_ADDR bd_addr, UINT8 all_phys, UINT8 tx_phy_mask,
260                                            UINT8 rx_phy_mask, UINT16 phy_options)
261 {
262     tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
263     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
264 
265     if (!p_lcb) {
266         cb_params.status = BTM_ILLEGAL_VALUE;
267         if (ble_5_hci_cb) {
268             ble_5_hci_cb(BTM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT, &cb_params);
269         }
270         BTM_TRACE_ERROR("%s, invalid parameters", __func__);
271         return BTM_ILLEGAL_VALUE;
272     }
273 
274 
275 
276     if (!btsnd_hcic_ble_set_phy(p_lcb->handle, all_phys, tx_phy_mask, rx_phy_mask, phy_options)) {
277         cb_params.status = BTM_ILLEGAL_VALUE;
278         BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT, &cb_params);
279     }
280 
281 
282     return BTM_SUCCESS;
283 }
284 
BTM_BleSetExtendedAdvRandaddr(UINT8 instance,BD_ADDR rand_addr)285 tBTM_STATUS BTM_BleSetExtendedAdvRandaddr(UINT8 instance, BD_ADDR rand_addr)
286 {
287     tBTM_STATUS status = BTM_SUCCESS;
288     tHCI_STATUS err = HCI_SUCCESS;
289     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
290 
291     if (instance >= MAX_BLE_ADV_INSTANCE || rand_addr == NULL) {
292         status = BTM_ILLEGAL_VALUE;
293         goto end;
294     }
295 
296     /*
297         A static address is a 48-bit randomly generated address and shall meet the following requirements:
298         • The two most significant bits of the address shall be equal to 1
299         • All bits of the random part of the address shall not be equal to 1
300         • All bits of the random part of the address shall not be equal to 0
301     */
302     BD_ADDR invalid_rand_addr_a, invalid_rand_addr_b;
303     memset(invalid_rand_addr_a, 0xff, sizeof(BD_ADDR));
304     memset(invalid_rand_addr_b, 0x00, sizeof(BD_ADDR));
305     if((rand_addr[0] & BT_STATIC_RAND_ADDR_MASK) == BT_STATIC_RAND_ADDR_MASK) {
306         invalid_rand_addr_b[0] = invalid_rand_addr_b[0] | BT_STATIC_RAND_ADDR_MASK;
307         if (memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) == 0
308         || memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) == 0) {
309             status = BTM_ILLEGAL_VALUE;
310             goto end;
311         }
312     } else if ((rand_addr[0] | BT_NON_RPA_MASK) == BT_NON_RPA_MASK) {
313         invalid_rand_addr_a[0] = invalid_rand_addr_a[0] & BT_NON_RPA_MASK;
314         if (memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) == 0
315         || memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) == 0) {
316             status = BTM_ILLEGAL_VALUE;
317             goto end;
318         }
319     } else {
320         BTM_TRACE_ERROR("%s invalid random address", __func__);
321         status = BTM_ILLEGAL_VALUE;
322         goto end;
323     }
324 
325     // set random address
326     if((err = btsnd_hcic_ble_set_extend_rand_address(instance, rand_addr)) != HCI_SUCCESS) {
327         BTM_TRACE_ERROR("%s, fail to send the hci command, the error code = %s(0x%x)",
328                         __func__, btm_ble_hci_status_to_str(err), err);
329         status = BTM_ILLEGAL_VALUE;
330     } else {
331         // set random address success, update address infor
332         if(extend_adv_cb.inst[instance].configured && extend_adv_cb.inst[instance].connetable) {
333             BTM_BleSetStaticAddr(rand_addr);
334             BTM_UpdateAddrInfor(BLE_ADDR_RANDOM, rand_addr);
335         }
336     }
337 
338 end:
339     cb_params.status = status;
340 
341     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, &cb_params);
342 
343     return status;
344 
345 }
BTM_BleSetExtendedAdvParams(UINT8 instance,tBTM_BLE_GAP_EXT_ADV_PARAMS * params)346 tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PARAMS *params)
347 {
348     tBTM_STATUS status = BTM_SUCCESS;
349     tHCI_STATUS err = HCI_SUCCESS;
350     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
351     bool use_rpa_addr = false;
352     BD_ADDR rand_addr;
353 
354     if (instance >= MAX_BLE_ADV_INSTANCE) {
355         status = BTM_ILLEGAL_VALUE;
356          BTM_TRACE_ERROR("%s invalid instance %d", __func__, instance);
357         goto end;
358     }
359 
360     if ((status = btm_ble_ext_adv_params_validate(params)) != BTM_SUCCESS) {
361         BTM_TRACE_ERROR("%s, invalid extend adv params.", __func__);
362     }
363 
364     if (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_CONNECTABLE) {
365         extend_adv_cb.inst[instance].connetable = true;
366     } else {
367         extend_adv_cb.inst[instance].connetable = false;
368     }
369 
370     if (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_SCANNABLE) {
371         extend_adv_cb.inst[instance].scannable = true;
372     } else {
373         extend_adv_cb.inst[instance].scannable = false;
374     }
375 
376     if (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_LEGACY) {
377         extend_adv_cb.inst[instance].legacy_pdu = true;
378     } else {
379         extend_adv_cb.inst[instance].legacy_pdu = false;
380     }
381 
382 #if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
383     // if own_addr_type == BLE_ADDR_PUBLIC_ID or BLE_ADDR_RANDOM_ID,
384     if((params->own_addr_type == BLE_ADDR_PUBLIC_ID || params->own_addr_type == BLE_ADDR_RANDOM_ID) && BTM_GetLocalResolvablePrivateAddr(rand_addr)) {
385         params->own_addr_type = BLE_ADDR_RANDOM;
386         use_rpa_addr = true;
387     } else if(params->own_addr_type == BLE_ADDR_PUBLIC_ID){
388         params->own_addr_type = BLE_ADDR_PUBLIC;
389     } else if (params->own_addr_type == BLE_ADDR_RANDOM_ID) {
390         params->own_addr_type = BLE_ADDR_RANDOM;
391     }
392 #else
393     btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = params->own_addr_type;
394 #endif
395 
396     if ((err = btsnd_hcic_ble_set_ext_adv_params(instance, params->type, params->interval_min, params->interval_max,
397                                       params->channel_map, params->own_addr_type, params->peer_addr_type,
398                                       params->peer_addr, params->filter_policy, params->tx_power,
399                                       params->primary_phy, params->max_skip,
400                                       params->secondary_phy, params->sid, params->scan_req_notif)) != HCI_SUCCESS) {
401         BTM_TRACE_ERROR("LE EA SetParams: cmd err=0x%x", err);
402         status = BTM_ILLEGAL_VALUE;
403         goto end;
404     }
405 
406     extend_adv_cb.inst[instance].configured = true;
407 
408 end:
409     if(use_rpa_addr) {
410         // update RPA address
411         if((err = btsnd_hcic_ble_set_extend_rand_address(instance, rand_addr)) != HCI_SUCCESS) {
412             BTM_TRACE_ERROR("LE EA SetParams: cmd err=0x%x", err);
413             status = BTM_ILLEGAL_VALUE;
414         } else {
415             // set addr success, update address infor
416             BTM_UpdateAddrInfor(BLE_ADDR_RANDOM, rand_addr);
417         }
418     }
419     cb_params.status = status;
420     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_SET_PARAMS_COMPLETE_EVT, &cb_params);
421 
422     return status;
423 }
424 
BTM_BleConfigExtendedAdvDataRaw(BOOLEAN is_scan_rsp,UINT8 instance,UINT16 len,UINT8 * data)425 tBTM_STATUS BTM_BleConfigExtendedAdvDataRaw(BOOLEAN is_scan_rsp, UINT8 instance, UINT16 len, UINT8 *data)
426 {
427     tBTM_STATUS status = BTM_SUCCESS;
428     tHCI_STATUS err = HCI_SUCCESS;
429     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
430     UINT16 rem_len = len;
431     UINT8 operation = 0;
432     UINT16 data_offset = 0;
433 
434 
435     if ((status = btm_ble_ext_adv_set_data_validate(instance, len, data)) != BTM_SUCCESS) {
436         BTM_TRACE_ERROR("%s, invalid extend adv data.", __func__);
437         goto end;
438     }
439 
440     do {
441         UINT8 send_data_len = (rem_len > BTM_BLE_EXT_ADV_DATA_LEN_MAX) ? BTM_BLE_EXT_ADV_DATA_LEN_MAX : rem_len;
442         if (len <= BTM_BLE_EXT_ADV_DATA_LEN_MAX) {
443             operation = BTM_BLE_ADV_DATA_OP_COMPLETE;
444         } else {
445             if (rem_len == len) {
446                 operation = BTM_BLE_ADV_DATA_OP_FIRST_FRAG;
447             } else if (rem_len <= BTM_BLE_EXT_ADV_DATA_LEN_MAX) {
448                 operation = BTM_BLE_ADV_DATA_OP_LAST_FRAG;
449             } else {
450 	        operation = BTM_BLE_ADV_DATA_OP_INTERMEDIATE_FRAG;
451 	    }
452         }
453         if (!is_scan_rsp) {
454             if ((err = btsnd_hcic_ble_set_ext_adv_data(instance, operation, 0, send_data_len, &data[data_offset])) != HCI_SUCCESS) {
455                 BTM_TRACE_ERROR("LE EA SetAdvData: cmd err=0x%x", err);
456                 status = BTM_ILLEGAL_VALUE;
457             }
458         } else {
459             if ((err = btsnd_hcic_ble_set_ext_adv_scan_rsp_data(instance, operation, 0, send_data_len, &data[data_offset])) != HCI_SUCCESS) {
460                 BTM_TRACE_ERROR("LE EA SetScanRspData: cmd err=0x%x", err);
461                 status = BTM_ILLEGAL_VALUE;
462             }
463         }
464 
465         rem_len -= send_data_len;
466 	data_offset += send_data_len;
467     } while (rem_len);
468 
469 end:
470     cb_params.status = status;
471     BTM_ExtBleCallbackTrigger(is_scan_rsp ? BTM_BLE_5_GAP_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_ADV_DATA_SET_COMPLETE_EVT, &cb_params);
472 
473     return status;
474 }
475 
BTM_BleStartExtAdv(BOOLEAN enable,UINT8 num,tBTM_BLE_EXT_ADV * ext_adv)476 tBTM_STATUS BTM_BleStartExtAdv(BOOLEAN enable, UINT8 num, tBTM_BLE_EXT_ADV *ext_adv)
477 {
478     tBTM_STATUS status = BTM_SUCCESS;
479     tHCI_STATUS err = HCI_SUCCESS;
480     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
481 
482     UINT8 *instance = NULL;
483     UINT16 *duration = NULL;
484     UINT8 *max_events = NULL;
485 
486     // when enable = true, ext_adv = NULL or num = 0, goto end
487     if ((!ext_adv || num == 0) && enable) {
488         status = BTM_ILLEGAL_VALUE;
489         BTM_TRACE_ERROR("%s invalid parameters", __func__);
490         goto end;
491     }
492 
493     if (num != 0 && ext_adv != NULL) {
494         instance = osi_malloc(num);
495         duration = osi_malloc(num*sizeof(UINT16));
496         max_events = osi_malloc(num*sizeof(UINT8));
497 
498         if (!instance || !duration || !max_events) {
499             status = BTM_NO_RESOURCES;
500             BTM_TRACE_ERROR("%s invalid parameters", __func__);
501             goto end;
502         }
503 
504         for (int i = 0; i < num; i++) {
505             instance[i] = ext_adv[i].instance;
506             duration[i] = ext_adv[i].duration;
507             max_events[i] = ext_adv[i].max_events;
508         }
509 
510         if ((err = btsnd_hcic_ble_ext_adv_enable(enable, num, instance,
511                                       duration, max_events)) != HCI_SUCCESS) {
512             BTM_TRACE_ERROR("LE EA En=%d: cmd err=0x%x", enable, err);
513             status = BTM_ILLEGAL_VALUE;
514         }
515 
516         osi_free(instance);
517         osi_free(duration);
518         osi_free(max_events);
519     } else {
520         // enable = false, num == 0 or ext_adv = NULL
521 
522         if ((err = btsnd_hcic_ble_ext_adv_enable(enable, num, NULL, NULL, NULL)) != HCI_SUCCESS) {
523             BTM_TRACE_ERROR("LE EA En=%d: cmd err=0x%x", enable, err);
524             status = BTM_ILLEGAL_VALUE;
525         }
526         goto end;
527     }
528 
529 
530 
531 end:
532 
533     if (!enable && status == BTM_SUCCESS) {
534         // disable all ext adv
535         if(num == 0) {
536 
537             for (uint8_t i = 0; i < MAX_BLE_ADV_INSTANCE; i++)
538             {
539                 adv_record[i].invalid = false;
540                 adv_record[i].instance = INVALID_VALUE;
541                 adv_record[i].duration = INVALID_VALUE;
542                 adv_record[i].max_events = INVALID_VALUE;
543                 adv_record[i].retry_count = 0;
544             }
545         } else {
546             for (uint8_t i = 0; i < num; i++)
547             {
548                 uint8_t index = ext_adv[i].instance;
549                 adv_record[index].invalid = false;
550                 adv_record[index].instance = INVALID_VALUE;
551                 adv_record[index].duration = INVALID_VALUE;
552                 adv_record[index].max_events = INVALID_VALUE;
553                 adv_record[index].retry_count = 0;
554             }
555         }
556     }
557     // start extend adv success, save the adv information
558     if(enable && status == BTM_SUCCESS) {
559         for (uint8_t i = 0; i < num; i++)
560         {
561             uint8_t index = ext_adv[i].instance;
562             adv_record[index].invalid = true;
563             adv_record[index].instance = ext_adv[i].instance;
564             adv_record[index].duration = ext_adv[i].duration;
565             adv_record[index].max_events = ext_adv[i].max_events;
566             adv_record[index].retry_count = 0;
567         }
568     }
569 
570     cb_params.status = status;
571     BTM_ExtBleCallbackTrigger(enable ? BTM_BLE_5_GAP_EXT_ADV_START_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_ADV_STOP_COMPLETE_EVT, &cb_params);
572 
573     return status;
574 }
575 
BTM_BleStartExtAdvRestart(uint8_t con_handle)576 tBTM_STATUS BTM_BleStartExtAdvRestart(uint8_t con_handle)
577 {
578    tBTM_BLE_EXT_ADV ext_adv;
579    uint8_t index = INVALID_VALUE;
580    for (uint8_t i = 0; i < MAX_BLE_ADV_INSTANCE; i++)
581    {
582        if(adv_record[i].ter_con_handle == con_handle) {
583            index = i;
584            break;
585        }
586     }
587 
588     if((index >= MAX_BLE_ADV_INSTANCE) || (!adv_record[index].invalid) || (adv_record[index].retry_count > GATTC_CONNECT_RETRY_COUNT)) {
589         return BTM_WRONG_MODE;
590     }
591 
592     adv_record[index].retry_count ++;
593     BTM_TRACE_DEBUG("remote device did not reveive aux connect response, retatrt the extend adv to reconnect, adv handle %d con_handle %d\n", index, con_handle);
594     ext_adv.instance = adv_record[index].instance;
595     ext_adv.duration = adv_record[index].duration;
596     ext_adv.max_events = adv_record[index].max_events;
597     return BTM_BleStartExtAdv(true, 1, &ext_adv);
598 }
599 
BTM_BleExtAdvSetRemove(UINT8 instance)600 tBTM_STATUS BTM_BleExtAdvSetRemove(UINT8 instance)
601 {
602     tBTM_STATUS status = BTM_SUCCESS;
603     tHCI_STATUS err = HCI_SUCCESS;
604     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
605 
606     if (instance >= MAX_BLE_ADV_INSTANCE) {
607         status = BTM_ILLEGAL_VALUE;
608         BTM_TRACE_ERROR("%s invalid instance %d", __func__, instance);
609         goto end;
610     }
611 
612     if ((err = btsnd_hcic_ble_remove_adv_set(instance)) != HCI_SUCCESS) {
613         BTM_TRACE_ERROR("LE EAS Rm: cmd err=0x%x", err);
614         status = BTM_ILLEGAL_VALUE;
615     } else {
616         extend_adv_cb.inst[instance].configured = false;
617         extend_adv_cb.inst[instance].legacy_pdu = false;
618         extend_adv_cb.inst[instance].directed = false;
619         extend_adv_cb.inst[instance].scannable = false;
620         extend_adv_cb.inst[instance].connetable = false;
621     }
622 
623 end:
624 
625     cb_params.status = status;
626 
627     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_SET_REMOVE_COMPLETE_EVT, &cb_params);
628 
629     return status;
630 }
631 
BTM_BleExtAdvSetClear(void)632 tBTM_STATUS BTM_BleExtAdvSetClear(void)
633 {
634     tBTM_STATUS status = BTM_SUCCESS;
635     tHCI_STATUS err = HCI_SUCCESS;
636     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
637 
638     if ((err = btsnd_hcic_ble_clear_adv_set()) != HCI_SUCCESS) {
639         BTM_TRACE_ERROR("LE EAS Clr: cmd err=0x%x", err);
640         status = BTM_ILLEGAL_VALUE;
641     } else {
642         for (uint8_t i = 0; i < MAX_BLE_ADV_INSTANCE; i++) {
643             extend_adv_cb.inst[i].configured = false;
644             extend_adv_cb.inst[i].legacy_pdu = false;
645             extend_adv_cb.inst[i].directed = false;
646             extend_adv_cb.inst[i].scannable = false;
647             extend_adv_cb.inst[i].connetable = false;
648         }
649     }
650 
651     cb_params.status = status;
652 
653     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_SET_CLEAR_COMPLETE_EVT, &cb_params);
654 
655     return status;
656 }
657 
BTM_BlePeriodicAdvSetParams(UINT8 instance,tBTM_BLE_Periodic_Adv_Params * params)658 tBTM_STATUS BTM_BlePeriodicAdvSetParams(UINT8 instance, tBTM_BLE_Periodic_Adv_Params *params)
659 {
660     tBTM_STATUS status = BTM_SUCCESS;
661     tHCI_STATUS err = HCI_SUCCESS;
662     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
663     //ext_adv_flag = true;
664 
665     if (instance >= MAX_BLE_ADV_INSTANCE) {
666         status = BTM_ILLEGAL_VALUE;
667         BTM_TRACE_ERROR("%s invalid instance %d", __func__, instance);
668         goto end;
669     }
670 
671     if (!extend_adv_cb.inst[instance].configured ||
672         extend_adv_cb.inst[instance].scannable ||
673         extend_adv_cb.inst[instance].connetable ||
674         extend_adv_cb.inst[instance].legacy_pdu) {
675         BTM_TRACE_ERROR("%s, instance = %d, Before set the periodic adv parameters, please configure the the \
676                 extend adv to nonscannable and nonconnectable first, and it shouldn't include the legacy bit.", __func__, instance);
677         status = BTM_ILLEGAL_VALUE;
678         goto end;
679     }
680 
681     if ((err= btsnd_hcic_ble_set_periodic_adv_params(instance, params->interval_min,
682                                                params->interval_max, params->properties)) != HCI_SUCCESS) {
683         BTM_TRACE_ERROR("LE PA SetParams: cmd err=0x%x", err);
684         status = BTM_ILLEGAL_VALUE;
685     }
686 
687 end:
688 
689     cb_params.status = status;
690 
691     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SET_PARAMS_COMPLETE_EVT, &cb_params);
692 
693     return status;
694 }
695 
BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance,UINT16 len,UINT8 * data)696 tBTM_STATUS BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance, UINT16 len, UINT8 *data)
697 {
698     tBTM_STATUS status = BTM_SUCCESS;
699     tHCI_STATUS err = HCI_SUCCESS;
700     uint16_t rem_len = len;
701     UINT8 operation = 0;
702     UINT16 data_offset = 0;
703     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
704 
705     if ((status = btm_ble_ext_adv_set_data_validate(instance, len, data)) != BTM_SUCCESS) {
706        BTM_TRACE_ERROR("%s, invalid extend adv data.", __func__);
707        goto end;
708     }
709 
710     do {
711         UINT8 send_data_len = (rem_len > BTM_BLE_PERIODIC_ADV_DATA_LEN_MAX) ? BTM_BLE_PERIODIC_ADV_DATA_LEN_MAX : rem_len;
712 
713         if (len <= BTM_BLE_EXT_ADV_DATA_LEN_MAX) {
714             operation = BTM_BLE_ADV_DATA_OP_COMPLETE;
715         } else {
716             if (rem_len == len) {
717                 operation = BTM_BLE_ADV_DATA_OP_FIRST_FRAG;
718             } else if (rem_len <= BTM_BLE_EXT_ADV_DATA_LEN_MAX) {
719                 operation = BTM_BLE_ADV_DATA_OP_LAST_FRAG;
720             } else {
721 	        operation = BTM_BLE_ADV_DATA_OP_INTERMEDIATE_FRAG;
722 	    }
723         }
724 
725         if ((err = btsnd_hcic_ble_set_periodic_adv_data(instance, operation, send_data_len, &data[data_offset])) != HCI_SUCCESS) {
726             BTM_TRACE_ERROR("LE PA SetData: cmd err=0x%x", err);
727             status = BTM_ILLEGAL_VALUE;
728         }
729         rem_len -= send_data_len;
730 	data_offset += send_data_len;
731     } while(rem_len);
732 
733 end:
734     cb_params.status = status;
735     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_DATA_SET_COMPLETE_EVT, &cb_params);
736 
737     return status;
738 }
739 
BTM_BlePeriodicAdvEnable(UINT8 instance,BOOLEAN enable)740 tBTM_STATUS BTM_BlePeriodicAdvEnable(UINT8 instance, BOOLEAN enable)
741 {
742     tBTM_STATUS status = BTM_SUCCESS;
743     tHCI_STATUS err = HCI_SUCCESS;
744     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
745 
746     if (instance >= MAX_BLE_ADV_INSTANCE) {
747         BTM_TRACE_ERROR("%s, invalid instance %d", __func__, instance);
748         status = BTM_ILLEGAL_VALUE;
749         goto end;
750     }
751 
752     if ((err = btsnd_hcic_ble_periodic_adv_enable(enable, instance)) != HCI_SUCCESS) {
753         BTM_TRACE_ERROR("LE PA En=%d: cmd err=0x%x", enable, err);
754         status = BTM_ILLEGAL_VALUE;
755     }
756 
757 end:
758 
759     cb_params.status = status;
760 
761     BTM_ExtBleCallbackTrigger(enable ? BTM_BLE_5_GAP_PERIODIC_ADV_START_COMPLETE_EVT : BTM_BLE_5_GAP_PERIODIC_ADV_STOP_COMPLETE_EVT, &cb_params);
762 
763     return status;
764 
765 }
766 
BTM_BlePeriodicAdvCreateSync(tBTM_BLE_Periodic_Sync_Params * params)767 tBTM_STATUS BTM_BlePeriodicAdvCreateSync(tBTM_BLE_Periodic_Sync_Params *params)
768 {
769     //tHCI_STATUS err = HCI_SUCCESS;
770     tBTM_STATUS status = BTM_SUCCESS;
771     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
772 
773     if (!params) {
774         status = BTM_ILLEGAL_VALUE;
775         BTM_TRACE_ERROR("%s, the parameter is NULL.", __func__);
776         goto end;
777     }
778 
779     if ((params->sync_timeout < 0x0a || params->sync_timeout > 0x4000)
780         || (params->filter_policy > 0x01) || (params->addr_type > 0x01) ||
781         (params->sid > 0xf) || (params->skip > 0x01F3)) {
782             status = BTM_ILLEGAL_VALUE;
783             BTM_TRACE_ERROR("%s, The sync parameters is invalid.", __func__);
784             goto end;
785     }
786 
787     if (!btsnd_hcic_ble_periodic_adv_create_sync(params->filter_policy, params->sid, params->addr_type,
788                                             params->addr, params->sync_timeout, 0)) {
789         BTM_TRACE_ERROR("LE PA CreateSync cmd failed");
790         status = BTM_ILLEGAL_VALUE;
791     }
792 
793 end:
794     if(status != BTM_SUCCESS) {
795         cb_params.status = status;
796         BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT, &cb_params);
797     }
798 
799     return status;
800 }
btm_set_phy_callback(UINT8 status)801 void btm_set_phy_callback(UINT8 status)
802 {
803     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
804     cb_params.status = status;
805 
806     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT, &cb_params);
807 
808 }
btm_create_sync_callback(UINT8 status)809 void btm_create_sync_callback(UINT8 status)
810 {
811     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
812     cb_params.status = status;
813 
814     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT, &cb_params);
815 }
816 
btm_read_phy_callback(uint8_t hci_status,uint16_t conn_handle,uint8_t tx_phy,uint8_t rx_phy)817 void btm_read_phy_callback(uint8_t hci_status, uint16_t conn_handle, uint8_t tx_phy, uint8_t rx_phy)
818 {
819     tBTM_STATUS status = BTM_SUCCESS;
820     tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(conn_handle);
821     if(hci_status != HCI_SUCCESS) {
822         status = BTM_ILLEGAL_VALUE;
823         BTM_TRACE_ERROR("%s error status %d", __func__, hci_status);
824     }
825     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
826     if(p_lcb) {
827        memcpy(cb_params.read_phy.addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
828     }
829     cb_params.read_phy.status = status;
830     cb_params.read_phy.tx_phy = tx_phy;
831     cb_params.read_phy.rx_phy = rx_phy;
832 
833     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_READ_PHY_COMPLETE_EVT, &cb_params);
834 }
835 
BTM_BlePeriodicAdvSyncCancel(void)836 tBTM_STATUS BTM_BlePeriodicAdvSyncCancel(void)
837 {
838     tHCI_STATUS err = HCI_SUCCESS;
839     tBTM_STATUS status = BTM_SUCCESS;
840     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
841 
842     if ((err = btsnd_hcic_ble_periodic_adv_create_sync_cancel()) != HCI_SUCCESS) {
843         BTM_TRACE_ERROR("LE PA SyncCancel, cmd err=0x%x", err);
844         status = BTM_ILLEGAL_VALUE;
845     }
846 
847     cb_params.status = status;
848 
849     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_CANCEL_COMPLETE_EVT, &cb_params);
850 
851     return status;
852 }
853 
BTM_BlePeriodicAdvSyncTerm(UINT16 sync_handle)854 tBTM_STATUS BTM_BlePeriodicAdvSyncTerm(UINT16 sync_handle)
855 {
856     tHCI_STATUS err = HCI_SUCCESS;
857     tBTM_STATUS status = BTM_SUCCESS;
858     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
859 
860     if (( err = btsnd_hcic_ble_periodic_adv_term_sync(sync_handle)) != HCI_SUCCESS) {
861         BTM_TRACE_ERROR("LE PA SyncTerm: cmd err=0x%x", err);
862         status = BTM_ILLEGAL_VALUE;
863     }
864 
865     cb_params.status = status;
866 
867     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_TERMINATE_COMPLETE_EVT, &cb_params);
868 
869     return status;
870 }
871 
BTM_BlePeriodicAdvAddDevToList(tBLE_ADDR_TYPE addr_type,BD_ADDR addr,UINT16 sid)872 tBTM_STATUS BTM_BlePeriodicAdvAddDevToList(tBLE_ADDR_TYPE addr_type, BD_ADDR addr, UINT16 sid)
873 {
874     tHCI_STATUS err = HCI_SUCCESS;
875     tBTM_STATUS status = BTM_SUCCESS;
876     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
877 
878     if (addr_type > BLE_ADDR_TYPE_MAX) {
879         status = BTM_ILLEGAL_VALUE;
880         BTM_TRACE_ERROR("%s invalid addr_type %d", __func__, addr_type);
881         goto end;
882     }
883 
884     if ((err = btsnd_hcic_ble_add_dev_to_periodic_adv_list(addr_type, addr, sid)) != HCI_SUCCESS) {
885         BTM_TRACE_ERROR("LE PA AddDevToList: cmd err=0x%x", err);
886         status = BTM_ILLEGAL_VALUE;
887     }
888 
889 end:
890     cb_params.status = status;
891     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_ADD_DEV_COMPLETE_EVT, &cb_params);
892 
893     return status;
894 }
895 
BTM_BlePeriodicAdvRemoveDevFromList(tBLE_ADDR_TYPE addr_type,BD_ADDR addr,UINT16 sid)896 tBTM_STATUS BTM_BlePeriodicAdvRemoveDevFromList(tBLE_ADDR_TYPE addr_type, BD_ADDR addr, UINT16 sid)
897 {
898     tHCI_STATUS err = HCI_SUCCESS;
899     tBTM_STATUS status = BTM_SUCCESS;
900     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
901 
902     if (addr_type > BLE_ADDR_TYPE_MAX) {
903         status = BTM_ILLEGAL_VALUE;
904         BTM_TRACE_ERROR("%s invalid addr_type %d", __func__, addr_type);
905         goto end;
906     }
907 
908     if ((err = btsnd_hcic_ble_rm_dev_from_periodic_adv_list(addr_type, addr, sid)) != HCI_SUCCESS) {
909         BTM_TRACE_ERROR("LE PA RmDevFromList: cmd err=0x%x", err);
910         status = BTM_ILLEGAL_VALUE;
911     }
912 
913 end:
914 
915     cb_params.status = status;
916     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_REMOVE_DEV_COMPLETE_EVT, &cb_params);
917     return status;
918 }
919 
BTM_BlePeriodicAdvClearDev(void)920 tBTM_STATUS BTM_BlePeriodicAdvClearDev(void)
921 {
922     tHCI_STATUS err = HCI_SUCCESS;
923     tBTM_STATUS status = BTM_SUCCESS;
924     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
925 
926     if ((err = btsnd_hcic_ble_clear_periodic_adv_list()) != HCI_SUCCESS) {
927         BTM_TRACE_ERROR("LE PA ClrDev: cmd err=0x%x", err);
928         status = BTM_ILLEGAL_VALUE;
929     }
930 
931     cb_params.status = status;
932     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_CLEAR_DEV_COMPLETE_EVT, &cb_params);
933 
934     return status;
935 }
936 
BTM_BleSetExtendedScanParams(tBTM_BLE_EXT_SCAN_PARAMS * params)937 tBTM_STATUS BTM_BleSetExtendedScanParams(tBTM_BLE_EXT_SCAN_PARAMS *params)
938 {
939     UINT8 phy_mask = 0;
940     tHCI_STATUS err = HCI_SUCCESS;
941     tBTM_STATUS status = BTM_SUCCESS;
942     tHCI_EXT_SCAN_PARAMS hci_params[2];
943     int phy_count = 0;
944     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
945 
946     if (!params) {
947         status = BTM_ILLEGAL_VALUE;
948         BTM_TRACE_ERROR("%s invalid parameters", __func__);
949         goto end;
950     }
951 
952     if (params->own_addr_type > BLE_ADDR_TYPE_MAX) {
953         status = BTM_ILLEGAL_VALUE;
954         goto end;
955     }
956 
957     if (params->cfg_mask & BTM_BLE_GAP_EXT_SCAN_UNCODE_MASK) {
958         phy_mask |= 0x01;
959         memcpy(&hci_params[phy_count], &params->uncoded_cfg, sizeof(tHCI_EXT_SCAN_PARAMS));
960         phy_count++;
961     }
962 
963     if (params->cfg_mask & BTM_BLE_GAP_EXT_SCAN_CODE_MASK) {
964         phy_mask |= 0x04;
965         memcpy(&hci_params[phy_count], &params->coded_cfg, sizeof(tHCI_EXT_SCAN_PARAMS));
966         phy_count++;
967     }
968 
969     if (BTM_BleUpdateOwnType(&params->own_addr_type, NULL) != 0 ) {
970         status = BTM_ILLEGAL_VALUE;
971         BTM_TRACE_ERROR("LE UpdateOwnType err");
972         goto end;
973     }
974 
975     extend_adv_cb.scan_duplicate = params->scan_duplicate;
976 
977     if ((err = btsnd_hcic_ble_set_ext_scan_params(params->own_addr_type, params->filter_policy, phy_mask, phy_count,
978                                       hci_params)) != HCI_SUCCESS) {
979         BTM_TRACE_ERROR("LE ES SetParams: cmd err=0x%x", err);
980         status = BTM_ILLEGAL_VALUE;
981     }
982 
983 end:
984 
985     cb_params.status = status;
986 
987     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_EXT_SCAN_PARAMS_COMPLETE_EVT, &cb_params);
988 
989     return cb_params.status;
990 }
991 
BTM_BleExtendedScan(BOOLEAN enable,UINT16 duration,UINT16 period)992 tBTM_STATUS BTM_BleExtendedScan(BOOLEAN enable, UINT16 duration, UINT16 period)
993 {
994     tHCI_STATUS err = HCI_SUCCESS;
995     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
996     tBTM_STATUS status = BTM_SUCCESS;
997 
998     if (extend_adv_cb.scan_duplicate > 0x03) {
999         status = BTM_ILLEGAL_VALUE;
1000         BTM_TRACE_ERROR("%s invalid scan_duplicate %d", __func__, extend_adv_cb.scan_duplicate);
1001         goto end;
1002     }
1003 
1004     if ((err = btsnd_hcic_ble_ext_scan_enable(enable, extend_adv_cb.scan_duplicate, duration, period)) != HCI_SUCCESS) {
1005         BTM_TRACE_ERROR("LE ES En=%d: cmd err=0x%x", enable, err);
1006         status = BTM_ILLEGAL_VALUE;
1007     }
1008 
1009 end:
1010 
1011     cb_params.status = status;
1012 
1013     BTM_ExtBleCallbackTrigger(enable ? BTM_BLE_5_GAP_EXT_SCAN_START_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_SCAN_STOP_COMPLETE_EVT, &cb_params);
1014 
1015     return status;
1016 }
1017 
BTM_BleSetPreferExtenedConnParams(BD_ADDR bd_addr,tBTM_EXT_CONN_PARAMS * params)1018 void BTM_BleSetPreferExtenedConnParams (BD_ADDR bd_addr, tBTM_EXT_CONN_PARAMS *params)
1019 {
1020     tBTM_SEC_DEV_REC  *p_dev_rec = btm_find_or_alloc_dev (bd_addr);
1021 
1022     if (p_dev_rec) {
1023         if (params) {
1024             memcpy(&p_dev_rec->ext_conn_params, params, sizeof(tBTM_EXT_CONN_PARAMS));
1025         } else {
1026             BTM_TRACE_ERROR("Invalid Extand connection parameters");
1027         }
1028     } else {
1029             BTM_TRACE_ERROR("Unknown Device, setting rejected");
1030     }
1031 
1032     return;
1033 }
1034 
btm_ble_extended_init(void)1035 void btm_ble_extended_init(void)
1036 {
1037 
1038 }
1039 
btm_ble_extended_cleanup(void)1040 void btm_ble_extended_cleanup(void)
1041 {
1042 
1043 }
1044 
btm_ble_ext_adv_params_validate(tBTM_BLE_GAP_EXT_ADV_PARAMS * params)1045 static tBTM_STATUS btm_ble_ext_adv_params_validate(tBTM_BLE_GAP_EXT_ADV_PARAMS *params)
1046 {
1047     if (!params) {
1048         return BTM_ILLEGAL_VALUE;
1049     }
1050 
1051     if (params->own_addr_type > BLE_ADDR_TYPE_MAX) {
1052 	BTM_TRACE_ERROR("%s, invalid own address type, line %d, addr type %d", __func__, __LINE__, params->own_addr_type);
1053         return BTM_ILLEGAL_VALUE;
1054     }
1055 
1056     if (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_LEGACY) {
1057         /* Not allowed for legacy PDUs. */
1058         if (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_INCLUDE_TX_PWR) {
1059 	    BTM_TRACE_ERROR("%s, The Legacy adv can't include tx power bit, line %d", __func__, __LINE__);
1060             return BTM_ILLEGAL_VALUE;
1061         }
1062     }
1063 
1064     if (!(params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_LEGACY)) {
1065         /* Not allowed for extended advertising PDUs */
1066         if ((params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_CONNECTABLE) &&
1067             (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_SCANNABLE)) {
1068             BTM_TRACE_ERROR("%s, For the Extend adv, the properties can't be connectable and scannable at the same time, line %d", __func__, __LINE__);
1069             return BTM_ILLEGAL_VALUE;
1070         }
1071 
1072         /* HD directed advertising allowed only for legacy PDUs */
1073         if (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_HD_DIRECTED) {
1074             BTM_TRACE_ERROR("%s, HD directed advertising allowed only for legacy PDUs. line %d", __func__, __LINE__);
1075             return BTM_ILLEGAL_VALUE;
1076         }
1077     }
1078 
1079     return BTM_SUCCESS;
1080 }
1081 
btm_ble_ext_adv_set_data_validate(UINT8 instance,UINT16 len,UINT8 * data)1082 static tBTM_STATUS btm_ble_ext_adv_set_data_validate(UINT8 instance, UINT16 len, UINT8 *data)
1083 {
1084     if (!data) {
1085         BTM_TRACE_ERROR("%s, the extend adv data is NULL. line %d", __func__, __LINE__);
1086         return BTM_ILLEGAL_VALUE;
1087     }
1088 
1089     if (instance >= MAX_BLE_ADV_INSTANCE) {
1090         BTM_TRACE_ERROR("%s, adv instance is %d, Exceeded the maximum. line %d", __func__, instance, __LINE__);
1091         return BTM_ILLEGAL_VALUE;
1092     }
1093 
1094     if (!extend_adv_cb.inst[instance].configured) {
1095         BTM_TRACE_ERROR("%s, The extend adv hasn't configured, please use the set_ext_adv_params API to set the ext adv parameters first. line %d", __func__, __LINE__);
1096         return BTM_ILLEGAL_VALUE;
1097     }
1098 
1099     /* Not allowed with the direted advertising for legacy */
1100     if (extend_adv_cb.inst[instance].legacy_pdu && extend_adv_cb.inst[instance].directed) {
1101 	BTM_TRACE_ERROR("%s, Not allowed with the direted advertising for legacy. line %d", __func__, __LINE__);
1102         return BTM_ILLEGAL_VALUE;
1103     }
1104 
1105     /* Always allowed with legacy PDU but limited to legacy length */
1106     if (extend_adv_cb.inst[instance].legacy_pdu) {
1107         if (len > 31) {
1108             BTM_TRACE_ERROR("%s, for the legacy adv, the adv data length can't exceed 31. line %d", __func__, __LINE__);
1109             return BTM_ILLEGAL_VALUE;
1110         }
1111     } else {
1112         if (len > controller_get_interface()->ble_get_ext_adv_data_max_len()) {
1113             BTM_TRACE_ERROR("%s, The adv data len(%d) is longer then the controller adv max len(%d)",
1114                 __func__, len, controller_get_interface()->ble_get_ext_adv_data_max_len());
1115             return BTM_ILLEGAL_VALUE;
1116         }
1117     }
1118 
1119     return BTM_SUCCESS;
1120 }
1121 
btm_ble_update_phy_evt(tBTM_BLE_UPDATE_PHY * params)1122 void btm_ble_update_phy_evt(tBTM_BLE_UPDATE_PHY *params)
1123 {
1124     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1125 
1126     if (!params) {
1127         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1128         return;
1129     }
1130     tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(params->conn_idx);
1131     if(!p_lcb)  {
1132         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1133         return;
1134     }
1135 
1136     cb_params.phy_update.status = params->status;
1137     cb_params.phy_update.tx_phy = params->tx_phy;
1138     cb_params.phy_update.rx_phy = params->rx_phy;
1139     memcpy(cb_params.phy_update.addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
1140 
1141     // If the user has register the callback function, should callback it to the application.
1142 
1143     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PHY_UPDATE_COMPLETE_EVT, &cb_params);
1144 
1145     return;
1146 }
1147 
btm_ble_scan_timeout_evt(void)1148 void btm_ble_scan_timeout_evt(void)
1149 {
1150     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SCAN_TIMEOUT_EVT, NULL);
1151 }
1152 
btm_ble_adv_set_terminated_evt(tBTM_BLE_ADV_TERMINAT * params)1153 void btm_ble_adv_set_terminated_evt(tBTM_BLE_ADV_TERMINAT *params)
1154 {
1155     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1156 
1157     if (!params) {
1158         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1159         return;
1160     }
1161 
1162     // adv terminated due to connection, save the adv handle and connection handle
1163     if(params->completed_event == 0x00) {
1164         adv_record[params->adv_handle].ter_con_handle = params->conn_handle;
1165     } else {
1166         adv_record[params->adv_handle].ter_con_handle = INVALID_VALUE;
1167         adv_record[params->adv_handle].invalid = false;
1168     }
1169 
1170     memcpy(&cb_params.adv_term, params, sizeof(tBTM_BLE_ADV_TERMINAT));
1171 
1172     // If the user has register the callback function, should callback it to the application.
1173     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_ADV_TERMINATED_EVT, &cb_params);
1174 
1175     return;
1176 }
1177 
btm_ble_ext_adv_report_evt(tBTM_BLE_EXT_ADV_REPORT * params)1178 void btm_ble_ext_adv_report_evt(tBTM_BLE_EXT_ADV_REPORT *params)
1179 {
1180     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1181 
1182     if (!params) {
1183         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1184         return;
1185     }
1186 
1187     memcpy(&cb_params.ext_adv_report, params, sizeof(tBTM_BLE_EXT_ADV_REPORT));
1188 
1189     // If the user has register the callback function, should callback it to the application.
1190     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_REPORT_EVT, &cb_params);
1191 
1192     return;
1193 
1194 }
1195 
btm_ble_scan_req_received_evt(tBTM_BLE_SCAN_REQ_RECEIVED * params)1196 void btm_ble_scan_req_received_evt(tBTM_BLE_SCAN_REQ_RECEIVED *params)
1197 {
1198     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1199 
1200     if (!params) {
1201         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1202         return;
1203     }
1204 
1205     memcpy(&cb_params.scan_req, params, sizeof(tBTM_BLE_SCAN_REQ_RECEIVED));
1206 
1207     // If the user has register the callback function, should callback it to the application.
1208     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SCAN_REQ_RECEIVED_EVT, &cb_params);
1209 
1210     return;
1211 }
1212 
btm_ble_channel_select_algorithm_evt(tBTM_BLE_CHANNEL_SEL_ALG * params)1213 void btm_ble_channel_select_algorithm_evt(tBTM_BLE_CHANNEL_SEL_ALG *params)
1214 {
1215     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1216 
1217     if (!params) {
1218         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1219         return;
1220     }
1221 
1222     memcpy(&cb_params.channel_sel, params, sizeof(tBTM_BLE_CHANNEL_SEL_ALG));
1223 
1224     // If the user has register the callback function, should callback it to the application.
1225     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_CHANNEL_SELETE_ALGORITHM_EVT, &cb_params);
1226 
1227     return;
1228 }
1229 
btm_ble_periodic_adv_report_evt(tBTM_PERIOD_ADV_REPORT * params)1230 void btm_ble_periodic_adv_report_evt(tBTM_PERIOD_ADV_REPORT *params)
1231 {
1232     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1233 
1234     if (!params) {
1235         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1236         return;
1237     }
1238 
1239     memcpy(&cb_params.period_adv_report, params, sizeof(tBTM_PERIOD_ADV_REPORT));
1240 
1241     // If the user has register the callback function, should callback it to the application.
1242     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_REPORT_EVT, &cb_params);
1243 
1244     return;
1245 
1246 }
1247 
btm_ble_periodic_adv_sync_lost_evt(tBTM_BLE_PERIOD_ADV_SYNC_LOST * params)1248 void btm_ble_periodic_adv_sync_lost_evt(tBTM_BLE_PERIOD_ADV_SYNC_LOST *params)
1249 {
1250     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1251 
1252     if (!params) {
1253         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1254         return;
1255     }
1256 
1257     memcpy(&cb_params.sync_lost, params, sizeof(tBTM_BLE_PERIOD_ADV_SYNC_LOST));
1258 
1259     // If the user has register the callback function, should callback it to the application.
1260     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_LOST_EVT, &cb_params);
1261 
1262     return;
1263 
1264 }
1265 
btm_ble_periodic_adv_sync_establish_evt(tBTM_BLE_PERIOD_ADV_SYNC_ESTAB * params)1266 void btm_ble_periodic_adv_sync_establish_evt(tBTM_BLE_PERIOD_ADV_SYNC_ESTAB *params)
1267 {
1268     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1269 
1270     if (!params) {
1271         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1272         return;
1273     }
1274 
1275     memcpy(&cb_params.sync_estab, params, sizeof(tBTM_BLE_PERIOD_ADV_SYNC_ESTAB));
1276 
1277     // If the user has register the callback function, should callback it to the application.
1278     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_ESTAB_EVT, &cb_params);
1279 
1280     return;
1281 
1282 }
1283 
1284 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
1285 
1286 #if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)
BTM_BlePeriodicAdvSetInfoTrans(BD_ADDR bd_addr,UINT16 service_data,UINT8 adv_handle)1287 void BTM_BlePeriodicAdvSetInfoTrans(BD_ADDR bd_addr, UINT16 service_data, UINT8 adv_handle)
1288 {
1289     tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
1290 
1291     if (!p_lcb) {
1292         BTM_TRACE_ERROR("%s, invalid parameters", __func__);
1293         return;
1294     }
1295 
1296     if (!btsnd_hcic_ble_periodic_adv_set_info_trans(p_lcb->handle, service_data, adv_handle)) {
1297         BTM_TRACE_ERROR("%s, hci cmd error", __func__);
1298     }
1299 }
1300 
BTM_BleSetPeriodicAdvSyncTransParams(BD_ADDR bd_addr,UINT8 mode,UINT16 skip,UINT16 sync_timeout,UINT8 cte_type)1301 void BTM_BleSetPeriodicAdvSyncTransParams(BD_ADDR bd_addr, UINT8 mode, UINT16 skip, UINT16 sync_timeout, UINT8 cte_type)
1302 {
1303     tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
1304 
1305     if (!p_lcb) {
1306         BTM_TRACE_ERROR("%s, invalid parameters", __func__);
1307         return;
1308     }
1309 
1310     if (!btsnd_hcic_ble_set_periodic_adv_sync_trans_params(p_lcb->handle, mode, skip, sync_timeout, cte_type)) {
1311         BTM_TRACE_ERROR("%s, hci cmd error", __func__);
1312     }
1313 }
1314 #endif // #if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)
1315