RFID.c 17 KB

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