Home | History | Annotate | Line # | Download | only in netbt
rfcomm.h revision 1.1.4.2
      1 /*	$NetBSD: rfcomm.h,v 1.1.4.2 2006/06/22 03:39:50 chap Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Iain Hibbert for Itronix Inc.
      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  * 3. The name of Itronix Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific
     19  *    prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 /*-
     34  * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin (at) yahoo.com>
     35  * All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  *
     46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56  * SUCH DAMAGE.
     57  *
     58  * $Id: rfcomm.h,v 1.1.4.2 2006/06/22 03:39:50 chap Exp $
     59  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_btsocket_rfcomm.h,v 1.4 2005/01/11 01:39:53 emax Exp $
     60  */
     61 
     62 #ifndef _NETBT_RFCOMM_H_
     63 #define _NETBT_RFCOMM_H_
     64 
     65 /*************************************************************************
     66  *************************************************************************
     67  **				RFCOMM					**
     68  *************************************************************************
     69  *************************************************************************/
     70 
     71 #define RFCOMM_MTU_MAX			32767
     72 #define RFCOMM_MTU_MIN			23
     73 #define RFCOMM_MTU_DEFAULT		127
     74 
     75 #define RFCOMM_CREDITS_MAX		255	/* in any single packet */
     76 #define RFCOMM_CREDITS_DEFAULT		7	/* default initial value */
     77 
     78 #define RFCOMM_CHANNEL_MIN		1
     79 #define RFCOMM_CHANNEL_MAX		30
     80 
     81 /* RFCOMM frame types */
     82 #define RFCOMM_FRAME_SABM		0x2f
     83 #define RFCOMM_FRAME_DISC		0x43
     84 #define RFCOMM_FRAME_UA			0x63
     85 #define RFCOMM_FRAME_DM			0x0f
     86 #define RFCOMM_FRAME_UIH		0xef
     87 
     88 /* RFCOMM MCC commands */
     89 #define RFCOMM_MCC_TEST			0x08	/* Test */
     90 #define RFCOMM_MCC_FCON			0x28	/* Flow Control on */
     91 #define RFCOMM_MCC_FCOFF		0x18	/* Flow Control off */
     92 #define RFCOMM_MCC_MSC			0x38	/* Modem Status Command */
     93 #define RFCOMM_MCC_RPN			0x24	/* Remote Port Negotiation */
     94 #define RFCOMM_MCC_RLS			0x14	/* Remote Line Status */
     95 #define RFCOMM_MCC_PN			0x20	/* Port Negotiation */
     96 #define RFCOMM_MCC_NSC			0x04	/* Non Supported Command */
     97 
     98 /* RFCOMM modem signals */
     99 #define RFCOMM_MSC_FC			0x02	/* Flow Control asserted */
    100 #define RFCOMM_MSC_RTC			0x04	/* Ready To Communicate */
    101 #define RFCOMM_MSC_RTR			0x08	/* Ready To Receive */
    102 #define RFCOMM_MSC_IC			0x40	/* Incomming Call (RING) */
    103 #define RFCOMM_MSC_DV			0x80	/* Data Valid */
    104 
    105 /* RPN parameters - baud rate */
    106 #define RFCOMM_RPN_BR_2400		0x0
    107 #define RFCOMM_RPN_BR_4800		0x1
    108 #define RFCOMM_RPN_BR_7200		0x2
    109 #define RFCOMM_RPN_BR_9600		0x3
    110 #define RFCOMM_RPN_BR_19200		0x4
    111 #define RFCOMM_RPN_BR_38400		0x5
    112 #define RFCOMM_RPN_BR_57600		0x6
    113 #define RFCOMM_RPN_BR_115200		0x7
    114 #define RFCOMM_RPN_BR_230400		0x8
    115 
    116 /* RPN parameters - data bits */
    117 #define RFCOMM_RPN_DATA_5		0x0
    118 #define RFCOMM_RPN_DATA_6		0x1
    119 #define RFCOMM_RPN_DATA_7		0x2
    120 #define RFCOMM_RPN_DATA_8		0x3
    121 
    122 /* RPN parameters - stop bit */
    123 #define RFCOMM_RPN_STOP_1		0
    124 #define RFCOMM_RPN_STOP_15		1
    125 
    126 /* RPN parameters - parity enable */
    127 #define RFCOMM_RPN_PARITY_NONE		0x0
    128 
    129 /* RPN parameters - parity type */
    130 #define RFCOMM_RPN_PARITY_ODD		0x0
    131 #define RFCOMM_RPN_PARITY_EVEN		0x1
    132 #define RFCOMM_RPN_PARITY_MARK		0x2
    133 #define RFCOMM_RPN_PARITY_SPACE		0x3
    134 
    135 /* RPN parameters - default line_setting */
    136 #define RFCOMM_RPN_8_N_1		0x03
    137 
    138 /* RPN parameters - flow control */
    139 #define RFCOMM_RPN_XON_CHAR		0x11
    140 #define RFCOMM_RPN_XOFF_CHAR		0x13
    141 #define RFCOMM_RPN_FLOW_NONE		0x00
    142 
    143 /* RPN parameters - mask */
    144 #define RFCOMM_RPN_PM_RATE		0x0001
    145 #define RFCOMM_RPN_PM_DATA		0x0002
    146 #define RFCOMM_RPN_PM_STOP		0x0004
    147 #define RFCOMM_RPN_PM_PARITY		0x0008
    148 #define RFCOMM_RPN_PM_PTYPE		0x0010
    149 #define RFCOMM_RPN_PM_XON		0x0020
    150 #define RFCOMM_RPN_PM_XOFF		0x0040
    151 
    152 #define RFCOMM_RPN_PM_FLOW		0x3f00
    153 
    154 #define RFCOMM_RPN_PM_ALL		0x3f7f
    155 
    156 /* RFCOMM command frame header */
    157 struct rfcomm_cmd_hdr
    158 {
    159 	uint8_t		address;
    160 	uint8_t		control;
    161 	uint8_t		length;
    162 	uint8_t		fcs;
    163 } __attribute__ ((__packed__));
    164 
    165 /* RFCOMM MSC command */
    166 struct rfcomm_mcc_msc
    167 {
    168 	uint8_t		address;
    169 	uint8_t		modem;
    170 	uint8_t		brk;
    171 } __attribute__ ((__packed__));
    172 
    173 /* RFCOMM RPN command */
    174 struct rfcomm_mcc_rpn
    175 {
    176 	uint8_t		dlci;
    177 	uint8_t		bit_rate;
    178 	uint8_t		line_settings;
    179 	uint8_t		flow_control;
    180 	uint8_t		xon_char;
    181 	uint8_t		xoff_char;
    182 	uint16_t	param_mask;
    183 } __attribute__ ((__packed__));
    184 
    185 /* RFCOMM RLS command */
    186 struct rfcomm_mcc_rls
    187 {
    188 	uint8_t		address;
    189 	uint8_t		status;
    190 } __attribute__ ((__packed__));
    191 
    192 /* RFCOMM PN command */
    193 struct rfcomm_mcc_pn
    194 {
    195 	uint8_t		dlci;
    196 	uint8_t		flow_control;
    197 	uint8_t		priority;
    198 	uint8_t		ack_timer;
    199 	uint16_t	mtu;
    200 	uint8_t		max_retrans;
    201 	uint8_t		credits;
    202 } __attribute__ ((__packed__));
    203 
    204 /* RFCOMM frame parsing macros */
    205 #define RFCOMM_DLCI(b)			(((b) & 0xfc) >> 2)
    206 #define RFCOMM_TYPE(b)			(((b) & 0xef))
    207 
    208 #define RFCOMM_EA(b)			(((b) & 0x01))
    209 #define RFCOMM_CR(b)			(((b) & 0x02) >> 1)
    210 #define RFCOMM_PF(b)			(((b) & 0x10) >> 4)
    211 
    212 #define RFCOMM_CHANNEL(dlci)		(((dlci) >> 1) & 0x2f)
    213 #define RFCOMM_DIRECTION(dlci)		((dlci) & 0x1)
    214 
    215 #define RFCOMM_MKADDRESS(cr, dlci) \
    216 	((((dlci) & 0x3f) << 2) | ((cr) << 1) | 0x01)
    217 
    218 #define RFCOMM_MKCONTROL(type, pf)	((((type) & 0xef) | ((pf) << 4)))
    219 #define RFCOMM_MKDLCI(dir, channel)	((((channel) & 0x1f) << 1) | (dir))
    220 
    221 /* RFCOMM MCC macros */
    222 #define RFCOMM_MCC_TYPE(b)		(((b) & 0xfc) >> 2)
    223 #define RFCOMM_MCC_LENGTH(b)		(((b) & 0xfe) >> 1)
    224 #define RFCOMM_MKMCC_TYPE(cr, type)	((((type) << 2) | ((cr) << 1) | 0x01))
    225 
    226 /* RPN macros */
    227 #define RFCOMM_RPN_DATA_BITS(line)	((line) & 0x3)
    228 #define RFCOMM_RPN_STOP_BITS(line)	(((line) >> 2) & 0x1)
    229 #define RFCOMM_RPN_PARITY(line)		(((line) >> 3) & 0x1)
    230 
    231 /*************************************************************************
    232  *************************************************************************
    233  **			SOCK_STREAM RFCOMM sockets			**
    234  *************************************************************************
    235  *************************************************************************/
    236 
    237 #define SO_RFCOMM_MTU		1	/* mtu */
    238 #define SO_RFCOMM_FC_INFO	2	/* flow control info (below) */
    239 
    240 /* Flow control information */
    241 struct rfcomm_fc_info {
    242 	uint8_t		lmodem;		/* modem signals (local) */
    243 	uint8_t		rmodem;		/* modem signals (remote) */
    244 	uint8_t		tx_cred;	/* TX credits */
    245 	uint8_t		rx_cred;	/* RX credits */
    246 	uint8_t		cfc;		/* credit flow control */
    247 	uint8_t		reserved;
    248 };
    249 
    250 #ifdef _KERNEL
    251 
    252 /* sysctl variables */
    253 extern int rfcomm_sendspace;
    254 extern int rfcomm_recvspace;
    255 extern int rfcomm_mtu_default;
    256 extern int rfcomm_ack_timeout;
    257 extern int rfcomm_mcc_timeout;
    258 
    259 /*
    260  * Bluetooth RFCOMM session data
    261  * One L2CAP connection == one RFCOMM session
    262  */
    263 
    264 /* Credit note */
    265 struct rfcomm_credit {
    266 	struct rfcomm_dlc		*rc_dlc;	/* owner */
    267 	uint16_t			 rc_len;	/* length */
    268 	SIMPLEQ_ENTRY(rfcomm_credit)	 rc_next;	/* next credit */
    269 };
    270 
    271 /* RFCOMM session data (one L2CAP channel) */
    272 struct rfcomm_session {
    273 	struct l2cap_channel		*rs_l2cap;	/* L2CAP pointer */
    274 	uint16_t			 rs_flags;	/* session flags */
    275 	uint16_t			 rs_state;	/* session state */
    276 	uint16_t			 rs_mtu;	/* default MTU */
    277 
    278 	SIMPLEQ_HEAD(,rfcomm_credit)	 rs_credits;	/* credit notes */
    279 	LIST_HEAD(,rfcomm_dlc)		 rs_dlcs;	/* DLC list */
    280 
    281 	struct callout			 rs_timeout;	/* timeout */
    282 
    283 	LIST_ENTRY(rfcomm_session)	 rs_next;	/* next session */
    284 };
    285 
    286 LIST_HEAD(rfcomm_session_list, rfcomm_session);
    287 extern struct rfcomm_session_list rfcomm_session_active;
    288 extern struct rfcomm_session_list rfcomm_session_listen;
    289 
    290 /* Session state */
    291 #define RFCOMM_SESSION_CLOSED		0
    292 #define RFCOMM_SESSION_WAIT_CONNECT	1
    293 #define RFCOMM_SESSION_OPEN		2
    294 #define RFCOMM_SESSION_WAIT_DISCONNECT	3
    295 #define RFCOMM_SESSION_LISTEN		4
    296 
    297 /* Session flags */
    298 #define RFCOMM_SESSION_INITIATOR	(1 << 0) /* we are initiator */
    299 #define RFCOMM_SESSION_CFC		(1 << 1) /* credit flow control */
    300 #define RFCOMM_SESSION_LFC		(1 << 2) /* local flow control */
    301 #define RFCOMM_SESSION_RFC		(1 << 3) /* remote flow control */
    302 #define RFCOMM_SESSION_FREE		(1 << 4) /* self lock out for free */
    303 
    304 #define IS_INITIATOR(rs)	((rs)->rs_flags & RFCOMM_SESSION_INITIATOR)
    305 
    306 /* Bluetooth RFCOMM DLC data (connection) */
    307 struct rfcomm_dlc {
    308 	struct rfcomm_session	*rd_session; /* RFCOMM session */
    309 	uint8_t			 rd_dlci;    /* RFCOMM DLCI */
    310 
    311 	uint16_t		 rd_flags;   /* DLC flags */
    312 	uint16_t		 rd_state;   /* DLC state */
    313 	uint16_t		 rd_mtu;     /* MTU */
    314 
    315 	struct sockaddr_bt	 rd_laddr;   /* local address */
    316 	struct sockaddr_bt	 rd_raddr;   /* remote address */
    317 
    318 	uint8_t			 rd_lmodem;  /* local modem signls */
    319 	uint8_t			 rd_rmodem;  /* remote modem signals */
    320 
    321 	int			 rd_rxcred;  /* receive credits (sent) */
    322 	size_t			 rd_rxsize;  /* receive buffer (bytes, avail) */
    323 	int			 rd_txcred;  /* transmit credits (unused) */
    324 	int			 rd_pending; /* packets sent but not complete */
    325 
    326 	struct callout		 rd_timeout; /* timeout */
    327 	struct mbuf		*rd_txbuf;   /* transmit buffer */
    328 
    329 	const struct btproto	*rd_proto;   /* upper layer callbacks */
    330 	void			*rd_upper;   /* upper layer argument */
    331 
    332 	LIST_ENTRY(rfcomm_dlc)	 rd_next;    /* next dlc on session */
    333 };
    334 
    335 /*
    336  * Credit Flow Control works in the following way.
    337  *
    338  * txcred is how many packets we can send. Received credit
    339  * is added to this value, and it is decremented each time
    340  * we send a packet.
    341  *
    342  * rxsize is the number of bytes that are available in the
    343  * upstream receive buffer.
    344  *
    345  * rxcred is the number of credits that we have previously
    346  * sent that are still unused. This value will be decreased
    347  * for each packet we receive and we will add to it when we
    348  * send credits. We calculate the amount of credits to send
    349  * by the cunning formula "(space / mtu) - sent" so that if
    350  * we get a bunch of small packets, we can continue sending
    351  * credits without risking buffer overflow.
    352  */
    353 
    354 /* DLC flags */
    355 #define RFCOMM_DLC_DETACH		(1 << 0) /* DLC to be detached */
    356 #define RFCOMM_DLC_SHUTDOWN		(1 << 1) /* DLC to be shutdown */
    357 
    358 /* DLC state */
    359 #define RFCOMM_DLC_CLOSED		0	/* no session */
    360 #define RFCOMM_DLC_WAIT_SESSION		1	/* waiting for session */
    361 #define RFCOMM_DLC_WAIT_CONNECT		2	/* waiting for connect */
    362 #define RFCOMM_DLC_OPEN			3	/* can send/receive */
    363 #define RFCOMM_DLC_WAIT_DISCONNECT	4	/* waiting for disconnect */
    364 #define RFCOMM_DLC_LISTEN		5	/* listening DLC */
    365 
    366 /*
    367  * Bluetooth RFCOMM socket kernel prototypes
    368  */
    369 
    370 struct socket;
    371 
    372 /* rfcomm_dlc.c */
    373 struct rfcomm_dlc *rfcomm_dlc_lookup(struct rfcomm_session *, int);
    374 struct rfcomm_dlc *rfcomm_dlc_newconn(struct rfcomm_session *, int);
    375 void rfcomm_dlc_close(struct rfcomm_dlc *, int);
    376 void rfcomm_dlc_timeout(void *);
    377 int rfcomm_dlc_connect(struct rfcomm_dlc *);
    378 void rfcomm_dlc_start(struct rfcomm_dlc *);
    379 
    380 /* rfcomm_session.c */
    381 struct rfcomm_session *rfcomm_session_alloc(struct rfcomm_session_list *, struct sockaddr_bt *);
    382 struct rfcomm_session *rfcomm_session_lookup(struct sockaddr_bt *, struct sockaddr_bt *);
    383 void rfcomm_session_free(struct rfcomm_session *);
    384 int rfcomm_session_send_frame(struct rfcomm_session *, int, int);
    385 int rfcomm_session_send_uih(struct rfcomm_session *, struct rfcomm_dlc *, int, struct mbuf *);
    386 int rfcomm_session_send_mcc(struct rfcomm_session *, int, uint8_t, void *, int);
    387 
    388 /* rfcomm_socket.c */
    389 int rfcomm_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *, struct lwp *);
    390 int rfcomm_ctloutput(int, struct socket *, int, int, struct mbuf **);
    391 
    392 /* rfcomm_upper.c */
    393 int rfcomm_attach(struct rfcomm_dlc **, const struct btproto *, void *);
    394 int rfcomm_bind(struct rfcomm_dlc *, struct sockaddr_bt *);
    395 int rfcomm_sockaddr(struct rfcomm_dlc *, struct sockaddr_bt *);
    396 int rfcomm_connect(struct rfcomm_dlc *, struct sockaddr_bt *);
    397 int rfcomm_peeraddr(struct rfcomm_dlc *, struct sockaddr_bt *);
    398 int rfcomm_disconnect(struct rfcomm_dlc *, int);
    399 int rfcomm_detach(struct rfcomm_dlc **);
    400 int rfcomm_listen(struct rfcomm_dlc *);
    401 int rfcomm_send(struct rfcomm_dlc *, struct mbuf *);
    402 int rfcomm_rcvd(struct rfcomm_dlc *, size_t);
    403 int rfcomm_setopt(struct rfcomm_dlc *, int, void *);
    404 int rfcomm_getopt(struct rfcomm_dlc *, int, void *);
    405 
    406 #endif /* _KERNEL */
    407 
    408 #endif /* _NETBT_RFCOMM_H_ */
    409