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