From ad6b9b3609207840448b6f3f3d72e438c49936e4 Mon Sep 17 00:00:00 2001
From: windowsair <msdn_02@sina.com>
Date: Fri, 31 Jan 2020 15:57:17 +0800
Subject: [PATCH] fix: Fixed some rookie mistakes This is a temporary "stable"
 release. See the Readme.md file in the root directory for more instructions.

---
 README.md                            |  10 +-
 components/DAP/config/DAP_config.h   |  15 ++-
 components/README.md                 |   4 +-
 components/USBIP/MSOS20Descriptors.c | 166 +++++++++++++--------------
 components/USBIP/MSOS20Descriptors.h | 160 +++++++++++++-------------
 components/USBIP/USB_handle.c        |  38 +++++-
 components/USBIP/USBd_config.c       |  34 +++---
 main/DAP_handle.c                    |  29 ++---
 main/main.c                          |   2 +
 main/tcp_server.c                    | 122 ++++++++++----------
 main/usbip_server.c                  |  23 ++--
 main/wifi_configuration.h            |   4 +-
 12 files changed, 318 insertions(+), 289 deletions(-)

diff --git a/README.md b/README.md
index e00d592..4a1abdf 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,7 @@
-# Setup
-1. DAP_Setup()
-2. Server begin 
+# Attention
 
-# Loop
\ No newline at end of file
+At present, the adaptation to WCID, WinUSB, etc. has all been completed. However, when transmitting data on the endpoint, we received an error message from USBIP. This is most likely a problem with the USBIP project itself.
+
+Due to the completeness of the USBIP protocol document, we have not yet understood its role in the Bulk transmission process, which may also lead to errors in subsequent processes.
+
+We will continue to try to make it work under USB HID. Once the USBIP problem is solved, we will immediately transfer it to work under WinUSB
\ No newline at end of file
diff --git a/components/DAP/config/DAP_config.h b/components/DAP/config/DAP_config.h
index fccf7d3..a61956e 100644
--- a/components/DAP/config/DAP_config.h
+++ b/components/DAP/config/DAP_config.h
@@ -29,6 +29,7 @@
 #define __DAP_CONFIG_H__
 
 #include <stdint.h>
+#include <string.h>
 #include "cmsis_compiler.h"
 #include "gpio.h"
 #include "gpio_struct.h"
@@ -153,8 +154,9 @@ __STATIC_INLINE uint8_t DAP_GetVendorString(char *str)
   ////TODO: fill this
   // In fact, Keil can get the corresponding information through USB
   // without filling in this information.
-  (void)str;
-  return (0U);
+  // (void)str;
+  strcpy(str, "windowsair");
+  return (sizeof("windowsair"));
 }
 
 /**
@@ -165,8 +167,9 @@ __STATIC_INLINE uint8_t DAP_GetVendorString(char *str)
  */
 __STATIC_INLINE uint8_t DAP_GetProductString(char *str)
 {
-  (void)str;
-  return (0U);
+  //(void)str;
+    strcpy(str, "CMSIS-DAP v2");
+  return (sizeof("CMSIS-DAP v2"));
 }
 
 /**
@@ -177,8 +180,8 @@ __STATIC_INLINE uint8_t DAP_GetProductString(char *str)
  */
 __STATIC_INLINE uint8_t DAP_GetSerNumString(char *str)
 {
-  (void)str;
-  return (0U);
+      strcpy(str, "1234");
+  return (sizeof("1234"));
 }
 
 ///@}
diff --git a/components/README.md b/components/README.md
index e52e5e9..3fc0da1 100644
--- a/components/README.md
+++ b/components/README.md
@@ -1,4 +1,2 @@
 # TODO 
