DoComm.c 115 KB

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