123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- #include "DeviceAdaptChecker.h"
- #include "fileutil.h"
- #include "PinPadClass.h"
- #include "CardIssuerClassPrivate.h"
- #include "IDCerClass.h"
- #include "RFICClass.h"
- #include "HSPScannerClass.h"
- #include "FingerPrintClass.h"
- #include "WatchDogClass.h"
- #include "GpioClass.h"
- #include "UpsClass.h"
- ErrorCodeEnum CAdapterLoadChecker::EditLDLibraryPathForVendor(LPCTSTR filePath, BOOL fAdd)
- {
- if (filePath == NULL || strlen(filePath) == 0) {
- return Error_Param;
- }
- const char* name = "LD_LIBRARY_PATH";
- char szOldValue[1025] = { '\0' };
- size_t size = 1024;
- int result = toolkit_getenv(name, szOldValue, &size);
- if (result != 0) {
- Dbg("getenv %s failed: %s", name, toolkit_strerror(result));
- if (!fAdd) {
- return Error_Succeed;
- }
- if (result == TOOLKIT_ENOENT) {
- return Error_NotExist;
- } else {
- return Error_Unexpect;
- }
- }
- Dbg("env(%s)=%s", name, szOldValue);
- CSimpleStringA strTmp(szOldValue);
- CSimpleStringA strNewPath(filePath);
- if (strTmp.IsStartWith(strNewPath)) {
- Dbg("already exist at front!!!!");
- if (fAdd) {
- return Error_AlreadyExist;
- } else {
- strNewPath = strTmp.SubString(strNewPath.GetLength() + 1, strTmp.GetLength() - 1);
- }
- } else if (strTmp.IndexOf(strNewPath) != -1) {
- Dbg("already exist at inner!");
- return Error_InvalidState;
- } else if (fAdd) {
- strNewPath += ":";
- strNewPath += strTmp;
- } else {
- return Error_Succeed;
- }
- FINALLY:
- result = toolkit_setenv(name, strNewPath);
- if (result != 0) {
- Dbg("setenv failed: %s", toolkit_strerror(result));
- return Error_Unexpect;
- }
- size = 1024;
- memset(szOldValue, '\0', sizeof(szOldValue));
- toolkit_getenv(name, szOldValue, &size);
- Dbg("finally:{%s}", szOldValue);
- return Error_Succeed;
- }
- ErrorCodeEnum CAdapterLoadChecker::CheckDeviceAndAdapterAvailable(const VendorLibInfoEx& libInfo, CSimpleStringA& strErrMsg)
- {
- ErrorCodeEnum result(Error_Succeed);
- int select = -1;
- for (int i = 0; libInfo.IsValid() && i < array_size(adapterList); ++i) {
- if (0 == strcmp(adapterList[i], libInfo.strDevice)) {
- select = i;
- break;
- }
- }
- CSimpleStringA strLibFullPath(libInfo.optCfg.strDepDirWithSlashOrFileName + libInfo.toLibNameString());
- if (!ExistsFileA(strLibFullPath)) {
- strErrMsg = CSimpleStringA::Format("%s 路径下找不到 %s 文件",
- libInfo.optCfg.strDepDirWithSlashOrFileName.GetData(), libInfo.toLibNameString().GetData());
- return Error_NotExist;
- }
- //bool toClearEnv(false);
- //CSimpleStringA strDepFullPath(libInfo.optCfg.strDepDirWithSlashOrFileName + libInfo.strVendor);
- //if (ExistsDirA(strDepFullPath)) {
- // ErrorCodeEnum ec = EditLDLibraryPathForVendor(strDepFullPath, TRUE);
- // if (ec == Error_Succeed || ec == Error_AlreadyExist) {
- // toClearEnv = true;
- // } else {
- // strErrMsg = CSimpleStringA::Format("设置适配器库加载路径失败:%s", SpStrError(ec));
- // return ec;
- // }
- //} else {
- // Dbg("{%s} dir not exists!", strDepFullPath.GetData());
- //}
-
- switch (select) {
- case 0: //PinPad
- {
- DevAdptLibHelper<PinPadClass> devImpl;
- result = devImpl.LoadUp(strLibFullPath);
- Dbg("Load returned: %s", SpStrError(result));
- if (Error_Succeed == result) {
- Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- result = devImpl->DevOpen(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- Dbg("open result: %s", SpStrError(result));
- if (result != Error_Succeed) {
- DevErrorInfo devErrInfo;
- memset(&devErrInfo, 0, sizeof(DevErrorInfo));
- devImpl->GetLastErr(devErrInfo);
- strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
- }
- devImpl.TearDown();
- } else {
- strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
- }
- }
- break;
- case 1: //CardIssuer
- {
- DevAdptLibHelper<CardIssuerClass> devImpl;
- result = devImpl.LoadUp(strLibFullPath);
- Dbg("Load returned: %s", SpStrError(result));
- if (Error_Succeed == result) {
- Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- BYTE noUse;
- result = devImpl->DevOpenEx(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate, DEV_OPEN_TYPE_COM, "", noUse);
- Dbg("open result: %s", SpStrError(result));
- if (result != Error_Succeed) {
- DevErrorInfo devErrInfo;
- memset(&devErrInfo, 0, sizeof(DevErrorInfo));
- devImpl->GetLastErr(devErrInfo);
- strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
- }
- devImpl.TearDown();
- } else {
- strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
- }
-
- }
- break;
- case 2: //IDCertificate
- {
- DevAdptLibHelper<IDCerClass> devImpl;
- result = devImpl.LoadUp(strLibFullPath);
- Dbg("Load returned: %s", SpStrError(result));
- if (Error_Succeed == result) {
- Dbg("%s: to open %p, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort);
- result = devImpl->DevOpen(libInfo.optCfg.dwPort);
- Dbg("open result: %s", SpStrError(result));
- if (result != Error_Succeed) {
- DevErrorInfo devErrInfo;
- memset(&devErrInfo, 0, sizeof(DevErrorInfo));
- devImpl->GetLastErr(devErrInfo);
- strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
- }
- devImpl.TearDown();
- } else {
- strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
- }
- }
- break;
- case 3: //ContactlessCard
- {
- DevAdptLibHelper<RFICClass> devImpl;
- result = devImpl.LoadUp(strLibFullPath);
- Dbg("Load returned: %s", SpStrError(result));
- if (Error_Succeed == result) {
- Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- result = devImpl->DevOpen(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- Dbg("open result: %s", SpStrError(result));
- if (result != Error_Succeed) {
- DevErrorInfo devErrInfo;
- memset(&devErrInfo, 0, sizeof(DevErrorInfo));
- devImpl->GetLastErr(devErrInfo);
- strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
- }
- devImpl.TearDown();
- } else {
- strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
- }
- }
- break;
- case 4: //HSPScanner
- {
- DevAdptLibHelper<CHSPSClass> devImpl;
- result = devImpl.LoadUp(strLibFullPath);
- Dbg("Load returned: %s", SpStrError(result));
- if (Error_Succeed == result) {
- Dbg("%s: to open %p", __FUNCTION__, devImpl.GetDevPointer());
- result = devImpl->DevOpen();
- Dbg("open result: %s", SpStrError(result));
- if (result != Error_Succeed) {
- DevErrorInfo devErrInfo;
- memset(&devErrInfo, 0, sizeof(DevErrorInfo));
- devImpl->GetLastErr(devErrInfo);
- strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
- }
- devImpl.TearDown();
- } else {
- strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
- }
- }
- break;
- case 5: //FingerPrint
- {
- DevAdptLibHelper<FingerPrintClass> devImpl;
- result = devImpl.LoadUp(strLibFullPath);
- Dbg("Load returned: %s", SpStrError(result));
- if (Error_Succeed == result) {
- Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- result = devImpl->DevOpen(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- Dbg("open result: %s", SpStrError(result));
- if (result != Error_Succeed) {
- DevErrorInfo devErrInfo;
- memset(&devErrInfo, 0, sizeof(DevErrorInfo));
- devImpl->GetLastErr(devErrInfo);
- strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
- }
- devImpl.TearDown();
- } else {
- strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
- }
- }
- break;
- case 6: //WatchDog
- {
- DevAdptLibHelper<WatchDogClass> devImpl;
- result = devImpl.LoadUp(strLibFullPath);
- Dbg("Load returned: %s", SpStrError(result));
- if (Error_Succeed == result) {
- Dbg("%s: to open %p", __FUNCTION__, devImpl.GetDevPointer());
- result = devImpl->DevOpen();
- Dbg("open result: %s", SpStrError(result));
- if (result != Error_Succeed) {
- DevErrorInfo devErrInfo;
- memset(&devErrInfo, 0, sizeof(DevErrorInfo));
- devImpl->GetLastErr(devErrInfo);
- strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
- }
- devImpl.TearDown();
- } else {
- strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
- }
- }
- break;
- case 7: //Gpio
- {
- DevAdptLibHelper<GpioClass> devImpl;
- result = devImpl.LoadUp(strLibFullPath);
- Dbg("Load returned: %s", SpStrError(result));
- if (Error_Succeed == result) {
- ///**TODO(Gifur@10/20/2021): 这里只针对UOS和新款Win大机 */
- GpioInitParam param = { libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate, 4, {true, true, false, true} };
- Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- result = devImpl->DevOpen(param);
- Dbg("open result: %s", SpStrError(result));
- if (result != Error_Succeed) {
- DevErrorInfo devErrInfo;
- memset(&devErrInfo, 0, sizeof(DevErrorInfo));
- devImpl->GetLastErr(devErrInfo);
- strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
- }
- devImpl.TearDown();
- } else {
- strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
- }
- }
- break;
- case 8: //Ups
- {
- DevAdptLibHelper<UpsClass> devImpl;
- result = devImpl.LoadUp(strLibFullPath);
- Dbg("Load returned: %s", SpStrError(result));
- if (Error_Succeed == result) {
- Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- result = devImpl->DevOpen(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
- Dbg("open result: %s", SpStrError(result));
- if (result != Error_Succeed) {
- DevErrorInfo devErrInfo;
- memset(&devErrInfo, 0, sizeof(DevErrorInfo));
- devImpl->GetLastErr(devErrInfo);
- strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
- }
- devImpl.TearDown();
- } else {
- strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
- }
- }
- break;
- default:
- result = Error_NotSupport;
- strErrMsg = CSimpleStringA::Format("%s: %s", libInfo.strDevice.GetData(), SpStrError(result));
- break;
- }
- //if (toClearEnv) {
- // ErrorCodeEnum ec = EditLDLibraryPathForVendor(strDepFullPath, FALSE);
- // Dbg("Clear Env Path returned: %s", SpStrError(ec));
- //}
- return result;
- }
|