Simon Ford / Mbed 2 deprecated WiiRacing

Dependencies:   mbed m3pi ID12RFIDIRQ

Committer:
simon
Date:
Mon Apr 11 23:03:22 2011 +0000
Revision:
0:e2ef12467862

        

Who changed what in which revision?

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