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