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