From 957cc01656335450f46b72f3b7ad2d3263ce4885 Mon Sep 17 00:00:00 2001
From: windowsair <msdn_02@sina.com>
Date: Fri, 24 Jan 2020 16:41:15 +0800
Subject: [PATCH] fix: C-style structure definition & other details
 improvements

---
 components/USBIP/USB_handle.c  | 106 +++++++++-------
 components/USBIP/USBd_config.c |  47 +++-----
 components/USBIP/USBd_config.h |  13 ++
 components/USBIP/usb_defs.h    | 214 +++++++++++++++------------------
 components/USBIP/usbip_defs.h  | 117 ++++++++----------
 main/usbip_server.c            |   8 +-
 main/usbip_server.h            |   3 +
 7 files changed, 248 insertions(+), 260 deletions(-)

diff --git a/components/USBIP/USB_handle.c b/components/USBIP/USB_handle.c
index 8b0cd44..56207a2 100644
--- a/components/USBIP/USB_handle.c
+++ b/components/USBIP/USB_handle.c
@@ -8,14 +8,21 @@
  * 
  */
 #include <stdint.h>
+#include "lwip/err.h"
+#include "lwip/sockets.h"
+#include "lwip/sys.h"
+#include <lwip/netdb.h>
 #include "USB_handle.h"
+#include "USBd_config.h"
+#include "usbip_server.h"
+#include "usb_defs.h"
+
 
 // handle functions
-static void handleGetDescriptor(usbip_stage2_header *header);
-static void handle_get_device_descriptor(usbip_stage2_header *header);
+static void handleGetDescriptor(struct usbip_stage2_header *header);
 
 ////TODO: fill this
-int handleUSBControlRequest(usbip_stage2_header *header)
+int handleUSBControlRequest(struct usbip_stage2_header *header)
 {
     // Table 9-3. Standard Device Requests
 
@@ -142,80 +149,87 @@ int handleUSBControlRequest(usbip_stage2_header *header)
 }
 
 ////TODO: fill this
-static void handleGetDescriptor(usbip_stage2_header *header)
+static void handleGetDescriptor(struct usbip_stage2_header *header)
 {
     // 9.4.3 Get Descriptor
     switch (header->u.cmd_submit.request.wValue.u8hi)
     {
-    case USB_DT_DEVICE:
-        handleGetDeviceDescriptor(header);
+    case USB_DT_DEVICE: // get device descriptor
+        os_printf("* GET 0x01 DEVICE DESCRIPTOR\r\n");
+        send_stage2_submit_data(header, 0, kUSBd0DeviceDescriptor, sizeof(kUSBd0DeviceDescriptor));
         break;
 
-    case USB_DT_CONFIGURATION:
-        handleGetConfigurationDescriptor(header);
+    case USB_DT_CONFIGURATION: // get configuration descriptor
+        os_printf("* GET 0x02 CONFIGURATION DESCRIPTOR\r\n");
+        ////TODO: ?
+        if (header->u.cmd_submit.data_length == USB_DT_CONFIGURATION_SIZE)
+        {
+            os_printf("Sending only first part of CONFIG\r\n");
+
+            send_stage2_submit(header, 0, header->u.cmd_submit.data_length);
+            send(kSock, kUSBd0ConfigDescriptor, sizeof(kUSBd0ConfigDescriptor), 0);
+
+        }
+        else
+        {
+            os_printf("Sending ALL CONFIG\r\n");
+
+            send_stage2_submit(header, 0, header->u.cmd_submit.data_length);
+            send(kSock, kUSBd0ConfigDescriptor, sizeof(kUSBd0ConfigDescriptor), 0);
+            send(kSock, kUSBd0InterfaceDescriptor, sizeof(kUSBd0InterfaceDescriptor), 0);
+
+        }
         break;
 
     case USB_DT_STRING:
-        handleGetStringDescriptor(header);
+        os_printf("* GET 0x03 STRING DESCRIPTOR\r\n");
+
+        if (header->u.cmd_submit.request.wValue.u8lo == 0) {
+            os_printf("** REQUESTED list of supported languages\r\n");
+            send_stage2_submit_data(header, 0, kLangDescriptor, sizeof(kLangDescriptor));
+        }
+        else{
+            os_printf("***Unsupported operation***\r\n");
+        }
         break;
 
     case USB_DT_INTERFACE:
-        handleGetInterfaceDescriptor(header);
+        os_printf("* GET 0x04 INTERFACE DESCRIPTOR (UNIMPLEMENTED)\r\n");
+        ////TODO:UNIMPLEMENTED
+        send_stage2_submit(header, 0, 0);
         break;
 
     case USB_DT_ENDPOINT:
-        handleGetEndpointDescriptor(header);
+        os_printf("* GET 0x05 ENDPOINT DESCRIPTOR (UNIMPLEMENTED)\r\n");
+        ////TODO:UNIMPLEMENTED
+        send_stage2_submit(header, 0, 0);
         break;
 
     case USB_DT_DEVICE_QUALIFIER:
-        handleGetDeviceQualifierDescriptor(header);
+        os_printf("* GET 0x06 DEVICE QUALIFIER DESCRIPTOR");
+
+        usb_device_qualifier_descriptor desc;
+
+        memset(&desc, 0, sizeof(usb_device_qualifier_descriptor));
+
+        send_stage2_submit_data(header, 0, &desc, sizeof(usb_device_qualifier_descriptor));
         break;
 
     case USB_DT_OTHER_SPEED_CONFIGURATION:
         os_printf("GET 0x07 [UNIMPLEMENTED] USB_DT_OTHER_SPEED_CONFIGURATION");
+        ////TODO:UNIMPLEMENTED
+        send_stage2_submit(header, 0, 0);
         break;
 
     case USB_DT_INTERFACE_POWER:
         os_printf("GET 0x08 [UNIMPLEMENTED] USB_DT_INTERFACE_POWER");
+        ////TODO:UNIMPLEMENTED
+        send_stage2_submit(header, 0, 0);
         break;
 
-    case USB_DT_REPORT:
-        handle_get_hid_report_descriptor(header);
-        break;
 
     default:
         os_printf("USB unknown Get Descriptor requested:%d", header->u.cmd_submit.request.wValue.u8lo);
         break;
     }
 }
