RFID.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 DcCommonInfo* ShmDcCommonData = NULL;
  15. static bool isCardScan = false;
  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. bool isAutorCompleteHandle(/*uint8_t *authorizeIndex*/)
  34. {
  35. // uint8_t i = 0;
  36. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  37. //struct ChargingInfoData *pDcChargingInfo = NULL;
  38. //struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  39. // 透過後臺停止充電的判斷
  40. if (isAuthorizedComplete()) {
  41. // 判斷後台回覆狀態
  42. if (canStartCharging() == false) {
  43. strcpy((char *)pSysConfig->UserId, "");
  44. ClearAuthorizedFlag();
  45. }
  46. return true;
  47. }
  48. return false;
  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. bool RfidStopCharging(void)
  81. {
  82. //當前沒有選槍
  83. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  84. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  85. if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL ) {
  86. strcpy((char *)pSysConfig->UserId, "");
  87. return false;
  88. }
  89. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  90. if (pDcChargingInfo->isRemoteStart) {
  91. } else {
  92. if (strcmp((char *)pSysConfig->UserId, (char *)pDcChargingInfo->StartUserId) == EQUAL) {
  93. ChargingTerminalProcess(pSysInfo->CurGunSelected);
  94. strcpy((char *)pSysConfig->UserId, "");
  95. log_info("index = %d, card number = %s, UserId = %s ",
  96. pSysInfo->CurGunSelected,
  97. pDcChargingInfo->StartUserId,
  98. pSysConfig->UserId);
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. }
  104. return false;
  105. }
  106. static void UserScanFunction(void)
  107. {
  108. bool idleReq = false;
  109. uint8_t i = 0;
  110. uint8_t stopReq = NO_DEFINE;
  111. char value[32] = {0};
  112. static uint8_t _authorizeIndex = NO_DEFINE;
  113. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  114. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  115. struct ChargingInfoData *pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  116. struct ChargingInfoData *pDcChargingInfo = NULL;
  117. GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
  118. // 當前非驗證的狀態
  119. if (IsAuthorizingMode()) {
  120. isAutorCompleteHandle(/*&_authorizeIndex*/);
  121. }
  122. //當前沒有選槍
  123. if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL) {
  124. strcpy((char *)pSysConfig->UserId, "");
  125. return;
  126. }
  127. // 先判斷現在是否可以提供刷卡
  128. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  129. // 2. 停止充電
  130. //if (pSysInfo->PageIndex == _LCM_ERROR) {
  131. if (pSysInfo->SystemPage == _LCM_MAINTAIN) {
  132. strcpy((char *)pSysConfig->UserId, "");
  133. return;
  134. }
  135. /*
  136. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  137. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  138. if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  139. stopReq = i;
  140. }
  141. if ((pDcChargingInfo->SystemStatus == S_AUTHORIZING &&
  142. pDcChargingInfo->IsAvailable == YES) ||
  143. (pGunIndexInfo->AcGunIndex > 0 &&
  144. pAcChargingInfo->SystemStatus == S_AUTHORIZING &&
  145. pAcChargingInfo->IsAvailable)
  146. ) {
  147. idleReq = true;
  148. }
  149. }
  150. if (pGunIndexInfo->AcGunIndex > 0 &&
  151. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  152. pAcChargingInfo->SystemStatus == S_CHARGING) {
  153. stopReq = DEFAULT_AC_INDEX;
  154. }
  155. */
  156. if (strlen((char *)pSysConfig->UserId) <= 0) {
  157. return;
  158. }
  159. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  160. // Stop Charging For AC
  161. /*if (pGunIndexInfo->AcGunIndex > 0 &&
  162. stopReq == DEFAULT_AC_INDEX &&
  163. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  164. log_info("ac stop charging ");
  165. log_info("index = %d, card number = %s, UserId = %s ",
  166. pSysInfo->CurGunSelectedByAc,
  167. pAcChargingInfo->StartUserId,
  168. pSysConfig->UserId);
  169. memset(value, 0, sizeof(value));
  170. memcpy(value,
  171. (uint8_t *)pAcChargingInfo->StartUserId,
  172. ARRAY_SIZE(pAcChargingInfo->StartUserId));
  173. if (strcmp((char *)pSysConfig->UserId, value) == EQUAL) {
  174. AcChargingTerminalProcess();
  175. }
  176. strcpy((char *)pSysConfig->UserId, "");
  177. } else if (stopReq < pSysConfig->TotalConnectorCount &&
  178. pDcChargingInfo->SystemStatus == S_CHARGING &&
  179. (pGunIndexInfo->AcGunIndex <= 0 ||
  180. (pGunIndexInfo->AcGunIndex > 0 &&
  181. pSysInfo->CurGunSelectedByAc == NO_DEFINE))
  182. ) {
  183. // Stop Charging
  184. if (RfidStopCharging())
  185. return;
  186. if (pGunIndexInfo->AcGunIndex > 0 &&
  187. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  188. _authorizeIndex = pSysInfo->CurGunSelectedByAc;
  189. } else {
  190. _authorizeIndex = pSysInfo->CurGunSelected;
  191. }
  192. strcpy((char *)pSysConfig->UserId, "");
  193. return;
  194. } else if (idleReq) {
  195. if (pSysConfig->TotalConnectorCount > 1 &&
  196. stopReq != 255 &&
  197. pSysInfo->IsAlternatvieConf == YES) {
  198. idleReq = false;
  199. strcpy((char *)pSysConfig->UserId, "");
  200. } else if ((pGunIndexInfo->AcGunIndex > 0 &&
  201. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  202. pDcChargingInfo->SystemStatus == S_AUTHORIZING) {*/
  203. if (pDcChargingInfo->SystemStatus == S_AUTHORIZING ) {
  204. log_info("// LCM => Authorizing");
  205. setSelGunWaitToAuthor(pSysInfo->CurGunSelected);
  206. // LCM => Authorizing
  207. pSysInfo->SystemPage = _LCM_START_AUTHORIZING;
  208. // 進入確認卡號狀態
  209. AuthorizingStart();
  210. } else {
  211. strcpy((char *)pSysConfig->UserId, "");
  212. }
  213. return;
  214. }
  215. bool GetIsCardScan(void)
  216. {
  217. return isCardScan;
  218. }
  219. void SetIsCardScan(bool value)
  220. {
  221. isCardScan = value;
  222. }
  223. void AuthorizeToCharge()
  224. {
  225. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  226. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  227. SelectGunInfo *ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  228. struct ChargingInfoData* pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  229. ShmDcCommonData = (DcCommonInfo*)GetShmDcCommonData();
  230. if (!isAuthorizedComplete()) {
  231. //StartSystemTimeoutDet(Timeout_Authorizing);
  232. } else {
  233. StopGunInfoTimeoutDet(pSysInfo->CurGunSelected);
  234. StartSystemTimeoutDet(Timeout_WaitBalance);
  235. if (ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance != FAIL_BALANCE_PRICES) {
  236. StopSystemTimeoutDet();
  237. // 判斷後台回覆狀態
  238. if (canStartCharging()) {
  239. // LCM => Authorize complete
  240. if (!pDcChargingInfo->ConnectorPlugIn)
  241. pSysInfo->SystemPage = _LCM_WAIT_PLUGIN;
  242. log_info("Wait Gun(%d) plugin",pSysInfo->CurGunSelected);
  243. DetectPluginStart();
  244. log_info("Gun(%d) Balance: %f",pSysInfo->CurGunSelected,
  245. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance);
  246. } else {
  247. // LCM => Authorize fail
  248. pSysInfo->SystemPage = _LCM_START_AUTHORIZE_FAIL;
  249. strcpy((char *)pSysConfig->UserId, "");
  250. ShmDcCommonData->AuthroizeType = IdTokenType_Central;
  251. }
  252. }
  253. ClearAuthorizedFlag();
  254. }
  255. }
  256. void ScannerCardProcess(void)
  257. {
  258. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  259. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  260. struct WARNING_CODE_INFO *pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  261. if (!isDetectPlugin() && pSysInfo->SystemPage == _LCM_START_SCAN &&
  262. pSysWarning->Level != WARN_LV_ER /*&&
  263. pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE*/) {
  264. isCardScan = true;
  265. // 處理刷卡及驗證卡號的動作
  266. UserScanFunction();
  267. }
  268. if (pSysInfo->SystemPage == _LCM_START_AUTHORIZING) {
  269. AuthorizeToCharge();
  270. } else if (pSysInfo->SystemPage == _LCM_START_AUTHORIZE_FAIL) {
  271. StartSystemTimeoutDet(Timeout_VerifyFail);
  272. isCardScan = false;
  273. } else if (pSysInfo->SystemPage == _LCM_WAIT_PLUGIN) {
  274. StartSystemTimeoutDet(Timeout_WaitPlug);
  275. } else {
  276. isCardScan = false;
  277. }
  278. }
  279. static int InitialRfidPort(void)
  280. {
  281. int fd = open(rfidPortName, O_RDWR);
  282. struct termios tios;
  283. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  284. if (fd != FAIL) {
  285. ioctl (fd, TCGETS, &tios);
  286. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  287. tios.c_lflag = 0;
  288. tios.c_iflag = 0;
  289. tios.c_oflag = 0;
  290. tios.c_cc[VMIN] = 0;
  291. tios.c_cc[VTIME] = (uint8_t) 1;
  292. tios.c_lflag = 0;
  293. tcflush(fd, TCIFLUSH);
  294. ioctl(fd, TCSETS, &tios);
  295. }
  296. if (fd < 0) {
  297. pAlarmCode->AlarmEvents.bits.RfidModuleCommFail = 1;
  298. }
  299. return fd;
  300. }
  301. void CreateRfidFork(void)
  302. {
  303. pid_t rfidRecPid;
  304. rfidRecPid = fork();
  305. if (rfidRecPid == 0) {
  306. int fd = -1;
  307. int isContinue = 1;
  308. RFID rfid = {0};
  309. fd = InitialRfidPort();
  310. int module_type = MODULE_EWT;
  311. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  312. ShmDcCommonData = (DcCommonInfo*)GetShmDcCommonData();
  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, module_type, &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. if (strlen((char*)pSysConfig->UserId) != 0) {
  397. ShmDcCommonData->AuthroizeType = IdTokenType_ISO14443;
  398. }
  399. if (rfid.cardType == ISO14443A) {
  400. sethaltCard(fd, module_type);
  401. } else if (rfid.cardType == IS014443B) {
  402. } else if (rfid.cardType == FELICA) {
  403. }
  404. }
  405. }
  406. }