This is a for debugging \\\\\\\"BLUE USB\\\\\\\". You can connect with HCI mode. How to connect White Wizard Board TANK *White Wizard Board - Motor Driver Board * p 21 - IN_R1 * p 22 - IN_R2 * p 23 - IN_L2 * p 24 - IN_L1

Dependencies:   mbed

Committer:
halfpitch
Date:
Sun Jul 10 01:31:52 2011 +0000
Revision:
0:a6476c138e84

        

Who changed what in which revision?

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