Module_Authorize.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. //===============================================
  318. // Ocpp AuthorizeTimeout
  319. //===============================================
  320. int Get_Ocpp_AuthorizeTimeout(void)
  321. {
  322. int timeout = AUTHORIZE_WAIT_OCPP_TIMEOUT;
  323. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  324. {
  325. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeTimeout].ItemData, "") != 0)
  326. {
  327. timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeTimeout].ItemData);
  328. }
  329. }
  330. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  331. {
  332. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[OCPPCommCtrlr_AuthorizeTimeout].variableAttribute[0].value, "") != 0)
  333. {
  334. timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[OCPPCommCtrlr_AuthorizeTimeout].variableAttribute[0].value);
  335. }
  336. }
  337. return timeout;
  338. }
  339. void SetAuthorize(AuthorizingInfoData *AuthInfo, unsigned char type)
  340. {
  341. if(AuthInfo->AuthIdType == _Type_MacAddress)
  342. {
  343. char prefix[501];
  344. char evccid[501 + 32];
  345. memset(prefix, 0x00, sizeof(prefix));
  346. memset(evccid, 0x00, sizeof(evccid));
  347. Get_Ocpp_EVCCID_Prefix(prefix);
  348. sprintf(evccid, "%s%s", prefix, (char *)&AuthInfo->AuthId);
  349. strncpy((char *)&AuthInfo->AuthId, evccid, sizeof(AuthInfo->AuthId));
  350. AUTH_INFO("*********** EVCCID [%s] ***********", AuthInfo->AuthId);
  351. }
  352. else if(strcmp((char *)&AuthInfo->AuthId, AUTO_START_CHARGING) == EQUAL)
  353. {
  354. Get_Ocpp_FreevendIdtag((char *)&AuthInfo->AuthId);
  355. _isAutoStart = true;
  356. AUTH_INFO("*********** FreevendId [%s] ***********", (char *)&AuthInfo->AuthId);
  357. if(strlen((char *)&AuthInfo->AuthId) == 0)
  358. {
  359. sprintf((char*)&AuthInfo->AuthId, "%s", ShmSysConfigAndInfo->SysConfig.SerialNumber);
  360. AUTH_INFO("*********** IdTag:SerialNumber [%s] ***********", (char *)&AuthInfo->AuthId);
  361. }
  362. }
  363. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, AuthInfo->AuthId, 32);
  364. AuthInfo->AuthStatus = _AuthorizeStatus_Busy;
  365. AuthInfo->AuthType = type;
  366. if(type == _AuthType_RemoteStart)
  367. {
  368. AuthInfo->AuthIdType = _Type_Central;
  369. }
  370. else if(_isAutoStart)
  371. {
  372. AuthInfo->AuthIdType = _Type_NoAuthorization;
  373. }
  374. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = AuthInfo->AuthTarget;
  375. ShmSysConfigAndInfo->SysInfo.AuthorizedType = type;
  376. _idType = AuthInfo->AuthIdType;
  377. }
  378. void CheckAutoStartChargingToSNToOcpp(void)
  379. {
  380. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.UserId, AUTO_START_CHARGING) == EQUAL)
  381. {
  382. int len = strlen((char *)&ShmSysConfigAndInfo->SysConfig.SerialNumber);
  383. if(len > 0 && len < sizeof(ShmSysConfigAndInfo->SysConfig.UserId))
  384. {
  385. strcpy((char *)&ShmSysConfigAndInfo->SysConfig.UserId, (char *)&ShmSysConfigAndInfo->SysConfig.SerialNumber);
  386. }
  387. else
  388. {
  389. strcpy((char *)&ShmSysConfigAndInfo->SysConfig.UserId, DEFAULT_SN);
  390. }
  391. AUTH_INFO("Received Auto Start Charging IdTag, Set SN [%s] To Authorize", ShmSysConfigAndInfo->SysConfig.UserId);
  392. }
  393. }
  394. bool IsRemoteStartRequest(int *connector)
  395. {
  396. AuthorizingInfoData *AuthInfo;
  397. // remote start check
  398. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  399. {
  400. if(Is_Ocpp_RemoteStartReq(index))
  401. {
  402. AuthInfo = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo;
  403. AuthInfo->AuthTarget = index + 1;
  404. Get_Ocpp_RemoteStartIdTag(index, (char *)AuthInfo->AuthId);
  405. SetAuthorize(AuthInfo, _AuthType_RemoteStart);
  406. Clean_Ocpp_RemoteStartReq(index);
  407. AuthInfo->AuthRequest = NO;
  408. *connector = index;
  409. return true;
  410. }
  411. }
  412. return false;
  413. }
  414. bool IsAuthorizingRequest(int *connector)
  415. {
  416. AuthorizingInfoData *AuthInfo;
  417. // connector authorize check
  418. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  419. {
  420. AuthInfo = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo;
  421. if(AuthInfo->AuthStatus == _AuthorizeStatus_Idle && AuthInfo->AuthRequest == YES)
  422. {
  423. if(strlen((char *)AuthInfo->AuthId) > 0)
  424. {
  425. SetAuthorize(AuthInfo, _AuthType_RFID);
  426. AuthInfo->AuthRequest = NO;
  427. *connector = index;
  428. return true;
  429. }
  430. }
  431. }
  432. return false;
  433. }
  434. bool IsDispenserAuthorizingRequest(int *dispenser)
  435. {
  436. AuthorizingInfoData *AuthInfo;
  437. // AUTO_GUN_SELECTION authorize check
  438. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  439. {
  440. AuthInfo = &ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo;
  441. if(AuthInfo->AuthStatus == _AuthorizeStatus_Idle && AuthInfo->AuthRequest == YES)
  442. {
  443. if(strlen((char *)AuthInfo->AuthId) > 0)
  444. {
  445. SetAuthorize(AuthInfo, _AuthType_RFID);
  446. AuthInfo->AuthRequest = NO;
  447. *dispenser = index;
  448. return true;
  449. }
  450. }
  451. }
  452. return false;
  453. }
  454. unsigned char Chk_OfflinePolicy(char *idTag)
  455. {
  456. if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  457. {
  458. AUTH_INFO("*********** Offline Policy [No Charging] ***********");
  459. return _AuthResult_Invalid;
  460. }
  461. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  462. {
  463. // set authorize pass when offline policy is free charge
  464. AUTH_INFO("*********** Offline Policy [Free Charging] ***********");
  465. return _AuthResult_Valid;
  466. }
  467. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  468. {
  469. BOOL find = false;
  470. // check white list
  471. for(int i = 0; i < 10; i++)
  472. {
  473. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  474. {
  475. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], idTag) == EQUAL)
  476. {
  477. find = true;
  478. AUTH_INFO("*********** [White Card OK] ***********");
  479. break;
  480. }
  481. }
  482. }
  483. AUTH_INFO("*********** Local Authorization %s ***********", find ? "OK" : "NG");
  484. if(find)
  485. {
  486. return _AuthResult_Valid;
  487. }
  488. else
  489. {
  490. return _AuthResult_Invalid;
  491. }
  492. }
  493. else
  494. {
  495. AUTH_INFO("*********** [Invalid Policy] ***********");
  496. return _AuthResult_Invalid;
  497. }
  498. return _AuthResult_Invalid;
  499. }
  500. void SysAuthInitial(void)
  501. {
  502. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, 32);
  503. ShmSysConfigAndInfo->SysInfo.AuthorizedDispenser = 0;
  504. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = 0;
  505. ShmSysConfigAndInfo->SysInfo.AuthorizedType = _AuthResult_None;
  506. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Idle;
  507. }
  508. void AuthTimeoutProcess(void)
  509. {
  510. int timeout = 0;
  511. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  512. {
  513. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus == _AuthorizeStatus_End)
  514. {
  515. if(_preGunAuthStatus[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus)
  516. {
  517. GetClockTime(&_GunAuth_Time[index]);
  518. }
  519. timeout = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus == _AuthResult_Valid ?
  520. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout : AUTHORIZE_COMPLETED_TIME;
  521. timeout += AUTH_TIME_OFFSET_TIME;
  522. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= timeout)
  523. {
  524. AUTH_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  525. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  526. }
  527. }
  528. _preGunAuthStatus[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus;
  529. }
  530. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  531. {
  532. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthStatus == _AuthorizeStatus_End)
  533. {
  534. if(_preDispenserAuthStatus[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthStatus)
  535. {
  536. GetClockTime(&_DispenserAuth_Time[index]);
  537. }
  538. timeout = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  539. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout : AUTHORIZE_COMPLETED_TIME;
  540. timeout += AUTH_TIME_OFFSET_TIME;
  541. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= timeout)
  542. {
  543. AUTH_INFO("*********** Dispenser %d Authorizing Initial ***********", index + 1);
  544. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  545. }
  546. }
  547. _preDispenserAuthStatus[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthStatus;
  548. }
  549. }
  550. int main(void)
  551. {
  552. int _dispenser = 0, _connector = 0;
  553. unsigned char _authResult = _AuthResult_None;
  554. int _AuthorizeTimeout = 0;
  555. bool _autoSelection = false;
  556. bool _needOcppAuthorize = false;
  557. bool _keepRun = false;
  558. if(InitShareMemory() == FAIL)
  559. {
  560. #ifdef SystemLogMessage
  561. LOG_ERROR("InitShareMemory NG");
  562. #endif
  563. sleep(5);
  564. return 0;
  565. }
  566. InitialConnector();
  567. _isAutoStart = false;
  568. _idType = _Type_ISO14443;
  569. while(1)
  570. {
  571. _keepRun = false;
  572. AuthTimeoutProcess();
  573. switch(ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  574. {
  575. case _AuthorizeStatus_Idle:
  576. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  577. {
  578. AUTH_INFO("[SysAuthStatus Idle]");
  579. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  580. _authResult = _AuthResult_None;
  581. _autoSelection = false;
  582. _isAutoStart = false;
  583. _idType = _Type_ISO14443;
  584. }
  585. if(IsRemoteStartRequest(&_connector))
  586. {
  587. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
  588. _keepRun = true;
  589. AUTH_INFO("Gun %d Remote Start Authorize", _connector + 1);
  590. break;
  591. }
  592. if(IsAuthorizingRequest(&_connector))
  593. {
  594. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
  595. _keepRun = true;
  596. AUTH_INFO("Gun %d RFID Authorize", _connector + 1);
  597. break;
  598. }
  599. if(IsDispenserAuthorizingRequest(&_dispenser))
  600. {
  601. _autoSelection = true;
  602. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
  603. _keepRun = true;
  604. AUTH_INFO("Dispenser %d AutoSelection Authorize", _dispenser + 1);
  605. break;
  606. }
  607. break;
  608. case _AuthorizeStatus_Wait:
  609. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  610. {
  611. AUTH_INFO("[SysAuthStatus Wait]");
  612. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  613. }
  614. _needOcppAuthorize = false;
  615. if(ShmSysConfigAndInfo->SysInfo.AuthorizedType == _AuthType_RemoteStart)
  616. {
  617. if(Is_Ocpp_AuthorizeRemoteTxRequests())
  618. {
  619. _needOcppAuthorize = true;
  620. }
  621. else
  622. {
  623. AUTH_INFO("*********** Gun %d Remote Start Authorizing Bypass ***********",
  624. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget);
  625. _authResult = _AuthResult_Valid;
  626. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  627. _keepRun = true;
  628. break;
  629. }
  630. }
  631. else if(ShmSysConfigAndInfo->SysInfo.AuthorizedType == _AuthType_RFID)
  632. {
  633. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  634. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  635. {
  636. _needOcppAuthorize = true;
  637. }
  638. else
  639. {
  640. if(_isAutoStart || ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  641. {
  642. _authResult = _AuthResult_Valid;
  643. AUTH_INFO("******** [Auto Start Charging Enable] ********");
  644. }
  645. else
  646. {
  647. _authResult = Chk_OfflinePolicy((char *)ShmSysConfigAndInfo->SysConfig.UserId);
  648. }
  649. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  650. _keepRun = true;
  651. break;
  652. }
  653. }
  654. if(_needOcppAuthorize)
  655. {
  656. //CheckAutoStartChargingToSNToOcpp();
  657. Set_Ocpp_AuthorizeIdType(_idType);
  658. Set_Ocpp_AuthorizeReq();
  659. AUTH_INFO("*********** Set OPCC AuthorizeReq ***********");
  660. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Busy;
  661. }
  662. break;
  663. case _AuthorizeStatus_Busy:
  664. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  665. {
  666. _AuthorizeTimeout = Get_Ocpp_AuthorizeTimeout();
  667. AUTH_INFO("[SysAuthStatus Busy] AuthorizeTimeout %d(s)", _AuthorizeTimeout);
  668. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  669. GetClockTime(&_SysAuth_Time);
  670. }
  671. if(GetTimeoutValue(_SysAuth_Time) / uSEC_VAL >= _AuthorizeTimeout)
  672. {
  673. AUTH_INFO("*********** Ocpp Authorizing Timeout ***********");
  674. _authResult = _AuthResult_Invalid;
  675. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  676. _keepRun = true;
  677. break;
  678. }
  679. else
  680. {
  681. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  682. {
  683. if(Is_Ocpp_AuthorizeConf())
  684. {
  685. BOOL accept = false;
  686. if(Is_Ocpp_Authorize_Status("Accepted"))
  687. {
  688. accept = true;
  689. }
  690. AUTH_INFO("*********** OCPP Authorize %s ***********", accept ? "OK" : "NG");
  691. _authResult = accept ? _AuthResult_Valid : _AuthResult_Invalid;
  692. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  693. _keepRun = true;
  694. break;
  695. }
  696. }
  697. else
  698. {
  699. AUTH_INFO("*********** OCPP Authorize Disconnected ***********");
  700. _authResult = Chk_OfflinePolicy((char *)ShmSysConfigAndInfo->SysConfig.UserId);
  701. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  702. _keepRun = true;
  703. break;
  704. }
  705. }
  706. break;
  707. case _AuthorizeStatus_Done:
  708. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  709. {
  710. AUTH_INFO("[SysAuthStatus Done]");
  711. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  712. GetClockTime(&_SysAuth_Time);
  713. }
  714. if(_autoSelection)
  715. {
  716. AUTH_INFO("*********** Dispense %d RFID Authorize %s ***********", _dispenser + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  717. if(_authResult == _AuthResult_Valid)
  718. {
  719. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, AUTO_START_CHARGING) == EQUAL)
  720. {
  721. sprintf((char*)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId, "%s",
  722. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  723. }
  724. }
  725. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthResult = _authResult;
  726. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthStatus = _AuthorizeStatus_End;
  727. }
  728. else
  729. {
  730. unsigned char connector = 0;
  731. connector = ShmSysConfigAndInfo->SysInfo.AuthorizedTarget - 1;
  732. if(chargingInfo[connector]->SystemStatus == S_IDLE && _authResult == _AuthResult_Valid)
  733. {
  734. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, AUTO_START_CHARGING) == EQUAL)
  735. {
  736. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%s", ShmSysConfigAndInfo->SysConfig.SerialNumber);
  737. }
  738. memcpy(chargingInfo[connector]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  739. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RFID)
  740. {
  741. AUTH_INFO("*********** Gun %d RFID Authorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  742. }
  743. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RemoteStart)
  744. {
  745. AUTH_INFO("*********** Gun %d RemoteAuthorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  746. Set_Connector_MiscCommand(connector, MISC_CONN_REMOTE_START);
  747. }
  748. }
  749. else if(chargingInfo[connector]->SystemStatus == S_RESERVATION && _authResult == _AuthResult_Valid)
  750. {
  751. _authResult = Is_Ocpp_ReserveNowIdTag(connector, (char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthId) ? _AuthResult_Valid : _AuthResult_Invalid;
  752. if(_authResult == _AuthResult_Valid)
  753. {
  754. memcpy(chargingInfo[connector]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  755. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RFID)
  756. {
  757. AUTH_INFO("*********** Gun %d RFID Reserve Authorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  758. }
  759. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RemoteStart)
  760. {
  761. AUTH_INFO("*********** Gun %d Reserve RemoteAuthorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
  762. Set_Connector_MiscCommand(connector, MISC_CONN_REMOTE_START);
  763. }
  764. }
  765. }
  766. else if(chargingInfo[connector]->SystemStatus == S_AUTHORIZING && _authResult == _AuthResult_Valid)
  767. {
  768. if(strcmp((char *)chargingInfo[connector]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  769. {
  770. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RFID)
  771. {
  772. AUTH_INFO("*********** Gun %d RFID Authorize Already OK ***********", connector + 1);
  773. }
  774. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RemoteStart)
  775. {
  776. AUTH_INFO("*********** Gun %d RemoteAuthorize Already OK ***********", connector + 1);
  777. }
  778. }
  779. else
  780. {
  781. _authResult = _AuthResult_Invalid;
  782. }
  783. }
  784. else
  785. {
  786. AUTH_INFO("*********** Gun %d Authorize %s Status Fail (%d) ***********",
  787. connector + 1, _authResult == _AuthResult_Valid ? "OK But" : "NG Or", chargingInfo[connector]->SystemStatus);
  788. _authResult = _AuthResult_Invalid;
  789. }
  790. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult = _authResult;
  791. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthStatus = _AuthorizeStatus_End;
  792. }
  793. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
  794. break;
  795. case _AuthorizeStatus_End:
  796. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  797. {
  798. AUTH_INFO("[SysAuthStatus End]");
  799. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  800. GetClockTime(&_SysAuth_Time);
  801. }
  802. if(GetTimeoutValue(_SysAuth_Time) / uSEC_VAL >= SYS_AUTHORIZE_COMP_TIMEOUT)
  803. {
  804. AUTH_INFO("*********** PowerCabinet Authorize Completed ***********");
  805. SysAuthInitial();
  806. }
  807. break;
  808. default:
  809. if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  810. {
  811. AUTH_INFO("[SysAuthStatus Unknown]");
  812. _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
  813. }
  814. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
  815. break;
  816. }
  817. if(!_keepRun)
  818. {
  819. usleep(AUTHORIZE_INTERVAL);
  820. }
  821. }
  822. return 0;
  823. }