CardReadAdapterFSM.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpHelper.h"
  4. #include "CardReadAdapterFSM.h"
  5. #include "GetDevInfoHelper.h"
  6. #include "EventCode.h"
  7. #include "ModuleMix.h"
  8. #include <algorithm>
  9. #include <map>
  10. using namespace std;
  11. //CSimpleStringA<T> ambigulous
  12. #define _ATL_NO_AUTOMATIC_NAMESPACE
  13. //#include <atltime.h>
  14. class CCardReadAdapterEntity;
  15. void CCardReadAdapterFSM::s0_on_entry()
  16. {
  17. LOG_FUNCTION();
  18. CSystemStaticInfo sysInfo;
  19. m_csMachineType = m_csSite = m_terminalNo = "";
  20. GetEntityBase()->GetFunction()->GetSystemStaticInfo(sysInfo);
  21. m_csMachineType = sysInfo.strMachineType;
  22. m_csSite = sysInfo.strSite;
  23. m_terminalNo = sysInfo.strTerminalID;
  24. m_majorVerion = sysInfo.MachineVersion.GetMajor();
  25. m_minorVerion = sysInfo.MachineVersion.GetMinor();
  26. Dbg("machineType:%s,site:%s,terminalNo:%s,machineVersion:%d.%d", (const char*)m_csMachineType, (const char*)m_csSite, (const char*)m_terminalNo
  27. , m_majorVerion, m_minorVerion);
  28. m_eMachineType = MachineType_Unknown;
  29. if (_strnicmp((const char*)m_csMachineType, "RVC.Stand2S", strlen("RVC.Stand2S")) == 0)
  30. m_eMachineType = MachineType_RVC_Stand2S;
  31. else if (_strnicmp((const char*)m_csMachineType, "RVC.PAD", strlen("RVC.PAD")) == 0)
  32. m_eMachineType = MachineType_RVC_PAD;
  33. else if (_strnicmp((const char*)m_csMachineType, "RVC.Wall", strlen("RVC.Wall")) == 0)
  34. m_eMachineType = MachineType_RVC_Wall;
  35. // --Josephus at 8:17:02 201741
  36. else if (_strnicmp((const char*)m_csMachineType, "RPM.Stand1S", strlen("RPM.Stand1S")) == 0)
  37. m_eMachineType = MachineType_RPM_Stand1S;
  38. else if (_strnicmp((const char*)m_csMachineType, "RVC.CardStore", strlen("RVC.CardStore")) == 0)
  39. m_eMachineType = MachineType_RVC_CardStore;
  40. else if (_strnicmp((const char*)m_csMachineType, "RVC.CardPrinter", strlen("RVC.CardPrinter")) == 0)
  41. m_eMachineType = MachineType_RVC_CardPrinter;
  42. else if (_strnicmp((const char*)m_csMachineType, "RVC.Desk2S", strlen("RVC.Desk2S")) == 0)
  43. m_eMachineType = MachineType_RVC_DESK2S;
  44. else if (_strnicmp((const char*)m_csMachineType, "RVC.Desk1S", strlen("RVC.Desk1S")) == 0)
  45. m_eMachineType = MachineType_RVC_DESK1S;
  46. else if (_strnicmp((const char*)m_csMachineType, "RVC.IL", strlen("RVC.IL")) == 0)
  47. {
  48. m_bRVCIL = true;
  49. m_eMachineType = MachineType_RVC_IL;
  50. }
  51. FSMEvent *pEvt;
  52. pEvt = new FSMEvent(USER_EVT_INIT);
  53. PostEventFIFO(pEvt);
  54. }
  55. void CCardReadAdapterFSM::s0_on_exit()
  56. {
  57. LOG_FUNCTION();
  58. }
  59. unsigned int CCardReadAdapterFSM::s0_on_event(FSMEvent* pEvt)
  60. {
  61. LOG_FUNCTION();
  62. Dbg("s0 evt %d", pEvt->iEvt);
  63. switch (pEvt->iEvt)
  64. {
  65. case USER_EVT_INIT:
  66. {
  67. InitTask* task = new InitTask(this);
  68. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  69. pEvt->SetHandled();
  70. return 0;
  71. }
  72. break;
  73. default:
  74. break;
  75. }
  76. return 0;
  77. }
  78. //Initializing
  79. void CCardReadAdapterFSM::s1_on_entry()
  80. {
  81. LOG_FUNCTION();
  82. }
  83. void CCardReadAdapterFSM::s1_on_exit()
  84. {
  85. LOG_FUNCTION();
  86. }
  87. unsigned int CCardReadAdapterFSM::s1_on_event(FSMEvent* pEvt)
  88. {
  89. LOG_FUNCTION();
  90. Dbg("s1 evt %d", pEvt->iEvt);
  91. switch (pEvt->iEvt)
  92. {
  93. case USER_EVT_INIT_FINISHED:
  94. pEvt->SetHandled();
  95. return pEvt->param1;
  96. default:
  97. break;
  98. }
  99. return 0;
  100. }
  101. //Idle
  102. void CCardReadAdapterFSM::s2_on_entry()
  103. {
  104. LOG_FUNCTION();
  105. GetEntityBase()->GetFunction()->SetUserDefineState(USER_CARDISSUER_IDLE);
  106. Dbg("set reset times 0.");
  107. m_resetTimes = 0;
  108. m_testResult = Error_Succeed;
  109. m_bIssuingExit = false;
  110. m_bCancelAccept = false;
  111. m_lastS2Event = m_lastS2EvtParam1 = 0;
  112. //oiltmp 测试便携厂商发卡
  113. //m_pCardIssuer->SetIssuerCounter(0, 1);
  114. //m_pCardIssuer->MoveCard(CI_MOVECARD_FROM_HOPPER);
  115. //MagTracks magTracks;
  116. //magTracks.eRange = CI_TRACK_RANGE_2_3;
  117. //m_pCardIssuer->MagRead(magTracks);
  118. //Dbg("%s,%d,%d", magTracks.track[1].data, magTracks.track[1].dwSize, magTracks.track[1].eStatus);
  119. //m_pCardIssuer->MoveCard(CI_MOVECARD_FRONT_GATE);
  120. //ErrorCodeEnum err = m_pIDCard->SetCardInType(CARD_IN_TYPE_ALL);
  121. //Dbg("accept[%d]",err);
  122. //Sleep(WAIT_INTERVAL*10);
  123. //LPCardAccount* ppCardAccount;
  124. ////err = m_pIDCard->GetCardAccount(IDC_TRACK_1|IDC_TRACK_2|IDC_TRACK_3,&ppCardAccount);
  125. ////Dbg("read[%d]",err);
  126. ////Dbg("%d,%d,%d",ppCardAccount[0]->dataStatus,(ppCardAccount[1])->dataStatus,(ppCardAccount[2])->dataStatus);//,pCardAccount->data);
  127. ////Dbg("%d,%d,%d",ppCardAccount[0]->length,(ppCardAccount[1])->length,(ppCardAccount[2])->length);//,pCardAccount->data);
  128. ////Dbg("read data[%s][%s][%s]",(char*)(ppCardAccount[0]->data),(char*)(ppCardAccount[1]->data),(char*)(ppCardAccount[2]->data));
  129. //err = m_pIDCard->DectectCardType(m_cardType);
  130. //Dbg("detectcardtyp[%d][%d]",err,m_cardType);
  131. //err = m_pIDCard->ActiveCard();
  132. //Dbg("activecard[%d]",err);
  133. //vector<AIDData> vAIDs;
  134. //MessageBoxA(0,0,0,0);
  135. //if (m_cardType == CARDTYPE_CPU_T0 || m_cardType == CARDTYPE_CPU_T1)
  136. //{
  137. // BuildSupportedAppList(vAIDs);
  138. // Dbg("[%s]",testIC);
  139. // AppSelected(m_vADFRec.at(0).name,m_vADFRec.at(0).nameLen);
  140. //}
  141. }
  142. void CCardReadAdapterFSM::s2_on_exit()
  143. {
  144. LOG_FUNCTION();
  145. }
  146. unsigned int CCardReadAdapterFSM::s2_on_event(FSMEvent* pEvt)
  147. {
  148. Dbg("s2 evt(%d),param1:%d", pEvt->iEvt,pEvt->param1);
  149. m_lastS2Event = pEvt->iEvt;
  150. m_lastS2EvtParam1 = pEvt->param1;
  151. int ret = 0;
  152. switch (pEvt->iEvt)
  153. {
  154. case USER_EVT_READ:
  155. {
  156. //oilyang@20200426 according to MachineType ***AND MachineVersion*** to decide which Card-like entity to be called
  157. //RVC.Stand2S, RVC.PAD, RVC.IL, RPM.Stand1S, RVC.CardStore, RVC.Wall, RVC.Desk2S, RVC.Desk1S
  158. // ContactlessCard:
  159. // --RVC.Stand2S, RVC.Desk2S(all MachineVersion), RVC.Desk1S(all MachineVersion)
  160. // CardSwiper:
  161. // --except for RVC.Stand2S,RVC.CardStore,RVC.CardPrinter
  162. // --oilyang@20200730 according to meeting with Ratail/Test/Hangzhou
  163. // Desk2S 2.1 have no CardSwiper,so no need call it
  164. // CardIssuer:
  165. // --no matter what MachineType is,just call it ,except for "RVC.Desk2S 2.0"
  166. // -- see the following "except for cross call" ,what the f**king line means?
  167. pEvt->SetHandled();
  168. CardReadEvent* cre = dynamic_cast<CardReadEvent*>(pEvt);
  169. bool bCardIssuer, bCardSwiper, bContactlessCard;
  170. bCardIssuer = bCardSwiper = bContactlessCard = false;
  171. if (m_eMachineType == MachineType_RVC_Stand2S || m_eMachineType == MachineType_RVC_DESK2S
  172. || m_eMachineType == MachineType_RVC_DESK1S)
  173. {
  174. ContactlessCardReadTask* cTask = new ContactlessCardReadTask(this);
  175. cTask->ctx = cre->ctx;
  176. if (IsTheEntity(cre->ctx->Req.module, Module_ContactlessCard) && IsContactlessCardSessionOK())
  177. {
  178. bContactlessCard = true;
  179. GetEntityBase()->GetFunction()->PostThreadPoolTask(cTask);
  180. Dbg("to ContactlessCard");
  181. }
  182. }
  183. //oilyang@20200730 according to meeting with Ratail/Test/Hangzhou
  184. //Desk2S 2.1 have no CardSwiper,so no need call it
  185. if (!(m_eMachineType == MachineType_RVC_Stand2S
  186. || m_eMachineType == MachineType_RVC_CardStore
  187. || m_eMachineType == MachineType_RVC_CardPrinter
  188. || m_eMachineType == MachineType_RVC_DESK1S
  189. || (m_eMachineType == MachineType_RVC_DESK2S && m_majorVerion == 2 && m_minorVerion == 1)))
  190. {
  191. CardSwiperReadTask* sTask = new CardSwiperReadTask(this);
  192. sTask->ctx = cre->ctx;
  193. if (IsTheEntity(cre->ctx->Req.module, Module_CardSwiper) && IsCardSwiperSessionOK())
  194. {
  195. bCardSwiper = true;
  196. GetEntityBase()->GetFunction()->PostThreadPoolTask(sTask);
  197. Dbg("to CardSwiper");
  198. }
  199. }
  200. //oilyang@20200426 暂时未发现这行代码的用处,可能简版?后来废弃。暂时保留,后续考虑去掉
  201. if ((cre->ctx->Req.reserved1.GetCount() == 0 || cre->ctx->Req.reserved1[0] != 1))//except for cross call
  202. {
  203. CardIssuerReadTask* iTask = new CardIssuerReadTask(this);
  204. iTask->ctx = cre->ctx;
  205. if (IsTheEntity(cre->ctx->Req.module, Module_CardIssuer) && IsCardIssuerSessionOK())
  206. {
  207. bCardIssuer = true;
  208. GetEntityBase()->GetFunction()->PostThreadPoolTask(iTask);
  209. Dbg("to CardIssuer");
  210. }
  211. if (bCardIssuer || bCardSwiper || bContactlessCard)
  212. Dbg("bCardIssuer,bCardSwiper,bContactlessCard:%d,%d,%d", bCardIssuer, bCardSwiper, bContactlessCard);
  213. else
  214. {
  215. Dbg("can't find the right entity.");
  216. ret = 1;//no need to change to "Working" state
  217. cre->ctx->Answer(Error_Unexpect);
  218. }
  219. }
  220. }
  221. break;
  222. case USER_EVT_PREONLINE:
  223. {
  224. PreOnlineEvent* poe = dynamic_cast<PreOnlineEvent*>(pEvt);
  225. PreOnlineTask* task = new PreOnlineTask(this);
  226. task->ctx = poe->ctx;
  227. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  228. pEvt->SetHandled();
  229. return 0;
  230. }
  231. case USER_EVT_WRITE:
  232. {
  233. pEvt->SetHandled();
  234. CardWriteEvent* cwe = dynamic_cast<CardWriteEvent*>(pEvt);
  235. WriteTask* task = new WriteTask(this);
  236. task->ctx = cwe->ctx;
  237. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  238. return 0;
  239. }
  240. case USER_EVT_POSTONLINE:
  241. {
  242. PostOnlineEvent* poe = dynamic_cast<PostOnlineEvent*>(pEvt);
  243. PostOnlineTask* task = new PostOnlineTask(this);
  244. task->ctx = poe->ctx;
  245. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  246. pEvt->SetHandled();
  247. return 0;
  248. }
  249. case USER_EVT_EXIT:
  250. {
  251. m_busCtx.eCardFromWhich = Card_In_No_Where;
  252. pEvt->SetHandled();
  253. DoExitTask* task = new DoExitTask(this);
  254. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  255. }
  256. break;
  257. case USER_EVT_EJECT:
  258. {
  259. pEvt->SetHandled();
  260. CardEjectEvent* cee = dynamic_cast<CardEjectEvent*>(pEvt);
  261. EjectTask* task = new EjectTask(this);
  262. task->ctx = cee->ctx;
  263. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  264. }
  265. break;
  266. case USER_EVT_SET_SOME_FLAG:
  267. {
  268. pEvt->SetHandled();
  269. SetSomeFlagEvent* ssfe = dynamic_cast<SetSomeFlagEvent*>(pEvt);
  270. SetSomeFlagTask* task = new SetSomeFlagTask(this);
  271. task->ctx = ssfe->ctx;
  272. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  273. }
  274. break;
  275. case USER_EVT_GET_DEV_INFO:
  276. {
  277. pEvt->SetHandled();
  278. GetDevInfoEvent* gdie = dynamic_cast<GetDevInfoEvent*>(pEvt);
  279. GetDevInfoTask* task = new GetDevInfoTask(this);
  280. task->ctx = gdie->ctx;
  281. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  282. }
  283. break;
  284. case USER_EVT_QUERY_CARD_INFO:
  285. {
  286. pEvt->SetHandled();
  287. QueryCardInfoEvent* qcie = dynamic_cast<QueryCardInfoEvent*>(pEvt);
  288. QueryCardInfoTask* task = new QueryCardInfoTask(this);
  289. task->ctx = qcie->ctx;
  290. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  291. }
  292. break;
  293. case USER_EVT_QUIT:
  294. pEvt->SetHandled();
  295. break;
  296. case USER_EVT_ISSUE:
  297. {
  298. pEvt->SetHandled();
  299. CardIssueEvent* cee = dynamic_cast<CardIssueEvent*>(pEvt);
  300. IssueTask* task = new IssueTask(this);
  301. task->ctx = cee->ctx;
  302. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  303. }
  304. break;
  305. case USER_EVT_CAPTURE:
  306. {
  307. pEvt->SetHandled();
  308. CardCaptureEvent* cee = dynamic_cast<CardCaptureEvent*>(pEvt);
  309. CaptureTask* task = new CaptureTask(this);
  310. task->ctx = cee->ctx;
  311. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  312. }
  313. break;
  314. case USER_EVT_GET_MATERIAL:
  315. {
  316. pEvt->SetHandled();
  317. GetMaterialEvent *gme = dynamic_cast<GetMaterialEvent*>(pEvt);
  318. GetMaterialCountTask *task = new GetMaterialCountTask(this);
  319. task->ctx = gme->ctx;
  320. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  321. break;
  322. }
  323. case USER_EVT_SET_MATERIAL:
  324. {
  325. pEvt->SetHandled();
  326. SetMaterialEvent *sme = dynamic_cast<SetMaterialEvent*>(pEvt);
  327. SetMaterialCountTask *task = new SetMaterialCountTask(this);
  328. task->ctx = sme->ctx;
  329. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  330. break;
  331. }
  332. case USER_EVT_GET_SCI_INFO:
  333. {
  334. pEvt->SetHandled();
  335. GetSCIInfoEvent* gsie = dynamic_cast<GetSCIInfoEvent*>(pEvt);
  336. GetSCIInfoTask* task = new GetSCIInfoTask(this);
  337. task->ctx = gsie->ctx;
  338. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  339. }
  340. break;
  341. case USER_EVT_OPEN_SAFELOCK:
  342. {
  343. pEvt->SetHandled();
  344. OpenSafeLockEvent *osle = dynamic_cast<OpenSafeLockEvent*>(pEvt);
  345. OpenSafeLockTask* task = new OpenSafeLockTask(this);
  346. task->ctx = osle->ctx;
  347. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  348. }
  349. break;
  350. case USER_EVT_MAGTRANS_INIT:
  351. {
  352. pEvt->SetHandled();
  353. MagTransferInitEvent* mtie = dynamic_cast<MagTransferInitEvent*>(pEvt);
  354. MagTransferInitTask* task = new MagTransferInitTask(this);
  355. task->ctx = mtie->ctx;
  356. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  357. }
  358. break;
  359. case USER_EVT_QUERY_CONN_INFO:
  360. {
  361. pEvt->SetHandled();
  362. QueryConnInfoEvent* qcie = dynamic_cast<QueryConnInfoEvent*>(pEvt);
  363. QueryConnInfoTask* task = new QueryConnInfoTask(this);
  364. task->ctx = qcie->ctx;
  365. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  366. }
  367. break;
  368. case USER_EVT_SAM_IC_COMMAND:
  369. {
  370. pEvt->SetHandled();
  371. SAMICCommandEvent* samice = dynamic_cast<SAMICCommandEvent*>(pEvt);
  372. SAMICCommandTask* task = new SAMICCommandTask(this);
  373. task->ctx = samice->ctx;
  374. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  375. }
  376. break;
  377. case USER_EVT_QUERY_PRINTER_STATUS:
  378. {
  379. pEvt->SetHandled();
  380. QueryPrinterStatusEvent* samice = dynamic_cast<QueryPrinterStatusEvent*>(pEvt);
  381. QueryPrinterStatusTask* task = new QueryPrinterStatusTask(this);
  382. task->ctx = samice->ctx;
  383. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  384. }
  385. break;
  386. case USER_EVT_PRINT:
  387. {
  388. pEvt->SetHandled();
  389. PrintEvent* samice = dynamic_cast<PrintEvent*>(pEvt);
  390. PrintTask* task = new PrintTask(this);
  391. task->ctx = samice->ctx;
  392. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  393. }
  394. break;
  395. case USER_EVT_QUERY_SCILIST:
  396. {
  397. pEvt->SetHandled();
  398. QuerySCIListEvent* samice = dynamic_cast<QuerySCIListEvent*>(pEvt);
  399. QuerySCIListTask* task = new QuerySCIListTask(this);
  400. task->ctx = samice->ctx;
  401. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  402. }
  403. break;
  404. case USER_EVT_BIND_SCI:
  405. {
  406. pEvt->SetHandled();
  407. BindSCIEvent* samice = dynamic_cast<BindSCIEvent*>(pEvt);
  408. BindSCITask* task = new BindSCITask(this);
  409. task->ctx = samice->ctx;
  410. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  411. }
  412. break;
  413. case USER_EVT_PREONLINE_ON_STORE:
  414. {
  415. pEvt->SetHandled();
  416. PreOnlineOnStoreEvent* samice = dynamic_cast<PreOnlineOnStoreEvent*>(pEvt);
  417. PreOnlineOnStoreTask* task = new PreOnlineOnStoreTask(this);
  418. task->ctx = samice->ctx;
  419. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  420. }
  421. break;
  422. case USER_EVT_GET_ADD_CARD_INFO:
  423. {
  424. pEvt->SetHandled();
  425. GetAddCardInfoEvent* samice = dynamic_cast<GetAddCardInfoEvent*>(pEvt);
  426. GetAddCardInfoTask* task = new GetAddCardInfoTask(this);
  427. task->ctx = samice->ctx;
  428. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  429. }
  430. break;
  431. case USER_EVT_NOTIFY_PREONLINE:
  432. {
  433. pEvt->SetHandled();
  434. NotifyPreonlineEvent* samice = dynamic_cast<NotifyPreonlineEvent*>(pEvt);
  435. NotifyPreOnlineTask* task = new NotifyPreOnlineTask(this);
  436. task->ctx = samice->ctx;
  437. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  438. }
  439. break;
  440. case USER_EVT_QUERY_CARD_INFO_ON_STORE:
  441. {
  442. pEvt->SetHandled();
  443. QueryCardInfoOnStoreEvent* samice = dynamic_cast<QueryCardInfoOnStoreEvent*>(pEvt);
  444. QueryCardInfoOnStoreTask* task = new QueryCardInfoOnStoreTask(this);
  445. task->ctx = samice->ctx;
  446. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  447. }
  448. break;
  449. case USER_EVT_CROSS_TERM_CALL:
  450. {
  451. pEvt->SetHandled();
  452. CrossTermCallEvent* ctce = dynamic_cast<CrossTermCallEvent*>(pEvt);
  453. CrossTermCallTask* task = new CrossTermCallTask(this);
  454. task->ctx = ctce->ctx;
  455. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  456. }
  457. break;
  458. case USER_EVT_PRINT_CARD_IM:
  459. {
  460. pEvt->SetHandled();
  461. PrintCardImEvent* pcie = dynamic_cast<PrintCardImEvent*>(pEvt);
  462. PrintCardImTask* task = new PrintCardImTask(this);
  463. task->ctx = pcie->ctx;
  464. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  465. }
  466. break;
  467. default:
  468. break;
  469. }
  470. return ret;
  471. }
  472. //Working
  473. void CCardReadAdapterFSM::s3_on_entry()
  474. {
  475. LOG_FUNCTION();
  476. }
  477. void CCardReadAdapterFSM::s3_on_exit()
  478. {
  479. LOG_FUNCTION();
  480. }
  481. unsigned int CCardReadAdapterFSM::s3_on_event(FSMEvent* pEvt)
  482. {
  483. Dbg("s3 evt %d,%d", pEvt->iEvt,pEvt->param1);
  484. int ret = 0;
  485. switch (pEvt->iEvt)
  486. {
  487. case USER_EVT_READ_FINISHED:
  488. {
  489. pEvt->SetHandled();
  490. ret = pEvt->param1;
  491. //if (pEvt->param1 == 0)
  492. CardReadFinishedEvent* crfe = dynamic_cast<CardReadFinishedEvent*>(pEvt);
  493. {
  494. if (m_bCancelAccept)
  495. {
  496. Dbg("front cancel");
  497. crfe->ctx->Answer(Error_Cancel);
  498. ret = 0;
  499. }
  500. }
  501. if (pEvt->param1 == 7)//CardIssuer Timeout,need to cancel ContactlessCard insert
  502. {
  503. Dbg("CardIssuer Timeout,need to cancel ContactlessCard insert");
  504. if (IsContactlessCardSessionOK())
  505. m_pContactless->CancelInsert();
  506. crfe->ctx->Answer(Error_TimeOut);
  507. ret = 0;
  508. break;
  509. }
  510. else if (pEvt->param1 == 8)//ContactlessCard Timeout,need to cancel CardIssuer insert
  511. {
  512. Dbg("ContactlessCard Timeout,need to cancel CardIssuer insert");
  513. if (IsCardIssuerSessionOK())
  514. m_pCardIssuer->CancelInsert();
  515. if (IsCardSwiperSessionOK())
  516. m_pCardSwiper->CancelInsert();
  517. crfe->ctx->Answer(Error_TimeOut);
  518. ret = 0;
  519. break;
  520. }
  521. else if (pEvt->param1 == 9)//CardSwiper Timeout,need to cancel ContactlessCard insert
  522. {
  523. Dbg("ContactlessCard Timeout,need to cancel CardIssuer insert");
  524. if (IsContactlessCardSessionOK())
  525. m_pContactless->CancelInsert();
  526. crfe->ctx->Answer(Error_TimeOut);
  527. ret = 0;
  528. break;
  529. }
  530. if (m_busCtx.eCardFromWhich == Card_In_ContactlessCard)
  531. {
  532. Dbg("as card in ContactlessCard,to cancel others");
  533. if (IsCardIssuerSessionOK())
  534. m_pCardIssuer->CancelInsert();
  535. if (IsCardSwiperSessionOK())
  536. m_pCardSwiper->CancelInsert();
  537. }
  538. else if (m_busCtx.eCardFromWhich == Card_In_CardIssuer)
  539. {
  540. Dbg("as card in CardIssuer,to cancel others");
  541. if (IsContactlessCardSessionOK())
  542. m_pContactless->CancelInsert();
  543. if (IsCardSwiperSessionOK())
  544. m_pCardSwiper->CancelInsert();
  545. }
  546. else if (m_busCtx.eCardFromWhich == Card_In_CardSwiper)
  547. {
  548. Dbg("as card in CardSwiper,to cancel others");
  549. if (IsCardIssuerSessionOK())
  550. m_pCardIssuer->CancelInsert();
  551. if (IsContactlessCardSessionOK())
  552. m_pContactless->CancelInsert();
  553. }
  554. return ret;
  555. }
  556. case USER_EVT_PREONLINE_FINISHED:
  557. pEvt->SetHandled();
  558. return 0;
  559. case USER_EVT_POSTONLINE_FINISHED:
  560. pEvt->SetHandled();
  561. return 0;
  562. case USER_EVT_READ_CANCEL:
  563. pEvt->SetHandled();
  564. m_bCancelAccept = true;
  565. CancelRead();
  566. break;
  567. case USER_EVT_READ_WAIT_MORE:
  568. {
  569. pEvt->SetHandled();
  570. ReadWaitMoreTask* task = new ReadWaitMoreTask(this);
  571. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  572. }
  573. break;
  574. case USER_EVT_EXIT:
  575. {
  576. m_busCtx.eCardFromWhich = Card_In_No_Where;
  577. pEvt->SetHandled();
  578. DoExitTask* task = new DoExitTask(this);
  579. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  580. }
  581. break;
  582. case USER_EVT_WRITE_FINISHED:
  583. case USER_EVT_SET_SOME_FLAG_FINISHED:
  584. case USER_EVT_GET_DEV_INFO_FINISHED:
  585. case USER_EVT_QUERY_CARD_INFO_FINISHED:
  586. case USER_EVT_GET_MATERIAL_FINISHED:
  587. case USER_EVT_SET_MATERIAL_FINISHED:
  588. case USER_EVT_GET_SCI_INFO_FINISHED:
  589. case USER_EVT_OPEN_SAFELOCK_FINISHED:
  590. case USER_EVT_MAGTRANS_INIT_FINISHED:
  591. case USER_EVT_QUERY_CONN_INFO_FINISHED:
  592. case USER_EVT_QUIT:
  593. case USER_EVT_SAM_IC_COMMAND_FINISHED:
  594. case USER_EVT_QUERY_PRINTER_STATUS_FINISHED:
  595. case USER_EVT_PRINT_FINISHED:
  596. case USER_EVT_QUERY_SCILIST_FINISHED:
  597. case USER_EVT_BIND_SCI_FINISHED:
  598. case USER_EVT_PREONLINE_ON_STORE_FINISHED:
  599. case USER_EVT_GET_ADD_CARD_INFO_FINISHED:
  600. case USER_EVT_NOTIFY_PREONLINE_FINISHED:
  601. case USER_EVT_QUERY_CARD_INFO_ON_STORE_FINISHED:
  602. case USER_EVT_CROSS_TERM_CALL_FINISHED:
  603. case USER_EVT_PRINT_CARD_IM_FINISHED:
  604. pEvt->SetHandled();
  605. break;
  606. default:
  607. break;
  608. }
  609. return ret;
  610. }
  611. //Failed
  612. void CCardReadAdapterFSM::s4_on_entry()
  613. {
  614. LOG_FUNCTION();
  615. m_lastS2Event = m_lastS2EvtParam1 = 0;
  616. GetEntityBase()->GetFunction()->SetUserDefineState(USER_CARDISSUER_HOLD);
  617. }
  618. void CCardReadAdapterFSM::s4_on_exit()
  619. {
  620. LOG_FUNCTION();
  621. }
  622. unsigned int CCardReadAdapterFSM::s4_on_event(FSMEvent* pEvt)
  623. {
  624. Dbg("s4 evt(%d)", pEvt->iEvt);
  625. switch (pEvt->iEvt)
  626. {
  627. case USER_EVT_EXIT:
  628. {
  629. pEvt->SetHandled();
  630. DoExitTask* task = new DoExitTask(this);
  631. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  632. }
  633. break;
  634. case USER_EVT_QUIT:
  635. {
  636. pEvt->SetHandled();
  637. return 0;
  638. }
  639. case USER_EVT_SET_SOME_FLAG:
  640. {
  641. pEvt->SetHandled();
  642. SetSomeFlagEvent* ssfe = dynamic_cast<SetSomeFlagEvent*>(pEvt);
  643. SetSomeFlagTask* task = new SetSomeFlagTask(this);
  644. task->ctx = ssfe->ctx;
  645. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  646. }
  647. break;
  648. case USER_EVT_GET_DEV_INFO:
  649. {
  650. pEvt->SetHandled();
  651. GetDevInfoEvent* gdie = dynamic_cast<GetDevInfoEvent*>(pEvt);
  652. GetDevInfoTask* task = new GetDevInfoTask(this);
  653. task->ctx = gdie->ctx;
  654. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  655. }
  656. break;
  657. case USER_EVT_QUERY_CARD_INFO:
  658. {
  659. pEvt->SetHandled();
  660. QueryCardInfoEvent* qcie = dynamic_cast<QueryCardInfoEvent*>(pEvt);
  661. QueryCardInfoTask* task = new QueryCardInfoTask(this);
  662. task->ctx = qcie->ctx;
  663. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  664. }
  665. break;
  666. default:
  667. break;
  668. }
  669. return 0;
  670. }
  671. ErrorCodeEnum CCardReadAdapterFSM::OnInit()
  672. {
  673. LOG_FUNCTION();
  674. m_lastS2Event = m_lastS2EvtParam1 = 0;
  675. m_busCtx.eCardFromWhich = Card_In_No_Where;
  676. return Error_Succeed;
  677. }
  678. ErrorCodeEnum CCardReadAdapterFSM::OnExit()
  679. {
  680. LOG_FUNCTION();
  681. FSMImpl<CCardReadAdapterFSM>::OnExit();
  682. return Error_Succeed;
  683. }
  684. int CCardReadAdapterFSM::Initial()
  685. {
  686. CheckSessionTask *pTask = new CheckSessionTask(this);
  687. GetEntityBase()->GetFunction()->PostThreadPoolTask(pTask);
  688. ErrorCodeEnum eErr;
  689. eErr = GetEntityBase()->GetFunction()->SetSysVar("CardEntityState", "E");
  690. if (eErr != Error_Succeed)
  691. {
  692. Dbg("SetSysVar CardEntityState failed.%d",eErr);
  693. }
  694. return 0;//oiltmp no need to connect. let function "CheckAndReconnectSession" do
  695. }
  696. int CCardReadAdapterFSM::DoWork(int type)
  697. {
  698. switch (type)
  699. {
  700. case USER_EVT_READ:
  701. {
  702. }
  703. break;
  704. default:
  705. Dbg("unkown type:%d",type);
  706. break;
  707. }
  708. return 0;
  709. }
  710. int CCardReadAdapterFSM::CardIssuerRead(SpReqAnsContext<CardReadAdapterService_Read_Req, CardReadAdapterService_Read_Ans>::Pointer ctx)
  711. {
  712. LOG_FUNCTION();
  713. if (m_bCancelAccept || (m_busCtx.eCardFromWhich == Card_In_ContactlessCard || m_busCtx.eCardFromWhich == Card_In_CardSwiper))
  714. {
  715. char msg[128];
  716. memset(msg, 0, 128);
  717. sprintf(msg, "Maybe the thread pool is wrong.CardIssuerRead,bCancel:%d,cardpos:%d", m_bCancelAccept, m_busCtx.eCardFromWhich);
  718. LogWarn(Severity_Low, Error_NotInit, CardReadAdapter_UserErrorCode_MayBe_ThreadPool_Wrong, msg);
  719. return 3;
  720. }
  721. ErrorCodeEnum eErr = Error_Unexpect;
  722. if (IsTheEntity(ctx->Req.module,Module_CardIssuer) && IsCardIssuerSessionOK())
  723. {
  724. CardIssuerService_QueryCardInfo_Req qciReq;
  725. CardIssuerService_QueryCardInfo_Ans qciAns;
  726. bool bHasCard = false, bNeedPreOnline = false;
  727. DWORD dwUsrErrCode = 0;
  728. eErr = m_pCardIssuer->QueryCardInfo(qciReq, qciAns, 10000,dwUsrErrCode);
  729. if (eErr == Error_Succeed)
  730. {
  731. if (qciAns.position == 2)//有卡
  732. {
  733. Dbg("CardIssuer has card");
  734. bHasCard = true;
  735. bNeedPreOnline = true;//不知道卡片类型,默认
  736. m_busCtx.eCardFromWhich = Card_In_CardIssuer;
  737. if (IsContactlessCardSessionOK())
  738. m_pContactless->CancelInsert();
  739. }
  740. else
  741. {
  742. //oilyang@20200730 according to meeting with Ratail/Test/Hangzhou
  743. //Desk2S 2.1 can insert into CardIssuer,so comment the following line
  744. //oilyang@20200426 if "RVC.Desk2S 2.1" ,call insert is forbidden
  745. //if (!(m_eMachineType == MachineType_RVC_DESK2S && m_majorVerion == 2 && m_minorVerion == 1))
  746. {
  747. CardIssuerService_Insert_Req req;
  748. CardIssuerService_Insert_Ans ans;
  749. req.aid = ctx->Req.aid;
  750. Dbg("to call cardissuer insert..");
  751. eErr = m_pCardIssuer->Insert(req, ans, 59000, dwUsrErrCode);
  752. if (eErr == Error_Succeed)
  753. {
  754. Dbg("Card insert into CardIssuer.");
  755. bHasCard = true;
  756. m_busCtx.eCardFromWhich = Card_In_CardIssuer;
  757. if (IsContactlessCardSessionOK())
  758. m_pContactless->CancelInsert();
  759. }
  760. else {
  761. Dbg("CardIssuer Insert return:%d", eErr);
  762. //oilyang@20200827 if time out, we must told front caller
  763. //AND set returned value of 0 to change FSM state
  764. if (eErr == Error_TimeOut)
  765. {
  766. return 7;
  767. }
  768. else
  769. return 1;//oilyang@20190709
  770. }
  771. }
  772. }
  773. if (bHasCard)
  774. {
  775. CardIssuerService_Read_Req reqR;
  776. CardIssuerService_Read_Ans ansR;
  777. reqR.aid = ctx->Req.aid;
  778. reqR.reserved1 = ctx->Req.lightPos;
  779. Dbg("to read ctx->Req.lightPos:%d", ctx->Req.lightPos);
  780. Sleep(100);//zjw@20190725 以防卡机状态还未跳转,这里就已经触发了Read操作
  781. eErr = m_pCardIssuer->Read(reqR, ansR, 50000, dwUsrErrCode);
  782. if (eErr == Error_Succeed)
  783. {
  784. ctx->Ans.track1 = ansR.track1;
  785. ctx->Ans.track2 = ansR.track2;
  786. ctx->Ans.track3 = ansR.track3;
  787. ctx->Ans.ICType = ansR.ICType;
  788. ctx->Ans.ICCardNo = ansR.ICData;
  789. ctx->Ans.status = ansR.status;
  790. if (m_eMachineType == MachineType_RVC_Stand2S)
  791. ctx->Ans.cardPos = CardPos_CardIssuer_Stand2S;
  792. else
  793. ctx->Ans.cardPos = CardPos_CardIssuer_Split;
  794. ctx->Ans.t2Account = ansR.t2Account;
  795. ctx->Ans.t2Region = ansR.t2Region;
  796. ctx->Ans.t2CardSerial = ansR.t2CardSerial;
  797. ctx->Ans.t2CVC = ansR.t2CVC;
  798. ctx->Ans.t2ExpireDate = ansR.t2ExpireDate;
  799. if (ansR.ICType == 1 || ansR.ICType == 3)
  800. bNeedPreOnline = true;
  801. }
  802. }
  803. }
  804. else {
  805. Dbg("CardIssuer QueryCardInfo return:", eErr);
  806. return 1;//oilyang@20190709
  807. }
  808. if (bHasCard && bNeedPreOnline)
  809. {
  810. CardIssuerService_PreOnline_Req req;
  811. CardIssuerService_PreOnline_Ans ans;
  812. req.businessData = ctx->Req.businessData;
  813. BroadcastReadICing(0);
  814. Dbg("to preonline");
  815. eErr = m_pCardIssuer->PreOnline(req, ans, 50000, dwUsrErrCode);
  816. if (eErr == Error_Succeed)
  817. {
  818. ctx->Ans.result = ans.result;
  819. ctx->Ans.status = 0;
  820. if (ctx->Ans.ICType == 1 || ctx->Ans.ICType == 3)
  821. {
  822. CardIssuerService_SetSomeFlag_Req ssfReq;
  823. CardIssuerService_SetSomeFlag_Ans ssfAns;
  824. ssfReq.IssueBusiness = 4;
  825. eErr = m_pCardIssuer->SetSomeFlag(ssfReq, ssfAns, 5000, dwUsrErrCode);
  826. if (eErr == Error_Succeed)
  827. ctx->Ans.ICCardNo = ssfAns.reserved2[0];
  828. }
  829. }
  830. else
  831. {
  832. ctx->Ans.result = "";
  833. ctx->Ans.status = -1;
  834. }
  835. }
  836. if (bHasCard)
  837. {
  838. Dbg("%d", eErr);
  839. ctx->Answer(eErr, dwUsrErrCode);
  840. }
  841. return 0;
  842. }
  843. return 1;
  844. }
  845. int CCardReadAdapterFSM::ContactlessCardRead(SpReqAnsContext<CardReadAdapterService_Read_Req, CardReadAdapterService_Read_Ans>::Pointer ctx)
  846. {
  847. LOG_FUNCTION();
  848. if (m_bCancelAccept || (m_busCtx.eCardFromWhich == Card_In_CardIssuer || m_busCtx.eCardFromWhich == Card_In_CardSwiper))
  849. {
  850. char msg[128];
  851. memset(msg, 0, 128);
  852. sprintf(msg, "Maybe the thread pool is wrong.ContactlessCardRead,bCancel:%d,cardpos:%d", m_bCancelAccept, m_busCtx.eCardFromWhich);
  853. LogWarn(Severity_Low, Error_NotInit, CardReadAdapter_UserErrorCode_MayBe_ThreadPool_Wrong, msg);
  854. return 3;
  855. }
  856. ErrorCodeEnum eErr = Error_Unexpect;
  857. if (IsTheEntity(ctx->Req.module, Module_ContactlessCard) && IsContactlessCardSessionOK())
  858. {
  859. bool bFindCard = false;
  860. //ContactlessCardService_QueryCardInfo_Req qciReq;
  861. //ContactlessCardService_QueryCardInfo_Ans qciAns;
  862. //eErr = m_pContactless->QueryCardInfo(qciReq, qciAns, 10000);
  863. //if (eErr == Error_Succeed)
  864. //{
  865. // if (qciAns.position == 4)//如果有卡,直接读卡
  866. // {
  867. // Dbg("find card on contactless card.");
  868. // m_busCtx.eCardFromWhich = Card_In_ContactlessCard;
  869. // m_pCardIssuer->CancelInsert();
  870. // bFindCard = true;
  871. // }
  872. // else
  873. // {
  874. ContactlessCardService_Insert_Req req;
  875. ContactlessCardService_Insert_Ans ans;
  876. req.aid = ctx->Req.aid;
  877. Dbg("to call contactless insert...");
  878. eErr = m_pContactless->Insert(req, ans, 56000);
  879. if (eErr == Error_Succeed)
  880. {
  881. ctx->Ans.track1 = ans.track1;
  882. ctx->Ans.track2 = ans.track2;
  883. ctx->Ans.track3 = ans.track3;
  884. ctx->Ans.ICType = ans.ICType;
  885. ctx->Ans.ICCardNo = ans.ICData;
  886. ctx->Ans.status = ans.status;
  887. ctx->Ans.cardPos = CardPos_ContactlessCard;
  888. ctx->Ans.t2Account = ans.t2Account;
  889. ctx->Ans.t2Region = ans.t2Region;
  890. ctx->Ans.t2CardSerial = ans.t2CardSerial;
  891. ctx->Ans.t2CVC = ans.t2CVC;
  892. ctx->Ans.t2ExpireDate = ans.t2ExpireDate;
  893. Dbg("Card in ContactlessCard.");
  894. m_busCtx.eCardFromWhich = Card_In_ContactlessCard;
  895. if (IsCardIssuerSessionOK())
  896. m_pCardIssuer->CancelInsert();
  897. if (ans.ICType == 4)
  898. {
  899. bFindCard = true;
  900. }
  901. }
  902. else if (eErr == Error_TimeOut)
  903. {
  904. Dbg("ContactlessCard timeout.");
  905. return 8;
  906. }
  907. else if (eErr == Error_Unexpect)
  908. {
  909. ctx->Answer(eErr);
  910. Dbg("read failed.");
  911. return 2;
  912. }
  913. // }
  914. //}
  915. if (bFindCard)
  916. {
  917. //oilyang@20210223 add,The Entity FSM is too late to change state ,as the call coming so fast
  918. Sleep(100);
  919. ContactlessCardService_PreOnline_Req req;
  920. ContactlessCardService_PreOnline_Ans ans;
  921. req.businessData = ctx->Req.businessData;
  922. BroadcastReadICing(0);
  923. eErr = m_pContactless->PreOnline(req, ans, 50000);
  924. Dbg("PreOnline result:%d", eErr);
  925. if (eErr == Error_Succeed)
  926. {
  927. ctx->Ans.result = ans.result;
  928. ctx->Ans.status = 0;
  929. }
  930. else
  931. {
  932. ctx->Ans.result = "";
  933. ctx->Ans.status = -1;
  934. }
  935. ctx->Answer(eErr);
  936. return 0;
  937. }
  938. //ctx->Answer(eErr);
  939. }
  940. return 1;
  941. }
  942. int CCardReadAdapterFSM::IssueCard(SpReqAnsContext<CardReadAdapterService_Issue_Req, CardReadAdapterService_Issue_Ans>::Pointer ctx)
  943. {
  944. if (IsTheEntity(ctx->Req.module, Module_CardIssuer) && IsCardIssuerSessionOK())
  945. {
  946. ErrorCodeEnum eErr = Error_Unexpect;
  947. CardIssuerService_IssueEx_Req req;
  948. CardIssuerService_IssueEx_Ans ans;
  949. req.hopper = ctx->Req.hopper;
  950. req.reserved1.Copy(ctx->Req.reserved1);
  951. req.reserved2.Copy(ctx->Req.reserved2);
  952. DWORD dwUsrErrCode = 0;
  953. eErr = m_pCardIssuer->IssueEx(req, ans, 60000, dwUsrErrCode);
  954. if (eErr == Error_Succeed)
  955. {
  956. if (ans.reserved1.GetCount() > 0)
  957. ctx->Ans.reserved1.Copy(ans.reserved1);
  958. if (ans.reserved2.GetCount() > 0)
  959. ctx->Ans.reserved2.Copy(ans.reserved2);
  960. // --Josephus at 11:09:47 201747
  961. //oilyang@20180315 kaku
  962. //if(m_busCtx.eCardFromWhich == Card_In_No_Where)
  963. if (req.hopper == 99 || req.hopper == 98 || req.hopper == 89)
  964. {
  965. if (ans.reserved1[0] == 0 && ans.reserved1[1] == 0)
  966. m_busCtx.eCardFromWhich = Card_In_CardIssuer;
  967. }
  968. else if (req.hopper == 88 || req.hopper == 87)
  969. {
  970. m_busCtx.eCardFromWhich = Card_In_No_Where;
  971. }
  972. else
  973. m_busCtx.eCardFromWhich = Card_In_CardIssuer;
  974. Dbg("req.hopper:%d Deem that CardIssuer has card after issuing succ. Set busCtx:findCard:%d,cardPos:%d"
  975. , req.hopper, ans.reserved1[0], ans.reserved1[1]);
  976. }
  977. ctx->Answer(eErr, dwUsrErrCode);
  978. return 0;
  979. }
  980. else
  981. {
  982. Dbg("issue card param error or connection is failed:(module)%d",ctx->Req.module);
  983. ctx->Answer(Error_Param);
  984. return 1;
  985. }
  986. }
  987. int CCardReadAdapterFSM::CardSwiperRead(SpReqAnsContext<CardReadAdapterService_Read_Req, CardReadAdapterService_Read_Ans>::Pointer ctx,int &eRetErr)
  988. {
  989. if (m_bCancelAccept || (m_busCtx.eCardFromWhich == Card_In_CardIssuer || m_busCtx.eCardFromWhich == Card_In_ContactlessCard))
  990. {
  991. char msg[128];
  992. memset(msg, 0, 128);
  993. sprintf(msg, "Maybe the thread pool is wrong.CardSwiperRead,bCancel:%d,cardpos:%d", m_bCancelAccept, m_busCtx.eCardFromWhich);
  994. LogWarn(Severity_Low, Error_NotInit, CardReadAdapter_UserErrorCode_MayBe_ThreadPool_Wrong, msg);
  995. return 3;
  996. }
  997. ErrorCodeEnum eErr = Error_Unexpect;
  998. if (IsTheEntity(ctx->Req.module, Module_CardSwiper) && IsCardSwiperSessionOK())
  999. {
  1000. CardSwiperService_Read_Req req;
  1001. CardSwiperService_Read_Ans ans;
  1002. req.aid = ctx->Req.aid;
  1003. req.LightPos = 0;
  1004. if (ctx->Req.lightPos == 0)
  1005. req.LightPos = 7;
  1006. if ((ctx->Req.lightPos&LightPos_CardSwiper_Mag) == LightPos_CardSwiper_Mag)
  1007. req.LightPos += 1;
  1008. if ((ctx->Req.lightPos&LightPos_CardSwiper_IC) == LightPos_CardSwiper_IC)
  1009. req.LightPos += 2;
  1010. if ((ctx->Req.lightPos&LightPos_CardSwiper_RFIC) == LightPos_CardSwiper_RFIC)
  1011. req.LightPos += 4;
  1012. Dbg("aid:%s,lightPos:%d",(const char*)req.aid,req.LightPos);
  1013. eErr = m_pCardSwiper->Read(req, ans, 61000);
  1014. Dbg("eErr:%d", eErr);
  1015. eRetErr = eErr;
  1016. if (eErr == Error_Succeed)
  1017. {
  1018. m_busCtx.eCardFromWhich = Card_In_CardSwiper;
  1019. //oilyang@20180913 由于CardSwiper没有插卡过程,直接进行pboc流程,需要提前取消其他实体的输入
  1020. if (IsCardIssuerSessionOK())
  1021. m_pCardIssuer->CancelInsert();
  1022. if (IsContactlessCardSessionOK())
  1023. m_pContactless->CancelInsert();
  1024. ctx->Ans.track1 = ans.track1;
  1025. ctx->Ans.track2 = ans.track2;
  1026. ctx->Ans.track3 = ans.track3;
  1027. if (ans.ICType == 5)//oilyang@20170204 如果插错卡,反馈为未检测到(有效)卡
  1028. ctx->Ans.ICType = 0;
  1029. else if (ans.ICType == 6)//身份证
  1030. ctx->Ans.ICType = 5;
  1031. else
  1032. ctx->Ans.ICType = ans.ICType;
  1033. ctx->Ans.ICCardNo = ans.ICData;
  1034. ctx->Ans.status = ans.status;
  1035. if (ans.CardPos == 2 || ans.CardPos == 5)
  1036. ctx->Ans.cardPos = CardPos_CardSwiper_IC_Insert;
  1037. else if (ans.CardPos == 4 || ans.CardPos == 6)
  1038. ctx->Ans.cardPos = CardPos_CardSwiper_RFIC;
  1039. else
  1040. ctx->Ans.cardPos = ans.CardPos;
  1041. ctx->Ans.t2Account = ans.t2Account;
  1042. ctx->Ans.t2Region = ans.t2Region;
  1043. ctx->Ans.t2CardSerial = ans.t2CardSerial;
  1044. ctx->Ans.t2CVC = ans.t2CVC;
  1045. ctx->Ans.t2ExpireDate = ans.t2ExpireDate;
  1046. Dbg("ICType:%d,%d,%s****%s", ctx->Ans.ICType, ans.ICType,(const char*)ctx->Ans.t2Account.SubString(0, 4), (const char*)ctx->Ans.t2Account.SubString(ctx->Ans.t2Account.GetLength() - 4, 4));
  1047. if (ans.ICType == 3 || ans.ICType == 4)
  1048. {
  1049. CardSwiperService_PreOnline_Req req;
  1050. CardSwiperService_PreOnline_Ans ans;
  1051. req.businessData = ctx->Req.businessData;
  1052. BroadcastReadICing(0);
  1053. eErr = m_pCardSwiper->PreOnline(req, ans, 50000);
  1054. if (eErr == Error_Succeed)
  1055. {
  1056. ctx->Ans.result = ans.result;
  1057. }
  1058. else
  1059. ctx->Ans.result = "";
  1060. }
  1061. }
  1062. if (eErr == Error_TimeOut)
  1063. {
  1064. Dbg("CardSwiper timeout.");
  1065. return 9;
  1066. }
  1067. else if (eErr == Error_InvalidState)
  1068. {
  1069. Dbg("CardSwiper in error state.");
  1070. return 1;
  1071. }
  1072. else
  1073. ctx->Answer(eErr);
  1074. }
  1075. else
  1076. {
  1077. Dbg("can't find the corresponding Entity.req.Module:%d", ctx->Req.module);
  1078. return 1;
  1079. //ctx->Answer(Error_Unexpect);
  1080. }
  1081. return 0;
  1082. }
  1083. void CCardReadAdapterFSM::CancelRead()
  1084. {
  1085. ErrorCodeEnum eErr = Error_Unexpect;
  1086. if (IsCardIssuerSessionOK())
  1087. {
  1088. Dbg("CardIssuer read canceled by front user.");
  1089. m_pCardIssuer->CancelInsert();
  1090. }
  1091. if (IsContactlessCardSessionOK())
  1092. {
  1093. Dbg("ContactlessCard read canceled by front user.");
  1094. m_pContactless->CancelInsert();
  1095. }
  1096. if (IsCardSwiperSessionOK())
  1097. {
  1098. eErr = m_pCardSwiper->CancelInsert();
  1099. Dbg("CardSwiper read canceled by front user.");
  1100. }
  1101. }
  1102. void CCardReadAdapterFSM::ReadWaitMore()
  1103. {
  1104. ErrorCodeEnum eErr = Error_Unexpect;
  1105. if (IsCardIssuerSessionOK())
  1106. {
  1107. m_pCardIssuer->InsertWaitMore();
  1108. }
  1109. if (IsContactlessCardSessionOK())
  1110. {
  1111. m_pContactless->InsertWaitMore();
  1112. }
  1113. if (IsCardSwiperSessionOK())
  1114. {
  1115. m_pCardSwiper->InsertWaitMore();
  1116. }
  1117. }
  1118. int CCardReadAdapterFSM::WriteTrack(SpReqAnsContext<CardReadAdapterService_WriteTrack_Req, CardReadAdapterService_WriteTrack_Ans>::Pointer ctx)
  1119. {
  1120. LOG_FUNCTION();
  1121. if (IsCardIssuerSessionOK())
  1122. {
  1123. CardIssuerService_WriteTrack_Req req;
  1124. CardIssuerService_WriteTrack_Ans ans;
  1125. req.co = ctx->Req.co;
  1126. req.mode = ctx->Req.mode;
  1127. req.track1 = ctx->Req.track1;
  1128. req.track2 = ctx->Req.track2;
  1129. req.track3 = ctx->Req.track3;
  1130. req.reserved = ctx->Req.reserved;
  1131. ErrorCodeEnum eErr = Error_Unexpect;
  1132. DWORD dwUsrErrCode = 0;
  1133. eErr = m_pCardIssuer->WriteTrack(req, ans, 20000, dwUsrErrCode);
  1134. if (eErr != Error_Succeed)
  1135. {
  1136. Dbg("WriteTrack failed.%d",eErr);
  1137. ctx->Answer(eErr, dwUsrErrCode);
  1138. }
  1139. else
  1140. {
  1141. ctx->Ans.result = ans.result;
  1142. ctx->Ans.reserved1 = ans.reserved1;
  1143. ctx->Ans.reserved2 = ans.reserved2;
  1144. Dbg("Write tracks ok.");
  1145. ctx->Answer(Error_Succeed);
  1146. }
  1147. }
  1148. return 0;
  1149. }
  1150. int CCardReadAdapterFSM::PreOnline(SpReqAnsContext<CardReadAdapterService_PreOnline_Req, CardReadAdapterService_PreOnline_Ans>::Pointer ctx)
  1151. {
  1152. LOG_FUNCTION();
  1153. ErrorCodeEnum eErr = Error_Unexpect;
  1154. DWORD dwUsrErrCode = 0;
  1155. if (IsTheEntity(ctx->Req.module, Module_CardIssuer) && IsCardIssuerSessionOK())
  1156. {
  1157. CardIssuerService_PreOnline_Req req;
  1158. CardIssuerService_PreOnline_Ans ans;
  1159. req.businessData = ctx->Req.businessData;
  1160. if (!ctx->Req.reserved2[0].IsNullOrEmpty())
  1161. req.reserved1 = ctx->Req.reserved2[0];
  1162. else
  1163. req.reserved1 = "";
  1164. if (!ctx->Req.reserved2[1].IsNullOrEmpty())
  1165. req.reserved2 = ctx->Req.reserved2[1];
  1166. else
  1167. req.reserved2 = "";
  1168. eErr = m_pCardIssuer->PreOnline(req, ans, 50000, dwUsrErrCode);
  1169. if (eErr == Error_Succeed)
  1170. {
  1171. ctx->Ans.result = ans.result;
  1172. }
  1173. ctx->Answer(eErr, dwUsrErrCode);
  1174. }
  1175. else if (IsTheEntity(ctx->Req.module, Module_ContactlessCard) && IsContactlessCardSessionOK() && m_busCtx.eCardFromWhich == Card_In_ContactlessCard)
  1176. {
  1177. ContactlessCardService_PreOnline_Req req;
  1178. ContactlessCardService_PreOnline_Ans ans;
  1179. req.businessData = ctx->Req.businessData;
  1180. //req.reserved1 = ctx->Req.reserved1;
  1181. //req.reserved2 = ctx->Req.reserved2;
  1182. eErr = m_pContactless->PreOnline(req, ans, 50000, dwUsrErrCode);
  1183. if (eErr == Error_Succeed)
  1184. {
  1185. ctx->Ans.result = ans.result;
  1186. }
  1187. ctx->Answer(eErr, dwUsrErrCode);
  1188. }
  1189. else if (IsTheEntity(ctx->Req.module, Module_CardSwiper) && IsCardSwiperSessionOK())
  1190. {
  1191. CardSwiperService_PreOnline_Req req;
  1192. CardSwiperService_PreOnline_Ans ans;
  1193. req.businessData = ctx->Req.businessData;
  1194. //req.reserved1 = ctx->Req.reserved1;
  1195. //req.reserved2 = ctx->Req.reserved2;
  1196. eErr = m_pCardSwiper->PreOnline(req, ans, 50000, dwUsrErrCode);
  1197. if (eErr == Error_Succeed)
  1198. {
  1199. ctx->Ans.result = ans.result;
  1200. }
  1201. ctx->Answer(eErr, dwUsrErrCode);
  1202. }
  1203. return 0;
  1204. }
  1205. int CCardReadAdapterFSM::PostOnline(SpReqAnsContext<CardReadAdapterService_PostOnline_Req, CardReadAdapterService_PostOnline_Ans>::Pointer ctx)
  1206. {
  1207. LOG_FUNCTION();
  1208. Dbg("ctx->Req.module:%d,cardIn(Iss 1,RF 2):%d",ctx->Req.module, m_busCtx.eCardFromWhich);
  1209. ErrorCodeEnum eErr = Error_Unexpect;
  1210. DWORD dwUsrErrCode = 0;
  1211. if (IsTheEntity(ctx->Req.module, Module_CardIssuer) && IsCardIssuerSessionOK() && m_busCtx.eCardFromWhich == Card_In_CardIssuer)
  1212. {
  1213. CardIssuerService_PostOnline_Req req;
  1214. CardIssuerService_PostOnline_Ans ans;
  1215. req.data = ctx->Req.data;
  1216. eErr = m_pCardIssuer->PostOnline(req, ans, 50000, dwUsrErrCode);
  1217. if (eErr == Error_Succeed)
  1218. {
  1219. ctx->Ans.result = ans.result;
  1220. }
  1221. ctx->Answer(eErr, dwUsrErrCode);
  1222. }
  1223. else if (IsTheEntity(ctx->Req.module, Module_ContactlessCard) && IsContactlessCardSessionOK() && m_busCtx.eCardFromWhich == Card_In_ContactlessCard)
  1224. {
  1225. ContactlessCardService_PostOnline_Req req;
  1226. ContactlessCardService_PostOnline_Ans ans;
  1227. req.data = ctx->Req.data;
  1228. eErr = m_pContactless->PostOnline(req, ans, 50000, dwUsrErrCode);
  1229. if (eErr == Error_Succeed)
  1230. {
  1231. ctx->Ans.result = ans.result;
  1232. }
  1233. ctx->Answer(eErr, dwUsrErrCode);
  1234. }
  1235. else if (IsTheEntity(ctx->Req.module, Module_CardSwiper) && IsCardSwiperSessionOK())
  1236. {
  1237. CardSwiperService_PostOnline_Req req;
  1238. CardSwiperService_PostOnline_Ans ans;
  1239. req.data = ctx->Req.data;
  1240. eErr = m_pCardSwiper->PostOnline(req, ans, 50000, dwUsrErrCode);
  1241. if (eErr == Error_Succeed)
  1242. {
  1243. ctx->Ans.result = ans.result;
  1244. }
  1245. ctx->Answer(eErr, dwUsrErrCode);
  1246. }
  1247. return 0;
  1248. }
  1249. int CCardReadAdapterFSM::EjectCard(SpReqAnsContext<CardReadAdapterService_Eject_Req, CardReadAdapterService_Eject_Ans>::Pointer ctx)
  1250. {
  1251. LOG_FUNCTION();
  1252. ErrorCodeEnum eErr = Error_Unexpect;
  1253. Dbg("before eject card,req.module %d,card in %d", ctx->Req.module,m_busCtx.eCardFromWhich);
  1254. DWORD dwUsrErrCode = 0;
  1255. if (IsTheEntity(ctx->Req.module, Module_CardIssuer) && IsCardIssuerSessionOK() && m_busCtx.eCardFromWhich == Card_In_CardIssuer)
  1256. {
  1257. CardIssuerService_Eject_Req req;
  1258. CardIssuerService_Eject_Ans ans;
  1259. eErr = m_pCardIssuer->Eject(req, ans, 50000, dwUsrErrCode);
  1260. if (eErr == Error_Succeed)
  1261. {
  1262. m_busCtx.eCardFromWhich = Card_In_No_Where;
  1263. Dbg("CardIssuer EjectCard suc.");
  1264. }
  1265. ctx->Answer(eErr, dwUsrErrCode);
  1266. }
  1267. else if (IsTheEntity(ctx->Req.module, Module_ContactlessCard) && IsContactlessCardSessionOK() && m_busCtx.eCardFromWhich == Card_In_ContactlessCard)
  1268. {
  1269. ContactlessCardService_Eject_Req req;
  1270. ContactlessCardService_Eject_Ans ans;
  1271. eErr = m_pContactless->Eject(req, ans, 50000, dwUsrErrCode);
  1272. if (eErr == Error_Succeed)
  1273. {
  1274. m_busCtx.eCardFromWhich = Card_In_No_Where;
  1275. Dbg("ContactlessCard EjectCard suc.");
  1276. }
  1277. ctx->Answer(eErr, dwUsrErrCode);
  1278. }
  1279. else if (IsTheEntity(ctx->Req.module, Module_CardSwiper) && IsCardSwiperSessionOK())
  1280. {
  1281. CardSwiperService_Eject_Req req;
  1282. CardSwiperService_Eject_Ans ans;
  1283. eErr = m_pCardSwiper->Eject(req, ans, 50000, dwUsrErrCode);
  1284. if (eErr == Error_Succeed)
  1285. {
  1286. m_busCtx.eCardFromWhich = Card_In_No_Where;
  1287. Dbg("CardSwiper EjectCard suc.");
  1288. }
  1289. ctx->Answer(eErr, dwUsrErrCode);
  1290. }
  1291. else
  1292. {
  1293. Dbg("nothing to do.");
  1294. ctx->Answer(Error_NotExist);
  1295. }
  1296. return 0;
  1297. }
  1298. int CCardReadAdapterFSM::CaptureCard(SpReqAnsContext<CardReadAdapterService_Capture_Req, CardReadAdapterService_Capture_Ans>::Pointer ctx)
  1299. {
  1300. LOG_FUNCTION();
  1301. ErrorCodeEnum eErr = Error_Unexpect;
  1302. DWORD dwUsrErrCode = 0;
  1303. if (IsTheEntity(ctx->Req.module, Module_CardIssuer) && IsCardIssuerSessionOK() && m_busCtx.eCardFromWhich == Card_In_CardIssuer)
  1304. {
  1305. CardIssuerService_Capture_Req req;
  1306. CardIssuerService_Capture_Ans ans;
  1307. eErr = m_pCardIssuer->Capture(req, ans, 15000, dwUsrErrCode);
  1308. if (eErr == Error_Succeed)
  1309. {
  1310. Dbg("Capture card suc.");
  1311. m_busCtx.eCardFromWhich = Card_In_No_Where;
  1312. }
  1313. ctx->Answer(eErr, dwUsrErrCode);
  1314. }
  1315. else
  1316. {
  1317. Dbg("Something wrong.the module(%d),cardinwhere(%d)", ctx->Req.module, m_busCtx.eCardFromWhich);
  1318. ctx->Answer(eErr);
  1319. }
  1320. return 0;
  1321. }
  1322. int CCardReadAdapterFSM::QueryCardInfo(SpReqAnsContext<CardReadAdapterService_QueryCardInfo_Req, CardReadAdapterService_QueryCardInfo_Ans>::Pointer ctx)
  1323. {
  1324. ErrorCodeEnum eErr = Error_Unexpect;
  1325. DWORD dwUsrErrCode = 0;
  1326. ctx->Ans.CardIssuerPos = ctx->Ans.ContactlessCardPos = ctx->Ans.CardSwiperPos = 0;
  1327. if (IsTheEntity(ctx->Req.module, Module_CardIssuer) && IsCardIssuerSessionOK() && m_busCtx.eCardFromWhich == Card_In_CardIssuer)
  1328. {
  1329. CardIssuerService_QueryCardInfo_Req req;
  1330. CardIssuerService_QueryCardInfo_Ans ans;
  1331. eErr = m_pCardIssuer->QueryCardInfo(req, ans, 10000, dwUsrErrCode);
  1332. if (eErr == Error_Succeed)
  1333. {
  1334. switch (ans.position)
  1335. {
  1336. case 1:
  1337. ctx->Ans.CardIssuerPos = -1;
  1338. break;
  1339. case 2:
  1340. ctx->Ans.CardIssuerPos = 1;
  1341. break;
  1342. default:
  1343. break;
  1344. }
  1345. }
  1346. }
  1347. if (IsTheEntity(ctx->Req.module, Module_ContactlessCard) && IsContactlessCardSessionOK() && m_busCtx.eCardFromWhich == Card_In_ContactlessCard)
  1348. {
  1349. ContactlessCardService_QueryCardInfo_Req req;
  1350. ContactlessCardService_QueryCardInfo_Ans ans;
  1351. eErr = m_pContactless->QueryCardInfo(req, ans, 10000, dwUsrErrCode);
  1352. if (eErr == Error_Succeed)
  1353. {
  1354. switch (ans.position)//oiltmp to add
  1355. {
  1356. case 2:
  1357. break;
  1358. case 3:
  1359. break;
  1360. case 4:
  1361. ctx->Ans.ContactlessCardPos = 1;
  1362. break;
  1363. default:
  1364. break;
  1365. }
  1366. }
  1367. else
  1368. ctx->Ans.ContactlessCardPos = -1;
  1369. }
  1370. if (IsTheEntity(ctx->Req.module, Module_CardSwiper) && IsCardSwiperSessionOK())
  1371. {
  1372. CardSwiperService_QueryCardInfo_Req req;
  1373. CardSwiperService_QueryCardInfo_Ans ans;
  1374. eErr = m_pCardSwiper->QueryCardInfo(req, ans, 10000, dwUsrErrCode);
  1375. if (eErr == Error_Succeed)
  1376. {
  1377. switch (ans.position)
  1378. {
  1379. case 2:
  1380. ctx->Ans.CardSwiperPos = 1;
  1381. break;
  1382. case 3:
  1383. ctx->Ans.CardSwiperPos = 2;
  1384. break;
  1385. case 4:
  1386. ctx->Ans.CardSwiperPos = 3;
  1387. break;
  1388. case 6:
  1389. ctx->Ans.CardSwiperPos = 4;
  1390. break;
  1391. default:
  1392. break;
  1393. }
  1394. }
  1395. else
  1396. ctx->Ans.CardSwiperPos = -1;
  1397. }
  1398. ctx->Answer(Error_Succeed);
  1399. Dbg("%d,position(CI,CC,CS) %d,%d,%d", m_busCtx.eCardFromWhich, ctx->Ans.CardIssuerPos, ctx->Ans.ContactlessCardPos, ctx->Ans.CardSwiperPos);
  1400. return 0;
  1401. }
  1402. void CCardReadAdapterFSM::CheckAndReconnectSession()
  1403. {
  1404. ErrorCodeEnum eErr = Error_Unexpect;
  1405. int cardIssuerCount = 0, contactlessCardCount = 0, cardSwiperCount = 0;
  1406. while (1)
  1407. {
  1408. //the first time to connect or need to reconnect
  1409. //CardIssuer
  1410. if (!m_bRVCIL && (m_pCardIssuer == NULL || (m_pCardIssuer != NULL && m_pCardIssuer->QuerySessionClosed())))
  1411. {
  1412. if (m_pCardIssuer != NULL)
  1413. {
  1414. m_pCardIssuer->GetFunction()->CloseSession();
  1415. m_pCardIssuer = NULL;
  1416. }
  1417. m_pCardIssuer = new CardIssuerService_ClientBase(GetEntityBase());
  1418. if (m_pCardIssuer != NULL)
  1419. {
  1420. eErr = m_pCardIssuer->Connect();
  1421. if (eErr != Error_Succeed)
  1422. {
  1423. if (cardIssuerCount%120 == 0)//about 10 minutes can't connect to entity
  1424. Dbg("connect to entity CardIssuer failed.%d", eErr);
  1425. cardIssuerCount++;
  1426. m_pCardIssuer->SafeDelete();
  1427. m_pCardIssuer = NULL;
  1428. }
  1429. else
  1430. {
  1431. cardIssuerCount = 0;
  1432. Dbg("Connect to entity CardIssuer suc.");
  1433. }
  1434. }
  1435. }
  1436. //ContactlessCard
  1437. if (!m_bRVCIL && (m_eMachineType != MachineType_RVC_PAD && m_eMachineType != MachineType_RVC_Wall && m_eMachineType != MachineType_RPM_Stand1S)
  1438. && (m_pContactless == NULL || (m_pContactless != NULL && m_pContactless->QuerySessionClosed())))
  1439. {
  1440. if (m_pContactless != NULL)
  1441. {
  1442. m_pContactless->GetFunction()->CloseSession();
  1443. m_pContactless = NULL;
  1444. }
  1445. m_pContactless = new ContactlessCardService_ClientBase(GetEntityBase());
  1446. if (m_pContactless != NULL)
  1447. {
  1448. eErr = m_pContactless->Connect();
  1449. if (eErr != Error_Succeed)
  1450. {
  1451. if (contactlessCardCount % 120 == 0)//about 10 minutes can't connect to entity
  1452. Dbg("connect to entity ContactlessCard failed.%d", eErr);
  1453. contactlessCardCount++;
  1454. m_pContactless->SafeDelete();
  1455. m_pContactless = NULL;
  1456. }
  1457. else
  1458. {
  1459. contactlessCardCount = 0;
  1460. Dbg("Connect to entity ContactlessCard suc.");
  1461. }
  1462. }
  1463. }
  1464. //CardSwiper
  1465. if (!m_bRVCIL && m_eMachineType != MachineType_RVC_Stand2S &&(m_pCardSwiper == NULL || (m_pCardSwiper != NULL && m_pCardSwiper->QuerySessionClosed())))
  1466. {
  1467. if (m_pCardSwiper != NULL)
  1468. {
  1469. m_pCardSwiper->GetFunction()->CloseSession();
  1470. m_pCardSwiper = NULL;
  1471. }
  1472. m_pCardSwiper = new CardSwiperService_ClientBase(GetEntityBase());
  1473. if (m_pCardSwiper != NULL)
  1474. {
  1475. eErr = m_pCardSwiper->Connect();
  1476. if (eErr != Error_Succeed)
  1477. {
  1478. if (cardSwiperCount%120 == 0)//about 10 minutes can't connect to entity
  1479. Dbg("connect to entity CardSwiper failed.%d", eErr);
  1480. cardSwiperCount++;
  1481. m_pCardSwiper->SafeDelete();
  1482. m_pCardSwiper = NULL;
  1483. }
  1484. else
  1485. {
  1486. cardSwiperCount = 0;
  1487. Dbg("Connect to entity CardSwiper suc.");
  1488. //CUUID uuidCSMsg;
  1489. //GetEntityBase()->GetFunction()->SubscribeBroadcast("CardSwiper", "", (IBroadcastListener*)m_pEty, uuidCSMsg);
  1490. }
  1491. }
  1492. }
  1493. Sleep(5000);
  1494. }
  1495. }
  1496. bool CCardReadAdapterFSM::IsCardIssuerSessionOK()
  1497. {
  1498. if (m_pCardIssuer != NULL && !m_pCardIssuer->QuerySessionClosed())
  1499. return true;
  1500. return false;
  1501. }
  1502. bool CCardReadAdapterFSM::IsContactlessCardSessionOK()
  1503. {
  1504. if (m_pContactless != NULL && !m_pContactless->QuerySessionClosed())
  1505. return true;
  1506. return false;
  1507. }
  1508. bool CCardReadAdapterFSM::IsCardSwiperSessionOK()
  1509. {
  1510. if (m_pCardSwiper != NULL && !m_pCardSwiper->QuerySessionClosed())
  1511. return true;
  1512. return false;
  1513. }
  1514. bool CCardReadAdapterFSM::IsTheEntity(int module,ModuleType eModule)
  1515. {
  1516. if (module == 0)
  1517. return true;
  1518. //oilyang@20180409 add req.module==99
  1519. else if (module == 99)
  1520. {
  1521. Dbg("req.module is 99,machineType is:%s",(const char*)m_csMachineType);
  1522. //oilyang@20200730 according to meeting with Ratail/Test/Hangzhou
  1523. //Desk2S 2.1 can insert into CardIssuer
  1524. //oilyang@20200612 if "RVC.Desk2S" ,no need to call CardIssuer(insert card)
  1525. //from guodan 20180905
  1526. //99:进卡读卡,功能集向除蓝牙发卡机之外的读卡实体发出进卡读卡指令。
  1527. if (eModule == Module_CardIssuer)
  1528. {
  1529. if (m_eMachineType == MachineType_RVC_Stand2S
  1530. || m_eMachineType == MachineType_RVC_CardStore
  1531. || m_eMachineType == MachineType_RVC_CardPrinter
  1532. || m_eMachineType == MachineType_RVC_DESK1S
  1533. || m_eMachineType == MachineType_RVC_DESK2S && m_majorVerion == 2 && m_minorVerion == 1)
  1534. return true;
  1535. else
  1536. return false;
  1537. }
  1538. else
  1539. return true;
  1540. }
  1541. else if ((module&eModule) == eModule)
  1542. return true;
  1543. else
  1544. return false;
  1545. }
  1546. int CCardReadAdapterFSM::SetSomeFlag(SpReqAnsContext<CardReadAdapterService_SetSomeFlag_Req, CardReadAdapterService_SetSomeFlag_Ans>::Pointer ctx)
  1547. {
  1548. LOG_FUNCTION();
  1549. if (IsCardIssuerSessionOK())
  1550. {
  1551. CardIssuerService_SetSomeFlag_Req req;
  1552. CardIssuerService_SetSomeFlag_Ans ans;
  1553. req.IssueBusiness = ctx->Req.IssueBusiness;
  1554. req.reserved1.Init(ctx->Req.reserved1.GetCount());
  1555. for (int i = 0; i < ctx->Req.reserved1.GetCount(); ++i)
  1556. {
  1557. req.reserved1[i] = ctx->Req.reserved1[i];
  1558. }
  1559. ErrorCodeEnum eErr = m_pCardIssuer->SetSomeFlag(req, ans,10000);
  1560. Dbg("CardIssuer SetSomeFlag ret:%d",eErr);
  1561. if (eErr == Error_Succeed)
  1562. {
  1563. ctx->Ans.reserved1.Copy(ans.reserved1);
  1564. ctx->Ans.reserved2.Copy(ans.reserved2);
  1565. ctx->Answer(Error_Succeed);
  1566. }
  1567. else
  1568. ctx->Answer(Error_Unexpect);
  1569. }
  1570. else
  1571. {
  1572. Dbg("SetSomeFlag can only apply to CardIssuer.Or CardIssuer connected failed.");
  1573. ctx->Answer(Error_Unexpect);
  1574. }
  1575. return 0;
  1576. }
  1577. int CCardReadAdapterFSM::GetDevInfo(SpReqAnsContext<CardReadAdapterService_GetDevInfo_Req, CardReadAdapterService_GetDevInfo_Ans>::Pointer ctx)
  1578. {
  1579. LOG_FUNCTION();
  1580. CSimpleStringA csCIType(""), csCIModel(""), csCIVersion(""), csCCType(""), csCCModel(""), csCCVersion(""), csCSType(""), csCSModel(""), csCSVersion("");
  1581. int ciState, ccState, csState;
  1582. bool bCI(false), bCC(false), bCS(false);
  1583. ErrorCodeEnum eErr = Error_Unexpect;
  1584. int count = 0;
  1585. if (IsTheEntity(ctx->Req.module, Module_CardIssuer) && IsCardIssuerSessionOK())
  1586. {
  1587. CardIssuerService_GetDevInfo_Req req;
  1588. CardIssuerService_GetDevInfo_Ans ans;
  1589. eErr = m_pCardIssuer->GetDevInfo(req, ans, 10000);
  1590. if (eErr == Error_Succeed)
  1591. {
  1592. bCI = true;
  1593. count++;
  1594. csCIType = ans.type;
  1595. csCIModel = ans.model;
  1596. csCIVersion = ans.version;
  1597. ciState = ans.state;
  1598. }
  1599. }
  1600. if (IsTheEntity(ctx->Req.module, Module_ContactlessCard) && IsContactlessCardSessionOK())
  1601. {
  1602. ContactlessCardService_GetDevInfo_Req req;
  1603. ContactlessCardService_GetDevInfo_Ans ans;
  1604. eErr = m_pContactless->GetDevInfo(req, ans, 10000);
  1605. if (eErr == Error_Succeed)
  1606. {
  1607. bCC = true;
  1608. count++;
  1609. csCCType = ans.type;
  1610. csCCModel = ans.model;
  1611. csCCVersion = ans.version;
  1612. ccState = ans.state;
  1613. }
  1614. }
  1615. if (IsTheEntity(ctx->Req.module, Module_CardSwiper) && IsCardSwiperSessionOK())
  1616. {
  1617. CardSwiperService_GetDevInfo_Req req;
  1618. CardSwiperService_GetDevInfo_Ans ans;
  1619. eErr = m_pCardSwiper->GetDevInfo(req, ans, 10000);
  1620. if (eErr == Error_Succeed)
  1621. {
  1622. bCS = true;
  1623. count++;
  1624. csCSType = ans.type;
  1625. csCSModel = ans.model;
  1626. csCSVersion = ans.version;
  1627. csState = ans.state;
  1628. }
  1629. }
  1630. ctx->Ans.ModuleNum = count;
  1631. ctx->Ans.ModuleSN.Init(count);
  1632. ctx->Ans.type.Init(count);
  1633. ctx->Ans.model.Init(count);
  1634. ctx->Ans.version.Init(count);
  1635. ctx->Ans.state.Init(count);
  1636. int index = 0;
  1637. Dbg("count:%d,index:%d,%d,%d,%d",count,index,bCI,bCC,bCS);
  1638. if (bCI)
  1639. {
  1640. ctx->Ans.ModuleSN[index] = Module_CardIssuer;
  1641. ctx->Ans.type[index] = csCIType;
  1642. ctx->Ans.model[index] = csCIModel;
  1643. ctx->Ans.version[index] = csCIVersion;
  1644. ctx->Ans.state[index] = ciState;
  1645. Dbg("CardIssuer,type:%s,model:%s,version:%s,state:%d", (const char*)csCIType, (const char*)csCIModel
  1646. , (const char*)csCIVersion, ciState);
  1647. index++;
  1648. }
  1649. if (bCC)
  1650. {
  1651. ctx->Ans.ModuleSN[index] = Module_ContactlessCard;
  1652. ctx->Ans.type[index] = csCCType;
  1653. ctx->Ans.model[index] = csCCModel;
  1654. ctx->Ans.version[index] = csCCVersion;
  1655. ctx->Ans.state[index] = ccState;
  1656. Dbg("ContactlessCard,type:%s,model:%s,version:%s,state:%d", (const char*)csCCType, (const char*)csCCModel
  1657. , (const char*)csCCVersion, ccState);
  1658. index++;
  1659. }
  1660. if (bCS)
  1661. {
  1662. ctx->Ans.ModuleSN[index] = Module_CardSwiper;
  1663. ctx->Ans.type[index] = csCSType;
  1664. ctx->Ans.model[index] = csCSModel;
  1665. ctx->Ans.version[index] = csCSVersion;
  1666. ctx->Ans.state[index] = csState;
  1667. Dbg("CardSwiper,type:%s,model:%s,version:%s,state:%d", (const char*)csCSType, (const char*)csCSModel
  1668. , (const char*)csCSVersion, csState);
  1669. index++;
  1670. }
  1671. ctx->Answer(Error_Succeed);
  1672. return 0;
  1673. }
  1674. int CCardReadAdapterFSM::GetMaterialCount(SpReqAnsContext<CardReadAdapterService_GetMaterialCount_Req, CardReadAdapterService_GetMaterialCount_Ans>::Pointer ctx)
  1675. {
  1676. LOG_FUNCTION();
  1677. DWORD dwUsrErrCode = 0;
  1678. if (IsCardIssuerSessionOK())
  1679. {
  1680. CardIssuerService_GetMaterialCountEx_Req req;
  1681. CardIssuerService_GetMaterialCountEx_Ans ans;
  1682. req.GetHopper.Copy(ctx->Req.GetHopper);
  1683. req.reserved1.Copy(ctx->Req.reserved1);
  1684. req.reserved2.Copy(ctx->Req.reserved2);
  1685. ErrorCodeEnum eErr = m_pCardIssuer->GetMaterialCountEx(req, ans, 10000, dwUsrErrCode);
  1686. if (eErr == Error_Succeed)
  1687. {
  1688. ctx->Ans.captured = ans.captured;
  1689. ctx->Ans.hasHopper.Copy(ans.hasHopper);
  1690. ctx->Ans.CardBoxNo.Copy(ans.CardBoxNo);
  1691. ctx->Ans.PsbCode.Copy(ans.PsbCode);
  1692. ctx->Ans.PsbName.Copy(ans.PsbName);
  1693. ctx->Ans.Maintainer.Copy(ans.Maintainer);
  1694. ctx->Ans.CardInit.Copy(ans.CardInit);
  1695. ctx->Ans.CardPercent.Copy(ans.CardPercent);
  1696. ctx->Ans.remains.Copy(ans.remains);
  1697. ctx->Ans.issued.Copy(ans.issued);
  1698. ctx->Ans.mixed.Copy(ans.mixed);
  1699. ctx->Ans.MaintainTime.Copy(ans.MaintainTime);
  1700. ctx->Ans.reserved1.Copy(ans.reserved1);
  1701. ctx->Ans.reserved2.Copy(ans.reserved2);
  1702. ctx->Answer(Error_Succeed);
  1703. }
  1704. else
  1705. ctx->Answer(Error_Unexpect, dwUsrErrCode);
  1706. }
  1707. else
  1708. {
  1709. Dbg("GetMaterialCountEx can only apply to CardIssuer.Or CardIssuer connected failed.");
  1710. ctx->Answer(Error_Unexpect);
  1711. }
  1712. return 0;
  1713. }
  1714. int CCardReadAdapterFSM::SetMaterialCount(SpReqAnsContext<CardReadAdapterService_SetMaterialCount_Req, CardReadAdapterService_SetMaterialCount_Ans>::Pointer ctx)
  1715. {
  1716. LOG_FUNCTION();
  1717. DWORD dwUsrErrCode = 0;
  1718. if (IsCardIssuerSessionOK())
  1719. {
  1720. CardIssuerService_SetMaterialCountEx_Req req;
  1721. CardIssuerService_SetMaterialCountEx_Ans ans;
  1722. req.SetCaptured = ctx->Req.SetCaptured;
  1723. req.captured = ctx->Req.captured;
  1724. req.SetHopper.Copy(ctx->Req.SetHopper);
  1725. req.CardBoxNo.Copy(ctx->Req.CardBoxNo);
  1726. req.PsbCode.Copy(ctx->Req.PsbCode);
  1727. req.PsbName.Copy(ctx->Req.PsbName);
  1728. req.Maintainer.Copy(ctx->Req.Maintainer);
  1729. req.MaintainTime.Copy(ctx->Req.MaintainTime);
  1730. req.CardInit.Copy(ctx->Req.CardInit);
  1731. req.CardPercent.Copy(ctx->Req.CardPercent);
  1732. req.remains.Copy(ctx->Req.remains);
  1733. req.issued.Copy(ctx->Req.issued);
  1734. req.mixed.Copy(ctx->Req.mixed);
  1735. req.reserved1.Copy(ctx->Req.reserved1);
  1736. req.reserved2.Copy(ctx->Req.reserved2);
  1737. ErrorCodeEnum eErr = m_pCardIssuer->SetMaterialCountEx(req, ans, 10000, dwUsrErrCode);
  1738. if (eErr == Error_Succeed)
  1739. {
  1740. ctx->Ans.reserved1.Copy(ans.reserved1);
  1741. ctx->Ans.reserved2.Copy(ans.reserved2);
  1742. ctx->Answer(Error_Succeed);
  1743. }
  1744. else
  1745. ctx->Answer(Error_Unexpect, dwUsrErrCode);
  1746. }
  1747. else
  1748. {
  1749. Dbg("SetMaterialCountEx can only apply to CardIssuer.Or CardIssuer connected failed.");
  1750. ctx->Answer(Error_Unexpect);
  1751. }
  1752. return 0;
  1753. }
  1754. int CCardReadAdapterFSM::GetSCIInfo(SpReqAnsContext<CardReadAdapterService_GetSCIInfo_Req, CardReadAdapterService_GetSCIInfo_Ans>::Pointer ctx)
  1755. {
  1756. if (IsCardIssuerSessionOK())
  1757. {
  1758. ErrorCodeEnum eErr = Error_Unexpect;
  1759. CardIssuerService_GetSCIInfo_Req req;
  1760. CardIssuerService_GetSCIInfo_Ans ans;
  1761. eErr = m_pCardIssuer->GetSCIInfo(req, ans, 15000);
  1762. if (eErr == Error_Succeed)
  1763. {
  1764. ctx->Ans.connected = ans.connected;
  1765. ctx->Ans.DevSN = ans.DevSN;
  1766. ctx->Ans.reserved1.Copy(ans.reserved1);
  1767. ctx->Ans.reserved2.Copy(ans.reserved2);
  1768. }
  1769. ctx->Answer(eErr);
  1770. }
  1771. else
  1772. {
  1773. Dbg("Entity CardIssuer is not ok.");
  1774. ctx->Answer(Error_Unexpect);
  1775. }
  1776. return 0;
  1777. }
  1778. int CCardReadAdapterFSM::OpenSafeLock(SpReqAnsContext<CardReadAdapterService_OpenSafeLock_Req, CardReadAdapterService_OpenSafeLock_Ans>::Pointer ctx)
  1779. {
  1780. LOG_FUNCTION();
  1781. if (IsCardIssuerSessionOK())
  1782. {
  1783. ErrorCodeEnum eErr = Error_Unexpect;
  1784. CardIssuerService_OpenSafeLock_Req req;
  1785. CardIssuerService_OpenSafeLock_Ans ans;
  1786. req.reserved1.Copy(ctx->Req.reserved1);
  1787. req.reserved2.Copy(ctx->Req.reserved2);
  1788. eErr = m_pCardIssuer->OpenSafeLock(req, ans, 15000);
  1789. if (eErr == Error_Succeed)
  1790. {
  1791. ctx->Ans.reserved1.Copy(ans.reserved1);
  1792. ctx->Ans.reserved2.Copy(ans.reserved2);
  1793. }
  1794. ctx->Answer(eErr);
  1795. }
  1796. else
  1797. {
  1798. Dbg("Entity CardIssuer is not ok.");
  1799. ctx->Answer(Error_Unexpect);
  1800. }
  1801. return 0;
  1802. }
  1803. int CCardReadAdapterFSM::MagTransferInit(SpReqAnsContext<CardReadAdapterService_MagTransferInit_Req, CardReadAdapterService_MagTransferInit_Ans>::Pointer ctx)
  1804. {
  1805. LOG_FUNCTION();
  1806. if (IsCardSwiperSessionOK())
  1807. {
  1808. ErrorCodeEnum eErr = Error_Unexpect;
  1809. CardSwiperService_MagTransferInit_Req req;
  1810. CardSwiperService_MagTransferInit_Ans ans;
  1811. req.data = ctx->Req.data;
  1812. eErr = m_pCardSwiper->MagTransferInit(req, ans, 15000);
  1813. if (eErr == Error_Succeed)
  1814. {
  1815. ctx->Ans.ex = ans.ex;
  1816. ctx->Ans.result = ans.result;
  1817. }
  1818. ctx->Answer(eErr);
  1819. }
  1820. else
  1821. {
  1822. Dbg("Entity CardSwiper is not ok.");
  1823. ctx->Answer(Error_Unexpect);
  1824. }
  1825. return 0;
  1826. }
  1827. int CCardReadAdapterFSM::QueryConnInfo(SpReqAnsContext<CardReadAdapterService_QueryConnInfo_Req, CardReadAdapterService_QueryConnInfo_Ans>::Pointer ctx)
  1828. {
  1829. if (IsCardSwiperSessionOK())
  1830. {
  1831. ErrorCodeEnum eErr = Error_Unexpect;
  1832. CardSwiperService_QueryConnInfo_Req req;
  1833. CardSwiperService_QueryConnInfo_Ans ans;
  1834. eErr = m_pCardSwiper->QueryConnInfo(req, ans, 15000);
  1835. if (eErr == Error_Succeed)
  1836. {
  1837. ctx->Ans.connect = ans.connect;
  1838. ctx->Ans.reserved1 = ans.reserved1;
  1839. ctx->Ans.reserved2 = ans.reserved2;
  1840. }
  1841. ctx->Answer(eErr);
  1842. }
  1843. else
  1844. {
  1845. Dbg("Entity CardSwiper is not ok.");
  1846. ctx->Answer(Error_Unexpect);
  1847. }
  1848. return 0;
  1849. }
  1850. void CCardReadAdapterFSM::DoExit()
  1851. {
  1852. ErrorCodeEnum eErr = Error_Unexpect;
  1853. if (IsCardIssuerSessionOK())
  1854. {
  1855. eErr = m_pCardIssuer->Exit();
  1856. }
  1857. if (IsContactlessCardSessionOK())
  1858. {
  1859. eErr = m_pContactless->Exit();
  1860. }
  1861. if (IsCardSwiperSessionOK())
  1862. {
  1863. eErr = m_pCardSwiper->Exit();
  1864. }
  1865. }
  1866. void CCardReadAdapterFSM::BroadcastReadICing(int status)
  1867. {
  1868. ReadICing evt;
  1869. evt.status = status;
  1870. SpSendBroadcast(GetEntityBase()->GetFunction(), SP_MSG_OF(ReadICing), SP_MSG_SIG_OF(ReadICing), evt);
  1871. }
  1872. int CCardReadAdapterFSM::SAMICCommand(SpReqAnsContext<CardReadAdapterService_SAMICCommand_Req, CardReadAdapterService_SAMICCommand_Ans>::Pointer ctx)
  1873. {
  1874. if (IsTheEntity(ctx->Req.module, Module_CardIssuer) && IsCardIssuerSessionOK())
  1875. {
  1876. ErrorCodeEnum eErr = Error_Unexpect;
  1877. CardIssuerService_SAMICCommand_Req req;
  1878. CardIssuerService_SAMICCommand_Ans ans;
  1879. req.cmdType = ctx->Req.cmdType;
  1880. req.param1.Copy(ctx->Req.param1);
  1881. req.param2.Copy(ctx->Req.param2);
  1882. req.reserved1.Copy(ctx->Req.reserved1);
  1883. req.reserved2.Copy(ctx->Req.reserved2);
  1884. eErr = m_pCardIssuer->SAMICCommand(req, ans, 50000);
  1885. if (eErr == Error_Succeed)
  1886. {
  1887. ctx->Ans.ret1.Copy(ans.ret1);
  1888. ctx->Ans.ret2.Copy(ans.ret2);
  1889. ctx->Ans.reserved1.Copy(ans.reserved1);
  1890. ctx->Ans.reserved2.Copy(ans.reserved2);
  1891. }
  1892. ctx->Answer(eErr);
  1893. return 0;
  1894. }
  1895. else
  1896. {
  1897. Dbg("SAMICCommand param error or connection is failed:(module)%d", ctx->Req.module);
  1898. ctx->Answer(Error_Param);
  1899. return 1;
  1900. }
  1901. }
  1902. int CCardReadAdapterFSM::QueryPrinterStatus(SpReqAnsContext<CardReadAdapterService_QueryPrinterStatus_Req, CardReadAdapterService_QueryPrinterStatus_Ans>::Pointer ctx)
  1903. {
  1904. LOG_FUNCTION();
  1905. if (IsCardIssuerSessionOK())
  1906. {
  1907. ErrorCodeEnum eErr = Error_Unexpect;
  1908. CardIssuerService_QueryPrinterStatus_Req req;
  1909. CardIssuerService_QueryPrinterStatus_Ans ans;
  1910. req.param1.Copy(ctx->Req.param1);
  1911. req.param2.Copy(ctx->Req.param2);
  1912. req.reserved1.Copy(ctx->Req.reserved1);
  1913. req.reserved2.Copy(ctx->Req.reserved2);
  1914. eErr = m_pCardIssuer->QueryPrinterStatus(req, ans, 20000);
  1915. if (eErr == Error_Succeed)
  1916. {
  1917. ctx->Ans.ret1.Copy(ans.ret1);
  1918. ctx->Ans.ret2.Copy(ans.ret2);
  1919. ctx->Ans.reserved1.Copy(ans.reserved1);
  1920. ctx->Ans.reserved2.Copy(ans.reserved2);
  1921. }
  1922. ctx->Answer(eErr);
  1923. return 0;
  1924. }
  1925. else
  1926. {
  1927. Dbg("CardIssuer session is not ok.");
  1928. ctx->Answer(Error_Param);
  1929. return 1;
  1930. }
  1931. }
  1932. int CCardReadAdapterFSM::Print(SpReqAnsContext<CardReadAdapterService_Print_Req, CardReadAdapterService_Print_Ans>::Pointer ctx)
  1933. {
  1934. LOG_FUNCTION();
  1935. if (IsCardIssuerSessionOK())
  1936. {
  1937. ErrorCodeEnum eErr = Error_Unexpect;
  1938. CardIssuerService_Print_Req req;
  1939. CardIssuerService_Print_Ans ans;
  1940. req.data1 = ctx->Req.data1;
  1941. req.data2 = ctx->Req.data2;
  1942. req.data3 = ctx->Req.data3;
  1943. req.data4 = ctx->Req.data4;
  1944. req.data5 = ctx->Req.data5;
  1945. req.data6 = ctx->Req.data6;
  1946. req.reserved1.Copy(ctx->Req.reserved1);
  1947. req.reserved2.Copy(ctx->Req.reserved2);
  1948. eErr = m_pCardIssuer->Print(req, ans, 50000);
  1949. if (eErr == Error_Succeed)
  1950. {
  1951. ctx->Ans.reserved1.Copy(ans.reserved1);
  1952. ctx->Ans.reserved2.Copy(ans.reserved2);
  1953. }
  1954. ctx->Answer(eErr);
  1955. return 0;
  1956. }
  1957. else
  1958. {
  1959. Dbg("CardIssuer session is not ok.");
  1960. ctx->Answer(Error_Param);
  1961. return 1;
  1962. }
  1963. }
  1964. int CCardReadAdapterFSM::QuerySCIList(SpReqAnsContext<CardReadAdapterService_QuerySCIList_Req, CardReadAdapterService_QuerySCIList_Ans>::Pointer ctx)
  1965. {
  1966. LOG_FUNCTION();
  1967. DWORD dwUsrErrCode = 0;
  1968. if (IsCardIssuerSessionOK())
  1969. {
  1970. ErrorCodeEnum eErr = Error_Unexpect;
  1971. CardIssuerService_QuerySCIList_Req req;
  1972. CardIssuerService_QuerySCIList_Ans ans;
  1973. req.reserved1.Copy(ctx->Req.reserved1);
  1974. req.reserved2.Copy(ctx->Req.reserved2);
  1975. eErr = m_pCardIssuer->QuerySCIList(req, ans, 50000, dwUsrErrCode);
  1976. if (eErr == Error_Succeed)
  1977. {
  1978. ctx->Ans.sciNo.Copy(ans.sciNo);
  1979. ctx->Ans.arrMateriel.Copy(ans.arrMateriel);
  1980. ctx->Ans.CardGroove.Copy(ans.CardGroove);
  1981. ctx->Ans.CardBoxNo.Copy(ans.CardBoxNo);
  1982. ctx->Ans.PsbCode.Copy(ans.PsbCode);
  1983. ctx->Ans.PsbName.Copy(ans.PsbName);
  1984. ctx->Ans.CardInit.Copy(ans.CardInit);
  1985. ctx->Ans.CardRemains.Copy(ans.CardRemains);
  1986. ctx->Ans.CardIssued.Copy(ans.CardIssued);
  1987. ctx->Ans.CardMixed.Copy(ans.CardMixed);
  1988. ctx->Ans.CardPercent.Copy(ans.CardPercent);
  1989. ctx->Ans.Maintainer.Copy(ans.Maintainer);
  1990. ctx->Ans.MaintainTime.Copy(ans.MaintainTime);
  1991. ctx->Ans.UpdateTime.Copy(ans.UpdateTime);
  1992. ctx->Ans.reserved1.Copy(ans.reserved1);
  1993. ctx->Ans.reserved2.Copy(ans.reserved2);
  1994. }
  1995. ctx->Answer(eErr, dwUsrErrCode);
  1996. return 0;
  1997. }
  1998. else
  1999. {
  2000. Dbg("CardIssuer session is not ok.");
  2001. ctx->Answer(Error_Param);
  2002. return 1;
  2003. }
  2004. }
  2005. int CCardReadAdapterFSM::BindSCI(SpReqAnsContext<CardReadAdapterService_BindSCI_Req, CardReadAdapterService_BindSCI_Ans>::Pointer ctx)
  2006. {
  2007. LOG_FUNCTION();
  2008. DWORD dwUsrErrCode = 0;
  2009. if (IsCardIssuerSessionOK())
  2010. {
  2011. ErrorCodeEnum eErr = Error_Unexpect;
  2012. CardIssuerService_BindSCI_Req req;
  2013. CardIssuerService_BindSCI_Ans ans;
  2014. req.type = ctx->Req.type;
  2015. req.sciNo = ctx->Req.sciNo;
  2016. req.reserved1.Copy(ctx->Req.reserved1);
  2017. req.reserved2.Copy(ctx->Req.reserved2);
  2018. eErr = m_pCardIssuer->BindSCI(req, ans, 50000, dwUsrErrCode);
  2019. if (eErr == Error_Succeed)
  2020. {
  2021. ctx->Ans.reserved1.Copy(ans.reserved1);
  2022. ctx->Ans.reserved2.Copy(ans.reserved2);
  2023. }
  2024. ctx->Answer(eErr, dwUsrErrCode);
  2025. return 0;
  2026. }
  2027. else
  2028. {
  2029. Dbg("CardIssuer session is not ok.");
  2030. ctx->Answer(Error_Param);
  2031. return 1;
  2032. }
  2033. }
  2034. int CCardReadAdapterFSM::PreOnlineOnStore(SpReqAnsContext<CardReadAdapterService_PreOnlineOnStore_Req, CardReadAdapterService_PreOnlineOnStore_Ans>::Pointer ctx)
  2035. {
  2036. LOG_FUNCTION();
  2037. if (IsCardIssuerSessionOK())
  2038. {
  2039. ErrorCodeEnum eErr = Error_Unexpect;
  2040. CardIssuerService_PreOnlineOnStore_Req req;
  2041. CardIssuerService_PreOnlineOnStore_Ans ans;
  2042. req.account = ctx->Req.account;
  2043. req.businessData = ctx->Req.businessData;
  2044. req.slot = ctx->Req.slot;
  2045. req.termNo = ctx->Req.termNo;
  2046. req.reserved1.Copy(ctx->Req.reserved1);
  2047. req.reserved2.Copy(ctx->Req.reserved2);
  2048. req.reserved3.Copy(ctx->Req.reserved3);
  2049. req.reserved4.Copy(ctx->Req.reserved4);
  2050. eErr = m_pCardIssuer->PreOnlineOnStore(req, ans, 63000);
  2051. if (eErr == Error_Succeed)
  2052. {
  2053. ctx->Ans.cardPos = ans.cardPos;
  2054. ctx->Ans.findCard = ans.findCard;
  2055. ctx->Ans.result = ans.result;
  2056. ctx->Ans.reserved1.Copy(ans.reserved1);
  2057. ctx->Ans.reserved2.Copy(ans.reserved2);
  2058. ctx->Ans.reserved3.Copy(ans.reserved3);
  2059. ctx->Ans.reserved4.Copy(ans.reserved4);
  2060. }
  2061. ctx->Answer(eErr);
  2062. }
  2063. else
  2064. {
  2065. Dbg("Entity CardIssuer is not ok.");
  2066. ctx->Answer(Error_Unexpect);
  2067. }
  2068. return 0;
  2069. }
  2070. int CCardReadAdapterFSM::NotifyPreonline(SpReqAnsContext<CardReadAdapterService_NotifyPreonline_Req, CardReadAdapterService_NotifyPreonline_Ans>::Pointer ctx)
  2071. {
  2072. LOG_FUNCTION();
  2073. if (IsCardIssuerSessionOK())
  2074. {
  2075. ErrorCodeEnum eErr = Error_Unexpect;
  2076. CardIssuerService_NotifyPreonline_Req req;
  2077. CardIssuerService_NotifyPreonline_Ans ans;
  2078. req.account = ctx->Req.account;
  2079. req.cardPos = ctx->Req.cardPos;
  2080. req.data = ctx->Req.data;
  2081. req.errCode = ctx->Req.errCode;
  2082. req.findCard = ctx->Req.findCard;
  2083. req.termNo = ctx->Req.termNo;
  2084. req.reserved1.Copy(ctx->Req.reserved1);
  2085. req.reserved2.Copy(ctx->Req.reserved2);
  2086. req.reserved3.Copy(ctx->Req.reserved3);
  2087. req.reserved4.Copy(ctx->Req.reserved4);
  2088. eErr = m_pCardIssuer->NotifyPreonline(req, ans, 100000);
  2089. if (eErr == Error_Succeed)
  2090. {
  2091. ctx->Ans.reserved1.Copy(ans.reserved1);
  2092. ctx->Ans.reserved2.Copy(ans.reserved2);
  2093. ctx->Ans.reserved3.Copy(ans.reserved3);
  2094. ctx->Ans.reserved4.Copy(ans.reserved4);
  2095. }
  2096. ctx->Answer(eErr);
  2097. }
  2098. else
  2099. {
  2100. Dbg("Entity CardIssuer is not ok.");
  2101. ctx->Answer(Error_Unexpect);
  2102. }
  2103. return 0;
  2104. }
  2105. int CCardReadAdapterFSM::QueryCardInfoOnStore(SpReqAnsContext<CardReadAdapterService_QueryCardInfoOnStore_Req, CardReadAdapterService_QueryCardInfoOnStore_Ans>::Pointer ctx)
  2106. {
  2107. LOG_FUNCTION();
  2108. if (IsCardIssuerSessionOK())
  2109. {
  2110. ErrorCodeEnum eErr = Error_Unexpect;
  2111. CardIssuerService_QueryCardInfoOnStore_Req req;
  2112. CardIssuerService_QueryCardInfoOnStore_Ans ans;
  2113. req.reserved1.Copy(ctx->Req.reserved1);
  2114. req.reserved2.Copy(ctx->Req.reserved2);
  2115. req.reserved3.Copy(ctx->Req.reserved3);
  2116. req.reserved4.Copy(ctx->Req.reserved4);
  2117. eErr = m_pCardIssuer->QueryCardInfoOnStore(req, ans, INT_MAX / 2 - 5000);
  2118. if (eErr == Error_Succeed)
  2119. {
  2120. ctx->Ans.cardPos = ans.cardPos;
  2121. ctx->Ans.findCard = ans.findCard;
  2122. ctx->Ans.reserved1.Copy(ans.reserved1);
  2123. ctx->Ans.reserved2.Copy(ans.reserved2);
  2124. ctx->Ans.reserved3.Copy(ans.reserved3);
  2125. ctx->Ans.reserved4.Copy(ans.reserved4);
  2126. }
  2127. ctx->Answer(eErr);
  2128. }
  2129. else
  2130. {
  2131. Dbg("Entity CardIssuer is not ok.");
  2132. ctx->Answer(Error_Unexpect);
  2133. }
  2134. return 0;
  2135. }
  2136. int CCardReadAdapterFSM::GetAddCardInfo(SpReqAnsContext<CardReadAdapterService_GetAddCardInfo_Req, CardReadAdapterService_GetAddCardInfo_Ans>::Pointer ctx)
  2137. {
  2138. LOG_FUNCTION();
  2139. if (IsCardIssuerSessionOK())
  2140. {
  2141. ErrorCodeEnum eErr = Error_Unexpect;
  2142. CardIssuerService_GetAddCardInfo_Req req;
  2143. CardIssuerService_GetAddCardInfo_Ans ans;
  2144. req.isSync = ctx->Req.isSync;
  2145. req.reserved1.Copy(ctx->Req.reserved1);
  2146. req.reserved2.Copy(ctx->Req.reserved2);
  2147. req.reserved3.Copy(ctx->Req.reserved3);
  2148. req.reserved4.Copy(ctx->Req.reserved4);
  2149. eErr = m_pCardIssuer->GetAddCardInfo(req, ans, 10000);
  2150. if (eErr == Error_Succeed)
  2151. {
  2152. ctx->Ans.account = ans.account;
  2153. ctx->Ans.cardSerial = ans.cardSerial;
  2154. ctx->Ans.count = ans.count;
  2155. ctx->Ans.slot = ans.slot;
  2156. ctx->Ans.reserved1.Copy(ans.reserved1);
  2157. ctx->Ans.reserved2.Copy(ans.reserved2);
  2158. ctx->Ans.reserved3.Copy(ans.reserved3);
  2159. ctx->Ans.reserved4.Copy(ans.reserved4);
  2160. }
  2161. ctx->Answer(eErr);
  2162. }
  2163. else
  2164. {
  2165. Dbg("Entity CardIssuer is not ok.");
  2166. ctx->Answer(Error_Unexpect);
  2167. }
  2168. return 0;
  2169. }
  2170. int CCardReadAdapterFSM::Test1(SpReqAnsContext<CardReadAdapterService_Test1_Req, CardReadAdapterService_Test1_Ans>::Pointer ctx)
  2171. {
  2172. LOG_FUNCTION();
  2173. ctx->Answer(Error_Succeed);
  2174. return 0;
  2175. }
  2176. int CCardReadAdapterFSM::Test2(SpReqAnsContext<CardReadAdapterService_Test2_Req, CardReadAdapterService_Test2_Ans>::Pointer ctx)
  2177. {
  2178. LOG_FUNCTION();
  2179. ctx->Answer(Error_Succeed);
  2180. return 0;
  2181. }
  2182. void CCardReadAdapterFSM::SelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
  2183. {
  2184. //for simple
  2185. pTransactionContext->SendAnswer(m_testResult);
  2186. }
  2187. int CCardReadAdapterFSM::CrossTermCall(SpReqAnsContext<CardReadAdapterService_CrossTermCall_Req, CardReadAdapterService_CrossTermCall_Ans>::Pointer cctx)
  2188. {
  2189. ErrorCodeEnum eErr = Error_Unexpect;
  2190. Dbg("CrossTermCall,type:%x", cctx->Req.type);
  2191. switch (cctx->Req.type)
  2192. {
  2193. case CardReadAdapterService_Method_Read:
  2194. {
  2195. int retErr;
  2196. DEFINE_PUPPET_CONTEXT_WITH_TYPE(CardReadAdapterService_Read_Req, CardReadAdapterService_Read_Ans);
  2197. if ((eErr = SpBlob2Object(cctx->Req.paramX, ctx->Req)) == Error_Succeed) {
  2198. if (this->CardSwiperRead(ctx, retErr) != 0) {
  2199. eErr = Error_Unexpect;
  2200. }
  2201. else {
  2202. eErr = SpObject2Blob(ctx->Ans, cctx->Ans.retParamX);
  2203. Dbg("after CardSwiperRead,eErr:%d,retErr:%x,paramX.len:%d", eErr, retErr, cctx->Ans.retParamX.m_iLength);
  2204. }
  2205. }
  2206. cctx->Answer((ErrorCodeEnum)retErr);
  2207. }
  2208. break;
  2209. case CardReadAdapterService_Method_CancelRead:
  2210. m_bCancelAccept = true;
  2211. CancelRead();
  2212. break;
  2213. case CardReadAdapterService_Method_ReadWaitMore:
  2214. {
  2215. ReadWaitMoreTask* task = new ReadWaitMoreTask(this);
  2216. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  2217. }
  2218. break;
  2219. case CardReadAdapterService_Method_Exit:
  2220. DoExit();
  2221. break;
  2222. default:
  2223. break;
  2224. }
  2225. return 0;
  2226. }
  2227. void CCardReadAdapterFSM::CrossTermInvokeInfo(SpOnewayCallContext<CardReadAdapterService_CrossTermInvokeInfo_Info>::Pointer ctx)
  2228. {
  2229. LOG_FUNCTION();
  2230. Dbg("entity sn:%x,type:%d,errcode:%x", ctx->Info.nEntityID, ctx->Info.type, ctx->Info.result);
  2231. if (ctx->Info.paramX.m_iLength <= 1)
  2232. {
  2233. }
  2234. ErrorCodeEnum eErr = Error_Succeed;
  2235. CSmartPointer<ITransactionContext> pTransactionContext;
  2236. switch (ctx->Info.type)
  2237. {
  2238. case CardReadAdapterService_Method_Read:
  2239. {
  2240. auto pt = static_cast<SpReqAnsContext<CardReadAdapterService_Read_Req,
  2241. CardReadAdapterService_Read_Ans>*>(GetCtx(ctx->Info.type));
  2242. SpReqAnsContext<CardReadAdapterService_Read_Req,
  2243. CardReadAdapterService_Read_Ans>::Pointer xctx;
  2244. xctx.Attach(pt);
  2245. if (ctx->Info.paramX.m_iLength > 0)
  2246. eErr = SpBlob2Object(ctx->Info.paramX, xctx->Ans);
  2247. Dbg("eErr:%d,Y len:%d", eErr, ctx->Info.paramX.m_iLength);
  2248. xctx->Answer((ErrorCodeEnum)ctx->Info.result);
  2249. }
  2250. break;
  2251. default:
  2252. break;
  2253. }
  2254. }
  2255. int CCardReadAdapterFSM::LocalCallHeartBeat(int method, CBlob &bbSend,bool bTwoWay)
  2256. {
  2257. Dbg("method id(type):%d", method);
  2258. HeartBeatService_ClientBase* pClient = new HeartBeatService_ClientBase(GetEntityBase());
  2259. if (pClient == NULL)
  2260. {
  2261. Dbg("create HeartBeat client failed.GetLastError:%d", GetLastError());
  2262. return -1;
  2263. }
  2264. ErrorCodeEnum eErr = Error_Unexpect;
  2265. eErr = pClient->Connect();
  2266. if (eErr != Error_Succeed)
  2267. {
  2268. Dbg("client connect to HeartBeat failed:%x", eErr);
  2269. return -1;
  2270. }
  2271. HeartBeatService_CrossTermCall_Req req;
  2272. HeartBeatService_CrossTermCall_Ans ans;
  2273. req.nEntityID = 0x21b;
  2274. req.isTwoWay = bTwoWay;
  2275. req.fromTerminalNo = m_terminalNo;
  2276. GetAttachedTerminal(req.toTerminalNo);
  2277. //req.toTerminalNo = "7717715004";
  2278. req.param1 = "";
  2279. req.param2 = "";
  2280. req.paramX = bbSend;
  2281. req.type = method;
  2282. eErr = pClient->CrossTermCall(req, ans, 20000);
  2283. Dbg("CrossTermCall to HeartBeat from %s to %s, result:%x", (const char*)req.fromTerminalNo, (const char*)req.toTerminalNo, eErr);
  2284. return 0;
  2285. }
  2286. bool CCardReadAdapterFSM::GetAttachedTerminal(CSimpleStringA &csTerm)
  2287. {
  2288. return GetEntityBase()->GetFunction()->GetSysVar("AttachedTerminal", csTerm);
  2289. }
  2290. void CCardReadAdapterFSM::CancelReadForEntity(ModuleType eModule)
  2291. {
  2292. LOG_FUNCTION();
  2293. if (IsTheEntity(eModule, Module_CardIssuer) && IsCardIssuerSessionOK())
  2294. {
  2295. Dbg("Set cancel read by ContactlessCard flag of CardIssuer.");
  2296. CardIssuerService_SetSomeFlag_Req req;
  2297. CardIssuerService_SetSomeFlag_Ans ans;
  2298. req.IssueBusiness = 3;
  2299. m_pCardIssuer->SetSomeFlag(req,ans,5000);
  2300. m_pCardIssuer->CancelInsert();
  2301. }
  2302. if (IsTheEntity(eModule, Module_ContactlessCard) && IsContactlessCardSessionOK())
  2303. {
  2304. Dbg("Cancel read by CardSwiper of ContactlessCard.");
  2305. ContactlessCardService_CancelInsert_Info info;
  2306. m_pContactless->CancelInsert();
  2307. }
  2308. }
  2309. int CCardReadAdapterFSM::PrintCardIm(SpReqAnsContext<CardReadAdapterService_PrintCardImmediately_Req, CardReadAdapterService_PrintCardImmediately_Ans>::Pointer ctx)
  2310. {
  2311. LOG_FUNCTION();
  2312. if (IsCardIssuerSessionOK())
  2313. {
  2314. ErrorCodeEnum eErr = Error_Unexpect;
  2315. CardIssuerService_PrintCardImmediately_Req req;
  2316. CardIssuerService_PrintCardImmediately_Ans ans;
  2317. req.formFile = ctx->Req.formFile;
  2318. req.printData = ctx->Req.printData;
  2319. req.reserved1.Copy(ctx->Req.reserved1);
  2320. req.reserved2.Copy(ctx->Req.reserved2);
  2321. req.reserved3.Copy(ctx->Req.reserved3);
  2322. req.reserved4.Copy(ctx->Req.reserved4);
  2323. int timeout = 60000;
  2324. if (ctx->Req.reserved1.GetCount() > 0 && ctx->Req.reserved1[0] > 50000 && ctx->Req.reserved1[0] < 200000)
  2325. timeout = ctx->Req.reserved1[0];
  2326. Dbg("timeout:%d",timeout);
  2327. eErr = m_pCardIssuer->PrintCardImmediately(req, ans, timeout);
  2328. if (eErr == Error_Succeed)
  2329. {
  2330. ctx->Ans.ret = ans.ret;
  2331. ctx->Ans.reserved1.Copy(ans.reserved1);
  2332. ctx->Ans.reserved2.Copy(ans.reserved2);
  2333. ctx->Ans.reserved3.Copy(ans.reserved3);
  2334. ctx->Ans.reserved4.Copy(ans.reserved4);
  2335. }
  2336. ctx->Answer(eErr);
  2337. return 0;
  2338. }
  2339. else
  2340. {
  2341. Dbg("CardIssuer session is not ok.");
  2342. ctx->Answer(Error_Param);
  2343. return 1;
  2344. }
  2345. }