-1. adapt to special hardware
-2. USB
-3. Significant Bit is difficult to judge, may need to dive into the details of usbip
\ No newline at end of file
+1. USB
\ No newline at end of file
diff --git a/components/USBIP/MSOS20Descriptors.c b/components/USBIP/MSOS20Descriptors.c
index 4337076..6621557 100644
--- a/components/USBIP/MSOS20Descriptors.c
+++ b/components/USBIP/MSOS20Descriptors.c
@@ -1,84 +1,84 @@
-/**
- * @file MSOS20Descriptors.c
- * @author windowsair
- * @brief Store related data of Microsoft OS 2.0 descriptor
- * @version 0.1
- * @date 2019-11-21
- * 
- * @copyright Copyright (c) 2019
- * 
- */
-
- ////TODO: refactoring into structure
-
-#include <stdint.h>
-#include "MSOS20Descriptors.h"
-
-#define USBShort(ui16Value)     ((ui16Value) & 0xff), ((ui16Value) >> 8) //((ui16Value) & 0xFF),(((ui16Value) >> 8) & 0xFF)
-
-
-
-// Microsoft OS 2.0 descriptor set header
-const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20] =
-{
-    // Microsoft OS 2.0 Descriptor Set header (Table 10)
-    0x0A, 0x00,  // wLength (Shall be set to 0x0A)
-    MS_OS_20_SET_HEADER_DESCRIPTOR, 0x00,
-    0x00, 0x00, 0x03, 0x06,  // dwWindowsVersion: Windows 8.1 (NTDDI_WINBLUE)
-    USBShort(kLengthOfMsOS20),  // wTotalLength
-
-    // Support WinUSB
-    // See https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/automatic-installation-of-winusb
-    
-    // Microsoft OS 2.0 compatible ID descriptor (Table 13)
-    0x14, 0x00,                                      // wLength
-    USBShort(MS_OS_20_FEATURE_COMPATIBLE_ID),        // wDescriptorType
-    'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00,        // compatibleID
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // subCompatibleID
-
-    // Microsoft OS 2.0 registry property descriptor (Table 14)
-    0x84, 0x00,                                      // wLength
-    USBShort(MS_OS_20_FEATURE_REG_PROPERTY),
-    0x07, 0x00,                                      // wPropertyDataType: REG_MULTI_SZ (Unicode Strings)
-    0x2A, 0x00,                                      // wPropertyNameLength
-    'D',0,'e',0,'v',0,'i',0,'c',0,'e',0,'I',0,'n',0,'t',0,'e',0,'r',0,
-    'f',0,'a',0,'c',0,'e',0,'G',0,'U',0,'I',0,'D',0,'s',0,0,0, 
-                                                     // Set to "DeviceInterfaceGUID" to support WinUSB
-    0x50, 0x00,                                      // wPropertyDataLength
-                                                     // WinUSB GUID
-    '{',0,'C',0,'D',0,'B',0,'3',0,'B',0,'5',0,'A',0,'D',0,'-',0,  
-    '2',0,'9',0,'3',0,'B',0,'-',0,'4',0,'6',0,'6',0,'3',0,'-',0,
-    'A',0,'A',0,'3',0,'6',0,'-',0,'1',0,'A',0,'A',0,'E',0,'4',0,
-    '6',0,'4',0,'6',0,'3',0,'7',0,'7',0,'6',0,'}',0,0,0,0,0,
-    // identify a CMSIS-DAP V2 configuration,
-    // must set to "{CDB3B5AD-293B-4663-AA36-1AAE46463776}"
-    
-};
-
-const uint8_t bosDescriptor[kLengthOfBos] =
-{
-    // Universal Serial Bus 3.0 Specification, Table 9-9.
-    0x05,                       // bLength of this descriptor
-    USB_DESCRIPTOR_TYPE_BOS,    // BOS Descriptor type(Constant)
-    USBShort(kLengthOfBos),     // wLength
-    0x01,                       // bNumDeviceCaps
-    
-    // Microsoft OS 2.0 platform capability descriptor header (Table 4)
-    // See also:
-    // Universal Serial Bus 3.0 Specification : Format of a Device Capability Descriptor, Table 9-10.
-
-    0x1C,       // bLength of this first device capability descriptor
-                // bLength -> The total length of the remaining arrays containing this field
-    USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // bDescriptorType
-    USB_DEVICE_CAPABILITY_TYPE_PLATFORM, // bDevCapabilityType
-
-    // Capability-Dependent (See USB3.0 Specification Table 9-10.)
-    0x00,                       // bReserved
-    USB_DEVICE_CAPABILITY_UUID, // MS_OS_20_Platform_Capability_ID
-
-    0x00, 0x00, 0x03, 0x06,     // dwWindowsVersion: Windows 8.1 (NTDDI_WINBLUE)
-    USBShort(kLengthOfMsOS20),  // wMSOSDescriptorSetTotalLength(length of descriptor set header)
-    kValueOfbMS_VendorCode,     // bMS_VendorCode (0x01 will be ok)
-                                ////TODO:change this
-    0,                          // bAltEnumCode
+/**
+ * @file MSOS20Descriptors.c
+ * @author windowsair
+ * @brief Store related data of Microsoft OS 2.0 descriptor
+ * @version 0.1
+ * @date 2019-11-21
+ * 
+ * @copyright Copyright (c) 2019
+ * 
+ */
+
+ ////TODO: refactoring into structure
+
+#include <stdint.h>
+#include "MSOS20Descriptors.h"
+
+#define USBShort(ui16Value)     ((ui16Value) & 0xff), ((ui16Value) >> 8) //((ui16Value) & 0xFF),(((ui16Value) >> 8) & 0xFF)
+
+
+
+// Microsoft OS 2.0 descriptor set header
+const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20] =
+{
+    // Microsoft OS 2.0 Descriptor Set header (Table 10)
+    0x0A, 0x00,  // wLength (Shall be set to 0x0A)
+    MS_OS_20_SET_HEADER_DESCRIPTOR, 0x00,
+    0x00, 0x00, 0x03, 0x06,  // dwWindowsVersion: Windows 8.1 (NTDDI_WINBLUE)
+    USBShort(kLengthOfMsOS20),  // wTotalLength
+
+    // Support WinUSB
+    // See https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/automatic-installation-of-winusb
+    
+    // Microsoft OS 2.0 compatible ID descriptor (Table 13)
+    0x14, 0x00,                                      // wLength
+    USBShort(MS_OS_20_FEATURE_COMPATIBLE_ID),        // wDescriptorType
+    'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00,        // compatibleID
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // subCompatibleID
+
+    // Microsoft OS 2.0 registry property descriptor (Table 14)
+    0x84, 0x00,                                      // wLength
+    USBShort(MS_OS_20_FEATURE_REG_PROPERTY),
+    0x07, 0x00,                                      // wPropertyDataType: REG_MULTI_SZ (Unicode Strings)
+    0x2A, 0x00,                                      // wPropertyNameLength
+    'D',0,'e',0,'v',0,'i',0,'c',0,'e',0,'I',0,'n',0,'t',0,'e',0,'r',0,
+    'f',0,'a',0,'c',0,'e',0,'G',0,'U',0,'I',0,'D',0,'s',0,0,0, 
+                                                     // Set to "DeviceInterfaceGUID" to support WinUSB
+    0x50, 0x00,                                      // wPropertyDataLength
+                                                     // WinUSB GUID
+    '{',0,'C',0,'D',0,'B',0,'3',0,'B',0,'5',0,'A',0,'D',0,'-',0,  
+    '2',0,'9',0,'3',0,'B',0,'-',0,'4',0,'6',0,'6',0,'3',0,'-',0,
+    'A',0,'A',0,'3',0,'6',0,'-',0,'1',0,'A',0,'A',0,'E',0,'4',0,
+    '6',0,'4',0,'6',0,'3',0,'7',0,'7',0,'6',0,'}',0,0,0,0,0,
+    // identify a CMSIS-DAP V2 configuration,
+    // must set to "{CDB3B5AD-293B-4663-AA36-1AAE46463776}"
+    
+};
+
+const uint8_t bosDescriptor[kLengthOfBos] =
+{
+    // Universal Serial Bus 3.0 Specification, Table 9-9.
+    0x05,                       // bLength of this descriptor
+    USB_DESCRIPTOR_TYPE_BOS,    // BOS Descriptor type(Constant)
+    USBShort(kLengthOfBos),     // wLength
+    0x01,                       // bNumDeviceCaps
+    
+    // Microsoft OS 2.0 platform capability descriptor header (Table 4)
+    // See also:
+    // Universal Serial Bus 3.0 Specification : Format of a Device Capability Descriptor, Table 9-10.
+
+    0x1C,       // bLength of this first device capability descriptor
+                // bLength -> The total length of the remaining arrays containing this field
+    USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // bDescriptorType
+    USB_DEVICE_CAPABILITY_TYPE_PLATFORM, // bDevCapabilityType
+
+    // Capability-Dependent (See USB3.0 Specification Table 9-10.)
+    0x00,                       // bReserved
+    USB_DEVICE_CAPABILITY_UUID, // MS_OS_20_Platform_Capability_ID
+
+    0x00, 0x00, 0x03, 0x06,     // dwWindowsVersion: Windows 8.1 (NTDDI_WINBLUE)
+    USBShort(kLengthOfMsOS20),  // wMSOSDescriptorSetTotalLength(length of descriptor set header)
+    kValueOfbMS_VendorCode,     // bMS_VendorCode (0x01 will be ok)
+                                ////TODO:change this
+    0,                          // bAltEnumCode
 };
