USB Device library for the STM32F103 with USB Device Firmware Upgrade (DFU) runtime support.
Dependents: STM32F103C8T6_WebUSBDFU STM32F103C8T6_USBDFU STM32F103C8T6_USBDFU dfu_usb_stm32f103
Fork of USBDevice_STM32F103 by
USBDFU/WebUSBDFU.cpp@68:c190028858f9, 2016-09-04 (annotated)
- Committer:
- devanlai
- Date:
- Sun Sep 04 09:54:47 2016 +0000
- Revision:
- 68:c190028858f9
- Parent:
- 67:39396cc073f2
- Child:
- 70:e410de636542
Add Microsoft OS Descriptors 1.0 Compatible ID support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
devanlai | 67:39396cc073f2 | 1 | /* |
devanlai | 67:39396cc073f2 | 2 | * Copyright 2016 Devan Lai |
devanlai | 67:39396cc073f2 | 3 | * |
devanlai | 67:39396cc073f2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
devanlai | 67:39396cc073f2 | 5 | * you may not use this file except in compliance with the License. |
devanlai | 67:39396cc073f2 | 6 | * You may obtain a copy of the License at |
devanlai | 67:39396cc073f2 | 7 | * |
devanlai | 67:39396cc073f2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
devanlai | 67:39396cc073f2 | 9 | * |
devanlai | 67:39396cc073f2 | 10 | * Unless required by applicable law or agreed to in writing, software |
devanlai | 67:39396cc073f2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
devanlai | 67:39396cc073f2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
devanlai | 67:39396cc073f2 | 13 | * See the License for the specific language governing permissions and |
devanlai | 67:39396cc073f2 | 14 | * limitations under the License. |
devanlai | 67:39396cc073f2 | 15 | */ |
devanlai | 67:39396cc073f2 | 16 | |
devanlai | 67:39396cc073f2 | 17 | #include "stdint.h" |
devanlai | 67:39396cc073f2 | 18 | |
devanlai | 67:39396cc073f2 | 19 | #include "USBHAL.h" |
devanlai | 67:39396cc073f2 | 20 | #include "WebUSBDFU.h" |
devanlai | 67:39396cc073f2 | 21 | #include "WebUSB.h" |
devanlai | 67:39396cc073f2 | 22 | #include "DFU.h" |
devanlai | 68:c190028858f9 | 23 | #include "WinUSB.h" |
devanlai | 67:39396cc073f2 | 24 | |
devanlai | 67:39396cc073f2 | 25 | #include "USBDescriptor.h" |
devanlai | 67:39396cc073f2 | 26 | |
devanlai | 67:39396cc073f2 | 27 | #define DEFAULT_CONFIGURATION (1) |
devanlai | 67:39396cc073f2 | 28 | #define DFU_INTERFACE_NUMBER (0) |
devanlai | 67:39396cc073f2 | 29 | |
devanlai | 67:39396cc073f2 | 30 | WebUSBDFU::WebUSBDFU(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect) |
devanlai | 67:39396cc073f2 | 31 | : WebUSBDevice(vendor_id, product_id, product_release), |
devanlai | 67:39396cc073f2 | 32 | detach(no_op) |
devanlai | 67:39396cc073f2 | 33 | { |
devanlai | 67:39396cc073f2 | 34 | if (connect) { |
devanlai | 67:39396cc073f2 | 35 | WebUSBDevice::connect(); |
devanlai | 67:39396cc073f2 | 36 | } |
devanlai | 67:39396cc073f2 | 37 | } |
devanlai | 67:39396cc073f2 | 38 | |
devanlai | 67:39396cc073f2 | 39 | void WebUSBDFU::attach(Callback<void()> func) { |
devanlai | 67:39396cc073f2 | 40 | if (func != NULL) { |
devanlai | 67:39396cc073f2 | 41 | detach.attach(func); |
devanlai | 67:39396cc073f2 | 42 | } else { |
devanlai | 67:39396cc073f2 | 43 | detach.attach(no_op); |
devanlai | 67:39396cc073f2 | 44 | } |
devanlai | 67:39396cc073f2 | 45 | } |
devanlai | 67:39396cc073f2 | 46 | |
devanlai | 67:39396cc073f2 | 47 | // |
devanlai | 67:39396cc073f2 | 48 | // Route callbacks from lower layers to class(es) |
devanlai | 67:39396cc073f2 | 49 | // |
devanlai | 67:39396cc073f2 | 50 | |
devanlai | 67:39396cc073f2 | 51 | |
devanlai | 67:39396cc073f2 | 52 | // Called in ISR context |
devanlai | 67:39396cc073f2 | 53 | // Called by USBDevice on Endpoint0 request |
devanlai | 67:39396cc073f2 | 54 | // This is used to handle extensions to standard requests |
devanlai | 67:39396cc073f2 | 55 | // and class specific requests |
devanlai | 67:39396cc073f2 | 56 | // Return true if class handles this request |
devanlai | 67:39396cc073f2 | 57 | bool WebUSBDFU::USBCallback_request() { |
devanlai | 67:39396cc073f2 | 58 | bool success = false; |
devanlai | 67:39396cc073f2 | 59 | CONTROL_TRANSFER * transfer = getTransferPtr(); |
devanlai | 68:c190028858f9 | 60 | |
devanlai | 68:c190028858f9 | 61 | // Handle the Microsoft OS Descriptors 1.0 special string descriptor request |
devanlai | 68:c190028858f9 | 62 | if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE) && |
devanlai | 68:c190028858f9 | 63 | (transfer->setup.bRequest == GET_DESCRIPTOR) && |
devanlai | 68:c190028858f9 | 64 | (DESCRIPTOR_TYPE(transfer->setup.wValue) == STRING_DESCRIPTOR) && |
devanlai | 68:c190028858f9 | 65 | (DESCRIPTOR_INDEX(transfer->setup.wValue) == 0xEE)) |
devanlai | 68:c190028858f9 | 66 | { |
devanlai | 68:c190028858f9 | 67 | static uint8_t msftStringDescriptor[] = { |
devanlai | 68:c190028858f9 | 68 | 0x12, /* bLength */ |
devanlai | 68:c190028858f9 | 69 | STRING_DESCRIPTOR, /* bDescriptorType */ |
devanlai | 68:c190028858f9 | 70 | 'M',0,'S',0,'F',0,'T',0,'1',0,'0',0,'0',0, /* qWSignature - MSFT100 */ |
devanlai | 68:c190028858f9 | 71 | WINUSB_VENDOR_CODE, /* bMS_VendorCode */ |
devanlai | 68:c190028858f9 | 72 | 0x00, /* bPad */ |
devanlai | 68:c190028858f9 | 73 | }; |
devanlai | 68:c190028858f9 | 74 | |
devanlai | 68:c190028858f9 | 75 | transfer->remaining = msftStringDescriptor[0]; |
devanlai | 68:c190028858f9 | 76 | transfer->ptr = msftStringDescriptor; |
devanlai | 68:c190028858f9 | 77 | transfer->direction = DEVICE_TO_HOST; |
devanlai | 68:c190028858f9 | 78 | success = true; |
devanlai | 68:c190028858f9 | 79 | } |
devanlai | 68:c190028858f9 | 80 | // Process Microsoft OS Descriptors 1.0 Compatible ID requests |
devanlai | 68:c190028858f9 | 81 | else if ((transfer->setup.bmRequestType.Type == VENDOR_TYPE) && |
devanlai | 68:c190028858f9 | 82 | (transfer->setup.bmRequestType.Recipient == DEVICE_RECIPIENT) && |
devanlai | 68:c190028858f9 | 83 | (transfer->setup.bRequest == WINUSB_VENDOR_CODE) && |
devanlai | 68:c190028858f9 | 84 | (transfer->setup.wIndex == WINUSB_GET_COMPATIBLE_ID_FEATURE_DESCRIPTOR)) |
devanlai | 68:c190028858f9 | 85 | { |
devanlai | 68:c190028858f9 | 86 | static uint8_t msftCompatibleIdDescriptor[] = { |
devanlai | 68:c190028858f9 | 87 | 0x28, 0x00, 0x00, 0x00, /* dwLength */ |
devanlai | 68:c190028858f9 | 88 | LSB(COMPATIBLE_ID_VERSION_1_0), /* bcdVersion (LSB) */ |
devanlai | 68:c190028858f9 | 89 | MSB(COMPATIBLE_ID_VERSION_1_0), /* bcdVersion (MSB) */ |
devanlai | 68:c190028858f9 | 90 | LSB(WINUSB_GET_COMPATIBLE_ID_FEATURE_DESCRIPTOR), /* wIndex (LSB) */ |
devanlai | 68:c190028858f9 | 91 | MSB(WINUSB_GET_COMPATIBLE_ID_FEATURE_DESCRIPTOR), /* wIndex (MSB) */ |
devanlai | 68:c190028858f9 | 92 | 0x01, /* bCount */ |
devanlai | 68:c190028858f9 | 93 | 0, 0, 0, 0, 0, 0, 0, /* reserved */ |
devanlai | 68:c190028858f9 | 94 | DFU_INTERFACE_NUMBER, /* bFirstInterfaceNumber */ |
devanlai | 68:c190028858f9 | 95 | 0x00, /* reserved */ |
devanlai | 68:c190028858f9 | 96 | 'W','I','N','U','S','B',0,0, /* compatible ID - WINUSB */ |
devanlai | 68:c190028858f9 | 97 | 0, 0, 0, 0, 0, 0, 0, 0, /* subCompatibleID */ |
devanlai | 68:c190028858f9 | 98 | 0, 0, 0, 0, 0, 0, /* reserved */ |
devanlai | 68:c190028858f9 | 99 | }; |
devanlai | 68:c190028858f9 | 100 | |
devanlai | 68:c190028858f9 | 101 | transfer->remaining = sizeof(msftCompatibleIdDescriptor); |
devanlai | 68:c190028858f9 | 102 | transfer->ptr = msftCompatibleIdDescriptor; |
devanlai | 68:c190028858f9 | 103 | transfer->direction = DEVICE_TO_HOST; |
devanlai | 68:c190028858f9 | 104 | success = true; |
devanlai | 68:c190028858f9 | 105 | } |
devanlai | 67:39396cc073f2 | 106 | // Process DFU class-specific requests |
devanlai | 68:c190028858f9 | 107 | else if (transfer->setup.bmRequestType.Type == CLASS_TYPE) |
devanlai | 67:39396cc073f2 | 108 | { |
devanlai | 67:39396cc073f2 | 109 | switch (transfer->setup.bRequest) |
devanlai | 67:39396cc073f2 | 110 | { |
devanlai | 67:39396cc073f2 | 111 | case DFU_DETACH: |
devanlai | 67:39396cc073f2 | 112 | detach.call(); |
devanlai | 67:39396cc073f2 | 113 | success = true; |
devanlai | 67:39396cc073f2 | 114 | default: |
devanlai | 67:39396cc073f2 | 115 | break; |
devanlai | 67:39396cc073f2 | 116 | } |
devanlai | 67:39396cc073f2 | 117 | } |
devanlai | 67:39396cc073f2 | 118 | |
devanlai | 67:39396cc073f2 | 119 | // Process WebUSB vendor requests |
devanlai | 67:39396cc073f2 | 120 | if (!success) |
devanlai | 67:39396cc073f2 | 121 | { |
devanlai | 67:39396cc073f2 | 122 | success = WebUSBDevice::USBCallback_request(); |
devanlai | 67:39396cc073f2 | 123 | } |
devanlai | 67:39396cc073f2 | 124 | |
devanlai | 67:39396cc073f2 | 125 | return success; |
devanlai | 67:39396cc073f2 | 126 | } |
devanlai | 67:39396cc073f2 | 127 | |
devanlai | 67:39396cc073f2 | 128 | // Called in ISR context |
devanlai | 67:39396cc073f2 | 129 | // Set configuration. Return false if the |
devanlai | 67:39396cc073f2 | 130 | // configuration is not supported |
devanlai | 67:39396cc073f2 | 131 | bool WebUSBDFU::USBCallback_setConfiguration(uint8_t configuration) { |
devanlai | 67:39396cc073f2 | 132 | if (configuration != DEFAULT_CONFIGURATION) { |
devanlai | 67:39396cc073f2 | 133 | return false; |
devanlai | 67:39396cc073f2 | 134 | } |
devanlai | 67:39396cc073f2 | 135 | |
devanlai | 67:39396cc073f2 | 136 | return true; |
devanlai | 67:39396cc073f2 | 137 | } |
devanlai | 67:39396cc073f2 | 138 | |
devanlai | 67:39396cc073f2 | 139 | |
devanlai | 67:39396cc073f2 | 140 | uint8_t * WebUSBDFU::stringIinterfaceDesc() { |
devanlai | 67:39396cc073f2 | 141 | static uint8_t stringIinterfaceDescriptor[] = { |
devanlai | 67:39396cc073f2 | 142 | 0x08, //bLength |
devanlai | 67:39396cc073f2 | 143 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
devanlai | 67:39396cc073f2 | 144 | 'D',0,'F',0,'U',0, //bString iInterface - DFU |
devanlai | 67:39396cc073f2 | 145 | }; |
devanlai | 67:39396cc073f2 | 146 | return stringIinterfaceDescriptor; |
devanlai | 67:39396cc073f2 | 147 | } |
devanlai | 67:39396cc073f2 | 148 | |
devanlai | 67:39396cc073f2 | 149 | uint8_t * WebUSBDFU::stringIproductDesc() { |
devanlai | 67:39396cc073f2 | 150 | static uint8_t stringIproductDescriptor[] = { |
devanlai | 67:39396cc073f2 | 151 | 0x16, //bLength |
devanlai | 67:39396cc073f2 | 152 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
devanlai | 67:39396cc073f2 | 153 | 'D',0,'F',0,'U',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - DFU device |
devanlai | 67:39396cc073f2 | 154 | }; |
devanlai | 67:39396cc073f2 | 155 | return stringIproductDescriptor; |
devanlai | 67:39396cc073f2 | 156 | } |
devanlai | 67:39396cc073f2 | 157 | |
devanlai | 67:39396cc073f2 | 158 | #define DFU_DESCRIPTOR_LENGTH (9) |
devanlai | 67:39396cc073f2 | 159 | #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ |
devanlai | 67:39396cc073f2 | 160 | + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ |
devanlai | 67:39396cc073f2 | 161 | + (1 * DFU_DESCRIPTOR_LENGTH)) |
devanlai | 67:39396cc073f2 | 162 | |
devanlai | 67:39396cc073f2 | 163 | #define DETACH_TIMEOUT 255 |
devanlai | 67:39396cc073f2 | 164 | #define DFU_TRANSFER_SIZE 1024 |
devanlai | 67:39396cc073f2 | 165 | |
devanlai | 67:39396cc073f2 | 166 | uint8_t * WebUSBDFU::configurationDesc() { |
devanlai | 67:39396cc073f2 | 167 | static uint8_t configurationDescriptor[] = { |
devanlai | 67:39396cc073f2 | 168 | CONFIGURATION_DESCRIPTOR_LENGTH,// bLength |
devanlai | 67:39396cc073f2 | 169 | CONFIGURATION_DESCRIPTOR, // bDescriptorType |
devanlai | 67:39396cc073f2 | 170 | LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) |
devanlai | 67:39396cc073f2 | 171 | MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) |
devanlai | 67:39396cc073f2 | 172 | 0x01, // bNumInterfaces |
devanlai | 67:39396cc073f2 | 173 | DEFAULT_CONFIGURATION, // bConfigurationValue |
devanlai | 67:39396cc073f2 | 174 | STRING_OFFSET_ICONFIGURATION, // iConfiguration |
devanlai | 67:39396cc073f2 | 175 | C_RESERVED | C_SELF_POWERED, // bmAttributes |
devanlai | 67:39396cc073f2 | 176 | C_POWER(0), // bMaxPower |
devanlai | 67:39396cc073f2 | 177 | |
devanlai | 67:39396cc073f2 | 178 | INTERFACE_DESCRIPTOR_LENGTH, // bLength |
devanlai | 67:39396cc073f2 | 179 | INTERFACE_DESCRIPTOR, // bDescriptorType |
devanlai | 67:39396cc073f2 | 180 | DFU_INTERFACE_NUMBER, // bInterfaceNumber |
devanlai | 67:39396cc073f2 | 181 | 0x00, // bAlternateSetting |
devanlai | 67:39396cc073f2 | 182 | 0x00, // bNumEndpoints |
devanlai | 67:39396cc073f2 | 183 | DFU_CLASS_APP_SPECIFIC, // bInterfaceClass |
devanlai | 67:39396cc073f2 | 184 | DFU_SUBCLASS_DFU, // bInterfaceSubClass |
devanlai | 67:39396cc073f2 | 185 | DFU_PROTO_RUNTIME, // bInterfaceProtocol |
devanlai | 67:39396cc073f2 | 186 | STRING_OFFSET_IINTERFACE, // iInterface |
devanlai | 67:39396cc073f2 | 187 | |
devanlai | 67:39396cc073f2 | 188 | DFU_DESCRIPTOR_LENGTH, // bLength |
devanlai | 67:39396cc073f2 | 189 | DFU_DESCRIPTOR, // bDescriptorType |
devanlai | 67:39396cc073f2 | 190 | (DFU_ATTR_WILL_DETACH // bmAttributes |
devanlai | 67:39396cc073f2 | 191 | |DFU_ATTR_CAN_DOWNLOAD), |
devanlai | 67:39396cc073f2 | 192 | LSB(DETACH_TIMEOUT), // wDetachTimeOut (LSB) |
devanlai | 67:39396cc073f2 | 193 | MSB(DETACH_TIMEOUT), // wDetachTimeOut (MSB) |
devanlai | 67:39396cc073f2 | 194 | LSB(DFU_TRANSFER_SIZE), // wTransferSize (LSB) |
devanlai | 67:39396cc073f2 | 195 | MSB(DFU_TRANSFER_SIZE), // wTransferSize (MSB) |
devanlai | 67:39396cc073f2 | 196 | LSB(DFU_VERSION_1_00), // bcdDFUVersion (LSB) |
devanlai | 67:39396cc073f2 | 197 | MSB(DFU_VERSION_1_00), // bcdDFUVersion (MSB) |
devanlai | 67:39396cc073f2 | 198 | }; |
devanlai | 67:39396cc073f2 | 199 | return configurationDescriptor; |
devanlai | 67:39396cc073f2 | 200 | } |
devanlai | 67:39396cc073f2 | 201 | |
devanlai | 67:39396cc073f2 | 202 | #define NUM_ORIGINS 1 |
devanlai | 67:39396cc073f2 | 203 | #define TOTAL_ORIGINS_LENGTH (WEBUSB_DESCRIPTOR_SET_LENGTH + \ |
devanlai | 67:39396cc073f2 | 204 | WEBUSB_CONFIGURATION_SUBSET_LENGTH + \ |
devanlai | 67:39396cc073f2 | 205 | WEBUSB_FUNCTION_SUBSET_LENGTH + \ |
devanlai | 67:39396cc073f2 | 206 | NUM_ORIGINS) |
devanlai | 67:39396cc073f2 | 207 | |
devanlai | 67:39396cc073f2 | 208 | uint8_t * WebUSBDFU::allowedOriginsDesc() { |
devanlai | 67:39396cc073f2 | 209 | static uint8_t allowedOriginsDescriptor[] = { |
devanlai | 67:39396cc073f2 | 210 | WEBUSB_DESCRIPTOR_SET_LENGTH, /* bLength */ |
devanlai | 67:39396cc073f2 | 211 | WEBUSB_DESCRIPTOR_SET_HEADER, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 212 | LSB(TOTAL_ORIGINS_LENGTH), /* wTotalLength (LSB) */ |
devanlai | 67:39396cc073f2 | 213 | MSB(TOTAL_ORIGINS_LENGTH), /* wTotalLength (MSB) */ |
devanlai | 67:39396cc073f2 | 214 | 0x01, /* bNumConfigurations */ |
devanlai | 67:39396cc073f2 | 215 | |
devanlai | 67:39396cc073f2 | 216 | WEBUSB_CONFIGURATION_SUBSET_LENGTH, /* bLength */ |
devanlai | 67:39396cc073f2 | 217 | WEBUSB_CONFIGURATION_SUBSET_HEADER, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 218 | DEFAULT_CONFIGURATION, /* bConfigurationValue */ |
devanlai | 67:39396cc073f2 | 219 | 0x01, /* bNumFunctions */ |
devanlai | 67:39396cc073f2 | 220 | |
devanlai | 67:39396cc073f2 | 221 | (WEBUSB_FUNCTION_SUBSET_LENGTH+NUM_ORIGINS),/* bLength */ |
devanlai | 67:39396cc073f2 | 222 | WEBUSB_FUNCTION_SUBSET_HEADER, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 223 | DFU_INTERFACE_NUMBER, /* bFirstInterfaceNumber */ |
devanlai | 67:39396cc073f2 | 224 | URL_OFFSET_ALLOWED_ORIGIN, /* iOrigin[] */ |
devanlai | 67:39396cc073f2 | 225 | }; |
devanlai | 67:39396cc073f2 | 226 | |
devanlai | 67:39396cc073f2 | 227 | return allowedOriginsDescriptor; |
devanlai | 67:39396cc073f2 | 228 | } |
devanlai | 67:39396cc073f2 | 229 | |
devanlai | 67:39396cc073f2 | 230 | uint8_t * WebUSBDFU::urlIallowedOrigin() { |
devanlai | 67:39396cc073f2 | 231 | static uint8_t urlIallowedOriginDescriptor[] = { |
devanlai | 67:39396cc073f2 | 232 | 0x16, /* bLength */ |
devanlai | 67:39396cc073f2 | 233 | WEBUSB_URL, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 234 | WEBUSB_URL_SCHEME_HTTPS,/* bScheme */ |
devanlai | 67:39396cc073f2 | 235 | 'd','e','v','a','n','l','a','i','.','g','i','t','h','u','b','.','i','o','/',/* URL - devanlai.github.io */ |
devanlai | 67:39396cc073f2 | 236 | }; |
devanlai | 67:39396cc073f2 | 237 | return urlIallowedOriginDescriptor; |
devanlai | 67:39396cc073f2 | 238 | } |
devanlai | 67:39396cc073f2 | 239 | |
devanlai | 67:39396cc073f2 | 240 | uint8_t * WebUSBDFU::urlIlandingPage() { |
devanlai | 67:39396cc073f2 | 241 | static uint8_t urlIlandingPageDescriptor[] = { |
devanlai | 67:39396cc073f2 | 242 | 0x26, /* bLength */ |
devanlai | 67:39396cc073f2 | 243 | WEBUSB_URL, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 244 | WEBUSB_URL_SCHEME_HTTPS,/* bScheme */ |
devanlai | 67:39396cc073f2 | 245 | 'd','e','v','a','n','l','a','i','.','g','i','t','h','u','b','.','i','o','/',/* URL - devanlai.github.io/webdfu/dfu-util/ */ |
devanlai | 67:39396cc073f2 | 246 | 'w','e','b','d','f','u','/','d','f','u','-','u','t','i','l','/' |
devanlai | 67:39396cc073f2 | 247 | }; |
devanlai | 67:39396cc073f2 | 248 | return urlIlandingPageDescriptor; |
devanlai | 67:39396cc073f2 | 249 | } |