DoComm.c 115 KB

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