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