/*!
    \file  tcp_client.c
    \brief TCP client demo program  

    \version 2017-02-10, V1.0.0, firmware for GD32F30x
    \version 2018-10-10, V1.1.0, firmware for GD32F30x
    \version 2018-12-25, V2.0.0, firmware for GD32F30x
*/

/*
    Copyright (c) 2018, GigaDevice Semiconductor Inc.

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "lwip/opt.h"
#include "lwip/sys.h"
#include "tcp_client.h"
#include <string.h>
#include <stdio.h>
#include "gd32f30x.h"
#include "main.h"
#include "lwip/tcp.h"
#include "lwip/memp.h"
#include "lwip/api.h"

#define TCP_CLIENT_TASK_PRIO            ( tskIDLE_PRIORITY + 5)
#define MAX_BUF_SIZE                    50
#define TIME_WAITING_FOR_CONNECT        ( ( portTickType ) 500 )

#if ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1))

struct recev_packet 
{
    int length;
    char bytes[MAX_BUF_SIZE];
};

static err_t tcp_client_recv(struct netconn *conn, struct netbuf *buf);

/*!
    \brief      called when a data is received 
    \param[in]  conn: the TCP netconn over which to send data
    \param[in]  buf: the received data
    \param[out] none
    \retval     err_t: error value
*/ 
static err_t tcp_client_recv(struct netconn *conn, struct netbuf *buf)
{
    struct pbuf *q;
    struct recev_packet recev_packet;
    uint32_t data_len = 0;


    /* we perform here any necessary processing on the buf */
    if(NULL != buf){    
        
        for(q = buf->p; q != NULL; q = q->next){
            /* if the received data size is larger than the size we want to get */     
            if(q->len > (MAX_BUF_SIZE - data_len)){
                /* only copy MAX_BUF_SIZE bytes data */
                memcpy((recev_packet.bytes + data_len), q->payload, (MAX_BUF_SIZE - data_len));
                data_len = MAX_BUF_SIZE;
            }else{
                /* copy q->len bytes data */
                memcpy((recev_packet.bytes + data_len), q->payload, q->len);
                data_len += q->len;
            }
                       
            if(data_len >= MAX_BUF_SIZE){ 
                break;
            }  
        }
        
        recev_packet.length = data_len;
        netconn_write(conn, (void *)&(recev_packet.bytes), recev_packet.length, NETCONN_COPY);
    }
  
    return ERR_OK;
}

/*!
    \brief      tcp_client task 
    \param[in]  arg: user supplied argument
    \param[out] none
    \retval     none
*/
static void tcp_client_task(void *arg)
{
    struct netconn *conn;
    struct ip_addr ipaddr;
    struct netbuf *buf;
    err_t ret, recv_err;

    IP4_ADDR(&ipaddr, IP_S_ADDR0, IP_S_ADDR1, IP_S_ADDR2, IP_S_ADDR3);
  
    while(1){
        /* creat TCP connection */
        conn = netconn_new(NETCONN_TCP);
        /* bind the new netconn to any IP address and port 1026 */
        recv_err = netconn_bind(conn, IP_ADDR_ANY, 1026);
      
        if((ERR_USE != recv_err)&&(ERR_ISCONN != recv_err)){
          
            /* connect the new netconn to remote server and port 1026 */
            ret = netconn_connect(conn, &ipaddr, 1026);
            if(ERR_OK == ret) {
              
                recv_err = netconn_recv(conn, &buf);
                while(ERR_OK == recv_err){ 

                    /* handle the received data */
                    tcp_client_recv(conn, buf);
                    netbuf_delete(buf);

                    recv_err = netconn_recv(conn, &buf);
                }
            }              
        }

        /* close connection and discard connection identifier */
        netconn_close(conn);
        netconn_delete(conn);
    }
}

#endif /* ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1)) */



#if LWIP_SOCKET
#include "lwip/sockets.h"
#define SERVER_IP_ADDRESS "192.168.2.22"
#define SERVER_PORT 8080
#define MESSAGE "hello, world"

static void tcp_client_task(void *arg)
{
    int ret, sockfd = -1;
    struct sockaddr_in svr_addr;
    char buf[100];

    /* Create the socket outside the loop */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        perror("Socket creation error");
        return;
    }

    struct sockaddr_in local_addr;
    local_addr.sin_family = AF_INET;
    local_addr.sin_port = htons(12345); // Set your desired local port
    local_addr.sin_addr.s_addr = INADDR_ANY;

    /* Bind the socket to a local address and port */
    if (bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0)
    {
        perror("Bind error");
        close(sockfd);
        return;
    }
		        /* Set up the server address to connect to */
        svr_addr.sin_family = AF_INET;
        svr_addr.sin_port = htons(SERVER_PORT);
        inet_aton(SERVER_IP_ADDRESS, &(svr_addr.sin_addr));

        /* Connect to the server */
			ret = connect(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
      if (ret < 0)
      {
            perror("Connect error");
            vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
      }
			while (1)
			{
        /* Send "hello, world" to the server */
        ret = send(sockfd, MESSAGE, sizeof(MESSAGE) - 1, 0);
        if (ret <= 0)
        {
            perror("Send error");
            vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
						ret = connect(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
						{
							perror("Connect error");
							vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
						}
            continue;
        }
/*
        //Receive response from the server (if needed) 
        ret = recv(sockfd, buf, sizeof(buf), 0);
        if (ret <= 0)
        {
            perror("Receive error");
            vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
            continue;
        }
*/
        /* Process the received data if needed */

        vTaskDelay(pdMS_TO_TICKS(5000)); // Delay for 5 seconds before the next iteration
    }

    /* Close the socket after the loop */
    close(sockfd);
}


#endif /* LWIP_SOCKET */


/*!
    \brief      initialize the tcp_client application
    \param[in]  none
    \param[out] none
    \retval     none
*/
void tcp_client_init(void)
{
    xTaskCreate(tcp_client_task, "TCP_CLIENT", DEFAULT_THREAD_STACKSIZE, NULL, TCP_CLIENT_TASK_PRIO, NULL);   
}

