DoComm.c 100 KB

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