Test Ver
Dependencies: mbed FatFileSystem
BlueUSB/AutoEvents.cpp@0:269589d8d2c2, 2012-11-17 (annotated)
- Committer:
- jksoft
- Date:
- Sat Nov 17 13:22:00 2012 +0000
- Revision:
- 0:269589d8d2c2
Test Program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:269589d8d2c2 | 1 | |
jksoft | 0:269589d8d2c2 | 2 | /* |
jksoft | 0:269589d8d2c2 | 3 | Copyright (c) 2010 Peter Barrett |
jksoft | 0:269589d8d2c2 | 4 | |
jksoft | 0:269589d8d2c2 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
jksoft | 0:269589d8d2c2 | 6 | of this software and associated documentation files (the "Software"), to deal |
jksoft | 0:269589d8d2c2 | 7 | in the Software without restriction, including without limitation the rights |
jksoft | 0:269589d8d2c2 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
jksoft | 0:269589d8d2c2 | 9 | copies of the Software, and to permit persons to whom the Software is |
jksoft | 0:269589d8d2c2 | 10 | furnished to do so, subject to the following conditions: |
jksoft | 0:269589d8d2c2 | 11 | |
jksoft | 0:269589d8d2c2 | 12 | The above copyright notice and this permission notice shall be included in |
jksoft | 0:269589d8d2c2 | 13 | all copies or substantial portions of the Software. |
jksoft | 0:269589d8d2c2 | 14 | |
jksoft | 0:269589d8d2c2 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
jksoft | 0:269589d8d2c2 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
jksoft | 0:269589d8d2c2 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
jksoft | 0:269589d8d2c2 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
jksoft | 0:269589d8d2c2 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jksoft | 0:269589d8d2c2 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
jksoft | 0:269589d8d2c2 | 21 | THE SOFTWARE. |
jksoft | 0:269589d8d2c2 | 22 | */ |
jksoft | 0:269589d8d2c2 | 23 | |
jksoft | 0:269589d8d2c2 | 24 | #include "mbed.h" |
jksoft | 0:269589d8d2c2 | 25 | #include "USBHost.h" |
jksoft | 0:269589d8d2c2 | 26 | #include "Utils.h" |
jksoft | 0:269589d8d2c2 | 27 | |
jksoft | 0:269589d8d2c2 | 28 | #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol) |
jksoft | 0:269589d8d2c2 | 29 | #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1) |
jksoft | 0:269589d8d2c2 | 30 | #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2) |
jksoft | 0:269589d8d2c2 | 31 | |
jksoft | 0:269589d8d2c2 | 32 | u8 auto_mouse[4]; // buttons,dx,dy,scroll |
jksoft | 0:269589d8d2c2 | 33 | u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6 |
jksoft | 0:269589d8d2c2 | 34 | u8 auto_joystick[4]; // x,y,buttons,throttle |
jksoft | 0:269589d8d2c2 | 35 | |
jksoft | 0:269589d8d2c2 | 36 | void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData) |
jksoft | 0:269589d8d2c2 | 37 | { |
jksoft | 0:269589d8d2c2 | 38 | int evt = (int)userData; |
jksoft | 0:269589d8d2c2 | 39 | switch (evt) |
jksoft | 0:269589d8d2c2 | 40 | { |
jksoft | 0:269589d8d2c2 | 41 | case AUTO_KEYBOARD: |
jksoft | 0:269589d8d2c2 | 42 | printf("AUTO_KEYBOARD "); |
jksoft | 0:269589d8d2c2 | 43 | break; |
jksoft | 0:269589d8d2c2 | 44 | case AUTO_MOUSE: |
jksoft | 0:269589d8d2c2 | 45 | printf("AUTO_MOUSE "); |
jksoft | 0:269589d8d2c2 | 46 | break; |
jksoft | 0:269589d8d2c2 | 47 | default: |
jksoft | 0:269589d8d2c2 | 48 | printf("HUH "); |
jksoft | 0:269589d8d2c2 | 49 | } |
jksoft | 0:269589d8d2c2 | 50 | printfBytes("data",data,len); |
jksoft | 0:269589d8d2c2 | 51 | USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData); |
jksoft | 0:269589d8d2c2 | 52 | } |
jksoft | 0:269589d8d2c2 | 53 | |
jksoft | 0:269589d8d2c2 | 54 | // Establish transfers for interrupt events |
jksoft | 0:269589d8d2c2 | 55 | void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed) |
jksoft | 0:269589d8d2c2 | 56 | { |
jksoft | 0:269589d8d2c2 | 57 | if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80)) |
jksoft | 0:269589d8d2c2 | 58 | return; |
jksoft | 0:269589d8d2c2 | 59 | |
jksoft | 0:269589d8d2c2 | 60 | // Make automatic interrupt enpoints for known devices |
jksoft | 0:269589d8d2c2 | 61 | u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol); |
jksoft | 0:269589d8d2c2 | 62 | u8* dst = 0; |
jksoft | 0:269589d8d2c2 | 63 | int len; |
jksoft | 0:269589d8d2c2 | 64 | switch (evt) |
jksoft | 0:269589d8d2c2 | 65 | { |
jksoft | 0:269589d8d2c2 | 66 | case AUTO_MOUSE: |
jksoft | 0:269589d8d2c2 | 67 | dst = auto_mouse; |
jksoft | 0:269589d8d2c2 | 68 | len = sizeof(auto_mouse); |
jksoft | 0:269589d8d2c2 | 69 | break; |
jksoft | 0:269589d8d2c2 | 70 | case AUTO_KEYBOARD: |
jksoft | 0:269589d8d2c2 | 71 | dst = auto_keyboard; |
jksoft | 0:269589d8d2c2 | 72 | len = sizeof(auto_keyboard); |
jksoft | 0:269589d8d2c2 | 73 | break; |
jksoft | 0:269589d8d2c2 | 74 | default: |
jksoft | 0:269589d8d2c2 | 75 | printf("Interrupt endpoint %02X %08X\n",ed->bEndpointAddress,evt); |
jksoft | 0:269589d8d2c2 | 76 | break; |
jksoft | 0:269589d8d2c2 | 77 | } |
jksoft | 0:269589d8d2c2 | 78 | if (dst) |
jksoft | 0:269589d8d2c2 | 79 | { |
jksoft | 0:269589d8d2c2 | 80 | printf("Auto Event for %02X %08X\n",ed->bEndpointAddress,evt); |
jksoft | 0:269589d8d2c2 | 81 | USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt); |
jksoft | 0:269589d8d2c2 | 82 | } |
jksoft | 0:269589d8d2c2 | 83 | } |
jksoft | 0:269589d8d2c2 | 84 | |
jksoft | 0:269589d8d2c2 | 85 | void PrintString(int device, int i) |
jksoft | 0:269589d8d2c2 | 86 | { |
jksoft | 0:269589d8d2c2 | 87 | u8 buffer[256]; |
jksoft | 0:269589d8d2c2 | 88 | int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255); |
jksoft | 0:269589d8d2c2 | 89 | if (le < 0) |
jksoft | 0:269589d8d2c2 | 90 | return; |
jksoft | 0:269589d8d2c2 | 91 | char* dst = (char*)buffer; |
jksoft | 0:269589d8d2c2 | 92 | for (int j = 2; j < le; j += 2) |
jksoft | 0:269589d8d2c2 | 93 | *dst++ = buffer[j]; |
jksoft | 0:269589d8d2c2 | 94 | *dst = 0; |
jksoft | 0:269589d8d2c2 | 95 | printf("%d:%s\n",i,(const char*)buffer); |
jksoft | 0:269589d8d2c2 | 96 | } |
jksoft | 0:269589d8d2c2 | 97 | |
jksoft | 0:269589d8d2c2 | 98 | // Walk descriptors and create endpoints for a given device |
jksoft | 0:269589d8d2c2 | 99 | int StartAutoEvent(int device, int configuration, int interfaceNumber) |
jksoft | 0:269589d8d2c2 | 100 | { |
jksoft | 0:269589d8d2c2 | 101 | u8 buffer[255]; |
jksoft | 0:269589d8d2c2 | 102 | int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); |
jksoft | 0:269589d8d2c2 | 103 | if (err < 0) |
jksoft | 0:269589d8d2c2 | 104 | return err; |
jksoft | 0:269589d8d2c2 | 105 | |
jksoft | 0:269589d8d2c2 | 106 | int len = buffer[2] | (buffer[3] << 8); |
jksoft | 0:269589d8d2c2 | 107 | u8* d = buffer; |
jksoft | 0:269589d8d2c2 | 108 | u8* end = d + len; |
jksoft | 0:269589d8d2c2 | 109 | while (d < end) |
jksoft | 0:269589d8d2c2 | 110 | { |
jksoft | 0:269589d8d2c2 | 111 | if (d[1] == DESCRIPTOR_TYPE_INTERFACE) |
jksoft | 0:269589d8d2c2 | 112 | { |
jksoft | 0:269589d8d2c2 | 113 | InterfaceDescriptor* id = (InterfaceDescriptor*)d; |
jksoft | 0:269589d8d2c2 | 114 | if (id->bInterfaceNumber == interfaceNumber) |
jksoft | 0:269589d8d2c2 | 115 | { |
jksoft | 0:269589d8d2c2 | 116 | d += d[0]; |
jksoft | 0:269589d8d2c2 | 117 | while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) |
jksoft | 0:269589d8d2c2 | 118 | { |
jksoft | 0:269589d8d2c2 | 119 | if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) |
jksoft | 0:269589d8d2c2 | 120 | AddAutoEvent(device,id,(EndpointDescriptor*)d); |
jksoft | 0:269589d8d2c2 | 121 | d += d[0]; |
jksoft | 0:269589d8d2c2 | 122 | } |
jksoft | 0:269589d8d2c2 | 123 | } |
jksoft | 0:269589d8d2c2 | 124 | } |
jksoft | 0:269589d8d2c2 | 125 | d += d[0]; |
jksoft | 0:269589d8d2c2 | 126 | } |
jksoft | 0:269589d8d2c2 | 127 | return 0; |
jksoft | 0:269589d8d2c2 | 128 | } |
jksoft | 0:269589d8d2c2 | 129 | |
jksoft | 0:269589d8d2c2 | 130 | // Implemented in main.cpp |
jksoft | 0:269589d8d2c2 | 131 | int OnDiskInsert(int device); |
jksoft | 0:269589d8d2c2 | 132 | |
jksoft | 0:269589d8d2c2 | 133 | // Implemented in TestShell.cpp |
jksoft | 0:269589d8d2c2 | 134 | int OnBluetoothInsert(int device); |
jksoft | 0:269589d8d2c2 | 135 | |
jksoft | 0:269589d8d2c2 | 136 | void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) |
jksoft | 0:269589d8d2c2 | 137 | { |
jksoft | 0:269589d8d2c2 | 138 | printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); |
jksoft | 0:269589d8d2c2 | 139 | char s[128]; |
jksoft | 0:269589d8d2c2 | 140 | for (int i = 1; i < 3; i++) |
jksoft | 0:269589d8d2c2 | 141 | { |
jksoft | 0:269589d8d2c2 | 142 | if (GetString(device,i,s,sizeof(s)) < 0) |
jksoft | 0:269589d8d2c2 | 143 | break; |
jksoft | 0:269589d8d2c2 | 144 | printf("%d: %s\n",i,s); |
jksoft | 0:269589d8d2c2 | 145 | } |
jksoft | 0:269589d8d2c2 | 146 | |
jksoft | 0:269589d8d2c2 | 147 | switch (interfaceDesc->bInterfaceClass) |
jksoft | 0:269589d8d2c2 | 148 | { |
jksoft | 0:269589d8d2c2 | 149 | case CLASS_MASS_STORAGE: |
jksoft | 0:269589d8d2c2 | 150 | if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50) |
jksoft | 0:269589d8d2c2 | 151 | OnDiskInsert(device); // it's SCSI! |
jksoft | 0:269589d8d2c2 | 152 | break; |
jksoft | 0:269589d8d2c2 | 153 | case CLASS_WIRELESS_CONTROLLER: |
jksoft | 0:269589d8d2c2 | 154 | if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01) |
jksoft | 0:269589d8d2c2 | 155 | OnBluetoothInsert(device); // it's bluetooth! |
jksoft | 0:269589d8d2c2 | 156 | break; |
jksoft | 0:269589d8d2c2 | 157 | default: |
jksoft | 0:269589d8d2c2 | 158 | StartAutoEvent(device,1,0); |
jksoft | 0:269589d8d2c2 | 159 | break; |
jksoft | 0:269589d8d2c2 | 160 | } |
jksoft | 0:269589d8d2c2 | 161 | } |