ROM Comm / USBDevice_STM32F103

Fork of USBDevice_STM32F103 by Devan Lai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WebUSBDevice.cpp Source File

WebUSBDevice.cpp

00001 /*
00002 * Copyright 2016 Devan Lai
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License");
00005 * you may not use this file except in compliance with the License.
00006 * You may obtain a copy of the License at
00007 *
00008 *    http://www.apache.org/licenses/LICENSE-2.0
00009 *
00010 * Unless required by applicable law or agreed to in writing, software
00011 * distributed under the License is distributed on an "AS IS" BASIS,
00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 * See the License for the specific language governing permissions and
00014 * limitations under the License.
00015 */
00016 
00017 #include "stdint.h"
00018 
00019 #include "USBEndpoints.h"
00020 #include "USBDevice.h"
00021 #include "USBDescriptor.h"
00022 #include "WebUSB.h"
00023 #include "WebUSBDevice.h"
00024 
00025 WebUSBDevice::WebUSBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
00026     : USBDevice(vendor_id, product_id, product_release)
00027 {
00028 
00029 };
00030 
00031 bool WebUSBDevice::requestGetDescriptor(void)
00032 {
00033     bool success = false;
00034     CONTROL_TRANSFER * transfer = getTransferPtr();
00035     switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
00036     {
00037         case BINARY_OBJECT_STORE_DESCRIPTOR:
00038             if (binaryObjectStoreDesc() != NULL)
00039             {
00040                 transfer->remaining = (binaryObjectStoreDesc()[2]
00041                                     | (binaryObjectStoreDesc()[3] << 8));
00042                 transfer->ptr = binaryObjectStoreDesc();
00043                 transfer->direction = DEVICE_TO_HOST;
00044                 success = true;
00045             }
00046             break;
00047         default:
00048             success = USBDevice::requestGetDescriptor();
00049             break;
00050     }
00051 
00052     return success;
00053 }
00054 
00055 bool WebUSBDevice::requestWebUSB(void)
00056 {
00057     bool success = false;
00058 
00059     CONTROL_TRANSFER * transfer = getTransferPtr();
00060     switch (transfer->setup.wIndex)
00061     {
00062         case WEBUSB_GET_ALLOWED_ORIGINS:
00063             if (allowedOriginsDesc())
00064             {
00065                 transfer->remaining = (allowedOriginsDesc()[2]
00066                                    |  (allowedOriginsDesc()[3] << 8));
00067                 transfer->ptr = allowedOriginsDesc();
00068                 transfer->direction = DEVICE_TO_HOST;
00069                 success = true;
00070             }
00071             break;
00072         case WEBUSB_GET_URL:
00073             if (transfer->setup.wValue == URL_OFFSET_LANDING_PAGE)
00074             {
00075                 transfer->remaining = urlIlandingPage()[0];
00076                 transfer->ptr = urlIlandingPage();
00077                 transfer->direction = DEVICE_TO_HOST;
00078                 success = true;
00079             }
00080             else if (transfer->setup.wValue == URL_OFFSET_ALLOWED_ORIGIN)
00081             {
00082                 transfer->remaining = urlIallowedOrigin()[0];
00083                 transfer->ptr = urlIallowedOrigin();
00084                 transfer->direction = DEVICE_TO_HOST;
00085                 success = true;
00086             }
00087             break;
00088         default:
00089             break;
00090     }
00091 
00092     return success;
00093 }
00094 
00095 bool WebUSBDevice::USBCallback_request()
00096 {
00097     bool success = false;
00098     /* Process WebUSB requests */
00099     CONTROL_TRANSFER * transfer = getTransferPtr();
00100     if ((transfer->setup.bmRequestType.Type == VENDOR_TYPE) &&
00101         (transfer->setup.bRequest == WEBUSB_VENDOR_CODE))
00102     {
00103         success = requestWebUSB();
00104     }
00105     
00106     return success;
00107 }
00108 
00109 uint8_t * WebUSBDevice::deviceDesc() {
00110     static uint8_t deviceDescriptor[] = {
00111         DEVICE_DESCRIPTOR_LENGTH,       /* bLength */
00112         DEVICE_DESCRIPTOR,              /* bDescriptorType */
00113         LSB(USB_VERSION_2_1),           /* bcdUSB (LSB) */
00114         MSB(USB_VERSION_2_1),           /* bcdUSB (MSB) */
00115         0x00,                           /* bDeviceClass */
00116         0x00,                           /* bDeviceSubClass */
00117         0x00,                           /* bDeviceprotocol */
00118         MAX_PACKET_SIZE_EP0,            /* bMaxPacketSize0 */
00119         (uint8_t)(LSB(VENDOR_ID)),                 /* idVendor (LSB) */
00120         (uint8_t)(MSB(VENDOR_ID)),                 /* idVendor (MSB) */
00121         (uint8_t)(LSB(PRODUCT_ID)),                /* idProduct (LSB) */
00122         (uint8_t)(MSB(PRODUCT_ID)),                /* idProduct (MSB) */
00123         (uint8_t)(LSB(PRODUCT_RELEASE)),           /* bcdDevice (LSB) */
00124         (uint8_t)(MSB(PRODUCT_RELEASE)),           /* bcdDevice (MSB) */
00125         STRING_OFFSET_IMANUFACTURER,    /* iManufacturer */
00126         STRING_OFFSET_IPRODUCT,         /* iProduct */
00127         STRING_OFFSET_ISERIAL,          /* iSerialNumber */
00128         0x01                            /* bNumConfigurations */
00129     };
00130     return deviceDescriptor;
00131 }
00132 
00133 #define WEBUSB_BOS_TOTAL_LENGTH (BINARY_OBJECT_STORE_DESCRIPTOR_LENGTH \
00134                                  + WEBUSB_PLATFORM_DESCRIPTOR_LENGTH)
00135 
00136 uint8_t * WebUSBDevice::binaryObjectStoreDesc() {
00137     static uint8_t binaryObjectStoreDescriptor[] = {
00138         BINARY_OBJECT_STORE_DESCRIPTOR_LENGTH, /* bLength */
00139         BINARY_OBJECT_STORE_DESCRIPTOR, /* bDescriptorType */
00140         LSB(WEBUSB_BOS_TOTAL_LENGTH),   /* wTotalLength (LSB) */
00141         MSB(WEBUSB_BOS_TOTAL_LENGTH),   /* wTotalLength (MSB) */
00142         0x01,                           /* bNumDeviceCaps */
00143         WEBUSB_PLATFORM_DESCRIPTOR_LENGTH, /* bLength */
00144         DEVICE_CAPABILITY_DESCRIPTOR,   /* bDescriptorType */
00145         USB_DC_PLATFORM,                /* bDevCapabilityType */
00146         0x00,                           /* bReserved */
00147         0x38, 0xB6, 0x08, 0x34,         /* PlatformCapabilityUUID */
00148         0xA9, 0x09, 0xA0, 0x47,
00149         0x8B, 0xFD, 0xA0, 0x76,
00150         0x88, 0x15, 0xB6, 0x65,
00151         LSB(WEBUSB_VERSION_1_0),        /* bcdVersion (LSB) */
00152         MSB(WEBUSB_VERSION_1_0),        /* bcdVersion (MSB) */
00153         WEBUSB_VENDOR_CODE,             /* bVendorCode */
00154         URL_OFFSET_LANDING_PAGE,        /* iLandingPage */
00155     };
00156     return binaryObjectStoreDescriptor;
00157 }