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

#include "includes.h"

// 根据系统参数初始化外设
uint8 driverapp_peripheral_init()
{
    uint8 byFlag = 0;
    SAVE_ALL_OAM_PARAM_T *pstOamParam = NULL;

    // SAVE_BTS_T *pstBts = NULL;
    SAVE_CONFIG_T *pstConfig = NULL;
    // SAVE_DEVICE_T *pstDevice = NULL;
    SAVE_NETWORK_T *pstNetwork = NULL;
    WIFI_PARAM_T stWifiParam = {0};
    SAVE_SYNC_T *pstSync = NULL;

    uint8 byCarrierId = 0;
    uint8 byCarrierGap = 0;

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

    // 参数
    pstSync = pstOamParam->pstSyncParam;
    pstConfig = pstOamParam->pstConfigParam;
    pstNetwork = pstOamParam->pstNetworkParam;

    // 配置接收同步模块
    pstSync = wmm_sync_query_param();
    byFlag = sync_set_rx_init_param(pstSync->byCarrierId, pstSync->byCarrierGap, pstSync->byRxSyncWordId);
    if (!byFlag)
    {
        return byFlag;
    }
    exti_cal_audio_signal_send_delay_times(pstSync->byRxSyncWordId); // 计算音频信号延迟发送次数

    // 配置发射同步模块
    byFlag = synctx_set_tx_init_param(pstSync->byTxCarrierId, pstSync->byCarrierGap, pstSync->byTxSyncWordId, pstSync->byTxPower);
    if (!byFlag)
    {
        return byFlag;
    }
    exti_save_sync_signal_forward_flag(pstSync->byTxFlag); // 保存同步信号转发标识符

    // 初始化dac(开关和增益)
    byFlag = driverapp_dac_init_config(pstConfig->byDacSwitch, pstConfig->byDacGain);
    if (!byFlag)
    {
        return 0;
    }

    // 初始化pa(开关和增益)
    driverapp_pa_config_gain(pstConfig->byPaGain); // 配置PA增益
    driverapp_pa_switch(pstConfig->byPaSwitch);    // 打开PA开关

    // 初始化WiFi模块参数(wifi名称、wifi密码，服务器IP，服务器端口，网管使能)
    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;
}