#include "lwip/tcp.h"
#include "HL_sci.h"
#include <string.h>
#include <stdlib.h>
#include <stdint.h>


#define SERVER_PORT 5001
#define CLIENT_PORT 6001
#define SERVER_IP "192.168.2.10"

#define CLIENT_IP "192.168.2.44"

#define sciREGx sciREG2

void sciDisplayTextudp(sciBASE_t *sci, uint8_t *text, uint32_t length);
uint8_t udpTitle[] ={"****************Connected to Server *******************"};
uint8_t udpSent[] ={"**************** Sent to Server *******************"};
uint8_t udpReceive[] ={"**************** Received From Server *******************"};
uint8_t recvclbk[] ={"**************** Into Receive Callback *******************"};



struct udp_pcb *pcb_udp, *pcb_udp1;
ip_addr_t server_ip, client_ip;
err_t err_udp = ERR_OK;

/*void udp_client_recv_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p,
                                   struct ip_addr *addr, u16_t port)
{
            //unsigned char *pucData;
            //unsigned long ulIdx;

            //Validate the contents of the datagram.
           // pucData = p->payload;
            //sciDisplayTextudp(sciREGx, recvclbk, sizeof(recvclbk));
}
*/
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{

  /*increment message count */
  sciDisplayTextudp(sciREGx, recvclbk, sizeof(recvclbk));
  /* Free receive pbuf */
  pbuf_free(p);
}

void udp_sendToServer(char input){
    //char* data = &input;
    char data[] = {"HELLO SERVER"};
    struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, strlen(data), PBUF_RAM);
    if(!p){
        //Buffer not allocated
        return;
    }
    memcpy(p->payload, data, strlen(data));
    err_udp = udp_send(pcb_udp, p);
    if(err_udp != ERR_OK){
    pbuf_free(p);
    return;
    }
    pbuf_free(p);
    //sciDisplayTextudp(sciREGx, udpSent, sizeof(udpSent));
}

void udp_connect_server(){
        //sciDisplayTextudp(sciREGx, udpTitle, sizeof(udpTitle));
        pcb_udp = udp_new();
        if(!pcb_udp){
            LWIP_ASSERT("udp_communication failed", pcb_udp != NULL);
        }

        if(pcb_udp != NULL){
            err_udp = udp_bind(pcb_udp, IP_ADDR_ANY, 0);
        }
        IP4_ADDR(&server_ip, 192,168,2,10);
        err_udp = udp_connect(pcb_udp, &server_ip,SERVER_PORT);

        if (err_udp == ERR_OK){
          /* Set a receive callback for the pcb_udp */
        // while(1){

          //udp_sendToServer('1');
          //udp_recv(pcb_udp, udp_receive_callback, NULL);

        // }

       //***********************************************************

         IP4_ADDR(&client_ip, 192,168,2,44);
         pcb_udp1 = udp_new();
         if(!pcb_udp1){
              LWIP_ASSERT("udp_communication failed", pcb_udp1 != NULL);
         }
         if(pcb_udp1 != NULL){
              err_udp = udp_bind(pcb_udp1,&client_ip, CLIENT_PORT);
         }
    }
}


 void udp_recvFromServer(){

   // sciDisplayTextudp(sciREGx, udpReceive, sizeof(udpReceive));
    udp_recv(pcb_udp1,udp_receive_callback, NULL);
}


void sciDisplayTextudp(sciBASE_t *sci, uint8_t *text, uint32_t length){
    while(length--){
        while((sci->FLR & 0x4) == 4);
        sciSendByte(sci, *text++);
    };
    return;
}




