桃井くん機体 高田くん仕様キー

Dependencies:   mbed

Fork of PS3_BlueUSB_user by ROBOSTEP_SHARE

Committer:
hirokimineshita
Date:
Thu Apr 30 05:59:05 2015 +0000
Revision:
0:736c76a75def
Child:
8:6d0de6154e8f
2015/04/30 PM 2 upload

Who changed what in which revision?

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