\ No newline at end of file
diff --git a/components/USBIP/MSOS20Descriptors.h b/components/USBIP/MSOS20Descriptors.h
index 61a7ea3..51bbcb1 100644
--- a/components/USBIP/MSOS20Descriptors.h
+++ b/components/USBIP/MSOS20Descriptors.h
@@ -1,81 +1,81 @@
-/**
- * @file MSOS20Descriptors.h
- * @author windowsair
- * @brief 
- * @version 0.1
- * @date 2019-11-21
- * 
- * @copyright Copyright (c) 2019
- * 
- */
-
-#ifndef __MSOS20DESCRIPTORS_H__
-#define __MSOS20DESCRIPTORS_H__
-
-#define kLengthOfMsOS20 0xA2
-#define kLengthOfBos 0x21
-#define kValueOfbMS_VendorCode  0x01// Just set to 0x01
-extern const uint8_t bosDescriptor[kLengthOfBos];
-extern const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20];
-
-/* Microsoft OS 2.0 Descriptors BEGIN */
-
-// Platform capability BOS descriptor, Table 1.
-#define USB_DEVICE_CAPABILITY_TYPE_PLATFORM 5
-
-// Platform capability UUID,  Table 3.
-// {D8DD60DF-4589-4CC7-9CD2-659D9E648A9F}
-#define USB_DEVICE_CAPABILITY_UUID  0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, 0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F
-
-
-// Microsoft OS 2.0 descriptor wIndex values enum, Table 8.
-#define MS_OS_20_DESCRIPTOR_INDEX 7
-#define MS_OS_20_SET_ALT_ENUMERATION 8
-
-
-// Microsoft OS 2.0 descriptor types enum for wDescriptorType values, Table 9.
-#define MS_OS_20_SET_HEADER_DESCRIPTOR 0x00
-#define MS_OS_20_SUBSET_HEADER_CONFIGURATION 0x01
-#define MS_OS_20_SUBSET_HEADER_FUNCTION 0x02
-#define MS_OS_20_FEATURE_COMPATIBLE_ID 0x03
-#define MS_OS_20_FEATURE_REG_PROPERTY 0x04
-#define MS_OS_20_FEATURE_MIN_RESUME_TIME 0x05
-#define MS_OS_20_FEATURE_MODEL_ID 0x06
-#define MS_OS_20_FEATURE_CCGP_DEVICE 0x07
-
-/* Microsoft OS 2.0 Descriptors END */
-
-
-
-/* Wireless USB Standard Extension Descriptor Types BEGIN */
-
-// Wireless USB Specification 1.1 revison 1.1, Table 7-21.
-#define USB_DESCRIPTOR_TYPE_SECURITY 12
-#define USB_DESCRIPTOR_TYPE_KEY 13
-#define USB_DESCRIPTOR_TYPE_ENCRYPTION_TYPE 14
-#define USB_DESCRIPTOR_TYPE_BOS 15
-#define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY 16
-#define USB_DESCRIPTOR_TYPE_WIRELESS_ENDPOINT_COMPANION 17
-
-/* Wireless USB Standard Extension Descriptor Types END */
-
-
-/* Microsoft Extended Compat ID OS Feature Descriptor BEGIN */
-
-#define USB_MS_EXTENDED_COMPAT_ID_VERSION 0x0100
-#define USB_MS_EXTENDED_COMPAT_ID_TYPE    0x04
-
-#define USB_COMPATID_NONE                {0}
-#define USB_SUBCOMPATID_NONE             {0}
-#define USB_COMPATID_WINUSB              "WINUSB\0"
-#define USB_COMPATID_RNDIS               "RNDIS\0\0"
-#define USB_COMPATID_PTP                 "PTP\0\0\0\0"
-#define USB_COMPATID_MTP                 "MTP\0\0\0\0"
-#define USB_COMPATID_BLUETOOTH           "BLUTUTH"
-#define USB_SUBCOMPATID_BT_V11           "11\0\0\0\0\0"
-#define USB_SUBCOMPATID_BT_V12           "12\0\0\0\0\0"
-#define USB_SUBCOMPATID_BT_V20EDR        "EDR\0\0\0\0"
-
-/* Microsoft Extended Compat ID OS Feature Descriptor END */
-
+/**
+ * @file MSOS20Descriptors.h
+ * @author windowsair
+ * @brief 
+ * @version 0.1
+ * @date 2019-11-21
+ * 
+ * @copyright Copyright (c) 2019
+ * 
+ */
+
+#ifndef __MSOS20DESCRIPTORS_H__
+#define __MSOS20DESCRIPTORS_H__
+
+#define kLengthOfMsOS20 0xA2
+#define kLengthOfBos 0x21
+#define kValueOfbMS_VendorCode  0x01// Just set to 0x01
+extern const uint8_t bosDescriptor[kLengthOfBos];
+extern const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20];
+
+/* Microsoft OS 2.0 Descriptors BEGIN */
+
+// Platform capability BOS descriptor, Table 1.
+#define USB_DEVICE_CAPABILITY_TYPE_PLATFORM 5
+
+// Platform capability UUID,  Table 3.
+// {D8DD60DF-4589-4CC7-9CD2-659D9E648A9F}
+#define USB_DEVICE_CAPABILITY_UUID  0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, 0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F
+
+
+// Microsoft OS 2.0 descriptor wIndex values enum, Table 8.
+#define MS_OS_20_DESCRIPTOR_INDEX 7
+#define MS_OS_20_SET_ALT_ENUMERATION 8
+
+
+// Microsoft OS 2.0 descriptor types enum for wDescriptorType values, Table 9.
+#define MS_OS_20_SET_HEADER_DESCRIPTOR 0x00
+#define MS_OS_20_SUBSET_HEADER_CONFIGURATION 0x01
+#define MS_OS_20_SUBSET_HEADER_FUNCTION 0x02
+#define MS_OS_20_FEATURE_COMPATIBLE_ID 0x03
+#define MS_OS_20_FEATURE_REG_PROPERTY 0x04
+#define MS_OS_20_FEATURE_MIN_RESUME_TIME 0x05
+#define MS_OS_20_FEATURE_MODEL_ID 0x06
+#define MS_OS_20_FEATURE_CCGP_DEVICE 0x07
+
+/* Microsoft OS 2.0 Descriptors END */
+
+
+
+/* Wireless USB Standard Extension Descriptor Types BEGIN */
+
+// Wireless USB Specification 1.1 revison 1.1, Table 7-21.
+#define USB_DESCRIPTOR_TYPE_SECURITY 12
+#define USB_DESCRIPTOR_TYPE_KEY 13
+#define USB_DESCRIPTOR_TYPE_ENCRYPTION_TYPE 14
+#define USB_DESCRIPTOR_TYPE_BOS 15
+#define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY 16
+#define USB_DESCRIPTOR_TYPE_WIRELESS_ENDPOINT_COMPANION 17
+
+/* Wireless USB Standard Extension Descriptor Types END */
+
+
+/* Microsoft Extended Compat ID OS Feature Descriptor BEGIN */
+
+#define USB_MS_EXTENDED_COMPAT_ID_VERSION 0x0100
+#define USB_MS_EXTENDED_COMPAT_ID_TYPE    0x04
+
+#define USB_COMPATID_NONE                {0}
+#define USB_SUBCOMPATID_NONE             {0}
+#define USB_COMPATID_WINUSB              "WINUSB\0"
+#define USB_COMPATID_RNDIS               "RNDIS\0\0"
+#define USB_COMPATID_PTP                 "PTP\0\0\0\0"
+#define USB_COMPATID_MTP                 "MTP\0\0\0\0"
+#define USB_COMPATID_BLUETOOTH           "BLUTUTH"
+#define USB_SUBCOMPATID_BT_V11           "11\0\0\0\0\0"
+#define USB_SUBCOMPATID_BT_V12           "12\0\0\0\0\0"
+#define USB_SUBCOMPATID_BT_V20EDR        "EDR\0\0\0\0"
+
+/* Microsoft Extended Compat ID OS Feature Descriptor END */
+
 #endif