-
-static void handle_get_device_descriptor(usbip_stage2_header *header)
-{
-    os_printf("* GET 0x01 DEVICE DESCRIPTOR\r\n");
-
-    usb_device_descriptor desc;
-
-    desc.bLength = USB_DT_DEVICE_SIZE;
-    desc.bDescriptorType = USB_DT_DEVICE;
-
-    desc.bcdUSB = 0x0110;
-
-    // defined at interface level
-    desc.bDeviceClass = 0x0;
-    desc.bDeviceSubClass = 0x0;
-    desc.bDeviceProtocol = 0x0;
-
-    desc.bMaxPacketSize0 = USB_HID_MAX_PACKET_SIZE;
-
-    desc.idVendor = USB_DEVICE_VENDOR_ID;
-    desc.idProduct = USB_DEVICE_PRODUCT_ID;
-    desc.bcdDevice = USB_DEVICE_VERSION;
-
-    desc.iManufacturer = STR_IMANUFACTURER;
-    desc.iProduct = STR_IPRODUCT;
-    desc.iSerialNumber = STR_ISERIAL;
-
-    desc.bNumConfigurations = 1;
-
-    send_stage2_submit_data(header, 0, &desc, sizeof(usb_device_descriptor));
-}
\ No newline at end of file
diff --git a/components/USBIP/USBd_config.c b/components/USBIP/USBd_config.c
index c857de3..e0f57ab 100644
--- a/components/USBIP/USBd_config.c
+++ b/components/USBIP/USBd_config.c
@@ -12,7 +12,6 @@
 #include <stdbool.h>
 #include "USBd_config.h"
 #include "usb_defs.h"
-#include "USBd_config_CustomClass_0.h"
 
 #define USBShort(ui16Value)     ((ui16Value) & 0xff), ((ui16Value) >> 8) //((ui16Value) & 0xFF),(((ui16Value) >> 8) & 0xFF)
 
@@ -54,7 +53,7 @@ const uint8_t kUSBd0DeviceDescriptor[0x12] =
 
 
 // Standard Interface Descriptor
