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