\ No newline at end of file
diff --git a/components/USBIP/USB_handle.c b/components/USBIP/USB_handle.c
index a6dd0ce..666d026 100644
--- a/components/USBIP/USB_handle.c
+++ b/components/USBIP/USB_handle.c
@@ -19,6 +19,12 @@
 #include "usb_defs.h"
 #include "MSOS20Descriptors.h"
 
+const char *strings_list[] = {
+    0, // reserved: available languages
+    "windowsair",
+    "CMSIS-DAP v2",
+    "1234",
+};
 // handle functions
 static void handleGetDescriptor(usbip_stage2_header *header);
 
@@ -192,7 +198,7 @@ static void handleGetDescriptor(usbip_stage2_header *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));
+        send_stage2_submit_data(header, 0, &kUSBd0DeviceDescriptor[0], sizeof(kUSBd0DeviceDescriptor));
         break;
 
     case USB_DT_CONFIGURATION: // get configuration descriptor
@@ -223,9 +229,29 @@ static void handleGetDescriptor(usbip_stage2_header *header)
             os_printf("** REQUESTED list of supported languages\r\n");
             send_stage2_submit_data(header, 0, kLangDescriptor, sizeof(kLangDescriptor));
         }
+        else if (header->u.cmd_submit.request.wValue.u8lo != 0xee)
+        {
+            os_printf("low bit : %d\r\n", (int)header->u.cmd_submit.request.wValue.u8lo);
+            os_printf("high bit : %d\r\n", (int)header->u.cmd_submit.request.wValue.u8hi);
+            int slen = strlen(strings_list[header->u.cmd_submit.request.wValue.u8lo]);
+            int wslen = slen * 2;
+            int buff_len = sizeof(usb_string_descriptor) + wslen;
+            char temp_buff[256];
+            usb_string_descriptor *desc = (usb_string_descriptor *)temp_buff;
+            desc->bLength = buff_len;
+            desc->bDescriptorType = USB_DT_STRING;
+            for (int i = 0; i < slen; i++)
+            {
+                desc->wData[i] = strings_list[header->u.cmd_submit.request.wValue.u8lo][i];
+                send_stage2_submit_data(header, 0, (uint8_t *)temp_buff, buff_len);
+            }
+        }
         else
         {
-            os_printf("***Unsupported operation***\r\n");
+            os_printf("low bit : %d\r\n", (int)header->u.cmd_submit.request.wValue.u8lo);
+            os_printf("high bit : %d\r\n", (int)header->u.cmd_submit.request.wValue.u8hi);
+            os_printf("***Unsupported String descriptor***\r\n");
+            
         }
         break;
 
