Module_Payment_Bazel8.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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=0;
  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 InfoMgmtRequest(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_Info = 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 "InfoMgmt");
  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_Info = xmlNewNode(NULL, BAD_CAST "Info");
  395. xmlNewChild(node_param_Info, NULL, BAD_CAST "Id", BAD_CAST "GetSystemVer");
  396. xmlAddChild(node_param, node_param_Info);
  397. xmlAddChild(node_req, node_param);
  398. xmlNodeDump(outputXmlPtr, NULL, (xmlNode *)node_req, 0, 0);
  399. n = sprintf(xmlBody, "%s", (const char *)outputXmlPtr->content);
  400. DEBUG_INFO("[%s] is a string %d chars long\n", xmlBody, n);
  401. xmlBufferFree(outputXmlPtr);
  402. xmlFreeDoc(doc);
  403. xmlCleanupParser();
  404. xmlMemoryDump();
  405. // Create protocol message
  406. memset(commandRaw.requestData, 0x00, ARRAY_SIZE(commandRaw.requestData));
  407. commandRaw.requestData[0] = STX;
  408. commandRaw.requestData[1] = (strlen(xmlBody)>>8)&0xff;
  409. commandRaw.requestData[2] = (strlen(xmlBody)>>0)&0xff;
  410. memcpy(&commandRaw.requestData[3], xmlBody, strlen(xmlBody));
  411. //show_raw(&commandRaw.requestData[0], (strlen(xmlBody)+3), NO);
  412. return pollingRequest(uart, commandRaw.requestData);
  413. }
  414. int preAuthRequest(int uart)
  415. {
  416. char xmlBody[1024]={0};
  417. int n;
  418. xmlBuffer * outputXmlPtr = xmlBufferCreate();
  419. xmlDocPtr doc = NULL;
  420. xmlNodePtr node_req = NULL;
  421. xmlNodePtr node_cmd = NULL;
  422. xmlNodePtr node_param = NULL;
  423. xmlNodePtr node_param_Txn = NULL;
  424. // Create xml message
  425. doc = xmlNewDoc(BAD_CAST "1.0");
  426. node_req = xmlNewNode(NULL, BAD_CAST "Req");
  427. xmlDocSetRootElement(doc, node_req);
  428. node_cmd = xmlNewNode(NULL, BAD_CAST "Cmd");
  429. xmlNewChild(node_cmd, NULL, BAD_CAST "CmdId", BAD_CAST "TxnStart");
  430. xmlNewChild(node_cmd, NULL, BAD_CAST "CmdTout", BAD_CAST "30");
  431. xmlAddChild(node_req, node_cmd);
  432. node_param = xmlNewNode(NULL, BAD_CAST "Param");
  433. node_param_Txn = xmlNewNode(NULL, BAD_CAST "Txn");
  434. xmlNewChild(node_param_Txn, NULL, BAD_CAST "TxnType", BAD_CAST "AuthOnly");
  435. xmlNewChild(node_param_Txn, NULL, BAD_CAST "AccType", BAD_CAST "Default");
  436. xmlNewChild(node_param_Txn, NULL, BAD_CAST "CurrCode", BAD_CAST ISO_4217_Currency_Code[ShmSysConfigAndInfo->SysConfig.BillingData.Currency]);
  437. //xmlNewChild(node_param_Txn, NULL, BAD_CAST "CurrCode", BAD_CAST "840");
  438. xmlNewChild(node_param_Txn, NULL, BAD_CAST "TxnAmt", BAD_CAST ShmOCPP16Data->ConfigurationTable.CoreProfile[PreAuthAmount].ItemData);
  439. //xmlNewChild(node_param_Txn, NULL, BAD_CAST "TxnAmt", BAD_CAST "0001.00");
  440. xmlAddChild(node_param, node_param_Txn);
  441. xmlAddChild(node_req, node_param);
  442. xmlNodeDump(outputXmlPtr, NULL, (xmlNode *)node_req, 0, 0);
  443. n = sprintf(xmlBody, "%s", (const char *)outputXmlPtr->content);
  444. DEBUG_INFO("[%s] is a string %d chars long\n", xmlBody, n);
  445. xmlBufferFree(outputXmlPtr);
  446. xmlFreeDoc(doc);
  447. xmlCleanupParser();
  448. xmlMemoryDump();
  449. // Create protocol message
  450. memset(commandRaw.requestData, 0x00, ARRAY_SIZE(commandRaw.requestData));
  451. commandRaw.requestData[0] = STX;
  452. commandRaw.requestData[1] = (strlen(xmlBody)>>8)&0xff;
  453. commandRaw.requestData[2] = (strlen(xmlBody)>>0)&0xff;
  454. memcpy(&commandRaw.requestData[3], xmlBody, strlen(xmlBody));
  455. //show_raw(&commandRaw.requestData[0], (strlen(xmlBody)+3), NO);
  456. return pollingRequest(uart, commandRaw.requestData);
  457. }
  458. int preAuthResponse(int uart)
  459. {
  460. int result = pollingResponse(uart, commandRaw.responseData);
  461. //char *xmlTestBody = "<Event><MesgId>21</MesgId><MesgStr>PLEASE PRESENT CARD</MesgStr></Event>";
  462. //char *xmlTestBody = "<Event><MesgId>27</MesgId><MesgStr>AUTHORIZING. PLEASE WAIT...</MesgStr></Event>";
  463. //char *xmlTestBody = "<Resp><Cmd><CmdId>TxnStartResp</CmdId><StatusCode>001F</StatusCode><StatusText>TransactionDeferred</StatusText></Cmd><Data><Txn><TxnId>62</TxnId></Txn></Data></Resp>";
  464. char xmlTestBody[1024] = {0};
  465. if(result == PASS)
  466. {
  467. memcpy(xmlTestBody, &commandRaw.responseData[3], ((commandRaw.responseData[1] << 8) | commandRaw.responseData[2]));
  468. xmlDocPtr doc = xmlParseDoc((const xmlChar *)xmlTestBody);
  469. xmlNode *root_element = xmlDocGetRootElement(doc);
  470. if(find_node(root_element, "Event") != NULL)
  471. {
  472. DEBUG_INFO("===== Get event =======================\n");
  473. ShmSysConfigAndInfo->SysInfo.bazel8.event.messageId = atoi((char*)xmlNodeGetContent(find_node(root_element, "MesgId")));
  474. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.event.messageString, "%s", xmlNodeGetContent(find_node(root_element, "MesgId")));
  475. ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = ON;
  476. DEBUG_INFO("Message id: %d\n", ShmSysConfigAndInfo->SysInfo.bazel8.event.messageId);
  477. DEBUG_INFO("%s: %s\n", root_element->children->next->name, xmlNodeGetContent(root_element->children->next));
  478. }
  479. else if((find_node(root_element, "Resp") != NULL) && (strstr((char*)xmlNodeGetContent(find_node(root_element, "CmdId")), "TxnStartResp") != NULL))
  480. {
  481. DEBUG_INFO("===== Get response =======================\n");
  482. ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn = ON;
  483. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode, "%s", xmlNodeGetContent(find_node(root_element, "StatusCode")));
  484. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusText, "%s", xmlNodeGetContent(find_node(root_element, "StatusText")));
  485. DEBUG_INFO("Status code: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode);
  486. DEBUG_INFO("Status text: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusText);
  487. if(strstr(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode,"0000") != NULL)
  488. {
  489. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId, "%s", xmlNodeGetContent(find_node(root_element, "TxnId")));
  490. DEBUG_INFO("TxnId: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId);
  491. }
  492. }
  493. else
  494. {
  495. DEBUG_INFO("===== Get unknown node =======================\n");
  496. }
  497. //DEBUG_INFO("===== End 0 =======================\n");
  498. xmlFreeDoc(doc);
  499. //DEBUG_INFO("===== End 1 =======================\n");
  500. xmlCleanupParser();
  501. //DEBUG_INFO("===== End 2 =======================\n");
  502. xmlMemoryDump();
  503. //DEBUG_INFO("===== End 3 =======================\n");
  504. }
  505. else
  506. {
  507. if((nSerial++ % 5)==0)
  508. DEBUG_INFO("===== Get preAuth Response is Fail =====( %d)==========\n", (nSerial-1));
  509. nSerial %= 0xffff;
  510. }
  511. return result;
  512. }
  513. int InfoMgmtResponse(int uart)
  514. {
  515. int result = pollingResponse(uart, commandRaw.responseData);
  516. //char *xmlTestBody = "<Resp><Cmd><CmdId>InfoMgmtResp</CmdId><StatusCode>001F</StatusCode><StatusText>TransactionDeferred</StatusText></Cmd>
  517. // <Data><Info><Id>InfoMgmt</Id><SysVer>[System Firmware Version]</SysVer>
  518. // <AppVer>[Application Version]</AppVer><SerialNum>[Serial Number]</SerialNum></Info></Data></Resp>";
  519. char xmlTestBody[1024] = {0};
  520. if(result == PASS)
  521. {
  522. memcpy(xmlTestBody, &commandRaw.responseData[3], ((commandRaw.responseData[1] << 8) | commandRaw.responseData[2]));
  523. xmlDocPtr doc = xmlParseDoc((const xmlChar *)xmlTestBody);
  524. xmlNode *root_element = xmlDocGetRootElement(doc);
  525. if((find_node(root_element, "Resp") != NULL) && (strstr((char*)xmlNodeGetContent(find_node(root_element, "CmdId")), "InfoMgmtResp") != NULL))
  526. {
  527. DEBUG_INFO("===== Get InfoMgmt response =======================\n");
  528. // ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn = ON;
  529. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode, "%s", xmlNodeGetContent(find_node(root_element, "StatusCode")));
  530. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusText, "%s", xmlNodeGetContent(find_node(root_element, "StatusText")));
  531. DEBUG_INFO("Status code: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode);
  532. DEBUG_INFO("Status text: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusText);
  533. if(strstr(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode,"0000") != NULL)
  534. {
  535. ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn = ON;
  536. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId, "%s", xmlNodeGetContent(find_node(root_element, "SysVer")));
  537. DEBUG_INFO("System Firmware Version: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId);
  538. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId, "%s", xmlNodeGetContent(find_node(root_element, "AppVer")));
  539. DEBUG_INFO("Application Version: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId);
  540. sprintf(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId, "%s", xmlNodeGetContent(find_node(root_element, "SerialNum")));
  541. DEBUG_INFO("Serial Number: %s\n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId);
  542. }
  543. }
  544. else
  545. {
  546. DEBUG_INFO("===== Get unknown node =======================\n");
  547. }
  548. //DEBUG_INFO("===== End 0 =======================\n");
  549. xmlFreeDoc(doc);
  550. //DEBUG_INFO("===== End 1 =======================\n");
  551. xmlCleanupParser();
  552. //DEBUG_INFO("===== End 2 =======================\n");
  553. xmlMemoryDump();
  554. //DEBUG_INFO("===== End 3 =======================\n");
  555. }
  556. else
  557. {
  558. if((nSerial++ % 5)==0)
  559. DEBUG_INFO("===== Get InfoMgmt Response is Fail =====( %d)==========\n", (nSerial-1));
  560. nSerial %= 0xffff;
  561. }
  562. return result;
  563. }
  564. //==========================================
  565. // Main loop
  566. //==========================================
  567. int main(void)
  568. {
  569. int UartFd;
  570. //int preAuthRetry = 0;
  571. int cmdRepeatCount = 0;
  572. // Henry
  573. int state[2] = {s_NONE, s_NONE};
  574. //uint8_t Rx_Cir_Buf[1024];
  575. //uint8_t Rx_Buf[1024];
  576. //int Rx_Cir_Idx[2], Rx_Buf_Idx;
  577. //===============================================
  578. // Initialization
  579. //===============================================
  580. // Rx_Cir_Idx[0] = Rx_Cir_Idx[1] = Rx_Buf_Idx = 0;
  581. //
  582. // for (int i=0; i<1024; i++)
  583. // {
  584. // Rx_Cir_Buf[i] =Rx_Buf[i] =0;
  585. // }
  586. #ifndef X86
  587. if(InitShareMemory() == FAIL)
  588. {
  589. DEBUG_ERROR("InitShareMemory NG\n");
  590. if(ShmStatusCodeData!=NULL)
  591. {
  592. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  593. }
  594. sleep(5);
  595. return FAIL;
  596. }
  597. #endif
  598. UartFd=InitComPort();
  599. if(UartFd<0)
  600. {
  601. DEBUG_ERROR("InitComPort NG\n");
  602. if(ShmStatusCodeData!=NULL)
  603. {
  604. #ifndef X86
  605. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  606. #endif
  607. }
  608. sleep(5);
  609. return FAIL;
  610. }
  611. else
  612. {
  613. DEBUG_INFO("%s port open success.\n", TTY_PORT);
  614. }
  615. DEBUG_INFO("Payment module initialize completed.\n");
  616. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false; // from CSU
  617. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes = false;
  618. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  619. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = false;
  620. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PaymentCommTimeout = false;
  621. //===============================================
  622. // Main loop
  623. //===============================================
  624. while (1) {
  625. // if(state[0] != state[1])
  626. // {
  627. // state[1] = 1;
  628. //
  629. // preAuthRequest(UartFd);
  630. //
  631. // }
  632. //
  633. // if(state[1] == 1)
  634. // {
  635. // // pollingResponse(UartFd, Rx_Buf);
  636. // preAuthResponse(UartFd);
  637. // }
  638. /*
  639. CSU 將 cmdPreAuth.isReq 設定 true 的同時也要將 cmdPreAuth.isRes 設定為 false,然後等 cmdPreAuth.isRes 為 true 才表示交易已完成。
  640. */
  641. switch(state[0])
  642. {
  643. case s_NONE:
  644. state[0] = s_INFO;
  645. state[1] = s_NONE;
  646. DEBUG_INFO(">> PayError: %d\n", ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PaymentCommTimeout);
  647. break;
  648. case s_INFO:
  649. if(state[0] != state[1])
  650. {
  651. state[1] = state[0];
  652. cmdRepeatCount = 0;
  653. DEBUG_INFO("Perpare Selftest...\n");
  654. }
  655. if(InfoMgmtRequest(UartFd))
  656. {
  657. cmdRepeatCount = 0;
  658. refreshStartTimer(&tmr[TIMER_READ_RESPONSE]);
  659. state[0] = s_INFO_WAIT_RESPONE;
  660. }
  661. else
  662. {
  663. cmdRepeatCount++;
  664. if(cmdRepeatCount > SPEC_REQUEST_RETRY)
  665. {
  666. DEBUG_WARN("preAuthRequest fail over count(%d).", cmdRepeatCount);
  667. cmdRepeatCount = 0;
  668. // Raise a error for Payment Task.
  669. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PaymentCommTimeout = true;
  670. DEBUG_INFO(" Raise a error for Payment Task...\n");
  671. DEBUG_INFO(">> PayError: %d\n", ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PaymentCommTimeout);
  672. state[0] = s_NONE;
  673. }
  674. }
  675. break;
  676. case s_INFO_WAIT_RESPONE:
  677. if(state[0] != state[1])
  678. {
  679. state[1] = state[0];
  680. ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn = OFF;
  681. nSerial = 0;
  682. DEBUG_INFO("Wait Selftest Respone...\n");
  683. }
  684. do {
  685. if( InfoMgmtResponse(UartFd) )
  686. {
  687. if(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn)
  688. {
  689. state[0] = s_IDLE;
  690. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PaymentCommTimeout = false;
  691. }
  692. }
  693. if((getDiffSecNow(tmr[TIMER_READ_RESPONSE]) > TIMEOUT_REQUEST))
  694. {
  695. DEBUG_WARN("Wait pre auth response timeout(%d secs).\n", TIMEOUT_REQUEST);
  696. // Raise a error for Payment Task.
  697. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PaymentCommTimeout = true;
  698. DEBUG_INFO(" Raise a error for Payment Task...\n");
  699. DEBUG_INFO(">> PayError: %d\n", ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PaymentCommTimeout);
  700. state[0] = s_NONE;
  701. break;
  702. }
  703. } while (ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn != ON);
  704. break;
  705. case s_IDLE:
  706. if(state[0] != state[1])
  707. {
  708. state[1] = state[0];
  709. DEBUG_INFO(" Enter the Idle state...\n");
  710. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  711. DEBUG_INFO(">> isResultPass: %d\n", ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass);
  712. DEBUG_INFO(">> isRes: %d\n", ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes);
  713. }
  714. if(ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq)
  715. {
  716. DEBUG_INFO(" Raise the isReq flag...\n");
  717. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  718. strcpy((char *)ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId, "");
  719. strcpy((char *)ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode, "");
  720. strcpy((char *)ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusText, "");
  721. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  722. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = false; // The CSU should be set to false.
  723. state[0] = s_SEND_AUTH_ONLY;
  724. }
  725. break;
  726. case s_SEND_AUTH_ONLY:
  727. if(state[0] != state[1])
  728. {
  729. state[1] = state[0];
  730. cmdRepeatCount = 0;
  731. DEBUG_INFO(" Send Auth-Only state...\n");
  732. }
  733. if(preAuthRequest(UartFd))
  734. {
  735. refreshStartTimer(&tmr[TIMER_READ_RESPONSE]);
  736. cmdRepeatCount = 0;
  737. state[0] = s_WAIT_RESPONE;
  738. }
  739. else
  740. {
  741. cmdRepeatCount++;
  742. if(cmdRepeatCount > SPEC_REQUEST_RETRY)
  743. {
  744. DEBUG_WARN("preAuthRequest fail over count(%d).", cmdRepeatCount);
  745. cmdRepeatCount = 0;
  746. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  747. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  748. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true; // To tell CSU is done.
  749. state[0] = s_IDLE;
  750. }
  751. }
  752. break;
  753. case s_WAIT_RESPONE:
  754. if(state[0] != state[1])
  755. {
  756. state[1] = state[0];
  757. ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn = OFF;
  758. ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = OFF;
  759. nSerial = 0;
  760. DEBUG_INFO(" Wait Auth-Only Respone state...\n");
  761. }
  762. do {
  763. if( preAuthResponse(UartFd) )
  764. {
  765. if(ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn)
  766. {
  767. // * 1. Announce CSU event content.
  768. ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = false;
  769. }
  770. else if(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn)
  771. {
  772. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = \
  773. ((strstr(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode, "0000") != NULL)?true:false);
  774. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true; // To tell CSU is done.
  775. DEBUG_INFO(">> isResultPass: %d\n", ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass);
  776. DEBUG_INFO(">> isRes: %d\n", ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes);
  777. }
  778. }
  779. if((getDiffSecNow(tmr[TIMER_READ_RESPONSE]) > TIMEOUT_REQUEST))
  780. {
  781. DEBUG_WARN("Wait pre auth response timeout(%d secs).\n", TIMEOUT_REQUEST);
  782. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  783. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true; // To tell CSU is done.
  784. break;
  785. }
  786. } while (ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn != ON);
  787. state[0] = s_IDLE;
  788. break;
  789. default:
  790. state[0] = s_IDLE;
  791. break;
  792. }
  793. }
  794. return FAIL;
  795. // for(;;)
  796. // {
  797. //
  798. // // need to run the script first : run_evse_stop.sh
  799. //
  800. // if(ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq)
  801. // {
  802. //
  803. // // Pre auth request
  804. // if(!ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes)
  805. // {
  806. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = false;
  807. // ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = false;
  808. // ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn = false;
  809. //
  810. // if(preAuthRequest(UartFd))
  811. // {
  812. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes = true;
  813. // cmdRepeatCount = 0;
  814. // refreshStartTimer(&tmr[TIMER_READ_RESPONSE]);
  815. // }
  816. //
  817. // if(cmdRepeatCount > SPEC_REQUEST_RETRY)
  818. // {
  819. // DEBUG_WARN("preAuthRequest fail over count(%d).", cmdRepeatCount);
  820. // cmdRepeatCount = 0;
  821. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  822. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  823. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true;
  824. // }
  825. // else
  826. // {
  827. // cmdRepeatCount += 1;
  828. // }
  829. // }
  830. // else
  831. // {
  832. // if((getInputBufferCount(UartFd) >= RAW_DATA_LENGTH))
  833. // {
  834. //
  835. // //DEBUG_INFO("\n\rTo wait Payment preAuth Response.\n\r");
  836. //
  837. // if(preAuthResponse(UartFd))
  838. // {
  839. // if(ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn)
  840. // {
  841. //
  842. // // * 1. Announce CSU event content.
  843. //
  844. // ShmSysConfigAndInfo->SysInfo.bazel8.event.isGetOn = false;
  845. // }
  846. // else if(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.isGetOn)
  847. // {
  848. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = ((strstr(ShmSysConfigAndInfo->SysInfo.bazel8.txResp.statusCode, "0000") != NULL)?true:false);
  849. //
  850. // if(ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass || (preAuthRetry++ >= 3))
  851. // {
  852. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  853. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true;
  854. //
  855. // preAuthRetry = 0;
  856. // }
  857. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes = false;
  858. // }
  859. // }
  860. // }
  861. //
  862. // if((getDiffSecNow(tmr[TIMER_READ_RESPONSE]) > TIMEOUT_REQUEST))
  863. // {
  864. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = false;
  865. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes = false;
  866. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass = false;
  867. // ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = true;
  868. // DEBUG_WARN("Wait pre auth response timeout(%d secs).", TIMEOUT_REQUEST);
  869. // }
  870. // }
  871. // }
  872. //
  873. // sleep(3);
  874. // }
  875. //
  876. // return FAIL;
  877. }