OSDN Git Service

hciblecmds: remove unnecessary checking
[android-x86/system-bt.git] / stack / hcic / hciblecmds.cc
1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /******************************************************************************
20  *
21  *  This file contains function of the HCIC unit to format and send HCI
22  *  commands.
23  *
24  ******************************************************************************/
25
26 #define LOG_TAG "bt_hciblecmds"
27
28 #include "bt_common.h"
29 #include "bt_target.h"
30 #include "btu.h"
31 #include "hcidefs.h"
32 #include "hcimsgs.h"
33 #include "osi/include/log.h"
34
35 #include <base/bind.h>
36 #include <stddef.h>
37 #include <string.h>
38
39 void btsnd_hcic_ble_set_local_used_feat(uint8_t feat_set[8]) {
40   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
41   uint8_t* pp = (uint8_t*)(p + 1);
42
43   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_SET_USED_FEAT_CMD;
44   p->offset = 0;
45
46   UINT16_TO_STREAM(pp, HCI_BLE_WRITE_LOCAL_SPT_FEAT);
47   ARRAY_TO_STREAM(pp, feat_set, HCIC_PARAM_SIZE_SET_USED_FEAT_CMD);
48
49   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
50 }
51
52 void btsnd_hcic_ble_set_random_addr(const RawAddress& random_bda) {
53   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
54   uint8_t* pp = (uint8_t*)(p + 1);
55
56   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_RANDOM_ADDR_CMD;
57   p->offset = 0;
58
59   UINT16_TO_STREAM(pp, HCI_BLE_WRITE_RANDOM_ADDR);
60   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_WRITE_RANDOM_ADDR_CMD);
61
62   BDADDR_TO_STREAM(pp, random_bda);
63
64   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
65 }
66
67 void btsnd_hcic_ble_write_adv_params(uint16_t adv_int_min, uint16_t adv_int_max,
68                                      uint8_t adv_type, uint8_t addr_type_own,
69                                      uint8_t addr_type_dir,
70                                      const RawAddress& direct_bda,
71                                      uint8_t channel_map,
72                                      uint8_t adv_filter_policy) {
73   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
74   uint8_t* pp = (uint8_t*)(p + 1);
75
76   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS;
77   p->offset = 0;
78
79   UINT16_TO_STREAM(pp, HCI_BLE_WRITE_ADV_PARAMS);
80   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS);
81
82   UINT16_TO_STREAM(pp, adv_int_min);
83   UINT16_TO_STREAM(pp, adv_int_max);
84   UINT8_TO_STREAM(pp, adv_type);
85   UINT8_TO_STREAM(pp, addr_type_own);
86   UINT8_TO_STREAM(pp, addr_type_dir);
87   BDADDR_TO_STREAM(pp, direct_bda);
88   UINT8_TO_STREAM(pp, channel_map);
89   UINT8_TO_STREAM(pp, adv_filter_policy);
90
91   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
92 }
93 void btsnd_hcic_ble_read_adv_chnl_tx_power(void) {
94   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
95   uint8_t* pp = (uint8_t*)(p + 1);
96
97   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_READ_CMD;
98   p->offset = 0;
99
100   UINT16_TO_STREAM(pp, HCI_BLE_READ_ADV_CHNL_TX_POWER);
101   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_READ_CMD);
102
103   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
104 }
105
106 void btsnd_hcic_ble_set_adv_data(uint8_t data_len, uint8_t* p_data) {
107   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
108   uint8_t* pp = (uint8_t*)(p + 1);
109
110   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1;
111   p->offset = 0;
112
113   UINT16_TO_STREAM(pp, HCI_BLE_WRITE_ADV_DATA);
114   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1);
115
116   memset(pp, 0, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA);
117
118   if (p_data != NULL && data_len > 0) {
119     if (data_len > HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA)
120       data_len = HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA;
121
122     UINT8_TO_STREAM(pp, data_len);
123
124     ARRAY_TO_STREAM(pp, p_data, data_len);
125   }
126   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
127 }
128 void btsnd_hcic_ble_set_scan_rsp_data(uint8_t data_len, uint8_t* p_scan_rsp) {
129   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
130   uint8_t* pp = (uint8_t*)(p + 1);
131
132   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_SCAN_RSP + 1;
133   p->offset = 0;
134
135   UINT16_TO_STREAM(pp, HCI_BLE_WRITE_SCAN_RSP_DATA);
136   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_WRITE_SCAN_RSP + 1);
137
138   memset(pp, 0, HCIC_PARAM_SIZE_BLE_WRITE_SCAN_RSP);
139
140   if (p_scan_rsp != NULL && data_len > 0) {
141     if (data_len > HCIC_PARAM_SIZE_BLE_WRITE_SCAN_RSP)
142       data_len = HCIC_PARAM_SIZE_BLE_WRITE_SCAN_RSP;
143
144     UINT8_TO_STREAM(pp, data_len);
145
146     ARRAY_TO_STREAM(pp, p_scan_rsp, data_len);
147   }
148
149   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
150 }
151
152 void btsnd_hcic_ble_set_adv_enable(uint8_t adv_enable) {
153   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
154   uint8_t* pp = (uint8_t*)(p + 1);
155
156   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_ADV_ENABLE;
157   p->offset = 0;
158
159   UINT16_TO_STREAM(pp, HCI_BLE_WRITE_ADV_ENABLE);
160   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_WRITE_ADV_ENABLE);
161
162   UINT8_TO_STREAM(pp, adv_enable);
163
164   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
165 }
166 void btsnd_hcic_ble_set_scan_params(uint8_t scan_type, uint16_t scan_int,
167                                     uint16_t scan_win, uint8_t addr_type_own,
168                                     uint8_t scan_filter_policy) {
169   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
170   uint8_t* pp = (uint8_t*)(p + 1);
171
172   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_SCAN_PARAM;
173   p->offset = 0;
174
175   UINT16_TO_STREAM(pp, HCI_BLE_WRITE_SCAN_PARAMS);
176   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_WRITE_SCAN_PARAM);
177
178   UINT8_TO_STREAM(pp, scan_type);
179   UINT16_TO_STREAM(pp, scan_int);
180   UINT16_TO_STREAM(pp, scan_win);
181   UINT8_TO_STREAM(pp, addr_type_own);
182   UINT8_TO_STREAM(pp, scan_filter_policy);
183
184   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
185 }
186
187 void btsnd_hcic_ble_set_scan_enable(uint8_t scan_enable, uint8_t duplicate) {
188   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
189   uint8_t* pp = (uint8_t*)(p + 1);
190
191   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_SCAN_ENABLE;
192   p->offset = 0;
193
194   UINT16_TO_STREAM(pp, HCI_BLE_WRITE_SCAN_ENABLE);
195   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_WRITE_SCAN_ENABLE);
196
197   UINT8_TO_STREAM(pp, scan_enable);
198   UINT8_TO_STREAM(pp, duplicate);
199
200   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
201 }
202
203 /* link layer connection management commands */
204 void btsnd_hcic_ble_create_ll_conn(
205     uint16_t scan_int, uint16_t scan_win, uint8_t init_filter_policy,
206     uint8_t addr_type_peer, const RawAddress& bda_peer, uint8_t addr_type_own,
207     uint16_t conn_int_min, uint16_t conn_int_max, uint16_t conn_latency,
208     uint16_t conn_timeout, uint16_t min_ce_len, uint16_t max_ce_len) {
209   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
210   uint8_t* pp = (uint8_t*)(p + 1);
211
212   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_CREATE_LL_CONN;
213   p->offset = 0;
214
215   UINT16_TO_STREAM(pp, HCI_BLE_CREATE_LL_CONN);
216   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_CREATE_LL_CONN);
217
218   UINT16_TO_STREAM(pp, scan_int);
219   UINT16_TO_STREAM(pp, scan_win);
220   UINT8_TO_STREAM(pp, init_filter_policy);
221
222   UINT8_TO_STREAM(pp, addr_type_peer);
223   BDADDR_TO_STREAM(pp, bda_peer);
224   UINT8_TO_STREAM(pp, addr_type_own);
225
226   UINT16_TO_STREAM(pp, conn_int_min);
227   UINT16_TO_STREAM(pp, conn_int_max);
228   UINT16_TO_STREAM(pp, conn_latency);
229   UINT16_TO_STREAM(pp, conn_timeout);
230
231   UINT16_TO_STREAM(pp, min_ce_len);
232   UINT16_TO_STREAM(pp, max_ce_len);
233
234   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
235 }
236
237 void btsnd_hcic_ble_create_conn_cancel(void) {
238   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
239   uint8_t* pp = (uint8_t*)(p + 1);
240
241   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_CREATE_CONN_CANCEL;
242   p->offset = 0;
243
244   UINT16_TO_STREAM(pp, HCI_BLE_CREATE_CONN_CANCEL);
245   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_CREATE_CONN_CANCEL);
246
247   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
248 }
249
250 void btsnd_hcic_ble_clear_white_list(void) {
251   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
252   uint8_t* pp = (uint8_t*)(p + 1);
253
254   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_CLEAR_WHITE_LIST;
255   p->offset = 0;
256
257   UINT16_TO_STREAM(pp, HCI_BLE_CLEAR_WHITE_LIST);
258   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_CLEAR_WHITE_LIST);
259
260   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
261 }
262
263 void btsnd_hcic_ble_add_white_list(uint8_t addr_type, const RawAddress& bda) {
264   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
265   uint8_t* pp = (uint8_t*)(p + 1);
266
267   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_ADD_WHITE_LIST;
268   p->offset = 0;
269
270   UINT16_TO_STREAM(pp, HCI_BLE_ADD_WHITE_LIST);
271   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_ADD_WHITE_LIST);
272
273   UINT8_TO_STREAM(pp, addr_type);
274   BDADDR_TO_STREAM(pp, bda);
275
276   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
277 }
278
279 void btsnd_hcic_ble_remove_from_white_list(uint8_t addr_type,
280                                            const RawAddress& bda) {
281   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
282   uint8_t* pp = (uint8_t*)(p + 1);
283
284   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_REMOVE_WHITE_LIST;
285   p->offset = 0;
286
287   UINT16_TO_STREAM(pp, HCI_BLE_REMOVE_WHITE_LIST);
288   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_REMOVE_WHITE_LIST);
289
290   UINT8_TO_STREAM(pp, addr_type);
291   BDADDR_TO_STREAM(pp, bda);
292
293   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
294 }
295
296 void btsnd_hcic_ble_upd_ll_conn_params(uint16_t handle, uint16_t conn_int_min,
297                                        uint16_t conn_int_max,
298                                        uint16_t conn_latency,
299                                        uint16_t conn_timeout,
300                                        uint16_t min_ce_len,
301                                        uint16_t max_ce_len) {
302   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
303   uint8_t* pp = (uint8_t*)(p + 1);
304
305   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_UPD_LL_CONN_PARAMS;
306   p->offset = 0;
307
308   UINT16_TO_STREAM(pp, HCI_BLE_UPD_LL_CONN_PARAMS);
309   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_UPD_LL_CONN_PARAMS);
310
311   UINT16_TO_STREAM(pp, handle);
312
313   UINT16_TO_STREAM(pp, conn_int_min);
314   UINT16_TO_STREAM(pp, conn_int_max);
315   UINT16_TO_STREAM(pp, conn_latency);
316   UINT16_TO_STREAM(pp, conn_timeout);
317   UINT16_TO_STREAM(pp, min_ce_len);
318   UINT16_TO_STREAM(pp, max_ce_len);
319
320   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
321 }
322
323 void btsnd_hcic_ble_set_host_chnl_class(
324     uint8_t chnl_map[HCIC_BLE_CHNL_MAP_SIZE]) {
325   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
326   uint8_t* pp = (uint8_t*)(p + 1);
327
328   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_SET_HOST_CHNL_CLASS;
329   p->offset = 0;
330
331   UINT16_TO_STREAM(pp, HCI_BLE_SET_HOST_CHNL_CLASS);
332   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_SET_HOST_CHNL_CLASS);
333
334   ARRAY_TO_STREAM(pp, chnl_map, HCIC_BLE_CHNL_MAP_SIZE);
335
336   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
337 }
338
339 void btsnd_hcic_ble_read_chnl_map(uint16_t handle) {
340   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
341   uint8_t* pp = (uint8_t*)(p + 1);
342
343   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_READ_CHNL_MAP;
344   p->offset = 0;
345
346   UINT16_TO_STREAM(pp, HCI_BLE_READ_CHNL_MAP);
347   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_READ_CHNL_MAP);
348
349   UINT16_TO_STREAM(pp, handle);
350
351   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
352 }
353
354 void btsnd_hcic_ble_read_remote_feat(uint16_t handle) {
355   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
356   uint8_t* pp = (uint8_t*)(p + 1);
357
358   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_READ_REMOTE_FEAT;
359   p->offset = 0;
360
361   UINT16_TO_STREAM(pp, HCI_BLE_READ_REMOTE_FEAT);
362   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_READ_REMOTE_FEAT);
363
364   UINT16_TO_STREAM(pp, handle);
365
366   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
367 }
368
369 /* security management commands */
370 void btsnd_hcic_ble_encrypt(uint8_t* key, uint8_t key_len, uint8_t* plain_text,
371                             uint8_t pt_len, void* p_cmd_cplt_cback) {
372   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
373   uint8_t* pp = (uint8_t*)(p + 1);
374
375   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_ENCRYPT;
376   p->offset = sizeof(void*);
377
378   *((void**)pp) =
379       p_cmd_cplt_cback; /* Store command complete callback in buffer */
380   pp += sizeof(void*);  /* Skip over callback pointer */
381
382   UINT16_TO_STREAM(pp, HCI_BLE_ENCRYPT);
383   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_ENCRYPT);
384
385   memset(pp, 0, HCIC_PARAM_SIZE_BLE_ENCRYPT);
386
387   if (key_len > HCIC_BLE_ENCRYT_KEY_SIZE) key_len = HCIC_BLE_ENCRYT_KEY_SIZE;
388   if (pt_len > HCIC_BLE_ENCRYT_KEY_SIZE) pt_len = HCIC_BLE_ENCRYT_KEY_SIZE;
389
390   ARRAY_TO_STREAM(pp, key, key_len);
391   pp += (HCIC_BLE_ENCRYT_KEY_SIZE - key_len);
392   ARRAY_TO_STREAM(pp, plain_text, pt_len);
393
394   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
395 }
396
397 void btsnd_hcic_ble_rand(base::Callback<void(BT_OCTET8)> cb) {
398   btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_RAND, nullptr, 0,
399                             base::Bind(
400                                 [](base::Callback<void(BT_OCTET8)> cb,
401                                    uint8_t* param, uint16_t param_len) {
402                                   LOG_WARN(LOG_TAG,
403                                       "LE Rand return status is not zero: %u", param[0]);
404                                   cb.Run(param + 1 /* skip status */);
405                                 },
406                                 std::move(cb)));
407 }
408
409 void btsnd_hcic_ble_start_enc(uint16_t handle,
410                               uint8_t rand[HCIC_BLE_RAND_DI_SIZE],
411                               uint16_t ediv,
412                               uint8_t ltk[HCIC_BLE_ENCRYT_KEY_SIZE]) {
413   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
414   uint8_t* pp = (uint8_t*)(p + 1);
415
416   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_START_ENC;
417   p->offset = 0;
418
419   UINT16_TO_STREAM(pp, HCI_BLE_START_ENC);
420   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_START_ENC);
421
422   UINT16_TO_STREAM(pp, handle);
423   ARRAY_TO_STREAM(pp, rand, HCIC_BLE_RAND_DI_SIZE);
424   UINT16_TO_STREAM(pp, ediv);
425   ARRAY_TO_STREAM(pp, ltk, HCIC_BLE_ENCRYT_KEY_SIZE);
426
427   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
428 }
429
430 void btsnd_hcic_ble_ltk_req_reply(uint16_t handle,
431                                   uint8_t ltk[HCIC_BLE_ENCRYT_KEY_SIZE]) {
432   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
433   uint8_t* pp = (uint8_t*)(p + 1);
434
435   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_LTK_REQ_REPLY;
436   p->offset = 0;
437
438   UINT16_TO_STREAM(pp, HCI_BLE_LTK_REQ_REPLY);
439   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_LTK_REQ_REPLY);
440
441   UINT16_TO_STREAM(pp, handle);
442   ARRAY_TO_STREAM(pp, ltk, HCIC_BLE_ENCRYT_KEY_SIZE);
443
444   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
445 }
446
447 void btsnd_hcic_ble_ltk_req_neg_reply(uint16_t handle) {
448   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
449   uint8_t* pp = (uint8_t*)(p + 1);
450
451   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_LTK_REQ_NEG_REPLY;
452   p->offset = 0;
453
454   UINT16_TO_STREAM(pp, HCI_BLE_LTK_REQ_NEG_REPLY);
455   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_LTK_REQ_NEG_REPLY);
456
457   UINT16_TO_STREAM(pp, handle);
458
459   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
460 }
461
462 void btsnd_hcic_ble_receiver_test(uint8_t rx_freq) {
463   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
464   uint8_t* pp = (uint8_t*)(p + 1);
465
466   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_PARAM1;
467   p->offset = 0;
468
469   UINT16_TO_STREAM(pp, HCI_BLE_RECEIVER_TEST);
470   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_WRITE_PARAM1);
471
472   UINT8_TO_STREAM(pp, rx_freq);
473
474   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
475 }
476
477 void btsnd_hcic_ble_transmitter_test(uint8_t tx_freq, uint8_t test_data_len,
478                                      uint8_t payload) {
479   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
480   uint8_t* pp = (uint8_t*)(p + 1);
481
482   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_PARAM3;
483   p->offset = 0;
484
485   UINT16_TO_STREAM(pp, HCI_BLE_TRANSMITTER_TEST);
486   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_WRITE_PARAM3);
487
488   UINT8_TO_STREAM(pp, tx_freq);
489   UINT8_TO_STREAM(pp, test_data_len);
490   UINT8_TO_STREAM(pp, payload);
491
492   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
493 }
494
495 void btsnd_hcic_ble_test_end(void) {
496   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
497   uint8_t* pp = (uint8_t*)(p + 1);
498
499   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_READ_CMD;
500   p->offset = 0;
501
502   UINT16_TO_STREAM(pp, HCI_BLE_TEST_END);
503   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_READ_CMD);
504
505   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
506 }
507
508 void btsnd_hcic_ble_read_host_supported(void) {
509   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
510   uint8_t* pp = (uint8_t*)(p + 1);
511
512   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_READ_CMD;
513   p->offset = 0;
514
515   UINT16_TO_STREAM(pp, HCI_READ_LE_HOST_SUPPORT);
516   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_READ_CMD);
517
518   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
519 }
520
521 #if (BLE_LLT_INCLUDED == TRUE)
522
523 void btsnd_hcic_ble_rc_param_req_reply(uint16_t handle, uint16_t conn_int_min,
524                                        uint16_t conn_int_max,
525                                        uint16_t conn_latency,
526                                        uint16_t conn_timeout,
527                                        uint16_t min_ce_len,
528                                        uint16_t max_ce_len) {
529   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
530   uint8_t* pp = (uint8_t*)(p + 1);
531
532   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_RC_PARAM_REQ_REPLY;
533   p->offset = 0;
534
535   UINT16_TO_STREAM(pp, HCI_BLE_RC_PARAM_REQ_REPLY);
536   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_RC_PARAM_REQ_REPLY);
537
538   UINT16_TO_STREAM(pp, handle);
539   UINT16_TO_STREAM(pp, conn_int_min);
540   UINT16_TO_STREAM(pp, conn_int_max);
541   UINT16_TO_STREAM(pp, conn_latency);
542   UINT16_TO_STREAM(pp, conn_timeout);
543   UINT16_TO_STREAM(pp, min_ce_len);
544   UINT16_TO_STREAM(pp, max_ce_len);
545
546   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
547 }
548
549 void btsnd_hcic_ble_rc_param_req_neg_reply(uint16_t handle, uint8_t reason) {
550   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
551   uint8_t* pp = (uint8_t*)(p + 1);
552
553   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_RC_PARAM_REQ_NEG_REPLY;
554   p->offset = 0;
555
556   UINT16_TO_STREAM(pp, HCI_BLE_RC_PARAM_REQ_NEG_REPLY);
557   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_RC_PARAM_REQ_NEG_REPLY);
558
559   UINT16_TO_STREAM(pp, handle);
560   UINT8_TO_STREAM(pp, reason);
561
562   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
563 }
564 #endif
565
566 void btsnd_hcic_ble_add_device_resolving_list(
567     uint8_t addr_type_peer, const RawAddress& bda_peer,
568     uint8_t irk_peer[HCIC_BLE_IRK_SIZE], uint8_t irk_local[HCIC_BLE_IRK_SIZE]) {
569   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
570   uint8_t* pp = (uint8_t*)(p + 1);
571
572   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_ADD_DEV_RESOLVING_LIST;
573   p->offset = 0;
574
575   UINT16_TO_STREAM(pp, HCI_BLE_ADD_DEV_RESOLVING_LIST);
576   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_ADD_DEV_RESOLVING_LIST);
577   UINT8_TO_STREAM(pp, addr_type_peer);
578   BDADDR_TO_STREAM(pp, bda_peer);
579   ARRAY_TO_STREAM(pp, irk_peer, HCIC_BLE_ENCRYT_KEY_SIZE);
580   ARRAY_TO_STREAM(pp, irk_local, HCIC_BLE_ENCRYT_KEY_SIZE);
581
582   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
583 }
584
585 void btsnd_hcic_ble_rm_device_resolving_list(uint8_t addr_type_peer,
586                                              const RawAddress& bda_peer) {
587   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
588   uint8_t* pp = (uint8_t*)(p + 1);
589
590   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_RM_DEV_RESOLVING_LIST;
591   p->offset = 0;
592
593   UINT16_TO_STREAM(pp, HCI_BLE_RM_DEV_RESOLVING_LIST);
594   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_RM_DEV_RESOLVING_LIST);
595   UINT8_TO_STREAM(pp, addr_type_peer);
596   BDADDR_TO_STREAM(pp, bda_peer);
597
598   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
599 }
600
601 void btsnd_hcic_ble_set_privacy_mode(uint8_t addr_type_peer,
602                                      const RawAddress& bda_peer,
603                                      uint8_t privacy_type) {
604   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
605   uint8_t* pp = (uint8_t*)(p + 1);
606
607   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_SET_PRIVACY_MODE;
608   p->offset = 0;
609
610   UINT16_TO_STREAM(pp, HCI_BLE_SET_PRIVACY_MODE);
611   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_SET_PRIVACY_MODE);
612   UINT8_TO_STREAM(pp, addr_type_peer);
613   BDADDR_TO_STREAM(pp, bda_peer);
614   UINT8_TO_STREAM(pp, privacy_type);
615
616   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
617 }
618
619 void btsnd_hcic_ble_clear_resolving_list(void) {
620   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
621   uint8_t* pp = (uint8_t*)(p + 1);
622
623   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_CLEAR_RESOLVING_LIST;
624   p->offset = 0;
625
626   UINT16_TO_STREAM(pp, HCI_BLE_CLEAR_RESOLVING_LIST);
627   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_CLEAR_RESOLVING_LIST);
628
629   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
630 }
631
632 void btsnd_hcic_ble_read_resolvable_addr_peer(uint8_t addr_type_peer,
633                                               const RawAddress& bda_peer) {
634   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
635   uint8_t* pp = (uint8_t*)(p + 1);
636
637   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_READ_RESOLVABLE_ADDR_PEER;
638   p->offset = 0;
639
640   UINT16_TO_STREAM(pp, HCI_BLE_READ_RESOLVABLE_ADDR_PEER);
641   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_READ_RESOLVABLE_ADDR_PEER);
642   UINT8_TO_STREAM(pp, addr_type_peer);
643   BDADDR_TO_STREAM(pp, bda_peer);
644
645   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
646 }
647
648 void btsnd_hcic_ble_read_resolvable_addr_local(uint8_t addr_type_peer,
649                                                const RawAddress& bda_peer) {
650   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
651   uint8_t* pp = (uint8_t*)(p + 1);
652
653   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_READ_RESOLVABLE_ADDR_LOCAL;
654   p->offset = 0;
655
656   UINT16_TO_STREAM(pp, HCI_BLE_READ_RESOLVABLE_ADDR_LOCAL);
657   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_READ_RESOLVABLE_ADDR_LOCAL);
658   UINT8_TO_STREAM(pp, addr_type_peer);
659   BDADDR_TO_STREAM(pp, bda_peer);
660
661   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
662 }
663
664 void btsnd_hcic_ble_set_addr_resolution_enable(uint8_t addr_resolution_enable) {
665   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
666   uint8_t* pp = (uint8_t*)(p + 1);
667
668   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_SET_ADDR_RESOLUTION_ENABLE;
669   p->offset = 0;
670
671   UINT16_TO_STREAM(pp, HCI_BLE_SET_ADDR_RESOLUTION_ENABLE);
672   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_SET_ADDR_RESOLUTION_ENABLE);
673   UINT8_TO_STREAM(pp, addr_resolution_enable);
674
675   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
676 }
677
678 void btsnd_hcic_ble_set_rand_priv_addr_timeout(uint16_t rpa_timout) {
679   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
680   uint8_t* pp = (uint8_t*)(p + 1);
681
682   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_SET_RAND_PRIV_ADDR_TIMOUT;
683   p->offset = 0;
684
685   UINT16_TO_STREAM(pp, HCI_BLE_SET_RAND_PRIV_ADDR_TIMOUT);
686   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_SET_RAND_PRIV_ADDR_TIMOUT);
687   UINT16_TO_STREAM(pp, rpa_timout);
688
689   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
690 }
691
692 void btsnd_hcic_ble_set_data_length(uint16_t conn_handle, uint16_t tx_octets,
693                                     uint16_t tx_time) {
694   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
695   uint8_t* pp = (uint8_t*)(p + 1);
696
697   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_SET_DATA_LENGTH;
698   p->offset = 0;
699
700   UINT16_TO_STREAM(pp, HCI_BLE_SET_DATA_LENGTH);
701   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_SET_DATA_LENGTH);
702
703   UINT16_TO_STREAM(pp, conn_handle);
704   UINT16_TO_STREAM(pp, tx_octets);
705   UINT16_TO_STREAM(pp, tx_time);
706
707   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
708 }
709
710 void btsnd_hcic_ble_enh_rx_test(uint8_t rx_chan, uint8_t phy,
711                                 uint8_t mod_index) {
712   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
713   uint8_t* pp = (uint8_t*)(p + 1);
714
715   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_ENH_RX_TEST;
716   p->offset = 0;
717
718   UINT16_TO_STREAM(pp, HCI_BLE_ENH_RECEIVER_TEST);
719   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_ENH_RX_TEST);
720
721   UINT8_TO_STREAM(pp, rx_chan);
722   UINT8_TO_STREAM(pp, phy);
723   UINT8_TO_STREAM(pp, mod_index);
724
725   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
726 }
727
728 void btsnd_hcic_ble_enh_tx_test(uint8_t tx_chan, uint8_t data_len,
729                                 uint8_t payload, uint8_t phy) {
730   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
731   uint8_t* pp = (uint8_t*)(p + 1);
732
733   p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_ENH_TX_TEST;
734   p->offset = 0;
735
736   UINT16_TO_STREAM(pp, HCI_BLE_ENH_TRANSMITTER_TEST);
737   UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_BLE_ENH_TX_TEST);
738   UINT8_TO_STREAM(pp, tx_chan);
739   UINT8_TO_STREAM(pp, data_len);
740   UINT8_TO_STREAM(pp, payload);
741   UINT8_TO_STREAM(pp, phy);
742
743   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
744 }
745
746 void btsnd_hcic_ble_set_extended_scan_params(uint8_t own_address_type,
747                                              uint8_t scanning_filter_policy,
748                                              uint8_t scanning_phys,
749                                              scanning_phy_cfg* phy_cfg) {
750   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
751   uint8_t* pp = (uint8_t*)(p + 1);
752
753   int phy_cnt =
754       std::bitset<std::numeric_limits<uint8_t>::digits>(scanning_phys).count();
755
756   uint16_t param_len = 3 + (5 * phy_cnt);
757   p->len = HCIC_PREAMBLE_SIZE + param_len;
758   p->offset = 0;
759
760   UINT16_TO_STREAM(pp, HCI_LE_SET_EXTENDED_SCAN_PARAMETERS);
761   UINT8_TO_STREAM(pp, param_len);
762
763   UINT8_TO_STREAM(pp, own_address_type);
764   UINT8_TO_STREAM(pp, scanning_filter_policy);
765   UINT8_TO_STREAM(pp, scanning_phys);
766
767   for (int i = 0; i < phy_cnt; i++) {
768     UINT8_TO_STREAM(pp, phy_cfg[i].scan_type);
769     UINT16_TO_STREAM(pp, phy_cfg[i].scan_int);
770     UINT16_TO_STREAM(pp, phy_cfg[i].scan_win);
771   }
772
773   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
774 }
775
776 void btsnd_hcic_ble_set_extended_scan_enable(uint8_t enable,
777                                              uint8_t filter_duplicates,
778                                              uint16_t duration,
779                                              uint16_t period) {
780   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
781   uint8_t* pp = (uint8_t*)(p + 1);
782
783   const int param_len = 6;
784   p->len = HCIC_PREAMBLE_SIZE + param_len;
785   p->offset = 0;
786
787   UINT16_TO_STREAM(pp, HCI_LE_SET_EXTENDED_SCAN_ENABLE);
788   UINT8_TO_STREAM(pp, param_len);
789
790   UINT8_TO_STREAM(pp, enable);
791   UINT8_TO_STREAM(pp, filter_duplicates);
792   UINT16_TO_STREAM(pp, duration);
793   UINT16_TO_STREAM(pp, period);
794
795   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
796 }
797
798 void btsnd_hcic_ble_ext_create_conn(uint8_t init_filter_policy,
799                                     uint8_t addr_type_own,
800                                     uint8_t addr_type_peer,
801                                     const RawAddress& bda_peer,
802                                     uint8_t initiating_phys,
803                                     EXT_CONN_PHY_CFG* phy_cfg) {
804   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
805   uint8_t* pp = (uint8_t*)(p + 1);
806
807   int phy_cnt =
808       std::bitset<std::numeric_limits<uint8_t>::digits>(initiating_phys)
809           .count();
810
811   /* param_len = initial_params + size_per_channel * num_of_channels */
812   uint8_t param_len = 10 + (16 * phy_cnt);
813
814   p->len = HCIC_PREAMBLE_SIZE + param_len;
815   p->offset = 0;
816
817   UINT16_TO_STREAM(pp, HCI_LE_EXTENDED_CREATE_CONNECTION);
818   UINT8_TO_STREAM(pp, param_len);
819
820   UINT8_TO_STREAM(pp, init_filter_policy);
821   UINT8_TO_STREAM(pp, addr_type_own);
822   UINT8_TO_STREAM(pp, addr_type_peer);
823   BDADDR_TO_STREAM(pp, bda_peer);
824
825   UINT8_TO_STREAM(pp, initiating_phys);
826
827   for (int i = 0; i < phy_cnt; i++) {
828     UINT16_TO_STREAM(pp, phy_cfg[i].scan_int);
829     UINT16_TO_STREAM(pp, phy_cfg[i].scan_win);
830     UINT16_TO_STREAM(pp, phy_cfg[i].conn_int_min);
831     UINT16_TO_STREAM(pp, phy_cfg[i].conn_int_max);
832     UINT16_TO_STREAM(pp, phy_cfg[i].conn_latency);
833     UINT16_TO_STREAM(pp, phy_cfg[i].sup_timeout);
834     UINT16_TO_STREAM(pp, phy_cfg[i].min_ce_len);
835     UINT16_TO_STREAM(pp, phy_cfg[i].max_ce_len);
836   }
837
838   btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
839 }