1 /** @file
2  *  @brief Internal APIs for Bluetooth L2CAP BR/EDR handling.
3  */
4 
5 /*
6  * Copyright (c) 2015-2016 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #include <zephyr/bluetooth/l2cap.h>
12 #include <zephyr/sys/iterable_sections.h>
13 #include "l2cap_br_interface.h"
14 
15 #define BT_L2CAP_CID_BR_SIG             0x0001
16 #define BT_L2CAP_CID_BR_SMP             0x0007
17 #define BT_L2CAP_PSM_RFCOMM             0x0003
18 
19 struct bt_l2cap_hdr {
20 	uint16_t len;
21 	uint16_t cid;
22 } __packed;
23 
24 struct bt_l2cap_sig_hdr {
25 	uint8_t  code;
26 	uint8_t  ident;
27 	uint16_t len;
28 } __packed;
29 
30 #define BT_L2CAP_REJ_NOT_UNDERSTOOD     0x0000
31 #define BT_L2CAP_REJ_MTU_EXCEEDED       0x0001
32 #define BT_L2CAP_REJ_INVALID_CID        0x0002
33 
34 #define BT_L2CAP_CMD_REJECT             0x01
35 struct bt_l2cap_cmd_reject {
36 	uint16_t reason;
37 	uint8_t  data[0];
38 } __packed;
39 
40 struct bt_l2cap_cmd_reject_cid_data {
41 	uint16_t scid;
42 	uint16_t dcid;
43 } __packed;
44 
45 #define BT_L2CAP_CONN_REQ               0x02
46 struct bt_l2cap_conn_req {
47 	uint16_t psm;
48 	uint16_t scid;
49 } __packed;
50 
51 /* command statuses in response */
52 #define BT_L2CAP_CS_NO_INFO             0x0000
53 #define BT_L2CAP_CS_AUTHEN_PEND         0x0001
54 
55 /* valid results in conn response on BR/EDR */
56 #define BT_L2CAP_BR_SUCCESS             0x0000
57 #define BT_L2CAP_BR_PENDING             0x0001
58 #define BT_L2CAP_BR_ERR_PSM_NOT_SUPP    0x0002
59 #define BT_L2CAP_BR_ERR_SEC_BLOCK       0x0003
60 #define BT_L2CAP_BR_ERR_NO_RESOURCES    0x0004
61 #define BT_L2CAP_BR_ERR_INVALID_SCID    0x0006
62 #define BT_L2CAP_BR_ERR_SCID_IN_USE     0x0007
63 
64 #define BT_L2CAP_CONN_RSP               0x03
65 struct bt_l2cap_conn_rsp {
66 	uint16_t dcid;
67 	uint16_t scid;
68 	uint16_t result;
69 	uint16_t status;
70 } __packed;
71 
72 #define BT_L2CAP_CONF_SUCCESS           0x0000
73 #define BT_L2CAP_CONF_UNACCEPT          0x0001
74 #define BT_L2CAP_CONF_REJECT            0x0002
75 #define BT_L2CAP_CONF_UNKNOWN_OPT       0x0003
76 #define BT_L2CAP_CONF_PENDING           0x0004
77 #define BT_L2CAP_CONF_FLOW_SPEC_REJECT  0x0005
78 
79 #define BT_L2CAP_CONF_FLAGS_C           BIT(0)
80 #define BT_L2CAP_CONF_FLAGS_MASK        BT_L2CAP_CONF_FLAGS_C
81 
82 #define BT_L2CAP_CONF_REQ               0x04
83 struct bt_l2cap_conf_req {
84 	uint16_t dcid;
85 	uint16_t flags;
86 	uint8_t  data[0];
87 } __packed;
88 
89 #define BT_L2CAP_CONF_RSP               0x05
90 struct bt_l2cap_conf_rsp {
91 	uint16_t scid;
92 	uint16_t flags;
93 	uint16_t result;
94 	uint8_t  data[0];
95 } __packed;
96 
97 /* Option type used by MTU config request data */
98 #define BT_L2CAP_CONF_OPT_MTU           0x01
99 #define BT_L2CAP_CONF_OPT_FLUSH_TIMEOUT 0x02
100 #define BT_L2CAP_CONF_OPT_QOS           0x03
101 #define BT_L2CAP_CONF_OPT_RET_FC        0x04
102 #define BT_L2CAP_CONF_OPT_FCS           0x05
103 #define BT_L2CAP_CONF_OPT_EXT_FLOW_SPEC 0x06
104 #define BT_L2CAP_CONF_OPT_EXT_WIN_SIZE  0x07
105 
106 /* Options bits selecting most significant bit (hint) in type field */
107 #define BT_L2CAP_CONF_HINT              0x80
108 #define BT_L2CAP_CONF_MASK              0x7f
109 
110 struct bt_l2cap_conf_opt {
111 	uint8_t type;
112 	uint8_t len;
113 	uint8_t data[0];
114 } __packed;
115 
116 struct bt_l2cap_conf_opt_mtu {
117 	uint16_t mtu;
118 } __packed;
119 
120 struct bt_l2cap_conf_opt_flush_timeout {
121 	uint16_t timeout;
122 } __packed;
123 
124 #define BT_L2CAP_QOS_TYPE_NO_TRAFFIC    0x00
125 #define BT_L2CAP_QOS_TYPE_BEST_EFFORT   0x01
126 #define BT_L2CAP_QOS_TYPE_GUARANTEED    0x02
127 struct bt_l2cap_conf_opt_qos {
128 	uint8_t flags;
129 	uint8_t service_type;
130 	uint32_t token_rate;
131 	uint32_t token_bucket_size;
132 	uint32_t peak_bandwidth;
133 	uint32_t latency;
134 	uint32_t delay_variation;
135 } __packed;
136 
137 #define BT_L2CAP_RET_FC_MODE_BASIC   0x00
138 #define BT_L2CAP_RET_FC_MODE_RET     0x01
139 #define BT_L2CAP_RET_FC_MODE_FC      0x02
140 #define BT_L2CAP_RET_FC_MODE_ENH_RET 0x03
141 #define BT_L2CAP_RET_FC_MODE_STREAM  0x04
142 struct bt_l2cap_conf_opt_ret_fc {
143 	uint8_t mode;
144 	uint8_t tx_windows_size;
145 	uint8_t max_transmit;
146 	uint16_t retransmission_timeout;
147 	uint16_t monitor_timeout;
148 	uint16_t mps;
149 } __packed;
150 
151 #define BT_L2CAP_FCS_TYPE_NO         0x00
152 #define BT_L2CAP_FCS_TYPE_16BIT      0x01
153 struct bt_l2cap_conf_opt_fcs {
154 	uint8_t type;
155 } __packed;
156 
157 #define BT_L2CAP_DISCONN_REQ            0x06
158 struct bt_l2cap_disconn_req {
159 	uint16_t dcid;
160 	uint16_t scid;
161 } __packed;
162 
163 #define BT_L2CAP_DISCONN_RSP            0x07
164 struct bt_l2cap_disconn_rsp {
165 	uint16_t dcid;
166 	uint16_t scid;
167 } __packed;
168 
169 #define BT_L2CAP_INFO_FEAT_MASK         0x0002
170 #define BT_L2CAP_INFO_FIXED_CHAN        0x0003
171 
172 #define BT_L2CAP_INFO_REQ               0x0a
173 struct bt_l2cap_info_req {
174 	uint16_t type;
175 } __packed;
176 
177 /* info result */
178 #define BT_L2CAP_INFO_SUCCESS           0x0000
179 #define BT_L2CAP_INFO_NOTSUPP           0x0001
180 
181 #define BT_L2CAP_INFO_RSP               0x0b
182 struct bt_l2cap_info_rsp {
183 	uint16_t type;
184 	uint16_t result;
185 	uint8_t  data[0];
186 } __packed;
187 
188 #define BR_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_br_chan, chan)
189 
190 /* Add channel to the connection */
191 void bt_l2cap_chan_add(struct bt_conn *conn, struct bt_l2cap_chan *chan,
192 		       bt_l2cap_chan_destroy_t destroy);
193 
194 /* Remove channel from the connection */
195 void bt_l2cap_chan_remove(struct bt_conn *conn, struct bt_l2cap_chan *chan);
196 
197 /* Delete channel */
198 void bt_l2cap_br_chan_del(struct bt_l2cap_chan *chan);
199 
200 const char *bt_l2cap_chan_state_str(bt_l2cap_chan_state_t state);
201 
202 #if defined(CONFIG_BT_L2CAP_LOG_LEVEL_DBG)
203 void bt_l2cap_br_chan_set_state_debug(struct bt_l2cap_chan *chan,
204 				   bt_l2cap_chan_state_t state,
205 				   const char *func, int line);
206 #define bt_l2cap_br_chan_set_state(_chan, _state) \
207 	bt_l2cap_br_chan_set_state_debug(_chan, _state, __func__, __LINE__)
208 #else
209 void bt_l2cap_br_chan_set_state(struct bt_l2cap_chan *chan,
210 			     bt_l2cap_chan_state_t state);
211 #endif /* CONFIG_BT_L2CAP_LOG_LEVEL_DBG */
212 
213 /* Prepare an L2CAP PDU to be sent over a connection */
214 struct net_buf *bt_l2cap_create_pdu_timeout(struct net_buf_pool *pool,
215 					    size_t reserve,
216 					    k_timeout_t timeout);
217 
218 #define bt_l2cap_create_pdu(_pool, _reserve) \
219 	bt_l2cap_create_pdu_timeout(_pool, _reserve, K_FOREVER)
220