DoComm.c 102 KB

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