RFID.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include "../Config.h"
  6. #include "../Log/log.h"
  7. #include "../Define/define.h"
  8. #include "../ShareMemory/shmMem.h"
  9. #include "../SelectGun/SelectGun.h"
  10. #include "../DataBase/DataBase.h"
  11. #include "main.h"
  12. #include "../timeout.h"
  13. static DcCommonInfo *ShmDcCommonData = NULL;
  14. static SelectGunInfo *ShmSelectGunInfo = NULL;
  15. static struct SysInfoData *pSysInfo = NULL;
  16. #define PREAUTHMONEY 888
  17. bool isDeductDb_ready;
  18. static RecordTransactionInfo LocalTransactionInfo;
  19. //------------------------------------------------------------------------------
  20. static char *rfidPortName = "/dev/ttyS2";
  21. static bool isCardScan = false;
  22. //------------------------------------------------------------------------------
  23. static bool canStartCharging(void)
  24. {
  25. uint8_t index = 0;
  26. char buf2[16] = "";
  27. memset(buf2, 0, ARRAY_SIZE(buf2));
  28. struct OCPP16Data *ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  29. for (index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++) {
  30. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  31. }
  32. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  33. // 因為無法得知實際的長度,所以只能用搜尋的方式
  34. if (strcmp(buf2, "Accepted") == EQUAL) {
  35. return true;
  36. }
  37. return false;
  38. }
  39. bool isAutorCompleteHandle(/*uint8_t *authorizeIndex*/)
  40. {
  41. // uint8_t i = 0;
  42. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  43. //struct ChargingInfoData *pDcChargingInfo = NULL;
  44. //struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  45. // 透過後臺停止充電的判斷
  46. if (isAuthorizedComplete()) {
  47. // 判斷後台回覆狀態
  48. if (canStartCharging() == false) {
  49. strcpy((char *)pSysConfig->UserId, "");
  50. ClearAuthorizedFlag();
  51. }
  52. return true;
  53. }
  54. return false;
  55. /*
  56. if (*(authorizeIndex) != NO_DEFINE) {
  57. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(*(authorizeIndex));
  58. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  59. strcmp((char *)pDcChargingInfo->StartUserId, "") != EQUAL) {
  60. // 先找 AC
  61. if (*(authorizeIndex) == DEFAULT_AC_INDEX) {
  62. AcChargingTerminalProcess();
  63. } else {
  64. ChargingTerminalProcess(*(authorizeIndex));
  65. }
  66. }
  67. strcpy((char *)pSysConfig->UserId, "");
  68. *(authorizeIndex) = NO_DEFINE;
  69. }
  70. ClearAuthorizedFlag();
  71. } else if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST) {
  72. // 白名單驗證
  73. for (i = 0; i < 10; i++) {
  74. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], "") == EQUAL) {
  75. continue;
  76. }
  77. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], (char *)pSysConfig->UserId) == EQUAL) {
  78. ChargingTerminalProcess(*(authorizeIndex));
  79. strcpy((char *)pSysConfig->UserId, "");
  80. ClearAuthorizedFlag();
  81. break;
  82. }
  83. }
  84. }*/
  85. }
  86. bool RfidStopCharging(void)
  87. {
  88. //當前沒有選槍
  89. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  90. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  91. if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL ) {
  92. strcpy((char *)pSysConfig->UserId, "");
  93. return false;
  94. }
  95. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  96. if (pDcChargingInfo->isRemoteStart) {
  97. } else {
  98. if (strcmp((char *)pSysConfig->UserId, (char *)pDcChargingInfo->StartUserId) == EQUAL) {
  99. ChargingTerminalProcess(pSysInfo->CurGunSelected);
  100. strcpy((char *)pSysConfig->UserId, "");
  101. log_info("index = %d, card number = %s, UserId = %s ",
  102. pSysInfo->CurGunSelected,
  103. pDcChargingInfo->StartUserId,
  104. pSysConfig->UserId);
  105. return true;
  106. } else {
  107. strcpy((char *)pSysConfig->UserId, "");
  108. return false;
  109. }
  110. }
  111. return false;
  112. }
  113. static void UserScanFunction(void)
  114. {
  115. // bool idleReq = false;
  116. //uint8_t i = 0;
  117. //uint8_t stopReq = NO_DEFINE;
  118. //char value[32] = {0};
  119. //static uint8_t _authorizeIndex = NO_DEFINE;
  120. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  121. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  122. //struct ChargingInfoData *pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  123. struct ChargingInfoData *pDcChargingInfo = NULL;
  124. //GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
  125. // 當前非驗證的狀態
  126. if (IsAuthorizingMode()) {
  127. isAutorCompleteHandle(/*&_authorizeIndex*/);
  128. }
  129. //當前沒有選槍
  130. /*
  131. if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL) {
  132. strcpy((char *)pSysConfig->UserId, "");
  133. return;
  134. }*/
  135. // 先判斷現在是否可以提供刷卡
  136. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  137. // 2. 停止充電
  138. //if (pSysInfo->PageIndex == _LCM_ERROR) {
  139. if (pSysInfo->SystemPage == _PAGE_MAINTAIN) {
  140. strcpy((char *)pSysConfig->UserId, "");
  141. return;
  142. }
  143. /*
  144. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  145. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  146. if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  147. stopReq = i;
  148. }
  149. if ((pDcChargingInfo->SystemStatus == S_AUTHORIZING &&
  150. pDcChargingInfo->IsAvailable == YES) ||
  151. (pGunIndexInfo->AcGunIndex > 0 &&
  152. pAcChargingInfo->SystemStatus == S_AUTHORIZING &&
  153. pAcChargingInfo->IsAvailable)
  154. ) {
  155. idleReq = true;
  156. }
  157. }
  158. if (pGunIndexInfo->AcGunIndex > 0 &&
  159. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  160. pAcChargingInfo->SystemStatus == S_CHARGING) {
  161. stopReq = DEFAULT_AC_INDEX;
  162. }
  163. */
  164. if (strlen((char *)pSysConfig->UserId) <= 0) {
  165. return;
  166. }
  167. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  168. // Stop Charging For AC
  169. /*if (pGunIndexInfo->AcGunIndex > 0 &&
  170. stopReq == DEFAULT_AC_INDEX &&
  171. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  172. log_info("ac stop charging ");
  173. log_info("index = %d, card number = %s, UserId = %s ",
  174. pSysInfo->CurGunSelectedByAc,
  175. pAcChargingInfo->StartUserId,
  176. pSysConfig->UserId);
  177. memset(value, 0, sizeof(value));
  178. memcpy(value,
  179. (uint8_t *)pAcChargingInfo->StartUserId,
  180. ARRAY_SIZE(pAcChargingInfo->StartUserId));
  181. if (strcmp((char *)pSysConfig->UserId, value) == EQUAL) {
  182. AcChargingTerminalProcess();
  183. }
  184. strcpy((char *)pSysConfig->UserId, "");
  185. } else if (stopReq < pSysConfig->TotalConnectorCount &&
  186. pDcChargingInfo->SystemStatus == S_CHARGING &&
  187. (pGunIndexInfo->AcGunIndex <= 0 ||
  188. (pGunIndexInfo->AcGunIndex > 0 &&
  189. pSysInfo->CurGunSelectedByAc == NO_DEFINE))
  190. ) {
  191. // Stop Charging
  192. if (RfidStopCharging())
  193. return;
  194. if (pGunIndexInfo->AcGunIndex > 0 &&
  195. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  196. _authorizeIndex = pSysInfo->CurGunSelectedByAc;
  197. } else {
  198. _authorizeIndex = pSysInfo->CurGunSelected;
  199. }
  200. strcpy((char *)pSysConfig->UserId, "");
  201. return;
  202. } else if (idleReq) {
  203. if (pSysConfig->TotalConnectorCount > 1 &&
  204. stopReq != 255 &&
  205. pSysInfo->IsAlternatvieConf == YES) {
  206. idleReq = false;
  207. strcpy((char *)pSysConfig->UserId, "");
  208. } else if ((pGunIndexInfo->AcGunIndex > 0 &&
  209. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  210. pDcChargingInfo->SystemStatus == S_AUTHORIZING) {*/
  211. if ( (pSysInfo->SystemPage == _PAGE_AUTHORIZE ||
  212. pSysInfo->SystemPage == _PAGE_SENSING) &&
  213. pDcChargingInfo->SystemStatus == S_IDLE) {
  214. log_info("// LCM => Authorizing");
  215. confirmSelGun(pSysInfo->CurGunSelected);
  216. setSelGunWaitToAuthor(pSysInfo->CurGunSelected);
  217. StartSystemTimeoutDet(Timeout_Authorizing);
  218. AuthorizingStart();
  219. pDcChargingInfo->SystemStatus = S_AUTHORIZING;
  220. // LCM => Authorizing
  221. //pSysInfo->SystemPage = _PAGE_AUTHORIZE;
  222. // 進入確認卡號狀態
  223. } else {
  224. //strcpy((char *)pSysConfig->UserId, "");
  225. }
  226. return;
  227. }
  228. bool GetIsCardScan(void)
  229. {
  230. return isCardScan;
  231. }
  232. void SetIsCardScan(bool value)
  233. {
  234. isCardScan = value;
  235. }
  236. void AuthorizeToCharge()
  237. {
  238. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  239. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  240. // SelectGunInfo *ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  241. // struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(pSysInfo->CurGunSelected);;
  242. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  243. if(isAuthorizedComplete())
  244. {
  245. if (canStartCharging()) {
  246. DetectPluginStart();
  247. if (ShmDcCommonData->AuthPass_flag[pSysInfo->CurGunSelected] == TRUE &&
  248. ShmDcCommonData->TradeCancel == FALSE) {
  249. pSysInfo->SystemPage = _PAGE_PLUGIN;
  250. }
  251. } else {
  252. log_info("LCM => Authorize fail");
  253. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  254. strcpy((char *)pSysConfig->UserId, "");
  255. }
  256. ClearAuthorizedFlag();
  257. }
  258. }
  259. void ScannerCardProcess(void)
  260. {
  261. //struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  262. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  263. struct WARNING_CODE_INFO *pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  264. struct ChargingInfoData *pDcChargingInfo = NULL;
  265. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  266. if (!isDetectPlugin() && (pSysInfo->SystemPage == _PAGE_SENSING ||
  267. pSysInfo->SystemPage == _PAGE_AUTHORIZE ) &&
  268. pDcChargingInfo->SystemStatus == S_IDLE &&
  269. pSysWarning->Level != WARN_LV_ER /*&&
  270. pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE*/) {
  271. //setSelGunWaitToAuthor(pSysInfo->CurGunSelected);
  272. isCardScan = true;
  273. // 處理刷卡及驗證卡號的動作
  274. UserScanFunction();
  275. }
  276. if (pDcChargingInfo->SystemStatus == S_AUTHORIZING && pSysInfo->SystemPage == _PAGE_SENSING ) {
  277. AuthorizeToCharge();
  278. } else if (pSysInfo->SystemPage == _PAGE_AUTHORIZE_FAIL) {
  279. StartSystemTimeoutDet(Timeout_VerifyFail);
  280. isCardScan = false;
  281. } else {
  282. isCardScan = false;
  283. }
  284. }
  285. void WritePayResult(int result ,uint8_t gunIndex)
  286. {
  287. memcpy(&ShmDcCommonData->TransactionInfo[gunIndex], &LocalTransactionInfo, sizeof(RecordTransactionInfo));
  288. if (result == TRUE)
  289. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_COMPLETE_PASS;
  290. else
  291. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_COMPLETE_FAIL;
  292. log_info("Gun[%d] TransactionId:%d DeductResult:%d IsDonateInvoice:%d Amount:%f",
  293. gunIndex, ShmDcCommonData->TransactionInfo[gunIndex].TransactionId,
  294. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult,
  295. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice,
  296. ShmDcCommonData->TransactionInfo[gunIndex].Amount);
  297. }
  298. void storePayResult(uint8_t gunIndex)
  299. {
  300. memset(&LocalTransactionInfo, 0, sizeof(RecordTransactionInfo));
  301. memcpy(&LocalTransactionInfo, &ShmDcCommonData->TransactionInfo[gunIndex] ,sizeof(RecordTransactionInfo));
  302. //LocalTransactionInfo.Amount = ShmDcCommonData->finalcost[gunIndex];
  303. }
  304. void PreAuthCompleteToCardReader(int fd,uint8_t gunIndex)
  305. {
  306. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  307. int result = 0;
  308. if (ShmDcCommonData->TransactionInfo[gunIndex].Amount > 0 && ShmDcCommonData->TransactionInfo[gunIndex].Amount < 1) {
  309. log_info("final cost less 1 : %f", ShmDcCommonData->TransactionInfo[gunIndex].Amount);
  310. ShmDcCommonData->TransactionInfo[gunIndex].Amount = 1;
  311. }
  312. storePayResult(gunIndex);
  313. result = CreditCardPreAuthComplete(fd,(int)ShmDcCommonData->TransactionInfo[gunIndex].Amount, &pSysConfig->ModelName[0],
  314. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.VemData[0],
  315. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard);
  316. //sleep(10);
  317. if (result > 0 ) {
  318. log_info("Credit Card Spend Money:%.1f", LocalTransactionInfo.Amount);
  319. pSysInfo->SystemPage = _PAGE_COMPLETE;
  320. LocalTransactionInfo.DeductResult = _DEDUCT_COMPLETE_PASS;
  321. WritePayResult(TRUE,gunIndex);
  322. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  323. } else {
  324. log_info("PAYING FAIL");
  325. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  326. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  327. LocalTransactionInfo.DeductResult = _DEDUCT_COMPLETE_FAIL;
  328. WritePayResult(FALSE,gunIndex);
  329. }
  330. ShmDcCommonData->TransactionInfo[gunIndex].IsUpload = FALSE;
  331. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  332. }
  333. int CreditCardCancelPreAuth(int fd, uint8_t gunIndex)
  334. {
  335. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  336. ShmDcCommonData->PreAuth_Result = CreditCardPreAuthCancel(fd, PREAUTHMONEY, &pSysConfig->ModelName[0],
  337. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.ApprovalNo[0],
  338. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.CardNo[0],
  339. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.VemData[0]);
  340. //sleep(10);
  341. if (ShmDcCommonData->PreAuth_Result >= 0) {
  342. strcpy((char*)pSysConfig->UserId, "");
  343. //ShmDcCommonData->PayFinish[gunIndex] = TRUE;
  344. log_info("Card Reader PreAuth Cancel Success");
  345. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_CANCEL;
  346. } else if (ShmDcCommonData->PreAuth_Result < 0) {
  347. log_info("Card Reader PreAuth Cancel Failure");
  348. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_PREAUTH;
  349. }
  350. ShmDcCommonData->TransactionInfo[gunIndex].IsUpload = FALSE;
  351. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  352. memset(&ShmDcCommonData->TransactionInfo[gunIndex], 0x00, sizeof(RecordTransactionInfo));
  353. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  354. return ShmDcCommonData->PreAuth_Result;
  355. }
  356. void ReDeductProcess(int fd)
  357. {
  358. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  359. int j;
  360. int result;
  361. int rededuct_gunIndex[128];
  362. RecordTransactionInfo deductInfo[128];
  363. int rededuct_num = 0;
  364. rededuct_num = DB_GetMultiReDeductInfo(&rededuct_gunIndex[0], &deductInfo[0]);
  365. if (rededuct_num == 0)
  366. log_info("No Rededuct Information");
  367. else {
  368. log_info("Rededuct Total Number:%d", rededuct_num);
  369. for (j = 0; j < rededuct_num; j++) {
  370. if (pSysInfo->SystemPage != _PAGE_IDLE && !ShmDcCommonData->Exe_ReDeduct) {
  371. ShmDcCommonData->RoutineReduct = FALSE;
  372. break;
  373. }
  374. log_info("Start Rededuct item [%d]",j);
  375. if (deductInfo[j].isIntoCharge == FALSE ||
  376. (deductInfo[j].Energy == 0 && deductInfo[j].Amount == 0)) {
  377. // 未進入充電或度數等於零
  378. result = CreditCardPreAuthCancel(fd, PREAUTHMONEY, &pSysConfig->ModelName[0],
  379. &deductInfo[j].pCreditCard.ApprovalNo[0],
  380. &deductInfo[j].pCreditCard.CardNo[0],
  381. &deductInfo[j].pCreditCard.VemData[0]);
  382. sleep(10);
  383. if (result > 0) {
  384. deductInfo[j].DeductResult = _DEDUCT_CANCEL;
  385. log_info("Backgroud PreAuthCancel OK");
  386. }
  387. } else {
  388. // 度數大於零
  389. if (deductInfo[j].Amount == 0 && deductInfo[j].Energy > 0) {
  390. deductInfo[j].Amount = deductInfo[j].Energy * ShmDcCommonData->ChargingRate;
  391. if (deductInfo[j].Amount < 1)
  392. deductInfo[j].Amount = 1;
  393. log_info("Error Close Charging recount amount:%.1f",deductInfo[j].Amount);
  394. }
  395. result = CreditCardPreAuthComplete(fd, (int)deductInfo[j].Amount, &pSysConfig->ModelName[0],
  396. &deductInfo[j].pCreditCard.VemData[0],
  397. &ShmDcCommonData->TransactionInfo[0].pCreditCard);
  398. sleep(7);
  399. if (result > 0) {
  400. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_PASS;
  401. log_info("Backgroud PreAuthComplete OK");
  402. } else if (result < 0) {
  403. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_FAIL;
  404. log_info("Backgroud ID:%d Amount:%d VemData:%s PreAuthComplete fail",
  405. deductInfo[j].TransactionId,
  406. (int)deductInfo[j].Amount,
  407. deductInfo[j].pCreditCard.VemData);
  408. }
  409. }
  410. deductInfo[j].IsUpload = FALSE;
  411. deductInfo[j].RedeductTime++;
  412. UpdateDeductInfoStatus(rededuct_gunIndex[j], &deductInfo[j]);
  413. }
  414. }
  415. ShmDcCommonData->RoutineReduct = TRUE;
  416. }
  417. static int InitialRfidPort(void)
  418. {
  419. int fd = open(rfidPortName, O_RDWR);
  420. struct termios tios;
  421. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  422. if (fd != FAIL) {
  423. ioctl (fd, TCGETS, &tios);
  424. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  425. tios.c_lflag = 0;
  426. tios.c_iflag = 0;
  427. tios.c_oflag = 0;
  428. tios.c_cc[VMIN] = 0;
  429. tios.c_cc[VTIME] = (uint8_t) 1;
  430. tios.c_lflag = 0;
  431. tcflush(fd, TCIFLUSH);
  432. ioctl(fd, TCSETS, &tios);
  433. }
  434. if (fd < 0) {
  435. pAlarmCode->AlarmEvents.bits.RfidModuleCommFail = 1;
  436. }
  437. return fd;
  438. }
  439. void CreateRfidFork(void)
  440. {
  441. pid_t rfidRecPid;
  442. int rededuct_gunIndex[128];
  443. RecordTransactionInfo deductInfo[128];
  444. int rededuct_num = 0;
  445. int donate = 0;
  446. rfidRecPid = fork();
  447. int j = 0;
  448. if (rfidRecPid == 0) {
  449. //char localTime[128] = {0};
  450. struct timeb SeqEndTime;
  451. struct tm *tm;
  452. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  453. int fd = -1;
  454. int isContinue = 1;
  455. //RFID rfid = {0};
  456. //RecordTransactionInfo deduct;
  457. fd = InitialRfidPort();
  458. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  459. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  460. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  461. struct ChargingInfoData *pDcChargingInfo = NULL;
  462. if (DeductDB_Open() != PASS) {
  463. isDeductDb_ready = false;
  464. } else {
  465. isDeductDb_ready = true;
  466. //InsertDeductInfo(0, &ShmDcCommonData->TransactionInfo);
  467. }
  468. int gunIndex;
  469. //int uploadIndex = 0;
  470. int ReAuthComplete_Index = 0;
  471. //log_info("RFID fork Child's PID is %d", getpid());
  472. int result;
  473. int is_idle = TRUE;
  474. while (isContinue) {
  475. result = 0;
  476. usleep(500000);
  477. ftime(&SeqEndTime);
  478. SeqEndTime.time = time(NULL);
  479. tm = localtime(&SeqEndTime.time);
  480. if (ShmDcCommonData->DebugFlag == TRUE ||
  481. ShmDcCommonData->is_RemoteStart[pSysInfo->CurGunSelected] == TRUE ||
  482. ShmDcCommonData->is_AutoStart[pSysInfo->CurGunSelected] == TRUE) {
  483. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  484. ShmDcCommonData->PreAuth_Result = 0;
  485. }
  486. is_idle = TRUE;
  487. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  488. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  489. if (pDcChargingInfo->SystemStatus != S_IDLE) {
  490. is_idle = FALSE;
  491. }
  492. if (ShmDcCommonData->StopCharge[gunIndex] == TRUE && pDcChargingInfo->Replug_flag == TRUE) {
  493. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  494. log_info("Not Into Charging cancel Trade");
  495. StopGunInfoTimeoutDet(gunIndex);
  496. ShmDcCommonData->TradeCancel = TRUE;
  497. pSysInfo->CurGunSelected = gunIndex;
  498. pSysInfo->SystemPage = _PAGE_SENSING;
  499. StartSystemTimeoutDet(Timeout_TradeCancel);
  500. CreditCardCancelPreAuth(fd, gunIndex);
  501. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  502. StopSystemTimeoutDet();
  503. } else {
  504. if (ShmDcCommonData->StopCharge[gunIndex] == TRUE && ShmDcCommonData->finalcost_flag[gunIndex] &&
  505. (pSysInfo->SystemPage != _PAGE_AUTHORIZE && pSysInfo->SystemPage != _PAGE_SENSING)) {
  506. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  507. pSysInfo->CurGunSelected = gunIndex;
  508. if (pDcChargingInfo->Replug_flag == TRUE) {
  509. log_info("Not Into Charging cancel Trade");
  510. ShmDcCommonData->TradeCancel = TRUE;
  511. pSysInfo->SystemPage = _PAGE_SENSING;
  512. StartSystemTimeoutDet(Timeout_TradeCancel);
  513. } else
  514. pSysInfo->SystemPage = _PAGE_PAYING;
  515. StopGunInfoTimeoutDet(gunIndex); //Timeout_FinalCost
  516. // Remote Start of AutoStart ByPass Credit Card Reader
  517. if (ShmDcCommonData->DebugFlag == TRUE ||
  518. ShmDcCommonData->is_RemoteStart[gunIndex] == TRUE ||
  519. ShmDcCommonData->is_AutoStart[gunIndex] == TRUE) {
  520. pSysInfo->SystemPage = _PAGE_COMPLETE;
  521. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  522. continue;
  523. }
  524. // 實際扣款
  525. if (ShmDcCommonData->TransactionInfo[gunIndex].Amount < 1) {
  526. log_info("Final Cost less 1 , Cancel Trade!!");
  527. if (CreditCardCancelPreAuth(fd, gunIndex) > 0) {
  528. pSysInfo->SystemPage = _PAGE_COMPLETE;
  529. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  530. } else {
  531. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  532. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  533. }
  534. } else {
  535. PreAuthCompleteToCardReader(fd, gunIndex);
  536. }
  537. }
  538. }
  539. } // for
  540. // 取消預授權
  541. if(ShmDcCommonData->PreAuth_Config == _CREDITCARD_CANCEL) {
  542. CreditCardCancelPreAuth(fd,pSysInfo->CurGunSelected);
  543. // 預授權
  544. } else if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_PREAUTH /* && ShmDcCommonData->GetCardNo[pSysInfo->CurGunSelected]*/) {
  545. donate = (int)ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected].IsDonateInvoice;
  546. memset(&ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected], 0x00, sizeof(RecordTransactionInfo));
  547. ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected].IsDonateInvoice = (unsigned char)donate;
  548. result = CreditCardPreAuth(fd, PREAUTHMONEY,&pSysConfig->ModelName[0], &ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected].pCreditCard);
  549. if (!ShmDcCommonData->TradeCancel)
  550. StopSystemTimeoutDet();
  551. if (ShmDcCommonData->TradeCancel == FALSE && result > 0) {
  552. pSysInfo->SystemPage = _PAGE_SENSING;
  553. }
  554. sleep(3);
  555. ShmDcCommonData->PreAuth_Result = result;
  556. //result = 1;
  557. if (result > 0 && strcmp(ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected].pCreditCard.CardNo,"") != 0 ) {
  558. ShmDcCommonData->AuthPass_flag[pSysInfo->CurGunSelected] = TRUE;
  559. strncpy((char*)pSysConfig->UserId, (char *)ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected].pCreditCard.CardNo, 20);
  560. log_info("Authorize card:%s", pSysConfig->UserId);
  561. ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected].DeductResult = _DEDUCT_PREAUTH;
  562. ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected].IsUpload = FALSE;
  563. InsertDeductInfo(ShmDcCommonData->ConnectorID[pSysInfo->CurGunSelected], &ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected]);
  564. log_info("PreAuth OK");
  565. } else if (result < 0) {
  566. if (ShmDcCommonData->TradeCancel == FALSE)
  567. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  568. ShmDcCommonData->AuthPass_flag[pSysInfo->CurGunSelected] = FALSE;
  569. memset(&ShmDcCommonData->TransactionInfo[pSysInfo->CurGunSelected].pCreditCard, 0, sizeof(TransInfo));
  570. log_info("PreAuth Fail");
  571. }
  572. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  573. }
  574. // 每日晚上11點進行補扣款
  575. if ((is_idle == TRUE && (tm->tm_hour == 15 && tm->tm_min < 30) &&
  576. pSysInfo->SystemPage == _PAGE_IDLE &&
  577. ShmDcCommonData->RoutineReduct == FALSE) || ShmDcCommonData->Exe_ReDeduct) {
  578. ReDeductProcess(fd);
  579. ShmDcCommonData->Exe_ReDeduct = 0;
  580. }
  581. // 每日晚上11點30分結帳
  582. if (is_idle == TRUE && pSysInfo->SystemPage == _PAGE_IDLE &&
  583. ((tm->tm_hour == 15 && tm->tm_min > 30 && ShmDcCommonData->RoutineSettlement == FALSE) ||
  584. ShmDcCommonData->UnionSettlement)) {
  585. StopSystemTimeoutDet();
  586. if (ShmDcCommonData->UnionSettlement)
  587. ShmDcCommonData->UnionSettlement = 0;
  588. else
  589. ShmDcCommonData->RoutineSettlement = TRUE;
  590. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  591. result = CreditCardUnionSettlement(fd, &pSysConfig->ModelName[0], &LocalTransactionInfo.pCreditCard);
  592. if (result > 0) {
  593. log_info("CreditCardUnionSettlement OK");
  594. }
  595. else
  596. log_info("CreditCardUnionSettlement FAIL");
  597. sleep(90);
  598. pSysInfo->SystemPage = _PAGE_IDLE;
  599. }
  600. if (tm->tm_hour == 16 && tm->tm_min == 0) {
  601. ShmDcCommonData->RoutineSettlement = FALSE;
  602. ShmDcCommonData->RoutineReduct = FALSE;
  603. }
  604. /*
  605. // 刷卡判斷
  606. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  607. !pSysConfig->isRFID) {
  608. continue;
  609. }
  610. if (getRequestCardSN(fd, 0, &rfid) == false) {
  611. continue;
  612. }
  613. //log_info("Get Card..-%s- ", pSysConfig->UserId);
  614. if (strlen((char *)pSysConfig->UserId) != 0) {
  615. continue;
  616. }
  617. if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_LITTLE) {
  618. switch (rfid.snType) {
  619. case RFID_SN_TYPE_6BYTE:
  620. sprintf((char *) pSysConfig->UserId,
  621. "%02X%02X%02X%02X%02X%02X",
  622. rfid.currentCard[0], rfid.currentCard[1],
  623. rfid.currentCard[2], rfid.currentCard[3],
  624. rfid.currentCard[4], rfid.currentCard[5]);
  625. break;
  626. case RFID_SN_TYPE_7BYTE:
  627. sprintf((char *) pSysConfig->UserId,
  628. "%02X%02X%02X%02X%02X%02X%02X",
  629. rfid.currentCard[0], rfid.currentCard[1],
  630. rfid.currentCard[2], rfid.currentCard[3],
  631. rfid.currentCard[4], rfid.currentCard[5],
  632. rfid.currentCard[6]);
  633. break;
  634. case RFID_SN_TYPE_10BYTE:
  635. sprintf((char *) pSysConfig->UserId,
  636. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  637. rfid.currentCard[0], rfid.currentCard[1],
  638. rfid.currentCard[2], rfid.currentCard[3],
  639. rfid.currentCard[4], rfid.currentCard[5],
  640. rfid.currentCard[6], rfid.currentCard[7],
  641. rfid.currentCard[8], rfid.currentCard[9]);
  642. break;
  643. case RFID_SN_TYPE_4BYTE:
  644. sprintf((char *) pSysConfig->UserId,
  645. "%02X%02X%02X%02X",
  646. rfid.currentCard[0], rfid.currentCard[1],
  647. rfid.currentCard[2], rfid.currentCard[3]);
  648. break;
  649. }
  650. } else if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_BIG) {
  651. switch (rfid.snType) {
  652. case RFID_SN_TYPE_6BYTE:
  653. sprintf((char *) pSysConfig->UserId,
  654. "%02X%02X%02X%02X%02X%02X",
  655. rfid.currentCard[5], rfid.currentCard[4],
  656. rfid.currentCard[3], rfid.currentCard[2],
  657. rfid.currentCard[1], rfid.currentCard[0]);
  658. break;
  659. case RFID_SN_TYPE_7BYTE:
  660. sprintf((char *) pSysConfig->UserId,
  661. "%02X%02X%02X%02X%02X%02X%02X",
  662. rfid.currentCard[6], rfid.currentCard[5],
  663. rfid.currentCard[4], rfid.currentCard[3],
  664. rfid.currentCard[2], rfid.currentCard[1],
  665. rfid.currentCard[0]);
  666. break;
  667. case RFID_SN_TYPE_10BYTE:
  668. sprintf((char *) pSysConfig->UserId,
  669. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  670. rfid.currentCard[9], rfid.currentCard[8],
  671. rfid.currentCard[7], rfid.currentCard[6],
  672. rfid.currentCard[5], rfid.currentCard[4],
  673. rfid.currentCard[3], rfid.currentCard[2],
  674. rfid.currentCard[1], rfid.currentCard[0]);
  675. break;
  676. case RFID_SN_TYPE_4BYTE:
  677. sprintf((char *) pSysConfig->UserId,
  678. "%02X%02X%02X%02X",
  679. rfid.currentCard[3], rfid.currentCard[2],
  680. rfid.currentCard[1], rfid.currentCard[0]);
  681. break;
  682. }
  683. }
  684. log_info("card number = %s", pSysConfig->UserId);
  685. */
  686. }
  687. }
  688. }