DoComm.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160
  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. strcpy(&ShmDcCommonData->pGunInfo[plugNum].ReservationID[0], (char*)&pCsuResult->Data.Data[1]);
  1222. } else {
  1223. strcpy(&ShmDcCommonData->pGunInfo[plugNum].ReservationID[0], "");
  1224. }
  1225. }
  1226. ReservationState[plugNum] = reservationState;
  1227. ShmDcCommonData->pGunInfo[plugNum].ReservationStatus = reservationState;
  1228. break;
  1229. case REG_REMOTE_START_NO_ID:
  1230. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1231. return COMMAND_RESULT_NG;
  1232. }
  1233. uint8_t remoteStartNoIdState = pCsuResult->Data.Data[0] == YES ? YES : NO;
  1234. if(remoteStartNoIdState != RemoteStartNoIDState)
  1235. {
  1236. log_info("RemoteStartNoID is %s", remoteStartNoIdState ? "trigger" : "expired");
  1237. }
  1238. RemoteStartNoIDState = remoteStartNoIdState;
  1239. break;
  1240. case REG_STATION_INFO:
  1241. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1242. return COMMAND_RESULT_NG;
  1243. }
  1244. StationVar _stationInfo;
  1245. memset((char *)&_stationInfo, 0x00, sizeof(StationVar));
  1246. _stationInfo.StationID = (pCsuResult->Data.Data[0] << 24) +
  1247. (pCsuResult->Data.Data[1] << 16) +
  1248. (pCsuResult->Data.Data[2] << 8) +
  1249. pCsuResult->Data.Data[3];
  1250. memcpy(_stationInfo.StationName, (char *)&pCsuResult->Data.Data[4], 64);
  1251. _stationInfo.WeatherID |= (pCsuResult->Data.Data[68] << 24);
  1252. _stationInfo.WeatherID |= (pCsuResult->Data.Data[69] << 16);
  1253. _stationInfo.WeatherID |= (pCsuResult->Data.Data[70] << 8);
  1254. _stationInfo.WeatherID |= pCsuResult->Data.Data[71];
  1255. uint8_t *pFloat = (uint8_t *)&_stationInfo.Temperature;
  1256. *(pFloat + 3) = pCsuResult->Data.Data[72];
  1257. *(pFloat + 2) = pCsuResult->Data.Data[73];
  1258. *(pFloat + 1) = pCsuResult->Data.Data[74];
  1259. *(pFloat) = pCsuResult->Data.Data[75];
  1260. ShmDcCommonData->WeatherID = _stationInfo.WeatherID;
  1261. ShmDcCommonData->Temperature = _stationInfo.Temperature;
  1262. ShmDcCommonData->Location = atoi(_stationInfo.StationName);
  1263. log_info("Station Name: %s, ID: %d, Weather: %d, Temperature: %.1f",
  1264. _stationInfo.StationName,
  1265. _stationInfo.StationID,
  1266. _stationInfo.WeatherID,
  1267. _stationInfo.Temperature);
  1268. break;
  1269. case REG_DEDUCT_INFO:
  1270. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1271. return COMMAND_RESULT_NG;
  1272. }
  1273. break;
  1274. case REG_READ_CABINET_SYSTEMID:
  1275. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1276. return COMMAND_RESULT_NG;
  1277. }
  1278. if (strcmp((char*)pSysConfig->SystemId, (char*)pCsuResult->Data.Data) != 0) {
  1279. strcpy((char*)pSysConfig->SystemId, (char*)pCsuResult->Data.Data);
  1280. log_info("System ID:%s", pSysConfig->SystemId);
  1281. }
  1282. break;
  1283. case REG_READ_DEFAULT_PRICE:
  1284. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1285. return COMMAND_RESULT_NG;
  1286. }
  1287. gMoreInfoReq[plugNum].bits.DefaultPriceReq = NO;
  1288. if (strcmp((char*)ShmDcCommonData->DefaultPriceString, (char*)pCsuResult->Data.Data) != EQUAL) {
  1289. strcpy((char*)ShmDcCommonData->DefaultPriceString, (char*)pCsuResult->Data.Data);
  1290. log_info("Default Price:%s", ShmDcCommonData->DefaultPriceString);
  1291. }
  1292. break;
  1293. case REG_READ_USER_PRICE:
  1294. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1295. return COMMAND_RESULT_NG;
  1296. }
  1297. gMoreInfoReq[plugNum].bits.UserPriceReq = NO;
  1298. if (strcmp((char*)ShmDcCommonData->pGunInfo[plugNum].UserPriceString, (char*)pCsuResult->Data.Data) != EQUAL) {
  1299. strcpy((char*)ShmDcCommonData->pGunInfo[plugNum].UserPriceString, (char*)pCsuResult->Data.Data);
  1300. log_info("User Price:%s", ShmDcCommonData->pGunInfo[plugNum].UserPriceString);
  1301. }
  1302. break;
  1303. case REG_RECEIPT_INFO:
  1304. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1305. return COMMAND_RESULT_NG;
  1306. }
  1307. gMoreInfoReq[plugNum].bits.ReceiptReq = NO;
  1308. if (strcmp((char*)ShmDcCommonData->pGunInfo[plugNum].ReceiptInfo, (char*)pCsuResult->Data.Data) != EQUAL) {
  1309. strcpy((char*)ShmDcCommonData->pGunInfo[plugNum].ReceiptInfo, (char*)pCsuResult->Data.Data);
  1310. log_info("Gun%d Receipt:%s", plugNum, ShmDcCommonData->pGunInfo[plugNum].ReceiptInfo);
  1311. }
  1312. break;
  1313. case REG_READ_CHARGING_TIMESTAMP:
  1314. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1315. return COMMAND_RESULT_NG;
  1316. }
  1317. if (rawDataLen <= 2) {
  1318. //log_info("No Charging timestamp");
  1319. break;
  1320. }
  1321. strcpy((char*)ShmDcCommonData->pGunInfo[plugNum].ChargeStartTime, (char*)&pCsuResult->Data.Data[0]);
  1322. int timelen = strlen((char*)ShmDcCommonData->pGunInfo[plugNum].ChargeStartTime);
  1323. if (timelen != 0) {
  1324. timelen = strcpy((char*)ShmDcCommonData->pGunInfo[plugNum].ChargeStopTime, (char*)&pCsuResult->Data.Data[timelen+1]);
  1325. }
  1326. break;
  1327. /*
  1328. case REG_POWER_CONSUMPTION_INFO:
  1329. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  1330. return COMMAND_RESULT_NG;
  1331. }
  1332. PowConsumValue* pValue =NULL;
  1333. pValue = (PowConsumValue*)&pCsuResult->Data.Data[0];
  1334. pValue->Gun1_Consumption = ntohl(pValue->Gun1_Consumption);
  1335. pValue->Gun2_Consumption = ntohl(pValue->Gun2_Consumption);
  1336. pValue->Gun3_Consumption = ntohl(pValue->Gun3_Consumption);
  1337. pValue->Gun4_Consumption = ntohl(pValue->Gun4_Consumption);
  1338. memcpy((char*)&ShmDcCommonData->pConsumption.Gun1_Consumption, (char*)&pValue->Gun1_Consumption, sizeof(uint32_t));
  1339. memcpy((char*)&ShmDcCommonData->pConsumption.Gun2_Consumption, (char*)&pValue->Gun2_Consumption, sizeof(uint32_t));
  1340. memcpy((char*)&ShmDcCommonData->pConsumption.Gun3_Consumption, (char*)&pValue->Gun3_Consumption, sizeof(uint32_t));
  1341. memcpy((char*)&ShmDcCommonData->pConsumption.Gun4_Consumption, (char*)&pValue->Gun4_Consumption, sizeof(uint32_t));
  1342. log_info("Gun1_Consumption:%f", ShmDcCommonData->pConsumption.Gun1_Consumption);
  1343. log_info("Gun2_Consumption:%f", ShmDcCommonData->pConsumption.Gun2_Consumption);
  1344. log_info("Gun3_Consumption:%f", ShmDcCommonData->pConsumption.Gun3_Consumption);
  1345. log_info("Gun4_Consumption:%f", ShmDcCommonData->pConsumption.Gun4_Consumption);
  1346. break;
  1347. */
  1348. default:
  1349. break;
  1350. }
  1351. return ret;
  1352. }
  1353. static int composeSocketData(int fd,
  1354. uint8_t id,
  1355. uint8_t opCode,
  1356. uint8_t reg,
  1357. uint8_t dataLen,
  1358. uint8_t *data)
  1359. {
  1360. int ret = PASS;
  1361. int size = 0;
  1362. int sendPktLen = 0;
  1363. uint8_t i = 0;
  1364. uint8_t plugNum = 0;
  1365. CsuCmdPkt csuCmdPkt = {0};
  1366. CsuResultPkt csuResult = {0};
  1367. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  1368. csuCmdPkt.Head.ID = id;
  1369. csuCmdPkt.Head.OP = opCode;
  1370. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  1371. csuCmdPkt.Data.Register = reg;
  1372. if ((data != NULL) && (dataLen > 0)) {
  1373. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  1374. }
  1375. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  1376. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  1377. // Not Let ask cabinet version trasmit into FAIL Network
  1378. //send command packet
  1379. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  1380. log_error("TCP socket send packet fail = reg %d size = %d",reg, size);
  1381. gDoCommGblData.DisConnCount++;
  1382. return FAIL;
  1383. }
  1384. //receive result head
  1385. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head),1)) < 0) {
  1386. log_error("TCP socket RX head fail = reg %d size = %d",reg, size);
  1387. // 若收不到以下封包,不產生錯誤
  1388. if (reg != REG_CABINET_DCM_VERSION && reg != REG_CABINET_OTHER_VERSION &&
  1389. reg != REG_TOTAL_PSU_QUANTITY && reg != REG_PSU_VERSION ) {
  1390. gDoCommGblData.DisConnCount++;
  1391. }
  1392. return FAIL;
  1393. }
  1394. //receive result raw data
  1395. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen,0)) < 0) {
  1396. log_error("TCP socket RX data fail = reg %d size %d",reg, size);
  1397. gDoCommGblData.DisConnCount++;
  1398. return FAIL;
  1399. }
  1400. for (i = 0; i < sizeof(gDoCommGblData.ConnectorID); i++) {
  1401. if (id == gDoCommGblData.ConnectorID[i]) {
  1402. plugNum = i;
  1403. break;
  1404. }
  1405. }
  1406. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, plugNum, csuCmdPkt.Data.Register);
  1407. gDoCommGblData.DisConnCount = 0; //送收資料沒有問題, 清除中斷網路計數
  1408. return ret;
  1409. }
  1410. static int writeDispenserRequest(int fd, uint8_t id, uint8_t gunIndex)
  1411. {
  1412. int ret = PASS;
  1413. int count = 0;
  1414. MiscCommand dispenserReq;
  1415. uint8_t data[6] = { 0 };
  1416. if (gConnectorActReq[gunIndex].bits.ChargingCancel)
  1417. {
  1418. dispenserReq.CMD = DISPENSER_REQ_CHARGING_CANCEL;
  1419. dispenserReq.Value[0] = 0;
  1420. dispenserReq.Value[1] = 0;
  1421. dispenserReq.Value[2] = 0;
  1422. dispenserReq.Value[3] = 1;
  1423. AddDispenserReq(data, &dispenserReq);
  1424. count++;
  1425. log_info("Write Gun %d CHARGING_CANCEL", gunIndex);
  1426. }
  1427. if ((ret = composeSocketData(fd,
  1428. id,
  1429. OP_WRITE_DATA,
  1430. REG_DISPENSER_REQUEST,
  1431. (count * 6),
  1432. &data[0])) == FAIL) {
  1433. return ret;
  1434. }
  1435. return ret;
  1436. }
  1437. static int readReservationState(int fd, uint8_t id)
  1438. {
  1439. int ret = PASS;
  1440. if ((ret = composeSocketData(fd,
  1441. id,
  1442. OP_READ_DATA,
  1443. REG_RESERVATION_IDTAG,
  1444. 0,
  1445. NULL)) == FAIL) {
  1446. return ret;
  1447. }
  1448. return ret;
  1449. }
  1450. static int readRemoteStartNoIDState(int fd)
  1451. {
  1452. int ret = PASS;
  1453. if ((ret = composeSocketData(fd,
  1454. ID_REGISTER,
  1455. OP_READ_DATA,
  1456. REG_REMOTE_START_NO_ID,
  1457. 0,
  1458. NULL)) == FAIL) {
  1459. return ret;
  1460. }
  1461. return ret;
  1462. }
  1463. static int readChargerStationInfo(int fd)
  1464. {
  1465. int ret = PASS;
  1466. if ((ret = composeSocketData(fd,
  1467. ID_REGISTER,
  1468. OP_READ_DATA,
  1469. REG_STATION_INFO,
  1470. 0,
  1471. NULL)) == FAIL) {
  1472. return ret;
  1473. }
  1474. return ret;
  1475. }
  1476. /*
  1477. static int writeDeductInfo(int fd, uint8_t id,uint8_t gunIndex, RecordTransactionInfo *transactionInfo)
  1478. {
  1479. int ret = PASS;
  1480. uint8_t dataBuf[104] = {0};
  1481. //int i;
  1482. memset((char *)dataBuf, 0x00, sizeof(dataBuf));
  1483. dataBuf[0] = transactionInfo->DeductResult;
  1484. dataBuf[1] = transactionInfo->IsDonateInvoice;
  1485. dataBuf[2] = (transactionInfo->TransactionId >> 24) & 0xFF;
  1486. dataBuf[3] = (transactionInfo->TransactionId >> 16) & 0xFF;
  1487. dataBuf[4] = (transactionInfo->TransactionId >> 8) & 0xFF;
  1488. dataBuf[5] = (transactionInfo->TransactionId & 0xFF);
  1489. int amount = (int)(transactionInfo->Amount * 100);
  1490. dataBuf[6] = (amount >> 24) & 0xFF;
  1491. dataBuf[7] = (amount >> 16) & 0xFF;
  1492. dataBuf[8] = (amount >> 8) & 0xFF;
  1493. dataBuf[9] = (amount & 0xFF);
  1494. memcpy((char *)&dataBuf[10], transactionInfo->pCreditCard.ApprovalNo,9);
  1495. memcpy((char *)&dataBuf[19], transactionInfo->pCreditCard.VemData, 64);
  1496. memcpy((char*)&dataBuf[83], transactionInfo->pCreditCard.CardNo, 20);
  1497. log_info("Gun[%d] TransactionId:%d DeductResult:%d IsDonateInvoice:%d Amount:%f Approva Num:'%s' VemData:'%s' CardNo:'%s'",
  1498. transactionInfo->ConnectorID,
  1499. transactionInfo->TransactionId,
  1500. transactionInfo->DeductResult,
  1501. transactionInfo->IsDonateInvoice,
  1502. transactionInfo->Amount,
  1503. transactionInfo->pCreditCard.ApprovalNo,
  1504. transactionInfo->pCreditCard.VemData,
  1505. transactionInfo->pCreditCard.CardNo);
  1506. // copy deduct result to dataBuf here
  1507. if ((ret = composeSocketData(fd,
  1508. id,
  1509. OP_WRITE_DATA,
  1510. REG_DEDUCT_INFO,
  1511. 104,
  1512. &dataBuf[0])) == FAIL) {
  1513. return ret;
  1514. }
  1515. return ret;
  1516. }
  1517. */
  1518. static int readChargingTimeStamp(int fd, uint8_t id)
  1519. {
  1520. int ret = PASS;
  1521. if ((ret = composeSocketData(fd,
  1522. id,
  1523. OP_READ_DATA,
  1524. REG_READ_CHARGING_TIMESTAMP,
  1525. 0,
  1526. NULL)) == FAIL) {
  1527. return ret;
  1528. }
  1529. return ret;
  1530. }
  1531. static int writeWaitPlugItState(int fd, uint8_t id)
  1532. {
  1533. int ret = PASS;
  1534. uint8_t data[2] = {pSysInfo->WaitForPlugit, 0};
  1535. if (_isplugin != pSysInfo->WaitForPlugit) {
  1536. log_info("Wait for plug:%s", pSysInfo->WaitForPlugit == 0 ? "disable" : "enable");
  1537. _isplugin = pSysInfo->WaitForPlugit;
  1538. }
  1539. //printf("WaitForPlugit = %d", pSysInfo->WaitForPlugit);
  1540. if ((ret = composeSocketData(fd,
  1541. id,
  1542. OP_WRITE_DATA,
  1543. REG_WAIT_PLUG_IT_STATE,
  1544. 1,
  1545. &data[0])) == FAIL) {
  1546. return ret;
  1547. }
  1548. return ret;
  1549. }
  1550. static int readQRcodeURLAndSystemDate(int fd)
  1551. {
  1552. int ret = PASS;
  1553. ret = composeSocketData(fd,
  1554. ID_REGISTER,
  1555. OP_READ_DATA,
  1556. REG_QRCODE_URL_INFO,
  1557. 0,
  1558. NULL);
  1559. return ret;
  1560. }
  1561. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1562. {
  1563. int ret = PASS;
  1564. uint8_t dataBuf[16] = {0};
  1565. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1566. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1567. if(pDcChargingInfo->PantographFlag == NO)
  1568. {
  1569. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(pDcChargingInfo->PresentChargingVoltage * 10));
  1570. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(pDcChargingInfo->PresentChargingCurrent * 10));
  1571. }
  1572. else
  1573. {
  1574. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Voltage));
  1575. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Current));
  1576. }
  1577. pPreChargingInfo->RemainChargingDuration = htonl(pDcChargingInfo->RemainChargingDuration);
  1578. pPreChargingInfo->EvBatterySoc = pDcChargingInfo->EvBatterySoc;
  1579. ret = composeSocketData(fd,
  1580. id,
  1581. OP_WRITE_DATA,
  1582. REG_PRESENT_CHARGING_INFO,
  1583. sizeof(PreChargingInfo),
  1584. (uint8_t *)pPreChargingInfo);
  1585. return ret;
  1586. }
  1587. static int writeOtherModuleVersion(int fd)
  1588. {
  1589. int ret = PASS;
  1590. uint8_t data[255] = {0};
  1591. uint8_t dataLen = 0;
  1592. //report other module version message
  1593. memcpy(&data[dataLen], pSysInfo->FanModuleFwRev, VERSION_BUF_SIZE);
  1594. dataLen += VERSION_BUF_SIZE;
  1595. memcpy(&data[dataLen], pSysInfo->RelayModuleFwRev, VERSION_BUF_SIZE);
  1596. dataLen += VERSION_BUF_SIZE;
  1597. memcpy(&data[dataLen], pSysInfo->Connector1FwRev, VERSION_BUF_SIZE);
  1598. dataLen += VERSION_BUF_SIZE;
  1599. memcpy(&data[dataLen], pSysInfo->Connector2FwRev, VERSION_BUF_SIZE);
  1600. dataLen += VERSION_BUF_SIZE;
  1601. memcpy(&data[dataLen], pSysInfo->LedModuleFwRev, VERSION_BUF_SIZE);
  1602. dataLen += VERSION_BUF_SIZE;
  1603. ret = composeSocketData(fd,
  1604. ID_REGISTER,
  1605. OP_WRITE_DATA,
  1606. REG_REPORT_OTHER_VERSION,
  1607. dataLen,
  1608. &data[0]);
  1609. return ret;
  1610. }
  1611. static int writeCsuModuleVersion(int fd)
  1612. {
  1613. int ret = PASS;
  1614. uint8_t data[255] = {0};
  1615. uint8_t dataLen = 0;
  1616. //report CSU platform version message
  1617. memcpy(&data[dataLen], pSysInfo->CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1618. dataLen += VERSION_BUF_SIZE;
  1619. memcpy(&data[dataLen], pSysInfo->CsuKernelFwRev, VERSION_BUF_SIZE);
  1620. dataLen += VERSION_BUF_SIZE;
  1621. memcpy(&data[dataLen], pSysInfo->CsuRootFsFwRev, VERSION_BUF_SIZE);
  1622. dataLen += VERSION_BUF_SIZE;
  1623. memcpy(&data[dataLen], pSysInfo->CsuPrimFwRev, VERSION_BUF_SIZE);
  1624. dataLen += VERSION_BUF_SIZE;
  1625. ret = composeSocketData(fd,
  1626. ID_REGISTER,
  1627. OP_WRITE_DATA,
  1628. REG_REPORT_CSU_VERSION,
  1629. dataLen,
  1630. &data[0]);
  1631. return ret;
  1632. }
  1633. static int readMiscCommand(int fd, uint8_t id)
  1634. {
  1635. int ret = PASS;
  1636. ret = composeSocketData(fd,
  1637. id,
  1638. OP_READ_DATA,
  1639. REG_MISC_CONTROL,
  1640. 0,
  1641. NULL);
  1642. return ret;
  1643. }
  1644. static int readChargePermission(int fd, uint8_t id)
  1645. {
  1646. return composeSocketData(fd,
  1647. id,
  1648. OP_READ_DATA,
  1649. REG_CHARGING_PERMISSION,
  1650. 0,
  1651. NULL);
  1652. }
  1653. static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id)
  1654. {
  1655. int ret = PASS;
  1656. uint8_t dataBuf[1] = {status};
  1657. ret = composeSocketData(fd,
  1658. id,
  1659. OP_WRITE_DATA,
  1660. REG_Ground_Fault_Detection,
  1661. 1,
  1662. &dataBuf[0]);
  1663. return ret;
  1664. }
  1665. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1666. {
  1667. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1668. return FAIL;
  1669. }
  1670. uint8_t data[50];
  1671. int datalen = strlen((char*)pUserID);
  1672. memcpy((uint8_t*)data, pUserID, datalen);
  1673. data[datalen] = '\0';
  1674. data[datalen+1] = ShmDcCommonData->AuthroizeType;
  1675. datalen += 2;
  1676. return composeSocketData(fd,
  1677. id,
  1678. OP_WRITE_DATA,
  1679. REG_USER_ID,
  1680. datalen,
  1681. &data[0]);
  1682. }
  1683. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1684. {
  1685. uint8_t dataBuf[15] = {'\0'};
  1686. uint32_t _consumption;
  1687. ConnectorState *pConnState = (ConnectorState *)dataBuf;
  1688. static char vendorErrorCodeTmp[2][WARNING_CODE_SIZE] = {0};
  1689. int ret = PASS;
  1690. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1691. pConnState->ConnectorTemp = pDcChargingInfo->ConnectorTemp;
  1692. pConnState->ChillerTemp = pDcChargingInfo->ChillerTemp;
  1693. pConnState->PlugIn = pDcChargingInfo->ConnectorPlugIn;
  1694. if (pDcChargingInfo->SystemStatus <= S_AUTHORIZING) {
  1695. pConnState->State = CONN_ST_IDLE; //idle
  1696. strncpy((char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1697. "",
  1698. sizeof(ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode));
  1699. strncpy(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE);
  1700. } else if ((pDcChargingInfo->SystemStatus <= S_PREPARING_FOR_EVSE) ||
  1701. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST0) ||
  1702. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST1)) {
  1703. pConnState->State = CONN_ST_PREPARING; //preparing
  1704. } else if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  1705. pConnState->State = CONN_ST_CHARGING; //charging
  1706. } else if (pDcChargingInfo->SystemStatus == S_TERMINATING ||
  1707. pDcChargingInfo->SystemStatus == S_COMPLETE) {
  1708. pConnState->State = CONN_ST_TERMINATING; //terminating
  1709. } else if ((pDcChargingInfo->SystemStatus == S_ALARM) ||
  1710. (pDcChargingInfo->SystemStatus == S_FAULT)) {
  1711. pConnState->State = CONN_ST_ALARM;
  1712. strncpy((char *)pConnState->WarningCode,
  1713. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1714. WARNING_CODE_SIZE);
  1715. if (strncmp(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE) == 0) {
  1716. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1717. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1718. WARNING_CODE_SIZE);
  1719. vendorErrorCodeTmp[plugNum][6] = '\0';
  1720. /*
  1721. log_info("1 ID = %d, VendorErrorCode = %s",
  1722. plugNum,
  1723. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1724. _VenderErrorCodeFlag = true;
  1725. */
  1726. } else {
  1727. if (strncmp(&vendorErrorCodeTmp[plugNum][0],
  1728. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1729. WARNING_CODE_SIZE) != 0
  1730. ) {
  1731. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1732. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1733. WARNING_CODE_SIZE);
  1734. vendorErrorCodeTmp[plugNum][6] = '\0';
  1735. log_info("2 ID = %d, VendorErrorCode = %s",
  1736. plugNum,
  1737. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1738. }
  1739. }
  1740. }
  1741. memcpy((char*)&_consumption, (char*)&ShmDcCommonData->pGunInfo[plugNum].PowerConsumption, sizeof(float));
  1742. pConnState->consumption = ntohl(_consumption);
  1743. ret = composeSocketData(fd,
  1744. id,
  1745. OP_WRITE_DATA,
  1746. REG_CONNECTOR_STATE,
  1747. sizeof(dataBuf) - 1,
  1748. &dataBuf[0]);
  1749. return ret;
  1750. }
  1751. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1752. {
  1753. int ret = PASS;
  1754. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1755. uint8_t dataBuf[2] = {pDcChargingInfo->ConnectorPlugIn, 0};
  1756. ret = composeSocketData(fd,
  1757. id,
  1758. OP_WRITE_DATA,
  1759. REG_PLUG_IN_STATE,
  1760. 1,
  1761. &dataBuf[0]);
  1762. return ret;
  1763. }
  1764. static int readSoftwareUpdate(int fd, uint8_t gunID)
  1765. {
  1766. int ret = PASS;
  1767. uint8_t dataBuf[2] = {pSysInfo->FirmwareUpdate, 0};
  1768. ret = composeSocketData(fd,
  1769. gunID,
  1770. OP_READ_DATA,
  1771. REG_SOFTWARE_UPDATE,
  1772. 1,
  1773. &dataBuf[0]);
  1774. return ret;
  1775. }
  1776. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1777. {
  1778. int ret = PASS;
  1779. float ChargingVolt, ChargingAmp;
  1780. uint8_t dataBuf[4] = {0};
  1781. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1782. ChargingVolt = pDcChargingInfo->EvBatterytargetVoltage;
  1783. ChargingAmp = pDcChargingInfo->EvBatterytargetCurrent;
  1784. ChargingVolt *= 10;
  1785. ChargingAmp *= 10;
  1786. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1787. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1788. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1789. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1790. ret = composeSocketData(fd,
  1791. id,
  1792. OP_WRITE_DATA,
  1793. REG_CHARGING_TARGET,
  1794. sizeof(dataBuf),
  1795. &dataBuf[0]);
  1796. return ret;
  1797. }
  1798. static int readChargingCapability(int fd, uint8_t id)
  1799. {
  1800. int ret = PASS;
  1801. ret = composeSocketData(fd,
  1802. id,
  1803. OP_READ_DATA,
  1804. REG_CHARGING_CAP,
  1805. 0,
  1806. NULL);
  1807. return ret;
  1808. }
  1809. static int readCabinetDCMVersion(int fd, uint8_t id)
  1810. {
  1811. int ret = PASS;
  1812. ret = composeSocketData(fd,
  1813. id,
  1814. OP_READ_DATA,
  1815. REG_CABINET_DCM_VERSION,
  1816. 0,
  1817. NULL);
  1818. return ret;
  1819. }
  1820. static int readCabinetOtherVersion(int fd, uint8_t id)
  1821. {
  1822. int ret = PASS;
  1823. ret = composeSocketData(fd,
  1824. id,
  1825. OP_READ_DATA,
  1826. REG_CABINET_OTHER_VERSION,
  1827. 0,
  1828. NULL);
  1829. return ret;
  1830. }
  1831. static int readCabinetPSUNumber(int fd, uint8_t id)
  1832. {
  1833. int ret = PASS;
  1834. ret = composeSocketData(fd,
  1835. id,
  1836. OP_READ_DATA,
  1837. REG_TOTAL_PSU_QUANTITY,
  1838. 0,
  1839. NULL);
  1840. return ret;
  1841. }
  1842. static int readCabinetEachPSUVersion(int fd, uint8_t id,uint8_t n_psu)
  1843. {
  1844. int ret = PASS;
  1845. uint8_t data_buf[0] = {0};
  1846. data_buf[0] = n_psu;
  1847. ret = composeSocketData(fd,
  1848. id,
  1849. OP_READ_DATA,
  1850. REG_PSU_VERSION,
  1851. sizeof(data_buf),
  1852. &data_buf[0]);
  1853. return ret;
  1854. }
  1855. static int readReceiptInfo(int fd, uint8_t id)
  1856. {
  1857. int ret = PASS;
  1858. ret = composeSocketData(fd,
  1859. id,
  1860. OP_READ_DATA,
  1861. REG_RECEIPT_INFO,
  1862. 0,
  1863. NULL);
  1864. return ret;
  1865. }
  1866. static int writeDispenserStatus(int fd, uint8_t gunID)
  1867. {
  1868. uint8_t warningCount = 0;
  1869. uint8_t count = 0;
  1870. uint8_t CurWarnCodeTmp[10][6] = {0};
  1871. uint8_t PreWarnCodeTmp[10][6] = {0};
  1872. int ret = PASS;
  1873. if ((pSysWarning->WarningCount <= 0) &&
  1874. (gPreSysWarningInfo.WarningCount <= 0)) {
  1875. return FAIL;
  1876. }
  1877. warningCount = pSysWarning->WarningCount;
  1878. for (count = 0; count < warningCount; count++) {
  1879. memcpy(CurWarnCodeTmp[count], pSysWarning->WarningCode[count], WARNING_CODE_SIZE);
  1880. }
  1881. warningCount = gPreSysWarningInfo.WarningCount;
  1882. for (count = 0; count < warningCount; count++) {
  1883. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1884. }
  1885. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1886. return FAIL;
  1887. }
  1888. ret = composeSocketData(fd,
  1889. gunID,
  1890. OP_WRITE_DATA,
  1891. REG_DISPENSER_STATUS,
  1892. strlen((char *)CurWarnCodeTmp),
  1893. &CurWarnCodeTmp[0][0]);
  1894. return ret;
  1895. }
  1896. static int readPowerCabinetStatus(int fd, uint8_t gunID)
  1897. {
  1898. int ret = PASS;
  1899. ret = composeSocketData(fd,
  1900. gunID,
  1901. OP_READ_DATA,
  1902. REG_POWER_CABINET_STATUS,
  1903. 0,
  1904. NULL);
  1905. return ret;
  1906. }
  1907. static int readConnectorID(int fd)
  1908. {
  1909. int ret = PASS;
  1910. ret = composeSocketData(fd,
  1911. ID_REGISTER,
  1912. OP_READ_DATA,
  1913. REG_CONNECTOR_ID,
  1914. 0,
  1915. NULL);
  1916. return ret;
  1917. }
  1918. static int readSystemID(int fd)
  1919. {
  1920. int ret = PASS;
  1921. ret = composeSocketData(fd,
  1922. ID_REGISTER,
  1923. OP_READ_DATA,
  1924. REG_READ_CABINET_SYSTEMID,
  1925. 0,
  1926. NULL);
  1927. return ret;
  1928. }
  1929. static int readDefaultPrice(int fd)
  1930. {
  1931. int ret = PASS;
  1932. ret = composeSocketData(fd,
  1933. ID_REGISTER,
  1934. OP_READ_DATA,
  1935. REG_READ_DEFAULT_PRICE,
  1936. 0,
  1937. NULL);
  1938. return ret;
  1939. }
  1940. static int readUserPrice(int fd, uint8_t gunID)
  1941. {
  1942. int ret = PASS;
  1943. ret = composeSocketData(fd,
  1944. gunID,
  1945. OP_READ_DATA,
  1946. REG_READ_USER_PRICE,
  1947. 0,
  1948. NULL);
  1949. return ret;
  1950. }
  1951. /*
  1952. static int readConsumptionInfo(int fd)
  1953. {
  1954. int ret = PASS;
  1955. ret = composeSocketData(fd,
  1956. ID_REGISTER,
  1957. OP_READ_DATA,
  1958. REG_POWER_CONSUMPTION_INFO,
  1959. 0,
  1960. NULL);
  1961. return ret;
  1962. }
  1963. */
  1964. static int WriteModelName(int fd)
  1965. {
  1966. int ret = PASS;
  1967. uint8_t Tmp = 0;
  1968. uint8_t TmpBuf[255] = {0};
  1969. memcpy(TmpBuf,
  1970. pSysConfig->ModelName,
  1971. strlen((char *)pSysConfig->ModelName));
  1972. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1973. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1974. ShmPrimaryMcuData->InputDet.bits.Key0);
  1975. TmpBuf[strlen((char *)pSysConfig->ModelName)] = Tmp; //Dispenser switch value
  1976. ret = composeSocketData(fd,
  1977. ID_REGISTER,
  1978. OP_WRITE_DATA,
  1979. REG_MODEL_NAME,
  1980. strlen((char *)pSysConfig->ModelName) + 1,
  1981. &TmpBuf[0]);
  1982. return ret;
  1983. }
  1984. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1985. {
  1986. sleep(3); //wait 1 second
  1987. if ((*pValue) >= maxCount) {
  1988. setTcpStatus(ABNORMAL);
  1989. } else {
  1990. (*pValue)++;
  1991. }
  1992. }
  1993. static int CheckNetworkStatus(void)
  1994. {
  1995. char *ipAddr = (char *)&pSysConfig->Eth0Interface.EthIpAddress;
  1996. //printf("Check network IP Header = %s\n", ipAddr);
  1997. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1998. return 1;
  1999. }
  2000. return 0;
  2001. }
  2002. static void updateFirmwareProcess(int fd, uint8_t gunID, uint8_t totalConnCount)
  2003. {
  2004. bool canUpdateFirmware = true;
  2005. uint8_t plugNum = 0;
  2006. uint8_t ackCount = 5;
  2007. struct timeb updateTime;
  2008. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  2009. if (pSysWarning->Level != 2) {
  2010. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  2011. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2012. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  2013. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  2014. canUpdateFirmware = false;
  2015. }
  2016. }
  2017. }
  2018. if (canUpdateFirmware) {
  2019. ftime(&updateTime);
  2020. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  2021. readSoftwareUpdate(fd, gunID);
  2022. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  2023. }
  2024. if (pSysInfo->FirmwareUpdate == YES) {
  2025. while (ackCount != 0) {
  2026. ftime(&updateTime);
  2027. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  2028. readSoftwareUpdate(fd, gunID);
  2029. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  2030. if (pSysInfo->FirmwareUpdate == NO) {
  2031. ackCount--;
  2032. }
  2033. }
  2034. usleep(128);
  2035. }
  2036. }
  2037. }
  2038. }
  2039. static void checkAuthorProcess(int fd, uint8_t plugNum)
  2040. {
  2041. int ret = 0;
  2042. uint8_t gunID = 0;
  2043. struct timeb AuthNowTime;
  2044. #if defined DD360Audi
  2045. gunID = gDoCommGblData.ConnectorID[pSysInfo->CurGunSelected];
  2046. //gunID = gDoCommGblData.ConnectorID[plugNum];
  2047. if (pSysConfig->AutoAuth_Disable) {
  2048. gunID = ID_REGISTER;
  2049. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0;
  2050. }
  2051. #else
  2052. gunID = gDoCommGblData.ConnectorID[pSysInfo->CurGunSelected];
  2053. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0; //非Audi 不需要等待主櫃回報餘額
  2054. #endif // DD360Audi
  2055. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) ||
  2056. (pSysInfo->AuthorizeFlag == YES)) {
  2057. ftime(&AuthNowTime);
  2058. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  2059. ret = writeUserID(fd,
  2060. gunID,
  2061. pSysConfig->UserId);
  2062. if (ret >= 0) {
  2063. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  2064. 0,
  2065. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  2066. if (ret == 0) {
  2067. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  2068. } else {
  2069. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  2070. }
  2071. //printf("%d Balance = %.2f, %.2f\n",
  2072. // plugNum,
  2073. // ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  2074. // FAIL_BALANCE_PRICES);
  2075. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  2076. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = YES; //isAuthorizedComplete
  2077. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
  2078. pSysInfo->AuthorizeFlag = NO;
  2079. ShmPsuData->SystemAvailablePower = NO;
  2080. ShmPsuData->SystemPresentPsuQuantity = NO;
  2081. }
  2082. }
  2083. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  2084. }
  2085. }
  2086. }
  2087. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  2088. {
  2089. int ret = DISPENSER_INIT_SUCC;
  2090. int isContinue = 1;
  2091. struct timeb NowTime;
  2092. while (isContinue) {
  2093. usleep(128);
  2094. ftime(&NowTime);
  2095. switch (curReg) {
  2096. case REG_MODEL_NAME:
  2097. if (WriteModelName(fd) == PASS) {
  2098. gDoCommGblData.DisConnCount = 0;
  2099. curReg = REG_CONNECTOR_ID;
  2100. } else {
  2101. sleep(1);
  2102. }
  2103. break;
  2104. case REG_CONNECTOR_ID:
  2105. if (readConnectorID(fd) == PASS) {
  2106. gDoCommGblData.DisConnCount = 0;
  2107. curReg = REG_REPORT_CSU_VERSION;
  2108. } else {
  2109. sleep(1);
  2110. }
  2111. break;
  2112. case REG_POWER_CABINET_STATUS:
  2113. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2114. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2115. readPowerCabinetStatus(fd, gunID);
  2116. ftime(&gRegTimeUp[plugNum][curReg]);
  2117. }
  2118. curReg = REG_DISPENSER_STATUS;
  2119. break;
  2120. case REG_DISPENSER_STATUS:
  2121. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2122. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2123. writeDispenserStatus(fd, gunID);
  2124. ftime(&gRegTimeUp[plugNum][curReg]);
  2125. }
  2126. curReg = REG_CHARGING_CAP;
  2127. break;
  2128. case REG_CHARGING_CAP:
  2129. if(gMoreInfoReq[plugNum].bits.FinalCostReq)
  2130. {
  2131. if (readChargingCapability(fd, gunID) == PASS) {
  2132. gMoreInfoReq[plugNum].bits.FinalCostReq = false;
  2133. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  2134. //ShmDcCommonData->TransactionInfo[plugNum].Amount = pDcChargingInfo->ChargingFee;
  2135. ShmDcCommonData->pGunInfo[plugNum].finalcost_flag = TRUE;
  2136. log_info("Set Final Cost Flag");
  2137. }
  2138. }
  2139. curReg = REG_PLUG_IN_STATE;
  2140. break;
  2141. //case REG_CHARGING_CAP:
  2142. // readChargingCapability(fd, plugNum, gunID);
  2143. // break;
  2144. //case REG_CHARGING_TARGET:
  2145. // writeChargingTarget(fd, plugNum, gunID);
  2146. // break;
  2147. //case REG_SOFTWARE_UPDATE:
  2148. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  2149. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > L OOP_RETRY_TIME||
  2150. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0 * 1) {
  2151. // readSoftwareUpdate(fd);
  2152. // ftime(&gRegTimeUp[plugNum][curReg]);
  2153. // }
  2154. // while (pSysInfo->FirmwareUpdate == YES) {
  2155. // ftime(&NowTime);
  2156. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowT ime) > LOOP_RETRY_TIME||
  2157. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2158. // readSoftwareUpdate(fd);
  2159. // }
  2160. // usleep(128);
  2161. // }
  2162. // }
  2163. // isContinue = 0;
  2164. // break;
  2165. case REG_PLUG_IN_STATE:
  2166. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2167. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2168. writePlugInStatus(fd, plugNum, gunID);
  2169. ftime(&gRegTimeUp[plugNum][curReg]);
  2170. }
  2171. curReg = REG_CONNECTOR_STATE;
  2172. break;
  2173. case REG_CONNECTOR_STATE:
  2174. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2175. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2176. writeConnectorState(fd, plugNum, gunID);
  2177. ftime(&gRegTimeUp[plugNum][curReg]);
  2178. }
  2179. curReg = REG_DISPENSER_REQUEST;
  2180. break;
  2181. case REG_DISPENSER_REQUEST:
  2182. /*
  2183. gConnectorActReq[plugNum].bits.ChargingCancel = ShmDcCommonData->OperateIDLE[plugNum];
  2184. if (gConnectorActReq[plugNum].Value != 0)
  2185. {
  2186. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2187. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2188. if (writeDispenserRequest(fd, gunID, plugNum) == PASS)
  2189. {
  2190. gConnectorActReq[plugNum].Value = 0;
  2191. log_info("Gun %d CHARGING_CANCEL OK", plugNum);
  2192. ShmDcCommonData->OperateIDLE[plugNum] = 0;
  2193. }
  2194. ftime(&gRegTimeUp[plugNum][curReg]);
  2195. }
  2196. }
  2197. */
  2198. curReg = REG_QRCODE_URL_INFO;
  2199. break;
  2200. case REG_QRCODE_URL_INFO:
  2201. if (gunID == 1) {
  2202. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10) ||
  2203. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0 ) {
  2204. readQRcodeURLAndSystemDate(fd);
  2205. ftime(&gRegTimeUp[plugNum][curReg]);
  2206. }
  2207. }
  2208. curReg = REG_READ_CABINET_SYSTEMID;
  2209. break;
  2210. //case REG_USER_ID:
  2211. // writeUserID(fd, gunID, uint8_t *pUserID);
  2212. // break;
  2213. //case REG_CHARGING_PERMISSION:
  2214. // readChargePermission(fd, gunID);
  2215. // break;
  2216. case REG_READ_CABINET_SYSTEMID:
  2217. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10) ||
  2218. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2219. readSystemID(fd);
  2220. ftime(&gRegTimeUp[plugNum][curReg]);
  2221. }
  2222. curReg = REG_MISC_CONTROL;
  2223. //check misc command from power cabinet
  2224. /*
  2225. if (gDoCommGblData.MiscCmd != 0) {
  2226. if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  2227. log_error("misc command failed = %x", gDoCommGblData.MiscCmd);
  2228. }
  2229. curReg = gDoCommGblData.MiscCmd;
  2230. } else if (gMoreInfoReq[plugNum].Value != 0) {
  2231. curReg = REG_RESERVATION_IDTAG;
  2232. } else {
  2233. isContinue = 0;
  2234. }*/
  2235. break;
  2236. case REG_MISC_CONTROL:
  2237. readMiscCommand(fd, gunID);
  2238. if(gMoreInfoReq[plugNum].Value == 0) {
  2239. isContinue = 0;
  2240. } else {
  2241. curReg = REG_RESERVATION_IDTAG;
  2242. }
  2243. break;
  2244. case REG_REPORT_CSU_VERSION:
  2245. case REG_REPORT_OTHER_VERSION:
  2246. if (gDoCommGblData.MiscCmd != 0) {
  2247. writeCsuModuleVersion(fd);
  2248. writeOtherModuleVersion(fd);
  2249. clearMiscCommand();
  2250. isContinue = 0;
  2251. } else {
  2252. //power up write dispenser version and get power cabinet system ID
  2253. if ((writeCsuModuleVersion(fd) == PASS) && (writeOtherModuleVersion(fd) == PASS) &&
  2254. (readQRcodeURLAndSystemDate(fd) == PASS)) {
  2255. //gDoCommGblData.DisConnCount = 0;
  2256. isContinue = 0;
  2257. }
  2258. }
  2259. break;
  2260. /*
  2261. case REG_POWER_CONSUMPTION_INFO:
  2262. if (pSysConfig->ShowInformation || ShmDcCommonData->DebugFlag) {
  2263. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2264. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2265. readConsumptionInfo(fd);
  2266. ftime(&gRegTimeUp[plugNum][curReg]);
  2267. }
  2268. }
  2269. //check misc command from power cabinet
  2270. if (gDoCommGblData.MiscCmd != 0) {
  2271. if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  2272. log_error("misc command failed = %x", gDoCommGblData.MiscCmd);
  2273. }
  2274. curReg = gDoCommGblData.MiscCmd;
  2275. } else if (gMoreInfoReq[plugNum].Value != 0) {
  2276. curReg = REG_RESERVATION_IDTAG;
  2277. } else {
  2278. isContinue = 0;
  2279. }
  2280. break;
  2281. */
  2282. case REG_RESERVATION_IDTAG:
  2283. if (gMoreInfoReq[plugNum].bits.ReservationReq) {
  2284. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2285. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2286. if (readReservationState(fd, gunID) == PASS)
  2287. {
  2288. gMoreInfoReq[plugNum].bits.ReservationReq = ReservationState[plugNum] == NO ? NO : YES;
  2289. }
  2290. ftime(&gRegTimeUp[plugNum][curReg]);
  2291. }
  2292. }
  2293. curReg = REG_REMOTE_START_NO_ID;
  2294. break;
  2295. case REG_REMOTE_START_NO_ID:
  2296. if (gMoreInfoReq[plugNum].bits.RemoteStartNoID) {
  2297. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2298. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2299. if (readRemoteStartNoIDState(fd) == PASS)
  2300. {
  2301. gMoreInfoReq[plugNum].bits.RemoteStartNoID = RemoteStartNoIDState == NO ? NO : YES;
  2302. }
  2303. ftime(&gRegTimeUp[plugNum][curReg]);
  2304. }
  2305. }
  2306. curReg = REG_STATION_INFO;
  2307. break;
  2308. case REG_STATION_INFO:
  2309. if (gMoreInfoReq[plugNum].bits.StationInfoReq) {
  2310. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2311. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2312. if (readChargerStationInfo(fd) == PASS)
  2313. {
  2314. gMoreInfoReq[plugNum].bits.StationInfoReq = NO;
  2315. }
  2316. ftime(&gRegTimeUp[plugNum][curReg]);
  2317. }
  2318. }
  2319. //isContinue = 0;
  2320. curReg = REG_READ_DEFAULT_PRICE;
  2321. break;
  2322. case REG_READ_DEFAULT_PRICE:
  2323. if (gMoreInfoReq[plugNum].bits.DefaultPriceReq) {
  2324. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2325. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2326. readDefaultPrice(fd);
  2327. ftime(&gRegTimeUp[plugNum][curReg]);
  2328. }
  2329. }
  2330. curReg = REG_READ_USER_PRICE;
  2331. break;
  2332. case REG_READ_USER_PRICE:
  2333. if (gMoreInfoReq[plugNum].bits.UserPriceReq) {
  2334. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2335. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2336. readUserPrice(fd,gunID);
  2337. ftime(&gRegTimeUp[plugNum][curReg]);
  2338. }
  2339. }
  2340. curReg = REG_RECEIPT_INFO;
  2341. break;
  2342. case REG_RECEIPT_INFO:
  2343. if (gMoreInfoReq[plugNum].bits.ReceiptReq) {
  2344. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  2345. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  2346. readReceiptInfo(fd, gunID);
  2347. ftime(&gRegTimeUp[plugNum][curReg]);
  2348. }
  2349. }
  2350. curReg = REG_REFUND_AMOUNT;
  2351. break;
  2352. case REG_REFUND_AMOUNT:
  2353. curReg = REG_PREPAYMENT_INFO;
  2354. break;
  2355. case REG_PREPAYMENT_INFO:
  2356. curReg = REG_PAYMENT_FAIL_REASON;
  2357. break;
  2358. case REG_PAYMENT_FAIL_REASON:
  2359. curReg = REG_CONNECTOR_QR_CODE;
  2360. break;
  2361. case REG_CONNECTOR_QR_CODE:
  2362. isContinue = 0;
  2363. break;
  2364. default:
  2365. log_error("error curReg = %x", curReg);
  2366. gDoCommGblData.MiscCmd = 0;
  2367. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  2368. isContinue = 0;
  2369. break;
  2370. }
  2371. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  2372. return DISPENSER_SOCKET_RECONN;
  2373. }
  2374. }//for
  2375. return ret;
  2376. }
  2377. //static bool isDetectPlugin()
  2378. //{
  2379. // if (pSysInfo->WaitForPlugit == YES) {
  2380. // return YES;
  2381. // }
  2382. //
  2383. // return NO;
  2384. //}
  2385. static void systemStatusProcess(int fd, uint8_t totalGun, uint8_t plugNum, uint8_t gunID)
  2386. {
  2387. uint8_t i = 0;
  2388. struct timeb AuthNowTime = {0};
  2389. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  2390. switch (pDcChargingInfo->SystemStatus) {
  2391. case S_IDLE:
  2392. case S_RESERVATION:
  2393. case S_MAINTAIN:
  2394. case S_ALARM:
  2395. case S_AUTHORIZING:
  2396. if(pDcChargingInfo->SystemStatus == S_ALARM &&
  2397. strlen((char*)ShmDcCommonData->pGunInfo[plugNum].ChargeStopTime) <= 1) {
  2398. ftime(&AuthNowTime);
  2399. if (DiffTimeb(gRegTimeUp[plugNum][REG_READ_CHARGING_TIMESTAMP], AuthNowTime) > LOOP_RETRY_TIME ||
  2400. DiffTimeb(gRegTimeUp[plugNum][REG_READ_CHARGING_TIMESTAMP], AuthNowTime) < 0
  2401. ) {
  2402. readChargingTimeStamp(fd, gunID);
  2403. ftime(&gRegTimeUp[plugNum][REG_READ_CHARGING_TIMESTAMP]);
  2404. }
  2405. }
  2406. checkAuthorProcess(fd, plugNum);
  2407. //authorization complete, write wait plug it state
  2408. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance == FAIL_BALANCE_PRICES) {
  2409. break;
  2410. }
  2411. ftime(&AuthNowTime);
  2412. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  2413. DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) < 0
  2414. ) {
  2415. writeWaitPlugItState(fd, gunID);
  2416. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  2417. }
  2418. /*
  2419. ftime(&AuthNowTime);
  2420. if (DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  2421. DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) < 0
  2422. ) {
  2423. readMiscCommand(fd, gunID);
  2424. ftime(&gRegTimeUp[plugNum][REG_MISC_CONTROL]);
  2425. }
  2426. */
  2427. ftime(&AuthNowTime);
  2428. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2429. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2430. ) {
  2431. readChargingCapability(fd, gunID);
  2432. if (pDcChargingInfo->PantographFlag)
  2433. writeGroundFaultDetection(fd, 0, gunID);
  2434. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2435. }
  2436. break;
  2437. case S_PREPARNING: //get permission
  2438. ftime(&AuthNowTime);
  2439. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME ||
  2440. DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) < 0
  2441. ) {
  2442. writeConnectorState(fd, plugNum, gunID);
  2443. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  2444. }
  2445. ftime(&AuthNowTime);
  2446. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > (LOOP_RETRY_TIME/2) ||
  2447. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  2448. ) {
  2449. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  2450. for (i = 0; i < totalGun; i++) {
  2451. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  2452. ShmPsuData->SystemAvailablePower += pDcChargingInfo->AvailableChargingPower;
  2453. }
  2454. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  2455. }
  2456. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  2457. }
  2458. ftime(&AuthNowTime);
  2459. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  2460. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  2461. ) {
  2462. writePresentChargingInfo(fd, plugNum, gunID);
  2463. if (pDcChargingInfo->PantographFlag)
  2464. writeGroundFaultDetection(fd, 0, gunID);
  2465. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  2466. }
  2467. break;
  2468. case S_PREPARING_FOR_EV://wait connector lock
  2469. ftime(&AuthNowTime);
  2470. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2471. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2472. ) {
  2473. readChargingCapability(fd, gunID);
  2474. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2475. }
  2476. ftime(&AuthNowTime);
  2477. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  2478. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  2479. ) {
  2480. if (readChargePermission(fd, gunID) == 0) {
  2481. log_info("S_PREPARING_FOR_EV Stop charging by power cabinet's permission = %d, %d",
  2482. plugNum,
  2483. REG_CHARGING_PERMISSION);
  2484. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  2485. }
  2486. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  2487. }
  2488. ftime(&AuthNowTime);
  2489. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  2490. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  2491. ) {
  2492. writePresentChargingInfo(fd, plugNum, gunID);
  2493. if (pDcChargingInfo->PantographFlag)
  2494. writeGroundFaultDetection(fd, 0, gunID);
  2495. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  2496. }
  2497. break;
  2498. case S_PREPARING_FOR_EVSE: //insaulation test
  2499. case S_CCS_PRECHARGE_ST0:
  2500. case S_CCS_PRECHARGE_ST1:
  2501. writeChargingTarget(fd, plugNum, gunID);
  2502. if (pDcChargingInfo->PantographFlag)
  2503. writeGroundFaultDetection(fd, 1, gunID);
  2504. ftime(&AuthNowTime);
  2505. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2506. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2507. ) {
  2508. readChargingCapability(fd, gunID);
  2509. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2510. }
  2511. ftime(&AuthNowTime);
  2512. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  2513. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  2514. ) {
  2515. if (readChargePermission(fd, gunID) == 0) {
  2516. log_info("S_PREPARING_FOR_EVSE Stop charging by power cabinet's permission = %d, %d",
  2517. plugNum,
  2518. REG_CHARGING_PERMISSION);
  2519. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  2520. }
  2521. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  2522. }
  2523. ftime(&AuthNowTime);
  2524. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  2525. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  2526. ) {
  2527. writePresentChargingInfo(fd, plugNum, gunID);
  2528. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  2529. }
  2530. break;
  2531. case S_CHARGING: //charging
  2532. case S_TERMINATING:
  2533. if(pDcChargingInfo->Type == _Type_GB || pDcChargingInfo->Type == _Type_Chademo)
  2534. {
  2535. if (pDcChargingInfo->PantographFlag)
  2536. writeGroundFaultDetection(fd, 0, gunID);
  2537. }
  2538. else
  2539. {
  2540. if (pDcChargingInfo->PantographFlag)
  2541. writeGroundFaultDetection(fd, 1, gunID);
  2542. }
  2543. ftime(&AuthNowTime);
  2544. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2545. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2546. ) {
  2547. readChargingCapability(fd, gunID);
  2548. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2549. }
  2550. ftime(&AuthNowTime);
  2551. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  2552. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  2553. ) {
  2554. if (readChargePermission(fd, gunID) == 0) {
  2555. if (pDcChargingInfo->StopChargeFlag != POWER_CABINET_STOP_CHARGING) {
  2556. log_info("Stop charging by power cabinet's permission = %d, %d",
  2557. plugNum,
  2558. REG_CHARGING_PERMISSION);
  2559. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  2560. }
  2561. //printf("%d StopChargeFlag = %d\n", plugNum, pDcChargingInfo->StopChargeFlag);
  2562. }
  2563. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  2564. }
  2565. writeChargingTarget(fd, plugNum, gunID);
  2566. ftime(&AuthNowTime);
  2567. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  2568. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  2569. ) {
  2570. writePresentChargingInfo(fd, plugNum, gunID);
  2571. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  2572. }
  2573. // 獲得Charging時間
  2574. if (strlen((char*)ShmDcCommonData->pGunInfo[plugNum].ChargeStopTime) <= 1) {
  2575. ftime(&AuthNowTime);
  2576. if (DiffTimeb(gRegTimeUp[plugNum][REG_READ_CHARGING_TIMESTAMP], AuthNowTime) > LOOP_RETRY_TIME ||
  2577. DiffTimeb(gRegTimeUp[plugNum][REG_READ_CHARGING_TIMESTAMP], AuthNowTime) < 0
  2578. ) {
  2579. readChargingTimeStamp(fd, gunID);
  2580. ftime(&gRegTimeUp[plugNum][REG_READ_CHARGING_TIMESTAMP]);
  2581. }
  2582. }
  2583. break;
  2584. case S_COMPLETE:
  2585. ftime(&AuthNowTime);
  2586. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  2587. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  2588. ) {
  2589. readChargingCapability(fd, gunID);
  2590. if (pDcChargingInfo->PantographFlag)
  2591. writeGroundFaultDetection(fd, 0, gunID);
  2592. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  2593. }
  2594. // 獲得Charging時間
  2595. if (strlen((char*)ShmDcCommonData->pGunInfo[plugNum].ChargeStopTime) <= 1) {
  2596. ftime(&AuthNowTime);
  2597. if (DiffTimeb(gRegTimeUp[plugNum][REG_READ_CHARGING_TIMESTAMP], AuthNowTime) > LOOP_RETRY_TIME ||
  2598. DiffTimeb(gRegTimeUp[plugNum][REG_READ_CHARGING_TIMESTAMP], AuthNowTime) < 0
  2599. ) {
  2600. readChargingTimeStamp(fd, gunID);
  2601. ftime(&gRegTimeUp[plugNum][REG_READ_CHARGING_TIMESTAMP]);
  2602. }
  2603. }
  2604. break;
  2605. default:
  2606. break;
  2607. }
  2608. }
  2609. static int networkCreatePorcess(void)
  2610. {
  2611. int fd = 0;
  2612. uint8_t discount = 0;
  2613. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  2614. usleep(128);
  2615. }
  2616. setTcpStatus(ABNORMAL);
  2617. /**************** Check Network **********/
  2618. //log_info("Check Dispenser Network Information");
  2619. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  2620. usleep(125);
  2621. }
  2622. while (!CheckNetworkStatus()) {
  2623. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  2624. if( gDoCommGblData.DisConnCount != discount ) {
  2625. discount = gDoCommGblData.DisConnCount;
  2626. log_error("disconnect count = % d", gDoCommGblData.DisConnCount);
  2627. }
  2628. }
  2629. log_info("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  2630. pSysConfig->Eth0Interface.EthIpAddress,
  2631. pSysConfig->Eth0Interface.EthSubmaskAddress,
  2632. pSysConfig->Eth0Interface.EthGatewayAddress);
  2633. gDoCommGblData.DisConnCount = 0;
  2634. discount = 0;
  2635. /**************** Power cabinet connection **********/
  2636. log_info("Connect to Power Cabinet");
  2637. while ((fd = doCommConnToServer()) <= 0) {
  2638. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  2639. if( gDoCommGblData.DisConnCount != discount && fd == -1) {
  2640. discount = gDoCommGblData.DisConnCount;
  2641. log_error("disconnect count = %d, fd = %d", gDoCommGblData.DisConnCount, fd);
  2642. }
  2643. }
  2644. log_info("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  2645. fd,
  2646. pSysConfig->Eth0Interface.EthIpAddress,
  2647. pSysConfig->Eth0Interface.EthSubmaskAddress,
  2648. pSysConfig->Eth0Interface.EthGatewayAddress);
  2649. gDoCommGblData.DisConnCount = 0;
  2650. destroySelectGun(DESTROY_ALL_SEL);
  2651. sleep(3);
  2652. setTcpStatus(NORMAL);
  2653. return fd;
  2654. }
  2655. int main(int argc, char *argv[])
  2656. {
  2657. uint8_t plugNum = 0, gunID = 0;
  2658. uint8_t index = 0;
  2659. uint8_t totalConnCount = 0;
  2660. uint8_t initDone = DISPENER_INIT_FAIL;
  2661. char tmpbuf[256] = {0};
  2662. int fd = 0;
  2663. int isContinue = 1;
  2664. InitSocketSigPipe();
  2665. /**************** Initialization **********/
  2666. if (CreateAllCsuShareMemory() == FAIL) {
  2667. log_error("create share memory error");
  2668. return FAIL;
  2669. }
  2670. MappingGunChargingInfo("DoComm Task");
  2671. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  2672. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  2673. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  2674. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  2675. ShmPsuData = (struct PsuData *)GetShmPsuData();
  2676. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  2677. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  2678. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  2679. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  2680. totalConnCount = pSysConfig->TotalConnectorCount;
  2681. for (index = 0; index < totalConnCount; index++) {
  2682. clearPricesInfo(index);
  2683. }
  2684. //initial trigger message timer
  2685. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  2686. ftime(&gRegTimeUp[0][index]);
  2687. usleep(128);
  2688. ftime(&gRegTimeUp[1][index]);
  2689. usleep(50000);
  2690. }
  2691. setTcpStatus(NORMAL);
  2692. gDoCommGblData.DisConnCount = 0;
  2693. while (isContinue) {
  2694. if (initDone == DISPENER_INIT_FAIL) {
  2695. fd = networkCreatePorcess();
  2696. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  2697. } else {
  2698. plugNum = 0;
  2699. gunID = 0;
  2700. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  2701. //checkAuthorProcess(fd, plugNum);
  2702. //plugNum : setup chargingData value for bottom layer
  2703. //gunID : connector Id from power cabinet, 1 = left gun, 2 = right gun,
  2704. gunID = gDoCommGblData.ConnectorID[plugNum];
  2705. systemStatusProcess(fd, totalConnCount, plugNum, gunID);
  2706. initDone = messageTrigger(fd,
  2707. plugNum,
  2708. gunID,
  2709. REG_POWER_CABINET_STATUS);
  2710. if (initDone == DISPENSER_SOCKET_RECONN) {
  2711. break;
  2712. }
  2713. }
  2714. }
  2715. if (initDone != DISPENSER_SOCKET_RECONN) {
  2716. //update dispenser firmware
  2717. updateFirmwareProcess(fd, gDoCommGblData.ConnectorID[0], totalConnCount);
  2718. usleep(100000);
  2719. continue;
  2720. }
  2721. //Ethernet error recovery handle
  2722. log_info("Disconnected from power cabinet...re-connecting");
  2723. setTcpStatus(ABNORMAL);
  2724. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  2725. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2726. sprintf(tmpbuf,
  2727. "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  2728. pSysConfig->SystemId);
  2729. system(tmpbuf);
  2730. }
  2731. gDoCommGblData.DisConnCount = 0;
  2732. gDoCommGblData.SeqNum = 0;
  2733. closeSocket(fd);
  2734. fd = 0;
  2735. initDone = DISPENER_INIT_FAIL;
  2736. }
  2737. }