#if (PERIODIC_LOG_ENB == 1) || defined(ENABLE_SOC_PRINT)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h> 
#include "stdint.h"
#include "logger.h"
#include "exo_common.h"
#include "exo_osal.h"
#include "obc_main.h"

#ifdef LINUX_TEMP_PORT
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif

#if (PERIODIC_LOG_ENB == 1) || defined(ENABLE_SOC_PRINT)
#include <sys/socket.h>
#include <arpa/inet.h>
#endif

#define MAX 80
#define PORT 9898
#define SA struct sockaddr


static int len;
//static int port;
static struct sockaddr_in servaddr, cli;
static struct timeval timeout;
static int connfd;
static int sockfd;
//static int loggerfd;
static uint8_t log_sock_cntct_flag = 0;
s_obc_sock_info logger_soc_inf;

void logger_simu_server_skt_init()
{

    timeout.tv_sec = 2;
    timeout.tv_usec = 0;

    // socket create and verification
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        DEBUG_CPRINT(("\nLOGGER_simu socket creation failed...\n"));
    }
    else
        DEBUG_CPRINT(("\nLOGGER simu Socket successfully created..\n"));
    bzero(&servaddr, sizeof(servaddr));
    // assign IP, PORT
#ifdef LINUX_TEMP_PORT
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(logger_soc_inf.ip_addr);
    servaddr.sin_port = atoi(logger_soc_inf.port_num);
#else
    ip4_addr_t log_ipaddr;
    IP4_ADDR(&log_ipaddr, ETH_IP_ADDR0, ETH_IP_ADDR1, ETH_IP_ADDR2, ETH_IP_ADDR3);
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = log_ipaddr.addr;
	servaddr.sin_port = LOG_IP_PORT;
#endif
    // Binding newly created socket to given IP and verification
    if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
        DEBUG_CPRINT(("\nLOGGER_simu socket bind failed...\n"));
    }
    else
        DEBUG_CPRINT(("\nLOGGER_simu Socket successfully binded..\n"));
    // Now server is ready to listen and verification
    if ((listen(sockfd, 5)) != 0) {
        DEBUG_CPRINT(("\nLOGGER Listen failed...\n"));
    }
    else
        DEBUG_CPRINT(("\nLOGGER Server listening..\n"));

}

void logger_socket_con()
{
    // Accept the data packet from client and verification
    len = sizeof(cli);
    connfd = accept(sockfd, (SA*)&cli,(socklen_t *) &len);
    if (connfd < 0) {
        log_sock_cntct_flag = 0;
        DEBUG_CPRINT(("\nLOGGER server accept failed...\n"));

    }
    else
    {
        log_sock_cntct_flag = 1;
        DEBUG_CPRINT(("\nLOGGER server client connected...\n"));
    }
    //    setsockopt (connfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
    //  setsockopt (connfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof timeout);

}

uint8_t logger_simu_data_write(uint8_t* buff, uint16_t length)
{
    int ret=0;
    /*    DEBUG_CPRINT(("LOGGER write data=="));
          for (int i=0;i<length;i++)
          {
          DEBUG_CPRINT(("%x\t",buff[i]));
          }
          DEBUG_CPRINT(("\n"));*/
    if(log_sock_cntct_flag)
    {
        if(check_socket_disconnected(connfd))
        {
            log_sock_cntct_flag = 0;
            ret = 1;
            close(connfd);
        }
        else
        {
            ret = write(connfd,buff,length);
            if(ret!=-1)
            {
                //DEBUG_CPRINT(("\nLOGGER_simu socket write success\n"));
                ret=0;
            }
            else
            {
                DEBUG_CPRINT(("\nLOGGER_simu socket write fail\n"));
                ret=1;
                log_sock_cntct_flag = 0;
                close(connfd);
            }
        }
    }

    return ret;
}

uint8_t logger_simu_data_read(uint8_t* buff, uint16_t length)
{
    int ret=0;


    if(log_sock_cntct_flag)
    {

        ret = read(connfd,buff,length);

        if(ret==0)
        {
            log_sock_cntct_flag = 0;
            close(connfd);
            return 0;
        }

        if(ret!=-1)
        {
            //  DEBUG_CPRINT(("\nLOGGER_simu socket Read success\n"));
            ret=0;
        }
        else
        {
            DEBUG_CPRINT(("\nLOGGER_simu socket Read fail\n"));
            ret=1;
        }

    }
    /*    DEBUG_CPRINT(("LOGGER Response==\n"));
          for (int i=0;i<length;i++)
          {
          DEBUG_CPRINT(("%x\t",buff[i]));
          }*/
    return ret;
}

void* logger_skt_simu(void* args)
{
	log_sock_cntct_flag = 0;
    logger_simu_server_skt_init();
    while(1)
    {
        if(log_sock_cntct_flag == 0)
        {
            logger_socket_con();
        }
#ifdef LINUX_TEMP_PORT
        sleep(1);
#else
        os_delay(1000);
#endif
    }
}

#endif

