// 系统参数模块，包括将参数保存到本地flash中和从flash中读出参数
// Author: 周涛
// Date: 2023-9-26

#include "includes.h"

// 模块初始化
uint8 sysparam_init()
{
    uint8 byFlag = 0;
    byFlag = sysparam_read_oam_param();
    if (!byFlag)
    {
        return 0;
    }

    return 1;
}

// 从flash中读取OAM参数并初始化各外设/器件
uint8 sysparam_read_oam_param()
{
    uint8 byFlag = 0;
    SAVE_ALL_OAM_PARAM_T *pstSaveAllOamParam = NULL;
    SAVE_ALL_OAM_PARAM_T *pstParamTmp = NULL;
    uint16 wLen = 0; // 数据长度
    uint8 *pbyParam = NULL;

    uint16 wDeviceLen = sizeof(SAVE_DEVICE_T);
    uint16 wNetworkLen = sizeof(SAVE_NETWORK_T);
    uint16 wConfigLen = sizeof(SAVE_CONFIG_T);
    uint16 wInstallLen = sizeof(SAVE_INSTALL_T);
    uint16 wSyncLen = sizeof(SAVE_SYNC_T);
    uint16 wBtsLen = sizeof(SAVE_BTS_T);

    uint32 dwProjectId = 0;
    uint32 dwBtsId = 0;

    // 获得保存参数的数据结构
    pstSaveAllOamParam = wmm_query_all_oam_param();
    if (NULL == pstSaveAllOamParam)
    {
        return 0;
    }

    // 计算参数长度
    wLen = 8 + wDeviceLen + wNetworkLen + wConfigLen + wInstallLen + wSyncLen + wBtsLen;
    if (wLen % 4 != 0)
    {
        wLen += 4 - wLen % 4; // 4字节对齐
    }

    // 申请内存
    pbyParam = (uint8 *)malloc(wLen);
    if (!pbyParam)
    {
        return 0;
    }
    SYS_MEMSET(pbyParam, 0, wLen);

    // 从flash中读取系统参数
    byFlag = driverapp_cpuflash_read_oam_param((uint32 *)pbyParam, wLen / 4);
    if (!byFlag) // 读取参数失败
    {
        free(pbyParam);
        pbyParam = NULL;
        return 0;
    }

    // 保存两个参数参数
    pstParamTmp = (SAVE_ALL_OAM_PARAM_T *)pbyParam;
    pstSaveAllOamParam->dwStoreFlag = pstParamTmp->dwStoreFlag;       // 保存参数标识符
    pstSaveAllOamParam->dwStoreVersion = pstParamTmp->dwStoreVersion; // 软件版本

    // 已经保存过参数，则用flash中的数据初始化参数结构
    if (pstSaveAllOamParam->dwStoreFlag == APP_WMM_STORE_FLAG)
    {
        // 用flash中的数据初始化各模块参数
        sysparam_config_init_param_with_flash_data(pbyParam, pstSaveAllOamParam);
    }

    // 根据系统参数初始化各种外设
    byFlag = driverapp_peripheral_init();
    if (!byFlag)
    {
        free(pbyParam);
        pbyParam = NULL;
        return 0;
    }

    // 设置VP层参数（项目编号，基站编号）
    SYS_MEMCPY(&dwProjectId, pstSaveAllOamParam->pstDeviceParam->abyProjectId, 4);
    SYS_MEMCPY(&dwBtsId, pstSaveAllOamParam->pstBtsParam->abyBtsId, 4);
    ps_vp_set_param(dwProjectId, dwBtsId);

    // 释放内存
    free(pbyParam);
    pbyParam = NULL;

    return 1;
}

