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