DoComm.c 92 KB

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