// 本地网管模块
// 杭州秒寻科技有限公司
// 周涛
// 2023-9-17
#include "includes.h"

// 定义本模块使用的全局变量
static GUI_RECEIVE_BUFFER_T gGui_stReceiveBuf = {0};

// 模块初始化
void gui_init()
{
    SYS_MEMSET((void *)&gGui_stReceiveBuf, 0, sizeof(GUI_RECEIVE_BUFFER_T)); // 初始化

    return;
}

// gui模块任务处理函数，负责轮询GUI串口，接收本地网管数据
void gui_process_func()
{
    uint16 head = 0;      // 帧头索引
    uint16 tail = 0;      // 帧尾索引
    uint16 wFrameLen = 0; // 帧长 = tail - head + 1
    uint8 byExist = 0;
    uint16 wStopIndex = 0;

    // 从串口接收数据
    gGui_stReceiveBuf.dwDataLen += driver_gui_receive_data((uint8 *)(&gGui_stReceiveBuf.abyDataBuf[gGui_stReceiveBuf.dwDataLen]), (NMC_FRAME_MAX_LEN - gGui_stReceiveBuf.dwDataLen));
    if (gGui_stReceiveBuf.dwDataLen > UDP_FRAME_MIN_LEN)
    {
        // 找帧头
        wStopIndex = gGui_stReceiveBuf.dwDataLen - 1;
        for (uint16 i = 0; i < gGui_stReceiveBuf.dwDataLen; i++)
        {
            if (i < wStopIndex)
            {
                if (gGui_stReceiveBuf.abyDataBuf[i] == PS_AP_HEAD_FLAG && gGui_stReceiveBuf.abyDataBuf[i + 1] != PS_AP_HEAD_FLAG)
                {
                    head = i;
                    byExist = 1;
                    break;
                }
            }
            else
            {
                if (gGui_stReceiveBuf.abyDataBuf[i] == PS_AP_HEAD_FLAG)
                {
                    head = i;
                    byExist = 1;
                    break;
                }
            }
        }

        // 找到了帧头
        if (byExist)
        {
            // 找帧尾
            byExist = 0;
            for (uint16 j = head + 1; j < gGui_stReceiveBuf.dwDataLen; j++)
            {
                if (gGui_stReceiveBuf.abyDataBuf[j] == PS_AP_TAIL_FLAG)
                {
                    tail = j;
                    byExist = 1;
                    break;
                }
            }

            // 找到了帧尾
            if (byExist)
            {
                wFrameLen = tail - head + 1;       // 计算帧长
                if (wFrameLen > UDP_FRAME_MIN_LEN) // 帧长合法
                {
                    // 把消息送到协议栈
                    ps_receive_data_from_outsite(&gGui_stReceiveBuf.abyDataBuf[head], PS_SOURCE_GUI, wFrameLen);
                }

                // 队列处理
                gGui_stReceiveBuf.dwDataLen -= (tail + 1); // 剩余数据长度
                if (gGui_stReceiveBuf.dwDataLen <= 0)
                {
                    SYS_MEMSET(gGui_stReceiveBuf.abyDataBuf, 0, GUI_FRAME_MAX_LEN); // 队列置零
                }
                else
                {
                    SYS_MEMCPY((void *)gGui_stReceiveBuf.abyDataBuf, (void *)&gGui_stReceiveBuf.abyDataBuf[tail + 1], gGui_stReceiveBuf.dwDataLen); // 拷贝队列剩余数据
                    SYS_MEMSET(&gGui_stReceiveBuf.abyDataBuf[gGui_stReceiveBuf.dwDataLen], 0, GUI_FRAME_MAX_LEN - gGui_stReceiveBuf.dwDataLen);     // 队列置零
                }
            }
        }
    }
}

// GUI模块将数据发送到本地网管串口
// pbyData：数据，wLen：数据长度
void gui_send_data_to_gui(uint8 *pbyData, uint16 wLen)
{
    int8 byFlag = 0;

    byFlag = driver_gui_send_data(pbyData, wLen);
    if (!byFlag)
    {
        return;
    }

    return;
}
