RFID.c 16 KB

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