Module_Authorize.c 29 KB

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