XOOMの動作状況を聞き処理を変えてみました。 USBケーブルを抜いた際に処理を終了するようにしました。

Dependencies:   mbed

Committer:
abe00makoto
Date:
Fri May 27 18:51:15 2011 +0000
Revision:
3:432e5675d240
Parent:
0:9fb6c423e32c
nexus one support
maybe support add XOOM ,nexus S

Who changed what in which revision?

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