Devan Lai / USBDevice_STM32F103

Dependents:   STM32F103C8T6_WebUSBDFU STM32F103C8T6_USBDFU STM32F103C8T6_USBDFU dfu_usb_stm32f103

Fork of USBDevice_STM32F103 by Zoltan Hudak

Files at this revision

API Documentation at this revision

Comitter:
devanlai
Date:
Sun Sep 04 03:13:09 2016 +0000
Parent:
66:390c4a31db54
Child:
68:c190028858f9
Child:
69:8d72761d52fd
Commit message:
Add WebUSBDevice and WebUSBDFU classes

Changed in this revision

USBDFU/DFU.h Show annotated file Show diff for this revision Revisions of this file
USBDFU/USBDFU.cpp Show annotated file Show diff for this revision Revisions of this file
USBDFU/USBDFU.h Show annotated file Show diff for this revision Revisions of this file
USBDFU/WebUSBDFU.cpp Show annotated file Show diff for this revision Revisions of this file
USBDFU/WebUSBDFU.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBDevice.h Show annotated file Show diff for this revision Revisions of this file
WebUSBDevice/WebUSB.h Show annotated file Show diff for this revision Revisions of this file
WebUSBDevice/WebUSBDevice.cpp Show annotated file Show diff for this revision Revisions of this file
WebUSBDevice/WebUSBDevice.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDFU/DFU.h	Sun Sep 04 03:13:09 2016 +0000
@@ -0,0 +1,59 @@
+/*
+* Copyright 2016 Devan Lai
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#ifndef DFU_H
+#define DFU_H
+
+enum DFURequest {
+    DFU_DETACH,
+    DFU_DNLOAD,
+    DFU_UPLOAD,
+    DFU_GETSTATUS,
+    DFU_CLRSTATUS,
+    DFU_GETSTATE,
+    DFU_ABORT,
+};
+
+enum DFUClass {
+    DFU_CLASS_APP_SPECIFIC = 0xFE,
+};
+
+enum DFUSubClass {
+    DFU_SUBCLASS_DFU = 0x01,
+};
+
+enum DFUProtocol {
+    DFU_PROTO_RUNTIME = 0x01,
+    DFU_PROTO_DFU = 0x02
+};
+
+enum DFUDescriptorType {
+    DFU_DESCRIPTOR = 0x21,
+};
+
+enum DFUAttributes {
+    DFU_ATTR_CAN_DOWNLOAD = 0x01,
+    DFU_ATTR_CAN_UPLOAD = 0x02,
+    DFU_ATTR_MANIFEST_TOLERANT = 0x04,
+    DFU_ATTR_WILL_DETACH = 0x08,
+};
+
+enum DFUVersion {
+    DFU_VERSION_1_00 = 0x0100,
+    DFUSE_VERSION_1_1A = 0x011A,
+};
+
+#endif
\ No newline at end of file
--- a/USBDFU/USBDFU.cpp	Mon Aug 29 00:47:17 2016 +0000
+++ b/USBDFU/USBDFU.cpp	Sun Sep 04 03:13:09 2016 +0000
@@ -19,7 +19,7 @@
 #include "stdint.h"
 #include "USBHAL.h"
 #include "USBDFU.h"
-
+#include "DFU.h"
 
 USBDFU::USBDFU(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect)
     : USBDevice(vendor_id, product_id, product_release),
--- a/USBDFU/USBDFU.h	Mon Aug 29 00:47:17 2016 +0000
+++ b/USBDFU/USBDFU.h	Sun Sep 04 03:13:09 2016 +0000
@@ -104,45 +104,6 @@
     * @returns true if class handles this request
     */
     virtual bool USBCallback_setConfiguration(uint8_t configuration);
