A compilation of code from different sources to provide support for a Playstation 3 controller via bluetooth on the m3pi.

Dependencies:   TextLCD mbed

Fork of mbed_TANK_PS3 by Yasuhiko YAMAMOTO

Committer:
srsmitherman
Date:
Sun Dec 30 05:16:28 2012 +0000
Revision:
1:ae49669c5e92
Child:
2:895f70862eb9
Bluetooth PS3 interface for m3pi. User must pair bluetooth dongle address to PS3 controller with other program. Works but needs tweaking.

Who changed what in which revision?

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