DoComm.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/socket.h>
  10. #include <sys/types.h>
  11. #include <sys/ipc.h>
  12. #include <sys/shm.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <assert.h>
  16. #include <time.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <signal.h>
  20. #include "../Log/log.h"
  21. #include "../ShareMemory/shmMem.h"
  22. #include "../Define/define.h"
  23. #include "../Config.h"
  24. #include "../SelectGun/SelectGun.h"
  25. #include "../timeout.h"
  26. #include "../DataBase/DataBase.h"
  27. #include "DoComm.h"
  28. //------------------------------------------------------------------------------
  29. static DoCommGblData gDoCommGblData = {0};
  30. static MoreInfoReq gMoreInfoReq[2] = {0};
  31. //static ChangePageReq gPageReq[2] = {0};
  32. static ConnectorActReqVar gConnectorActReq[2] = {0};
  33. static struct SysConfigData *pSysConfig = NULL;
  34. static struct SysInfoData *pSysInfo = NULL;
  35. static struct WARNING_CODE_INFO *pSysWarning = NULL;
  36. static struct AlarmCodeData *pAlarmCode = NULL;
  37. static struct PsuData *ShmPsuData = NULL;
  38. static struct OCPP16Data *ShmOCPP16Data = NULL;
  39. static struct PrimaryMcuData *ShmPrimaryMcuData = NULL;
  40. static SelectGunInfo *ShmSelectGunInfo = NULL;
  41. static DcCommonInfo *ShmDcCommonData = NULL;
  42. //static struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY]
  43. static struct ChargingInfoData *pDcChargingInfo = NULL;
  44. static struct timeb gRegTimeUp[2][MAX_REGISTER_NUM] = {0};
  45. static struct WARNING_CODE_INFO gPreSysWarningInfo = {0};
  46. // Hexdump
  47. char old_Hexdump[10240];
  48. static uint8_t RemoteStartNoIDState = NO;
  49. static uint8_t ReservationState[2] = {0};
  50. static char ReservationIdTag[2][32];
  51. static uint8_t DeductResultReq[2] = {0};
  52. static int LineStatusCode[2] = {0};
  53. static unsigned int LedIntensity = 0;
  54. static int TimeZoneOffset = 0;
  55. //------------------------------------------------------------------------------
  56. static void removeFaultCodeToBuf(uint8_t *Code);
  57. static void addFaultCodeToBuf(uint8_t *Code);
  58. static int readMiscCommand(int fd, uint8_t id);
  59. static int writeCsuModuleVersion(int fd);
  60. static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id);
  61. //------------------------------------------------------------------------------
  62. //--- Common function ---
  63. //------------------------------------------------------------------------------
  64. void GetClockTime(struct timespec *_now_time, void *null)
  65. {
  66. clock_gettime(CLOCK_MONOTONIC, _now_time);
  67. }
  68. static int DiffTimeb(struct timeb ST, struct timeb ET)
  69. {
  70. //return milli-second
  71. unsigned int StartTime, StopTime;
  72. StartTime = (unsigned int)ST.time;
  73. StopTime = (unsigned int)ET.time;
  74. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  75. }
  76. /**
  77. * [hexdump : check data]
  78. * @Author Jerry
  79. * @DateTime 2018-12-21
  80. * @param p [description]
  81. * @param size [description]
  82. */
  83. static void Hexdump(const void *p, size_t size)
  84. {
  85. const uint8_t *c = p;
  86. char message[10240] = {0};
  87. uint32_t i = 0;
  88. uint32_t message_len = 0;
  89. assert(p);
  90. //printf("Dumping %u bytes from %p:\n", size, p);
  91. message_len += sprintf(&message[message_len], "\nDumping %u bytes from %p:\n", (unsigned int)size, p);
  92. while (size > 0) {
  93. for (i = 0; i < 16; i++) {
  94. if (i < size) {
  95. //printf("%02x ", c[i]);
  96. message_len += sprintf(&message[message_len], "%02x ", c[i]);
  97. } else {
  98. //printf(" ");
  99. message_len += sprintf(&message[message_len], " ");
  100. }
  101. }
  102. for (i = 0; i < 16; i++) {
  103. if (i < size) {
  104. //printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  105. message_len += sprintf(&message[message_len], "%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  106. } else {
  107. //printf(" ");
  108. message_len += sprintf(&message[message_len], " ");
  109. }
  110. }
  111. //printf("\n");
  112. message_len += sprintf(&message[message_len], "\n");
  113. c += 16;
  114. if (size <= 16) {
  115. break;
  116. }
  117. size -= 16;
  118. }
  119. //message_len += sprintf(&message[message_len], "\n");
  120. if( strcmp(old_Hexdump,message) != EQUAL ) {
  121. log_info("%s", message);
  122. strcpy(old_Hexdump,message);
  123. }
  124. }
  125. static int string2ByteArray(char *input, uint8_t *output)
  126. {
  127. int loop = 0;
  128. int i = 0;
  129. while (input[loop] != '\0') {
  130. output[i++] = input[loop++];
  131. }
  132. output[loop] = '\0';
  133. return loop + 1;
  134. }
  135. static void unixSocketSigPipeHandle(int sig)
  136. {
  137. log_error("socket packet error %x", sig);
  138. }
  139. static void InitSocketSigPipe(void)
  140. {
  141. struct sigaction action;
  142. action.sa_handler = unixSocketSigPipeHandle;
  143. sigemptyset(&action.sa_mask);
  144. action.sa_flags = 0;
  145. sigaction(SIGPIPE, &action, NULL);
  146. }
  147. //------------------------------------------------------------------------------
  148. static void setTcpStatus(uint8_t setValue)
  149. {
  150. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = setValue;
  151. }
  152. //------------------------------------------------------------------------------
  153. //--- TCP socket function ---
  154. //------------------------------------------------------------------------------
  155. static int sendTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  156. {
  157. int size = -1;
  158. size = send(fd, data, dataLen , 0);
  159. if ((size < 0) || (errno == EAGAIN)) {
  160. if (size < 0) {
  161. log_error("Send Socket Size = %d, EAGAIN error %d:%s", size, errno, strerror(errno));
  162. }
  163. }
  164. return size;
  165. }
  166. static int recvTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  167. {
  168. int size = -1;
  169. uint8_t *pdata = (uint8_t *)data;
  170. size = recv(fd, pdata, dataLen, MSG_WAITALL);
  171. if ((errno == EAGAIN) || (size < 0)) {
  172. log_error("Receive Socket Size = %d, EAGAIN error %d:%s", size, errno, strerror(errno));
  173. }
  174. return size;
  175. }
  176. static int getSO_ERROR(int fd)
  177. {
  178. int err = 1;
  179. socklen_t len = sizeof err;
  180. if (-1 == getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &len)) {
  181. log_error("getSO_ERROR");
  182. }
  183. if (err) {
  184. errno = err; // set errno to the socket SO_ERROR
  185. }
  186. return err;
  187. }
  188. static void closeSocket(int fd) // *not* the Windows closesocket()
  189. {
  190. if (fd < 0) {
  191. return;
  192. }
  193. getSO_ERROR(fd); // first clear any errors, which can cause close to fail
  194. if (shutdown(fd, SHUT_RDWR) < 0) { // secondly, terminate the 'reliable' delivery
  195. if (errno != ENOTCONN && errno != EINVAL) { // SGI causes EINVAL
  196. log_info("shutdown");
  197. }
  198. }
  199. if (close(fd) < 0) { // finally call close()
  200. log_info("client socket close");
  201. }
  202. }
  203. static int doCommConnToServer(void)
  204. {
  205. struct sockaddr_in dest;
  206. struct timeval tv;
  207. int flag;
  208. int TcpSock = 0;
  209. //if (TcpSock > 0) {
  210. // close(TcpSock);
  211. //}
  212. if ((TcpSock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  213. log_error("Open TCP socket NG");
  214. return -1;
  215. }
  216. //flag = fcntl (TcpSock, F_GETFL, 0);
  217. //if (flag >= 0) {
  218. // flag |= O_NONBLOCK;
  219. // fcntl(TcpSock, F_SETFL, flag );
  220. //}
  221. tv.tv_sec = 3;
  222. tv.tv_usec = 0;
  223. setsockopt(TcpSock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)); //設定等待3s
  224. setsockopt(TcpSock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)); //設定傳送3s
  225. flag = 1;
  226. setsockopt(TcpSock, SOL_SOCKET, MSG_NOSIGNAL, &flag, sizeof(flag));
  227. memset(&dest, 0, sizeof(dest));
  228. dest.sin_family = AF_INET;
  229. dest.sin_port = htons(DoTcpPort);
  230. inet_aton(DoIPAddress, (struct in_addr *) &dest.sin_addr.s_addr);
  231. if (connect(TcpSock, (struct sockaddr *) &dest, sizeof(dest)) != 0) {
  232. close(TcpSock);
  233. return -1;
  234. }
  235. return TcpSock;
  236. }
  237. //------------------------------------------------------------------------------
  238. //--- Audi select gun ---
  239. //------------------------------------------------------------------------------
  240. static void clearPricesInfo(uint8_t id)
  241. {
  242. memset(&ShmSelectGunInfo->PricesInfo[id], 0, sizeof(PricesInfo));
  243. ShmSelectGunInfo->PricesInfo[id].Balance = FAIL_BALANCE_PRICES;
  244. }
  245. static void ClearAuthorizedFlag(void)
  246. {
  247. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  248. pSysInfo->AuthorizeFlag = NO;
  249. }
  250. static void ClearDetectPluginFlag(void)
  251. {
  252. pSysInfo->WaitForPlugit = NO;
  253. }
  254. static bool isDetectPlugin(void)
  255. {
  256. if (pSysInfo->WaitForPlugit == YES) {
  257. return YES;
  258. }
  259. return NO;
  260. }
  261. static void destroySelectGun(uint8_t curGun)
  262. {
  263. uint8_t i = 0;
  264. uint8_t totalGun = pSysConfig->TotalConnectorCount;
  265. if (curGun == DESTROY_ALL_SEL) {
  266. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  267. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  268. for (i = 0; i < totalGun; i++) {
  269. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  270. pDcChargingInfo->TimeoutFlag = Timeout_None;
  271. clearPricesInfo(i);
  272. }
  273. pSysInfo->CurGunSelected = 0;
  274. strcpy((char *)pSysConfig->UserId, "");
  275. }
  276. //for charging timeout or complete
  277. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
  278. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  279. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  280. ClearAuthorizedFlag();
  281. }
  282. clearPricesInfo(curGun);
  283. }
  284. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
  285. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  286. ClearAuthorizedFlag();
  287. }
  288. clearPricesInfo(curGun);
  289. }
  290. }
  291. static int getConfirmSelectedGun(uint8_t curSel)
  292. {
  293. if (((curSel == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  294. ((curSel == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  295. return PASS;
  296. }
  297. return FAIL;
  298. }
  299. static int getSelGunWaitToAuthor(uint8_t curGun)
  300. {
  301. if (curGun == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  302. return FAIL;
  303. }
  304. if (curGun == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  305. return FAIL;
  306. }
  307. return PASS;
  308. }
  309. static void setConfirmSelGun(uint8_t selGun)
  310. {
  311. if (selGun == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_RELEASE) {
  312. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  313. //printf("confirmSelGun left");
  314. } else if (selGun == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_RELEASE) {
  315. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  316. //printf("confirmSelGun right");
  317. }
  318. }
  319. //------------------------------------------------------------------------------
  320. //--- DoComm function ---
  321. //------------------------------------------------------------------------------
  322. static int compareOpcode(uint8_t opCode)
  323. {
  324. if (opCode != OP_WAIT_RESPONSE) {
  325. //log_error("response operative code fail");
  326. return FAIL;
  327. }
  328. return PASS;
  329. }
  330. static int compareResult(uint8_t result)
  331. {
  332. if (result != COMMAND_RESULT_OK) {
  333. //log_error("response result fail");
  334. return FAIL;
  335. }
  336. return PASS;
  337. }
  338. static int compareRegister(uint8_t srcReg, uint8_t destReg)
  339. {
  340. if (srcReg != destReg) {
  341. //log_error("response register fail");
  342. return FAIL;
  343. }
  344. return PASS;
  345. }
  346. static float transPricesUnit(int prices)
  347. {
  348. //printf("prices = %.2f", prices * PRICES_UNIT);
  349. return (prices * PRICES_UNIT);
  350. }
  351. static void clearMiscCommand(void)
  352. {
  353. gDoCommGblData.MiscCmd = 0;
  354. }
  355. static int qrCodeUrlInfoHandle(uint8_t *data)
  356. {
  357. //int len = 0;
  358. //char cmdBuf[128] = {0};
  359. char localTime[128] = {0};
  360. uint16_t timeLen = 0;
  361. struct timeb SeqEndTime;
  362. struct tm *tm;
  363. if ((char *)&data[0] == '\0') {
  364. log_error("QR code date error");
  365. return FAIL;
  366. }
  367. //get local system time
  368. ftime(&SeqEndTime);
  369. SeqEndTime.time = time(NULL);
  370. tm = localtime(&SeqEndTime.time);
  371. timeLen = sprintf(localTime, "%04d-%02d-%02d %02d:%02d",
  372. tm->tm_year + 1900,
  373. tm->tm_mon + 1,
  374. tm->tm_mday,
  375. tm->tm_hour,
  376. tm->tm_min);
  377. //copy QR code string
  378. if (strncmp((char *)localTime, (char *)&data[0], timeLen - 2) != 0) {
  379. memset(pSysConfig->SystemId, '\0', sizeof(pSysConfig->SystemId));
  380. //printf("data = %s", data);
  381. //len =
  382. string2ByteArray((char *)data, (uint8_t *)pSysConfig->SystemId);
  383. //printf("SystemId = %s", pSysConfig->SystemId);
  384. //log_info("SystemId:%s",pSysConfig->SystemId);
  385. }
  386. sprintf(ShmDcCommonData->PresentTime, "%04d/%02d/%02d",
  387. tm->tm_year + 1900,
  388. tm->tm_mon + 1,
  389. tm->tm_mday);
  390. //if ((char *)&data[len] == '\0') {
  391. // log_error("power cabinet system date error");
  392. // return FAIL;
  393. //}
  394. //set system date
  395. //if (strncmp((char *)localTime, (char *)&data[len], timeLen) != 0) {
  396. // sprintf(cmdBuf, "date -s \"%s\" >> /dev/null", (char *)&data[len]);
  397. // system(cmdBuf);
  398. // log_info("local time = %s, data time = %s", localTime, (char *)&data[len]);
  399. // ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  400. // strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  401. // sleep(3);
  402. // //gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  403. //}
  404. return PASS;
  405. }
  406. static int updateFirmwareHandle(uint8_t *imgName)
  407. {
  408. int ret = PASS;
  409. char cmdBuf[1024] = {0};
  410. sprintf(cmdBuf, "/mnt/%s", imgName);
  411. log_info("Program ready to check file %s", cmdBuf);
  412. if ( access(cmdBuf, F_OK) != -1) {
  413. log_info("File '%s' exist.", cmdBuf);
  414. pSysInfo->FirmwareUpdate = YES;
  415. } else {
  416. log_info("File '%s' doesn't exist.", cmdBuf);
  417. ret = FAIL;
  418. }
  419. #if 0
  420. char cmdBuf[1024] = {0};
  421. int status = 0;
  422. system("touch ./tftpUpdate.sh"); //創建shell
  423. sprintf(cmdBuf, "echo tftp -gr %s -l %s/%s %s; > ./tftpUpdate.sh",
  424. imgName,
  425. IMAGE_FILE_PATH,
  426. imgName,
  427. DoIPAddress);
  428. system("chmod +x ./tftpUpdate.sh"); //修改權限
  429. status = system("sh tftpUpdate.sh"); //執行shell
  430. if (-1 == status) {
  431. printf("system error!");
  432. } else {
  433. printf("exit status value = [0x%x]", status);
  434. if (WIFEXITED(status)) {
  435. if (0 == WEXITSTATUS(status)) {
  436. printf("run shell script successfully.");
  437. pSysInfo->FirmwareUpdate = YES;
  438. } else {
  439. printf("run shell script fail, script exit code: %d", WEXITSTATUS(status));
  440. }
  441. } else {
  442. printf("exit status = [%d]", WEXITSTATUS(status));
  443. }
  444. }
  445. system("rm -rf ./tftpUpdate.sh"); //刪除shell
  446. #endif //0
  447. return ret;
  448. }
  449. static int miscCommandHandle(uint8_t dataLen, uint8_t plugNum, uint8_t *data)
  450. {
  451. int ret = PASS;
  452. uint8_t i = 0, j = 0;
  453. uint8_t cmdCount = (dataLen / 6);
  454. uint16_t cmd = 0;
  455. uint32_t value = 0;
  456. float prices = 0;
  457. MiscCommand *pMiscCmd = NULL;
  458. if (cmdCount < 1) {
  459. gDoCommGblData.MiscCmd = 0;
  460. //printf("cmdCount fail = %d, data len = %d", cmdCount, dataLen);
  461. return FAIL;
  462. }
  463. for (i = 0; i < cmdCount; i++) {
  464. pMiscCmd = (MiscCommand *)data + i;
  465. cmd = ntohs(pMiscCmd->CMD);
  466. value = ntohl(*((uint32_t *)&pMiscCmd->Value[0]));
  467. //log_info("misc command = %x, value = %d", cmd, value);
  468. switch (cmd) {
  469. //--- Execute parameter ---
  470. case MISC_CMD_CONNECOTOR_TIMEOUT:
  471. ShmSelectGunInfo->RemoteSetup.ConnectionTimeout = value;
  472. log_info("connection timeout = %d", ShmSelectGunInfo->RemoteSetup.ConnectionTimeout);
  473. clearMiscCommand();
  474. break;
  475. case MISC_CMD_OPERATIVE_STATE:
  476. if ((value > YES) || (value < NO)) {
  477. if (plugNum == 1) {
  478. clearMiscCommand();
  479. }
  480. break;
  481. }
  482. log_info("change availability plugNum = %d, value = %d", plugNum, value);
  483. ShmOCPP16Data->CsMsg.bits[plugNum].ChangeAvailabilityReq = YES;
  484. if (value == YES) {
  485. strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Operative");
  486. } else if (value == NO) {
  487. strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Inoperative");
  488. }
  489. if (plugNum == 1) {
  490. clearMiscCommand();
  491. }
  492. break;
  493. case MISC_CMD_DEFAULT_PRICES:
  494. prices = transPricesUnit((int)value);
  495. log_info("default prices = %.2f", prices);
  496. if (prices > 0)
  497. ShmDcCommonData->ChargingRate = prices;
  498. //pSysConfig->BillingData.isBilling = YES;
  499. for (j = 0; j < 24; j++) {
  500. pSysConfig->BillingData.Fee[j] = prices;
  501. }
  502. clearMiscCommand();
  503. break;
  504. case MISC_CMD_DEFAULT_CURRENCY:
  505. if (value < 0) {
  506. clearMiscCommand();
  507. return FAIL;
  508. }
  509. //log_info("default currency = %s%c", (uint8_t *)Currency[value]);
  510. log_info("default currency = %s", (uint8_t *)GetCurrency(value));
  511. pSysConfig->BillingData.Currency = value;
  512. clearMiscCommand();
  513. break;
  514. case MISC_CMD_ACCOUNT_BALANCE:
  515. if (pSysInfo->CurGunSelected != (plugNum)) {
  516. clearMiscCommand();
  517. break;
  518. }
  519. #if defined DD360Audi
  520. if (getConfirmSelectedGun(plugNum) == FAIL) {
  521. log_info("Remote start not select gun");
  522. break;
  523. }
  524. #endif //DD360Audi
  525. ShmSelectGunInfo->PricesInfo[plugNum].Balance = transPricesUnit((int)value);
  526. log_info("%d misc balance = %.2f", plugNum, ShmSelectGunInfo->PricesInfo[plugNum].Balance);
  527. clearMiscCommand();
  528. break;
  529. case MISC_CMD_BACKEND_STATUS :
  530. ShmSelectGunInfo->EthDevStatus.Backend = value;
  531. break;
  532. case MISC_CMD_ETHERNET_STATUS :
  533. ShmSelectGunInfo->EthDevStatus.Ethernet = value;
  534. break;
  535. case MISC_CMD_WIFI_STATUS :
  536. ShmSelectGunInfo->EthDevStatus.Wifi = value;
  537. break;
  538. case MISC_CMD_4G_STATUS :
  539. ShmSelectGunInfo->EthDevStatus.FourG = value;
  540. break;
  541. case MISC_CMD_BILLING_INFO:
  542. pSysConfig->BillingData.isBilling = value;
  543. break;
  544. case MISC_CMD_WEB_STOP_CHARGING:
  545. pSysConfig->StopChargingByButton = value;
  546. break;
  547. //--- Control Dispenser ---
  548. case MISC_CMD_HARDWARE_REBOOT:
  549. if (value != YES) {
  550. clearMiscCommand();
  551. break;
  552. }
  553. log_info("Hardware reboot");
  554. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  555. strcpy((char *)ShmOCPP16Data->Reset.Type, "Hard");
  556. clearMiscCommand();
  557. break;
  558. case MISC_CMD_SOFTWARE_RESTART:
  559. if (value != YES) {
  560. clearMiscCommand();
  561. break;
  562. }
  563. clearMiscCommand();
  564. log_info("Software reboot");
  565. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  566. strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  567. break;
  568. case MISC_CMD_REMOTE_START_CHARGING:
  569. if (value != YES) {
  570. if (plugNum == 1) {
  571. clearMiscCommand();
  572. }
  573. break;
  574. }
  575. log_info("Remote start charging plugNum = %d", plugNum);
  576. if (getSelGunWaitToAuthor(plugNum) == FAIL) {
  577. log_error("Remote start gun already charging");
  578. break;
  579. }
  580. if (pSysInfo->CurGunSelected != (plugNum)) {
  581. if (plugNum == LEFT_GUN_NUM &&
  582. (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  583. ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  584. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  585. strcpy((char *)pSysConfig->UserId, "");
  586. pSysInfo->WaitForPlugit = NO;
  587. sleep(1); //Jerry add
  588. pSysInfo->SystemPage = _LCM_VIEW;
  589. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  590. pSysInfo->SystemTimeoutFlag = Timeout_None;
  591. }
  592. if (plugNum == RIGHT_GUN_NUM &&
  593. (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  594. ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  595. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  596. strcpy((char *)pSysConfig->UserId, "");
  597. pSysInfo->WaitForPlugit = NO;
  598. sleep(1); //Jerry add
  599. pSysInfo->SystemPage = _LCM_VIEW;
  600. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  601. pSysInfo->SystemTimeoutFlag = Timeout_None;
  602. }
  603. //pSysInfo->CurGunSelected = (plugNum);
  604. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  605. }
  606. setConfirmSelGun(pSysInfo->CurGunSelected);
  607. ShmOCPP16Data->CsMsg.bits[plugNum].RemoteStartTransactionReq = YES;
  608. ShmSelectGunInfo->PricesInfo[plugNum].Balance = 0.00;
  609. clearMiscCommand();
  610. break;
  611. case MISC_CMD_REMOTE_STOP_CHARGING:
  612. if (value != YES) {
  613. if (plugNum == 1) {
  614. clearMiscCommand();
  615. }
  616. break;
  617. }
  618. strcpy((char *)pSysConfig->UserId, "");
  619. pSysInfo->WaitForPlugit = NO;
  620. pSysInfo->SystemPage = _LCM_VIEW;
  621. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  622. pSysInfo->SystemTimeoutFlag = Timeout_None;
  623. destroySelectGun(plugNum);
  624. clearMiscCommand();
  625. break;
  626. case MISC_CMD_REMOTE_UNLOCK:
  627. if (value != YES) {
  628. if (plugNum == 1) {
  629. clearMiscCommand();
  630. }
  631. break;
  632. }
  633. if (isDetectPlugin() == YES) {
  634. ClearDetectPluginFlag();
  635. strcpy((char *)pSysConfig->UserId, "");
  636. pSysInfo->WaitForPlugit = NO;
  637. pSysInfo->SystemPage = _LCM_VIEW;
  638. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  639. pSysInfo->SystemTimeoutFlag = Timeout_None;
  640. destroySelectGun(plugNum);
  641. } else {
  642. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  643. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  644. }
  645. break;
  646. case MISC_CMD_AUTH_DISABLE:
  647. pSysConfig->AuthorisationMode = value;
  648. log_info("Auto Authorize Disable:%d", pSysConfig->AuthorisationMode);
  649. //clearMiscCommand();
  650. break;
  651. case MISC_CMD_EVCCID_ENABLE:
  652. pSysConfig->isAuthrizeByEVCCID = value;
  653. log_info("Authorize By EVCCID:%d",pSysConfig->isAuthrizeByEVCCID);
  654. //clearMiscCommand();
  655. break;
  656. case MISC_CMD_RESERVATION:
  657. if(value != YES)
  658. {
  659. break;
  660. }
  661. gMoreInfoReq[plugNum].bits.ReservationReq = YES;
  662. break;
  663. case MISC_CMD_CHANGE_LCM_PAGE:
  664. if(value == LCM_PAGE_REMOTE_START_NO_ID)
  665. {
  666. gMoreInfoReq[plugNum].bits.RemoteStartNoID = YES;
  667. }
  668. /*
  669. if(value == LCM_PAGE_JOIN_LINE)
  670. {
  671. if(!gPageReq[plugNum].bits.JoinLine)
  672. {
  673. log_info("Gun %d JoinLine page is trigger", plugNum);
  674. }
  675. gPageReq[plugNum].bits.JoinLine = YES;
  676. }
  677. if(value == LCM_PAGE_PAYMENT_SWITCH)
  678. {
  679. if(!gPageReq[plugNum].bits.PaymentSwitch)
  680. {
  681. log_info("Gun %d PaymentSwitch page is trigger", plugNum);
  682. }
  683. gPageReq[plugNum].bits.PaymentSwitch = YES;
  684. }*/
  685. break;
  686. case MISC_CMD_QR_CODE_REQ:
  687. break;
  688. case MISC_CMD_STATION_INFO_REQ:
  689. if(value != YES)
  690. {
  691. break;
  692. }
  693. gMoreInfoReq[plugNum].bits.StationInfoReq = YES;
  694. break;
  695. case MISC_CMD_FINAL_COST_REQ:
  696. if(value != YES)
  697. {
  698. break;
  699. }
  700. gMoreInfoReq[plugNum].bits.FinalCostReq = YES;
  701. break;
  702. case MISC_CMD_LINE_STATUS_REQ:
  703. LineStatusCode[plugNum] = value;
  704. ShmDcCommonData->LineStatus[plugNum] = value;
  705. log_info("Gun %d Line Status Code: %d", plugNum, LineStatusCode[plugNum]);
  706. break;
  707. case MISC_CMD_LED_INTENSITY:
  708. LedIntensity = value;
  709. log_info("Led Intensity: %d", LedIntensity);
  710. break;
  711. case MISC_CMD_TIME_OFFSET:
  712. TimeZoneOffset = value;
  713. log_info("Time Zone Offset: %d", TimeZoneOffset);
  714. ShmDcCommonData->TzOffset = TimeZoneOffset;
  715. break;
  716. default:
  717. clearMiscCommand();
  718. break;
  719. }
  720. usleep(128);
  721. }
  722. return ret;
  723. }
  724. void AddDispenserReq(uint8_t* buff, MiscCommand* req)
  725. {
  726. buff[0] = (req->CMD >> 8) & 0xFF;
  727. buff[1] = req->CMD & 0xFF;
  728. buff[2] = req->Value[0];
  729. buff[3] = req->Value[1];
  730. buff[4] = req->Value[2];
  731. buff[5] = req->Value[3];
  732. }
  733. static int chargingcapabilityHandle(uint8_t *data, uint8_t plugNum)
  734. {
  735. uint8_t addr = 0;
  736. float MaxVolt, MaxCurrent, MaxPower;
  737. static PricesInfo pricesInfo[2] = {0};
  738. CapabilityInfo *pCapabilityInfo = NULL;
  739. AccountInfo *pAccountInfo = NULL;
  740. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  741. //--- charging capability information --------------------------------------
  742. pCapabilityInfo = (CapabilityInfo *)&data[0];
  743. MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
  744. if (MaxVolt >= 0) {
  745. if (MaxVolt > MAX_VOLTAGE) {
  746. MaxVolt = MAX_VOLTAGE;
  747. }
  748. pDcChargingInfo->MaximumChargingVoltage = MaxVolt;
  749. }
  750. MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
  751. if (MaxCurrent >= 0) {
  752. if (MaxCurrent > MAX_CURRENCY) {
  753. MaxCurrent = MAX_CURRENCY;
  754. }
  755. pDcChargingInfo->AvailableChargingCurrent = MaxCurrent;
  756. }
  757. MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
  758. if (MaxPower >= 0) {
  759. if (MaxPower > MAX_POWER) {
  760. MaxPower = MAX_POWER;
  761. }
  762. pDcChargingInfo->AvailableChargingPower = MaxPower;
  763. }
  764. //MaxVolt = (float)(data[0] << 8 |data[1]);
  765. //MaxCurrent = (float)(data[2] << 8 | data[3]);
  766. //MaxPower = (float)(data[4] << 8 | data[5]);
  767. //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,", MaxVolt, MaxCurrent, MaxPower);
  768. //--- user prices information ----------------------------------------------
  769. addr = (sizeof(CapabilityInfo) - 2); //2 byte reserved
  770. pAccountInfo = (AccountInfo *)&data[addr];
  771. pSysConfig->BillingData.Currency = pAccountInfo->Currency;
  772. ShmSelectGunInfo->PricesInfo[plugNum].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  773. pDcChargingInfo->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  774. ShmSelectGunInfo->PricesInfo[plugNum].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  775. ShmSelectGunInfo->PricesInfo[plugNum].Discount = transPricesUnit(ntohl(pAccountInfo->Discount));
  776. ShmSelectGunInfo->PricesInfo[plugNum].TransactionId = ntohl(pAccountInfo->Transaction);
  777. if ((pricesInfo[plugNum].UserPrices != ShmSelectGunInfo->PricesInfo[plugNum].UserPrices) ||
  778. (pricesInfo[plugNum].Balance != ShmSelectGunInfo->PricesInfo[plugNum].Balance) ||
  779. (pricesInfo[plugNum].Discount != ShmSelectGunInfo->PricesInfo[plugNum].Discount) ||
  780. (pricesInfo[plugNum].TransactionId != ShmSelectGunInfo->PricesInfo[plugNum].TransactionId)) {
  781. pricesInfo[plugNum].UserPrices = ShmSelectGunInfo->PricesInfo[plugNum].UserPrices;
  782. pricesInfo[plugNum].Balance = ShmSelectGunInfo->PricesInfo[plugNum].Balance;
  783. pricesInfo[plugNum].Discount = ShmSelectGunInfo->PricesInfo[plugNum].Discount;
  784. pricesInfo[plugNum].TransactionId = ShmSelectGunInfo->PricesInfo[plugNum].TransactionId;
  785. log_info("id = %d, transaction id = %d, user prices = %.2f, Discount = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s",
  786. plugNum,
  787. ShmSelectGunInfo->PricesInfo[plugNum].TransactionId,
  788. ShmSelectGunInfo->PricesInfo[plugNum].UserPrices,
  789. ShmSelectGunInfo->PricesInfo[plugNum].Discount,
  790. pDcChargingInfo->ChargingFee,
  791. ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  792. (uint8_t *)GetCurrency(pSysConfig->BillingData.Currency)
  793. );
  794. }
  795. return PASS;
  796. }
  797. static void addFaultCodeToBuf(uint8_t *Code)
  798. {
  799. uint8_t warningCount = pSysWarning->WarningCount;
  800. if (warningCount < 10) {
  801. memcpy(&pSysWarning->WarningCode[warningCount][0],
  802. Code,
  803. strlen((char *)Code));
  804. pSysWarning->WarningCount++;
  805. }
  806. }
  807. static void removeFaultCodeToBuf(uint8_t *Code)
  808. {
  809. uint8_t find = 0x01;
  810. uint8_t i = 0;
  811. char _code[7] = {0};
  812. sprintf(_code, "%s", Code);
  813. // 把相關的錯誤碼一次移除,避免重複顯示
  814. while (find) {
  815. usleep(128);
  816. find = 0x00;
  817. for (i = 0; i < pSysWarning->WarningCount; i++) {
  818. usleep(128);
  819. if (find == 0x00) {
  820. if (memcmp(&pSysWarning->WarningCode[i][0], _code, 7) == 0) {
  821. find = 0x01;
  822. }
  823. } else {
  824. memcpy(&pSysWarning->WarningCode[i - 1][0],
  825. &pSysWarning->WarningCode[i][0], 7);
  826. }
  827. }
  828. if (find) {
  829. pSysWarning->WarningCount--;
  830. }
  831. }
  832. }
  833. bool CompareArrayIsZero(uint8_t *array, unsigned int array_size)
  834. {
  835. uint8_t i;
  836. if (array != NULL) {
  837. for (i = 0; i < array_size; i++) {
  838. if (array[i] != 0) {
  839. return false;
  840. }
  841. }
  842. }
  843. return true;
  844. }
  845. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  846. {
  847. uint8_t ret = 0;
  848. uint8_t i = 0;
  849. uint8_t count = 0;
  850. uint8_t EventCodeTmp[7] = {0};
  851. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  852. uint8_t remaindLen = 0;
  853. //uint8_t statusCodeError = 0;
  854. if (dataLen > 0) {
  855. //Hexdump((uint8_t *)data, dataLen);
  856. remaindLen = dataLen % WARNING_CODE_SIZE;
  857. if (remaindLen != 0) {
  858. log_info("fail status code length = %d", dataLen);
  859. dataLen -= remaindLen;
  860. }
  861. if (dataLen < WARNING_CODE_SIZE) {
  862. log_error("fail status code length = %d", dataLen);
  863. //Hexdump(data, dataLen);
  864. if (pSysWarning->WarningCount > 0) {
  865. for (i = 0; i < pSysWarning->WarningCount; i++) {
  866. usleep(128);
  867. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet
  868. (pSysWarning->WarningCode[i][0] >= 'B' &&
  869. pSysWarning->WarningCode[i][1] >= '4')) {
  870. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  871. memcpy(EventCodeTmp,
  872. pSysWarning->WarningCode[i],
  873. sizeof(EventCodeTmp));
  874. removeFaultCodeToBuf(EventCodeTmp);
  875. }
  876. }
  877. }
  878. return FAIL;
  879. }
  880. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  881. usleep(128);
  882. if (strncmp((char *)&data[count + 1], "1", 1) != 0 &&
  883. strncmp((char *)&data[count + 1], "2", 1) != 0 &&
  884. strncmp((char *)&data[count + 1], "3", 1) != 0 &&
  885. (strncmp((char *)&data[count], "B", 1) != 0 &&
  886. strncmp((char *)&data[count + 1], "4", 1) != 0)
  887. ) {
  888. log_error("error status code = %s", (char *)&data[count]);
  889. continue;
  890. }
  891. // misc command status code handle
  892. if (strncmp((char *)&data[count], MISC_ST_MISC_CMD, WARNING_CODE_SIZE) == 0) {
  893. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  894. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  895. continue;
  896. } else if (strncmp((char *)&data[count], MISC_ST_VERSION, WARNING_CODE_SIZE) == 0) {
  897. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  898. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  899. continue;
  900. }
  901. if (CompareArrayIsZero((uint8_t *)&data[count], WARNING_CODE_SIZE) == true) {
  902. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  903. memcpy(EventCodeTmp, (char *)&data[count], sizeof(EventCodeTmp));
  904. log_error("error status = %s", EventCodeTmp);
  905. continue;
  906. }
  907. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  908. ret = 0;
  909. for (i = 0; i < pSysWarning->WarningCount; i++) {
  910. usleep(128);
  911. if (memcmp(&pSysWarning->WarningCode[i][0],
  912. StatusArray[count / WARNING_CODE_SIZE],
  913. WARNING_CODE_SIZE) == 0) {
  914. ret = 1;
  915. break;
  916. }
  917. }
  918. if (ret == 0) {
  919. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  920. }
  921. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  922. }
  923. } else {
  924. if (CompareArrayIsZero(data, WARNING_CODE_SIZE) == false) {
  925. log_error("power cabinet status code data length is zero, but have data");
  926. //Hexdump((uint8_t *)data, WARNING_CODE_SIZE);
  927. }
  928. }
  929. for (i = 0; i < pSysWarning->WarningCount; i++) {
  930. usleep(128);
  931. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet 0x33
  932. (pSysWarning->WarningCode[i][0] >= 'B' &&
  933. pSysWarning->WarningCode[i][1] >= '4')) {
  934. ret = 0;
  935. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  936. usleep(128);
  937. if (memcmp(&pSysWarning->WarningCode[i][0],
  938. StatusArray[count],
  939. WARNING_CODE_SIZE) == 0) {
  940. ret = 1;
  941. break;
  942. }
  943. }
  944. if (ret == 0) {
  945. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  946. memcpy(EventCodeTmp,
  947. pSysWarning->WarningCode[i],
  948. sizeof(EventCodeTmp));
  949. removeFaultCodeToBuf(EventCodeTmp);
  950. }
  951. }
  952. }
  953. return PASS;
  954. }
  955. void GetCabientDcmRevHandle(uint8_t *data)
  956. {
  957. PCDCMVer *pVersion = (PCDCMVer *)&data[0];
  958. strcpy(ShmDcCommonData->CabinetModelName , pVersion->CabinetModelName);
  959. strcpy(ShmDcCommonData->CabinetBoolLoaderVersion, pVersion->CabinetBoolLoaderVersion);
  960. strcpy(ShmDcCommonData->CabinetKernelVersion , pVersion->CabinetKernelVersion);
  961. strcpy(ShmDcCommonData->CabinetRFSystemVersion , pVersion->CabinetRFSystemVersion);
  962. strcpy(ShmDcCommonData->CabinetPrimaryVersion , pVersion->CabinetPrimaryVersion);
  963. strcpy(ShmDcCommonData->CabinetIPAddr , pVersion->CabinetIPAddr);
  964. }
  965. void GetCabientOtherRevHandle(uint8_t *data)
  966. {
  967. PCOthVer *pVersion = (PCOthVer *)&data[0];
  968. strcpy(ShmDcCommonData->CabinetRelay0Version , pVersion->CabinetRelay0Version);
  969. strcpy(ShmDcCommonData->CabinetRelay1Version , pVersion->CabinetRelay1Version);
  970. strcpy(ShmDcCommonData->CabinetFanVersion , pVersion->CabinetFanVersion);
  971. }
  972. void GetCabientEachPsuRevHandle(uint8_t *data)
  973. {
  974. PCnPsuVer *pVersion = (PCnPsuVer *)&data[0];
  975. strcpy(ShmDcCommonData->PsuVer[pVersion->n_PSU].DCVersion, pVersion->DCVersion);
  976. strcpy(ShmDcCommonData->PsuVer[pVersion->n_PSU].FPCVersion, pVersion->FPCVersion);
  977. }
  978. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t plugNum, uint8_t reg)
  979. {
  980. int ret = PASS;
  981. uint8_t rawDataLen = 0;
  982. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  983. SoftwareUpdInfo *pSoftwareUpd = NULL;
  984. PcPsuOutput *pPcPsuOutput = NULL;
  985. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  986. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  987. compareResult(pCsuResult->Data.Result) == FAIL ||
  988. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  989. if (reg != REG_CHARGING_PERMISSION) {
  990. return FAIL;
  991. }
  992. }
  993. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  994. switch (pCsuResult->Data.Register) {
  995. case REG_MODEL_NAME:
  996. break;
  997. case REG_CONNECTOR_ID:
  998. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  999. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  1000. ShmDcCommonData->ConnectorId[0] = pCsuResult->Data.Data[0];
  1001. ShmDcCommonData->ConnectorId[1] = pCsuResult->Data.Data[1];
  1002. log_info("OK (Left connector ID = %d, Right connector ID = %d)",
  1003. pCsuResult->Data.Data[0],
  1004. pCsuResult->Data.Data[1]);
  1005. break;
  1006. case REG_POWER_CABINET_STATUS:
  1007. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  1008. break;
  1009. case REG_DISPENSER_STATUS:
  1010. memset(&gPreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  1011. memcpy((uint8_t *)&gPreSysWarningInfo,
  1012. pSysWarning,
  1013. sizeof(struct WARNING_CODE_INFO));
  1014. break;
  1015. case REG_CHARGING_CAP:
  1016. chargingcapabilityHandle(pCsuResult->Data.Data, plugNum);
  1017. break;
  1018. case REG_CHARGING_TARGET:
  1019. break;
  1020. case REG_SOFTWARE_UPDATE:
  1021. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  1022. if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
  1023. (rawDataLen == 0) ||
  1024. pSoftwareUpd->UpdateState != 1) {
  1025. ret = FAIL;
  1026. break;
  1027. }
  1028. ret = FAIL;
  1029. ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
  1030. break;
  1031. case REG_PLUG_IN_STATE:
  1032. break;
  1033. case REG_CONNECTOR_STATE:
  1034. break;
  1035. case REG_USER_ID:
  1036. //log_info("USER ID:%s", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  1037. //if (pCsuResult->Data.Data[0] <= 0) {
  1038. // return FAIL;
  1039. //}
  1040. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1041. return COMMAND_RESULT_NG;
  1042. }
  1043. ShmSelectGunInfo->AuthorStateFromCabinet[plugNum] = pCsuResult->Data.Data[0];
  1044. return pCsuResult->Data.Data[0];
  1045. break;
  1046. case REG_CHARGING_PERMISSION:
  1047. //log_info("id = %d, result = %s, action = %s",
  1048. // pCsuResult->Head.ID,
  1049. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  1050. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  1051. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1052. return COMMAND_RESULT_NG;
  1053. }
  1054. ShmSelectGunInfo->WaitDoCommPermission[plugNum] = pCsuResult->Data.Data[0];
  1055. return pCsuResult->Data.Data[0];
  1056. break;
  1057. case REG_MISC_CONTROL:
  1058. miscCommandHandle(rawDataLen, plugNum, pCsuResult->Data.Data);
  1059. break;
  1060. case REG_REPORT_CSU_VERSION:
  1061. case REG_REPORT_OTHER_VERSION:
  1062. break;
  1063. case REG_PRESENT_CHARGING_INFO:
  1064. pPcPsuOutput = (PcPsuOutput *)&pCsuResult->Data.Data[0];
  1065. ShmDcCommonData->PcPsuOutput[plugNum].Voltage = ntohs((uint16_t)pPcPsuOutput->Voltage);
  1066. ShmDcCommonData->PcPsuOutput[plugNum].Current = ntohs((uint16_t)pPcPsuOutput->Current);
  1067. //log_info("%d power cabinet PSU output vol = %f, cur = %f",
  1068. // plugNum,
  1069. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Voltage,
  1070. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Current);
  1071. break;
  1072. case REG_QRCODE_URL_INFO:
  1073. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  1074. break;
  1075. case REG_WAIT_PLUG_IT_STATE:
  1076. break;
  1077. case REG_Ground_Fault_Detection:
  1078. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1079. return COMMAND_RESULT_NG;
  1080. }
  1081. //集電弓relay 不打開才能進入動作
  1082. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1083. if(pDcChargingInfo->PantographFlag == YES)
  1084. {
  1085. GroundFaultDetection *gfd = (GroundFaultDetection *)&pCsuResult->Data.Data[0];
  1086. if(pDcChargingInfo->GroundFaultStatus != gfd->Status)
  1087. {
  1088. log_info("id = %d, GFD Result = %d",
  1089. pCsuResult->Head.ID,
  1090. gfd->Status);
  1091. }
  1092. pDcChargingInfo->GroundFaultStatus = gfd->Status;
  1093. }
  1094. break;
  1095. case REG_RESERVATION_IDTAG:
  1096. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1097. return COMMAND_RESULT_NG;
  1098. }
  1099. uint8_t reservationState = pCsuResult->Data.Data[0] == YES ? YES : NO;
  1100. memset(&ReservationIdTag[plugNum][0], 0x00, 32);
  1101. if(reservationState)
  1102. {
  1103. strcpy(&ReservationIdTag[plugNum][0], (char *)&pCsuResult->Data.Data[1]);
  1104. }
  1105. if(reservationState != ReservationState[plugNum])
  1106. {
  1107. log_info("id = %d reservation is %s", pCsuResult->Head.ID, reservationState ? "trigger" : "expired");
  1108. if(reservationState)
  1109. {
  1110. log_info("id = %d reservation idTag: %s", pCsuResult->Head.ID, (char *)&ReservationIdTag[plugNum][0]);
  1111. }
  1112. }
  1113. ReservationState[plugNum] = reservationState;
  1114. break;
  1115. case REG_REMOTE_START_NO_ID:
  1116. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1117. return COMMAND_RESULT_NG;
  1118. }
  1119. uint8_t remoteStartNoIdState = pCsuResult->Data.Data[0] == YES ? YES : NO;
  1120. if(remoteStartNoIdState != RemoteStartNoIDState)
  1121. {
  1122. log_info("RemoteStartNoID is %s", remoteStartNoIdState ? "trigger" : "expired");
  1123. }
  1124. RemoteStartNoIDState = remoteStartNoIdState;
  1125. break;
  1126. case REG_STATION_INFO:
  1127. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1128. return COMMAND_RESULT_NG;
  1129. }
  1130. StationVar _stationInfo;
  1131. memset((char *)&_stationInfo, 0x00, sizeof(StationVar));
  1132. _stationInfo.StationID = (pCsuResult->Data.Data[0] << 24) +
  1133. (pCsuResult->Data.Data[1] << 16) +
  1134. (pCsuResult->Data.Data[2] << 8) +
  1135. pCsuResult->Data.Data[3];
  1136. memcpy(_stationInfo.StationName, (char *)&pCsuResult->Data.Data[4], 64);
  1137. _stationInfo.WeatherID |= (pCsuResult->Data.Data[68] << 24);
  1138. _stationInfo.WeatherID |= (pCsuResult->Data.Data[69] << 16);
  1139. _stationInfo.WeatherID |= (pCsuResult->Data.Data[70] << 8);
  1140. _stationInfo.WeatherID |= pCsuResult->Data.Data[71];
  1141. uint8_t *pFloat = (uint8_t *)&_stationInfo.Temperature;
  1142. *(pFloat + 3) = pCsuResult->Data.Data[72];
  1143. *(pFloat + 2) = pCsuResult->Data.Data[73];
  1144. *(pFloat + 1) = pCsuResult->Data.Data[74];
  1145. *(pFloat) = pCsuResult->Data.Data[75];
  1146. ShmDcCommonData->WeatherID = _stationInfo.WeatherID;
  1147. ShmDcCommonData->Temperature = _stationInfo.Temperature;
  1148. ShmDcCommonData->Location = atoi(_stationInfo.StationName);
  1149. log_info("Station Name: %s, ID: %d, Weather: %d, Temperature: %.1f",
  1150. _stationInfo.StationName,
  1151. _stationInfo.StationID,
  1152. _stationInfo.WeatherID,
  1153. _stationInfo.Temperature);
  1154. break;
  1155. case REG_DEDUCT_INFO:
  1156. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1157. return COMMAND_RESULT_NG;
  1158. }
  1159. break;
  1160. default:
  1161. break;
  1162. }
  1163. return ret;
  1164. }
  1165. static int composeSocketData(int fd,
  1166. uint8_t id,
  1167. uint8_t opCode,
  1168. uint8_t reg,
  1169. uint8_t dataLen,
  1170. uint8_t *data)
  1171. {
  1172. int ret = PASS;
  1173. int size = 0;
  1174. int sendPktLen = 0;
  1175. uint8_t i = 0;
  1176. uint8_t plugNum = 0;
  1177. CsuCmdPkt csuCmdPkt = {0};
  1178. CsuResultPkt csuResult = {0};
  1179. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  1180. csuCmdPkt.Head.ID = id;
  1181. csuCmdPkt.Head.OP = opCode;
  1182. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  1183. csuCmdPkt.Data.Register = reg;
  1184. if ((data != NULL) && (dataLen > 0)) {
  1185. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  1186. }
  1187. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  1188. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  1189. // Not Let ask cabinet version trasmit into FAIL Network
  1190. if (reg == REG_CABINET_DCM_VERSION || reg == REG_CABINET_OTHER_VERSION ||
  1191. reg == REG_TOTAL_PSU_QUANTITY || reg == REG_PSU_VERSION) {
  1192. gDoCommGblData.DisConnCount = 0;
  1193. }
  1194. //send command packet
  1195. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  1196. log_error("TCP socket send packet fail = %d", size);
  1197. gDoCommGblData.DisConnCount++;
  1198. return FAIL;
  1199. }
  1200. //receive result head
  1201. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  1202. log_error("TCP socket RX head fail = %d", size);
  1203. gDoCommGblData.DisConnCount++;
  1204. return FAIL;
  1205. }
  1206. //receive result raw data
  1207. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  1208. log_error("TCP socket RX data fail = %d", size);
  1209. gDoCommGblData.DisConnCount++;
  1210. return FAIL;
  1211. }
  1212. for (i = 0; i < sizeof(gDoCommGblData.ConnectorID); i++) {
  1213. if (id == gDoCommGblData.ConnectorID[i]) {
  1214. plugNum = i;
  1215. break;
  1216. }
  1217. }
  1218. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, plugNum, csuCmdPkt.Data.Register);
  1219. gDoCommGblData.DisConnCount = 0; //送收資料沒有問題, 清除中斷網路計數
  1220. return ret;
  1221. }
  1222. static int writeDispenserRequest(int fd, uint8_t id, uint8_t gunIndex)
  1223. {
  1224. int ret = PASS;
  1225. int count = 0;
  1226. MiscCommand dispenserReq;
  1227. uint8_t data[6] = { 0 };
  1228. if (gConnectorActReq[gunIndex].bits.ChargingCancel)
  1229. {
  1230. dispenserReq.CMD = DISPENSER_REQ_CHARGING_CANCEL;
  1231. dispenserReq.Value[0] = 0;
  1232. dispenserReq.Value[1] = 0;
  1233. dispenserReq.Value[2] = 0;
  1234. dispenserReq.Value[3] = 1;
  1235. AddDispenserReq(data, &dispenserReq);
  1236. count++;
  1237. log_info("Write Gun %d CHARGING_CANCEL", gunIndex);
  1238. }
  1239. if ((ret = composeSocketData(fd,
  1240. id,
  1241. OP_WRITE_DATA,
  1242. REG_DISPENSER_REQUEST,
  1243. (count * 6),
  1244. &data[0])) == FAIL) {
  1245. return ret;
  1246. }
  1247. return ret;
  1248. }
  1249. static int readReservationState(int fd, uint8_t id)
  1250. {
  1251. int ret = PASS;
  1252. if ((ret = composeSocketData(fd,
  1253. id,
  1254. OP_READ_DATA,
  1255. REG_RESERVATION_IDTAG,
  1256. 0,
  1257. NULL)) == FAIL) {
  1258. return ret;
  1259. }
  1260. return ret;
  1261. }
  1262. static int readRemoteStartNoIDState(int fd)
  1263. {
  1264. int ret = PASS;
  1265. if ((ret = composeSocketData(fd,
  1266. ID_REGISTER,
  1267. OP_READ_DATA,
  1268. REG_REMOTE_START_NO_ID,
  1269. 0,
  1270. NULL)) == FAIL) {
  1271. return ret;
  1272. }
  1273. return ret;
  1274. }
  1275. static int readChargerStationInfo(int fd)
  1276. {
  1277. int ret = PASS;
  1278. if ((ret = composeSocketData(fd,
  1279. ID_REGISTER,
  1280. OP_READ_DATA,
  1281. REG_STATION_INFO,
  1282. 0,
  1283. NULL)) == FAIL) {
  1284. return ret;
  1285. }
  1286. return ret;
  1287. }
  1288. static int writeDeductInfo(int fd, uint8_t id,uint8_t gunIndex, RecordTransactionInfo *transactionInfo)
  1289. {
  1290. int ret = PASS;
  1291. uint8_t dataBuf[22] = {0};
  1292. int i;
  1293. memset((char *)dataBuf, 0x00, sizeof(dataBuf));
  1294. dataBuf[0] = transactionInfo->DeductResult;
  1295. dataBuf[1] = transactionInfo->IsDonateInvoice;
  1296. dataBuf[2] = (transactionInfo->TransactionId >> 24) & 0xFF;
  1297. dataBuf[3] = (transactionInfo->TransactionId >> 16) & 0xFF;
  1298. dataBuf[4] = (transactionInfo->TransactionId >> 8) & 0xFF;
  1299. dataBuf[5] = (transactionInfo->TransactionId & 0xFF);
  1300. int amount = (int)(transactionInfo->Amount * 100);
  1301. dataBuf[6] = (amount >> 24) & 0xFF;
  1302. dataBuf[7] = (amount >> 16) & 0xFF;
  1303. dataBuf[8] = (amount >> 8) & 0xFF;
  1304. dataBuf[9] = (amount & 0xFF);
  1305. memcpy((char *)&dataBuf[10], transactionInfo->ApprovalNo,9);
  1306. /*
  1307. for(i=0;i<22;i++) {
  1308. log_info("dataBuf[%d]:0x%x",i,dataBuf[i]);
  1309. }
  1310. */
  1311. log_info("Gun[%d] TransactionId:%d DeductResult:%d IsDonateInvoice:%d Amount:%f",
  1312. gunIndex,transactionInfo->TransactionId,transactionInfo->DeductResult,transactionInfo->IsDonateInvoice,transactionInfo->Amount);
  1313. // copy deduct result to dataBuf here
  1314. if ((ret = composeSocketData(fd,
  1315. id,
  1316. OP_WRITE_DATA,
  1317. REG_DEDUCT_INFO,
  1318. 22,
  1319. &dataBuf[0])) == FAIL) {
  1320. return ret;
  1321. }
  1322. return ret;
  1323. }
  1324. static int writeWaitPlugItState(int fd, uint8_t id)
  1325. {
  1326. int ret = PASS;
  1327. uint8_t data[2] = {pSysInfo->WaitForPlugit, 0};
  1328. //printf("WaitForPlugit = %d", pSysInfo->WaitForPlugit);
  1329. if ((ret = composeSocketData(fd,
  1330. id,
  1331. OP_WRITE_DATA,
  1332. REG_WAIT_PLUG_IT_STATE,
  1333. 1,
  1334. &data[0])) == FAIL) {
  1335. return ret;
  1336. }
  1337. return ret;
  1338. }
  1339. static int readQRcodeURLAndSystemDate(int fd)
  1340. {
  1341. int ret = PASS;
  1342. ret = composeSocketData(fd,
  1343. ID_REGISTER,
  1344. OP_READ_DATA,
  1345. REG_QRCODE_URL_INFO,
  1346. 0,
  1347. NULL);
  1348. return ret;
  1349. }
  1350. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1351. {
  1352. int ret = PASS;
  1353. uint8_t dataBuf[16] = {0};
  1354. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1355. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1356. if(pDcChargingInfo->PantographFlag == NO)
  1357. {
  1358. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(pDcChargingInfo->PresentChargingVoltage * 10));
  1359. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(pDcChargingInfo->PresentChargingCurrent * 10));
  1360. }
  1361. else
  1362. {
  1363. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Voltage));
  1364. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Current));
  1365. }
  1366. pPreChargingInfo->RemainChargingDuration = htonl(pDcChargingInfo->RemainChargingDuration);
  1367. pPreChargingInfo->EvBatterySoc = pDcChargingInfo->EvBatterySoc;
  1368. ret = composeSocketData(fd,
  1369. id,
  1370. OP_WRITE_DATA,
  1371. REG_PRESENT_CHARGING_INFO,
  1372. sizeof(PreChargingInfo),
  1373. (uint8_t *)pPreChargingInfo);
  1374. return ret;
  1375. }
  1376. static int writeOtherModuleVersion(int fd)
  1377. {
  1378. int ret = PASS;
  1379. uint8_t data[255] = {0};
  1380. uint8_t dataLen = 0;
  1381. //report other module version message
  1382. memcpy(&data[dataLen], pSysInfo->FanModuleFwRev, VERSION_BUF_SIZE);
  1383. dataLen += VERSION_BUF_SIZE;
  1384. memcpy(&data[dataLen], pSysInfo->RelayModuleFwRev, VERSION_BUF_SIZE);
  1385. dataLen += VERSION_BUF_SIZE;
  1386. memcpy(&data[dataLen], pSysInfo->Connector1FwRev, VERSION_BUF_SIZE);
  1387. dataLen += VERSION_BUF_SIZE;
  1388. memcpy(&data[dataLen], pSysInfo->Connector2FwRev, VERSION_BUF_SIZE);
  1389. dataLen += VERSION_BUF_SIZE;
  1390. memcpy(&data[dataLen], pSysInfo->LedModuleFwRev, VERSION_BUF_SIZE);
  1391. dataLen += VERSION_BUF_SIZE;
  1392. ret = composeSocketData(fd,
  1393. ID_REGISTER,
  1394. OP_WRITE_DATA,
  1395. REG_REPORT_OTHER_VERSION,
  1396. dataLen,
  1397. &data[0]);
  1398. return ret;
  1399. }
  1400. static int writeCsuModuleVersion(int fd)
  1401. {
  1402. int ret = PASS;
  1403. uint8_t data[255] = {0};
  1404. uint8_t dataLen = 0;
  1405. //report CSU platform version message
  1406. memcpy(&data[dataLen], pSysInfo->CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1407. dataLen += VERSION_BUF_SIZE;
  1408. memcpy(&data[dataLen], pSysInfo->CsuKernelFwRev, VERSION_BUF_SIZE);
  1409. dataLen += VERSION_BUF_SIZE;
  1410. memcpy(&data[dataLen], pSysInfo->CsuRootFsFwRev, VERSION_BUF_SIZE);
  1411. dataLen += VERSION_BUF_SIZE;
  1412. memcpy(&data[dataLen], pSysInfo->CsuPrimFwRev, VERSION_BUF_SIZE);
  1413. dataLen += VERSION_BUF_SIZE;
  1414. ret = composeSocketData(fd,
  1415. ID_REGISTER,
  1416. OP_WRITE_DATA,
  1417. REG_REPORT_CSU_VERSION,
  1418. dataLen,
  1419. &data[0]);
  1420. return ret;
  1421. }
  1422. static int readMiscCommand(int fd, uint8_t id)
  1423. {
  1424. int ret = PASS;
  1425. ret = composeSocketData(fd,
  1426. id,
  1427. OP_READ_DATA,
  1428. REG_MISC_CONTROL,
  1429. 0,
  1430. NULL);
  1431. return ret;
  1432. }
  1433. static int readChargePermission(int fd, uint8_t id)
  1434. {
  1435. return composeSocketData(fd,
  1436. id,
  1437. OP_READ_DATA,
  1438. REG_CHARGING_PERMISSION,
  1439. 0,
  1440. NULL);
  1441. }
  1442. static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id)
  1443. {
  1444. int ret = PASS;
  1445. uint8_t dataBuf[1] = {status};
  1446. ret = composeSocketData(fd,
  1447. id,
  1448. OP_WRITE_DATA,
  1449. REG_Ground_Fault_Detection,
  1450. 1,
  1451. &dataBuf[0]);
  1452. return ret;
  1453. }
  1454. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1455. {
  1456. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1457. return FAIL;
  1458. }
  1459. return composeSocketData(fd,
  1460. id,
  1461. OP_WRITE_DATA,
  1462. REG_USER_ID,
  1463. strlen((char *)pUserID),
  1464. pUserID);
  1465. }
  1466. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1467. {
  1468. uint8_t dataBuf[10] = {'\0'};
  1469. ConnectorState *pConnState = (ConnectorState *)dataBuf;
  1470. static char vendorErrorCodeTmp[2][WARNING_CODE_SIZE] = {0};
  1471. int ret = PASS;
  1472. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1473. pConnState->ConnectorTemp = pDcChargingInfo->ConnectorTemp;
  1474. pConnState->ChillerTemp = pDcChargingInfo->ChillerTemp;
  1475. pConnState->PlugIn = pDcChargingInfo->ConnectorPlugIn;
  1476. if (pDcChargingInfo->SystemStatus <= S_AUTHORIZING) {
  1477. pConnState->State = CONN_ST_IDLE; //idle
  1478. strncpy((char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1479. "",
  1480. sizeof(ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode));
  1481. strncpy(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE);
  1482. } else if ((pDcChargingInfo->SystemStatus <= S_PREPARING_FOR_EVSE) ||
  1483. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST0) ||
  1484. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST1)) {
  1485. pConnState->State = CONN_ST_PREPARING; //preparing
  1486. } else if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  1487. pConnState->State = CONN_ST_CHARGING; //charging
  1488. } else if (pDcChargingInfo->SystemStatus == S_TERMINATING ||
  1489. pDcChargingInfo->SystemStatus == S_COMPLETE) {
  1490. pConnState->State = CONN_ST_TERMINATING; //terminating
  1491. } else if ((pDcChargingInfo->SystemStatus == S_ALARM) ||
  1492. (pDcChargingInfo->SystemStatus == S_FAULT)) {
  1493. pConnState->State = CONN_ST_ALARM;
  1494. strncpy((char *)pConnState->WarningCode,
  1495. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1496. WARNING_CODE_SIZE);
  1497. if (strncmp(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE) == 0) {
  1498. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1499. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1500. WARNING_CODE_SIZE);
  1501. vendorErrorCodeTmp[plugNum][6] = '\0';
  1502. /*
  1503. log_info("1 ID = %d, VendorErrorCode = %s",
  1504. plugNum,
  1505. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1506. _VenderErrorCodeFlag = true;
  1507. */
  1508. } else {
  1509. if (strncmp(&vendorErrorCodeTmp[plugNum][0],
  1510. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1511. WARNING_CODE_SIZE) != 0
  1512. ) {
  1513. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1514. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1515. WARNING_CODE_SIZE);
  1516. vendorErrorCodeTmp[plugNum][6] = '\0';
  1517. log_info("2 ID = %d, VendorErrorCode = %s",
  1518. plugNum,
  1519. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1520. }
  1521. }
  1522. }
  1523. ret = composeSocketData(fd,
  1524. id,
  1525. OP_WRITE_DATA,
  1526. REG_CONNECTOR_STATE,
  1527. sizeof(dataBuf) - 1,
  1528. &dataBuf[0]);
  1529. return ret;
  1530. }
  1531. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1532. {
  1533. int ret = PASS;
  1534. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1535. uint8_t dataBuf[2] = {pDcChargingInfo->ConnectorPlugIn, 0};
  1536. ret = composeSocketData(fd,
  1537. id,
  1538. OP_WRITE_DATA,
  1539. REG_PLUG_IN_STATE,
  1540. 1,
  1541. &dataBuf[0]);
  1542. return ret;
  1543. }
  1544. static int readSoftwareUpdate(int fd, uint8_t gunID)
  1545. {
  1546. int ret = PASS;
  1547. uint8_t dataBuf[2] = {pSysInfo->FirmwareUpdate, 0};
  1548. ret = composeSocketData(fd,
  1549. gunID,
  1550. OP_READ_DATA,
  1551. REG_SOFTWARE_UPDATE,
  1552. 1,
  1553. &dataBuf[0]);
  1554. return ret;
  1555. }
  1556. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1557. {
  1558. int ret = PASS;
  1559. float ChargingVolt, ChargingAmp;
  1560. uint8_t dataBuf[4] = {0};
  1561. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1562. ChargingVolt = pDcChargingInfo->EvBatterytargetVoltage;
  1563. ChargingAmp = pDcChargingInfo->EvBatterytargetCurrent;
  1564. ChargingVolt *= 10;
  1565. ChargingAmp *= 10;
  1566. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1567. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1568. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1569. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1570. ret = composeSocketData(fd,
  1571. id,
  1572. OP_WRITE_DATA,
  1573. REG_CHARGING_TARGET,
  1574. sizeof(dataBuf),
  1575. &dataBuf[0]);
  1576. return ret;
  1577. }
  1578. static int readChargingCapability(int fd, uint8_t id)
  1579. {
  1580. int ret = PASS;
  1581. ret = composeSocketData(fd,
  1582. id,
  1583. OP_READ_DATA,
  1584. REG_CHARGING_CAP,
  1585. 0,
  1586. NULL);
  1587. return ret;
  1588. }
  1589. static int readCabinetDCMVersion(int fd, uint8_t id)
  1590. {
  1591. int ret = PASS;
  1592. ret = composeSocketData(fd,
  1593. id,
  1594. OP_READ_DATA,
  1595. REG_CABINET_DCM_VERSION,
  1596. 0,
  1597. NULL);
  1598. return ret;
  1599. }
  1600. static int readCabinetOtherVersion(int fd, uint8_t id)
  1601. {
  1602. int ret = PASS;
  1603. ret = composeSocketData(fd,
  1604. id,
  1605. OP_READ_DATA,
  1606. REG_CABINET_OTHER_VERSION,
  1607. 0,
  1608. NULL);
  1609. return ret;
  1610. }
  1611. static int readCabinetPSUNumber(int fd, uint8_t id)
  1612. {
  1613. int ret = PASS;
  1614. ret = composeSocketData(fd,
  1615. id,
  1616. OP_READ_DATA,
  1617. REG_TOTAL_PSU_QUANTITY,
  1618. 0,
  1619. NULL);
  1620. return ret;
  1621. }
  1622. static int readCabinetEachPSUVersion(int fd, uint8_t id,uint8_t n_psu)
  1623. {
  1624. int ret = PASS;
  1625. uint8_t data_buf[0] = {0};
  1626. data_buf[0] = n_psu;
  1627. ret = composeSocketData(fd,
  1628. id,
  1629. OP_READ_DATA,
  1630. REG_PSU_VERSION,
  1631. sizeof(data_buf),
  1632. &data_buf[0]);
  1633. return ret;
  1634. }
  1635. static int writeDispenserStatus(int fd, uint8_t gunID)
  1636. {
  1637. uint8_t warningCount = 0;
  1638. uint8_t count = 0;
  1639. uint8_t CurWarnCodeTmp[10][6] = {0};
  1640. uint8_t PreWarnCodeTmp[10][6] = {0};
  1641. int ret = PASS;
  1642. if ((pSysWarning->WarningCount <= 0) &&
  1643. (gPreSysWarningInfo.WarningCount <= 0)) {
  1644. return FAIL;
  1645. }
  1646. warningCount = pSysWarning->WarningCount;
  1647. for (count = 0; count < warningCount; count++) {
  1648. memcpy(CurWarnCodeTmp[count], pSysWarning->WarningCode[count], WARNING_CODE_SIZE);
  1649. }
  1650. warningCount = gPreSysWarningInfo.WarningCount;
  1651. for (count = 0; count < warningCount; count++) {
  1652. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1653. }
  1654. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1655. return FAIL;
  1656. }
  1657. ret = composeSocketData(fd,
  1658. gunID,
  1659. OP_WRITE_DATA,
  1660. REG_DISPENSER_STATUS,
  1661. strlen((char *)CurWarnCodeTmp),
  1662. &CurWarnCodeTmp[0][0]);
  1663. return ret;
  1664. }
  1665. static int readPowerCabinetStatus(int fd, uint8_t gunID)
  1666. {
  1667. int ret = PASS;
  1668. ret = composeSocketData(fd,
  1669. gunID,
  1670. OP_READ_DATA,
  1671. REG_POWER_CABINET_STATUS,
  1672. 0,
  1673. NULL);
  1674. return ret;
  1675. }
  1676. static int readConnectorID(int fd)
  1677. {
  1678. int ret = PASS;
  1679. ret = composeSocketData(fd,
  1680. ID_REGISTER,
  1681. OP_READ_DATA,
  1682. REG_CONNECTOR_ID,
  1683. 0,
  1684. NULL);
  1685. return ret;
  1686. }
  1687. static int WriteModelName(int fd)
  1688. {
  1689. int ret = PASS;
  1690. uint8_t Tmp = 0;
  1691. uint8_t TmpBuf[255] = {0};
  1692. memcpy(TmpBuf,
  1693. pSysConfig->ModelName,
  1694. strlen((char *)pSysConfig->ModelName));
  1695. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1696. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1697. ShmPrimaryMcuData->InputDet.bits.Key0);
  1698. TmpBuf[strlen((char *)pSysConfig->ModelName)] = Tmp; //Dispenser switch value
  1699. ret = composeSocketData(fd,
  1700. ID_REGISTER,
  1701. OP_WRITE_DATA,
  1702. REG_MODEL_NAME,
  1703. strlen((char *)pSysConfig->ModelName) + 1,
  1704. &TmpBuf[0]);
  1705. return ret;
  1706. }
  1707. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1708. {
  1709. sleep(3); //wait 1 second
  1710. if ((*pValue) >= maxCount) {
  1711. setTcpStatus(ABNORMAL);
  1712. } else {
  1713. (*pValue)++;
  1714. }
  1715. }
  1716. static int CheckNetworkStatus(void)
  1717. {
  1718. char *ipAddr = (char *)&pSysConfig->Eth0Interface.EthIpAddress;
  1719. //printf("Check network IP Header = %s\n", ipAddr);
  1720. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1721. return 1;
  1722. }
  1723. return 0;
  1724. }
  1725. static void updateFirmwareProcess(int fd, uint8_t gunID, uint8_t totalConnCount)
  1726. {
  1727. bool canUpdateFirmware = true;
  1728. uint8_t plugNum = 0;
  1729. uint8_t ackCount = 5;
  1730. struct timeb updateTime;
  1731. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1732. if (pSysWarning->Level != 2) {
  1733. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1734. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  1735. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  1736. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  1737. canUpdateFirmware = false;
  1738. }
  1739. }
  1740. }
  1741. if (canUpdateFirmware) {
  1742. ftime(&updateTime);
  1743. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1744. readSoftwareUpdate(fd, gunID);
  1745. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1746. }
  1747. if (pSysInfo->FirmwareUpdate == YES) {
  1748. while (ackCount != 0) {
  1749. ftime(&updateTime);
  1750. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1751. readSoftwareUpdate(fd, gunID);
  1752. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1753. if (pSysInfo->FirmwareUpdate == NO) {
  1754. ackCount--;
  1755. }
  1756. }
  1757. usleep(128);
  1758. }
  1759. }
  1760. }
  1761. }
  1762. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1763. {
  1764. int ret = 0;
  1765. uint8_t gunID = 0;
  1766. struct timeb AuthNowTime;
  1767. #if defined DD360Audi
  1768. gunID = gDoCommGblData.ConnectorID[pSysInfo->CurGunSelected];
  1769. //gunID = gDoCommGblData.ConnectorID[plugNum];
  1770. if (pSysConfig->AutoAuth_Disable) {
  1771. gunID = ID_REGISTER;
  1772. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0;
  1773. }
  1774. #else
  1775. gunID = gDoCommGblData.ConnectorID[pSysInfo->CurGunSelected];
  1776. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0; //非Audi 不需要等待主櫃回報餘額
  1777. #endif // DD360Audi
  1778. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) ||
  1779. (pSysInfo->AuthorizeFlag == YES)) {
  1780. ftime(&AuthNowTime);
  1781. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1782. ret = writeUserID(fd,
  1783. gunID,
  1784. pSysConfig->UserId);
  1785. if (ret >= 0) {
  1786. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1787. 0,
  1788. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1789. if (ret == 0) {
  1790. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1791. } else {
  1792. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1793. }
  1794. //printf("%d Balance = %.2f, %.2f\n",
  1795. // plugNum,
  1796. // ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  1797. // FAIL_BALANCE_PRICES);
  1798. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1799. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = YES; //isAuthorizedComplete
  1800. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
  1801. pSysInfo->AuthorizeFlag = NO;
  1802. ShmPsuData->SystemAvailablePower = NO;
  1803. ShmPsuData->SystemPresentPsuQuantity = NO;
  1804. }
  1805. }
  1806. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1807. }
  1808. }
  1809. }
  1810. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1811. {
  1812. int ret = DISPENSER_INIT_SUCC;
  1813. int isContinue = 1;
  1814. struct timeb NowTime;
  1815. while (isContinue) {
  1816. usleep(128);
  1817. ftime(&NowTime);
  1818. switch (curReg) {
  1819. case REG_MODEL_NAME:
  1820. if (WriteModelName(fd) == PASS) {
  1821. gDoCommGblData.DisConnCount = 0;
  1822. curReg = REG_CONNECTOR_ID;
  1823. } else {
  1824. sleep(1);
  1825. }
  1826. break;
  1827. case REG_CONNECTOR_ID:
  1828. if (readConnectorID(fd) == PASS) {
  1829. gDoCommGblData.DisConnCount = 0;
  1830. curReg = REG_REPORT_CSU_VERSION;
  1831. } else {
  1832. sleep(1);
  1833. }
  1834. break;
  1835. case REG_POWER_CABINET_STATUS:
  1836. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1837. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1838. readPowerCabinetStatus(fd, gunID);
  1839. ftime(&gRegTimeUp[plugNum][curReg]);
  1840. }
  1841. curReg = REG_DISPENSER_STATUS;
  1842. break;
  1843. case REG_DISPENSER_STATUS:
  1844. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1845. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1846. writeDispenserStatus(fd, gunID);
  1847. ftime(&gRegTimeUp[plugNum][curReg]);
  1848. }
  1849. curReg = REG_CHARGING_CAP;
  1850. break;
  1851. case REG_CHARGING_CAP:
  1852. if(gMoreInfoReq[plugNum].bits.FinalCostReq)
  1853. {
  1854. if (readChargingCapability(fd, gunID) == PASS) {
  1855. gMoreInfoReq[plugNum].bits.FinalCostReq = false;
  1856. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1857. ShmDcCommonData->finalcost[plugNum] = pDcChargingInfo->ChargingFee;
  1858. ShmDcCommonData->finalcost_flag[plugNum] = TRUE;
  1859. log_info("Gun %d get final cost %d", plugNum, pDcChargingInfo->ChargingFee);
  1860. }
  1861. }
  1862. curReg = REG_PLUG_IN_STATE;
  1863. break;
  1864. //case REG_CHARGING_CAP:
  1865. // readChargingCapability(fd, plugNum, gunID);
  1866. // break;
  1867. //case REG_CHARGING_TARGET:
  1868. // writeChargingTarget(fd, plugNum, gunID);
  1869. // break;
  1870. //case REG_SOFTWARE_UPDATE:
  1871. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1872. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > L OOP_RETRY_TIME||
  1873. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0 * 1) {
  1874. // readSoftwareUpdate(fd);
  1875. // ftime(&gRegTimeUp[plugNum][curReg]);
  1876. // }
  1877. // while (pSysInfo->FirmwareUpdate == YES) {
  1878. // ftime(&NowTime);
  1879. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowT ime) > LOOP_RETRY_TIME||
  1880. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1881. // readSoftwareUpdate(fd);
  1882. // }
  1883. // usleep(128);
  1884. // }
  1885. // }
  1886. // isContinue = 0;
  1887. // break;
  1888. case REG_PLUG_IN_STATE:
  1889. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1890. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1891. writePlugInStatus(fd, plugNum, gunID);
  1892. ftime(&gRegTimeUp[plugNum][curReg]);
  1893. }
  1894. curReg = REG_CONNECTOR_STATE;
  1895. break;
  1896. case REG_CONNECTOR_STATE:
  1897. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1898. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1899. writeConnectorState(fd, plugNum, gunID);
  1900. ftime(&gRegTimeUp[plugNum][curReg]);
  1901. }
  1902. curReg = REG_QRCODE_URL_INFO;
  1903. break;
  1904. //case REG_USER_ID:
  1905. // writeUserID(fd, gunID, uint8_t *pUserID);
  1906. // break;
  1907. //case REG_CHARGING_PERMISSION:
  1908. // readChargePermission(fd, gunID);
  1909. // break;
  1910. case REG_MISC_CONTROL:
  1911. readMiscCommand(fd, gunID);
  1912. if(gMoreInfoReq[plugNum].Value == 0)
  1913. {
  1914. isContinue = 0;
  1915. }
  1916. else
  1917. {
  1918. curReg = REG_RESERVATION_IDTAG;
  1919. }
  1920. break;
  1921. case REG_REPORT_CSU_VERSION:
  1922. case REG_REPORT_OTHER_VERSION:
  1923. if (gDoCommGblData.MiscCmd != 0) {
  1924. writeCsuModuleVersion(fd);
  1925. writeOtherModuleVersion(fd);
  1926. clearMiscCommand();
  1927. isContinue = 0;
  1928. } else {
  1929. //power up write dispenser version and get power cabinet system ID
  1930. if ((writeCsuModuleVersion(fd) == PASS) &&
  1931. (writeOtherModuleVersion(fd) == PASS) &&
  1932. (readQRcodeURLAndSystemDate(fd) == PASS)
  1933. ) {
  1934. //gDoCommGblData.DisConnCount = 0;
  1935. isContinue = 0;
  1936. }
  1937. }
  1938. break;
  1939. //case REG_PRESENT_CHARGING_INFO:
  1940. // break;
  1941. case REG_QRCODE_URL_INFO:
  1942. if (gunID == 1) {
  1943. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10) ||
  1944. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0
  1945. ) {
  1946. readQRcodeURLAndSystemDate(fd);
  1947. ftime(&gRegTimeUp[plugNum][curReg]);
  1948. }
  1949. }
  1950. //check misc command from power cabinet
  1951. if (gDoCommGblData.MiscCmd != 0) {
  1952. if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  1953. log_error("misc command failed = %x", gDoCommGblData.MiscCmd);
  1954. }
  1955. curReg = gDoCommGblData.MiscCmd;
  1956. } else if (gMoreInfoReq[plugNum].Value != 0) {
  1957. curReg = REG_RESERVATION_IDTAG;
  1958. } else {
  1959. isContinue = 0;
  1960. }
  1961. //curReg = REG_SOFTWARE_UPDATE;
  1962. break;
  1963. //case REG_WAIT_PLUG_IT_STATE:
  1964. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1965. // writeWaitPlugItState(fd, gunID);
  1966. // ftime(&gRegTimeUp[plugNum][curReg]);
  1967. // }
  1968. // isContinue = 0;
  1969. // break;
  1970. //case REG_CABINET_DCM_VERSION:
  1971. // break;
  1972. //case REG_CABINET_OTHER_VERSION:
  1973. // break;
  1974. //case REG_TOTAL_PSU_QUANTITY:
  1975. // break;
  1976. //case REG_PSU_VERSION:
  1977. // break;
  1978. case REG_RESERVATION_IDTAG:
  1979. if(gMoreInfoReq[plugNum].bits.ReservationReq)
  1980. {
  1981. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1982. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1983. if(readReservationState(fd, gunID) == PASS)
  1984. {
  1985. gMoreInfoReq[plugNum].bits.ReservationReq = ReservationState[plugNum] == NO ? NO : YES;
  1986. }
  1987. ftime(&gRegTimeUp[plugNum][curReg]);
  1988. }
  1989. }
  1990. curReg = REG_DISPENSER_REQUEST;
  1991. break;
  1992. //case REG_DISPENSER_REQUEST:
  1993. // break;
  1994. case REG_DISPENSER_REQUEST:
  1995. if (gConnectorActReq[plugNum].Value != 0)
  1996. {
  1997. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1998. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1999. if (writeDispenserRequest(fd, gunID, plugNum) == PASS)
  2000. {
  2001. gConnectorActReq[plugNum].Value = 0;
  2002. log_info("Gun %d CHARGING_CANCEL OK");
  2003. }
  2004. ftime(&gRegTimeUp[plugNum][curReg]);
  2005. }
  2006. }
  2007. curReg = REG_REMOTE_START_NO_ID;
  2008. break;
  2009. case REG_REMOTE_START_NO_ID:
  2010. if(gMoreInfoReq[plugNum].bits.RemoteStartNoID)
  2011. {
  2012. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2013. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2014. if(readRemoteStartNoIDState(fd) == PASS)
  2015. {
  2016. gMoreInfoReq[plugNum].bits.RemoteStartNoID = RemoteStartNoIDState == NO ? NO : YES;
  2017. }
  2018. ftime(&gRegTimeUp[plugNum][curReg]);
  2019. }
  2020. }
  2021. curReg = REG_REFUND_AMOUNT;
  2022. break;
  2023. case REG_REFUND_AMOUNT:
  2024. curReg = REG_PREPAYMENT_INFO;
  2025. break;
  2026. case REG_PREPAYMENT_INFO:
  2027. curReg = REG_PAYMENT_FAIL_REASON;
  2028. break;
  2029. case REG_PAYMENT_FAIL_REASON:
  2030. curReg = REG_CONNECTOR_QR_CODE;
  2031. break;
  2032. case REG_CONNECTOR_QR_CODE:
  2033. curReg = REG_STATION_INFO;
  2034. break;
  2035. case REG_STATION_INFO:
  2036. if(gMoreInfoReq[plugNum].bits.StationInfoReq)
  2037. {
  2038. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2039. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2040. if(readChargerStationInfo(fd) == PASS)
  2041. {
  2042. gMoreInfoReq[plugNum].bits.StationInfoReq = NO;
  2043. }
  2044. ftime(&gRegTimeUp[plugNum][curReg]);
  2045. }
  2046. }
  2047. isContinue = 0;
  2048. break;
  2049. default:
  2050. log_error("error curReg = %x", curReg);
  2051. gDoCommGblData.MiscCmd = 0;
  2052. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  2053. isContinue = 0;
  2054. break;
  2055. }
  2056. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  2057. return DISPENSER_SOCKET_RECONN;
  2058. }
  2059. }//for
  2060. return ret;
  2061. }
  2062. //static bool isDetectPlugin()
  2063. //{
  2064. // if (pSysInfo->WaitForPlugit == YES) {
  2065. // return YES;
  2066. // }
  2067. //
  2068. // return NO;
  2069. //}
  2070. static void systemStatusProcess(int fd, uint8_t totalGun, uint8_t plugNum, uint8_t gunID)
  2071. {
  2072. uint8_t i = 0;
  2073. struct timeb AuthNowTime = {0};
  2074. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  2075. switch (pDcChargingInfo->SystemStatus) {
  2076. case S_IDLE:
  2077. case S_RESERVATION:
  2078. case S_MAINTAIN:
  2079. case S_ALARM:
  2080. case S_AUTHORIZING:
  2081. if(pDcChargingInfo->SystemStatus != S_ALARM)
  2082. {
  2083. DeductResultReq[plugNum] = NO;
  2084. }
  2085. checkAuthorProcess(fd, plugNum);
  2086. //authorization complete, write wait plug it state
  2087. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance == FAIL_BALANCE_PRICES) {
  2088. break;
  2089. }
  2090. ftime(&AuthNowTime);
  2091. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  2092. DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) < 0
  2093. ) {
  2094. writeWaitPlugItState(fd, gunID);
  2095. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  2096. }
  2097. /*
  2098. ftime(&AuthNowTime);
  2099. if (DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  2100. DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) < 0
  2101. ) {
  2102. readMiscCommand(fd, gunID);
  2103. ftime(&gRegTimeUp[plugNum][REG_MISC_CONTROL]);
  2104. }
  2105. */
  2106. ftime(&AuthNowTime);
  2107. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2108. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2109. ) {
  2110. readChargingCapability(fd, gunID);
  2111. if (pDcChargingInfo->PantographFlag)
  2112. writeGroundFaultDetection(fd, 0, gunID);
  2113. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2114. }
  2115. break;
  2116. case S_PREPARNING: //get permission
  2117. ftime(&AuthNowTime);
  2118. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME ||
  2119. DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) < 0
  2120. ) {
  2121. writeConnectorState(fd, plugNum, gunID);
  2122. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  2123. }
  2124. ftime(&AuthNowTime);
  2125. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  2126. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  2127. ) {
  2128. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  2129. for (i = 0; i < totalGun; i++) {
  2130. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  2131. ShmPsuData->SystemAvailablePower += pDcChargingInfo->AvailableChargingPower;
  2132. }
  2133. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  2134. }
  2135. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  2136. }
  2137. ftime(&AuthNowTime);
  2138. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  2139. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  2140. ) {
  2141. writePresentChargingInfo(fd, plugNum, gunID);
  2142. if (pDcChargingInfo->PantographFlag)
  2143. writeGroundFaultDetection(fd, 0, gunID);
  2144. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  2145. }
  2146. break;
  2147. case S_PREPARING_FOR_EV://wait connector lock
  2148. ftime(&AuthNowTime);
  2149. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2150. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2151. ) {
  2152. readChargingCapability(fd, gunID);
  2153. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2154. }
  2155. ftime(&AuthNowTime);
  2156. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  2157. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  2158. ) {
  2159. if (readChargePermission(fd, gunID) == 0) {
  2160. log_info("S_PREPARING_FOR_EV Stop charging by power cabinet's permission = %d, %d",
  2161. plugNum,
  2162. REG_CHARGING_PERMISSION);
  2163. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  2164. }
  2165. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  2166. }
  2167. ftime(&AuthNowTime);
  2168. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  2169. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  2170. ) {
  2171. writePresentChargingInfo(fd, plugNum, gunID);
  2172. if (pDcChargingInfo->PantographFlag)
  2173. writeGroundFaultDetection(fd, 0, gunID);
  2174. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  2175. }
  2176. break;
  2177. case S_PREPARING_FOR_EVSE: //insaulation test
  2178. case S_CCS_PRECHARGE_ST0:
  2179. case S_CCS_PRECHARGE_ST1:
  2180. writeChargingTarget(fd, plugNum, gunID);
  2181. if (pDcChargingInfo->PantographFlag)
  2182. writeGroundFaultDetection(fd, 1, gunID);
  2183. ftime(&AuthNowTime);
  2184. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2185. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2186. ) {
  2187. readChargingCapability(fd, gunID);
  2188. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2189. }
  2190. ftime(&AuthNowTime);
  2191. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  2192. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  2193. ) {
  2194. if (readChargePermission(fd, gunID) == 0) {
  2195. log_info("S_PREPARING_FOR_EVSE Stop charging by power cabinet's permission = %d, %d",
  2196. plugNum,
  2197. REG_CHARGING_PERMISSION);
  2198. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  2199. }
  2200. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  2201. }
  2202. ftime(&AuthNowTime);
  2203. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  2204. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  2205. ) {
  2206. writePresentChargingInfo(fd, plugNum, gunID);
  2207. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  2208. }
  2209. break;
  2210. case S_CHARGING: //charging
  2211. case S_TERMINATING:
  2212. if(pDcChargingInfo->Type == _Type_GB || pDcChargingInfo->Type == _Type_Chademo)
  2213. {
  2214. if (pDcChargingInfo->PantographFlag)
  2215. writeGroundFaultDetection(fd, 0, gunID);
  2216. }
  2217. else
  2218. {
  2219. if (pDcChargingInfo->PantographFlag)
  2220. writeGroundFaultDetection(fd, 1, gunID);
  2221. }
  2222. ftime(&AuthNowTime);
  2223. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2224. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2225. ) {
  2226. readChargingCapability(fd, gunID);
  2227. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2228. }
  2229. ftime(&AuthNowTime);
  2230. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  2231. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  2232. ) {
  2233. if (readChargePermission(fd, gunID) == 0) {
  2234. log_info("Stop charging by power cabinet's permission = %d, %d",
  2235. plugNum,
  2236. REG_CHARGING_PERMISSION);
  2237. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  2238. //printf("%d StopChargeFlag = %d\n", plugNum, pDcChargingInfo->StopChargeFlag);
  2239. }
  2240. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  2241. }
  2242. writeChargingTarget(fd, plugNum, gunID);
  2243. ftime(&AuthNowTime);
  2244. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  2245. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  2246. ) {
  2247. writePresentChargingInfo(fd, plugNum, gunID);
  2248. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  2249. }
  2250. break;
  2251. case S_COMPLETE:
  2252. ftime(&AuthNowTime);
  2253. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2254. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2255. ) {
  2256. readChargingCapability(fd, gunID);
  2257. if (pDcChargingInfo->PantographFlag)
  2258. writeGroundFaultDetection(fd, 0, gunID);
  2259. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2260. }
  2261. break;
  2262. default:
  2263. break;
  2264. }
  2265. }
  2266. static int networkCreatePorcess(void)
  2267. {
  2268. int fd = 0;
  2269. uint8_t discount = 0;
  2270. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  2271. usleep(128);
  2272. }
  2273. setTcpStatus(ABNORMAL);
  2274. /**************** Check Network **********/
  2275. //log_info("Check Dispenser Network Information");
  2276. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  2277. usleep(125);
  2278. }
  2279. while (!CheckNetworkStatus()) {
  2280. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  2281. if( gDoCommGblData.DisConnCount != discount ) {
  2282. discount = gDoCommGblData.DisConnCount;
  2283. log_error("disconnect count = % d", gDoCommGblData.DisConnCount);
  2284. }
  2285. }
  2286. log_info("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  2287. pSysConfig->Eth0Interface.EthIpAddress,
  2288. pSysConfig->Eth0Interface.EthSubmaskAddress,
  2289. pSysConfig->Eth0Interface.EthGatewayAddress);
  2290. gDoCommGblData.DisConnCount = 0;
  2291. discount = 0;
  2292. /**************** Power cabinet connection **********/
  2293. log_info("Connect to Power Cabinet");
  2294. while ((fd = doCommConnToServer()) <= 0) {
  2295. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  2296. if( gDoCommGblData.DisConnCount != discount && fd == -1) {
  2297. discount = gDoCommGblData.DisConnCount;
  2298. log_error("disconnect count = %d, fd = %d", gDoCommGblData.DisConnCount, fd);
  2299. }
  2300. }
  2301. log_info("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  2302. fd,
  2303. pSysConfig->Eth0Interface.EthIpAddress,
  2304. pSysConfig->Eth0Interface.EthSubmaskAddress,
  2305. pSysConfig->Eth0Interface.EthGatewayAddress);
  2306. gDoCommGblData.DisConnCount = 0;
  2307. destroySelectGun(DESTROY_ALL_SEL);
  2308. sleep(3);
  2309. setTcpStatus(NORMAL);
  2310. return fd;
  2311. }
  2312. int main(int argc, char *argv[])
  2313. {
  2314. uint8_t plugNum = 0, gunID = 0;
  2315. uint8_t index = 0;
  2316. uint8_t totalConnCount = 0;
  2317. uint8_t initDone = DISPENER_INIT_FAIL;
  2318. char tmpbuf[256] = {0};
  2319. int fd = 0;
  2320. int isContinue = 1;
  2321. InitSocketSigPipe();
  2322. /**************** Initialization **********/
  2323. if (CreateAllCsuShareMemory() == FAIL) {
  2324. log_error("create share memory error");
  2325. return FAIL;
  2326. }
  2327. MappingGunChargingInfo("DoComm Task");
  2328. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  2329. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  2330. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  2331. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  2332. ShmPsuData = (struct PsuData *)GetShmPsuData();
  2333. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  2334. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  2335. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  2336. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  2337. totalConnCount = pSysConfig->TotalConnectorCount;
  2338. for (index = 0; index < totalConnCount; index++) {
  2339. clearPricesInfo(index);
  2340. }
  2341. //initial trigger message timer
  2342. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  2343. ftime(&gRegTimeUp[0][index]);
  2344. usleep(128);
  2345. ftime(&gRegTimeUp[1][index]);
  2346. usleep(50000);
  2347. }
  2348. setTcpStatus(NORMAL);
  2349. while (isContinue) {
  2350. if (initDone == DISPENER_INIT_FAIL) {
  2351. fd = networkCreatePorcess();
  2352. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  2353. } else {
  2354. plugNum = 0;
  2355. gunID = 0;
  2356. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  2357. //checkAuthorProcess(fd, plugNum);
  2358. //plugNum : setup chargingData value for bottom layer
  2359. //gunID : connector Id from power cabinet, 1 = left gun, 2 = right gun,
  2360. gunID = gDoCommGblData.ConnectorID[plugNum];
  2361. systemStatusProcess(fd, totalConnCount, plugNum, gunID);
  2362. initDone = messageTrigger(fd,
  2363. plugNum,
  2364. gunID,
  2365. REG_POWER_CABINET_STATUS);
  2366. if (initDone == DISPENSER_SOCKET_RECONN) {
  2367. break;
  2368. }
  2369. }
  2370. }
  2371. if (initDone != DISPENSER_SOCKET_RECONN) {
  2372. //update dispenser firmware
  2373. updateFirmwareProcess(fd, gDoCommGblData.ConnectorID[0], totalConnCount);
  2374. usleep(100000);
  2375. continue;
  2376. }
  2377. //Ethernet error recovery handle
  2378. log_info("Disconnected from power cabinet...re-connecting");
  2379. setTcpStatus(ABNORMAL);
  2380. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  2381. sprintf(tmpbuf,
  2382. "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  2383. pSysConfig->SystemId);
  2384. system(tmpbuf);
  2385. }
  2386. gDoCommGblData.DisConnCount = 0;
  2387. gDoCommGblData.SeqNum = 0;
  2388. closeSocket(fd);
  2389. fd = 0;
  2390. initDone = DISPENER_INIT_FAIL;
  2391. }
  2392. }