-
-    enum DFURequest {
-        DFU_DETACH,
-        DFU_DNLOAD,
-        DFU_UPLOAD,
-        DFU_GETSTATUS,
-        DFU_CLRSTATUS,
-        DFU_GETSTATE,
-        DFU_ABORT,
-    };
-    
-    enum DFUClass {
-        DFU_CLASS_APP_SPECIFIC = 0xFE,
-    };
-    
-    enum DFUSubClass {
-        DFU_SUBCLASS_DFU = 0x01,
-    };
-    
-    enum DFUProtocol {
-        DFU_PROTO_RUNTIME = 0x01,
-        DFU_PROTO_DFU = 0x02
-    };
-    
-    enum DFUDescriptorType {
-        DFU_DESCRIPTOR = 0x21,
-    };
-    
-    enum DFUAttributes {
-        DFU_ATTR_CAN_DOWNLOAD = 0x01,
-        DFU_ATTR_CAN_UPLOAD = 0x02,
-        DFU_ATTR_MANIFEST_TOLERANT = 0x04,
-        DFU_ATTR_WILL_DETACH = 0x08,
-    };
-    
-    enum DFUVersion {
-        DFU_VERSION_1_00 = 0x0100,
-        DFUSE_VERSION_1_1A = 0x011A,
-    };
     
 private:
     Callback<void()> detach;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDFU/WebUSBDFU.cpp	Sun Sep 04 03:13:09 2016 +0000
