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 
76 #define BT_L2CAP_CONF_REQ               0x04
77 struct bt_l2cap_conf_req {
78 	uint16_t dcid;
79 	uint16_t flags;
80 	uint8_t  data[0];
81 } __packed;
82 
83 #define BT_L2CAP_CONF_RSP               0x05
84 struct bt_l2cap_conf_rsp {
85 	uint16_t scid;
86 	uint16_t flags;
87 	uint16_t result;
88 	uint8_t  data[0];
89 } __packed;
90 
91 /* Option type used by MTU config request data */
92 #define BT_L2CAP_CONF_OPT_MTU           0x01
93 /* Options bits selecting most significant bit (hint) in type field */
94 #define BT_L2CAP_CONF_HINT              0x80
95 #define BT_L2CAP_CONF_MASK              0x7f
96 
97 struct bt_l2cap_conf_opt {
98 	uint8_t type;
99 	uint8_t len;
100 	uint8_t data[0];
101 } __packed;
102 
103 #define BT_L2CAP_DISCONN_REQ            0x06
104 struct bt_l2cap_disconn_req {
105 	uint16_t dcid;
106 	uint16_t scid;
107 } __packed;
108 
109 #define BT_L2CAP_DISCONN_RSP            0x07
110 struct bt_l2cap_disconn_rsp {
111 	uint16_t dcid;
112 	uint16_t scid;
113 } __packed;
114 
115 #define BT_L2CAP_INFO_FEAT_MASK         0x0002
116 #define BT_L2CAP_INFO_FIXED_CHAN        0x0003
117 
118 #define BT_L2CAP_INFO_REQ               0x0a
119 struct bt_l2cap_info_req {
120 	uint16_t type;
121 } __packed;
122 
123 /* info result */
124 #define BT_L2CAP_INFO_SUCCESS           0x0000
125 #define BT_L2CAP_INFO_NOTSUPP           0x0001
126 
127 #define BT_L2CAP_INFO_RSP               0x0b
128 struct bt_l2cap_info_rsp {
129 	uint16_t type;
130 	uint16_t result;
131 	uint8_t  data[0];
132 } __packed;
133 
134 #define BR_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_br_chan, chan)
135 
136 /* Add channel to the connection */
137 void bt_l2cap_chan_add(struct bt_conn *conn, struct bt_l2cap_chan *chan,
138 		       bt_l2cap_chan_destroy_t destroy);
139 
140 /* Remove channel from the connection */
141 void bt_l2cap_chan_remove(struct bt_conn *conn, struct bt_l2cap_chan *chan);
142 
143 /* Delete channel */
144 void bt_l2cap_br_chan_del(struct bt_l2cap_chan *chan);
145 
146 const char *bt_l2cap_chan_state_str(bt_l2cap_chan_state_t state);
147 
148 #if defined(CONFIG_BT_L2CAP_LOG_LEVEL_DBG)
149 void bt_l2cap_br_chan_set_state_debug(struct bt_l2cap_chan *chan,
150 				   bt_l2cap_chan_state_t state,
151 				   const char *func, int line);
152 #define bt_l2cap_br_chan_set_state(_chan, _state) \
153 	bt_l2cap_br_chan_set_state_debug(_chan, _state, __func__, __LINE__)
154 #else
155 void bt_l2cap_br_chan_set_state(struct bt_l2cap_chan *chan,
156 			     bt_l2cap_chan_state_t state);
157 #endif /* CONFIG_BT_L2CAP_LOG_LEVEL_DBG */
158 
159 /* Prepare an L2CAP PDU to be sent over a connection */
160 struct net_buf *bt_l2cap_create_pdu_timeout(struct net_buf_pool *pool,
161 					    size_t reserve,
162 					    k_timeout_t timeout);
163 
164 #define bt_l2cap_create_pdu(_pool, _reserve) \
165 	bt_l2cap_create_pdu_timeout(_pool, _reserve, K_FOREVER)
166