RFID.c 17 KB

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