DoComm.c 107 KB

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