BlueUSB with USB-> SERIAL (CP210x) GPIO pins working.

Dependencies:   mbed

Committer:
tecnosys
Date:
Fri Apr 23 05:04:28 2010 +0000
Revision:
0:a14eaa2e1445

        

Who changed what in which revision?

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