1 /** @file
2  *  @brief Internal APIs for Bluetooth RFCOMM handling.
3  */
4 
5 /*
6  * Copyright (c) 2015-2016 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #include <bluetooth/rfcomm.h>
12 
13 typedef enum {
14 	BT_RFCOMM_CFC_UNKNOWN,
15 	BT_RFCOMM_CFC_NOT_SUPPORTED,
16 	BT_RFCOMM_CFC_SUPPORTED,
17 } __packed bt_rfcomm_cfc_t;
18 
19 /* RFCOMM signalling connection specific context */
20 struct bt_rfcomm_session {
21 	/* L2CAP channel this context is associated with */
22 	struct bt_l2cap_br_chan br_chan;
23 	/* Response Timeout eXpired (RTX) timer */
24 	struct k_work_delayable rtx_work;
25 	/* Binary sem for aggregate fc */
26 	struct k_sem fc;
27 	struct bt_rfcomm_dlc *dlcs;
28 	uint16_t mtu;
29 	uint8_t state;
30 	bt_rfcomm_role_t role;
31 	bt_rfcomm_cfc_t cfc;
32 };
33 
34 enum {
35 	BT_RFCOMM_STATE_IDLE,
36 	BT_RFCOMM_STATE_INIT,
37 	BT_RFCOMM_STATE_SECURITY_PENDING,
38 	BT_RFCOMM_STATE_CONNECTING,
39 	BT_RFCOMM_STATE_CONNECTED,
40 	BT_RFCOMM_STATE_CONFIG,
41 	BT_RFCOMM_STATE_USER_DISCONNECT,
42 	BT_RFCOMM_STATE_DISCONNECTING,
43 	BT_RFCOMM_STATE_DISCONNECTED,
44 };
45 
46 struct bt_rfcomm_hdr {
47 	uint8_t address;
48 	uint8_t control;
49 	uint8_t length;
50 } __packed;
51 
52 #define BT_RFCOMM_SABM  0x2f
53 #define BT_RFCOMM_UA    0x63
54 #define BT_RFCOMM_UIH   0xef
55 
56 struct bt_rfcomm_msg_hdr {
57 	uint8_t type;
58 	uint8_t len;
59 } __packed;
60 
61 #define BT_RFCOMM_PN    0x20
62 struct bt_rfcomm_pn {
63 	uint8_t  dlci;
64 	uint8_t  flow_ctrl;
65 	uint8_t  priority;
66 	uint8_t  ack_timer;
67 	uint16_t mtu;
68 	uint8_t  max_retrans;
69 	uint8_t  credits;
70 } __packed;
71 
72 #define BT_RFCOMM_MSC    0x38
73 struct bt_rfcomm_msc {
74 	uint8_t  dlci;
75 	uint8_t  v24_signal;
76 } __packed;
77 
78 #define BT_RFCOMM_DISC  0x43
79 #define BT_RFCOMM_DM    0x0f
80 
81 #define BT_RFCOMM_RLS   0x14
82 struct bt_rfcomm_rls {
83 	uint8_t  dlci;
84 	uint8_t  line_status;
85 } __packed;
86 
87 #define BT_RFCOMM_RPN   0x24
88 struct bt_rfcomm_rpn {
89 	uint8_t  dlci;
90 	uint8_t  baud_rate;
91 	uint8_t  line_settings;
92 	uint8_t  flow_control;
93 	uint8_t  xon_char;
94 	uint8_t  xoff_char;
95 	uint16_t param_mask;
96 } __packed;
97 
98 #define BT_RFCOMM_TEST  0x08
99 #define BT_RFCOMM_NSC   0x04
100 
101 #define BT_RFCOMM_FCON  0x28
102 #define BT_RFCOMM_FCOFF 0x18
103 
104 /* Default RPN Settings */
105 #define BT_RFCOMM_RPN_BAUD_RATE_9600    0x03
106 #define BT_RFCOMM_RPN_DATA_BITS_8       0x03
107 #define BT_RFCOMM_RPN_STOP_BITS_1       0x00
108 #define BT_RFCOMM_RPN_PARITY_NONE       0x00
109 #define BT_RFCOMM_RPN_FLOW_NONE         0x00
110 #define BT_RFCOMM_RPN_XON_CHAR          0x11
111 #define BT_RFCOMM_RPN_XOFF_CHAR         0x13
112 
113 /* Set 1 to all the param mask except reserved */
114 #define BT_RFCOMM_RPN_PARAM_MASK_ALL    0x3f7f
115 
116 #define BT_RFCOMM_SET_LINE_SETTINGS(data, stop, parity) ((data & 0x3) | \
117 							 ((stop & 0x1) << 2) | \
118 							 ((parity & 0x7) << 3))
119 
120 /* DV = 1 IC = 0 RTR = 1 RTC = 1 FC = 0 EXT = 0 */
121 #define BT_RFCOMM_DEFAULT_V24_SIG 0x8d
122 
123 #define BT_RFCOMM_GET_FC(v24_signal) (((v24_signal) & 0x02) >> 1)
124 
125 #define BT_RFCOMM_SIG_MIN_MTU   23
126 #define BT_RFCOMM_SIG_MAX_MTU   32767
127 
128 #define BT_RFCOMM_CHECK_MTU(mtu) (!!((mtu) >= BT_RFCOMM_SIG_MIN_MTU && \
129 				     (mtu) <= BT_RFCOMM_SIG_MAX_MTU))
130 
131 /* Helper to calculate needed outgoing buffer size.
132  * Length in rfcomm header can be two bytes depending on user data length.
133  * One byte in the tail should be reserved for FCS.
134  */
135 #define BT_RFCOMM_BUF_SIZE(mtu) (BT_BUF_RESERVE + \
136 				 BT_HCI_ACL_HDR_SIZE + BT_L2CAP_HDR_SIZE + \
137 				 sizeof(struct bt_rfcomm_hdr) + 1 + (mtu) + \
138 				 BT_RFCOMM_FCS_SIZE)
139 
140 #define BT_RFCOMM_GET_DLCI(addr)           (((addr) & 0xfc) >> 2)
141 #define BT_RFCOMM_GET_FRAME_TYPE(ctrl)     ((ctrl) & 0xef)
142 #define BT_RFCOMM_GET_MSG_TYPE(type)       (((type) & 0xfc) >> 2)
143 #define BT_RFCOMM_GET_MSG_CR(type)         (((type) & 0x02) >> 1)
144 #define BT_RFCOMM_GET_LEN(len)             (((len) & 0xfe) >> 1)
145 #define BT_RFCOMM_GET_CHANNEL(dlci)        ((dlci) >> 1)
146 #define BT_RFCOMM_GET_PF(ctrl)             (((ctrl) & 0x10) >> 4)
147 
148 #define BT_RFCOMM_SET_ADDR(dlci, cr)       ((((dlci) & 0x3f) << 2) | \
149 					    ((cr) << 1) | 0x01)
150 #define BT_RFCOMM_SET_CTRL(type, pf)       (((type) & 0xef) | ((pf) << 4))
151 #define BT_RFCOMM_SET_LEN_8(len)           (((len) << 1) | 1)
152 #define BT_RFCOMM_SET_LEN_16(len)          ((len) << 1)
153 #define BT_RFCOMM_SET_MSG_TYPE(type, cr)   (((type) << 2) | (cr << 1) | 0x01)
154 
155 #define BT_RFCOMM_LEN_EXTENDED(len)        (!((len) & 0x01))
156 
157 /* For CR in UIH Packet header
158  * Initiating station have the C/R bit set to 1 and those sent by the
159  * responding station have the C/R bit set to 0
160  */
161 #define BT_RFCOMM_UIH_CR(role)             ((role) == BT_RFCOMM_ROLE_INITIATOR)
162 
163 /* For CR in Non UIH Packet header
164  * Command
165  * Initiator --> Responder 1
166  * Responder --> Initiator 0
167  * Response
168  * Initiator --> Responder 0
169  * Responder --> Initiator 1
170  */
171 #define BT_RFCOMM_CMD_CR(role)             ((role) == BT_RFCOMM_ROLE_INITIATOR)
172 #define BT_RFCOMM_RESP_CR(role)            ((role) == BT_RFCOMM_ROLE_ACCEPTOR)
173 
174 /* For CR in MSG header
175  * If the C/R bit is set to 1 the message is a command,
176  * if it is set to 0 the message is a response.
177  */
178 #define BT_RFCOMM_MSG_CMD_CR               1
179 #define BT_RFCOMM_MSG_RESP_CR              0
180 
181 #define BT_RFCOMM_DLCI(role, channel)      ((((channel) & 0x1f) << 1) | \
182 					    ((role) == BT_RFCOMM_ROLE_ACCEPTOR))
183 
184 /* Excluding ext bit */
185 #define BT_RFCOMM_MAX_LEN_8 127
186 
187 /* Length can be 2 bytes depending on data size */
188 #define BT_RFCOMM_HDR_SIZE  (sizeof(struct bt_rfcomm_hdr) + 1)
189 #define BT_RFCOMM_FCS_SIZE  1
190 
191 #define BT_RFCOMM_FCS_LEN_UIH      2
192 #define BT_RFCOMM_FCS_LEN_NON_UIH  3
193 
194 /* For non UIH packets
195  * The P bit set to 1 shall be used to solicit a response frame with the
196  * F bit set to 1 from the other station.
197  */
198 #define BT_RFCOMM_PF_NON_UIH         1
199 
200 /* For UIH packets
201  * Both stations set the P-bit to 0
202  * If credit based flow control is used, If P/F is 1 then one credit byte
203  * will be there after control in the frame else no credit byte.
204  */
205 #define BT_RFCOMM_PF_UIH             0
206 #define BT_RFCOMM_PF_UIH_CREDIT      1
207 #define BT_RFCOMM_PF_UIH_NO_CREDIT   0
208 
209 #define BT_RFCOMM_PN_CFC_CMD   0xf0
210 #define BT_RFCOMM_PN_CFC_RESP  0xe0
211 
212 /* Initialize RFCOMM signal layer */
213 void bt_rfcomm_init(void);
214