DoComm.c 100 KB

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