1 /** @file
2  *  @brief Bluetooth HCI RAW channel handling
3  */
4 
5 /*
6  * Copyright (c) 2016 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_HCI_RAW_H_
11 #define ZEPHYR_INCLUDE_BLUETOOTH_HCI_RAW_H_
12 
13 /**
14  * @brief HCI RAW channel
15  * @defgroup hci_raw HCI RAW channel
16  * @ingroup bluetooth
17  * @{
18  */
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #if defined(CONFIG_BT_CTLR_TX_BUFFER_SIZE)
25 #define BT_L2CAP_MTU (CONFIG_BT_CTLR_TX_BUFFER_SIZE - BT_L2CAP_HDR_SIZE)
26 #else
27 #define BT_L2CAP_MTU 65 /* 64-byte public key + opcode */
28 #endif /* CONFIG_BT_CTLR */
29 
30 /** Data size needed for ACL buffers */
31 #define BT_BUF_ACL_SIZE BT_L2CAP_BUF_SIZE(BT_L2CAP_MTU)
32 
33 #if defined(CONFIG_BT_CTLR_TX_BUFFERS)
34 #define BT_HCI_ACL_COUNT CONFIG_BT_CTLR_TX_BUFFERS
35 #else
36 #define BT_HCI_ACL_COUNT 6
37 #endif
38 
39 #define BT_BUF_TX_SIZE MAX(BT_BUF_RX_SIZE, BT_BUF_ACL_SIZE)
40 
41 /** @brief Send packet to the Bluetooth controller
42  *
43  * Send packet to the Bluetooth controller. Caller needs to
44  * implement netbuf pool.
45  *
46  * @param buf netbuf packet to be send
47  *
48  * @return Zero on success or (negative) error code otherwise.
49  */
50 int bt_send(struct net_buf *buf);
51 
52 enum {
53 	/** Passthrough mode
54 	 *
55 	 *  While in this mode the buffers are passed as is between the stack
56 	 *  and the driver.
57 	 */
58 	BT_HCI_RAW_MODE_PASSTHROUGH = 0x00,
59 
60 	/** H:4 mode
61 	 *
62 	 *  While in this mode H:4 headers will added into the buffers
63 	 *  according to the buffer type when coming from the stack and will be
64 	 *  removed and used to set the buffer type.
65 	 */
66 	BT_HCI_RAW_MODE_H4 = 0x01,
67 };
68 
69 /** @brief Set Bluetooth RAW channel mode
70  *
71  *  Set access mode of Bluetooth RAW channel.
72  *
73  *  @param mode Access mode.
74  *
75  *  @return Zero on success or (negative) error code otherwise.
76  */
77 int bt_hci_raw_set_mode(u8_t mode);
78 
79 /** @brief Get Bluetooth RAW channel mode
80  *
81  *  Get access mode of Bluetooth RAW channel.
82  *
83  *  @return Access mode.
84  */
85 u8_t bt_hci_raw_get_mode(void);
86 
87 #define BT_HCI_ERR_EXT_HANDLED  0xff
88 
89 /** Helper macro to define a command extension
90  *
91  *  @param _op Opcode of the command.
92  *  @param _min_len Minimal length of the command.
93  *  @param _func Handler function to be called.
94  */
95 #define BT_HCI_RAW_CMD_EXT(_op, _min_len, _func) \
96 	{ \
97 		.op = _op, \
98 		.min_len = _min_len, \
99 		.func = _func, \
100 	}
101 
102 struct bt_hci_raw_cmd_ext {
103 	/** Opcode of the command */
104 	u16_t  op;
105 
106 	/** Minimal length of the command */
107 	size_t min_len;
108 
109 	/** Handler function.
110 	 *
111 	 *  Handler function to be called when a command is intercepted.
112 	 *
113 	 *  @param buf Buffer containing the command.
114 	 *
115 	 *  @return HCI Status code or BT_HCI_ERR_EXT_HANDLED if command has
116 	 *  been handled already and a response has been sent as oppose to
117 	 *  BT_HCI_ERR_SUCCESS which just indicates that the command can be
118 	 *  sent to the controller to be processed.
119 	 */
120 	u8_t   (*func)(struct net_buf *buf);
121 };
122 
123 /** @brief Register Bluetooth RAW command extension table
124  *
125  *  Register Bluetooth RAW channel command extension table, opcodes in this
126  *  table are intercepted to sent to the handler function.
127  *
128  *  @param cmds Pointer to the command extension table.
129  *  @param size Size of the command extension table.
130  */
131 void bt_hci_raw_cmd_ext_register(struct bt_hci_raw_cmd_ext *cmds, size_t size);
132 
133 /** @brief Enable Bluetooth RAW channel:
134  *
135  *  Enable Bluetooth RAW HCI channel.
136  *
137  *  @param rx_queue netbuf queue where HCI packets received from the Bluetooth
138  *  controller are to be queued. The queue is defined in the caller while
139  *  the available buffers pools are handled in the stack.
140  *
141  *  @return Zero on success or (negative) error code otherwise.
142  */
143 int bt_enable_raw(struct kfifo *rx_queue);
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 /**
149  * @}
150  */
151 
152 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_HCI_RAW_H_ */
153