@@ -242,7 +268,7 @@ static void handleGetDescriptor(usbip_stage2_header *header)
         break;
 
     case USB_DT_DEVICE_QUALIFIER:
-        os_printf("* GET 0x06 DEVICE QUALIFIER DESCRIPTOR");
+        os_printf("* GET 0x06 DEVICE QUALIFIER DESCRIPTOR\r\n");
 
         usb_device_qualifier_descriptor desc;
 
@@ -252,13 +278,13 @@ static void handleGetDescriptor(usbip_stage2_header *header)
         break;
 
     case USB_DT_OTHER_SPEED_CONFIGURATION:
-        os_printf("GET 0x07 [UNIMPLEMENTED] USB_DT_OTHER_SPEED_CONFIGURATION");
+        os_printf("GET 0x07 [UNIMPLEMENTED] USB_DT_OTHER_SPEED_CONFIGURATION\r\n");
         ////TODO:UNIMPLEMENTED
         send_stage2_submit(header, 0, 0);
         break;
 
     case USB_DT_INTERFACE_POWER:
-        os_printf("GET 0x08 [UNIMPLEMENTED] USB_DT_INTERFACE_POWER");
+        os_printf("GET 0x08 [UNIMPLEMENTED] USB_DT_INTERFACE_POWER\r\n");
         ////TODO:UNIMPLEMENTED
         send_stage2_submit(header, 0, 0);
         break;
@@ -268,7 +294,7 @@ static void handleGetDescriptor(usbip_stage2_header *header)
         send_stage2_submit_data(header, 0, bosDescriptor, sizeof(bosDescriptor));
         break;
     default:
-        os_printf("USB unknown Get Descriptor requested:%d", header->u.cmd_submit.request.wValue.u8lo);
+        os_printf("USB unknown Get Descriptor requested:%d\r\n", header->u.cmd_submit.request.wValue.u8lo);
         break;
     }
 }
diff --git a/components/USBIP/USBd_config.c b/components/USBIP/USBd_config.c
index c266c16..f28d636 100644
--- a/components/USBIP/USBd_config.c
+++ b/components/USBIP/USBd_config.c
@@ -25,7 +25,7 @@
 const uint8_t kUSBd0DeviceDescriptor[0x12] =
 {
     0x12,             // bLength
-    USB_DT_DEVICE, // bDescriptorType (constant)
+    USB_DT_DEVICE,    // bDescriptorType
     USBShort(0x0210), // bcdUSB 
     ////TODO: Is it also available elsewhere?
 
@@ -34,14 +34,14 @@ const uint8_t kUSBd0DeviceDescriptor[0x12] =
     0x00, // bDeviceSubClass
     0x00, // bDeviceProtocol
     
-    USBD0_MAX_PACKET0,                  // Maximum packet size for default pipe.
-    USBShort(USBD0_DEV_DESC_IDVENDOR),  // Vendor ID (VID).
-    USBShort(USBD0_DEV_DESC_IDPRODUCT), // Product ID (PID).
-    USBShort(USBD0_DEV_DESC_BCDDEVICE), // Device Version BCD.
-    0x01,                               // Index of Manufacturer string identifier.
-    0x02,                               // Index of Product string identifier.
-    0x03 * USBD0_STR_DESC_SER_EN,       // Index of Product serial number.
-    0x01                                // Number of configurations.
+    USBD0_MAX_PACKET0,                  // bMaxPacketSize0 Maximum packet size for default pipe.
+    USBShort(USBD0_DEV_DESC_IDVENDOR),  // idVendor Vendor ID (VID).
+    USBShort(USBD0_DEV_DESC_IDPRODUCT), // idProduct Product ID (PID).
+    USBShort(USBD0_DEV_DESC_BCDDEVICE), // bcdDevice Device Version BCD.
+    0x01,                               // iManufacturer Index of Manufacturer string identifier.
+    0x02,                               // iProduct Index of Product string identifier.
+    0x03 * USBD0_STR_DESC_SER_EN,       // iSerialNumber Index of Product serial number.
+    0x01                                // bNumConfigurations Number of configurations.
 };
 
 
