![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Code to control a Traxster robot using a Wimote and Android app
Fork of BlueUSB by
BlueUSB/TestShell.cpp@1:accdaa84fe8d, 2014-04-28 (annotated)
- Committer:
- aswild
- Date:
- Mon Apr 28 16:06:32 2014 +0000
- Revision:
- 1:accdaa84fe8d
Wiimote controlled Traxster robot using BlueUSB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 1:accdaa84fe8d | 1 | |
aswild | 1:accdaa84fe8d | 2 | /* |
aswild | 1:accdaa84fe8d | 3 | Copyright (c) 2010 Peter Barrett |
aswild | 1:accdaa84fe8d | 4 | |
aswild | 1:accdaa84fe8d | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
aswild | 1:accdaa84fe8d | 6 | of this software and associated documentation files (the "Software"), to deal |
aswild | 1:accdaa84fe8d | 7 | in the Software without restriction, including without limitation the rights |
aswild | 1:accdaa84fe8d | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
aswild | 1:accdaa84fe8d | 9 | copies of the Software, and to permit persons to whom the Software is |
aswild | 1:accdaa84fe8d | 10 | furnished to do so, subject to the following conditions: |
aswild | 1:accdaa84fe8d | 11 | |
aswild | 1:accdaa84fe8d | 12 | The above copyright notice and this permission notice shall be included in |
aswild | 1:accdaa84fe8d | 13 | all copies or substantial portions of the Software. |
aswild | 1:accdaa84fe8d | 14 | |
aswild | 1:accdaa84fe8d | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
aswild | 1:accdaa84fe8d | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
aswild | 1:accdaa84fe8d | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
aswild | 1:accdaa84fe8d | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
aswild | 1:accdaa84fe8d | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
aswild | 1:accdaa84fe8d | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
aswild | 1:accdaa84fe8d | 21 | THE SOFTWARE. |
aswild | 1:accdaa84fe8d | 22 | */ |
aswild | 1:accdaa84fe8d | 23 | |
aswild | 1:accdaa84fe8d | 24 | #include <stdio.h> |
aswild | 1:accdaa84fe8d | 25 | #include <stdlib.h> |
aswild | 1:accdaa84fe8d | 26 | #include <stdio.h> |
aswild | 1:accdaa84fe8d | 27 | #include <string.h> |
aswild | 1:accdaa84fe8d | 28 | |
aswild | 1:accdaa84fe8d | 29 | #include "mbed.h" |
aswild | 1:accdaa84fe8d | 30 | #include "Utils.h" |
aswild | 1:accdaa84fe8d | 31 | #include "USBHost.h" |
aswild | 1:accdaa84fe8d | 32 | #include "hci.h" |
aswild | 1:accdaa84fe8d | 33 | |
aswild | 1:accdaa84fe8d | 34 | |
aswild | 1:accdaa84fe8d | 35 | |
aswild | 1:accdaa84fe8d | 36 | void ProcessHID(const unsigned char*); |
aswild | 1:accdaa84fe8d | 37 | |
aswild | 1:accdaa84fe8d | 38 | void printf(const BD_ADDR* addr) |
aswild | 1:accdaa84fe8d | 39 | { |
aswild | 1:accdaa84fe8d | 40 | const u8* a = addr->addr; |
aswild | 1:accdaa84fe8d | 41 | printf("%02X:%02X:%02X:%02X:%02X:%02X",a[5],a[4],a[3],a[2],a[1],a[0]); |
aswild | 1:accdaa84fe8d | 42 | } |
aswild | 1:accdaa84fe8d | 43 | |
aswild | 1:accdaa84fe8d | 44 | #define MAX_HCL_SIZE 260 |
aswild | 1:accdaa84fe8d | 45 | #define MAX_ACL_SIZE 400 |
aswild | 1:accdaa84fe8d | 46 | |
aswild | 1:accdaa84fe8d | 47 | class HCITransportUSB : public HCITransport |
aswild | 1:accdaa84fe8d | 48 | { |
aswild | 1:accdaa84fe8d | 49 | int _device; |
aswild | 1:accdaa84fe8d | 50 | u8* _hciBuffer; |
aswild | 1:accdaa84fe8d | 51 | u8* _aclBuffer; |
aswild | 1:accdaa84fe8d | 52 | |
aswild | 1:accdaa84fe8d | 53 | public: |
aswild | 1:accdaa84fe8d | 54 | void Open(int device, u8* hciBuffer, u8* aclBuffer) |
aswild | 1:accdaa84fe8d | 55 | { |
aswild | 1:accdaa84fe8d | 56 | _device = device; |
aswild | 1:accdaa84fe8d | 57 | _hciBuffer = hciBuffer; |
aswild | 1:accdaa84fe8d | 58 | _aclBuffer = aclBuffer; |
aswild | 1:accdaa84fe8d | 59 | USBInterruptTransfer(_device,0x81,_hciBuffer,MAX_HCL_SIZE,HciCallback,this); |
aswild | 1:accdaa84fe8d | 60 | USBBulkTransfer(_device,0x82,_aclBuffer,MAX_ACL_SIZE,AclCallback,this); |
aswild | 1:accdaa84fe8d | 61 | } |
aswild | 1:accdaa84fe8d | 62 | |
aswild | 1:accdaa84fe8d | 63 | static void HciCallback(int device, int endpoint, int status, u8* data, int len, void* userData) |
aswild | 1:accdaa84fe8d | 64 | { |
aswild | 1:accdaa84fe8d | 65 | HCI* t = ((HCITransportUSB*)userData)->_target; |
aswild | 1:accdaa84fe8d | 66 | if (t) |
aswild | 1:accdaa84fe8d | 67 | t->HCIRecv(data,len); |
aswild | 1:accdaa84fe8d | 68 | USBInterruptTransfer(device,0x81,data,MAX_HCL_SIZE,HciCallback,userData); |
aswild | 1:accdaa84fe8d | 69 | } |
aswild | 1:accdaa84fe8d | 70 | |
aswild | 1:accdaa84fe8d | 71 | static void AclCallback(int device, int endpoint, int status, u8* data, int len, void* userData) |
aswild | 1:accdaa84fe8d | 72 | { |
aswild | 1:accdaa84fe8d | 73 | HCI* t = ((HCITransportUSB*)userData)->_target; |
aswild | 1:accdaa84fe8d | 74 | if (t) |
aswild | 1:accdaa84fe8d | 75 | t->ACLRecv(data,len); |
aswild | 1:accdaa84fe8d | 76 | USBBulkTransfer(device,0x82,data,MAX_ACL_SIZE,AclCallback,userData); |
aswild | 1:accdaa84fe8d | 77 | } |
aswild | 1:accdaa84fe8d | 78 | |
aswild | 1:accdaa84fe8d | 79 | virtual void HCISend(const u8* data, int len) |
aswild | 1:accdaa84fe8d | 80 | { |
aswild | 1:accdaa84fe8d | 81 | USBControlTransfer(_device,REQUEST_TYPE_CLASS, 0, 0, 0,(u8*)data,len); |
aswild | 1:accdaa84fe8d | 82 | } |
aswild | 1:accdaa84fe8d | 83 | |
aswild | 1:accdaa84fe8d | 84 | virtual void ACLSend(const u8* data, int len) |
aswild | 1:accdaa84fe8d | 85 | { |
aswild | 1:accdaa84fe8d | 86 | USBBulkTransfer(_device,0x02,(u8*)data,len); |
aswild | 1:accdaa84fe8d | 87 | } |
aswild | 1:accdaa84fe8d | 88 | }; |
aswild | 1:accdaa84fe8d | 89 | |
aswild | 1:accdaa84fe8d | 90 | |
aswild | 1:accdaa84fe8d | 91 | #define WII_REMOTE 0x042500 |
aswild | 1:accdaa84fe8d | 92 | |
aswild | 1:accdaa84fe8d | 93 | class HIDBluetooth |
aswild | 1:accdaa84fe8d | 94 | { |
aswild | 1:accdaa84fe8d | 95 | int _control; // Sockets for control (out) and interrupt (in) |
aswild | 1:accdaa84fe8d | 96 | int _interrupt; |
aswild | 1:accdaa84fe8d | 97 | int _devClass; |
aswild | 1:accdaa84fe8d | 98 | BD_ADDR _addr; |
aswild | 1:accdaa84fe8d | 99 | u8 _pad[2]; // Struct align |
aswild | 1:accdaa84fe8d | 100 | |
aswild | 1:accdaa84fe8d | 101 | public: |
aswild | 1:accdaa84fe8d | 102 | HIDBluetooth() : _control(0),_interrupt(0),_devClass(0) {}; |
aswild | 1:accdaa84fe8d | 103 | |
aswild | 1:accdaa84fe8d | 104 | bool InUse() |
aswild | 1:accdaa84fe8d | 105 | { |
aswild | 1:accdaa84fe8d | 106 | return _control != 0; |
aswild | 1:accdaa84fe8d | 107 | } |
aswild | 1:accdaa84fe8d | 108 | |
aswild | 1:accdaa84fe8d | 109 | static void OnHidInterrupt(int socket, SocketState state, const u8* data, int len, void* userData) |
aswild | 1:accdaa84fe8d | 110 | { |
aswild | 1:accdaa84fe8d | 111 | HIDBluetooth* t = (HIDBluetooth*)userData; |
aswild | 1:accdaa84fe8d | 112 | if (data) |
aswild | 1:accdaa84fe8d | 113 | { |
aswild | 1:accdaa84fe8d | 114 | if (t->_devClass == WII_REMOTE && data[1] == 0x30) |
aswild | 1:accdaa84fe8d | 115 | { |
aswild | 1:accdaa84fe8d | 116 | printf("================wii====================\n"); |
aswild | 1:accdaa84fe8d | 117 | t->Led(); |
aswild | 1:accdaa84fe8d | 118 | t->Hid(); // ask for accelerometer |
aswild | 1:accdaa84fe8d | 119 | t->_devClass = 0; |
aswild | 1:accdaa84fe8d | 120 | } |
aswild | 1:accdaa84fe8d | 121 | |
aswild | 1:accdaa84fe8d | 122 | const u8* d = data; |
aswild | 1:accdaa84fe8d | 123 | switch (d[1]) |
aswild | 1:accdaa84fe8d | 124 | { |
aswild | 1:accdaa84fe8d | 125 | case 0x02: |
aswild | 1:accdaa84fe8d | 126 | { |
aswild | 1:accdaa84fe8d | 127 | int x = (signed char)d[3]; |
aswild | 1:accdaa84fe8d | 128 | int y = (signed char)d[4]; |
aswild | 1:accdaa84fe8d | 129 | printf("Mouse %2X dx:%d dy:%d\n",d[2],x,y); |
aswild | 1:accdaa84fe8d | 130 | } |
aswild | 1:accdaa84fe8d | 131 | break; |
aswild | 1:accdaa84fe8d | 132 | |
aswild | 1:accdaa84fe8d | 133 | case 0x37: // Accelerometer http://wiki.wiimoteproject.com/Reports |
aswild | 1:accdaa84fe8d | 134 | { |
aswild | 1:accdaa84fe8d | 135 | int pad = (d[2] & 0x9F) | ((d[3] & 0x9F) << 8); |
aswild | 1:accdaa84fe8d | 136 | int x = (d[2] & 0x60) >> 5 | d[4] << 2; |
aswild | 1:accdaa84fe8d | 137 | int y = (d[3] & 0x20) >> 4 | d[5] << 2; |
aswild | 1:accdaa84fe8d | 138 | int z = (d[3] & 0x40) >> 5 | d[6] << 2; |
aswild | 1:accdaa84fe8d | 139 | printf("WII %04X %d %d %d\n",pad,x,y,z); |
aswild | 1:accdaa84fe8d | 140 | } |
aswild | 1:accdaa84fe8d | 141 | break; |
aswild | 1:accdaa84fe8d | 142 | |
aswild | 1:accdaa84fe8d | 143 | // STUFF ACTUALLY HAPPENS HERE |
aswild | 1:accdaa84fe8d | 144 | case 0x31: // basic button/accel report |
aswild | 1:accdaa84fe8d | 145 | ProcessHID(data); |
aswild | 1:accdaa84fe8d | 146 | break; |
aswild | 1:accdaa84fe8d | 147 | default: |
aswild | 1:accdaa84fe8d | 148 | printHex(data,len); |
aswild | 1:accdaa84fe8d | 149 | } |
aswild | 1:accdaa84fe8d | 150 | } |
aswild | 1:accdaa84fe8d | 151 | } |
aswild | 1:accdaa84fe8d | 152 | |
aswild | 1:accdaa84fe8d | 153 | void Led(int id = 0x10) |
aswild | 1:accdaa84fe8d | 154 | { |
aswild | 1:accdaa84fe8d | 155 | u8 led[3] = {0x52, 0x11, id}; |
aswild | 1:accdaa84fe8d | 156 | if (_control) |
aswild | 1:accdaa84fe8d | 157 | Socket_Send(_control,led,3); |
aswild | 1:accdaa84fe8d | 158 | } |
aswild | 1:accdaa84fe8d | 159 | |
aswild | 1:accdaa84fe8d | 160 | void Hid(int report = 0x31) |
aswild | 1:accdaa84fe8d | 161 | { |
aswild | 1:accdaa84fe8d | 162 | u8 hid[4] = { 0x52, 0x12, 0x00, report }; |
aswild | 1:accdaa84fe8d | 163 | if (_control != -1) |
aswild | 1:accdaa84fe8d | 164 | Socket_Send(_control,hid,4); |
aswild | 1:accdaa84fe8d | 165 | } |
aswild | 1:accdaa84fe8d | 166 | |
aswild | 1:accdaa84fe8d | 167 | static void OnHidControl(int socket, SocketState state, const u8* data, int len, void* userData) |
aswild | 1:accdaa84fe8d | 168 | { |
aswild | 1:accdaa84fe8d | 169 | printf("OnHidControl\n"); |
aswild | 1:accdaa84fe8d | 170 | if (data) |
aswild | 1:accdaa84fe8d | 171 | printHex(data,len); |
aswild | 1:accdaa84fe8d | 172 | } |
aswild | 1:accdaa84fe8d | 173 | |
aswild | 1:accdaa84fe8d | 174 | void Open(BD_ADDR* bdAddr, inquiry_info* info) |
aswild | 1:accdaa84fe8d | 175 | { |
aswild | 1:accdaa84fe8d | 176 | printf("L2CAPAddr size %d\n",sizeof(L2CAPAddr)); |
aswild | 1:accdaa84fe8d | 177 | _addr = *bdAddr; |
aswild | 1:accdaa84fe8d | 178 | L2CAPAddr sockAddr; |
aswild | 1:accdaa84fe8d | 179 | sockAddr.bdaddr = _addr; |
aswild | 1:accdaa84fe8d | 180 | sockAddr.psm = L2CAP_PSM_HID_INTR; |
aswild | 1:accdaa84fe8d | 181 | printf("Socket_Open size %d\n",sizeof(L2CAPAddr)); |
aswild | 1:accdaa84fe8d | 182 | _interrupt = Socket_Open(SOCKET_L2CAP,&sockAddr.hdr,OnHidInterrupt,this); |
aswild | 1:accdaa84fe8d | 183 | sockAddr.psm = L2CAP_PSM_HID_CNTL; |
aswild | 1:accdaa84fe8d | 184 | _control = Socket_Open(SOCKET_L2CAP,&sockAddr.hdr,OnHidControl,this); |
aswild | 1:accdaa84fe8d | 185 | |
aswild | 1:accdaa84fe8d | 186 | printfBytes("OPEN DEVICE CLASS",info->dev_class,3); |
aswild | 1:accdaa84fe8d | 187 | _devClass = (info->dev_class[0] << 16) | (info->dev_class[1] << 8) | info->dev_class[2]; |
aswild | 1:accdaa84fe8d | 188 | } |
aswild | 1:accdaa84fe8d | 189 | |
aswild | 1:accdaa84fe8d | 190 | void Close() |
aswild | 1:accdaa84fe8d | 191 | { |
aswild | 1:accdaa84fe8d | 192 | if (_control) |
aswild | 1:accdaa84fe8d | 193 | Socket_Close(_control); |
aswild | 1:accdaa84fe8d | 194 | if (_interrupt) |
aswild | 1:accdaa84fe8d | 195 | Socket_Close(_interrupt); |
aswild | 1:accdaa84fe8d | 196 | _control = _interrupt = 0; |
aswild | 1:accdaa84fe8d | 197 | } |
aswild | 1:accdaa84fe8d | 198 | }; |
aswild | 1:accdaa84fe8d | 199 | |
aswild | 1:accdaa84fe8d | 200 | |
aswild | 1:accdaa84fe8d | 201 | HCI* gHCI = 0; |
aswild | 1:accdaa84fe8d | 202 | |
aswild | 1:accdaa84fe8d | 203 | #define MAX_HID_DEVICES 8 |
aswild | 1:accdaa84fe8d | 204 | |
aswild | 1:accdaa84fe8d | 205 | int GetConsoleChar(); |
aswild | 1:accdaa84fe8d | 206 | class ShellApp |
aswild | 1:accdaa84fe8d | 207 | { |
aswild | 1:accdaa84fe8d | 208 | char _line[64]; |
aswild | 1:accdaa84fe8d | 209 | HIDBluetooth _hids[MAX_HID_DEVICES]; |
aswild | 1:accdaa84fe8d | 210 | |
aswild | 1:accdaa84fe8d | 211 | public: |
aswild | 1:accdaa84fe8d | 212 | void Ready() |
aswild | 1:accdaa84fe8d | 213 | { |
aswild | 1:accdaa84fe8d | 214 | printf("HIDBluetooth %d\n",sizeof(HIDBluetooth)); |
aswild | 1:accdaa84fe8d | 215 | memset(_hids,0,sizeof(_hids)); |
aswild | 1:accdaa84fe8d | 216 | Inquiry(); |
aswild | 1:accdaa84fe8d | 217 | |
aswild | 1:accdaa84fe8d | 218 | } |
aswild | 1:accdaa84fe8d | 219 | |
aswild | 1:accdaa84fe8d | 220 | // We have connected to a device |
aswild | 1:accdaa84fe8d | 221 | void ConnectionComplete(HCI* hci, connection_info* info) |
aswild | 1:accdaa84fe8d | 222 | { |
aswild | 1:accdaa84fe8d | 223 | printf("ConnectionComplete "); |
aswild | 1:accdaa84fe8d | 224 | BD_ADDR* a = &info->bdaddr; |
aswild | 1:accdaa84fe8d | 225 | printf(a); |
aswild | 1:accdaa84fe8d | 226 | BTDevice* bt = hci->Find(a); |
aswild | 1:accdaa84fe8d | 227 | HIDBluetooth* hid = NewHIDBluetooth(); |
aswild | 1:accdaa84fe8d | 228 | printf("%08x %08x\n",bt,hid); |
aswild | 1:accdaa84fe8d | 229 | if (hid) |
aswild | 1:accdaa84fe8d | 230 | hid->Open(a,&bt->_info); |
aswild | 1:accdaa84fe8d | 231 | } |
aswild | 1:accdaa84fe8d | 232 | |
aswild | 1:accdaa84fe8d | 233 | HIDBluetooth* NewHIDBluetooth() |
aswild | 1:accdaa84fe8d | 234 | { |
aswild | 1:accdaa84fe8d | 235 | for (int i = 0; i < MAX_HID_DEVICES; i++) |
aswild | 1:accdaa84fe8d | 236 | if (!_hids[i].InUse()) |
aswild | 1:accdaa84fe8d | 237 | return _hids+i; |
aswild | 1:accdaa84fe8d | 238 | return 0; |
aswild | 1:accdaa84fe8d | 239 | } |
aswild | 1:accdaa84fe8d | 240 | |
aswild | 1:accdaa84fe8d | 241 | void ConnectDevices() |
aswild | 1:accdaa84fe8d | 242 | { |
aswild | 1:accdaa84fe8d | 243 | BTDevice* devs[8]; |
aswild | 1:accdaa84fe8d | 244 | int count = gHCI->GetDevices(devs,8); |
aswild | 1:accdaa84fe8d | 245 | for (int i = 0; i < count; i++) |
aswild | 1:accdaa84fe8d | 246 | { |
aswild | 1:accdaa84fe8d | 247 | printfBytes("DEVICE CLASS",devs[i]->_info.dev_class,3); |
aswild | 1:accdaa84fe8d | 248 | if (devs[i]->_handle == 0) |
aswild | 1:accdaa84fe8d | 249 | { |
aswild | 1:accdaa84fe8d | 250 | BD_ADDR* bd = &devs[i]->_info.bdaddr; |
aswild | 1:accdaa84fe8d | 251 | if ((bd->addr)[0] == 0xA8) |
aswild | 1:accdaa84fe8d | 252 | { |
aswild | 1:accdaa84fe8d | 253 | printf("Connecting to "); |
aswild | 1:accdaa84fe8d | 254 | printf(bd); |
aswild | 1:accdaa84fe8d | 255 | printf("\n"); |
aswild | 1:accdaa84fe8d | 256 | gHCI->CreateConnection(bd); |
aswild | 1:accdaa84fe8d | 257 | } |
aswild | 1:accdaa84fe8d | 258 | } |
aswild | 1:accdaa84fe8d | 259 | } |
aswild | 1:accdaa84fe8d | 260 | } |
aswild | 1:accdaa84fe8d | 261 | |
aswild | 1:accdaa84fe8d | 262 | const char* ReadLine() |
aswild | 1:accdaa84fe8d | 263 | { |
aswild | 1:accdaa84fe8d | 264 | int i; |
aswild | 1:accdaa84fe8d | 265 | for (i = 0; i < 255; ) |
aswild | 1:accdaa84fe8d | 266 | { |
aswild | 1:accdaa84fe8d | 267 | USBLoop(); |
aswild | 1:accdaa84fe8d | 268 | int c = GetConsoleChar(); |
aswild | 1:accdaa84fe8d | 269 | if (c == -1) |
aswild | 1:accdaa84fe8d | 270 | continue; |
aswild | 1:accdaa84fe8d | 271 | if (c == '\n' || c == 13) |
aswild | 1:accdaa84fe8d | 272 | break; |
aswild | 1:accdaa84fe8d | 273 | _line[i++] = c; |
aswild | 1:accdaa84fe8d | 274 | } |
aswild | 1:accdaa84fe8d | 275 | _line[i] = 0; |
aswild | 1:accdaa84fe8d | 276 | return _line; |
aswild | 1:accdaa84fe8d | 277 | } |
aswild | 1:accdaa84fe8d | 278 | |
aswild | 1:accdaa84fe8d | 279 | void Inquiry() |
aswild | 1:accdaa84fe8d | 280 | { |
aswild | 1:accdaa84fe8d | 281 | printf("Inquiry..\n"); |
aswild | 1:accdaa84fe8d | 282 | gHCI->Inquiry(3); |
aswild | 1:accdaa84fe8d | 283 | } |
aswild | 1:accdaa84fe8d | 284 | |
aswild | 1:accdaa84fe8d | 285 | void List() |
aswild | 1:accdaa84fe8d | 286 | { |
aswild | 1:accdaa84fe8d | 287 | #if 0 |
aswild | 1:accdaa84fe8d | 288 | printf("%d devices\n",_deviceCount); |
aswild | 1:accdaa84fe8d | 289 | for (int i = 0; i < _deviceCount; i++) |
aswild | 1:accdaa84fe8d | 290 | { |
aswild | 1:accdaa84fe8d | 291 | printf(&_devices[i].info.bdaddr); |
aswild | 1:accdaa84fe8d | 292 | printf("\n"); |
aswild | 1:accdaa84fe8d | 293 | } |
aswild | 1:accdaa84fe8d | 294 | #endif |
aswild | 1:accdaa84fe8d | 295 | } |
aswild | 1:accdaa84fe8d | 296 | |
aswild | 1:accdaa84fe8d | 297 | void Connect() |
aswild | 1:accdaa84fe8d | 298 | { |
aswild | 1:accdaa84fe8d | 299 | ConnectDevices(); |
aswild | 1:accdaa84fe8d | 300 | } |
aswild | 1:accdaa84fe8d | 301 | |
aswild | 1:accdaa84fe8d | 302 | void Disconnect() |
aswild | 1:accdaa84fe8d | 303 | { |
aswild | 1:accdaa84fe8d | 304 | gHCI->DisconnectAll(); |
aswild | 1:accdaa84fe8d | 305 | } |
aswild | 1:accdaa84fe8d | 306 | |
aswild | 1:accdaa84fe8d | 307 | void CloseMouse() |
aswild | 1:accdaa84fe8d | 308 | { |
aswild | 1:accdaa84fe8d | 309 | } |
aswild | 1:accdaa84fe8d | 310 | |
aswild | 1:accdaa84fe8d | 311 | void Quit() |
aswild | 1:accdaa84fe8d | 312 | { |
aswild | 1:accdaa84fe8d | 313 | CloseMouse(); |
aswild | 1:accdaa84fe8d | 314 | } |
aswild | 1:accdaa84fe8d | 315 | |
aswild | 1:accdaa84fe8d | 316 | void Run() |
aswild | 1:accdaa84fe8d | 317 | { |
aswild | 1:accdaa84fe8d | 318 | for (;;) |
aswild | 1:accdaa84fe8d | 319 | USBLoop(); |
aswild | 1:accdaa84fe8d | 320 | } |
aswild | 1:accdaa84fe8d | 321 | }; |
aswild | 1:accdaa84fe8d | 322 | |
aswild | 1:accdaa84fe8d | 323 | // Instance |
aswild | 1:accdaa84fe8d | 324 | ShellApp gApp; |
aswild | 1:accdaa84fe8d | 325 | |
aswild | 1:accdaa84fe8d | 326 | static int HciCallback(HCI* hci, HCI_CALLBACK_EVENT evt, const u8* data, int len) |
aswild | 1:accdaa84fe8d | 327 | { |
aswild | 1:accdaa84fe8d | 328 | switch (evt) |
aswild | 1:accdaa84fe8d | 329 | { |
aswild | 1:accdaa84fe8d | 330 | case CALLBACK_READY: |
aswild | 1:accdaa84fe8d | 331 | printf("CALLBACK_READY\n"); |
aswild | 1:accdaa84fe8d | 332 | gApp.Ready(); |
aswild | 1:accdaa84fe8d | 333 | break; |
aswild | 1:accdaa84fe8d | 334 | |
aswild | 1:accdaa84fe8d | 335 | case CALLBACK_INQUIRY_RESULT: |
aswild | 1:accdaa84fe8d | 336 | printf("CALLBACK_INQUIRY_RESULT "); |
aswild | 1:accdaa84fe8d | 337 | printf((BD_ADDR*)data); |
aswild | 1:accdaa84fe8d | 338 | printf("\n"); |
aswild | 1:accdaa84fe8d | 339 | break; |
aswild | 1:accdaa84fe8d | 340 | |
aswild | 1:accdaa84fe8d | 341 | case CALLBACK_INQUIRY_DONE: |
aswild | 1:accdaa84fe8d | 342 | printf("CALLBACK_INQUIRY_DONE\n"); |
aswild | 1:accdaa84fe8d | 343 | gApp.ConnectDevices(); |
aswild | 1:accdaa84fe8d | 344 | break; |
aswild | 1:accdaa84fe8d | 345 | |
aswild | 1:accdaa84fe8d | 346 | case CALLBACK_REMOTE_NAME: |
aswild | 1:accdaa84fe8d | 347 | { |
aswild | 1:accdaa84fe8d | 348 | BD_ADDR* addr = (BD_ADDR*)data; |
aswild | 1:accdaa84fe8d | 349 | const char* name = (const char*)(data + 6); |
aswild | 1:accdaa84fe8d | 350 | printf(addr); |
aswild | 1:accdaa84fe8d | 351 | printf(" % s\n",name); |
aswild | 1:accdaa84fe8d | 352 | } |
aswild | 1:accdaa84fe8d | 353 | break; |
aswild | 1:accdaa84fe8d | 354 | |
aswild | 1:accdaa84fe8d | 355 | case CALLBACK_CONNECTION_COMPLETE: |
aswild | 1:accdaa84fe8d | 356 | gApp.ConnectionComplete(hci,(connection_info*)data); |
aswild | 1:accdaa84fe8d | 357 | break; |
aswild | 1:accdaa84fe8d | 358 | }; |
aswild | 1:accdaa84fe8d | 359 | return 0; |
aswild | 1:accdaa84fe8d | 360 | } |
aswild | 1:accdaa84fe8d | 361 | |
aswild | 1:accdaa84fe8d | 362 | // these should be placed in the DMA SRAM |
aswild | 1:accdaa84fe8d | 363 | typedef struct |
aswild | 1:accdaa84fe8d | 364 | { |
aswild | 1:accdaa84fe8d | 365 | u8 _hciBuffer[MAX_HCL_SIZE]; |
aswild | 1:accdaa84fe8d | 366 | u8 _aclBuffer[MAX_ACL_SIZE]; |
aswild | 1:accdaa84fe8d | 367 | } SRAMPlacement; |
aswild | 1:accdaa84fe8d | 368 | |
aswild | 1:accdaa84fe8d | 369 | HCITransportUSB _HCITransportUSB; |
aswild | 1:accdaa84fe8d | 370 | HCI _HCI; |
aswild | 1:accdaa84fe8d | 371 | |
aswild | 1:accdaa84fe8d | 372 | u8* USBGetBuffer(u32* len); |
aswild | 1:accdaa84fe8d | 373 | int OnBluetoothInsert(int device) |
aswild | 1:accdaa84fe8d | 374 | { |
aswild | 1:accdaa84fe8d | 375 | printf("Bluetooth inserted of %d\n",device); |
aswild | 1:accdaa84fe8d | 376 | u32 sramLen; |
aswild | 1:accdaa84fe8d | 377 | u8* sram = USBGetBuffer(&sramLen); |
aswild | 1:accdaa84fe8d | 378 | sram = (u8*)(((u32)sram + 1023) & ~1023); |
aswild | 1:accdaa84fe8d | 379 | SRAMPlacement* s = (SRAMPlacement*)sram; |
aswild | 1:accdaa84fe8d | 380 | _HCITransportUSB.Open(device,s->_hciBuffer,s->_aclBuffer); |
aswild | 1:accdaa84fe8d | 381 | _HCI.Open(&_HCITransportUSB,HciCallback); |
aswild | 1:accdaa84fe8d | 382 | RegisterSocketHandler(SOCKET_L2CAP,&_HCI); |
aswild | 1:accdaa84fe8d | 383 | gHCI = &_HCI; |
aswild | 1:accdaa84fe8d | 384 | gApp.Inquiry(); |
aswild | 1:accdaa84fe8d | 385 | return 0; |
aswild | 1:accdaa84fe8d | 386 | } |
aswild | 1:accdaa84fe8d | 387 | |
aswild | 1:accdaa84fe8d | 388 | void RunWiimote() |
aswild | 1:accdaa84fe8d | 389 | { |
aswild | 1:accdaa84fe8d | 390 | USBInit(); |
aswild | 1:accdaa84fe8d | 391 | gApp.Run(); |
aswild | 1:accdaa84fe8d | 392 | } |