Module_Payment_Bazel8.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. * Module_Payment_Ominixpay.c
  3. *
  4. * Created on: 2022/04/29
  5. * Author: Folus Wen
  6. * run_evse_stop.sh
  7. */
  8. #include "Module_Payment_Bazel8.h"
  9. struct COMMAND commandRaw;
  10. struct timespec tmr[TIMER_CNT];
  11. uint16_t nSerial;
  12. //==========================================
  13. // Common routine
  14. //==========================================
  15. int StoreLogMsg(const char *fmt, ...)
  16. {
  17. char Buf[65536+256];
  18. char buffer[65536];
  19. //char Buf[4096+256];
  20. //char buffer[4096];
  21. time_t CurrentTime;
  22. struct tm *tm;
  23. struct timeval tv;
  24. va_list args;
  25. va_start(args, fmt);
  26. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  27. va_end(args);
  28. memset(Buf,0,sizeof(Buf));
  29. CurrentTime = time(NULL);
  30. tm=localtime(&CurrentTime);
  31. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  32. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]PaymentLog",
  33. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  34. buffer,
  35. tm->tm_year+1900,tm->tm_mon+1);
  36. system((const char*)Buf);
  37. #ifdef ConsloePrintLog
  38. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  39. #endif
  40. return rc;
  41. }
  42. /**
  43. * Execute shell command
  44. * @param cmd: shell command string
  45. * @return shell command execution result
  46. */
  47. int runShellCmd(const char*cmd)
  48. {
  49. int result = FAIL;
  50. char buf[256];
  51. FILE *fp;
  52. fp = popen(cmd, "r");
  53. if(fp != NULL)
  54. {
  55. while(fgets(buf, sizeof(buf), fp) != NULL)
  56. {
  57. DEBUG_INFO("%s\n", buf);
  58. }
  59. result = PASS;
  60. }
  61. pclose(fp);
  62. return result;
  63. }
  64. /**
  65. *
  66. * @param timer
  67. */
  68. void refreshStartTimer(struct timespec *timer)
  69. {
  70. clock_gettime(CLOCK_MONOTONIC, timer);
  71. }
  72. /**
  73. *
  74. * @param timer
  75. * @return
  76. */
  77. int getDiffSecNow(struct timespec timer)
  78. {
  79. struct timespec timerNow;
  80. clock_gettime(CLOCK_MONOTONIC, &timerNow);
  81. return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
  82. }
  83. /**
  84. *
  85. * @param ST
  86. */
  87. long long DiffTimebWithNow(struct timeb ST)
  88. {
  89. //return milli-second
  90. struct timeb ET;
  91. long long StartTime,StopTime;
  92. ftime(&ET);
  93. StartTime=(long long)ST.time;
  94. StopTime=(long long)ET.time;
  95. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  96. }
  97. /**
  98. *
  99. * @return
  100. */
  101. int getTimePassSinceToday()
  102. {
  103. int result = -1;
  104. static time_t lastTime;
  105. time_t t;
  106. struct tm *tmStartToday;
  107. struct timeb tbStartToday;
  108. t=time(NULL);
  109. if(difftime(t, lastTime)>0)
  110. {
  111. tmStartToday=localtime(&t);
  112. tmStartToday->tm_hour = 0;
  113. tmStartToday->tm_min = 0;
  114. tmStartToday->tm_sec = 0;
  115. tbStartToday.time = mktime(tmStartToday);
  116. tbStartToday.millitm = 0;
  117. result = DiffTimebWithNow(tbStartToday)/1000;
  118. lastTime = t;
  119. }
  120. return result;
  121. }
  122. /**
  123. * Calculate time differential
  124. * @param ST: start time
  125. * @param ET: end time
  126. * @return time differential in million seconds
  127. */
  128. int DiffTimeb(struct timeb ST, struct timeb ET)
  129. {
  130. //return milli-second
  131. unsigned int StartTime,StopTime;
  132. StartTime=(unsigned int)ST.time;
  133. StopTime=(unsigned int)ET.time;
  134. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  135. }
  136. /**
  137. * Show communication raw data to debug info
  138. * @param data: raw data
  139. * @param len: data length
  140. * @param isRX: is receive data
  141. */
  142. void show_raw(uint8_t *data, uint16_t len, uint8_t isRX)
  143. {
  144. uint8_t output[512]={0};
  145. if(isRX)
  146. DEBUG_INFO("RX ---------------------------------------------\n");
  147. else
  148. DEBUG_INFO("TX ---------------------------------------------\n");
  149. DEBUG_INFO(" 0 1 2 3 4 5 6 7 8 9 A B C D E F \n");
  150. DEBUG_INFO("------------------------------------------------\n");
  151. memset(output, 0x00, ARRAY_SIZE(output));
  152. for(int idx=0;idx<len;idx++)
  153. {
  154. if(strlen((char*)output)<48)
  155. sprintf((char*)output, "%s%02X ", output, data[idx]);
  156. else
  157. {
  158. DEBUG_INFO("%s\n", output);
  159. memset(output, 0x00, ARRAY_SIZE(output));
  160. sprintf((char*)output, "%s%02X ", output, data[idx]);
  161. }
  162. }
  163. DEBUG_INFO("%s\n", output);
  164. }
  165. /**
  166. *
  167. * @param dec: number in dec
  168. * @return number in bcd
  169. */
  170. int decTobcd(int dec)
  171. {
  172. return (dec/10 * 16)+ (dec%10);
  173. }
  174. /**
  175. *
  176. * @param data: message array
  177. * @param dataLen: command & data field length in array
  178. * @return check sum result
  179. */
  180. int calChksum(unsigned char *data)
  181. {
  182. int result = 0;
  183. for(uint16_t idx=1;idx<(((data[1] << 8) | (data[2]))+3); idx++)
  184. {
  185. result ^= data[idx];
  186. }
  187. // printf("\n\rThe checksum : %x \n\r", result);
  188. return (result&0xff);
  189. }
  190. //==========================================
  191. // Init share memory
  192. //==========================================
  193. /**
  194. * Share memory initialization
  195. * @return function result
  196. */
  197. int InitShareMemory()
  198. {
  199. int result = PASS;
  200. int MeterSMId;
  201. #ifndef X86
  202. //init ShmSysConfigAndInfo
  203. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  204. {
  205. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  206. result = FAIL;
  207. }
  208. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  209. {
  210. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  211. result = FAIL;
  212. }
  213. else
  214. {}
  215. //init ShmStatusCodeData
  216. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  217. {
  218. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  219. result = FAIL;
  220. }
  221. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  222. {
  223. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  224. result = FAIL;
  225. }
  226. else
  227. {}
  228. //init ShmOCPP16Data
  229. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  230. {
  231. DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
  232. result = FAIL;
  233. }
  234. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  235. {
  236. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  237. result = FAIL;
  238. }
  239. else
  240. {}
  241. #endif
  242. return result;
  243. }
  244. //==========================================
  245. // Init com port
  246. //==========================================
  247. /**
  248. * TTY port initialization
  249. * @return port initial result
  250. */
  251. int InitComPort()
  252. {
  253. int fd;
  254. struct termios tios;
  255. fd = open(TTY_PORT, O_RDWR);
  256. if(fd<=0)
  257. {
  258. return FAIL;
  259. }
  260. ioctl (fd, TCGETS, &tios);
  261. tios.c_cflag = B9600| CS8 | CLOCAL | CREAD;
  262. tios.c_lflag = 0;
  263. tios.c_iflag = 0;
  264. tios.c_oflag = 0;
  265. tios.c_cc[VMIN]=0; // data length threshold, 0 bytes
  266. tios.c_cc[VTIME]=(unsigned char)5; // timeout threshold, 0.5 seconds
  267. tios.c_lflag=0;
  268. tcflush(fd, TCIFLUSH);
  269. ioctl (fd, TCSETS, &tios);
  270. return fd;
  271. }
  272. /**
  273. *
  274. * @param serial port handle
  275. * @param request message data
  276. * @param response message data
  277. * @return
  278. */
  279. int pollingRequest(int uart, unsigned char* cmd)
  280. {
  281. int result=FAIL;
  282. int length;
  283. tcflush(uart,TCIOFLUSH);
  284. length = ((cmd[1]<<8)|cmd[2])+4;
  285. cmd[(length-1)] = calChksum(cmd);
  286. show_raw(cmd, length, NO);
  287. if(write(uart, cmd, length ) > 0)
  288. {
  289. result = PASS;
  290. }
  291. else
  292. {
  293. DEBUG_ERROR("pollingRequest fail.\n");
  294. }
  295. return result;
  296. }
  297. /**
  298. *
  299. * @param uart
  300. * @param rx
  301. * @return
  302. */
  303. int pollingResponse(int uart, unsigned char* rx)
  304. {
  305. int result =FAIL;
  306. int len = 0;
  307. int Total_len[2] = {0, 0};
  308. int CSUM[2]={0,0};
  309. uint8_t buf[512];
  310. // DEBUG_INFO("\n\r===== Prepare to read serial data. ===============\n\r");
  311. do {
  312. len =read(uart, buf, 512);
  313. if(len == 0)
  314. break;
  315. // printf ("This is a string %d chars long\n", len);
  316. for (int i=0; i<len; i++)
  317. {
  318. rx[(Total_len[0]+i)] =buf[i];
  319. }
  320. memcpy(&rx[(Total_len[0])], buf, len);
  321. if(Total_len[0] == 0)
  322. Total_len[1] = ((buf[1] << 8) | (buf[2]))+4; // lead 3 + csum
  323. Total_len[0] = Total_len[0] + len;
  324. usleep(10);
  325. } while (Total_len[0] < Total_len[1]);
  326. if(Total_len[0] >0)
  327. {
  328. show_raw(rx, Total_len[1], YES);
  329. CSUM[0] = rx[(((rx[1] << 8) | (rx[2]))+3)];
  330. CSUM[1] = calChksum(&rx[0]);
  331. if(CSUM[0] == CSUM[1])
  332. {
  333. result = PASS;
  334. DEBUG_INFO("\n\r===== Receive data checksum (%x , %x) OK. ===============\n\r", CSUM[0], CSUM[1]);
  335. }
  336. else
  337. {
  338. DEBUG_INFO("\n\r===== Receive data checksum (%x , %x) Fail. ===============\n\r", CSUM[0], CSUM[1]);
  339. }
  340. }
  341. return result;
  342. }
  343. /**
  344. *
  345. * @param uart
  346. * @return
  347. */
  348. int getInputBufferCount(int uart)
  349. {
  350. int bytes_avail;
  351. ioctl(uart, FIONREAD, &bytes_avail);
  352. return bytes_avail;
  353. }
  354. /**
  355. *
  356. * @param node
  357. * @param node_name
  358. * @return
  359. */
  360. xmlNode *find_node(xmlNode *node, char *node_name)
  361. {
  362. xmlNode *result;
  363. if (node == NULL) return NULL;
  364. while(node)
  365. {
  366. if((node->type == XML_ELEMENT_NODE) && (strcmp((char*)node->name, node_name) == 0)) return node;
  367. if((result = find_node(node->children, node_name))) return result;
  368. node = node->next;
  369. }
  370. return NULL;
  371. }
  372. //==========================================
  373. // Reader operation function
  374. //==========================================
  375. int preAuthRequest(int uart)
  376. {
  377. char xmlBody[1024]={0};
  378. int n;
  379. xmlBuffer * outputXmlPtr = xmlBufferCreate();
  380. xmlDocPtr doc = NULL;
  381. xmlNodePtr node_req = NULL;
  382. xmlNodePtr node_cmd = NULL;
  383. xmlNodePtr node_param = NULL;
  384. xmlNodePtr node_param_Txn = NULL;
  385. // Create xml message
  386. doc = xmlNewDoc(BAD_CAST "1.0");
  387. node_req = xmlNewNode(NULL, BAD_CAST "Req");
  388. xmlDocSetRootElement(doc, node_req);
  389. node_cmd = xmlNewNode(NULL, BAD_CAST "Cmd");
  390. xmlNewChild(node_cmd, NULL, BAD_CAST "CmdId", BAD_CAST "TxnStart");
  391. xmlNewChild(node_cmd, NULL, BAD_CAST "CmdTout", BAD_CAST "30");
  392. xmlAddChild(node_req, node_cmd);
  393. node_param = xmlNewNode(NULL, BAD_CAST "Param");
  394. node_param_Txn = xmlNewNode(NULL, BAD_CAST "Txn");
  395. xmlNewChild(node_param_Txn, NULL, BAD_CAST "TxnType", BAD_CAST "AuthOnly");
  396. xmlNewChild(node_param_Txn, NULL, BAD_CAST "AccType", BAD_CAST "Default");
  397. xmlNewChild(node_param_Txn, NULL, BAD_CAST "CurrCode", BAD_CAST ISO_4217_Currency_Code[ShmSysConfigAndInfo->SysConfig.BillingData.Currency]);
  398. //xmlNewChild(node_param_Txn, NULL, BAD_CAST "CurrCode", BAD_CAST "840");
  399. xmlNewChild(node_param_Txn, NULL, BAD_CAST "TxnAmt", BAD_CAST ShmOCPP16Data->ConfigurationTable.CoreProfile[PreAuthAmount].ItemData);
  400. //xmlNewChild(node_param_Txn, NULL, BAD_CAST "TxnAmt", BAD_CAST "0001.00");
  401. xmlAddChild(node_param, node_param_Txn);
  402. xmlAddChild(node_req, node_param);
  403. xmlNodeDump(outputXmlPtr, NULL, (xmlNode *)node_req, 0, 0);
  404. n = sprintf(xmlBody, "%s", (const char *)outputXmlPtr->content);
  405. printf ("[%s] is a string %d chars long\n", xmlBody, n);
  406. xmlBufferFree(outputXmlPtr);
  407. xmlFreeDoc(doc);
  408. xmlCleanupParser();
  409. xmlMemoryDump();
  410. // Create protocol message
  411. memset(commandRaw.requestData, 0x00, ARRAY_SIZE(commandRaw.requestData));
  412. commandRaw.requestData[0] = STX;
  413. commandRaw.requestData[1] = (strlen(xmlBody)>>8)&0xff;
  414. commandRaw.requestData[2] = (strlen(xmlBody)>>0)&0xff;
  415. memcpy(&commandRaw.requestData[3], xmlBody, strlen(xmlBody));
  416. show_raw(&commandRaw.requestData[0], (strlen(xmlBody)+3), NO);
  417. return pollingRequest(uart, commandRaw.requestData);
  418. }
  419. int preAuthResponse(int uart)
  420. {
  421. int result = pollingResponse(uart, commandRaw.responseData);
  422. //char *xmlTestBody = "<Event><MesgId>21</MesgId><MesgStr>PLEASE PRESENT CARD</MesgStr></Event>";
  423. //char *xmlTestBody = "<Event><MesgId>27</MesgId><MesgStr>AUTHORIZING. PLEASE WAIT...</MesgStr></Event>";
  424. //char *xmlTestBody = "<Resp><Cmd><CmdId>TxnStartResp</CmdId><StatusCode>001F</StatusCode><StatusText>TransactionDeferred</StatusText></Cmd><Data><Txn><TxnId>62</TxnId></Txn></Data></Resp>";
  425. char xmlTestBody[1024] = {0};
  426. if(result == PASS)
  427. {
  428. memcpy(xmlTestBody, &commandRaw.responseData[3], ((commandRaw.responseData[1] << 8) | commandRaw.responseData[2]));
  429. xmlDocPtr doc = xmlParseDoc((const xmlChar *)xmlTestBody);
  430. xmlNode *root_element = xmlDocGetRootElement(doc);
  431. if(find_node(root_element, "Event") != NULL)
  432. {
  433. DEBUG_INFO("===== Get event =======================\n");
  434. ShmSysConfigAndInfo->SysInfo.bazel8.event.messageId = atoi((char*)xmlNodeGetContent(find_node(root_element, "MesgId")));
  435. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.event.messageString, "%s", xmlNodeGetContent(find_node(root_element, "MesgId")));
  436. ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = ON;
  437. DEBUG_INFO("Message id: %d\n", ShmSysConfigAndInfo->SysInfo.bazel8.event.messageId);
  438. DEBUG_INFO("%s: %s\n", root_element->children->next->name, xmlNodeGetContent(root_element->children->next));
  439. }
  440. else if((find_node(root_element, "Resp") != NULL) && (strstr((char*)xmlNodeGetContent(find_node(root_element, "CmdId")), "TxnStartResp") != NULL))
  441. {
  442. DEBUG_INFO("===== Get response =======================\n");
  443. ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn = ON;
  444. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode, "%s", xmlNodeGetContent(find_node(root_element, "StatusCode")));
  445. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusText, "%s", xmlNodeGetContent(find_node(root_element, "StatusText")));
  446. DEBUG_INFO("Status code: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode);
  447. DEBUG_INFO("Status text: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusText);
  448. if(strstr(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode,"0000") != NULL)
  449. {
  450. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId, "%s", xmlNodeGetContent(find_node(root_element, "TxnId")));
  451. DEBUG_INFO("TxnId: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId);
  452. }
  453. }
  454. else
  455. {
  456. DEBUG_INFO("===== Get unknown node =======================\n");
  457. }
  458. //DEBUG_INFO("===== End 0 =======================\n");
  459. xmlFreeDoc(doc);
  460. //DEBUG_INFO("===== End 1 =======================\n");
  461. xmlCleanupParser();
  462. //DEBUG_INFO("===== End 2 =======================\n");
  463. xmlMemoryDump();
  464. //DEBUG_INFO("===== End 3 =======================\n");
  465. }
  466. else
  467. {
  468. DEBUG_INFO("===== Get preAuth Response is Fail =====( %d)==========\n", nSerial++);
  469. nSerial %= 0xffff;
  470. }
  471. return result;
  472. }
  473. //==========================================
  474. // Main loop
  475. //==========================================
  476. int main(void)
  477. {
  478. int UartFd;
  479. int preAuthRetry = 0;
  480. int cmdRepeatCount = 0;
  481. // Henry
  482. int state[2] = {s_IDLE, s_IDLE};
  483. //uint8_t Rx_Cir_Buf[1024];
  484. //uint8_t Rx_Buf[1024];
  485. //int Rx_Cir_Idx[2], Rx_Buf_Idx;
  486. //===============================================
  487. // Initialization
  488. //===============================================
  489. // Rx_Cir_Idx[0] = Rx_Cir_Idx[1] = Rx_Buf_Idx = 0;
  490. //
  491. // for (int i=0; i<1024; i++)
  492. // {
  493. // Rx_Cir_Buf[i] =Rx_Buf[i] =0;
  494. // }
  495. #ifndef X86
  496. if(InitShareMemory() == FAIL)
  497. {
  498. DEBUG_ERROR("InitShareMemory NG\n");
  499. if(ShmStatusCodeData!=NULL)
  500. {
  501. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  502. }
  503. sleep(5);
  504. return FAIL;
  505. }
  506. #endif
  507. UartFd=InitComPort();
  508. if(UartFd<0)
  509. {
  510. DEBUG_ERROR("InitComPort NG\n");
  511. if(ShmStatusCodeData!=NULL)
  512. {
  513. #ifndef X86
  514. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  515. #endif
  516. }
  517. sleep(5);
  518. return FAIL;
  519. }
  520. else
  521. {
  522. DEBUG_INFO("%s port open success.\n", TTY_PORT);
  523. }
  524. DEBUG_INFO("Payment module initialize completed.\n");
  525. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = true; // from CSU
  526. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes = false;
  527. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  528. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = false;
  529. //===============================================
  530. // Main loop
  531. //===============================================
  532. while (1) {
  533. // if(state[0] != state[1])
  534. // {
  535. // state[1] = 1;
  536. //
  537. // preAuthRequest(UartFd);
  538. //
  539. // }
  540. //
  541. // if(state[1] == 1)
  542. // {
  543. // // pollingResponse(UartFd, Rx_Buf);
  544. // preAuthResponse(UartFd);
  545. // }
  546. /*
  547. CSU 將 cmdPreAuth.isReq 設定 true 的同時也要將 cmdPreAuth.isRes 設定為 false,然後等 cmdPreAuth.isRes 為 true 才表示交易已完成。
  548. */
  549. switch(state[0])
  550. {
  551. case s_IDLE:
  552. if(state[0] != state[1])
  553. {
  554. state[1] = state[0];
  555. DEBUG_INFO(" >> Back to Idle.\n");
  556. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  557. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  558. //ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = false; // The CSU should be set to false.
  559. }
  560. if(ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq)
  561. {
  562. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  563. state[0] = s_SEND_AUTH_ONLY;
  564. }
  565. break;
  566. case s_SEND_AUTH_ONLY:
  567. if(state[0] != state[1])
  568. {
  569. state[1] = state[0];
  570. if(preAuthRequest(UartFd))
  571. {
  572. refreshStartTimer(&tmr[TIMER_READ_RESPONSE]);
  573. cmdRepeatCount = 0;
  574. state[0] = s_WAIT_RESPONE;
  575. }
  576. if(cmdRepeatCount > SPEC_REQUEST_RETRY)
  577. {
  578. DEBUG_WARN("preAuthRequest fail over count(%d).", cmdRepeatCount);
  579. cmdRepeatCount = 0;
  580. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  581. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  582. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true; // To tell CSU is done.
  583. state[0] = s_IDLE;
  584. }
  585. else
  586. {
  587. cmdRepeatCount++;
  588. }
  589. }
  590. break;
  591. case s_WAIT_RESPONE:
  592. if(state[0] != state[1])
  593. {
  594. state[1] = state[0];
  595. ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn = OFF;
  596. ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = OFF;
  597. nSerial = 0;
  598. }
  599. do {
  600. if( preAuthResponse(UartFd) )
  601. {
  602. if(ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn)
  603. {
  604. // * 1. Announce CSU event content.
  605. ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = false;
  606. // if(ShmSysConfigAndInfo->SysInfo.bazel8.event.messageId == 21) // Please Present Card.
  607. // refreshStartTimer(&tmr[TIMER_READ_RESPONSE]);
  608. }
  609. else if(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn)
  610. {
  611. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = \
  612. ((strstr(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode, "0000") != NULL)?true:false);
  613. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true; // To tell CSU is done.
  614. }
  615. }
  616. if((getDiffSecNow(tmr[TIMER_READ_RESPONSE]) > TIMEOUT_REQUEST))
  617. {
  618. DEBUG_WARN("Wait pre auth response timeout(%d secs).\n", TIMEOUT_REQUEST);
  619. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  620. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true; // To tell CSU is done.
  621. break;
  622. }
  623. } while (ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn != ON);
  624. state[0] = s_IDLE;
  625. break;
  626. default:
  627. state[0] = s_IDLE;
  628. break;
  629. }
  630. }
  631. return FAIL;
  632. for(;;)
  633. {
  634. // need to run the script first : run_evse_stop.sh
  635. if(ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq)
  636. {
  637. // Pre auth request
  638. if(!ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes)
  639. {
  640. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = false;
  641. ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = false;
  642. ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn = false;
  643. if(preAuthRequest(UartFd))
  644. {
  645. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes = true;
  646. cmdRepeatCount = 0;
  647. refreshStartTimer(&tmr[TIMER_READ_RESPONSE]);
  648. }
  649. if(cmdRepeatCount > SPEC_REQUEST_RETRY)
  650. {
  651. DEBUG_WARN("preAuthRequest fail over count(%d).", cmdRepeatCount);
  652. cmdRepeatCount = 0;
  653. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  654. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  655. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true;
  656. }
  657. else
  658. {
  659. cmdRepeatCount += 1;
  660. }
  661. }
  662. else
  663. {
  664. if((getInputBufferCount(UartFd) >= RAW_DATA_LENGTH))
  665. {
  666. //DEBUG_INFO("\n\rTo wait Payment preAuth Response.\n\r");
  667. if(preAuthResponse(UartFd))
  668. {
  669. if(ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn)
  670. {
  671. // * 1. Announce CSU event content.
  672. ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = false;
  673. }
  674. else if(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn)
  675. {
  676. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = ((strstr(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode, "0000") != NULL)?true:false);
  677. if(ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass || (preAuthRetry++ >= 3))
  678. {
  679. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  680. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true;
  681. preAuthRetry = 0;
  682. }
  683. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes = false;
  684. }
  685. }
  686. }
  687. if((getDiffSecNow(tmr[TIMER_READ_RESPONSE]) > TIMEOUT_REQUEST))
  688. {
  689. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  690. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes = false;
  691. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  692. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true;
  693. DEBUG_WARN("Wait pre auth response timeout(%d secs).", TIMEOUT_REQUEST);
  694. }
  695. }
  696. }
  697. sleep(3);
  698. }
  699. return FAIL;
  700. }