/****************************************************************************
** File name    :  HTConfig.cpp
** Description  :  config parameter 
** Create date  :  2018.09.01
** Auther by    :  Liuyx
** Version info :  V1.0.01
** Copyright (C) 2002-2018 xi'an huatek, Inc Co., Ltd
** Update record:  
** DATE         AUTHER   DESC 
** -------------------------------------------------------------------------               
** 2018.09.01   Liuyx    first build
****************************************************************************/
#include "HTPublic.h"
#include "HTConst.h"
#include "HTConfig.h"
#include "HTLogger.h"

static const char *_FILE_ = "HTConfig.cpp" ;

TConfig::TConfig()
{
    b_monitor_enable = false ;   // 监控标志 true:启用 false: 不启用
    
    // database config parameter
    memset(m_dbHostIP,0x00, sizeof(m_dbHostIP));
    memset(m_dbName,  0x00, sizeof(m_dbName));
    memset(m_dbUserName, 0x00, sizeof(m_dbUserName));
    memset(m_dbUserPass, 0x00, sizeof(m_dbUserPass));
    memset(m_mbus_mode, 0x00, sizeof(m_mbus_mode));  // Modbus 通信模式 1:TCP, 2:RTU 3:PLUS 4: TCP&RTU
    
    memset(m_mbus_tcp_addr, 0x00, sizeof(m_mbus_tcp_addr));       // 通信地址, IP or COMM...
    memset(m_comtrade_path, 0x00, sizeof(m_comtrade_path));
    memset(m_comtrade_bak_path, 0x00, sizeof(m_comtrade_bak_path));
    memset(m_ied_model_config_file, 0x00, sizeof(m_ied_model_config_file));
    memset(m_ied_comtrade_path, 0x00, sizeof(m_ied_comtrade_path));
    m_dbMaxConnect = 1 ;
    m_dbMinConnect = 1 ;
    m_dbPort = 3306 ;
        
    memset(m_remote_addr, 0x00, sizeof(m_remote_addr));
    m_remote_port = 2404 ;
    // socket config
    m_fd_limit = 1024 ;
    
    m_recv_num = 1 ;
    m_parse_num = 1 ;
    m_opration_num = 1;
    m_respone_num = 1 ;
}

TConfig::~TConfig()
{
    b_monitor_enable = false ;   // 监控标志 true:启用 false: 不启用
    
    // database config parameter
    memset(m_dbHostIP,0x00, sizeof(m_dbHostIP));
    memset(m_dbName,  0x00, sizeof(m_dbName));
    memset(m_dbUserName, 0x00, sizeof(m_dbUserName));
    memset(m_dbUserPass, 0x00, sizeof(m_dbUserPass));
    
    memset(m_mbus_mode, 0x00, sizeof(m_mbus_mode));  // Modbus 通信模式 1:TCP, 2:RTU 3:PLUS 4: TCP&RTU
    memset(m_mbus_tcp_addr, 0x00, sizeof(m_mbus_tcp_addr));       // 通信地址, IP or COMM...
    memset(m_comtrade_path, 0x00, sizeof(m_comtrade_path));
    memset(m_comtrade_bak_path, 0x00, sizeof(m_comtrade_bak_path));
    memset(m_ied_model_config_file, 0x00, sizeof(m_ied_model_config_file));
    m_dbMaxConnect = 0 ;
    m_dbMinConnect = 0 ;
    m_dbPort = 3306 ;
    
    memset(m_remote_addr, 0x00, sizeof(m_remote_addr));
    m_remote_port = 2404 ;
        
    m_recv_num = 0 ;
    m_parse_num = 0 ;
    m_opration_num = 0;
    m_respone_num = 0 ;
}

// 设置数据库连接端口
void TConfig::setdbPort(unsigned int port) 
{ 
    m_dbPort= port; 
}

// 设置数据库连接端口
unsigned int TConfig::getdbPort() 
{ 
    return m_dbPort; 
}

// 设置数据库主机地址
void TConfig::setdbHostIP(char *pszDBHostIP)
{
    if(!pszDBHostIP) return ;
    strcpy(m_dbHostIP, pszDBHostIP);
}
// 获取数据库主机地址
char *TConfig::getdbHostIP()
{
    return m_dbHostIP;
}