@@ -0,0 +1,203 @@
+/*
+* Copyright 2016 Devan Lai
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#include "stdint.h"
+
+#include "USBHAL.h"
+#include "WebUSBDFU.h"
+#include "WebUSB.h"
+#include "DFU.h"
+
+#include "USBDescriptor.h"
+
+#define DEFAULT_CONFIGURATION (1)
+#define DFU_INTERFACE_NUMBER  (0)
+
+WebUSBDFU::WebUSBDFU(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect)
+    : WebUSBDevice(vendor_id, product_id, product_release),
+      detach(no_op)
+{
+    if (connect) {
+        WebUSBDevice::connect();
+    }
+}
+
+void WebUSBDFU::attach(Callback<void()> func) {
+    if (func != NULL) {
+        detach.attach(func);
+    } else {
+        detach.attach(no_op);
+    }
+}
+
+//
+//  Route callbacks from lower layers to class(es)
+//
+
+
+// Called in ISR context
+// Called by USBDevice on Endpoint0 request
+// This is used to handle extensions to standard requests
+// and class specific requests
+// Return true if class handles this request
+bool WebUSBDFU::USBCallback_request() {
+    bool success = false;
+    CONTROL_TRANSFER * transfer = getTransferPtr();
+
+    // Process DFU class-specific requests
+    if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
+    {
+        switch (transfer->setup.bRequest)
+        {
+            case DFU_DETACH:
+                 detach.call();
+                 success = true;
+            default:
+                break;
+        }
+    }
+
+    // Process WebUSB vendor requests 
+    if (!success)
+    {
+        success = WebUSBDevice::USBCallback_request();
+    }
+
+    return success;
+}
+
+// Called in ISR context
+// Set configuration. Return false if the
+// configuration is not supported
+bool WebUSBDFU::USBCallback_setConfiguration(uint8_t configuration) {
+    if (configuration != DEFAULT_CONFIGURATION) {
+        return false;
+    }
+
+    return true;
+}
+
+
+uint8_t * WebUSBDFU::stringIinterfaceDesc() {
+    static uint8_t stringIinterfaceDescriptor[] = {
+        0x08,               //bLength
+        STRING_DESCRIPTOR,  //bDescriptorType 0x03
+        'D',0,'F',0,'U',0,  //bString iInterface - DFU
+    };
+    return stringIinterfaceDescriptor;
+}
+
+uint8_t * WebUSBDFU::stringIproductDesc() {
+    static uint8_t stringIproductDescriptor[] = {
+        0x16,                                                       //bLength
+        STRING_DESCRIPTOR,                                          //bDescriptorType 0x03
+        'D',0,'F',0,'U',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - DFU device
+    };
+    return stringIproductDescriptor;
+}
+
+#define DFU_DESCRIPTOR_LENGTH (9)
+#define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
+                               + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
+                               + (1 * DFU_DESCRIPTOR_LENGTH))
+
+#define DETACH_TIMEOUT 255
+#define DFU_TRANSFER_SIZE 1024
+
+uint8_t * WebUSBDFU::configurationDesc() {
+    static uint8_t configurationDescriptor[] = {
+        CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
+        CONFIGURATION_DESCRIPTOR,       // bDescriptorType
+        LSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (LSB)
+        MSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (MSB)
+        0x01,                           // bNumInterfaces
+        DEFAULT_CONFIGURATION,          // bConfigurationValue
+        STRING_OFFSET_ICONFIGURATION,   // iConfiguration
+        C_RESERVED | C_SELF_POWERED,    // bmAttributes
+        C_POWER(0),                     // bMaxPower
+
+        INTERFACE_DESCRIPTOR_LENGTH,    // bLength
+        INTERFACE_DESCRIPTOR,           // bDescriptorType
+        DFU_INTERFACE_NUMBER,           // bInterfaceNumber
+        0x00,                           // bAlternateSetting
+        0x00,                           // bNumEndpoints
+        DFU_CLASS_APP_SPECIFIC,         // bInterfaceClass
+        DFU_SUBCLASS_DFU,               // bInterfaceSubClass
+        DFU_PROTO_RUNTIME,              // bInterfaceProtocol
+        STRING_OFFSET_IINTERFACE,       // iInterface
+
+        DFU_DESCRIPTOR_LENGTH,          // bLength
+        DFU_DESCRIPTOR,                 // bDescriptorType
+        (DFU_ATTR_WILL_DETACH           // bmAttributes
+        |DFU_ATTR_CAN_DOWNLOAD),
+        LSB(DETACH_TIMEOUT),            // wDetachTimeOut (LSB)
+        MSB(DETACH_TIMEOUT),            // wDetachTimeOut (MSB)
+        LSB(DFU_TRANSFER_SIZE),         // wTransferSize (LSB)
+        MSB(DFU_TRANSFER_SIZE),         // wTransferSize (MSB)
+        LSB(DFU_VERSION_1_00),          // bcdDFUVersion (LSB)
+        MSB(DFU_VERSION_1_00),          // bcdDFUVersion (MSB)
+    };
+    return configurationDescriptor;
+}
+
+#define NUM_ORIGINS 1
+#define TOTAL_ORIGINS_LENGTH (WEBUSB_DESCRIPTOR_SET_LENGTH + \
+                              WEBUSB_CONFIGURATION_SUBSET_LENGTH + \
+                              WEBUSB_FUNCTION_SUBSET_LENGTH + \
+                              NUM_ORIGINS)
+
+uint8_t * WebUSBDFU::allowedOriginsDesc() {
+    static uint8_t allowedOriginsDescriptor[] = {
+        WEBUSB_DESCRIPTOR_SET_LENGTH,   /* bLength */
+        WEBUSB_DESCRIPTOR_SET_HEADER,   /* bDescriptorType */
+        LSB(TOTAL_ORIGINS_LENGTH),      /* wTotalLength (LSB) */
+        MSB(TOTAL_ORIGINS_LENGTH),      /* wTotalLength (MSB) */
+        0x01,                           /* bNumConfigurations */
+        
+        WEBUSB_CONFIGURATION_SUBSET_LENGTH, /* bLength */
+        WEBUSB_CONFIGURATION_SUBSET_HEADER, /* bDescriptorType */
+        DEFAULT_CONFIGURATION,          /* bConfigurationValue */
+        0x01,                           /* bNumFunctions */
+        
+        (WEBUSB_FUNCTION_SUBSET_LENGTH+NUM_ORIGINS),/* bLength */
+        WEBUSB_FUNCTION_SUBSET_HEADER,  /* bDescriptorType */
+        DFU_INTERFACE_NUMBER,           /* bFirstInterfaceNumber */
+        URL_OFFSET_ALLOWED_ORIGIN,      /* iOrigin[] */
+    };
+    
+    return allowedOriginsDescriptor;    
+}
+
+uint8_t * WebUSBDFU::urlIallowedOrigin() {
+    static uint8_t urlIallowedOriginDescriptor[] = {
+        0x16,                  /* bLength */
+        WEBUSB_URL,            /* bDescriptorType */
+        WEBUSB_URL_SCHEME_HTTPS,/* bScheme */
+        'd','e','v','a','n','l','a','i','.','g','i','t','h','u','b','.','i','o','/',/* URL - devanlai.github.io */
+    };
+    return urlIallowedOriginDescriptor;
+}
+
+uint8_t * WebUSBDFU::urlIlandingPage() {
+    static uint8_t urlIlandingPageDescriptor[] = {
+        0x26,                  /* bLength */
+        WEBUSB_URL,            /* bDescriptorType */
+        WEBUSB_URL_SCHEME_HTTPS,/* bScheme */
+        'd','e','v','a','n','l','a','i','.','g','i','t','h','u','b','.','i','o','/',/* URL - devanlai.github.io/webdfu/dfu-util/ */
+        'w','e','b','d','f','u','/','d','f','u','-','u','t','i','l','/'
+    };
+    return urlIlandingPageDescriptor;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDFU/WebUSBDFU.h	Sun Sep 04 03:13:09 2016 +0000
@@ -0,0 +1,121 @@
+/*
+* Copyright 2016 Devan Lai
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#ifndef WEB_USB_DFU_H
+#define WEB_USB_DFU_H
+
+#include "WebUSBDevice.h"
+
+class WebUSBDFU : public WebUSBDevice {
+public:
+    /**
+    * Constructor
+    *
+    * @param vendor_id Your vendor_id
+    * @param product_id Your product_id
+    * @param product_release Your product_release
+    * @param connect Connect the device
+    */
+    WebUSBDFU(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0006, uint16_t product_release = 0x0001, bool connect = true);
+
+    /**
+     *  Attach a callback called when a DFU detach request is received 
+     *
+     *  @param tptr pointer to the object to call the member function on
+     *  @param mptr pointer to the member function to be called
+     */
+    template<typename T>
+    void attach(T* tptr, void (T::*mptr)(void)) {
+        if((mptr != NULL) && (tptr != NULL)) {
+            detach.attach(tptr, mptr);
+        }
+    }
+
+    /**
+     * Attach a callback called when a DFU detach request is received
+     *
+     * @param func function pointer
+     */
+    void attach(Callback<void()> func);
+
+protected:
+    /*
+    * Get string product descriptor
+    *
+    * @returns pointer to the string product descriptor
+    */
+    virtual uint8_t * stringIproductDesc();
+
+    /*
+    * Get string interface descriptor
+    *
+    * @returns pointer to the string interface descriptor
+    */
+    virtual uint8_t * stringIinterfaceDesc();
+
+    /*
+    * Get configuration descriptor
+    *
+    * @returns pointer to the configuration descriptor
+    */
+    virtual uint8_t * configurationDesc();
+
+    /*
+    * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
+    * This is used to handle extensions to standard requests
+    * and class specific requests
+    *
+    * @returns true if class handles this request
+    */
+    virtual bool USBCallback_request();
+
+    /*
+    * Called by USBDevice layer. Set configuration of the device.
+    * For instance, you can add all endpoints that you need on this function.
+    *
+    * @param configuration Number of the configuration
+    * @returns true if class handles this request
+    */
+    virtual bool USBCallback_setConfiguration(uint8_t configuration);
+    
+    /*
+    * Get the WebUSB allowed origin descriptor
+    *
+    * @returns pointer to the WebUSB allowed origin descriptor
+    */
+    virtual uint8_t * allowedOriginsDesc();
+    
+    /*
+    * Get WebUSB landing page URL descriptor
+    *
+    * @returns pointer to the landing page URL descriptor
+    */
+    virtual uint8_t * urlIlandingPage();
+    
+    /*
+    * Get WebUSB allowed origin URL descriptor
+    *
+    * @returns pointer to the allowed origin URL descriptor
+    */
+    virtual uint8_t * urlIallowedOrigin();
+    
+private:
+    Callback<void()> detach;
+    
+    static void no_op() {};
+};
+
+#endif
\ No newline at end of file
--- a/USBDevice/USBDevice.h	Mon Aug 29 00:47:17 2016 +0000
+++ b/USBDevice/USBDevice.h	Sun Sep 04 03:13:09 2016 +0000
@@ -236,6 +236,7 @@
     virtual void EP0in(void);
     virtual void connectStateChanged(unsigned int connected);
     virtual void suspendStateChanged(unsigned int suspended);
