DeviceAdapterCheckerImpl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "DeviceAdaptChecker.h"
  2. #include "fileutil.h"
  3. #include "PinPadClass.h"
  4. #include "CardIssuerClassPrivate.h"
  5. #include "IDCerClass.h"
  6. #include "RFICClass.h"
  7. #include "HSPScannerClass.h"
  8. #include "FingerPrintClass.h"
  9. #include "WatchDogClass.h"
  10. #include "GpioClass.h"
  11. #include "UpsClass.h"
  12. ErrorCodeEnum CAdapterLoadChecker::EditLDLibraryPathForVendor(LPCTSTR filePath, BOOL fAdd)
  13. {
  14. if (filePath == NULL || strlen(filePath) == 0) {
  15. return Error_Param;
  16. }
  17. const char* name = "LD_LIBRARY_PATH";
  18. char szOldValue[1025] = { '\0' };
  19. size_t size = 1024;
  20. int result = toolkit_getenv(name, szOldValue, &size);
  21. if (result != 0) {
  22. Dbg("getenv %s failed: %s", name, toolkit_strerror(result));
  23. if (!fAdd) {
  24. return Error_Succeed;
  25. }
  26. if (result == TOOLKIT_ENOENT) {
  27. return Error_NotExist;
  28. } else {
  29. return Error_Unexpect;
  30. }
  31. }
  32. Dbg("env(%s)=%s", name, szOldValue);
  33. CSimpleStringA strTmp(szOldValue);
  34. CSimpleStringA strNewPath(filePath);
  35. if (strTmp.IsStartWith(strNewPath)) {
  36. Dbg("already exist at front!!!!");
  37. if (fAdd) {
  38. return Error_AlreadyExist;
  39. } else {
  40. strNewPath = strTmp.SubString(strNewPath.GetLength() + 1, strTmp.GetLength() - 1);
  41. }
  42. } else if (strTmp.IndexOf(strNewPath) != -1) {
  43. Dbg("already exist at inner!");
  44. return Error_InvalidState;
  45. } else if (fAdd) {
  46. strNewPath += ":";
  47. strNewPath += strTmp;
  48. } else {
  49. return Error_Succeed;
  50. }
  51. FINALLY:
  52. result = toolkit_setenv(name, strNewPath);
  53. if (result != 0) {
  54. Dbg("setenv failed: %s", toolkit_strerror(result));
  55. return Error_Unexpect;
  56. }
  57. size = 1024;
  58. memset(szOldValue, '\0', sizeof(szOldValue));
  59. toolkit_getenv(name, szOldValue, &size);
  60. Dbg("finally:{%s}", szOldValue);
  61. return Error_Succeed;
  62. }
  63. ErrorCodeEnum CAdapterLoadChecker::CheckDeviceAndAdapterAvailable(const VendorLibInfoEx& libInfo, CSimpleStringA& strErrMsg)
  64. {
  65. ErrorCodeEnum result(Error_Succeed);
  66. int select = -1;
  67. for (int i = 0; libInfo.IsValid() && i < array_size(adapterList); ++i) {
  68. if (0 == strcmp(adapterList[i], libInfo.strDevice)) {
  69. select = i;
  70. break;
  71. }
  72. }
  73. CSimpleStringA strLibFullPath(libInfo.optCfg.strDepDirWithSlashOrFileName + libInfo.toLibNameString());
  74. if (!ExistsFileA(strLibFullPath)) {
  75. strErrMsg = CSimpleStringA::Format("%s 路径下找不到 %s 文件",
  76. libInfo.optCfg.strDepDirWithSlashOrFileName.GetData(), libInfo.toLibNameString().GetData());
  77. return Error_NotExist;
  78. }
  79. //bool toClearEnv(false);
  80. //CSimpleStringA strDepFullPath(libInfo.optCfg.strDepDirWithSlashOrFileName + libInfo.strVendor);
  81. //if (ExistsDirA(strDepFullPath)) {
  82. // ErrorCodeEnum ec = EditLDLibraryPathForVendor(strDepFullPath, TRUE);
  83. // if (ec == Error_Succeed || ec == Error_AlreadyExist) {
  84. // toClearEnv = true;
  85. // } else {
  86. // strErrMsg = CSimpleStringA::Format("设置适配器库加载路径失败:%s", SpStrError(ec));
  87. // return ec;
  88. // }
  89. //} else {
  90. // Dbg("{%s} dir not exists!", strDepFullPath.GetData());
  91. //}
  92. switch (select) {
  93. case 0: //PinPad
  94. {
  95. DevAdptLibHelper<PinPadClass> devImpl;
  96. result = devImpl.LoadUp(strLibFullPath);
  97. Dbg("Load returned: %s", SpStrError(result));
  98. if (Error_Succeed == result) {
  99. Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  100. result = devImpl->DevOpen(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  101. Dbg("open result: %s", SpStrError(result));
  102. if (result != Error_Succeed) {
  103. DevErrorInfo devErrInfo;
  104. memset(&devErrInfo, 0, sizeof(DevErrorInfo));
  105. devImpl->GetLastErr(devErrInfo);
  106. strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
  107. }
  108. devImpl.TearDown();
  109. } else {
  110. strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
  111. }
  112. }
  113. break;
  114. case 1: //CardIssuer
  115. {
  116. DevAdptLibHelper<CardIssuerClass> devImpl;
  117. result = devImpl.LoadUp(strLibFullPath);
  118. Dbg("Load returned: %s", SpStrError(result));
  119. if (Error_Succeed == result) {
  120. Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  121. BYTE noUse;
  122. result = devImpl->DevOpenEx(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate, DEV_OPEN_TYPE_COM, "", noUse);
  123. Dbg("open result: %s", SpStrError(result));
  124. if (result != Error_Succeed) {
  125. DevErrorInfo devErrInfo;
  126. memset(&devErrInfo, 0, sizeof(DevErrorInfo));
  127. devImpl->GetLastErr(devErrInfo);
  128. strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
  129. }
  130. devImpl.TearDown();
  131. } else {
  132. strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
  133. }
  134. }
  135. break;
  136. case 2: //IDCertificate
  137. {
  138. DevAdptLibHelper<IDCerClass> devImpl;
  139. result = devImpl.LoadUp(strLibFullPath);
  140. Dbg("Load returned: %s", SpStrError(result));
  141. if (Error_Succeed == result) {
  142. Dbg("%s: to open %p, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort);
  143. result = devImpl->DevOpen(libInfo.optCfg.dwPort);
  144. Dbg("open result: %s", SpStrError(result));
  145. if (result != Error_Succeed) {
  146. DevErrorInfo devErrInfo;
  147. memset(&devErrInfo, 0, sizeof(DevErrorInfo));
  148. devImpl->GetLastErr(devErrInfo);
  149. strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
  150. }
  151. devImpl.TearDown();
  152. } else {
  153. strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
  154. }
  155. }
  156. break;
  157. case 3: //ContactlessCard
  158. {
  159. DevAdptLibHelper<RFICClass> devImpl;
  160. result = devImpl.LoadUp(strLibFullPath);
  161. Dbg("Load returned: %s", SpStrError(result));
  162. if (Error_Succeed == result) {
  163. Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  164. result = devImpl->DevOpen(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  165. Dbg("open result: %s", SpStrError(result));
  166. if (result != Error_Succeed) {
  167. DevErrorInfo devErrInfo;
  168. memset(&devErrInfo, 0, sizeof(DevErrorInfo));
  169. devImpl->GetLastErr(devErrInfo);
  170. strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
  171. }
  172. devImpl.TearDown();
  173. } else {
  174. strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
  175. }
  176. }
  177. break;
  178. case 4: //HSPScanner
  179. {
  180. DevAdptLibHelper<CHSPSClass> devImpl;
  181. result = devImpl.LoadUp(strLibFullPath);
  182. Dbg("Load returned: %s", SpStrError(result));
  183. if (Error_Succeed == result) {
  184. Dbg("%s: to open %p", __FUNCTION__, devImpl.GetDevPointer());
  185. result = devImpl->DevOpen();
  186. Dbg("open result: %s", SpStrError(result));
  187. if (result != Error_Succeed) {
  188. DevErrorInfo devErrInfo;
  189. memset(&devErrInfo, 0, sizeof(DevErrorInfo));
  190. devImpl->GetLastErr(devErrInfo);
  191. strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
  192. }
  193. devImpl.TearDown();
  194. } else {
  195. strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
  196. }
  197. }
  198. break;
  199. case 5: //FingerPrint
  200. {
  201. DevAdptLibHelper<FingerPrintClass> devImpl;
  202. result = devImpl.LoadUp(strLibFullPath);
  203. Dbg("Load returned: %s", SpStrError(result));
  204. if (Error_Succeed == result) {
  205. Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  206. result = devImpl->DevOpen(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  207. Dbg("open result: %s", SpStrError(result));
  208. if (result != Error_Succeed) {
  209. DevErrorInfo devErrInfo;
  210. memset(&devErrInfo, 0, sizeof(DevErrorInfo));
  211. devImpl->GetLastErr(devErrInfo);
  212. strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
  213. }
  214. devImpl.TearDown();
  215. } else {
  216. strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
  217. }
  218. }
  219. break;
  220. case 6: //WatchDog
  221. {
  222. DevAdptLibHelper<WatchDogClass> devImpl;
  223. result = devImpl.LoadUp(strLibFullPath);
  224. Dbg("Load returned: %s", SpStrError(result));
  225. if (Error_Succeed == result) {
  226. Dbg("%s: to open %p", __FUNCTION__, devImpl.GetDevPointer());
  227. result = devImpl->DevOpen();
  228. Dbg("open result: %s", SpStrError(result));
  229. if (result != Error_Succeed) {
  230. DevErrorInfo devErrInfo;
  231. memset(&devErrInfo, 0, sizeof(DevErrorInfo));
  232. devImpl->GetLastErr(devErrInfo);
  233. strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
  234. }
  235. devImpl.TearDown();
  236. } else {
  237. strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
  238. }
  239. }
  240. break;
  241. case 7: //Gpio
  242. {
  243. DevAdptLibHelper<GpioClass> devImpl;
  244. result = devImpl.LoadUp(strLibFullPath);
  245. Dbg("Load returned: %s", SpStrError(result));
  246. if (Error_Succeed == result) {
  247. ///**TODO(Gifur@10/20/2021): 这里只针对UOS和新款Win大机 */
  248. GpioInitParam param = { libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate, 4, {true, true, false, true} };
  249. Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  250. result = devImpl->DevOpen(param);
  251. Dbg("open result: %s", SpStrError(result));
  252. if (result != Error_Succeed) {
  253. DevErrorInfo devErrInfo;
  254. memset(&devErrInfo, 0, sizeof(DevErrorInfo));
  255. devImpl->GetLastErr(devErrInfo);
  256. strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
  257. }
  258. devImpl.TearDown();
  259. } else {
  260. strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
  261. }
  262. }
  263. break;
  264. case 8: //Ups
  265. {
  266. DevAdptLibHelper<UpsClass> devImpl;
  267. result = devImpl.LoadUp(strLibFullPath);
  268. Dbg("Load returned: %s", SpStrError(result));
  269. if (Error_Succeed == result) {
  270. Dbg("%s: to open %p, %d, %d", __FUNCTION__, devImpl.GetDevPointer(), libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  271. result = devImpl->DevOpen(libInfo.optCfg.dwPort, libInfo.optCfg.dwBaudRate);
  272. Dbg("open result: %s", SpStrError(result));
  273. if (result != Error_Succeed) {
  274. DevErrorInfo devErrInfo;
  275. memset(&devErrInfo, 0, sizeof(DevErrorInfo));
  276. devImpl->GetLastErr(devErrInfo);
  277. strErrMsg = CSimpleStringA::Format("打开设备失败:[%s] %s", SpStrError(result), devErrInfo.szErrMsg);
  278. }
  279. devImpl.TearDown();
  280. } else {
  281. strErrMsg = CSimpleStringA::Format("文件加载或获取对象失败:%s", SpStrError(result));
  282. }
  283. }
  284. break;
  285. default:
  286. result = Error_NotSupport;
  287. strErrMsg = CSimpleStringA::Format("%s: %s", libInfo.strDevice.GetData(), SpStrError(result));
  288. break;
  289. }
  290. //if (toClearEnv) {
  291. // ErrorCodeEnum ec = EditLDLibraryPathForVendor(strDepFullPath, FALSE);
  292. // Dbg("Clear Env Path returned: %s", SpStrError(ec));
  293. //}
  294. return result;
  295. }