@@ -60,7 +60,7 @@ const uint8_t kUSBd0InterfaceDescriptor[0x1E]=
     USB_DT_INTERFACE,                       // bDescriptorType
     USBD_CUSTOM_CLASS0_IF0_NUM,             // bInterfaceNumber                                    
     USBD_CUSTOM_CLASS0_IF0_ALT,             // bAlternateSetting                                     
-    0x03,                                   // bNumEndpoints(we will use three endpoints)
+    0x03,                                   // bNumEndpoints(we will use 3 endpoints)
                                             // 
     USBD_CUSTOM_CLASS0_IF0_CLASS,           // bInterfaceClass                                      
     USBD_CUSTOM_CLASS0_IF0_SUBCLASS,        // bInterfaceSubClass                                    
@@ -91,7 +91,7 @@ const uint8_t kUSBd0InterfaceDescriptor[0x1E]=
     USB_DT_ENDPOINT,                                           // bDescriptorType                                      
     0x01,                                                      // bEndpointAddress
     USB_ENDPOINT_ATTR_BULK,                                    // bmAttributes           
-    USBShort(512),    // wMaxPacketSize   
+    USBShort(512),                                             // wMaxPacketSize   
     // We assume that it always runs in High Speed.
     0x00, // bInterval 
 
@@ -102,16 +102,17 @@ const uint8_t kUSBd0InterfaceDescriptor[0x1E]=
     USB_DT_ENDPOINT,                                           // bDescriptorType                                      
     0x81,                                                      // bEndpointAddress
     USB_ENDPOINT_ATTR_BULK,                                    // bmAttributes           
-    USBShort(512),    // wMaxPacketSize   
-    0x00, // bInterval 
+    USBShort(512),                                             // wMaxPacketSize   
+    0x00,                                                      // bInterval 
 
     /*                 Pysical endpoint 2                */
+    
     // "Endpoint 3: Bulk In (optional) – used for streaming SWO trace" Device -> PC
     0x07,                                                      // bLength                                          
     USB_DT_ENDPOINT,                                           // bDescriptorType                                      
     0x82,                                                      // bEndpointAddress
     USB_ENDPOINT_ATTR_BULK,                                    // bmAttributes           
-    USBShort(512),    // wMaxPacketSize   
+    USBShort(512),                                             // wMaxPacketSize   
     0x00,                                                      // bInterval 
 
 
@@ -123,9 +124,8 @@ const uint8_t kUSBd0ConfigDescriptor[LENGTHOFCONFIGDESCRIPTOR] =
 {
     // Configuration descriptor header.
 
-    0x09,            // bLength -> 0x09?? may be ok... 1-23
-    0x03,                                   // bDescriptorType 
-                                            // constant, set to 0x03
+    0x09,                                   // bLength
+    USB_DT_CONFIGURATION,                   // bDescriptorType 
                                        
     USBShort((sizeof(kUSBd0InterfaceDescriptor)) + (LENGTHOFCONFIGDESCRIPTOR)),  
                                             // wTotalLength
diff --git a/main/DAP_handle.c b/main/DAP_handle.c
index 02975b9..d80e4e3 100644
--- a/main/DAP_handle.c
+++ b/main/DAP_handle.c
@@ -12,7 +12,7 @@
 #include "usbip_server.h"
 #include "DAP_handle.h"
 #include "DAP.h"
-
+#include "esp_libc.h"
 ////TODO: Merge this
 #define DAP_PACKET_SIZE 512
 
@@ -30,16 +30,18 @@ void handle_dap_data_request(usbip_stage2_header *header)
     data_in = &(data_in[sizeof(usbip_stage2_header)]);
     // Point to the beginning of the URB packet
     dap_respond = DAP_ProcessCommand((uint8_t *)data_in, (uint8_t *)data_out);
-    send_stage2_submit(header, 0, 0);
+    handle_dap_data_response(header);
+    //send_stage2_submit(header, 0, 0);
 }
 void handle_dap_data_response(usbip_stage2_header *header)
 {
     if (dap_respond)
     {
-        dap_respond = 0;
-        //os_printf("*** Will respond");
-        send_stage2_submit_data(header, 0, data_out, DAP_PACKET_SIZE);
 
+        //os_printf("*** Will respond");
+
+        send_stage2_submit_data(header, 0, data_out, (dap_respond & 0x0000FFFF));
+        dap_respond = 0;
         //os_printf("*** RESPONDED ***");
     }
     else
@@ -66,18 +68,19 @@ void handle_swo_trace_response(usbip_stage2_header *header)
     }
 }
 
