RFID.c 16 KB

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