DoComm.c 93 KB

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