mod_gpio.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. #include "stdafx.h"
  2. #include "mod_gpio.h"
  3. #include "publicFunExport.h"
  4. #include <bitset>
  5. const int MAX_GPIO_INIT_TRIES = 3;
  6. const int INPUT_PORT_2 = 2;
  7. const int MAX_MOVE_HOLD_TIMES = 5000;
  8. const int ON_EVENT_DETECT_TIMOUE_MILLSECS = 500;
  9. const int REPEAT_FIRE_TIMEOUT_VALUE = 10000; //10s
  10. #define SETBIT(x,y) x|=(1<<(y))
  11. #define CLEARBIT(x,y) x&=((1<<(y))^0xffffff)
  12. using namespace SP::Module::Comm;
  13. static const char* GetDriverPortStringZhCN(int pin)
  14. {
  15. switch (pin) {
  16. case 1: return "【驱动】读卡发卡器提示灯";
  17. case 2: return "【驱动】前端USB通断控制";
  18. case 3: return "【驱动】身份证阅读器提示灯";
  19. case 4: return "【驱动】密码键盘提示灯";
  20. case 5: return "【驱动】脸部照明灯";
  21. case 6: return "【驱动】故障灯";
  22. case 7: return "【驱动】读卡发卡器维护提示灯";
  23. case 8: return "【驱动】前端USB口提示灯";
  24. case 9: return "【驱动】非接IC读卡器提示灯";
  25. case 10: return "";
  26. case 11: return "【驱动】指纹仪提示灯";
  27. case 12: return "【驱动】凭条打印提示灯";
  28. case 13: return "【驱动】高拍仪提示灯";
  29. case 14: return "";
  30. case 15: return "";
  31. case 16: return "";
  32. case 17: return "【接收】震动探测器";
  33. case 18: return "【接收】机具门感应开关";
  34. case 19: return "【接收】话机提机感应开关";
  35. case 20: return "【接收】人体探测感应器";
  36. case 21: return "【接收】发卡器卡嘴覆盖探测器";
  37. //case 22: return "";
  38. //case 23: return "";
  39. //case 24: return "";
  40. //case 25: return "";
  41. case 26: return "【驱动】高拍仪提示灯";
  42. //case 27: return "";
  43. //case 28: return "";
  44. //case 29: return "";
  45. //case 30: return "";
  46. //case 31: return "";
  47. //case 32: return "";
  48. //case 33: return "";
  49. //case 34: return "";
  50. //case 35: return "";
  51. //case 36: return "";
  52. //case 37: return "";
  53. //case 38: return "";
  54. //case 39: return "";
  55. //case 40: return "";
  56. //case 41: return "";
  57. //case 42: return "";
  58. default:
  59. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unkown pin sequence: %d", pin);
  60. return "";
  61. break;
  62. }
  63. }
  64. static const char* pinStatusName[8][2] =
  65. {
  66. {"无震动", "**有震动**"},
  67. {"关门", "**开门**"},
  68. {"挂机", "**摘机**"},
  69. {"无移动", "**移动**"},
  70. {"卡嘴正常", "**卡嘴不正常**"},
  71. {"Pin 22 Normal", "**Pin 22 High**"},
  72. {"Pin 23 Normal", "**Pin 23 High**"},
  73. {"Pin 24 Normal", "**Pin 24 High**"}
  74. };
  75. static const char* GetDriverPortString(int pin)
  76. {
  77. switch (pin) {
  78. case 1: return "[Driver] CarIssuer Tip Light";
  79. case 2: return "[Driver] USB Switch";
  80. case 3: return "[Driver] IDCer Tip Light";
  81. case 4: return "[Driver] PinPad Tip Light";
  82. case 5: return "[Driver] Face Light";
  83. case 6: return "[Driver] Maintain Light";
  84. case 7: return "[Driver] CardIssuser Mouse Maintain Light";
  85. case 8: return "[Driver] USB Tip Light";
  86. case 9: return "[Driver] RF Tip Light";
  87. case 10: return "";
  88. case 11: return "[Driver] FingerPrint Tip Light";
  89. case 12: return "[Driver] Printer Tip Light";
  90. case 13: return "[Driver] HSPSCanner Read Light";
  91. case 14: return "";
  92. case 15: return "";
  93. case 16: return "";
  94. case 17: return "[Receiver] Shake Detecter";
  95. case 18: return "[Receiver] Door Detecter";
  96. case 19: return "[Receiver] Phone Detecter";
  97. case 20: return "[Receiver] Body Detecter";
  98. case 21: return "[Receiver] CardIssuer Mouse Detecter";
  99. //case 22: return "";
  100. //case 23: return "";
  101. //case 24: return "";
  102. //case 25: return "";
  103. case 26: return "[Driver] HSPSCanner Tip Light";
  104. //case 27: return "";
  105. //case 28: return "";
  106. //case 29: return "";
  107. //case 30: return "";
  108. //case 31: return "";
  109. //case 32: return "";
  110. //case 33: return "";
  111. //case 34: return "";
  112. //case 35: return "";
  113. //case 36: return "";
  114. //case 37: return "";
  115. //case 38: return "";
  116. //case 39: return "";
  117. //case 40: return "";
  118. //case 41: return "";
  119. //case 42: return "";
  120. default:
  121. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unkown pin sequence: %d", pin);
  122. return "";
  123. break;
  124. }
  125. }
  126. #define LIGHT_STRING_CONVERT(str) case str : return #str; break;
  127. static const char* GetLightSeqString(int seq)
  128. {
  129. switch (seq) {
  130. LIGHT_STRING_CONVERT(UNKNOWN_DEVICE)
  131. LIGHT_STRING_CONVERT(CARDREADER)
  132. LIGHT_STRING_CONVERT(CARDREADER_RED)
  133. LIGHT_STRING_CONVERT(IDCERTIFICATE)
  134. LIGHT_STRING_CONVERT(PINPAD)
  135. LIGHT_STRING_CONVERT(SHAKEDETECT)
  136. LIGHT_STRING_CONVERT(SWITCHINDUCTOR)
  137. LIGHT_STRING_CONVERT(PHONEPICKUP)
  138. LIGHT_STRING_CONVERT(MOVEDETECT)
  139. LIGHT_STRING_CONVERT(CARDGATEDETECT)
  140. LIGHT_STRING_CONVERT(HEADLIGHT)
  141. LIGHT_STRING_CONVERT(HEADLIGHT_RED)
  142. LIGHT_STRING_CONVERT(CONTACTLESSCARD)
  143. LIGHT_STRING_CONVERT(HEADLIGHT_ASSIST)
  144. LIGHT_STRING_CONVERT(HSPSCANNER)
  145. LIGHT_STRING_CONVERT(FINGERPRINT)
  146. default:
  147. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unkonwn LightSeq: %d", seq);
  148. return "UnConver One";
  149. break;
  150. }
  151. }
  152. #undef LIGHT_STRING_CONVERT
  153. /** Just for Debug [Gifur@2024911]*/
  154. void LogPinStatus(ULONG val)
  155. {
  156. char byteString[9] = { 0 };
  157. _itoa((int)val, byteString, 2);
  158. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("ReadPort returned btInput[%s]", byteString);
  159. std::bitset<8> pinSet((int)val);
  160. if (pinSet[0])
  161. {
  162. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[0][1]);
  163. }
  164. else
  165. {
  166. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[0][0]);
  167. }
  168. if (pinSet[1])
  169. {
  170. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[1][1]);
  171. }
  172. else
  173. {
  174. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[1][0]);
  175. }
  176. if (pinSet[2])
  177. {
  178. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[2][1]);
  179. }
  180. else
  181. {
  182. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[2][0]);
  183. }
  184. if (pinSet[3])
  185. {
  186. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[3][1]);
  187. }
  188. else
  189. {
  190. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[3][0]);
  191. }
  192. if (pinSet[4])
  193. {
  194. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[4][1]);
  195. }
  196. else
  197. {
  198. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[4][0]);
  199. }
  200. if (pinSet[5])
  201. {
  202. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[5][1]);
  203. }
  204. else
  205. {
  206. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[5][0]);
  207. }
  208. if (pinSet[6])
  209. {
  210. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[6][1]);
  211. }
  212. else
  213. {
  214. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[6][0]);
  215. }
  216. if (pinSet[7])
  217. {
  218. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[7][1]);
  219. }
  220. else
  221. {
  222. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)(pinStatusName[7][0]);
  223. }
  224. return;
  225. }
  226. CSimpleStringA CombineJsonContext(const char* szMessage)
  227. {
  228. CSimpleStringA result(true);
  229. if (szMessage == NULL || strlen(szMessage) == 0) {
  230. return result;
  231. }
  232. result = CSimpleStringA::Format("{\"addition\": \"%s\"}", szMessage);
  233. return result;
  234. }
  235. void CGpioServiceSession::Handle_GetDevInfo(SpReqAnsContext<GpioService_GetDevInfo_Req,
  236. GpioService_GetDevInfo_Ans>::Pointer ctx)
  237. {
  238. DbgToBeidou(ctx->link, __FUNCTION__)();
  239. m_pEntity->GetDevInfo(ctx);
  240. }
  241. void CGpioServiceSession::Handle_Exit(SpOnewayCallContext<GpioService_Exit_Info>::Pointer ctx)
  242. {
  243. DbgToBeidou(ctx->link, __FUNCTION__)();
  244. m_pEntity->Exit(ctx);
  245. }
  246. /** 因为GPIO没有状态机的概念,所以抛送信息的代码单独实现,没有跟其他硬件类似使用基类中的公有函数 [Gifur@2023810]*/
  247. void CGpioEntity::ToLogRootINIInfo()
  248. {
  249. CSmartPointer<IConfigInfo> spConfigRoot;
  250. GetFunction()->OpenConfig(Config_Root, spConfigRoot);
  251. CSimpleStringA entityName(GetEntityName());
  252. if (!entityName.IsNullOrEmpty()) {
  253. CSimpleStringA sectionName = CSimpleStringA::Format("Device.%s", entityName.GetData());
  254. CSimpleStringA csVendor(true), csVersion(true), csBatch(true), csPort(true), csBaudrate(true), csFileName(true);
  255. spConfigRoot->ReadConfigValue(sectionName, "File", csFileName);
  256. if (csFileName.IsNullOrEmpty()) {
  257. spConfigRoot->ReadConfigValue(sectionName, "Vendor", csVendor);
  258. spConfigRoot->ReadConfigValue(sectionName, "Version", csVersion);
  259. spConfigRoot->ReadConfigValue(sectionName, "Batch", csBatch);
  260. }
  261. spConfigRoot->ReadConfigValue(sectionName, "Port", csPort);
  262. spConfigRoot->ReadConfigValue(sectionName, "Baudrate", csBaudrate);
  263. std::map<std::string, std::string> rootInfo;
  264. CSimpleStringA csCheckData(true), csKeySN(true);
  265. rootInfo["File"] = csFileName.GetData();
  266. rootInfo["Vendor"] = csVendor.GetData();
  267. rootInfo["Version"] = csVersion.GetData();
  268. rootInfo["Batch"] = csBatch.GetData();
  269. rootInfo["Port"] = csPort.GetData();
  270. rootInfo["Baudrate"] = csBaudrate.GetData();
  271. std::pair<bool, std::string> strResult;
  272. strResult = generateJsonStr(rootInfo);
  273. LogWarn(Severity_Low, Error_Debug, GPIO_UserErrorCode_Real_Root_Config, strResult.second.c_str());
  274. }
  275. return;
  276. }
  277. struct GpioInitTask : public ITaskSp {
  278. CGpioEntity* m_entity;
  279. CSmartPointer<ITransactionContext> m_pTransactionContext;
  280. GpioInitTask(CGpioEntity* entity, CSmartPointer<ITransactionContext> pTransactionContext) :m_entity(entity), m_pTransactionContext(pTransactionContext) {}
  281. void Process()
  282. {
  283. m_entity->InitializeVendorLogSwitch();
  284. ErrorCodeEnum err = m_entity->Initial();
  285. if (err == Error_Succeed)
  286. {
  287. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Gpio open succeeded.");
  288. //oiltest 20130118 for audiodg handle
  289. m_entity->StartDetectWorkThread();
  290. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("subscribelog...");
  291. //oiltmp the subscribelog should distinct machine type?
  292. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidHealth, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "HealthManager");
  293. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidIDC, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "IDCertificate");
  294. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidFP, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "FingerPrint");
  295. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidKB, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "PinPad");
  296. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidCR, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "CardIssuerStand");
  297. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidCA, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "CustomerAware");
  298. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidCC, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "ContactlessCard");
  299. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidDD, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "DeviceControl");
  300. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidCS, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "CardIssuerStore");
  301. m_entity->GetFunction()->SubscribeLog(m_entity->m_uuidHSPscanner, m_entity, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "HSPScanner");
  302. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sub ok...");
  303. }
  304. else
  305. {
  306. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("gpio open failed: %s.", SpStrError(err));
  307. }
  308. m_pTransactionContext->SendAnswer(Error_Succeed);
  309. }
  310. };
  311. void CGpioEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  312. {
  313. GetFunction()->RegistSysVarEvent("UIState", this);
  314. GpioInitTask* initTask = new GpioInitTask(this, pTransactionContext);
  315. GetFunction()->PostThreadPoolTask(initTask);
  316. }
  317. void CGpioEntity::ToLogWarnInfoAboutTerm(const AdapterInfo& m_adapterInfo)
  318. {
  319. std::map<std::string, std::string> termInfo;
  320. char* strFileHash = new char[128];
  321. ZeroMemory(strFileHash, 128);
  322. BYTE fileHash[32];
  323. SM3File(const_cast<char*>(m_adapterInfo.adapterFilePath.GetData()), fileHash);
  324. SP::Module::Util::HexBuf2StrBuf(fileHash, &strFileHash, 32);
  325. DWORD fileSize = SP::Module::FileSystem::FetchFileSize(m_adapterInfo.adapterFilePath);
  326. termInfo["VendorDllName"] = m_adapterInfo.adapterFileName;
  327. termInfo["VendorDllFileHash"] = strFileHash;
  328. ZeroMemory(strFileHash, 128);
  329. termInfo["VendorDllFileSize"] = _itoa(fileSize, strFileHash, 10);
  330. termInfo["szModel"] = m_adapterInfo.devCatInfo.szModel;
  331. termInfo["szType"] = m_adapterInfo.devCatInfo.szType;
  332. termInfo["Port"] = m_adapterInfo.strPort.GetData();
  333. termInfo["PortNum"] = m_adapterInfo.strPortNum.GetData();
  334. termInfo["Baudrate"] = m_adapterInfo.strBaudrate.GetData();
  335. if (strFileHash != nullptr) {
  336. delete[] strFileHash;
  337. strFileHash = nullptr;
  338. }
  339. std::pair<bool, std::string> strResult;
  340. strResult = generateJsonStr(termInfo);
  341. LogWarn(Severity_Low, Error_Unexpect, GPIO_UserErrorCode_LogInfoAboutTerm, strResult.second.c_str());
  342. return;
  343. }
  344. void CGpioEntity::SetErrorAndLog(ErrorCodeEnum errCode
  345. , DWORD userCode
  346. , CSimpleStringA devApi
  347. , CSimpleStringA funPath
  348. , bool bInBusiness /*= false*/
  349. , int costTime /*= 0*/
  350. , CSimpleStringA logCode /*= ""*/
  351. , CSimpleStringA context /*= ""*/)
  352. {
  353. DevErrorInfo devErrInfo;
  354. ZeroMemory(&devErrInfo, sizeof(devErrInfo));
  355. if (m_hDevHelper != nullptr) {
  356. m_hDevHelper->GetLastErr(devErrInfo);
  357. }
  358. else {
  359. strcpy(devErrInfo.szErrMsg, "{\"Description\": \"DevAdapter handle is null\"}");
  360. }
  361. const CSimpleStringA alarmMsg = CSimpleStringA::Format("{\"Function\":\"%s\", \"DevApi\":\"%s\", \"ReturnCode\":\"%s\", \"Msg\":\"%s\", \"Context\":\"%s\"}"
  362. , funPath.GetData(), devApi.GetData(), SpStrError(errCode), devErrInfo.szErrMsg, context.GetData());
  363. std::map<std::string, std::string> msgInfo;
  364. msgInfo["ReturnCode"] = SpStrError(errCode);
  365. msgInfo["ErrMsg"] = devErrInfo.szErrMsg;
  366. msgInfo["Context"] = context.GetData();
  367. std::pair<bool, std::string> strResult;
  368. strResult = generateJsonStr(msgInfo);
  369. CSimpleStringA csErrMsgWithReturnCode = strResult.second.c_str();
  370. CSimpleStringA tmpRTA(true), tmpDesc(true);
  371. if (GetFunction()->GetVTMErrMsg(userCode, tmpDesc, tmpRTA) != Error_Succeed)
  372. tmpRTA = CSimpleStringA::Format("0x%X", userCode);//if map failed, use dwCode instead
  373. if (bInBusiness) {
  374. LogError(Severity_High, errCode, userCode, alarmMsg.GetData());
  375. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_USER).setLogCode(logCode).setAPI(devApi).setResultCode(tmpRTA.GetData()).setCostTime(costTime)(csErrMsgWithReturnCode.GetData());
  376. }
  377. else {
  378. LogWarn(Severity_High, errCode, userCode, alarmMsg.GetData());
  379. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setLogCode(logCode).setAPI(devApi).setResultCode(tmpRTA.GetData()).setCostTime(costTime)(csErrMsgWithReturnCode.GetData());
  380. }
  381. return;
  382. }
  383. bool CGpioEntity::DetectBit(ULONG data, int pos)
  384. {
  385. if (!m_bFuncVer2) {
  386. ULONG tmp = 0;
  387. SETBIT(tmp, pos);
  388. return (data & tmp);
  389. }
  390. #if defined(RVC_OS_LINUX)
  391. else {
  392. DWORD dwReq = (GPIO_DEV_SN_SENSOR_SHAKE << pos);
  393. return (data & dwReq);
  394. }
  395. #endif //RVC_OS_LINUX
  396. return 0;
  397. }
  398. ErrorCodeEnum CGpioEntity::Initial()
  399. {
  400. CSimpleStringA dllName;
  401. ErrorCodeEnum eErrDev = ExtractVendorLibFullPath(dllName);
  402. if (eErrDev != Error_Succeed) {
  403. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("load vendor dll or so(%s) failed, ec=%s", dllName.GetData(), SpStrError(eErrDev));
  404. m_bOpening = false;
  405. return Error_DevLoadFileFailed;
  406. }
  407. LogWarn(Severity_Low, Error_Unexpect, GPIO_UserErrorCode_RootInfo, dllName.GetData());
  408. m_adapterInfo.adapterFilePath = dllName;
  409. m_adapterInfo.adapterFileName = vendorLibInfo.toLibNameString();
  410. m_adapterInfo.strVendor = vendorLibInfo.strVendor;
  411. m_adapterInfo.strVersion = vendorLibInfo.strVersion;
  412. m_adapterInfo.strBatch = vendorLibInfo.strBatch;
  413. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  414. CSmartPointer<IConfigInfo> spConfigRoot;
  415. eErrDev = spEntityFunction->OpenConfig(Config_Root, spConfigRoot);
  416. if (eErrDev != Error_Succeed) {
  417. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("open cfg file failed %s", SpStrError(eErrDev));
  418. m_bOpening = false;
  419. return eErrDev;
  420. }
  421. CSimpleStringA csPort(true), csBaudrate(true);
  422. CSimpleStringA csPortNum(true);
  423. spConfigRoot->ReadConfigValue("Device.Gpio", "Port", csPort);
  424. spConfigRoot->ReadConfigValue("Device.Gpio", "Baudrate", csBaudrate);
  425. spConfigRoot->ReadConfigValue("Device.Gpio", "PortNum", csPortNum);
  426. m_adapterInfo.strPort = csPort;
  427. m_adapterInfo.strBaudrate = csBaudrate;
  428. m_adapterInfo.strPortNum = csPortNum; //Just load from Config
  429. m_hDevHelper.SetAdapterName(GetEntityName());
  430. eErrDev = m_hDevHelper.LoadUp(dllName);
  431. if (eErrDev != Error_Succeed) {
  432. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("load up %s failed: %s", dllName.GetData(), SpStrError(eErrDev));
  433. m_bOpening = false;
  434. return Error_DevLoadFileFailed;
  435. }
  436. GpioInitParam initParam;
  437. initParam.dwPort = m_adapterInfo.GetPortInt();
  438. initParam.dwBaudRate = m_adapterInfo.GetBaudrateInt();
  439. //TODO: [Gifur@202495]
  440. initParam.dir[0] = true;
  441. initParam.dir[1] = true;
  442. initParam.dir[2] = false;
  443. initParam.dir[3] = false;
  444. initParam.dwPortNum = 3;
  445. if (m_adapterInfo.GetPortNumInt() == 4) {
  446. m_bNewVersion = TRUE;
  447. initParam.dwPortNum = 4;
  448. initParam.dir[3] = true;
  449. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("New available 4 port num");
  450. }
  451. int initTries = 0;
  452. ErrorCodeEnum err;
  453. do {
  454. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to open device...port(%d), baudrate(%d).", m_adapterInfo.GetPortInt(), m_adapterInfo.GetBaudrateInt());
  455. ULONGLONG ullStart = SP::Module::Comm::RVCGetTickCount();
  456. err = m_hDevHelper->DevOpen(initParam);
  457. ULONGLONG ullEnd = SP::Module::Comm::RVCGetTickCount();
  458. if (err == Error_Succeed) {
  459. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("DevAdapter::DevOpen").setCostTime(ullEnd - ullStart)("open device succ");
  460. ToLogRootINIInfo(); //WARN:这里是重新读一遍原生的root配置信息
  461. DevCategoryInfo devCatInfo;
  462. ZeroMemory(devCatInfo.szModel, sizeof(devCatInfo.szModel));
  463. ZeroMemory(devCatInfo.szType, sizeof(devCatInfo.szType));
  464. ZeroMemory(devCatInfo.szVendor, sizeof(devCatInfo.szVendor));
  465. ullStart = SP::Module::Comm::RVCGetTickCount();
  466. err = m_hDevHelper->GetDevCategory(devCatInfo);
  467. ullEnd = SP::Module::Comm::RVCGetTickCount();
  468. if (err == Error_Succeed) {
  469. m_adapterInfo.FulfillCategoryInfo(devCatInfo);
  470. ToLogWarnInfoAboutTerm(m_adapterInfo);
  471. #if defined(RVC_OS_LINUX)
  472. CSimpleStringA strType(devCatInfo.szType);
  473. if (strType.IndexOf("FUNCVER=2.0") != -1) {
  474. m_bFuncVer2 = TRUE;
  475. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Detect new interface version: %s", strType.GetData());
  476. }
  477. #endif //RVC_OS_LINUX
  478. } else {
  479. SetErrorAndLog(err, GPIO_UserErrorCode_GetDevCategory_Failed, "DevAdapter::GetDevCategory", "FulfillAdapterDevCategory", false, ullEnd - ullStart);
  480. }
  481. m_bOpened = true;
  482. break;
  483. } else {
  484. SetErrorAndLog(err, GPIO_UserErrorCode_DevOpen_Failed, "DevAdapter::DevOpen", __FUNCTION__, false, ullEnd - ullStart);
  485. Sleep(300);
  486. initTries++;
  487. continue;
  488. }
  489. } while (initTries < MAX_GPIO_INIT_TRIES);
  490. m_bOpening = false;
  491. if (!m_bOpened) {
  492. err = Error_DevConnFailed;
  493. }
  494. else {
  495. if (!m_bFuncVer2) {
  496. ULONGLONG ullStart = SP::Module::Comm::RVCGetTickCount();
  497. eErrDev = m_hDevHelper->WritePort(0, 0x00);
  498. ULONGLONG ullEnd = SP::Module::Comm::RVCGetTickCount();
  499. if (eErrDev != Error_Succeed)
  500. SetErrorAndLog(eErrDev, GPIO_UserErrorCode_WritePort_Failed, "DevAdapter::WritePort", __FUNCTION__, false, ullEnd - ullStart, "", CombineJsonContext("WritePort(0) after DevOpen"));
  501. ullStart = SP::Module::Comm::RVCGetTickCount();
  502. eErrDev = m_hDevHelper->WritePort(1, 0x00);
  503. ullEnd = SP::Module::Comm::RVCGetTickCount();
  504. if (eErrDev != Error_Succeed)
  505. SetErrorAndLog(eErrDev, GPIO_UserErrorCode_WritePort_Failed, "DevAdapter::WritePort", __FUNCTION__, false, ullEnd - ullStart, "", CombineJsonContext("WritePort(1) after DevOpen"));
  506. if (m_bNewVersion) {
  507. ullStart = SP::Module::Comm::RVCGetTickCount();
  508. eErrDev = m_hDevHelper->WritePort(3, 0x00);
  509. ullEnd = SP::Module::Comm::RVCGetTickCount();
  510. if (eErrDev != Error_Succeed)
  511. SetErrorAndLog(eErrDev, GPIO_UserErrorCode_WritePort_Failed, "DevAdapter::WritePort", __FUNCTION__, false, ullEnd - ullStart, "", CombineJsonContext("WritePort(3) after DevOpen"));
  512. }
  513. }
  514. else {
  515. #if defined(RVC_OS_LINUX)
  516. ULONGLONG ullStart = SP::Module::Comm::RVCGetTickCount();
  517. eErrDev = m_hDevHelper->SetStatus(GPIO_DEV_SN_LIGHT_SENSOR_ALL, GPIO_DEV_LIGHT_MODE_RESET);
  518. ULONGLONG ullEnd = SP::Module::Comm::RVCGetTickCount();
  519. if (eErrDev != Error_Succeed)
  520. SetErrorAndLog(eErrDev, GPIO_UserErrorCode_SetStatus_Failed, "DevAdapter::SetStatus", __FUNCTION__, false, ullEnd - ullStart, "", CombineJsonContext("SetStatus after DevOpen"));
  521. #endif //RVC_OS_LINUX
  522. }
  523. }
  524. m_eMachineType = SP::Module::Comm::GetTerminalMachineInfo(this).type;
  525. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("machine type: %s", SP::Module::Comm::Type2Str(m_eMachineType));
  526. m_moveHoldTimes = MAX_MOVE_HOLD_TIMES;//oiltmp about 5000*300ms = 25 minutes
  527. m_moveDisappearTimes = m_moveHoldTimes;
  528. return err;
  529. }
  530. void CGpioEntity::OnLog(const CAutoArray<CUUID>& SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  531. const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  532. const CAutoArray<DWORD>& Param, const char* pszEntityName, const char* pszModuleName, const char* pszMessage, const linkContext& pLinkInfo)
  533. {
  534. GpioService_Set_Info Req;
  535. if (!m_bOpened) {
  536. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("usercode [%x], but device is not open", dwUserCode);
  537. return;
  538. }
  539. else {
  540. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("receive usercode [%x]", dwUserCode);
  541. }
  542. Req.close = 0;
  543. switch (dwUserCode) {
  544. case LOG_EVT_CARDISSUER_GREEN_ON:
  545. case LOG_EVT_CARDISSUER_STORE_GREEN_ON:
  546. Req.devseq = CARDREADER;
  547. Req.mode = 1;
  548. break;
  549. case LOG_EVT_CARDISSUER_RED_ON:
  550. Req.devseq = CARDREADER_RED;
  551. Req.mode = 1;
  552. break;
  553. case LOG_EVT_IDCERTIFICATE_GREEN_ON:
  554. Req.devseq = IDCERTIFICATE;
  555. Req.mode = 1;
  556. break;
  557. case LOG_EVT_FINGERPRINT_GREEN_ON:
  558. Req.devseq = FINGERPRINT;
  559. Req.mode = 1;
  560. break;
  561. case LOG_EVT_PINPAD_GREEN_ON:
  562. Req.devseq = PINPAD;
  563. Req.mode = 1;
  564. break;
  565. case LOG_EVT_HEADLIGHT_GREEN_ON:
  566. m_bHeadLightFlag = true;
  567. Req.devseq = HEADLIGHT;
  568. Req.mode = 0;
  569. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("machine light on");
  570. break;
  571. case LOG_EVT_HEADLIGHT_RED_ON:
  572. Req.devseq = HEADLIGHT_RED;
  573. Req.mode = 1;
  574. break;
  575. case LOG_EVT_CONTACTLESS_CARD_GREEN_ON:
  576. Req.devseq = CONTACTLESSCARD;
  577. Req.mode = 1;
  578. break;
  579. case LOG_EVT_IEBROWSER_LIGHT_ASSISTANT_ON:
  580. Req.devseq = HEADLIGHT_ASSIST;
  581. Req.mode = 1;
  582. break;
  583. case LOG_EVT_CARDISSUER_GREEN_OFF:
  584. case LOG_EVT_CARDISSUER_STORE_GREEN_OFF:
  585. Req.devseq = CARDREADER;
  586. Req.mode = 1;
  587. Req.close = 1;
  588. break;
  589. case LOG_EVT_CARDISSUER_RED_OFF:
  590. Req.devseq = CARDREADER_RED;
  591. Req.mode = 1;
  592. Req.close = 1;
  593. break;
  594. case LOG_EVT_IDCERTIFICATE_GREEN_OFF:
  595. Req.devseq = IDCERTIFICATE;
  596. Req.mode = 1;
  597. Req.close = 1;
  598. break;
  599. case LOG_EVT_FINGERPRINT_GREEN_OFF:
  600. Req.devseq = FINGERPRINT;
  601. Req.mode = 1;
  602. Req.close = 1;
  603. break;
  604. case LOG_EVT_PINPAD_GREEN_OFF:
  605. Req.devseq = PINPAD;
  606. Req.mode = 1;
  607. Req.close = 1;
  608. break;
  609. case LOG_EVT_HEADLIGHT_GREEN_OFF:
  610. m_bHeadLightFlag = false;
  611. Req.devseq = HEADLIGHT;
  612. Req.mode = 1;
  613. Req.close = 1;
  614. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("machine light off");
  615. break;
  616. case LOG_EVT_HEADLIGHT_RED_OFF:
  617. Req.devseq = HEADLIGHT_RED;
  618. Req.mode = 1;
  619. Req.close = 1;
  620. break;
  621. case LOG_EVT_CONTACTLESS_CARD_GREEN_OFF:
  622. Req.devseq = CONTACTLESSCARD;
  623. Req.mode = 1;
  624. Req.close = 1;
  625. break;
  626. case LOG_EVT_IEBROWSER_LIGHT_ASSISTANT_OFF:
  627. Req.devseq = HEADLIGHT_ASSIST;
  628. Req.mode = 1;
  629. Req.close = 1;
  630. break;
  631. case LOG_EVT_HSPS_LIGHT_ON:
  632. Req.devseq = HSPSCANNER;
  633. Req.mode = 1;
  634. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("hspscanner light on");
  635. break;
  636. case LOG_EVT_HSPS_LIGHT_OFF:
  637. Req.devseq = HSPSCANNER;
  638. Req.mode = 1;
  639. Req.close = 1;
  640. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("hspscanner light off");
  641. break;
  642. default:
  643. //DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("unkown event: 0x%X", dwUserCode);
  644. return;
  645. }
  646. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("devseq[%d],mode[%d]close[%d]", Req.devseq, Req.mode, Req.close);
  647. m_bFuncVer2 ? SetEx(Req) : Set(Req);
  648. }
  649. void CGpioEntity::WritePin(DWORD dwPinSeq, bool bHighLevel)
  650. {
  651. ErrorCodeEnum result(Error_Succeed);
  652. int idx = -1;
  653. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Write Pin %s with %s", GetDriverPortString(dwPinSeq + 1), bHighLevel ? "[Active]" : "[InActive]");
  654. if (dwPinSeq > 16) {/** 目前只有一个高拍仪的提示灯会超过该值 [Gifur@202495]*/
  655. if (!m_bNewVersion || dwPinSeq < 24) {
  656. return;
  657. }
  658. idx = 3;
  659. } else if (dwPinSeq < 8) {
  660. idx = 0;
  661. } else {
  662. idx = 1;
  663. }
  664. if (bHighLevel)
  665. SETBIT(m_btOutputStatus[idx], dwPinSeq - 8 * idx);
  666. else
  667. CLEARBIT(m_btOutputStatus[idx], dwPinSeq - 8 * idx);
  668. const auto iBegin = SP::Module::Comm::RVCGetTickCount();
  669. result = m_hDevHelper->WritePort(idx, m_btOutputStatus[idx]);
  670. const auto iEnd = SP::Module::Comm::RVCGetTickCount();
  671. if (result != Error_Succeed) {
  672. SetErrorAndLog(result, GPIO_UserErrorCode_WritePort_Failed, "DevAdapter::WritePort", __FUNCTION__, false, iEnd - iBegin, "", CombineJsonContext(CSimpleStringA::Format("write port[%d] with %d", idx, m_btOutputStatus[idx])));
  673. }
  674. else {
  675. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("DevAdapter::WritePort").setAPI(__FUNCTION__).setCostTime(iEnd - iBegin)("write port[%d] with %d succ", idx, m_btOutputStatus[idx]);
  676. }
  677. return;
  678. }
  679. void CGpioEntity::SetEx(GpioService_Set_Info req)
  680. {
  681. #if defined(RVC_OS_LINUX)
  682. int devicePort;
  683. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("SetEx request %s arrived!", GetLightSeqString(req.devseq));
  684. switch (req.devseq) {
  685. case CARDREADER:
  686. {
  687. devicePort = GPIO_DEV_SN_LIGHT_CARDISSUER;
  688. break;
  689. }
  690. case CARDREADER_RED:
  691. {
  692. devicePort = GPIO_DEV_SN_LIGHT_CARDISSUER_MAINTAIN;
  693. break;
  694. }
  695. case IDCERTIFICATE:
  696. {
  697. devicePort = GPIO_DEV_SN_LIGHT_IDCERTIFICATE;
  698. break;
  699. }
  700. case FINGERPRINT:
  701. {
  702. devicePort = GPIO_DEV_SN_LIGHT_FINGERPRINT;
  703. break;
  704. }
  705. case PINPAD:
  706. {
  707. devicePort = GPIO_DEV_SN_LIGHT_PINPAD;
  708. break;
  709. }
  710. case HEADLIGHT:
  711. {
  712. devicePort = GPIO_DEV_SN_LIGHT_FACE;
  713. break;
  714. }
  715. case HEADLIGHT_RED:
  716. {
  717. devicePort = 6/*GPIO_DEV_SN_LIGHT_MACHINE_FAULT*/;
  718. break;
  719. }
  720. case CONTACTLESSCARD:
  721. {
  722. devicePort = GPIO_DEV_SN_LIGHT_CONTACTLESSCARD;
  723. break;
  724. }
  725. case HEADLIGHT_ASSIST:
  726. {
  727. devicePort = GPIO_DEV_SN_LIGHT_FACE;
  728. break;
  729. }
  730. break;
  731. default:
  732. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setAPI(__FUNCTION__)("unsupport device seq: %d", req.devseq);
  733. return;
  734. }
  735. if (m_hDevHelper != nullptr) {
  736. DWORD dwReq = devicePort;
  737. DWORD dwMode = 0;
  738. if (!req.close) {
  739. dwMode |= GPIO_DEV_LIGHT_MODE_COLOR_NORMAL;
  740. if (!!req.mode) {
  741. dwMode |= GPIO_DEV_LIGHT_MODE_LIGHT_FLICKER;
  742. } else {
  743. dwMode |= GPIO_DEV_LIGHT_MODE_LIGHT_SUSTAIN;
  744. }
  745. }
  746. const auto iBegin = SP::Module::Comm::RVCGetTickCount();
  747. const ErrorCodeEnum ec = m_hDevHelper->SetStatus(dwReq, dwMode);
  748. const auto iEnd = SP::Module::Comm::RVCGetTickCount();
  749. if (ec != Error_Succeed) {
  750. SetErrorAndLog(ec, GPIO_UserErrorCode_SetStatus_Failed, "DevAdapter::SetStatus", __FUNCTION__, false, iEnd - iBegin, "", CombineJsonContext(CSimpleStringA::Format("SetStatus with 0x%X,0x%X", dwReq, dwMode)));
  751. }
  752. else {
  753. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("DevAdapter::SetStatus").setAPI(__FUNCTION__).setCostTime(iEnd - iBegin)("SetStatus with 0x%X,0x%X", dwReq, dwMode);
  754. }
  755. } else {
  756. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("dev helper object is invalid pointer!");
  757. }
  758. #endif //RVC_OS_LINUX
  759. }
  760. void CGpioEntity::Set(GpioService_Set_Info req)
  761. {
  762. CSimpleStringA activeModeSecName = "";
  763. int devicePort;
  764. OutDrivingInfo odi;
  765. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Set request %s arrived!", GetLightSeqString(req.devseq));
  766. switch (req.devseq) {
  767. case CARDREADER:
  768. case PINPAD:
  769. case CONTACTLESSCARD:
  770. {
  771. devicePort = req.devseq == CARDREADER ? 0 : (req.devseq == PINPAD ? 3 : 8);
  772. odi.OutputMode = OM_POSITIVE_FLICKER;
  773. odi.StopMode = SM_CYCLE;
  774. odi.SetTime = 200;
  775. odi.ResetTime = 200;
  776. odi.TimeOut = 5000;
  777. break;
  778. }
  779. case CARDREADER_RED:
  780. case HEADLIGHT_RED:
  781. {
  782. devicePort = req.devseq == CARDREADER_RED ? 6 : 5;
  783. odi.OutputMode = OM_POSITIVE_LEVEL;
  784. odi.StopMode = SM_CALLTRIGGER;
  785. odi.SetTime = 200;
  786. odi.ResetTime = 200;
  787. odi.TimeOut = 3000;
  788. break;
  789. }
  790. case IDCERTIFICATE:
  791. case FINGERPRINT:
  792. {
  793. devicePort = req.devseq == IDCERTIFICATE ? 2 : 10;
  794. odi.OutputMode = OM_POSITIVE_FLICKER;
  795. odi.StopMode = SM_CYCLE;
  796. odi.SetTime = 150;
  797. odi.ResetTime = 150;
  798. odi.TimeOut = 5000;
  799. break;
  800. }
  801. case HEADLIGHT:
  802. case HSPSCANNER:
  803. {
  804. devicePort = req.devseq == HEADLIGHT ? 4 : 12;
  805. odi.OutputMode = OM_POSITIVE_LEVEL;
  806. odi.StopMode = SM_CALLTRIGGER;
  807. odi.SetTime = 1000;
  808. odi.ResetTime = 1000;
  809. odi.TimeOut = 5000;
  810. break;
  811. }
  812. case HEADLIGHT_ASSIST:
  813. {
  814. devicePort = 4;
  815. odi.OutputMode = OM_POSITIVE_FLICKER;
  816. odi.StopMode = SM_CYCLE;
  817. odi.SetTime = 1000;
  818. odi.ResetTime = 500;
  819. odi.TimeOut = 5000;
  820. break;
  821. }
  822. default:
  823. odi.OutputMode = OM_POSITIVE_LEVEL;
  824. odi.StopMode = SM_CALLTRIGGER;
  825. odi.SetTime = 200;
  826. odi.ResetTime = 200;
  827. odi.TimeOut = 5000;
  828. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("unsupport device seq: %d", req.devseq);
  829. return;
  830. }
  831. SetOutDriving(req, odi, 0, devicePort);
  832. }
  833. void CGpioEntity::StartDetectWorkThread()
  834. {
  835. ReceivingInfo ri;
  836. ri.ContinuousTriggerTime = 2;
  837. GetContextInfo* pGci = new GetContextInfo();
  838. /** 注意设备序号是实体内定义的逻辑,不是设备端口号,最后用作于计时器的ID去了*/
  839. pGci->timerID = PHONEPICKUP;
  840. m_PickUpTimeStamp = m_PutDownTimeStamp = SP::Module::Comm::RVCGetTickCount();
  841. m_DoorOpenTimeStamp = m_DoorCloseTimeStamp = SP::Module::Comm::RVCGetTickCount();
  842. ITimerListener* pListener = new TimerOutHelper<CGpioEntity>(this, &CGpioEntity::OnEventDetect, pGci);
  843. GetFunction()->SetTimer(pGci->timerID, pListener, 100);
  844. }
  845. void CGpioEntity::OnPositiveFlickerSetTimerout(void* pData)
  846. {
  847. SetContextInfo* pSCI = (SetContextInfo*)pData;
  848. GetFunction()->KillTimer(pSCI->timerID);
  849. WritePin(pSCI->pinSeq, false); //实际操作
  850. pSCI->timerID = pSCI->timerID;
  851. ITimerListener* pListener = new TimerOutHelper<CGpioEntity>(this, &CGpioEntity::OnPositiveFlickerResetTimerout, pSCI, true);
  852. GetFunction()->SetTimer(pSCI->timerID, pListener, pSCI->resetTime);
  853. }
  854. void CGpioEntity::OnPositiveFlickerResetTimerout(void* pData)
  855. {
  856. SetContextInfo* pSCI = (SetContextInfo*)pData;
  857. GetFunction()->KillTimer(pSCI->timerID);
  858. WritePin(pSCI->pinSeq, true); //实际操作
  859. ITimerListener* pListener = new TimerOutHelper<CGpioEntity>(this, &CGpioEntity::OnPositiveFlickerSetTimerout, pSCI, true);
  860. GetFunction()->SetTimer(pSCI->timerID, pListener, pSCI->setTime);
  861. }
  862. void CGpioEntity::OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
  863. {
  864. if (!m_bOpened && m_hDevHelper) {
  865. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("let selftest to restart it!");
  866. pTransactionContext->SendAnswer(Error_InvalidState);
  867. return;
  868. }
  869. else if(!m_bOpened) {
  870. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("process failed before DevOpen, do not try to restart");
  871. pTransactionContext->SendAnswer(Error_Succeed);
  872. return;
  873. }
  874. pTransactionContext->SendAnswer(Error_Succeed);
  875. return;
  876. }
  877. void CGpioEntity::OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName)
  878. {
  879. if ((_strnicmp(pszKey, "UIState", strlen("UIState")) == 0))
  880. {
  881. if (_strnicmp(pszValue, "M", strlen("M")) == 0)
  882. SetInWhatPage(PageType_MainPage);
  883. else if (_strnicmp(pszValue, "U", strlen("U")) == 0)
  884. SetInWhatPage(PageType_UserDesktop);
  885. else
  886. SetInWhatPage(PageType_Other);
  887. }
  888. }
  889. void CGpioEntity::GetDevInfo(SpReqAnsContext<GpioService_GetDevInfo_Req, GpioService_GetDevInfo_Ans>::Pointer ctx)
  890. {
  891. if (m_bOpening) {
  892. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setLogCode(GpioService_LogCode_GetDevInfo).setAPI(__FUNCTION__)("device is opening");
  893. ctx->Answer(Error_NotInit);
  894. return;
  895. }
  896. if (!m_bOpened) {
  897. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setLogCode(GpioService_LogCode_GetDevInfo).setResultCode("RTA2901").setAPI(__FUNCTION__)("GPIO未打开");
  898. ctx->Answer(Error_DevNotAvailable, GPIO_UserErrorCode_DevOpen_Failed);
  899. }
  900. else {
  901. DevCategoryInfo info = { 0 };
  902. const ULONGLONG ullStart = SP::Module::Comm::RVCGetTickCount();
  903. ErrorCodeEnum erroCode = m_hDevHelper->GetDevCategory(info);
  904. const ULONGLONG ullEnd = SP::Module::Comm::RVCGetTickCount();
  905. ctx->Ans.state = (int)erroCode;
  906. if (erroCode == Error_Succeed) {
  907. ctx->Ans.model = info.szModel;
  908. ctx->Ans.type = info.szType;
  909. ctx->Ans.version = CSimpleStringA::Format("%d.%d.%d.%d",
  910. info.version.wMajor, info.version.wMinor, info.version.wRevision, info.version.wBuild);
  911. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(GpioService_LogCode_GetDevInfo)
  912. .setAPI("DevAdapter::GetDevCategory").setCostTime(ullEnd - ullStart)("model: %s, state:%d, type:%s, version:%s"
  913. , ctx->Ans.model.GetData(), ctx->Ans.state, ctx->Ans.type.GetData(), ctx->Ans.version.GetData());
  914. }
  915. else {
  916. SetErrorAndLog(erroCode, GPIO_UserErrorCode_GetDevCategory_Failed, "DevAdapter::GetDevCategory", __FUNCTION__, IsInBusiness(), ullEnd - ullStart, GpioService_LogCode_GetDevInfo);
  917. }
  918. ctx->Answer(Error_Succeed);
  919. }
  920. }
  921. //老接口,用于控制灯
  922. bool CGpioEntity::SetOutDriving(GpioService_Set_Info req, OutDrivingInfo od, ULONG iIndex, ULONG pinSeq)
  923. {
  924. if (req.close == 1) {
  925. GetFunction()->KillTimer(req.devseq);
  926. //oilyang add 20161222
  927. //如果关闭照明灯,以感知为准
  928. if (pinSeq == m_headlightDevPort)
  929. WritePin(pinSeq, m_bHeadLightFlag);
  930. else {
  931. if (pinSeq == PIN_HSPSCANNER_PREVIEW_LIGHT) {
  932. //额外多关闭高拍仪提示灯
  933. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("off light hspsanner.");
  934. WritePin(PIN_HSPSCANNER_LIGHT, false);
  935. }
  936. WritePin(pinSeq, false);
  937. }
  938. return true;
  939. }
  940. switch (od.OutputMode) {
  941. case OM_POSITIVE_LEVEL:
  942. {
  943. if (od.StopMode == SM_CALLTRIGGER) {
  944. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("0.0 mode: pinSeq=%u", pinSeq);
  945. //if it is hspsanner
  946. if (pinSeq == PIN_HSPSCANNER_PREVIEW_LIGHT) {
  947. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("light hspsanner.");
  948. WritePin(PIN_HSPSCANNER_LIGHT, true);
  949. }
  950. WritePin(pinSeq, true);
  951. }
  952. }
  953. break;
  954. case OM_POSITIVE_FLICKER:
  955. if (od.StopMode == SM_CYCLE) {
  956. SetContextInfo* pSci = new SetContextInfo();
  957. pSci->pinSeq = pinSeq;
  958. pSci->timerID = req.devseq;
  959. pSci->setTime = od.SetTime;
  960. pSci->resetTime = od.ResetTime;
  961. pSci->timeout = od.TimeOut;
  962. WritePin(pinSeq, true);
  963. ITimerListener* pListener = new TimerOutHelper<CGpioEntity>(this, &CGpioEntity::OnPositiveFlickerSetTimerout, pSci, true);
  964. GetFunction()->SetTimer(pSci->timerID, pListener, pSci->setTime);
  965. }
  966. break;
  967. default:
  968. break;
  969. }
  970. return true;
  971. }
  972. void CGpioEntity::OnEventDetect(void* pData)
  973. {
  974. GetContextInfo* pGci = (GetContextInfo*)pData;
  975. ULONG input = 0;
  976. BYTE btInput;
  977. ErrorCodeEnum err(Error_Succeed);
  978. if (!m_bFuncVer2) {
  979. const auto iBegin = SP::Module::Comm::RVCGetTickCount();
  980. err = m_hDevHelper->ReadPort(INPUT_PORT_2, btInput);
  981. const auto iEnd = SP::Module::Comm::RVCGetTickCount();
  982. if (err != Error_Succeed) {
  983. SetErrorAndLog(err, GPIO_UserErrorCode_ReadPort_Failed, "DevAdapter::ReadPort", __FUNCTION__, false, iEnd - iBegin, "",
  984. CombineJsonContext(CSimpleStringA::Format("read port[%d] with %d", INPUT_PORT_2, btInput)));
  985. }
  986. else if(btInput != m_btLastRevcInput){
  987. m_btLastRevcInput = btInput;
  988. LogPinStatus(btInput);
  989. }
  990. }
  991. else {
  992. #if defined(RVC_OS_LINUX)
  993. DWORD dwReq(GPIO_DEV_SN_LIGHT_SENSOR_ALL);
  994. DWORD dwMode = 0;
  995. const auto iBegin = SP::Module::Comm::RVCGetTickCount();
  996. err = m_hDevHelper->DetectStatus(dwReq, dwMode);
  997. const auto iEnd = SP::Module::Comm::RVCGetTickCount();
  998. btInput = dwMode;
  999. if (err != Error_Succeed) {
  1000. SetErrorAndLog(err, GPIO_UserErrorCode_DetectStatus_Failed, "DevAdapter::DetectStatus", __FUNCTION__, false, iEnd - iBegin, "",
  1001. CombineJsonContext(CSimpleStringA::Format("detect status[%d] with %d", dwReq, dwMode)));
  1002. }
  1003. else if (btInput != m_btLastRevcInput) {
  1004. m_btLastRevcInput = btInput;
  1005. LogPinStatus(btInput);
  1006. }
  1007. #else
  1008. err = Error_NotSupport;
  1009. #endif //RVC_OS_LINUX
  1010. }
  1011. if (err != Error_Succeed) {
  1012. m_btLastRevcInput = (BYTE)(-1);
  1013. GetFunction()->ResetTimer(pGci->timerID, ON_EVENT_DETECT_TIMOUE_MILLSECS);
  1014. return;
  1015. }
  1016. /** 震动感应 */
  1017. if (DetectBit(btInput, VIBRATIONSENSOR) && !m_bVibrationFlag) {
  1018. m_bVibrationFlag = true;
  1019. LogEvent(Severity_Middle, LOG_EVT_VIBRATIONSENSOR, CSimpleStringA::Format("%s [Active] line: %d", GetDriverPortString(17), __LINE__));
  1020. } else if (!DetectBit(btInput, VIBRATIONSENSOR) && m_bVibrationFlag) {
  1021. LogEvent(Severity_Middle, LOG_EVT_VIBRATIONSENSOR_OFF, CSimpleStringA::Format("%s [InActive] line: %d", GetDriverPortString(17), __LINE__));
  1022. m_bVibrationFlag = false;
  1023. }
  1024. /** 机箱门开关检测*/
  1025. if (DetectBit(btInput, OPENSENSOR) && !m_bOpenFlag) {
  1026. m_bOpenFlag = true;
  1027. m_DoorOpenTimeStamp = SP::Module::Comm::RVCGetTickCount();
  1028. LogEvent(Severity_Middle, LOG_EVT_OPENSENSOR_ON, CSimpleStringA::Format("%s [Active] line: %d", GetDriverPortString(18), __LINE__));
  1029. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(GpioService_LogCode_Detect_DoorOpen)("[GPIO] The chassis door is open");
  1030. }
  1031. else if (!DetectBit(btInput, OPENSENSOR) && m_bOpenFlag) {
  1032. m_bOpenFlag = false;
  1033. m_DoorCloseTimeStamp = SP::Module::Comm::RVCGetTickCount();
  1034. LogEvent(Severity_Middle, LOG_EVT_OPENSENSOR_OFF, CSimpleStringA::Format("%s [InActive] line: %d", GetDriverPortString(18), __LINE__));
  1035. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(GpioService_LogCode_Detect_DoorClose).setCostTime(m_DoorCloseTimeStamp-m_DoorOpenTimeStamp)("[GPIO] The chassis door is close");
  1036. }
  1037. /**话机检测*/
  1038. if (DetectBit(btInput, PICKUPSENSOR)) {
  1039. if (m_ePickUpFlag == InActive) {
  1040. m_ePickUpFlag = Actived;
  1041. m_PickUpTimeStamp = SP::Module::Comm::RVCGetTickCount();
  1042. LogEvent(Severity_Middle, LOG_EVT_PICKUP, "Life the Phone up");
  1043. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(GpioService_LogCode_Detect_PhonePickup)("[GPIO] 话机提起");
  1044. }
  1045. else if (m_ePickUpFlag == UnknownStatus) {
  1046. m_ePickUpFlag = Actived;
  1047. m_PickUpTimeStamp = SP::Module::Comm::RVCGetTickCount();
  1048. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(GpioService_LogCode_Detect_PhonePickup)("[GPIO] 话机提起(启动时)");
  1049. }
  1050. } else if (!DetectBit(btInput, PICKUPSENSOR)) {
  1051. if (m_ePickUpFlag == Actived) {
  1052. m_ePickUpFlag = InActive;
  1053. m_PutDownTimeStamp = SP::Module::Comm::RVCGetTickCount();
  1054. LogEvent(Severity_Middle, LOG_EVT_ONHOOK, "Put the Phone down");
  1055. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(GpioService_LogCode_Detect_PhonePutDown).setCostTime(m_PutDownTimeStamp - m_PickUpTimeStamp)("话机放下");
  1056. }
  1057. else if (m_ePickUpFlag == UnknownStatus) {
  1058. m_ePickUpFlag = InActive;
  1059. m_PutDownTimeStamp = SP::Module::Comm::RVCGetTickCount();
  1060. }
  1061. }
  1062. /*物体靠近感知检测*/
  1063. if (DetectBit(btInput, MOVESENSOR)) {
  1064. if (!m_bMoveFlag) {
  1065. m_bMoveFlag = true;
  1066. m_moveDisappearTimes = 0;
  1067. LogEvent(Severity_Middle, LOG_EVT_MOVESENSOR_ON, CSimpleStringA::Format("%s [Active] line: %d, times: %lu", GetDriverPortString(20), __LINE__, m_moveHoldTimes));
  1068. }
  1069. /** 只是发一次时间时,发现感知实体没有收到或者不处理第一次发出的时间,所以需要沿用原有的发送频率,再继续发送消失的事件,下同 [Gifur@202496]*/
  1070. else if(m_moveHoldTimes != 0 && (m_moveHoldTimes % (REPEAT_FIRE_TIMEOUT_VALUE / ON_EVENT_DETECT_TIMOUE_MILLSECS))== 0){
  1071. LogEvent(Severity_Middle, LOG_EVT_MOVESENSOR_ON, CSimpleStringA::Format("%s [Active] line: %d, times: %lu", GetDriverPortString(20), __LINE__, m_moveHoldTimes));
  1072. }
  1073. const bool toCheck = !!(m_moveHoldTimes != 0 && (m_moveHoldTimes % (8/*mins*/ * 60 * 1000 / ON_EVENT_DETECT_TIMOUE_MILLSECS) == 0));
  1074. if (toCheck) {
  1075. SYSTEMTIME localTime;
  1076. GetLocalTime(&localTime);
  1077. if (localTime.wHour < (WORD)8 || (localTime.wHour == 8 && localTime.wMinute <= (WORD)30) || localTime.wHour >= (WORD)18)
  1078. {
  1079. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA2903")("Move detect is abnormal(off work).");
  1080. LogWarn(Severity_High, Error_Unexpect, LOG_EVT_MOVEDETECT_ABNORMAL_OFFWORK, "Move detect is abnormal(off work).");
  1081. }
  1082. else {
  1083. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA2902")("Move detect is abnormal(Work time).");
  1084. LogWarn(Severity_High, Error_Unexpect, LOG_EVT_MOVEDETECT_ABNORMAL_WORKTIME, "Move detect is abnormal(Work time).");
  1085. }
  1086. }
  1087. m_moveHoldTimes++;
  1088. }
  1089. else if (!DetectBit(btInput, MOVESENSOR)) {
  1090. if (m_bMoveFlag) {
  1091. m_bMoveFlag = false;
  1092. m_moveHoldTimes = 0;
  1093. LogEvent(Severity_Middle, LOG_EVT_MOVESENSOR_OFF, CSimpleStringA::Format("%s [InActive] line: %d", GetDriverPortString(20), __LINE__));
  1094. }
  1095. else if (m_moveDisappearTimes != 0 && (m_moveDisappearTimes % (REPEAT_FIRE_TIMEOUT_VALUE / ON_EVENT_DETECT_TIMOUE_MILLSECS)) == 0){
  1096. LogEvent(Severity_Middle, LOG_EVT_MOVESENSOR_OFF, CSimpleStringA::Format("%s [InActive] line: %d", GetDriverPortString(20), __LINE__));
  1097. }
  1098. m_moveDisappearTimes++;
  1099. }
  1100. /*卡嘴异物检测*/
  1101. if (DetectBit(btInput, CARDGATESENSOR) && !m_bCardGateFlag) {
  1102. m_bCardGateFlag = true;
  1103. LogEvent(Severity_Middle, LOG_EVT_CARDGATESENSOR, CSimpleStringA::Format("%s [Active] line: %d", GetDriverPortString(21), __LINE__));
  1104. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(GpioService_LogCode_Detect_CardMouseJam)("[GPIO] The card mouse is jam");
  1105. }
  1106. else if (!DetectBit(btInput, CARDGATESENSOR) && m_bCardGateFlag) {
  1107. LogEvent(Severity_Middle, LOG_EVT_CARDGATESENSOR_OFF, CSimpleStringA::Format("%s [InActive] line: %d", GetDriverPortString(21), __LINE__));
  1108. m_bCardGateFlag = false;
  1109. }
  1110. GetFunction()->ResetTimer(pGci->timerID, ON_EVENT_DETECT_TIMOUE_MILLSECS);
  1111. }
  1112. SP_BEGIN_ENTITY_MAP()
  1113. SP_ENTITY(CGpioEntity)
  1114. SP_END_ENTITY_MAP()