// 设置数据库最大连接池数
void  TConfig::setdbMaxConnect(unsigned int max) 
{ 
    m_dbMaxConnect = max; 
}
// 获取数据库最大连接池数
unsigned int  TConfig::getdbMaxConnect() 
{ 
    return m_dbMaxConnect; 
}

// 设置数据库最小连接池数
void  TConfig::setdbMinConnect(unsigned int min)
{
    m_dbMinConnect = min; 
}
// 获取数据库最小连接池数
unsigned int  TConfig::getdbMinConnect() 
{ 
    return m_dbMinConnect; 
}

// 设置数据库服务名称
void TConfig::setdbName(char *pszDBName)  
{ 
    if(!pszDBName) return ;
    strcpy(m_dbName, pszDBName);
}
// 获取数据库服务名称
char *TConfig::getdbName()  
{ 
    return m_dbName; 
}
// 设置数据库用户名
void TConfig::setdbUserName(char *pszDBUserName) 
{ 
    if(!pszDBUserName) return ;
    strcpy(m_dbUserName, pszDBUserName);
}
// 获取数据库用户名
char *TConfig::getdbUserName() 
{ 
    return m_dbUserName; 
}

// 设置数据库用户密码
void TConfig::setdbUserPass(char *pszDBUserPass)
{
    if(!pszDBUserPass) return ;
    strcpy(m_dbUserPass, pszDBUserPass);
}
// 获取数据库用户密码
char *TConfig::getdbUserPass() 
{
    return m_dbUserPass; 
}

// 设置本地服务器IP
void TConfig::setLocalAddr(char *pszAddr)
{
    if (!pszAddr) return;
    strcpy(m_local_addr, pszAddr);
}
// 获取本地服务器IP
char *TConfig::getLocalAddr()
{
    return m_local_addr;
}

// 设置本地服务器端口
void TConfig::setLocalPort(unsigned short iPort)
{
    m_local_port = iPort;
}

// 获取本地服务器端口
unsigned short TConfig::getLocalPort()
{
    return m_local_port;
}


// 设置远程服务器IP
void TConfig::setRemoteAddr(char *pszAddr)
{
    if(!pszAddr) return ;
    strcpy(m_remote_addr, pszAddr);
}
// 获取远程服务器IP
char *TConfig::getRemoteAddr()
{
    return m_remote_addr;
}

// 设置远程服务器端口
void TConfig::setRemotePort(unsigned short iPort)
{
    m_remote_port = iPort;
}

// 获取远程服务器端口
unsigned short TConfig::getRemotePort()
{
    return m_remote_port;
}

// socket 相关属性参数配置
void  TConfig::setFdLimit(unsigned int iCount)
{
    m_fd_limit = iCount;
}

unsigned int  TConfig::getFdLimit()
{
    return m_fd_limit;
}

void  TConfig::setK(unsigned int k)
{
    uK = k;
}
void  TConfig::setW(unsigned int w)
{
    uW = w;
}
unsigned int  TConfig::getK()
{
    return uK;
}
unsigned int  TConfig::getW()
{
    return uW;
}
void  TConfig::setTimeout0(unsigned int t)
{
    t0 = t;
}
void  TConfig::setTimeout1(unsigned int t)
{
    t1 = t;
}
unsigned int  TConfig::getTimeout0()
{
    return t0;
}
unsigned int  TConfig::getTimeout1()
{
    return t1;
}
void  TConfig::setTimeout2(unsigned int t)
{
    t2 = t;
}
unsigned int  TConfig::getTimeout2()
{
    return t2;
}
void  TConfig::setTimeout3(unsigned int t)
{
    t3 = t;
}
unsigned int  TConfig::getTimeout3()
{
    return t3;
}

