123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918 |
- /*
- * Module_Authorize.c
- *
- * Created on: 2021年11月16日
- * Author: 7978
- */
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/can.h>
- #include <linux/can/raw.h>
- #include <linux/wireless.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <stdio.h> /*標準輸入輸出定義*/
- #include <stdlib.h> /*標準函數庫定義*/
- #include <unistd.h> /*Unix 標準函數定義*/
- #include <fcntl.h> /*檔控制定義*/
- #include <termios.h> /*PPSIX 終端控制定義*/
- #include <errno.h> /*錯誤號定義*/
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include <ifaddrs.h>
- #include <signal.h>
- #include <net/if_arp.h>
- #include "../../define.h"
- #include "Module_Authorize.h"
- #include "Module_EvComm.h"
- #include "Config.h"
- #include "Common.h"
- #define AUTHORIZE_INTERVAL 100000 // unit: 1us
- #define AUTHORIZE_WAIT_OCPP_TIMEOUT 30 // unit: 1s
- #define SYS_AUTHORIZE_COMP_TIMEOUT 1 // unit: 1s
- #define AUTH_TIME_OFFSET_TIME 6 // unit: 1s
- #define AUTO_START_CHARGING "AutoStartCharging"
- #define DEFAULT_SN "NeedSetupSN"
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- ChargerInfoData *ShmChargerInfo;
- struct ChargingInfoData *chargingInfo[GENERAL_GUN_QUANTITY];
- struct OCPP16Data *ShmOCPP16Data;
- struct OCPP20Data *ShmOCPP20Data;
- struct timespec _SysAuth_Time;
- struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
- struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
- bool _isAutoStart = false;
- unsigned char _idType = _Type_ISO14443;
- unsigned char _preSysAuthStatus = 0;
- unsigned char _preGunAuthStatus[GENERAL_GUN_QUANTITY];
- unsigned char _preDispenserAuthStatus[GENERAL_GUN_QUANTITY];
- //==========================================
- // Init all share memory
- //==========================================
- int InitShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //creat ShmSysConfigAndInfo
- if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- LOG_ERROR("shmget ShmSysConfigAndInfo NG");
- #endif
- result = FAIL;
- }
- else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- LOG_ERROR("shmat ShmSysConfigAndInfo NG");
- #endif
- result = FAIL;
- }
- else
- {
- }
- if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- LOG_ERROR("shmat ChargerInfoData NG");
- #endif
- result = FAIL;
- }
- else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- LOG_ERROR("shmat ChargerInfoData NG");
- #endif
- result = FAIL;
- }
- if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- LOG_ERROR("shmat ShmOCPP16Data NG");
- #endif
- result = FAIL;
- }
- else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- LOG_ERROR("shmat ShmOCPP16Data NG");
- result = FAIL;
- }
- if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), 0777)) < 0)
- {
- LOG_ERROR("[main]CreatShareMemory:shmget OCPP20Data NG\n");
- result = FAIL;
- }
- else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- LOG_ERROR("[main]CreatShareMemory:shmat OCPP20Data NG\n");
- result = FAIL;
- }
- return result;
- }
- void InitialConnector(void)
- {
- for(int i = 0; i < CONNECTOR_QUANTITY; i++)
- {
- chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
- }
- }
- void Set_Connector_MiscCommand(int connector, int misc_cmd)
- {
- ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue |= misc_cmd;
- }
- //===============================================
- // Ocpp Remote Start
- //===============================================
- bool Is_Ocpp_RemoteStartReq(int gun_index)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
- }
- return false;
- }
- void Clean_Ocpp_RemoteStartReq(int gun_index)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
- return;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
- return;
- }
- }
- void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
- return;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
- return;
- }
- strcpy(idTag, "");
- }
- //===============================================
- // Ocpp AuthorizeRemoteTxRequests
- //===============================================
- bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
- }
- return false;
- }
- //===============================================
- // Ocpp AuthorizeReq
- //===============================================
- void Set_Ocpp_AuthorizeReq(void)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
- ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
- return;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
- ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
- return;
- }
- }
- bool Is_Ocpp_AuthorizeConf(void)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
- }
- return false;
- }
- bool Is_Ocpp_Authorize_Status(char *status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
- }
- return false;
- }
- //===============================================
- // Ocpp >Authorize.idToken.type
- //===============================================
- void Set_Ocpp_AuthorizeIdType(unsigned char type)
- {
- char *str_IdType[] = {
- STR_ID_TYPE_CENTRAL,
- STR_ID_TYPE_EMAID,
- STR_ID_TYPE_ISO14443,
- STR_ID_TYPE_ISO15693,
- STR_ID_TYPE_KEYCODE,
- STR_ID_TYPE_LOCAL,
- STR_ID_TYPE_MAC_ADDRESS,
- STR_ID_TYPE_NO_AUTHORIZATION};
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- strncpy((char *)ShmOCPP20Data->Authorize.idToken.type, str_IdType[type], sizeof(ShmOCPP20Data->Authorize.idToken.type));
- ShmOCPP20Data->Authorize.idToken.type[sizeof(ShmOCPP20Data->Authorize.idToken.type) - 1] = '\0';
- }
- }
- //===============================================
- // Ocpp Reserve Now
- //===============================================
- void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
- return;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
- return;
- }
- strcpy(idTag, "");
- return;
- }
- bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
- {
- char ReserveIdTag[32];
- Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
- return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
- }
- //===============================================
- // Ocpp Freevend Idtag
- //===============================================
- bool Get_Ocpp_FreevendIdtag(char *idTag)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- strcpy(idTag, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
- return true;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- strcpy(idTag, (char *)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value);
- return true;
- }
- strcpy(idTag, "");
- return false;
- }
- //===============================================
- // Ocpp EVCCID_PREFIX
- //===============================================
- void Get_Ocpp_EVCCID_Prefix(char *prefix)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- strcpy(prefix, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[EVCCID_PREFIX].ItemData);
- return;
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- strcpy(prefix, "");
- return;
- }
- strcpy(prefix, "");
- }
- //===============================================
- // Ocpp AuthorizeTimeout
- //===============================================
- int Get_Ocpp_AuthorizeTimeout(void)
- {
- int timeout = AUTHORIZE_WAIT_OCPP_TIMEOUT;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeTimeout].ItemData, "") != 0)
- {
- timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeTimeout].ItemData);
- }
- }
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[OCPPCommCtrlr_AuthorizeTimeout].variableAttribute[0].value, "") != 0)
- {
- timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[OCPPCommCtrlr_AuthorizeTimeout].variableAttribute[0].value);
- }
- }
- return timeout;
- }
- void SetAuthorize(AuthorizingInfoData *AuthInfo, unsigned char type)
- {
- if(AuthInfo->AuthIdType == _Type_MacAddress)
- {
- char prefix[501];
- char evccid[501 + 32];
- memset(prefix, 0x00, sizeof(prefix));
- memset(evccid, 0x00, sizeof(evccid));
- Get_Ocpp_EVCCID_Prefix(prefix);
- sprintf(evccid, "%s%s", prefix, (char *)&AuthInfo->AuthId);
- strncpy((char *)&AuthInfo->AuthId, evccid, sizeof(AuthInfo->AuthId));
- AUTH_INFO("*********** EVCCID [%s] ***********", AuthInfo->AuthId);
- }
- else if(strcmp((char *)&AuthInfo->AuthId, AUTO_START_CHARGING) == EQUAL)
- {
- Get_Ocpp_FreevendIdtag((char *)&AuthInfo->AuthId);
- _isAutoStart = true;
- AUTH_INFO("*********** FreevendId [%s] ***********", (char *)&AuthInfo->AuthId);
- if(strlen((char *)&AuthInfo->AuthId) == 0)
- {
- sprintf((char*)&AuthInfo->AuthId, "%s", ShmSysConfigAndInfo->SysConfig.SerialNumber);
- AUTH_INFO("*********** IdTag:SerialNumber [%s] ***********", (char *)&AuthInfo->AuthId);
- }
- }
- memcpy(ShmSysConfigAndInfo->SysConfig.UserId, AuthInfo->AuthId, 32);
- AuthInfo->AuthStatus = _AuthorizeStatus_Busy;
- AuthInfo->AuthType = type;
- if(type == _AuthType_RemoteStart)
- {
- AuthInfo->AuthIdType = _Type_Central;
- }
- else if(_isAutoStart)
- {
- AuthInfo->AuthIdType = _Type_NoAuthorization;
- }
- ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = AuthInfo->AuthTarget;
- ShmSysConfigAndInfo->SysInfo.AuthorizedType = type;
- _idType = AuthInfo->AuthIdType;
- }
- void CheckAutoStartChargingToSNToOcpp(void)
- {
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.UserId, AUTO_START_CHARGING) == EQUAL)
- {
- int len = strlen((char *)&ShmSysConfigAndInfo->SysConfig.SerialNumber);
- if(len > 0 && len < sizeof(ShmSysConfigAndInfo->SysConfig.UserId))
- {
- strcpy((char *)&ShmSysConfigAndInfo->SysConfig.UserId, (char *)&ShmSysConfigAndInfo->SysConfig.SerialNumber);
- }
- else
- {
- strcpy((char *)&ShmSysConfigAndInfo->SysConfig.UserId, DEFAULT_SN);
- }
- AUTH_INFO("Received Auto Start Charging IdTag, Set SN [%s] To Authorize", ShmSysConfigAndInfo->SysConfig.UserId);
- }
- }
- bool IsRemoteStartRequest(int *connector)
- {
- AuthorizingInfoData *AuthInfo;
- // remote start check
- for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
- {
- if(Is_Ocpp_RemoteStartReq(index))
- {
- AuthInfo = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo;
- AuthInfo->AuthTarget = index + 1;
- Get_Ocpp_RemoteStartIdTag(index, (char *)AuthInfo->AuthId);
- SetAuthorize(AuthInfo, _AuthType_RemoteStart);
- Clean_Ocpp_RemoteStartReq(index);
- AuthInfo->AuthRequest = NO;
- *connector = index;
- return true;
- }
- }
- return false;
- }
- bool IsAuthorizingRequest(int *connector)
- {
- AuthorizingInfoData *AuthInfo;
- // connector authorize check
- for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
- {
- AuthInfo = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo;
- if(AuthInfo->AuthStatus == _AuthorizeStatus_Idle && AuthInfo->AuthRequest == YES)
- {
- if(strlen((char *)AuthInfo->AuthId) > 0)
- {
- SetAuthorize(AuthInfo, _AuthType_RFID);
- AuthInfo->AuthRequest = NO;
- *connector = index;
- return true;
- }
- }
- }
- return false;
- }
- bool IsDispenserAuthorizingRequest(int *dispenser)
- {
- AuthorizingInfoData *AuthInfo;
- // AUTO_GUN_SELECTION authorize check
- for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
- {
- AuthInfo = &ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo;
- if(AuthInfo->AuthStatus == _AuthorizeStatus_Idle && AuthInfo->AuthRequest == YES)
- {
- if(strlen((char *)AuthInfo->AuthId) > 0)
- {
- SetAuthorize(AuthInfo, _AuthType_RFID);
- AuthInfo->AuthRequest = NO;
- *dispenser = index;
- return true;
- }
- }
- }
- return false;
- }
- unsigned char Chk_OfflinePolicy(char *idTag)
- {
- if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
- {
- AUTH_INFO("*********** Offline Policy [No Charging] ***********");
- return _AuthResult_Invalid;
- }
- else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
- {
- // set authorize pass when offline policy is free charge
- AUTH_INFO("*********** Offline Policy [Free Charging] ***********");
- return _AuthResult_Valid;
- }
- else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
- {
- BOOL find = false;
- // check white list
- for(int i = 0; i < 10; i++)
- {
- if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
- {
- if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], idTag) == EQUAL)
- {
- find = true;
- AUTH_INFO("*********** [White Card OK] ***********");
- break;
- }
- }
- }
- AUTH_INFO("*********** Local Authorization %s ***********", find ? "OK" : "NG");
- if(find)
- {
- return _AuthResult_Valid;
- }
- else
- {
- return _AuthResult_Invalid;
- }
- }
- else
- {
- AUTH_INFO("*********** [Invalid Policy] ***********");
- return _AuthResult_Invalid;
- }
- return _AuthResult_Invalid;
- }
- void SysAuthInitial(void)
- {
- memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, 32);
- ShmSysConfigAndInfo->SysInfo.AuthorizedDispenser = 0;
- ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = 0;
- ShmSysConfigAndInfo->SysInfo.AuthorizedType = _AuthResult_None;
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Idle;
- }
- void AuthTimeoutProcess(void)
- {
- int timeout = 0;
- for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
- {
- if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus == _AuthorizeStatus_End)
- {
- if(_preGunAuthStatus[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus)
- {
- GetClockTime(&_GunAuth_Time[index]);
- }
- timeout = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus == _AuthResult_Valid ?
- ShmChargerInfo->CabinetMiscValue.ConnectionTimeout : AUTHORIZE_COMPLETED_TIME;
- timeout += AUTH_TIME_OFFSET_TIME;
- if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= timeout)
- {
- AUTH_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
- memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
- }
- }
- _preGunAuthStatus[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthStatus;
- }
- for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
- {
- if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthStatus == _AuthorizeStatus_End)
- {
- if(_preDispenserAuthStatus[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthStatus)
- {
- GetClockTime(&_DispenserAuth_Time[index]);
- }
- timeout = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
- ShmChargerInfo->CabinetMiscValue.ConnectionTimeout : AUTHORIZE_COMPLETED_TIME;
- timeout += AUTH_TIME_OFFSET_TIME;
- if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= timeout)
- {
- AUTH_INFO("*********** Dispenser %d Authorizing Initial ***********", index + 1);
- memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
- }
- }
- _preDispenserAuthStatus[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthStatus;
- }
- }
- int main(void)
- {
- int _dispenser = 0, _connector = 0;
- unsigned char _authResult = _AuthResult_None;
- int _AuthorizeTimeout = 0;
- bool _autoSelection = false;
- bool _needOcppAuthorize = false;
- bool _keepRun = false;
- if(InitShareMemory() == FAIL)
- {
- #ifdef SystemLogMessage
- LOG_ERROR("InitShareMemory NG");
- #endif
- sleep(5);
- return 0;
- }
- InitialConnector();
- _isAutoStart = false;
- _idType = _Type_ISO14443;
- while(1)
- {
- _keepRun = false;
- AuthTimeoutProcess();
- switch(ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
- {
- case _AuthorizeStatus_Idle:
- if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
- {
- AUTH_INFO("[SysAuthStatus Idle]");
- _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
- _authResult = _AuthResult_None;
- _autoSelection = false;
- _isAutoStart = false;
- _idType = _Type_ISO14443;
- }
- if(IsRemoteStartRequest(&_connector))
- {
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
- _keepRun = true;
- AUTH_INFO("Gun %d Remote Start Authorize", _connector + 1);
- break;
- }
- if(IsAuthorizingRequest(&_connector))
- {
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
- _keepRun = true;
- AUTH_INFO("Gun %d RFID Authorize", _connector + 1);
- break;
- }
- if(IsDispenserAuthorizingRequest(&_dispenser))
- {
- _autoSelection = true;
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
- _keepRun = true;
- AUTH_INFO("Dispenser %d AutoSelection Authorize", _dispenser + 1);
- break;
- }
- break;
- case _AuthorizeStatus_Wait:
- if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
- {
- AUTH_INFO("[SysAuthStatus Wait]");
- _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
- }
- _needOcppAuthorize = false;
- if(ShmSysConfigAndInfo->SysInfo.AuthorizedType == _AuthType_RemoteStart)
- {
- if(Is_Ocpp_AuthorizeRemoteTxRequests())
- {
- _needOcppAuthorize = true;
- }
- else
- {
- AUTH_INFO("*********** Gun %d Remote Start Authorizing Bypass ***********",
- ShmSysConfigAndInfo->SysInfo.AuthorizedTarget);
- _authResult = _AuthResult_Valid;
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
- _keepRun = true;
- break;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.AuthorizedType == _AuthType_RFID)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
- ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
- {
- _needOcppAuthorize = true;
- }
- else
- {
- if(_isAutoStart || ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
- {
- _authResult = _AuthResult_Valid;
- AUTH_INFO("******** [Auto Start Charging Enable] ********");
- }
- else
- {
- _authResult = Chk_OfflinePolicy((char *)ShmSysConfigAndInfo->SysConfig.UserId);
- }
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
- _keepRun = true;
- break;
- }
- }
- if(_needOcppAuthorize)
- {
- //CheckAutoStartChargingToSNToOcpp();
- Set_Ocpp_AuthorizeIdType(_idType);
- Set_Ocpp_AuthorizeReq();
- AUTH_INFO("*********** Set OPCC AuthorizeReq ***********");
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Busy;
- }
- break;
- case _AuthorizeStatus_Busy:
- if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
- {
- _AuthorizeTimeout = Get_Ocpp_AuthorizeTimeout();
- AUTH_INFO("[SysAuthStatus Busy] AuthorizeTimeout %d(s)", _AuthorizeTimeout);
- _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
- GetClockTime(&_SysAuth_Time);
- }
- if(GetTimeoutValue(_SysAuth_Time) / uSEC_VAL >= _AuthorizeTimeout)
- {
- AUTH_INFO("*********** Ocpp Authorizing Timeout ***********");
- _authResult = _AuthResult_Invalid;
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
- _keepRun = true;
- break;
- }
- else
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
- {
- if(Is_Ocpp_AuthorizeConf())
- {
- BOOL accept = false;
- if(Is_Ocpp_Authorize_Status("Accepted"))
- {
- accept = true;
- }
- AUTH_INFO("*********** OCPP Authorize %s ***********", accept ? "OK" : "NG");
- _authResult = accept ? _AuthResult_Valid : _AuthResult_Invalid;
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
- _keepRun = true;
- break;
- }
- }
- else
- {
- AUTH_INFO("*********** OCPP Authorize Disconnected ***********");
- _authResult = Chk_OfflinePolicy((char *)ShmSysConfigAndInfo->SysConfig.UserId);
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
- _keepRun = true;
- break;
- }
- }
- break;
- case _AuthorizeStatus_Done:
- if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
- {
- AUTH_INFO("[SysAuthStatus Done]");
- _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
- GetClockTime(&_SysAuth_Time);
- }
- if(_autoSelection)
- {
- AUTH_INFO("*********** Dispense %d RFID Authorize %s ***********", _dispenser + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
- if(_authResult == _AuthResult_Valid)
- {
- if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, AUTO_START_CHARGING) == EQUAL)
- {
- sprintf((char*)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId, "%s",
- ShmSysConfigAndInfo->SysConfig.SerialNumber);
- }
- }
- ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthResult = _authResult;
- ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthStatus = _AuthorizeStatus_End;
- }
- else
- {
- unsigned char connector = 0;
- connector = ShmSysConfigAndInfo->SysInfo.AuthorizedTarget - 1;
- if(chargingInfo[connector]->SystemStatus == S_IDLE && _authResult == _AuthResult_Valid)
- {
- if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, AUTO_START_CHARGING) == EQUAL)
- {
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%s", ShmSysConfigAndInfo->SysConfig.SerialNumber);
- }
- memcpy(chargingInfo[connector]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
- if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RFID)
- {
- AUTH_INFO("*********** Gun %d RFID Authorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
- }
- if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RemoteStart)
- {
- AUTH_INFO("*********** Gun %d RemoteAuthorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
- Set_Connector_MiscCommand(connector, MISC_CONN_REMOTE_START);
- }
- }
- else if(chargingInfo[connector]->SystemStatus == S_RESERVATION && _authResult == _AuthResult_Valid)
- {
- _authResult = Is_Ocpp_ReserveNowIdTag(connector, (char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthId) ? _AuthResult_Valid : _AuthResult_Invalid;
- if(_authResult == _AuthResult_Valid)
- {
- memcpy(chargingInfo[connector]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
- if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RFID)
- {
- AUTH_INFO("*********** Gun %d RFID Reserve Authorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
- }
- if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RemoteStart)
- {
- AUTH_INFO("*********** Gun %d Reserve RemoteAuthorize %s ***********", connector + 1, _authResult == _AuthResult_Valid ? "OK" : "NG");
- Set_Connector_MiscCommand(connector, MISC_CONN_REMOTE_START);
- }
- }
- }
- else if(chargingInfo[connector]->SystemStatus == S_AUTHORIZING && _authResult == _AuthResult_Valid)
- {
- if(strcmp((char *)chargingInfo[connector]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
- {
- if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RFID)
- {
- AUTH_INFO("*********** Gun %d RFID Authorize Already OK ***********", connector + 1);
- }
- if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthType == _AuthType_RemoteStart)
- {
- AUTH_INFO("*********** Gun %d RemoteAuthorize Already OK ***********", connector + 1);
- }
- }
- else
- {
- _authResult = _AuthResult_Invalid;
- }
- }
- else
- {
- AUTH_INFO("*********** Gun %d Authorize %s Status Fail (%d) ***********",
- connector + 1, _authResult == _AuthResult_Valid ? "OK But" : "NG Or", chargingInfo[connector]->SystemStatus);
- _authResult = _AuthResult_Invalid;
- }
- ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult = _authResult;
- ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthStatus = _AuthorizeStatus_End;
- }
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
- break;
- case _AuthorizeStatus_End:
- if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
- {
- AUTH_INFO("[SysAuthStatus End]");
- _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
- GetClockTime(&_SysAuth_Time);
- }
- if(GetTimeoutValue(_SysAuth_Time) / uSEC_VAL >= SYS_AUTHORIZE_COMP_TIMEOUT)
- {
- AUTH_INFO("*********** PowerCabinet Authorize Completed ***********");
- SysAuthInitial();
- }
- break;
- default:
- if(_preSysAuthStatus != ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
- {
- AUTH_INFO("[SysAuthStatus Unknown]");
- _preSysAuthStatus = ShmSysConfigAndInfo->SysInfo.AuthorizedStatus;
- }
- ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
- break;
- }
- if(!_keepRun)
- {
- usleep(AUTHORIZE_INTERVAL);
- }
- }
- return 0;
- }
|