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