void TConfig::setLogDebug(char yes)
{
    m_logdebug = yes;  // debug logger
}
void TConfig::setLogError(char yes)
{
    m_logerror = yes;  //error logger 
}
void TConfig::setLogWarning(char yes)
{
    m_logwarning = yes;  // warning logger
}
void TConfig::setLogPack(char yes)
{
    m_logpack = yes;  // package print logger
} 
void TConfig::setLogConsole(char yes)
{
    m_logconsole = yes;  // console print logger
}
void TConfig::setLogMessage(char yes)
{
    m_logmessage = yes;  // message print logger
}
char TConfig::isLogDebug(void)
{
    return m_logdebug;  // debug logger
}
char TConfig::isLogError(void)
{
    return m_logerror;  //error logger 
}
char TConfig::isLogWarning(void)
{
    return m_logwarning ;  // warning logger
}
char TConfig::isLogPack(void)
{
    return m_logpack ;  // package print logger
} 
char TConfig::isLogConsole(void)
{
    return m_logconsole; // console print logger
}
char TConfig::isLogMessage(void)
{
    return m_logmessage; // message print logger
}

void TConfig::setServerVersion(char *pver) 
{
    if(pver == NULL) {
        strcpy(m_version, "000.00.000");
        return ;
    }
    strcpy(m_version, pver);
}
char *TConfig::getServerVersion()
{
    return m_version;
}


//JT/T809 服务数据交换监控
void TConfig::setMonitor(bool status)
{
    b_monitor_enable = status;
}
bool TConfig::getMonitor()
{
    return b_monitor_enable;
}

void TConfig::setRecvThreadNumber(unsigned int n)
{
    m_recv_num = n ;
}
unsigned int TConfig::getRecvThreadNumber()
{
    return m_recv_num ;
}

void TConfig::setParseThreadNumber(unsigned int n)  // 解析104线程数
{
    m_parse_num = n ;
}
unsigned int TConfig::getParseThreadNumber()
{
    return m_parse_num ;
}

void TConfig::setOprationThreadNumber(unsigned int n)  // 入库线程数
{
    m_opration_num = n ;
}
unsigned int TConfig::getOprationThreadNumber()
{
    return m_opration_num ;
}

void TConfig::setResponeThreadNumber(unsigned int n)  // 应答线程数
{
    m_respone_num = n ;
}
unsigned int TConfig::getResponeThreadNumber()
{
    return m_respone_num ;
}

// 通信协议使能
void TConfig::setCommProtocol(int protocol)
{
    m_comm_protocol = protocol;
}
int TConfig::getCommProtocol()
{
    return m_comm_protocol;        // 1:modbus 2: iec61850
}

void TConfig::setModbusMode(char *mode)
{
    if(mode) strcpy(m_mbus_mode, mode);
}
char *TConfig::getModbusMode()
{
    return m_mbus_mode ;
}
void TConfig::setModbusTcpAddr(char *addr)
{
    if (addr) {
        strcpy(m_mbus_tcp_addr,addr);
    }
}
char *TConfig::getModbusTcpAddr()
{
    return m_mbus_tcp_addr;
}

void TConfig::setModbusTcpPort(int port)
{
    m_mbus_tcp_port = port;
}
int TConfig::getModbusTcpPort()
{
    return m_mbus_tcp_port;
}

void TConfig::setModbusRtuPort(char *port)
{
    if (port){
        strcpy(m_mbus_rtu_port, port);
    }
}
char *TConfig::getModbusRtuPort()
{
    return m_mbus_rtu_port;
}

// 波特率
void TConfig::setModbusBaud(int baud)
{
    m_mbus_baud = baud ;
}
int TConfig::getModbusBaud()
{
    return m_mbus_baud;
}
// 校验位
void TConfig::setModbusParity(char parity)
{
    m_mbus_parity = parity;
}
char TConfig::getModbusParity()
{
    return m_mbus_parity;
}
// 数据位
void TConfig::setModbusDataBit(int dbit)
{
    m_mbus_data_bit = dbit;
}
int TConfig::getModbusDataBit()
{
    return m_mbus_data_bit;
}
// 停止位
void TConfig::setModbusStopBit(int sbit)
{
    m_mbus_stop_bit = sbit;
}
int TConfig::getModbusStopBit()
{
    return m_mbus_stop_bit;
}
// Wifi 密码
void TConfig::setWifiPasswd(char *passwd)
{
    if (passwd)
        strcpy(m_mbus_wifi_pass, passwd);
}
char* TConfig::getWifiPasswd()
{
    return m_mbus_wifi_pass;
}

