DoComm.c 94 KB

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