DoComm.c 106 KB

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