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