test

Dependencies:   mbed KondoServoLibrary Encoder

Committer:
koheim
Date:
Thu Mar 12 07:32:03 2020 +0000
Revision:
21:0f12ee2322e4
Parent:
8:6d0de6154e8f
a;

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