DoComm.c 113 KB

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