refactor: Cache friendly
This commit is contained in:
		
							parent
							
								
									9ef7c4535f
								
							
						
					
					
						commit
						c4bacc62ce
					
				
							
								
								
									
										9
									
								
								Makefile
								
								
								
								
							
							
						
						
									
										9
									
								
								Makefile
								
								
								
								
							|  | @ -1,9 +0,0 @@ | ||||||
| #
 |  | ||||||
| # This is a project Makefile. It is assumed the directory this Makefile resides in is a
 |  | ||||||
| # project subdirectory.
 |  | ||||||
| #
 |  | ||||||
| 
 |  | ||||||
| PROJECT_NAME := tcp_server |  | ||||||
| 
 |  | ||||||
| include $(IDF_PATH)/make/project.mk |  | ||||||
| 
 |  | ||||||
|  | @ -31,44 +31,40 @@ | ||||||
| #include "lwip/sys.h" | #include "lwip/sys.h" | ||||||
| #include <lwip/netdb.h> | #include <lwip/netdb.h> | ||||||
| 
 | 
 | ||||||
|  | #if (USE_WINUSB == 1) | ||||||
|  | typedef struct | ||||||
|  | { | ||||||
|  |     uint32_t length; | ||||||
|  |     uint8_t buf[DAP_PACKET_SIZE]; | ||||||
|  | } DapPacket_t; | ||||||
|  | #else | ||||||
|  | typedef struct | ||||||
|  | { | ||||||
|  |     uint8_t buf[DAP_PACKET_SIZE]; | ||||||
|  | } DapPacket_t; | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | #define DAP_HANDLE_SIZE (sizeof(DapPacket_t)) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| extern int kSock; | extern int kSock; | ||||||
| extern TaskHandle_t kDAPTaskHandle; | extern TaskHandle_t kDAPTaskHandle; | ||||||
| 
 | 
 | ||||||
| int kRestartDAPHandle = 0; | int kRestartDAPHandle = 0; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| #if (USE_WINUSB == 1) | static DapPacket_t DAPDataProcessed; | ||||||
| typedef struct |  | ||||||
| { |  | ||||||
|     uint32_t length; |  | ||||||
|     uint8_t buf[DAP_PACKET_SIZE]; |  | ||||||
| } DAPPacetDataType; |  | ||||||
| #else |  | ||||||
| typedef struct |  | ||||||
| { |  | ||||||
|     uint8_t buf[DAP_PACKET_SIZE]; |  | ||||||
| } DAPPacetDataType; |  | ||||||
| #endif |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| #define DAP_HANDLE_SIZE (sizeof(DAPPacetDataType)) |  | ||||||
| 
 |  | ||||||
| static DAPPacetDataType DAPDataProcessed; |  | ||||||
| static int dap_respond = 0; | static int dap_respond = 0; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| // SWO Trace
 | // SWO Trace
 | ||||||
| static uint8_t *swo_data_to_send = NULL; | static uint8_t *swo_data_to_send = NULL; | ||||||
| static uint32_t swo_data_num; | static uint32_t swo_data_num; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| // DAP handle
 | // DAP handle
 | ||||||
| static RingbufHandle_t dap_dataIN_handle = NULL; | static RingbufHandle_t dap_dataIN_handle = NULL; | ||||||
| static RingbufHandle_t dap_dataOUT_handle = NULL; | static RingbufHandle_t dap_dataOUT_handle = NULL; | ||||||
| static SemaphoreHandle_t data_response_mux = NULL; | static SemaphoreHandle_t data_response_mux = NULL; | ||||||
| 
 | 
 | ||||||
| static void unpack(void *data, int size); |  | ||||||
| 
 | 
 | ||||||
| void handle_dap_data_request(usbip_stage2_header *header, uint32_t length) | void handle_dap_data_request(usbip_stage2_header *header, uint32_t length) | ||||||
| { | { | ||||||
|  | @ -149,7 +145,7 @@ void DAP_Thread(void *argument) | ||||||
|     data_response_mux = xSemaphoreCreateMutex(); |     data_response_mux = xSemaphoreCreateMutex(); | ||||||
|     size_t packetSize; |     size_t packetSize; | ||||||
|     int resLength; |     int resLength; | ||||||
|     DAPPacetDataType *item; |     DapPacket_t *item; | ||||||
| 
 | 
 | ||||||
|     if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL || |     if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL || | ||||||
|         data_response_mux == NULL) |         data_response_mux == NULL) | ||||||
|  | @ -180,7 +176,7 @@ void DAP_Thread(void *argument) | ||||||
| 
 | 
 | ||||||
