#include "ftp_server.h"
#include "fly_param.h"
#include "ftpd.h"
#include "os_cpu.h"
#include "sockets.h"
#include <stdio.h>

INT32S g_ftp_cmd_socket  = -1; /* 创建与ftp交互的socket */
INT32S g_ftp_data_socket = -1; /* 创建与ftp交互的socket */

void ftp_server_cmd_task(void)
{

    /* socket 创建 */
    INT8U                     buf[500];
    INT32S                    ret = 0;
    static INT32S             ftp_cmd_sockfd;
    static struct sockaddr_in svr_addr = {0};
    struct sockaddr_in        clt_addr;
    INT32U                    non_block = 1;
    INT32U                    len;
    INT32U                    i_ctrl_cmd;
    static const char         ftp_reply_unkown[] = "500 Unknown command\r\n";
    ftp_cmd_t                *cmd_match;
    ftp_mbox_t                msgbox;

    msgbox.current_dir[0]    = 0; // 路径为空，即根目录
    svr_addr.sin_family      = AF_INET;
    svr_addr.sin_port        = htons(FTP_CMD_PORT);
    svr_addr.sin_addr.s_addr = htons(INADDR_ANY);

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

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

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

        g_ftp_cmd_socket = accept(ftp_cmd_sockfd, NULL, NULL);
        while (-1 != g_ftp_cmd_socket)
        {
            msgbox.cmd_socket = g_ftp_cmd_socket; // 当前 ftp 控制端口连接句柄

            send(g_ftp_cmd_socket, msg200, sizeof(msg200) - 1, 0); // 控制端口反馈
            while (-1 != g_ftp_cmd_socket)
            {
                ret = recv(g_ftp_cmd_socket, buf, sizeof(buf) - 1, 0);
                if (ret <= 0)
                {
                    lwip_close(g_ftp_cmd_socket);
                    g_ftp_cmd_socket = -1;
                    break;
                }
                i_ctrl_cmd = FTP_STR2ID(buf);

                if (buf[3] < 'A' || buf[3] > 'z') // 有些命令只有三个字节，需要判断
                {
                    i_ctrl_cmd &= 0x00ffffff;
                    msgbox.arg    = buf + 4;
                    msgbox.arglen = sizeof(buf) - 1 - 4;
                }
                else
                {
                    msgbox.arg    = buf + 5;
                    msgbox.arglen = sizeof(buf) - 1 - 5;
                }

                cmd_match = ftp_search_command(i_ctrl_cmd); // 匹配命令号

                if (NULL == cmd_match)
                    send(msgbox.cmd_socket, ftp_reply_unkown, sizeof(ftp_reply_unkown) - 1, 0);
                else
                    cmd_match->func(&msgbox);
            }
            lwip_close(g_ftp_cmd_socket);
            g_ftp_cmd_socket = -1;
            break;
        }

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

void ftp_server_data_task(void)
{
    /* socket 创建 */
    INT8U                     buf[500];
    INT32S                    ret = 0;
    static INT32S             ftp_data_sockfd;
    static struct sockaddr_in svr_addr = {0};
    struct sockaddr_in        clt_addr;
    INT32U                    len;
    ftp_mbox_t               *msgbox;
    char                     *ctrl_msg;
    INT8U                     err = 0;

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

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

        ret = bind(ftp_data_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));

        if (ret < 0)
        {
            lwip_close(ftp_data_sockfd);
            ftp_data_sockfd = -1;
            continue;
        }

        ret = listen(ftp_data_sockfd, 1);

        if (ret < 0)
        {
            lwip_close(ftp_data_sockfd);
            continue;
        }
        // len               = sizeof(clt_addr);
        // g_ftp_data_socket = accept(ftp_data_sockfd, (struct sockaddr *)&clt_addr, len);
        g_ftp_data_socket = accept(ftp_data_sockfd, NULL, NULL);
        // printf("1111111\r\n");
        while (-1 != g_ftp_data_socket)
        {
            msgbox = (ftp_mbox_t *)OSMboxPend(ftp_mbox, 50, &err);
            // ret = recv(g_ftp_data_socket, buf, sizeof(buf) - 1, 0);
            // if (ret <= 0)
            // {
            //     lwip_close(g_ftp_data_socket);
            //     g_ftp_data_socket = -1;
            //     break;
            // }
            // if (msgbox->arglen >= 2)
            // {
            if (err == 0)
            {
                switch (msgbox->event) // 根据不同的操作命令进行操作
                {
                case FTP_LIST:
                    ctrl_msg = data_port_list_file(msgbox);
                    break;

                    // case FTP_SEND_FILE_DATA:
                    //     ctrl_msg = data_port_send_file(data_port_conn, msgbox);
                    //     break;

                    // case FTP_RECV_FILE:
                    //     ctrl_msg = data_port_recv_file(msgbox);
                    //     break;

                default:;
                }
                send(msgbox->cmd_socket, ctrl_msg, strlen(ctrl_msg), 0); // 控制端口反馈
                // }
            }
            else
            {
                lwip_close(g_ftp_data_socket);
                g_ftp_data_socket = -1;
                break;
            }
        }

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