Module_Authorize.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Module_Authorize.c
  3. *
  4. * Created on: 2021年11月16日
  5. * Author: 7978
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/can.h>
  19. #include <linux/can/raw.h>
  20. #include <linux/wireless.h>
  21. #include <arpa/inet.h>
  22. #include <netinet/in.h>
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include <stdio.h> /*標準輸入輸出定義*/
  26. #include <stdlib.h> /*標準函數庫定義*/
  27. #include <unistd.h> /*Unix 標準函數定義*/
  28. #include <fcntl.h> /*檔控制定義*/
  29. #include <termios.h> /*PPSIX 終端控制定義*/
  30. #include <errno.h> /*錯誤號定義*/
  31. #include <errno.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <ctype.h>
  35. #include <ifaddrs.h>
  36. #include <signal.h>
  37. #include <net/if_arp.h>
  38. #include "../../define.h"
  39. #include "Module_Authorize.h"
  40. #include "Module_EvComm.h"
  41. #include "Config.h"
  42. #include "Common.h"
  43. #define AUTHORIZE_INTERVAL 100000 // unit: 1us
  44. #define AUTHORIZE_WAIT_OCPP_TIMEOUT 30 // unit: 1s
  45. #define SYS_AUTHORIZE_COMP_TIMEOUT 1 // unit: 1s
  46. #define AUTH_TIME_OFFSET_TIME 3 // unit: 1s
  47. #define AUTO_START_CHARGING "AutoStartCharging"
  48. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  49. ChargerInfoData *ShmChargerInfo;
  50. struct ChargingInfoData *chargingInfo[GENERAL_GUN_QUANTITY];
  51. struct OCPP16Data *ShmOCPP16Data;
  52. struct OCPP20Data *ShmOCPP20Data;
  53. struct timespec _SysAuth_Time;
  54. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  55. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  56. unsigned char _preSysAuthStatus = 0;
  57. unsigned char _preGunAuthStatus[GENERAL_GUN_QUANTITY];
  58. unsigned char _preDispenserAuthStatus[GENERAL_GUN_QUANTITY];
  59. //==========================================
  60. // Init all share memory
  61. //==========================================
  62. int InitShareMemory(void)
  63. {
  64. int result = PASS;
  65. int MeterSMId;
  66. //creat ShmSysConfigAndInfo
  67. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  68. {
  69. #ifdef SystemLogMessage
  70. LOG_ERROR("shmget ShmSysConfigAndInfo NG");
  71. #endif
  72. result = FAIL;
  73. }
  74. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  75. {
  76. #ifdef SystemLogMessage
  77. LOG_ERROR("shmat ShmSysConfigAndInfo NG");
  78. #endif
  79. result = FAIL;
  80. }
  81. else
  82. {
  83. }
  84. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
  85. {
  86. #ifdef SystemLogMessage
  87. LOG_ERROR("shmat ChargerInfoData NG");
  88. #endif
  89. result = FAIL;
  90. }
  91. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  92. {
  93. #ifdef SystemLogMessage
  94. LOG_ERROR("shmat ChargerInfoData NG");
  95. #endif
  96. result = FAIL;
  97. }
  98. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  99. {
  100. #ifdef SystemLogMessage
  101. LOG_ERROR("shmat ShmOCPP16Data NG");
  102. #endif
  103. result = FAIL;
  104. }
  105. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  106. {
  107. LOG_ERROR("shmat ShmOCPP16Data NG");
  108. result = FAIL;
  109. }
  110. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), 0777)) < 0)
  111. {
  112. LOG_ERROR("[main]CreatShareMemory:shmget OCPP20Data NG\n");
  113. result = FAIL;
  114. }
  115. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  116. {
  117. LOG_ERROR("[main]CreatShareMemory:shmat OCPP20Data NG\n");
  118. result = FAIL;
  119. }
  120. return result;
  121. }
  122. void InitialConnector(void)
  123. {
  124. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  125. {
  126. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  127. }
  128. }
  129. //===============================================
  130. // Ocpp Remote Start
  131. //===============================================
  132. bool Is_Ocpp_RemoteStartReq(int gun_index)
  133. {
  134. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  135. {
  136. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  137. }
  138. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  139. {
  140. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  141. }
  142. return false;
  143. }
  144. void Clean_Ocpp_RemoteStartReq(int gun_index)
  145. {
  146. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  147. {
  148. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  149. return;
  150. }
  151. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  152. {
  153. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  154. return;
  155. }
  156. }
  157. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  158. {
  159. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  160. {
  161. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  162. return;
  163. }
  164. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  165. {
  166. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  167. return;
  168. }
  169. strcpy(idTag, "");
  170. }
  171. //===============================================
  172. // Ocpp AuthorizeRemoteTxRequests
  173. //===============================================
  174. bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
  175. {
  176. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  177. {
  178. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
  179. }
  180. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  181. {
  182. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  183. }
  184. return false;
  185. }
  186. //===============================================
  187. // Ocpp AuthorizeReq
  188. //===============================================
  189. void Set_Ocpp_AuthorizeReq(void)
  190. {
  191. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  192. {
  193. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  194. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  195. return;
  196. }
  197. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  198. {
  199. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
  200. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
  201. return;
  202. }
  203. }
  204. bool Is_Ocpp_AuthorizeConf(void)
  205. {
  206. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  207. {
  208. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  209. }
  210. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  211. {
  212. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  213. }
  214. return false;
  215. }
  216. bool Is_Ocpp_Authorize_Status(char *status)
  217. {
  218. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  219. {
  220. return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  221. }
  222. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  223. {
  224. return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
  225. }
  226. return false;
  227. }
  228. //===============================================
  229. // Ocpp Reserve Now
  230. //===============================================
  231. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  232. {
  233. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  234. {
  235. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  236. return;
  237. }
  238. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  239. {
  240. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  241. return;
  242. }
  243. strcpy(idTag, "");
  244. return;
  245. }
  246. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  247. {
  248. char ReserveIdTag[32];
  249. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  250. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  251. }
  252. void SetAuthorize(AuthorizingInfoData *AuthInfo, unsigned char type)
  253. {
  254. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, AuthInfo->AuthId, 32);
  255. AuthInfo->AuthStatus = _AuthorizeStatus_Busy;
  256. AuthInfo->AuthType = type;
  257. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = AuthInfo->AuthTarget;
  258. ShmSysConfigAndInfo->SysInfo.AuthorizedType = type;
  259. }
  260. bool IsRemoteStartRequest(int *connector)
  261. {
  262. AuthorizingInfoData *AuthInfo;
  263. // remote start check
  264. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  265. {
  266. if(Is_Ocpp_RemoteStartReq(index))
  267. {
  268. AuthInfo = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo;
  269. AuthInfo->AuthTarget = index + 1;
  270. Get_Ocpp_RemoteStartIdTag(index, (char *)AuthInfo->AuthId);
  271. SetAuthorize(AuthInfo, _AuthType_RemoteStart);
  272. Clean_Ocpp_RemoteStartReq(index);
  273. AuthInfo->AuthRequest = NO;
  274. *connector = index;
  275. return true;
  276. }
  277. }
  278. return false;
  279. }
  280. bool IsAuthorizingRequest(int *connector)
  281. {
  282. AuthorizingInfoData *AuthInfo;
  283. // connector authorize check
  284. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  285. {
  286. AuthInfo = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo;
  287. if(AuthInfo->AuthStatus == _AuthorizeStatus_Idle && AuthInfo->AuthRequest == YES)
  288. {
  289. if(strlen((char *)AuthInfo->AuthId) > 0)
  290. {
  291. SetAuthorize(AuthInfo, _AuthType_RFID);
  292. AuthInfo->AuthRequest = NO;
  293. *connector = index;
  294. return true;
  295. }
  296. }
  297. }
  298. return false;
  299. }
  300. bool IsDispenserAuthorizingRequest(int *dispenser)
  301. {
  302. AuthorizingInfoData *AuthInfo;
  303. // AUTO_GUN_SELECTION authorize check
  304. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  305. {
  306. AuthInfo = &ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo;
  307. if(AuthInfo->AuthStatus == _AuthorizeStatus_Idle && AuthInfo->AuthRequest == YES)
  308. {
  309. if(strlen((char *)AuthInfo->AuthId) > 0)
  310. {
  311. SetAuthorize(AuthInfo, _AuthType_RFID);
  312. AuthInfo->AuthRequest = NO;
  313. *dispenser = index;
  314. return true;
  315. }
  316. }
  317. }
  318. return false;
  319. }
  320. unsigned char Chk_OfflinePolicy(char *idTag)
  321. {
  322. if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  323. {
  324. AUTH_INFO("*********** Offline Policy [No Charging] ***********");
  325. return _AuthResult_Invalid;
  326. }
  327. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  328. {
  329. // set authorize pass when offline policy is free charge
  330. AUTH_INFO("*********** Offline Policy [Free Charging] ***********");
  331. return _AuthResult_Valid;
  332. }
  333. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  334. {
  335. BOOL find = false;
  336. // check white list
  337. for(int i = 0; i < 10; i++)
  338. {
  339. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  340. {
  341. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], idTag) == EQUAL)
  342. {
  343. find = true;
  344. AUTH_INFO("*********** [White Card OK] ***********");
  345. break;
  346. }
  347. }
  348. }
  349. if(find == false)
  350. {
  351. if((ShmChargerInfo->AuthInfo.AuthMode.bits.AutoStartEnable || ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) &&
  352. strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, AUTO_START_CHARGING) == EQUAL)
  353. {
  354. find = true;
  355. AUTH_INFO("******** [Auto Start Charging] ********");
  356. }
  357. }
  358. AUTH_INFO("*********** Local Authorization %s ***********", find ? "OK" : "NG");
  359. if(find)
  360. {
  361. return _AuthResult_Valid;
  362. }
  363. else
  364. {
  365. return _AuthResult_Invalid;
  366. }
  367. }
  368. else
  369. {
  370. AUTH_INFO("*********** [Invalid Policy] ***********");
  371. return _AuthResult_Invalid;
  372. }
  373. return _AuthResult_Invalid;
  374. }
  375. void SysAuthInitial(void)
  376. {
  377. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, 32);
  378. ShmSysConfigAndInfo->SysInfo.AuthorizedDispenser = 0;
  379. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = 0;
  380. ShmSysConfigAndInfo->SysInfo.AuthorizedType = _AuthResult_None;
  381. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Idle;
  382. }
  383. void AuthTimeoutProcess(void)
  384. {
  385. int timeout = 0;
  386. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  387. {
  388. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus == _AuthorizeStatus_End)
  389. {
  390. if(_preGunAuthStatus[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus)
  391. {
  392. GetClockTime(&_GunAuth_Time[index]);
  393. }
  394. timeout = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus == _AuthResult_Valid ?
  395. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout : AUTHORIZE_COMPLETED_TIME;
  396. timeout += AUTH_TIME_OFFSET_TIME;
  397. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= timeout)
  398. {
  399. AUTH_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  400. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  401. }
  402. }
  403. _preGunAuthStatus[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus;
  404. }
  405. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  406. {
  407. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthStatus == _AuthorizeStatus_End)
  408. {
  409. if(_preDispenserAuthStatus[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthStatus)
  410. {
  411. GetClockTime(&_DispenserAuth_Time[index]);
  412. }
  413. timeout = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  414. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout : AUTHORIZE_COMPLETED_TIME;
  415. timeout += AUTH_TIME_OFFSET_TIME;
  416. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= timeout)
  417. {
  418. AUTH_INFO("*********** Dispenser %d Authorizing Initial ***********", index + 1);
  419. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  420. }
  421. }
  422. _preDispenserAuthStatus[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthStatus;
  423. }
  424. }
  425. int main(void)
  426. {
  427. int _dispenser = 0, _connector = 0;
  428. unsigned char _authResult = _AuthResult_None;
  429. bool _autoSelection = false;
  430. bool _needOcppAuthorize = false;
  431. bool _keepRun = false;
  432. if(InitShareMemory() == FAIL)
  433. {
  434. #ifdef SystemLogMessage
  435. LOG_ERROR("InitShareMemory NG");
  436. #endif
  437. sleep(5);
  438. return 0;
  439. }
  440. InitialConnector();
  441. while(1)
  442. {
  443. _keepRun = false;
  444. AuthTimeoutProcess();
  445. switch(ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  446. {
  447. case _AuthorizeStatus_Idle:
  448. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  449. {
  450. AUTH_INFO("[SysAuthStatus Idle]");
  451. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  452. _authResult = _AuthResult_None;
  453. _autoSelection = false;
  454. }
  455. if(IsRemoteStartRequest(&_connector))
  456. {
  457. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
  458. _keepRun = true;
  459. AUTH_INFO("Gun %d Remote Start Authorize", _connector + 1);
  460. break;
  461. }
  462. if(IsAuthorizingRequest(&_connector))
  463. {
  464. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
  465. _keepRun = true;
  466. AUTH_INFO("Gun %d RFID Authorize", _connector + 1);
  467. break;
  468. }
  469. if(IsDispenserAuthorizingRequest(&_dispenser))
  470. {
  471. _autoSelection = true;
  472. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
  473. _keepRun = true;
  474. AUTH_INFO("Dispenser %d AutoSelection Authorize", index + 1);
  475. break;
  476. }
  477. break;
  478. case _AuthorizeStatus_Wait:
  479. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  480. {
  481. AUTH_INFO("[SysAuthStatus Wait]");
  482. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  483. }
  484. _needOcppAuthorize = false;
  485. if(ShmSysConfigAndInfo->SysInfo.AuthorizedType == _AuthType_RemoteStart)
  486. {
  487. if(Is_Ocpp_AuthorizeRemoteTxRequests())
  488. {
  489. _needOcppAuthorize = true;
  490. }
  491. else
  492. {
  493. AUTH_INFO("*********** Gun %d Remote Start Authorizing Bypass ***********",
  494. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget);
  495. _authResult = _AuthResult_Valid;
  496. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  497. _keepRun = true;
  498. break;
  499. }
  500. }
  501. else if(ShmSysConfigAndInfo->SysInfo.AuthorizedType == _AuthType_RFID)
  502. {
  503. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  504. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  505. !ShmChargerInfo->AuthInfo.AuthMode.bits.AutoStartEnable)
  506. {
  507. _needOcppAuthorize = true;
  508. }
  509. else
  510. {
  511. _authResult = Chk_OfflinePolicy((char *)ShmSysConfigAndInfo->SysConfig.UserId);
  512. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  513. _keepRun = true;
  514. break;
  515. }
  516. }
  517. if(_needOcppAuthorize)
  518. {
  519. Set_Ocpp_AuthorizeReq();
  520. AUTH_INFO("*********** Set OPCC AuthorizeReq ***********");
  521. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Busy;
  522. }
  523. break;
  524. case _AuthorizeStatus_Busy:
  525. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  526. {
  527. AUTH_INFO("[SysAuthStatus Busy]");
  528. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  529. GetClockTime(&_SysAuth_Time);
  530. }
  531. if(GetTimeoutValue(_SysAuth_Time) / uSEC_VAL >= AUTHORIZE_WAIT_OCPP_TIMEOUT)
  532. {
  533. AUTH_INFO("*********** Ocpp Authorizing Timeout ***********");
  534. _authResult = _AuthResult_Invalid;
  535. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  536. _keepRun = true;
  537. break;
  538. }
  539. else
  540. {
  541. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  542. {
  543. if(Is_Ocpp_AuthorizeConf())
  544. {
  545. BOOL accept = false;
  546. if(Is_Ocpp_Authorize_Status("Accepted"))
  547. {
  548. accept = true;
  549. }
  550. AUTH_INFO("*********** OCPP Authorize %s ***********", accept ? "OK" : "NG");
  551. _authResult = accept ? _AuthResult_Valid : _AuthResult_Invalid;
  552. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  553. _keepRun = true;
  554. break;
  555. }
  556. }
  557. else
  558. {
  559. AUTH_INFO("*********** OCPP Authorize Disconnected ***********");
  560. _authResult = Chk_OfflinePolicy((char *)ShmSysConfigAndInfo->SysConfig.UserId);
  561. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  562. _keepRun = true;
  563. break;
  564. }
  565. }
  566. break;
  567. case _AuthorizeStatus_Done:
  568. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  569. {
  570. AUTH_INFO("[SysAuthStatus Done]");
  571. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  572. GetClockTime(&_SysAuth_Time);
  573. }
  574. if(_autoSelection)
  575. {
  576. AUTH_INFO("*********** Dispense %d RFID Authorize %s ***********", _dispenser + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  577. if(_authResult == _AuthResult_Valid)
  578. {
  579. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, AUTO_START_CHARGING) == EQUAL)
  580. {
  581. sprintf((char*)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId, "%s",
  582. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  583. }
  584. }
  585. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthResult = _authResult;
  586. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthStatus = _AuthorizeStatus_End;
  587. }
  588. else
  589. {
  590. unsigned char connector = 0;
  591. connector = ShmSysConfigAndInfo->SysInfo.AuthorizedTarget - 1;
  592. if(chargingInfo[connector]->SystemStatus == S_IDLE && _authResult == _AuthResult_Valid)
  593. {
  594. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, AUTO_START_CHARGING) == EQUAL)
  595. {
  596. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%s%s",
  597. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber);
  598. }
  599. memcpy(chargingInfo[connector]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  600. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RFID)
  601. {
  602. AUTH_INFO("*********** Gun %d RFID Authorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  603. }
  604. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RemoteStart)
  605. {
  606. AUTH_INFO("*********** Gun %d RemoteAuthorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  607. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartRequest = true;
  608. }
  609. }
  610. else if(chargingInfo[connector]->SystemStatus == S_RESERVATION && _authResult == _AuthResult_Valid)
  611. {
  612. _authResult = Is_Ocpp_ReserveNowIdTag(connector, (char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthId) ? _AuthResult_Valid : _AuthResult_Invalid;
  613. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RFID)
  614. {
  615. AUTH_INFO("*********** Gun %d RFID Reserve Authorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  616. }
  617. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RemoteStart)
  618. {
  619. AUTH_INFO("*********** Gun %d Reserve RemoteAuthorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  620. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartRequest = true;
  621. }
  622. }
  623. else
  624. {
  625. AUTH_INFO("*********** Gun %d Authorize OK But Status Fail (%d) ***********", connector + 1, chargingInfo[connector]->SystemStatus);
  626. _authResult = _AuthResult_Invalid;
  627. }
  628. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult = _authResult;
  629. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthStatus = _AuthorizeStatus_End;
  630. }
  631. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
  632. break;
  633. case _AuthorizeStatus_End:
  634. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  635. {
  636. AUTH_INFO("[SysAuthStatus End]");
  637. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  638. GetClockTime(&_SysAuth_Time);
  639. }
  640. if(GetTimeoutValue(_SysAuth_Time) / uSEC_VAL >= SYS_AUTHORIZE_COMP_TIMEOUT)
  641. {
  642. AUTH_INFO("*********** PowerCabinet Authorize Completed ***********");
  643. SysAuthInitial();
  644. }
  645. break;
  646. default:
  647. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  648. {
  649. AUTH_INFO("[SysAuthStatus Unknown]");
  650. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  651. }
  652. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
  653. break;
  654. }
  655. if(!_keepRun)
  656. {
  657. usleep(AUTHORIZE_INTERVAL);
  658. }
  659. }
  660. return 0;
  661. }