RFID.c 17 KB

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