DoComm.c 119 KB

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