-
 // SWO Data Queue Transfer
 //   buf:    pointer to buffer with data
 //   num:    number of bytes to transfer
-void SWO_QueueTransfer (uint8_t *buf, uint32_t num) {
-  swo_data_to_send = buf;
-  num_swo_data = num;
-  swo_trace_respond = 1;
+void SWO_QueueTransfer(uint8_t *buf, uint32_t num)
+{
+    swo_data_to_send = buf;
+    num_swo_data = num;
+    swo_trace_respond = 1;
 }
 
 // SWO Data Abort Transfer
-void SWO_AbortTransfer (void) {
-  //USBD_EndpointAbort(0U, USB_ENDPOINT_IN(2U));
-  ////TODO: unlink might be useful ...
+void SWO_AbortTransfer(void)
+{
+    //USBD_EndpointAbort(0U, USB_ENDPOINT_IN(2U));
+    ////TODO: unlink might be useful ...
 }
\ No newline at end of file
diff --git a/main/main.c b/main/main.c
index b658cec..5676c15 100644
--- a/main/main.c
+++ b/main/main.c
@@ -34,6 +34,7 @@
 
 extern void SWO_Thread(void *argument);
 extern void usart_monitor_task(void *argument); 
+extern void DAP_Setup(void);
 
 /* FreeRTOS event group to signal when we are connected & ready to make a request */
 static EventGroupHandle_t wifi_event_group;
@@ -136,6 +137,7 @@ void app_main()
     ESP_ERROR_CHECK(nvs_flash_init());
     initialise_wifi();
     wait_for_ip();
+    DAP_Setup();                          // DAP Setup 
     
     xTaskCreate(timer_create_task, "timer_create", 512, NULL, 10, NULL);
     xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 5, NULL);
diff --git a/main/tcp_server.c b/main/tcp_server.c
index 5031600..fbb16b7 100644
--- a/main/tcp_server.c
+++ b/main/tcp_server.c
@@ -65,7 +65,7 @@ void tcp_server_task(void *pvParameters)
             os_printf("Unable to create socket: errno %d\r\n", errno);
             break;
         }
-        os_printf("Socket created");
+        os_printf("Socket created\r\n");
 
         int err = bind(listen_sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
         if (err != 0)
@@ -73,7 +73,7 @@ void tcp_server_task(void *pvParameters)
             os_printf("Socket unable to bind: errno %d\r\n", errno);
             break;
         }
-        os_printf("Socket binded");
+        os_printf("Socket binded\r\n");
 
         err = listen(listen_sock, 1);
         if (err != 0)
@@ -81,7 +81,7 @@ void tcp_server_task(void *pvParameters)
             os_printf("Error occured during listen: errno %d\r\n", errno);
             break;
         }
-        os_printf("Socket listening");
+        os_printf("Socket listening\r\n");
 
 #ifdef CONFIG_EXAMPLE_IPV6
         struct sockaddr_in6 sourceAddr; // Large enough for both IPv4 or IPv6
@@ -89,82 +89,76 @@ void tcp_server_task(void *pvParameters)
         struct sockaddr_in sourceAddr;
 #endif
         uint32_t addrLen = sizeof(sourceAddr);
-        kSock = accept(listen_sock, (struct sockaddr *)&sourceAddr, &addrLen);
-        if (kSock < 0)
-        {
-            os_printf("Unable to accept connection: errno %d\r\n", errno);
-            break;
-        }
-        os_printf("Socket accepted");
-
         while (1)
         {
-            int len = recv(kSock, rx_buffer, 2047, 0);
-            // Error occured during receiving
-            if (len < 0)
+            kSock = accept(listen_sock, (struct sockaddr *)&sourceAddr, &addrLen);
+            if (kSock < 0)
             {
-                os_printf("recv failed: errno %d\r\n", errno);
+                os_printf("Unable to accept connection: errno %d\r\n", errno);
                 break;
             }
-            // Connection closed
-            else if (len == 0)
-            {
-                os_printf("Connection closed\r\n");
-                break;
-            }
-            // Data received
-            else
+            os_printf("Socket accepted\r\n");
+
+            while (1)
             {
+                int len = recv(kSock, rx_buffer, 2047, 0);
+                // Error occured during receiving
+                if (len < 0)
+                {
+                    os_printf("recv failed: errno %d\r\n", errno);
+                    break;
+                }
+                // Connection closed
+                else if (len == 0)
+                {
+                    os_printf("Connection closed\r\n");
+                    break;
+                }
+                // Data received
+                else
+                {
 #ifdef CONFIG_EXAMPLE_IPV6
-                // Get the sender's ip address as string
-                if (sourceAddr.sin6_family == PF_INET)
-                {
-                    inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
-                }
-                else if (sourceAddr.sin6_family == PF_INET6)
-                {
-                    inet6_ntoa_r(sourceAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
-                }
+                    // Get the sender's ip address as string
+                    if (sourceAddr.sin6_family == PF_INET)
+                    {
+                        inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
+                    }
+                    else if (sourceAddr.sin6_family == PF_INET6)
+                    {
+                        inet6_ntoa_r(sourceAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
+                    }
 #else
-                inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
+                    inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
 #endif
 
-                rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
-                //os_printf("Received %d bytes from %s:\r\n", len, addr_str);
-                // os_printf("%s", rx_buffer);
-                switch (kState)
-                {
-                case ACCEPTING:
-                    kState = ATTACHING;
-                    break;
+                    switch (kState)
+                    {
+                    case ACCEPTING:
+                        kState = ATTACHING;
 
-                case ATTACHING:
-                    attach(rx_buffer, len);
-                    break;
+                    case ATTACHING:
+                        attach(rx_buffer, len);
+                        break;
 
-                case EMULATING:
-                    emulate(rx_buffer, len);
-                    break;
+                    case EMULATING:
+                        emulate(rx_buffer, len);
+                        break;
+                    default:
+                        os_printf("unkonw kstate!\r\n");
+                    }
                 }
-
-                // int err = send(sock, rx_buffer, len, 0);
-                // if (err < 0)
-                // {
-                //     os_printf("Error occured during sending: errno %d\r\n", errno);
-                //     break;
-                // }
             }
-        }
-        kState = ACCEPTING;
-        if (kSock != -1)
-        {
-            os_printf("Shutting down socket and restarting...\r\n");
-            shutdown(kSock, 0);
-            close(kSock);
+            // kState = ACCEPTING;
+            if (kSock != -1)
+            {
+                os_printf("Shutting down socket and restarting...\r\n");
+                //shutdown(kSock, 0);
+                close(kSock);
 
-            shutdown(listen_sock, 0);
-            close(listen_sock);
-            vTaskDelay(5);
+                //shutdown(listen_sock, 0);
+                //close(listen_sock);
+                //vTaskDelay(5);
+            }
         }
     }
     vTaskDelete(NULL);
diff --git a/main/usbip_server.c b/main/usbip_server.c
index cf87a53..f9ad467 100644
--- a/main/usbip_server.c
+++ b/main/usbip_server.c
@@ -53,7 +53,7 @@ int attach(uint8_t *buffer, uint32_t length)
         os_printf("attach Unknown command: %d\r\n", command);
         break;
     }
-    return 0; ////TODO: ...
+    return 0;
 }
 
 static int read_stage1_command(uint8_t *buffer, uint32_t length)
