main.c 175 KB

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