main.c 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <stdint.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "Config.h"
  31. #include <stdbool.h>
  32. #include <dirent.h>
  33. #include "timeout.h"
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define BUFFER_SIZE 128
  38. #define YES 1
  39. #define NO 0
  40. #define NORMAL 0
  41. #define ABNORMAL 1
  42. #define EQUAL 0
  43. #define BTN_RELEASE 0
  44. #define BTN_PRESS 1
  45. #define MAX_BUF 64
  46. #define MtdBlockSize 0x600000
  47. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  48. #define UPGRADE_FAN 0x02
  49. #define UPGRADE_RB 0x03
  50. #define UPGRADE_PRI 0x04
  51. #define UPGRADE_AC 0x05
  52. #define SYSTEM_MIN_VOL 150
  53. #define NO_DEFINE 255
  54. #define DEFAULT_AC_INDEX 2
  55. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  56. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  57. int whileLoopTime = 10000; // 10 ms
  58. int wtdFd = -1;
  59. bool IsAuthorizingMode();
  60. void ClearAuthorizedFlag();
  61. bool isDetectPlugin();
  62. void ClearDetectPluginFlag();
  63. int mystrcmp(unsigned char *p1, unsigned char *p2);
  64. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  65. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  66. void ChargingTerminalProcess(byte gunIndex);
  67. void ChkPrimaryStatus();
  68. void StartSystemTimeoutDet(unsigned char flag);
  69. void StopSystemTimeoutDet();
  70. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  71. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  72. int StoreLogMsg_1(const char *fmt, ...);
  73. unsigned long GetTimeoutValue(struct timeval _sour_time);
  74. void gpio_set_value(unsigned int gpio, unsigned int value);
  75. void PRINTF_FUNC(char *string, ...);
  76. void ChangeGunSelectByIndex(byte sel);
  77. void RecordAlarmCode(byte gunIndex, char *code);
  78. void RecordWarningCode(byte gunIndex, char *code);
  79. void ReleaseWarningCodeByString(byte gunIndex, char *code);
  80. void ReleaseAlarmCode(byte gunIndex);
  81. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  82. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  83. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  84. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  85. struct StatusCodeData *ShmStatusCodeData;
  86. struct PsuData *ShmPsuData;
  87. struct CHAdeMOData *ShmCHAdeMOData;
  88. struct GBTData *ShmGBTData;
  89. struct CcsData *ShmCcsData;
  90. struct PrimaryMcuData *ShmPrimaryMcuData;
  91. struct FanModuleData *ShmFanModuleData;
  92. struct RelayModuleData *ShmRelayModuleData;
  93. struct OCPP16Data *ShmOCPP16Data;
  94. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  95. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  96. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  97. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  98. // for initial index to check EV board type is correct
  99. byte _gunIndex = 0;
  100. byte _acgunIndex = 0;
  101. byte _chademoIndex = 0;
  102. byte _ccsIndex = 0;
  103. byte _gb_Index = 0;
  104. byte _ac_Index = 0;
  105. byte bd0_1_status = 0;
  106. byte bd0_2_status = 0;
  107. byte bd1_1_status = 0;
  108. byte bd1_2_status = 0;
  109. bool isCardScan = false;
  110. bool isModelNameMatch = true;
  111. int rfidFd = -1;
  112. char* rfidPortName = "/dev/ttyS2";
  113. char* fwVersion = "D0.13.00.0000.00";
  114. //================================================
  115. // initial can-bus
  116. //================================================
  117. int InitCanBus()
  118. {
  119. int s0,nbytes;
  120. struct timeval tv;
  121. struct ifreq ifr0;
  122. struct sockaddr_can addr0;
  123. system("/sbin/ip link set can0 down");
  124. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  125. system("/sbin/ip link set can0 up");
  126. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  127. tv.tv_sec = 0;
  128. tv.tv_usec = 10000;
  129. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  130. {
  131. #ifdef SystemLogMessage
  132. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  133. #endif
  134. }
  135. nbytes=40960;
  136. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  137. {
  138. #ifdef SystemLogMessage
  139. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  140. #endif
  141. }
  142. nbytes=40960;
  143. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  144. {
  145. #ifdef SystemLogMessage
  146. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  147. #endif
  148. }
  149. strcpy(ifr0.ifr_name, "can0" );
  150. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  151. addr0.can_family = AF_CAN;
  152. addr0.can_ifindex = ifr0.ifr_ifindex;
  153. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  154. return s0;
  155. }
  156. //================================================
  157. // initial uart port
  158. //================================================
  159. char *_priPortName = "/dev/ttyS1";
  160. char *_485PortName = "/dev/ttyS5";
  161. int InitComPort(byte target)
  162. {
  163. int fd;
  164. struct termios tios;
  165. if(target == UPGRADE_PRI)
  166. fd = open(_priPortName, O_RDWR);
  167. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC)
  168. fd = open(_485PortName, O_RDWR);
  169. if(fd<=0)
  170. {
  171. #ifdef SystemLogMessage
  172. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  173. #endif
  174. return -1;
  175. }
  176. ioctl (fd, TCGETS, &tios);
  177. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  178. tios.c_lflag = 0;
  179. tios.c_iflag = 0;
  180. tios.c_oflag = 0;
  181. tios.c_cc[VMIN]=0;
  182. tios.c_cc[VTIME]=(unsigned char)1;
  183. tios.c_lflag=0;
  184. tcflush(fd, TCIFLUSH);
  185. ioctl (fd, TCSETS, &tios);
  186. return fd;
  187. }
  188. //=================================
  189. // Common routine
  190. //=================================
  191. int InitWatchDog()
  192. {
  193. int fd;
  194. system("/usr/bin/fuser -k /dev/watchdog");
  195. sleep(1);
  196. system("echo V > /dev/watchdog");
  197. sleep(1);
  198. fd=open("/dev/watchdog", O_RDWR);
  199. if(fd<=0)
  200. {
  201. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  202. }
  203. return fd;
  204. }
  205. int StoreLogMsg_1(const char *fmt, ...)
  206. {
  207. char Buf[4096+256];
  208. char buffer[4096];
  209. time_t CurrentTime;
  210. struct tm *tm;
  211. va_list args;
  212. va_start(args, fmt);
  213. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  214. va_end(args);
  215. memset(Buf,0,sizeof(Buf));
  216. CurrentTime = time(NULL);
  217. tm=localtime(&CurrentTime);
  218. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  219. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  220. buffer,
  221. tm->tm_year+1900,tm->tm_mon+1);
  222. system(Buf);
  223. return rc;
  224. }
  225. unsigned long GetTimeoutValue(struct timeval _sour_time)
  226. {
  227. struct timeval _end_time;
  228. gettimeofday(&_end_time, NULL);
  229. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  230. }
  231. int mystrcmp(unsigned char *p1, unsigned char *p2)
  232. {
  233. while(*p1==*p2)
  234. {
  235. if(*p1=='\0' || *p2=='\0')
  236. break;
  237. p1++;
  238. p2++;
  239. }
  240. if(*p1=='\0' && *p2=='\0')
  241. return(PASS);
  242. else
  243. return(FAIL);
  244. }
  245. int DiffTimeb(struct timeb ST, struct timeb ET)
  246. {
  247. //return milli-second
  248. unsigned int StartTime,StopTime;
  249. StartTime=(unsigned int)ST.time;
  250. StopTime=(unsigned int)ET.time;
  251. return (StopTime-StartTime);
  252. }
  253. bool CheckTimeOut(struct timeb ST)
  254. {
  255. struct timeb ET;
  256. unsigned int StartTime, StopTime;
  257. ftime(&ET);
  258. StartTime = (unsigned int) ST.time;
  259. StopTime = (unsigned int) ET.time;
  260. return (StopTime > StartTime)? YES : NO;
  261. }
  262. void setChargerMode(byte gun_index, byte mode)
  263. {
  264. chargingInfo[gun_index]->SystemStatus = mode;
  265. }
  266. void PRINTF_FUNC(char *string, ...)
  267. {
  268. va_list args;
  269. char buffer[4096];
  270. va_start(args, string);
  271. vsnprintf(buffer, sizeof(buffer), string, args);
  272. va_end(args);
  273. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  274. printf("%s \n", buffer);
  275. else
  276. DEBUG_INFO_MSG("%s \n", buffer);
  277. }
  278. //==========================================
  279. // Check interface status
  280. //==========================================
  281. int isInterfaceUp(const char *interface)
  282. {
  283. int result = FAIL;
  284. FILE *fp;
  285. char cmd[256];
  286. char buf[512];
  287. strcpy(cmd, "ifconfig");
  288. fp = popen(cmd, "r");
  289. if(fp != NULL)
  290. {
  291. while(fgets(buf, sizeof(buf), fp) != NULL)
  292. {
  293. if(strstr(buf, interface) > 0)
  294. {
  295. result = PASS;
  296. }
  297. }
  298. }
  299. pclose(fp);
  300. return result;
  301. }
  302. //=================================
  303. // Create all share memory
  304. //=================================
  305. int CreateShareMemory()
  306. {
  307. int MeterSMId;
  308. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  309. {
  310. #ifdef SystemLogMessage
  311. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  312. #endif
  313. return 0;
  314. }
  315. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  316. {
  317. #ifdef SystemLogMessage
  318. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  319. #endif
  320. return 0;
  321. }
  322. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  323. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  324. {
  325. #ifdef SystemLogMessage
  326. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  327. #endif
  328. return 0;
  329. }
  330. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  331. {
  332. #ifdef SystemLogMessage
  333. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  334. #endif
  335. return 0;
  336. }
  337. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  338. //creat ShmPsuData
  339. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  340. {
  341. #ifdef SystemLogMessage
  342. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  343. #endif
  344. return 0;
  345. }
  346. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  347. {
  348. #ifdef SystemLogMessage
  349. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  350. #endif
  351. return 0;
  352. }
  353. memset(ShmPsuData, 0, sizeof(struct PsuData));
  354. if(CHAdeMO_QUANTITY > 0)
  355. {
  356. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  357. {
  358. #ifdef SystemLogMessage
  359. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  360. #endif
  361. return 0;
  362. }
  363. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  364. {
  365. #ifdef SystemLogMessage
  366. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  367. #endif
  368. return 0;
  369. }
  370. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  371. }
  372. if(GB_QUANTITY > 0)
  373. {
  374. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  375. {
  376. #ifdef SystemLogMessage
  377. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  378. #endif
  379. return 0;
  380. }
  381. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  382. {
  383. #ifdef SystemLogMessage
  384. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  385. #endif
  386. return 0;
  387. }
  388. memset(ShmGBTData, 0, sizeof(struct GBTData));
  389. }
  390. //creat ShmCcsData
  391. if(CCS_QUANTITY > 0)
  392. {
  393. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  394. {
  395. #ifdef SystemLogMessage
  396. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  397. #endif
  398. return 0;
  399. }
  400. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  401. {
  402. #ifdef SystemLogMessage
  403. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  404. #endif
  405. return 0;
  406. }
  407. memset(ShmCcsData, 0, sizeof(struct CcsData));
  408. }
  409. //creat ShmPrimaryMcuData
  410. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  411. {
  412. #ifdef SystemLogMessage
  413. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  414. #endif
  415. return 0;
  416. }
  417. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  418. {
  419. #ifdef SystemLogMessage
  420. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  421. #endif
  422. return 0;
  423. }
  424. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  425. //creat ShmFanModuleData
  426. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  427. {
  428. #ifdef SystemLogMessage
  429. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  430. #endif
  431. return 0;
  432. }
  433. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  434. {
  435. #ifdef SystemLogMessage
  436. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  437. #endif
  438. return 0;
  439. }
  440. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  441. //creat ShmRelayModuleData
  442. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  443. {
  444. #ifdef SystemLogMessage
  445. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  446. #endif
  447. return 0;
  448. }
  449. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  450. {
  451. #ifdef SystemLogMessage
  452. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  453. #endif
  454. return 0;
  455. }
  456. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  457. //creat ShmOCPP16Data
  458. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  459. {
  460. #ifdef SystemLogMessage
  461. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  462. #endif
  463. return 0;
  464. }
  465. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  466. {
  467. #ifdef SystemLogMessage
  468. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  469. #endif
  470. return 0;
  471. }
  472. // memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  473. return 1;
  474. }
  475. //=================================
  476. // LCM Page
  477. //=================================
  478. void ChangeLcmByIndex(byte page_index)
  479. {
  480. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2 ||
  481. page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
  482. {
  483. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  484. }
  485. }
  486. //======================================================
  487. // Peripheral initial
  488. //======================================================
  489. void InitGPIO()
  490. {
  491. /*****************0~3, 4 bank, bank x 32+ num*********************/
  492. /***************************************************************/
  493. /*************** GPIO 0 ***************************************/
  494. /***************************************************************/
  495. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  496. system("echo 22 > /sys/class/gpio/export");
  497. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  498. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  499. system("echo 23 > /sys/class/gpio/export");
  500. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  501. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  502. system("echo 26 > /sys/class/gpio/export");
  503. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  504. system("echo 1 > /sys/class/gpio/gpio26/value");
  505. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  506. system("echo 27 > /sys/class/gpio/export");
  507. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  508. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  509. system("echo 29 > /sys/class/gpio/export");
  510. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  511. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  512. system("echo 19 > /sys/class/gpio/export");
  513. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  514. system("echo 1 > /sys/class/gpio/gpio19/value");
  515. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  516. system("echo 20 > /sys/class/gpio/export");
  517. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  518. /***************************************************************/
  519. /*************** GPIO 1 ***************************************/
  520. /***************************************************************/
  521. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  522. system("echo 44 > /sys/class/gpio/export");
  523. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  524. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  525. system("echo 45 > /sys/class/gpio/export");
  526. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  527. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  528. system("echo 46 > /sys/class/gpio/export");
  529. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  530. system("echo 0 > /sys/class/gpio/gpio46/value");
  531. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  532. system("echo 47 > /sys/class/gpio/export");
  533. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  534. /***************************************************************/
  535. /*************** GPIO 2 ***************************************/
  536. /***************************************************************/
  537. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  538. system("echo 89 > /sys/class/gpio/export");
  539. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  540. system("echo 1 > /sys/class/gpio/gpio89/value");
  541. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  542. system("echo 87 > /sys/class/gpio/export");
  543. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  544. system("echo 0 > /sys/class/gpio/gpio87/value");
  545. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  546. system("echo 88 > /sys/class/gpio/export");
  547. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  548. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  549. system("echo 86 > /sys/class/gpio/export");
  550. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  551. /***************************************************************/
  552. /*************** GPIO 3 ***************************************/
  553. /***************************************************************/
  554. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  555. system("echo 111 > /sys/class/gpio/export");
  556. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  557. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  558. system("echo 114 > /sys/class/gpio/export");
  559. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  560. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  561. system("echo 113 > /sys/class/gpio/export");
  562. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  563. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  564. system("echo 110 > /sys/class/gpio/export");
  565. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  566. system("echo 0 > /sys/class/gpio/gpio110/value");
  567. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  568. system("echo 115 > /sys/class/gpio/export");
  569. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  570. system("echo 0 > /sys/class/gpio/gpio115/value");
  571. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  572. system("echo 112 > /sys/class/gpio/export");
  573. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  574. system("echo 1 > /sys/class/gpio/gpio112/value");
  575. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  576. system("echo 116 > /sys/class/gpio/export");
  577. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  578. system("echo 0 > /sys/class/gpio/gpio116/value");
  579. #ifdef SystemLogMessage
  580. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK");
  581. #endif
  582. }
  583. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  584. {
  585. int fd,wrd;
  586. unsigned char *buf;
  587. unsigned int ChkSum,ChkSumOrg;
  588. if((buf=malloc(MtdBlockSize))==NULL)
  589. {
  590. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  591. if(ShmStatusCodeData!=NULL)
  592. {
  593. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  594. }
  595. sleep(5);
  596. system("reboot -f");
  597. sleep(5);
  598. system("reboot -f");
  599. }
  600. memset(buf, 0, MtdBlockSize);
  601. //================================================
  602. // Load configuration from mtdblock10
  603. //================================================
  604. fd = open("/dev/mtdblock10", O_RDWR);
  605. if (fd < 0)
  606. {
  607. free(buf);
  608. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  609. if(ShmStatusCodeData!=NULL)
  610. {
  611. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  612. }
  613. sleep(5);
  614. system("reboot -f");
  615. sleep(5);
  616. system("reboot -f");
  617. }
  618. wrd=read(fd, buf, MtdBlockSize);
  619. close(fd);
  620. if(wrd<MtdBlockSize)
  621. {
  622. free(buf);
  623. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  624. if(ShmStatusCodeData!=NULL)
  625. {
  626. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  627. }
  628. sleep(5);
  629. system("reboot -f");
  630. sleep(5);
  631. system("reboot -f");
  632. }
  633. ChkSum=0;
  634. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  635. {
  636. ChkSum+=buf[wrd];
  637. }
  638. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  639. //================================================
  640. // Load configuration from mtdblock11
  641. //================================================
  642. if(ChkSum!=ChkSumOrg)
  643. {
  644. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  645. fd = open("/dev/mtdblock11", O_RDWR);
  646. if (fd < 0)
  647. {
  648. free(buf);
  649. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  650. if(ShmStatusCodeData!=NULL)
  651. {
  652. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  653. }
  654. sleep(5);
  655. system("reboot -f");
  656. sleep(5);
  657. system("reboot -f");
  658. }
  659. memset(buf, 0, MtdBlockSize);
  660. wrd=read(fd, buf,MtdBlockSize);
  661. close(fd);
  662. if(wrd<MtdBlockSize)
  663. {
  664. free(buf);
  665. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  666. if(ShmStatusCodeData!=NULL)
  667. {
  668. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  669. }
  670. sleep(5);
  671. system("reboot -f");
  672. sleep(5);
  673. system("reboot -f");
  674. }
  675. ChkSum=0;
  676. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  677. {
  678. ChkSum+=buf[wrd];
  679. }
  680. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  681. //================================================
  682. // Load configuration from mtdblock12 (Factory default)
  683. //================================================
  684. if(ChkSum!=ChkSumOrg)
  685. {
  686. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  687. fd = open("/dev/mtdblock12", O_RDWR);
  688. if (fd < 0)
  689. {
  690. free(buf);
  691. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  692. if(ShmStatusCodeData!=NULL)
  693. {
  694. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  695. }
  696. sleep(5);
  697. system("reboot -f");
  698. sleep(5);
  699. system("reboot -f");
  700. }
  701. memset(buf, 0, MtdBlockSize);
  702. wrd=read(fd, buf,MtdBlockSize);
  703. close(fd);
  704. if(wrd<MtdBlockSize)
  705. {
  706. free(buf);
  707. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  708. if(ShmStatusCodeData!=NULL)
  709. {
  710. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  711. }
  712. sleep(5);
  713. system("reboot -f");
  714. sleep(5);
  715. system("reboot -f");
  716. }
  717. ChkSum=0;
  718. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  719. {
  720. ChkSum+=buf[wrd];
  721. }
  722. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  723. if(ChkSum!=ChkSumOrg)
  724. {
  725. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  726. free(buf);
  727. system("cd /root;./FactoryConfig -m");
  728. system("sync");
  729. sleep(5);
  730. system("reboot -f");
  731. sleep(5);
  732. system("reboot -f");
  733. return FAIL;
  734. }
  735. }
  736. }
  737. //load OK
  738. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  739. free(buf);
  740. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  741. return PASS;
  742. }
  743. int isReachableInternet()
  744. {
  745. int result = FAIL;
  746. FILE *fp;
  747. char cmd[256];
  748. char buf[512];
  749. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  750. {
  751. strcpy(cmd, "ping -c 1 -w 3 ");
  752. strcat(cmd, valid_Internet[idx]);
  753. fp = popen(cmd, "r");
  754. if(fp != NULL)
  755. {
  756. while(fgets(buf, sizeof(buf), fp) != NULL)
  757. {
  758. if(strstr(buf, "transmitted") > 0)
  759. {
  760. if(strstr(buf,"100%") != NULL)
  761. {
  762. //NULL
  763. }
  764. else
  765. {
  766. result = PASS;
  767. }
  768. }
  769. }
  770. }
  771. pclose(fp);
  772. }
  773. return result;
  774. }
  775. void InitEthernet()
  776. {
  777. char tmpbuf[256];
  778. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  779. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  780. sleep(2);
  781. //Init Eth0 for internet
  782. memset(tmpbuf,0,256);
  783. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  784. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  785. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  786. system(tmpbuf);
  787. memset(tmpbuf,0,256);
  788. sprintf(tmpbuf,"route add default gw %s eth0 ",
  789. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  790. system(tmpbuf);
  791. //Init Eth1 for administrator tool
  792. memset(tmpbuf,0,256);
  793. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  794. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  795. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  796. system(tmpbuf);
  797. //Run DHCP client if enabled
  798. system("killall udhcpc");
  799. system("rm -rf /etc/resolv.conf");
  800. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  801. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  802. if(ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthDhcpClient == 0)
  803. system("/sbin/udhcpc -i eth0 -s /root/dhcp_script/eth0.script > /dev/null &");
  804. //Upgrade system id to /etc/hostname
  805. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  806. system(tmpbuf);
  807. pid_t pid = fork();
  808. uint8_t cnt_pingDNS_Fail;
  809. if(pid == 0)
  810. {
  811. for(;;)
  812. {
  813. if(isReachableInternet() == PASS)
  814. {
  815. ShmSysConfigAndInfo->SysInfo.InternetConn = YES;
  816. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= NO;
  817. cnt_pingDNS_Fail = 0;
  818. }
  819. else
  820. {
  821. if(++cnt_pingDNS_Fail > 3)
  822. {
  823. ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
  824. //ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= YES;
  825. }
  826. }
  827. sleep(5);
  828. }
  829. }
  830. #ifdef SystemLogMessage
  831. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK");
  832. #endif
  833. }
  834. int InitialRfidPort()
  835. {
  836. int uartO2 = open(rfidPortName, O_RDWR);
  837. struct termios tios;
  838. if (uartO2 != FAIL)
  839. {
  840. ioctl (uartO2, TCGETS, &tios);
  841. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  842. tios.c_lflag = 0;
  843. tios.c_iflag = 0;
  844. tios.c_oflag = 0;
  845. tios.c_cc[VMIN] = 0;
  846. tios.c_cc[VTIME] = (unsigned char) 1;
  847. tios.c_lflag = 0;
  848. tcflush(uartO2, TCIFLUSH);
  849. ioctl(uartO2, TCSETS, &tios);
  850. }
  851. if (uartO2 < 0)
  852. {
  853. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  854. }
  855. return uartO2;
  856. }
  857. void GetMacAddress()
  858. {
  859. for (byte index = 0; index < 2; index++)
  860. {
  861. int fd;
  862. struct ifreq ifr;
  863. char tarEth[5];
  864. char Mac[18];
  865. sprintf(tarEth,"eth%d",index);
  866. fd = socket(AF_INET, SOCK_DGRAM, 0);
  867. ifr.ifr_addr.sa_family = AF_INET;
  868. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  869. ioctl(fd, SIOCGIFHWADDR, &ifr);
  870. close(fd);
  871. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  872. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  873. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  874. if (index == 0)
  875. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  876. else
  877. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  878. }
  879. }
  880. void GetFirmwareVersion()
  881. {
  882. // Get CSU root file system version
  883. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  884. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  885. for(uint8_t idx=0;idx<3;idx++)
  886. {
  887. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  888. {
  889. chademo++;
  890. count++;
  891. }
  892. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  893. {
  894. gb++;
  895. count++;
  896. }
  897. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  898. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  899. {
  900. ccs++;
  901. count++;
  902. }
  903. }
  904. if (count == 1)
  905. {
  906. if (chademo > 0)
  907. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  908. else if (ccs > 0)
  909. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  910. else if (gb > 0)
  911. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  912. }
  913. else
  914. {
  915. if (chademo > 0 && ccs > 0)
  916. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  917. else if (chademo > 0 && gb > 0)
  918. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  919. else if (ccs > 0 && gb > 0)
  920. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  921. }
  922. // Get network option from model name
  923. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  924. {
  925. case 'B':
  926. case 'U':
  927. //Blue tooth
  928. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  929. break;
  930. case 'W':
  931. // WIFI
  932. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  933. break;
  934. case 'T':
  935. // 3G/4G
  936. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  937. break;
  938. default:
  939. // LAN
  940. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  941. break;
  942. }
  943. // Get rating power from model name
  944. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  945. // Get IEC or UL
  946. char _buf[3] = {0};
  947. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  948. if (strcmp(_buf, "YE") == EQUAL)
  949. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  950. else if (strcmp(_buf, "WU") == EQUAL)
  951. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  952. }
  953. void InitialShareMemoryInfo()
  954. {
  955. FILE *fp;
  956. char cmd[512];
  957. char buf[512];
  958. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  959. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  960. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  961. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  962. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  963. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  964. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  965. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  966. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  967. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  968. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  969. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  970. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  971. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  972. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  973. sprintf(cmd, "/bin/uname -r");
  974. fp = popen(cmd, "r");
  975. if(fp == NULL)
  976. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  977. else
  978. {
  979. while(fgets(buf, sizeof(buf), fp) != NULL)
  980. {
  981. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  982. }
  983. }
  984. // 雙槍 CCS + Chademo
  985. GetFirmwareVersion();
  986. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  987. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  988. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  989. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  990. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  991. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  992. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  993. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  994. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  995. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  996. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  997. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  998. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  999. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1000. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1001. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1002. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1003. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1004. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1005. ShmSysConfigAndInfo->SysInfo.OrderCharging = FAIL;
  1006. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1007. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1008. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1009. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1010. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1011. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1012. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1013. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1014. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1015. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1016. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1017. ShmRelayModuleData->SelfTest_Comp = NO;
  1018. ShmFanModuleData->SelfTest_Comp = NO;
  1019. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1020. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1021. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1022. ShmFanModuleData->TestFanSpeed = 0;
  1023. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1024. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1025. char EvsePower[2];
  1026. EvsePower[2] = '\0';
  1027. unsigned short buf_pow = 0;
  1028. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6)
  1029. {
  1030. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  1031. if (strcmp(EvsePower, "15") == EQUAL)
  1032. buf_pow = 150;
  1033. else if (strcmp(EvsePower, "30") == EQUAL)
  1034. buf_pow = 30;
  1035. else if (strcmp(EvsePower, "60") == EQUAL)
  1036. buf_pow = 60;
  1037. else if (strcmp(EvsePower, "18") == EQUAL)
  1038. buf_pow = 180;
  1039. else if (strcmp(EvsePower, "36") == EQUAL)
  1040. buf_pow = 360;
  1041. ShmSysConfigAndInfo->SysConfig.RatingCurrent = (buf_pow / 30) * 100;
  1042. if(ShmSysConfigAndInfo->SysConfig.MaxChargingPower == 0 ||
  1043. ShmSysConfigAndInfo->SysConfig.MaxChargingPower > buf_pow)
  1044. {
  1045. ShmSysConfigAndInfo->SysConfig.MaxChargingPower = buf_pow;
  1046. }
  1047. }
  1048. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1049. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1050. }
  1051. int Initialization()
  1052. {
  1053. // 初始化卡號驗證的 Flag
  1054. ClearAuthorizedFlag();
  1055. // 初始化插槍驗證的 Flag
  1056. ClearDetectPluginFlag();
  1057. // UART 2 for Rfid
  1058. rfidFd = InitialRfidPort();
  1059. int pinOut[2] = { 115, 116 };
  1060. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1061. {
  1062. if (chargingInfo[count]->Type == _Type_Chademo)
  1063. {
  1064. gpio_set_value(pinOut[count], 0x01);
  1065. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1066. }
  1067. else if (chargingInfo[count]->Type == _Type_GB)
  1068. {
  1069. gpio_set_value(pinOut[count], 0x01);
  1070. ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1071. }
  1072. else if (chargingInfo[count]->Type == _Type_CCS_2)
  1073. {
  1074. if (ShmCcsData->CommProtocol == 0x01)
  1075. {
  1076. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1077. gpio_set_value(pinOut[1], 0x00);
  1078. else
  1079. gpio_set_value(pinOut[count], 0x00);
  1080. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1081. }
  1082. }
  1083. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1084. }
  1085. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  1086. {
  1087. if (ac_chargingInfo[count]->Type == _Type_AC)
  1088. {
  1089. ac_chargingInfo[count]->SelfTest_Comp = NO;
  1090. }
  1091. }
  1092. PRINTF_FUNC("Initialization OK \n");
  1093. return PASS;
  1094. }
  1095. bool InitialSystemDefaultConfig()
  1096. {
  1097. bool result = true;
  1098. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1099. InitGPIO();
  1100. InitEthernet();
  1101. GetMacAddress();
  1102. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1103. // sleep(3);
  1104. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1105. // sleep(1);
  1106. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up");
  1107. return result;
  1108. }
  1109. void DisplaySelfTestFailReason()
  1110. {
  1111. // RB、FB、407、EV 小板中有些板子無回應
  1112. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1113. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; }
  1114. if (ShmFanModuleData->SelfTest_Comp == NO)
  1115. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; }
  1116. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1117. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; }
  1118. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1119. {
  1120. if (chargingInfo[index]->Type == _Type_Chademo)
  1121. {
  1122. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1123. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; }
  1124. }
  1125. else if (chargingInfo[index]->Type == _Type_GB)
  1126. {
  1127. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1128. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; }
  1129. }
  1130. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1131. {
  1132. if (ShmCcsData->CommProtocol == 0x01)
  1133. {
  1134. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1135. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; }
  1136. }
  1137. }
  1138. }
  1139. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1140. {
  1141. // 先借 GBT 顯示
  1142. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1143. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; }
  1144. }
  1145. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1146. {
  1147. // AC Contact 未搭上
  1148. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1149. }
  1150. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1151. {
  1152. // PSU 通訊問題
  1153. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1154. }
  1155. }
  1156. void SelfTestRun()
  1157. {
  1158. bool evInitFlag = false;
  1159. StartSystemTimeoutDet(Timeout_SelftestChk);
  1160. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1161. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  1162. {
  1163. ChkPrimaryStatus();
  1164. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  1165. {
  1166. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1167. return;
  1168. }
  1169. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  1170. {
  1171. if (ShmPsuData->Work_Step == _NO_WORKING ||
  1172. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  1173. {
  1174. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1175. return;
  1176. }
  1177. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  1178. {
  1179. case _STEST_VERSION:
  1180. {
  1181. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  1182. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  1183. {
  1184. //PRINTF_FUNC("RB pass \n");
  1185. ShmRelayModuleData->SelfTest_Comp = YES;
  1186. }
  1187. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  1188. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  1189. {
  1190. //PRINTF_FUNC("Fan pass \n");
  1191. ShmFanModuleData->SelfTest_Comp = YES;
  1192. }
  1193. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  1194. ShmPrimaryMcuData->version[0] != '\0')
  1195. {
  1196. //PRINTF_FUNC("407 pass \n");
  1197. ShmPrimaryMcuData->SelfTest_Comp = YES;
  1198. }
  1199. // EV 小板
  1200. if (!evInitFlag)
  1201. {
  1202. evInitFlag = YES;
  1203. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1204. {
  1205. if (chargingInfo[index]->Type == _Type_Chademo)
  1206. {
  1207. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  1208. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1209. {
  1210. //PRINTF_FUNC("chademo pass \n");
  1211. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1212. }
  1213. else
  1214. {
  1215. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1216. evInitFlag = NO;
  1217. }
  1218. }
  1219. else if (chargingInfo[index]->Type == _Type_GB)
  1220. {
  1221. if (strlen((char *)ShmGBTData->evse[chargingInfo[index]->type_index].version) != 0 ||
  1222. ShmGBTData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1223. {
  1224. //PRINTF_FUNC("GBT pass \n");
  1225. ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1226. }
  1227. else
  1228. {
  1229. //PRINTF_FUNC("GBT fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1230. evInitFlag = NO;
  1231. }
  1232. }
  1233. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1234. {
  1235. if (ShmCcsData->CommProtocol == 0x01)
  1236. {
  1237. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  1238. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  1239. {
  1240. //PRINTF_FUNC("ccs fw = %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1241. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1242. }
  1243. else
  1244. {
  1245. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1246. evInitFlag = NO;
  1247. }
  1248. }
  1249. }
  1250. }
  1251. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1252. {
  1253. if (ac_chargingInfo[index]->Type == _Type_AC)
  1254. {
  1255. if (strlen((char *)ac_chargingInfo[index]->version) != 0 ||
  1256. ac_chargingInfo[index]->version[0] != '\0')
  1257. {
  1258. ac_chargingInfo[index]->SelfTest_Comp = YES;
  1259. }
  1260. else
  1261. {
  1262. evInitFlag = NO;
  1263. }
  1264. }
  1265. }
  1266. }
  1267. if (ShmFanModuleData->SelfTest_Comp &&
  1268. ShmRelayModuleData->SelfTest_Comp &&
  1269. ShmPrimaryMcuData->SelfTest_Comp &&
  1270. evInitFlag)
  1271. {
  1272. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  1273. }
  1274. }
  1275. break;
  1276. case _STEST_AC_CONTACTOR:
  1277. {
  1278. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1279. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1280. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  1281. {
  1282. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1283. PRINTF_FUNC("Communication board pass. \n");
  1284. }
  1285. }
  1286. break;
  1287. case _STEST_PSU_DETECT:
  1288. {
  1289. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  1290. {
  1291. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1292. }
  1293. }
  1294. break;
  1295. case _STEST_PSU_CAP:
  1296. {
  1297. // 此測試是要確認當前總輸出能力
  1298. // 如果沒有 PSU 模組請 bypass
  1299. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  1300. {
  1301. sleep(1);
  1302. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1303. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1304. }
  1305. }
  1306. break;
  1307. }
  1308. }
  1309. else
  1310. break;
  1311. usleep(100000);
  1312. }
  1313. }
  1314. int SpawnTask()
  1315. {
  1316. sleep(2);
  1317. system("/root/Module_EventLogging &");
  1318. system("/root/Module_PrimaryComm &");
  1319. system("/root/Module_EvComm &");
  1320. system("/root/Module_LcmControl &");
  1321. system("/root/Module_InternalComm &");
  1322. system("/root/Module_PsuComm &");
  1323. system("/root/Module_ProduceUtils &");
  1324. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  1325. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  1326. {
  1327. system("/root/OcppBackend &");
  1328. }
  1329. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1330. {
  1331. system("/root/Module_4g &");
  1332. }
  1333. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1334. {
  1335. system("/root/Module_Wifi &");
  1336. }
  1337. return PASS;
  1338. }
  1339. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1340. {
  1341. int result = PASS;
  1342. int fd,wrd;
  1343. unsigned int i,Chk;
  1344. unsigned char *ptr, *BufTmp;
  1345. Chk=0;
  1346. ptr=(unsigned char *)UsrData;
  1347. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  1348. {
  1349. memset(BufTmp, 0, MtdBlockSize);
  1350. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  1351. for(i=0; i<MtdBlockSize-4; i++)
  1352. Chk+=*(ptr+i);
  1353. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  1354. fd = open("/dev/mtdblock10", O_RDWR);
  1355. if (fd > 0)
  1356. {
  1357. wrd=write(fd, BufTmp, MtdBlockSize);
  1358. close(fd);
  1359. if(wrd >= MtdBlockSize)
  1360. {
  1361. fd = open("/dev/mtdblock11", O_RDWR);
  1362. if (fd > 0)
  1363. {
  1364. wrd=write(fd, BufTmp, MtdBlockSize);
  1365. close(fd);
  1366. if(wrd < MtdBlockSize)
  1367. {
  1368. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  1369. result = FAIL;
  1370. }
  1371. }
  1372. else
  1373. {
  1374. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  1375. result = FAIL;
  1376. }
  1377. }
  1378. else
  1379. {
  1380. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  1381. result = FAIL;
  1382. }
  1383. }
  1384. else
  1385. {
  1386. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  1387. result = FAIL;
  1388. }
  1389. }
  1390. else
  1391. {
  1392. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  1393. result = FAIL;
  1394. }
  1395. if(BufTmp != NULL)
  1396. free(BufTmp);
  1397. return result;
  1398. }
  1399. //===============================================
  1400. // Common Detect Chk - Stop Charging ?
  1401. //===============================================
  1402. bool isEvBoardStopChargeFlag(byte gunIndex)
  1403. {
  1404. return chargingInfo[gunIndex]->StopChargeFlag;
  1405. }
  1406. //===============================================
  1407. // 掃描插槍狀況
  1408. //===============================================
  1409. void ClearDetectPluginFlag()
  1410. {
  1411. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  1412. }
  1413. void DetectPluginStart()
  1414. {
  1415. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  1416. }
  1417. bool isDetectPlugin()
  1418. {
  1419. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  1420. return YES;
  1421. return NO;
  1422. }
  1423. //===============================================
  1424. // Common Detect Chk - Chademo
  1425. //===============================================
  1426. bool isEvGunLocked_chademo(byte gunIndex)
  1427. {
  1428. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1429. }
  1430. bool isEvContactorWelding_chademo(byte gunIndex)
  1431. {
  1432. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  1433. }
  1434. bool isEvStopReq_chademo(byte gunIndex)
  1435. {
  1436. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  1437. }
  1438. bool isEvStopCharging_chademo(byte gunIndex)
  1439. {
  1440. if (isEvGunLocked_chademo(gunIndex) == NO)
  1441. {
  1442. // 無鎖槍 = 停止
  1443. PRINTF_FUNC("gun locked none (%d) \n", gunIndex);
  1444. return YES;
  1445. }
  1446. return NO;
  1447. }
  1448. byte isPrechargeStatus_chademo(byte gunIndex)
  1449. {
  1450. byte result = 0x00;
  1451. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1452. return result;
  1453. }
  1454. //===============================================
  1455. // Common Detect Chk - GB
  1456. //===============================================
  1457. bool isEvGunLocked_gb(byte gunIndex)
  1458. {
  1459. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1460. }
  1461. bool isEvStopCharging_gb(byte gunIndex)
  1462. {
  1463. if (isEvGunLocked_gb(gunIndex) == NO)
  1464. {
  1465. // 無鎖槍 = 停止
  1466. PRINTF_FUNC("gun locked none. \n");
  1467. return YES;
  1468. }
  1469. return NO;
  1470. }
  1471. byte isPrechargeStatus_gb(byte gunIndex)
  1472. {
  1473. byte result = 0x00;
  1474. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1475. return result;
  1476. }
  1477. //===============================================
  1478. // Common Detect Chk - CCS
  1479. //===============================================
  1480. bool isEvGunLocked_ccs(byte gunIndex)
  1481. {
  1482. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1483. }
  1484. byte isPrechargeStatus_ccs(byte gunIndex)
  1485. {
  1486. byte result = 0x00;
  1487. if (ShmCcsData->CommProtocol == 0x01)
  1488. {
  1489. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1490. }
  1491. return result;
  1492. }
  1493. bool isEvStopCharging_ccs(byte gunIndex)
  1494. {
  1495. if (isEvGunLocked_ccs(gunIndex) == NO)
  1496. {
  1497. // 無鎖槍 = 停止
  1498. PRINTF_FUNC("gun locked none. \n");
  1499. return YES;
  1500. }
  1501. return NO;
  1502. }
  1503. //===============================================
  1504. // Callback
  1505. //===============================================
  1506. void DisplayChargingInfo()
  1507. {
  1508. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  1509. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  1510. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  1511. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)
  1512. {
  1513. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1514. }
  1515. else
  1516. {
  1517. for (byte i = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount - 1; i >= 0; i--)
  1518. {
  1519. if (chargingInfo[i]->SystemStatus != S_IDLE)
  1520. {
  1521. ChangeGunSelectByIndex(i);
  1522. break;
  1523. }
  1524. }
  1525. }
  1526. usleep(50000);
  1527. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1528. }
  1529. void _AutoReturnTimeout()
  1530. {
  1531. PRINTF_FUNC("*********** _AutoReturnTimeout *********** \n");
  1532. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  1533. {
  1534. ClearDetectPluginFlag();
  1535. }
  1536. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  1537. {
  1538. DetectPluginStart();
  1539. }
  1540. usleep(50000);
  1541. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1542. }
  1543. void _SelfTestTimeout()
  1544. {
  1545. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  1546. {
  1547. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  1548. {
  1549. setChargerMode(gun_index, MODE_ALARM);
  1550. }
  1551. }
  1552. ShmPsuData->Work_Step = _NO_WORKING;
  1553. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1554. PRINTF_FUNC("Self test timeout. \n");
  1555. }
  1556. void _AuthorizedTimeout()
  1557. {
  1558. if(IsAuthorizingMode())
  1559. {
  1560. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  1561. isCardScan = false;
  1562. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1563. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1564. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1565. ClearAuthorizedFlag();
  1566. }
  1567. }
  1568. void _DetectPlugInTimeout()
  1569. {
  1570. PRINTF_FUNC("*********** _DetectPlugInTimeout *********** \n");
  1571. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1572. ClearDetectPluginFlag();
  1573. usleep(50000);
  1574. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1575. }
  1576. void _DetectEvChargingEnableTimeout(byte gunIndex)
  1577. {
  1578. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1579. {
  1580. if(!isEvGunLocked_chademo(gunIndex))
  1581. {
  1582. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  1583. }
  1584. }
  1585. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  1586. {
  1587. if(!isEvGunLocked_ccs(gunIndex))
  1588. {
  1589. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  1590. }
  1591. }
  1592. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1593. {
  1594. if(!isEvGunLocked_ccs(gunIndex))
  1595. {
  1596. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  1597. }
  1598. }
  1599. ChargingTerminalProcess(gunIndex);
  1600. _AutoReturnTimeout();
  1601. }
  1602. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  1603. {
  1604. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  1605. setChargerMode(gunIndex, MODE_IDLE);
  1606. _AutoReturnTimeout();
  1607. }
  1608. void _PrepareTimeout(byte gunIndex)
  1609. {
  1610. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  1611. setChargerMode(gunIndex, MODE_IDLE);
  1612. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  1613. _AutoReturnTimeout();
  1614. }
  1615. void _CcsPrechargeTimeout(byte gunIndex)
  1616. {
  1617. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  1618. setChargerMode(gunIndex, MODE_IDLE);
  1619. }
  1620. //===============================================
  1621. // 取得卡號與卡號驗證
  1622. //===============================================
  1623. bool canStartCharging()
  1624. {
  1625. char buf2[16] = "";
  1626. memset(buf2, 0, ARRAY_SIZE(buf2));
  1627. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
  1628. {
  1629. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  1630. }
  1631. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  1632. // 因為無法得知實際的長度,所以只能用搜尋的方式
  1633. if(strcmp(buf2, "Accepted") == EQUAL)
  1634. return true;
  1635. else
  1636. {
  1637. //NULL
  1638. }
  1639. return false;
  1640. }
  1641. void AuthorizingStart()
  1642. {
  1643. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1644. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  1645. }
  1646. void ClearAuthorizedFlag()
  1647. {
  1648. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1649. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  1650. }
  1651. bool isAuthorizedComplete()
  1652. {
  1653. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
  1654. return false;
  1655. return true;
  1656. }
  1657. bool IsAuthorizingMode()
  1658. {
  1659. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  1660. return false;
  1661. return true;
  1662. }
  1663. //===============================================
  1664. // 紀錄 Alarm Code
  1665. //===============================================
  1666. void RecordAlarmCode(byte gunIndex, char *code)
  1667. {
  1668. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  1669. if (strcmp(code, "012234") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  1670. if (strcmp(code, "012235") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  1671. if (strcmp(code, "012236") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  1672. if (strcmp(code, "012288") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  1673. if (strcmp(code, "012289") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  1674. if (strcmp(code, "012290") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  1675. }
  1676. void RecordWarningCode(byte gunIndex, char *code)
  1677. {
  1678. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, code, 6);
  1679. if (strcmp(code, "012296") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  1680. if (strcmp(code, "012297") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  1681. if (strcmp(code, "012298") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  1682. }
  1683. void ReleaseAlarmCode(byte gunIndex)
  1684. {
  1685. bool isCleanCheck = false;
  1686. char code[7];
  1687. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1688. {
  1689. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL &&
  1690. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  1691. {
  1692. memcpy(code, "012234", 6);
  1693. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1694. isCleanCheck = true;
  1695. }
  1696. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL &&
  1697. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail == YES)
  1698. {
  1699. memcpy(code, "012289", 6);
  1700. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1701. isCleanCheck = true;
  1702. }
  1703. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES)
  1704. {
  1705. ReleaseWarningCodeByString(gunIndex, "012296");
  1706. }
  1707. }
  1708. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  1709. {
  1710. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL &&
  1711. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  1712. {
  1713. memcpy(code, "012236", 6);
  1714. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1715. isCleanCheck = true;
  1716. }
  1717. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL &&
  1718. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail == YES)
  1719. {
  1720. memcpy(code, "012290", 6);
  1721. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1722. isCleanCheck = true;
  1723. }
  1724. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES)
  1725. {
  1726. ReleaseWarningCodeByString(gunIndex, "012298");
  1727. }
  1728. }
  1729. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1730. {
  1731. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL &&
  1732. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  1733. {
  1734. memcpy(code, "012235", 6);
  1735. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1736. isCleanCheck = true;
  1737. }
  1738. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL &&
  1739. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail == YES)
  1740. {
  1741. memcpy(code, "012288", 6);
  1742. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1743. isCleanCheck = true;
  1744. }
  1745. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES)
  1746. {
  1747. ReleaseWarningCodeByString(gunIndex, "012297");
  1748. }
  1749. }
  1750. if (isCleanCheck)
  1751. {
  1752. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1753. {
  1754. if (index == gunIndex)
  1755. continue;
  1756. else
  1757. {
  1758. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, code, 6) != EQUAL)
  1759. {
  1760. if (strncmp(code, "012234", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  1761. if (strncmp(code, "012289", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  1762. if (strncmp(code, "012236", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  1763. if (strncmp(code, "012290", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  1764. if (strncmp(code, "012235", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  1765. if (strncmp(code, "012288", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  1766. }
  1767. }
  1768. }
  1769. }
  1770. }
  1771. void ReleaseWarningCodeByString(byte gunIndex, char *code)
  1772. {
  1773. bool isCleanCheck = false;
  1774. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1775. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES)
  1776. {
  1777. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1778. isCleanCheck = true;
  1779. }
  1780. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1781. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES)
  1782. {
  1783. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1784. isCleanCheck = true;
  1785. }
  1786. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1787. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES)
  1788. {
  1789. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1790. isCleanCheck = true;
  1791. }
  1792. if (isCleanCheck)
  1793. {
  1794. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1795. {
  1796. if (index == gunIndex)
  1797. continue;
  1798. else
  1799. {
  1800. if (strncmp((char *)chargingInfo[index]->ConnectorWarningCode, code, 6) != EQUAL)
  1801. {
  1802. if (strncmp(code, "012296", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  1803. if (strncmp(code, "012297", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  1804. if (strncmp(code, "012298", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  1805. }
  1806. }
  1807. }
  1808. }
  1809. }
  1810. //===============================================
  1811. // EmergencyStop and Charging Stop
  1812. //===============================================
  1813. void ChargingTerminalProcess(byte gunIndex)
  1814. {
  1815. setChargerMode(gunIndex, MODE_TERMINATING);
  1816. }
  1817. void AcChargingTerminalProcess()
  1818. {
  1819. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  1820. }
  1821. void StopChargingProcessByString(byte level)
  1822. {
  1823. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  1824. {
  1825. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  1826. }
  1827. }
  1828. void ReleaseChargingProcessByString(byte level)
  1829. {
  1830. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  1831. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  1832. }
  1833. // 一般錯誤停止充電處理函式
  1834. void BoardErrOccurByString(byte index, char *code)
  1835. {
  1836. byte level = 1;
  1837. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  1838. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1839. {
  1840. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  1841. {
  1842. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  1843. }
  1844. ChargingTerminalProcess(index);
  1845. }
  1846. StopChargingProcessByString(level);
  1847. }
  1848. // 急停狀況的停止充電處理函式
  1849. void EmcOccureByString(char *code)
  1850. {
  1851. byte level = 2;
  1852. // 嚴重的急停有四種 : EMC 按鈕、Mainbreak、Dooropen、SPD Trip
  1853. // 其錯誤等級為 2
  1854. if (strncmp(code, "012251", 6) == EQUAL || strncmp(code, "012252", 6) == EQUAL ||
  1855. strncmp(code, "012237", 6) == EQUAL || strncmp(code, "012238", 6) == EQUAL)
  1856. {
  1857. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  1858. {
  1859. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  1860. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1861. {
  1862. ChargingTerminalProcess(gun);
  1863. }
  1864. StopChargingProcessByString(level);
  1865. }
  1866. }
  1867. }
  1868. void ReleaseBoardErrOccurByString(byte index, char *code)
  1869. {
  1870. bool isTrigger = false;
  1871. byte level = 1;
  1872. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  1873. {
  1874. isTrigger = true;
  1875. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1876. }
  1877. if (isTrigger)
  1878. {
  1879. ReleaseChargingProcessByString(level);
  1880. }
  1881. }
  1882. void ReleaseEmsOccureByString(byte index, char *code)
  1883. {
  1884. bool isTrigger = false;
  1885. byte level = 2;
  1886. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  1887. {
  1888. isTrigger = true;
  1889. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  1890. }
  1891. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  1892. {
  1893. isTrigger = true;
  1894. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  1895. }
  1896. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  1897. {
  1898. isTrigger = true;
  1899. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  1900. }
  1901. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  1902. {
  1903. isTrigger = true;
  1904. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1905. }
  1906. if (isTrigger)
  1907. {
  1908. ReleaseChargingProcessByString(level);
  1909. }
  1910. }
  1911. //===============================================
  1912. // 確認硬體 (按鈕) 狀態
  1913. //===============================================
  1914. bool leftBtnPush = false;
  1915. bool rightBtnPush = false;
  1916. void ChkPrimaryStatus()
  1917. {
  1918. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  1919. {
  1920. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  1921. EmcOccureByString("012251");
  1922. }
  1923. else
  1924. ReleaseEmsOccureByString(0, "012251");
  1925. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  1926. {
  1927. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  1928. EmcOccureByString("012238");
  1929. }
  1930. else
  1931. ReleaseEmsOccureByString(0, "012238");
  1932. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  1933. {
  1934. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  1935. EmcOccureByString("012237");
  1936. }
  1937. else
  1938. ReleaseEmsOccureByString(0, "012237");
  1939. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  1940. {
  1941. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  1942. EmcOccureByString("012252");
  1943. }
  1944. else
  1945. ReleaseEmsOccureByString(0, "012252");
  1946. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  1947. {
  1948. if(!leftBtnPush)
  1949. {
  1950. leftBtnPush = true;
  1951. PRINTF_FUNC("left btn down............................... \n");
  1952. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1953. {
  1954. case S_IDLE:
  1955. {
  1956. if(isDetectPlugin())
  1957. {
  1958. _DetectPlugInTimeout();
  1959. StopSystemTimeoutDet();
  1960. }
  1961. }
  1962. break;
  1963. case S_REASSIGN_CHECK:
  1964. case S_REASSIGN:
  1965. case S_PREPARNING:
  1966. case S_PREPARING_FOR_EV:
  1967. case S_PREPARING_FOR_EVSE:
  1968. case S_CCS_PRECHARGE_ST0:
  1969. case S_CCS_PRECHARGE_ST1:
  1970. {
  1971. // 取消充電
  1972. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1973. }
  1974. break;
  1975. case S_CHARGING:
  1976. {
  1977. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  1978. {
  1979. // 停止充電
  1980. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1981. }
  1982. }
  1983. break;
  1984. case S_COMPLETE:
  1985. {
  1986. // 回 IDLE
  1987. //PRINTF_FUNC("right btn down.................S_COMPLETE \n");
  1988. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_IDLE;
  1989. }
  1990. break;
  1991. }
  1992. }
  1993. }
  1994. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  1995. {
  1996. if(leftBtnPush)
  1997. {
  1998. leftBtnPush = false;
  1999. PRINTF_FUNC("left btn up............................... \n");
  2000. }
  2001. }
  2002. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  2003. {
  2004. if(!rightBtnPush)
  2005. {
  2006. rightBtnPush = true;
  2007. PRINTF_FUNC("right btn down............................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2008. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2009. {
  2010. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2011. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2012. }
  2013. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2014. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)
  2015. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2016. else
  2017. {
  2018. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2019. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2020. }
  2021. }
  2022. }
  2023. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  2024. {
  2025. if(rightBtnPush)
  2026. {
  2027. rightBtnPush = false;
  2028. PRINTF_FUNC("right btn up............................... \n");
  2029. }
  2030. }
  2031. }
  2032. //===============================================
  2033. // 確認各小板偵測的錯誤狀況
  2034. //===============================================
  2035. void CheckErrorOccurStatus(byte index)
  2036. {
  2037. // 小板
  2038. if (chargingInfo[index]->Type == _Type_Chademo)
  2039. {
  2040. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
  2041. BoardErrOccurByString(index, "011012");
  2042. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  2043. BoardErrOccurByString(index, "012234");
  2044. }
  2045. else if (chargingInfo[index]->Type == _Type_GB)
  2046. {
  2047. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES)
  2048. BoardErrOccurByString(index, "011016");
  2049. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  2050. BoardErrOccurByString(index, "012236");
  2051. }
  2052. else if (chargingInfo[index]->Type == _Type_CCS_2)
  2053. {
  2054. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
  2055. BoardErrOccurByString(index, "011014");
  2056. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  2057. BoardErrOccurByString(index, "012235");
  2058. }
  2059. // RB
  2060. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2061. {
  2062. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2063. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2064. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2065. {
  2066. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2067. {
  2068. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2069. StopChargingProcessByString(2);
  2070. }
  2071. }
  2072. else
  2073. {
  2074. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2075. {
  2076. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2077. ReleaseChargingProcessByString(2);
  2078. }
  2079. }
  2080. }
  2081. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2082. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2083. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2084. {
  2085. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2086. {
  2087. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2088. StopChargingProcessByString(2);
  2089. }
  2090. }
  2091. else
  2092. {
  2093. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2094. {
  2095. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2096. ReleaseChargingProcessByString(2);
  2097. }
  2098. }
  2099. }
  2100. //===============================================
  2101. // 確認 GPIO 狀態
  2102. //===============================================
  2103. void gpio_set_value(unsigned int gpio, unsigned int value)
  2104. {
  2105. int fd;
  2106. char buf[MAX_BUF];
  2107. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2108. fd = open(buf, O_WRONLY);
  2109. if (fd < 0)
  2110. {
  2111. perror("gpio/set-value");
  2112. return;
  2113. }
  2114. if (value)
  2115. write(fd, "1", 2);
  2116. else
  2117. write(fd, "0", 2);
  2118. close(fd);
  2119. }
  2120. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2121. {
  2122. int fd;
  2123. char buf[MAX_BUF];
  2124. char ch;
  2125. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2126. fd = open(buf, O_RDONLY);
  2127. if (fd < 0){
  2128. perror("gpio/get-value");
  2129. return fd;
  2130. }
  2131. read(fd, &ch, 1);
  2132. if (ch != '0') {
  2133. *value = 1;
  2134. } else {
  2135. *value = 0;
  2136. }
  2137. close(fd);
  2138. return 0;
  2139. }
  2140. void CheckGunTypeFromHw()
  2141. {
  2142. int pinIn[4] = { 22, 23, 44, 45 };
  2143. unsigned int gpioValue = 0;
  2144. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2145. {
  2146. gpio_get_value(pinIn[i], &gpioValue);
  2147. switch (pinIn[i])
  2148. {
  2149. case 22:
  2150. bd1_1_status = gpioValue;
  2151. break;
  2152. case 23:
  2153. bd1_2_status = gpioValue;
  2154. break;
  2155. case 44:
  2156. bd0_1_status = gpioValue;
  2157. break;
  2158. case 45:
  2159. bd0_2_status = gpioValue;
  2160. break;
  2161. }
  2162. }
  2163. }
  2164. void CheckGpioInStatus()
  2165. {
  2166. int pinIn[2] = { 27, 47 };
  2167. unsigned int gpioValue = 0;
  2168. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2169. {
  2170. gpio_get_value(pinIn[i], &gpioValue);
  2171. if (gpioValue == 0x01)
  2172. {
  2173. switch(pinIn[i])
  2174. {
  2175. // 小板緊急停止
  2176. case 47:
  2177. {
  2178. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2179. {
  2180. if (chargingInfo[i]->slotsIndex == 1)
  2181. {
  2182. if (chargingInfo[i]->Type == _Type_Chademo)
  2183. BoardErrOccurByString(i, "023730");
  2184. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2185. BoardErrOccurByString(i, "013627");
  2186. break;
  2187. }
  2188. }
  2189. }
  2190. break;
  2191. case 27:
  2192. {
  2193. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2194. {
  2195. if (chargingInfo[i]->slotsIndex == 3)
  2196. {
  2197. if (chargingInfo[i]->Type == _Type_Chademo)
  2198. BoardErrOccurByString(i, "023730");
  2199. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2200. BoardErrOccurByString(i, "013627");
  2201. break;
  2202. }
  2203. }
  2204. }
  2205. break;
  2206. }
  2207. }
  2208. else
  2209. {
  2210. switch (pinIn[i])
  2211. {
  2212. // 小板解除緊急停止
  2213. case 47:
  2214. {
  2215. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2216. {
  2217. if (chargingInfo[i]->slotsIndex == 1)
  2218. {
  2219. if (chargingInfo[i]->Type == _Type_Chademo)
  2220. ReleaseBoardErrOccurByString(i, "023730");
  2221. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2222. ReleaseBoardErrOccurByString(i, "013627");
  2223. break;
  2224. }
  2225. }
  2226. }
  2227. break;
  2228. case 27:
  2229. {
  2230. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2231. {
  2232. if (chargingInfo[i]->slotsIndex == 3)
  2233. {
  2234. if (chargingInfo[i]->Type == _Type_Chademo)
  2235. ReleaseBoardErrOccurByString(i, "023730");
  2236. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2237. ReleaseBoardErrOccurByString(i, "013627");
  2238. break;
  2239. }
  2240. }
  2241. }
  2242. break;
  2243. }
  2244. }
  2245. }
  2246. }
  2247. //===============================================
  2248. // Main process
  2249. //===============================================
  2250. // 檢查 Byte 中某個 Bit 的值
  2251. // _byte : 欲改變的 byte
  2252. // _bit : 該 byte 的第幾個 bit
  2253. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2254. {
  2255. return ( _byte & mask_table[_bit] ) != 0x00;
  2256. }
  2257. // 設定 Byte 中某個 Bit的值
  2258. // _byte : 欲改變的 byte
  2259. // _bit : 該 byte 的第幾個 bit
  2260. // value : 修改的值為 0 or 1
  2261. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2262. {
  2263. if(value == 1)
  2264. *_byte |= (1 << _bit);
  2265. else if (value == 0)
  2266. *_byte ^= (1 << _bit);
  2267. }
  2268. void UserScanFunction()
  2269. {
  2270. bool idleReq = false;
  2271. unsigned char stopReq = 255;
  2272. // 當前非驗證的狀態
  2273. if(!IsAuthorizingMode())
  2274. {
  2275. // 先判斷現在是否可以提供刷卡
  2276. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  2277. // 2. 停止充電
  2278. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX)
  2279. {
  2280. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2281. return;
  2282. }
  2283. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2284. {
  2285. if (chargingInfo[i]->SystemStatus == S_CHARGING)
  2286. {
  2287. stopReq = i;
  2288. }
  2289. if ((chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable) == YES ||
  2290. (_acgunIndex > 0 && ac_chargingInfo[0]->SystemStatus == S_IDLE && ac_chargingInfo[0]->IsAvailable))
  2291. {
  2292. idleReq = true;
  2293. }
  2294. }
  2295. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2296. ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  2297. {
  2298. stopReq = DEFAULT_AC_INDEX;
  2299. }
  2300. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  2301. {
  2302. if (_acgunIndex > 0 && stopReq == DEFAULT_AC_INDEX && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  2303. {
  2304. char value[32];
  2305. PRINTF_FUNC("ac stop charging \n");
  2306. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  2307. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2308. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId,
  2309. ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  2310. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2311. {
  2312. AcChargingTerminalProcess();
  2313. }
  2314. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2315. }
  2316. else if (stopReq < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2317. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING &&
  2318. (_acgunIndex <= 0 || (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)))
  2319. {
  2320. char value[32];
  2321. PRINTF_FUNC("stop charging \n");
  2322. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  2323. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2324. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  2325. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  2326. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2327. {
  2328. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2329. }
  2330. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2331. }
  2332. else if (idleReq)
  2333. {
  2334. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  2335. stopReq != 255 &&
  2336. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2337. {
  2338. idleReq = false;
  2339. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2340. }
  2341. else if ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  2342. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_IDLE)
  2343. {
  2344. PRINTF_FUNC("// LCM => Authorizing \n");
  2345. // LCM => Authorizing
  2346. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  2347. // 進入確認卡號狀態
  2348. AuthorizingStart();
  2349. }
  2350. else
  2351. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2352. }
  2353. else
  2354. {
  2355. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2356. }
  2357. }
  2358. }
  2359. }
  2360. unsigned char isModeChange(unsigned char gun_index)
  2361. {
  2362. unsigned char result = NO;
  2363. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  2364. {
  2365. result = YES;
  2366. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  2367. }
  2368. return result;
  2369. }
  2370. void ScannerCardProcess()
  2371. {
  2372. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != 2 &&
  2373. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  2374. {
  2375. isCardScan = true;
  2376. // 處理刷卡及驗證卡號的動作
  2377. UserScanFunction();
  2378. }
  2379. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  2380. {
  2381. StartSystemTimeoutDet(Timeout_Authorizing);
  2382. // 確認驗證卡號完成沒
  2383. if (isAuthorizedComplete() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2384. {
  2385. StopSystemTimeoutDet();
  2386. // 判斷後台回覆狀態
  2387. if(canStartCharging() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2388. {
  2389. // LCM => Authorize complete
  2390. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2391. }
  2392. else
  2393. {
  2394. // LCM => Authorize fail
  2395. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2396. }
  2397. ClearAuthorizedFlag();
  2398. }
  2399. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  2400. {
  2401. // 白名單驗證
  2402. for (int i = 0; i < 10; i++)
  2403. {
  2404. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  2405. {
  2406. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  2407. {
  2408. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2409. ClearAuthorizedFlag();
  2410. break;
  2411. }
  2412. }
  2413. }
  2414. }
  2415. }
  2416. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2417. {
  2418. StartSystemTimeoutDet(Timeout_VerifyFail);
  2419. isCardScan = false;
  2420. }
  2421. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2422. {
  2423. StartSystemTimeoutDet(Timeout_VerifyComp);
  2424. }
  2425. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2426. {
  2427. StartSystemTimeoutDet(Timeout_WaitPlug);
  2428. }
  2429. else
  2430. isCardScan = false;
  2431. }
  2432. bool AddGunInfoByConnector(byte typeValue, byte slots)
  2433. {
  2434. bool result = true;
  2435. switch (typeValue)
  2436. {
  2437. case '0': // none
  2438. break;
  2439. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  2440. break;
  2441. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  2442. break;
  2443. case '3': // IEC 62196-2 Type 2 Plug
  2444. case '4': // IEC 62196-2 Type 2 Socket
  2445. if (AC_QUANTITY > _ac_Index)
  2446. {
  2447. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  2448. // AC 固定 index
  2449. ac_chargingInfo[_acgunIndex]->Index = 1;
  2450. ac_chargingInfo[_acgunIndex]->SystemStatus = S_BOOTING;
  2451. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  2452. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  2453. _ac_Index++;
  2454. _acgunIndex++;
  2455. }
  2456. else
  2457. result = false;
  2458. break;
  2459. case '5': // GB/T AC Plug
  2460. break;
  2461. case '6': // GB/T AC Socket
  2462. break;
  2463. case 'J': // CHAdeMO
  2464. {
  2465. if (CHAdeMO_QUANTITY > _chademoIndex)
  2466. {
  2467. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  2468. chargingInfo[_gunIndex]->Index = _gunIndex;
  2469. chargingInfo[_gunIndex]->slotsIndex = slots;
  2470. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2471. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  2472. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  2473. chargingInfo[_gunIndex]->IsAvailable = YES;
  2474. _chademoIndex++;
  2475. _gunIndex++;
  2476. }
  2477. else
  2478. result = false;
  2479. }
  2480. break;
  2481. case 'U': // CCS1 combo
  2482. case 'E': // CCS2 combo
  2483. {
  2484. if (CCS_QUANTITY > _ccsIndex)
  2485. {
  2486. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  2487. chargingInfo[_gunIndex]->Index = _gunIndex;
  2488. chargingInfo[_gunIndex]->slotsIndex = slots;
  2489. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2490. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  2491. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  2492. chargingInfo[_gunIndex]->IsAvailable = YES;
  2493. // 現階段預設為走 DIN70121
  2494. ShmCcsData->CommProtocol = 0x01;
  2495. _ccsIndex++;
  2496. _gunIndex++;
  2497. }
  2498. else
  2499. result = false;
  2500. }
  2501. break;
  2502. case 'G': // GBT DC
  2503. {
  2504. if (GB_QUANTITY > _gb_Index)
  2505. {
  2506. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  2507. chargingInfo[_gunIndex]->Index = _gunIndex;
  2508. chargingInfo[_gunIndex]->slotsIndex = slots;
  2509. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2510. chargingInfo[_gunIndex]->Type = _Type_GB;
  2511. chargingInfo[_gunIndex]->type_index = _gb_Index;
  2512. chargingInfo[_gunIndex]->IsAvailable = YES;
  2513. _gb_Index++;
  2514. _gunIndex++;
  2515. }
  2516. else
  2517. result = false;
  2518. }
  2519. break;
  2520. case 'D': // GBT DC x 2
  2521. break;
  2522. }
  2523. return result;
  2524. }
  2525. bool CheckConnectorTypeStatus()
  2526. {
  2527. bool result = true;
  2528. PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  2529. bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  2530. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  2531. {
  2532. byte slots = 1;
  2533. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  2534. {
  2535. if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  2536. {
  2537. return false;
  2538. }
  2539. slots++;
  2540. }
  2541. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  2542. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  2543. PRINTF_FUNC("DC _gunCount = %d, AC _gunCount = %d \n",
  2544. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  2545. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  2546. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  2547. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  2548. result = false;
  2549. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  2550. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2551. {
  2552. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  2553. {
  2554. // 與硬體相同 type : Chademo
  2555. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2556. {
  2557. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2558. }
  2559. }
  2560. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  2561. {
  2562. // 與硬體相同 type : CCS
  2563. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2564. {
  2565. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2566. }
  2567. }
  2568. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  2569. {
  2570. // 與硬體相同 type : GB
  2571. if (chargingInfo[gunIndex]->Type == _Type_GB)
  2572. {
  2573. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2574. }
  2575. }
  2576. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  2577. {
  2578. // 與硬體相同 type : Chademo
  2579. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2580. {
  2581. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2582. }
  2583. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2584. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2585. }
  2586. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  2587. {
  2588. // 與硬體相同 type : CCS
  2589. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2590. {
  2591. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2592. }
  2593. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2594. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2595. }
  2596. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  2597. {
  2598. // 與硬體相同 type : GB
  2599. if (chargingInfo[gunIndex]->Type == _Type_GB)
  2600. {
  2601. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2602. }
  2603. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2604. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2605. }
  2606. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  2607. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  2608. result = false;
  2609. }
  2610. }
  2611. else
  2612. {
  2613. // Module Name 不正確 - 告警
  2614. result = false;
  2615. }
  2616. return result;
  2617. }
  2618. void KillTask()
  2619. {
  2620. ChangeLcmByIndex(_LCM_FIX);
  2621. system("killall Module_EventLogging");
  2622. system("killall Module_PrimaryComm");
  2623. system("killall Module_EvComm");
  2624. system("killall Module_LcmControl");
  2625. system("killall Module_InternalComm");
  2626. system("killall Module_PsuComm");
  2627. system("killall OcppBackend &");
  2628. system("killall Module_4g &");
  2629. system("killall Module_Wifi &");
  2630. }
  2631. char CheckUpdateProcess()
  2632. {
  2633. DIR *d;
  2634. struct dirent *dir;
  2635. d = opendir("/mnt/");
  2636. if (d)
  2637. {
  2638. long int MaxLen=48*1024*1024, ImageLen = 0;
  2639. while ((dir = readdir(d)) != NULL)
  2640. {
  2641. char *new_str;
  2642. new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
  2643. new_str[0] = '\0';
  2644. strcat(new_str, "/mnt/");
  2645. strcat(new_str, dir->d_name);
  2646. int fd = open(new_str, O_RDONLY);
  2647. if (fd < 0)
  2648. {
  2649. return FAIL;
  2650. }
  2651. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2652. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2653. //get the image length
  2654. ImageLen = read(fd, ptr, MaxLen);
  2655. if (ImageLen > 20)
  2656. {
  2657. unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
  2658. PRINTF_FUNC("Typed...%x \r\n", Type);
  2659. switch (Type)
  2660. {
  2661. case CSU_BOOTLOADER:
  2662. case CSU_KERNEL_CONFIGURATION:
  2663. case CSU_KERNEL_IMAGE:
  2664. case CSU_ROOT_FILE_SYSTEM:
  2665. case CSU_USER_CONFIGURATION:
  2666. {
  2667. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2668. return PASS;
  2669. else
  2670. return FAIL;
  2671. }
  2672. break;
  2673. case CCS_BOARD_BOOTLOADER:
  2674. case CCS_BOARD_KERNEL_CONFIGURATION:
  2675. case CCS_BOARD_KERNEL_IMAGE:
  2676. case CCS_BOARD_FILE_SYSTEM:
  2677. {
  2678. bool isPass = true;
  2679. int CanFd = InitCanBus();
  2680. if (CanFd > 0)
  2681. {
  2682. for(byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2683. {
  2684. if (!isPass)
  2685. break;
  2686. if (chargingInfo[index]->Type == _Type_CCS_2)
  2687. {
  2688. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL)
  2689. {
  2690. isPass = false;
  2691. }
  2692. }
  2693. }
  2694. }
  2695. else
  2696. {
  2697. printf("Upgrade CCS open CAN FD fail.\n");
  2698. isPass = false;
  2699. }
  2700. return isPass;
  2701. }
  2702. break;
  2703. case CSU_PRIMARY_CONTROLLER:
  2704. case RELAY_CONTROL_BOARD:
  2705. case FAN_CONTROL_BOARD:
  2706. case AC_WALLMOUNT_CONTROLLER:
  2707. {
  2708. byte target = 0x00;
  2709. if (Type == CSU_PRIMARY_CONTROLLER)
  2710. target = UPGRADE_PRI;
  2711. else if (Type == RELAY_CONTROL_BOARD)
  2712. target = UPGRADE_RB;
  2713. else if (Type == FAN_CONTROL_BOARD)
  2714. target = UPGRADE_FAN;
  2715. else if (Type == AC_WALLMOUNT_CONTROLLER)
  2716. target = UPGRADE_AC;
  2717. int fd = InitComPort(target);
  2718. if (Upgrade_UART(fd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2719. return PASS;
  2720. else
  2721. return FAIL;
  2722. close(fd);
  2723. }
  2724. break;
  2725. case CHAdeMO_BOARD:
  2726. case GB_BOARD:
  2727. {
  2728. bool isPass = true;
  2729. int CanFd = InitCanBus();
  2730. if (CanFd > 0)
  2731. {
  2732. for(byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2733. {
  2734. if (!isPass)
  2735. break;
  2736. if ((Type == CHAdeMO_BOARD && chargingInfo[index]->Type == _Type_Chademo) ||
  2737. (Type == GB_BOARD && chargingInfo[index]->Type == _Type_GB))
  2738. {
  2739. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2740. return PASS;
  2741. else
  2742. return FAIL;
  2743. }
  2744. }
  2745. }
  2746. else
  2747. {
  2748. PRINTF_FUNC("Upgrad FD fail. \n");
  2749. isPass = false;
  2750. }
  2751. return isPass;
  2752. break;
  2753. }
  2754. }
  2755. }
  2756. free(new_str);
  2757. free(ptr);
  2758. }
  2759. }
  2760. free(dir);
  2761. closedir(d);
  2762. return FAIL;
  2763. }
  2764. void CreateRfidFork()
  2765. {
  2766. pid_t rfidRecPid;
  2767. rfidRecPid = fork();
  2768. if (rfidRecPid == 0)
  2769. {
  2770. while(true)
  2771. {
  2772. // 刷卡判斷
  2773. RFID rfid;
  2774. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  2775. !ShmSysConfigAndInfo->SysConfig.isRFID)
  2776. {}
  2777. else if(getRequestCardSN(rfidFd, 0, &rfid))
  2778. {
  2779. PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  2780. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  2781. {
  2782. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  2783. {
  2784. switch (rfid.snType)
  2785. {
  2786. case RFID_SN_TYPE_6BYTE:
  2787. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2788. "%02X%02X%02X%02X%02X%02X",
  2789. rfid.currentCard[0], rfid.currentCard[1],
  2790. rfid.currentCard[2], rfid.currentCard[3],
  2791. rfid.currentCard[4], rfid.currentCard[5]);
  2792. break;
  2793. case RFID_SN_TYPE_7BYTE:
  2794. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2795. "%02X%02X%02X%02X%02X%02X%02X",
  2796. rfid.currentCard[0], rfid.currentCard[1],
  2797. rfid.currentCard[2], rfid.currentCard[3],
  2798. rfid.currentCard[4], rfid.currentCard[5],
  2799. rfid.currentCard[6]);
  2800. break;
  2801. case RFID_SN_TYPE_10BYTE:
  2802. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2803. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  2804. rfid.currentCard[0], rfid.currentCard[1],
  2805. rfid.currentCard[2], rfid.currentCard[3],
  2806. rfid.currentCard[4], rfid.currentCard[5],
  2807. rfid.currentCard[6], rfid.currentCard[7],
  2808. rfid.currentCard[8], rfid.currentCard[9]);
  2809. break;
  2810. case RFID_SN_TYPE_4BYTE:
  2811. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2812. "%02X%02X%02X%02X",
  2813. rfid.currentCard[0], rfid.currentCard[1],
  2814. rfid.currentCard[2], rfid.currentCard[3]);
  2815. break;
  2816. }
  2817. }
  2818. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  2819. {
  2820. switch (rfid.snType)
  2821. {
  2822. case RFID_SN_TYPE_6BYTE:
  2823. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2824. "%02X%02X%02X%02X%02X%02X",
  2825. rfid.currentCard[5], rfid.currentCard[4],
  2826. rfid.currentCard[3], rfid.currentCard[2],
  2827. rfid.currentCard[1], rfid.currentCard[0]);
  2828. break;
  2829. case RFID_SN_TYPE_7BYTE:
  2830. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2831. "%02X%02X%02X%02X%02X%02X%02X",
  2832. rfid.currentCard[6], rfid.currentCard[5],
  2833. rfid.currentCard[4], rfid.currentCard[3],
  2834. rfid.currentCard[2], rfid.currentCard[1],
  2835. rfid.currentCard[0]);
  2836. break;
  2837. case RFID_SN_TYPE_10BYTE:
  2838. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2839. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  2840. rfid.currentCard[9], rfid.currentCard[8],
  2841. rfid.currentCard[7], rfid.currentCard[6],
  2842. rfid.currentCard[5], rfid.currentCard[4],
  2843. rfid.currentCard[3], rfid.currentCard[2],
  2844. rfid.currentCard[1], rfid.currentCard[0]);
  2845. break;
  2846. case RFID_SN_TYPE_4BYTE:
  2847. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2848. "%02X%02X%02X%02X",
  2849. rfid.currentCard[3], rfid.currentCard[2],
  2850. rfid.currentCard[1], rfid.currentCard[0]);
  2851. break;
  2852. }
  2853. }
  2854. PRINTF_FUNC("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2855. }
  2856. }
  2857. sleep(1);
  2858. }
  2859. }
  2860. }
  2861. void StartSystemTimeoutDet(unsigned char flag)
  2862. {
  2863. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  2864. {
  2865. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  2866. }
  2867. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  2868. }
  2869. void StopSystemTimeoutDet()
  2870. {
  2871. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  2872. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  2873. }
  2874. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  2875. {
  2876. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2877. {
  2878. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  2879. {
  2880. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  2881. }
  2882. chargingInfo[gunIndex]->TimeoutFlag = flag;
  2883. }
  2884. }
  2885. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  2886. {
  2887. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2888. {
  2889. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  2890. }
  2891. }
  2892. void CreateTimeoutFork()
  2893. {
  2894. pid_t timeoutPid;
  2895. timeoutPid = fork();
  2896. if (timeoutPid > 0)
  2897. {
  2898. while(true)
  2899. {
  2900. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  2901. // 系統
  2902. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  2903. {
  2904. case Timeout_SelftestChk:
  2905. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2906. {
  2907. _SelfTestTimeout();
  2908. StopSystemTimeoutDet();
  2909. }
  2910. break;
  2911. case Timeout_Authorizing:
  2912. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 30000000)
  2913. {
  2914. _AuthorizedTimeout();
  2915. StopSystemTimeoutDet();
  2916. }
  2917. break;
  2918. case Timeout_VerifyFail:
  2919. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2920. {
  2921. _AutoReturnTimeout();
  2922. StopSystemTimeoutDet();
  2923. }
  2924. break;
  2925. case Timeout_VerifyComp:
  2926. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2927. {
  2928. _AutoReturnTimeout();
  2929. StopSystemTimeoutDet();
  2930. }
  2931. break;
  2932. case Timeout_WaitPlug:
  2933. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2934. {
  2935. _DetectPlugInTimeout();
  2936. StopSystemTimeoutDet();
  2937. }
  2938. break;
  2939. case Timeout_ReturnToChargingGunDet:
  2940. {
  2941. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 30000000)
  2942. {
  2943. DisplayChargingInfo();
  2944. StopSystemTimeoutDet();
  2945. }
  2946. }
  2947. break;
  2948. }
  2949. // 各槍
  2950. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2951. {
  2952. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  2953. switch(chargingInfo[gun_index]->TimeoutFlag)
  2954. {
  2955. case Timeout_Preparing:
  2956. {
  2957. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 30000000)
  2958. {
  2959. _PrepareTimeout(gun_index);
  2960. StopGunInfoTimeoutDet(gun_index);
  2961. }
  2962. }
  2963. break;
  2964. case Timeout_EvChargingDet:
  2965. {
  2966. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 120000000)
  2967. {
  2968. _DetectEvChargingEnableTimeout(gun_index);
  2969. StopGunInfoTimeoutDet(gun_index);
  2970. }
  2971. }
  2972. break;
  2973. case Timeout_EvseChargingDet:
  2974. {
  2975. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2976. {
  2977. _DetectEvseChargingEnableTimeout(gun_index);
  2978. StopGunInfoTimeoutDet(gun_index);
  2979. }
  2980. }
  2981. break;
  2982. case Timeout_EvseCompleteDet:
  2983. {
  2984. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  2985. {
  2986. StopGunInfoTimeoutDet(gun_index);
  2987. }
  2988. }
  2989. break;
  2990. case Timeout_ForCcsPrechargeDet:
  2991. {
  2992. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2993. {
  2994. _CcsPrechargeTimeout(gun_index);
  2995. StopGunInfoTimeoutDet(gun_index);
  2996. }
  2997. }
  2998. break;
  2999. }
  3000. }
  3001. sleep(1);
  3002. }
  3003. }
  3004. }
  3005. void GetSystemTime()
  3006. {
  3007. struct timeb csuTime;
  3008. struct tm *tmCSU;
  3009. ftime(&csuTime);
  3010. tmCSU = localtime(&csuTime.time);
  3011. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3012. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3013. tmCSU->tm_sec);
  3014. // byte date[14];
  3015. //
  3016. //
  3017. // //sprintf(&date, "%d", );
  3018. //
  3019. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  3020. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  3021. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  3022. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  3023. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  3024. //
  3025. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  3026. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  3027. //
  3028. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  3029. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  3030. //
  3031. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  3032. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  3033. //
  3034. // date[10] = (tmCSU->tm_min) / 10 % 10;
  3035. // date[11] = (tmCSU->tm_min) / 1 % 10;
  3036. //
  3037. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  3038. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  3039. // PRINTF_FUNC("%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x \n", date[0], date[1], date[2], date[3],
  3040. // date[4], date[5], date[6], date[7],
  3041. // date[8], date[9], date[10], date[11],
  3042. // date[12], date[13]);
  3043. }
  3044. void CheckFactoryConfigFunction()
  3045. {
  3046. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3047. {
  3048. system("cd /root;./FactoryConfig -m");
  3049. system("sync");
  3050. sleep(5);
  3051. system("reboot -f");
  3052. sleep(5);
  3053. system("reboot -f");
  3054. }
  3055. }
  3056. void CheckFwUpdateFunction()
  3057. {
  3058. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  3059. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  3060. {
  3061. DEBUG_INFO_MSG("ftp : update start. \n");
  3062. KillTask();
  3063. if (CheckUpdateProcess() == PASS)
  3064. DEBUG_INFO_MSG("ftp : update complete. \n");
  3065. else
  3066. DEBUG_INFO_MSG("ftp : update fail. \n");
  3067. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  3068. sleep(5);
  3069. system("reboot -f");
  3070. }
  3071. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  3072. {
  3073. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  3074. }
  3075. else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  3076. {
  3077. DEBUG_INFO_MSG("Backend : update start. \n");
  3078. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3079. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3080. KillTask();
  3081. if (CheckUpdateProcess() == PASS)
  3082. {
  3083. DEBUG_INFO_MSG("Backend : update complete. \n");
  3084. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3085. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3086. }
  3087. else
  3088. {
  3089. DEBUG_INFO_MSG("Backend : update fail. \n");
  3090. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3091. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3092. }
  3093. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3094. sleep(5);
  3095. system("reboot -f");
  3096. }
  3097. else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed") == EQUAL)
  3098. {
  3099. DEBUG_ERROR_MSG("Backend to download file fail. \n");
  3100. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3101. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3102. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3103. }
  3104. }
  3105. //===============================================
  3106. // Check reservation date is expired
  3107. //===============================================
  3108. int isReservationExpired(unsigned char gun_index)
  3109. {
  3110. int result = NO;
  3111. struct tm expiredDate;
  3112. struct timeb expiredTime;
  3113. if (sscanf((char*) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  3114. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  3115. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  3116. &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  3117. {
  3118. expiredDate.tm_year -= 1900;
  3119. expiredDate.tm_mon -= 1;
  3120. expiredTime.time = mktime(&expiredDate);
  3121. if (!CheckTimeOut(expiredTime))
  3122. {
  3123. result = YES;
  3124. }
  3125. }
  3126. return result;
  3127. }
  3128. //===============================================
  3129. // OCPP
  3130. //===============================================
  3131. void CheckOcppStatus()
  3132. {
  3133. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  3134. {
  3135. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = YES;
  3136. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  3137. }
  3138. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
  3139. {
  3140. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  3141. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  3142. {
  3143. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  3144. sleep(3);
  3145. system("reboot -f");
  3146. }
  3147. else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  3148. {
  3149. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  3150. sleep(3);
  3151. KillTask();
  3152. system("/usr/bin/run_evse_restart.sh");
  3153. }
  3154. }
  3155. }
  3156. void OcppStartTransation(byte gunIndex)
  3157. {
  3158. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  3159. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  3160. else
  3161. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3162. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  3163. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionReq = YES;
  3164. }
  3165. void OcppStopTransation(byte gunIndex)
  3166. {
  3167. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  3168. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  3169. else
  3170. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3171. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  3172. ShmOCPP16Data->CpMsg.bits[gunIndex].StopTransactionReq = YES;
  3173. }
  3174. bool OcppRemoteStop(byte gunIndex)
  3175. {
  3176. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  3177. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  3178. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  3179. return result;
  3180. }
  3181. void OcppRemoteStartChk()
  3182. {
  3183. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3184. {}
  3185. else if(!isDetectPlugin())
  3186. {
  3187. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3188. {
  3189. if ((chargingInfo[gun_index]->SystemStatus == S_IDLE || chargingInfo[gun_index]->SystemStatus == S_RESERVATION)&&
  3190. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES)
  3191. {
  3192. ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  3193. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = NO;
  3194. DetectPluginStart();
  3195. break;
  3196. }
  3197. }
  3198. }
  3199. }
  3200. void ChkOcppStatus(byte gunIndex)
  3201. {
  3202. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  3203. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES)
  3204. {
  3205. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  3206. if (isReservationExpired(gunIndex))
  3207. {
  3208. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3209. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3210. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  3211. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  3212. }
  3213. }
  3214. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  3215. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES)
  3216. {
  3217. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  3218. if (isReservationExpired(gunIndex))
  3219. {
  3220. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3221. DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3222. chargingInfo[gunIndex]->ReservationId = 0;
  3223. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  3224. }
  3225. }
  3226. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES)
  3227. {
  3228. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3229. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3230. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  3231. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL)
  3232. chargingInfo[gunIndex]->IsAvailable = YES;
  3233. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL)
  3234. chargingInfo[gunIndex]->IsAvailable = NO;
  3235. }
  3236. }
  3237. bool CheckBackendChargingTimeout(byte gunIndex)
  3238. {
  3239. bool result = false;
  3240. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3241. {
  3242. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  3243. {
  3244. if (chargingInfo[gunIndex]->RemainChargingDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  3245. result = true;
  3246. }
  3247. }
  3248. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3249. {
  3250. // 隨插即充電的要看 offline
  3251. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  3252. {
  3253. if (chargingInfo[gunIndex]->RemainChargingDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  3254. result = true;
  3255. }
  3256. }
  3257. return result;
  3258. }
  3259. bool CheckBackendChargingEnergy(byte gunIndex)
  3260. {
  3261. bool result = false;
  3262. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3263. {
  3264. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  3265. {
  3266. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  3267. result = true;
  3268. }
  3269. }
  3270. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3271. {
  3272. // 隨插即充電的要看 offline
  3273. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  3274. {
  3275. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  3276. result = true;
  3277. }
  3278. }
  3279. return result;
  3280. }
  3281. //===============================================
  3282. // Config process
  3283. //===============================================
  3284. void AddPlugInTimes(byte gunIndex)
  3285. {
  3286. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3287. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  3288. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  3289. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  3290. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  3291. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  3292. }
  3293. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  3294. {
  3295. char cmdBuf[32];
  3296. struct timeb csuTime;
  3297. struct tm *tmCSU;
  3298. ftime(&csuTime);
  3299. tmCSU = localtime(&csuTime.time);
  3300. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  3301. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3302. tmCSU->tm_sec);
  3303. if (isStart)
  3304. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  3305. else
  3306. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  3307. }
  3308. void zipLogFiles()
  3309. {
  3310. const char* logPath = "/Storage/SystemLog";
  3311. // 獲取目錄
  3312. DIR* pDir = opendir(logPath);
  3313. if (pDir != NULL)
  3314. {
  3315. struct timeb csuTime;
  3316. struct tm *tmCSU;
  3317. ftime(&csuTime);
  3318. tmCSU = localtime(&csuTime.time);
  3319. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3320. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3321. // tmCSU->tm_sec);
  3322. // Read items inside the folder
  3323. struct dirent* pEntry = NULL;
  3324. while ((pEntry = readdir(pDir)) != NULL)
  3325. {
  3326. if (strcmp(pEntry->d_name, ".") != 0 &&
  3327. strcmp(pEntry->d_name, "..") != 0 &&
  3328. strncmp(pEntry->d_name, "[", 1) == 0 &&
  3329. strstr(pEntry->d_name, "tar") < 0)
  3330. {
  3331. char yearC[5];
  3332. unsigned short year = 0;
  3333. char monthC[3];
  3334. unsigned short month = 0;
  3335. yearC[4] = '\0';
  3336. strncpy(yearC, pEntry->d_name + 1, 4);
  3337. monthC[2] = '\0';
  3338. strncpy(monthC, pEntry->d_name + 6, 2);
  3339. year = atoi(yearC);
  3340. month = atoi(monthC);
  3341. if (year != 0)
  3342. {
  3343. if (year < tmCSU->tm_year + 1900 ||
  3344. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  3345. {
  3346. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  3347. char file[256];
  3348. memset(file, 0x00, sizeof(file));
  3349. strcat(file, "tar zcvf ");
  3350. strcat(file, logPath);
  3351. strncat(file, "/", 1);
  3352. strcat(file, pEntry->d_name);
  3353. strcat(file, ".tar");
  3354. strncat(file, " ", 1);
  3355. strcat(file, logPath);
  3356. strncat(file, "/", 1);
  3357. strcat(file, pEntry->d_name);
  3358. PRINTF_FUNC("zip = %s \n", file);
  3359. system(file);
  3360. }
  3361. }
  3362. }
  3363. }
  3364. }
  3365. // Close folder
  3366. closedir(pDir);
  3367. }
  3368. void ChangeGunSelectByIndex(byte sel)
  3369. {
  3370. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  3371. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  3372. }
  3373. void CheckIsAlternatvieByModelName()
  3374. {
  3375. // 黑白機 ?
  3376. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UT1PH") == EQUAL ||
  3377. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0ET1PH") == EQUAL)
  3378. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  3379. else
  3380. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  3381. }
  3382. void StopProcessingLoop()
  3383. {
  3384. for (;;)
  3385. {
  3386. CheckFactoryConfigFunction();
  3387. CheckFwUpdateFunction();
  3388. sleep(1);
  3389. }
  3390. }
  3391. void CreateWatchdog()
  3392. {
  3393. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  3394. {
  3395. wtdFd = InitWatchDog();
  3396. if (wtdFd < 0)
  3397. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  3398. }
  3399. }
  3400. bool IsConnectorWholeIdle()
  3401. {
  3402. bool result = true;
  3403. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  3404. {
  3405. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  3406. chargingInfo[count]->SystemStatus != S_RESERVATION)
  3407. {
  3408. result = false;
  3409. break;
  3410. }
  3411. }
  3412. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  3413. {
  3414. if (ac_chargingInfo[count]->SystemStatus != S_IDLE &&
  3415. ac_chargingInfo[count]->IsErrorOccur == NO)
  3416. {
  3417. result = false;
  3418. break;
  3419. }
  3420. }
  3421. return result;
  3422. }
  3423. void ClearAlarmCodeWhenAcOff()
  3424. {
  3425. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  3426. {
  3427. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  3428. }
  3429. }
  3430. int main(void)
  3431. {
  3432. if(CreateShareMemory() == 0)
  3433. {
  3434. #ifdef SystemLogMessage
  3435. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  3436. #endif
  3437. if(ShmStatusCodeData!=NULL)
  3438. {
  3439. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  3440. }
  3441. return 0;
  3442. sleep(5);
  3443. system("reboot -f");
  3444. sleep(5);
  3445. system("reboot -f");
  3446. }
  3447. PRINTF_FUNC("Chademo = %d, CCS = %d, GB = %d, AC = %d \n",
  3448. CHAdeMO_QUANTITY, CCS_QUANTITY, GB_QUANTITY, AC_QUANTITY);
  3449. PRINTF_FUNC("\n"); PRINTF_FUNC("Initial SystemConfig and Info.......\n");
  3450. if (!InitialSystemDefaultConfig())
  3451. {
  3452. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  3453. StopProcessingLoop();
  3454. }
  3455. PRINTF_FUNC("CheckConnectorTypeStatus. \n");
  3456. CheckGunTypeFromHw();
  3457. PRINTF_FUNC("CheckIsAlternatvieByModelName. \n");
  3458. CheckIsAlternatvieByModelName();
  3459. PRINTF_FUNC("ModelName = %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  3460. PRINTF_FUNC("InitialShareMemoryInfo \n");
  3461. InitialShareMemoryInfo();
  3462. PRINTF_FUNC("ChargerType (IEC or UL) = %d \n", ShmSysConfigAndInfo->SysInfo.ChargerType);
  3463. ChangeLcmByIndex(_LCM_INIT);
  3464. if (!CheckConnectorTypeStatus())
  3465. isModelNameMatch = false;
  3466. Initialization();
  3467. PRINTF_FUNC("Spawn all Task. \n");
  3468. SpawnTask();
  3469. if (!isModelNameMatch)
  3470. {
  3471. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  3472. ChangeLcmByIndex(_LCM_FIX);
  3473. // Module Name 與硬體對應不正確
  3474. PRINTF_FUNC("Module Name & HW info none match. \n");
  3475. DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
  3476. sleep(3);
  3477. KillTask();
  3478. StopProcessingLoop();
  3479. }
  3480. PRINTF_FUNC("Module Name & HW info correct. Initialize.......\n");
  3481. CreateTimeoutFork();
  3482. PRINTF_FUNC("Self test. \n");
  3483. SelfTestRun();
  3484. StopSystemTimeoutDet();
  3485. PRINTF_FUNC("SelfTestSeq = %d, Work_Step = %d \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
  3486. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  3487. ShmPsuData->Work_Step == _NO_WORKING)
  3488. {
  3489. DisplaySelfTestFailReason();
  3490. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3491. {
  3492. setChargerMode(gun_index, MODE_ALARM);
  3493. }
  3494. ChangeLcmByIndex(_LCM_FIX);
  3495. sleep(3);
  3496. KillTask();
  3497. StopProcessingLoop();
  3498. }
  3499. else
  3500. {
  3501. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3502. {
  3503. setChargerMode(gun_index, MODE_IDLE);
  3504. }
  3505. }
  3506. ChangeLcmByIndex(_LCM_IDLE);
  3507. sleep(1);
  3508. //***** 須新增的偵測 *****//
  3509. // 1. Thernal - 控制風扇轉速
  3510. // 2. ouput fuse - 控制風扇轉速
  3511. CreateRfidFork();
  3512. // Create Watchdog
  3513. //CreateWatchdog();
  3514. // Main loop
  3515. PRINTF_FUNC("Main Loop. \n");
  3516. for (;;)
  3517. {
  3518. CheckOcppStatus();
  3519. ChkPrimaryStatus();
  3520. if ((IsConnectorWholeIdle() || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) &&
  3521. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  3522. {
  3523. CheckFactoryConfigFunction();
  3524. CheckFwUpdateFunction();
  3525. }
  3526. // OCPP 邏輯
  3527. OcppRemoteStartChk();
  3528. // 讀卡邏輯
  3529. ScannerCardProcess();
  3530. // 當 AC 沒有搭上時,清除一些錯誤碼
  3531. ClearAlarmCodeWhenAcOff();
  3532. if (_acgunIndex > 0 && isDetectPlugin() && !isCardScan)
  3533. {
  3534. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  3535. }
  3536. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3537. {
  3538. CheckGpioInStatus();
  3539. CheckErrorOccurStatus(gun_index);
  3540. ChkOcppStatus(gun_index);
  3541. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  3542. switch(chargingInfo[gun_index]->SystemStatus)
  3543. {
  3544. case S_IDLE:
  3545. case S_RESERVATION:
  3546. {
  3547. if (chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  3548. isModeChange(gun_index))
  3549. {
  3550. PRINTF_FUNC("S_IDLE================================== %x \n", gun_index);
  3551. chargingInfo[gun_index]->RemainChargingDuration = 0;
  3552. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  3553. }
  3554. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION &&
  3555. isModeChange(gun_index))
  3556. {
  3557. PRINTF_FUNC("S_RESERVATION....................%x \n", gun_index);
  3558. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  3559. }
  3560. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  3561. {
  3562. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  3563. {
  3564. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  3565. }
  3566. ClearDetectPluginFlag();
  3567. }
  3568. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3569. {/* 不給充電 */}
  3570. else
  3571. {
  3572. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_FIX)
  3573. {
  3574. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  3575. }
  3576. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  3577. {
  3578. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  3579. {
  3580. // 均充 -> 最大充
  3581. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  3582. {
  3583. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  3584. {
  3585. PRINTF_FUNC("=============Smart Charging============= Step 11 \n");
  3586. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  3587. }
  3588. }
  3589. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  3590. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  3591. {
  3592. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  3593. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  3594. }
  3595. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  3596. {
  3597. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3598. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3599. }
  3600. }
  3601. else
  3602. {
  3603. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3604. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3605. }
  3606. }
  3607. else
  3608. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3609. { // Idle 正常程序起點
  3610. // 判斷是否有啟用檢查插槍
  3611. if(isDetectPlugin())
  3612. {
  3613. // 卡號驗證成功後,等待充電槍插入充電車
  3614. if ((ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL &&
  3615. ShmSysConfigAndInfo->SysInfo.OrderCharging < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount))
  3616. {
  3617. if (chargingInfo[(unsigned char)ShmSysConfigAndInfo->SysInfo.OrderCharging]->ConnectorPlugIn == YES &&
  3618. chargingInfo[gun_index]->IsAvailable)
  3619. {
  3620. PRINTF_FUNC("-----------------1----------------- %d \n", ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3621. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3622. AddPlugInTimes(ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3623. setChargerMode(ShmSysConfigAndInfo->SysInfo.OrderCharging, MODE_REASSIGN_CHECK);
  3624. ClearDetectPluginFlag();
  3625. continue;
  3626. }
  3627. }
  3628. else
  3629. {
  3630. if (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable &&
  3631. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  3632. {
  3633. PRINTF_FUNC("-----------------2----------------- \n");
  3634. ChangeGunSelectByIndex(gun_index);
  3635. AddPlugInTimes(gun_index);
  3636. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  3637. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  3638. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3639. // 當前操作的槍號,進入 Preparing
  3640. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  3641. ClearDetectPluginFlag();
  3642. continue;
  3643. }
  3644. }
  3645. if (!isCardScan)
  3646. {
  3647. // LCM => Waiting for plugging
  3648. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  3649. }
  3650. }
  3651. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  3652. {
  3653. if (!isReservationExpired(gun_index))
  3654. {
  3655. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  3656. }
  3657. }
  3658. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  3659. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  3660. {
  3661. PRINTF_FUNC("-----------------3----------------- \n");
  3662. ChangeGunSelectByIndex(gun_index);
  3663. AddPlugInTimes(gun_index);
  3664. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  3665. ClearDetectPluginFlag();
  3666. continue;
  3667. }
  3668. else
  3669. {
  3670. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3671. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  3672. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  3673. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  3674. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  3675. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  3676. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  3677. {
  3678. bool isReturnTimeout = false;
  3679. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  3680. {
  3681. if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected ||
  3682. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  3683. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX))
  3684. {
  3685. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  3686. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  3687. {
  3688. isReturnTimeout = true;
  3689. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3690. }
  3691. }
  3692. }
  3693. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  3694. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  3695. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)
  3696. {
  3697. isReturnTimeout = true;
  3698. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3699. }
  3700. if (!isReturnTimeout)
  3701. StopSystemTimeoutDet();
  3702. }
  3703. }
  3704. } // Idle 正常程序終點
  3705. }
  3706. ReleaseAlarmCode(gun_index);
  3707. }
  3708. break;
  3709. case S_REASSIGN_CHECK:
  3710. {
  3711. if (isModeChange(gun_index))
  3712. {
  3713. PRINTF_FUNC("S_REASSIGN_CHECK================================== %x \n", gun_index);
  3714. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  3715. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL)
  3716. ShmSysConfigAndInfo->SysInfo.OrderCharging = FAIL;
  3717. StopSystemTimeoutDet();
  3718. }
  3719. bool isRessign = false;
  3720. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  3721. {
  3722. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  3723. {
  3724. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3725. {
  3726. // 有其他槍已經分配好 psu 模塊
  3727. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  3728. chargingInfo[index]->SystemStatus >= S_PREPARNING)
  3729. {
  3730. PRINTF_FUNC("=============Smart Charging============= Step 1 \n");
  3731. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  3732. isRessign = true;
  3733. break;
  3734. }
  3735. }
  3736. }
  3737. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  3738. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
  3739. {
  3740. // 如果在切換最大充的過程中,需等待最大充切換完成後,在走均充流程
  3741. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  3742. {
  3743. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  3744. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  3745. {
  3746. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  3747. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  3748. }
  3749. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  3750. {
  3751. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3752. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3753. }
  3754. }
  3755. continue;
  3756. }
  3757. }
  3758. if (isRessign)
  3759. setChargerMode(gun_index, MODE_REASSIGN);
  3760. else
  3761. setChargerMode(gun_index, MODE_PRECHARGE);
  3762. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3763. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3764. }
  3765. break;
  3766. case S_REASSIGN:
  3767. {
  3768. if (isModeChange(gun_index))
  3769. {
  3770. PRINTF_FUNC("S_REASSIGN================================== %x \n", gun_index);
  3771. }
  3772. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  3773. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  3774. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  3775. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  3776. {
  3777. setChargerMode(gun_index, MODE_PRECHARGE);
  3778. }
  3779. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  3780. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  3781. {
  3782. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  3783. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  3784. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3785. }
  3786. //PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
  3787. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3788. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3789. }
  3790. break;
  3791. case S_PREPARNING:
  3792. {
  3793. if (isModeChange(gun_index))
  3794. {
  3795. PRINTF_FUNC("S_PREPARNING================================== %x \n", gun_index);
  3796. StopGunInfoTimeoutDet(gun_index);
  3797. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  3798. }
  3799. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10)
  3800. {
  3801. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  3802. }
  3803. if (isEvBoardStopChargeFlag(gun_index) == YES || OcppRemoteStop(gun_index) == YES)
  3804. {
  3805. // 板端或後臺要求停止
  3806. ChargingTerminalProcess(gun_index);
  3807. }
  3808. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3809. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3810. }
  3811. break;
  3812. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  3813. {
  3814. if (isModeChange(gun_index))
  3815. {
  3816. PRINTF_FUNC("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  3817. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3818. StopGunInfoTimeoutDet(gun_index);
  3819. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  3820. }
  3821. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3822. {
  3823. // 檢查車端的槍鎖是否為鎖上
  3824. if (isEvGunLocked_chademo(gun_index) == YES)
  3825. {
  3826. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3827. }
  3828. }
  3829. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3830. {
  3831. // 檢查車端的 charging enable 是否為 1
  3832. if (isEvGunLocked_gb(gun_index) == YES)
  3833. {
  3834. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3835. }
  3836. }
  3837. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3838. {
  3839. // 檢查車端的 charging enable 是否為 1
  3840. if (isEvGunLocked_ccs(gun_index) == YES)
  3841. {
  3842. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3843. }
  3844. }
  3845. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3846. OcppRemoteStop(gun_index) == YES)
  3847. {
  3848. // 板端或後臺要求停止
  3849. ChargingTerminalProcess(gun_index);
  3850. }
  3851. // LCM => Pre-charging
  3852. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3853. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3854. }
  3855. break;
  3856. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  3857. {
  3858. if (isModeChange(gun_index))
  3859. {
  3860. PRINTF_FUNC("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  3861. StopGunInfoTimeoutDet(gun_index);
  3862. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  3863. }
  3864. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3865. {
  3866. // 檢查樁端的 GFD 結果
  3867. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
  3868. {
  3869. // 當前操作的槍號,進入 Charging
  3870. setChargerMode(gun_index, MODE_CHARGING);
  3871. }
  3872. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3873. {
  3874. // GFD 錯誤停止
  3875. RecordAlarmCode(gun_index, "012234");
  3876. }
  3877. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3878. {
  3879. // GFD 警告
  3880. RecordWarningCode(gun_index, "012296");
  3881. }
  3882. }
  3883. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3884. {
  3885. // 檢查樁端的 GFD 結果
  3886. if (isPrechargeStatus_gb(gun_index) > 5 && isPrechargeStatus_gb(gun_index) < 9)
  3887. {
  3888. setChargerMode(gun_index, MODE_CHARGING);
  3889. }
  3890. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3891. {
  3892. // GFD 錯誤停止
  3893. RecordAlarmCode(gun_index, "012236");
  3894. }
  3895. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3896. {
  3897. // GFD 警告
  3898. RecordWarningCode(gun_index, "012298");
  3899. }
  3900. }
  3901. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3902. {
  3903. // 檢查樁端的 GFD 結果
  3904. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  3905. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3906. {
  3907. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  3908. }
  3909. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3910. {
  3911. // GFD 錯誤停止
  3912. RecordAlarmCode(gun_index, "012235");
  3913. }
  3914. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3915. {
  3916. // GFD 警告
  3917. RecordWarningCode(gun_index, "012297");
  3918. }
  3919. }
  3920. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3921. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
  3922. OcppRemoteStop(gun_index) == YES)
  3923. {
  3924. // 板端或後臺要求停止
  3925. ChargingTerminalProcess(gun_index);
  3926. }
  3927. // LCM => Pre-charging
  3928. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3929. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3930. }
  3931. break;
  3932. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  3933. {
  3934. if (isModeChange(gun_index))
  3935. {
  3936. PRINTF_FUNC("S_CHARGING================================== %x \n", gun_index);
  3937. StopGunInfoTimeoutDet(gun_index);
  3938. ftime(&startChargingTime[gun_index]);
  3939. ChangeStartOrStopDateTime(YES, gun_index);
  3940. OcppStartTransation(gun_index);
  3941. }
  3942. ftime(&endChargingTime[gun_index]);
  3943. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  3944. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3945. {
  3946. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3947. {
  3948. // GFD 錯誤停止
  3949. RecordAlarmCode(gun_index, "012234");
  3950. }
  3951. else if ((chargingInfo[gun_index]->EvBatterytargetVoltage > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  3952. (chargingInfo[gun_index]->RemainChargingDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  3953. {
  3954. // UVP
  3955. RecordAlarmCode(gun_index, "012289");
  3956. ChargingTerminalProcess(gun_index);
  3957. }
  3958. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3959. {
  3960. // GFD 警告
  3961. RecordWarningCode(gun_index, "012296");
  3962. }
  3963. else
  3964. ReleaseWarningCodeByString(gun_index, "012296");
  3965. }
  3966. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3967. {
  3968. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3969. {
  3970. // GFD 錯誤停止
  3971. RecordAlarmCode(gun_index, "012236");
  3972. }
  3973. else if (isPrechargeStatus_gb(gun_index) == 10 &&
  3974. ((chargingInfo[gun_index]->EvBatterytargetVoltage > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  3975. (chargingInfo[gun_index]->RemainChargingDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL)))
  3976. {
  3977. // UVP
  3978. RecordAlarmCode(gun_index, "012290");
  3979. ChargingTerminalProcess(gun_index);
  3980. }
  3981. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3982. {
  3983. // GFD 警告
  3984. RecordWarningCode(gun_index, "012298");
  3985. }
  3986. else
  3987. ReleaseWarningCodeByString(gun_index, "012298");
  3988. }
  3989. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3990. {
  3991. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3992. {
  3993. // GFD 錯誤停止
  3994. RecordAlarmCode(gun_index, "012235");
  3995. }
  3996. else if ((chargingInfo[gun_index]->EvBatterytargetVoltage > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  3997. (chargingInfo[gun_index]->RemainChargingDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  3998. {
  3999. // UVP
  4000. RecordAlarmCode(gun_index, "012288");
  4001. ChargingTerminalProcess(gun_index);
  4002. }
  4003. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  4004. {
  4005. // GFD 警告
  4006. RecordWarningCode(gun_index, "012297");
  4007. }
  4008. else
  4009. ReleaseWarningCodeByString(gun_index, "012297");
  4010. }
  4011. if (isEvBoardStopChargeFlag(gun_index) ||
  4012. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
  4013. OcppRemoteStop(gun_index) ||
  4014. CheckBackendChargingTimeout(gun_index) ||
  4015. CheckBackendChargingEnergy(gun_index))
  4016. {
  4017. PRINTF_FUNC("S_CHARGING=======Stop=========================== %f \n", chargingInfo[gun_index]->EvBatterytargetVoltage);
  4018. // 板端或後臺要求停止
  4019. ChargingTerminalProcess(gun_index);
  4020. }
  4021. // LCM => Charging
  4022. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  4023. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  4024. }
  4025. break;
  4026. case S_TERMINATING:
  4027. {
  4028. if (isModeChange(gun_index))
  4029. {
  4030. PRINTF_FUNC ("terminating......................... %x \n", gun_index);
  4031. StopGunInfoTimeoutDet(gun_index);
  4032. }
  4033. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  4034. {
  4035. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  4036. if (isEvStopCharging_chademo(gun_index) == YES ||
  4037. isPrechargeStatus_chademo(gun_index) <= 0)
  4038. {
  4039. setChargerMode(gun_index, MODE_COMPLETE);
  4040. }
  4041. }
  4042. else if (chargingInfo[gun_index]->Type == _Type_GB)
  4043. {
  4044. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  4045. if (isEvStopCharging_gb(gun_index) == YES ||
  4046. isPrechargeStatus_gb(gun_index) <= 0)
  4047. {
  4048. setChargerMode(gun_index, MODE_COMPLETE);
  4049. }
  4050. }
  4051. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  4052. {
  4053. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  4054. if (isEvStopCharging_ccs(gun_index) == YES &&
  4055. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  4056. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  4057. {
  4058. setChargerMode(gun_index, MODE_COMPLETE);
  4059. }
  4060. }
  4061. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  4062. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  4063. }
  4064. break;
  4065. case S_COMPLETE:
  4066. {
  4067. if (isModeChange(gun_index))
  4068. {
  4069. PRINTF_FUNC ("complete......................... %x \n", gun_index);
  4070. OcppStopTransation(gun_index);
  4071. ftime(&endChargingTime[gun_index]);
  4072. if (chargingInfo[gun_index]->RemainChargingDuration != 0)
  4073. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  4074. StopGunInfoTimeoutDet(gun_index);
  4075. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  4076. ChangeStartOrStopDateTime(NO, gun_index);
  4077. }
  4078. if(chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  4079. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  4080. {
  4081. setChargerMode(gun_index, MODE_IDLE);
  4082. }
  4083. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  4084. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  4085. }
  4086. break;
  4087. case S_CCS_PRECHARGE_ST0:
  4088. {
  4089. if (isModeChange(gun_index))
  4090. {
  4091. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  4092. StopGunInfoTimeoutDet(gun_index);
  4093. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  4094. }
  4095. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  4096. {
  4097. // GFD 錯誤停止
  4098. RecordAlarmCode(gun_index, "012235");
  4099. }
  4100. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  4101. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  4102. {
  4103. // 板端要求停止
  4104. ChargingTerminalProcess(gun_index);
  4105. }
  4106. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  4107. // 切換 D+ Relay to Precharge Relay
  4108. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  4109. {
  4110. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  4111. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  4112. {
  4113. PRINTF_FUNC("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  4114. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  4115. }
  4116. }
  4117. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  4118. {
  4119. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  4120. }
  4121. break;
  4122. }
  4123. case S_CCS_PRECHARGE_ST1:
  4124. {
  4125. if (isModeChange(gun_index))
  4126. {
  4127. PRINTF_FUNC("CCS Precharge Processing 2....................%x \n", gun_index);
  4128. }
  4129. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  4130. {
  4131. // GFD 錯誤停止
  4132. RecordAlarmCode(gun_index, "012235");
  4133. }
  4134. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  4135. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  4136. {
  4137. // 板端要求停止
  4138. ChargingTerminalProcess(gun_index);
  4139. }
  4140. // 等待小板通知進入充電
  4141. // 切換 D+ Relay to Precharge Relay
  4142. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  4143. {
  4144. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  4145. setChargerMode(gun_index, MODE_CHARGING);
  4146. }
  4147. break;
  4148. }
  4149. }
  4150. }
  4151. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  4152. {
  4153. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  4154. }
  4155. else
  4156. {
  4157. bool dcPageRun = false;
  4158. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  4159. {
  4160. if (ac_chargingInfo[0]->SystemStatus == S_IDLE)
  4161. ChangeLcmByIndex(_LCM_IDLE);
  4162. else if (ac_chargingInfo[0]->SystemStatus == S_PREPARNING)
  4163. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  4164. else if (ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  4165. ChangeLcmByIndex(_LCM_CHARGING);
  4166. else if (ac_chargingInfo[0]->SystemStatus == S_TERMINATING ||
  4167. ac_chargingInfo[0]->SystemStatus == S_COMPLETE)
  4168. ChangeLcmByIndex(_LCM_COMPLETE);
  4169. else
  4170. dcPageRun = true;
  4171. }
  4172. else
  4173. dcPageRun = true;
  4174. if (dcPageRun)
  4175. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  4176. }
  4177. write(wtdFd, "a", 1);
  4178. usleep(whileLoopTime);
  4179. }
  4180. return FAIL;
  4181. }