// 外设初始化业务驱动
// 杭州秒寻科技有限公司
// 周涛
// 2023-09-26

#include "includes.h"

// 初始化外设
uint8 driverapp_peripheral_init()
{
    uint8 byFlag = 0;
    SAVE_ALL_OAM_PARAM_T *pstOamParam = NULL;
    SAVE_SYNC_T *pstSync = NULL;
    SAVE_NETWORK_T *pstNetwork = NULL;
    WIFI_PARAM_T stWifiParam = {0};

    uint8 byCarrierId = 0;
    uint8 byCarrierGap = 0;

    // 获得oam参数
    pstOamParam = wmm_query_all_oam_param();
    if (NULL == pstOamParam)
    {
        return 0;
    }

    // 已经保存过参数
    if (pstOamParam->dwStoreFlag == APP_WMM_STORE_FLAG)
    {
        ////配置同步模块
        pstSync = pstOamParam->pstSyncParam; // 获得同步结构指针

        // 初始化同步模块频率
        byCarrierId = pstSync->byCarrierId;
        byCarrierGap = pstSync->byCarrierGap;
        byFlag = sync_freq_config_carrier_freq(byCarrierId, byCarrierGap);
        if (!byFlag)
        {
            return byFlag;
        }

        // 配置发射功率
        sync_power_config_tx_power(pstSync->byTxPower);

        // 配置北斗gps使能信号
        driver_cpld_bdgps_enable(pstSync->byBdGpsEnabled);

        //// 配置WiFi模块参数(wifi名称、wifi密码，服务器IP，服务器端口，网管使能)
        pstNetwork = pstOamParam->pstNetworkParam; // 获得网络参数指针
        SYS_MEMCPY(stWifiParam.abyApName, pstNetwork->abyApName, 20);
        SYS_MEMCPY(stWifiParam.abyApPassword, pstNetwork->abyApPassword, 20);
        SYS_MEMCPY(stWifiParam.abyMonitorIP, pstNetwork->abyMonitorIP, 32);
        SYS_MEMCPY(stWifiParam.abyMonitorPort, pstNetwork->abyMonitorPort, 2);
        stWifiParam.bySwitch = pstNetwork->bySwitch;
        wifi_cmd_set_param(&stWifiParam);
    }

    return 1;
}