This fork re-enables FRDM boards and adds WebUSB CDC functionality
Fork of USBDevice_STM32F103 by
USBDFU/WebUSBDFU.cpp@67:39396cc073f2, 2016-09-04 (annotated)
- Committer:
- devanlai
- Date:
- Sun Sep 04 03:13:09 2016 +0000
- Revision:
- 67:39396cc073f2
- Child:
- 68:c190028858f9
- Child:
- 69:8d72761d52fd
Add WebUSBDevice and WebUSBDFU classes
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 | 67:39396cc073f2 | 23 | |
devanlai | 67:39396cc073f2 | 24 | #include "USBDescriptor.h" |
devanlai | 67:39396cc073f2 | 25 | |
devanlai | 67:39396cc073f2 | 26 | #define DEFAULT_CONFIGURATION (1) |
devanlai | 67:39396cc073f2 | 27 | #define DFU_INTERFACE_NUMBER (0) |
devanlai | 67:39396cc073f2 | 28 | |
devanlai | 67:39396cc073f2 | 29 | WebUSBDFU::WebUSBDFU(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect) |
devanlai | 67:39396cc073f2 | 30 | : WebUSBDevice(vendor_id, product_id, product_release), |
devanlai | 67:39396cc073f2 | 31 | detach(no_op) |
devanlai | 67:39396cc073f2 | 32 | { |
devanlai | 67:39396cc073f2 | 33 | if (connect) { |
devanlai | 67:39396cc073f2 | 34 | WebUSBDevice::connect(); |
devanlai | 67:39396cc073f2 | 35 | } |
devanlai | 67:39396cc073f2 | 36 | } |
devanlai | 67:39396cc073f2 | 37 | |
devanlai | 67:39396cc073f2 | 38 | void WebUSBDFU::attach(Callback<void()> func) { |
devanlai | 67:39396cc073f2 | 39 | if (func != NULL) { |
devanlai | 67:39396cc073f2 | 40 | detach.attach(func); |
devanlai | 67:39396cc073f2 | 41 | } else { |
devanlai | 67:39396cc073f2 | 42 | detach.attach(no_op); |
devanlai | 67:39396cc073f2 | 43 | } |
devanlai | 67:39396cc073f2 | 44 | } |
devanlai | 67:39396cc073f2 | 45 | |
devanlai | 67:39396cc073f2 | 46 | // |
devanlai | 67:39396cc073f2 | 47 | // Route callbacks from lower layers to class(es) |
devanlai | 67:39396cc073f2 | 48 | // |
devanlai | 67:39396cc073f2 | 49 | |
devanlai | 67:39396cc073f2 | 50 | |
devanlai | 67:39396cc073f2 | 51 | // Called in ISR context |
devanlai | 67:39396cc073f2 | 52 | // Called by USBDevice on Endpoint0 request |
devanlai | 67:39396cc073f2 | 53 | // This is used to handle extensions to standard requests |
devanlai | 67:39396cc073f2 | 54 | // and class specific requests |
devanlai | 67:39396cc073f2 | 55 | // Return true if class handles this request |
devanlai | 67:39396cc073f2 | 56 | bool WebUSBDFU::USBCallback_request() { |
devanlai | 67:39396cc073f2 | 57 | bool success = false; |
devanlai | 67:39396cc073f2 | 58 | CONTROL_TRANSFER * transfer = getTransferPtr(); |
devanlai | 67:39396cc073f2 | 59 | |
devanlai | 67:39396cc073f2 | 60 | // Process DFU class-specific requests |
devanlai | 67:39396cc073f2 | 61 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) |
devanlai | 67:39396cc073f2 | 62 | { |
devanlai | 67:39396cc073f2 | 63 | switch (transfer->setup.bRequest) |
devanlai | 67:39396cc073f2 | 64 | { |
devanlai | 67:39396cc073f2 | 65 | case DFU_DETACH: |
devanlai | 67:39396cc073f2 | 66 | detach.call(); |
devanlai | 67:39396cc073f2 | 67 | success = true; |
devanlai | 67:39396cc073f2 | 68 | default: |
devanlai | 67:39396cc073f2 | 69 | break; |
devanlai | 67:39396cc073f2 | 70 | } |
devanlai | 67:39396cc073f2 | 71 | } |
devanlai | 67:39396cc073f2 | 72 | |
devanlai | 67:39396cc073f2 | 73 | // Process WebUSB vendor requests |
devanlai | 67:39396cc073f2 | 74 | if (!success) |
devanlai | 67:39396cc073f2 | 75 | { |
devanlai | 67:39396cc073f2 | 76 | success = WebUSBDevice::USBCallback_request(); |
devanlai | 67:39396cc073f2 | 77 | } |
devanlai | 67:39396cc073f2 | 78 | |
devanlai | 67:39396cc073f2 | 79 | return success; |
devanlai | 67:39396cc073f2 | 80 | } |
devanlai | 67:39396cc073f2 | 81 | |
devanlai | 67:39396cc073f2 | 82 | // Called in ISR context |
devanlai | 67:39396cc073f2 | 83 | // Set configuration. Return false if the |
devanlai | 67:39396cc073f2 | 84 | // configuration is not supported |
devanlai | 67:39396cc073f2 | 85 | bool WebUSBDFU::USBCallback_setConfiguration(uint8_t configuration) { |
devanlai | 67:39396cc073f2 | 86 | if (configuration != DEFAULT_CONFIGURATION) { |
devanlai | 67:39396cc073f2 | 87 | return false; |
devanlai | 67:39396cc073f2 | 88 | } |
devanlai | 67:39396cc073f2 | 89 | |
devanlai | 67:39396cc073f2 | 90 | return true; |
devanlai | 67:39396cc073f2 | 91 | } |
devanlai | 67:39396cc073f2 | 92 | |
devanlai | 67:39396cc073f2 | 93 | |
devanlai | 67:39396cc073f2 | 94 | uint8_t * WebUSBDFU::stringIinterfaceDesc() { |
devanlai | 67:39396cc073f2 | 95 | static uint8_t stringIinterfaceDescriptor[] = { |
devanlai | 67:39396cc073f2 | 96 | 0x08, //bLength |
devanlai | 67:39396cc073f2 | 97 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
devanlai | 67:39396cc073f2 | 98 | 'D',0,'F',0,'U',0, //bString iInterface - DFU |
devanlai | 67:39396cc073f2 | 99 | }; |
devanlai | 67:39396cc073f2 | 100 | return stringIinterfaceDescriptor; |
devanlai | 67:39396cc073f2 | 101 | } |
devanlai | 67:39396cc073f2 | 102 | |
devanlai | 67:39396cc073f2 | 103 | uint8_t * WebUSBDFU::stringIproductDesc() { |
devanlai | 67:39396cc073f2 | 104 | static uint8_t stringIproductDescriptor[] = { |
devanlai | 67:39396cc073f2 | 105 | 0x16, //bLength |
devanlai | 67:39396cc073f2 | 106 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
devanlai | 67:39396cc073f2 | 107 | '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 | 108 | }; |
devanlai | 67:39396cc073f2 | 109 | return stringIproductDescriptor; |
devanlai | 67:39396cc073f2 | 110 | } |
devanlai | 67:39396cc073f2 | 111 | |
devanlai | 67:39396cc073f2 | 112 | #define DFU_DESCRIPTOR_LENGTH (9) |
devanlai | 67:39396cc073f2 | 113 | #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ |
devanlai | 67:39396cc073f2 | 114 | + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ |
devanlai | 67:39396cc073f2 | 115 | + (1 * DFU_DESCRIPTOR_LENGTH)) |
devanlai | 67:39396cc073f2 | 116 | |
devanlai | 67:39396cc073f2 | 117 | #define DETACH_TIMEOUT 255 |
devanlai | 67:39396cc073f2 | 118 | #define DFU_TRANSFER_SIZE 1024 |
devanlai | 67:39396cc073f2 | 119 | |
devanlai | 67:39396cc073f2 | 120 | uint8_t * WebUSBDFU::configurationDesc() { |
devanlai | 67:39396cc073f2 | 121 | static uint8_t configurationDescriptor[] = { |
devanlai | 67:39396cc073f2 | 122 | CONFIGURATION_DESCRIPTOR_LENGTH,// bLength |
devanlai | 67:39396cc073f2 | 123 | CONFIGURATION_DESCRIPTOR, // bDescriptorType |
devanlai | 67:39396cc073f2 | 124 | LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) |
devanlai | 67:39396cc073f2 | 125 | MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) |
devanlai | 67:39396cc073f2 | 126 | 0x01, // bNumInterfaces |
devanlai | 67:39396cc073f2 | 127 | DEFAULT_CONFIGURATION, // bConfigurationValue |
devanlai | 67:39396cc073f2 | 128 | STRING_OFFSET_ICONFIGURATION, // iConfiguration |
devanlai | 67:39396cc073f2 | 129 | C_RESERVED | C_SELF_POWERED, // bmAttributes |
devanlai | 67:39396cc073f2 | 130 | C_POWER(0), // bMaxPower |
devanlai | 67:39396cc073f2 | 131 | |
devanlai | 67:39396cc073f2 | 132 | INTERFACE_DESCRIPTOR_LENGTH, // bLength |
devanlai | 67:39396cc073f2 | 133 | INTERFACE_DESCRIPTOR, // bDescriptorType |
devanlai | 67:39396cc073f2 | 134 | DFU_INTERFACE_NUMBER, // bInterfaceNumber |
devanlai | 67:39396cc073f2 | 135 | 0x00, // bAlternateSetting |
devanlai | 67:39396cc073f2 | 136 | 0x00, // bNumEndpoints |
devanlai | 67:39396cc073f2 | 137 | DFU_CLASS_APP_SPECIFIC, // bInterfaceClass |
devanlai | 67:39396cc073f2 | 138 | DFU_SUBCLASS_DFU, // bInterfaceSubClass |
devanlai | 67:39396cc073f2 | 139 | DFU_PROTO_RUNTIME, // bInterfaceProtocol |
devanlai | 67:39396cc073f2 | 140 | STRING_OFFSET_IINTERFACE, // iInterface |
devanlai | 67:39396cc073f2 | 141 | |
devanlai | 67:39396cc073f2 | 142 | DFU_DESCRIPTOR_LENGTH, // bLength |
devanlai | 67:39396cc073f2 | 143 | DFU_DESCRIPTOR, // bDescriptorType |
devanlai | 67:39396cc073f2 | 144 | (DFU_ATTR_WILL_DETACH // bmAttributes |
devanlai | 67:39396cc073f2 | 145 | |DFU_ATTR_CAN_DOWNLOAD), |
devanlai | 67:39396cc073f2 | 146 | LSB(DETACH_TIMEOUT), // wDetachTimeOut (LSB) |
devanlai | 67:39396cc073f2 | 147 | MSB(DETACH_TIMEOUT), // wDetachTimeOut (MSB) |
devanlai | 67:39396cc073f2 | 148 | LSB(DFU_TRANSFER_SIZE), // wTransferSize (LSB) |
devanlai | 67:39396cc073f2 | 149 | MSB(DFU_TRANSFER_SIZE), // wTransferSize (MSB) |
devanlai | 67:39396cc073f2 | 150 | LSB(DFU_VERSION_1_00), // bcdDFUVersion (LSB) |
devanlai | 67:39396cc073f2 | 151 | MSB(DFU_VERSION_1_00), // bcdDFUVersion (MSB) |
devanlai | 67:39396cc073f2 | 152 | }; |
devanlai | 67:39396cc073f2 | 153 | return configurationDescriptor; |
devanlai | 67:39396cc073f2 | 154 | } |
devanlai | 67:39396cc073f2 | 155 | |
devanlai | 67:39396cc073f2 | 156 | #define NUM_ORIGINS 1 |
devanlai | 67:39396cc073f2 | 157 | #define TOTAL_ORIGINS_LENGTH (WEBUSB_DESCRIPTOR_SET_LENGTH + \ |
devanlai | 67:39396cc073f2 | 158 | WEBUSB_CONFIGURATION_SUBSET_LENGTH + \ |
devanlai | 67:39396cc073f2 | 159 | WEBUSB_FUNCTION_SUBSET_LENGTH + \ |
devanlai | 67:39396cc073f2 | 160 | NUM_ORIGINS) |
devanlai | 67:39396cc073f2 | 161 | |
devanlai | 67:39396cc073f2 | 162 | uint8_t * WebUSBDFU::allowedOriginsDesc() { |
devanlai | 67:39396cc073f2 | 163 | static uint8_t allowedOriginsDescriptor[] = { |
devanlai | 67:39396cc073f2 | 164 | WEBUSB_DESCRIPTOR_SET_LENGTH, /* bLength */ |
devanlai | 67:39396cc073f2 | 165 | WEBUSB_DESCRIPTOR_SET_HEADER, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 166 | LSB(TOTAL_ORIGINS_LENGTH), /* wTotalLength (LSB) */ |
devanlai | 67:39396cc073f2 | 167 | MSB(TOTAL_ORIGINS_LENGTH), /* wTotalLength (MSB) */ |
devanlai | 67:39396cc073f2 | 168 | 0x01, /* bNumConfigurations */ |
devanlai | 67:39396cc073f2 | 169 | |
devanlai | 67:39396cc073f2 | 170 | WEBUSB_CONFIGURATION_SUBSET_LENGTH, /* bLength */ |
devanlai | 67:39396cc073f2 | 171 | WEBUSB_CONFIGURATION_SUBSET_HEADER, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 172 | DEFAULT_CONFIGURATION, /* bConfigurationValue */ |
devanlai | 67:39396cc073f2 | 173 | 0x01, /* bNumFunctions */ |
devanlai | 67:39396cc073f2 | 174 | |
devanlai | 67:39396cc073f2 | 175 | (WEBUSB_FUNCTION_SUBSET_LENGTH+NUM_ORIGINS),/* bLength */ |
devanlai | 67:39396cc073f2 | 176 | WEBUSB_FUNCTION_SUBSET_HEADER, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 177 | DFU_INTERFACE_NUMBER, /* bFirstInterfaceNumber */ |
devanlai | 67:39396cc073f2 | 178 | URL_OFFSET_ALLOWED_ORIGIN, /* iOrigin[] */ |
devanlai | 67:39396cc073f2 | 179 | }; |
devanlai | 67:39396cc073f2 | 180 | |
devanlai | 67:39396cc073f2 | 181 | return allowedOriginsDescriptor; |
devanlai | 67:39396cc073f2 | 182 | } |
devanlai | 67:39396cc073f2 | 183 | |
devanlai | 67:39396cc073f2 | 184 | uint8_t * WebUSBDFU::urlIallowedOrigin() { |
devanlai | 67:39396cc073f2 | 185 | static uint8_t urlIallowedOriginDescriptor[] = { |
devanlai | 67:39396cc073f2 | 186 | 0x16, /* bLength */ |
devanlai | 67:39396cc073f2 | 187 | WEBUSB_URL, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 188 | WEBUSB_URL_SCHEME_HTTPS,/* bScheme */ |
devanlai | 67:39396cc073f2 | 189 | 'd','e','v','a','n','l','a','i','.','g','i','t','h','u','b','.','i','o','/',/* URL - devanlai.github.io */ |
devanlai | 67:39396cc073f2 | 190 | }; |
devanlai | 67:39396cc073f2 | 191 | return urlIallowedOriginDescriptor; |
devanlai | 67:39396cc073f2 | 192 | } |
devanlai | 67:39396cc073f2 | 193 | |
devanlai | 67:39396cc073f2 | 194 | uint8_t * WebUSBDFU::urlIlandingPage() { |
devanlai | 67:39396cc073f2 | 195 | static uint8_t urlIlandingPageDescriptor[] = { |
devanlai | 67:39396cc073f2 | 196 | 0x26, /* bLength */ |
devanlai | 67:39396cc073f2 | 197 | WEBUSB_URL, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 198 | WEBUSB_URL_SCHEME_HTTPS,/* bScheme */ |
devanlai | 67:39396cc073f2 | 199 | '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 | 200 | 'w','e','b','d','f','u','/','d','f','u','-','u','t','i','l','/' |
devanlai | 67:39396cc073f2 | 201 | }; |
devanlai | 67:39396cc073f2 | 202 | return urlIlandingPageDescriptor; |
devanlai | 67:39396cc073f2 | 203 | } |