/****************************************************************************
** File name    :  HTMmslite.cpp
** Description  :  Sisco mms IEC 61850 库API
**              ： 第三方库
** Create date  :  2019.09.01
** Auther by    :  Liuyx
** Version info :  V1.0.01
** Copyrigth By:  xi'an huatek, Inc Co., Ltd
** Update record:
** DATE         AUTHER   DESC
** -------------------------------------------------------------------------
** 2019.09.01   Liuyx    first build
****************************************************************************/
#include "HTGlobal.h"
#include "HTMmsMapping.h"

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


/*******************************************************************************
** Function Name :  load_sisco_mmslib
** Description   :  加载61850库
** Parameter In  :  None
**   param name  :  
** Parameter Out :  None
**   param name  :  
** Return Code   :  void *
**        !NULL  :  Successed
**         NULL  :  Failed
** Create by     :  Liuyx
** Create date   :  2020:7:22 13:40
** History modify:  Liuyx
** Copyright (C), 2012-2020, Xi'an Huatek. Co., Ltd.
**-----------------------------------------------------------------------
** Modify by     :  Date            : Context
** Liuyx  :  2020/07/22          : Created
********************************************************************************/
static ST_VOID *load_sisco_mmslib()
{
    ST_VOID *dl_handle = NULL;
#if (defined(_WIN32) || defined(_MSC_VER))
    dl_handle = LoadLibrary(TEXT("cosmos_ld.dll"));
#elif (defined(__UNIX__) || defined(UNIX) || defined(LINUX) || defined(linux))
    //ST_VOID *dl_handle = NULL;
    ST_CHAR *stError = NULL;
    dl_handle = dlopen("libcosmos_ld.so", RTLD_LAZY);
    stError = dlerror();
    if (stError)
    {
        dl_handle = dlopen("libcosmos_ld.so", RTLD_LAZY);
        stError = dlerror();
        if (stError)
        {
            printf("error:%s", stError);
        }
    }
#else
    printf("Warn : ## This is not a supported platform!");
#endif
    return dl_handle;
}


/*******************************************************************************
** Function Name :  unload_sisco_mmslib
** Description   :  释放61850库
** Parameter In  :  dl_handle
**   param name  :  
** Parameter Out :  None
**   param name  :  
** Return Code   :  void
** Create by     :  liuyx
** Create date   :  2020:7:22 15:20
** History modify:  liuyx
** Copyright (C), 2012-2020, Xi'an Huatek. Co., Ltd.
**-----------------------------------------------------------------------
** Modify by     :  Date            : Context
** liuyx  :  2020/07/22          : created
********************************************************************************/
void  unload_sisco_mmslib(ST_VOID *dl_handle)
{
#if (defined(_WIN32) || defined(_MSC_VER))
    FreeLibrary((HMODULE)dl_handle);
#elif (defined(__UNIX__) || defined(UNIX) || defined(LINUX) || defined(linux))
    dlclose(dl_handle);
#else
    printf("Warn : ## This is not a supported platform!");
#endif
}

void *get_sisco_func_addr(ST_VOID *dl_handle, ST_CHAR *fun_name)
{
#if (defined(_WIN32) || defined(_MSC_VER))
    return GetProcAddress((HMODULE)dl_handle, fun_name);
#elif (defined(__UNIX__) || defined(UNIX) || defined(LINUX) || defined(linux))
    return dlsym(dl_handle, fun_name);
#else
    printf("Warn : ## This is not a supported platform!");
#endif
}

ST_VOID  *getFuncAddr(ST_VOID *dp_handle, ST_CHAR *fun_name)
{
#if (defined(_WIN32) || defined(_MSC_VER))
    return GetProcAddress((HMODULE)dp_handle, fun_name);
#elif (defined(__UNIX__) || defined(UNIX) || defined(LINUX) || defined(linux))
    return dlsym(dp_handle, fun_name);
#else
    printf("Warn : ## This is not a supported platform!");
#endif
}
/*******************************************************************************
** Function Name :  init_mmslite
** Description   :  初始化mmslite环境
** Parameter In  :  加载库句柄：dl_handle
**   param name  :  
** Parameter Out :  None
**   param name  :  
** Return Code   :  void
**        RET_OK :  Successed
**      RET_FAIL :  Failed
** Create by     :  Liuyx
** Create date   :  2020:7:27 14:13
** History modify:  Liuyx
** Copyright (C), 2012-2020, Xi'an Huatek. Co., Ltd.
**-----------------------------------------------------------------------
** Modify by     :  Date            : Context
** Liuyx  :  2020/07/27          : created
********************************************************************************/
void init_mmslite(ST_VOID *dl_handle)
{
    typedef ST_RET(*ai_init_envir_type)(ST_CHAR*);
    // 为函数指针声明类型定义
    typedef void(*pfv) ();

    if (!dl_handle) {
        vPrtLogMsg(LOG_ERROR, RET_FAIL, "init_mmslite faile!");
        return;
    }
    ai_init_envir_type ai_init_envir_ad = NULL;
    ai_init_envir_ad = (ai_init_envir_type)getFuncAddr(dl_handle, (ST_CHAR*)"ai_init_envir");
    
}
/*****************************************************************************
* thread_iec61850_service_proc - iec61850 服务线程
******************************************************************************/
void *thread_iec61850_service_proc(void *arg)
{
    vPrtLogMsg(LOG_DEBUG, 0, "thread_iec61850_service_proc = %d startup...", GETTID());
    return NULL;
}

