// 同步模块
// 杭州秒寻科技有限公司
// 周涛
// 2024-3-17

#include "includes.h"

// 定义一个全局变量，保存参数
static SAVE_SYNC_T g_stSaveSyncParam = {0};

// 模块初始化
void wmm_sync_init()
{
    SYS_MEMSET(&g_stSaveSyncParam, 0, sizeof(SAVE_SYNC_T));
    wmm_sync_set_param_with_default_data();
}

// 初始化默认参数
void wmm_sync_set_param_with_default_data()
{
    SYS_MEMSET(&g_stSaveSyncParam, 0, sizeof(SAVE_SYNC_T));

    g_stSaveSyncParam.byCarrierId = 30;
    g_stSaveSyncParam.byRxSyncWordId = 1;
    g_stSaveSyncParam.byCarrierGap = 250;
    g_stSaveSyncParam.wRssi = -128;

    g_stSaveSyncParam.byTxFlag = 1; // 关闭
    g_stSaveSyncParam.byTxCarrierId = 34;
    g_stSaveSyncParam.byTxSyncWordId = 2;
    g_stSaveSyncParam.byTxPower = -10;

    g_stSaveSyncParam.byBdGpsEnabled = 0; // 北斗GPS信号关闭
}

// 基于flash中的数据配置设备参数
void wmm_sync_set_param_with_flash_data(uint8 *pbyData)
{
    SYS_MEMCPY((void *)&g_stSaveSyncParam, (void *)pbyData, sizeof(SAVE_SYNC_T));
}

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

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

    switch (wCommand)
    {
    case ORDER_SYNC_CENTER_FREQ: // 载波ID
        SYS_MEMCPY(pstLtv->abyData, &g_stSaveSyncParam.byCarrierId, wLen);
        break;
    case ORDER_SYNC_FREQ_OFFSET: // 载波间隔
        SYS_MEMCPY(pstLtv->abyData, &g_stSaveSyncParam.byCarrierGap, wLen);
        break;
    case ORDER_SYNC_TX_POWER: // 发射功率
        SYS_MEMCPY(pstLtv->abyData, &g_stSaveSyncParam.byTxPower, wLen);
        break;
    case ORDER_SYNC_RSSI: // 接收电平
        break;
    case ORDER_SYNC_BD_GPS_ENABLE: // 北斗GPS信号使能
        SYS_MEMCPY(pstLtv->abyData, &g_stSaveSyncParam.byBdGpsEnabled, wLen);
    default:
        break;
    }

    return 0;
}

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

    switch (wCommand)
    {
    case ORDER_SYNC_CENTER_FREQ: // 载波ID
        SYS_MEMCPY(&g_stSaveSyncParam.byCarrierId, pstLtv->abyData, wLen);
        break;
    case ORDER_SYNC_FREQ_OFFSET: // 载波间隔
        SYS_MEMCPY(&g_stSaveSyncParam.byCarrierGap, pstLtv->abyData, wLen);
        break;
    case ORDER_SYNC_TX_POWER: // 发射功率
        SYS_MEMCPY(&g_stSaveSyncParam.byTxPower, pstLtv->abyData, wLen);
        sync_power_config_tx_power(g_stSaveSyncParam.byTxPower);
        break;
    case ORDER_SYNC_RSSI: // 接收电平
        break;
    case ORDER_SYNC_BD_GPS_ENABLE: // 北斗GPS信号使能
        SYS_MEMCPY(&g_stSaveSyncParam.byBdGpsEnabled, pstLtv->abyData, wLen);
    default:
        break;
    }

    return 1;
}