#include "net.h"
#include "fly_param.h"
#include "iec104.h"
#include "iec104_queue.h"
#include "os_cpu.h"
#include "stm32f4xx_gpio.h"
#include "ucos_ii.h"
#include <stdio.h>

INT32S g_iec104_socket                = -1; /* 创建与104交互的socket */
INT32S g_modbus_tcp_socket            = -1; /* 创建与104交互的socket */
INT32S g_modbus_tcp_socket_link_state = 0;  /* 与4G模块Socket信息初始化标志:1初始化完成 0：未初始化 */
INT32S g_iec104_socket_link_state     = 0;  /* 与4G模块Socket信息初始化标志:1初始化完成 0：未初始化 */
u8     g_data_req_flag                = 0;

void fly_mosbus_init_flag(void)
{
    g_data_req_flag = 0;
    return;
}

int modbus_tcp_rec_module(INT8U *rec_buf)
{
    INT32S buf_len = 0;

    buf_len = recv(g_modbus_tcp_socket, rec_buf, MAX_RECV_SIZE, 0);

    if (buf_len <= 0)
    {
        fly_mosbus_init_flag();
        closesocket(g_modbus_tcp_socket);
        g_modbus_tcp_socket = -1;
        return -1;
    }
    return buf_len;
}

err_t fly_test_process(INT8U *rec_buf, INT32S buf_len)
{
    INT8U response[2] = {0xBB, 0xAA};
    if (((buf_len == 2) && ((INT8U *)rec_buf)[0] == 0xAA) && (((INT8U *)rec_buf)[1] == 0xBB))
    {
        send(g_modbus_tcp_socket, (void *)response, sizeof(response), 0);
    }
    return 1;
}

void iec104_task(void)
{
    /* socket 创建 */
    INT8U                     buf[500];
    INT32S                    ret = 0;
    static INT32S             iec104_sockfd;
    static struct sockaddr_in svr_addr = {0};
    struct sockaddr_in        clt_addr;
    INT32U                    non_block = 1;

    //     if (g_iec104_socket_link_state == 1)
    //     {
    //         goto SOCKET_LINK;
    //     }
    //     iec104_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    //     if (iec104_sockfd < 0)
    //     {
    //         return -1;
    //     }
    //     memset(&svr_addr, 0, sizeof(struct sockaddr_in));

    //     /* 端口号设置为2404 */
    //     svr_addr.sin_family      = AF_INET;
    //     svr_addr.sin_port        = htons(IEC_PORT);
    //     svr_addr.sin_addr.s_addr = htons(INADDR_ANY);

    //     /* 绑定 */
    //     ret = bind(iec104_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
    //     if (ret < 0)
    //     {
    //         lwip_close(iec104_sockfd);
    //         return -1;
    //     }
    //     /* 监听1个连接 */
    //     ret = listen(iec104_sockfd, 1);
    //     if (ret < 0)
    //     {
    //         lwip_close(iec104_sockfd);
    //         return -1;
    //     }
    //     g_iec104_socket_link_state = 1; /* 初始化完成后, socket信息标志置位 */

    // SOCKET_LINK:
    //     /* 建链 */
    //     len             = sizeof(clt_addr);
    //     g_iec104_socket = accept(iec104_sockfd, (struct sockaddr *)&clt_addr, len);
    //     if (g_iec104_socket < 0)
    //     {
    //         return g_iec104_socket;
    //     }
    //     ret = ioctlsocket(g_iec104_socket, FIONBIO, (u_long *)&non_block);

    //     if (ret <= 0)
    //     {
    //         // DEBUG("the other side has been closed (%d).\n", sizeof(buf) - 1);
    //         lwip_close(g_iec104_socket);
    //         g_iec104_socket = -1;
    //     }
    //     return g_iec104_socket;

    svr_addr.sin_family      = AF_INET;
    svr_addr.sin_port        = htons(IEC_PORT);
    svr_addr.sin_addr.s_addr = htons(INADDR_ANY);

    while (1)
    {
        iec104_sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (iec104_sockfd < 0)
        {
            continue;
        }

        ret = bind(iec104_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
        if (ret < 0)
        {
            lwip_close(iec104_sockfd);
            iec104_sockfd = -1;
            continue;
        }

        ret = listen(iec104_sockfd, 1);
        if (ret < 0)
        {
            lwip_close(iec104_sockfd);
            continue;
        }

        g_iec104_socket = accept(iec104_sockfd, NULL, NULL);

        while (-1 != g_iec104_socket)
        {
            ret = recv(g_iec104_socket, buf, sizeof(buf) - 1, 0);
            if (ret <= 0)
            {
                lwip_close(g_iec104_socket);
                g_iec104_socket = -1;
                break;
            }

            dump_hex(buf, ret);
            iec104_receive(buf, ret);
        }

        lwip_close(iec104_sockfd);
        iec104_sockfd = -1;
    }
}

INT32S iec104_rec_master(char *rec_buf)
{
    INT32S buf_len = 0;

    buf_len = recv(g_iec104_socket, rec_buf, MAX_RECV_SIZE, 0);

    if (buf_len <= 0)
    {
        // g_data_req_flag = 0;
        closesocket(g_iec104_socket);
        g_iec104_socket = -1;
        return -1;
    }
    iec104_receive(rec_buf, buf_len);
    return buf_len;
}

int modbus_tcp_connect_module(void)
{
    /* socket 创建 */
    INT8U                     buf[500];
    INT32S                    ret = 0;
    static INT32S             modbus_tcp_sockfd;
    static struct sockaddr_in svr_addr = {0};
    struct sockaddr_in        clt_addr;
    INT32U                    len       = 0;
    INT32U                    non_block = 1;

    if (g_modbus_tcp_socket_link_state == 1)
    {
        goto SOCKET_LINK;
    }
    modbus_tcp_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (modbus_tcp_sockfd < 0)
    {
        return -1;
    }
    memset(&svr_addr, 0, sizeof(struct sockaddr_in));

    /* 端口号设置为8080 */
    svr_addr.sin_family      = AF_INET;
    svr_addr.sin_port        = htons(TCP_PORT);
    svr_addr.sin_addr.s_addr = htons(INADDR_ANY);

    /* 绑定 */
    ret = bind(modbus_tcp_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
    if (ret < 0)
    {
        lwip_close(modbus_tcp_sockfd);
        return -1;
    }
    /* 监听1个连接 */
    ret = listen(modbus_tcp_sockfd, 1);
    if (ret < 0)
    {
        lwip_close(modbus_tcp_sockfd);
        return -1;
    }
    g_modbus_tcp_socket_link_state = 1; /* 初始化完成后, socket信息标志置位 */

SOCKET_LINK:
    /* 建链 */
    g_modbus_tcp_socket = accept(modbus_tcp_sockfd, NULL, NULL);
    if (g_modbus_tcp_socket < 0)
    {
        return g_modbus_tcp_socket;
    }
    ret = ioctlsocket(g_modbus_tcp_socket, FIONBIO, (u_long *)&non_block);

    if (ret < 0)
    {
        lwip_close(g_modbus_tcp_socket);
        g_modbus_tcp_socket = -1;
    }
    return g_modbus_tcp_socket;
}