Dependencies:   mbed

Committer:
abe00makoto
Date:
Thu May 26 19:39:37 2011 +0000
Revision:
1:237cfff63ef8
Parent:
0:e939856c1939

        

Who changed what in which revision?

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