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