/**
* @file		loopback.c
* @brief		Loopback Test Module Source File
* @version	1.0
* @date		2013/02/22
* @par Revision
*		2013/02/22 - 1.0 Release
* @author	
* \n\n @par Copyright (C) 2013 WIZnet. All rights reserved.
*/

#include "stm32f10x.h"
#include "config.h"
#include "W5200\w5200.h"
#include "W5200\socket.h"
#include "util.h"
#include "APPs\loopback.h"
#include <stdio.h>
#include "common/common.h"


int8_t data_buf[TX_RX_MAX_BUF_SIZE];

extern uint8 Dest_IP[4] ;
extern uint16 Dest_PORT ;

void loopback_tcps(uint8_t sock, uint16_t port)
{
  int32_t ret;
  int32_t SendLen, ReSendLen;
  
  ret = TCPRecv(sock, data_buf, TX_RX_MAX_BUF_SIZE);
  
  if(ret > 0){				// Received
    SendLen = TCPSend(sock, data_buf, ret);
    
    if(SendLen < ret){
      while(SendLen != ret){
        ReSendLen = TCPReSend(sock);
        
        if(ReSendLen > 0){
          SendLen += ReSendLen;
          
        } else if(ReSendLen == SOCKERR_WINDOW_FULL){
          LOG("Window Full");
          TCPClose(sock);
          DBG("TCP Socket Close");
          while(1);
          
        } else{
          break;
        }
      }
    }
    
  } else if(ret == SOCKERR_NOT_TCP){	// Not TCP Socket, It's UDP Socket
    DBG("UDP Socket Close");
    UDPClose(sock);
  } else if(ret == SOCKERR_CLOSED){		// Socket Closed
    LOGA("TCPS Loop-Back Started - sock(%d):port(%d)",(uint16_t)sock,port);
    TCPServerOpen(sock, port);
  }
  
  if(getSn_SR(sock) == SOCKSTAT_CLOSE_WAIT){	// Close waiting
    TCPClose(sock);
  }
}
void loopback_tcpc(uint8_t sock, uint16_t port)
{							
  int32_t ret;
  int32_t SendLen, ReSendLen;
  
  ret = TCPRecv(sock, data_buf, TX_RX_MAX_BUF_SIZE);
  if(ret > 0){				// Received
    SendLen = TCPSend(sock, data_buf, ret);
    
    if(SendLen < ret){
      while(SendLen != ret){
        ReSendLen = TCPReSend(sock);
        
        if(ReSendLen > 0){
          SendLen += ReSendLen;
          
        } else if(ReSendLen == SOCKERR_WINDOW_FULL){
          LOG("Window Full");
          TCPClose(sock);
          DBG("TCP Socket Close");
          while(1);
          
        } else{
          break;
        }
      }
    }
    
  } else if(ret == SOCKERR_NOT_TCP){	// Not TCP Socket, It's UDP Socket
    DBG("UDP Socket Close");
    UDPClose(sock);
  } else if(ret == SOCKERR_CLOSED){		// Socket Closed
    LOGA("TCPC Loop-Back Started - sock(%d):port(%d)",(uint16_t)sock,port);
    TCPClientOpen(sock, port,(uint8_t*)Dest_IP,Dest_PORT);
  }
  
  if(getSn_SR(sock) == SOCKSTAT_CLOSE_WAIT){	// Close waiting
    TCPClose(sock);
  }
}
/**
* Start UDP Loopback Test (Device Side).
* For Loopback Test, need PC side Loopback test program 
* @param sock Socket number to use
* @param port Port number to use
*/
void loopback_udp(uint8_t sock, uint16_t port)
{
  int32_t ret;
  uint32_t destip = 0;
  uint16_t destport;
  
  ret = UDPRecv(sock, data_buf, TX_RX_MAX_BUF_SIZE, (uint8_t*)&destip, &destport);
  
  if(ret > 0){				// Received
    ret = UDPSend(sock, data_buf, ret, (uint8_t*)&destip ,destport);
    
    if(ret == SOCKERR_TIME_OUT){
      ERR("Timeout");
      UDPClose(sock);
      DBG("UDP Socket Close");
    }
    
  } else if(ret == SOCKERR_NOT_UDP){	// Not UDP Socket, It's TCP Socket
    DBG("TCP Socket Close");
    TCPClose(sock);
  } else if(ret == SOCKERR_CLOSED){		// Socket Closed
    LOGA("UDP Loop-Back Started - sock(%d):port(%d)",(uint16_t)sock, port);
    UDPOpen(sock, port);
  }
}