// IEC61850 Configuration
void TConfig::setIEDAddress(int idx, ST_IED_ADDR paddr)
{
    if (m_ied_count > HT_MAX_IED_COUNT) return;
    memcpy(&m_ied_addr[m_ied_count++], &paddr, sizeof(ST_IED_ADDR));
}
ST_IED_ADDR* TConfig::getIEDAddrsss(int idx)
{
    return (idx < m_ied_count ? &m_ied_addr[idx]:NULL);
}
void TConfig::setIEDCount(int c)
{
    if (c > HT_MAX_IED_COUNT || c < 0) return;
    m_ied_count = c;
}
int TConfig::getIEDCount()
{
    return m_ied_count;
}

void TConfig::setIEDModelConfigFile(char *ied_file_name)
{
    if (ied_file_name)
        strcpy(m_ied_model_config_file, ied_file_name);
}
char* TConfig::getIEDModelConfigFile()
{
    return m_ied_model_config_file;
}

// Comtrade file cache path
void TConfig::setComtradePath(char *path)
{
    if (path)
        strcpy(m_comtrade_path, path);
}
char* TConfig::getComtradePath()
{
    return m_comtrade_path ;
}
void TConfig::setComtradeBakPath(char *path)
{
    if (path)
        strcpy(m_comtrade_bak_path, path);
}
char* TConfig::getComtradeBakPath()
{
    return m_comtrade_bak_path;
}
void TConfig::setIEDComtradePath(char *path)
{
    if (path)
        strcpy(m_ied_comtrade_path, path);
}
char* TConfig::getIEDComtradePath()
{
    return m_ied_comtrade_path;
}

void TConfig::setIEDComtradeRemove(bool isRemove)
{
    b_ied_comtrade_remove = isRemove;
}
bool TConfig::getIEDComtradeRemove()
{
    return b_ied_comtrade_remove;
}

