Based on PS3_BlueUSB with reference to http://blog.goo.ne.jp/roboz80/e/10e7bf38d3a63b996ca2894e9fb5e3b6

Dependencies:   TextLCD mbed

Committer:
kenbumono
Date:
Tue Jul 05 08:25:40 2011 +0000
Revision:
0:44619612f575

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenbumono 0:44619612f575 1
kenbumono 0:44619612f575 2 /*
kenbumono 0:44619612f575 3 Copyright (c) 2010 Peter Barrett
kenbumono 0:44619612f575 4
kenbumono 0:44619612f575 5 Permission is hereby granted, free of charge, to any person obtaining a copy
kenbumono 0:44619612f575 6 of this software and associated documentation files (the "Software"), to deal
kenbumono 0:44619612f575 7 in the Software without restriction, including without limitation the rights
kenbumono 0:44619612f575 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
kenbumono 0:44619612f575 9 copies of the Software, and to permit persons to whom the Software is
kenbumono 0:44619612f575 10 furnished to do so, subject to the following conditions:
kenbumono 0:44619612f575 11
kenbumono 0:44619612f575 12 The above copyright notice and this permission notice shall be included in
kenbumono 0:44619612f575 13 all copies or substantial portions of the Software.
kenbumono 0:44619612f575 14
kenbumono 0:44619612f575 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
kenbumono 0:44619612f575 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
kenbumono 0:44619612f575 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
kenbumono 0:44619612f575 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
kenbumono 0:44619612f575 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenbumono 0:44619612f575 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
kenbumono 0:44619612f575 21 THE SOFTWARE.
kenbumono 0:44619612f575 22 */
kenbumono 0:44619612f575 23
kenbumono 0:44619612f575 24 /*
kenbumono 0:44619612f575 25 Tue Apr 26 2011 Bart Janssens: added PS3 Bluetooth support
kenbumono 0:44619612f575 26 */
kenbumono 0:44619612f575 27
kenbumono 0:44619612f575 28 #include <stdio.h>
kenbumono 0:44619612f575 29 #include <stdlib.h>
kenbumono 0:44619612f575 30 #include <string.h>
kenbumono 0:44619612f575 31
kenbumono 0:44619612f575 32 #include "Utils.h"
kenbumono 0:44619612f575 33 #include "USBHost.h"
kenbumono 0:44619612f575 34 #include "hci.h"
kenbumono 0:44619612f575 35 #include "ps3.h"
kenbumono 0:44619612f575 36
kenbumono 0:44619612f575 37 #include "mbed.h"
kenbumono 0:44619612f575 38
kenbumono 0:44619612f575 39 void printf(const BD_ADDR* addr)
kenbumono 0:44619612f575 40 {
kenbumono 0:44619612f575 41 const u8* a = addr->addr;
kenbumono 0:44619612f575 42 printf("%02X:%02X:%02X:%02X:%02X:%02X",a[5],a[4],a[3],a[2],a[1],a[0]);
kenbumono 0:44619612f575 43 }
kenbumono 0:44619612f575 44
kenbumono 0:44619612f575 45 #define MAX_HCL_SIZE 260
kenbumono 0:44619612f575 46 #define MAX_ACL_SIZE 400
kenbumono 0:44619612f575 47
kenbumono 0:44619612f575 48 class HCITransportUSB : public HCITransport
kenbumono 0:44619612f575 49 {
kenbumono 0:44619612f575 50 int _device;
kenbumono 0:44619612f575 51 u8* _hciBuffer;
kenbumono 0:44619612f575 52 u8* _aclBuffer;
kenbumono 0:44619612f575 53
kenbumono 0:44619612f575 54 public:
kenbumono 0:44619612f575 55 void Open(int device, u8* hciBuffer, u8* aclBuffer)
kenbumono 0:44619612f575 56 {
kenbumono 0:44619612f575 57 _device = device;
kenbumono 0:44619612f575 58 _hciBuffer = hciBuffer;
kenbumono 0:44619612f575 59 _aclBuffer = aclBuffer;
kenbumono 0:44619612f575 60 USBInterruptTransfer(_device,0x81,_hciBuffer,MAX_HCL_SIZE,HciCallback,this);
kenbumono 0:44619612f575 61 USBBulkTransfer(_device,0x82,_aclBuffer,MAX_ACL_SIZE,AclCallback,this);
kenbumono 0:44619612f575 62 }
kenbumono 0:44619612f575 63
kenbumono 0:44619612f575 64 static void HciCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
kenbumono 0:44619612f575 65 {
kenbumono 0:44619612f575 66 HCI* t = ((HCITransportUSB*)userData)->_target;
kenbumono 0:44619612f575 67 if (t)
kenbumono 0:44619612f575 68 t->HCIRecv(data,len);
kenbumono 0:44619612f575 69 USBInterruptTransfer(device,0x81,data,MAX_HCL_SIZE,HciCallback,userData);
kenbumono 0:44619612f575 70 }
kenbumono 0:44619612f575 71
kenbumono 0:44619612f575 72 static void AclCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
kenbumono 0:44619612f575 73 {
kenbumono 0:44619612f575 74 HCI* t = ((HCITransportUSB*)userData)->_target;
kenbumono 0:44619612f575 75 if (t)
kenbumono 0:44619612f575 76 t->ACLRecv(data,len);
kenbumono 0:44619612f575 77 USBBulkTransfer(device,0x82,data,MAX_ACL_SIZE,AclCallback,userData);
kenbumono 0:44619612f575 78 }
kenbumono 0:44619612f575 79
kenbumono 0:44619612f575 80 virtual void HCISend(const u8* data, int len)
kenbumono 0:44619612f575 81 {
kenbumono 0:44619612f575 82 USBControlTransfer(_device,REQUEST_TYPE_CLASS, 0, 0, 0,(u8*)data,len);
kenbumono 0:44619612f575 83 }
kenbumono 0:44619612f575 84
kenbumono 0:44619612f575 85 virtual void ACLSend(const u8* data, int len)
kenbumono 0:44619612f575 86 {
kenbumono 0:44619612f575 87 USBBulkTransfer(_device,0x02,(u8*)data,len);
kenbumono 0:44619612f575 88 }
kenbumono 0:44619612f575 89 };
kenbumono 0:44619612f575 90
kenbumono 0:44619612f575 91
kenbumono 0:44619612f575 92 #define WII_REMOTE 0x042500
kenbumono 0:44619612f575 93 #define PS3_REMOTE 0x080500
kenbumono 0:44619612f575 94
kenbumono 0:44619612f575 95 class HIDBluetooth
kenbumono 0:44619612f575 96 {
kenbumono 0:44619612f575 97 int _control; // Sockets for control (out) and interrupt (in)
kenbumono 0:44619612f575 98 int _interrupt;
kenbumono 0:44619612f575 99 int _devClass;
kenbumono 0:44619612f575 100 BD_ADDR _addr;
kenbumono 0:44619612f575 101 u8 _pad[2]; // Struct align
kenbumono 0:44619612f575 102 int _ready;
kenbumono 0:44619612f575 103 Timeout _timeout;
kenbumono 0:44619612f575 104 int _count;
kenbumono 0:44619612f575 105
kenbumono 0:44619612f575 106 public:
kenbumono 0:44619612f575 107 HIDBluetooth() : _control(0),_interrupt(0),_devClass(0), _ready(1) {};
kenbumono 0:44619612f575 108
kenbumono 0:44619612f575 109
kenbumono 0:44619612f575 110 bool InUse()
kenbumono 0:44619612f575 111 {
kenbumono 0:44619612f575 112 return _control != 0;
kenbumono 0:44619612f575 113 }
kenbumono 0:44619612f575 114
kenbumono 0:44619612f575 115 void attimeout()
kenbumono 0:44619612f575 116 {
kenbumono 0:44619612f575 117 printf("Timeout reached\r\n");
kenbumono 0:44619612f575 118 }
kenbumono 0:44619612f575 119
kenbumono 0:44619612f575 120 static void OnHidInterrupt(int socket, SocketState state,const u8* data, int len, void* userData)
kenbumono 0:44619612f575 121 {
kenbumono 0:44619612f575 122 HIDBluetooth* t = (HIDBluetooth*)userData;
kenbumono 0:44619612f575 123 t->_ready = 0;
kenbumono 0:44619612f575 124 if (data)
kenbumono 0:44619612f575 125 {
kenbumono 0:44619612f575 126 //printf("devClass = %06X \r\n",t->_devClass);
kenbumono 0:44619612f575 127 if (t->_devClass == WII_REMOTE && data[1] == 0x30)
kenbumono 0:44619612f575 128 {
kenbumono 0:44619612f575 129 printf("================wii====================\r\n");
kenbumono 0:44619612f575 130 t->WIILed();
kenbumono 0:44619612f575 131 t->WIIHid(); // ask for accelerometer
kenbumono 0:44619612f575 132 t->_devClass = 0;
kenbumono 0:44619612f575 133
kenbumono 0:44619612f575 134
kenbumono 0:44619612f575 135 const u8* d = data;
kenbumono 0:44619612f575 136 switch (d[1])
kenbumono 0:44619612f575 137 {
kenbumono 0:44619612f575 138 case 0x02:
kenbumono 0:44619612f575 139 {
kenbumono 0:44619612f575 140 int x = (signed char)d[3];
kenbumono 0:44619612f575 141 int y = (signed char)d[4];
kenbumono 0:44619612f575 142 printf("Mouse %2X dx:%d dy:%d\r\n",d[2],x,y);
kenbumono 0:44619612f575 143 }
kenbumono 0:44619612f575 144 break;
kenbumono 0:44619612f575 145
kenbumono 0:44619612f575 146 case 0x37: // Accelerometer http://wiki.wiimoteproject.com/Reports
kenbumono 0:44619612f575 147 {
kenbumono 0:44619612f575 148 int pad = (d[2] & 0x9F) | ((d[3] & 0x9F) << 8);
kenbumono 0:44619612f575 149 int x = (d[2] & 0x60) >> 5 | d[4] << 2;
kenbumono 0:44619612f575 150 int y = (d[3] & 0x20) >> 4 | d[5] << 2;
kenbumono 0:44619612f575 151 int z = (d[3] & 0x40) >> 5 | d[6] << 2;
kenbumono 0:44619612f575 152 printf("WII %04X %d %d %d\r\n",pad,x,y,z);
kenbumono 0:44619612f575 153 }
kenbumono 0:44619612f575 154 break;
kenbumono 0:44619612f575 155 default:
kenbumono 0:44619612f575 156 printHex(data,len);
kenbumono 0:44619612f575 157 }
kenbumono 0:44619612f575 158 }
kenbumono 0:44619612f575 159 if (t->_devClass == PS3_REMOTE)
kenbumono 0:44619612f575 160 {
kenbumono 0:44619612f575 161 t->_count ++;
kenbumono 0:44619612f575 162 if (t->_count == 25) t->_count = 1;
kenbumono 0:44619612f575 163 ParsePs3Result((data + 1), sizeof(ps3report),t->_count);
kenbumono 0:44619612f575 164 }
kenbumono 0:44619612f575 165 else {
kenbumono 0:44619612f575 166 printf("Not yet implemented \r\n");
kenbumono 0:44619612f575 167
kenbumono 0:44619612f575 168 }
kenbumono 0:44619612f575 169 }
kenbumono 0:44619612f575 170
kenbumono 0:44619612f575 171 }
kenbumono 0:44619612f575 172
kenbumono 0:44619612f575 173 static void OnHidControl(int socket, SocketState state, const u8* data, int len, void* userData)
kenbumono 0:44619612f575 174 {
kenbumono 0:44619612f575 175 //HIDBluetooth* t = (HIDBluetooth*)userData;
kenbumono 0:44619612f575 176
kenbumono 0:44619612f575 177 //printf("OnHidControl\r\n");
kenbumono 0:44619612f575 178
kenbumono 0:44619612f575 179 }
kenbumono 0:44619612f575 180
kenbumono 0:44619612f575 181 static void OnAcceptCtrlSocket(int socket, SocketState state, const u8* data, int len, void* userData)
kenbumono 0:44619612f575 182 {
kenbumono 0:44619612f575 183 HIDBluetooth* t = (HIDBluetooth*)userData;
kenbumono 0:44619612f575 184
kenbumono 0:44619612f575 185 t->_control = socket;
kenbumono 0:44619612f575 186
kenbumono 0:44619612f575 187 //printf("Ctrl Socket number = %d \r\n", socket);
kenbumono 0:44619612f575 188
kenbumono 0:44619612f575 189 Socket_Accept(socket,OnHidControl,userData);
kenbumono 0:44619612f575 190 u8 enable[6] = {
kenbumono 0:44619612f575 191 0x53, /* HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE */
kenbumono 0:44619612f575 192 0xf4, 0x42, 0x03, 0x00, 0x00 };
kenbumono 0:44619612f575 193 Socket_Send(socket,enable,6);
kenbumono 0:44619612f575 194
kenbumono 0:44619612f575 195
kenbumono 0:44619612f575 196 }
kenbumono 0:44619612f575 197
kenbumono 0:44619612f575 198 static void OnAcceptDataSocket(int socket, SocketState state, const u8* data, int len, void* userData)
kenbumono 0:44619612f575 199 {
kenbumono 0:44619612f575 200 HIDBluetooth* t = (HIDBluetooth*)userData;
kenbumono 0:44619612f575 201 t->_interrupt = socket;
kenbumono 0:44619612f575 202
kenbumono 0:44619612f575 203 printf("OnAcceptDataSocket: Data Socket accept here \r\n");
kenbumono 0:44619612f575 204 printf("OnAcceptDataSocket: Data Socket number = %d \r\n", socket);
kenbumono 0:44619612f575 205
kenbumono 0:44619612f575 206 //printf("OnAcceptDataSocket: Ctrl Socket = %d Data Socket accept = %d \r\n", t->_control, t->_interrupt);
kenbumono 0:44619612f575 207
kenbumono 0:44619612f575 208 Socket_Accept(socket,OnHidInterrupt,userData);
kenbumono 0:44619612f575 209
kenbumono 0:44619612f575 210 //if (data)
kenbumono 0:44619612f575 211 // printHex(data,len);
kenbumono 0:44619612f575 212 }
kenbumono 0:44619612f575 213
kenbumono 0:44619612f575 214 void Open(BD_ADDR* bdAddr, inquiry_info* info)
kenbumono 0:44619612f575 215 {
kenbumono 0:44619612f575 216 printf("L2CAPAddr size %d\r\n",sizeof(L2CAPAddr));
kenbumono 0:44619612f575 217 _addr = *bdAddr;
kenbumono 0:44619612f575 218 L2CAPAddr sockAddr;
kenbumono 0:44619612f575 219 sockAddr.bdaddr = _addr;
kenbumono 0:44619612f575 220 sockAddr.psm = L2CAP_PSM_HID_INTR;
kenbumono 0:44619612f575 221 printf("Socket_Open size %d\r\n",sizeof(L2CAPAddr));
kenbumono 0:44619612f575 222 _interrupt = Socket_Open(SOCKET_L2CAP,&sockAddr.hdr,OnHidInterrupt,this);
kenbumono 0:44619612f575 223 sockAddr.psm = L2CAP_PSM_HID_CNTL;
kenbumono 0:44619612f575 224 _control = Socket_Open(SOCKET_L2CAP,&sockAddr.hdr,OnHidControl,this);
kenbumono 0:44619612f575 225
kenbumono 0:44619612f575 226 printfBytes("OPEN DEVICE CLASS",info->dev_class,3);
kenbumono 0:44619612f575 227 _devClass = (info->dev_class[0] << 16) | (info->dev_class[1] << 8) | info->dev_class[2];
kenbumono 0:44619612f575 228 }
kenbumono 0:44619612f575 229
kenbumono 0:44619612f575 230 void Listen(BD_ADDR* bdAddr, inquiry_info* info)
kenbumono 0:44619612f575 231 {
kenbumono 0:44619612f575 232 int result;
kenbumono 0:44619612f575 233 //printf("L2CAPAddr size %d\r\n",sizeof(L2CAPAddr));
kenbumono 0:44619612f575 234 _addr = *bdAddr;
kenbumono 0:44619612f575 235 L2CAPAddr sockAddr;
kenbumono 0:44619612f575 236 sockAddr.bdaddr = _addr;
kenbumono 0:44619612f575 237
kenbumono 0:44619612f575 238 _count = 1;
kenbumono 0:44619612f575 239 _ready = 1;
kenbumono 0:44619612f575 240
kenbumono 0:44619612f575 241 // set a buffer for the led&rumble report
kenbumono 0:44619612f575 242 u8 abuffer[37] = {
kenbumono 0:44619612f575 243 0x52, /* HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_OUTPUT */
kenbumono 0:44619612f575 244 0x01,
kenbumono 0:44619612f575 245 0x00, 0x00, 0x00, 0x00, 0x00,
kenbumono 0:44619612f575 246 0x00, 0x00, 0x00, 0x00, 0x1E,
kenbumono 0:44619612f575 247 0xff, 0x27, 0x10, 0x00, 0x32,
kenbumono 0:44619612f575 248 0xff, 0x27, 0x10, 0x00, 0x32,
kenbumono 0:44619612f575 249 0xff, 0x27, 0x10, 0x00, 0x32,
kenbumono 0:44619612f575 250 0xff, 0x27, 0x10, 0x00, 0x32,
kenbumono 0:44619612f575 251 0x00, 0x00, 0x00, 0x00, 0x00,
kenbumono 0:44619612f575 252 };
kenbumono 0:44619612f575 253 memcpy(_ledrumble,abuffer,37);
kenbumono 0:44619612f575 254
kenbumono 0:44619612f575 255 result = Socket_Listen(SOCKET_L2CAP,L2CAP_PSM_HID_CNTL,OnAcceptCtrlSocket,this);
kenbumono 0:44619612f575 256 printf("listen return code ctrl socket = %d \r\n", result);
kenbumono 0:44619612f575 257
kenbumono 0:44619612f575 258
kenbumono 0:44619612f575 259 result = Socket_Listen(SOCKET_L2CAP,L2CAP_PSM_HID_INTR,OnAcceptDataSocket,this);
kenbumono 0:44619612f575 260 printf("listen return code data socket = %d \r\n", result);
kenbumono 0:44619612f575 261
kenbumono 0:44619612f575 262 printfBytes("OPEN DEVICE CLASS",info->dev_class,3);
kenbumono 0:44619612f575 263 _devClass = (info->dev_class[0] << 16) | (info->dev_class[1] << 8) | info->dev_class[2];
kenbumono 0:44619612f575 264
kenbumono 0:44619612f575 265 while (_ready){ // wait till we receive data from PS3Hid
kenbumono 0:44619612f575 266 USBLoop();
kenbumono 0:44619612f575 267 }
kenbumono 0:44619612f575 268 USBLoop();
kenbumono 0:44619612f575 269
kenbumono 0:44619612f575 270
kenbumono 0:44619612f575 271
kenbumono 0:44619612f575 272 }
kenbumono 0:44619612f575 273
kenbumono 0:44619612f575 274 void Close()
kenbumono 0:44619612f575 275 {
kenbumono 0:44619612f575 276 if (_control)
kenbumono 0:44619612f575 277 Socket_Close(_control);
kenbumono 0:44619612f575 278 if (_interrupt)
kenbumono 0:44619612f575 279 Socket_Close(_interrupt);
kenbumono 0:44619612f575 280 _control = _interrupt = 0;
kenbumono 0:44619612f575 281 }
kenbumono 0:44619612f575 282
kenbumono 0:44619612f575 283 void WIILed(int id = 0x10)
kenbumono 0:44619612f575 284 {
kenbumono 0:44619612f575 285 u8 led[3] = {0x52, 0x11, id};
kenbumono 0:44619612f575 286 if (_control)
kenbumono 0:44619612f575 287 Socket_Send(_control,led,3);
kenbumono 0:44619612f575 288 }
kenbumono 0:44619612f575 289
kenbumono 0:44619612f575 290 void WIIHid(int report = 0x37)
kenbumono 0:44619612f575 291 {
kenbumono 0:44619612f575 292 u8 hid[4] = { 0x52, 0x12, 0x00, report };
kenbumono 0:44619612f575 293 if (_control != -1)
kenbumono 0:44619612f575 294 Socket_Send(_control,hid,4);
kenbumono 0:44619612f575 295 }
kenbumono 0:44619612f575 296
kenbumono 0:44619612f575 297
kenbumono 0:44619612f575 298
kenbumono 0:44619612f575 299 void Ps3Hid_Led(int i)
kenbumono 0:44619612f575 300 {
kenbumono 0:44619612f575 301 printf("Ps3Hid led %d\r\n",i);
kenbumono 0:44619612f575 302 u8 ledpattern[7] = {0x02, 0x04, 0x08, 0x10, 0x12, 0x14, 0x18 };
kenbumono 0:44619612f575 303 u8 buf[37];
kenbumono 0:44619612f575 304
kenbumono 0:44619612f575 305 if (i < 7) _ledrumble[11] = ledpattern[i];
kenbumono 0:44619612f575 306 memcpy(buf, _ledrumble, 37);
kenbumono 0:44619612f575 307
kenbumono 0:44619612f575 308 if (_control != -1)
kenbumono 0:44619612f575 309 Socket_Send(_control,buf,37);
kenbumono 0:44619612f575 310 wait_ms(4);
kenbumono 0:44619612f575 311 }
kenbumono 0:44619612f575 312
kenbumono 0:44619612f575 313 void Ps3Hid_Rumble(u8 duration_right, u8 power_right, u8 duration_left, u8 power_left )
kenbumono 0:44619612f575 314 {
kenbumono 0:44619612f575 315 printf("Ps3Hid rumble \r\n");
kenbumono 0:44619612f575 316 u8 buf[37];
kenbumono 0:44619612f575 317
kenbumono 0:44619612f575 318 memcpy(buf, _ledrumble, 37);
kenbumono 0:44619612f575 319 buf[3] = duration_right;
kenbumono 0:44619612f575 320 buf[4] = power_right;
kenbumono 0:44619612f575 321 buf[5] = duration_left;
kenbumono 0:44619612f575 322 buf[6] = power_left;
kenbumono 0:44619612f575 323
kenbumono 0:44619612f575 324 if (_control != -1)
kenbumono 0:44619612f575 325 Socket_Send(_control,buf,37);
kenbumono 0:44619612f575 326 wait_ms(4);
kenbumono 0:44619612f575 327 }
kenbumono 0:44619612f575 328
kenbumono 0:44619612f575 329 int CheckHID()
kenbumono 0:44619612f575 330 {
kenbumono 0:44619612f575 331 printf("CheckHID \r\n");
kenbumono 0:44619612f575 332 printf("Ctrl = %d Intr = %d \r\n", _control, _interrupt);
kenbumono 0:44619612f575 333 if (_control < 1) {
kenbumono 0:44619612f575 334 printf("Ps3 not ready \r\n");
kenbumono 0:44619612f575 335 return 1;
kenbumono 0:44619612f575 336 } else {
kenbumono 0:44619612f575 337 printf("Ps3 ready %d \r\n",_control);
kenbumono 0:44619612f575 338 return 0;
kenbumono 0:44619612f575 339 }
kenbumono 0:44619612f575 340 }
kenbumono 0:44619612f575 341 private:
kenbumono 0:44619612f575 342 u8 _ledrumble[37] ;
kenbumono 0:44619612f575 343 };
kenbumono 0:44619612f575 344
kenbumono 0:44619612f575 345
kenbumono 0:44619612f575 346 HCI* gHCI = 0;
kenbumono 0:44619612f575 347
kenbumono 0:44619612f575 348 #define MAX_HID_DEVICES 8
kenbumono 0:44619612f575 349
kenbumono 0:44619612f575 350 int GetConsoleChar();
kenbumono 0:44619612f575 351 class ShellApp
kenbumono 0:44619612f575 352 {
kenbumono 0:44619612f575 353 char _line[64];
kenbumono 0:44619612f575 354 HIDBluetooth _hids[MAX_HID_DEVICES];
kenbumono 0:44619612f575 355
kenbumono 0:44619612f575 356 public:
kenbumono 0:44619612f575 357 void Ready()
kenbumono 0:44619612f575 358 {
kenbumono 0:44619612f575 359 printf("HIDBluetooth %d\r\n",sizeof(HIDBluetooth));
kenbumono 0:44619612f575 360 memset(_hids,0,sizeof(_hids));
kenbumono 0:44619612f575 361 //Inquiry();
kenbumono 0:44619612f575 362 Scan();
kenbumono 0:44619612f575 363 }
kenbumono 0:44619612f575 364
kenbumono 0:44619612f575 365 // We have connected to a device
kenbumono 0:44619612f575 366 void ConnectionComplete(HCI* hci, connection_info* info)
kenbumono 0:44619612f575 367 {
kenbumono 0:44619612f575 368 printf("ConnectionComplete ");
kenbumono 0:44619612f575 369 BD_ADDR* a = &info->bdaddr;
kenbumono 0:44619612f575 370 printf(a);
kenbumono 0:44619612f575 371 BTDevice* bt = hci->Find(a);
kenbumono 0:44619612f575 372 HIDBluetooth* hid = NewHIDBluetooth();
kenbumono 0:44619612f575 373 printf("%08x %08x\r\n",bt,hid);
kenbumono 0:44619612f575 374 if (hid)
kenbumono 0:44619612f575 375 hid->Listen(a,&bt->_info); // use Listen for PS3, Open for WII
kenbumono 0:44619612f575 376 hid->Ps3Hid_Led(0); // set led 1
kenbumono 0:44619612f575 377 hid->Ps3Hid_Rumble(0x20,0xff,0x20,0xff); // rumble
kenbumono 0:44619612f575 378
kenbumono 0:44619612f575 379 }
kenbumono 0:44619612f575 380
kenbumono 0:44619612f575 381 HIDBluetooth* NewHIDBluetooth()
kenbumono 0:44619612f575 382 {
kenbumono 0:44619612f575 383 for (int i = 0; i < MAX_HID_DEVICES; i++)
kenbumono 0:44619612f575 384 if (!_hids[i].InUse())
kenbumono 0:44619612f575 385 return _hids+i;
kenbumono 0:44619612f575 386 return 0;
kenbumono 0:44619612f575 387 }
kenbumono 0:44619612f575 388
kenbumono 0:44619612f575 389 void ConnectDevices()
kenbumono 0:44619612f575 390 {
kenbumono 0:44619612f575 391 printf("ConnectDevices\n");
kenbumono 0:44619612f575 392
kenbumono 0:44619612f575 393 BTDevice* devs[8];
kenbumono 0:44619612f575 394 int count = gHCI->GetDevices(devs,8);
kenbumono 0:44619612f575 395 for (int i = 0; i < count; i++)
kenbumono 0:44619612f575 396 {
kenbumono 0:44619612f575 397 printfBytes("DEVICE CLASS",devs[i]->_info.dev_class,3);
kenbumono 0:44619612f575 398 if (devs[i]->_handle == 0)
kenbumono 0:44619612f575 399 {
kenbumono 0:44619612f575 400 BD_ADDR* bd = &devs[i]->_info.bdaddr;
kenbumono 0:44619612f575 401 printf("Connecting to ");
kenbumono 0:44619612f575 402 printf(bd);
kenbumono 0:44619612f575 403 printf("\r\n");
kenbumono 0:44619612f575 404 gHCI->CreateConnection(bd);
kenbumono 0:44619612f575 405 }
kenbumono 0:44619612f575 406 }
kenbumono 0:44619612f575 407 }
kenbumono 0:44619612f575 408
kenbumono 0:44619612f575 409 const char* ReadLine()
kenbumono 0:44619612f575 410 {
kenbumono 0:44619612f575 411 int i;
kenbumono 0:44619612f575 412 for (i = 0; i < 255; )
kenbumono 0:44619612f575 413 {
kenbumono 0:44619612f575 414 USBLoop();
kenbumono 0:44619612f575 415 int c = GetConsoleChar();
kenbumono 0:44619612f575 416 if (c == -1)
kenbumono 0:44619612f575 417 continue;
kenbumono 0:44619612f575 418 if (c == '\n' || c == 13)
kenbumono 0:44619612f575 419 break;
kenbumono 0:44619612f575 420 _line[i++] = c;
kenbumono 0:44619612f575 421 }
kenbumono 0:44619612f575 422 _line[i] = 0;
kenbumono 0:44619612f575 423 return _line;
kenbumono 0:44619612f575 424 }
kenbumono 0:44619612f575 425
kenbumono 0:44619612f575 426 void Inquiry()
kenbumono 0:44619612f575 427 {
kenbumono 0:44619612f575 428 printf("Inquiry..\r\n");
kenbumono 0:44619612f575 429 gHCI->Inquiry();
kenbumono 0:44619612f575 430 }
kenbumono 0:44619612f575 431
kenbumono 0:44619612f575 432 void List()
kenbumono 0:44619612f575 433 {
kenbumono 0:44619612f575 434 #if 0
kenbumono 0:44619612f575 435 printf("%d devices\r\n",_deviceCount);
kenbumono 0:44619612f575 436 for (int i = 0; i < _deviceCount; i++)
kenbumono 0:44619612f575 437 {
kenbumono 0:44619612f575 438 printf(&_devices[i].info.bdaddr);
kenbumono 0:44619612f575 439 printf("\r\n");
kenbumono 0:44619612f575 440 }
kenbumono 0:44619612f575 441 #endif
kenbumono 0:44619612f575 442 }
kenbumono 0:44619612f575 443
kenbumono 0:44619612f575 444 void Scan()
kenbumono 0:44619612f575 445 {
kenbumono 0:44619612f575 446 printf("Scanning...\r\n");
kenbumono 0:44619612f575 447 gHCI->WriteScanEnable();
kenbumono 0:44619612f575 448 }
kenbumono 0:44619612f575 449
kenbumono 0:44619612f575 450 void Connect()
kenbumono 0:44619612f575 451 {
kenbumono 0:44619612f575 452 ConnectDevices();
kenbumono 0:44619612f575 453 }
kenbumono 0:44619612f575 454
kenbumono 0:44619612f575 455
kenbumono 0:44619612f575 456 void Disconnect()
kenbumono 0:44619612f575 457 {
kenbumono 0:44619612f575 458 gHCI->DisconnectAll();
kenbumono 0:44619612f575 459 }
kenbumono 0:44619612f575 460
kenbumono 0:44619612f575 461 void CloseMouse()
kenbumono 0:44619612f575 462 {
kenbumono 0:44619612f575 463 }
kenbumono 0:44619612f575 464
kenbumono 0:44619612f575 465 void Quit()
kenbumono 0:44619612f575 466 {
kenbumono 0:44619612f575 467 CloseMouse();
kenbumono 0:44619612f575 468 }
kenbumono 0:44619612f575 469
kenbumono 0:44619612f575 470 void Run()
kenbumono 0:44619612f575 471 {
kenbumono 0:44619612f575 472 for(;;)
kenbumono 0:44619612f575 473 {
kenbumono 0:44619612f575 474 const char* cmd = ReadLine();
kenbumono 0:44619612f575 475 if (strcmp(cmd,"scan") == 0 || strcmp(cmd,"inquiry") == 0)
kenbumono 0:44619612f575 476 Inquiry();
kenbumono 0:44619612f575 477 else if (strcmp(cmd,"ls") == 0)
kenbumono 0:44619612f575 478 List();
kenbumono 0:44619612f575 479 else if (strcmp(cmd,"connect") == 0)
kenbumono 0:44619612f575 480 Connect();
kenbumono 0:44619612f575 481 else if (strcmp(cmd,"disconnect") == 0)
kenbumono 0:44619612f575 482 Disconnect();
kenbumono 0:44619612f575 483 else if (strcmp(cmd,"q")== 0)
kenbumono 0:44619612f575 484 {
kenbumono 0:44619612f575 485 Quit();
kenbumono 0:44619612f575 486 break;
kenbumono 0:44619612f575 487 } else {
kenbumono 0:44619612f575 488 printf("eh? %s\r\n",cmd);
kenbumono 0:44619612f575 489 }
kenbumono 0:44619612f575 490 }
kenbumono 0:44619612f575 491 }
kenbumono 0:44619612f575 492 };
kenbumono 0:44619612f575 493
kenbumono 0:44619612f575 494 // Instance
kenbumono 0:44619612f575 495 ShellApp gApp;
kenbumono 0:44619612f575 496
kenbumono 0:44619612f575 497 static int HciCallback(HCI* hci, HCI_CALLBACK_EVENT evt, const u8* data, int len)
kenbumono 0:44619612f575 498 {
kenbumono 0:44619612f575 499 switch (evt)
kenbumono 0:44619612f575 500 {
kenbumono 0:44619612f575 501 case CALLBACK_READY:
kenbumono 0:44619612f575 502 printf("CALLBACK_READY\r\n");
kenbumono 0:44619612f575 503 gApp.Ready();
kenbumono 0:44619612f575 504 break;
kenbumono 0:44619612f575 505
kenbumono 0:44619612f575 506 case CALLBACK_INQUIRY_RESULT:
kenbumono 0:44619612f575 507 printf("CALLBACK_INQUIRY_RESULT ");
kenbumono 0:44619612f575 508 printf((BD_ADDR*)data);
kenbumono 0:44619612f575 509 printf("\r\n");
kenbumono 0:44619612f575 510 break;
kenbumono 0:44619612f575 511
kenbumono 0:44619612f575 512 case CALLBACK_INQUIRY_DONE:
kenbumono 0:44619612f575 513 printf("CALLBACK_INQUIRY_DONE\r\n");
kenbumono 0:44619612f575 514 gApp.ConnectDevices();
kenbumono 0:44619612f575 515 break;
kenbumono 0:44619612f575 516
kenbumono 0:44619612f575 517 case CALLBACK_REMOTE_NAME:
kenbumono 0:44619612f575 518 {
kenbumono 0:44619612f575 519 BD_ADDR* addr = (BD_ADDR*)data;
kenbumono 0:44619612f575 520 const char* name = (const char*)(data + 6);
kenbumono 0:44619612f575 521 printf(addr);
kenbumono 0:44619612f575 522 printf(" % s\r\n",name);
kenbumono 0:44619612f575 523 }
kenbumono 0:44619612f575 524 break;
kenbumono 0:44619612f575 525
kenbumono 0:44619612f575 526 case CALLBACK_CONNECTION_COMPLETE:
kenbumono 0:44619612f575 527 gApp.ConnectionComplete(hci,(connection_info*)data);
kenbumono 0:44619612f575 528 break;
kenbumono 0:44619612f575 529 };
kenbumono 0:44619612f575 530 return 0;
kenbumono 0:44619612f575 531 }
kenbumono 0:44619612f575 532
kenbumono 0:44619612f575 533 // these should be placed in the DMA SRAM
kenbumono 0:44619612f575 534 typedef struct
kenbumono 0:44619612f575 535 {
kenbumono 0:44619612f575 536 u8 _hciBuffer[MAX_HCL_SIZE];
kenbumono 0:44619612f575 537 u8 _aclBuffer[MAX_ACL_SIZE];
kenbumono 0:44619612f575 538 } SRAMPlacement;
kenbumono 0:44619612f575 539
kenbumono 0:44619612f575 540 HCITransportUSB _HCITransportUSB;
kenbumono 0:44619612f575 541 HCI _HCI;
kenbumono 0:44619612f575 542
kenbumono 0:44619612f575 543 u8* USBGetBuffer(u32* len);
kenbumono 0:44619612f575 544 int OnBluetoothInsert(int device)
kenbumono 0:44619612f575 545 {
kenbumono 0:44619612f575 546 printf("Bluetooth inserted of %d\r\n",device);
kenbumono 0:44619612f575 547 u32 sramLen;
kenbumono 0:44619612f575 548 u8* sram = USBGetBuffer(&sramLen);
kenbumono 0:44619612f575 549 sram = (u8*)(((u32)sram + 1023) & ~1023);
kenbumono 0:44619612f575 550 SRAMPlacement* s = (SRAMPlacement*)sram;
kenbumono 0:44619612f575 551 _HCITransportUSB.Open(device,s->_hciBuffer,s->_aclBuffer);
kenbumono 0:44619612f575 552 _HCI.Open(&_HCITransportUSB,HciCallback);
kenbumono 0:44619612f575 553 RegisterSocketHandler(SOCKET_L2CAP,&_HCI);
kenbumono 0:44619612f575 554 gHCI = &_HCI;
kenbumono 0:44619612f575 555 //gApp.Inquiry();
kenbumono 0:44619612f575 556 //gApp.Scan();
kenbumono 0:44619612f575 557 gApp.Connect();
kenbumono 0:44619612f575 558 return 0;
kenbumono 0:44619612f575 559 }
kenbumono 0:44619612f575 560
kenbumono 0:44619612f575 561 void TestShell()
kenbumono 0:44619612f575 562 {
kenbumono 0:44619612f575 563 USBInit();
kenbumono 0:44619612f575 564 gApp.Run();
kenbumono 0:44619612f575 565 }