BlackOneとAndroidの連携デモプログラム AndroidAccessoryを改造してBlackOneとAndroidが連携できるようにしました。 サポートしているのは、デモアプリの ”Buttons” B1-SW1, B2-SW2, B3-SW3 ”LED2” RGB-LED のみです。 LCDに表示するイメージをマイクロSDカードに入れてLCDのソケットに挿入しておく必要があります。 イメージは、320X240ドットで”\Image”という名前のフォルダの直下に”10.jpg”という名前で入れてください。

Dependencies:   TextLCD mbed

Committer:
techand
Date:
Fri Dec 23 04:33:33 2011 +0000
Revision:
0:7b556109fd46

        

Who changed what in which revision?

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