DoComm.c 107 KB

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