XOOMの動作状況を聞き処理を変えてみました。 USBケーブルを抜いた際に処理を終了するようにしました。

Dependencies:   mbed

Committer:
abe00makoto
Date:
Fri May 27 18:51:15 2011 +0000
Revision:
3:432e5675d240
Parent:
0:9fb6c423e32c
nexus one support
maybe support add XOOM ,nexus S

Who changed what in which revision?

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