This fork re-enables FRDM boards and adds WebUSB CDC functionality
Fork of USBDevice_STM32F103 by
WebUSBDevice/WebUSBDevice.cpp@67:39396cc073f2, 2016-09-04 (annotated)
- Committer:
- devanlai
- Date:
- Sun Sep 04 03:13:09 2016 +0000
- Revision:
- 67:39396cc073f2
- Child:
- 72:1d8a6665d607
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 "USBEndpoints.h" |
devanlai | 67:39396cc073f2 | 20 | #include "USBDevice.h" |
devanlai | 67:39396cc073f2 | 21 | #include "USBDescriptor.h" |
devanlai | 67:39396cc073f2 | 22 | #include "WebUSB.h" |
devanlai | 67:39396cc073f2 | 23 | #include "WebUSBDevice.h" |
devanlai | 67:39396cc073f2 | 24 | |
devanlai | 67:39396cc073f2 | 25 | WebUSBDevice::WebUSBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release) |
devanlai | 67:39396cc073f2 | 26 | : USBDevice(vendor_id, product_id, product_release) |
devanlai | 67:39396cc073f2 | 27 | { |
devanlai | 67:39396cc073f2 | 28 | |
devanlai | 67:39396cc073f2 | 29 | }; |
devanlai | 67:39396cc073f2 | 30 | |
devanlai | 67:39396cc073f2 | 31 | bool WebUSBDevice::requestGetDescriptor(void) |
devanlai | 67:39396cc073f2 | 32 | { |
devanlai | 67:39396cc073f2 | 33 | bool success = false; |
devanlai | 67:39396cc073f2 | 34 | CONTROL_TRANSFER * transfer = getTransferPtr(); |
devanlai | 67:39396cc073f2 | 35 | switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) |
devanlai | 67:39396cc073f2 | 36 | { |
devanlai | 67:39396cc073f2 | 37 | case BINARY_OBJECT_STORE_DESCRIPTOR: |
devanlai | 67:39396cc073f2 | 38 | if (binaryObjectStoreDesc() != NULL) |
devanlai | 67:39396cc073f2 | 39 | { |
devanlai | 67:39396cc073f2 | 40 | transfer->remaining = (binaryObjectStoreDesc()[2] |
devanlai | 67:39396cc073f2 | 41 | | (binaryObjectStoreDesc()[3] << 8)); |
devanlai | 67:39396cc073f2 | 42 | transfer->ptr = binaryObjectStoreDesc(); |
devanlai | 67:39396cc073f2 | 43 | transfer->direction = DEVICE_TO_HOST; |
devanlai | 67:39396cc073f2 | 44 | success = true; |
devanlai | 67:39396cc073f2 | 45 | } |
devanlai | 67:39396cc073f2 | 46 | break; |
devanlai | 67:39396cc073f2 | 47 | default: |
devanlai | 67:39396cc073f2 | 48 | success = USBDevice::requestGetDescriptor(); |
devanlai | 67:39396cc073f2 | 49 | break; |
devanlai | 67:39396cc073f2 | 50 | } |
devanlai | 67:39396cc073f2 | 51 | |
devanlai | 67:39396cc073f2 | 52 | return success; |
devanlai | 67:39396cc073f2 | 53 | } |
devanlai | 67:39396cc073f2 | 54 | |
devanlai | 67:39396cc073f2 | 55 | bool WebUSBDevice::requestWebUSB(void) |
devanlai | 67:39396cc073f2 | 56 | { |
devanlai | 67:39396cc073f2 | 57 | bool success = false; |
devanlai | 67:39396cc073f2 | 58 | |
devanlai | 67:39396cc073f2 | 59 | CONTROL_TRANSFER * transfer = getTransferPtr(); |
devanlai | 67:39396cc073f2 | 60 | switch (transfer->setup.wIndex) |
devanlai | 67:39396cc073f2 | 61 | { |
devanlai | 67:39396cc073f2 | 62 | case WEBUSB_GET_ALLOWED_ORIGINS: |
devanlai | 67:39396cc073f2 | 63 | if (allowedOriginsDesc()) |
devanlai | 67:39396cc073f2 | 64 | { |
devanlai | 67:39396cc073f2 | 65 | transfer->remaining = (allowedOriginsDesc()[2] |
devanlai | 67:39396cc073f2 | 66 | | (allowedOriginsDesc()[3] << 8)); |
devanlai | 67:39396cc073f2 | 67 | transfer->ptr = allowedOriginsDesc(); |
devanlai | 67:39396cc073f2 | 68 | transfer->direction = DEVICE_TO_HOST; |
devanlai | 67:39396cc073f2 | 69 | success = true; |
devanlai | 67:39396cc073f2 | 70 | } |
devanlai | 67:39396cc073f2 | 71 | break; |
devanlai | 67:39396cc073f2 | 72 | case WEBUSB_GET_URL: |
devanlai | 67:39396cc073f2 | 73 | if (transfer->setup.wValue == URL_OFFSET_LANDING_PAGE) |
devanlai | 67:39396cc073f2 | 74 | { |
devanlai | 67:39396cc073f2 | 75 | transfer->remaining = urlIlandingPage()[0]; |
devanlai | 67:39396cc073f2 | 76 | transfer->ptr = urlIlandingPage(); |
devanlai | 67:39396cc073f2 | 77 | transfer->direction = DEVICE_TO_HOST; |
devanlai | 67:39396cc073f2 | 78 | success = true; |
devanlai | 67:39396cc073f2 | 79 | } |
devanlai | 67:39396cc073f2 | 80 | else if (transfer->setup.wValue == URL_OFFSET_ALLOWED_ORIGIN) |
devanlai | 67:39396cc073f2 | 81 | { |
devanlai | 67:39396cc073f2 | 82 | transfer->remaining = urlIallowedOrigin()[0]; |
devanlai | 67:39396cc073f2 | 83 | transfer->ptr = urlIallowedOrigin(); |
devanlai | 67:39396cc073f2 | 84 | transfer->direction = DEVICE_TO_HOST; |
devanlai | 67:39396cc073f2 | 85 | success = true; |
devanlai | 67:39396cc073f2 | 86 | } |
devanlai | 67:39396cc073f2 | 87 | break; |
devanlai | 67:39396cc073f2 | 88 | default: |
devanlai | 67:39396cc073f2 | 89 | break; |
devanlai | 67:39396cc073f2 | 90 | } |
devanlai | 67:39396cc073f2 | 91 | |
devanlai | 67:39396cc073f2 | 92 | return success; |
devanlai | 67:39396cc073f2 | 93 | } |
devanlai | 67:39396cc073f2 | 94 | |
devanlai | 67:39396cc073f2 | 95 | bool WebUSBDevice::USBCallback_request() |
devanlai | 67:39396cc073f2 | 96 | { |
devanlai | 67:39396cc073f2 | 97 | bool success = false; |
devanlai | 67:39396cc073f2 | 98 | /* Process WebUSB requests */ |
devanlai | 67:39396cc073f2 | 99 | CONTROL_TRANSFER * transfer = getTransferPtr(); |
devanlai | 67:39396cc073f2 | 100 | if ((transfer->setup.bmRequestType.Type == VENDOR_TYPE) && |
devanlai | 67:39396cc073f2 | 101 | (transfer->setup.bRequest == WEBUSB_VENDOR_CODE)) |
devanlai | 67:39396cc073f2 | 102 | { |
devanlai | 67:39396cc073f2 | 103 | success = requestWebUSB(); |
devanlai | 67:39396cc073f2 | 104 | } |
devanlai | 67:39396cc073f2 | 105 | |
devanlai | 67:39396cc073f2 | 106 | return success; |
devanlai | 67:39396cc073f2 | 107 | } |
devanlai | 67:39396cc073f2 | 108 | |
devanlai | 67:39396cc073f2 | 109 | uint8_t * WebUSBDevice::deviceDesc() { |
devanlai | 67:39396cc073f2 | 110 | static uint8_t deviceDescriptor[] = { |
devanlai | 67:39396cc073f2 | 111 | DEVICE_DESCRIPTOR_LENGTH, /* bLength */ |
devanlai | 67:39396cc073f2 | 112 | DEVICE_DESCRIPTOR, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 113 | LSB(USB_VERSION_2_1), /* bcdUSB (LSB) */ |
devanlai | 67:39396cc073f2 | 114 | MSB(USB_VERSION_2_1), /* bcdUSB (MSB) */ |
devanlai | 67:39396cc073f2 | 115 | 0x00, /* bDeviceClass */ |
devanlai | 67:39396cc073f2 | 116 | 0x00, /* bDeviceSubClass */ |
devanlai | 67:39396cc073f2 | 117 | 0x00, /* bDeviceprotocol */ |
devanlai | 67:39396cc073f2 | 118 | MAX_PACKET_SIZE_EP0, /* bMaxPacketSize0 */ |
devanlai | 67:39396cc073f2 | 119 | (uint8_t)(LSB(VENDOR_ID)), /* idVendor (LSB) */ |
devanlai | 67:39396cc073f2 | 120 | (uint8_t)(MSB(VENDOR_ID)), /* idVendor (MSB) */ |
devanlai | 67:39396cc073f2 | 121 | (uint8_t)(LSB(PRODUCT_ID)), /* idProduct (LSB) */ |
devanlai | 67:39396cc073f2 | 122 | (uint8_t)(MSB(PRODUCT_ID)), /* idProduct (MSB) */ |
devanlai | 67:39396cc073f2 | 123 | (uint8_t)(LSB(PRODUCT_RELEASE)), /* bcdDevice (LSB) */ |
devanlai | 67:39396cc073f2 | 124 | (uint8_t)(MSB(PRODUCT_RELEASE)), /* bcdDevice (MSB) */ |
devanlai | 67:39396cc073f2 | 125 | STRING_OFFSET_IMANUFACTURER, /* iManufacturer */ |
devanlai | 67:39396cc073f2 | 126 | STRING_OFFSET_IPRODUCT, /* iProduct */ |
devanlai | 67:39396cc073f2 | 127 | STRING_OFFSET_ISERIAL, /* iSerialNumber */ |
devanlai | 67:39396cc073f2 | 128 | 0x01 /* bNumConfigurations */ |
devanlai | 67:39396cc073f2 | 129 | }; |
devanlai | 67:39396cc073f2 | 130 | return deviceDescriptor; |
devanlai | 67:39396cc073f2 | 131 | } |
devanlai | 67:39396cc073f2 | 132 | |
devanlai | 67:39396cc073f2 | 133 | #define WEBUSB_BOS_TOTAL_LENGTH (BINARY_OBJECT_STORE_DESCRIPTOR_LENGTH \ |
devanlai | 67:39396cc073f2 | 134 | + WEBUSB_PLATFORM_DESCRIPTOR_LENGTH) |
devanlai | 67:39396cc073f2 | 135 | |
devanlai | 67:39396cc073f2 | 136 | uint8_t * WebUSBDevice::binaryObjectStoreDesc() { |
devanlai | 67:39396cc073f2 | 137 | static uint8_t binaryObjectStoreDescriptor[] = { |
devanlai | 67:39396cc073f2 | 138 | BINARY_OBJECT_STORE_DESCRIPTOR_LENGTH, /* bLength */ |
devanlai | 67:39396cc073f2 | 139 | BINARY_OBJECT_STORE_DESCRIPTOR, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 140 | LSB(WEBUSB_BOS_TOTAL_LENGTH), /* wTotalLength (LSB) */ |
devanlai | 67:39396cc073f2 | 141 | MSB(WEBUSB_BOS_TOTAL_LENGTH), /* wTotalLength (MSB) */ |
devanlai | 67:39396cc073f2 | 142 | 0x01, /* bNumDeviceCaps */ |
devanlai | 67:39396cc073f2 | 143 | WEBUSB_PLATFORM_DESCRIPTOR_LENGTH, /* bLength */ |
devanlai | 67:39396cc073f2 | 144 | DEVICE_CAPABILITY_DESCRIPTOR, /* bDescriptorType */ |
devanlai | 67:39396cc073f2 | 145 | USB_DC_PLATFORM, /* bDevCapabilityType */ |
devanlai | 67:39396cc073f2 | 146 | 0x00, /* bReserved */ |
devanlai | 67:39396cc073f2 | 147 | 0x38, 0xB6, 0x08, 0x34, /* PlatformCapabilityUUID */ |
devanlai | 67:39396cc073f2 | 148 | 0xA9, 0x09, 0xA0, 0x47, |
devanlai | 67:39396cc073f2 | 149 | 0x8B, 0xFD, 0xA0, 0x76, |
devanlai | 67:39396cc073f2 | 150 | 0x88, 0x15, 0xB6, 0x65, |
devanlai | 67:39396cc073f2 | 151 | LSB(WEBUSB_VERSION_1_0), /* bcdVersion (LSB) */ |
devanlai | 67:39396cc073f2 | 152 | MSB(WEBUSB_VERSION_1_0), /* bcdVersion (MSB) */ |
devanlai | 67:39396cc073f2 | 153 | WEBUSB_VENDOR_CODE, /* bVendorCode */ |
devanlai | 67:39396cc073f2 | 154 | URL_OFFSET_LANDING_PAGE, /* iLandingPage */ |
devanlai | 67:39396cc073f2 | 155 | }; |
devanlai | 67:39396cc073f2 | 156 | return binaryObjectStoreDescriptor; |
devanlai | 67:39396cc073f2 | 157 | } |