
/**
 * \file
 *         An example of how to write uIP applications
 *         with protosockets.
 */

/*
 * This is a short example of how to write uIP applications using
 * protosockets.
 */

#include "socket_app.h"
#include "uip.h"
#include <string.h>
#include "dpuser.h"
#include "dpcom.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"


extern DPUINT EtherFlag;
extern volatile DPULONG sock_image_requested_address;
extern volatile DPULONG sock_return_bytes;
extern DPUCHAR page_global_buffer[PAGE_BUFFER_SIZE];

extern xSemaphoreHandle webSemHndl;
extern xSemaphoreHandle switchOneSemHndl;

DPULONG SocketConnect = 0;
unsigned int firsttime1 =1;
/*
 * Declaration of the protosocket function that handles the connection
 * (defined at the end of the code).
 */
static int handle_connection(struct socket_app_state *s);
/*---------------------------------------------------------------------------*/
/*
 * The initialization function. We must explicitly call this function
 * from the system initialization code, some time after uip_init() is
 * called.
 */
void
socket_app_init(void)
{
  /* We start to listen for connections on TCP port 1000. */
  uip_listen(HTONS(1234));
}
/*---------------------------------------------------------------------------*/
/*
 * In hello-world.h we have defined the UIP_APPCALL macro to
 * socket_app_appcall so that this funcion is uIP's application
 * function. This function is called whenever an uIP event occurs
 * (e.g. when a new connection is established, new data arrives, sent
 * data is acknowledged, data needs to be retransmitted, etc.).
 */
void
socket_app_appcall(void)
{
  /*
   * The uip_conn structure has a field called "appstate" that holds
   * the application state of the connection. We make a pointer to
   * this to access it easier.
   */
   struct socket_app_state *s = &(uip_conn->appstate);

  /*
   * If a new connection was just established, we should initialize
   * the protosocket in our applications' state structure.
   */
  if(uip_connected()) {
    PSOCK_INIT(&s->p, page_global_buffer, 8200);
	//  PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));

    SocketConnect = 1;
  }

  /*
   * Finally, we run the protosocket function that actually handles
   * the communication. We pass it a pointer to the application state
   * of the current connection.
   */
//	if(EtherFlag)
//	{
//  xSemaphoreTake( webSemHndl, portMAX_DELAY );
  handle_connection(s);
//  xSemaphoreGive(switchOneSemHndl);

//		EtherFlag = 0;
//	}
}
/*---------------------------------------------------------------------------*/
/*
 * This is the protosocket function that handles the communication. A
 * protosocket function must always return an int, but must never
 * explicitly return - all return statements are hidden in the PSOCK
 * macros.
 */
static int
handle_connection(struct socket_app_state *s)
{
	unsigned int i = 0;

//	if(EtherFlag)
//	{
#if 0
		PSOCK_BEGIN(&s->p);


	    for(i=0;i<100;i++)
	    {
	        s->outputbuffer[i] = 0;
	    }

	    sprintf(s->outputbuffer,"%x",image_requested_address);


	    s->outputbuffer[5] = '\n';

	    for(i =0;i< 8;i++)
	    {
	    	printf("image_requested_address = %d\n\r", s->outputbuffer[i]);
	    }


	    PSOCK_SEND_STR(&s->p, s->outputbuffer);


	    for(i=0;i<100;i++)
	    {
	    	s->outputbuffer[i] = 0;
	    }
	    for(i=0; i<0x7FFFFF; i++)
	    	;

	    printf("return_bytes = %d \n\r", return_bytes);

	    sprintf(s->outputbuffer,"%x", return_bytes);

	    s->outputbuffer[5]= '\n';

	    PSOCK_SEND_STR(&s->p, s->outputbuffer);


	    PSOCK_READTO(&s->p, '\0');

	    EtherFlag = 0;

	    PSOCK_END(&s->p);
#endif
//	    EtherFlag = 0;

	    /* This semaphore will wait for the user input to run webserver */
        //if(xSemaphoreTake( webSemHndl, 5 ) == pdTRUE)
	    //xSemaphoreTake( webSemHndl, portMAX_DELAY );

	    if(EtherFlag)
        {
    		PSOCK_BEGIN(&s->p);
	     sprintf(s->outputbuffer,"%x\n",sock_image_requested_address);

	    //for(i =0;i< 8;i++)
	    //{
	    	//printf("image_requested_address = %s", s->outputbuffer);
	    //}


	    PSOCK_SEND_STR(&s->p, s->outputbuffer);


	    //printf("return_bytes = %d \n\r", return_bytes);

	    sprintf(s->outputbuffer,"%x\n", sock_return_bytes);

	    PSOCK_SEND_STR(&s->p, s->outputbuffer);


	    PSOCK_READ_N_BYTES(&s->p, sock_return_bytes);
	    i = PSOCK_DATALEN(&s->p);
        if (i != sock_return_bytes)
        {
        	printf("Data length mismatch %d != %d\n\r",sock_return_bytes,i);
        }
	    //printf("Data Len =%d \n\r",i );
      //  page_global_buffer[i-1] = ' ';
      //  printf("Data Len2 =%d, %s\n\r",i,page_global_buffer );
#if 0
	    for(i=0; i<1024; i++)
	    {
	    	page_global_buffer[i] = s->inputbuffer[i];
	    }
#endif
	    EtherFlag = 0;
	    xSemaphoreGive(switchOneSemHndl);
	    PSOCK_END(&s->p);
	    //PSOCK_CLOSE(&s->p);
        }//sem end
	    //

	//}

	    //if(! (strcmp(s->inputbuffer, "Q\n") ) )
	    //	PSOCK_CLOSE(&s->p);

}


/*---------------------------------------------------------------------------*/