+    virtual bool requestGetDescriptor(void);
     uint8_t * findDescriptor(uint8_t descriptorType);
     CONTROL_TRANSFER * getTransferPtr(void);
 
@@ -245,7 +246,6 @@
 
 private:
     bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
-    bool requestGetDescriptor(void);
     bool controlOut(void);
     bool controlIn(void);
     bool requestSetAddress(void);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebUSBDevice/WebUSB.h	Sun Sep 04 03:13:09 2016 +0000
@@ -0,0 +1,70 @@
+/*
+* Copyright 2016 Devan Lai
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#ifndef WEB_USB_H
+#define WEB_USB_H
+
+/* USB 2.1 Standard descriptor types */
+#define BINARY_OBJECT_STORE_DESCRIPTOR (15)
+#define DEVICE_CAPABILITY_DESCRIPTOR (16)
+
+/* WebUSB descriptor types */
+#define WEBUSB_DESCRIPTOR_SET_HEADER       0
+#define WEBUSB_CONFIGURATION_SUBSET_HEADER 1
+#define WEBUSB_FUNCTION_SUBSET_HEADER      2
+#define WEBUSB_URL                         3
+
+/* WebUSB URL schemes */
+#define WEBUSB_URL_SCHEME_HTTP 0
+#define WEBUSB_URL_SCHEME_HTTPS 1
+
+/* WebUSB descriptor lengths */
+#define BINARY_OBJECT_STORE_DESCRIPTOR_LENGTH 0x05
+#define WEBUSB_PLATFORM_DESCRIPTOR_LENGTH  0x18
+#define WEBUSB_DESCRIPTOR_SET_LENGTH       5
+#define WEBUSB_CONFIGURATION_SUBSET_LENGTH 4
+#define WEBUSB_FUNCTION_SUBSET_LENGTH      3
+
+/* WebUSB URL offsets */
+#define URL_OFFSET_ALLOWED_ORIGIN          1
+#define URL_OFFSET_LANDING_PAGE            2
+
+/* USB Specification Release Number */
+#define USB_VERSION_2_1 (0x0210)
+#define WEBUSB_VERSION_1_0 (0x0100)
+
+/* bDevCapabilityTypes in device capability descriptors*/
+#define USB_DC_WIRELESS_USB                1
+#define USB_DC_USB_2_0_EXTENSION           2
+#define USB_DC_SUPERSPEED_USB              3
+#define USB_DC_CONTAINER_ID                4
+#define USB_DC_PLATFORM                    5
+#define USB_DC_POWER_DELIVERY_CAPABILITY   6
+#define USB_DC_BATTERY_INFO_CAPABILITY     7
+#define USB_DC_PD_CONSUMER_PORT_CAPABILITY 8
+#define USB_DC_PD_PROVIDER_PORT_CAPABILITY 9
+#define USB_DC_SUPERSPEED_PLUS             10
+#define USB_DC_PRECISION_TIME_MEASUREMENT  11
+#define USB_DC_WIRELESS_USB_EXT            12
+
+/* WebUSB Vendor code */
+#define WEBUSB_VENDOR_CODE                 0x57
+
+/* WebUSB requests */
+#define WEBUSB_GET_ALLOWED_ORIGINS         0x01
+#define WEBUSB_GET_URL                     0x02
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebUSBDevice/WebUSBDevice.cpp	Sun Sep 04 03:13:09 2016 +0000
@@ -0,0 +1,157 @@
+/*
+* Copyright 2016 Devan Lai
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#include "stdint.h"
+
+#include "USBEndpoints.h"
+#include "USBDevice.h"
+#include "USBDescriptor.h"
+#include "WebUSB.h"
+#include "WebUSBDevice.h"
+
+WebUSBDevice::WebUSBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
+    : USBDevice(vendor_id, product_id, product_release)
+{
+
+};
+
+bool WebUSBDevice::requestGetDescriptor(void)
+{
+    bool success = false;
+    CONTROL_TRANSFER * transfer = getTransferPtr();
+    switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
+    {
+        case BINARY_OBJECT_STORE_DESCRIPTOR:
+            if (binaryObjectStoreDesc() != NULL)
+            {
+                transfer->remaining = (binaryObjectStoreDesc()[2]
+                                    | (binaryObjectStoreDesc()[3] << 8));
+                transfer->ptr = binaryObjectStoreDesc();
+                transfer->direction = DEVICE_TO_HOST;
+                success = true;
+            }
+            break;
+        default:
+            success = USBDevice::requestGetDescriptor();
+            break;
+    }
+
+    return success;
+}
+
+bool WebUSBDevice::requestWebUSB(void)
+{
+    bool success = false;
+
+    CONTROL_TRANSFER * transfer = getTransferPtr();
+    switch (transfer->setup.wIndex)
+    {
+        case WEBUSB_GET_ALLOWED_ORIGINS:
+            if (allowedOriginsDesc())
+            {
+                transfer->remaining = (allowedOriginsDesc()[2]
+                                   |  (allowedOriginsDesc()[3] << 8));
+                transfer->ptr = allowedOriginsDesc();
+                transfer->direction = DEVICE_TO_HOST;
+                success = true;
+            }
+            break;
+        case WEBUSB_GET_URL:
+            if (transfer->setup.wValue == URL_OFFSET_LANDING_PAGE)
+            {
+                transfer->remaining = urlIlandingPage()[0];
+                transfer->ptr = urlIlandingPage();
+                transfer->direction = DEVICE_TO_HOST;
+                success = true;
+            }
+            else if (transfer->setup.wValue == URL_OFFSET_ALLOWED_ORIGIN)
+            {
+                transfer->remaining = urlIallowedOrigin()[0];
+                transfer->ptr = urlIallowedOrigin();
+                transfer->direction = DEVICE_TO_HOST;
+                success = true;
+            }
+            break;
+        default:
+            break;
+    }
+
+    return success;
+}
+
+bool WebUSBDevice::USBCallback_request()
+{
+    bool success = false;
+    /* Process WebUSB requests */
+    CONTROL_TRANSFER * transfer = getTransferPtr();
+    if ((transfer->setup.bmRequestType.Type == VENDOR_TYPE) &&
+        (transfer->setup.bRequest == WEBUSB_VENDOR_CODE))
+    {
+        success = requestWebUSB();
+    }
+    
+    return success;
+}
+
+uint8_t * WebUSBDevice::deviceDesc() {
+    static uint8_t deviceDescriptor[] = {
+        DEVICE_DESCRIPTOR_LENGTH,       /* bLength */
+        DEVICE_DESCRIPTOR,              /* bDescriptorType */
+        LSB(USB_VERSION_2_1),           /* bcdUSB (LSB) */
+        MSB(USB_VERSION_2_1),           /* bcdUSB (MSB) */
+        0x00,                           /* bDeviceClass */
+        0x00,                           /* bDeviceSubClass */
+        0x00,                           /* bDeviceprotocol */
+        MAX_PACKET_SIZE_EP0,            /* bMaxPacketSize0 */
+        (uint8_t)(LSB(VENDOR_ID)),                 /* idVendor (LSB) */
+        (uint8_t)(MSB(VENDOR_ID)),                 /* idVendor (MSB) */
+        (uint8_t)(LSB(PRODUCT_ID)),                /* idProduct (LSB) */
+        (uint8_t)(MSB(PRODUCT_ID)),                /* idProduct (MSB) */
+        (uint8_t)(LSB(PRODUCT_RELEASE)),           /* bcdDevice (LSB) */
+        (uint8_t)(MSB(PRODUCT_RELEASE)),           /* bcdDevice (MSB) */
+        STRING_OFFSET_IMANUFACTURER,    /* iManufacturer */
+        STRING_OFFSET_IPRODUCT,         /* iProduct */
+        STRING_OFFSET_ISERIAL,          /* iSerialNumber */
+        0x01                            /* bNumConfigurations */
+    };
+    return deviceDescriptor;
+}
+
+#define WEBUSB_BOS_TOTAL_LENGTH (BINARY_OBJECT_STORE_DESCRIPTOR_LENGTH \
+                                 + WEBUSB_PLATFORM_DESCRIPTOR_LENGTH)
+
+uint8_t * WebUSBDevice::binaryObjectStoreDesc() {
+    static uint8_t binaryObjectStoreDescriptor[] = {
+        BINARY_OBJECT_STORE_DESCRIPTOR_LENGTH, /* bLength */
+        BINARY_OBJECT_STORE_DESCRIPTOR, /* bDescriptorType */
+        LSB(WEBUSB_BOS_TOTAL_LENGTH),   /* wTotalLength (LSB) */
+        MSB(WEBUSB_BOS_TOTAL_LENGTH),   /* wTotalLength (MSB) */
+        0x01,                           /* bNumDeviceCaps */
+        WEBUSB_PLATFORM_DESCRIPTOR_LENGTH, /* bLength */
+        DEVICE_CAPABILITY_DESCRIPTOR,   /* bDescriptorType */
+        USB_DC_PLATFORM,                /* bDevCapabilityType */
+        0x00,                           /* bReserved */
+        0x38, 0xB6, 0x08, 0x34,         /* PlatformCapabilityUUID */
+        0xA9, 0x09, 0xA0, 0x47,
+        0x8B, 0xFD, 0xA0, 0x76,
+        0x88, 0x15, 0xB6, 0x65,
+        LSB(WEBUSB_VERSION_1_0),        /* bcdVersion (LSB) */
+        MSB(WEBUSB_VERSION_1_0),        /* bcdVersion (MSB) */
+        WEBUSB_VENDOR_CODE,             /* bVendorCode */
+        URL_OFFSET_LANDING_PAGE,        /* iLandingPage */
+    };
+    return binaryObjectStoreDescriptor;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebUSBDevice/WebUSBDevice.h	Sun Sep 04 03:13:09 2016 +0000
@@ -0,0 +1,79 @@
+/*
+* Copyright 2016 Devan Lai
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#ifndef WEBUSB_DEVICE_H
+#define WEBUSB_DEVICE_H
+
+#include "USBDevice.h"
+
+class WebUSBDevice: public USBDevice
+{
+public:
+    WebUSBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
+
+    /*
+    * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
+    * This is used to handle extensions to standard requests
+    * and class specific requests
+    *
+    * @returns true if class handles this request
+    */
+    virtual bool USBCallback_request();
+
+    /*
+    * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
+    *
+    * @returns pointer to the device descriptor
+    */
+    virtual uint8_t * deviceDesc();
+
+    /*
+    * Get binary object store descriptor
+    *
+    * @returns pointer to the binary object store descriptor
+    */
+    virtual uint8_t * binaryObjectStoreDesc();
+    
+    /*
+    * Get the WebUSB allowed origin descriptor
+    *
+    * @returns pointer to the WebUSB allowed origin descriptor
+    */
+    virtual uint8_t * allowedOriginsDesc() = 0;
+    
+    /*
+    * Get WebUSB landing page URL descriptor
+    *
+    * @returns pointer to the landing page URL descriptor
+    */
+    virtual uint8_t * urlIlandingPage() = 0;
+    
+    /*
+    * Get WebUSB allowed origin URL descriptor
+    *
+    * @returns pointer to the allowed origin URL descriptor
+    */
+    virtual uint8_t * urlIallowedOrigin() = 0;
+
+protected:
+    virtual bool requestGetDescriptor(void);
+    virtual bool requestWebUSB(void);
+
+private:
+
+};
+
+#endif