DoComm.c 91 KB

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