// 上报模块
// 杭州秒寻科技有限公司
// 周涛
// 2024-9-30
#include "includes.h"

// 定义一个全局变量，保存告警参数
static SAVE_REPORT_T g_stSaveReportParam = {0};

// 模块初始化
void oam_report_init()
{
    SYS_MEMSET(&g_stSaveReportParam, 0, sizeof(SAVE_REPORT_T));
    return;
}

// 网管查询设备上报信息
// wCommand:  查询命令，pstLtv：当前监控对象
void oam_report_query(uint16 wCommand, uint8 *pbyData)
{
    LTV_T *pstLtv = (LTV_T *)pbyData;
    uint16 wLen = pstLtv->wLen - 4; // 净荷长度

    switch (wCommand)
    {
    case ORDER_REPORT_SYNC: // 同步告警
        SYS_MEMCPY(pstLtv->abyData, &g_stSaveReportParam.bySync, wLen);
        break;
    default:
        break;
    }

    return;
}

// 设备上报应答
// wCommand:  查询命令，pstLtv：当前监控对象
void oam_report_resp_ok(uint16 wCommand, uint8 *pbyData)
{
    LTV_T *pstLtv = (LTV_T *)pbyData;
    uint16 wLen = pstLtv->wLen - 4; // 净荷长度

    switch (wCommand)
    {
    case ORDER_REPORT_HEART:              // 心跳应答
        monitor_report_udp_heart_reset(); // UDP心跳状态复位
        break;
    default:
        break;
    }

    return;
}

// 上报网络心跳，每3分钟上报一次
void oam_report_heart()
{
    uint8 abyData[21] = {0}; // 心跳帧长为21字节
    PS_HEAD_T *pstHead = (PS_HEAD_T *)abyData;
    LTV_T *pstNetHeart = NULL;
    PS_TAIL_T *pstTail = NULL;

    SAVE_DEVICE_T *pstDevice = NULL;
    SAVE_BTS_T *pstBts = NULL;
    uint32 dwBtsId = 0;

    // 初始化变量
    pstDevice = wmm_query_device_param();
    if (pstDevice == NULL)
    {
        return;
    }
    dwBtsId = wmm_bts_query_bts_id(); // 基站ID

    //////组织帧结构
    // AP头，1字节
    pstHead->stApHead.byStartFlag = PS_AP_HEAD_FLAG;

    // VP，11字节
    SYS_MEMCPY(pstHead->stVpHead.abyProjectID, pstDevice->abyProjectId, 4);
    pstHead->stVpHead.byPackageID = 0x02;
    SYS_MEMCPY(pstHead->stVpHead.abyBtsID, &dwBtsId, 4);
    pstHead->stVpHead.byVpFlag = VP_CMMD; // 命令
    pstHead->stVpHead.byMcpType = MCP_A;

    // MCP，2字节
    pstHead->stMcpHead.byComd = MCP_A_REPORT;
    pstHead->stMcpHead.byResp = MCP_CMD;

    // 心跳包，4字节
    pstNetHeart = (LTV_T *)(abyData + sizeof(PS_HEAD_T));
    pstNetHeart->wLen = 4; // 4个字节，不包含数据
    pstNetHeart->wTag = ORDER_REPORT_HEART;

    // AP尾，3字节
    pstTail = (PS_TAIL_T *)(abyData + sizeof(PS_HEAD_T) + 4);
    pstTail->wCrc = 0x00;
    pstTail->byStopFlag = PS_AP_TAIL_FLAG;

    // 发送给AP层下行单元
    ps_down_ap_out(abyData, PS_SOURCE_ALARM, 21);

    return;
}
