DoComm.c 108 KB

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