1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <zephyr/sys/byteorder.h>
9 #include <zephyr/drivers/bluetooth.h>
10 #include "common/assert.h"
11 
bt_hci_evt_create(uint8_t evt,uint8_t len)12 struct net_buf *bt_hci_evt_create(uint8_t evt, uint8_t len)
13 {
14 	struct bt_hci_evt_hdr *hdr;
15 	struct net_buf *buf;
16 
17 	buf = bt_buf_get_evt(evt, false, K_FOREVER);
18 
19 	BT_ASSERT(buf);
20 
21 	hdr = net_buf_add(buf, sizeof(*hdr));
22 	hdr->evt = evt;
23 	hdr->len = len;
24 
25 	return buf;
26 }
27 
bt_hci_cmd_complete_create(uint16_t op,uint8_t plen)28 struct net_buf *bt_hci_cmd_complete_create(uint16_t op, uint8_t plen)
29 {
30 	struct net_buf *buf;
31 	struct bt_hci_evt_cmd_complete *cc;
32 
33 	buf = bt_hci_evt_create(BT_HCI_EVT_CMD_COMPLETE, sizeof(*cc) + plen);
34 
35 	cc = net_buf_add(buf, sizeof(*cc));
36 
37 	/* The Num_HCI_Command_Packets parameter allows the Controller to
38 	 * indicate the number of HCI command packets the Host can send to the
39 	 * Controller. If the Controller requires the Host to stop sending
40 	 * commands, Num_HCI_Command_Packets will be set to zero.
41 	 *
42 	 * NOTE: Zephyr Controller (and may be other Controllers) do not support
43 	 *       higher Number of HCI Command packets than 1.
44 	 */
45 	cc->ncmd = 1U;
46 
47 	cc->opcode = sys_cpu_to_le16(op);
48 
49 	return buf;
50 }
51 
bt_hci_cmd_status_create(uint16_t op,uint8_t status)52 struct net_buf *bt_hci_cmd_status_create(uint16_t op, uint8_t status)
53 {
54 	struct net_buf *buf;
55 	struct bt_hci_evt_cmd_status *cs;
56 
57 	buf = bt_hci_evt_create(BT_HCI_EVT_CMD_STATUS, sizeof(*cs));
58 
59 	cs = net_buf_add(buf, sizeof(*cs));
60 	cs->status = status;
61 
62 	/* The Num_HCI_Command_Packets parameter allows the Controller to
63 	 * indicate the number of HCI command packets the Host can send to the
64 	 * Controller. If the Controller requires the Host to stop sending
65 	 * commands, Num_HCI_Command_Packets will be set to zero.
66 	 *
67 	 * NOTE: Zephyr Controller (and may be other Controllers) do not support
68 	 *       higher Number of HCI Command packets than 1.
69 	 */
70 	cs->ncmd = 1U;
71 
72 	cs->opcode = sys_cpu_to_le16(op);
73 
74 	return buf;
75 }
76