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