Module_Systex.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * Module_RFID.c
  3. *
  4. * Created on: 2019-10-24
  5. * Update: 2020-10-19
  6. * Author: Eason Yang
  7. * Version: V0.03
  8. *
  9. * History
  10. * 1. Added : Reyax code.
  11. *
  12. *
  13. *
  14. *
  15. *
  16. */
  17. #include "Module_Systex.h"
  18. #define RequestLength 600
  19. #define ResponseLength 600
  20. //==================================
  21. // PRINT OUT LOG FORMAT
  22. //==================================
  23. #define SystemLogMessage
  24. #define DEBUG_INFO_1(format, args...) StoreLogMessage("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  25. #define DEBUG_WARN_1(format, args...) StoreLogMessage("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  26. #define DEBUG_ERROR_1(format, args...) StoreLogMessage("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  27. //==================================
  28. // HOST ID, offset = 0, length = 2 bytes ,
  29. //==================================
  30. unsigned char HOST_ID_CreditCard[] = {0x31, 0x30};//信用卡
  31. unsigned char HOST_ID_eTicket_Polling[] = {0x35, 0x30};//電子票證
  32. //==================================
  33. // Transaction Type, offset = 2, length = 2 bytes ,
  34. //==================================
  35. //-------------尋卡---------------
  36. unsigned char TRANS_TYPE_Search[] = {0x30, 0x30}; //讀取卡片卡號 or 條碼資料
  37. //--------------信用卡一般消費---------------
  38. unsigned char TRANS_TYPE_Sale[] = {0x31, 0x31}; //消費交易 : 信用卡,票證,掃碼支付
  39. unsigned char TRANS_TYPE_Void[] = {0x30, 0x35};//取消交易 : 信用卡,
  40. unsigned char TRANS_TYPE_Refund[] = {0x31, 0x32};//退貨交易 : 信用卡,票證,掃碼支付
  41. //--------------信用卡預授權---------------
  42. unsigned char TRANS_TYPE_Pre_Auth[] = {0x31, 0x33}; //預授權交易 : 信用卡
  43. unsigned char TRANS_TYPE_Pre_Auth_Complete[] = {0x33, 0x31}; //預授權完成交易 : 信用卡
  44. unsigned char TRANS_TYPE_Pre_Auth_Cancel[] = {0x33, 0x32}; //取消預授權交易 : 信用卡
  45. //--------------連動結帳---------------
  46. unsigned char TRANS_TYPE_UnionSettlement[] = {0x35, 0x41}; //連動結帳請款
  47. //==================================
  48. // SystemLog message
  49. //==================================
  50. #ifdef SystemLogMessage
  51. int StoreLogMessage(const char *fmt, ...)
  52. {
  53. char Buf[4096 + 256];
  54. char buffer[4096];
  55. time_t CurrentTime;
  56. struct tm *tm;
  57. va_list args;
  58. va_start(args, fmt);
  59. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  60. va_end(args);
  61. memset(Buf, 0, sizeof(Buf));
  62. CurrentTime = time(NULL);
  63. tm = localtime(&CurrentTime);
  64. sprintf(Buf,
  65. "echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]RFID_SystemLog",
  66. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
  67. tm->tm_min, tm->tm_sec, buffer, tm->tm_year + 1900, tm->tm_mon + 1);
  68. system(Buf);
  69. #ifdef Debug
  70. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year + 1900,
  71. tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  72. buffer);
  73. #endif
  74. return rc;
  75. }
  76. #endif
  77. //==========================================
  78. // Module's command send/receive
  79. //==========================================
  80. int SendRequest(int uart, unsigned char* cmd, int length)
  81. {
  82. int len, TotalRtn=0, Rtn,i;
  83. unsigned int StartTime;
  84. unsigned char LRC=0;
  85. unsigned char SendBuffer[RequestLength+4], RecvBuf[8];
  86. len=length+3;
  87. memset(SendBuffer,0,RequestLength+4);
  88. SendBuffer[0]=0x02;
  89. memcpy(SendBuffer+1,cmd,length);
  90. SendBuffer[length+1]=0x03;
  91. for(i=1;i<=length+1;i++)
  92. {
  93. LRC^=SendBuffer[i];
  94. }
  95. SendBuffer[length+2]=LRC;
  96. //sleep(2); //required to make flush work, for some reason
  97. //for(int i=0;i<len;i++)
  98. //printf("%x\n",SendBuffer[i]);
  99. //printf("strlen(SendBuffer)=%d\n",strlen(SendBuffer));
  100. //printf("SendBuffer[0]=%x,SendBuffer[601]=%x,SendBuffer[602]=%x \n",SendBuffer[i],SendBuffer[601],SendBuffer[602]);
  101. //tcflush(uart,TCIOFLUSH);
  102. while(1)
  103. {
  104. Rtn=write(uart, SendBuffer+TotalRtn, len-TotalRtn);
  105. if(Rtn>0)
  106. {
  107. TotalRtn+=Rtn;
  108. if(TotalRtn>=len)
  109. {
  110. //printf("Rtn=%d,TotalRtn=%d\n",Rtn,TotalRtn);
  111. break;
  112. }
  113. }
  114. else
  115. {
  116. DEBUG_INFO_1("SendRequest: write fail (%d / %d)\n", TotalRtn,len);
  117. return -1;
  118. }
  119. }
  120. //usleep(100000);
  121. TotalRtn=0;
  122. memset(SendBuffer,0,RequestLength+4);
  123. StartTime = time(NULL);
  124. while(1)
  125. {
  126. Rtn=read(uart, SendBuffer+TotalRtn, 2-TotalRtn);
  127. if(Rtn>0)
  128. {
  129. TotalRtn+=Rtn;
  130. if(TotalRtn>=2)
  131. {
  132. if((SendBuffer[0]==0x06)&&(SendBuffer[1]==0x06))
  133. return TotalRtn;
  134. else
  135. {
  136. DEBUG_INFO_1("SendRequest: not recv ACKACK(%x, %x)\n", SendBuffer[0],SendBuffer[1]);
  137. read(uart, SendBuffer, RequestLength);
  138. return -1;
  139. }
  140. }
  141. }
  142. else
  143. {
  144. if((time(NULL)-StartTime)>3)
  145. {
  146. DEBUG_INFO_1("SendRequest: read fail (%d / 6)\n", TotalRtn);
  147. return -1;
  148. }
  149. }
  150. }
  151. }
  152. int RecvResponse(int uart, unsigned char* rx)
  153. {
  154. int len, TotalRtn=0, Rtn,i;
  155. unsigned int StartTime;
  156. unsigned char LRC=0, counter=0;
  157. unsigned char RecvBuffer[ResponseLength+4], tmpBuf[16];
  158. reRecv:
  159. len=ResponseLength+3;
  160. memset(RecvBuffer,0,ResponseLength+4);
  161. LRC=0;
  162. TotalRtn=0;
  163. StartTime = time(NULL);
  164. while(1)
  165. {
  166. Rtn=read(uart, RecvBuffer+TotalRtn, len-TotalRtn);
  167. if(Rtn>0)
  168. {
  169. TotalRtn+=Rtn;
  170. //for(int i=0;i<TotalRtn;i++)
  171. //printf("%x",RecvBuffer[i]);
  172. //printf("\n");
  173. if(TotalRtn>=len)
  174. {
  175. if((RecvBuffer[0]!=0x02)||(RecvBuffer[ResponseLength+1]!=0x03))
  176. {
  177. memset(tmpBuf,0,sizeof(tmpBuf));
  178. tmpBuf[0]=tmpBuf[1]=0x06;
  179. Rtn=write(uart, tmpBuf, 2);
  180. DEBUG_INFO_1("RecvResponse: Not Response format (STX=%c , ETX=%c)\n", RecvBuffer[0],RecvBuffer[ResponseLength+1]);
  181. return -1;
  182. }
  183. for(i=1;i<=ResponseLength+1;i++)
  184. {
  185. LRC^=RecvBuffer[i];
  186. }
  187. if(RecvBuffer[ResponseLength+2]!=LRC)
  188. {
  189. if(counter++<2)
  190. {
  191. memset(tmpBuf,0,sizeof(tmpBuf));
  192. tmpBuf[0]=tmpBuf[1]=0x15;
  193. Rtn=write(uart, tmpBuf, 2);
  194. goto reRecv;
  195. }
  196. else
  197. {
  198. memset(tmpBuf,0,sizeof(tmpBuf));
  199. tmpBuf[0]=tmpBuf[1]=0x06;
  200. Rtn=write(uart, tmpBuf, 2);
  201. DEBUG_INFO_1("RecvResponse: 2 times LRC Error (%d / %d)\n", LRC,RecvBuffer[ResponseLength+2]);
  202. return -1;
  203. }
  204. }
  205. memset(tmpBuf,0,sizeof(tmpBuf));
  206. memcpy(tmpBuf,RecvBuffer+1,8 );
  207. if((strstr(tmpBuf,"00000000")==NULL)&&(strstr(tmpBuf,"000000TD")==NULL))
  208. {
  209. DEBUG_INFO_1("RecvResponse: Response Code failed (%s)\n", tmpBuf);
  210. memset(tmpBuf,0,sizeof(tmpBuf));
  211. tmpBuf[0]=tmpBuf[1]=0x06;
  212. Rtn=write(uart, tmpBuf, 2);
  213. return -1;
  214. }
  215. memset(tmpBuf,0,sizeof(tmpBuf));
  216. tmpBuf[0]=tmpBuf[1]=0x06;
  217. Rtn=write(uart, tmpBuf, 2);
  218. memcpy(rx,RecvBuffer+1,ResponseLength);
  219. return ResponseLength;
  220. }
  221. }
  222. else
  223. {
  224. if((time(NULL)-StartTime)>40)//卡機30 seconds timeout
  225. {
  226. DEBUG_INFO_1("RecvResponse: read fail (%d / %d)\n", TotalRtn,len);
  227. return -1;
  228. }
  229. }
  230. }
  231. }
  232. //==========================================
  233. // CreditCardSearch
  234. // Input :
  235. //
  236. // Output :
  237. //==========================================
  238. int CreditCardSearch(int Fd,unsigned char *EVSEID, struct TransInfo *TransInfoReturn)
  239. {
  240. unsigned char Buffer[RequestLength], *ptr, tmpbuf[12];
  241. memset(Buffer,0x20,RequestLength);
  242. ptr=Buffer;
  243. strncpy(ptr,HOST_ID_CreditCard,2);//Host ID
  244. ptr+=2;
  245. strncpy(ptr,TRANS_TYPE_Search,2);//Transaction type
  246. ptr+=2;
  247. /*memset(tmpbuf,0,sizeof(tmpbuf));
  248. sprintf(tmpbuf,"%d", PreCost);
  249. if(strlen(tmpbuf)>12)
  250. {
  251. DEBUG_INFO_1("CreditCardPreAuthCancel: Wrong PreCost (PreCost=%d)\n", PreCost);
  252. return -1;
  253. }
  254. memset(ptr,0x30,12);
  255. strncpy(ptr+12-(strlen(tmpbuf)+2),tmpbuf,strlen(tmpbuf));//Trans Amount
  256. */
  257. ptr+=12;//Trans Amount
  258. //strncpy(ptr,ApprovalNo, strlen(ApprovalNo));
  259. ptr+=12;//Approval No
  260. //strncpy(ptr,CardNum, strlen(CardNum));
  261. ptr+=20;//Card No
  262. strncpy(ptr,EVSEID, strlen(EVSEID));
  263. ptr+=18;//Store Id
  264. //ptr+=2;//Period
  265. if(SendRequest(Fd, Buffer, RequestLength)>0)
  266. {
  267. memset(Buffer,0,RequestLength);
  268. if(RecvResponse(Fd,Buffer)>0)
  269. {
  270. strncpy(TransInfoReturn->TransDate,Buffer+56,6);
  271. strncpy(TransInfoReturn->TransTime,Buffer+62,6);
  272. strncpy(TransInfoReturn->CardNo,Buffer+24,20);
  273. return 1;
  274. }
  275. else
  276. {
  277. DEBUG_INFO_1("CreditCardUnionSettlement: RecvResponse failed\n");
  278. return -1;
  279. }
  280. }
  281. else
  282. {
  283. DEBUG_INFO_1("CreditCardUnionSettlement: SendRequest failed\n");
  284. return -1;
  285. }
  286. }
  287. //==========================================
  288. // CreditCardPreAuth
  289. // Input :
  290. // PreCost : 預扣金額*100, e.g., $1234 = 123400, $1234.56=123456
  291. // EVSEID: EVSE ID, max length is 18
  292. // Output :
  293. // ApprovalNo : EDC簽單調閱編號或授權碼[信用卡退貨交易, max length is 12
  294. // RRN : 信用卡交易序號, max length is 12
  295. // CardNum: 卡號,max length is 20
  296. // VemData : 無人自助設備交易資訊 交易別31/32 (預授權完成/預授權取消) 必要欄位 資訊來源為交易別13 (預授權)的回傳, max length is 64
  297. //==========================================
  298. int CreditCardPreAuth(int Fd, int PreCost,unsigned char *EVSEID, struct TransInfo *TransInfoReturn)
  299. {
  300. unsigned char Buffer[RequestLength], *ptr, tmpbuf[12];
  301. memset(Buffer,0x20,RequestLength);
  302. ptr=Buffer;
  303. strncpy(ptr,HOST_ID_CreditCard,2);//Host ID
  304. ptr+=2;
  305. strncpy(ptr,TRANS_TYPE_Pre_Auth,2);//Transaction type
  306. ptr+=2;
  307. memset(tmpbuf,0,sizeof(tmpbuf));
  308. sprintf(tmpbuf,"%d", PreCost);
  309. if(strlen(tmpbuf)>12)
  310. {
  311. DEBUG_INFO_1("CreditCardPreAuth: Wrong PreCost (PreCost=%d)\n", PreCost);
  312. return -1;
  313. }
  314. memset(ptr,0x30,12);
  315. strncpy(ptr+12-(strlen(tmpbuf)+2),tmpbuf,strlen(tmpbuf));//Trans Amount
  316. ptr+=12;//Trans Amount
  317. ptr+=12;//Approval No
  318. ptr+=20;//Card No
  319. strncpy(ptr,EVSEID, strlen(EVSEID));
  320. ptr+=18;//Store Id
  321. //ptr+=2;//Period
  322. if(SendRequest(Fd, Buffer, RequestLength)>0)
  323. {
  324. memset(Buffer,0,RequestLength);
  325. if(RecvResponse(Fd,Buffer)>0)
  326. {
  327. //for(int i=0;i<RequestLength;i++)
  328. //printf("%x, ",Buffer[i] );
  329. strncpy(TransInfoReturn->TransAmount,Buffer+44,12);
  330. strncpy(TransInfoReturn->TransDate,Buffer+56,6);
  331. strncpy(TransInfoReturn->TransTime,Buffer+62,6);
  332. strncpy(TransInfoReturn->StoreId,Buffer+97,18);
  333. strncpy(TransInfoReturn->ApprovalNo,Buffer+12,12);
  334. strncpy(TransInfoReturn->RRN,Buffer+124,12);
  335. strncpy(TransInfoReturn->CardNo,Buffer+24,20);
  336. strncpy(TransInfoReturn->VemData,Buffer+246,64);
  337. return 1;
  338. }
  339. else
  340. {
  341. DEBUG_INFO_1("CreditCardPreAuth: RecvResponse failed\n");
  342. return -1;
  343. }
  344. }
  345. else
  346. {
  347. DEBUG_INFO_1("CreditCardPreAuth: SendRequest failed\n");
  348. return -1;
  349. }
  350. }
  351. //==========================================
  352. // CreditCardAuthComplete
  353. // Input :
  354. // PreCost : 扣金額*100, e.g., $1234 = 123400, $1234.56=123456
  355. // EVSEID: EVSE ID, max length is 18
  356. // VemData : the VEM data from CreditCardPreAuth, max length is 64
  357. // Output :
  358. //==========================================
  359. int CreditCardPreAuthComplete(int Fd, int PreCost,unsigned char *EVSEID, unsigned char *VemData,struct TransInfo *TransInfoReturn)
  360. {
  361. unsigned char Buffer[RequestLength], *ptr, tmpbuf[12];
  362. memset(Buffer,0x20,RequestLength);
  363. ptr=Buffer;
  364. strncpy(ptr,HOST_ID_CreditCard,2);//Host ID
  365. ptr+=2;
  366. strncpy(ptr,TRANS_TYPE_Pre_Auth_Complete,2);//Transaction type
  367. ptr+=2;
  368. memset(tmpbuf,0,sizeof(tmpbuf));
  369. sprintf(tmpbuf,"%d", PreCost);
  370. if(strlen(tmpbuf)>12)
  371. {
  372. DEBUG_INFO_1("CreditCardAuthComplete: Wrong PreCost (PreCost=%d)\n", PreCost);
  373. return -1;
  374. }
  375. memset(ptr,0x30,12);
  376. strncpy(ptr+12-(strlen(tmpbuf)+2),tmpbuf,strlen(tmpbuf));//Trans Amount
  377. ptr+=12;//Trans Amount
  378. ptr+=12;//Approval No
  379. ptr+=20;//Card No
  380. strncpy(ptr,EVSEID, strlen(EVSEID));
  381. ptr+=18;//Store Id
  382. //ptr+=2;//Period
  383. strncpy(Buffer+500,VemData,64);
  384. if(SendRequest(Fd, Buffer, RequestLength)>0)
  385. {
  386. memset(Buffer,0,RequestLength);
  387. if(RecvResponse(Fd,Buffer)>0)
  388. {
  389. strncpy(TransInfoReturn->TransAmount,Buffer+44,12);
  390. strncpy(TransInfoReturn->TransDate,Buffer+56,6);
  391. strncpy(TransInfoReturn->TransTime,Buffer+62,6);
  392. strncpy(TransInfoReturn->StoreId,Buffer+97,18);
  393. strncpy(TransInfoReturn->ApprovalNo,Buffer+12,12);
  394. strncpy(TransInfoReturn->RRN,Buffer+124,12);
  395. strncpy(TransInfoReturn->CardNo,Buffer+24,20);
  396. strncpy(TransInfoReturn->VemData,Buffer+246,64);
  397. return 1;
  398. }
  399. else
  400. {
  401. DEBUG_INFO_1("CreditCardAuthComplete: RecvResponse failed\n");
  402. return -1;
  403. }
  404. }
  405. else
  406. {
  407. DEBUG_INFO_1("CreditCardAuthComplete: SendRequest failed\n");
  408. return -1;
  409. }
  410. }
  411. //==========================================
  412. // CreditCardPreAuthCancel
  413. // Input :
  414. // PreCost : 預扣金額*100, e.g., $1234 = 123400, $1234.56=123456
  415. // EVSEID: EVSE ID, max length is 18
  416. // ApprovalNo: max length is 12
  417. // CardNum : max length is 20
  418. // Output :
  419. //==========================================
  420. int CreditCardPreAuthCancel(int Fd, int PreCost,unsigned char *EVSEID, unsigned char *ApprovalNo, unsigned char *CardNum,unsigned char *VemData)
  421. {
  422. unsigned char Buffer[RequestLength], *ptr, tmpbuf[12];
  423. memset(Buffer,0x20,RequestLength);
  424. ptr=Buffer;
  425. strncpy(ptr,HOST_ID_CreditCard,2);//Host ID
  426. ptr+=2;
  427. strncpy(ptr,TRANS_TYPE_Pre_Auth_Cancel,2);//Transaction type
  428. ptr+=2;
  429. memset(tmpbuf,0,sizeof(tmpbuf));
  430. sprintf(tmpbuf,"%d", PreCost);
  431. if(strlen(tmpbuf)>12)
  432. {
  433. DEBUG_INFO_1("CreditCardPreAuthCancel: Wrong PreCost (PreCost=%d)\n", PreCost);
  434. return -1;
  435. }
  436. memset(ptr,0x30,12);
  437. strncpy(ptr+12-(strlen(tmpbuf)+2),tmpbuf,strlen(tmpbuf));//Trans Amount
  438. ptr+=12;//Trans Amount
  439. strncpy(ptr,ApprovalNo, strlen(ApprovalNo));
  440. ptr+=12;//Approval No
  441. strncpy(ptr,CardNum, strlen(CardNum));
  442. ptr+=20;//Card No
  443. strncpy(ptr,EVSEID, strlen(EVSEID));
  444. ptr+=18;//Store Id
  445. //ptr+=2;//Period
  446. strncpy(Buffer+500,VemData,64);
  447. if(SendRequest(Fd, Buffer, RequestLength)>0)
  448. {
  449. memset(Buffer,0,RequestLength);
  450. if(RecvResponse(Fd,Buffer)>0)
  451. {
  452. return 1;
  453. }
  454. else
  455. {
  456. DEBUG_INFO_1("CreditCardPreAuthCancel: RecvResponse failed\n");
  457. return -1;
  458. }
  459. }
  460. else
  461. {
  462. DEBUG_INFO_1("CreditCardPreAuthCancel: SendRequest failed\n");
  463. return -1;
  464. }
  465. }
  466. //==========================================
  467. // CreditCardUnionSettlement
  468. // Input :
  469. //
  470. // Output :
  471. //==========================================
  472. int CreditCardUnionSettlement(int Fd,unsigned char *EVSEID, struct TransInfo *TransInfoReturn)
  473. {
  474. unsigned char Buffer[RequestLength], *ptr, tmpbuf[12];
  475. memset(Buffer,0x20,RequestLength);
  476. ptr=Buffer;
  477. strncpy(ptr,HOST_ID_CreditCard,2);//Host ID
  478. ptr+=2;
  479. strncpy(ptr,TRANS_TYPE_UnionSettlement,2);//Transaction type
  480. ptr+=2;
  481. /*memset(tmpbuf,0,sizeof(tmpbuf));
  482. sprintf(tmpbuf,"%d", PreCost);
  483. if(strlen(tmpbuf)>12)
  484. {
  485. DEBUG_INFO_1("CreditCardPreAuthCancel: Wrong PreCost (PreCost=%d)\n", PreCost);
  486. return -1;
  487. }
  488. memset(ptr,0x30,12);
  489. strncpy(ptr+12-(strlen(tmpbuf)+2),tmpbuf,strlen(tmpbuf));//Trans Amount
  490. */
  491. ptr+=12;//Trans Amount
  492. //strncpy(ptr,ApprovalNo, strlen(ApprovalNo));
  493. ptr+=12;//Approval No
  494. //strncpy(ptr,CardNum, strlen(CardNum));
  495. ptr+=20;//Card No
  496. strncpy(ptr,EVSEID, strlen(EVSEID));
  497. ptr+=18;//Store Id
  498. //ptr+=2;//Period
  499. if(SendRequest(Fd, Buffer, RequestLength)>0)
  500. {
  501. memset(Buffer,0,RequestLength);
  502. if(RecvResponse(Fd,Buffer)>0)
  503. {
  504. strncpy(TransInfoReturn->TransAmount,Buffer+44,12);
  505. strncpy(TransInfoReturn->TransDate,Buffer+56,6);
  506. strncpy(TransInfoReturn->TransTime,Buffer+62,6);
  507. strncpy(TransInfoReturn->StoreId,Buffer+97,18);
  508. strncpy(TransInfoReturn->ApprovalNo,Buffer+12,12);
  509. strncpy(TransInfoReturn->RRN,Buffer+124,12);
  510. strncpy(TransInfoReturn->CardNo,Buffer+24,20);
  511. strncpy(TransInfoReturn->VemData,Buffer+246,64);
  512. return 1;
  513. }
  514. else
  515. {
  516. DEBUG_INFO_1("CreditCardUnionSettlement: RecvResponse failed\n");
  517. return -1;
  518. }
  519. }
  520. else
  521. {
  522. DEBUG_INFO_1("CreditCardUnionSettlement: SendRequest failed\n");
  523. return -1;
  524. }
  525. }
  526. #if 0
  527. int InitComPort()
  528. {
  529. int fd;
  530. struct termios tios;
  531. fd = open("/dev/ttyS2", O_RDWR);
  532. if(fd<=0)
  533. {
  534. DEBUG_INFO_1("open /dev/ttyS2 NG\n");
  535. return -1;
  536. }
  537. ioctl (fd, TCGETS, &tios);
  538. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  539. tios.c_lflag = 0;
  540. tios.c_iflag = 0;
  541. tios.c_oflag = 0;
  542. tios.c_cc[VMIN]=0;
  543. tios.c_cc[VTIME]=(unsigned char)5; // timeout 500ms
  544. tios.c_lflag=0;
  545. tcflush(fd, TCIFLUSH);
  546. ioctl (fd, TCSETS, &tios);
  547. return fd;
  548. }
  549. int main(int argc, char *argv[])
  550. {
  551. int UartFd;
  552. struct TransInfo ReturnTransInfo;
  553. UartFd=InitComPort();
  554. if(UartFd<0)
  555. {
  556. printf("InitComPort NG\n");
  557. return;
  558. }
  559. //sleep(3);
  560. memset(&ReturnTransInfo,0,sizeof(struct TransInfo));
  561. printf("CreditCardPreAuth......\n");
  562. if(CreditCardPreAuth(UartFd, 1000,"TCC EVSE", &ReturnTransInfo)<=0)
  563. {
  564. printf("CreditCardPreAuth NG\n");
  565. return;
  566. }
  567. printf("ApprovalNo=%s\n",ReturnTransInfo.ApprovalNo);
  568. printf("RRN=%s\n",ReturnTransInfo.RRN);
  569. printf("CardNum=%s\n",ReturnTransInfo.CardNo);
  570. printf("VemData=%s\n",ReturnTransInfo.VemData);
  571. sleep(20);
  572. /*if(CreditCardPreAuthCancel(UartFd, 1000,"TCC EVSE", &ReturnTransInfo.ApprovalNo, &ReturnTransInfo.CardNo,&ReturnTransInfo.VemData)<=0)
  573. {
  574. printf("CreditCardPreAuthCancel NG\n");
  575. //return;
  576. } */
  577. printf("CreditCardPreAuthComplete......\n");
  578. if(CreditCardPreAuthComplete(UartFd, 500,"TCC EVSE", &ReturnTransInfo.VemData, &ReturnTransInfo)<=0)
  579. {
  580. printf("CreditCardPreAuthComplete NG\n");
  581. return;
  582. }
  583. sleep(15);
  584. if(CreditCardUnionSettlement(UartFd,"TCC EVSE",&ReturnTransInfo)<=0)
  585. {
  586. printf("CreditCardUnionSettlement NG\n");
  587. //return;
  588. }
  589. }
  590. #endif