xref: /freebsd/sys/netgraph/bluetooth/include/ng_btsocket_rfcomm.h (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*
2  * ng_btsocket_rfcomm.h
3  */
4 
5 /*-
6  * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: ng_btsocket_rfcomm.h,v 1.10 2003/03/29 22:27:42 max Exp $
31  * $FreeBSD$
32  */
33 
34 #ifndef _NETGRAPH_BTSOCKET_RFCOMM_H_
35 #define _NETGRAPH_BTSOCKET_RFCOMM_H_
36 
37 /*****************************************************************************
38  *****************************************************************************
39  **                              RFCOMM                                     **
40  *****************************************************************************
41  *****************************************************************************/
42 
43 /* XXX FIXME this does not belong here */
44 
45 #define RFCOMM_DEFAULT_MTU		667
46 #define RFCOMM_MAX_MTU			1024
47 
48 #define RFCOMM_DEFAULT_CREDITS		7
49 #define RFCOMM_MAX_CREDITS		40
50 
51 /* RFCOMM frame types */
52 #define RFCOMM_FRAME_SABM		0x2f
53 #define RFCOMM_FRAME_DISC		0x43
54 #define RFCOMM_FRAME_UA			0x63
55 #define RFCOMM_FRAME_DM			0x0f
56 #define RFCOMM_FRAME_UIH		0xef
57 
58 /* RFCOMM MCC commands */
59 #define RFCOMM_MCC_TEST			0x08 /* Test */
60 #define RFCOMM_MCC_FCON			0x28 /* Flow Control on */
61 #define RFCOMM_MCC_FCOFF		0x18 /* Flow Control off */
62 #define RFCOMM_MCC_MSC			0x38 /* Modem Status Command */
63 #define RFCOMM_MCC_RPN			0x24 /* Remote Port Negotiation */
64 #define RFCOMM_MCC_RLS			0x14 /* Remote Line Status */
65 #define RFCOMM_MCC_PN			0x20 /* Port Negotiation */
66 #define RFCOMM_MCC_NSC			0x04 /* Non Supported Command */
67 
68 /* RFCOMM modem signals */
69 #define RFCOMM_MODEM_FC			0x02 /* Flow Control asserted */
70 #define RFCOMM_MODEM_RTC		0x04 /* Ready To Communicate */
71 #define RFCOMM_MODEM_RTR		0x08 /* Ready To Receive */
72 #define RFCOMM_MODEM_IC			0x40 /* Incomming Call */
73 #define RFCOMM_MODEM_DV			0x80 /* Data Valid */
74 
75 /* RPN parameters - baud rate */
76 #define RFCOMM_RPN_BR_2400		0x0
77 #define RFCOMM_RPN_BR_4800		0x1
78 #define RFCOMM_RPN_BR_7200		0x2
79 #define RFCOMM_RPN_BR_9600		0x3
80 #define RFCOMM_RPN_BR_19200		0x4
81 #define RFCOMM_RPN_BR_38400		0x5
82 #define RFCOMM_RPN_BR_57600		0x6
83 #define RFCOMM_RPN_BR_115200		0x7
84 #define RFCOMM_RPN_BR_230400		0x8
85 
86 /* RPN parameters - data bits */
87 #define RFCOMM_RPN_DATA_5		0x0
88 #define RFCOMM_RPN_DATA_6		0x2
89 #define RFCOMM_RPN_DATA_7		0x1
90 #define RFCOMM_RPN_DATA_8		0x3
91 
92 /* RPN parameters - stop bit */
93 #define RFCOMM_RPN_STOP_1		0
94 #define RFCOMM_RPN_STOP_15		1
95 
96 /* RPN parameters - parity */
97 #define RFCOMM_RPN_PARITY_NONE		0x0
98 #define RFCOMM_RPN_PARITY_ODD		0x4
99 #define RFCOMM_RPN_PARITY_EVEN		0x5
100 #define RFCOMM_RPN_PARITY_MARK		0x6
101 #define RFCOMM_RPN_PARITY_SPACE		0x7
102 
103 /* RPN parameters - flow control */
104 #define RFCOMM_RPN_FLOW_NONE		0x00
105 #define RFCOMM_RPN_XON_CHAR		0x11
106 #define RFCOMM_RPN_XOFF_CHAR		0x13
107 
108 /* RPN parameters - mask */
109 #define RFCOMM_RPN_PM_BITRATE		0x0001
110 #define RFCOMM_RPN_PM_DATA		0x0002
111 #define RFCOMM_RPN_PM_STOP		0x0004
112 #define RFCOMM_RPN_PM_PARITY		0x0008
113 #define RFCOMM_RPN_PM_PARITY_TYPE	0x0010
114 #define RFCOMM_RPN_PM_XON		0x0020
115 #define RFCOMM_RPN_PM_XOFF		0x0040
116 #define RFCOMM_RPN_PM_FLOW		0x3F00
117 #define RFCOMM_RPN_PM_ALL		0x3F7F
118 
119 /* RFCOMM frame header */
120 struct rfcomm_frame_hdr
121 {
122 	u_int8_t	address;
123 	u_int8_t	control;
124 	u_int8_t	length;	/* Actual size could be 2 bytes */
125 } __attribute__ ((packed));
126 
127 /* RFCOMM command frame header */
128 struct rfcomm_cmd_hdr
129 {
130 	u_int8_t	address;
131 	u_int8_t	control;
132 	u_int8_t	length;
133 	u_int8_t	fcs;
134 } __attribute__ ((packed));
135 
136 /* RFCOMM MCC command header */
137 struct rfcomm_mcc_hdr
138 {
139 	u_int8_t	type;
140 	u_int8_t	length; /* XXX FIXME Can actual size be 2 bytes?? */
141 } __attribute__ ((packed));
142 
143 /* RFCOMM MSC command */
144 struct rfcomm_mcc_msc
145 {
146 	u_int8_t	address;
147 	u_int8_t	modem;
148 } __attribute__ ((packed));
149 
150 /* RFCOMM RPN command */
151 struct rfcomm_mcc_rpn
152 {
153 	u_int8_t	dlci;
154 	u_int8_t	bit_rate;
155 	u_int8_t	line_settings;
156 	u_int8_t	flow_control;
157 	u_int8_t	xon_char;
158 	u_int8_t	xoff_char;
159 	u_int16_t	param_mask;
160 } __attribute__ ((packed));
161 
162 /* RFCOMM RLS command */
163 struct rfcomm_mcc_rls
164 {
165 	u_int8_t	address;
166 	u_int8_t	status;
167 } __attribute__ ((packed));
168 
169 /* RFCOMM PN command */
170 struct rfcomm_mcc_pn
171 {
172 	u_int8_t	dlci;
173 	u_int8_t	flow_control;
174 	u_int8_t	priority;
175 	u_int8_t	ack_timer;
176 	u_int16_t	mtu;
177 	u_int8_t	max_retrans;
178 	u_int8_t	credits;
179 } __attribute__ ((packed));
180 
181 /* RFCOMM frame parsing macros */
182 #define RFCOMM_DLCI(b)			(((b) & 0xfc) >> 2)
183 #define RFCOMM_CHANNEL(b)		(((b) & 0xf8) >> 3)
184 #define RFCOMM_DIRECTION(b)		(((b) & 0x04) >> 2)
185 #define RFCOMM_TYPE(b)			(((b) & 0xef))
186 
187 #define RFCOMM_EA(b)			(((b) & 0x01))
188 #define RFCOMM_CR(b)			(((b) & 0x02) >> 1)
189 #define RFCOMM_PF(b)			(((b) & 0x10) >> 4)
190 
191 #define RFCOMM_SRVCHANNEL(dlci)		((dlci) >> 1)
192 
193 #define RFCOMM_MKADDRESS(cr, dlci) \
194 	((((dlci) & 0x3f) << 2) | ((cr) << 1) | 0x01)
195 
196 #define RFCOMM_MKCONTROL(type, pf)	((((type) & 0xef) | ((pf) << 4)))
197 #define RFCOMM_MKDLCI(dir, channel)	((((channel) & 0x1f) << 1) | (dir))
198 
199 #define RFCOMM_MKLEN8(len)		(((len) << 1) | 1)
200 #define RFCOMM_MKLEN16(len)		((len) << 1)
201 
202 /* RFCOMM MCC macros */
203 #define RFCOMM_MCC_TYPE(b)		(((b) & 0xfc) >> 2)
204 #define RFCOMM_MCC_LENGTH(b)		(((b) & 0xfe) >> 1)
205 #define RFCOMM_MKMCC_TYPE(cr, type)	((((type) << 2) | ((cr) << 1) | 0x01))
206 
207 /* RPN macros */
208 #define RFCOMM_RPN_DATA_BITS(line)	((line) & 0x3)
209 #define RFCOMM_RPN_STOP_BITS(line)	(((line) >> 2) & 0x1)
210 #define RFCOMM_RPN_PARITY(line)		(((line) >> 3) & 0x3)
211 #define RFCOMM_MKRPN_LINE_SETTINGS(data, stop, parity) \
212 	(((data) & 0x3) | (((stop) & 0x1) << 2) | (((parity) & 0x3) << 3))
213 
214 /*****************************************************************************
215  *****************************************************************************
216  **                      SOCK_STREAM RFCOMM sockets                         **
217  *****************************************************************************
218  *****************************************************************************/
219 
220 #define NG_BTSOCKET_RFCOMM_SENDSPACE \
221 	(RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 2)
222 #define NG_BTSOCKET_RFCOMM_RECVSPACE \
223 	(RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 2)
224 
225 /*
226  * Bluetooth RFCOMM session. One L2CAP connection == one RFCOMM session
227  */
228 
229 struct ng_btsocket_rfcomm_pcb;
230 struct ng_btsocket_rfcomm_session;
231 
232 struct ng_btsocket_rfcomm_session {
233 	struct socket				*l2so;	 /* L2CAP socket */
234 
235 	u_int16_t				 state;  /* session state */
236 #define NG_BTSOCKET_RFCOMM_SESSION_CLOSED	 0
237 #define NG_BTSOCKET_RFCOMM_SESSION_LISTENING	 1
238 #define NG_BTSOCKET_RFCOMM_SESSION_CONNECTING	 2
239 #define NG_BTSOCKET_RFCOMM_SESSION_CONNECTED	 3
240 #define NG_BTSOCKET_RFCOMM_SESSION_OPEN		 4
241 #define NG_BTSOCKET_RFCOMM_SESSION_DISCONNECTING 5
242 
243 	u_int16_t				 flags;  /* session flags */
244 #define NG_BTSOCKET_RFCOMM_SESSION_INITIATOR	(1 << 0) /* initiator */
245 #define NG_BTSOCKET_RFCOMM_SESSION_LFC		(1 << 1) /* local flow */
246 #define NG_BTSOCKET_RFCOMM_SESSION_RFC		(1 << 2) /* remote flow */
247 
248 #define INITIATOR(s) \
249 	(((s)->flags & NG_BTSOCKET_RFCOMM_SESSION_INITIATOR)? 1 : 0)
250 
251 	u_int16_t				 mtu;    /* default MTU */
252 	struct ng_bt_mbufq			 outq;   /* outgoing queue */
253 
254 	struct mtx				 session_mtx; /* session lock */
255 	LIST_HEAD(, ng_btsocket_rfcomm_pcb)	 dlcs;	 /* active DLC */
256 
257 	LIST_ENTRY(ng_btsocket_rfcomm_session)	 next;	 /* link to next */
258 };
259 typedef struct ng_btsocket_rfcomm_session	ng_btsocket_rfcomm_session_t;
260 typedef struct ng_btsocket_rfcomm_session *	ng_btsocket_rfcomm_session_p;
261 
262 /*
263  * Bluetooth RFCOMM socket PCB (DLC)
264  */
265 
266 struct ng_btsocket_rfcomm_pcb {
267 	struct socket				*so;	  /* RFCOMM socket */
268 	struct ng_btsocket_rfcomm_session	*session; /* RFCOMM session */
269 
270 	u_int16_t				 flags;   /* DLC flags */
271 #define NG_BTSOCKET_RFCOMM_DLC_TIMO		(1 << 0)  /* timeout pending */
272 #define NG_BTSOCKET_RFCOMM_DLC_CFC		(1 << 1)  /* credit flow ctrl */
273 #define NG_BTSOCKET_RFCOMM_DLC_TIMEDOUT		(1 << 2)  /* timeout happend */
274 #define NG_BTSOCKET_RFCOMM_DLC_DETACHED		(1 << 3)  /* DLC detached */
275 #define NG_BTSOCKET_RFCOMM_DLC_SENDING		(1 << 4)  /* send pending */
276 
277 	u_int16_t				 state;   /* DLC state */
278 #define NG_BTSOCKET_RFCOMM_DLC_CLOSED		0
279 #define NG_BTSOCKET_RFCOMM_DLC_W4_CONNECT	1
280 #define NG_BTSOCKET_RFCOMM_DLC_CONFIGURING	2
281 #define NG_BTSOCKET_RFCOMM_DLC_CONNECTING	3
282 #define NG_BTSOCKET_RFCOMM_DLC_CONNECTED	4
283 #define NG_BTSOCKET_RFCOMM_DLC_DISCONNECTING	5
284 
285 	bdaddr_t				 src;     /* source address */
286 	bdaddr_t				 dst;     /* dest. address */
287 
288 	u_int8_t				 channel; /* RFCOMM channel */
289 	u_int8_t				 dlci;    /* RFCOMM DLCI */
290 
291 	u_int8_t				 lmodem;  /* local mdm signls */
292 	u_int8_t				 rmodem;  /* remote -/- */
293 
294 	u_int16_t				 mtu;	  /* MTU */
295 	int16_t					 rx_cred; /* RX credits */
296 	int16_t					 tx_cred; /* TX credits */
297 
298 	struct mtx				 pcb_mtx; /* PCB lock */
299 	struct callout				 timo;    /* timeout */
300 
301 	LIST_ENTRY(ng_btsocket_rfcomm_pcb)	 session_next;/* link to next */
302 	LIST_ENTRY(ng_btsocket_rfcomm_pcb)	 next;	  /* link to next */
303 };
304 typedef struct ng_btsocket_rfcomm_pcb	ng_btsocket_rfcomm_pcb_t;
305 typedef struct ng_btsocket_rfcomm_pcb *	ng_btsocket_rfcomm_pcb_p;
306 
307 #define	so2rfcomm_pcb(so) \
308 	((struct ng_btsocket_rfcomm_pcb *)((so)->so_pcb))
309 
310 /*
311  * Bluetooth RFCOMM socket methods
312  */
313 
314 #ifdef _KERNEL
315 
316 void ng_btsocket_rfcomm_init       (void);
317 void ng_btsocket_rfcomm_abort      (struct socket *);
318 void ng_btsocket_rfcomm_close      (struct socket *);
319 int  ng_btsocket_rfcomm_accept     (struct socket *, struct sockaddr **);
320 int  ng_btsocket_rfcomm_attach     (struct socket *, int, struct thread *);
321 int  ng_btsocket_rfcomm_bind       (struct socket *, struct sockaddr *,
322                                     struct thread *);
323 int  ng_btsocket_rfcomm_connect    (struct socket *, struct sockaddr *,
324                                     struct thread *);
325 int  ng_btsocket_rfcomm_control    (struct socket *, u_long, caddr_t,
326                                     struct ifnet *, struct thread *);
327 int  ng_btsocket_rfcomm_ctloutput  (struct socket *, struct sockopt *);
328 void ng_btsocket_rfcomm_detach     (struct socket *);
329 int  ng_btsocket_rfcomm_disconnect (struct socket *);
330 int  ng_btsocket_rfcomm_listen     (struct socket *, int, struct thread *);
331 int  ng_btsocket_rfcomm_peeraddr   (struct socket *, struct sockaddr **);
332 int  ng_btsocket_rfcomm_send       (struct socket *, int, struct mbuf *,
333                                     struct sockaddr *, struct mbuf *,
334                                     struct thread *);
335 int  ng_btsocket_rfcomm_sockaddr   (struct socket *, struct sockaddr **);
336 
337 #endif /* _KERNEL */
338 
339 #endif /* _NETGRAPH_BTSOCKET_RFCOMM_H_ */
340 
341