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_FEATURE:
114         return "HCI_ERR_UNSUPPORTED_LMP_FEATURE";
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;
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     }
704 
705     if (len > controller_get_interface()->ble_get_ext_adv_data_max_len()) {
706         BTM_TRACE_ERROR("%s, The adv data len(%d) is longer then the controller adv max len(%d)",
707             __func__, len, controller_get_interface()->ble_get_ext_adv_data_max_len());
708         status = BTM_ILLEGAL_VALUE;
709     }
710 
711     rem_len = len;
712 
713     do {
714         UINT8 send_data_len = (rem_len > BTM_BLE_PERIODIC_ADV_DATA_LEN_MAX) ? BTM_BLE_PERIODIC_ADV_DATA_LEN_MAX : rem_len;
715 
716         if (len <= BTM_BLE_EXT_ADV_DATA_LEN_MAX) {
717             operation = BTM_BLE_ADV_DATA_OP_COMPLETE;
718         } else {
719             if (rem_len == len) {
720                 operation = BTM_BLE_ADV_DATA_OP_FIRST_FRAG;
721             } else if (rem_len <= BTM_BLE_EXT_ADV_DATA_LEN_MAX) {
722                 operation = BTM_BLE_ADV_DATA_OP_LAST_FRAG;
723             } else {
724 	        operation = BTM_BLE_ADV_DATA_OP_INTERMEDIATE_FRAG;
725 	    }
726         }
727 
728         if ((err = btsnd_hcic_ble_set_periodic_adv_data(instance, operation, send_data_len, &data[data_offset])) != HCI_SUCCESS) {
729             BTM_TRACE_ERROR("LE PA SetData: cmd err=0x%x", err);
730             status = BTM_ILLEGAL_VALUE;
731         }
732         rem_len -= send_data_len;
733 	data_offset += send_data_len;
734     } while(rem_len);
735 
736     cb_params.status = status;
737 
738     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_DATA_SET_COMPLETE_EVT, &cb_params);
739 
740     return status;
741 }
742 
BTM_BlePeriodicAdvEnable(UINT8 instance,BOOLEAN enable)743 tBTM_STATUS BTM_BlePeriodicAdvEnable(UINT8 instance, BOOLEAN enable)
744 {
745     tBTM_STATUS status = BTM_SUCCESS;
746     tHCI_STATUS err = HCI_SUCCESS;
747     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
748 
749     if (instance >= MAX_BLE_ADV_INSTANCE) {
750         BTM_TRACE_ERROR("%s, invalid instance %d", __func__, instance);
751         status = BTM_ILLEGAL_VALUE;
752         goto end;
753     }
754 
755     if ((err = btsnd_hcic_ble_periodic_adv_enable(enable, instance)) != HCI_SUCCESS) {
756         BTM_TRACE_ERROR("LE PA En=%d: cmd err=0x%x", enable, err);
757         status = BTM_ILLEGAL_VALUE;
758     }
759 
760 end:
761 
762     cb_params.status = status;
763 
764     BTM_ExtBleCallbackTrigger(enable ? BTM_BLE_5_GAP_PERIODIC_ADV_START_COMPLETE_EVT : BTM_BLE_5_GAP_PERIODIC_ADV_STOP_COMPLETE_EVT, &cb_params);
765 
766     return status;
767 
768 }
769 
BTM_BlePeriodicAdvCreateSync(tBTM_BLE_Periodic_Sync_Params * params)770 tBTM_STATUS BTM_BlePeriodicAdvCreateSync(tBTM_BLE_Periodic_Sync_Params *params)
771 {
772     //tHCI_STATUS err = HCI_SUCCESS;
773     tBTM_STATUS status = BTM_SUCCESS;
774     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
775 
776     if (!params) {
777         status = BTM_ILLEGAL_VALUE;
778         BTM_TRACE_ERROR("%s, the parameter is NULL.", __func__);
779         goto end;
780     }
781 
782     if ((params->sync_timeout < 0x0a || params->sync_timeout > 0x4000)
783         || (params->filter_policy > 0x01) || (params->addr_type > 0x01) ||
784         (params->sid > 0xf) || (params->skip > 0x01F3)) {
785             status = BTM_ILLEGAL_VALUE;
786             BTM_TRACE_ERROR("%s, The sync parameters is invalid.", __func__);
787             goto end;
788     }
789 
790     if (!btsnd_hcic_ble_periodic_adv_create_sync(params->filter_policy, params->sid, params->addr_type,
791                                             params->addr, params->sync_timeout, 0)) {
792         BTM_TRACE_ERROR("LE PA CreateSync cmd failed");
793         status = BTM_ILLEGAL_VALUE;
794     }
795 
796 end:
797     if(status != BTM_SUCCESS) {
798         cb_params.status = status;
799         BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT, &cb_params);
800     }
801 
802     return status;
803 }
btm_set_phy_callback(UINT8 status)804 void btm_set_phy_callback(UINT8 status)
805 {
806     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
807     cb_params.status = status;
808 
809     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT, &cb_params);
810 
811 }
btm_create_sync_callback(UINT8 status)812 void btm_create_sync_callback(UINT8 status)
813 {
814     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
815     cb_params.status = status;
816 
817     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT, &cb_params);
818 }
819 
btm_read_phy_callback(uint8_t hci_status,uint16_t conn_handle,uint8_t tx_phy,uint8_t rx_phy)820 void btm_read_phy_callback(uint8_t hci_status, uint16_t conn_handle, uint8_t tx_phy, uint8_t rx_phy)
821 {
822     tBTM_STATUS status = BTM_SUCCESS;
823     tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(conn_handle);
824     if(hci_status != HCI_SUCCESS) {
825         status = BTM_ILLEGAL_VALUE;
826         BTM_TRACE_ERROR("%s error status %d", __func__, hci_status);
827     }
828     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
829     if(p_lcb) {
830        memcpy(cb_params.read_phy.addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
831     }
832     cb_params.read_phy.status = status;
833     cb_params.read_phy.tx_phy = tx_phy;
834     cb_params.read_phy.rx_phy = rx_phy;
835 
836     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_READ_PHY_COMPLETE_EVT, &cb_params);
837 }
838 
BTM_BlePeriodicAdvSyncCancel(void)839 tBTM_STATUS BTM_BlePeriodicAdvSyncCancel(void)
840 {
841     tHCI_STATUS err = HCI_SUCCESS;
842     tBTM_STATUS status = BTM_SUCCESS;
843     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
844 
845     if ((err = btsnd_hcic_ble_periodic_adv_create_sync_cancel()) != HCI_SUCCESS) {
846         BTM_TRACE_ERROR("LE PA SyncCancel, cmd err=0x%x", err);
847         status = BTM_ILLEGAL_VALUE;
848     }
849 
850     cb_params.status = status;
851 
852     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_CANCEL_COMPLETE_EVT, &cb_params);
853 
854     return status;
855 }
856 
BTM_BlePeriodicAdvSyncTerm(UINT16 sync_handle)857 tBTM_STATUS BTM_BlePeriodicAdvSyncTerm(UINT16 sync_handle)
858 {
859     tHCI_STATUS err = HCI_SUCCESS;
860     tBTM_STATUS status = BTM_SUCCESS;
861     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
862 
863     if (( err = btsnd_hcic_ble_periodic_adv_term_sync(sync_handle)) != HCI_SUCCESS) {
864         BTM_TRACE_ERROR("LE PA SyncTerm: cmd err=0x%x", err);
865         status = BTM_ILLEGAL_VALUE;
866     }
867 
868     cb_params.status = status;
869 
870     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_TERMINATE_COMPLETE_EVT, &cb_params);
871 
872     return status;
873 }
874 
BTM_BlePeriodicAdvAddDevToList(tBLE_ADDR_TYPE addr_type,BD_ADDR addr,UINT16 sid)875 tBTM_STATUS BTM_BlePeriodicAdvAddDevToList(tBLE_ADDR_TYPE addr_type, BD_ADDR addr, UINT16 sid)
876 {
877     tHCI_STATUS err = HCI_SUCCESS;
878     tBTM_STATUS status = BTM_SUCCESS;
879     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
880 
881     if (addr_type > BLE_ADDR_TYPE_MAX) {
882         status = BTM_ILLEGAL_VALUE;
883         BTM_TRACE_ERROR("%s invalid addr_type %d", __func__, addr_type);
884         goto end;
885     }
886 
887     if ((err = btsnd_hcic_ble_add_dev_to_periodic_adv_list(addr_type, addr, sid)) != HCI_SUCCESS) {
888         BTM_TRACE_ERROR("LE PA AddDevToList: cmd err=0x%x", err);
889         status = BTM_ILLEGAL_VALUE;
890     }
891 
892 end:
893     cb_params.status = status;
894     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_ADD_DEV_COMPLETE_EVT, &cb_params);
895 
896     return status;
897 }
898 
BTM_BlePeriodicAdvRemoveDevFromList(tBLE_ADDR_TYPE addr_type,BD_ADDR addr,UINT16 sid)899 tBTM_STATUS BTM_BlePeriodicAdvRemoveDevFromList(tBLE_ADDR_TYPE addr_type, BD_ADDR addr, UINT16 sid)
900 {
901     tHCI_STATUS err = HCI_SUCCESS;
902     tBTM_STATUS status = BTM_SUCCESS;
903     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
904 
905     if (addr_type > BLE_ADDR_TYPE_MAX) {
906         status = BTM_ILLEGAL_VALUE;
907         BTM_TRACE_ERROR("%s invalid addr_type %d", __func__, addr_type);
908         goto end;
909     }
910 
911     if ((err = btsnd_hcic_ble_rm_dev_from_periodic_adv_list(addr_type, addr, sid)) != HCI_SUCCESS) {
912         BTM_TRACE_ERROR("LE PA RmDevFromList: cmd err=0x%x", err);
913         status = BTM_ILLEGAL_VALUE;
914     }
915 
916 end:
917 
918     cb_params.status = status;
919     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_REMOVE_DEV_COMPLETE_EVT, &cb_params);
920     return status;
921 }
922 
BTM_BlePeriodicAdvClearDev(void)923 tBTM_STATUS BTM_BlePeriodicAdvClearDev(void)
924 {
925     tHCI_STATUS err = HCI_SUCCESS;
926     tBTM_STATUS status = BTM_SUCCESS;
927     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
928 
929     if ((err = btsnd_hcic_ble_clear_periodic_adv_list()) != HCI_SUCCESS) {
930         BTM_TRACE_ERROR("LE PA ClrDev: cmd err=0x%x", err);
931         status = BTM_ILLEGAL_VALUE;
932     }
933 
934     cb_params.status = status;
935     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_CLEAR_DEV_COMPLETE_EVT, &cb_params);
936 
937     return status;
938 }
939 
BTM_BleSetExtendedScanParams(tBTM_BLE_EXT_SCAN_PARAMS * params)940 tBTM_STATUS BTM_BleSetExtendedScanParams(tBTM_BLE_EXT_SCAN_PARAMS *params)
941 {
942     UINT8 phy_mask = 0;
943     tHCI_STATUS err = HCI_SUCCESS;
944     tBTM_STATUS status = BTM_SUCCESS;
945     tHCI_EXT_SCAN_PARAMS hci_params[2];
946     int phy_count = 0;
947     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
948 
949     if (!params) {
950         status = BTM_ILLEGAL_VALUE;
951         BTM_TRACE_ERROR("%s invalid parameters", __func__);
952         goto end;
953     }
954 
955     if (params->own_addr_type > BLE_ADDR_TYPE_MAX) {
956         status = BTM_ILLEGAL_VALUE;
957         goto end;
958     }
959 
960     if (params->cfg_mask & BTM_BLE_GAP_EXT_SCAN_UNCODE_MASK) {
961         phy_mask |= 0x01;
962         memcpy(&hci_params[phy_count], &params->uncoded_cfg, sizeof(tHCI_EXT_SCAN_PARAMS));
963         phy_count++;
964     }
965 
966     if (params->cfg_mask & BTM_BLE_GAP_EXT_SCAN_CODE_MASK) {
967         phy_mask |= 0x04;
968         memcpy(&hci_params[phy_count], &params->coded_cfg, sizeof(tHCI_EXT_SCAN_PARAMS));
969         phy_count++;
970     }
971 
972     if (BTM_BleUpdateOwnType(&params->own_addr_type, NULL) != 0 ) {
973         status = BTM_ILLEGAL_VALUE;
974         BTM_TRACE_ERROR("LE UpdateOwnType err");
975         goto end;
976     }
977 
978     extend_adv_cb.scan_duplicate = params->scan_duplicate;
979 
980     if ((err = btsnd_hcic_ble_set_ext_scan_params(params->own_addr_type, params->filter_policy, phy_mask, phy_count,
981                                       hci_params)) != HCI_SUCCESS) {
982         BTM_TRACE_ERROR("LE ES SetParams: cmd err=0x%x", err);
983         status = BTM_ILLEGAL_VALUE;
984     }
985 
986 end:
987 
988     cb_params.status = status;
989 
990     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_EXT_SCAN_PARAMS_COMPLETE_EVT, &cb_params);
991 
992     return cb_params.status;
993 }
994 
BTM_BleExtendedScan(BOOLEAN enable,UINT16 duration,UINT16 period)995 tBTM_STATUS BTM_BleExtendedScan(BOOLEAN enable, UINT16 duration, UINT16 period)
996 {
997     tHCI_STATUS err = HCI_SUCCESS;
998     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
999     tBTM_STATUS status = BTM_SUCCESS;
1000 
1001     if (extend_adv_cb.scan_duplicate > 0x03) {
1002         status = BTM_ILLEGAL_VALUE;
1003         BTM_TRACE_ERROR("%s invalid scan_duplicate %d", __func__, extend_adv_cb.scan_duplicate);
1004         goto end;
1005     }
1006 
1007     if ((err = btsnd_hcic_ble_ext_scan_enable(enable, extend_adv_cb.scan_duplicate, duration, period)) != HCI_SUCCESS) {
1008         BTM_TRACE_ERROR("LE ES En=%d: cmd err=0x%x", enable, err);
1009         status = BTM_ILLEGAL_VALUE;
1010     }
1011 
1012 end:
1013 
1014     cb_params.status = status;
1015 
1016     BTM_ExtBleCallbackTrigger(enable ? BTM_BLE_5_GAP_EXT_SCAN_START_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_SCAN_STOP_COMPLETE_EVT, &cb_params);
1017 
1018     return status;
1019 }
1020 
BTM_BleSetPreferExtenedConnParams(BD_ADDR bd_addr,tBTM_EXT_CONN_PARAMS * params)1021 void BTM_BleSetPreferExtenedConnParams (BD_ADDR bd_addr, tBTM_EXT_CONN_PARAMS *params)
1022 {
1023     tBTM_SEC_DEV_REC  *p_dev_rec = btm_find_or_alloc_dev (bd_addr);
1024 
1025     if (p_dev_rec) {
1026         if (params) {
1027             memcpy(&p_dev_rec->ext_conn_params, params, sizeof(tBTM_EXT_CONN_PARAMS));
1028         } else {
1029             BTM_TRACE_ERROR("Invalid Extand connection parameters");
1030         }
1031     } else {
1032             BTM_TRACE_ERROR("Unknown Device, setting rejected");
1033     }
1034 
1035     return;
1036 }
1037 
1038 
btm_ble_extended_init(void)1039 void btm_ble_extended_init(void)
1040 {
1041 
1042 }
1043 
btm_ble_extended_cleanup(void)1044 void btm_ble_extended_cleanup(void)
1045 {
1046 
1047 }
1048 
btm_ble_ext_adv_params_validate(tBTM_BLE_GAP_EXT_ADV_PARAMS * params)1049 static tBTM_STATUS btm_ble_ext_adv_params_validate(tBTM_BLE_GAP_EXT_ADV_PARAMS *params)
1050 {
1051     if (!params) {
1052         return BTM_ILLEGAL_VALUE;
1053     }
1054 
1055     if (params->own_addr_type > BLE_ADDR_TYPE_MAX) {
1056 	BTM_TRACE_ERROR("%s, invalid own address type, line %d, addr type %d", __func__, __LINE__, params->own_addr_type);
1057         return BTM_ILLEGAL_VALUE;
1058     }
1059 
1060     if (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_LEGACY) {
1061         /* Not allowed for legacy PDUs. */
1062         if (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_INCLUDE_TX_PWR) {
1063 	    BTM_TRACE_ERROR("%s, The Legacy adv can't include tx power bit, line %d", __func__, __LINE__);
1064             return BTM_ILLEGAL_VALUE;
1065         }
1066     }
1067 
1068     if (!(params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_LEGACY)) {
1069         /* Not allowed for extended advertising PDUs */
1070         if ((params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_CONNECTABLE) &&
1071             (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_SCANNABLE)) {
1072             BTM_TRACE_ERROR("%s, For the Extend adv, the properties can't be connectable and scannable at the same time, line %d", __func__, __LINE__);
1073             return BTM_ILLEGAL_VALUE;
1074         }
1075 
1076         /* HD directed advertising allowed only for legacy PDUs */
1077         if (params->type & BTM_BLE_GAP_SET_EXT_ADV_PROP_HD_DIRECTED) {
1078             BTM_TRACE_ERROR("%s, HD directed advertising allowed only for legacy PDUs. line %d", __func__, __LINE__);
1079             return BTM_ILLEGAL_VALUE;
1080         }
1081     }
1082 
1083     return BTM_SUCCESS;
1084 }
1085 
btm_ble_ext_adv_set_data_validate(UINT8 instance,UINT16 len,UINT8 * data)1086 static tBTM_STATUS btm_ble_ext_adv_set_data_validate(UINT8 instance, UINT16 len, UINT8 *data)
1087 {
1088     if (!data) {
1089         BTM_TRACE_ERROR("%s, the extend adv data is NULL. line %d", __func__, __LINE__);
1090         return BTM_ILLEGAL_VALUE;
1091     }
1092 
1093     if (instance >= MAX_BLE_ADV_INSTANCE) {
1094         BTM_TRACE_ERROR("%s, adv instance is %d, Exceeded the maximum. line %d", __func__, instance, __LINE__);
1095         return BTM_ILLEGAL_VALUE;
1096     }
1097 
1098     if (!extend_adv_cb.inst[instance].configured) {
1099         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__);
1100         return BTM_ILLEGAL_VALUE;
1101     }
1102 
1103     /* Not allowed with the direted advertising for legacy */
1104     if (extend_adv_cb.inst[instance].legacy_pdu && extend_adv_cb.inst[instance].directed) {
1105 	BTM_TRACE_ERROR("%s, Not allowed with the direted advertising for legacy. line %d", __func__, __LINE__);
1106         return BTM_ILLEGAL_VALUE;
1107     }
1108 
1109     /* Always allowed with legacy PDU but limited to legacy length */
1110     if (extend_adv_cb.inst[instance].legacy_pdu) {
1111         if (len > 31) {
1112             BTM_TRACE_ERROR("%s, for the legacy adv, the adv data length can't exceed 31. line %d", __func__, __LINE__);
1113             return BTM_ILLEGAL_VALUE;
1114         }
1115     }
1116 
1117     return BTM_SUCCESS;
1118 }
1119 
btm_ble_update_phy_evt(tBTM_BLE_UPDATE_PHY * params)1120 void btm_ble_update_phy_evt(tBTM_BLE_UPDATE_PHY *params)
1121 {
1122     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1123 
1124     if (!params) {
1125         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1126         return;
1127     }
1128     tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(params->conn_idx);
1129     if(!p_lcb)  {
1130         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1131         return;
1132     }
1133 
1134     cb_params.phy_update.status = params->status;
1135     cb_params.phy_update.tx_phy = params->tx_phy;
1136     cb_params.phy_update.rx_phy = params->rx_phy;
1137     memcpy(cb_params.phy_update.addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
1138 
1139     // If the user has register the callback function, should callback it to the application.
1140 
1141     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PHY_UPDATE_COMPLETE_EVT, &cb_params);
1142 
1143     return;
1144 }
1145 
btm_ble_scan_timeout_evt(void)1146 void btm_ble_scan_timeout_evt(void)
1147 {
1148     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SCAN_TIMEOUT_EVT, NULL);
1149 }
1150 
btm_ble_adv_set_terminated_evt(tBTM_BLE_ADV_TERMINAT * params)1151 void btm_ble_adv_set_terminated_evt(tBTM_BLE_ADV_TERMINAT *params)
1152 {
1153     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1154 
1155     if (!params) {
1156         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1157         return;
1158     }
1159 
1160     // adv terminated due to connection, save the adv handle and connection handle
1161     if(params->completed_event == 0x00) {
1162         adv_record[params->adv_handle].ter_con_handle = params->conn_handle;
1163     } else {
1164         adv_record[params->adv_handle].ter_con_handle = INVALID_VALUE;
1165         adv_record[params->adv_handle].invalid = false;
1166     }
1167 
1168     memcpy(&cb_params.adv_term, params, sizeof(tBTM_BLE_ADV_TERMINAT));
1169 
1170     // If the user has register the callback function, should callback it to the application.
1171     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_ADV_TERMINATED_EVT, &cb_params);
1172 
1173     return;
1174 }
1175 
btm_ble_ext_adv_report_evt(tBTM_BLE_EXT_ADV_REPORT * params)1176 void btm_ble_ext_adv_report_evt(tBTM_BLE_EXT_ADV_REPORT *params)
1177 {
1178     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1179 
1180     if (!params) {
1181         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1182         return;
1183     }
1184 
1185     memcpy(&cb_params.ext_adv_report, params, sizeof(tBTM_BLE_EXT_ADV_REPORT));
1186 
1187     // If the user has register the callback function, should callback it to the application.
1188     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_REPORT_EVT, &cb_params);
1189 
1190     return;
1191 
1192 }
1193 
btm_ble_scan_req_received_evt(tBTM_BLE_SCAN_REQ_RECEIVED * params)1194 void btm_ble_scan_req_received_evt(tBTM_BLE_SCAN_REQ_RECEIVED *params)
1195 {
1196     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1197 
1198     if (!params) {
1199         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1200         return;
1201     }
1202 
1203     memcpy(&cb_params.scan_req, params, sizeof(tBTM_BLE_SCAN_REQ_RECEIVED));
1204 
1205     // If the user has register the callback function, should callback it to the application.
1206     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SCAN_REQ_RECEIVED_EVT, &cb_params);
1207 
1208     return;
1209 }
1210 
btm_ble_channel_select_algorithm_evt(tBTM_BLE_CHANNEL_SEL_ALG * params)1211 void btm_ble_channel_select_algorithm_evt(tBTM_BLE_CHANNEL_SEL_ALG *params)
1212 {
1213     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1214 
1215     if (!params) {
1216         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1217         return;
1218     }
1219 
1220     memcpy(&cb_params.channel_sel, params, sizeof(tBTM_BLE_CHANNEL_SEL_ALG));
1221 
1222     // If the user has register the callback function, should callback it to the application.
1223     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_CHANNEL_SELETE_ALGORITHM_EVT, &cb_params);
1224 
1225     return;
1226 }
1227 
btm_ble_periodic_adv_report_evt(tBTM_PERIOD_ADV_REPORT * params)1228 void btm_ble_periodic_adv_report_evt(tBTM_PERIOD_ADV_REPORT *params)
1229 {
1230     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1231 
1232     if (!params) {
1233         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1234         return;
1235     }
1236 
1237     memcpy(&cb_params.period_adv_report, params, sizeof(tBTM_PERIOD_ADV_REPORT));
1238 
1239     // If the user has register the callback function, should callback it to the application.
1240     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_REPORT_EVT, &cb_params);
1241 
1242     return;
1243 
1244 }
1245 
btm_ble_periodic_adv_sync_lost_evt(tBTM_BLE_PERIOD_ADV_SYNC_LOST * params)1246 void btm_ble_periodic_adv_sync_lost_evt(tBTM_BLE_PERIOD_ADV_SYNC_LOST *params)
1247 {
1248     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1249 
1250     if (!params) {
1251         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1252         return;
1253     }
1254 
1255     memcpy(&cb_params.sync_lost, params, sizeof(tBTM_BLE_PERIOD_ADV_SYNC_LOST));
1256 
1257     // If the user has register the callback function, should callback it to the application.
1258     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_LOST_EVT, &cb_params);
1259 
1260     return;
1261 
1262 }
1263 
btm_ble_periodic_adv_sync_establish_evt(tBTM_BLE_PERIOD_ADV_SYNC_ESTAB * params)1264 void btm_ble_periodic_adv_sync_establish_evt(tBTM_BLE_PERIOD_ADV_SYNC_ESTAB *params)
1265 {
1266     tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
1267 
1268     if (!params) {
1269         BTM_TRACE_ERROR("%s, Invalid params.", __func__);
1270         return;
1271     }
1272 
1273     memcpy(&cb_params.sync_estab, params, sizeof(tBTM_BLE_PERIOD_ADV_SYNC_ESTAB));
1274 
1275     // If the user has register the callback function, should callback it to the application.
1276     BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_ESTAB_EVT, &cb_params);
1277 
1278     return;
1279 
1280 }
1281 
1282 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
1283