/*****************************************************************************
Copyright: 2013-2018, ZhiWan Tech. Co., Ltd.
File name:ps_mcp_network.c
Description: 网管参数管理模块
Author: 周涛
Version: 0.0.01
Date: 2022-4-23
History:
*****************************************************************************/
#include "includes.h"

// 定义模块全局变量
static SAVE_NETWORK_T g_stSaveNetworkParam = {0};

// 模块初始化
void wmm_network_init()
{
    SYS_MEMSET(&g_stSaveNetworkParam, 0, sizeof(SAVE_NETWORK_T));
    wmm_network_set_param_with_default_data();
    return;
}

// 配置默认参数
void wmm_network_set_param_with_default_data()
{
    char *pbyWifiName = "bts_wifi";
    char *pbyWifiPwd = "12345678";
    char *pbyIp = "192.168.1.10";
    uint16 wPort = 8972;

    // 初始化参数
    SYS_MEMSET(&g_stSaveNetworkParam, 0, sizeof(SAVE_NETWORK_T));
    SYS_MEMCPY((void *)g_stSaveNetworkParam.abyApName, (void *)pbyWifiName, SYS_STRLEN(pbyWifiName));
    SYS_MEMCPY((void *)g_stSaveNetworkParam.abyApPassword, (void *)pbyWifiPwd, SYS_STRLEN(pbyWifiPwd));
    SYS_MEMCPY((void *)g_stSaveNetworkParam.abyMonitorIP, (void *)pbyIp, SYS_STRLEN(pbyIp));
    SYS_MEMCPY((void *)g_stSaveNetworkParam.abyMonitorPort, (void *)&wPort, sizeof(uint16));
    g_stSaveNetworkParam.bySwitch = 0;
}

// 配置flash参数
void wmm_network_set_param_with_flash_data(uint8 *pbyData)
{
    SYS_MEMCPY((void *)&g_stSaveNetworkParam, (void *)pbyData, sizeof(SAVE_NETWORK_T));
}

// 查询参数
// wCommand:  查询命令，pbyLtv：当前监控对象
uint8 wmm_network_query(uint16 wCommand, uint8 *pbyLtv)
{
    LTV_T *pstLtv = (LTV_T *)pbyLtv;
    uint16 wLen = pstLtv->wLen - 4; // 净荷长度

    switch (wCommand)
    {
    case ORDER_NETWORK_WIFI_NAME:
        SYS_MEMCPY(pstLtv->abyData, g_stSaveNetworkParam.abyApName, wLen);
        break;
    case ORDER_NETWORK_WIFI_PWD:
        SYS_MEMCPY(pstLtv->abyData, g_stSaveNetworkParam.abyApPassword, wLen);
        break;
    case ORDER_NETWORK_SERVICE_IP:
        SYS_MEMCPY(pstLtv->abyData, g_stSaveNetworkParam.abyMonitorIP, wLen);
        break;
    case ORDER_NETWORK_SERVICE_PORT:
        SYS_MEMCPY(pstLtv->abyData, g_stSaveNetworkParam.abyMonitorPort, wLen);
        break;
    case ORDER_NETWORK_SWITCH:
        SYS_MEMCPY(pstLtv->abyData, &g_stSaveNetworkParam.bySwitch, wLen);
        break;
    default:
        break;
    }

    return 0;
}

// 配置参数
// wCommand:  查询命令，pbyLtv：当前监控对象
uint8 wmm_network_config(uint16 wCommand, uint8 *pbyLtv)
{
    LTV_T *pstLtv = (LTV_T *)pbyLtv;
    uint16 wLen = pstLtv->wLen - 4; // 净荷长度

    switch (wCommand)
    {
    case ORDER_NETWORK_WIFI_NAME:
        SYS_MEMCPY(g_stSaveNetworkParam.abyApName, pstLtv->abyData, wLen);
        break;
    case ORDER_NETWORK_WIFI_PWD:
        SYS_MEMCPY(g_stSaveNetworkParam.abyApPassword, pstLtv->abyData, wLen);
        break;
    case ORDER_NETWORK_SERVICE_IP:
        SYS_MEMCPY(g_stSaveNetworkParam.abyMonitorIP, pstLtv->abyData, wLen);
        break;
    case ORDER_NETWORK_SERVICE_PORT:
        SYS_MEMCPY(g_stSaveNetworkParam.abyMonitorPort, pstLtv->abyData, wLen);
        break;
    case ORDER_NETWORK_SWITCH:
        SYS_MEMCPY(&g_stSaveNetworkParam.bySwitch, pstLtv->abyData, wLen);
        break;
    default:
        break;
    }

    return 0;
}

// 向外暴露参数，方便其他模块使用参数
SAVE_NETWORK_T *wmm_network_query_param()
{
    return (SAVE_NETWORK_T *)(&g_stSaveNetworkParam);
}

// 获得网管中心使能标识符
uint8 wmm_network_get_omc_switch_flag()
{
    return g_stSaveNetworkParam.bySwitch;
}