-const uint8_t kUSBd0InterfaceDescriptor[]=
+const uint8_t kUSBd0InterfaceDescriptor[0x1E]=
 {
     0x09,                                   // bLength
     USB_DT_INTERFACE,                       // bDescriptorType
@@ -72,16 +71,6 @@ const uint8_t kUSBd0InterfaceDescriptor[]=
 
     // Standard Endpoint Descriptor
 
-    0x07,                                        // bLength
-    USB_DTYPE_ENDPOINT,                          // bDescriptorType
-    USBD_CUSTOM_CLASS0_IF0_EP0_BENDPOINTADDRESS, // bEndpointAddress -> set 0x01 for endpoint 0
-    USB_EP_ATTR_BULK,                            // bmAttributes -> Endpoint is a bulk endpoint.
-    USBShort(USBD_CUSTOM_CLASS0_IF0_EP0_HS_WMAXPACKETSIZE),  
-                                                 // wMaxPacketSize -> The maximum packet size: 512 bytes
-    // We assume that it always runs in High Speed.
-    USBD_CUSTOM_CLASS0_IF0_EP0_HS_BINTERVAL,     // bInterval
-
-
     // Endpoint 1: Bulk Out – used for commands received from host PC.
     // Endpoint 2: Bulk In – used for responses send to host PC.
     // Endpoint 3: Bulk In (optional) – used for streaming SWO trace
@@ -98,36 +87,38 @@ const uint8_t kUSBd0InterfaceDescriptor[]=
 
     // "Endpoint 1: Bulk Out – used for commands received from host PC."  PC -> Device
     0x07,                                                      // bLength                                          
-    USB_DTYPE_ENDPOINT,                                        // bDescriptorType                                      
+    USB_DT_ENDPOINT,                                           // bDescriptorType                                      
     0x01,                                                      // bEndpointAddress
-    USB_ENDPOINT_ATTR_BULK,                                          // bmAttributes           
-    USBShort(USBD_CUSTOM_CLASS0_IF0_EP1_HS_WMAXPACKETSIZE),    // wMaxPacketSize   
-    USBD_CUSTOM_CLASS0_IF0_EP1_HS_BINTERVAL, // bInterval 
+    USB_ENDPOINT_ATTR_BULK,                                    // bmAttributes           
+    USBShort(512),    // wMaxPacketSize   
+    // We assume that it always runs in High Speed.
+    0x00, // bInterval 
 
     /*                 Pysical endpoint 1                 */
     
     // "Endpoint 2: Bulk In – used for responses send to host PC." Device -> PC
     0x07,                                                      // bLength                                          
-    USB_DTYPE_ENDPOINT,                                        // bDescriptorType                                      
+    USB_DT_ENDPOINT,                                           // bDescriptorType                                      
     0x81,                                                      // bEndpointAddress
-    USB_ENDPOINT_ATTR_BULK,                                          // bmAttributes           
-    USBShort(USBD_CUSTOM_CLASS0_IF0_EP2_HS_WMAXPACKETSIZE),    // wMaxPacketSize   
-    USBD_CUSTOM_CLASS0_IF0_EP2_HS_BINTERVAL, // bInterval 
+    USB_ENDPOINT_ATTR_BULK,                                    // bmAttributes           
+    USBShort(512),    // wMaxPacketSize   
+    0x00, // bInterval 
 
     /*                 Pysical endpoint 2                */
     // "Endpoint 3: Bulk In (optional) – used for streaming SWO trace" Device -> PC
     0x07,                                                      // bLength                                          
-    USB_DTYPE_ENDPOINT,                                        // bDescriptorType                                      
+    USB_DT_ENDPOINT,                                           // bDescriptorType                                      
     0x82,                                                      // bEndpointAddress
-    USB_ENDPOINT_ATTR_BULK,                                          // bmAttributes           
-    USBShort(USBD_CUSTOM_CLASS0_IF0_EP3_HS_WMAXPACKETSIZE),    // wMaxPacketSize   
-    USBD_CUSTOM_CLASS0_IF0_EP3_HS_BINTERVAL, // bInterval 
+    USB_ENDPOINT_ATTR_BULK,                                    // bmAttributes           
+    USBShort(512),    // wMaxPacketSize   
+    0x00,                                                      // bInterval 
 
 
 };
 
 // Standard Configuration Descriptor
