DoComm.c 113 KB

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