Oskar Weigl
/
USBHost_Barcode
This program uses code taken from another program called BlueUSB
L2CAP.cpp@0:31713f62f35b, 2011-12-10 (annotated)
- Committer:
- madcowswe
- Date:
- Sat Dec 10 18:45:31 2011 +0000
- Revision:
- 0:31713f62f35b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
madcowswe | 0:31713f62f35b | 1 | /* |
madcowswe | 0:31713f62f35b | 2 | Copyright (c) 2010 Peter Barrett |
madcowswe | 0:31713f62f35b | 3 | |
madcowswe | 0:31713f62f35b | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
madcowswe | 0:31713f62f35b | 5 | of this software and associated documentation files (the "Software"), to deal |
madcowswe | 0:31713f62f35b | 6 | in the Software without restriction, including without limitation the rights |
madcowswe | 0:31713f62f35b | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
madcowswe | 0:31713f62f35b | 8 | copies of the Software, and to permit persons to whom the Software is |
madcowswe | 0:31713f62f35b | 9 | furnished to do so, subject to the following conditions: |
madcowswe | 0:31713f62f35b | 10 | |
madcowswe | 0:31713f62f35b | 11 | The above copyright notice and this permission notice shall be included in |
madcowswe | 0:31713f62f35b | 12 | all copies or substantial portions of the Software. |
madcowswe | 0:31713f62f35b | 13 | |
madcowswe | 0:31713f62f35b | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
madcowswe | 0:31713f62f35b | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
madcowswe | 0:31713f62f35b | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
madcowswe | 0:31713f62f35b | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
madcowswe | 0:31713f62f35b | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
madcowswe | 0:31713f62f35b | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
madcowswe | 0:31713f62f35b | 20 | THE SOFTWARE. |
madcowswe | 0:31713f62f35b | 21 | */ |
madcowswe | 0:31713f62f35b | 22 | |
madcowswe | 0:31713f62f35b | 23 | |
madcowswe | 0:31713f62f35b | 24 | #include <stdio.h> |
madcowswe | 0:31713f62f35b | 25 | #include <stdlib.h> |
madcowswe | 0:31713f62f35b | 26 | #include <stdio.h> |
madcowswe | 0:31713f62f35b | 27 | #include <string.h> |
madcowswe | 0:31713f62f35b | 28 | |
madcowswe | 0:31713f62f35b | 29 | #include "Utils.h" |
madcowswe | 0:31713f62f35b | 30 | #include "hci.h" |
madcowswe | 0:31713f62f35b | 31 | |
madcowswe | 0:31713f62f35b | 32 | #define L2CAP_COMMAND_REJ 0x01 |
madcowswe | 0:31713f62f35b | 33 | #define L2CAP_CONN_REQ 0x02 |
madcowswe | 0:31713f62f35b | 34 | #define L2CAP_CONN_RSP 0x03 |
madcowswe | 0:31713f62f35b | 35 | #define L2CAP_CONF_REQ 0x04 |
madcowswe | 0:31713f62f35b | 36 | #define L2CAP_CONF_RSP 0x05 |
madcowswe | 0:31713f62f35b | 37 | #define L2CAP_DISCONN_REQ 0x06 |
madcowswe | 0:31713f62f35b | 38 | #define L2CAP_DISCONN_RSP 0x07 |
madcowswe | 0:31713f62f35b | 39 | #define L2CAP_ECHO_REQ 0x08 |
madcowswe | 0:31713f62f35b | 40 | #define L2CAP_ECHO_RSP 0x09 |
madcowswe | 0:31713f62f35b | 41 | #define L2CAP_INFO_REQ 0x0a |
madcowswe | 0:31713f62f35b | 42 | #define L2CAP_INFO_RSP 0x0b |
madcowswe | 0:31713f62f35b | 43 | |
madcowswe | 0:31713f62f35b | 44 | |
madcowswe | 0:31713f62f35b | 45 | /* L2CAP command codes */ |
madcowswe | 0:31713f62f35b | 46 | const char* L2CAP_ComandCodeStr(int c) |
madcowswe | 0:31713f62f35b | 47 | { |
madcowswe | 0:31713f62f35b | 48 | switch (c) |
madcowswe | 0:31713f62f35b | 49 | { |
madcowswe | 0:31713f62f35b | 50 | case L2CAP_COMMAND_REJ: return "L2CAP_COMMAND_REJ"; |
madcowswe | 0:31713f62f35b | 51 | case L2CAP_CONN_REQ: return "L2CAP_CONN_REQ"; |
madcowswe | 0:31713f62f35b | 52 | case L2CAP_CONN_RSP: return "L2CAP_CONN_RSP"; |
madcowswe | 0:31713f62f35b | 53 | case L2CAP_CONF_REQ: return "L2CAP_CONF_REQ"; |
madcowswe | 0:31713f62f35b | 54 | case L2CAP_CONF_RSP: return "L2CAP_CONF_RSP"; |
madcowswe | 0:31713f62f35b | 55 | case L2CAP_DISCONN_REQ: return "L2CAP_DISCONN_REQ"; |
madcowswe | 0:31713f62f35b | 56 | case L2CAP_DISCONN_RSP: return "L2CAP_DISCONN_RSP"; |
madcowswe | 0:31713f62f35b | 57 | case L2CAP_ECHO_REQ: return "L2CAP_ECHO_REQ"; |
madcowswe | 0:31713f62f35b | 58 | case L2CAP_ECHO_RSP: return "L2CAP_ECHO_RSP"; |
madcowswe | 0:31713f62f35b | 59 | case L2CAP_INFO_REQ: return "L2CAP_INFO_REQ"; |
madcowswe | 0:31713f62f35b | 60 | case L2CAP_INFO_RSP: return "L2CAP_INFO_RSP"; |
madcowswe | 0:31713f62f35b | 61 | } |
madcowswe | 0:31713f62f35b | 62 | return "unknown"; |
madcowswe | 0:31713f62f35b | 63 | } |
madcowswe | 0:31713f62f35b | 64 | |
madcowswe | 0:31713f62f35b | 65 | typedef struct |
madcowswe | 0:31713f62f35b | 66 | { |
madcowswe | 0:31713f62f35b | 67 | u16 handle; |
madcowswe | 0:31713f62f35b | 68 | u16 length; // total |
madcowswe | 0:31713f62f35b | 69 | u16 l2capLength; // length -4 |
madcowswe | 0:31713f62f35b | 70 | u16 cid; // Signaling packet CID = 1 |
madcowswe | 0:31713f62f35b | 71 | u8 data[64]; // Largest thing to send!!! todo |
madcowswe | 0:31713f62f35b | 72 | } L2CAPData; |
madcowswe | 0:31713f62f35b | 73 | |
madcowswe | 0:31713f62f35b | 74 | typedef struct |
madcowswe | 0:31713f62f35b | 75 | { |
madcowswe | 0:31713f62f35b | 76 | u16 handle; |
madcowswe | 0:31713f62f35b | 77 | u16 length; // total |
madcowswe | 0:31713f62f35b | 78 | u16 l2capLength; // length -4 |
madcowswe | 0:31713f62f35b | 79 | u16 cid; // Signaling packet CID = 1 |
madcowswe | 0:31713f62f35b | 80 | |
madcowswe | 0:31713f62f35b | 81 | // Payload |
madcowswe | 0:31713f62f35b | 82 | u8 cmd; // |
madcowswe | 0:31713f62f35b | 83 | u8 id; |
madcowswe | 0:31713f62f35b | 84 | u16 cmdLength; // total-8 |
madcowswe | 0:31713f62f35b | 85 | u16 params[4]; // Params |
madcowswe | 0:31713f62f35b | 86 | } L2CAPCmd; |
madcowswe | 0:31713f62f35b | 87 | |
madcowswe | 0:31713f62f35b | 88 | // |
madcowswe | 0:31713f62f35b | 89 | void BTDevice::Init() |
madcowswe | 0:31713f62f35b | 90 | { |
madcowswe | 0:31713f62f35b | 91 | memset(&_info,0,sizeof(inquiry_info)); |
madcowswe | 0:31713f62f35b | 92 | _handle = 0; |
madcowswe | 0:31713f62f35b | 93 | _name[0] = 0; |
madcowswe | 0:31713f62f35b | 94 | _state = 0; |
madcowswe | 0:31713f62f35b | 95 | } |
madcowswe | 0:31713f62f35b | 96 | |
madcowswe | 0:31713f62f35b | 97 | // virtual SocketHandler |
madcowswe | 0:31713f62f35b | 98 | int BTDevice::Open(SocketInternal* sock, SocketAddrHdr* addr) |
madcowswe | 0:31713f62f35b | 99 | { |
madcowswe | 0:31713f62f35b | 100 | L2CAPSocket* s = (L2CAPSocket*)sock; |
madcowswe | 0:31713f62f35b | 101 | L2CAPAddr* a = (L2CAPAddr*)addr; |
madcowswe | 0:31713f62f35b | 102 | s->scid = 0x40 + sock->ID-1; // are these reserved? |
madcowswe | 0:31713f62f35b | 103 | s->dcid = 0; |
madcowswe | 0:31713f62f35b | 104 | Connect(s->scid,a->psm); |
madcowswe | 0:31713f62f35b | 105 | return sock->ID; |
madcowswe | 0:31713f62f35b | 106 | } |
madcowswe | 0:31713f62f35b | 107 | |
madcowswe | 0:31713f62f35b | 108 | // virtual SocketHandler |
madcowswe | 0:31713f62f35b | 109 | int BTDevice::Send(SocketInternal* sock, const u8* data, int len) |
madcowswe | 0:31713f62f35b | 110 | { |
madcowswe | 0:31713f62f35b | 111 | L2CAPData d; |
madcowswe | 0:31713f62f35b | 112 | L2CAPSocket* s = (L2CAPSocket*)sock; |
madcowswe | 0:31713f62f35b | 113 | |
madcowswe | 0:31713f62f35b | 114 | d.handle = _handle | 0x2000; |
madcowswe | 0:31713f62f35b | 115 | d.length = 4 + len; |
madcowswe | 0:31713f62f35b | 116 | d.l2capLength = len; |
madcowswe | 0:31713f62f35b | 117 | d.cid = s->dcid; |
madcowswe | 0:31713f62f35b | 118 | |
madcowswe | 0:31713f62f35b | 119 | if (len > 64) |
madcowswe | 0:31713f62f35b | 120 | return -1; |
madcowswe | 0:31713f62f35b | 121 | memcpy(d.data,data,len); |
madcowswe | 0:31713f62f35b | 122 | return Send((u8*)&d,len+8); |
madcowswe | 0:31713f62f35b | 123 | } |
madcowswe | 0:31713f62f35b | 124 | |
madcowswe | 0:31713f62f35b | 125 | // virtual SocketHandler |
madcowswe | 0:31713f62f35b | 126 | int BTDevice::Close(SocketInternal* sock) |
madcowswe | 0:31713f62f35b | 127 | { |
madcowswe | 0:31713f62f35b | 128 | printf("L2CAP close %d\n",sock->ID); |
madcowswe | 0:31713f62f35b | 129 | L2CAPSocket* s = (L2CAPSocket*)sock; |
madcowswe | 0:31713f62f35b | 130 | return Disconnect(s->scid,s->dcid); |
madcowswe | 0:31713f62f35b | 131 | } |
madcowswe | 0:31713f62f35b | 132 | |
madcowswe | 0:31713f62f35b | 133 | L2CAPSocket* BTDevice::SCIDToSocket(int scid) |
madcowswe | 0:31713f62f35b | 134 | { |
madcowswe | 0:31713f62f35b | 135 | return (L2CAPSocket*)GetSocketInternal(scid-0x40+1); |
madcowswe | 0:31713f62f35b | 136 | } |
madcowswe | 0:31713f62f35b | 137 | |
madcowswe | 0:31713f62f35b | 138 | int BTDevice::Send(const u8* data, int len) |
madcowswe | 0:31713f62f35b | 139 | { |
madcowswe | 0:31713f62f35b | 140 | _transport->ACLSend(data,len); |
madcowswe | 0:31713f62f35b | 141 | return 0; |
madcowswe | 0:31713f62f35b | 142 | } |
madcowswe | 0:31713f62f35b | 143 | |
madcowswe | 0:31713f62f35b | 144 | int BTDevice::Send(u8 c, u8 id, u16* params, int count) |
madcowswe | 0:31713f62f35b | 145 | { |
madcowswe | 0:31713f62f35b | 146 | L2CAPCmd cmd; |
madcowswe | 0:31713f62f35b | 147 | cmd.handle = _handle | 0x2000; |
madcowswe | 0:31713f62f35b | 148 | cmd.length = 8 + count*2; |
madcowswe | 0:31713f62f35b | 149 | |
madcowswe | 0:31713f62f35b | 150 | cmd.l2capLength = cmd.length-4; |
madcowswe | 0:31713f62f35b | 151 | cmd.cid = 1; // Signaling packet |
madcowswe | 0:31713f62f35b | 152 | |
madcowswe | 0:31713f62f35b | 153 | cmd.cmd = c; |
madcowswe | 0:31713f62f35b | 154 | cmd.id = id; |
madcowswe | 0:31713f62f35b | 155 | cmd.cmdLength = count*2; |
madcowswe | 0:31713f62f35b | 156 | for (int i = 0; i < count; i++) |
madcowswe | 0:31713f62f35b | 157 | cmd.params[i] = params[i]; |
madcowswe | 0:31713f62f35b | 158 | return Send((u8*)&cmd,cmd.length+4); |
madcowswe | 0:31713f62f35b | 159 | } |
madcowswe | 0:31713f62f35b | 160 | |
madcowswe | 0:31713f62f35b | 161 | int BTDevice::Connect(int scid, int psm) |
madcowswe | 0:31713f62f35b | 162 | { |
madcowswe | 0:31713f62f35b | 163 | u16 p[2]; |
madcowswe | 0:31713f62f35b | 164 | p[0] = psm; |
madcowswe | 0:31713f62f35b | 165 | p[1] = scid; |
madcowswe | 0:31713f62f35b | 166 | return Send(L2CAP_CONN_REQ,_txid++,p,2); |
madcowswe | 0:31713f62f35b | 167 | } |
madcowswe | 0:31713f62f35b | 168 | |
madcowswe | 0:31713f62f35b | 169 | int BTDevice::Disconnect(int scid, int dcid) |
madcowswe | 0:31713f62f35b | 170 | { |
madcowswe | 0:31713f62f35b | 171 | u16 p[2]; |
madcowswe | 0:31713f62f35b | 172 | p[0] = dcid; |
madcowswe | 0:31713f62f35b | 173 | p[1] = scid; |
madcowswe | 0:31713f62f35b | 174 | return Send(L2CAP_DISCONN_REQ,_txid++,p,2); |
madcowswe | 0:31713f62f35b | 175 | } |
madcowswe | 0:31713f62f35b | 176 | |
madcowswe | 0:31713f62f35b | 177 | int BTDevice::ConfigureRequest(int dcid) |
madcowswe | 0:31713f62f35b | 178 | { |
madcowswe | 0:31713f62f35b | 179 | u16 p[4]; |
madcowswe | 0:31713f62f35b | 180 | p[0] = dcid; |
madcowswe | 0:31713f62f35b | 181 | p[1] = 0; |
madcowswe | 0:31713f62f35b | 182 | p[2] = 0x0201; // Options |
madcowswe | 0:31713f62f35b | 183 | p[3] = 0x02A0; // 672 |
madcowswe | 0:31713f62f35b | 184 | return Send(L2CAP_CONF_REQ,_txid++,p,4); |
madcowswe | 0:31713f62f35b | 185 | } |
madcowswe | 0:31713f62f35b | 186 | |
madcowswe | 0:31713f62f35b | 187 | int BTDevice::ConfigureResponse(u8 rxid, int dcid) |
madcowswe | 0:31713f62f35b | 188 | { |
madcowswe | 0:31713f62f35b | 189 | u16 p[3]; |
madcowswe | 0:31713f62f35b | 190 | p[0] = dcid; |
madcowswe | 0:31713f62f35b | 191 | p[1] = 0; |
madcowswe | 0:31713f62f35b | 192 | p[2] = 0; |
madcowswe | 0:31713f62f35b | 193 | return Send(L2CAP_CONF_RSP,rxid,p,3); |
madcowswe | 0:31713f62f35b | 194 | } |
madcowswe | 0:31713f62f35b | 195 | |
madcowswe | 0:31713f62f35b | 196 | int BTDevice::DisconnectResponse(u8 rxid, int scid, int dcid) |
madcowswe | 0:31713f62f35b | 197 | { |
madcowswe | 0:31713f62f35b | 198 | u16 p[2]; |
madcowswe | 0:31713f62f35b | 199 | p[0] = dcid; |
madcowswe | 0:31713f62f35b | 200 | p[1] = scid; |
madcowswe | 0:31713f62f35b | 201 | return Send(L2CAP_DISCONN_RSP,rxid,p,2); |
madcowswe | 0:31713f62f35b | 202 | } |
madcowswe | 0:31713f62f35b | 203 | |
madcowswe | 0:31713f62f35b | 204 | void BTDevice::Control(const u8* data, int len) |
madcowswe | 0:31713f62f35b | 205 | { |
madcowswe | 0:31713f62f35b | 206 | int cc = data[8]; |
madcowswe | 0:31713f62f35b | 207 | printf(L2CAP_ComandCodeStr(cc)); |
madcowswe | 0:31713f62f35b | 208 | int result = LE16(data+16); |
madcowswe | 0:31713f62f35b | 209 | printf(" Result %d\n",result); |
madcowswe | 0:31713f62f35b | 210 | switch (cc) |
madcowswe | 0:31713f62f35b | 211 | { |
madcowswe | 0:31713f62f35b | 212 | case L2CAP_COMMAND_REJ: |
madcowswe | 0:31713f62f35b | 213 | break; |
madcowswe | 0:31713f62f35b | 214 | case L2CAP_CONN_REQ: |
madcowswe | 0:31713f62f35b | 215 | break; |
madcowswe | 0:31713f62f35b | 216 | |
madcowswe | 0:31713f62f35b | 217 | // Response to our initial connect from Remote |
madcowswe | 0:31713f62f35b | 218 | case L2CAP_CONN_RSP: |
madcowswe | 0:31713f62f35b | 219 | { |
madcowswe | 0:31713f62f35b | 220 | if (result == 0) |
madcowswe | 0:31713f62f35b | 221 | { |
madcowswe | 0:31713f62f35b | 222 | int dcid = LE16(data+12); |
madcowswe | 0:31713f62f35b | 223 | int scid = LE16(data+14); |
madcowswe | 0:31713f62f35b | 224 | L2CAPSocket* s = SCIDToSocket(scid); |
madcowswe | 0:31713f62f35b | 225 | if (s) |
madcowswe | 0:31713f62f35b | 226 | { |
madcowswe | 0:31713f62f35b | 227 | s->dcid = dcid; |
madcowswe | 0:31713f62f35b | 228 | ConfigureRequest(dcid); |
madcowswe | 0:31713f62f35b | 229 | } |
madcowswe | 0:31713f62f35b | 230 | } else |
madcowswe | 0:31713f62f35b | 231 | printf("Connect failed?\n"); |
madcowswe | 0:31713f62f35b | 232 | } |
madcowswe | 0:31713f62f35b | 233 | break; |
madcowswe | 0:31713f62f35b | 234 | |
madcowswe | 0:31713f62f35b | 235 | case L2CAP_CONF_RSP: |
madcowswe | 0:31713f62f35b | 236 | { |
madcowswe | 0:31713f62f35b | 237 | int scid = LE16(data+12); |
madcowswe | 0:31713f62f35b | 238 | SocketInternal* s = (SocketInternal*)SCIDToSocket(scid); |
madcowswe | 0:31713f62f35b | 239 | if (s) |
madcowswe | 0:31713f62f35b | 240 | s->SetState(SocketState_Open); |
madcowswe | 0:31713f62f35b | 241 | } |
madcowswe | 0:31713f62f35b | 242 | break; |
madcowswe | 0:31713f62f35b | 243 | |
madcowswe | 0:31713f62f35b | 244 | case L2CAP_CONF_REQ: |
madcowswe | 0:31713f62f35b | 245 | { |
madcowswe | 0:31713f62f35b | 246 | int scid = LE16(data+12); |
madcowswe | 0:31713f62f35b | 247 | L2CAPSocket* s = SCIDToSocket(scid); |
madcowswe | 0:31713f62f35b | 248 | if (s) |
madcowswe | 0:31713f62f35b | 249 | ConfigureResponse(data[9],s->dcid); |
madcowswe | 0:31713f62f35b | 250 | } |
madcowswe | 0:31713f62f35b | 251 | break; |
madcowswe | 0:31713f62f35b | 252 | } |
madcowswe | 0:31713f62f35b | 253 | } |
madcowswe | 0:31713f62f35b | 254 | |
madcowswe | 0:31713f62f35b | 255 | void BTDevice::ACLRecv(const u8* data, int len) |
madcowswe | 0:31713f62f35b | 256 | { |
madcowswe | 0:31713f62f35b | 257 | // printfBytes("L2CP",data,16); |
madcowswe | 0:31713f62f35b | 258 | int handle = LE16(data); |
madcowswe | 0:31713f62f35b | 259 | if (handle != (0x2000 | _handle)) |
madcowswe | 0:31713f62f35b | 260 | return; |
madcowswe | 0:31713f62f35b | 261 | |
madcowswe | 0:31713f62f35b | 262 | int cid = LE16(data+6); |
madcowswe | 0:31713f62f35b | 263 | if (cid == 1) |
madcowswe | 0:31713f62f35b | 264 | { |
madcowswe | 0:31713f62f35b | 265 | Control(data,len); |
madcowswe | 0:31713f62f35b | 266 | return; |
madcowswe | 0:31713f62f35b | 267 | } |
madcowswe | 0:31713f62f35b | 268 | |
madcowswe | 0:31713f62f35b | 269 | SocketInternal* s = (SocketInternal*)SCIDToSocket(cid); |
madcowswe | 0:31713f62f35b | 270 | if (s) |
madcowswe | 0:31713f62f35b | 271 | s->Recv(data+8,LE16(data+2)-4); |
madcowswe | 0:31713f62f35b | 272 | else |
madcowswe | 0:31713f62f35b | 273 | printf("Bad event cid %d\n",cid); |
madcowswe | 0:31713f62f35b | 274 | } |