-const uint8_t kUSBd0ConfigDescriptor[] =
+#define LENGTHOFCONFIGDESCRIPTOR 9
+const uint8_t kUSBd0ConfigDescriptor[LENGTHOFCONFIGDESCRIPTOR] =
 {
     // Configuration descriptor header.
 
@@ -135,7 +126,7 @@ const uint8_t kUSBd0ConfigDescriptor[] =
     0x03,                                   // bDescriptorType 
                                             // constant, set to 0x03
                                        
-    USBShort((sizeof(kUSBd0InterfaceDescriptor)) + (sizeof(kUSBd0ConfigDescriptor))),  
+    USBShort((sizeof(kUSBd0InterfaceDescriptor)) + (LENGTHOFCONFIGDESCRIPTOR)),  
                                             // wTotalLength
 
     0x01,                                   // bNumInterfaces 
@@ -155,7 +146,7 @@ const uint8_t kUSBd0ConfigDescriptor[] =
  */
 
 
-const uint8_t kLangDescriptor[] =
+const uint8_t kLangDescriptor[0x04] =
 {
     4,
     USB_DT_STRING,
@@ -210,7 +201,7 @@ const uint8_t kInterfaceString[0x2C] =
     'P', 0
 };
 
-const uint8_t * const kUSBd0StringDescriptorsSet[] =
+const uint8_t * const kUSBd0StringDescriptorsSet[0x05] =
 {
     kLangDescriptor,
     kManufacturerString,
diff --git a/components/USBIP/USBd_config.h b/components/USBIP/USBd_config.h
index 88e213a..6af8944 100644
--- a/components/USBIP/USBd_config.h
+++ b/components/USBIP/USBd_config.h
@@ -38,4 +38,17 @@
 
 
 
+
+
+/////////////////////////////////////////////
+extern const uint8_t kUSBd0DeviceDescriptor[0x12];
+extern const uint8_t kUSBd0InterfaceDescriptor[0x1E];
+extern const uint8_t kUSBd0ConfigDescriptor[0x09];
+extern const uint8_t kLangDescriptor[0x04];
+extern const uint8_t kManufacturerString[0x28];
+extern const uint8_t kProductString[0x18];
+extern const uint8_t kSerialNumberString[0x1A];
+extern const uint8_t kInterfaceString[0x2C];
+
+
 #endif
\ No newline at end of file
diff --git a/components/USBIP/usb_defs.h b/components/USBIP/usb_defs.h
index 995a97e..31898da 100644
--- a/components/USBIP/usb_defs.h
+++ b/components/USBIP/usb_defs.h
@@ -17,37 +17,32 @@
 
 #include <stdint.h>
 
-
-#define USB_CLASS_MISCELLANEOUS_DEVICE                     0xef
-#define USB_MISC_SUBCLASS_COMMON                           0x02
+#define USB_CLASS_MISCELLANEOUS_DEVICE 0xef
+#define USB_MISC_SUBCLASS_COMMON 0x02
 #define USB_MISC_PROTOCOL_INTERFACE_ASSOCIATION_DESCRIPTOR 0x01
 
-
-union word_t {
-    struct {
+typedef union {
+    struct
+    {
         uint8_t u8lo;
         uint8_t u8hi;
     } __attribute__((packed));
     uint16_t u16;
-};
+} word_t;
 
-struct usb_standard_request
+typedef struct
 {
-    uint8_t  bmRequestType;
-    uint8_t  bRequest;
+    uint8_t bmRequestType;
+    uint8_t bRequest;
     word_t wValue; // 16bit
     word_t wIndex;
     word_t wLength;
-} __attribute__((packed));
-
-
-
-
+} __attribute__((packed)) usb_standard_request;
 
 //#define USB_CLASS_HID	3
 
-#define USB_DT_HID      0x21
-#define USB_DT_REPORT   0x22
+#define USB_DT_HID 0x21
+#define USB_DT_REPORT 0x22
 
 //struct usb_hid_descriptor {
 //    uint8_t bLength;
@@ -63,84 +58,79 @@ struct usb_standard_request
 //    uint16_t wReportLength;
 //} __attribute__((packed));
 
-
 #define USB_DT_REPORT_SIZE sizeof(struct usb_hid_report_descriptor)
 
-
 /* Class Definition */
-#define USB_CLASS_VENDOR			0xFF
+#define USB_CLASS_VENDOR 0xFF
 
 ///////////////////////////////////////////////////////////////
 /* Table 9-2. Format of Setup Data */
 /* bmRequestType bit definitions */
 
 /* bit 7 : Direction */
-#define USB_REQ_TYPE_OUT		    0x00 // Host-to-device
-#define USB_REQ_TYPE_IN				0x80 // Device-to-host
+#define USB_REQ_TYPE_OUT 0x00 // Host-to-device
+#define USB_REQ_TYPE_IN 0x80  // Device-to-host
 /* bits 6..5 : Type */
-#define USB_REQ_TYPE_STANDARD	    0x00
-#define USB_REQ_TYPE_CLASS			0x20
-#define USB_REQ_TYPE_VENDOR			0x40
-#define USB_REQ_TYPE_RESERVED	    0x60
+#define USB_REQ_TYPE_STANDARD 0x00
+#define USB_REQ_TYPE_CLASS 0x20
+#define USB_REQ_TYPE_VENDOR 0x40
+#define USB_REQ_TYPE_RESERVED 0x60
 /* bits 4..0 : Recipient */
-#define USB_REQ_TYPE_DEVICE			0x00
-#define USB_REQ_TYPE_INTERFACE		0x01
-#define USB_REQ_TYPE_ENDPOINT		0x02
-#define USB_REQ_TYPE_OTHER			0x03
-#define USB_REQ_TYPE_RESERVED		0x1F
+#define USB_REQ_TYPE_DEVICE 0x00
+#define USB_REQ_TYPE_INTERFACE 0x01
+#define USB_REQ_TYPE_ENDPOINT 0x02
+#define USB_REQ_TYPE_OTHER 0x03
+#define USB_REQ_TYPE_RESERVED 0x1F
 ///////////////////////////////////////////////////////////////
 
-
 ///////////////////////////////////////////////////////////////
 /* USB Standard Request Codes - Table 9-4 */
-#define USB_REQ_GET_STATUS			    0
-#define USB_REQ_CLEAR_FEATURE		    1
+#define USB_REQ_GET_STATUS 0
+#define USB_REQ_CLEAR_FEATURE 1
 /* Reserved for future use: 2 */
-#define USB_REQ_SET_FEATURE			    3 
+#define USB_REQ_SET_FEATURE 3
 /* Reserved for future use: 3 */
-#define USB_REQ_SET_ADDRESS			    5
-#define USB_REQ_GET_DESCRIPTOR			6
-#define USB_REQ_SET_DESCRIPTOR			7
-#define USB_REQ_GET_CONFIGURATION		8
-#define USB_REQ_SET_CONFIGURATION		9
-#define USB_REQ_GET_INTERFACE			10
-#define USB_REQ_SET_INTERFACE			11
-#define USB_REQ_SET_SYNCH_FRAME			12
+#define USB_REQ_SET_ADDRESS 5
+#define USB_REQ_GET_DESCRIPTOR 6
+#define USB_REQ_SET_DESCRIPTOR 7
+#define USB_REQ_GET_CONFIGURATION 8
+#define USB_REQ_SET_CONFIGURATION 9
+#define USB_REQ_GET_INTERFACE 10
+#define USB_REQ_SET_INTERFACE 11
+#define USB_REQ_SET_SYNCH_FRAME 12
 ///////////////////////////////////////////////////////////////
 
-
 ///////////////////////////////////////////////////////////////
 /* USB Descriptor Types - Table 9-5 */
-#define USB_DT_DEVICE				        1
-#define USB_DT_CONFIGURATION			    2
-#define USB_DT_STRING				        3
-#define USB_DT_INTERFACE			        4
-#define USB_DT_ENDPOINT				        5
-#define USB_DT_DEVICE_QUALIFIER			    6
-#define USB_DT_OTHER_SPEED_CONFIGURATION	7
-#define USB_DT_INTERFACE_POWER			    8
+#define USB_DT_DEVICE 1
+#define USB_DT_CONFIGURATION 2
+#define USB_DT_STRING 3
+#define USB_DT_INTERFACE 4
+#define USB_DT_ENDPOINT 5
+#define USB_DT_DEVICE_QUALIFIER 6
+#define USB_DT_OTHER_SPEED_CONFIGURATION 7
+#define USB_DT_INTERFACE_POWER 8
 /* From ECNs */
-#define USB_DT_OTG				            9
-#define USB_DT_DEBUG				        10
-#define USB_DT_INTERFACE_ASSOCIATION		11
+#define USB_DT_OTG 9
+#define USB_DT_DEBUG 10
+#define USB_DT_INTERFACE_ASSOCIATION 11
 ///////////////////////////////////////////////////////////////
 
-
 ///////////////////////////////////////////////////////////////
 /* USB Standard Feature Selectors - Table 9-6 */
-#define USB_FEAT_ENDPOINT_HALT			    0 // Recipient: Device
-#define USB_FEAT_DEVICE_REMOTE_WAKEUP		1 // Recipient: Endpoint
-#define USB_FEAT_TEST_MODE			        2 // Recipient: Device
+#define USB_FEAT_ENDPOINT_HALT 0        // Recipient: Device
+#define USB_FEAT_DEVICE_REMOTE_WAKEUP 1 // Recipient: Endpoint
+#define USB_FEAT_TEST_MODE 2            // Recipient: Device
 
 /* Information Returned by a GetStatus() Request to a Device - Figure 9-4 */
-#define USB_DEV_STATUS_SELF_POWERED		0x01
-#define USB_DEV_STATUS_REMOTE_WAKEUP	0x02
+#define USB_DEV_STATUS_SELF_POWERED 0x01
+#define USB_DEV_STATUS_REMOTE_WAKEUP 0x02
 ///////////////////////////////////////////////////////////////
 
-
 ///////////////////////////////////////////////////////////////
 /* USB Standard Device Descriptor - Table 9-8 */
-struct usb_device_descriptor {
+typedef struct
+{
     uint8_t bLength;
     uint8_t bDescriptorType;
     uint16_t bcdUSB;
@@ -155,17 +145,16 @@ struct usb_device_descriptor {
     uint8_t iProduct;
     uint8_t iSerialNumber;
     uint8_t bNumConfigurations;
-} __attribute__((packed));
-#define USB_DT_DEVICE_SIZE sizeof(struct usb_device_descriptor)
+} __attribute__((packed)) usb_device_descriptor;
+#define USB_DT_DEVICE_SIZE sizeof(usb_device_descriptor)
 ///////////////////////////////////////////////////////////////
 
-
-
 ///////////////////////////////////////////////////////////////
 /* USB Device_Qualifier Descriptor - Table 9-9
  * Not used in this implementation.
  */
-struct usb_device_qualifier_descriptor {
+typedef struct
+{
     uint8_t bLength;
     uint8_t bDescriptorType;
     uint16_t bcdUSB;
@@ -175,25 +164,24 @@ struct usb_device_qualifier_descriptor {
     uint8_t bMaxPacketSize0;
     uint8_t bNumConfigurations;
     uint8_t bReserved;
-} __attribute__((packed));
+} __attribute__((packed)) usb_device_qualifier_descriptor;
 ///////////////////////////////////////////////////////////////
 
-
 /* This is only defined as a top level named struct to improve c++
  * compatibility.  You should never need to instance this struct
  * in user code! */
-struct usb_interface {
+typedef struct
+{
     uint8_t *cur_altsetting;
     uint8_t num_altsetting;
     const struct usb_iface_assoc_descriptor *iface_assoc;
     const struct usb_interface_descriptor *altsetting;
-};
-
-
+} __attribute__((packed)) usb_interface;
 
 ///////////////////////////////////////////////////////////////
 /* USB Standard Configuration Descriptor - Table 9-10 */
-struct usb_config_descriptor {
+typedef struct
+{
     uint8_t bLength;
     uint8_t bDescriptorType;
     uint16_t wTotalLength;
@@ -202,25 +190,23 @@ struct usb_config_descriptor {
     uint8_t iConfiguration;
     uint8_t bmAttributes;
     uint8_t bMaxPower;
-} __attribute__((packed));
-#define USB_DT_CONFIGURATION_SIZE sizeof(struct usb_config_descriptor)
+} __attribute__((packed)) usb_config_descriptor;
+#define USB_DT_CONFIGURATION_SIZE sizeof(usb_config_descriptor)
 ///////////////////////////////////////////////////////////////
 
-
-
 /* USB Configuration Descriptor *bmAttributes* bit definitions */
-#define USB_CONFIG_ATTR_DEFAULT			    0x80	/** always required (USB2.0 table 9-10) */
-#define USB_CONFIG_ATTR_SELF_POWERED		0x40
-#define USB_CONFIG_ATTR_REMOTE_WAKEUP		0x20
+#define USB_CONFIG_ATTR_DEFAULT 0x80 /** always required (USB2.0 table 9-10) */
+#define USB_CONFIG_ATTR_SELF_POWERED 0x40
+#define USB_CONFIG_ATTR_REMOTE_WAKEUP 0x20
 
 /* Other Speed Configuration is the same as Configuration Descriptor.
  *  - Table 9-11
  */
 
-
 ///////////////////////////////////////////////////////////////
 /* USB Standard Interface Descriptor - Table 9-12 */
-struct usb_interface_descriptor {
+typedef struct
+{
     uint8_t bLength;
     uint8_t bDescriptorType;
     uint8_t bInterfaceNumber;
@@ -230,15 +216,14 @@ struct usb_interface_descriptor {
     uint8_t bInterfaceSubClass;
     uint8_t bInterfaceProtocol;
     uint8_t iInterface;
-} __attribute__((packed));
-#define USB_DT_INTERFACE_SIZE sizeof(struct usb_interface_descriptor)
+} __attribute__((packed)) usb_interface_descriptor;
+#define USB_DT_INTERFACE_SIZE sizeof(usb_interface_descriptor)
 ///////////////////////////////////////////////////////////////
 
-
-
 ///////////////////////////////////////////////////////////////
 /* USB Standard Endpoint Descriptor - Table 9-13 */
-struct usb_endpoint_descriptor {
+typedef struct usb_endpoint_descriptor
+{
     uint8_t bLength;
     uint8_t bDescriptorType;
     uint8_t bEndpointAddress;
@@ -246,52 +231,50 @@ struct usb_endpoint_descriptor {
     uint16_t wMaxPacketSize;
     uint8_t bInterval;
 } __attribute__((packed));
-#define USB_DT_ENDPOINT_SIZE sizeof(struct usb_endpoint_descriptor)
+#define USB_DT_ENDPOINT_SIZE sizeof(usb_endpoint_descriptor)
 ///////////////////////////////////////////////////////////////
 
-
 /* USB bEndpointAddress helper macros */
 #define USB_ENDPOINT_ADDR_OUT(x) (x)
 #define USB_ENDPOINT_ADDR_IN(x) (0x80 | (x))
 
-
 ///////////////////////////////////////////////////////////////
 /* USB Endpoint Descriptor bmAttributes bit definitions - Table 9-13 */
 /* bits 1..0 : Transfer type */
-#define USB_ENDPOINT_ATTR_CONTROL		0x00
-#define USB_ENDPOINT_ATTR_ISOCHRONOUS   0x01
-#define USB_ENDPOINT_ATTR_BULK			0x02
-#define USB_ENDPOINT_ATTR_INTERRUPT		0x03
-#define USB_ENDPOINT_ATTR_TYPE		    0x03
+#define USB_ENDPOINT_ATTR_CONTROL 0x00
+#define USB_ENDPOINT_ATTR_ISOCHRONOUS 0x01
+#define USB_ENDPOINT_ATTR_BULK 0x02
+#define USB_ENDPOINT_ATTR_INTERRUPT 0x03
+#define USB_ENDPOINT_ATTR_TYPE 0x03
 // If not an isochronous endpoint, bits 5..2 are reserved
 // and must be set to zero.
 /* bits 3..2 : Sync type (only if ISOCHRONOUS) */
-#define USB_ENDPOINT_ATTR_NOSYNC		0x00
-#define USB_ENDPOINT_ATTR_ASYNC			0x04
-#define USB_ENDPOINT_ATTR_ADAPTIVE		0x08
-#define USB_ENDPOINT_ATTR_SYNC			0x0C
-#define USB_ENDPOINT_ATTR_SYNCTYPE		0x0C
+#define USB_ENDPOINT_ATTR_NOSYNC 0x00
+#define USB_ENDPOINT_ATTR_ASYNC 0x04
+#define USB_ENDPOINT_ATTR_ADAPTIVE 0x08
+#define USB_ENDPOINT_ATTR_SYNC 0x0C
+#define USB_ENDPOINT_ATTR_SYNCTYPE 0x0C
 /* bits 5..4 : Usage type (only if ISOCHRONOUS) */
-#define USB_ENDPOINT_ATTR_DATA			0x00
-#define USB_ENDPOINT_ATTR_FEEDBACK		0x10
+#define USB_ENDPOINT_ATTR_DATA 0x00
+#define USB_ENDPOINT_ATTR_FEEDBACK 0x10
 #define USB_ENDPOINT_ATTR_IMPLICIT_FEEDBACK_DATA 0x20
-#define USB_ENDPOINT_ATTR_USAGETYPE		0x30
+#define USB_ENDPOINT_ATTR_USAGETYPE 0x30
 ///////////////////////////////////////////////////////////////
 
-
-
 ///////////////////////////////////////////////////////////////
 /* Table 9-15 specifies String Descriptor Zero.
  * Table 9-16 specified UNICODE String Descriptor.
  */
-struct usb_string_descriptor {
+typedef struct
+{
     uint8_t bLength;
     uint8_t bDescriptorType;
     uint16_t wData[];
-} __attribute__((packed));
+} __attribute__((packed)) usb_string_descriptor;
 
 /* From ECN: Interface Association Descriptors, Table 9-Z */
-struct usb_iface_assoc_descriptor {
+typedef struct
+{
     uint8_t bLength;
     uint8_t bDescriptorType;
     uint8_t bFirstInterface;
@@ -300,17 +283,14 @@ struct usb_iface_assoc_descriptor {
     uint8_t bFunctionSubClass;
     uint8_t bFunctionProtocol;
     uint8_t iFunction;
-} __attribute__((packed));
+} __attribute__((packed)) usb_iface_assoc_descriptor;
 #define USB_DT_INTERFACE_ASSOCIATION_SIZE \
-				sizeof(struct usb_iface_assoc_descriptor)
+    sizeof(usb_iface_assoc_descriptor)
 
-enum usb_language_id {
+enum usb_language_id
+{
     USB_LANGID_ENGLISH_US = 0x409,
 };
 ///////////////////////////////////////////////////////////////
 
-
-
-
-
 #endif
diff --git a/components/USBIP/usbip_defs.h b/components/USBIP/usbip_defs.h
index eb3935c..c977914 100644
--- a/components/USBIP/usbip_defs.h
+++ b/components/USBIP/usbip_defs.h
@@ -12,7 +12,6 @@
 // usbip_stage2_header
 // usbip_stage1_response_devlist
 
-
 //
 // Created by thevoidnn on 10/25/17.
 //
@@ -26,36 +25,39 @@
 #define USBIP_SYSFS_PATH_SIZE 256
 #define USBIP_BUSID_SIZE 32
 
-enum usbip_stage1_command {
+enum usbip_stage1_command
+{
     // Offset 2
-    USBIP_STAGE1_CMD_DEVICE_LIST   = 0x05, // OP_REQ_DEVLIST
+    USBIP_STAGE1_CMD_DEVICE_LIST = 0x05,   // OP_REQ_DEVLIST
     USBIP_STAGE1_CMD_DEVICE_ATTACH = 0x03, // OP_REQ_IMPORT
 };
 
-enum usbip_stager2_command {       ////TODO: change to stage2
-    //Offset 0 
-    USBIP_STAGE2_REQ_SUBMIT = 0x0001, 
+enum usbip_stager2_command
+{ ////TODO: change to stage2
+    //Offset 0
+    USBIP_STAGE2_REQ_SUBMIT = 0x0001,
     USBIP_STAGE2_REQ_UNLINK = 0x0002,
     USBIP_STAGE2_RSP_SUBMIT = 0x0003,
     USBIP_STAGE2_RSP_UNLINK = 0x0004,
 };
 
-enum usbip_stage2_direction {
+enum usbip_stage2_direction
+{
     USBIP_DIR_OUT = 0x00,
-    USBIP_DIR_IN  = 0x01,
+    USBIP_DIR_IN = 0x01,
 };
 
-struct usbip_stage1_header {
+typedef struct
+{
     uint16_t version;
     uint16_t command;
-    uint32_t  status;
-} __attribute__ ((__packed__));
+    uint32_t status;
+} __attribute__((__packed__)) usbip_stage1_header;
 /////////////////////////////////////////////////////////////
 
-
-
 // Device description
-struct usbip_stage1_usb_device {
+typedef struct
+{
     char path[USBIP_SYSFS_PATH_SIZE];
     char busid[USBIP_BUSID_SIZE];
 
@@ -74,39 +76,33 @@ struct usbip_stage1_usb_device {
     uint8_t bConfigurationValue;
     uint8_t bNumConfigurations;
     uint8_t bNumInterfaces;
-} __attribute__((packed));
+} __attribute__((packed)) usbip_stage1_usb_device;
 
 // Interface description
-struct usbip_stage1_usb_interface {
+typedef struct
+{
     uint8_t bInterfaceClass;
     uint8_t bInterfaceSubClass;
     uint8_t bInterfaceProtocol;
     uint8_t padding;
-} __attribute__((packed));
+} __attribute__((packed)) usbip_stage1_usb_interface;
 
+typedef struct
+{
+    usbip_stage1_usb_device udev;
+    usbip_stage1_usb_interface uinf[];
+} __attribute__((packed)) usbip_stage1_response_devlist_entry;
 
-
-
-struct usbip_stage1_response_devlist_entry {
-    struct usbip_stage1_usb_device    udev;
-    struct usbip_stage1_usb_interface uinf[];
-} __attribute__((packed));
-
-struct usbip_stage1_response_devlist {
-    uint32_t  list_size;
+typedef struct
+{
+    uint32_t list_size;
     usbip_stage1_response_devlist_entry devices[];
-} __attribute__ ((__packed__));
-
-
-
-
+} __attribute__((__packed__)) usbip_stage1_response_devlist;
 
 ///////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////
 
-
-
 /**
  * struct usbip_header_basic - data pertinent to every URB request
  * RESPONSE & REQUEST
@@ -119,13 +115,14 @@ struct usbip_stage1_response_devlist {
  * @direction: direction of the transfer
  * @ep: endpoint number
  */
-struct usbip_stage2_header_basic {
+typedef struct
+{
     uint32_t command;
     uint32_t seqnum;
     uint32_t devid;
     uint32_t direction;
     uint32_t ep;
-} __attribute__((packed));
+} __attribute__((packed)) usbip_stage2_header_basic;
 
 /**
  * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
@@ -138,7 +135,8 @@ struct usbip_stage2_header_basic {
  * @interval: maximum time for the request on the server-side host controller
  * @setup: setup data for a control request
  */
-struct usbip_stage2_header_cmd_submit {
+typedef struct
+{
     uint32_t transfer_flags;
     int32_t data_length;
 
@@ -151,7 +149,7 @@ struct usbip_stage2_header_cmd_submit {
         uint8_t setup[8];
         usb_standard_request request;
     };
-} __attribute__((packed));
+} __attribute__((packed)) usbip_stage2_header_cmd_submit;
 
 /**
  * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
@@ -163,61 +161,50 @@ struct usbip_stage2_header_cmd_submit {
  * @number_of_packets: number of isochronous packets
  * @error_count: number of errors for isochronous transfers
  */
-struct usbip_stage2_header_ret_submit {
+typedef struct
+{
     int32_t status;
-    int32_t data_length;//actual_length
+    int32_t data_length; //actual_length
     int32_t start_frame;
     int32_t number_of_packets;
     int32_t error_count;
-} __attribute__((packed));
-
-
-
+} __attribute__((packed)) usbip_stage2_header_ret_submit;
 
 /**
  * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
  * >>>REQUEST
  * @seqnum: the URB seqnum to unlink
  */
-struct usbip_stage2_header_cmd_unlink {
+typedef struct
+{
     uint32_t seqnum;
-} __attribute__((packed));
+} __attribute__((packed)) usbip_stage2_header_cmd_unlink;
 
 /**
  * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
  * <<<RESPONSE
  * @status: return status of the request
  */
-struct usbip_stage2_header_ret_unlink {
+typedef struct
+{
     int32_t status;
-} __attribute__((packed));
-
-
-
-
-
-
+} __attribute__((packed)) usbip_stage2_header_ret_unlink;
 
 /**
  * struct usbip_header - common header for all usbip packets
  * @base: the basic header
  * @u: packet type dependent header
  */
-struct usbip_stage2_header {
+typedef struct
+{
     struct usbip_stage2_header_basic base;
 
     union {
-        struct usbip_stage2_header_cmd_submit	cmd_submit;
-        struct usbip_stage2_header_ret_submit	ret_submit;
-        struct usbip_stage2_header_cmd_unlink	cmd_unlink;
-        struct usbip_stage2_header_ret_unlink	ret_unlink;
+        struct usbip_stage2_header_cmd_submit cmd_submit;
+        struct usbip_stage2_header_ret_submit ret_submit;
+        struct usbip_stage2_header_cmd_unlink cmd_unlink;
+        struct usbip_stage2_header_ret_unlink ret_unlink;
     } u;
-} __attribute__((packed));
-
-
-
-
-
-
+} __attribute__((packed)) usbip_stage2_header;
 
 #endif
diff --git a/main/usbip_server.c b/main/usbip_server.c
index 38faf78..b18d235 100644
--- a/main/usbip_server.c
+++ b/main/usbip_server.c
@@ -7,6 +7,7 @@
 #include "usbip_server.h"
 #include "usbip_defs.h"
 #include "usb_defs.h"
+#include "USBd_config.h"
 
 // attach helper function
 static int read_stage1_command(uint8_t *buffer, uint32_t length);
@@ -22,7 +23,6 @@ static void pack(void *data, int size);
 static void unpack(void *data, int size);
 static int handle_submit(usbip_stage2_header *header);
 static int handle_control_request(usbip_stage2_header *header);
-static void send_stage2_submit(usbip_stage2_header *req_header, int32_t status, int32_t data_length);
 
 int attach(uint8_t *buffer, uint32_t length)
 {
@@ -107,7 +107,7 @@ static void send_device_list()
     // we have only 1 device, so:
     response_devlist.list_size = htonl(1);
 
-    send(socket, (uint8_t *)&response_devlist, sizeof(usbip_stage1_response_devlist), 0);
+    send(kSock, (uint8_t *)&response_devlist, sizeof(usbip_stage1_response_devlist), 0);
 
     // may be foreach:
 
@@ -296,13 +296,13 @@ static int handle_submit(usbip_stage2_header *header)
 
     default:
         os_printf("*** WARN ! UNKNOWN ENDPOINT: ");
-        os_printf((int)header.base.ep);
+        os_printf((int)header->base.ep);
         return -1;
     }
     return 0;
 }
 
-static void send_stage2_submit(usbip_stage2_header *req_header, int32_t status, int32_t data_length)
+void send_stage2_submit(usbip_stage2_header *req_header, int32_t status, int32_t data_length)
 {
 
     req_header->base.command = USBIP_STAGE2_RSP_SUBMIT;
diff --git a/main/usbip_server.h b/main/usbip_server.h
index c085c28..12df7d6 100644
--- a/main/usbip_server.h
+++ b/main/usbip_server.h
@@ -1,6 +1,7 @@
 #ifndef __USBIP_SERVER_H__
 #define __USBIP_SERVER_H__
 #include <stdint.h>
+#include "usbip_defs.h"
 enum state_t
 {
     ACCEPTING,
@@ -12,5 +13,7 @@ extern int kSock;
 
 int attach(uint8_t *buffer, uint32_t length);
 int emulate(uint8_t *buffer, uint32_t length);
+void send_stage2_submit_data(usbip_stage2_header *req_header, int32_t status, void *data, int32_t data_length);
+void send_stage2_submit(usbip_stage2_header *req_header, int32_t status, int32_t data_length);
 
 #endif
\ No newline at end of file