123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdarg.h>
- #include <string.h>
- #include <stdbool.h>
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <assert.h>
- #include <time.h>
- #include <errno.h>
- #include <unistd.h>
- #include <signal.h>
- #include "../Log/log.h"
- #include "../ShareMemory/shmMem.h"
- #include "../Define/define.h"
- #include "../Config.h"
- #include "../SelectGun/SelectGun.h"
- #include "../timeout.h"
- #include "DoComm.h"
- //------------------------------------------------------------------------------
- //#define log_info(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- //#define log_warn(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- //#define log_error(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- //------------------------------------------------------------------------------
- static DoCommGblData gDoCommGblData = {0};
- static struct SysConfigData *pSysConfig = NULL;
- static struct SysInfoData *pSysInfo = NULL;
- static struct WARNING_CODE_INFO *pSysWarning = NULL;
- static struct AlarmCodeData *pAlarmCode = NULL;
- static struct PsuData *ShmPsuData = NULL;
- static struct OCPP16Data *ShmOCPP16Data = NULL;
- static struct PrimaryMcuData *ShmPrimaryMcuData = NULL;
- static SelectGunInfo *ShmSelectGunInfo = NULL;
- static DcCommonInfo *ShmDcCommonData = NULL;
- //static struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY]
- static struct ChargingInfoData *pDcChargingInfo = NULL;
- static struct timeb gRegTimeUp[2][MAX_REGISTER_NUM] = {0};
- static struct WARNING_CODE_INFO gPreSysWarningInfo = {0};
- //------------------------------------------------------------------------------
- static void removeFaultCodeToBuf(uint8_t *Code);
- static void addFaultCodeToBuf(uint8_t *Code);
- static int readMiscCommand(int fd, uint8_t id);
- static int writeCsuModuleVersion(int fd);
- static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id);
- //------------------------------------------------------------------------------
- //--- Common function ---
- //------------------------------------------------------------------------------
- void GetClockTime(struct timespec *_now_time, void *null)
- {
- clock_gettime(CLOCK_MONOTONIC, _now_time);
- }
- /*static int StoreLogMsg(const char *fmt, ...)
- {
- char Buf[4096 + 256];
- char buffer[4096];
- va_list args;
- struct timeb SeqEndTime;
- struct tm *tm;
- va_start(args, fmt);
- int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
- va_end(args);
- memset(Buf, 0, sizeof(Buf));
- ftime(&SeqEndTime);
- SeqEndTime.time = time(NULL);
- tm = localtime(&SeqEndTime.time);
- if (pSysConfig->SwitchDebugFlag == 1) {
- sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
- tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
- printf("%s \n", Buf);
- } else {
- sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
- tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
- buffer,
- tm->tm_year + 1900, tm->tm_mon + 1);
- system(Buf);
- }
- return rc;
- }
- */
- static int DiffTimeb(struct timeb ST, struct timeb ET)
- {
- //return milli-second
- unsigned int StartTime, StopTime;
- StartTime = (unsigned int)ST.time;
- StopTime = (unsigned int)ET.time;
- return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
- }
- /**
- * [hexdump : check data]
- * @Author Jerry
- * @DateTime 2018-12-21
- * @param p [description]
- * @param size [description]
- */
- static void Hexdump(const void *p, size_t size)
- {
- const uint8_t *c = p;
- char message[10240] = {0};
- uint32_t i = 0;
- uint32_t message_len = 0;
- assert(p);
- //printf("Dumping %u bytes from %p:\r\n", size, p);
- message_len += sprintf(&message[message_len], "\r\nDumping %u bytes from %p:\r\n", (unsigned int)size, p);
- while (size > 0) {
- for (i = 0; i < 16; i++) {
- if (i < size) {
- //printf("%02x ", c[i]);
- message_len += sprintf(&message[message_len], "%02x ", c[i]);
- } else {
- //printf(" ");
- message_len += sprintf(&message[message_len], " ");
- }
- }
- for (i = 0; i < 16; i++) {
- if (i < size) {
- //printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
- message_len += sprintf(&message[message_len], "%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
- } else {
- //printf(" ");
- message_len += sprintf(&message[message_len], " ");
- }
- }
- //printf("\n");
- message_len += sprintf(&message[message_len], "\r\n");
- c += 16;
- if (size <= 16) {
- break;
- }
- size -= 16;
- }
- message_len += sprintf(&message[message_len], "\r\n");
- log_info("%s", message);
- }
- static int string2ByteArray(char *input, uint8_t *output)
- {
- int loop = 0;
- int i = 0;
- while (input[loop] != '\0') {
- output[i++] = input[loop++];
- }
- output[loop] = '\0';
- return loop + 1;
- }
- static void unixSocketSigPipeHandle(int sig)
- {
- log_error("socket packet error %x\r\n", sig);
- }
- static void InitSocketSigPipe(void)
- {
- struct sigaction action;
- action.sa_handler = unixSocketSigPipeHandle;
- sigemptyset(&action.sa_mask);
- action.sa_flags = 0;
- sigaction(SIGPIPE, &action, NULL);
- }
- //------------------------------------------------------------------------------
- static void setTcpStatus(uint8_t setValue)
- {
- pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = setValue;
- }
- //------------------------------------------------------------------------------
- //--- TCP socket function ---
- //------------------------------------------------------------------------------
- static int sendTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
- {
- int size = -1;
- size = send(fd, data, dataLen , 0);
- if ((size < 0) || (errno == EAGAIN)) {
- if (size < 0) {
- log_error("Size = %d, EAGAIN error %d:%s\r\n", size, errno, strerror(errno));
- }
- }
- return size;
- }
- static int recvTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
- {
- int size = -1;
- uint8_t *pdata = (uint8_t *)data;
- size = recv(fd, pdata, dataLen, MSG_WAITALL);
- if ((errno == EAGAIN) || (size < 0)) {
- log_error("Size = %d, EAGAIN error %d:%s\r\n", size, errno, strerror(errno));
- }
- return size;
- }
- static int getSO_ERROR(int fd)
- {
- int err = 1;
- socklen_t len = sizeof err;
- if (-1 == getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &len)) {
- log_error("getSO_ERROR\r\n");
- }
- if (err) {
- errno = err; // set errno to the socket SO_ERROR
- }
- return err;
- }
- static void closeSocket(int fd) // *not* the Windows closesocket()
- {
- if (fd < 0) {
- return;
- }
- getSO_ERROR(fd); // first clear any errors, which can cause close to fail
- if (shutdown(fd, SHUT_RDWR) < 0) { // secondly, terminate the 'reliable' delivery
- if (errno != ENOTCONN && errno != EINVAL) { // SGI causes EINVAL
- log_info("shutdown\r\n");
- }
- }
- if (close(fd) < 0) { // finally call close()
- log_info("client socket close\r\n");
- }
- }
- static int doCommConnToServer(void)
- {
- struct sockaddr_in dest;
- struct timeval tv;
- int flag;
- int TcpSock = 0;
- //if (TcpSock > 0) {
- // close(TcpSock);
- //}
- if ((TcpSock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- log_error("Open TCP socket NG");
- return -1;
- }
- //flag = fcntl (TcpSock, F_GETFL, 0);
- //if (flag >= 0) {
- // flag |= O_NONBLOCK;
- // fcntl(TcpSock, F_SETFL, flag );
- //}
- tv.tv_sec = 3;
- tv.tv_usec = 0;
- setsockopt(TcpSock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)); //設定等待3s
- setsockopt(TcpSock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)); //設定傳送3s
- flag = 1;
- setsockopt(TcpSock, SOL_SOCKET, MSG_NOSIGNAL, &flag, sizeof(flag));
- memset(&dest, 0, sizeof(dest));
- dest.sin_family = AF_INET;
- dest.sin_port = htons(DoTcpPort);
- inet_aton(DoIPAddress, (struct in_addr *) &dest.sin_addr.s_addr);
- if (connect(TcpSock, (struct sockaddr *) &dest, sizeof(dest)) != 0) {
- close(TcpSock);
- return -1;
- }
- return TcpSock;
- }
- //------------------------------------------------------------------------------
- //--- Audi select gun ---
- //------------------------------------------------------------------------------
- static void clearPricesInfo(uint8_t id)
- {
- memset(&ShmSelectGunInfo->PricesInfo[id], 0, sizeof(PricesInfo));
- ShmSelectGunInfo->PricesInfo[id].Balance = FAIL_BALANCE_PRICES;
- }
- static void ClearAuthorizedFlag(void)
- {
- ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
- pSysInfo->AuthorizeFlag = NO;
- }
- static void ClearDetectPluginFlag(void)
- {
- pSysInfo->WaitForPlugit = NO;
- }
- static bool isDetectPlugin(void)
- {
- if (pSysInfo->WaitForPlugit == YES) {
- return YES;
- }
- return NO;
- }
- static void destroySelectGun(uint8_t curGun)
- {
- uint8_t i = 0;
- uint8_t totalGun = pSysConfig->TotalConnectorCount;
- if (curGun == DESTROY_ALL_SEL) {
- ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
- ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
- for (i = 0; i < totalGun; i++) {
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
- pDcChargingInfo->TimeoutFlag = Timeout_None;
- clearPricesInfo(i);
- }
- pSysInfo->CurGunSelected = 0;
- strcpy((char *)pSysConfig->UserId, "");
- }
- //for charging timeout or complete
- if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
- ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
- if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
- ClearAuthorizedFlag();
- }
- clearPricesInfo(curGun);
- }
- if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
- if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
- ClearAuthorizedFlag();
- }
- clearPricesInfo(curGun);
- }
- }
- static int getConfirmSelectedGun(uint8_t curSel)
- {
- if (((curSel == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
- ((curSel == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
- return PASS;
- }
- return FAIL;
- }
- static int getSelGunWaitToAuthor(uint8_t curGun)
- {
- if (curGun == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
- return FAIL;
- }
- if (curGun == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
- return FAIL;
- }
- return PASS;
- }
- static void setConfirmSelGun(uint8_t selGun)
- {
- if (selGun == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_RELEASE) {
- ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
- //printf("confirmSelGun left\r\n");
- } else if (selGun == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_RELEASE) {
- ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
- //printf("confirmSelGun right\r\n");
- }
- }
- //------------------------------------------------------------------------------
- //--- DoComm function ---
- //------------------------------------------------------------------------------
- static int compareOpcode(uint8_t opCode)
- {
- if (opCode != OP_WAIT_RESPONSE) {
- //log_error("response operative code fail\r\n");
- return FAIL;
- }
- return PASS;
- }
- static int compareResult(uint8_t result)
- {
- if (result != COMMAND_RESULT_OK) {
- //log_error("response result fail\r\n");
- return FAIL;
- }
- return PASS;
- }
- static int compareRegister(uint8_t srcReg, uint8_t destReg)
- {
- if (srcReg != destReg) {
- //log_error("response register fail\r\n");
- return FAIL;
- }
- return PASS;
- }
- static float transPricesUnit(int prices)
- {
- //printf("prices = %.2f\r\n", prices * PRICES_UNIT);
- return (prices * PRICES_UNIT);
- }
- static void clearMiscCommand(void)
- {
- gDoCommGblData.MiscCmd = 0;
- }
- static int qrCodeUrlInfoHandle(uint8_t *data)
- {
- //int len = 0;
- //char cmdBuf[128] = {0};
- char localTime[128] = {0};
- uint16_t timeLen = 0;
- struct timeb SeqEndTime;
- struct tm *tm;
- if ((char *)&data[0] == '\0') {
- log_error("QR code date error\r\n");
- return FAIL;
- }
- //get local system time
- ftime(&SeqEndTime);
- SeqEndTime.time = time(NULL);
- tm = localtime(&SeqEndTime.time);
- timeLen = sprintf(localTime, "%04d-%02d-%02d %02d:%02d",
- tm->tm_year + 1900,
- tm->tm_mon + 1,
- tm->tm_mday,
- tm->tm_hour,
- tm->tm_min);
- //copy QR code string
- if (strncmp((char *)localTime, (char *)&data[0], timeLen - 2) != 0) {
- memset(pSysConfig->SystemId, '\0', sizeof(pSysConfig->SystemId));
- //printf("data = %s\r\n", data);
- //len =
- string2ByteArray((char *)data, (uint8_t *)pSysConfig->SystemId);
- //printf("SystemId = %s\r\n", pSysConfig->SystemId);
- }
- //if ((char *)&data[len] == '\0') {
- // log_error("power cabinet system date error\r\n");
- // return FAIL;
- //}
- //set system date
- //if (strncmp((char *)localTime, (char *)&data[len], timeLen) != 0) {
- // sprintf(cmdBuf, "date -s \"%s\" >> /dev/null", (char *)&data[len]);
- // system(cmdBuf);
- // log_info("local time = %s, data time = %s\r\n", localTime, (char *)&data[len]);
- // ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
- // strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
- // sleep(3);
- // //gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
- //}
- return PASS;
- }
- static int updateFirmwareHandle(uint8_t *imgName)
- {
- int ret = PASS;
- char cmdBuf[1024] = {0};
- sprintf(cmdBuf, "/mnt/%s", imgName);
- log_info("Program ready to check file %s\n", cmdBuf);
- if ( access(cmdBuf, F_OK) != -1) {
- log_info("File '%s' exist.\n", cmdBuf);
- pSysInfo->FirmwareUpdate = YES;
- } else {
- log_info("File '%s' doesn't exist.\n", cmdBuf);
- ret = FAIL;
- }
- #if 0
- char cmdBuf[1024] = {0};
- int status = 0;
- system("touch ./tftpUpdate.sh"); //創建shell
- sprintf(cmdBuf, "echo tftp -gr %s -l %s/%s %s; > ./tftpUpdate.sh",
- imgName,
- IMAGE_FILE_PATH,
- imgName,
- DoIPAddress);
- system("chmod +x ./tftpUpdate.sh"); //修改權限
- status = system("sh tftpUpdate.sh"); //執行shell
- if (-1 == status) {
- printf("system error!");
- } else {
- printf("exit status value = [0x%x]\n", status);
- if (WIFEXITED(status)) {
- if (0 == WEXITSTATUS(status)) {
- printf("run shell script successfully.\n");
- pSysInfo->FirmwareUpdate = YES;
- } else {
- printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));
- }
- } else {
- printf("exit status = [%d]\n", WEXITSTATUS(status));
- }
- }
- system("rm -rf ./tftpUpdate.sh"); //刪除shell
- #endif //0
- return ret;
- }
- static int miscCommandHandle(uint8_t dataLen, uint8_t plugNum, uint8_t *data)
- {
- int ret = PASS;
- uint8_t i = 0, j = 0;
- uint8_t cmdCount = (dataLen / 6);
- uint16_t cmd = 0;
- uint32_t value = 0;
- float prices = 0;
- MiscCommand *pMiscCmd = NULL;
- if (cmdCount < 1) {
- gDoCommGblData.MiscCmd = 0;
- //printf("cmdCount fail = %d, data len = %d\r\n", cmdCount, dataLen);
- return FAIL;
- }
- for (i = 0; i < cmdCount; i++) {
- pMiscCmd = (MiscCommand *)data + i;
- cmd = ntohs(pMiscCmd->CMD);
- value = ntohl(*((uint32_t *)&pMiscCmd->Value[0]));
- //log_info("misc command = %x, value = %d\r\n", cmd, value);
- switch (cmd) {
- //--- Execute parameter ---
- case MISC_CMD_CONNECOTOR_TIMEOUT:
- ShmSelectGunInfo->RemoteSetup.ConnectionTimeout = value;
- log_info("connection timeout = %d\r\n", ShmSelectGunInfo->RemoteSetup.ConnectionTimeout);
- clearMiscCommand();
- break;
- case MISC_CMD_OPERATIVE_STATE:
- if ((value > YES) || (value < NO)) {
- if (plugNum == 1) {
- clearMiscCommand();
- }
- break;
- }
- log_info("change availability plugNum = %d, value = %d\r\n", plugNum, value);
- ShmOCPP16Data->CsMsg.bits[plugNum].ChangeAvailabilityReq = YES;
- if (value == YES) {
- strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Operative");
- } else if (value == NO) {
- strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Inoperative");
- }
- if (plugNum == 1) {
- clearMiscCommand();
- }
- break;
- case MISC_CMD_DEFAULT_PRICES:
- prices = transPricesUnit((int)value);
- log_info("default prices = %.2f\r\n", prices);
- //pSysConfig->BillingData.isBilling = YES;
- for (j = 0; j < 24; j++) {
- pSysConfig->BillingData.Fee[j] = prices;
- }
- clearMiscCommand();
- break;
- case MISC_CMD_DEFAULT_CURRENCY:
- if (value < 0) {
- clearMiscCommand();
- return FAIL;
- }
- //log_info("default currency = %s%c\r\n", (uint8_t *)Currency[value]);
- log_info("default currency = %s\r\n", (uint8_t *)GetCurrency(value));
- pSysConfig->BillingData.Currency = value;
- clearMiscCommand();
- break;
- case MISC_CMD_ACCOUNT_BALANCE:
- if (pSysInfo->CurGunSelected != (plugNum)) {
- clearMiscCommand();
- break;
- }
- #if defined DD360Audi
- if (getConfirmSelectedGun(plugNum) == FAIL) {
- log_info("Remote start not select gun\r\n");
- break;
- }
- #endif //DD360Audi
- ShmSelectGunInfo->PricesInfo[plugNum].Balance = transPricesUnit((int)value);
- log_info("%d misc balance = %.2f\r\n", plugNum, ShmSelectGunInfo->PricesInfo[plugNum].Balance);
- clearMiscCommand();
- break;
- case MISC_CMD_BACKEND_STATUS :
- ShmSelectGunInfo->EthDevStatus.Backend = value;
- break;
- case MISC_CMD_ETHERNET_STATUS :
- ShmSelectGunInfo->EthDevStatus.Ethernet = value;
- break;
- case MISC_CMD_WIFI_STATUS :
- ShmSelectGunInfo->EthDevStatus.Wifi = value;
- break;
- case MISC_CMD_4G_STATUS :
- ShmSelectGunInfo->EthDevStatus.FourG = value;
- break;
- case MISC_CMD_BILLING_INFO:
- pSysConfig->BillingData.isBilling = value;
- break;
- case MISC_CMD_WEB_STOP_CHARGING:
- pSysConfig->StopChargingByButton = value;
- break;
- //--- Control Dispenser ---
- case MISC_CMD_HARDWARE_REBOOT:
- if (value != YES) {
- clearMiscCommand();
- break;
- }
- log_info("Hardware reboot\r\n");
- ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
- strcpy((char *)ShmOCPP16Data->Reset.Type, "Hard");
- clearMiscCommand();
- break;
- case MISC_CMD_SOFTWARE_RESTART:
- if (value != YES) {
- clearMiscCommand();
- break;
- }
- clearMiscCommand();
- log_info("Software reboot\r\n");
- ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
- strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
- break;
- case MISC_CMD_REMOTE_START_CHARGING:
- if (value != YES) {
- if (plugNum == 1) {
- clearMiscCommand();
- }
- break;
- }
- log_info("Remote start charging plugNum = %d\r\n", plugNum);
- #if defined DD360Audi
- if (getSelGunWaitToAuthor(plugNum) == FAIL) {
- log_error("Remote start gun already charging\r\n");
- break;
- }
- if (pSysInfo->CurGunSelected != (plugNum)) {
- if (plugNum == LEFT_GUN_NUM &&
- (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
- ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
- ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
- strcpy((char *)pSysConfig->UserId, "");
- pSysInfo->WaitForPlugit = NO;
- sleep(1); //Jerry add
- pSysInfo->SystemPage = _LCM_SELECT_GUN;
- GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
- pSysInfo->SystemTimeoutFlag = Timeout_None;
- }
- if (plugNum == RIGHT_GUN_NUM &&
- (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
- ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
- ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
- strcpy((char *)pSysConfig->UserId, "");
- pSysInfo->WaitForPlugit = NO;
- sleep(1); //Jerry add
- pSysInfo->SystemPage = _LCM_SELECT_GUN;
- GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
- pSysInfo->SystemTimeoutFlag = Timeout_None;
- }
- pSysInfo->CurGunSelected = (plugNum);
- pSysInfo->CurGunSelectedByAc = NO_DEFINE;
- }
- #else
- pSysInfo->CurGunSelected = (plugNum);
- pSysInfo->CurGunSelectedByAc = NO_DEFINE;
- #endif //DD360Audi
- setConfirmSelGun(pSysInfo->CurGunSelected);
- ShmOCPP16Data->CsMsg.bits[plugNum].RemoteStartTransactionReq = YES;
- ShmSelectGunInfo->PricesInfo[plugNum].Balance = 0.00;
- clearMiscCommand();
- break;
- case MISC_CMD_REMOTE_STOP_CHARGING:
- if (value != YES) {
- if (plugNum == 1) {
- clearMiscCommand();
- }
- break;
- }
- strcpy((char *)pSysConfig->UserId, "");
- pSysInfo->WaitForPlugit = NO;
- pSysInfo->SystemPage = _LCM_SELECT_GUN;
- GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
- pSysInfo->SystemTimeoutFlag = Timeout_None;
- destroySelectGun(plugNum);
- clearMiscCommand();
- break;
- case MISC_CMD_REMOTE_UNLOCK:
- if (value != YES) {
- if (plugNum == 1) {
- clearMiscCommand();
- }
- break;
- }
- if (isDetectPlugin() == YES) {
- ClearDetectPluginFlag();
- strcpy((char *)pSysConfig->UserId, "");
- pSysInfo->WaitForPlugit = NO;
- pSysInfo->SystemPage = _LCM_SELECT_GUN;
- GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
- pSysInfo->SystemTimeoutFlag = Timeout_None;
- destroySelectGun(plugNum);
- } else {
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
- pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
- }
- break;
- default:
- clearMiscCommand();
- break;
- }
- usleep(128);
- }
- return ret;
- }
- static int chargingcapabilityHandle(uint8_t *data, uint8_t plugNum)
- {
- uint8_t addr = 0;
- float MaxVolt, MaxCurrent, MaxPower;
- static PricesInfo pricesInfo[2] = {0};
- CapabilityInfo *pCapabilityInfo = NULL;
- AccountInfo *pAccountInfo = NULL;
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
- //--- charging capability information --------------------------------------
- pCapabilityInfo = (CapabilityInfo *)&data[0];
- MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
- if (MaxVolt >= 0) {
- if (MaxVolt > MAX_VOLTAGE) {
- MaxVolt = MAX_VOLTAGE;
- }
- pDcChargingInfo->MaximumChargingVoltage = MaxVolt;
- }
- MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
- if (MaxCurrent >= 0) {
- if (MaxCurrent > MAX_CURRENCY) {
- MaxCurrent = MAX_CURRENCY;
- }
- pDcChargingInfo->AvailableChargingCurrent = MaxCurrent;
- }
- MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
- if (MaxPower >= 0) {
- if (MaxPower > MAX_POWER) {
- MaxPower = MAX_POWER;
- }
- pDcChargingInfo->AvailableChargingPower = MaxPower;
- }
- //MaxVolt = (float)(data[0] << 8 |data[1]);
- //MaxCurrent = (float)(data[2] << 8 | data[3]);
- //MaxPower = (float)(data[4] << 8 | data[5]);
- //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,\n", MaxVolt, MaxCurrent, MaxPower);
- //--- user prices information ----------------------------------------------
- addr = (sizeof(CapabilityInfo) - 2); //2 byte reserved
- pAccountInfo = (AccountInfo *)&data[addr];
- pSysConfig->BillingData.Currency = pAccountInfo->Currency;
- ShmSelectGunInfo->PricesInfo[plugNum].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
- pDcChargingInfo->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
- ShmSelectGunInfo->PricesInfo[plugNum].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
- if ((pricesInfo[plugNum].UserPrices != ShmSelectGunInfo->PricesInfo[plugNum].UserPrices) ||
- (pricesInfo[plugNum].Balance != ShmSelectGunInfo->PricesInfo[plugNum].Balance)) {
- pricesInfo[plugNum].UserPrices = ShmSelectGunInfo->PricesInfo[plugNum].UserPrices;
- pricesInfo[plugNum].Balance = ShmSelectGunInfo->PricesInfo[plugNum].Balance;
- log_info("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s\r\n",
- plugNum,
- ShmSelectGunInfo->PricesInfo[plugNum].UserPrices,
- pDcChargingInfo->ChargingFee,
- ShmSelectGunInfo->PricesInfo[plugNum].Balance,
- (uint8_t *)GetCurrency(pSysConfig->BillingData.Currency)
- );
- }
- return PASS;
- }
- static void addFaultCodeToBuf(uint8_t *Code)
- {
- uint8_t warningCount = pSysWarning->WarningCount;
- if (warningCount < 10) {
- memcpy(&pSysWarning->WarningCode[warningCount][0],
- Code,
- strlen((char *)Code));
- pSysWarning->WarningCount++;
- }
- }
- static void removeFaultCodeToBuf(uint8_t *Code)
- {
- uint8_t find = 0x01;
- uint8_t i = 0;
- char _code[7] = {0};
- sprintf(_code, "%s", Code);
- // 把相關的錯誤碼一次移除,避免重複顯示
- while (find) {
- usleep(128);
- find = 0x00;
- for (i = 0; i < pSysWarning->WarningCount; i++) {
- usleep(128);
- if (find == 0x00) {
- if (memcmp(&pSysWarning->WarningCode[i][0], _code, 7) == 0) {
- find = 0x01;
- }
- } else {
- memcpy(&pSysWarning->WarningCode[i - 1][0],
- &pSysWarning->WarningCode[i][0], 7);
- }
- }
- if (find) {
- pSysWarning->WarningCount--;
- }
- }
- }
- bool CompareArrayIsZero(uint8_t *array, unsigned int array_size)
- {
- uint8_t i;
- if (array != NULL) {
- for (i = 0; i < array_size; i++) {
- if (array[i] != 0) {
- return false;
- }
- }
- }
- return true;
- }
- static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
- {
- uint8_t ret = 0;
- uint8_t i = 0;
- uint8_t count = 0;
- uint8_t EventCodeTmp[7] = {0};
- uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
- uint8_t remaindLen = 0;
- //uint8_t statusCodeError = 0;
- if (dataLen > 0) {
- Hexdump((uint8_t *)data, dataLen);
- remaindLen = dataLen % WARNING_CODE_SIZE;
- if (remaindLen != 0) {
- log_info("fail status code length = %d\r\n", dataLen);
- dataLen -= remaindLen;
- }
- if (dataLen < WARNING_CODE_SIZE) {
- log_error("fail status code length = %d\r\n", dataLen);
- Hexdump(data, dataLen);
- if (pSysWarning->WarningCount > 0) {
- for (i = 0; i < pSysWarning->WarningCount; i++) {
- usleep(128);
- if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet
- (pSysWarning->WarningCode[i][0] >= 'B' &&
- pSysWarning->WarningCode[i][1] >= '4')) {
- memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
- memcpy(EventCodeTmp,
- pSysWarning->WarningCode[i],
- sizeof(EventCodeTmp));
- removeFaultCodeToBuf(EventCodeTmp);
- }
- }
- }
- return FAIL;
- }
- for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
- usleep(128);
- if (strncmp((char *)&data[count + 1], "1", 1) != 0 &&
- strncmp((char *)&data[count + 1], "2", 1) != 0 &&
- strncmp((char *)&data[count + 1], "3", 1) != 0 &&
- (strncmp((char *)&data[count], "B", 1) != 0 &&
- strncmp((char *)&data[count + 1], "4", 1) != 0)
- ) {
- log_error("error status code = %s", (char *)&data[count]);
- continue;
- }
- // misc command status code handle
- if (strncmp((char *)&data[count], MISC_ST_MISC_CMD, WARNING_CODE_SIZE) == 0) {
- gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
- memset((char *)&data[count], 0, WARNING_CODE_SIZE);
- continue;
- } else if (strncmp((char *)&data[count], MISC_ST_VERSION, WARNING_CODE_SIZE) == 0) {
- gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
- memset((char *)&data[count], 0, WARNING_CODE_SIZE);
- continue;
- }
- if (CompareArrayIsZero((uint8_t *)&data[count], WARNING_CODE_SIZE) == true) {
- memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
- memcpy(EventCodeTmp, (char *)&data[count], sizeof(EventCodeTmp));
- log_error("error status = %s\r\n", EventCodeTmp);
- continue;
- }
- strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
- ret = 0;
- for (i = 0; i < pSysWarning->WarningCount; i++) {
- usleep(128);
- if (memcmp(&pSysWarning->WarningCode[i][0],
- StatusArray[count / WARNING_CODE_SIZE],
- WARNING_CODE_SIZE) == 0) {
- ret = 1;
- break;
- }
- }
- if (ret == 0) {
- addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
- }
- //Rtn=StatusCodeProcessing(StatusArray[count/6]);
- }
- } else {
- if (CompareArrayIsZero(data, WARNING_CODE_SIZE) == false) {
- log_error("power cabinet status code data length is zero, but have data\r\n");
- Hexdump((uint8_t *)data, WARNING_CODE_SIZE);
- }
- }
- for (i = 0; i < pSysWarning->WarningCount; i++) {
- usleep(128);
- if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet 0x33
- (pSysWarning->WarningCode[i][0] >= 'B' &&
- pSysWarning->WarningCode[i][1] >= '4')) {
- ret = 0;
- for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
- usleep(128);
- if (memcmp(&pSysWarning->WarningCode[i][0],
- StatusArray[count],
- WARNING_CODE_SIZE) == 0) {
- ret = 1;
- break;
- }
- }
- if (ret == 0) {
- memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
- memcpy(EventCodeTmp,
- pSysWarning->WarningCode[i],
- sizeof(EventCodeTmp));
- removeFaultCodeToBuf(EventCodeTmp);
- }
- }
- }
- return PASS;
- }
- static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t plugNum, uint8_t reg)
- {
- int ret = PASS;
- uint8_t rawDataLen = 0;
- CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
- SoftwareUpdInfo *pSoftwareUpd = NULL;
- PcPsuOutput *pPcPsuOutput = NULL;
- //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
- if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
- compareResult(pCsuResult->Data.Result) == FAIL ||
- compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
- if (reg != REG_CHARGING_PERMISSION) {
- return FAIL;
- }
- }
- rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
- switch (pCsuResult->Data.Register) {
- case REG_MODEL_NAME:
- break;
- case REG_CONNECTOR_ID:
- gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
- gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
- log_info("OK (Left connector ID = %d, Right connector ID = %d)\n",
- pCsuResult->Data.Data[0],
- pCsuResult->Data.Data[1]);
- break;
- case REG_POWER_CABINET_STATUS:
- ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
- break;
- case REG_DISPENSER_STATUS:
- memset(&gPreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
- memcpy((uint8_t *)&gPreSysWarningInfo,
- pSysWarning,
- sizeof(struct WARNING_CODE_INFO));
- break;
- case REG_CHARGING_CAP:
- chargingcapabilityHandle(pCsuResult->Data.Data, plugNum);
- break;
- case REG_CHARGING_TARGET:
- break;
- case REG_SOFTWARE_UPDATE:
- pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
- if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
- (rawDataLen == 0) ||
- pSoftwareUpd->UpdateState != 1) {
- ret = FAIL;
- break;
- }
- ret = FAIL;
- ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
- break;
- case REG_PLUG_IN_STATE:
- break;
- case REG_CONNECTOR_STATE:
- break;
- case REG_USER_ID:
- //log_info("%s\n", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
- //if (pCsuResult->Data.Data[0] <= 0) {
- // return FAIL;
- //}
- if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
- return COMMAND_RESULT_NG;
- }
- ShmSelectGunInfo->AuthorStateFromCabinet[plugNum] = pCsuResult->Data.Data[0];
- return pCsuResult->Data.Data[0];
- break;
- case REG_CHARGING_PERMISSION:
- //log_info("id = %d, result = %s, action = %s\r\n",
- // pCsuResult->Head.ID,
- // pCsuResult->Data.Result == 1 ? "OK" : "NG",
- // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
- if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
- return COMMAND_RESULT_NG;
- }
- ShmSelectGunInfo->WaitDoCommPermission[plugNum] = pCsuResult->Data.Data[0];
- return pCsuResult->Data.Data[0];
- break;
- case REG_MISC_CONTROL:
- miscCommandHandle(rawDataLen, plugNum, pCsuResult->Data.Data);
- break;
- case REG_REPORT_CSU_VERSION:
- case REG_REPORT_OTHER_VERSION:
- break;
- case REG_PRESENT_CHARGING_INFO:
- pPcPsuOutput = (PcPsuOutput *)&pCsuResult->Data.Data[0];
- ShmDcCommonData->PcPsuOutput[plugNum].Voltage = ntohs((uint16_t)pPcPsuOutput->Voltage);
- ShmDcCommonData->PcPsuOutput[plugNum].Current = ntohs((uint16_t)pPcPsuOutput->Current);
- //log_info("%d power cabinet PSU output vol = %f, cur = %f\r\n",
- // plugNum,
- // (float)ShmDcCommonData->PcPsuOutput[plugNum].Voltage,
- // (float)ShmDcCommonData->PcPsuOutput[plugNum].Current);
- break;
- case REG_QRCODE_URL_INFO:
- ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
- break;
- case REG_WAIT_PLUG_IT_STATE:
- break;
- case REG_Ground_Fault_Detection:
- if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
- return COMMAND_RESULT_NG;
- }
- //集電弓relay 不打開才能進入動作
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
- if(pDcChargingInfo->PantographFlag == YES)
- {
- GroundFaultDetection *gfd = (GroundFaultDetection *)&pCsuResult->Data.Data[0];
- if(pDcChargingInfo->GroundFaultStatus != gfd->Status)
- {
- log_info("id = %d, GFD Result = %d\r\n",
- pCsuResult->Head.ID,
- gfd->Status);
- }
- pDcChargingInfo->GroundFaultStatus = gfd->Status;
- }
- break;
- default:
- break;
- }
- return ret;
- }
- static int composeSocketData(int fd,
- uint8_t id,
- uint8_t opCode,
- uint8_t reg,
- uint8_t dataLen,
- uint8_t *data)
- {
- int ret = PASS;
- int size = 0;
- int sendPktLen = 0;
- uint8_t i = 0;
- uint8_t plugNum = 0;
- CsuCmdPkt csuCmdPkt = {0};
- CsuResultPkt csuResult = {0};
- csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
- csuCmdPkt.Head.ID = id;
- csuCmdPkt.Head.OP = opCode;
- csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
- csuCmdPkt.Data.Register = reg;
- if ((data != NULL) && (dataLen > 0)) {
- memcpy(csuCmdPkt.Data.Data, data, dataLen);
- }
- sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
- //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
- //send command packet
- if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
- log_error("TCP socket send packet fail = %d\r\n", size);
- gDoCommGblData.DisConnCount++;
- return FAIL;
- }
- //receive result head
- if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
- log_error("TCP socket RX head fail = %d\r\n", size);
- gDoCommGblData.DisConnCount++;
- return FAIL;
- }
- //receive result raw data
- if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
- log_error("TCP socket RX data fail = %d\r\n", size);
- gDoCommGblData.DisConnCount++;
- return FAIL;
- }
- for (i = 0; i < sizeof(gDoCommGblData.ConnectorID); i++) {
- if (id == gDoCommGblData.ConnectorID[i]) {
- plugNum = i;
- break;
- }
- }
- ret = responsePackeHandle(fd, (uint8_t *)&csuResult, plugNum, csuCmdPkt.Data.Register);
- gDoCommGblData.DisConnCount = 0; //送收資料沒有問題, 清除中斷網路計數
- return ret;
- }
- static int writeWaitPlugItState(int fd, uint8_t id)
- {
- int ret = PASS;
- uint8_t data[2] = {pSysInfo->WaitForPlugit, 0};
- //printf("WaitForPlugit = %d\r\n", pSysInfo->WaitForPlugit);
- if ((ret = composeSocketData(fd,
- id,
- OP_WRITE_DATA,
- REG_WAIT_PLUG_IT_STATE,
- 1,
- &data[0])) == FAIL) {
- return ret;
- }
- return ret;
- }
- static int readQRcodeURLAndSystemDate(int fd)
- {
- int ret = PASS;
- ret = composeSocketData(fd,
- ID_REGISTER,
- OP_READ_DATA,
- REG_QRCODE_URL_INFO,
- 0,
- NULL);
- return ret;
- }
- static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
- {
- int ret = PASS;
- uint8_t dataBuf[16] = {0};
- PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
- if(pDcChargingInfo->PantographFlag == NO)
- {
- pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(pDcChargingInfo->PresentChargingVoltage * 10));
- pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(pDcChargingInfo->PresentChargingCurrent * 10));
- }
- else
- {
- pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Voltage));
- pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Current));
- }
- pPreChargingInfo->RemainChargingDuration = htonl(pDcChargingInfo->RemainChargingDuration);
- pPreChargingInfo->EvBatterySoc = pDcChargingInfo->EvBatterySoc;
- ret = composeSocketData(fd,
- id,
- OP_WRITE_DATA,
- REG_PRESENT_CHARGING_INFO,
- sizeof(PreChargingInfo),
- (uint8_t *)pPreChargingInfo);
- return ret;
- }
- static int writeOtherModuleVersion(int fd)
- {
- int ret = PASS;
- uint8_t data[255] = {0};
- uint8_t dataLen = 0;
- //report other module version message
- memcpy(&data[dataLen], pSysInfo->FanModuleFwRev, VERSION_BUF_SIZE);
- dataLen += VERSION_BUF_SIZE;
- memcpy(&data[dataLen], pSysInfo->RelayModuleFwRev, VERSION_BUF_SIZE);
- dataLen += VERSION_BUF_SIZE;
- memcpy(&data[dataLen], pSysInfo->Connector1FwRev, VERSION_BUF_SIZE);
- dataLen += VERSION_BUF_SIZE;
- memcpy(&data[dataLen], pSysInfo->Connector2FwRev, VERSION_BUF_SIZE);
- dataLen += VERSION_BUF_SIZE;
- memcpy(&data[dataLen], pSysInfo->LedModuleFwRev, VERSION_BUF_SIZE);
- dataLen += VERSION_BUF_SIZE;
- ret = composeSocketData(fd,
- ID_REGISTER,
- OP_WRITE_DATA,
- REG_REPORT_OTHER_VERSION,
- dataLen,
- &data[0]);
- return ret;
- }
- static int writeCsuModuleVersion(int fd)
- {
- int ret = PASS;
- uint8_t data[255] = {0};
- uint8_t dataLen = 0;
- //report CSU platform version message
- memcpy(&data[dataLen], pSysInfo->CsuBootLoadFwRev, VERSION_BUF_SIZE);
- dataLen += VERSION_BUF_SIZE;
- memcpy(&data[dataLen], pSysInfo->CsuKernelFwRev, VERSION_BUF_SIZE);
- dataLen += VERSION_BUF_SIZE;
- memcpy(&data[dataLen], pSysInfo->CsuRootFsFwRev, VERSION_BUF_SIZE);
- dataLen += VERSION_BUF_SIZE;
- memcpy(&data[dataLen], pSysInfo->CsuPrimFwRev, VERSION_BUF_SIZE);
- dataLen += VERSION_BUF_SIZE;
- ret = composeSocketData(fd,
- ID_REGISTER,
- OP_WRITE_DATA,
- REG_REPORT_CSU_VERSION,
- dataLen,
- &data[0]);
- return ret;
- }
- static int readMiscCommand(int fd, uint8_t id)
- {
- int ret = PASS;
- ret = composeSocketData(fd,
- id,
- OP_READ_DATA,
- REG_MISC_CONTROL,
- 0,
- NULL);
- return ret;
- }
- static int readChargePermission(int fd, uint8_t id)
- {
- return composeSocketData(fd,
- id,
- OP_READ_DATA,
- REG_CHARGING_PERMISSION,
- 0,
- NULL);
- }
- static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id)
- {
- int ret = PASS;
- uint8_t dataBuf[1] = {status};
- ret = composeSocketData(fd,
- id,
- OP_WRITE_DATA,
- REG_Ground_Fault_Detection,
- 1,
- &dataBuf[0]);
- return ret;
- }
- static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
- {
- if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
- return FAIL;
- }
- return composeSocketData(fd,
- id,
- OP_WRITE_DATA,
- REG_USER_ID,
- strlen((char *)pUserID),
- pUserID);
- }
- static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
- {
- uint8_t dataBuf[10] = {'\0'};
- ConnectorState *pConnState = (ConnectorState *)dataBuf;
- static char vendorErrorCodeTmp[2][WARNING_CODE_SIZE] = {0};
- int ret = PASS;
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
- pConnState->ConnectorTemp = pDcChargingInfo->ConnectorTemp;
- pConnState->ChillerTemp = pDcChargingInfo->ChillerTemp;
- if (pDcChargingInfo->SystemStatus <= S_AUTHORIZING) {
- pConnState->State = CONN_ST_IDLE; //idle
- strncpy((char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
- "",
- sizeof(ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode));
- strncpy(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE);
- } else if ((pDcChargingInfo->SystemStatus <= S_PREPARING_FOR_EVSE) ||
- (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST0) ||
- (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST1)) {
- pConnState->State = CONN_ST_PREPARING; //preparing
- } else if (pDcChargingInfo->SystemStatus == S_CHARGING) {
- pConnState->State = CONN_ST_CHARGING; //charging
- } else if (pDcChargingInfo->SystemStatus == S_TERMINATING) {
- pConnState->State = CONN_ST_TERMINATING; //terminating
- } else if ((pDcChargingInfo->SystemStatus == S_ALARM) ||
- (pDcChargingInfo->SystemStatus == S_FAULT)) {
- pConnState->State = CONN_ST_ALARM;
- strncpy((char *)pConnState->WarningCode,
- (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
- WARNING_CODE_SIZE);
- if (strncmp(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE) == 0) {
- strncpy(&vendorErrorCodeTmp[plugNum][0],
- (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
- WARNING_CODE_SIZE);
- vendorErrorCodeTmp[plugNum][6] = '\0';
- log_info("1 ID = %d, VendorErrorCode = %s\r\n",
- plugNum,
- (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
- } else {
- if (strncmp(&vendorErrorCodeTmp[plugNum][0],
- (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
- WARNING_CODE_SIZE) != 0
- ) {
- strncpy(&vendorErrorCodeTmp[plugNum][0],
- (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
- WARNING_CODE_SIZE);
- vendorErrorCodeTmp[plugNum][6] = '\0';
- log_info("2 ID = %d, VendorErrorCode = %s\r\n",
- plugNum,
- (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
- }
- }
- }
- ret = composeSocketData(fd,
- id,
- OP_WRITE_DATA,
- REG_CONNECTOR_STATE,
- sizeof(dataBuf) - 1,
- &dataBuf[0]);
- return ret;
- }
- static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
- {
- int ret = PASS;
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
- uint8_t dataBuf[2] = {pDcChargingInfo->ConnectorPlugIn, 0};
- ret = composeSocketData(fd,
- id,
- OP_WRITE_DATA,
- REG_PLUG_IN_STATE,
- 1,
- &dataBuf[0]);
- return ret;
- }
- static int readSoftwareUpdate(int fd, uint8_t gunID)
- {
- int ret = PASS;
- uint8_t dataBuf[2] = {pSysInfo->FirmwareUpdate, 0};
- ret = composeSocketData(fd,
- gunID,
- OP_READ_DATA,
- REG_SOFTWARE_UPDATE,
- 1,
- &dataBuf[0]);
- return ret;
- }
- static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
- {
- int ret = PASS;
- float ChargingVolt, ChargingAmp;
- uint8_t dataBuf[4] = {0};
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
- ChargingVolt = pDcChargingInfo->EvBatterytargetVoltage;
- ChargingAmp = pDcChargingInfo->EvBatterytargetCurrent;
- ChargingVolt *= 10;
- ChargingAmp *= 10;
- dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
- dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
- dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
- dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
- ret = composeSocketData(fd,
- id,
- OP_WRITE_DATA,
- REG_CHARGING_TARGET,
- sizeof(dataBuf),
- &dataBuf[0]);
- return ret;
- }
- static int readChargingCapability(int fd, uint8_t id)
- {
- int ret = PASS;
- ret = composeSocketData(fd,
- id,
- OP_READ_DATA,
- REG_CHARGING_CAP,
- 0,
- NULL);
- return ret;
- }
- static int writeDispenserStatus(int fd, uint8_t gunID)
- {
- uint8_t warningCount = 0;
- uint8_t count = 0;
- uint8_t CurWarnCodeTmp[10][6] = {0};
- uint8_t PreWarnCodeTmp[10][6] = {0};
- int ret = PASS;
- if ((pSysWarning->WarningCount <= 0) &&
- (gPreSysWarningInfo.WarningCount <= 0)) {
- return FAIL;
- }
- warningCount = pSysWarning->WarningCount;
- for (count = 0; count < warningCount; count++) {
- memcpy(CurWarnCodeTmp[count], pSysWarning->WarningCode[count], WARNING_CODE_SIZE);
- }
- warningCount = gPreSysWarningInfo.WarningCount;
- for (count = 0; count < warningCount; count++) {
- memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
- }
- if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
- return FAIL;
- }
- ret = composeSocketData(fd,
- gunID,
- OP_WRITE_DATA,
- REG_DISPENSER_STATUS,
- strlen((char *)CurWarnCodeTmp),
- &CurWarnCodeTmp[0][0]);
- return ret;
- }
- static int readPowerCabinetStatus(int fd, uint8_t gunID)
- {
- int ret = PASS;
- ret = composeSocketData(fd,
- gunID,
- OP_READ_DATA,
- REG_POWER_CABINET_STATUS,
- 0,
- NULL);
- return ret;
- }
- static int readConnectorID(int fd)
- {
- int ret = PASS;
- ret = composeSocketData(fd,
- ID_REGISTER,
- OP_READ_DATA,
- REG_CONNECTOR_ID,
- 0,
- NULL);
- return ret;
- }
- static int WriteModelName(int fd)
- {
- int ret = PASS;
- uint8_t Tmp = 0;
- uint8_t TmpBuf[255] = {0};
- memcpy(TmpBuf,
- pSysConfig->ModelName,
- strlen((char *)pSysConfig->ModelName));
- Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
- ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
- ShmPrimaryMcuData->InputDet.bits.Key0);
- TmpBuf[strlen((char *)pSysConfig->ModelName)] = Tmp; //Dispenser switch value
- ret = composeSocketData(fd,
- ID_REGISTER,
- OP_WRITE_DATA,
- REG_MODEL_NAME,
- strlen((char *)pSysConfig->ModelName) + 1,
- &TmpBuf[0]);
- return ret;
- }
- static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
- {
- sleep(3); //wait 1 second
- if ((*pValue) >= maxCount) {
- setTcpStatus(ABNORMAL);
- } else {
- (*pValue)++;
- }
- }
- static int CheckNetworkStatus(void)
- {
- char *ipAddr = (char *)&pSysConfig->Eth0Interface.EthIpAddress;
- //printf("Check network IP Header = %s\n", ipAddr);
- if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
- return 1;
- }
- return 0;
- }
- static void updateFirmwareProcess(int fd, uint8_t gunID, uint8_t totalConnCount)
- {
- bool canUpdateFirmware = true;
- uint8_t plugNum = 0;
- uint8_t ackCount = 5;
- struct timeb updateTime;
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
- if (pSysWarning->Level != 2) {
- for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
- if (pDcChargingInfo->SystemStatus != S_IDLE &&
- pDcChargingInfo->SystemStatus != S_RESERVATION &&
- pDcChargingInfo->SystemStatus != S_MAINTAIN) {
- canUpdateFirmware = false;
- }
- }
- }
- if (canUpdateFirmware) {
- ftime(&updateTime);
- if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
- readSoftwareUpdate(fd, gunID);
- ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
- }
- if (pSysInfo->FirmwareUpdate == YES) {
- while (ackCount != 0) {
- ftime(&updateTime);
- if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
- readSoftwareUpdate(fd, gunID);
- ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
- if (pSysInfo->FirmwareUpdate == NO) {
- ackCount--;
- }
- }
- usleep(128);
- }
- }
- }
- }
- static void checkAuthorProcess(int fd, uint8_t plugNum)
- {
- int ret = 0;
- uint8_t gunID = 0;
- struct timeb AuthNowTime;
- #if defined DD360Audi
- gunID = gDoCommGblData.ConnectorID[pSysInfo->CurGunSelected];
- //gunID = gDoCommGblData.ConnectorID[plugNum];
- #else
- gunID = ID_REGISTER;
- ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0; //非Audi 不需要等待主櫃回報餘額
- #endif // DD360Audi
- if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) ||
- (pSysInfo->AuthorizeFlag == YES)) {
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
- ret = writeUserID(fd,
- gunID,
- pSysConfig->UserId);
- if (ret >= 0) {
- memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
- 0,
- sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
- if (ret == 0) {
- strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
- } else {
- strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
- }
- //printf("%d Balance = %.2f, %.2f\r\n",
- // plugNum,
- // ShmSelectGunInfo->PricesInfo[plugNum].Balance,
- // FAIL_BALANCE_PRICES);
- if (ShmSelectGunInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
- ShmOCPP16Data->SpMsg.bits.AuthorizeConf = YES; //isAuthorizedComplete
- ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
- pSysInfo->AuthorizeFlag = NO;
- ShmPsuData->SystemAvailablePower = NO;
- ShmPsuData->SystemPresentPsuQuantity = NO;
- }
- }
- ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
- }
- }
- }
- static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
- {
- int ret = DISPENSER_INIT_SUCC;
- int isContinue = 1;
- struct timeb NowTime;
- while (isContinue) {
- usleep(128);
- ftime(&NowTime);
- switch (curReg) {
- case REG_MODEL_NAME:
- if (WriteModelName(fd) == PASS) {
- gDoCommGblData.DisConnCount = 0;
- curReg = REG_CONNECTOR_ID;
- } else {
- sleep(1);
- }
- break;
- case REG_CONNECTOR_ID:
- if (readConnectorID(fd) == PASS) {
- gDoCommGblData.DisConnCount = 0;
- curReg = REG_REPORT_CSU_VERSION;
- } else {
- sleep(1);
- }
- break;
- case REG_POWER_CABINET_STATUS:
- if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
- readPowerCabinetStatus(fd, gunID);
- ftime(&gRegTimeUp[plugNum][curReg]);
- }
- curReg = REG_DISPENSER_STATUS;
- break;
- case REG_DISPENSER_STATUS:
- if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
- writeDispenserStatus(fd, gunID);
- ftime(&gRegTimeUp[plugNum][curReg]);
- }
- curReg = REG_PLUG_IN_STATE;
- break;
- //case REG_CHARGING_CAP:
- // readChargingCapability(fd, plugNum, gunID);
- // break;
- //case REG_CHARGING_TARGET:
- // writeChargingTarget(fd, plugNum, gunID);
- // break;
- //case REG_SOFTWARE_UPDATE:
- // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
- // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > L OOP_RETRY_TIME||
- //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0 * 1) {
- // readSoftwareUpdate(fd);
- // ftime(&gRegTimeUp[plugNum][curReg]);
- // }
- // while (pSysInfo->FirmwareUpdate == YES) {
- // ftime(&NowTime);
- // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowT ime) > LOOP_RETRY_TIME||
- //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
- // readSoftwareUpdate(fd);
- // }
- // usleep(128);
- // }
- // }
- // isContinue = 0;
- // break;
- case REG_PLUG_IN_STATE:
- if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
- writePlugInStatus(fd, plugNum, gunID);
- ftime(&gRegTimeUp[plugNum][curReg]);
- }
- curReg = REG_CONNECTOR_STATE;
- break;
- case REG_CONNECTOR_STATE:
- if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
- writeConnectorState(fd, plugNum, gunID);
- ftime(&gRegTimeUp[plugNum][curReg]);
- }
- //check misc command from power cabinet
- if (gDoCommGblData.MiscCmd != 0) {
- if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
- log_error("misc command failed = %x\r\n", gDoCommGblData.MiscCmd);
- }
- curReg = gDoCommGblData.MiscCmd;
- } else {
- curReg = REG_QRCODE_URL_INFO;
- }
- break;
- //case REG_USER_ID:
- // writeUserID(fd, gunID, uint8_t *pUserID);
- // break;
- //case REG_CHARGING_PERMISSION:
- // readChargePermission(fd, gunID);
- // break;
- case REG_MISC_CONTROL:
- readMiscCommand(fd, gunID);
- isContinue = 0;
- break;
- case REG_REPORT_CSU_VERSION:
- case REG_REPORT_OTHER_VERSION:
- if (gDoCommGblData.MiscCmd != 0) {
- writeCsuModuleVersion(fd);
- writeOtherModuleVersion(fd);
- clearMiscCommand();
- isContinue = 0;
- } else {
- //power up write dispenser version and get power cabinet system ID
- if ((writeCsuModuleVersion(fd) == PASS) &&
- (writeOtherModuleVersion(fd) == PASS) &&
- (readQRcodeURLAndSystemDate(fd) == PASS)
- ) {
- //gDoCommGblData.DisConnCount = 0;
- isContinue = 0;
- }
- }
- break;
- //case REG_PRESENT_CHARGING_INFO:
- // break;
- case REG_QRCODE_URL_INFO:
- if (gunID == 1) {
- if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10) ||
- DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0
- ) {
- readQRcodeURLAndSystemDate(fd);
- ftime(&gRegTimeUp[plugNum][curReg]);
- }
- }
- isContinue = 0;
- //curReg = REG_SOFTWARE_UPDATE;
- break;
- //case REG_WAIT_PLUG_IT_STATE:
- // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
- // writeWaitPlugItState(fd, gunID);
- // ftime(&gRegTimeUp[plugNum][curReg]);
- // }
- // isContinue = 0;
- // break;
- default:
- log_error("error curReg = %x\r\n", curReg);
- gDoCommGblData.MiscCmd = 0;
- gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
- isContinue = 0;
- break;
- }
- if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
- return DISPENSER_SOCKET_RECONN;
- }
- }//for
- return ret;
- }
- //static bool isDetectPlugin()
- //{
- // if (pSysInfo->WaitForPlugit == YES) {
- // return YES;
- // }
- //
- // return NO;
- //}
- static void systemStatusProcess(int fd, uint8_t totalGun, uint8_t plugNum, uint8_t gunID)
- {
- uint8_t i = 0;
- struct timeb AuthNowTime = {0};
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
- switch (pDcChargingInfo->SystemStatus) {
- case S_IDLE:
- case S_RESERVATION:
- case S_MAINTAIN:
- case S_ALARM:
- case S_AUTHORIZING:
- checkAuthorProcess(fd, plugNum);
- //authorization complete, write wait plug it state
- if (ShmSelectGunInfo->PricesInfo[plugNum].Balance == FAIL_BALANCE_PRICES) {
- break;
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
- DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) < 0
- ) {
- writeWaitPlugItState(fd, gunID);
- ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
- ) {
- readChargingCapability(fd, gunID);
- writeGroundFaultDetection(fd, 0, gunID);
- ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
- }
- break;
- case S_PREPARNING: //get permission
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) < 0
- ) {
- writeConnectorState(fd, plugNum, gunID);
- ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
- ) {
- if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
- for (i = 0; i < totalGun; i++) {
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
- ShmPsuData->SystemAvailablePower += pDcChargingInfo->AvailableChargingPower;
- }
- ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
- }
- ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
- ) {
- writePresentChargingInfo(fd, plugNum, gunID);
- writeGroundFaultDetection(fd, 0, gunID);
- ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
- }
- break;
- case S_PREPARING_FOR_EV://wait connector lock
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
- ) {
- readChargingCapability(fd, gunID);
- ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
- ) {
- if (readChargePermission(fd, gunID) == 0) {
- log_info("S_PREPARING_FOR_EV Stop charging by power cabinet's permission = %d, %d\r\n",
- plugNum,
- REG_CHARGING_PERMISSION);
- pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
- }
- ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
- ) {
- writePresentChargingInfo(fd, plugNum, gunID);
- writeGroundFaultDetection(fd, 0, gunID);
- ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
- }
- break;
- case S_PREPARING_FOR_EVSE: //insaulation test
- case S_CCS_PRECHARGE_ST0:
- case S_CCS_PRECHARGE_ST1:
- writeChargingTarget(fd, plugNum, gunID);
- writeGroundFaultDetection(fd, 1, gunID);
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
- ) {
- readChargingCapability(fd, gunID);
- ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
- ) {
- if (readChargePermission(fd, gunID) == 0) {
- log_info("S_PREPARING_FOR_EVSE Stop charging by power cabinet's permission = %d, %d\r\n",
- plugNum,
- REG_CHARGING_PERMISSION);
- pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
- }
- ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
- ) {
- writePresentChargingInfo(fd, plugNum, gunID);
- ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
- }
- break;
- case S_CHARGING: //charging
- case S_TERMINATING:
- if(pDcChargingInfo->Type == _Type_GB || pDcChargingInfo->Type == _Type_Chademo)
- {
- writeGroundFaultDetection(fd, 0, gunID);
- }
- else
- {
- writeGroundFaultDetection(fd, 1, gunID);
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
- ) {
- readChargingCapability(fd, gunID);
- ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
- }
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
- ) {
- if (readChargePermission(fd, gunID) == 0) {
- log_info("Stop charging by power cabinet's permission = %d, %d\r\n",
- plugNum,
- REG_CHARGING_PERMISSION);
- pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
- //printf("%d StopChargeFlag = %d\r\n", plugNum, pDcChargingInfo->StopChargeFlag);
- }
- ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
- }
- writeChargingTarget(fd, plugNum, gunID);
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
- ) {
- writePresentChargingInfo(fd, plugNum, gunID);
- ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
- }
- break;
- case S_COMPLETE:
- ftime(&AuthNowTime);
- if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
- DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
- ) {
- readChargingCapability(fd, gunID);
- writeGroundFaultDetection(fd, 0, gunID);
- ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
- }
- break;
- default:
- break;
- }
- }
- static int networkCreatePorcess(void)
- {
- int fd = 0;
- while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
- usleep(128);
- }
- setTcpStatus(ABNORMAL);
- /**************** Check Network **********/
- log_info("Check Dispenser Network Information");
- while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
- usleep(125);
- }
- while (!CheckNetworkStatus()) {
- calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
- log_error("disconnect count = % d\r\n", gDoCommGblData.DisConnCount);
- }
- log_info("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
- pSysConfig->Eth0Interface.EthIpAddress,
- pSysConfig->Eth0Interface.EthSubmaskAddress,
- pSysConfig->Eth0Interface.EthGatewayAddress);
- gDoCommGblData.DisConnCount = 0;
- /**************** Power cabinet connection **********/
- log_info("Connect to Power Cabinet");
- while ((fd = doCommConnToServer()) <= 0) {
- calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
- log_error("disconnect count = %d, fd = %d\r\n", gDoCommGblData.DisConnCount, fd);
- }
- log_info("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
- fd,
- pSysConfig->Eth0Interface.EthIpAddress,
- pSysConfig->Eth0Interface.EthSubmaskAddress,
- pSysConfig->Eth0Interface.EthGatewayAddress);
- gDoCommGblData.DisConnCount = 0;
- destroySelectGun(DESTROY_ALL_SEL);
- sleep(3);
- setTcpStatus(NORMAL);
- return fd;
- }
- int main(int argc, char *argv[])
- {
- uint8_t plugNum = 0, gunID = 0;
- uint8_t index = 0;
- uint8_t totalConnCount = 0;
- uint8_t initDone = DISPENER_INIT_FAIL;
- char tmpbuf[256] = {0};
- int fd = 0;
- int isContinue = 1;
- InitSocketSigPipe();
- /**************** Initialization **********/
- if (CreateAllCsuShareMemory() == FAIL) {
- log_error("create share memory error\r\n");
- return FAIL;
- }
- MappingGunChargingInfo("DoComm Task");
- pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
- pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
- pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
- pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
- ShmPsuData = (struct PsuData *)GetShmPsuData();
- ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
- ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
- ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
- ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
- totalConnCount = pSysConfig->TotalConnectorCount;
- for (index = 0; index < totalConnCount; index++) {
- clearPricesInfo(index);
- }
- //initial trigger message timer
- for (index = 0; index < MAX_REGISTER_NUM; index++) {
- ftime(&gRegTimeUp[0][index]);
- usleep(128);
- ftime(&gRegTimeUp[1][index]);
- usleep(50000);
- }
- setTcpStatus(NORMAL);
- while (isContinue) {
- if (initDone == DISPENER_INIT_FAIL) {
- fd = networkCreatePorcess();
- initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
- } else {
- plugNum = 0;
- gunID = 0;
- for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
- //checkAuthorProcess(fd, plugNum);
- //plugNum : setup chargingData value for bottom layer
- //gunID : connector Id from power cabinet, 1 = left gun, 2 = right gun,
- gunID = gDoCommGblData.ConnectorID[plugNum];
- systemStatusProcess(fd, totalConnCount, plugNum, gunID);
- initDone = messageTrigger(fd,
- plugNum,
- gunID,
- REG_POWER_CABINET_STATUS);
- if (initDone == DISPENSER_SOCKET_RECONN) {
- break;
- }
- }
- }
- if (initDone != DISPENSER_SOCKET_RECONN) {
- //update dispenser firmware
- updateFirmwareProcess(fd, gDoCommGblData.ConnectorID[0], totalConnCount);
- usleep(100000);
- continue;
- }
- //Ethernet error recovery handle
- log_info("Disconnected from power cabinet...re-connecting");
- setTcpStatus(ABNORMAL);
- if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
- sprintf(tmpbuf,
- "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
- pSysConfig->SystemId);
- system(tmpbuf);
- }
- gDoCommGblData.DisConnCount = 0;
- gDoCommGblData.SeqNum = 0;
- closeSocket(fd);
- fd = 0;
- initDone = DISPENER_INIT_FAIL;
- }
- }
|