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