|             ulTaskNotifyTake(pdFALSE, portMAX_DELAY); |             ulTaskNotifyTake(pdFALSE, portMAX_DELAY); | ||||||
|             packetSize = 0; |             packetSize = 0; | ||||||
|             item = (DAPPacetDataType *)xRingbufferReceiveUpTo(dap_dataIN_handle, &packetSize, |             item = (DapPacket_t *)xRingbufferReceiveUpTo(dap_dataIN_handle, &packetSize, | ||||||
|                                                          pdMS_TO_TICKS(1), DAP_HANDLE_SIZE); |                                                          pdMS_TO_TICKS(1), DAP_HANDLE_SIZE); | ||||||
|             if (packetSize == 0) |             if (packetSize == 0) | ||||||
|             { |             { | ||||||
|  | @ -222,26 +218,26 @@ void DAP_Thread(void *argument) | ||||||
| 
 | 
 | ||||||
| int fast_reply(uint8_t *buf, uint32_t length) | int fast_reply(uint8_t *buf, uint32_t length) | ||||||
| { | { | ||||||
|     if (length == 48 && buf[3] == 1 && buf[15] == 1 && buf[19] == 1) |     usbip_stage2_header *buf_header = (usbip_stage2_header *)buf; | ||||||
|  |     if (length == 48 && | ||||||
|  |         buf_header->base.command == PP_HTONL(USBIP_STAGE2_REQ_SUBMIT) && | ||||||
|  |         buf_header->base.direction == PP_HTONL(USBIP_DIR_IN) && | ||||||
|  |         buf_header->base.ep == PP_HTONL(1)) | ||||||
|     { |     { | ||||||
|         if (dap_respond > 0) |         if (dap_respond > 0) | ||||||
|         { |         { | ||||||
|             DAPPacetDataType *item; |             DapPacket_t *item; | ||||||
|             size_t packetSize = 0; |             size_t packetSize = 0; | ||||||
|             item = (DAPPacetDataType *)xRingbufferReceiveUpTo(dap_dataOUT_handle, &packetSize, |             item = (DapPacket_t *)xRingbufferReceiveUpTo(dap_dataOUT_handle, &packetSize, | ||||||
|                                                          pdMS_TO_TICKS(10), DAP_HANDLE_SIZE); |                                                          pdMS_TO_TICKS(10), DAP_HANDLE_SIZE); | ||||||
|             if (packetSize == DAP_HANDLE_SIZE) |             if (packetSize == DAP_HANDLE_SIZE) | ||||||
|             { |             { | ||||||
|                 unpack((uint32_t *)buf, sizeof(usbip_stage2_header)); |  | ||||||
| 
 |  | ||||||
| #if (USE_WINUSB == 1) | #if (USE_WINUSB == 1) | ||||||
|                 uint32_t resLength = item->length; |                 send_stage2_submit_data_fast((usbip_stage2_header *)buf, item->buf, item->length); | ||||||
|                 send_stage2_submit_data_fast((usbip_stage2_header *)buf, 0, item->buf, resLength); |  | ||||||
| #else | #else | ||||||
|                 send_stage2_submit_data_fast((usbip_stage2_header *)buf, 0, item->buf, DAP_HANDLE_SIZE); |                 send_stage2_submit_data_fast((usbip_stage2_header *)buf, item->buf, DAP_HANDLE_SIZE); | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|                 if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE) |                 if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE) | ||||||
|                 { |                 { | ||||||
|                     --dap_respond; |                     --dap_respond; | ||||||
|  | @ -259,28 +255,14 @@ int fast_reply(uint8_t *buf, uint32_t length) | ||||||
|         } |         } | ||||||
|         else |         else | ||||||
|         { |         { | ||||||
|             //// TODO: ep0 dir 0 ?
 |             buf_header->base.command = PP_HTONL(USBIP_STAGE2_RSP_SUBMIT); | ||||||
|             buf[0x3] = 0x3; // command
 |             buf_header->base.direction = PP_HTONL(USBIP_DIR_OUT); | ||||||
|             buf[0xF] = 0;  // direction
 |             buf_header->u.ret_submit.status = 0; | ||||||
|             buf[0x16] = 0; |             buf_header->u.ret_submit.data_length = 0; | ||||||
|             buf[0x17] = 0; |             buf_header->u.ret_submit.error_count = 0; | ||||||
|             buf[27] = 0; |  | ||||||
|             buf[39] = 0; |  | ||||||
|             send(kSock, buf, 48, 0); |             send(kSock, buf, 48, 0); | ||||||
|             return 1; |             return 1; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
| 
 |  | ||||||
| static void unpack(void *data, int size) |  | ||||||
| { |  | ||||||
|     // Ignore the setup field
 |  | ||||||
|     int sz = (size / sizeof(uint32_t)) - 2; |  | ||||||
|     uint32_t *ptr = (uint32_t *)data; |  | ||||||
| 
 |  | ||||||
|     for (int i = 0; i < sz; i++) |  | ||||||
|     { |  | ||||||
|         ptr[i] = ntohl(ptr[i]); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  | @ -339,7 +339,7 @@ void send_stage2_submit(usbip_stage2_header *req_header, int32_t status, int32_t | ||||||
| 
 | 
 | ||||||
|     req_header->u.ret_submit.status = status; |     req_header->u.ret_submit.status = status; | ||||||
|     req_header->u.ret_submit.data_length = data_length; |     req_header->u.ret_submit.data_length = data_length; | ||||||
| 
 |     // already unpacked
 | ||||||
|     pack(req_header, sizeof(usbip_stage2_header)); |     pack(req_header, sizeof(usbip_stage2_header)); | ||||||
|     send(kSock, req_header, sizeof(usbip_stage2_header), 0); |     send(kSock, req_header, sizeof(usbip_stage2_header), 0); | ||||||
| } | } | ||||||
|  | @ -355,19 +355,16 @@ void send_stage2_submit_data(usbip_stage2_header *req_header, int32_t status, co | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void send_stage2_submit_data_fast(usbip_stage2_header *req_header, int32_t status, const void *const data, int32_t data_length) | void send_stage2_submit_data_fast(usbip_stage2_header *req_header, const void *const data, int32_t data_length) | ||||||
| { | { | ||||||
|     uint8_t * send_buf = (uint8_t *)req_header; |     uint8_t * send_buf = (uint8_t *)req_header; | ||||||
| 
 | 
 | ||||||
|     req_header->base.command = USBIP_STAGE2_RSP_SUBMIT; |     req_header->base.command = PP_HTONL(USBIP_STAGE2_RSP_SUBMIT); | ||||||
|     req_header->base.direction = !(req_header->base.direction); |     req_header->base.direction = htonl(!(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.data_length = htonl(data_length); | ||||||
| 
 | 
 | ||||||
|     req_header->u.ret_submit.status = status; |  | ||||||
|     req_header->u.ret_submit.data_length = data_length; |  | ||||||
| 
 |  | ||||||
|     pack(req_header, sizeof(usbip_stage2_header)); |  | ||||||
| 
 | 
 | ||||||
|     // payload
 |     // payload
 | ||||||
|     memcpy(&send_buf[sizeof(usbip_stage2_header)], data, data_length); |     memcpy(&send_buf[sizeof(usbip_stage2_header)], data, data_length); | ||||||
|  |  | ||||||
|  | @ -15,7 +15,7 @@ int attach(uint8_t *buffer, uint32_t length); | ||||||
| int emulate(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, const void * const data, int32_t data_length); | 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(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); | ||||||
| void send_stage2_submit_data_fast(usbip_stage2_header *req_header, int32_t status, const void *const data, int32_t data_length); | void send_stage2_submit_data_fast(usbip_stage2_header *req_header, const void *const data, int32_t data_length); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
		Loading…
	
		Reference in New Issue