RFID.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 "main.h"
  11. #include "../timeout.h"
  12. //------------------------------------------------------------------------------
  13. static char *rfidPortName = "/dev/ttyS2";
  14. static bool isCardScan = false;
  15. //------------------------------------------------------------------------------
  16. static bool canStartCharging(void)
  17. {
  18. uint8_t index = 0;
  19. char buf2[16] = "";
  20. memset(buf2, 0, ARRAY_SIZE(buf2));
  21. struct OCPP16Data *ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  22. for (index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++) {
  23. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  24. }
  25. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  26. // 因為無法得知實際的長度,所以只能用搜尋的方式
  27. if (strcmp(buf2, "Accepted") == EQUAL) {
  28. return true;
  29. }
  30. return false;
  31. }
  32. static void isAutorCompleteHandle(uint8_t *authorizeIndex)
  33. {
  34. uint8_t i = 0;
  35. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  36. struct ChargingInfoData *pDcChargingInfo = NULL;
  37. // 透過後臺停止充電的判斷
  38. if (isAuthorizedComplete()
  39. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  40. || (pSysInfo->OcppConnStatus == NO &&
  41. pSysConfig->OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  42. #endif //!defined DD360 && !defined DD360Audi
  43. ) {
  44. // 判斷後台回覆狀態
  45. if (canStartCharging() == false) {
  46. strcpy((char *)pSysConfig->UserId, "");
  47. ClearAuthorizedFlag();
  48. return;
  49. }
  50. if (*(authorizeIndex) != NO_DEFINE) {
  51. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(*(authorizeIndex));
  52. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  53. strcmp((char *)pDcChargingInfo->StartUserId, "") != EQUAL) {
  54. // 先找 AC
  55. if (*(authorizeIndex) == DEFAULT_AC_INDEX) {
  56. AcChargingTerminalProcess();
  57. } else {
  58. ChargingTerminalProcess(*(authorizeIndex));
  59. }
  60. }
  61. strcpy((char *)pSysConfig->UserId, "");
  62. *(authorizeIndex) = NO_DEFINE;
  63. }
  64. ClearAuthorizedFlag();
  65. } else if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST) {
  66. // 白名單驗證
  67. for (i = 0; i < 10; i++) {
  68. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], "") == EQUAL) {
  69. continue;
  70. }
  71. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], (char *)pSysConfig->UserId) == EQUAL) {
  72. ChargingTerminalProcess(*(authorizeIndex));
  73. strcpy((char *)pSysConfig->UserId, "");
  74. ClearAuthorizedFlag();
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. static void UserScanFunction(void)
  81. {
  82. bool idleReq = false;
  83. uint8_t i = 0;
  84. uint8_t stopReq = NO_DEFINE;
  85. char value[32] = {0};
  86. static uint8_t _authorizeIndex = NO_DEFINE;
  87. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  88. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  89. struct ChargingInfoData *pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  90. struct ChargingInfoData *pDcChargingInfo = NULL;
  91. GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
  92. // 當前非驗證的狀態
  93. if (IsAuthorizingMode()) {
  94. isAutorCompleteHandle(&_authorizeIndex);
  95. }
  96. //當前沒有選槍
  97. if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL) {
  98. strcpy((char *)pSysConfig->UserId, "");
  99. return;
  100. }
  101. /*
  102. if (pSysConfig->EVCCID_Authorize) {
  103. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  104. if (strcmp( (char *)pSysConfig->UserId, (char *) pDcChargingInfo->EVCCID) != EQUAL )
  105. return;
  106. }*/
  107. // 先判斷現在是否可以提供刷卡
  108. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  109. // 2. 停止充電
  110. if (pSysInfo->PageIndex == _LCM_FIX) {
  111. strcpy((char *)pSysConfig->UserId, "");
  112. return;
  113. }
  114. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  115. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  116. if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  117. stopReq = i;
  118. }
  119. if ((pDcChargingInfo->SystemStatus == S_IDLE &&
  120. pDcChargingInfo->IsAvailable == YES) ||
  121. (pGunIndexInfo->AcGunIndex > 0 &&
  122. pAcChargingInfo->SystemStatus == S_IDLE &&
  123. pAcChargingInfo->IsAvailable)
  124. ) {
  125. idleReq = true;
  126. }
  127. }
  128. if (pGunIndexInfo->AcGunIndex > 0 &&
  129. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  130. pAcChargingInfo->SystemStatus == S_CHARGING) {
  131. stopReq = DEFAULT_AC_INDEX;
  132. }
  133. if (strlen((char *)pSysConfig->UserId) <= 0) {
  134. return;
  135. }
  136. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  137. if (pGunIndexInfo->AcGunIndex > 0 &&
  138. stopReq == DEFAULT_AC_INDEX &&
  139. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  140. log_info("ac stop charging ");
  141. log_info("index = %d, card number = %s, UserId = %s ",
  142. pSysInfo->CurGunSelectedByAc,
  143. pAcChargingInfo->StartUserId,
  144. pSysConfig->UserId);
  145. memset(value, 0, sizeof(value));
  146. memcpy(value,
  147. (uint8_t *)pAcChargingInfo->StartUserId,
  148. ARRAY_SIZE(pAcChargingInfo->StartUserId));
  149. if (strcmp((char *)pSysConfig->UserId, value) == EQUAL) {
  150. AcChargingTerminalProcess();
  151. }
  152. strcpy((char *)pSysConfig->UserId, "");
  153. } else if (stopReq < pSysConfig->TotalConnectorCount &&
  154. pDcChargingInfo->SystemStatus == S_CHARGING &&
  155. (pGunIndexInfo->AcGunIndex <= 0 ||
  156. (pGunIndexInfo->AcGunIndex > 0 &&
  157. pSysInfo->CurGunSelectedByAc == NO_DEFINE))
  158. ) {
  159. log_info("stop charging ");
  160. log_info("index = %d, card number = %s, UserId = %s ",
  161. pSysInfo->CurGunSelected,
  162. pDcChargingInfo->StartUserId,
  163. pSysConfig->UserId);
  164. memset(value, 0, sizeof(value));
  165. memcpy(value,
  166. (uint8_t *)pDcChargingInfo->StartUserId,
  167. ARRAY_SIZE(pDcChargingInfo->StartUserId));
  168. // 同一張卡直接停掉
  169. if (strcmp((char *)pSysConfig->UserId, value) == EQUAL) {
  170. ChargingTerminalProcess(pSysInfo->CurGunSelected);
  171. strcpy((char *)pSysConfig->UserId, "");
  172. return;
  173. }
  174. // 進驗證
  175. if (pGunIndexInfo->AcGunIndex > 0 &&
  176. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  177. _authorizeIndex = pSysInfo->CurGunSelectedByAc;
  178. } else {
  179. _authorizeIndex = pSysInfo->CurGunSelected;
  180. }
  181. #if defined DD360 || defined DD360Audi || defined DD360ComBox
  182. strcpy((char *)pSysConfig->UserId, "");
  183. return;
  184. #endif //defined DD360 || defined DD360Audi || defined DD360ComBox
  185. StartSystemTimeoutDet(Timeout_AuthorizingForStop);
  186. AuthorizingStart();
  187. } else if (idleReq) {
  188. if (pSysConfig->TotalConnectorCount > 1 &&
  189. stopReq != 255 &&
  190. pSysInfo->IsAlternatvieConf == YES) {
  191. idleReq = false;
  192. strcpy((char *)pSysConfig->UserId, "");
  193. } else if ((pGunIndexInfo->AcGunIndex > 0 &&
  194. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  195. pDcChargingInfo->SystemStatus == S_IDLE) {
  196. log_info("// LCM => Authorizing");
  197. setSelGunWaitToAuthor(pSysInfo->CurGunSelected);
  198. // LCM => Authorizing
  199. pSysInfo->SystemPage = _LCM_AUTHORIZING;
  200. // 進入確認卡號狀態
  201. AuthorizingStart();
  202. } else {
  203. strcpy((char *)pSysConfig->UserId, "");
  204. }
  205. } else {
  206. strcpy((char *)pSysConfig->UserId, "");
  207. }
  208. return;
  209. }
  210. bool GetIsCardScan(void)
  211. {
  212. return isCardScan;
  213. }
  214. void SetIsCardScan(bool value)
  215. {
  216. isCardScan = value;
  217. }
  218. void ScannerCardProcess(void)
  219. {
  220. int i = 0;
  221. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  222. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  223. struct WARNING_CODE_INFO *pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  224. SelectGunInfo *ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  225. if (!isDetectPlugin() &&
  226. !isCardScan &&
  227. pSysWarning->Level != WARN_LV_ER /*&&
  228. pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE*/) {
  229. isCardScan = true;
  230. // 處理刷卡及驗證卡號的動作
  231. UserScanFunction();
  232. }
  233. if (pSysInfo->PageIndex == _LCM_AUTHORIZING) {
  234. if(!isAuthorizedComplete())
  235. StartSystemTimeoutDet(Timeout_Authorizing);
  236. //printf("isAuthorizedComplete = %d, %f", isAuthorizedComplete(), ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance);
  237. // 確認驗證卡號完成沒
  238. if (isAuthorizedComplete()
  239. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  240. || pSysConfig->OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING
  241. #endif //!defined DD360 && !defined DD360Audi && !defined DD360ComBox
  242. ) {
  243. //StopSystemTimeoutDet();
  244. StartSystemTimeoutDet(Timeout_WaitBalance);
  245. if (ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance != FAIL_BALANCE_PRICES) {
  246. StopSystemTimeoutDet();
  247. // 判斷後台回覆狀態
  248. if (canStartCharging()) {
  249. // LCM => Authorize complete
  250. pSysInfo->SystemPage = _LCM_AUTHORIZ_COMP;
  251. } else {
  252. // LCM => Authorize fail
  253. pSysInfo->SystemPage = _LCM_AUTHORIZ_FAIL;
  254. strcpy((char *)pSysConfig->UserId, "");
  255. }
  256. } ClearAuthorizedFlag();
  257. } else if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST) {
  258. // 白名單驗證
  259. for (i = 0; i < 10; i++) {
  260. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], "") == EQUAL) {
  261. continue;
  262. }
  263. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], (char *)pSysConfig->UserId) == EQUAL) {
  264. pSysInfo->SystemPage = _LCM_AUTHORIZ_COMP;
  265. ClearAuthorizedFlag();
  266. break;
  267. }
  268. }
  269. }
  270. } else if (pSysInfo->PageIndex == _LCM_AUTHORIZ_FAIL) {
  271. StartSystemTimeoutDet(Timeout_VerifyFail);
  272. isCardScan = false;
  273. } else if (pSysInfo->PageIndex == _LCM_AUTHORIZ_COMP) {
  274. StartSystemTimeoutDet(Timeout_VerifyComp);
  275. } else if (pSysInfo->PageIndex == _LCM_WAIT_FOR_PLUG) {
  276. StartSystemTimeoutDet(Timeout_WaitPlug);
  277. } else {
  278. isCardScan = false;
  279. }
  280. }
  281. static int InitialRfidPort(void)
  282. {
  283. int fd = open(rfidPortName, O_RDWR);
  284. struct termios tios;
  285. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  286. if (fd != FAIL) {
  287. ioctl (fd, TCGETS, &tios);
  288. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  289. tios.c_lflag = 0;
  290. tios.c_iflag = 0;
  291. tios.c_oflag = 0;
  292. tios.c_cc[VMIN] = 0;
  293. tios.c_cc[VTIME] = (uint8_t) 1;
  294. tios.c_lflag = 0;
  295. tcflush(fd, TCIFLUSH);
  296. ioctl(fd, TCSETS, &tios);
  297. }
  298. if (fd < 0) {
  299. pAlarmCode->AlarmEvents.bits.RfidModuleCommFail = 1;
  300. }
  301. return fd;
  302. }
  303. void CreateRfidFork(void)
  304. {
  305. pid_t rfidRecPid;
  306. rfidRecPid = fork();
  307. if (rfidRecPid == 0) {
  308. int fd = -1;
  309. int isContinue = 1;
  310. RFID rfid = {0};
  311. fd = InitialRfidPort();
  312. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  313. //log_info("RFID fork Child's PID is %d", getpid());
  314. while (isContinue) {
  315. usleep(500000);
  316. // 刷卡判斷
  317. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  318. !pSysConfig->isRFID) {
  319. continue;
  320. }
  321. if (getRequestCardSN(fd, 0, &rfid) == false) {
  322. continue;
  323. }
  324. //log_info("Get Card..-%s- ", pSysConfig->UserId);
  325. if (strlen((char *)pSysConfig->UserId) != 0) {
  326. continue;
  327. }
  328. if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_LITTLE) {
  329. switch (rfid.snType) {
  330. case RFID_SN_TYPE_6BYTE:
  331. sprintf((char *) pSysConfig->UserId,
  332. "%02X%02X%02X%02X%02X%02X",
  333. rfid.currentCard[0], rfid.currentCard[1],
  334. rfid.currentCard[2], rfid.currentCard[3],
  335. rfid.currentCard[4], rfid.currentCard[5]);
  336. break;
  337. case RFID_SN_TYPE_7BYTE:
  338. sprintf((char *) pSysConfig->UserId,
  339. "%02X%02X%02X%02X%02X%02X%02X",
  340. rfid.currentCard[0], rfid.currentCard[1],
  341. rfid.currentCard[2], rfid.currentCard[3],
  342. rfid.currentCard[4], rfid.currentCard[5],
  343. rfid.currentCard[6]);
  344. break;
  345. case RFID_SN_TYPE_10BYTE:
  346. sprintf((char *) pSysConfig->UserId,
  347. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  348. rfid.currentCard[0], rfid.currentCard[1],
  349. rfid.currentCard[2], rfid.currentCard[3],
  350. rfid.currentCard[4], rfid.currentCard[5],
  351. rfid.currentCard[6], rfid.currentCard[7],
  352. rfid.currentCard[8], rfid.currentCard[9]);
  353. break;
  354. case RFID_SN_TYPE_4BYTE:
  355. sprintf((char *) pSysConfig->UserId,
  356. "%02X%02X%02X%02X",
  357. rfid.currentCard[0], rfid.currentCard[1],
  358. rfid.currentCard[2], rfid.currentCard[3]);
  359. break;
  360. }
  361. } else if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_BIG) {
  362. switch (rfid.snType) {
  363. case RFID_SN_TYPE_6BYTE:
  364. sprintf((char *) pSysConfig->UserId,
  365. "%02X%02X%02X%02X%02X%02X",
  366. rfid.currentCard[5], rfid.currentCard[4],
  367. rfid.currentCard[3], rfid.currentCard[2],
  368. rfid.currentCard[1], rfid.currentCard[0]);
  369. break;
  370. case RFID_SN_TYPE_7BYTE:
  371. sprintf((char *) pSysConfig->UserId,
  372. "%02X%02X%02X%02X%02X%02X%02X",
  373. rfid.currentCard[6], rfid.currentCard[5],
  374. rfid.currentCard[4], rfid.currentCard[3],
  375. rfid.currentCard[2], rfid.currentCard[1],
  376. rfid.currentCard[0]);
  377. break;
  378. case RFID_SN_TYPE_10BYTE:
  379. sprintf((char *) pSysConfig->UserId,
  380. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  381. rfid.currentCard[9], rfid.currentCard[8],
  382. rfid.currentCard[7], rfid.currentCard[6],
  383. rfid.currentCard[5], rfid.currentCard[4],
  384. rfid.currentCard[3], rfid.currentCard[2],
  385. rfid.currentCard[1], rfid.currentCard[0]);
  386. break;
  387. case RFID_SN_TYPE_4BYTE:
  388. sprintf((char *) pSysConfig->UserId,
  389. "%02X%02X%02X%02X",
  390. rfid.currentCard[3], rfid.currentCard[2],
  391. rfid.currentCard[1], rfid.currentCard[0]);
  392. break;
  393. }
  394. }
  395. log_info("card number = %s", pSysConfig->UserId);
  396. }
  397. }
  398. }