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