DoComm.c 107 KB

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