// 在线升级模块
// 杭州秒寻科技有限公司
// 周涛
// 2024-5-9

#include "includes.h"

// 升级请求控制命令
// pbyData:数据
uint8 wmm_upgrade_control_cmd(uint8 *pbyData)
{
    uint8 byFlag = 0;

    PS_HEAD_T *pstHead = (PS_HEAD_T *)pbyData;
    LTV_B_T *pstLtv = (LTV_B_T *)(pbyData + sizeof(PS_HEAD_T));
    uint16 wCommand = 0; // 监控命令

    uint32 dwFileSize = 0;

    // 切换软件工作模式
    wCommand = pstLtv->wTag;
    if (wCommand == OAM_UPGRADE_SWITCH_SOFT_WORK_MODE)
    {
        // 擦除下载区
        byFlag = driverapp_cpuflash_upgrade_erase_download_block();
        if (!byFlag)
        {
            return byFlag;
        }
    }
    // 升级文件通知
    else if (wCommand == OAM_UPGRADE_NOTICE_UPGRADE_FILE_INFO)
    {
        dwFileSize = *(uint32 *)&pstLtv->abyData[0];
        byFlag = driverapp_cpuflash_upgrade_notice_file_info(dwFileSize);
        if (!byFlag)
        {
            return byFlag;
        }
    }
    // 升级结束
    else if (wCommand == OAM_UPGRADE_NOTICE_UPGRADE_OVER)
    {
        dwFileSize = *(uint32 *)&pstLtv->abyData[0];
        byFlag = driverapp_cpuflash_upgrade_update_flag(1, dwFileSize);
        if (!byFlag)
        {
            return byFlag;
        }
    }
    else
    {
        return byFlag;
    }

    return 1;
}

// 升级文件传输命令
// pbyData:数据
uint8 wmm_upgrade_file_transfer_cmd(uint8 *pbyData)
{
    uint8 byFlag = 0;

    PS_HEAD_T *pstHead = (PS_HEAD_T *)pbyData;
    LTV_B_T *pstLtv = (LTV_B_T *)(pbyData + sizeof(PS_HEAD_T));

    uint16 wPayloadLen = pstLtv->wLen;
    wPayloadLen -= 8; // 升级文件数据长度(字节数)

    uint32 dwCurrentFrameNo = 0; // 当前帧号
    dwCurrentFrameNo = *(uint32 *)&pstLtv->abyData[0];

    byFlag = driverapp_cpuflash_upgrade_write_data(dwCurrentFrameNo, (uint32 *)&pstLtv->abyData[4], wPayloadLen);
    if (!byFlag)
    {
        return byFlag;
    }

    return 1;
}