/*************************************************************************
 Function iniGetString
   Rectives a character string from the specified section in the
   specified inifile
 Param In     :
   pszSection :  address of section
   pszEntry   :  address of entry
   uiBufLen   :  size of destination buffer
   pszFileName:  address of inifile name
 Param Out    :
   pszRetBuf  :  destination buffer
 Return Code  :
   0          :  success
   <0         :  failed
*************************************************************************/
int TConfig::iniGetString(const char *pszSection, const char *pszEntry, char *pszRetBuf, unsigned int uiBufLen)
{
    FILE *fpIni;
    char szBuf[DEF_BUFFER_1K+1], *psz1, *psz2, *psz;
    int iSectFlag, iLen;

    if((fpIni= fopen(HT_CONFIG_FILE,"r")) == NULL)   return (-1);

    /*** check section ***/
    iSectFlag= 0;
    while(!feof(fpIni)) {
        if (fgets(szBuf,DEF_BUFFER_1K,fpIni) == NULL)   break;
        psz= szBuf;

        while(*psz!='[' && *psz!='#' && *psz!='\0')   psz++;
        if (*psz!='[')   continue;
        psz++;
        while(*psz==' ' || *psz=='\t')   psz++;
        psz1= psz;
        while(*psz!=']' && *psz!='\0')   psz++;
        if (*psz=='\0')  continue;
        while(*(psz-1)==' ' || *(psz-1)=='\t')   psz--;
        *psz= '\0';
        if (!strcmp(psz1, pszSection)) {
            iSectFlag= 1;
            break;
        }
    }/*** while ***/
    if (!iSectFlag) {
        fclose(fpIni);
        return (-1);
    }

    /*** check entry ***/
    while(!feof(fpIni))   {
        if(fgets(szBuf,DEF_BUFFER_1K,fpIni) == NULL)   break;
        psz= szBuf;
        while(*psz==' ' || *psz=='\t')   psz++;
        if (*psz=='#' || *psz=='\0')   continue;
        if (*psz=='[')   break;

        psz1= psz;
        while(*psz!='=' && *psz!='\0')   psz++;
        if (*psz=='\0')   continue;
        psz2= psz+1;
        if (psz1==psz)   continue;
        while(*(psz-1)==' ' || *(psz-1)=='\t')   psz--;
        *psz= '\0';

    #ifdef _WIN32
        if(strcmp(psz1,pszEntry))   continue;
    #else
        if(strcasecmp(psz1, pszEntry)) continue;
    #endif
        fclose(fpIni);

        psz= psz2;
        while(*psz==' ' || *psz=='\t')  psz++;
        psz2= psz;
        while(*psz!='#' && *psz!='\0' && !(*psz=='/' && (*(psz+1)=='*'||*(psz+1)=='/'))) psz++;
        while(*(psz-1)==' ' || *(psz-1)=='\t' || *(psz-1)==0x0a || *(psz-1)==0x0d) 
		{
			*(psz-1)= '\0';
			psz--;
		}
		//*psz= '\0';
        iLen= strlen(psz2);
        if (iLen==0) return (-1);
        if (iLen > (int)uiBufLen)   iLen= uiBufLen;
        memcpy(pszRetBuf, psz2, iLen);
        *(pszRetBuf+iLen)= '\0';
        return (0);
    }
    fclose(fpIni);
    return (-1);
}
// 加载服务配置参数
bool TConfig::getIniConfig()
{
    int iRet = -1;
    char szTmp[256];

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_LOGGER","logger", szTmp, sizeof(szTmp))) < 0) {
        printf("get logger parameter failed, %s\n", szTmp);
    }

    if(strstr(szTmp, "debug") == NULL)  setLogDebug(0x00);
    else setLogDebug(0x01);

    if(strstr(szTmp, "error") == NULL)  setLogError(0x00);
    else setLogError(0x01);

    if(strstr(szTmp, "warning") == NULL)  setLogWarning(0x00);
    else setLogWarning(0x01);
    
    if(strstr(szTmp, "package") == NULL)  setLogPack(0x00);
    else setLogPack(0x01);

    if(strstr(szTmp, "console") == NULL)  setLogConsole(0x00);
    else setLogConsole(0x01);
    
    if(strstr(szTmp, "message") == NULL)  setLogMessage(0x00);
    else setLogMessage(0x01);

    ////////////////////////////////////////////////////////////////////////////
    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_MONITOR","monitor_enable", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get monitor_enable parameter failed");
        return false;
    }
    if(stringcasecmp(szTmp, "true") == 0)
        setMonitor(true);    // 启用监控报文功能
    else setMonitor(false) ; 

    ////////////////////////////////////////////////////////////////////////////
    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_VERSION","ht_server_version", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get ht_server_version parameter failed");
        return false;
    }
    setServerVersion(szTmp);


    ////////////////////////////////////////////////////////////////////////////
    // 加载数据库配置参数
    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_DATABASE","db_host_addr", szTmp, sizeof(szTmp))) < 0) {
        // vPrtLogMsg(LOG_ERROR, iRet, "get db_host_addr parameter failed");
        // return false;
        setdbHostIP((char*)"127.0.0.1");
    }
    else setdbHostIP(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_DATABASE","db_host_port", szTmp, sizeof(szTmp))) < 0) {
        //vPrtLogMsg(LOG_ERROR, iRet, "get db_host_port parameter failed");
        //return false;
        setdbPort(3306);
    }
    else setdbPort((unsigned int)atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_DATABASE","db_name", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get db_name parameter failed");
        return false;
    }
    setdbName(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_DATABASE","db_username", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get db_username parameter failed");
        return false;
    }
    setdbUserName(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_DATABASE","db_password", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get db_password parameter failed");
        return false;
    }
    setdbUserPass(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_DATABASE","db_min_conn", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get db_min_conn parameter failed");
        return false;
    }
    setdbMinConnect((unsigned int)atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_DATABASE","db_max_conn", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get db_max_conn parameter failed");
        return false;
    }
    setdbMaxConnect((unsigned int)atoi(szTmp));

    ////////////////////////////////////////////////////////////////////////////
    // 加载服务器端参数
    memset(szTmp, 0x00, sizeof(szTmp));
    if ((iRet = iniGetString("HT_LOCAL_HOST", "local_host_addr", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get local_host_addr parameter failed");
        return false;
    }
    setLocalAddr(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));  // 服务器端口
    if ((iRet = iniGetString("HT_LOCAL_HOST", "local_tcp_port", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get local_tcp_port parameter failed,set define:2404!");
        strcpy(szTmp, "2404");
        //return false;
    }
    setLocalPort((unsigned int)atoi(szTmp));

    // 加载服务器端参数
    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_REMOTE_HOST","remote_host_addr", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get remote_host_addr parameter failed");
        return false;
    }
    setRemoteAddr(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));  // 服务器端口
    if((iRet = iniGetString("HT_REMOTE_HOST","remote_host_port", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get remote_host_port parameter failed,set define:2404!");
        strcpy(szTmp, "2404") ;
        //return false;
    }
    setRemotePort((unsigned int)atoi(szTmp));

    ////////////////////////////////////////////////////////////////////////////
    // socket 相关属性参数配置
    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_SOCKET","max_epoll_limit", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get max_epoll_limit parameter failed");
        return false;
    }
    setFdLimit((unsigned int)atoi(szTmp));
    
    memset(szTmp, 0x00, sizeof(szTmp));
    if ((iRet = iniGetString("HT_SOCKET", "timeout_t0", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get timeout_t0 parameter failed");
        return false;
    }
    setTimeout0((unsigned int)atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_SOCKET","timeout_t1", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get timeout_t1 parameter failed");
        return false;
    }
    setTimeout1((unsigned int)atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_SOCKET","timeout_t2", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get timeout_t2 parameter failed");
        return false;
    }
    setTimeout2((unsigned int)atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_SOCKET","timeout_t3", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get timeout_t3 parameter failed");
        return false;
    }
    setTimeout3((unsigned int)atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));
    if ((iRet = iniGetString("HT_SOCKET", "iec_k", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get iec_k parameter failed");
        return false;
    }
    setK((unsigned int)atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));
    if ((iRet = iniGetString("HT_SOCKET", "iec_w", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get iec_w parameter failed");
        return false;
    }
    setW((unsigned int)atoi(szTmp));
    ////////////////////////////////////////////////////////////////////////////
    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_THREAD_NUMBER","thread_recv_num", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get thread_recv_num parameter failed");
        return false;
    }
    setRecvThreadNumber((unsigned int)atoi(szTmp));
    
    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_THREAD_NUMBER","thread_parse_num", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get thread_parse_num parameter failed");
        return false;
    }
    setParseThreadNumber((unsigned int)atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));
    if((iRet = iniGetString("HT_THREAD_NUMBER","thread_opration_num", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get thread_opration_num parameter failed");
        return false;
    }
    setOprationThreadNumber((unsigned int)atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));  //
    if((iRet = iniGetString("HT_THREAD_NUMBER","thread_respone_num", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get thread_respone_num parameter failed");
        return false;
    }
    setResponeThreadNumber((unsigned int)atoi(szTmp));
    
    ////////////////////////////////////////////////////////////////////////////
    
    // Modbus configure
    memset(szTmp, 0x00, sizeof(szTmp));  // TCP\RTU
    if ((iRet = iniGetString("MODBUS_CONF", "modbus_mode", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get modbus_mode parameter failed");
        return false;
    }
    setModbusMode(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));  // addr
    if ((iRet = iniGetString("MODBUS_CONF", "modbus_tcp_addr", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get modbus_tcp_addr parameter failed");
        return false;
    }
    setModbusTcpAddr(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));  // tcp_port
    if ((iRet = iniGetString("MODBUS_CONF", "modbus_tcp_port", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get modbus_tcp_port parameter failed");
        return false;
    }
    setModbusTcpPort(atoi(szTmp));

    memset(szTmp, 0x00, sizeof(szTmp));  // rtu_port
    if ((iRet = iniGetString("MODBUS_CONF", "modbus_rtu_port", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get modbus_rtu_port parameter failed");
        return false;
    }
    setModbusRtuPort(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));  // rtu_port
    if ((iRet = iniGetString("MODBUS_CONF", "modbus_baud", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get modbus_baud parameter failed");
        return false;
    }
    setModbusBaud(atoi(szTmp));


    memset(szTmp, 0x00, sizeof(szTmp));  // parity
    if ((iRet = iniGetString("MODBUS_CONF", "modbus_parity", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get modbus_parity parameter failed");
        return false;
    }
    setModbusParity(szTmp[0]);

    memset(szTmp, 0x00, sizeof(szTmp));  // modbus_databit
    if ((iRet = iniGetString("MODBUS_CONF", "modbus_databit", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get modbus_databit parameter failed");
        return false;
    }
    setModbusDataBit(atoi(szTmp));


    memset(szTmp, 0x00, sizeof(szTmp));  // modbus_stopbit
    if ((iRet = iniGetString("MODBUS_CONF", "modbus_stopbit", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get modbus_stopbit parameter failed");
        return false;
    }
    setModbusStopBit(atoi(szTmp));
    /***************************************************************************************/
    // IEC61850 configuration
    memset(szTmp, 0x00, sizeof(szTmp));  // IED model config file(.cfg)
    if ((iRet = iniGetString("HT61850_CONF", "ied_model_config_file", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get ied_model_config_file parameter failed");
        return false;
    }
    setIEDModelConfigFile(szTmp);
    
    memset(szTmp, 0x00, sizeof(szTmp));  // IED comtrade file rootpath config)
    if ((iRet = iniGetString("HT61850_CONF", "ied_comtrade_path", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get ied_comtrade_path parameter failed");
        return false;
    }
    setIEDComtradePath(szTmp);

    memset(szTmp, 0x00, sizeof(szTmp));  // IED comtrade file rootpath config)
    if ((iRet = iniGetString("HT61850_CONF", "ied_comtrade_path", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get ied_comtrade_path parameter failed");
        return false;
    }
    if (stringncasecmp(szTmp, "true", 4) == 0)  setIEDComtradeRemove(true);
    else setIEDComtradeRemove(false);

    memset(szTmp, 0x00, sizeof(szTmp));  // comtrade file cache path
    if ((iRet = iniGetString("HT61850_CONF", "comtrade_path", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get comtrade_path parameter failed");
        return false;
    }
    setComtradePath(szTmp);
    
    memset(szTmp, 0x00, sizeof(szTmp));  // comtrade file bak path
    if ((iRet = iniGetString("HT61850_CONF", "comtrade_bak_path", szTmp, sizeof(szTmp))) < 0) {
        vPrtLogMsg(LOG_ERROR, iRet, "get comtrade_bak_path parameter failed");
        return false;
    }
    setComtradeBakPath(szTmp);
    return true;
}

void TConfig::showConfig()
{
//     vPrtLogMsg(LOG_DEBUG, 0, "local_server_name=%s local_host_addr=%s local_tcp_port=%d\n"
//         "db_host_addr=%s db_port=%d db_name=%s db_username=%s db_password=%s\n"
//         "db_min_conn=%d db_max_conn=%d max_epoll_limit=%d loacl_active_interval=%d\n"
//         "local_tcp_recv_timeout=%d local_udp_recv_timeout=%d\n"
//         //"gov_user_name=%s gov_user_pass=%s gov_host_addr=%s gov_tcp_port=%d gov_tcp_recv_timeout=%d",
//         "gov_tcp_recv_timeout=%d",
//         m_LocalName, m_LocalAddr, m_LocalTcpPort,
//         m_dbHostIP,m_dbPort,m_dbName,m_dbUserName,m_dbUserPass,m_dbMinConnect,m_dbMaxConnect,
//         m_fd_limit,m_active_interval,m_tcp_recv_timeout,m_udp_recv_timeout,
//         //m_GovUserName,m_GovUserPass,m_GovHostAddr,m_GovTcpPort,m_GovTcpRecvTimeout);
//         m_GovTcpRecvTimeout);
}
