1 /* 2 * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com 3 * All rights reserved. Distributed under the terms of the MIT License. 4 * 5 */ 6 7 /*- 8 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com> 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. 19 */ 20 21 22 /************************************************************************** 23 ************************************************************************** 24 ** Common defines and types (L2CAP) 25 ************************************************************************** 26 **************************************************************************/ 27 28 #ifndef _L2CAP_ 29 #define _L2CAP_ 30 31 #include <bluetooth/bluetooth.h> 32 33 // TODO: from BSD compatibility layer 34 #define htole16(x) (x) 35 36 37 /* 38 * Channel IDs are assigned relative to the instance of L2CAP node, i.e. 39 * relative to the unit. So the total number of channels that unit can have 40 * open at the same time is 0xffff - 0x0040 = 0xffbf (65471). This number 41 * does not depend on number of connections. 42 */ 43 #define L2CAP_NULL_CID 0x0000 /* DO NOT USE THIS CID */ 44 #define L2CAP_SIGNAL_CID 0x0001 /* signaling channel ID */ 45 #define L2CAP_CLT_CID 0x0002 /* connectionless channel ID */ 46 /* 0x0003 - 0x003f Reserved */ 47 #define L2CAP_FIRST_CID 0x0040 /* dynamically alloc. (start) */ 48 #define L2CAP_LAST_CID 0xffff /* dynamically alloc. (end) */ 49 50 51 /* 52 * L2CAP signaling command ident's are assigned relative to the connection, 53 * because there is only one signaling channel (cid == 0x01) for every 54 * connection. So up to 254 (0xff - 0x01) L2CAP commands can be pending at the 55 * same time for the same connection. 56 */ 57 #define L2CAP_NULL_IDENT 0x00 /* DO NOT USE THIS IDENT */ 58 #define L2CAP_FIRST_IDENT 0x01 /* dynamically alloc. (start) */ 59 #define L2CAP_LAST_IDENT 0xff /* dynamically alloc. (end) */ 60 61 62 /* L2CAP MTU */ 63 #define L2CAP_MTU_MINIMUM 48 64 #define L2CAP_MTU_DEFAULT 672 65 #define L2CAP_MTU_MAXIMUM 0xffff 66 67 /* L2CAP flush and link timeouts */ 68 #define L2CAP_FLUSH_TIMO_DEFAULT 0xffff /* always retransmit */ 69 #define L2CAP_LINK_TIMO_DEFAULT 0xffff 70 71 /* L2CAP Command Reject reasons */ 72 #define L2CAP_REJ_NOT_UNDERSTOOD 0x0000 73 #define L2CAP_REJ_MTU_EXCEEDED 0x0001 74 #define L2CAP_REJ_INVALID_CID 0x0002 75 /* 0x0003 - 0xffff - reserved for future use */ 76 77 /* Protocol/Service Multioplexor (PSM) values */ 78 #define L2CAP_PSM_ANY 0x0000 /* Any/Invalid PSM */ 79 #define L2CAP_PSM_SDP 0x0001 /* Service Discovery Protocol */ 80 #define L2CAP_PSM_RFCOMM 0x0003 /* RFCOMM protocol */ 81 #define L2CAP_PSM_TCP 0x0005 /* Telephony Control Protocol */ 82 #define L2CAP_PSM_TCS 0x0007 /* TCS cordless */ 83 #define L2CAP_PSM_BNEP 0x000F /* BNEP */ 84 #define L2CAP_PSM_HID_CTRL 0x0011 /* HID Control */ 85 #define L2CAP_PSM_HID_INT 0x0013 /* HID Interrupt */ 86 #define L2CAP_PSM_UPnP 0x0015 /* UPnP (ESDP) */ 87 #define L2CAP_PSM_AVCTP 0x0017 /* AVCTP */ 88 #define L2CAP_PSM_AVDTP 0x0019 /* AVDTP */ 89 /* < 0x1000 - reserved for future use */ 90 /* 0x1001 < x < 0xFFFF dinamically assigned */ 91 92 /* L2CAP Connection response command result codes */ 93 #define L2CAP_SUCCESS 0x0000 94 #define L2CAP_PENDING 0x0001 95 #define L2CAP_PSM_NOT_SUPPORTED 0x0002 96 #define L2CAP_SEQUIRY_BLOCK 0x0003 97 #define L2CAP_NO_RESOURCES 0x0004 98 #define L2CAP_TIMEOUT 0xeeee 99 #define L2CAP_UNKNOWN 0xffff 100 /* 0x0005 - 0xffff - reserved for future use */ 101 102 /* L2CAP Connection response status codes */ 103 #define L2CAP_NO_INFO 0x0000 104 #define L2CAP_AUTH_PENDING 0x0001 105 #define L2CAP_AUTZ_PENDING 0x0002 106 /* 0x0003 - 0xffff - reserved for future use */ 107 108 /* L2CAP Configuration response result codes */ 109 #define L2CAP_UNACCEPTABLE_PARAMS 0x0001 110 #define L2CAP_REJECT 0x0002 111 #define L2CAP_UNKNOWN_OPTION 0x0003 112 /* 0x0003 - 0xffff - reserved for future use */ 113 114 /* L2CAP Configuration options */ 115 #define L2CAP_OPT_CFLAG_BIT 0x0001 116 #define L2CAP_OPT_CFLAG(flags) ((flags) & L2CAP_OPT_CFLAG_BIT) 117 #define L2CAP_OPT_HINT_BIT 0x80 118 #define L2CAP_OPT_HINT(type) ((type) & L2CAP_OPT_HINT_BIT) 119 #define L2CAP_OPT_HINT_MASK 0x7f 120 #define L2CAP_OPT_MTU 0x01 121 #define L2CAP_OPT_MTU_SIZE sizeof(uint16) 122 #define L2CAP_OPT_FLUSH_TIMO 0x02 123 #define L2CAP_OPT_FLUSH_TIMO_SIZE sizeof(uint16) 124 #define L2CAP_OPT_QOS 0x03 125 #define L2CAP_OPT_QOS_SIZE sizeof(l2cap_qos) 126 /* 0x4 - 0xff - reserved for future use */ 127 128 /* L2CAP Information request type codes */ 129 #define L2CAP_CONNLESS_MTU 0x0001 130 #define L2CAP_EXTENDED_MASK 0x0002 131 /* 0x0003 - 0xffff - reserved for future use */ 132 133 /* L2CAP Information response codes */ 134 #define L2CAP_NOT_SUPPORTED 0x0001 135 /* 0x0002 - 0xffff - reserved for future use */ 136 137 /* L2CAP flow (QoS) */ 138 typedef struct { 139 uint8 flags; /* reserved for future use */ 140 uint8 service_type; /* service type */ 141 uint32 token_rate; /* bytes per second */ 142 uint32 token_bucket_size; /* bytes */ 143 uint32 peak_bandwidth; /* bytes per second */ 144 uint32 latency; /* microseconds */ 145 uint32 delay_variation; /* microseconds */ 146 } __attribute__ ((packed)) l2cap_flow_t; 147 148 149 /************************************************************************** 150 ************************************************************************** 151 ** Link level defines, headers and types 152 ************************************************************************** 153 **************************************************************************/ 154 155 /* L2CAP header */ 156 typedef struct { 157 uint16 length; /* payload size */ 158 uint16 dcid; /* destination channel ID */ 159 } __attribute__ ((packed)) l2cap_hdr_t; 160 161 162 /* L2CAP ConnectionLess Traffic (CLT) (if destination cid == 0x2) */ 163 typedef struct { 164 uint16 psm; /* Protocol/Service Multiplexor */ 165 } __attribute__ ((packed)) l2cap_clt_hdr_t; 166 167 #define L2CAP_CLT_MTU_MAXIMUM (L2CAP_MTU_MAXIMUM - sizeof(l2cap_clt_hdr_t)) 168 169 /* L2CAP command header */ 170 typedef struct { 171 uint8 code; /* command OpCode */ 172 uint8 ident; /* identifier to match request and response */ 173 uint16 length; /* command parameters length */ 174 } __attribute__ ((packed)) l2cap_cmd_hdr_t; 175 176 177 /* L2CAP Command Reject */ 178 #define L2CAP_CMD_REJ 0x01 179 typedef struct { 180 uint16 reason; /* reason to reject command */ 181 /* uint8 data[]; -- optional data (depends on reason) */ 182 } __attribute__ ((packed)) l2cap_cmd_rej_cp; 183 184 /* CommandReject data */ 185 typedef union { 186 /* L2CAP_REJ_MTU_EXCEEDED */ 187 struct { 188 uint16 mtu; /* actual signaling MTU */ 189 } __attribute__ ((packed)) mtu; 190 /* L2CAP_REJ_INVALID_CID */ 191 struct { 192 uint16 scid; /* local CID */ 193 uint16 dcid; /* remote CID */ 194 } __attribute__ ((packed)) cid; 195 } l2cap_cmd_rej_data_t; 196 typedef l2cap_cmd_rej_data_t * l2cap_cmd_rej_data_p; 197 198 /* L2CAP Connection Request */ 199 #define L2CAP_CON_REQ 0x02 200 typedef struct { 201 uint16 psm; /* Protocol/Service Multiplexor (PSM) */ 202 uint16 scid; /* source channel ID */ 203 } __attribute__ ((packed)) l2cap_con_req_cp; 204 205 /* L2CAP Connection Response */ 206 #define L2CAP_CON_RSP 0x03 207 typedef struct { 208 uint16 dcid; /* destination channel ID */ 209 uint16 scid; /* source channel ID */ 210 uint16 result; /* 0x00 - success */ 211 uint16 status; /* more info if result != 0x00 */ 212 } __attribute__ ((packed)) l2cap_con_rsp_cp; 213 214 /* L2CAP Configuration Request */ 215 #define L2CAP_CFG_REQ 0x04 216 typedef struct { 217 uint16 dcid; /* destination channel ID */ 218 uint16 flags; /* flags */ 219 /* uint8 options[] -- options */ 220 } __attribute__ ((packed)) l2cap_cfg_req_cp; 221 222 /* L2CAP Configuration Response */ 223 #define L2CAP_CFG_RSP 0x05 224 typedef struct { 225 uint16 scid; /* source channel ID */ 226 uint16 flags; /* flags */ 227 uint16 result; /* 0x00 - success */ 228 /* uint8 options[] -- options */ 229 } __attribute__ ((packed)) l2cap_cfg_rsp_cp; 230 231 /* L2CAP configuration option */ 232 typedef struct { 233 uint8 type; 234 uint8 length; 235 /* uint8 value[] -- option value (depends on type) */ 236 } __attribute__ ((packed)) l2cap_cfg_opt_t; 237 typedef l2cap_cfg_opt_t * l2cap_cfg_opt_p; 238 239 /* L2CAP configuration option value */ 240 typedef union { 241 uint16 mtu; /* L2CAP_OPT_MTU */ 242 uint16 flush_timo; /* L2CAP_OPT_FLUSH_TIMO */ 243 l2cap_flow_t flow; /* L2CAP_OPT_QOS */ 244 } l2cap_cfg_opt_val_t; 245 typedef l2cap_cfg_opt_val_t * l2cap_cfg_opt_val_p; 246 247 /* L2CAP Disconnect Request */ 248 #define L2CAP_DISCON_REQ 0x06 249 typedef struct { 250 uint16 dcid; /* destination channel ID */ 251 uint16 scid; /* source channel ID */ 252 } __attribute__ ((packed)) l2cap_discon_req_cp; 253 254 /* L2CAP Disconnect Response */ 255 #define L2CAP_DISCON_RSP 0x07 256 typedef l2cap_discon_req_cp l2cap_discon_rsp_cp; 257 258 /* L2CAP Echo Request */ 259 #define L2CAP_ECHO_REQ 0x08 260 /* No command parameters, only optional data */ 261 262 /* L2CAP Echo Response */ 263 #define L2CAP_ECHO_RSP 0x09 264 #define L2CAP_MAX_ECHO_SIZE \ 265 (L2CAP_MTU_MAXIMUM - sizeof(l2cap_cmd_hdr_t)) 266 /* No command parameters, only optional data */ 267 268 /* L2CAP Information Request */ 269 #define L2CAP_INFO_REQ 0x0a 270 typedef struct { 271 uint16 type; /* requested information type */ 272 } __attribute__ ((packed)) l2cap_info_req_cp; 273 274 /* L2CAP Information Response */ 275 #define L2CAP_INFO_RSP 0x0b 276 typedef struct { 277 uint16 type; /* requested information type */ 278 uint16 result; /* 0x00 - success */ 279 /* uint8 info[] -- info data (depends on type) 280 * 281 * L2CAP_CONNLESS_MTU - 2 bytes connectionless MTU 282 */ 283 } __attribute__ ((packed)) l2cap_info_rsp_cp; 284 285 typedef union { 286 /* L2CAP_CONNLESS_MTU */ 287 struct { 288 uint16 mtu; 289 } __attribute__ ((packed)) mtu; 290 } l2cap_info_rsp_data_t; 291 typedef l2cap_info_rsp_data_t * l2cap_info_rsp_data_p; 292 293 294 #endif 295 296