@@ -130,16 +130,15 @@ static void send_device_list()
 
 static void send_device_info()
 {
-    os_printf("Sending device info...");
+    os_printf("Sending device info...\r\n");
     usbip_stage1_usb_device device;
 
     strcpy(device.path, "/sys/devices/pci0000:00/0000:00:01.2/usb1/1-1");
     strcpy(device.busid, "1-1");
 
     device.busnum = htonl(1);
-    device.devnum = htonl(2);
-    device.speed = htonl(2); // what is this???
-    //// TODO: 0200H for USB2.0
+    device.devnum = htonl(1);
+    device.speed = htonl(3); // See usb_device_speed enum
 
     device.idVendor = htons(USBD0_DEV_DESC_IDVENDOR);
     device.idProduct = htons(USBD0_DEV_DESC_IDPRODUCT);
@@ -183,7 +182,7 @@ int emulate(uint8_t *buffer, uint32_t length)
     int command = read_stage2_command((usbip_stage2_header *)buffer, length);
     if (command < 0)
     {
-        return -1;
+        return -1;     
     }
 
     switch (command)
@@ -211,7 +210,7 @@ static int read_stage2_command(usbip_stage2_header *header, uint32_t length)
     }
 
     //client.readBytes((uint8_t *)&header, sizeof(usbip_stage2_header));
-    unpack((uint32_t *)&header, sizeof(usbip_stage2_header));
+    unpack((uint32_t *)header, sizeof(usbip_stage2_header));
     return header->base.command;
 }
 
@@ -263,7 +262,7 @@ static void unpack(void *data, int size)
 }
 
 /**
- * @brief 
+ * @brief USB transaction processing
  *
  */
 static int handle_submit(usbip_stage2_header *header)
@@ -324,18 +323,18 @@ void send_stage2_submit(usbip_stage2_header *req_header, int32_t status, int32_t
 {
 
     req_header->base.command = USBIP_STAGE2_RSP_SUBMIT;
-    req_header->base.direction = !req_header->base.direction;
+    req_header->base.direction = !(req_header->base.direction);
 
-    memset(&req_header->u.ret_submit, 0, sizeof(usbip_stage2_header_ret_submit));
+    memset(&(req_header->u.ret_submit), 0, sizeof(usbip_stage2_header_ret_submit));
 
     req_header->u.ret_submit.status = status;
     req_header->u.ret_submit.data_length = data_length;
 
-    pack(&req_header, sizeof(usbip_stage2_header));
+    pack(req_header, sizeof(usbip_stage2_header));
     send(kSock, req_header, sizeof(usbip_stage2_header), 0);
 }
 
-void send_stage2_submit_data(usbip_stage2_header *req_header, int32_t status, const void *const data, int32_t data_length)
+void send_stage2_submit_data(usbip_stage2_header *req_header, int32_t status, const uint8_t *const data, int32_t data_length)
 {
 
     send_stage2_submit(req_header, status, data_length);
diff --git a/main/wifi_configuration.h b/main/wifi_configuration.h
index cb2701c..407261e 100644
--- a/main/wifi_configuration.h
+++ b/main/wifi_configuration.h
@@ -13,6 +13,8 @@
 #define EXAMPLE_WIFI_SSID "DAP"
 #define EXAMPLE_WIFI_PASS "12345678"
 
-#define PORT 22350
+#define PORT 3240
+
+#define CONFIG_EXAMPLE_IPV4 1
 
 #endif 
\ No newline at end of file