// 用保存于flash中的参数配置模块
//  pbyData:数据
//  pstSaveAllOamParam:Oam参数数据
void sysparam_config_init_param_with_flash_data(uint8 *pbyData, SAVE_ALL_OAM_PARAM_T *pstSaveAllOamParam)
{
    uint8 *pbyParam = pbyData; // 数据
    uint8 byFixLen = 8;        // flash保存字符+版本号

    uint8 *pbyDeviceParam = NULL;
    uint8 *pbyNetworkParam = NULL;
    uint8 *pbyConfigParam = NULL;
    uint8 *pbyInstallParam = NULL;
    uint8 *pbySyncParam = NULL;
    uint8 *pbyBtsParam = NULL;

    uint16 wDeviceLen = sizeof(SAVE_DEVICE_T);
    uint16 wNetworkLen = sizeof(SAVE_NETWORK_T);
    uint16 wConfigLen = sizeof(SAVE_CONFIG_T);
    uint16 wInstallLen = sizeof(SAVE_INSTALL_T);
    uint16 wSyncLen = sizeof(SAVE_SYNC_T);
    uint16 wBtsLen = sizeof(SAVE_BTS_T);

    // 保存设备参数
    pbyDeviceParam = pbyParam + byFixLen;
    wmm_device_set_param_with_flash_data(pbyDeviceParam);

    // 保存网络参数
    pbyNetworkParam = pbyParam + byFixLen + wDeviceLen;
    wmm_network_set_param_with_flash_data(pbyNetworkParam);

    // 保存配置参数
    pbyConfigParam = pbyParam + byFixLen + wDeviceLen + wNetworkLen;
    wmm_config_set_param_with_flash_data(pbyConfigParam);

    // 保存安装参数
    pbyInstallParam = pbyParam + byFixLen + wDeviceLen + wNetworkLen + wConfigLen;
    wmm_install_set_param_with_flash_data(pbyInstallParam);

    // 保存同步参数
    pbySyncParam = pbyParam + byFixLen + wDeviceLen + wNetworkLen + wConfigLen + wInstallLen;
    wmm_sync_set_param_with_flash_data(pbySyncParam);

    // 保存基站参数
    pbyBtsParam = pbyParam + byFixLen + wDeviceLen + wNetworkLen + wConfigLen + wInstallLen + wSyncLen;
    wmm_bts_set_param_with_flash_data(pbyBtsParam);

    return;
}

// 保存系统参数至flash中
uint8 sysparam_save_oam_param()
{
    uint8 byFlag = 0;
    SAVE_ALL_OAM_PARAM_T *pstSaveAllOamParam = NULL;
    uint16 wLen = 0;
    uint8 *pbyParam = NULL;
    uint16 wDeviceLen = sizeof(SAVE_DEVICE_T);
    uint16 wNetworkLen = sizeof(SAVE_NETWORK_T);
    uint16 wConfigLen = sizeof(SAVE_CONFIG_T);
    uint16 wInstallLen = sizeof(SAVE_INSTALL_T);
    uint16 wSyncLen = sizeof(SAVE_SYNC_T);
    uint16 wBtsLen = sizeof(SAVE_BTS_T);

    pstSaveAllOamParam = wmm_query_all_oam_param();
    if (NULL == pstSaveAllOamParam)
    {
        return 0;
    }

    // 计算数据长度
    wLen = 8 + wDeviceLen + wNetworkLen + wConfigLen + wInstallLen + wSyncLen + wBtsLen;
    if (wLen % 4 != 0)
    {
        wLen += 4 - wLen % 4; // 4字节对齐处理
    }

    // 申请内存
    pbyParam = (uint8 *)malloc(wLen);
    if (NULL == pbyParam)
    {
        return 0;
    }
    SYS_MEMSET(pbyParam, 0, wLen);

    // 组织数据
    *(uint32 *)pbyParam = APP_WMM_STORE_FLAG;
    *((uint32 *)(pbyParam + 4)) = APP_WMM_STORE_VERSION;
    SYS_MEMCPY(pbyParam + 8, (uint8 *)pstSaveAllOamParam->pstDeviceParam, wDeviceLen);                                                            // 保存设备参数
    SYS_MEMCPY(&pbyParam[8 + wDeviceLen], (uint8 *)pstSaveAllOamParam->pstNetworkParam, wNetworkLen);                                             // 保存网管参数
    SYS_MEMCPY(&pbyParam[8 + wDeviceLen + wNetworkLen], (uint8 *)pstSaveAllOamParam->pstConfigParam, wConfigLen);                                 // 保存配置参数
    SYS_MEMCPY(&pbyParam[8 + wDeviceLen + wNetworkLen + wConfigLen], (uint8 *)pstSaveAllOamParam->pstInstallParam, wInstallLen);                  // 保存安装参数
    SYS_MEMCPY(&pbyParam[8 + wDeviceLen + wNetworkLen + wConfigLen + wInstallLen], (uint8 *)pstSaveAllOamParam->pstSyncParam, wSyncLen);          // 保存同步参数
    SYS_MEMCPY(&pbyParam[8 + wDeviceLen + wNetworkLen + wConfigLen + wInstallLen + wSyncLen], (uint8 *)pstSaveAllOamParam->pstBtsParam, wBtsLen); // 保存基站参数

    // 保存参数到flash中
    byFlag = driverapp_cpuflash_save_oam_param((uint32 *)pbyParam, wLen / 4);
    if (!byFlag)
    {
        free(pbyParam);
        pbyParam = NULL;
        return 0;
    }

    free(pbyParam);
    pbyParam = NULL;

    return 1;
}