Module_Authorize.c 33 KB

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