DoComm.c 100 KB

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