233 lines
6.2 KiB
C++
Raw Normal View History

2025-03-12 11:08:50 +08:00
/**
@file DogAuthImpl.cpp
@brief
@author
*/
#include "DogAuthImpl.h"
#include "Common.h"
#include "public/pub_logger_api/logger.h"
#include "pub_utility_api/CommonConfigParse.h"
#include "pub_utility_api/FileUtil.h"
#include <QString>
2025-03-12 16:30:28 +08:00
#include "boost/concept_check.hpp"
2025-03-12 11:08:50 +08:00
#ifndef DISABLE_DOG_AUTH
#ifdef KBD_X86
#include "sys_dog_auth_api/vendor_code.h"
#endif
#endif
2025-03-12 16:30:28 +08:00
#define AUTH_MODEL_LICENCE 1
#define AUTH_MODEL_SUPPERDOG 2
2025-03-12 11:08:50 +08:00
#define MEM_MEMBUFFER_SIZE 128
#define PRODUCT_ID_PLAT 40
#define PRODUCT_ID_ISCS 41
#define PRODUCT_ID_PSCADA 42
#define PRODUCT_ID_EMS 43
#define PRODUCT_ID_PSMS 44
2025-03-12 14:17:01 +08:00
iot_sys::CDogAuthImpl::CDogAuthImpl()
2025-03-12 11:08:50 +08:00
{
#ifndef DISABLE_DOG_AUTH
m_nProductId = PRODUCT_ID_PSCADA;
m_ptrLicenseAuth = NULL;
#ifdef KBD_X86
m_dogStatus = DOG_NOT_FOUND ;
m_dogHandle = 0 ;
#endif
#endif
}
2025-03-12 14:17:01 +08:00
iot_sys::CDogAuthImpl::~CDogAuthImpl()
2025-03-12 11:08:50 +08:00
{
#ifndef DISABLE_DOG_AUTH
#ifdef KBD_X86
if (m_dogHandle )
{
dog_logout(m_dogHandle);
m_dogStatus = DOG_NOT_FOUND ;
m_dogHandle = 0 ;
}
#endif
#endif
}
//检查系统授权状态
2025-03-12 14:17:01 +08:00
int iot_sys::CDogAuthImpl::checkAuthStatus()
2025-03-12 11:08:50 +08:00
{
2025-03-12 16:30:28 +08:00
// if(iotSuccess == checkDogAuthStatus())
// {
// return iotSuccess;
// }
if(iotSuccess == checLicenseAuthStatus())
{
return iotSuccess;
}
return iotFailed;
2025-03-12 11:08:50 +08:00
}
/**
@brief License授权状态
2025-03-12 16:30:28 +08:00
@return iotSuccess,
2025-03-12 11:08:50 +08:00
*/
2025-03-12 14:17:01 +08:00
int iot_sys::CDogAuthImpl::checLicenseAuthStatus()
2025-03-12 11:08:50 +08:00
{
#ifndef DISABLE_DOG_AUTH
2025-03-12 16:30:28 +08:00
QString strAppId ;
2025-03-12 11:08:50 +08:00
2025-03-12 14:17:01 +08:00
std::string strLicensePath = iot_public::CFileUtil::getAbsolutePath( "../../product/common/license/" ) ;
2025-03-12 16:30:28 +08:00
if(!m_ptrLicenseAuth->checkRegistration(QString::fromStdString(strLicensePath), "license.txt",strAppId) )
2025-03-12 11:08:50 +08:00
{
2025-03-12 16:30:28 +08:00
LOGINFO("授权校验失败,AppId=[%s]\n",strAppId.toStdString().c_str());
return iotFailed;
2025-03-12 11:08:50 +08:00
}
2025-03-12 16:30:28 +08:00
if(strAppId == "PSCADA" || strAppId == "EMS")
2025-03-12 11:08:50 +08:00
{
2025-03-12 16:30:28 +08:00
return iotSuccess;
2025-03-12 11:08:50 +08:00
}
2025-03-12 16:30:28 +08:00
else
2025-03-12 11:08:50 +08:00
{
2025-03-12 16:30:28 +08:00
LOGINFO("授权产品ID不匹配,AppId=[%s]\n", strAppId.toStdString().c_str());
return iotFailed;
2025-03-12 11:08:50 +08:00
}
#endif
2025-03-12 16:30:28 +08:00
return iotSuccess;
2025-03-12 11:08:50 +08:00
}
/**
@brief
2025-03-12 16:30:28 +08:00
@return iotSuccess,
2025-03-12 11:08:50 +08:00
*/
2025-03-12 14:17:01 +08:00
int iot_sys::CDogAuthImpl::checkDogAuthStatus()
2025-03-12 11:08:50 +08:00
{
#ifndef DISABLE_DOG_AUTH
#ifdef KBD_X86
unsigned char membuffer[MEM_MEMBUFFER_SIZE];
//如果狗状态异常,进行一次登录操作
//====================================================================================
if (DOG_STATUS_OK != m_dogStatus) //读写失败返回错误码
{
loginSupperDog(m_nProductId);
}
//< //狗状态正常:dog_read read from data file
//====================================================================================
m_dogStatus = dog_read(m_dogHandle,DOG_FILEID_RW, /* file ID */0, /* offset */
MEM_MEMBUFFER_SIZE,/* length */&membuffer[0]); /* file data */
switch (m_dogStatus)
{
case DOG_STATUS_OK:
LOGDEBUG("超级狗文件读写状态:OK\n");
break;
case DOG_INV_HND:
LOGDEBUG("超级狗文件读写状态:handle not active\n");
break;
case DOG_INV_FILEID:
LOGDEBUG("超级狗文件读写状态:invalid file id\n");
break;
case DOG_MEM_RANGE:
LOGDEBUG("超级狗文件读写状态:exceeds data file range\n");
break;
case DOG_NOT_FOUND:
LOGDEBUG("超级狗文件读写状态:key/license container not available\n");
break;
default:
LOGDEBUG("超级狗文件读写状态:read memory failed\n");
}
2025-03-12 16:30:28 +08:00
if (DOG_STATUS_OK != m_dogStatus) { //读写失败返回错误码
return m_dogStatus; //
}
2025-03-12 11:08:50 +08:00
#else
2025-03-12 16:30:28 +08:00
return iotFailed;
2025-03-12 11:08:50 +08:00
#endif
#endif
2025-03-12 16:30:28 +08:00
return iotSuccess;
2025-03-12 11:08:50 +08:00
}
/* @brief 初始化 */
2025-03-12 14:17:01 +08:00
int iot_sys::CDogAuthImpl::initialize()
2025-03-12 11:08:50 +08:00
{
#ifndef DISABLE_DOG_AUTH
//获取软件产品编号 不同编号对应不同ID
//====================================================================================
2025-03-12 14:17:01 +08:00
iot_public::CCommonConfigParse objCfgParse;
2025-03-12 11:08:50 +08:00
//加载配置文件,失败则使用默认值,构造函数中已设置 m_nProductId = PRODUCT_ID_PSCADA
2025-03-12 14:54:22 +08:00
if(iotSuccess == objCfgParse.load(
2025-03-12 14:17:01 +08:00
iot_public::CFileUtil::getPathOfCfgFile("setup_config.xml",CN_DIR_PRODUCT)))
2025-03-12 11:08:50 +08:00
{
//获取产品ID
2025-03-12 14:54:22 +08:00
if(iotSuccess != objCfgParse.getIntValue("setup_config","software_product_id", m_nProductId))
2025-03-12 11:08:50 +08:00
{
LOGWARN("CDogAuthImpl::initialize() get product id fail, use default.\n");
}
}
else
{
LOGWARN("CDogAuthImpl::initialize() load setup_config.xml failed, use default.\n");
}
m_ptrLicenseAuth = getLicenseAuthInstance(); //
if(NULL == m_ptrLicenseAuth)
{
LOGERROR("CDogAuthImpl::initialize() getLicenseAuthInstance fail .\n");
2025-03-12 14:54:22 +08:00
return iotFailed;
2025-03-12 11:08:50 +08:00
}
#endif
2025-03-12 14:54:22 +08:00
return iotSuccess;
2025-03-12 11:08:50 +08:00
}
/**
* @brief :CDogAuthImpl::loginSupperDog()
*
2025-03-12 14:54:22 +08:00
* @return iotSuccessiotFailed
2025-03-12 11:08:50 +08:00
*/
2025-03-12 14:17:01 +08:00
int iot_sys::CDogAuthImpl::loginSupperDog(int nProductId)
2025-03-12 11:08:50 +08:00
{
2025-03-12 16:30:28 +08:00
boost::ignore_unused_variable_warning(nProductId);
2025-03-12 11:08:50 +08:00
#ifndef DISABLE_DOG_AUTH
#ifdef KBD_X86
//超级狗登陆状态不正常,退出
//====================================================================================
if (m_dogStatus != DOG_STATUS_OK) //登陆状态不正常
{
//< 重新登陆
LOGDEBUG("初始化加密狗授权信息产品ID[%d].\n ", nProductId);
m_dogStatus = dog_login(nProductId, (dog_vendor_code_t *)vendorCode, &m_dogHandle);
switch (m_dogStatus)
{
case DOG_STATUS_OK:
LOGDEBUG("超级狗登陆状态:OK\n");
break;
case DOG_FEATURE_NOT_FOUND:
LOGDEBUG("超级狗登陆状态:login to default feature failed\n");
break;
case DOG_NOT_FOUND:
LOGDEBUG("超级狗登陆状态:no SuperDog with vendor code DEMOMA found\n");
break;
case DOG_INV_VCODE:
LOGDEBUG("超级狗登陆状态:invalid vendor code\n");
break;
case DOG_LOCAL_COMM_ERR:
LOGDEBUG("超级狗登陆状态:communication error between API and local SuperDog License Manager\n");
break;
default:
LOGDEBUG("超级狗登陆状态:login to default feature failed with status %d\n", m_dogStatus);
}
if (DOG_STATUS_OK != m_dogStatus) //登陆失败,句柄=-1
{
dog_logout(m_dogHandle);
}
}
#else
2025-03-12 14:54:22 +08:00
return iotFailed;
2025-03-12 11:08:50 +08:00
#endif
#endif
2025-03-12 14:54:22 +08:00
return iotSuccess;
2025-03-12 11:08:50 +08:00
}