1 /*
2  * Copyright (C) 2016 YunOS Project. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <ble_os.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <bt_errno.h>
21 #include <atomic.h>
22 #include <misc/util.h>
23 #include <misc/slist.h>
24 #include <misc/byteorder.h>
25 #include <misc/stack.h>
26 #include <misc/__assert.h>
27 
28 #include <settings/settings.h>
29 
30 #include <bluetooth/bluetooth.h>
31 #include <bluetooth/conn.h>
32 #include <bluetooth/l2cap.h>
33 #include <bluetooth/hci.h>
34 #include <bluetooth/hci_vs.h>
35 #include <bluetooth/hci_driver.h>
36 
37 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_CORE)
38 #include "common/log.h"
39 
40 #include "common/rpa.h"
41 #include "keys.h"
42 #include "monitor.h"
43 #include "hci_core.h"
44 #include "hci_ecc.h"
45 #include "ecc.h"
46 
47 #include "conn_internal.h"
48 #include "l2cap_internal.h"
49 #include "gatt_internal.h"
50 #include "smp.h"
51 #include "bluetooth/crypto.h"
52 #include "settings.h"
53 //#include "soc.h" //only for phy6220
54 #include "hci_api.h"
55 
56 #if defined(CONFIG_BT_USE_HCI_API)
57 
58 #define __hci_api_weak__ __attribute__((weak))
59 
60 struct hci_debug_counter_t g_hci_debug_counter = {0};
61 
62 struct cmd_state_set {
63 	atomic_t *target;
64 	int bit;
65 	bool val;
66 };
67 
68 struct cmd_data {
69 	/** HCI status of the command completion */
70 	u8_t  status;
71 
72 	/** The command OpCode that the buffer contains */
73 	u16_t opcode;
74 
75 	/** The state to update when command completes with success. */
76 	struct cmd_state_set *state;
77 
78 	/** Used by bt_hci_cmd_send_sync. */
79 	struct k_sem *sync;
80 };
81 
82 extern void cmd_state_set_init(struct cmd_state_set *state, atomic_t *target, int bit,
83 			bool val);
84 static struct cmd_data cmd_data[CONFIG_BT_HCI_CMD_COUNT];
85 
86 #define cmd(buf) (&cmd_data[net_buf_id(buf)])
87 
88 #define HCI_CMD_TIMEOUT	  K_SECONDS(10)
89 
90 
91 /* HCI command buffers. Derive the needed size from BT_BUF_RX_SIZE since
92  * the same buffer is also used for the response.
93  */
94 #define CMD_BUF_SIZE BT_BUF_RX_SIZE
95 NET_BUF_POOL_DEFINE(hci_cmd_pool, CONFIG_BT_HCI_CMD_COUNT,
96                     CMD_BUF_SIZE, BT_BUF_USER_DATA_MIN, NULL);
97 
98 
99 /* Number of commands controller can accept */
100 static struct k_sem		ncmd_sem;
101 
102 /* Last sent HCI command */
103 static struct net_buf		*sent_cmd;
104 
105 /* Queue for outgoing HCI commands */
106 static struct kfifo		     cmd_tx_queue;
107 
handle_event(u8_t event,struct net_buf * buf,const struct event_handler * handlers,size_t num_handlers)108 static inline void handle_event(u8_t event, struct net_buf *buf,
109 				const struct event_handler *handlers,
110 				size_t num_handlers)
111 {
112 	size_t i;
113 
114 	for (i = 0; i < num_handlers; i++) {
115 		const struct event_handler *handler = &handlers[i];
116 
117 		if (handler->event != event) {
118 			continue;
119 		}
120 
121 		if (buf->len < handler->min_len) {
122 			BT_ERR("Too small (%u bytes) event 0x%02x",
123 			       buf->len, event);
124 			return;
125 		}
126 
127 		handler->handler(buf);
128 		return;
129 	}
130 
131 	BT_WARN("Unhandled event 0x%02x len %u: %s", event,
132 		buf->len, bt_hex(buf->data, buf->len));
133 }
134 
hci_num_completed_packets(struct net_buf * buf)135 void hci_num_completed_packets(struct net_buf *buf)
136 {
137 	struct bt_hci_evt_num_completed_packets *evt = (void *)buf->data;
138 	int i;
139 
140 	BT_DBG("num_handles %u", evt->num_handles);
141 
142 	for (i = 0; i < evt->num_handles; i++) {
143 		u16_t handle, count;
144 
145 		handle = sys_le16_to_cpu(evt->h[i].handle);
146 		count = sys_le16_to_cpu(evt->h[i].count);
147 
148 		hci_api_num_complete_packets(1, &handle, &count);
149 	}
150 }
151 
bt_buf_get_cmd_complete(k_timeout_t timeout)152 struct net_buf *bt_buf_get_cmd_complete(k_timeout_t timeout)
153 {
154 	struct net_buf *buf;
155 	unsigned int key;
156 
157 	key = irq_lock();
158 	buf = sent_cmd;
159 	sent_cmd = NULL;
160 	irq_unlock(key);
161 
162 	BT_DBG("sent_cmd %p", buf);
163 
164 	if (buf) {
165 		bt_buf_set_type(buf, BT_BUF_EVT);
166 		buf->len = 0U;
167 		net_buf_reserve(buf, BT_BUF_RESERVE);
168 
169 		return buf;
170 	}
171 
172 	return bt_buf_get_rx(BT_BUF_EVT, timeout);
173 }
174 
hci_cmd_done(u16_t opcode,u8_t status,struct net_buf * buf)175 static void hci_cmd_done(u16_t opcode, u8_t status, struct net_buf *buf)
176 {
177     BT_DBG("opcode 0x%04x status 0x%02x buf %p", opcode, status, buf);
178 
179     if (net_buf_pool_get(buf->pool_id) != &hci_cmd_pool) {
180         BT_WARN("opcode 0x%04x pool id %u pool %p != &hci_cmd_pool %p",
181                 opcode, buf->pool_id, net_buf_pool_get(buf->pool_id),
182                 &hci_cmd_pool);
183         return;
184     }
185 
186     if (cmd(buf)->opcode != opcode) {
187         BT_WARN("OpCode 0x%04x completed instead of expected 0x%04x",
188                 opcode, cmd(buf)->opcode);
189     }
190 
191 	if (cmd(buf)->state && !status) {
192 		struct cmd_state_set *update = cmd(buf)->state;
193 
194 		atomic_set_bit_to(update->target, update->bit, update->val);
195 	}
196 
197     /* If the command was synchronous wake up bt_hci_cmd_send_sync() */
198     if (cmd(buf)->sync) {
199         cmd(buf)->status = status;
200         k_sem_give(cmd(buf)->sync);
201     }
202 }
203 
hci_cmd_complete(struct net_buf * buf)204 void hci_cmd_complete(struct net_buf *buf)
205 {
206 	struct bt_hci_evt_cmd_complete *evt;
207 	u8_t status, ncmd;
208 	u16_t opcode;
209 
210 	evt = net_buf_pull_mem(buf, sizeof(*evt));
211 	ncmd = evt->ncmd;
212 	opcode = sys_le16_to_cpu(evt->opcode);
213 
214 	BT_DBG("opcode 0x%04x", opcode);
215 
216 	/* All command return parameters have a 1-byte status in the
217 	 * beginning, so we can safely make this generalization.
218 	 */
219 	status = buf->data[0];
220 
221 	hci_cmd_done(opcode, status, buf);
222 
223 	/* Allow next command to be sent */
224 	if (ncmd) {
225 		k_sem_give(&ncmd_sem);
226 	}
227 }
228 
hci_cmd_status(struct net_buf * buf)229 void hci_cmd_status(struct net_buf *buf)
230 {
231 	struct bt_hci_evt_cmd_status *evt;
232 	u16_t opcode;
233 	u8_t ncmd;
234 
235 	evt = net_buf_pull_mem(buf, sizeof(*evt));
236 	opcode = sys_le16_to_cpu(evt->opcode);
237 	ncmd = evt->ncmd;
238 
239 	BT_DBG("opcode 0x%04x", opcode);
240 
241 	hci_cmd_done(opcode, evt->status, buf);
242 
243 	/* Allow next command to be sent */
244 	if (ncmd) {
245 		k_sem_give(&ncmd_sem);
246 	}
247 }
248 
send_cmd(void)249 void send_cmd(void)
250 {
251 	struct net_buf *buf;
252 	int err;
253 
254 	/* Get next command */
255 	BT_DBG("calling net_buf_get");
256 	buf = net_buf_get(&cmd_tx_queue, K_NO_WAIT);
257 	BT_ASSERT(buf);
258 
259 	/* Wait until ncmd > 0 */
260 	BT_DBG("calling sem_take_wait");
261 	k_sem_take(&ncmd_sem, K_FOREVER);
262 
263 	/* Clear out any existing sent command */
264 	if (sent_cmd) {
265 		BT_ERR("Uncleared pending sent_cmd");
266 		net_buf_unref(sent_cmd);
267 		sent_cmd = NULL;
268 	}
269 
270 	sent_cmd = net_buf_ref(buf);
271 
272 	BT_DBG("Sending command 0x%04x (buf %p) to driver",
273 	       cmd(buf)->opcode, buf);
274 
275 	err = bt_send(buf);
276 	if (err) {
277 		BT_ERR("Unable to send to driver (err %d)", err);
278 		k_sem_give(&ncmd_sem);
279 		hci_cmd_done(cmd(buf)->opcode, BT_HCI_ERR_UNSPECIFIED, buf);
280 		net_buf_unref(sent_cmd);
281 		sent_cmd = NULL;
282 		net_buf_unref(buf);
283 	}
284 }
285 
bt_hci_get_cmd_opcode(struct net_buf * buf)286 u16_t bt_hci_get_cmd_opcode(struct net_buf *buf)
287 {
288     return cmd(buf)->opcode;
289 }
290 
bt_hci_cmd_create(u16_t opcode,u8_t param_len)291 struct net_buf *bt_hci_cmd_create(u16_t opcode, u8_t param_len)
292 {
293     struct bt_hci_cmd_hdr *hdr;
294     struct net_buf *buf;
295 
296     BT_DBG("opcode 0x%04x param_len %u", opcode, param_len);
297 
298     buf = net_buf_alloc(&hci_cmd_pool, K_FOREVER);
299     __ASSERT_NO_MSG(buf);
300 
301     BT_DBG("buf %p", buf);
302 
303 	net_buf_reserve(buf, BT_BUF_RESERVE);
304 
305     bt_buf_set_type(buf, BT_BUF_CMD);
306 
307 	cmd(buf)->opcode = opcode;
308 	cmd(buf)->sync = NULL;
309 	cmd(buf)->state = NULL;
310 
311 	hdr = net_buf_add(buf, sizeof(*hdr));
312 	hdr->opcode = sys_cpu_to_le16(opcode);
313 	hdr->param_len = param_len;
314 
315 	return buf;
316 }
317 
bt_hci_cmd_send(u16_t opcode,struct net_buf * buf)318 int bt_hci_cmd_send(u16_t opcode, struct net_buf *buf)
319 {
320 	if (!buf) {
321 		buf = bt_hci_cmd_create(opcode, 0);
322 		if (!buf) {
323 			return -ENOBUFS;
324 		}
325 	}
326 
327 	BT_DBG("opcode 0x%04x len %u", opcode, buf->len);
328 
329     /* Host Number of Completed Packets can ignore the ncmd value
330      * and does not generate any cmd complete/status events.
331      */
332     if (opcode == BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS) {
333         int err;
334 
335         err = bt_send(buf);
336         if (err) {
337             BT_ERR("Unable to send to driver (err %d)", err);
338             net_buf_unref(buf);
339         }
340 
341         return err;
342     }
343 
344     net_buf_put(&cmd_tx_queue, buf);
345 
346     return 0;
347 }
348 
bt_hci_cmd_send_sync(u16_t opcode,struct net_buf * buf,struct net_buf ** rsp)349 int bt_hci_cmd_send_sync(u16_t opcode, struct net_buf *buf,
350 			 struct net_buf **rsp)
351 {
352 	struct k_sem sync_sem;
353 	u8_t status;
354 	int err;
355 
356 	if (!buf) {
357 		buf = bt_hci_cmd_create(opcode, 0);
358 		if (!buf) {
359 			return -ENOBUFS;
360 		}
361 	}
362 
363 	BT_DBG("buf %p opcode 0x%04x len %u", buf, opcode, buf->len);
364 
365 	k_sem_init(&sync_sem, 0, 1);
366 	cmd(buf)->sync = &sync_sem;
367 
368 	/* Make sure the buffer stays around until the command completes */
369 	net_buf_ref(buf);
370 
371 	net_buf_put(&cmd_tx_queue, buf);
372 
373 	err = k_sem_take(&sync_sem, HCI_CMD_TIMEOUT);
374 	BT_ASSERT_MSG(err == 0, "k_sem_take failed with err %d", err);
375 
376     k_sem_delete(&sync_sem);
377 
378 	status = cmd(buf)->status;
379 	if (status) {
380 		BT_WARN("opcode 0x%04x status 0x%02x", opcode, status);
381 		net_buf_unref(buf);
382 
383 		switch (status) {
384 		case BT_HCI_ERR_CONN_LIMIT_EXCEEDED:
385 			return -ECONNREFUSED;
386 		default:
387 			return -EIO;
388 		}
389 	}
390 
391 	BT_DBG("rsp %p opcode 0x%04x len %u", buf, opcode, buf->len);
392 
393 	if (rsp) {
394 		*rsp = buf;
395 	} else {
396 		net_buf_unref(buf);
397 	}
398 
399 	return 0;
400 }
401 
hci_api_le_scan_enable(u8_t enable,u8_t filter_dup)402 __hci_api_weak__ int hci_api_le_scan_enable(u8_t enable, u8_t filter_dup)
403 {
404     struct bt_hci_cp_le_set_scan_enable *cp;
405     struct net_buf *buf;
406     int err;
407 
408     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_ENABLE, sizeof(*cp));
409 
410     if (!buf) {
411         return -ENOBUFS;
412     }
413 
414     cp = net_buf_add(buf, sizeof(*cp));
415 
416     if (enable == BT_HCI_LE_SCAN_ENABLE) {
417         cp->filter_dup = atomic_test_bit(bt_dev.flags,
418                                          BT_DEV_SCAN_FILTER_DUP);
419     } else {
420         cp->filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_DISABLE;
421     }
422 
423     cp->enable = enable;
424 
425     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_SCAN_ENABLE, buf, NULL);
426 
427     return err;
428 }
429 
hci_api_le_scan_param_set(u8_t scan_type,u16_t interval,u16_t window,u8_t addr_type,u8_t filter_policy)430 __hci_api_weak__  int hci_api_le_scan_param_set(u8_t scan_type, u16_t interval, u16_t window, u8_t addr_type, u8_t filter_policy)
431 {
432     struct bt_hci_cp_le_set_scan_param set_param;
433     struct net_buf *buf;
434 
435     memset(&set_param, 0, sizeof(set_param));
436 
437     set_param.scan_type = scan_type;
438     set_param.addr_type = addr_type;
439 
440     /* for the rest parameters apply default values according to
441      *  spec 4.2, vol2, part E, 7.8.10
442      */
443     set_param.interval = sys_cpu_to_le16(interval);
444     set_param.window = sys_cpu_to_le16(window);
445     set_param.filter_policy = filter_policy;
446 
447     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_PARAM, sizeof(set_param));
448 
449     if (!buf) {
450         return -ENOBUFS;
451     }
452 
453     net_buf_add_mem(buf, &set_param, sizeof(set_param));
454 
455     bt_hci_cmd_send(BT_HCI_OP_LE_SET_SCAN_PARAM, buf);
456 
457     return 0;
458 }
459 
hci_api_le_get_max_data_len(uint16_t * tx_octets,uint16_t * tx_time)460 __hci_api_weak__  int hci_api_le_get_max_data_len(uint16_t *tx_octets, uint16_t *tx_time)
461 {
462     struct bt_hci_rp_le_read_max_data_len *rp;
463     struct net_buf *rsp;
464     int err;
465 
466     if (tx_octets == NULL || tx_time == NULL) {
467         return -EINVAL;
468     }
469 
470     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_MAX_DATA_LEN, NULL, &rsp);
471 
472     if (err) {
473         return err;
474     }
475 
476     rp = (void *)rsp->data;
477     *tx_octets = sys_le16_to_cpu(rp->max_tx_octets);
478     *tx_time = sys_le16_to_cpu(rp->max_tx_time);
479     net_buf_unref(rsp);
480     return 0;
481 }
482 
hci_api_le_get_default_data_len(uint16_t * tx_octets,uint16_t * tx_time)483 __hci_api_weak__  int hci_api_le_get_default_data_len(uint16_t *tx_octets, uint16_t *tx_time)
484 {
485     struct bt_hci_rp_le_read_default_data_len *rp;
486     struct net_buf *rsp;
487     int err;
488 
489     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_DEFAULT_DATA_LEN, NULL, &rsp);
490 
491     if (err) {
492         return err;
493     }
494 
495     rp = (void *)rsp->data;
496     *tx_octets = sys_le16_to_cpu(rp->max_tx_octets);
497     *tx_time = sys_le16_to_cpu(rp->max_tx_time);
498     net_buf_unref(rsp);
499 
500     return 0;
501 }
502 
hci_api_le_set_default_data_len(uint16_t tx_octets,uint16_t tx_time)503 __hci_api_weak__  int hci_api_le_set_default_data_len(uint16_t tx_octets, uint16_t tx_time)
504 {
505     struct bt_hci_cp_le_write_default_data_len *cp;
506     struct net_buf *buf;
507     int err;
508 
509     buf = bt_hci_cmd_create(BT_HCI_OP_LE_WRITE_DEFAULT_DATA_LEN, sizeof(*cp));
510 
511     if (!buf) {
512         return -ENOBUFS;
513     }
514 
515     cp = net_buf_add(buf, sizeof(*cp));
516     cp->max_tx_octets = sys_cpu_to_le16(tx_octets);
517     cp->max_tx_time = sys_cpu_to_le16(tx_time);
518     err = bt_hci_cmd_send(BT_HCI_OP_LE_WRITE_DEFAULT_DATA_LEN, buf);
519 
520     if (err) {
521         return err;
522     }
523 
524     return 0;
525 }
526 
hci_api_le_set_data_len(int16_t conn_handle,uint16_t tx_octets,uint16_t tx_time)527 __hci_api_weak__  int hci_api_le_set_data_len(int16_t conn_handle, uint16_t tx_octets, uint16_t tx_time)
528 {
529     struct bt_hci_cp_le_set_data_len *cp;
530     struct net_buf *buf;
531     int err;
532 
533     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_DATA_LEN, sizeof(*cp));
534 
535     if (!buf) {
536         return -ENOBUFS;
537     }
538 
539     cp = net_buf_add(buf, sizeof(*cp));
540     cp->handle = sys_cpu_to_le16(conn_handle);
541     cp->tx_octets = sys_cpu_to_le16(tx_octets);
542     cp->tx_time = sys_cpu_to_le16(tx_time);
543     err = bt_hci_cmd_send(BT_HCI_OP_LE_SET_DATA_LEN, buf);
544 
545     if (err) {
546         return err;
547     }
548 
549     return 0;
550 }
551 
hci_vs_set_bd_add(uint8_t addr[6])552 static int hci_vs_set_bd_add(uint8_t addr[6])
553 {
554 #if defined(CONFIG_BT_HCI_VS_EXT)
555     struct net_buf *buf;
556     int err;
557 
558     buf = bt_hci_cmd_create(BT_HCI_OP_VS_WRITE_BD_ADDR, sizeof(*addr));
559 
560     if (!buf) {
561         return -ENOBUFS;
562     }
563 
564     net_buf_add_mem(buf, addr, sizeof(*addr));
565 
566     err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_WRITE_BD_ADDR, buf, NULL);
567 
568     if (err) {
569         return err;
570     }
571 
572     return 0;
573 #else
574     return -ENOTSUP;
575 #endif
576 }
577 
hci_api_le_set_bdaddr(uint8_t bdaddr[6])578 __hci_api_weak__  int hci_api_le_set_bdaddr(uint8_t bdaddr[6])
579 {
580     return hci_vs_set_bd_add(bdaddr);
581 }
582 
hci_api_reset()583 __hci_api_weak__  int hci_api_reset()
584 {
585     int err;
586     struct net_buf *rsp;
587     u8_t status;
588     /* Send HCI_RESET */
589     err = bt_hci_cmd_send_sync(BT_HCI_OP_RESET, NULL, &rsp);
590 
591     if (err) {
592         return err;
593     }
594 
595     status = rsp->data[0];
596     net_buf_unref(rsp);
597     return status;
598 }
599 
hci_api_read_local_feature(uint8_t feature[8])600 __hci_api_weak__  int hci_api_read_local_feature(uint8_t feature[8])
601 {
602     int err;
603     struct net_buf *rsp;
604 
605     err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_LOCAL_FEATURES, NULL, &rsp);
606 
607     if (err) {
608         return err;
609     }
610 
611     struct bt_hci_rp_read_local_features *rp = (void *)rsp->data;
612 
613     if (rp->status) {
614         net_buf_unref(rsp);
615         return rp->status;
616     }
617 
618     memcpy(feature, rp->features, 8);
619     net_buf_unref(rsp);
620     return 0;
621 }
622 
hci_api_read_local_version_info(uint8_t * hci_version,uint8_t * lmp_version,uint16_t * hci_revision,uint16_t * lmp_subversion,uint16_t * manufacturer)623 __hci_api_weak__  int hci_api_read_local_version_info(uint8_t   *hci_version,
624         uint8_t            *lmp_version,
625         uint16_t               *hci_revision,
626         uint16_t               *lmp_subversion,
627         uint16_t           *manufacturer)
628 {
629     int err;
630     struct net_buf *rsp;
631 
632     err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_LOCAL_VERSION_INFO, NULL, &rsp);
633 
634     if (err) {
635         return err;
636     }
637 
638     struct bt_hci_rp_read_local_version_info *rp = (void *)rsp->data;
639 
640     if (rp->status) {
641         net_buf_unref(rsp);
642         return rp->status;
643     }
644 
645     *hci_version = rp->hci_version;
646     *lmp_version = rp->lmp_version;
647     *hci_revision = rp->hci_revision;
648     *lmp_subversion = rp->lmp_subversion;
649     *manufacturer = rp->manufacturer;
650     net_buf_unref(rsp);
651     return 0;
652 }
hci_api_read_bdaddr(u8_t bdaddr[6])653 __hci_api_weak__  int hci_api_read_bdaddr(u8_t bdaddr[6])
654 {
655     int err;
656     struct net_buf *rsp;
657 
658     err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_BD_ADDR, NULL, &rsp);
659 
660     if (err) {
661         return err;
662     }
663 
664     struct bt_hci_rp_read_bd_addr *rp = (void *)rsp->data;
665 
666     if (rp->status) {
667         net_buf_unref(rsp);
668         return rp->status;
669     }
670 
671     if (!bt_addr_cmp(&rp->bdaddr, BT_ADDR_ANY) ||
672         !bt_addr_cmp(&rp->bdaddr, BT_ADDR_NONE)) {
673         net_buf_unref(rsp);
674         return -ENODATA;
675     }
676 
677     memcpy(bdaddr, &rp->bdaddr, 6);
678     net_buf_unref(rsp);
679     return 0;
680 }
681 
hci_api_read_local_support_command(uint8_t supported_commands[64])682 __hci_api_weak__  int hci_api_read_local_support_command(uint8_t supported_commands[64])
683 {
684     int err;
685     struct net_buf *rsp;
686 
687     err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_SUPPORTED_COMMANDS, NULL, &rsp);
688 
689     if (err) {
690         return err;
691     }
692 
693     struct bt_hci_rp_read_supported_commands *rp = (void *)rsp->data;
694 
695     if (rp->status) {
696         net_buf_unref(rsp);
697         return rp->status;
698     }
699 
700     memcpy(supported_commands, rp->commands, 64);
701     net_buf_unref(rsp);
702     return 0;
703 }
704 
hci_api_set_host_buffer_size(uint16_t acl_mtu,uint8_t sco_mtu,uint16_t acl_pkts,uint16_t sco_pkts)705 __hci_api_weak__  int hci_api_set_host_buffer_size(uint16_t acl_mtu, uint8_t  sco_mtu,
706         uint16_t acl_pkts, uint16_t sco_pkts)
707 {
708     struct bt_hci_cp_host_buffer_size *hbs;
709     struct net_buf *buf;
710 
711     buf = bt_hci_cmd_create(BT_HCI_OP_HOST_BUFFER_SIZE, sizeof(*hbs));
712 
713     if (!buf) {
714         return -ENOBUFS;
715     }
716 
717     hbs = net_buf_add(buf, sizeof(*hbs));
718     memset(hbs, 0, sizeof(*hbs));
719     hbs->acl_mtu = sys_cpu_to_le16(acl_mtu);
720     hbs->acl_pkts = sys_cpu_to_le16(acl_pkts);
721 
722     return bt_hci_cmd_send_sync(BT_HCI_OP_HOST_BUFFER_SIZE, buf, NULL);
723 }
724 
hci_api_set_host_flow_enable(uint8_t enable)725 __hci_api_weak__  int hci_api_set_host_flow_enable(uint8_t enable)
726 {
727     struct net_buf *buf;
728 
729     buf = bt_hci_cmd_create(BT_HCI_OP_SET_CTL_TO_HOST_FLOW, 1);
730 
731     if (!buf) {
732         return -ENOBUFS;
733     }
734 
735     net_buf_add_u8(buf, enable);
736 
737     return bt_hci_cmd_send_sync(BT_HCI_OP_SET_CTL_TO_HOST_FLOW, buf, NULL);
738 }
739 
hci_api_le_read_local_feature(uint8_t feature[8])740 __hci_api_weak__  int hci_api_le_read_local_feature(uint8_t feature[8])
741 {
742     int err;
743     struct net_buf *rsp;
744 
745     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_LOCAL_FEATURES, NULL, &rsp);
746 
747     if (err) {
748         return err;
749     }
750 
751     struct bt_hci_rp_le_read_local_features *rp = (void *)rsp->data;
752 
753     if (rp->status) {
754         net_buf_unref(rsp);
755         return rp->status;
756     }
757 
758     memcpy(feature, rp->features, 8);
759     net_buf_unref(rsp);
760     return 0;
761 }
762 
hci_api_le_read_buffer_size_complete(uint16_t * le_max_len,uint8_t * le_max_num)763 __hci_api_weak__  int hci_api_le_read_buffer_size_complete(uint16_t *le_max_len, uint8_t *le_max_num)
764 {
765     int err;
766     struct net_buf *rsp;
767 
768     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_BUFFER_SIZE, NULL, &rsp);
769 
770     if (err) {
771         return err;
772     }
773 
774     struct bt_hci_rp_le_read_buffer_size *rp = (void *)rsp->data;
775 
776     if (rp->status) {
777         net_buf_unref(rsp);
778         return rp->status;
779     }
780 
781     *le_max_len = sys_le16_to_cpu(rp->le_max_len);
782     *le_max_num = rp->le_max_num;
783 
784     net_buf_unref(rsp);
785     return 0;
786 }
787 
hci_api_le_read_support_states(uint64_t * states)788 __hci_api_weak__  int hci_api_le_read_support_states(uint64_t *states)
789 {
790     int err;
791     struct net_buf *rsp;
792 
793     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_SUPP_STATES, NULL, &rsp);
794 
795     if (err) {
796         return err;
797     }
798 
799     struct bt_hci_rp_le_read_supp_states *rp = (void *)rsp->data;
800 
801     if (rp->status) {
802         net_buf_unref(rsp);
803         return rp->status;
804     }
805 
806     *states = sys_get_le64(rp->le_states);
807     net_buf_unref(rsp);
808     return 0;
809 }
810 
hci_api_le_read_rl_size(uint8_t * rl_size)811 __hci_api_weak__  int hci_api_le_read_rl_size(uint8_t *rl_size)
812 {
813     int err;
814     struct net_buf *rsp;
815 
816     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_RL_SIZE, NULL, &rsp);
817 
818     if (err) {
819         return err;
820     }
821 
822     struct bt_hci_rp_le_read_rl_size *rp = (void *)rsp->data;
823 
824     if (rp->status) {
825         net_buf_unref(rsp);
826         return rp->status;
827     }
828 
829     *rl_size = rp->rl_size;
830     net_buf_unref(rsp);
831     return 0;
832 }
833 
hci_api_le_set_event_mask(uint64_t mask)834 __hci_api_weak__  int hci_api_le_set_event_mask(uint64_t mask)
835 {
836     struct bt_hci_cp_le_set_event_mask *cp_mask;
837     struct net_buf *buf;
838 
839     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EVENT_MASK, sizeof(*cp_mask));
840 
841     if (!buf) {
842         return -ENOBUFS;
843     }
844 
845     cp_mask = net_buf_add(buf, sizeof(*cp_mask));
846     sys_put_le64(mask, cp_mask->events);
847     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EVENT_MASK, buf, NULL);
848 }
849 
hci_api_set_event_mask(uint64_t mask)850 __hci_api_weak__  int hci_api_set_event_mask(uint64_t mask)
851 {
852     struct bt_hci_cp_set_event_mask *ev;
853     struct net_buf *buf;
854 
855     buf = bt_hci_cmd_create(BT_HCI_OP_SET_EVENT_MASK, sizeof(*ev));
856 
857     if (!buf) {
858         return -ENOBUFS;
859     }
860 
861     ev = net_buf_add(buf, sizeof(*ev));
862     sys_put_le64(mask, ev->events);
863     return bt_hci_cmd_send_sync(BT_HCI_OP_SET_EVENT_MASK, buf, NULL);
864 }
865 
hci_api_num_complete_packets(u8_t num_handles,u16_t * handles,u16_t * counts)866 __hci_api_weak__ int hci_api_num_complete_packets(u8_t num_handles,
867         u16_t   *handles,
868         u16_t   *counts)
869 {
870     int i;
871     for (i = 0; i < num_handles; i++) {
872         u16_t handle, count;
873 
874         handle = handles[i];
875         count = counts[i];
876 
877         BT_DBG("handle %u count %u", handle, count);
878 
879         extern void bt_hci_num_complete_packets(u16_t handle, u16_t count);
880         bt_hci_num_complete_packets(handle, count);
881     }
882     return 0;
883 }
884 
hci_api_white_list_size(uint8_t * size)885 __hci_api_weak__ int hci_api_white_list_size(uint8_t *size)
886 {
887     int err;
888     struct net_buf *rsp;
889     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_WL_SIZE, NULL, &rsp);
890 
891     if (err) {
892         return err;
893     }
894 
895     struct bt_hci_rp_le_read_wl_size *rp = (void *)rsp->data;
896 
897     if (rp->status) {
898         net_buf_unref(rsp);
899         return rp->status;
900     }
901 
902     *size = rp->wl_size;
903 
904     net_buf_unref(rsp);
905     return 0;
906 }
907 
908 
hci_api_white_list_add(uint8_t peer_addr_type,uint8_t peer_addr[6])909 __hci_api_weak__ int hci_api_white_list_add(uint8_t peer_addr_type, uint8_t peer_addr[6])
910 {
911     struct  bt_hci_cp_le_add_dev_to_wl *wl_addr;
912     struct net_buf *buf;
913 
914     buf = bt_hci_cmd_create(BT_HCI_OP_LE_ADD_DEV_TO_WL, sizeof(*wl_addr));
915 
916     if(!buf) {
917         return -ENOBUFS;
918     }
919 
920     wl_addr = net_buf_add(buf, sizeof(*wl_addr));
921 
922     wl_addr->addr.type = peer_addr_type;
923     memcpy(wl_addr->addr.a.val,peer_addr,sizeof(wl_addr->addr.a.val));
924 
925     return  bt_hci_cmd_send_sync(BT_HCI_OP_LE_ADD_DEV_TO_WL, buf, NULL);
926 }
927 
hci_api_white_list_remove(uint8_t peer_addr_type,uint8_t peer_addr[6])928 __hci_api_weak__ int hci_api_white_list_remove(uint8_t peer_addr_type, uint8_t peer_addr[6])
929 {
930     struct  bt_hci_cp_le_add_dev_to_wl *wl_addr;
931     struct net_buf *buf;
932 
933     buf = bt_hci_cmd_create(BT_HCI_OP_LE_REM_DEV_FROM_WL, sizeof(*wl_addr));
934 
935     if(!buf) {
936         return -ENOBUFS;
937     }
938 
939     wl_addr = net_buf_add(buf, sizeof(*wl_addr));
940 
941     wl_addr->addr.type = peer_addr_type;
942     memcpy(wl_addr->addr.a.val,peer_addr,sizeof(wl_addr->addr.a.val));
943 
944     return  bt_hci_cmd_send_sync(BT_HCI_OP_LE_REM_DEV_FROM_WL, buf, NULL);
945 }
946 
hci_api_white_list_clear()947 __hci_api_weak__ int hci_api_white_list_clear()
948 {
949     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CLEAR_WL, NULL, NULL);
950 }
951 
952 
953 #if defined(CONFIG_BT_HCI_VS_EXT)
954 #if defined(CONFIG_BT_DEBUG)
vs_hw_platform(u16_t platform)955 static const char *vs_hw_platform(u16_t platform)
956 {
957     static const char *const plat_str[] = {
958         "reserved", "Intel Corporation", "Nordic Semiconductor",
959         "NXP Semiconductors"
960     };
961 
962     if (platform < ARRAY_SIZE(plat_str)) {
963         return plat_str[platform];
964     }
965 
966     return "unknown";
967 }
968 
vs_hw_variant(u16_t platform,u16_t variant)969 static const char *vs_hw_variant(u16_t platform, u16_t variant)
970 {
971     static const char *const nordic_str[] = {
972         "reserved", "nRF51x", "nRF52x"
973     };
974 
975     if (platform != BT_HCI_VS_HW_PLAT_NORDIC) {
976         return "unknown";
977     }
978 
979     if (variant < ARRAY_SIZE(nordic_str)) {
980         return nordic_str[variant];
981     }
982 
983     return "unknown";
984 }
985 
vs_fw_variant(u8_t variant)986 static const char *vs_fw_variant(u8_t variant)
987 {
988     static const char *const var_str[] = {
989         "Standard Bluetooth controller",
990         "Vendor specific controller",
991         "Firmware loader",
992         "Rescue image",
993     };
994 
995     if (variant < ARRAY_SIZE(var_str)) {
996         return var_str[variant];
997     }
998 
999     return "unknown";
1000 }
1001 #endif /* CONFIG_BT_DEBUG */
1002 
hci_vs_init(void)1003 static void hci_vs_init(void)
1004 {
1005     union {
1006         struct bt_hci_rp_vs_read_version_info *info;
1007         struct bt_hci_rp_vs_read_supported_commands *cmds;
1008         struct bt_hci_rp_vs_read_supported_features *feat;
1009     } rp;
1010     struct net_buf *rsp;
1011     int err;
1012 
1013     /* If heuristics is enabled, try to guess HCI VS support by looking
1014      * at the HCI version and identity address. We haven't tried to set
1015      * a static random address yet at this point, so the identity will
1016      * either be zeroes or a valid public address.
1017      */
1018     if (IS_ENABLED(CONFIG_BT_HCI_VS_EXT_DETECT) &&
1019         (bt_dev.hci_version < BT_HCI_VERSION_5_0 ||
1020          (!atomic_test_bit(bt_dev.flags, BT_DEV_USER_ID_ADDR) &&
1021           bt_addr_le_cmp(&bt_dev.id_addr[0], BT_ADDR_LE_ANY)))) {
1022         BT_WARN("Controller doesn't seem to support Zephyr vendor HCI");
1023         return;
1024     }
1025 
1026     err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_READ_VERSION_INFO, NULL, &rsp);
1027 
1028     if (err) {
1029         BT_WARN("Vendor HCI extensions not available");
1030         return;
1031     }
1032 
1033 #if defined(CONFIG_BT_DEBUG)
1034     rp.info = (void *)rsp->data;
1035     BT_INFO("HW Platform: %s (0x%04x)",
1036             vs_hw_platform(sys_le16_to_cpu(rp.info->hw_platform)),
1037             sys_le16_to_cpu(rp.info->hw_platform));
1038     BT_INFO("HW Variant: %s (0x%04x)",
1039             vs_hw_variant(sys_le16_to_cpu(rp.info->hw_platform),
1040                           sys_le16_to_cpu(rp.info->hw_variant)),
1041             sys_le16_to_cpu(rp.info->hw_variant));
1042     BT_INFO("Firmware: %s (0x%02x) Version %u.%u Build %u",
1043             vs_fw_variant(rp.info->fw_variant), rp.info->fw_variant,
1044             rp.info->fw_version, sys_le16_to_cpu(rp.info->fw_revision),
1045             sys_le32_to_cpu(rp.info->fw_build));
1046 #endif /* CONFIG_BT_DEBUG */
1047 
1048     net_buf_unref(rsp);
1049 
1050     err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_READ_SUPPORTED_COMMANDS,
1051                                NULL, &rsp);
1052 
1053     if (err) {
1054         BT_WARN("Failed to read supported vendor features");
1055         return;
1056     }
1057 
1058     rp.cmds = (void *)rsp->data;
1059     memcpy(bt_dev.vs_commands, rp.cmds->commands, BT_DEV_VS_CMDS_MAX);
1060     net_buf_unref(rsp);
1061 
1062     err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_READ_SUPPORTED_FEATURES,
1063                                NULL, &rsp);
1064 
1065     if (err) {
1066         BT_WARN("Failed to read supported vendor commands");
1067         return;
1068     }
1069 
1070     rp.feat = (void *)rsp->data;
1071     memcpy(bt_dev.vs_features, rp.feat->features, BT_DEV_VS_FEAT_MAX);
1072     net_buf_unref(rsp);
1073 }
1074 
1075 #endif /* CONFIG_BT_HCI_VS_EXT */
1076 
hci_api_vs_init()1077 __hci_api_weak__  int hci_api_vs_init()
1078 {
1079 #if defined(CONFIG_BT_HCI_VS_EXT)
1080     hci_vs_init();
1081     return 0;
1082 #else
1083     return 0;
1084 #endif
1085 }
1086 
hci_api_le_adv_enable(uint8_t enable)1087 __hci_api_weak__  int hci_api_le_adv_enable(uint8_t enable)
1088 {
1089     struct net_buf *buf;
1090 
1091     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_ENABLE, 1);
1092 
1093     if (!buf) {
1094         return -ENOBUFS;
1095     }
1096 
1097     net_buf_add_u8(buf, enable);
1098 
1099     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_ENABLE, buf, NULL);
1100 }
1101 
hci_api_le_adv_param(uint16_t min_interval,uint16_t max_interval,uint8_t type,uint8_t own_addr_type,uint8_t direct_addr_type,uint8_t direct_addr[6],uint8_t channel_map,uint8_t filter_policy)1102 __hci_api_weak__  int hci_api_le_adv_param(uint16_t        min_interval,
1103         uint16_t        max_interval,
1104         uint8_t         type,
1105         uint8_t         own_addr_type,
1106         uint8_t  direct_addr_type,
1107         uint8_t  direct_addr[6],
1108         uint8_t         channel_map,
1109         uint8_t         filter_policy)
1110 {
1111     struct bt_hci_cp_le_set_adv_param set_param;
1112     struct net_buf *buf;
1113 
1114     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_PARAM, sizeof(set_param));
1115 
1116     if (!buf) {
1117         return -ENOBUFS;
1118     }
1119 
1120     set_param.min_interval = min_interval;
1121     set_param.max_interval = max_interval;
1122     set_param.type = type;
1123     set_param.own_addr_type = own_addr_type;
1124     set_param.direct_addr.type = direct_addr_type;
1125     memcpy(set_param.direct_addr.a.val, direct_addr, 6);
1126     set_param.channel_map = channel_map;
1127     set_param.filter_policy = filter_policy;
1128     net_buf_add_mem(buf, &set_param, sizeof(set_param));
1129 
1130     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_PARAM, buf, NULL);
1131 }
1132 
hci_api_le_set_random_addr(const uint8_t addr[6])1133 __hci_api_weak__  int hci_api_le_set_random_addr(const uint8_t addr[6])
1134 {
1135     struct net_buf *buf;
1136 
1137     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, 6);
1138 
1139     if (!buf) {
1140         return -ENOBUFS;
1141     }
1142 
1143     net_buf_add_mem(buf, addr, 6);
1144 
1145     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, buf, NULL);
1146 }
1147 
hci_api_le_set_ad_data(uint8_t len,uint8_t data[31])1148 __hci_api_weak__  int hci_api_le_set_ad_data(uint8_t len, uint8_t data[31])
1149 {
1150     struct bt_hci_cp_le_set_adv_data *set_data;
1151     struct net_buf *buf;
1152     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_DATA, sizeof(*set_data));
1153 
1154     if (!buf) {
1155         return -ENOBUFS;
1156     }
1157 
1158     set_data = net_buf_add(buf, sizeof(*set_data));
1159     set_data->len = len;
1160     memcpy(set_data->data, data, 31);
1161     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_DATA, buf, NULL);
1162 }
1163 
hci_api_le_set_sd_data(uint8_t len,uint8_t data[31])1164 __hci_api_weak__  int hci_api_le_set_sd_data(uint8_t len, uint8_t data[31])
1165 {
1166     struct bt_hci_cp_le_set_adv_data *set_data;
1167     struct net_buf *buf;
1168     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(*set_data));
1169 
1170     if (!buf) {
1171         return -ENOBUFS;
1172     }
1173 
1174     set_data = net_buf_add(buf, sizeof(*set_data));
1175     set_data->len = len;
1176     memcpy(set_data->data, data, 31);
1177     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, buf, NULL);
1178 }
1179 
hci_api_le_create_conn(uint16_t scan_interval,uint16_t scan_window,uint8_t filter_policy,uint8_t peer_addr_type,const uint8_t peer_addr[6],uint8_t own_addr_type,uint16_t conn_interval_min,uint16_t conn_interval_max,uint16_t conn_latency,uint16_t supervision_timeout,uint16_t min_ce_len,uint16_t max_ce_len)1180 __hci_api_weak__ int hci_api_le_create_conn(uint16_t scan_interval,
1181         uint16_t scan_window,
1182         uint8_t filter_policy,
1183         uint8_t peer_addr_type,
1184         const uint8_t peer_addr[6],
1185         uint8_t own_addr_type,
1186         uint16_t conn_interval_min,
1187         uint16_t conn_interval_max,
1188         uint16_t conn_latency,
1189         uint16_t supervision_timeout,
1190         uint16_t min_ce_len,
1191         uint16_t max_ce_len)
1192 {
1193     struct net_buf *buf;
1194     struct bt_hci_cp_le_create_conn *cp;
1195 
1196     buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_CONN, sizeof(*cp));
1197 
1198     if (!buf) {
1199         return -ENOBUFS;
1200     }
1201 
1202     cp = net_buf_add(buf, sizeof(*cp));
1203     memset(cp, 0, sizeof(*cp));
1204 
1205     /* Interval == window for continuous scanning */
1206     cp->scan_interval = sys_cpu_to_le16(scan_interval);
1207     cp->scan_window = cp->scan_interval;
1208 
1209     cp->peer_addr.type = peer_addr_type;
1210     memcpy(cp->peer_addr.a.val, peer_addr, 6);
1211     cp->own_addr_type = own_addr_type;
1212     cp->conn_interval_min = sys_cpu_to_le16(conn_interval_min);
1213     cp->conn_interval_max = sys_cpu_to_le16(conn_interval_max);
1214     cp->conn_latency = sys_cpu_to_le16(conn_latency);
1215     cp->supervision_timeout = sys_cpu_to_le16(supervision_timeout);
1216 
1217     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CREATE_CONN, buf, NULL);
1218 }
1219 
hci_api_le_create_conn_cancel()1220 __hci_api_weak__ int hci_api_le_create_conn_cancel()
1221 {
1222     return bt_hci_cmd_send(BT_HCI_OP_LE_CREATE_CONN_CANCEL,
1223                            NULL);
1224 }
1225 
hci_api_le_disconnect(uint16_t conn_hanlde,uint8_t reason)1226 __hci_api_weak__ int hci_api_le_disconnect(uint16_t conn_hanlde, uint8_t reason)
1227 {
1228     struct net_buf *buf;
1229     struct bt_hci_cp_disconnect *disconn;
1230 
1231     buf = bt_hci_cmd_create(BT_HCI_OP_DISCONNECT, sizeof(*disconn));
1232 
1233     if (!buf) {
1234         return -ENOBUFS;
1235     }
1236 
1237     disconn = net_buf_add(buf, sizeof(*disconn));
1238     disconn->handle = sys_cpu_to_le16(conn_hanlde);
1239     disconn->reason = reason;
1240 
1241     return bt_hci_cmd_send(BT_HCI_OP_DISCONNECT, buf);
1242 }
1243 
hci_api_le_read_remote_features(uint16_t conn_handle)1244 __hci_api_weak__ int hci_api_le_read_remote_features(uint16_t conn_handle)
1245 {
1246     struct bt_hci_cp_le_read_remote_features *cp;
1247     struct net_buf *buf;
1248 
1249     buf = bt_hci_cmd_create(BT_HCI_OP_LE_READ_REMOTE_FEATURES,
1250                             sizeof(*cp));
1251 
1252     if (!buf) {
1253         return -ENOBUFS;
1254     }
1255 
1256     cp = net_buf_add(buf, sizeof(*cp));
1257     cp->handle = sys_cpu_to_le16(conn_handle);
1258     return bt_hci_cmd_send(BT_HCI_OP_LE_READ_REMOTE_FEATURES, buf);
1259 }
1260 
hci_api_le_read_remote_version(uint16_t conn_handle)1261 __hci_api_weak__ int hci_api_le_read_remote_version(uint16_t conn_handle)
1262 {
1263 	struct bt_hci_cp_read_remote_version_info *cp;
1264 	struct net_buf *buf;
1265 
1266 	buf = bt_hci_cmd_create(BT_HCI_OP_READ_REMOTE_VERSION_INFO,
1267 				sizeof(*cp));
1268 	if (!buf) {
1269 		return -ENOBUFS;
1270 	}
1271 
1272 	cp = net_buf_add(buf, sizeof(*cp));
1273 	cp->handle = sys_cpu_to_le16(conn_handle);
1274 
1275 	return bt_hci_cmd_send_sync(BT_HCI_OP_READ_REMOTE_VERSION_INFO, buf,
1276 				    NULL);
1277 }
1278 
hci_api_host_num_complete_packets(uint8_t num_handles,struct handle_count * phc)1279 __hci_api_weak__ int hci_api_host_num_complete_packets(uint8_t num_handles, struct handle_count *phc)
1280 {
1281     struct net_buf *buf;
1282     struct bt_hci_cp_host_num_completed_packets *cp;
1283     struct bt_hci_handle_count *hc;
1284 
1285     buf = bt_hci_cmd_create(BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS,
1286                             sizeof(*cp) + sizeof(*hc));
1287 
1288     if (!buf) {
1289         return -ENOBUFS;
1290     }
1291 
1292     cp = net_buf_add(buf, sizeof(*cp));
1293     cp->num_handles = sys_cpu_to_le16(num_handles);
1294 
1295     hc = net_buf_add(buf, sizeof(*hc));
1296     hc->handle = sys_cpu_to_le16(phc[0].handle);
1297     hc->count  = sys_cpu_to_le16(phc[0].count);
1298 
1299     return bt_hci_cmd_send(BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS, buf);
1300 }
1301 
hci_api_le_conn_updata(uint16_t conn_handle,uint16_t conn_interval_min,uint16_t conn_interval_max,uint16_t conn_latency,uint16_t supervision_timeout,uint16_t min_ce_len,uint16_t max_ce_len)1302 __hci_api_weak__ int hci_api_le_conn_updata(uint16_t conn_handle,
1303         uint16_t conn_interval_min,
1304         uint16_t conn_interval_max,
1305         uint16_t conn_latency,
1306         uint16_t supervision_timeout,
1307         uint16_t min_ce_len,
1308         uint16_t max_ce_len)
1309 {
1310     struct hci_cp_le_conn_update *conn_update;
1311     struct net_buf *buf;
1312 
1313     buf = bt_hci_cmd_create(BT_HCI_OP_LE_CONN_UPDATE,
1314                             sizeof(*conn_update));
1315 
1316     if (!buf) {
1317         return -ENOBUFS;
1318     }
1319 
1320     conn_update = net_buf_add(buf, sizeof(*conn_update));
1321     memset(conn_update, 0, sizeof(*conn_update));
1322     conn_update->handle = sys_cpu_to_le16(conn_handle);
1323     conn_update->conn_interval_min = sys_cpu_to_le16(conn_interval_min);
1324     conn_update->conn_interval_max = sys_cpu_to_le16(conn_interval_max);
1325     conn_update->conn_latency = sys_cpu_to_le16(conn_latency);
1326     conn_update->supervision_timeout = sys_cpu_to_le16(supervision_timeout);
1327 
1328     return bt_hci_cmd_send(BT_HCI_OP_LE_CONN_UPDATE, buf);
1329 }
1330 
hci_api_le_start_encrypt(uint16_t conn_handle,u8_t rand[8],u8_t ediv[2],uint8_t ltk[16])1331 __hci_api_weak__ int hci_api_le_start_encrypt(uint16_t conn_handle,
1332         u8_t rand[8],
1333         u8_t ediv[2],
1334         uint8_t  ltk[16])
1335 {
1336     struct bt_hci_cp_le_start_encryption *cp;
1337     struct net_buf *buf;
1338 
1339     buf = bt_hci_cmd_create(BT_HCI_OP_LE_START_ENCRYPTION, sizeof(*cp));
1340 
1341     if (!buf) {
1342         return -ENOBUFS;
1343     }
1344 
1345     cp = net_buf_add(buf, sizeof(*cp));
1346     cp->handle = sys_cpu_to_le16(conn_handle);
1347     memcpy(&cp->rand, rand, sizeof(cp->rand));
1348     memcpy(&cp->ediv, ediv, sizeof(cp->ediv));
1349     memcpy(cp->ltk, ltk, sizeof(cp->ltk));
1350 
1351     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_START_ENCRYPTION, buf, NULL);
1352 }
1353 
hci_api_le_enctypt_ltk_req_reply(uint16_t conn_handle,uint8_t ltk[16])1354 __hci_api_weak__ int hci_api_le_enctypt_ltk_req_reply(uint16_t conn_handle, uint8_t  ltk[16])
1355 {
1356     struct net_buf *buf;
1357     struct bt_hci_cp_le_ltk_req_reply *cp;
1358 
1359     buf = bt_hci_cmd_create(BT_HCI_OP_LE_LTK_REQ_REPLY,
1360                             sizeof(*cp));
1361 
1362     if (!buf) {
1363         return -ENOBUFS;
1364     }
1365 
1366     cp = net_buf_add(buf, sizeof(*cp));
1367     cp->handle = conn_handle;
1368 
1369     memcpy(cp->ltk, ltk, 16);
1370 
1371     return bt_hci_cmd_send(BT_HCI_OP_LE_LTK_REQ_REPLY, buf);
1372 }
1373 
hci_api_le_enctypt_ltk_req_neg_reply(uint16_t conn_handle)1374 __hci_api_weak__ int hci_api_le_enctypt_ltk_req_neg_reply(uint16_t conn_handle)
1375 {
1376     struct bt_hci_cp_le_ltk_req_neg_reply *cp;
1377     struct net_buf *buf;
1378 
1379     buf = bt_hci_cmd_create(BT_HCI_OP_LE_LTK_REQ_NEG_REPLY, sizeof(*cp));
1380 
1381     if (!buf) {
1382         return -ENOBUFS;
1383     }
1384 
1385     cp = net_buf_add(buf, sizeof(*cp));
1386     cp->handle = conn_handle;
1387 
1388     return bt_hci_cmd_send(BT_HCI_OP_LE_LTK_REQ_NEG_REPLY, buf);
1389 }
1390 
hci_api_le_rand(uint8_t random_data[8])1391 __hci_api_weak__ int hci_api_le_rand(uint8_t random_data[8])
1392 {
1393     struct bt_hci_rp_le_rand *rp;
1394     struct net_buf *rsp;
1395     int err;
1396     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_RAND, NULL, &rsp);
1397 
1398     if (err) {
1399         return err;
1400     }
1401 
1402     rp = (void *)rsp->data;
1403     memcpy(random_data, rp->rand, sizeof(rp->rand));
1404     return 0;
1405 }
1406 
hci_api_le_enc(uint8_t key[16],uint8_t plaintext[16],uint8_t ciphertext[16])1407 __hci_api_weak__ int hci_api_le_enc(uint8_t key[16], uint8_t plaintext[16], uint8_t ciphertext[16])
1408 {
1409     struct bt_hci_cp_le_encrypt *cp;
1410     struct bt_hci_rp_le_encrypt *rp;
1411     int err;
1412     struct net_buf *buf;
1413     struct net_buf *rsp;
1414 
1415     buf = bt_hci_cmd_create(BT_HCI_OP_LE_ENCRYPT, sizeof(*cp));
1416 
1417     if (!buf) {
1418         return -ENOBUFS;
1419     }
1420 
1421     cp = net_buf_add(buf, sizeof(*cp));
1422     memcpy(cp->key, key, sizeof(cp->key));
1423     memcpy(cp->plaintext, plaintext, sizeof(cp->plaintext));
1424 
1425     err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_ENCRYPT, buf, &rsp);
1426 
1427     if (err) {
1428         return err;
1429     }
1430 
1431     rp = (void *)rsp->data;
1432     memcpy(ciphertext, rp->enc_data, sizeof(rp->enc_data));
1433 
1434     return 0;
1435 }
1436 
hci_api_le_set_phy(u16_t handle,u8_t all_phys,u8_t tx_phys,u8_t rx_phys,u16_t phy_opts)1437 __hci_api_weak__ int hci_api_le_set_phy(u16_t  handle,
1438                                         u8_t   all_phys,
1439                                         u8_t   tx_phys,
1440                                         u8_t   rx_phys,
1441                                         u16_t  phy_opts)
1442 {
1443     struct bt_hci_cp_le_set_phy *cp;
1444     struct net_buf *buf;
1445 
1446     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PHY, sizeof(*cp));
1447 
1448     if (!buf) {
1449         return -ENOBUFS;
1450     }
1451 
1452     cp = net_buf_add(buf, sizeof(*cp));
1453     cp->handle = sys_cpu_to_le16(handle);
1454     cp->all_phys = all_phys;
1455     cp->tx_phys = tx_phys;
1456     cp->rx_phys = rx_phys;
1457     cp->phy_opts = phy_opts;
1458     return bt_hci_cmd_send(BT_HCI_OP_LE_SET_PHY, buf);
1459 }
1460 
hci_api_le_conn_param_req_reply(uint16_t handle,uint16_t interval_min,uint16_t interval_max,uint16_t latency,uint16_t timeout,uint16_t min_ce_len,uint16_t max_ce_len)1461 __hci_api_weak__ int hci_api_le_conn_param_req_reply(uint16_t handle,
1462         uint16_t interval_min,
1463         uint16_t interval_max,
1464         uint16_t latency,
1465         uint16_t timeout,
1466         uint16_t min_ce_len,
1467         uint16_t max_ce_len)
1468 {
1469     struct bt_hci_cp_le_conn_param_req_reply *cp;
1470     struct net_buf *buf;
1471 
1472     buf = bt_hci_cmd_create(BT_HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(*cp));
1473 
1474     if (!buf) {
1475         return -ENOBUFS;
1476     }
1477 
1478     cp = net_buf_add(buf, sizeof(*cp));
1479     memset(cp, 0, sizeof(*cp));
1480 
1481     cp->handle = sys_cpu_to_le16(handle);
1482     cp->interval_min = sys_cpu_to_le16(interval_min);
1483     cp->interval_max = sys_cpu_to_le16(interval_max);
1484     cp->latency = sys_cpu_to_le16(latency);
1485     cp->timeout = sys_cpu_to_le16(timeout);
1486 
1487     return bt_hci_cmd_send(BT_HCI_OP_LE_CONN_PARAM_REQ_REPLY, buf);
1488 }
1489 
1490 
hci_api_le_conn_param_neg_reply(uint16_t handle,uint8_t reason)1491 __hci_api_weak__ int hci_api_le_conn_param_neg_reply(uint16_t handle,
1492         uint8_t reason)
1493 {
1494     struct bt_hci_cp_le_conn_param_req_neg_reply *cp;
1495     struct net_buf *buf;
1496 
1497     buf = bt_hci_cmd_create(BT_HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY,
1498                             sizeof(*cp));
1499 
1500     if (!buf) {
1501         return -ENOBUFS;
1502     }
1503 
1504     cp = net_buf_add(buf, sizeof(*cp));
1505     cp->handle = sys_cpu_to_le16(handle);
1506     cp->reason = sys_cpu_to_le16(reason);
1507 
1508     return bt_hci_cmd_send(BT_HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, buf);
1509 }
1510 
hci_api_le_add_dev_to_rl(uint8_t type,uint8_t peer_id_addr[6],uint8_t peer_irk[16],uint8_t local_irk[16])1511 __hci_api_weak__ int hci_api_le_add_dev_to_rl(uint8_t type,
1512         uint8_t peer_id_addr[6],
1513         uint8_t peer_irk[16],
1514         uint8_t local_irk[16])
1515 {
1516     struct bt_hci_cp_le_add_dev_to_rl *cp;
1517     struct net_buf *buf;
1518 
1519     buf = bt_hci_cmd_create(BT_HCI_OP_LE_ADD_DEV_TO_RL, sizeof(*cp));
1520 
1521     if (!buf) {
1522         return -ENOBUFS;
1523     }
1524 
1525     cp = net_buf_add(buf, sizeof(*cp));
1526     memset(cp, 0, sizeof(*cp));
1527 
1528     cp->peer_id_addr.type = type;
1529     memcpy(cp->peer_id_addr.a.val, peer_id_addr, 6);
1530 
1531     if (peer_irk) {
1532         memcpy(cp->peer_irk, peer_irk, 16);
1533     }
1534 
1535     if (local_irk) {
1536         memcpy(cp->local_irk, local_irk, 16);
1537     }
1538 
1539     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_ADD_DEV_TO_RL, buf, NULL);
1540 }
1541 
hci_api_le_remove_dev_from_rl(uint8_t type,const uint8_t peer_id_addr[6])1542 __hci_api_weak__ int hci_api_le_remove_dev_from_rl(uint8_t type,
1543         const uint8_t peer_id_addr[6])
1544 {
1545     struct bt_hci_cp_le_rem_dev_from_rl *cp;
1546     struct net_buf *buf;
1547 
1548     buf = bt_hci_cmd_create(BT_HCI_OP_LE_REM_DEV_FROM_RL, sizeof(*cp));
1549 
1550     if (!buf) {
1551         return -ENOBUFS;
1552     }
1553 
1554     cp = net_buf_add(buf, sizeof(*cp));
1555     memset(cp, 0, sizeof(*cp));
1556 
1557     cp->peer_id_addr.type = type;
1558     memcpy(cp->peer_id_addr.a.val, peer_id_addr, 6);
1559 
1560     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_REM_DEV_FROM_RL, buf, NULL);
1561 }
1562 
hci_api_le_clear_rl()1563 __hci_api_weak__ int hci_api_le_clear_rl()
1564 {
1565     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CLEAR_RL, NULL, NULL);
1566 }
1567 
hci_api_le_set_addr_res_enable(uint8_t enable)1568 __hci_api_weak__ int hci_api_le_set_addr_res_enable(uint8_t enable)
1569 {
1570     struct net_buf *buf;
1571 
1572     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADDR_RES_ENABLE, 1);
1573 
1574     if (!buf) {
1575         return -ENOBUFS;
1576     }
1577 
1578     net_buf_add_u8(buf, enable);
1579 
1580     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADDR_RES_ENABLE,
1581                                 buf, NULL);
1582 }
1583 
hci_api_le_set_privacy_mode(uint8_t type,uint8_t id_addr[6],uint8_t mode)1584 __hci_api_weak__ int hci_api_le_set_privacy_mode(uint8_t type,
1585         uint8_t id_addr[6],
1586         uint8_t mode)
1587 {
1588     struct bt_hci_cp_le_set_privacy_mode cp;
1589     struct net_buf *buf;
1590 
1591     cp.id_addr.type = type;
1592     memcpy(cp.id_addr.a.val, id_addr, 6);
1593 
1594     cp.mode = mode;
1595 
1596     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PRIVACY_MODE, sizeof(cp));
1597 
1598     if (!buf) {
1599         return -ENOBUFS;
1600     }
1601 
1602     net_buf_add_mem(buf, &cp, sizeof(cp));
1603 
1604     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PRIVACY_MODE, buf, NULL);
1605 }
1606 
hci_api_le_gen_p256_pubkey()1607 __hci_api_weak__ int hci_api_le_gen_p256_pubkey()
1608 {
1609     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_P256_PUBLIC_KEY, NULL, NULL);
1610 }
1611 
hci_api_le_gen_dhkey(uint8_t remote_pk[64])1612 __hci_api_weak__ int hci_api_le_gen_dhkey(uint8_t remote_pk[64])
1613 {
1614     struct bt_hci_cp_le_generate_dhkey *cp;
1615     struct net_buf *buf;
1616 
1617     buf = bt_hci_cmd_create(BT_HCI_OP_LE_GENERATE_DHKEY, sizeof(*cp));
1618 
1619     if (!buf) {
1620         return -ENOBUFS;
1621     }
1622 
1623     cp = net_buf_add(buf, sizeof(*cp));
1624     memcpy(cp->key, remote_pk, sizeof(cp->key));
1625 
1626     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_GENERATE_DHKEY, buf, NULL);
1627 }
1628 
hci_api_read_buffer_size(uint16_t * acl_max_len,uint8_t * sco_max_len,uint16_t * acl_max_num,uint16_t * sco_max_num)1629 __hci_api_weak__ int hci_api_read_buffer_size(uint16_t *acl_max_len,
1630         uint8_t  *sco_max_len,
1631         uint16_t *acl_max_num,
1632         uint16_t *sco_max_num)
1633 {
1634     struct bt_hci_rp_read_buffer_size *rp;
1635     struct net_buf *rsp;
1636     int err;
1637 
1638     /* Use BR/EDR buffer size if LE reports zero buffers */
1639     err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_BUFFER_SIZE, NULL, &rsp);
1640 
1641     if (err) {
1642         return err;
1643     }
1644 
1645     rp = (void *)rsp->data;
1646 
1647     if (rp->status) {
1648         net_buf_unref(rsp);
1649         return rp->status;
1650     }
1651 
1652     *acl_max_len = sys_le16_to_cpu(rp->acl_max_len);
1653     *sco_max_len = rp->sco_max_len;
1654     *acl_max_num = sys_le16_to_cpu(rp->acl_max_num);
1655     *sco_max_num = sys_le16_to_cpu(rp->sco_max_num);
1656     net_buf_unref(rsp);
1657 
1658     return 0;
1659 }
1660 
hci_api_le_write_host_supp(uint8_t le,uint8_t simul)1661 __hci_api_weak__ int hci_api_le_write_host_supp(uint8_t le, uint8_t simul)
1662 {
1663     struct bt_hci_cp_write_le_host_supp *cp_le;
1664     struct net_buf *buf;
1665 
1666     buf = bt_hci_cmd_create(BT_HCI_OP_LE_WRITE_LE_HOST_SUPP,
1667                             sizeof(*cp_le));
1668 
1669     if (!buf) {
1670         return -ENOBUFS;
1671     }
1672 
1673     cp_le = net_buf_add(buf, sizeof(*cp_le));
1674 
1675     /* Explicitly enable LE for dual-mode controllers */
1676     cp_le->le = le;
1677     cp_le->simul = simul;
1678     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_WRITE_LE_HOST_SUPP, buf, NULL);
1679 }
1680 
process_events(struct k_poll_event * ev,int count)1681 static void process_events(struct k_poll_event *ev, int count)
1682 {
1683 	BT_DBG("count %d", count);
1684 
1685 	for (; count; ev++, count--) {
1686 		BT_DBG("ev->state %u", ev->state);
1687 
1688 		switch (ev->state) {
1689 		case K_POLL_STATE_SIGNALED:
1690 			break;
1691 		case K_POLL_STATE_FIFO_DATA_AVAILABLE:
1692 			if (ev->tag == BT_EVENT_CMD_TX) {
1693 				send_cmd();
1694 			} else if (IS_ENABLED(CONFIG_BT_CONN)) {
1695 				struct bt_conn *conn;
1696 
1697 				if (ev->tag == BT_EVENT_CONN_TX_QUEUE) {
1698 					conn = CONTAINER_OF(ev->fifo,
1699 							    struct bt_conn,
1700 							    tx_queue);
1701 					bt_conn_process_tx(conn);
1702 				}
1703 			}
1704 			break;
1705 		case K_POLL_STATE_NOT_READY:
1706 			break;
1707 		default:
1708 			BT_WARN("Unexpected k_poll event state %u", ev->state);
1709 			break;
1710 		}
1711 	}
1712 }
1713 
hci_data_buf_overflow(struct net_buf * buf)1714 static void hci_data_buf_overflow(struct net_buf *buf)
1715 {
1716 	struct bt_hci_evt_data_buf_overflow *evt = (void *)buf->data;
1717 
1718 	BT_WARN("Data buffer overflow (link type 0x%02x)", evt->link_type);
1719 	(void)evt;
1720 }
1721 
1722 static const struct event_handler prio_events[] = {
1723 	EVENT_HANDLER(BT_HCI_EVT_CMD_COMPLETE, hci_cmd_complete,
1724 		      sizeof(struct bt_hci_evt_cmd_complete)),
1725 	EVENT_HANDLER(BT_HCI_EVT_CMD_STATUS, hci_cmd_status,
1726 		      sizeof(struct bt_hci_evt_cmd_status)),
1727 #if defined(CONFIG_BT_CONN)
1728 	EVENT_HANDLER(BT_HCI_EVT_DATA_BUF_OVERFLOW,
1729 		      hci_data_buf_overflow,
1730 		      sizeof(struct bt_hci_evt_data_buf_overflow)),
1731 	EVENT_HANDLER(BT_HCI_EVT_NUM_COMPLETED_PACKETS,
1732 		      hci_num_completed_packets,
1733 		      sizeof(struct bt_hci_evt_num_completed_packets)),
1734 #endif /* CONFIG_BT_CONN */
1735 };
1736 
bt_recv_prio(struct net_buf * buf)1737 __hci_api_weak__ int bt_recv_prio(struct net_buf *buf)
1738 {
1739 	struct bt_hci_evt_hdr *hdr;
1740 
1741 	bt_monitor_send(bt_monitor_opcode(buf), buf->data, buf->len);
1742 
1743 	BT_ASSERT(bt_buf_get_type(buf) == BT_BUF_EVT);
1744 	BT_ASSERT(buf->len >= sizeof(*hdr));
1745 
1746     hdr = net_buf_pull_mem(buf, sizeof(*hdr));
1747 	BT_ASSERT(bt_hci_evt_is_prio(hdr->evt));
1748 
1749 	handle_event(hdr->evt, buf, prio_events, ARRAY_SIZE(prio_events));
1750 
1751 	net_buf_unref(buf);
1752 
1753     return 0;
1754 }
1755 
1756 #if defined(CONFIG_BT_CONN)
1757 /* command FIFO + conn_change signal + MAX_CONN * 2 (tx & tx_notify) */
1758 #define EV_COUNT (2 + (CONFIG_BT_MAX_CONN * 2))
1759 #else
1760 /* command FIFO */
1761 #define EV_COUNT 1
1762 #endif
1763 
hci_tx_thread(void * p1)1764 static void hci_tx_thread(void *p1)
1765 {
1766 	static struct k_poll_event events[EV_COUNT] = {
1767 		K_POLL_EVENT_STATIC_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE,
1768 						K_POLL_MODE_NOTIFY_ONLY,
1769 						&cmd_tx_queue,
1770 						BT_EVENT_CMD_TX),
1771 	};
1772 
1773     BT_DBG("Started");
1774 
1775 	while (1) {
1776 		int ev_count, err;
1777 
1778 		events[0].state = K_POLL_STATE_NOT_READY;
1779 		ev_count = 1;
1780 
1781 		if (IS_ENABLED(CONFIG_BT_CONN)) {
1782 			ev_count += bt_conn_prepare_events(&events[1]);
1783 		}
1784 
1785         BT_DBG("Calling k_poll with %d events", ev_count);
1786 
1787 #ifndef YULONG_HCI
1788 		err = k_poll(events, ev_count, K_FOREVER);
1789 #endif
1790 
1791 		BT_ASSERT(err == 0);
1792 
1793 		process_events(events, ev_count);
1794 
1795 		/* Make sure we don't hog the CPU if there's all the time
1796 		 * some ready events.
1797 		 */
1798 		k_yield();
1799 	}
1800 }
1801 
hci_api_init()1802 __hci_api_weak__ int hci_api_init()
1803 {
1804 
1805 #if !defined(CONFIG_BT_WAIT_NOP)
1806     k_sem_init(&ncmd_sem,1,1);
1807 #else
1808     k_sem_init(&ncmd_sem,0,1);
1809 #endif
1810 
1811     NET_BUF_POOL_INIT(hci_cmd_pool);
1812 
1813     k_fifo_init(&cmd_tx_queue);
1814 
1815 #ifdef CONFIG_BT_TINYCRYPT_ECC
1816     bt_hci_ecc_init();
1817 #endif
1818 
1819     return 0;
1820 }
1821 
hci_api_le_ext_adv_enable(uint8_t enable,uint16_t set_num,struct ext_adv_set_t adv_sets[])1822 __hci_api_weak__ int hci_api_le_ext_adv_enable(uint8_t enable, uint16_t set_num, struct ext_adv_set_t adv_sets[])
1823 {
1824 	struct net_buf *buf;
1825 	struct cmd_state_set state;
1826 	int i;
1827 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_ADV_ENABLE, 2 + 4 * set_num);
1828 	if (!buf) {
1829 		return -ENOBUFS;
1830 	}
1831 
1832 	net_buf_add_u8(buf, enable);
1833 
1834 	net_buf_add_u8(buf, set_num);
1835 
1836 	for (i = 0; i < set_num; i++)
1837 	{
1838 		net_buf_add_u8(buf, adv_sets[i].adv_handle);
1839 		net_buf_add_le16(buf,sys_cpu_to_le16(adv_sets[i].duration));
1840 		net_buf_add_u8(buf, adv_sets[i].max_ext_adv_evts);
1841 	}
1842 
1843 	//cmd_state_set_init(&state, adv->flags, BT_ADV_ENABLED, enable);
1844 	cmd(buf)->state = &state;
1845 
1846 	return  bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_ADV_ENABLE, buf, NULL);
1847 }
1848 
hci_api_le_set_adv_random_addr(uint8_t handle,const uint8_t addr[6])1849 __hci_api_weak__ int hci_api_le_set_adv_random_addr(uint8_t handle, const uint8_t addr[6])
1850 {
1851     struct bt_hci_cp_le_set_adv_set_random_addr *cp;
1852 	struct net_buf *buf;
1853 
1854 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_SET_RANDOM_ADDR,
1855 				sizeof(*cp));
1856 	if (!buf) {
1857 		return -ENOBUFS;
1858 	}
1859 
1860 	cp = net_buf_add(buf, sizeof(*cp));
1861 
1862 	cp->handle = handle;
1863 	memcpy(&cp->bdaddr, addr, 6);
1864 
1865 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_SET_RANDOM_ADDR, buf,
1866 				   NULL);
1867 }
1868 
hci_api_le_ext_scan_enable(uint8_t enable,uint8_t filter_dup,uint16_t duration,uint16_t period)1869 __hci_api_weak__ int hci_api_le_ext_scan_enable(uint8_t enable,uint8_t filter_dup,uint16_t duration, uint16_t period)
1870 {
1871     struct bt_hci_cp_le_set_ext_scan_enable *cp;
1872 	struct cmd_state_set state;
1873 	struct net_buf *buf;
1874 
1875 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_SCAN_ENABLE, sizeof(*cp));
1876 	if (!buf) {
1877 		return -ENOBUFS;
1878 	}
1879 
1880 	cp = net_buf_add(buf, sizeof(*cp));
1881 
1882 	cp->enable = enable;
1883     cp->filter_dup = filter_dup;
1884 	cp->duration = sys_cpu_to_le16(duration);
1885 	cp->period = 0;
1886 
1887 	cmd_state_set_init(&state, bt_dev.flags, BT_DEV_SCANNING, enable == BT_HCI_LE_SCAN_ENABLE);
1888 	cmd(buf)->state = &state;
1889 
1890 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_SCAN_ENABLE, buf, NULL);
1891 }
1892 
hci_api_le_ext_scan_param_set(uint8_t own_addr_type,uint8_t filter_policy,uint8_t scan_phys,struct ext_scan_param_t params[])1893 __hci_api_weak__ int hci_api_le_ext_scan_param_set(uint8_t own_addr_type,uint8_t filter_policy,uint8_t scan_phys, struct ext_scan_param_t params[])
1894 {
1895     struct bt_hci_cp_le_set_ext_scan_param *set_param;
1896 	struct net_buf *buf;
1897 
1898     u8_t phys_num = (scan_phys & BT_HCI_LE_EXT_SCAN_PHY_1M)? 1 : 0 + (scan_phys & BT_HCI_LE_EXT_SCAN_PHY_CODED)? 1 : 0;
1899 
1900 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_SCAN_PARAM,
1901 				sizeof(*set_param) +
1902 				phys_num * sizeof(struct bt_hci_ext_scan_phy));
1903 	if (!buf) {
1904 		return -ENOBUFS;
1905 	}
1906 
1907 	set_param = net_buf_add(buf, sizeof(*set_param));
1908 	set_param->own_addr_type = own_addr_type;
1909 	set_param->phys = scan_phys;
1910     set_param->filter_policy = filter_policy;
1911 
1912 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_SCAN_PARAM, buf, NULL);
1913 }
1914 
1915 
hci_api_le_read_phy(uint16_t conn_handle,uint8_t * tx_phy,uint8_t * rx_phy)1916 __hci_api_weak__ int hci_api_le_read_phy(uint16_t conn_handle, 	uint8_t  *tx_phy , uint8_t *rx_phy)
1917 {
1918     struct bt_hci_cp_le_read_phy *cp;
1919 	struct bt_hci_rp_le_read_phy *rp;
1920 	struct net_buf *buf, *rsp;
1921 	int err;
1922 
1923 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_READ_PHY, sizeof(*cp));
1924 	if (!buf) {
1925 		return -ENOBUFS;
1926 	}
1927 
1928 	cp = net_buf_add(buf, sizeof(*cp));
1929 	cp->handle = sys_cpu_to_le16(conn_handle);
1930 
1931 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_PHY, buf, &rsp);
1932 	if (err) {
1933 		return err;
1934 	}
1935 
1936 	rp = (void *)rsp->data;
1937 	*tx_phy = rp->tx_phy;
1938 	*rx_phy = rp->rx_phy;
1939 	net_buf_unref(rsp);
1940     return 0;
1941 }
1942 
hci_api_le_create_conn_ext(uint8_t filter_policy,uint8_t own_addr_type,uint8_t peer_addr_type,uint8_t peer_addr[6],uint8_t init_phys,struct ext_conn_phy_params_t params[])1943 __hci_api_weak__ int hci_api_le_create_conn_ext(uint8_t filter_policy,
1944                                       uint8_t own_addr_type,
1945                                       uint8_t peer_addr_type,
1946                                       uint8_t peer_addr[6],
1947                                       uint8_t init_phys,
1948                                       struct ext_conn_phy_params_t params[])
1949 {
1950     struct bt_hci_cp_le_ext_create_conn *cp;
1951     struct bt_hci_ext_conn_phy *phy;
1952     struct cmd_state_set state;
1953     struct net_buf *buf;
1954     u8_t num_phys;
1955     struct ext_conn_phy_params_t *pparams = params;
1956 
1957     num_phys = (init_phys & BT_HCI_LE_EXT_SCAN_PHY_1M) ? 1:0 + (init_phys & BT_HCI_LE_EXT_SCAN_PHY_CODED) ? 1:0;
1958 
1959     buf = bt_hci_cmd_create(BT_HCI_OP_LE_EXT_CREATE_CONN, sizeof(*cp) + num_phys * sizeof(*phy));
1960     if (!buf) {
1961         return -ENOBUFS;
1962     }
1963 
1964     cp = net_buf_add(buf, sizeof(*cp));
1965     (void)memset(cp, 0, sizeof(*cp));
1966     cp->filter_policy = filter_policy;
1967     cp->peer_addr.type = peer_addr_type;
1968     memcpy(cp->peer_addr.a.val, peer_addr, 6);
1969 
1970     cp->own_addr_type = own_addr_type;
1971     cp->phys = init_phys;
1972 
1973     if (init_phys & BT_HCI_LE_EXT_SCAN_PHY_1M) {
1974         phy = net_buf_add(buf, sizeof(*phy));
1975         phy->scan_interval = sys_cpu_to_le16(pparams->scan_interval);
1976         phy->scan_window = sys_cpu_to_le16(pparams->scan_window);
1977         phy->conn_interval_min = sys_cpu_to_le16(pparams->conn_interval_min);
1978         phy->conn_interval_max = sys_cpu_to_le16(pparams->conn_interval_max);
1979         phy->conn_latency = sys_cpu_to_le16(pparams->conn_latency);
1980         phy->supervision_timeout = sys_cpu_to_le16(pparams->supervision_timeout);
1981         phy->min_ce_len = 0;
1982         phy->max_ce_len = 0;
1983         pparams++;
1984     }
1985 
1986     if (init_phys & BT_HCI_LE_EXT_SCAN_PHY_CODED) {
1987         phy = net_buf_add(buf, sizeof(*phy));
1988         phy->scan_interval = sys_cpu_to_le16(pparams->scan_interval);
1989         phy->scan_window = sys_cpu_to_le16(pparams->scan_window);
1990         phy->conn_interval_min = sys_cpu_to_le16(pparams->conn_interval_min);
1991         phy->conn_interval_max = sys_cpu_to_le16(pparams->conn_interval_max);
1992         phy->conn_latency = sys_cpu_to_le16(pparams->conn_latency);
1993         phy->supervision_timeout = sys_cpu_to_le16(pparams->supervision_timeout);
1994         phy->min_ce_len = 0;
1995         phy->max_ce_len = 0;
1996     }
1997 
1998     cmd_state_set_init(&state, bt_dev.flags, BT_DEV_INITIATING, true);
1999     cmd(buf)->state = &state;
2000 
2001     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_EXT_CREATE_CONN, buf, NULL);
2002 }
2003 
hci_api_le_rpa_timeout_set(uint16_t timeout)2004 __hci_api_weak__ int hci_api_le_rpa_timeout_set(uint16_t timeout)
2005 {
2006 	struct net_buf *buf;
2007     struct bt_hci_cp_le_set_rpa_timeout *cp;
2008 
2009     buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RPA_TIMEOUT,
2010                 sizeof(*cp));
2011     if (!buf) {
2012         return -ENOBUFS;
2013     }
2014 
2015     cp = net_buf_add(buf, sizeof(*cp));
2016     cp->rpa_timeout = sys_cpu_to_le16(timeout);
2017     return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_RPA_TIMEOUT, buf,
2018                    NULL);
2019 }
2020 
hci_api_le_set_ext_ad_data(uint8_t handle,uint8_t op,uint8_t frag_pref,uint8_t len,uint8_t data[251])2021 __hci_api_weak__ int hci_api_le_set_ext_ad_data(uint8_t handle, uint8_t op, uint8_t  frag_pref, uint8_t  len, uint8_t  data[251])
2022 {
2023     struct bt_hci_cp_le_set_ext_adv_data *set_data;
2024 	struct net_buf *buf;
2025 
2026 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_ADV_DATA, sizeof(*set_data));
2027 	if (!buf) {
2028 		return -ENOBUFS;
2029 	}
2030 
2031 	set_data = net_buf_add(buf, sizeof(*set_data));
2032 	(void)memset(set_data, 0, sizeof(*set_data));
2033 
2034 	set_data->handle = handle;
2035 	set_data->op = op;
2036 	set_data->frag_pref = frag_pref;
2037     set_data->len = len;
2038     memcpy(set_data->data, data, len);
2039 
2040 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_ADV_DATA, buf, NULL);
2041 }
2042 
hci_api_le_set_ext_sd_data(uint8_t handle,uint8_t op,uint8_t frag_pref,uint8_t len,uint8_t data[251])2043 __hci_api_weak__ int hci_api_le_set_ext_sd_data(uint8_t handle, uint8_t op, uint8_t  frag_pref, uint8_t  len, uint8_t  data[251])
2044 {
2045     struct bt_hci_cp_le_set_ext_scan_rsp_data *set_data;
2046 	struct net_buf *buf;
2047 
2048 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, sizeof(*set_data));
2049 	if (!buf) {
2050 		return -ENOBUFS;
2051 	}
2052 
2053 	set_data = net_buf_add(buf, sizeof(*set_data));
2054 	(void)memset(set_data, 0, sizeof(*set_data));
2055 
2056 	set_data->handle = handle;
2057 	set_data->op = op;
2058 	set_data->frag_pref = frag_pref;
2059     set_data->len = len;
2060     memcpy(set_data->data, data, len);
2061 
2062 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, buf, NULL);
2063 }
2064 
hci_api_le_remove_adv_set(uint8_t handle)2065 __hci_api_weak__ int hci_api_le_remove_adv_set(uint8_t handle)
2066 {
2067 	struct bt_hci_cp_le_remove_adv_set *cp;
2068 	struct net_buf *buf;
2069 
2070 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_REMOVE_ADV_SET, sizeof(*cp));
2071 	if (!buf) {
2072 		BT_WARN("No HCI buffers");
2073 		return -ENOBUFS;
2074 	}
2075 
2076 	cp = net_buf_add(buf, sizeof(*cp));
2077 	cp->handle = handle;
2078 
2079 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_REMOVE_ADV_SET, buf, NULL);
2080 }
2081 
hci_api_le_set_host_chan_classif(u8_t ch_map[5])2082 __hci_api_weak__ int hci_api_le_set_host_chan_classif(u8_t ch_map[5])
2083 {
2084     struct bt_hci_cp_le_set_host_chan_classif *cp;
2085 	struct net_buf *buf;
2086 
2087 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_HOST_CHAN_CLASSIF,
2088 				sizeof(*cp));
2089 	if (!buf) {
2090 		return -ENOBUFS;
2091 	}
2092 
2093 	cp = net_buf_add(buf, sizeof(*cp));
2094 
2095 	memcpy(&cp->ch_map[0], &ch_map[0], 5);
2096 
2097 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_HOST_CHAN_CLASSIF,
2098 				    buf, NULL);
2099 }
2100 
2101 #endif
2102