A compilation of code from different sources to provide support for a Playstation 3 controller via bluetooth on the m3pi.
Fork of mbed_TANK_PS3 by
L2CAP.cpp@0:44619612f575, 2011-07-05 (annotated)
- Committer:
- kenbumono
- Date:
- Tue Jul 05 08:25:40 2011 +0000
- Revision:
- 0:44619612f575
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenbumono | 0:44619612f575 | 1 | /* |
kenbumono | 0:44619612f575 | 2 | Copyright (c) 2010 Peter Barrett |
kenbumono | 0:44619612f575 | 3 | |
kenbumono | 0:44619612f575 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
kenbumono | 0:44619612f575 | 5 | of this software and associated documentation files (the "Software"), to deal |
kenbumono | 0:44619612f575 | 6 | in the Software without restriction, including without limitation the rights |
kenbumono | 0:44619612f575 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kenbumono | 0:44619612f575 | 8 | copies of the Software, and to permit persons to whom the Software is |
kenbumono | 0:44619612f575 | 9 | furnished to do so, subject to the following conditions: |
kenbumono | 0:44619612f575 | 10 | |
kenbumono | 0:44619612f575 | 11 | The above copyright notice and this permission notice shall be included in |
kenbumono | 0:44619612f575 | 12 | all copies or substantial portions of the Software. |
kenbumono | 0:44619612f575 | 13 | |
kenbumono | 0:44619612f575 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kenbumono | 0:44619612f575 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kenbumono | 0:44619612f575 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kenbumono | 0:44619612f575 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kenbumono | 0:44619612f575 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenbumono | 0:44619612f575 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
kenbumono | 0:44619612f575 | 20 | THE SOFTWARE. |
kenbumono | 0:44619612f575 | 21 | */ |
kenbumono | 0:44619612f575 | 22 | |
kenbumono | 0:44619612f575 | 23 | |
kenbumono | 0:44619612f575 | 24 | #include <stdio.h> |
kenbumono | 0:44619612f575 | 25 | #include <stdlib.h> |
kenbumono | 0:44619612f575 | 26 | #include <stdio.h> |
kenbumono | 0:44619612f575 | 27 | #include <string.h> |
kenbumono | 0:44619612f575 | 28 | |
kenbumono | 0:44619612f575 | 29 | #include "Utils.h" |
kenbumono | 0:44619612f575 | 30 | #include "hci.h" |
kenbumono | 0:44619612f575 | 31 | |
kenbumono | 0:44619612f575 | 32 | #define L2CAP_COMMAND_REJ 0x01 |
kenbumono | 0:44619612f575 | 33 | #define L2CAP_CONN_REQ 0x02 |
kenbumono | 0:44619612f575 | 34 | #define L2CAP_CONN_RSP 0x03 |
kenbumono | 0:44619612f575 | 35 | #define L2CAP_CONF_REQ 0x04 |
kenbumono | 0:44619612f575 | 36 | #define L2CAP_CONF_RSP 0x05 |
kenbumono | 0:44619612f575 | 37 | #define L2CAP_DISCONN_REQ 0x06 |
kenbumono | 0:44619612f575 | 38 | #define L2CAP_DISCONN_RSP 0x07 |
kenbumono | 0:44619612f575 | 39 | #define L2CAP_ECHO_REQ 0x08 |
kenbumono | 0:44619612f575 | 40 | #define L2CAP_ECHO_RSP 0x09 |
kenbumono | 0:44619612f575 | 41 | #define L2CAP_INFO_REQ 0x0a |
kenbumono | 0:44619612f575 | 42 | #define L2CAP_INFO_RSP 0x0b |
kenbumono | 0:44619612f575 | 43 | |
kenbumono | 0:44619612f575 | 44 | |
kenbumono | 0:44619612f575 | 45 | /* L2CAP command codes */ |
kenbumono | 0:44619612f575 | 46 | const char* L2CAP_ComandCodeStr(int c) |
kenbumono | 0:44619612f575 | 47 | { |
kenbumono | 0:44619612f575 | 48 | switch (c) |
kenbumono | 0:44619612f575 | 49 | { |
kenbumono | 0:44619612f575 | 50 | case L2CAP_COMMAND_REJ: return "L2CAP_COMMAND_REJ"; |
kenbumono | 0:44619612f575 | 51 | case L2CAP_CONN_REQ: return "L2CAP_CONN_REQ"; |
kenbumono | 0:44619612f575 | 52 | case L2CAP_CONN_RSP: return "L2CAP_CONN_RSP"; |
kenbumono | 0:44619612f575 | 53 | case L2CAP_CONF_REQ: return "L2CAP_CONF_REQ"; |
kenbumono | 0:44619612f575 | 54 | case L2CAP_CONF_RSP: return "L2CAP_CONF_RSP"; |
kenbumono | 0:44619612f575 | 55 | case L2CAP_DISCONN_REQ: return "L2CAP_DISCONN_REQ"; |
kenbumono | 0:44619612f575 | 56 | case L2CAP_DISCONN_RSP: return "L2CAP_DISCONN_RSP"; |
kenbumono | 0:44619612f575 | 57 | case L2CAP_ECHO_REQ: return "L2CAP_ECHO_REQ"; |
kenbumono | 0:44619612f575 | 58 | case L2CAP_ECHO_RSP: return "L2CAP_ECHO_RSP"; |
kenbumono | 0:44619612f575 | 59 | case L2CAP_INFO_REQ: return "L2CAP_INFO_REQ"; |
kenbumono | 0:44619612f575 | 60 | case L2CAP_INFO_RSP: return "L2CAP_INFO_RSP"; |
kenbumono | 0:44619612f575 | 61 | } |
kenbumono | 0:44619612f575 | 62 | return "unknown"; |
kenbumono | 0:44619612f575 | 63 | } |
kenbumono | 0:44619612f575 | 64 | |
kenbumono | 0:44619612f575 | 65 | typedef struct |
kenbumono | 0:44619612f575 | 66 | { |
kenbumono | 0:44619612f575 | 67 | u16 handle; |
kenbumono | 0:44619612f575 | 68 | u16 length; // total |
kenbumono | 0:44619612f575 | 69 | u16 l2capLength; // length -4 |
kenbumono | 0:44619612f575 | 70 | u16 cid; // Signaling packet CID = 1 |
kenbumono | 0:44619612f575 | 71 | u8 data[64]; // Largest thing to send!!! todo |
kenbumono | 0:44619612f575 | 72 | } L2CAPData; |
kenbumono | 0:44619612f575 | 73 | |
kenbumono | 0:44619612f575 | 74 | typedef struct |
kenbumono | 0:44619612f575 | 75 | { |
kenbumono | 0:44619612f575 | 76 | u16 handle; |
kenbumono | 0:44619612f575 | 77 | u16 length; // total |
kenbumono | 0:44619612f575 | 78 | u16 l2capLength; // length -4 |
kenbumono | 0:44619612f575 | 79 | u16 cid; // Signaling packet CID = 1 |
kenbumono | 0:44619612f575 | 80 | |
kenbumono | 0:44619612f575 | 81 | // Payload |
kenbumono | 0:44619612f575 | 82 | u8 cmd; // |
kenbumono | 0:44619612f575 | 83 | u8 id; |
kenbumono | 0:44619612f575 | 84 | u16 cmdLength; // total-8 |
kenbumono | 0:44619612f575 | 85 | u16 params[4]; // Params |
kenbumono | 0:44619612f575 | 86 | } L2CAPCmd; |
kenbumono | 0:44619612f575 | 87 | |
kenbumono | 0:44619612f575 | 88 | void printf(const BD_ADDR* addr); |
kenbumono | 0:44619612f575 | 89 | |
kenbumono | 0:44619612f575 | 90 | // |
kenbumono | 0:44619612f575 | 91 | void BTDevice::Init() |
kenbumono | 0:44619612f575 | 92 | { |
kenbumono | 0:44619612f575 | 93 | memset(&_info,0,sizeof(inquiry_info)); |
kenbumono | 0:44619612f575 | 94 | _handle = 0; |
kenbumono | 0:44619612f575 | 95 | _name[0] = 0; |
kenbumono | 0:44619612f575 | 96 | _state = 0; |
kenbumono | 0:44619612f575 | 97 | } |
kenbumono | 0:44619612f575 | 98 | |
kenbumono | 0:44619612f575 | 99 | // virtual SocketHandler |
kenbumono | 0:44619612f575 | 100 | int BTDevice::Open(SocketInternal* sock, SocketAddrHdr* addr) |
kenbumono | 0:44619612f575 | 101 | { |
kenbumono | 0:44619612f575 | 102 | printf("Call to BTDevice Open \r\n"); |
kenbumono | 0:44619612f575 | 103 | L2CAPSocket* s = (L2CAPSocket*)sock; |
kenbumono | 0:44619612f575 | 104 | L2CAPAddr* a = (L2CAPAddr*)addr; |
kenbumono | 0:44619612f575 | 105 | s->scid = 0x40 + sock->ID-1; // are these reserved? |
kenbumono | 0:44619612f575 | 106 | s->dcid = 0; |
kenbumono | 0:44619612f575 | 107 | Connect(s->scid,a->psm); |
kenbumono | 0:44619612f575 | 108 | return sock->ID; |
kenbumono | 0:44619612f575 | 109 | } |
kenbumono | 0:44619612f575 | 110 | |
kenbumono | 0:44619612f575 | 111 | // virtual SocketHandler |
kenbumono | 0:44619612f575 | 112 | int BTDevice::Create(SocketInternal* sock, SocketAddrHdr* addr) |
kenbumono | 0:44619612f575 | 113 | { |
kenbumono | 0:44619612f575 | 114 | printf("Call to BTDevice Create \r\n"); |
kenbumono | 0:44619612f575 | 115 | L2CAPSocket* s = (L2CAPSocket*)sock; |
kenbumono | 0:44619612f575 | 116 | L2CAPAddr* a = (L2CAPAddr*)addr; |
kenbumono | 0:44619612f575 | 117 | |
kenbumono | 0:44619612f575 | 118 | //Connect(s->scid,a->psm); |
kenbumono | 0:44619612f575 | 119 | return sock->ID; |
kenbumono | 0:44619612f575 | 120 | } |
kenbumono | 0:44619612f575 | 121 | |
kenbumono | 0:44619612f575 | 122 | int BTDevice::Accept(SocketInternal* sock, SocketAddrHdr* addr) |
kenbumono | 0:44619612f575 | 123 | { |
kenbumono | 0:44619612f575 | 124 | printf("Call to BTDevice Accept \r\n"); |
kenbumono | 0:44619612f575 | 125 | L2CAPSocket* s = (L2CAPSocket*)sock; |
kenbumono | 0:44619612f575 | 126 | L2CAPAddr* a = (L2CAPAddr*)addr; |
kenbumono | 0:44619612f575 | 127 | |
kenbumono | 0:44619612f575 | 128 | |
kenbumono | 0:44619612f575 | 129 | printf("ID = %d scid = %d dcid = %d \r\n",sock->ID, s->scid, s->dcid); |
kenbumono | 0:44619612f575 | 130 | |
kenbumono | 0:44619612f575 | 131 | return sock->ID; |
kenbumono | 0:44619612f575 | 132 | } |
kenbumono | 0:44619612f575 | 133 | |
kenbumono | 0:44619612f575 | 134 | |
kenbumono | 0:44619612f575 | 135 | |
kenbumono | 0:44619612f575 | 136 | |
kenbumono | 0:44619612f575 | 137 | // virtual SocketHandler |
kenbumono | 0:44619612f575 | 138 | int BTDevice::Send(SocketInternal* sock, const u8* data, int len) |
kenbumono | 0:44619612f575 | 139 | { |
kenbumono | 0:44619612f575 | 140 | printf("Call to BTDevice Send \r\n"); |
kenbumono | 0:44619612f575 | 141 | L2CAPData d; |
kenbumono | 0:44619612f575 | 142 | L2CAPSocket* s = (L2CAPSocket*)sock; |
kenbumono | 0:44619612f575 | 143 | |
kenbumono | 0:44619612f575 | 144 | d.handle = _handle | 0x2000; |
kenbumono | 0:44619612f575 | 145 | d.length = 4 + len; |
kenbumono | 0:44619612f575 | 146 | d.l2capLength = len; |
kenbumono | 0:44619612f575 | 147 | d.cid = s->dcid; |
kenbumono | 0:44619612f575 | 148 | |
kenbumono | 0:44619612f575 | 149 | if (len > 64) |
kenbumono | 0:44619612f575 | 150 | return -1; |
kenbumono | 0:44619612f575 | 151 | memcpy(d.data,data,len); |
kenbumono | 0:44619612f575 | 152 | return Send((u8*)&d,len+8); |
kenbumono | 0:44619612f575 | 153 | } |
kenbumono | 0:44619612f575 | 154 | |
kenbumono | 0:44619612f575 | 155 | // virtual SocketHandler |
kenbumono | 0:44619612f575 | 156 | int BTDevice::Close(SocketInternal* sock) |
kenbumono | 0:44619612f575 | 157 | { |
kenbumono | 0:44619612f575 | 158 | printf("L2CAP close %d\r\n",sock->ID); |
kenbumono | 0:44619612f575 | 159 | L2CAPSocket* s = (L2CAPSocket*)sock; |
kenbumono | 0:44619612f575 | 160 | return Disconnect(s->scid,s->dcid); |
kenbumono | 0:44619612f575 | 161 | } |
kenbumono | 0:44619612f575 | 162 | |
kenbumono | 0:44619612f575 | 163 | L2CAPSocket* BTDevice::SCIDToSocket(int scid) |
kenbumono | 0:44619612f575 | 164 | { |
kenbumono | 0:44619612f575 | 165 | return (L2CAPSocket*)GetSocketInternal(scid-0x40+1); |
kenbumono | 0:44619612f575 | 166 | } |
kenbumono | 0:44619612f575 | 167 | |
kenbumono | 0:44619612f575 | 168 | int BTDevice::Send(const u8* data, int len) |
kenbumono | 0:44619612f575 | 169 | { |
kenbumono | 0:44619612f575 | 170 | printfBytes("L2CP send: ",data,len); |
kenbumono | 0:44619612f575 | 171 | _transport->ACLSend(data,len); |
kenbumono | 0:44619612f575 | 172 | return 0; |
kenbumono | 0:44619612f575 | 173 | } |
kenbumono | 0:44619612f575 | 174 | |
kenbumono | 0:44619612f575 | 175 | int BTDevice::Send(u8 c, u8 id, u16* params, int count) |
kenbumono | 0:44619612f575 | 176 | { |
kenbumono | 0:44619612f575 | 177 | L2CAPCmd cmd; |
kenbumono | 0:44619612f575 | 178 | cmd.handle = _handle | 0x2000; |
kenbumono | 0:44619612f575 | 179 | cmd.length = 8 + count*2; |
kenbumono | 0:44619612f575 | 180 | |
kenbumono | 0:44619612f575 | 181 | cmd.l2capLength = cmd.length-4; |
kenbumono | 0:44619612f575 | 182 | cmd.cid = 1; // Signaling packet |
kenbumono | 0:44619612f575 | 183 | |
kenbumono | 0:44619612f575 | 184 | cmd.cmd = c; |
kenbumono | 0:44619612f575 | 185 | cmd.id = id; |
kenbumono | 0:44619612f575 | 186 | cmd.cmdLength = count*2; |
kenbumono | 0:44619612f575 | 187 | for (int i = 0; i < count; i++) |
kenbumono | 0:44619612f575 | 188 | cmd.params[i] = params[i]; |
kenbumono | 0:44619612f575 | 189 | return Send((u8*)&cmd,cmd.length+4); |
kenbumono | 0:44619612f575 | 190 | } |
kenbumono | 0:44619612f575 | 191 | |
kenbumono | 0:44619612f575 | 192 | int BTDevice::Connect(int scid, int psm) |
kenbumono | 0:44619612f575 | 193 | { |
kenbumono | 0:44619612f575 | 194 | u16 p[2]; |
kenbumono | 0:44619612f575 | 195 | p[0] = psm; |
kenbumono | 0:44619612f575 | 196 | p[1] = scid; |
kenbumono | 0:44619612f575 | 197 | return Send(L2CAP_CONN_REQ,_txid++,p,2); |
kenbumono | 0:44619612f575 | 198 | } |
kenbumono | 0:44619612f575 | 199 | |
kenbumono | 0:44619612f575 | 200 | int BTDevice::Disconnect(int scid, int dcid) |
kenbumono | 0:44619612f575 | 201 | { |
kenbumono | 0:44619612f575 | 202 | u16 p[2]; |
kenbumono | 0:44619612f575 | 203 | p[0] = dcid; |
kenbumono | 0:44619612f575 | 204 | p[1] = scid; |
kenbumono | 0:44619612f575 | 205 | return Send(L2CAP_DISCONN_REQ,_txid++,p,2); |
kenbumono | 0:44619612f575 | 206 | } |
kenbumono | 0:44619612f575 | 207 | |
kenbumono | 0:44619612f575 | 208 | int BTDevice::ConfigureRequest(int dcid) |
kenbumono | 0:44619612f575 | 209 | { |
kenbumono | 0:44619612f575 | 210 | u16 p[4]; |
kenbumono | 0:44619612f575 | 211 | p[0] = dcid; |
kenbumono | 0:44619612f575 | 212 | p[1] = 0; |
kenbumono | 0:44619612f575 | 213 | p[2] = 0x0201; // Options |
kenbumono | 0:44619612f575 | 214 | p[3] = 0x02A0; // 672 |
kenbumono | 0:44619612f575 | 215 | return Send(L2CAP_CONF_REQ,_txid++,p,4); |
kenbumono | 0:44619612f575 | 216 | } |
kenbumono | 0:44619612f575 | 217 | |
kenbumono | 0:44619612f575 | 218 | int BTDevice::ConfigureResponse(u8 rxid, int dcid) |
kenbumono | 0:44619612f575 | 219 | { |
kenbumono | 0:44619612f575 | 220 | u16 p[3]; |
kenbumono | 0:44619612f575 | 221 | p[0] = dcid; |
kenbumono | 0:44619612f575 | 222 | p[1] = 0; |
kenbumono | 0:44619612f575 | 223 | p[2] = 0; |
kenbumono | 0:44619612f575 | 224 | return Send(L2CAP_CONF_RSP,rxid,p,3); |
kenbumono | 0:44619612f575 | 225 | } |
kenbumono | 0:44619612f575 | 226 | |
kenbumono | 0:44619612f575 | 227 | int BTDevice::DisconnectResponse(u8 rxid, int scid, int dcid) |
kenbumono | 0:44619612f575 | 228 | { |
kenbumono | 0:44619612f575 | 229 | u16 p[2]; |
kenbumono | 0:44619612f575 | 230 | p[0] = dcid; |
kenbumono | 0:44619612f575 | 231 | p[1] = scid; |
kenbumono | 0:44619612f575 | 232 | return Send(L2CAP_DISCONN_RSP,rxid,p,2); |
kenbumono | 0:44619612f575 | 233 | } |
kenbumono | 0:44619612f575 | 234 | |
kenbumono | 0:44619612f575 | 235 | int BTDevice::AcceptResponse(u8 rxid, int scid, int dcid) |
kenbumono | 0:44619612f575 | 236 | { |
kenbumono | 0:44619612f575 | 237 | printf("Connection accepted \r\n"); |
kenbumono | 0:44619612f575 | 238 | u16 p[4]; |
kenbumono | 0:44619612f575 | 239 | p[0] = scid; |
kenbumono | 0:44619612f575 | 240 | p[1] = dcid; |
kenbumono | 0:44619612f575 | 241 | p[2] = L2CAP_CONN_SUCCESS; |
kenbumono | 0:44619612f575 | 242 | p[3] = 0; |
kenbumono | 0:44619612f575 | 243 | |
kenbumono | 0:44619612f575 | 244 | return Send(L2CAP_CONN_RSP,rxid,p,4); |
kenbumono | 0:44619612f575 | 245 | |
kenbumono | 0:44619612f575 | 246 | } |
kenbumono | 0:44619612f575 | 247 | |
kenbumono | 0:44619612f575 | 248 | int BTDevice::RefuseResponse(u8 rxid) |
kenbumono | 0:44619612f575 | 249 | { |
kenbumono | 0:44619612f575 | 250 | printf("Connection refused \r\n"); |
kenbumono | 0:44619612f575 | 251 | u16 p[2]; |
kenbumono | 0:44619612f575 | 252 | p[0] = L2CAP_CONN_REF_PSM; |
kenbumono | 0:44619612f575 | 253 | p[1] = 0; |
kenbumono | 0:44619612f575 | 254 | |
kenbumono | 0:44619612f575 | 255 | return Send(L2CAP_CONN_RSP,rxid,p,2); |
kenbumono | 0:44619612f575 | 256 | } |
kenbumono | 0:44619612f575 | 257 | |
kenbumono | 0:44619612f575 | 258 | |
kenbumono | 0:44619612f575 | 259 | //int BTDevice::InUse(int psm) |
kenbumono | 0:44619612f575 | 260 | //{ |
kenbumono | 0:44619612f575 | 261 | // for (int i = 0; i < MAX_PORTS; i++){ |
kenbumono | 0:44619612f575 | 262 | // printf("Listen Q %d = %d \r\n",i, _listen[i]); |
kenbumono | 0:44619612f575 | 263 | // if ( _listen[i] == psm ) { |
kenbumono | 0:44619612f575 | 264 | // printf("We are listening on port %d \r\n",psm); //in use |
kenbumono | 0:44619612f575 | 265 | // return 0; |
kenbumono | 0:44619612f575 | 266 | // } |
kenbumono | 0:44619612f575 | 267 | // } |
kenbumono | 0:44619612f575 | 268 | // printf("We are not listening on port %d \r\n",psm); |
kenbumono | 0:44619612f575 | 269 | // return 1; |
kenbumono | 0:44619612f575 | 270 | //} |
kenbumono | 0:44619612f575 | 271 | |
kenbumono | 0:44619612f575 | 272 | |
kenbumono | 0:44619612f575 | 273 | |
kenbumono | 0:44619612f575 | 274 | |
kenbumono | 0:44619612f575 | 275 | void BTDevice::Control(const BD_ADDR* addr, const u8* data, int len) |
kenbumono | 0:44619612f575 | 276 | { |
kenbumono | 0:44619612f575 | 277 | |
kenbumono | 0:44619612f575 | 278 | SocketInternal* sock; |
kenbumono | 0:44619612f575 | 279 | L2CAPSocket* s; |
kenbumono | 0:44619612f575 | 280 | int psm,scid,dcid,flags; |
kenbumono | 0:44619612f575 | 281 | |
kenbumono | 0:44619612f575 | 282 | printf("From address "); |
kenbumono | 0:44619612f575 | 283 | printf(addr); |
kenbumono | 0:44619612f575 | 284 | printf(" : "); |
kenbumono | 0:44619612f575 | 285 | |
kenbumono | 0:44619612f575 | 286 | int cc = data[8]; |
kenbumono | 0:44619612f575 | 287 | printf(L2CAP_ComandCodeStr(cc)); |
kenbumono | 0:44619612f575 | 288 | int result = LE16(data+16); |
kenbumono | 0:44619612f575 | 289 | printf(" Result %d\r\n",result); |
kenbumono | 0:44619612f575 | 290 | |
kenbumono | 0:44619612f575 | 291 | |
kenbumono | 0:44619612f575 | 292 | switch (cc) |
kenbumono | 0:44619612f575 | 293 | { |
kenbumono | 0:44619612f575 | 294 | case L2CAP_COMMAND_REJ: |
kenbumono | 0:44619612f575 | 295 | break; |
kenbumono | 0:44619612f575 | 296 | case L2CAP_CONN_REQ: |
kenbumono | 0:44619612f575 | 297 | { |
kenbumono | 0:44619612f575 | 298 | psm = LE16(data+12); |
kenbumono | 0:44619612f575 | 299 | scid = LE16(data+14); |
kenbumono | 0:44619612f575 | 300 | printf("Connection request scid = %d psm = %d \r\n",scid,psm); |
kenbumono | 0:44619612f575 | 301 | |
kenbumono | 0:44619612f575 | 302 | // check if we listen on the port |
kenbumono | 0:44619612f575 | 303 | //if ( InUse(psm) ) { |
kenbumono | 0:44619612f575 | 304 | if ( Socket_InUse(SOCKET_L2CAP,psm) ) { |
kenbumono | 0:44619612f575 | 305 | RefuseResponse(data[9]); |
kenbumono | 0:44619612f575 | 306 | |
kenbumono | 0:44619612f575 | 307 | |
kenbumono | 0:44619612f575 | 308 | } else { |
kenbumono | 0:44619612f575 | 309 | L2CAPAddr sockAddr; |
kenbumono | 0:44619612f575 | 310 | sockAddr.bdaddr = *addr; |
kenbumono | 0:44619612f575 | 311 | sock = Socket_Create(SOCKET_L2CAP, &sockAddr.hdr, psm); |
kenbumono | 0:44619612f575 | 312 | s = (L2CAPSocket*)sock; |
kenbumono | 0:44619612f575 | 313 | s->scid = 0x40 + sock->ID-1; // are these reserved? |
kenbumono | 0:44619612f575 | 314 | s->dcid = scid; |
kenbumono | 0:44619612f575 | 315 | AcceptResponse(data[9],s->scid,s->dcid); |
kenbumono | 0:44619612f575 | 316 | |
kenbumono | 0:44619612f575 | 317 | ConfigureRequest(s->dcid); // handshake |
kenbumono | 0:44619612f575 | 318 | |
kenbumono | 0:44619612f575 | 319 | sock->SetState(SocketState_Accept); |
kenbumono | 0:44619612f575 | 320 | } |
kenbumono | 0:44619612f575 | 321 | |
kenbumono | 0:44619612f575 | 322 | /** |
kenbumono | 0:44619612f575 | 323 | for (int i = 0; i < MAX_PORTS; i++){ |
kenbumono | 0:44619612f575 | 324 | if ( _listen[i] == psm ) { |
kenbumono | 0:44619612f575 | 325 | printf("We are listening on port %d \r\n",psm); //in use |
kenbumono | 0:44619612f575 | 326 | RefuseResponse(data[9]); |
kenbumono | 0:44619612f575 | 327 | break; |
kenbumono | 0:44619612f575 | 328 | } |
kenbumono | 0:44619612f575 | 329 | } |
kenbumono | 0:44619612f575 | 330 | **/ |
kenbumono | 0:44619612f575 | 331 | |
kenbumono | 0:44619612f575 | 332 | //L2CAPAddr sockAddr; |
kenbumono | 0:44619612f575 | 333 | //sockAddr.bdaddr = addr; |
kenbumono | 0:44619612f575 | 334 | //sockAddr.psm = psm; |
kenbumono | 0:44619612f575 | 335 | |
kenbumono | 0:44619612f575 | 336 | } |
kenbumono | 0:44619612f575 | 337 | break; |
kenbumono | 0:44619612f575 | 338 | |
kenbumono | 0:44619612f575 | 339 | // Response to our initial connect from Remote |
kenbumono | 0:44619612f575 | 340 | case L2CAP_CONN_RSP: |
kenbumono | 0:44619612f575 | 341 | { |
kenbumono | 0:44619612f575 | 342 | if (result == 0) |
kenbumono | 0:44619612f575 | 343 | { |
kenbumono | 0:44619612f575 | 344 | printf("Connect succeeded\r\n"); |
kenbumono | 0:44619612f575 | 345 | dcid = LE16(data+12); |
kenbumono | 0:44619612f575 | 346 | scid = LE16(data+14); |
kenbumono | 0:44619612f575 | 347 | L2CAPSocket* s = SCIDToSocket(scid); |
kenbumono | 0:44619612f575 | 348 | if (s) |
kenbumono | 0:44619612f575 | 349 | { |
kenbumono | 0:44619612f575 | 350 | s->dcid = dcid; |
kenbumono | 0:44619612f575 | 351 | ConfigureRequest(dcid); |
kenbumono | 0:44619612f575 | 352 | } |
kenbumono | 0:44619612f575 | 353 | } else |
kenbumono | 0:44619612f575 | 354 | printf("Connect failed?\r\n"); |
kenbumono | 0:44619612f575 | 355 | } |
kenbumono | 0:44619612f575 | 356 | break; |
kenbumono | 0:44619612f575 | 357 | |
kenbumono | 0:44619612f575 | 358 | case L2CAP_CONF_RSP: |
kenbumono | 0:44619612f575 | 359 | { |
kenbumono | 0:44619612f575 | 360 | scid = LE16(data+12); |
kenbumono | 0:44619612f575 | 361 | SocketInternal* s = (SocketInternal*)SCIDToSocket(scid); |
kenbumono | 0:44619612f575 | 362 | if (s) |
kenbumono | 0:44619612f575 | 363 | s->SetState(SocketState_Open); |
kenbumono | 0:44619612f575 | 364 | } |
kenbumono | 0:44619612f575 | 365 | break; |
kenbumono | 0:44619612f575 | 366 | |
kenbumono | 0:44619612f575 | 367 | case L2CAP_CONF_REQ: |
kenbumono | 0:44619612f575 | 368 | { |
kenbumono | 0:44619612f575 | 369 | u16 dcid = LE16(data+12); |
kenbumono | 0:44619612f575 | 370 | u16 flags = LE16(data+14); |
kenbumono | 0:44619612f575 | 371 | printf("Config request dcid = %02X flags = %02X\r\n", dcid, flags); |
kenbumono | 0:44619612f575 | 372 | scid = LE16(data+12); |
kenbumono | 0:44619612f575 | 373 | L2CAPSocket* s = SCIDToSocket(scid); |
kenbumono | 0:44619612f575 | 374 | if (s) |
kenbumono | 0:44619612f575 | 375 | ConfigureResponse(data[9],s->dcid); |
kenbumono | 0:44619612f575 | 376 | } |
kenbumono | 0:44619612f575 | 377 | break; |
kenbumono | 0:44619612f575 | 378 | case L2CAP_DISCONN_REQ: { |
kenbumono | 0:44619612f575 | 379 | int dcid = LE16(data+12); |
kenbumono | 0:44619612f575 | 380 | int scid = LE16(data+14); |
kenbumono | 0:44619612f575 | 381 | L2CAPSocket* s = SCIDToSocket(scid); |
kenbumono | 0:44619612f575 | 382 | //s->si.SetState(SocketState_Closed); |
kenbumono | 0:44619612f575 | 383 | DisconnectResponse(data[9], scid, dcid); |
kenbumono | 0:44619612f575 | 384 | } |
kenbumono | 0:44619612f575 | 385 | break; |
kenbumono | 0:44619612f575 | 386 | |
kenbumono | 0:44619612f575 | 387 | } |
kenbumono | 0:44619612f575 | 388 | } |
kenbumono | 0:44619612f575 | 389 | |
kenbumono | 0:44619612f575 | 390 | void BTDevice::ACLRecv(const BD_ADDR* addr, const u8* data, int len) |
kenbumono | 0:44619612f575 | 391 | { |
kenbumono | 0:44619612f575 | 392 | //printfBytes("L2CP recv: ",data,16); |
kenbumono | 0:44619612f575 | 393 | int handle = LE16(data); |
kenbumono | 0:44619612f575 | 394 | if (handle != (0x2000 | _handle)) |
kenbumono | 0:44619612f575 | 395 | return; |
kenbumono | 0:44619612f575 | 396 | |
kenbumono | 0:44619612f575 | 397 | int cid = LE16(data+6); |
kenbumono | 0:44619612f575 | 398 | if (cid == 1) |
kenbumono | 0:44619612f575 | 399 | { |
kenbumono | 0:44619612f575 | 400 | Control(addr,data,len); |
kenbumono | 0:44619612f575 | 401 | return; |
kenbumono | 0:44619612f575 | 402 | } |
kenbumono | 0:44619612f575 | 403 | |
kenbumono | 0:44619612f575 | 404 | SocketInternal* s = (SocketInternal*)SCIDToSocket(cid); |
kenbumono | 0:44619612f575 | 405 | if (s) |
kenbumono | 0:44619612f575 | 406 | s->Recv(data+8,LE16(data+2)-4); |
kenbumono | 0:44619612f575 | 407 | else |
kenbumono | 0:44619612f575 | 408 | printf("Bad event cid %d\r\n",cid); |
kenbumono | 0:44619612f575 | 409 | } |