main.c 194 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030
  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 <stdbool.h>
  30. #include <dirent.h>
  31. #include <signal.h>
  32. #include "../Config.h"
  33. #include "main.h"
  34. #include "../common.h"
  35. #include "../timeout.h"
  36. #include "../Log/log.h"
  37. #include "../DataBase/DataBase.h"
  38. #include "../Define/define.h"
  39. #include "../ShareMemory/shmMem.h"
  40. #include "../SelectGun/SelectGun.h"
  41. //------------------------------------------------------------------------------
  42. static struct SysInfoData *pSysInfo = NULL;
  43. static struct SysConfigData *pSysConfig = NULL;
  44. static struct WARNING_CODE_INFO *pSysWarning = NULL;
  45. static struct AlarmCodeData *pAlarmCode = NULL;
  46. static struct FaultCodeData *pFaultCode = NULL;
  47. static struct InfoCodeData *pInfoCode = NULL;
  48. static struct PsuData *ShmPsuData = NULL;
  49. static struct CHAdeMOData *ShmCHAdeMOData = NULL;
  50. static struct GBTData *ShmGBTData = NULL;
  51. static struct CcsData *ShmCcsData = NULL;
  52. static struct PrimaryMcuData *ShmPrimaryMcuData = NULL;
  53. static struct FanModuleData *ShmFanModuleData = NULL;
  54. static struct RelayModuleData *ShmRelayModuleData = NULL;
  55. static struct LedModuleData *ShmLedModuleData = NULL;
  56. static struct OCPP16Data *ShmOCPP16Data = NULL;
  57. static struct OCPP20Data* ShmOCPP20Data = NULL;
  58. static DcCommonInfo *ShmDcCommonData = NULL;
  59. static struct ChargingInfoData *pDcChargingInfo = NULL;
  60. static struct ChargingInfoData *pAcChargingInfo = NULL;
  61. static struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  62. static struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  63. static SelectGunInfo *ShmSelectGunInfo = NULL; //Jerry add
  64. static EvBoardErrMsg gEvBoardErr = {0};
  65. static ChillerTempErr gChillerTempErr = {0};
  66. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  67. struct StatusCodeData *ShmStatusCodeData;
  68. #define DERATING_TARGET_LEVEL 5
  69. // for initial index to check EV board type is correct
  70. uint8_t bd0_1_status = 0;
  71. uint8_t bd0_2_status = 0;
  72. uint8_t bd1_1_status = 0;
  73. uint8_t bd1_2_status = 0;
  74. char *fwVersion = "V2.22.00.0000.00"; // Phihong version
  75. char* DebugVersion = "V2.22.00"; // Software debug version
  76. //sqlite3 *localDb;
  77. bool isDb_ready;
  78. //------------------------------------------------------------------------------
  79. void ClearDetectPluginFlag(int gunIndex);
  80. long long DiffTimebWithNow(struct timeb ST);
  81. uint8_t DetectBitValue(uint8_t _byte, uint8_t _bit);
  82. void SetBitValue(uint8_t *_byte, uint8_t _bit, uint8_t value);
  83. unsigned long GetTimeoutValue(struct timeval _sour_time);
  84. unsigned long GetClockTimeoutValue(struct timespec _start_time);
  85. void GetClockTime(struct timespec *_now_time, void *null);
  86. void gpio_set_value(unsigned int gpio, unsigned int value);
  87. void InformOcppErrOccur(uint8_t codeType);
  88. void RecordAlarmCode(uint8_t gunIndex, char *code);
  89. void ReleaseAlarmCode(uint8_t gunIndex);
  90. void ResetChargerAlarmCode(uint8_t gunIndex, char *code);
  91. void AdjustChargerCurrent(void);
  92. void UpdateErrorCodeToOcpp(uint8_t index);
  93. //------------------------------------------------------------------------------
  94. //Primary.c
  95. extern void ChkPrimaryStatus(void);
  96. //RFID.c
  97. extern void CreateRfidFork(void);
  98. extern void ScannerCardProcess(int gunIndex);
  99. extern bool RfidStopCharging(void);
  100. extern bool GetIsCardScan(void);
  101. extern void SetIsCardScan(bool value);
  102. //SelfTest.c
  103. extern void SelfTestRun(void);
  104. //UpgradeFW.c
  105. /*extern void CheckFwUpdateFunction(void);*/
  106. //Ethernet.c
  107. extern void InitEthernet(void);
  108. extern void GetMacAddress(void);
  109. //WatchDog.c
  110. extern void CreateWatchdog(void);
  111. extern void TryCloseWatchdog(void);
  112. extern void TryFeedWatchdog(void);
  113. //ZipFile.c
  114. extern void zipLogFiles(void);
  115. //------------------------------------------------------------------------------
  116. //--- share memory value ---
  117. //------------------------------------------------------------------------------
  118. static void changeLcmPage(uint8_t index)
  119. {
  120. pSysInfo->SystemPage = index;
  121. }
  122. static uint8_t getCurLcmPage(void)
  123. {
  124. return pSysInfo->SystemPage;
  125. }
  126. static void systemPageRestoreInit(void)
  127. {
  128. int is_idle = TRUE;
  129. int gunIndex;
  130. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  131. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  132. // 檢查電樁狀態是否為idle狀態
  133. if ((pDcChargingInfo->SystemStatus >= S_AUTHORIZING &&
  134. pDcChargingInfo->SystemStatus <= S_RESERVATION) ||
  135. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  136. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST0)) {
  137. is_idle = FALSE;
  138. }
  139. }
  140. if (is_idle)
  141. pSysInfo->SystemPage = _PAGE_IDLE;
  142. else
  143. pSysInfo->SystemPage = _PAGE_SELECT_GUN;
  144. }
  145. //------------------------------------------------------------------------------
  146. void destroySelGun(uint8_t curGun)
  147. {
  148. uint8_t i = 0;
  149. uint8_t totalGun = pSysConfig->TotalConnectorCount;
  150. //for status timeout
  151. if (curGun == DESTROY_ALL_SEL) {
  152. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  153. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  154. log_info("destroy all gun = %d, %d",
  155. ShmSelectGunInfo->SelGunInfo.LeftGun,
  156. ShmSelectGunInfo->SelGunInfo.RightGun);
  157. for (i = 0; i < totalGun; i++) {
  158. StopGunInfoTimeoutDet(i);
  159. memset(&ShmSelectGunInfo->PricesInfo[i], 0, sizeof(PricesInfo));
  160. }
  161. pSysInfo->CurGunSelected = 0;
  162. strcpy((char *)pSysConfig->UserId, "");
  163. //changeLcmPage(_LCM_VIEW);
  164. return;
  165. }
  166. //for charging timeout or complete
  167. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
  168. if (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  169. ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  170. //changeLcmPage(_PAGE_SELECT_GUN);
  171. }
  172. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  173. StopGunInfoTimeoutDet(LEFT_GUN_NUM);
  174. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  175. ClearAuthorizedFlag();
  176. }
  177. log_info("destroy left gun, cur page = %d", getCurLcmPage());
  178. if (getCurLcmPage() == pSysInfo->SystemPage) {
  179. log_info("clear left balance");
  180. memset(&ShmSelectGunInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  181. ShmSelectGunInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  182. }
  183. }
  184. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
  185. if (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  186. ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  187. //changeLcmPage(_PAGE_SELECT_GUN);
  188. }
  189. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  190. StopGunInfoTimeoutDet(RIGHT_GUN_NUM);
  191. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  192. ClearAuthorizedFlag();
  193. }
  194. log_info("destroy right gun, cur page = %d", getCurLcmPage());
  195. if (getCurLcmPage() == pSysInfo->SystemPage) {
  196. log_info("clear right balance");
  197. memset(&ShmSelectGunInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  198. ShmSelectGunInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  199. }
  200. }
  201. }
  202. static int waitRightGunPlugIt(uint8_t curGun)
  203. {
  204. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  205. return PASS;
  206. }
  207. return FAIL;
  208. }
  209. static int waitLeftGunPlugIt(uint8_t curGun)
  210. {
  211. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  212. return PASS;
  213. }
  214. return FAIL;
  215. }
  216. void setSelGunWaitToAuthor(uint8_t curSel)
  217. {
  218. if (curSel == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM) {
  219. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_ATHOR;
  220. //log_info("setSelGunWaitToAuthor left");
  221. StopGunInfoTimeoutDet(curSel);
  222. } else if (curSel == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM) {
  223. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_ATHOR;
  224. //log_info("setSelGunWaitToAuthor right");
  225. StopGunInfoTimeoutDet(curSel);
  226. }
  227. }
  228. int getConfirmSelectedGun(uint8_t curSel)
  229. {
  230. if (((curSel == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  231. ((curSel == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  232. return PASS;
  233. }
  234. return FAIL;
  235. }
  236. void confirmSelGun(uint8_t selGun)
  237. {
  238. if (selGun == LEFT_GUN_NUM) {
  239. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  240. //printf("confirmSelGun left");
  241. } else if (selGun == RIGHT_GUN_NUM) {
  242. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  243. //printf("confirmSelGun right");
  244. }
  245. //changeLcmPage(_PAGE_PLUGIN);
  246. //StartGunInfoTimeoutDet(selGun, Timeout_SelectGun);
  247. //StartSystemTimeoutDet(Timeout_ReturnViewPage);
  248. }
  249. static void GetFirmwareVersion(void)
  250. {
  251. // Get CSU root file system version
  252. sprintf((char *)pSysInfo->CsuRootFsFwRev, fwVersion);
  253. sprintf((char*)ShmDcCommonData->DebugVersion, DebugVersion);
  254. uint8_t count = 0, chademo = 0, ccs = 0, gb = 0;
  255. for (uint8_t idx = 0; idx < 3; idx++) {
  256. if (pSysConfig->ModelName[7 + idx] == 'J') {
  257. chademo++;
  258. count++;
  259. } else if (pSysConfig->ModelName[7 + idx] == 'G') {
  260. gb++;
  261. count++;
  262. } else if (pSysConfig->ModelName[7 + idx] == 'U' ||
  263. pSysConfig->ModelName[7 + idx] == 'V' ||
  264. pSysConfig->ModelName[7 + idx] == 'E') {
  265. ccs++;
  266. count++;
  267. }
  268. }
  269. if (count == 1) {
  270. if (chademo > 0) {
  271. pSysInfo->CsuRootFsFwRev[7] = '1';
  272. } else if (ccs > 0) {
  273. pSysInfo->CsuRootFsFwRev[7] = '2';
  274. } else if (gb > 0) {
  275. pSysInfo->CsuRootFsFwRev[7] = '3';
  276. }
  277. } else {
  278. if (chademo > 0 && ccs > 0) {
  279. pSysInfo->CsuRootFsFwRev[7] = '4';
  280. } else if (chademo > 0 && gb > 0) {
  281. pSysInfo->CsuRootFsFwRev[7] = '5';
  282. } else if (ccs > 0 && gb > 0) {
  283. pSysInfo->CsuRootFsFwRev[7] = '6';
  284. }
  285. }
  286. // Get network option from model name
  287. switch (pSysConfig->ModelName[10]) {
  288. case 'B':
  289. case 'U':
  290. //Blue tooth
  291. pSysInfo->CsuRootFsFwRev[9] = '3';
  292. break;
  293. case 'W':
  294. // WIFI
  295. pSysInfo->CsuRootFsFwRev[9] = '1';
  296. break;
  297. case 'T':
  298. // 3G/4G
  299. pSysInfo->CsuRootFsFwRev[9] = '2';
  300. break;
  301. case 'D': //DS60-120 add
  302. pSysInfo->CsuRootFsFwRev[9] = '5';
  303. break;
  304. default:
  305. // LAN
  306. pSysInfo->CsuRootFsFwRev[9] = '0';
  307. break;
  308. }
  309. // Get rating power from model name
  310. memcpy(&pSysInfo->CsuRootFsFwRev[10], &pSysConfig->ModelName[4], 0x03);
  311. // Get IEC or UL
  312. char _buf[3] = {0};
  313. memcpy(_buf, &pSysConfig->ModelName[2], 2);
  314. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL) {
  315. pSysInfo->ChargerType = _CHARGER_TYPE_IEC;
  316. log_info("IEC model");
  317. } else if (strcmp(_buf, "WU") == EQUAL) {
  318. pSysInfo->ChargerType = _CHARGER_TYPE_UL;
  319. log_info("UL model");
  320. }
  321. }
  322. static void checkGunOTPState(uint8_t gunIndex)
  323. {
  324. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  325. //水冷機溫度檢測
  326. if (((ShmDcCommonData->ChillerValve.MultiChillerGun & 0x80) >> 7) == YES) {
  327. if (pDcChargingInfo->ChillerTemp != UNDEFINED_TEMP) {
  328. if (pDcChargingInfo->ChillerTemp >= GUN_OTP_VALUE) {
  329. if (((gunIndex == 0) &&
  330. ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  331. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)))
  332. ||
  333. ((gunIndex == 1) &&
  334. ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  335. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)))
  336. ) {
  337. RecordAlarmCode(gunIndex, "012323");
  338. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = YES;
  339. }
  340. else
  341. {
  342. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  343. }
  344. } else if (pDcChargingInfo->ChillerTemp != 0 &&
  345. pDcChargingInfo->ChillerTemp < GUN_OTP_RECOVERY) {
  346. //ResetChargerAlarmCode(gunIndex, "012323");
  347. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  348. }
  349. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  350. } else {
  351. // 沒接上 Sensor or 異常
  352. //RecordAlarmCode(gunIndex, "011038");
  353. //ResetChargerAlarmCode(gunIndex, "012323");
  354. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  355. if (((gunIndex == 0) &&
  356. ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  357. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)))
  358. ||
  359. ((gunIndex == 1) &&
  360. ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  361. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)))
  362. ) {
  363. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = YES;
  364. }
  365. else
  366. {
  367. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  368. }
  369. }
  370. if(pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault == YES)
  371. {
  372. RecordAlarmCode(gunIndex, "011037");
  373. }
  374. }
  375. switch (pDcChargingInfo->Type) {
  376. case _Type_Chademo:
  377. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  378. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  379. RecordAlarmCode(gunIndex, "012229");
  380. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = YES;
  381. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  382. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  383. //ResetChargerAlarmCode(gunIndex, "012229");
  384. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  385. }
  386. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  387. } else {
  388. // 沒接上 Sensor or 異常
  389. //RecordAlarmCode(gunIndex, "011018");
  390. //ResetChargerAlarmCode(gunIndex, "012229");
  391. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  392. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = YES;
  393. if ((gunIndex == 0) &&
  394. (strncmp((char *)&pSysConfig->ModelName[7], "J", 1) == 0)) {
  395. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  396. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  397. } else if ((gunIndex == 1) &&
  398. ((strncmp((char *)&pSysConfig->ModelName[9], "J", 1) == 0) )) {
  399. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  400. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  401. }
  402. }
  403. break;
  404. case _Type_CCS_2:
  405. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  406. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  407. //ResetChargerAlarmCode(gunIndex, "011019");
  408. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  409. RecordAlarmCode(gunIndex, "012230");
  410. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = YES;
  411. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  412. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  413. //ResetChargerAlarmCode(gunIndex, "012230");
  414. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  415. }
  416. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  417. //ResetChargerAlarmCode(gunIndex, "011019");
  418. } else {
  419. // 沒接上 Sensor or 異常
  420. //RecordAlarmCode(gunIndex, "011019");
  421. //ResetChargerAlarmCode(gunIndex, "012230");
  422. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  423. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = YES;
  424. }
  425. break;
  426. case _Type_GB:
  427. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  428. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  429. RecordAlarmCode(gunIndex, "012231");
  430. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = YES;
  431. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  432. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  433. //ResetChargerAlarmCode(gunIndex, "012231");
  434. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  435. }
  436. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  437. } else {
  438. // 沒接上 Sensor or 異常
  439. //RecordAlarmCode(gunIndex, "011020");
  440. //ResetChargerAlarmCode(gunIndex, "012231");
  441. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  442. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = YES;
  443. }
  444. break;
  445. }
  446. }
  447. static void collectError(uint8_t gunIndex)
  448. {
  449. gEvBoardErr.GunErrMessage |= ShmDcCommonData->ConnectErrList[gunIndex].GunErrMessage;
  450. gChillerTempErr.TempErrMsg |= ShmDcCommonData->ChillerTempErr[gunIndex].TempErrMsg;
  451. }
  452. static void checkGBTAlarmState(uint8_t gunType)
  453. {
  454. // GFD Trip
  455. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  456. pAlarmCode->AlarmEvents.bits.GbGfdTrip = YES;
  457. } else {
  458. pAlarmCode->AlarmEvents.bits.GbGfdTrip = NO;
  459. }
  460. // UVP
  461. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  462. pAlarmCode->AlarmEvents.bits.GbtOutputUVPFail = YES;
  463. } else {
  464. pAlarmCode->AlarmEvents.bits.GbtOutputUVPFail = NO;
  465. }
  466. // OTP
  467. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  468. pAlarmCode->AlarmEvents.bits.GbConnectorOTP = YES;
  469. } else {
  470. pAlarmCode->AlarmEvents.bits.GbConnectorOTP = NO;
  471. }
  472. // OVP
  473. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  474. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = YES;
  475. } else {
  476. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = NO;
  477. }
  478. // GFD Warning
  479. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  480. pAlarmCode->AlarmEvents.bits.GbGroundfaultWarning = YES;
  481. } else {
  482. pAlarmCode->AlarmEvents.bits.GbGroundfaultWarning = NO;
  483. }
  484. // Relay Welding
  485. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  486. pFaultCode->FaultEvents.bits.GbOutputRelayWelding = YES;
  487. } else {
  488. pFaultCode->FaultEvents.bits.GbOutputRelayWelding = NO;
  489. }
  490. // Relay Driving
  491. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  492. pFaultCode->FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  493. } else {
  494. pFaultCode->FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  495. }
  496. // Connect temp Sensor broken
  497. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  498. pFaultCode->FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  499. } else {
  500. pFaultCode->FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  501. }
  502. }
  503. static void checkCCSAlarmState(uint8_t gunType)
  504. {
  505. // GFD Trip
  506. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  507. pAlarmCode->AlarmEvents.bits.CcsGfdTrip = YES;
  508. } else {
  509. pAlarmCode->AlarmEvents.bits.CcsGfdTrip = NO;
  510. }
  511. // UVP
  512. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  513. pAlarmCode->AlarmEvents.bits.CcsOutputUVPFail = YES;
  514. } else {
  515. pAlarmCode->AlarmEvents.bits.CcsOutputUVPFail = NO;
  516. }
  517. // OTP
  518. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  519. pAlarmCode->AlarmEvents.bits.CcsConnectorOTP = YES;
  520. } else {
  521. pAlarmCode->AlarmEvents.bits.CcsConnectorOTP = NO;
  522. }
  523. // OVP
  524. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  525. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = YES;
  526. } else {
  527. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = NO;
  528. }
  529. // GFD Warning
  530. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  531. pAlarmCode->AlarmEvents.bits.CcsGroundfaultWarning = YES;
  532. } else {
  533. pAlarmCode->AlarmEvents.bits.CcsGroundfaultWarning = NO;
  534. }
  535. // Relay Welding
  536. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  537. pFaultCode->FaultEvents.bits.CcsOutputRelayWelding = YES;
  538. } else {
  539. pFaultCode->FaultEvents.bits.CcsOutputRelayWelding = NO;
  540. }
  541. // Relay Driving
  542. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  543. pFaultCode->FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  544. } else {
  545. pFaultCode->FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  546. }
  547. // Connect temp Sensor broken
  548. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  549. pFaultCode->FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  550. } else {
  551. pFaultCode->FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  552. }
  553. }
  554. static void checkChaDeMoAlarmState(uint8_t gunType)
  555. {
  556. // GFD Trip
  557. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  558. pAlarmCode->AlarmEvents.bits.ChademoGfdTrip = YES;
  559. } else {
  560. pAlarmCode->AlarmEvents.bits.ChademoGfdTrip = NO;
  561. }
  562. // UVP
  563. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  564. pAlarmCode->AlarmEvents.bits.ChademoOutputUVPFail = YES;
  565. } else {
  566. pAlarmCode->AlarmEvents.bits.ChademoOutputUVPFail = NO;
  567. }
  568. // OTP
  569. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  570. pAlarmCode->AlarmEvents.bits.ChademoConnectorOTP = YES;
  571. } else {
  572. pAlarmCode->AlarmEvents.bits.ChademoConnectorOTP = NO;
  573. }
  574. // OVP
  575. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  576. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = YES;
  577. } else {
  578. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = NO;
  579. }
  580. // GFD Warning
  581. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  582. pAlarmCode->AlarmEvents.bits.ChademoGroundWarning = YES;
  583. } else {
  584. pAlarmCode->AlarmEvents.bits.ChademoGroundWarning = NO;
  585. }
  586. // Relay Welding
  587. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  588. pFaultCode->FaultEvents.bits.ChademoOutputRelayWelding = YES;
  589. } else {
  590. pFaultCode->FaultEvents.bits.ChademoOutputRelayWelding = NO;
  591. }
  592. // Relay Driving
  593. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  594. pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  595. } else {
  596. pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  597. }
  598. // Connect temp Sensor broken
  599. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  600. pFaultCode->FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  601. } else {
  602. pFaultCode->FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  603. }
  604. }
  605. static void checkChillerAlarmState(void)
  606. {
  607. if (DetectBitValue(gChillerTempErr.TempErrMsg, 0)) {
  608. pAlarmCode->AlarmEvents.bits.SystemChillerOTP = YES;
  609. } else {
  610. pAlarmCode->AlarmEvents.bits.SystemChillerOTP = NO;
  611. }
  612. if (DetectBitValue(gChillerTempErr.TempErrMsg, 1)) {
  613. pFaultCode->FaultEvents.bits.ChillerTempSensorBroken = YES;
  614. } else {
  615. pFaultCode->FaultEvents.bits.ChillerTempSensorBroken = NO;
  616. }
  617. }
  618. static void checkEvBoardAlarmState(uint8_t gunType)
  619. {
  620. switch (gunType) {
  621. case _Type_Chademo:
  622. checkChaDeMoAlarmState(gunType);
  623. break;
  624. case _Type_CCS_2:
  625. checkCCSAlarmState(gunType);
  626. break;
  627. case _Type_GB:
  628. checkGBTAlarmState(gunType);
  629. break;
  630. }
  631. }
  632. unsigned long GetTimeoutValue(struct timeval _sour_time)
  633. {
  634. struct timeval _end_time;
  635. gettimeofday(&_end_time, NULL);
  636. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  637. }
  638. void GetClockTime(struct timespec *_now_time, void *null)
  639. {
  640. clock_gettime(CLOCK_MONOTONIC, _now_time);
  641. }
  642. // return value unit: 1us
  643. unsigned long GetClockTimeoutValue(struct timespec _start_time)
  644. {
  645. struct timespec ts_end;
  646. unsigned long ret = 0;
  647. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  648. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec/ 1000)));
  649. return ret;
  650. }
  651. int DiffTimeb(struct timeb ST, struct timeb ET)
  652. {
  653. //return milli-second
  654. unsigned int StartTime, StopTime;
  655. StartTime = (unsigned int)ST.time;
  656. StopTime = (unsigned int)ET.time;
  657. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  658. return (StopTime - StartTime);
  659. }
  660. bool CheckTimeOut(struct timeb ST)
  661. {
  662. struct timeb ET;
  663. unsigned int StartTime, StopTime;
  664. ftime(&ET);
  665. StartTime = (unsigned int) ST.time;
  666. StopTime = (unsigned int) ET.time;
  667. return (StopTime > StartTime) ? YES : NO;
  668. }
  669. void setChargerMode(uint8_t gunIndex, uint8_t mode)
  670. {
  671. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  672. pDcChargingInfo->SystemStatus = mode;
  673. }
  674. long long DiffTimebWithNow(struct timeb ST)
  675. {
  676. //return milli-second
  677. struct timeb ET;
  678. long long StartTime, StopTime;
  679. ftime(&ET);
  680. StartTime = (long long)ST.time;
  681. StopTime = (long long)ET.time;
  682. return ((StopTime - StartTime) * 1000) + (ET.millitm - ST.millitm);
  683. }
  684. //==========================================
  685. // Log
  686. //==========================================
  687. void CheckFwSlotStatusLog(void)
  688. {
  689. if (bd0_1_status == 0 && bd0_2_status == 1) {
  690. log_info("Connector 1 : Chademo");
  691. } else if (bd0_1_status == 1 && bd0_2_status == 0) {
  692. log_info("Connector 1 : CCS");
  693. } else if (bd0_1_status == 1 && bd0_2_status == 1) {
  694. log_info("Connector 1 : GB");
  695. }
  696. if (bd1_1_status == 0 && bd1_2_status == 1) {
  697. log_info("Connector 2 : Chademo");
  698. } else if (bd1_1_status == 1 && bd1_2_status == 0) {
  699. log_info("Connector 2 : CCS");
  700. } else if (bd1_1_status == 1 && bd1_2_status == 1) {
  701. log_info("Connector 2 : GB");
  702. }
  703. }
  704. void CheckHwSlotStatusLog(uint8_t index)
  705. {
  706. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  707. if (pDcChargingInfo->Type == _Type_Chademo) {
  708. log_info("Hw check : Connector %d, Type : Chademo, Evboard_id = %d ",
  709. index, pDcChargingInfo->Evboard_id);
  710. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  711. log_info("Hw check : Connector %d, Type : CCS, Evboard_id = %d ",
  712. index, pDcChargingInfo->Evboard_id);
  713. } else if (pDcChargingInfo->Type == _Type_GB) {
  714. log_info("Hw check : Connector %d, Type : GB, Evboard_id = %d ",
  715. index, pDcChargingInfo->Evboard_id);
  716. }
  717. }
  718. //=================================
  719. // LCM Page
  720. //=================================
  721. void ChangeLcmByIndex(uint8_t page_index)
  722. {
  723. if (pSysWarning->Level != WARN_LV_ER
  724. ) {
  725. pSysInfo->PageIndex = page_index;
  726. //log_info("LCM index:%d",pSysInfo->PageIndex);
  727. }
  728. }
  729. //======================================================
  730. // Peripheral initial
  731. //======================================================
  732. void InitGPIO()
  733. {
  734. /*****************0~3, 4 bank, bank x 32+ num*********************/
  735. /***************************************************************/
  736. /*************** GPIO 0 ***************************************/
  737. /***************************************************************/
  738. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  739. system("echo 22 > /sys/class/gpio/export");
  740. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  741. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  742. system("echo 23 > /sys/class/gpio/export");
  743. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  744. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  745. system("echo 26 > /sys/class/gpio/export");
  746. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  747. system("echo 1 > /sys/class/gpio/gpio26/value");
  748. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  749. system("echo 27 > /sys/class/gpio/export");
  750. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  751. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  752. system("echo 29 > /sys/class/gpio/export");
  753. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  754. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  755. system("echo 19 > /sys/class/gpio/export");
  756. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  757. system("echo 1 > /sys/class/gpio/gpio19/value");
  758. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  759. system("echo 20 > /sys/class/gpio/export");
  760. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  761. /***************************************************************/
  762. /*************** GPIO 1 ***************************************/
  763. /***************************************************************/
  764. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  765. system("echo 44 > /sys/class/gpio/export");
  766. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  767. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  768. system("echo 45 > /sys/class/gpio/export");
  769. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  770. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  771. system("echo 46 > /sys/class/gpio/export");
  772. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  773. system("echo 0 > /sys/class/gpio/gpio46/value");
  774. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  775. system("echo 47 > /sys/class/gpio/export");
  776. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  777. /***************************************************************/
  778. /*************** GPIO 2 ***************************************/
  779. /***************************************************************/
  780. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  781. system("echo 89 > /sys/class/gpio/export");
  782. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  783. system("echo 1 > /sys/class/gpio/gpio89/value");
  784. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  785. system("echo 87 > /sys/class/gpio/export");
  786. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  787. system("echo 0 > /sys/class/gpio/gpio87/value");
  788. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  789. system("echo 88 > /sys/class/gpio/export");
  790. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  791. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  792. system("echo 86 > /sys/class/gpio/export");
  793. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  794. /***************************************************************/
  795. /*************** GPIO 3 ***************************************/
  796. /***************************************************************/
  797. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  798. system("echo 111 > /sys/class/gpio/export");
  799. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  800. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  801. system("echo 114 > /sys/class/gpio/export");
  802. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  803. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  804. system("echo 113 > /sys/class/gpio/export");
  805. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  806. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  807. system("echo 110 > /sys/class/gpio/export");
  808. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  809. system("echo 0 > /sys/class/gpio/gpio110/value");
  810. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  811. system("echo 115 > /sys/class/gpio/export");
  812. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  813. system("echo 0 > /sys/class/gpio/gpio115/value");
  814. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  815. system("echo 112 > /sys/class/gpio/export");
  816. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  817. system("echo 1 > /sys/class/gpio/gpio112/value");
  818. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  819. system("echo 116 > /sys/class/gpio/export");
  820. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  821. system("echo 0 > /sys/class/gpio/gpio116/value");
  822. log_info("Initial GPIO OK");
  823. return ;
  824. }
  825. int LoadSysConfigAndInfo()
  826. {
  827. int fd, wrd;
  828. unsigned char *buf;
  829. unsigned int ChkSum, ChkSumOrg;
  830. if ((buf = malloc(MtdBlockSize)) == NULL) {
  831. log_error("malloc buffer NG,rebooting..");
  832. if (pAlarmCode != NULL) {
  833. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  834. }
  835. sleep(5);
  836. system("reboot -f");
  837. sleep(5);
  838. system("reboot -f");
  839. }
  840. memset(buf, 0, MtdBlockSize);
  841. //================================================
  842. // Load configuration from mtdblock10
  843. //================================================
  844. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  845. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  846. if (fd < 0) {
  847. free(buf);
  848. log_error("open mtdblock10 NG,rebooting..");
  849. if (pAlarmCode != NULL) {
  850. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  851. }
  852. sleep(5);
  853. system("reboot -f");
  854. sleep(5);
  855. system("reboot -f");
  856. }
  857. wrd = read(fd, buf, MtdBlockSize);
  858. close(fd);
  859. if (wrd < MtdBlockSize) {
  860. free(buf);
  861. log_error("read SysConfigData data NG,rebooting..");
  862. if (pAlarmCode != NULL) {
  863. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  864. }
  865. sleep(5);
  866. system("reboot -f");
  867. sleep(5);
  868. system("reboot -f");
  869. }
  870. ChkSum = 0;
  871. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  872. ChkSum += buf[wrd];
  873. }
  874. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  875. memcpy(&pSysConfig->ModelName, buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev)), ARRAY_SIZE(pSysConfig->ModelName));
  876. memcpy(&pSysConfig->SerialNumber, buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev) + ARRAY_SIZE(pSysConfig->ModelName) + ARRAY_SIZE(pSysConfig->AcModelName)), ARRAY_SIZE(pSysConfig->SerialNumber));
  877. //================================================
  878. // Load configuration from mtdblock11
  879. //================================================
  880. if (ChkSum != ChkSumOrg) {
  881. log_error("Primary SysConfigData checksum NG, read backup");
  882. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  883. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  884. if (fd < 0) {
  885. free(buf);
  886. log_error("open mtdblock11 (backup) NG,rebooting..");
  887. if (pAlarmCode != NULL) {
  888. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  889. }
  890. sleep(5);
  891. system("reboot -f");
  892. sleep(5);
  893. system("reboot -f");
  894. }
  895. memset(buf, 0, MtdBlockSize);
  896. wrd = read(fd, buf, MtdBlockSize);
  897. close(fd);
  898. if (wrd < MtdBlockSize) {
  899. free(buf);
  900. log_error("read backup SysConfigData data NG,rebooting..");
  901. if (pAlarmCode != NULL) {
  902. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  903. }
  904. sleep(5);
  905. system("reboot -f");
  906. sleep(5);
  907. system("reboot -f");
  908. }
  909. ChkSum = 0;
  910. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  911. ChkSum += buf[wrd];
  912. }
  913. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  914. //================================================
  915. // Load configuration from mtdblock12 (Factory default)
  916. //================================================
  917. if (ChkSum != ChkSumOrg) {
  918. log_warn("backup SysConfigData checksum NG, read Factory default");
  919. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  920. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  921. if (fd < 0) {
  922. log_error("open mtdblock12 (Factory default) NG,rebooting..");
  923. free(buf);
  924. if (pAlarmCode != NULL) {
  925. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  926. }
  927. sleep(5);
  928. system("reboot -f");
  929. sleep(5);
  930. system("reboot -f");
  931. }
  932. memset(buf, 0, MtdBlockSize);
  933. wrd = read(fd, buf, MtdBlockSize);
  934. close(fd);
  935. if (wrd < MtdBlockSize) {
  936. log_error("read factory default SysConfigData data NG,rebooting..");
  937. free(buf);
  938. if (pAlarmCode != NULL) {
  939. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  940. }
  941. sleep(5);
  942. system("reboot -f");
  943. sleep(5);
  944. system("reboot -f");
  945. }
  946. ChkSum = 0;
  947. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  948. ChkSum += buf[wrd];
  949. }
  950. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  951. memcpy(buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev)), &pSysConfig->ModelName, ARRAY_SIZE(pSysConfig->ModelName));
  952. memcpy(buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev) + ARRAY_SIZE(pSysConfig->ModelName) + ARRAY_SIZE(pSysConfig->AcModelName)), &pSysConfig->SerialNumber, ARRAY_SIZE(pSysConfig->SerialNumber));
  953. if (ChkSum != ChkSumOrg) {
  954. log_warn("factory default SysConfigData checksum NG, restore factory default");
  955. free(buf);
  956. system("cd /root;./FactoryConfig -m");
  957. system("rm -f /Storage/OCPP/OCPPConfiguration");
  958. system("sync");
  959. sleep(5);
  960. system("reboot -f");
  961. sleep(5);
  962. system("reboot -f");
  963. return FAIL;
  964. }
  965. }
  966. }
  967. //load OK
  968. memcpy(pSysConfig, buf, sizeof(struct SysConfigData));
  969. free(buf);
  970. system("rm -f /mnt/EvseConfig.bin");
  971. // SysConfig in flash is empty (0xffffffff)
  972. if ((strlen((char *)pSysConfig->ModelName) > ARRAY_SIZE(pSysConfig->ModelName)) ||
  973. (strlen((char *)pSysConfig->SerialNumber) > ARRAY_SIZE(pSysConfig->SerialNumber)) ||
  974. (strlen((char *)pSysConfig->SystemId) > ARRAY_SIZE(pSysConfig->SystemId)) ||
  975. (pSysConfig->Eth0Interface.EthDhcpClient == 0xff)) {
  976. if (strlen((char *)pSysConfig->ModelName) > ARRAY_SIZE(pSysConfig->ModelName)) {
  977. memset(pSysConfig->ModelName, 0x00, ARRAY_SIZE(pSysConfig->ModelName));
  978. }
  979. if (strlen((char *)pSysConfig->SerialNumber) > ARRAY_SIZE(pSysConfig->SerialNumber)) {
  980. memset(pSysConfig->SerialNumber, 0x00, ARRAY_SIZE(pSysConfig->SerialNumber));
  981. }
  982. if (strlen((char *)pSysConfig->SystemId) > ARRAY_SIZE(pSysConfig->SystemId)) {
  983. memset(pSysConfig->SystemId, 0x00, ARRAY_SIZE(pSysConfig->SystemId));
  984. }
  985. if (pSysConfig->Eth0Interface.EthDhcpClient == 0xff) {
  986. log_info("Ethernet dhcp config is null.");
  987. }
  988. if (strlen((char *)pSysConfig->ModelName) == 0x00) {
  989. log_info("Model name over length.");
  990. }
  991. if (strlen((char *)pSysConfig->SerialNumber) == 0x00) {
  992. log_info("Model serial number over length.");
  993. }
  994. if (strlen((char *)pSysConfig->SystemId) == 0x00) {
  995. log_info("SystemId over length.");
  996. }
  997. system("cd /root;./FactoryConfig -m");
  998. sleep(3);
  999. system("/usr/bin/run_evse_restart.sh");
  1000. }
  1001. log_info("Load SysConfigData OK");
  1002. return PASS;
  1003. }
  1004. int Initialization(void)
  1005. {
  1006. uint8_t count = 0;
  1007. uint8_t pinOut[2] = {116, 115};
  1008. // 初始化卡號驗證的 Flag
  1009. ClearAuthorizedFlag();
  1010. for (count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  1011. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  1012. // 初始化插槍驗證的 Flag
  1013. ClearDetectPluginFlag(count);
  1014. pDcChargingInfo->RemoteStartFlag = NO;
  1015. if (pDcChargingInfo->Type == _Type_Chademo) {
  1016. gpio_set_value(pinOut[count], 0x00);
  1017. ShmCHAdeMOData->evse[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  1018. } else if (pDcChargingInfo->Type == _Type_GB) {
  1019. gpio_set_value(pinOut[count], 0x00);
  1020. ShmGBTData->evse[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  1021. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1022. //if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) { //DS60-120 remove
  1023. if (pSysConfig->TotalConnectorCount == 1) {
  1024. gpio_set_value(pinOut[1], 0x01);
  1025. } else {
  1026. gpio_set_value(pinOut[count], 0x01);
  1027. }
  1028. ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  1029. //}
  1030. }
  1031. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1032. }
  1033. for (count = 0; count < pSysConfig->AcConnectorCount; count++) {
  1034. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  1035. pAcChargingInfo->RemoteStartFlag = NO;
  1036. if (pAcChargingInfo->Type == _Type_AC) {
  1037. pAcChargingInfo->SelfTest_Comp = NO;
  1038. strcpy((char *)ShmOCPP16Data->StatusNotification[count + pSysConfig->TotalConnectorCount].ErrorCode, "NoError");
  1039. }
  1040. }
  1041. return PASS;
  1042. }
  1043. bool InitialSystemDefaultConfig()
  1044. {
  1045. bool result = true;
  1046. LoadSysConfigAndInfo();
  1047. InitGPIO();
  1048. InitEthernet();
  1049. GetMacAddress();
  1050. return result;
  1051. }
  1052. //顯示自檢錯誤原因,觸發對應的flag
  1053. bool DisplaySelfTestFailReason()
  1054. {
  1055. bool result = false;
  1056. uint8_t index = 0;
  1057. // RB、FB、407、EV 小板中有些板子無回應
  1058. if (ShmRelayModuleData->SelfTest_Comp == NO) {
  1059. pAlarmCode->AlarmEvents.bits.RelayboardStestFail = true;
  1060. }
  1061. if (ShmFanModuleData->SelfTest_Comp == NO) {
  1062. pAlarmCode->AlarmEvents.bits.FanboardStestFail = true;
  1063. }
  1064. if (ShmPrimaryMcuData->SelfTest_Comp == NO) {
  1065. pAlarmCode->AlarmEvents.bits.PrimaryStestFail = true;
  1066. }
  1067. if (ShmLedModuleData->SelfTest_Comp == NO) {
  1068. pAlarmCode->AlarmEvents.bits.LedboardStestFail = true;
  1069. }
  1070. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  1071. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1072. if (pDcChargingInfo->Type == _Type_Chademo) {
  1073. if (ShmCHAdeMOData->evse[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1074. pAlarmCode->AlarmEvents.bits.ChademoboardStestFail = true;
  1075. }
  1076. } else if (pDcChargingInfo->Type == _Type_GB) {
  1077. if (ShmGBTData->evse[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1078. pAlarmCode->AlarmEvents.bits.GbtboardStestFail = true;
  1079. }
  1080. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1081. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1082. if (ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1083. pAlarmCode->AlarmEvents.bits.CCSboardStestFail = true;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. for (index = 0; index < pSysConfig->AcConnectorCount; index++) {
  1089. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(index);
  1090. // 先借 GBT 顯示
  1091. if (pAcChargingInfo->SelfTest_Comp == NO) {
  1092. pAlarmCode->AlarmEvents.bits.AcConnectorStestFail = true;
  1093. }
  1094. }
  1095. if (pSysInfo->AcContactorStatus == NO) {
  1096. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox &&!defined DD360UCar
  1097. // AC Contact 未搭上
  1098. pAlarmCode->AlarmEvents.bits.AcContactStestFail = true;
  1099. result = true;
  1100. #endif // !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  1101. }
  1102. //else if (pAlarmCode->AlarmEvents.bits.PsuDipSwitchStestFail == YES) { //DS60-120 add
  1103. // result = true;
  1104. //}
  1105. else if (ShmPsuData->SystemAvailablePower <= 0 &&
  1106. ShmPsuData->SystemAvailableCurrent <= 0) {
  1107. // PSU 通訊問題
  1108. pAlarmCode->AlarmEvents.bits.PsuModuleStestFail = true;
  1109. result = true;
  1110. }
  1111. return result;
  1112. }
  1113. //===============================================
  1114. // Common Detect Chk - Stop Charging ?
  1115. //===============================================
  1116. int isEvBoardStopChargeFlag(uint8_t gunIndex)
  1117. {
  1118. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1119. //printf("StopChargeFlag = %d", chargingInfo[gunIndex]->StopChargeFlag);
  1120. return pDcChargingInfo->StopChargeFlag;
  1121. }
  1122. bool isEvBoardNormalStopChargeFlag(uint8_t gunIndex)
  1123. {
  1124. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1125. return pDcChargingInfo->NormalStopChargeFlag;
  1126. }
  1127. //===============================================
  1128. // 掃描插槍狀況
  1129. //===============================================
  1130. void ClearDetectPluginFlag(int gunIndex)
  1131. {
  1132. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1133. if (pDcChargingInfo->RemoteStartFlag == YES) {
  1134. pDcChargingInfo->RemoteStartFlag = NO;
  1135. }
  1136. ShmDcCommonData->pGunInfo[gunIndex].WaitForPlugit = NO;
  1137. if (pSysInfo->OrderCharging != NO_DEFINE) {
  1138. pSysInfo->OrderCharging = NO_DEFINE;
  1139. }
  1140. }
  1141. void DetectPluginStart(int gunIndex)
  1142. {
  1143. //pSysInfo->WaitForPlugit = YES;
  1144. ShmDcCommonData->pGunInfo[gunIndex].WaitForPlugit = YES;
  1145. }
  1146. bool isDetectPlugin(int gunIndex)
  1147. {
  1148. if (ShmDcCommonData->pGunInfo[gunIndex].WaitForPlugit == YES) {
  1149. return true;
  1150. }
  1151. /*
  1152. if (pSysInfo->WaitForPlugit == YES) {
  1153. return true;
  1154. }
  1155. */
  1156. return false;
  1157. }
  1158. //===============================================
  1159. // Common Detect Chk - Chademo
  1160. //===============================================
  1161. bool isEvGunLocked_chademo(uint8_t gunIndex)
  1162. {
  1163. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1164. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1165. }
  1166. bool isEvContactorWelding_chademo(uint8_t gunIndex)
  1167. {
  1168. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1169. return DetectBitValue(ShmCHAdeMOData->ev[pDcChargingInfo->type_index].EvDetection, 3);
  1170. }
  1171. bool isEvStopReq_chademo(uint8_t gunIndex)
  1172. {
  1173. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1174. return DetectBitValue(ShmCHAdeMOData->ev[pDcChargingInfo->type_index].EvDetection, 4);
  1175. }
  1176. bool isEvStopCharging_chademo(uint8_t gunIndex)
  1177. {
  1178. if (isEvGunLocked_chademo(gunIndex) == NO) {
  1179. // 無鎖槍 = 停止
  1180. //log_info("gun locked none (%d) ", gunIndex);
  1181. return YES;
  1182. }
  1183. return NO;
  1184. }
  1185. uint8_t isPrechargeStatus_chademo(uint8_t gunIndex)
  1186. {
  1187. uint8_t result = 0x00;
  1188. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1189. result = ShmCHAdeMOData->ev[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1190. return result;
  1191. }
  1192. //===============================================
  1193. // Common Detect Chk - GB
  1194. //===============================================
  1195. bool isEvGunLocked_gb(uint8_t gunIndex)
  1196. {
  1197. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1198. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1199. }
  1200. bool isEvStopCharging_gb(uint8_t gunIndex)
  1201. {
  1202. if (isEvGunLocked_gb(gunIndex) == NO) {
  1203. // 無鎖槍 = 停止
  1204. //log_info("gun locked none. ");
  1205. return YES;
  1206. }
  1207. return NO;
  1208. }
  1209. uint8_t isPrechargeStatus_gb(uint8_t gunIndex)
  1210. {
  1211. uint8_t result = 0x00;
  1212. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1213. result = ShmGBTData->ev[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1214. return result;
  1215. }
  1216. //===============================================
  1217. // Common Detect Chk - CCS
  1218. //===============================================
  1219. bool isEvGunLocked_ccs(uint8_t gunIndex)
  1220. {
  1221. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1222. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1223. }
  1224. uint8_t isPrechargeStatus_ccs(uint8_t gunIndex)
  1225. {
  1226. //uint8_t result = 0x00;
  1227. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1228. return ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1229. }
  1230. bool isEvStopCharging_ccs(uint8_t gunIndex)
  1231. {
  1232. if (isEvGunLocked_ccs(gunIndex) == NO) {
  1233. // 無鎖槍 = 停止
  1234. //log_info("gun locked none. ");
  1235. return YES;
  1236. }
  1237. return NO;
  1238. }
  1239. //===============================================
  1240. // Callback
  1241. //===============================================
  1242. void DisplayChargingInfo()
  1243. {
  1244. uint8_t i = 0;
  1245. log_info("*********** DisplayChargingInfo *********** ");
  1246. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1247. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1248. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  1249. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  1250. ChangeGunSelectByIndex(i);
  1251. return;
  1252. }
  1253. }
  1254. if (pSysConfig->AcConnectorCount > 0 &&
  1255. pSysInfo->CurGunSelectedByAc == NO_DEFINE)
  1256. {
  1257. pAcChargingInfo = (struct ChargingInfoData*)GetAcChargingInfoData(0);
  1258. if (pAcChargingInfo->SystemStatus >= S_PREPARNING &&
  1259. pAcChargingInfo->SystemStatus <= S_COMPLETE)
  1260. {
  1261. pSysInfo->CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1262. }
  1263. }
  1264. usleep(50000);
  1265. systemPageRestoreInit();
  1266. }
  1267. void _AutoReturnTimeout(int gunIndex)
  1268. {
  1269. log_info("*********** _AutoReturnTimeout(%d) *********** ", pSysInfo->PageIndex);
  1270. if (pSysInfo->PageIndex == _PAGE_PRECHARGE) {
  1271. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1272. pDcChargingInfo->SystemStatus = S_ALARM;
  1273. //pSysInfo->SystemPage = _LCM_ERROR;
  1274. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  1275. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  1276. ShmDcCommonData->PreAuth_Result = 0;
  1277. //pSysInfo->SystemPage = _PAGE_PAYING;
  1278. //StartGunInfoTimeoutDet(pSysInfo->CurGunSelected,Timeout_FinalCost);
  1279. ClearDetectPluginFlag(gunIndex);
  1280. }
  1281. }
  1282. void _SelfTestTimeout(void)
  1283. {
  1284. if (pSysInfo->BootingStatus != BOOT_COMPLETE) {
  1285. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  1286. setChargerMode(gunIndex, MODE_ALARM);
  1287. }
  1288. }
  1289. ShmPsuData->Work_Step = _NO_WORKING;
  1290. pSysInfo->SelfTestSeq = _STEST_FAIL;
  1291. log_info("Self test timeout. ");
  1292. }
  1293. void _AuthorizedTimeout(void)
  1294. {
  1295. int i;
  1296. //if (IsAuthorizingMode()) {
  1297. log_info("*********** _AuthorizedTimeout *********** ");
  1298. StopSystemTimeoutDet();
  1299. //isCardScan = false;
  1300. SetIsCardScan(false);
  1301. //StopSystemTimeoutDet();
  1302. StopGunInfoTimeoutDet(pSysInfo->CurGunSelected);
  1303. ShmDcCommonData->TradeCancel = TRUE;
  1304. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  1305. ShmDcCommonData->PreAuth_Result = 0;
  1306. pSysInfo->SystemPage = _PAGE_SENSING;
  1307. StartSystemTimeoutDet(Timeout_TradeCancel);
  1308. for (i = 0; i <= 30; i++) {
  1309. if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_IDLE)
  1310. break;
  1311. sleep(1);
  1312. }
  1313. ShmDcCommonData->PreAuth_Result = 0;
  1314. StopSystemTimeoutDet();
  1315. ClearDetectPluginFlag(pSysInfo->CurGunSelected);
  1316. strcpy((char*)pSysConfig->UserId, "");
  1317. ClearAuthorizedFlag();
  1318. setChargerMode(pSysInfo->CurGunSelected, S_IDLE);
  1319. // StartSystemTimeoutDet(Timeout_ReturnViewPage);
  1320. //}
  1321. }
  1322. void _DetectPlugInTimeout(uint8_t gunIndex)
  1323. {
  1324. int i;
  1325. log_info("*********** Gun%d _DetectPlugInTimeout *********** ",gunIndex);
  1326. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  1327. StopSystemTimeoutDet();
  1328. StopGunInfoTimeoutDet(gunIndex);
  1329. if (pDcChargingInfo->RemoteStartFlag || pDcChargingInfo->isRemoteStart) {
  1330. StopSystemTimeoutDet();
  1331. ClearDetectPluginFlag(gunIndex);
  1332. strcpy((char*)pSysConfig->UserId, "");
  1333. setChargerMode(gunIndex, S_IDLE);
  1334. return;
  1335. }
  1336. pSysInfo->CurGunSelected = gunIndex;
  1337. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  1338. ShmDcCommonData->PreAuth_Result = 0;
  1339. pSysInfo->SystemPage = _PAGE_SENSING;
  1340. ShmDcCommonData->TradeCancel = TRUE;
  1341. StartSystemTimeoutDet(Timeout_TradeCancel);
  1342. for(i=0;i<=30;i++) {
  1343. if(ShmDcCommonData->PreAuth_Config == _CREDITCARD_IDLE)
  1344. break;
  1345. sleep(1);
  1346. }
  1347. ShmDcCommonData->PreAuth_Result = 0;
  1348. if (pSysInfo->CurGunSelected == gunIndex) {
  1349. systemPageRestoreInit();
  1350. }
  1351. StopSystemTimeoutDet();
  1352. ClearDetectPluginFlag(gunIndex);
  1353. strcpy((char *)pSysConfig->UserId, "");
  1354. setChargerMode(gunIndex, S_IDLE);
  1355. //systemPageRestoreInit();
  1356. }
  1357. void _DetectEvChargingEnableTimeout(uint8_t gunIndex)
  1358. {
  1359. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1360. if (pDcChargingInfo->Type == _Type_Chademo) {
  1361. if (!isEvGunLocked_chademo(gunIndex)) {
  1362. log_info("*********** _DetectEvChargingEnableTimeout (chademo) ***********");
  1363. }
  1364. } else if (pDcChargingInfo->Type == _Type_GB) {
  1365. if (!isEvGunLocked_ccs(gunIndex)) {
  1366. log_info("*********** _DetectEvChargingEnableTimeout (gb) ***********");
  1367. }
  1368. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1369. if (!isEvGunLocked_ccs(gunIndex)) {
  1370. log_info("*********** _DetectEvChargingEnableTimeout (ccs) ***********");
  1371. }
  1372. }
  1373. //pSysInfo->SystemPage = _LCM_ERROR;
  1374. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  1375. ChargingTerminalProcess(gunIndex);
  1376. _AutoReturnTimeout(gunIndex);
  1377. }
  1378. void _DetectEvseChargingEnableTimeout(uint8_t gunIndex)
  1379. {
  1380. log_info("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********");
  1381. ChargingTerminalProcess(gunIndex);
  1382. //pSysInfo->SystemPage = _LCM_ERROR;
  1383. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  1384. _AutoReturnTimeout(gunIndex);
  1385. }
  1386. void _PrepareTimeout(uint8_t gunIndex)
  1387. {
  1388. log_info("*********** _PrepareTimeout ***********");
  1389. ChargingTerminalProcess(gunIndex);
  1390. pAlarmCode->AlarmEvents.bits.PsuNoResource = YES;
  1391. //pSysInfo->SystemPage = _LCM_ERROR;
  1392. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  1393. _AutoReturnTimeout(gunIndex);
  1394. }
  1395. void _CcsPrechargeTimeout(uint8_t gunIndex)
  1396. {
  1397. log_info("*********** _CcsPrechargeTimeout ***********");
  1398. ChargingTerminalProcess(gunIndex);
  1399. //pSysInfo->SystemPage = _LCM_ERROR;
  1400. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  1401. }
  1402. void _LinkErrorTimeout(uint8_t gunIndex)
  1403. {
  1404. log_info("*********** _LinkErrorTimeout ***********");
  1405. setChargerMode(pSysInfo->CurGunSelected, S_IDLE);
  1406. systemPageRestoreInit();
  1407. }
  1408. //===============================================
  1409. // 取得卡號與卡號驗證
  1410. //===============================================
  1411. void AuthorizingStart(void)
  1412. {
  1413. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1414. pSysInfo->AuthorizeFlag = YES;
  1415. }
  1416. void ClearAuthorizedFlag(void)
  1417. {
  1418. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1419. pSysInfo->AuthorizeFlag = NO;
  1420. }
  1421. bool isAuthorizedComplete(void)
  1422. {
  1423. if (pSysInfo->AuthorizeFlag == YES) {
  1424. return false;
  1425. }
  1426. return true;
  1427. }
  1428. bool IsAuthorizingMode()
  1429. {
  1430. if (pSysInfo->AuthorizeFlag == NO) {
  1431. return false;
  1432. }
  1433. return true;
  1434. }
  1435. //===============================================
  1436. // 紀錄 Alarm Code
  1437. //===============================================
  1438. void ResetChargerAlarmCode(uint8_t gunIndex, char *code)
  1439. {
  1440. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1441. if (strcmp(code, "012234") == EQUAL) {
  1442. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  1443. } else if (strcmp(code, "012235") == EQUAL) {
  1444. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  1445. } else if (strcmp(code, "012236") == EQUAL) {
  1446. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  1447. } else if (strcmp(code, "012288") == EQUAL) {
  1448. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  1449. } else if (strcmp(code, "012289") == EQUAL) {
  1450. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  1451. } else if (strcmp(code, "012290") == EQUAL) {
  1452. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  1453. } else if (strcmp(code, "012229") == EQUAL) {
  1454. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  1455. } else if (strcmp(code, "012230") == EQUAL) {
  1456. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  1457. } else if (strcmp(code, "012231") == EQUAL) {
  1458. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  1459. } else if (strcmp(code, "011011") == EQUAL) {
  1460. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  1461. } else if (strcmp(code, "011013") == EQUAL) {
  1462. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  1463. } else if (strcmp(code, "011015") == EQUAL) {
  1464. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  1465. } else if (strcmp(code, "011012") == EQUAL) {
  1466. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  1467. } else if (strcmp(code, "011014") == EQUAL) {
  1468. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  1469. } else if (strcmp(code, "011016") == EQUAL) {
  1470. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  1471. } else if (strcmp(code, "011018") == EQUAL) {
  1472. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  1473. } else if (strcmp(code, "011019") == EQUAL) {
  1474. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  1475. } else if (strcmp(code, "011020") == EQUAL) {
  1476. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  1477. } else if (strcmp(code, "012323") == EQUAL) {
  1478. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  1479. } else if (strcmp(code, "011038") == EQUAL) {
  1480. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  1481. }
  1482. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012229", 6) == EQUAL ||
  1483. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012230", 6) == EQUAL ||
  1484. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012231", 6) == EQUAL ||
  1485. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011011", 6) == EQUAL ||
  1486. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011013", 6) == EQUAL ||
  1487. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011015", 6) == EQUAL ||
  1488. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011012", 6) == EQUAL ||
  1489. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011014", 6) == EQUAL ||
  1490. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011016", 6) == EQUAL ||
  1491. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011018", 6) == EQUAL ||
  1492. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011019", 6) == EQUAL ||
  1493. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011020", 6) == EQUAL ||
  1494. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012323", 6) == EQUAL ||
  1495. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011038", 6) == EQUAL) {
  1496. strncpy((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6);
  1497. }
  1498. }
  1499. void RecordAlarmCode(uint8_t gunIndex, char *code)
  1500. {
  1501. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1502. if((pDcChargingInfo->SystemStatus > S_IDLE && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1503. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1504. {
  1505. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1506. memcpy(pDcChargingInfo->ConnectorAlarmCode, code, 6);
  1507. }
  1508. if (pDcChargingInfo->StopChargeFlag == NO)
  1509. {
  1510. log_info("RecordAlarmCode set Stop Charge Flag");
  1511. pDcChargingInfo->StopChargeFlag = YES;
  1512. }
  1513. }
  1514. }
  1515. void ReleaseAlarmCode(uint8_t gunIndex)
  1516. {
  1517. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1518. //if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1519. // return;
  1520. //}
  1521. switch (pDcChargingInfo->Type) {
  1522. case _Type_Chademo:
  1523. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  1524. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  1525. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP = NO;
  1526. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  1527. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  1528. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  1529. break;
  1530. case _Type_CCS_2:
  1531. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  1532. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  1533. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP = NO;
  1534. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  1535. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  1536. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  1537. break;
  1538. case _Type_GB:
  1539. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  1540. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  1541. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP = NO;
  1542. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  1543. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  1544. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  1545. break;
  1546. }
  1547. memcpy(pDcChargingInfo->ConnectorAlarmCode, "", 6);
  1548. }
  1549. //===============================================
  1550. // EmergencyStop and Charging Stop
  1551. //===============================================
  1552. void ChargingTerminalProcess(uint8_t gunIndex)
  1553. {
  1554. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  1555. //if (pDcChargingInfo->SystemStatus == S_CHARGING)
  1556. //ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  1557. setChargerMode(gunIndex, MODE_TERMINATING);
  1558. }
  1559. void ChargingAlarmProcess(uint8_t gunIndex)
  1560. {
  1561. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  1562. //if (pDcChargingInfo->SystemStatus == S_CHARGING)
  1563. //ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  1564. UpdateErrorCodeToOcpp(gunIndex);
  1565. setChargerMode(gunIndex, MODE_ALARM);
  1566. }
  1567. void AcChargingTerminalProcess(void)
  1568. {
  1569. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  1570. pAcChargingInfo->SystemStatus = MODE_TERMINATING;
  1571. }
  1572. void StopChargingProcessByString(uint8_t level)
  1573. {
  1574. if (level > pSysWarning->Level) {
  1575. pSysWarning->Level = level;
  1576. }
  1577. }
  1578. void ReleaseChargingProcessByString(uint8_t level)
  1579. {
  1580. if (level >= pSysWarning->Level) {
  1581. pSysWarning->Level = WARN_LV_NL;
  1582. }
  1583. }
  1584. // 一般錯誤停止充電處理函式
  1585. void BoardErrOccurByString(uint8_t index, char *code)
  1586. {
  1587. uint8_t level = 1;
  1588. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1589. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1590. pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1591. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1592. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1593. if (strncmp(code, "023730", 6) == EQUAL &&
  1594. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop == NO
  1595. ) {
  1596. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  1597. }
  1598. ChargingTerminalProcess(index);
  1599. }
  1600. StopChargingProcessByString(level);
  1601. }
  1602. void ReleaseBoardErrOccurByString(uint8_t index, char *code)
  1603. {
  1604. bool isTrigger = false;
  1605. uint8_t level = 1;
  1606. if (strncmp(code, "023730", 6) == 0 &&
  1607. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop == YES) {
  1608. isTrigger = true;
  1609. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1610. }
  1611. if (isTrigger) {
  1612. ReleaseChargingProcessByString(level);
  1613. }
  1614. }
  1615. // 急停狀況的停止充電處理函式
  1616. void EmcOccureByString(char *code)
  1617. {
  1618. uint8_t level = 2;
  1619. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  1620. // 其錯誤等級為 2
  1621. //DS60-120 remove
  1622. if (strncmp(code, "012251", 6) == EQUAL ||
  1623. strncmp(code, "012252", 6) == EQUAL ||
  1624. strncmp(code, "012238", 6) == EQUAL ||
  1625. strncmp(code, "042251", 6) == EQUAL ||
  1626. strncmp(code, "042252", 6) == EQUAL ||
  1627. strncmp(code, "012304", 6) == EQUAL ||
  1628. strncmp(code, "042327", 6) == EQUAL ||
  1629. strncmp(code, "042328", 6) == EQUAL ||
  1630. strncmp(code, "042200", 6) == EQUAL ||
  1631. strncmp(code, "042201", 6) == EQUAL ||
  1632. strncmp(code, "042202", 6) == EQUAL ||
  1633. strncmp(code, "042267", 6) == EQUAL) {
  1634. for (uint8_t gun = 0; gun < pSysConfig->TotalConnectorCount; gun++) {
  1635. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gun);
  1636. //strncpy((char *)ShmOCPP16Data->StatusNotification[gun].VendorErrorCode, code, 6);
  1637. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1638. pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1639. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1640. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1641. //ChargingTerminalProcess(gun);
  1642. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1643. memcpy(pDcChargingInfo->ConnectorAlarmCode, code, 6);
  1644. }
  1645. ChargingAlarmProcess(gun);
  1646. }
  1647. }
  1648. StopChargingProcessByString(level);
  1649. InformOcppErrOccur(4);
  1650. }
  1651. }
  1652. void ReleaseEmsOccureByString(uint8_t index, char *code)
  1653. {
  1654. bool isTrigger = false;
  1655. uint8_t level = 2;
  1656. if (strncmp(code, "042251", 6) == 0 ) {
  1657. isTrigger = true;
  1658. } else if (strncmp(code, "012251", 6) == 0 &&
  1659. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES) {
  1660. isTrigger = true;
  1661. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip = NO;
  1662. } else if (strncmp(code, "012252", 6) == 0 &&
  1663. pAlarmCode->AlarmEvents.bits.DoorOpen == YES) {
  1664. isTrigger = true;
  1665. pAlarmCode->AlarmEvents.bits.DoorOpen = NO;
  1666. } else if (strncmp(code, "012237", 6) == 0 &&
  1667. pAlarmCode->AlarmEvents.bits.SpdTrip == YES) {
  1668. isTrigger = true;
  1669. pAlarmCode->AlarmEvents.bits.SpdTrip = NO;
  1670. } else if (strncmp(code, "012238", 6) == 0 &&
  1671. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  1672. isTrigger = true;
  1673. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1674. }
  1675. if (isTrigger) {
  1676. ReleaseChargingProcessByString(level);
  1677. InformOcppErrOccur(6);
  1678. }
  1679. }
  1680. static void checkOvpState(uint8_t gunIndex, uint8_t gunType)
  1681. {
  1682. switch(gunType) {
  1683. case _Type_Chademo:
  1684. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP == YES)
  1685. RecordAlarmCode(gunIndex, "012217");
  1686. break;
  1687. case _Type_CCS_2:
  1688. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP == YES)
  1689. RecordAlarmCode(gunIndex, "012219");
  1690. break;
  1691. case _Type_GB:
  1692. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP == YES)
  1693. RecordAlarmCode(gunIndex, "012221");
  1694. break;
  1695. }
  1696. }
  1697. //===============================================
  1698. // 確認各小板偵測的錯誤狀況
  1699. //===============================================
  1700. void CheckErrorOccurStatus(uint8_t index)
  1701. {
  1702. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1703. // RB
  1704. if (pSysConfig->PhaseLossPolicy == YES) {
  1705. if (pAlarmCode->AlarmEvents.bits.SystemL1InputUVP == YES ||
  1706. pAlarmCode->AlarmEvents.bits.SystemL2InputUVP == YES ||
  1707. pAlarmCode->AlarmEvents.bits.SystemL3InputUVP == YES) {
  1708. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  1709. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  1710. StopChargingProcessByString(2);
  1711. InformOcppErrOccur(13);
  1712. }
  1713. //DS60-120 add -----
  1714. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1715. if (pAlarmCode->AlarmEvents.bits.SystemL1InputUVP == YES) {
  1716. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012203", 6);
  1717. } else if (pAlarmCode->AlarmEvents.bits.SystemL2InputUVP == YES) {
  1718. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012204", 6);
  1719. } else if (pAlarmCode->AlarmEvents.bits.SystemL3InputUVP == YES) {
  1720. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012205", 6);
  1721. }
  1722. }
  1723. log_info("1 CheckErrorOccurStatus");
  1724. pDcChargingInfo->StopChargeFlag = YES;
  1725. //------------------------------------------------------------------
  1726. } else {
  1727. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  1728. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1729. ReleaseChargingProcessByString(2);
  1730. InformOcppErrOccur(6);
  1731. }
  1732. }
  1733. } else {
  1734. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  1735. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1736. ReleaseChargingProcessByString(2);
  1737. InformOcppErrOccur(6);
  1738. }
  1739. }
  1740. if (pAlarmCode->AlarmEvents.bits.SystemL1InputOVP == YES ||
  1741. pAlarmCode->AlarmEvents.bits.SystemL2InputOVP == YES ||
  1742. pAlarmCode->AlarmEvents.bits.SystemL3InputOVP == YES) {
  1743. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  1744. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  1745. StopChargingProcessByString(2);
  1746. InformOcppErrOccur(14);
  1747. }
  1748. //DS60-120 -----
  1749. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1750. if (pAlarmCode->AlarmEvents.bits.SystemL1InputOVP == YES) {
  1751. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012200", 6);
  1752. } else if (pAlarmCode->AlarmEvents.bits.SystemL2InputOVP == YES) {
  1753. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012201", 6);
  1754. } else if (pAlarmCode->AlarmEvents.bits.SystemL3InputOVP == YES) {
  1755. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012202", 6);
  1756. }
  1757. }
  1758. log_info("2 CheckErrorOccurStatus");
  1759. pDcChargingInfo->StopChargeFlag = YES;
  1760. //----------------------------------------------------------------------
  1761. } else {
  1762. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP) {
  1763. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1764. ReleaseChargingProcessByString(2);
  1765. InformOcppErrOccur(6);
  1766. }
  1767. }
  1768. checkOvpState(index, pDcChargingInfo->Type);
  1769. //--------------------------------------------------------------------------
  1770. if (strlen((char *)pDcChargingInfo->ConnectorAlarmCode) == 0) {
  1771. //Primary
  1772. if (pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES) {
  1773. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012251", 6);
  1774. } else if (pAlarmCode->AlarmEvents.bits.DoorOpen == YES) {
  1775. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012252", 6);
  1776. } else if (pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  1777. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012238", 6);
  1778. } else if (pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES) {
  1779. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012304", 6);
  1780. } else if (pInfoCode->InfoEvents.bits.BackendDisconnectedViaEthernet == YES) {
  1781. memcpy(pDcChargingInfo->ConnectorAlarmCode, "033900", 6);
  1782. }
  1783. //Chiller temperature
  1784. //else if (pAlarmCode->AlarmEvents.bits.SystemChillerOTP == YES) {
  1785. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "012323", 6);
  1786. //} else if (pFaultCode->FaultEvents.bits.ChillerTempSensorBroken == YES) {
  1787. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "011038", 6);
  1788. //}
  1789. }
  1790. /*
  1791. if (pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES ||
  1792. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES ||
  1793. pAlarmCode->AlarmEvents.bits.DoorOpen == YES ||
  1794. pSysWarning->ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  1795. pAlarmCode->AlarmEvents.bits.PsuFailureAlarm == YES ||
  1796. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES ||
  1797. //Power cabinet alarm status
  1798. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop == YES ||
  1799. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen == YES ||
  1800. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP == YES ||
  1801. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP == YES ||
  1802. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP == YES ||
  1803. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP == YES ||
  1804. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP == YES ||
  1805. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure == YES
  1806. ) {
  1807. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1808. pDcChargingInfo->SystemStatus < S_COMPLETE) ||
  1809. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1810. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1 ))
  1811. ChargingAlarmProcess(index);
  1812. pSysWarning->Level = WARN_LV_ER;
  1813. }*/
  1814. }
  1815. //===============================================
  1816. // 確認 GPIO 狀態
  1817. //===============================================
  1818. void gpio_set_value(unsigned int gpio, unsigned int value)
  1819. {
  1820. int fd;
  1821. char buf[MAX_BUF];
  1822. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1823. fd = open(buf, O_WRONLY);
  1824. if (fd < 0) {
  1825. perror("gpio/set-value");
  1826. return;
  1827. }
  1828. if (value) {
  1829. write(fd, "1", 2);
  1830. } else {
  1831. write(fd, "0", 2);
  1832. }
  1833. close(fd);
  1834. }
  1835. int gpio_get_value(unsigned int gpio, unsigned int *value)
  1836. {
  1837. int fd;
  1838. char buf[MAX_BUF];
  1839. char ch;
  1840. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1841. fd = open(buf, O_RDONLY);
  1842. if (fd < 0) {
  1843. perror("gpio/get-value");
  1844. return fd;
  1845. }
  1846. read(fd, &ch, 1);
  1847. if (ch != '0') {
  1848. *value = 1;
  1849. } else {
  1850. *value = 0;
  1851. }
  1852. close(fd);
  1853. return 0;
  1854. }
  1855. void CheckGunTypeFromHw()
  1856. {
  1857. uint8_t i = 0;
  1858. int pinIn[4] = {22, 23, 44, 45};
  1859. unsigned int gpioValue = 0;
  1860. uint8_t tmp[2] = {0};
  1861. log_info("ModelName = %s", pSysConfig->ModelName);
  1862. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1863. gpio_get_value(pinIn[i], &gpioValue);
  1864. switch (pinIn[i]) {
  1865. //right slot
  1866. case 22:
  1867. bd1_1_status = gpioValue;
  1868. break;
  1869. case 23:
  1870. bd1_2_status = gpioValue;
  1871. break;
  1872. //left slot
  1873. case 44:
  1874. bd0_1_status = gpioValue;
  1875. break;
  1876. case 45:
  1877. bd0_2_status = gpioValue;
  1878. break;
  1879. }
  1880. }
  1881. //BD1(Left-CCS-CND1-SMR2-左槍), BD2(Right-CHADEMO-CND2-SMR1-右槍), CCS: 10 , CHAdeMO: 01 , GBT: 11
  1882. //CcsChargingData [0至1] 分別為 Right至Left
  1883. //model name 槍順序左至右分別為Right至Left
  1884. tmp[1] = (bd0_1_status << 4 | bd0_2_status);
  1885. tmp[0] = (bd1_1_status << 4 | bd1_2_status);
  1886. for (i = 0; i < 2; i++) {
  1887. switch (tmp[i]) {
  1888. case 0x01:
  1889. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CHAdeMO");
  1890. break;
  1891. case 0x10:
  1892. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CCS");
  1893. break;
  1894. case 0x11:
  1895. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "GBT");
  1896. break;
  1897. case 0x00:
  1898. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "None");
  1899. break;
  1900. }
  1901. }
  1902. }
  1903. void CheckGpioInStatus()
  1904. {
  1905. int i = 0;
  1906. int pinIn[2] = { 27, 47 };//{IO BD1_2, IO BD2_2}
  1907. unsigned int gpioValue = 0;
  1908. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1909. gpio_get_value(pinIn[i], &gpioValue);
  1910. if (gpioValue == 0x01) {
  1911. switch (pinIn[i]) {
  1912. // 小板緊急停止
  1913. case 47:
  1914. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1915. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1916. if (pDcChargingInfo->slotsIndex == 1) {
  1917. if (pDcChargingInfo->Type == _Type_Chademo) {
  1918. BoardErrOccurByString(i, "023730");
  1919. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1920. BoardErrOccurByString(i, "013627");
  1921. }
  1922. break;
  1923. }
  1924. }
  1925. break;
  1926. case 27:
  1927. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1928. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1929. if (pDcChargingInfo->slotsIndex == 3) {
  1930. if (pDcChargingInfo->Type == _Type_Chademo) {
  1931. BoardErrOccurByString(i, "023730");
  1932. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1933. BoardErrOccurByString(i, "013627");
  1934. }
  1935. break;
  1936. }
  1937. }
  1938. break;
  1939. }
  1940. } else {
  1941. switch (pinIn[i]) {
  1942. // 小板解除緊急停止
  1943. case 47:
  1944. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1945. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1946. if (pDcChargingInfo->slotsIndex == 1) {
  1947. if (pDcChargingInfo->Type == _Type_Chademo) {
  1948. ReleaseBoardErrOccurByString(i, "023730");
  1949. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1950. ReleaseBoardErrOccurByString(i, "013627");
  1951. }
  1952. break;
  1953. }
  1954. }
  1955. break;
  1956. case 27:
  1957. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1958. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1959. if (pDcChargingInfo->slotsIndex == 3) {
  1960. if (pDcChargingInfo->Type == _Type_Chademo) {
  1961. ReleaseBoardErrOccurByString(i, "023730");
  1962. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1963. ReleaseBoardErrOccurByString(i, "013627");
  1964. }
  1965. break;
  1966. }
  1967. }
  1968. break;
  1969. }
  1970. }
  1971. }
  1972. }
  1973. //===============================================
  1974. // Main process
  1975. //===============================================
  1976. // 檢查 uint8_t 中某個 Bit 的值
  1977. // _byte : 欲改變的 byte
  1978. // _bit : 該 byte 的第幾個 bit
  1979. uint8_t DetectBitValue(uint8_t _byte, uint8_t _bit)
  1980. {
  1981. uint8_t mask_table[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
  1982. return ( _byte & mask_table[_bit] ) != 0x00;
  1983. }
  1984. // 設定 Byte 中某個 Bit的值
  1985. // _byte : 欲改變的 byte
  1986. // _bit : 該 byte 的第幾個 bit
  1987. // value : 修改的值為 0 or 1
  1988. void SetBitValue(uint8_t *_byte, uint8_t _bit, uint8_t value)
  1989. {
  1990. if (value == 1) {
  1991. *_byte |= (1 << _bit);
  1992. } else if (value == 0) {
  1993. *_byte ^= (1 << _bit);
  1994. }
  1995. }
  1996. uint8_t isModeChange(uint8_t gunIndex)
  1997. {
  1998. uint8_t result = NO;
  1999. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2000. if (pDcChargingInfo->SystemStatus == pDcChargingInfo->PreviousSystemStatus) {
  2001. return result;
  2002. }
  2003. pDcChargingInfo->PreviousSystemStatus = pDcChargingInfo->SystemStatus;
  2004. ShmDcCommonData->SystemModeChange[gunIndex] = YES; //for Module_EvRxComm print temperature message
  2005. return YES;
  2006. }
  2007. static int checkSlotGpioMappingGunType(uint8_t gunIndex, uint8_t gunType)
  2008. {
  2009. uint8_t slot1 = 0, slot2 = 0;
  2010. if (gunIndex == 0) {
  2011. slot1 = bd0_1_status;
  2012. slot2 = bd0_2_status;
  2013. } else if (gunIndex == 1) {
  2014. slot1 = bd1_1_status;
  2015. slot2 = bd1_2_status;
  2016. }
  2017. log_info("slot %d gpio1 = %d, gpio2 = %d, type = %d",
  2018. gunIndex,
  2019. slot1,
  2020. slot2,
  2021. gunType);
  2022. switch (gunType) {
  2023. case _Type_Chademo:
  2024. if (slot1 != NO && slot2 != YES) {
  2025. return FAIL;
  2026. }
  2027. break;
  2028. case _Type_CCS_2:
  2029. if (slot1 != YES && slot2 != NO) {
  2030. return FAIL;
  2031. }
  2032. break;
  2033. case _Type_GB:
  2034. if (slot1 != YES && slot2 != YES) {
  2035. return FAIL;
  2036. }
  2037. break;
  2038. }
  2039. return PASS;
  2040. }
  2041. bool CheckConnectorTypeStatus(void)
  2042. {
  2043. bool result = true;
  2044. uint8_t gunIndex = 0;
  2045. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  2046. struct ChargingInfoData *pDcChargingInfo = NULL;
  2047. if (!MappingGunChargingInfo("CSU Task")) {
  2048. log_error("CheckConnectorTypeStatus MappingGunChargingInfo failed");
  2049. return false;
  2050. }
  2051. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  2052. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2053. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2054. if (checkSlotGpioMappingGunType(gunIndex, pDcChargingInfo->Type) == FAIL) {
  2055. return false;
  2056. }
  2057. pDcChargingInfo->SystemStatus = S_BOOTING;
  2058. switch (gunIndex) {
  2059. case 0:
  2060. if (pSysConfig->TotalConnectorCount == 1) {
  2061. if ((bd0_1_status == YES || bd0_2_status == YES) &&
  2062. (bd1_1_status == NO && bd1_2_status == NO)) {
  2063. pDcChargingInfo->Evboard_id = 0x01;
  2064. }
  2065. if ((bd0_1_status == NO && bd0_2_status == NO) &&
  2066. (bd1_1_status == YES || bd1_2_status == YES)
  2067. ) {
  2068. pDcChargingInfo->Evboard_id = 0x02;
  2069. }
  2070. break;
  2071. }
  2072. pDcChargingInfo->Evboard_id = 0x01;
  2073. break;
  2074. case 1:
  2075. pDcChargingInfo->Evboard_id = 0x02;
  2076. break;
  2077. }
  2078. CheckHwSlotStatusLog(gunIndex);
  2079. if (pDcChargingInfo->Evboard_id == 0x00) {
  2080. return false;
  2081. }
  2082. }
  2083. AdjustChargerCurrent();
  2084. return result;
  2085. }
  2086. int SpawnTask(void)
  2087. {
  2088. if(SIMULATION)
  2089. system("/root/simulation &");
  2090. sleep(2);
  2091. system("/root/Module_EventLogging &");
  2092. system("/root/Module_PrimaryComm &");
  2093. system("/root/Module_EvComm &");
  2094. system("/root/Module_LcmControl &");
  2095. system("/root/Module_InternalComm &");
  2096. system("/root/Module_ProduceUtils &");
  2097. system("/root/Module_UpdateFW &");
  2098. system("/root/Module_DoComm &");
  2099. return PASS;
  2100. }
  2101. void KillTask(void)
  2102. {
  2103. //ChangeLcmByIndex(_LCM_ERROR);
  2104. ChangeLcmByIndex(_PAGE_MAINTAIN);
  2105. system("killall Module_EventLogging");
  2106. system("killall Module_PrimaryComm");
  2107. system("killall Module_EvComm");
  2108. system("killall Module_LcmControl");
  2109. system("killall Module_InternalComm");
  2110. system("killall Module_UpdateFW");
  2111. system("killall Module_ChkSysTask");
  2112. //system("killall Module_DoComm");
  2113. return ;
  2114. }
  2115. void KillTaskExceptPrimary(void)
  2116. {
  2117. //ChangeLcmByIndex(_LCM_ERROR);
  2118. ChangeLcmByIndex(_PAGE_MAINTAIN);
  2119. system("killall Module_EvComm");
  2120. system("killall Module_InternalComm");
  2121. return;
  2122. }
  2123. void KillAllTask(void)
  2124. {
  2125. //ChangeLcmByIndex(_LCM_ERROR);
  2126. ChangeLcmByIndex(_PAGE_MAINTAIN);
  2127. system("killall Module_EventLogging");
  2128. system("killall Module_PrimaryComm");
  2129. system("killall Module_EvComm");
  2130. system("killall Module_LcmControl");
  2131. system("killall Module_InternalComm");
  2132. system("killall Module_UpdateFW");
  2133. system("killall Module_ChkSysTask");
  2134. system("killall Module_DoComm");
  2135. return ;
  2136. }
  2137. void StartSystemTimeoutDet(uint8_t flag)
  2138. {
  2139. if (pSysInfo->SystemTimeoutFlag != flag) {
  2140. //log_info("Set System Timeout Flag %d", flag);
  2141. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2142. }
  2143. pSysInfo->SystemTimeoutFlag = flag;
  2144. }
  2145. void StopSystemTimeoutDet(void)
  2146. {
  2147. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2148. pSysInfo->SystemTimeoutFlag = Timeout_None;
  2149. }
  2150. void StartGunInfoTimeoutDet(uint8_t gunIndex, uint8_t flag)
  2151. {
  2152. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2153. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2154. if (pDcChargingInfo->TimeoutFlag != flag) {
  2155. GetClockTime(&pDcChargingInfo->ConnectorTimeout, NULL);
  2156. //log_info("Set Gun%d Timeout Flag %d", gunIndex, flag);
  2157. }
  2158. pDcChargingInfo->TimeoutFlag = flag;
  2159. }
  2160. }
  2161. void StopGunInfoTimeoutDet(uint8_t gunIndex)
  2162. {
  2163. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2164. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2165. pDcChargingInfo->TimeoutFlag = Timeout_None;
  2166. }
  2167. }
  2168. void CheckConnectionTimeout(void)
  2169. {
  2170. if (ShmSelectGunInfo->RemoteSetup.ConnectionTimeout != 0) { //Jerry add
  2171. _connectionTimeout = ShmSelectGunInfo->RemoteSetup.ConnectionTimeout;
  2172. } else {
  2173. _connectionTimeout = CONN_PLUG_TIMEOUT;
  2174. }
  2175. return;
  2176. }
  2177. void _evccidlinktimeout(uint8_t gunIndex)
  2178. {
  2179. log_info("Getting EVCCID Timeout");
  2180. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2181. pDcChargingInfo->isEVCCIDVerify = true;
  2182. StopGunInfoTimeoutDet(gunIndex);
  2183. destroySelGun(pSysInfo->CurGunSelected);
  2184. systemPageRestoreInit();
  2185. }
  2186. void CreateTimeoutFork(void)
  2187. {
  2188. pid_t timeoutPid;
  2189. timeoutPid = fork();
  2190. if (timeoutPid == 0) {
  2191. GetClockTime(&_cmdSubPriority_time, NULL);
  2192. CheckConnectionTimeout();
  2193. //log_info("Timeout Fork Child's PID is %d", getpid());
  2194. while (1) {
  2195. if ((GetClockTimeoutValue(_cmdSubPriority_time) / 1000) > 5000) {
  2196. CheckConnectionTimeout();
  2197. GetClockTime(&_cmdSubPriority_time, NULL);
  2198. }
  2199. /*
  2200. if (pSysInfo->SystemTimeoutFlag != 0)
  2201. log_info("Timeout ***********SystemTimeoutFlag = %d(%d) ********",pSysInfo->SystemTimeoutFlag,
  2202. GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL);
  2203. */
  2204. // 系統
  2205. switch (pSysInfo->SystemTimeoutFlag) {
  2206. case Timeout_SelftestChk:
  2207. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT) {
  2208. _SelfTestTimeout();
  2209. StopSystemTimeoutDet();
  2210. destroySelGun(DESTROY_ALL_SEL); //jerry add
  2211. }
  2212. break;
  2213. case Timeout_ReturnViewPage:
  2214. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_VIEWPAGE_TIMEOUT) {
  2215. StopSystemTimeoutDet();
  2216. systemPageRestoreInit();
  2217. }
  2218. break;
  2219. case Timeout_Authorizing:
  2220. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT) {
  2221. log_error("Authorizing Timeout");
  2222. _AuthorizedTimeout();
  2223. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2224. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2225. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2226. log_error("Author timeout restart DoComm");
  2227. system("killall Module_DoComm");
  2228. }
  2229. }
  2230. break;
  2231. case Timeout_WaitBalance:
  2232. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= BALANCE_TIMEOUT) {
  2233. log_error("Wait Balance timeout");
  2234. _AuthorizedTimeout();
  2235. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2236. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2237. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2238. log_error("Author timeout restart DoComm");
  2239. system("killall Module_DoComm");
  2240. }
  2241. }
  2242. break;
  2243. case Timeout_VerifyFail:
  2244. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT) {
  2245. log_info("Timeout_VerifyFail");
  2246. StopSystemTimeoutDet();
  2247. systemPageRestoreInit();
  2248. if (!ShmDcCommonData->pGunInfo[pSysInfo->CurGunSelected].ParkingStatus)
  2249. setChargerMode(pSysInfo->CurGunSelected, S_IDLE);
  2250. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2251. ShmDcCommonData->pGunInfo[pSysInfo->CurGunSelected].GetParkingBill = FALSE;
  2252. }
  2253. break;
  2254. /*
  2255. case Timeout_WaitPlug:
  2256. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout) {
  2257. _DetectPlugInTimeout();
  2258. destroySelGun(pSysInfo->CurGunSelected);
  2259. }
  2260. break;
  2261. */
  2262. /*
  2263. case Timeout_ReturnToChargingGunDet:
  2264. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE) {
  2265. if (getCurLcmPage() != _LCM_VIEW &&
  2266. getCurLcmPage() != _LCM_DETAIL_VIEW ) {
  2267. destroySelGun(pSysInfo->CurGunSelected); //jerry add
  2268. }
  2269. DisplayChargingInfo();
  2270. StopSystemTimeoutDet();
  2271. }
  2272. break;
  2273. */
  2274. case Timeout_ScanCard:
  2275. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_SCANCARD_TIMEOUT) {
  2276. log_info("Timeout_ScanCard");
  2277. strcpy((char *)pSysConfig->UserId, "");
  2278. ClearAuthorizedFlag();
  2279. StopSystemTimeoutDet();
  2280. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2281. pDcChargingInfo->SystemStatus = S_IDLE;
  2282. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  2283. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2284. }
  2285. break;
  2286. case Timeout_Terminating:
  2287. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TERMINATING_TIMEOUT) {
  2288. StopSystemTimeoutDet();
  2289. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2290. log_info("Terminating timeout");
  2291. setChargerMode(pSysInfo->CurGunSelected, S_COMPLETE);
  2292. }
  2293. break;
  2294. case Timeout_AddLine:
  2295. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_ADDLINE_TIMEOUT) {
  2296. log_info("Timeout_AddLine");
  2297. StopSystemTimeoutDet();
  2298. systemPageRestoreInit();
  2299. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2300. }
  2301. break;
  2302. case Timeout_DonateComfirm:
  2303. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_DONATECOMFIRM_TIMEOUT) {
  2304. log_info("Timeout_DonateComfirm");
  2305. StopSystemTimeoutDet();
  2306. systemPageRestoreInit();
  2307. }
  2308. break;
  2309. case Timeout_SelectPayMode:
  2310. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_SELECTPAY_TIMEOUT) {
  2311. log_info("Timeout_SelectPayMode");
  2312. StopSystemTimeoutDet();
  2313. systemPageRestoreInit();
  2314. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2315. }
  2316. break;
  2317. case Timeout_TradeCancel:
  2318. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_TRADECANCEL_TIMEOUT) {
  2319. log_info("Timeout_TradeCancel");
  2320. StopSystemTimeoutDet();
  2321. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2322. setChargerMode(pSysInfo->CurGunSelected, MODE_IDLE);
  2323. systemPageRestoreInit();
  2324. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2325. ShmDcCommonData->pGunInfo[pSysInfo->CurGunSelected].GetParkingBill = FALSE;
  2326. }
  2327. break;
  2328. case Timeout_LINEPAYING:
  2329. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_LINEPAYING_TIMEOUT) {
  2330. log_info("Timeout_LINEPAYING");
  2331. StopSystemTimeoutDet();
  2332. systemPageRestoreInit();
  2333. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2334. }
  2335. break;
  2336. }
  2337. // 各槍
  2338. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2339. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2340. /*
  2341. if (pDcChargingInfo->TimeoutFlag != 0)
  2342. log_info("Timeout ***********GunTimeoutFlag = %d(%d) ********",pDcChargingInfo->TimeoutFlag,
  2343. GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL);
  2344. */
  2345. switch (pDcChargingInfo->TimeoutFlag) {
  2346. case Timeout_AuthorizingForStop:
  2347. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT) {
  2348. log_info("Gun %d Timeout_AuthorizingForStop", gunIndex);
  2349. strcpy((char*)pSysConfig->UserId, "");
  2350. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  2351. pDcChargingInfo->SystemStatus = S_IDLE;
  2352. ClearAuthorizedFlag();
  2353. StopGunInfoTimeoutDet(gunIndex);
  2354. if (pSysInfo->CurGunSelected == gunIndex)
  2355. systemPageRestoreInit();
  2356. }
  2357. break;
  2358. case Timeout_WaitPlug:
  2359. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= _connectionTimeout) {
  2360. _DetectPlugInTimeout(gunIndex);
  2361. destroySelGun(gunIndex);
  2362. }
  2363. break;
  2364. case Timeout_EVCCID_Link:
  2365. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= EVCCID_LINK_TIMEOUT) {
  2366. _evccidlinktimeout(gunIndex);
  2367. }
  2368. break;
  2369. case Timeout_LinkError:
  2370. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= LINKERROR_TIMEOUT) {
  2371. _LinkErrorTimeout(gunIndex);
  2372. StopGunInfoTimeoutDet(gunIndex);
  2373. destroySelGun(gunIndex);
  2374. }
  2375. break;
  2376. case Timeout_Preparing:
  2377. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_PREPARE_TIMEOUT) {
  2378. _PrepareTimeout(gunIndex);
  2379. StopGunInfoTimeoutDet(gunIndex);
  2380. destroySelGun(gunIndex); //jerry add
  2381. }
  2382. break;
  2383. case Timeout_EvChargingDet:
  2384. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT) {
  2385. _DetectEvChargingEnableTimeout(gunIndex);
  2386. StopGunInfoTimeoutDet(gunIndex);
  2387. destroySelGun(gunIndex); //jerry add
  2388. }
  2389. break;
  2390. case Timeout_EvseChargingDet:
  2391. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT) {
  2392. _DetectEvseChargingEnableTimeout(gunIndex);
  2393. StopGunInfoTimeoutDet(gunIndex);
  2394. destroySelGun(gunIndex); //jerry add
  2395. }
  2396. break;
  2397. case Timeout_EvseCompleteDet:
  2398. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT) {
  2399. StopGunInfoTimeoutDet(gunIndex);
  2400. destroySelGun(gunIndex); //jerry add
  2401. }
  2402. break;
  2403. case Timeout_ForCcsPrechargeDet:
  2404. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT) {
  2405. _CcsPrechargeTimeout(gunIndex);
  2406. StopGunInfoTimeoutDet(gunIndex);
  2407. destroySelGun(gunIndex); //jerry add
  2408. }
  2409. break;
  2410. case Timeout_PlugOutGun:
  2411. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= PLUGOUTGUN_TIMEOUT) {
  2412. log_info("Gun[%d] Timeout_PlugOutGun",gunIndex);
  2413. setChargerMode(gunIndex, MODE_IDLE);
  2414. destroySelGun(gunIndex); //Jerry add
  2415. StopGunInfoTimeoutDet(gunIndex);
  2416. if (pSysInfo->CurGunSelected == gunIndex)
  2417. systemPageRestoreInit();
  2418. }
  2419. break;
  2420. case Timeout_FinalCost:
  2421. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_FINALCOST_TIMEOUT) {
  2422. log_info("Gun[%d] Timeout_FinalCost",gunIndex);
  2423. StopGunInfoTimeoutDet(gunIndex);
  2424. if (ShmDcCommonData->finalcost_flag[gunIndex] == FALSE) {
  2425. log_info("Not Get Final Cost");
  2426. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  2427. if (pSysInfo->CurGunSelected == gunIndex)
  2428. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  2429. }
  2430. }
  2431. break;
  2432. case Timeout_LineReigster:
  2433. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_LINEREGISTER_TIMEOUT) {
  2434. log_info("Gun[%d] Timeout_LineReigster",gunIndex);
  2435. StopGunInfoTimeoutDet(gunIndex);
  2436. systemPageRestoreInit();
  2437. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = FALSE;
  2438. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  2439. }
  2440. break;
  2441. case Timeout_ParkingBill:
  2442. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_PARKINGBILL_TIMEOUT) {
  2443. log_info("Gun[%d] Timeout_LineReigster",gunIndex);
  2444. StopGunInfoTimeoutDet(gunIndex);
  2445. systemPageRestoreInit();
  2446. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = FALSE;
  2447. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  2448. ShmDcCommonData->pGunInfo[gunIndex].GetParkingBill = FALSE;
  2449. }
  2450. break;
  2451. case Timeout_ExitPage:
  2452. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_EXITPAGE_TIMEOUT) {
  2453. log_info("Gun[%d] Timeout_ExitPage", gunIndex);
  2454. StopGunInfoTimeoutDet(gunIndex);
  2455. ShmDcCommonData->is_exit[gunIndex] = TRUE;
  2456. if (pSysInfo->CurGunSelected == gunIndex)
  2457. pSysInfo->SystemPage = _PAGE_EXIT;
  2458. StartGunInfoTimeoutDet(gunIndex, Timeout_PlugOutGun);
  2459. }
  2460. break;
  2461. case Timeout_CompletPlugout:
  2462. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_COMPLETE_PLUGOUT_TIMEOUT) {
  2463. log_info("Gun[%d] Timeout_CompletePlugout", gunIndex);
  2464. setChargerMode(gunIndex, MODE_IDLE);
  2465. destroySelGun(gunIndex); //Jerry add
  2466. StopGunInfoTimeoutDet(gunIndex);
  2467. }
  2468. break;
  2469. case Timeout_WaitParkingInfo:
  2470. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= WAIT_PARKING_INFO_TIMEOUT) {
  2471. log_info("Gun[%d] Timeout_WaitParkingInfo", gunIndex);
  2472. StopGunInfoTimeoutDet(gunIndex);
  2473. systemPageRestoreInit();
  2474. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  2475. ShmDcCommonData->pGunInfo[gunIndex].GetParkingBill = FALSE;
  2476. }
  2477. break;
  2478. case Timeout_ParkingSelect:
  2479. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_PARK_SELECTPAY_TIMEOUT) {
  2480. log_info("Gun[%d] Timeout_ParkingSelect", gunIndex);
  2481. StopGunInfoTimeoutDet(gunIndex);
  2482. systemPageRestoreInit();
  2483. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  2484. ShmDcCommonData->pGunInfo[gunIndex].GetParkingBill = FALSE;
  2485. }
  2486. break;
  2487. case Timeout_ParkingLeave:
  2488. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_EXITPAGE_TIMEOUT) {
  2489. log_info("Gun[%d] Timeout_ParkingLeave", gunIndex);
  2490. StopGunInfoTimeoutDet(gunIndex);
  2491. systemPageRestoreInit();
  2492. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  2493. ShmDcCommonData->pGunInfo[gunIndex].isParking = FALSE;
  2494. ShmDcCommonData->pGunInfo[gunIndex].GetParkingBill = FALSE;
  2495. }
  2496. break;
  2497. }
  2498. }
  2499. sleep(1);
  2500. }
  2501. }
  2502. }
  2503. void CheckFactoryConfigFunction(void)
  2504. {
  2505. char Buf[256] = {0};
  2506. if (pSysInfo->FactoryConfiguration) {
  2507. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  2508. pSysConfig->ModelName,
  2509. pSysConfig->SerialNumber);
  2510. system(Buf);
  2511. system("rm -f /Storage/OCPP/OCPPConfiguration");
  2512. system("sync");
  2513. sleep(5);
  2514. system("reboot -f");
  2515. sleep(5);
  2516. system("reboot -f");
  2517. }
  2518. }
  2519. //===============================================
  2520. // Check reservation date is expired
  2521. //===============================================
  2522. int isReservationExpired(uint8_t gunIndex)
  2523. {
  2524. int result = NO;
  2525. struct tm expiredDate;
  2526. struct timeb expiredTime;
  2527. if (sscanf((char *) ShmOCPP16Data->ReserveNow[gunIndex].ExpiryDate,
  2528. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  2529. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  2530. &expiredDate.tm_min, &expiredDate.tm_sec) == 6) {
  2531. expiredDate.tm_year -= 1900;
  2532. expiredDate.tm_mon -= 1;
  2533. expiredTime.time = mktime(&expiredDate);
  2534. if (!CheckTimeOut(expiredTime)) {
  2535. result = YES;
  2536. }
  2537. }
  2538. return result;
  2539. }
  2540. //===============================================
  2541. // OCPP
  2542. //===============================================
  2543. void CheckOcppStatus(void)
  2544. {
  2545. bool canReset = true;
  2546. //bool canHardReset = true; //DS60-120 add
  2547. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES) {
  2548. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  2549. }
  2550. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES) {
  2551. if (pSysWarning->Level != WARN_LV_ER) {
  2552. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2553. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2554. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2555. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  2556. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  2557. canReset = false;
  2558. if (pDcChargingInfo->SystemStatus >= S_REASSIGN &&
  2559. pDcChargingInfo->SystemStatus < S_TERMINATING) {
  2560. //canHardReset = false;
  2561. ChargingTerminalProcess(_index);
  2562. //restartFlag = 1;
  2563. }
  2564. }
  2565. }
  2566. }
  2567. if (canReset) {
  2568. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  2569. sprintf((char *)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2570. if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL) {
  2571. log_error("****** Hard Reboot ****** ");
  2572. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2573. sleep(3);
  2574. system("reboot -f");
  2575. } else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL) {
  2576. log_error("****** Soft Reboot ****** ");
  2577. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2578. sleep(3);
  2579. system("killall OcppBackend &");
  2580. KillAllTask();
  2581. TryCloseWatchdog();
  2582. system("/usr/bin/run_evse_restart.sh");
  2583. }
  2584. }
  2585. }
  2586. }
  2587. void OcppStopTransation(uint8_t gunIndex)
  2588. {
  2589. uint8_t _OcppGunIndex = gunIndex;
  2590. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2591. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  2592. if (pSysConfig->AcConnectorCount == 1 && gunIndex == 1) {
  2593. _OcppGunIndex = 2;
  2594. }
  2595. if (strcmp((char *)pDcChargingInfo->StartUserId, "") == EQUAL) {
  2596. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2597. } else {
  2598. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)pDcChargingInfo->StartUserId);
  2599. }
  2600. log_info("IdTag = %s ", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2601. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  2602. }
  2603. bool OcppRemoteStop(uint8_t gunIndex)
  2604. {
  2605. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2606. // 有 AC 槍的話
  2607. if (acDirIndex > 0 && gunIndex > 0) {
  2608. gunIndex += acDirIndex;
  2609. }
  2610. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  2611. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES) {
  2612. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  2613. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  2614. }
  2615. return result;
  2616. }
  2617. bool WifiScheduleStop(uint8_t gunIndex)
  2618. {
  2619. bool result = false;
  2620. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2621. result = pDcChargingInfo->schedule.isTriggerStop;
  2622. pDcChargingInfo->schedule.isTriggerStop = NO;
  2623. return result;
  2624. }
  2625. void OcppRemoteStartChk()
  2626. {
  2627. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING) {
  2628. } else {
  2629. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  2630. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2631. if (!isDetectPlugin(acDirIndex)) {
  2632. for (uint8_t ac_index = 0; ac_index < pSysConfig->AcConnectorCount; ac_index++) {
  2633. pAcChargingInfo = (struct ChargingInfoData*)GetAcChargingInfoData(ac_index);
  2634. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES) {
  2635. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  2636. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  2637. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2638. pAcChargingInfo->RemoteStartFlag = YES;
  2639. pSysInfo->OrderCharging = YES;
  2640. //pSysInfo->OrderCharging = DEFAULT_AC_INDEX;
  2641. ShmOCPP16Data->CsMsg.bits[pSysConfig->TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  2642. DetectPluginStart(acDirIndex);
  2643. return;
  2644. }
  2645. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2646. }
  2647. }
  2648. }
  2649. //uint8_t threeGunIndex = 0;
  2650. //uint8_t dcIndex = 0;
  2651. //bool isGunUsingStatus = false;
  2652. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2653. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  2654. /*
  2655. // 如果有 AC 槍,且 DC 槍也有兩把
  2656. if (acDirIndex == 1 && _index == 1) {
  2657. threeGunIndex = 1;
  2658. }
  2659. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES) {
  2660. dcIndex = _index;
  2661. }
  2662. if (pDcChargingInfo->SystemStatus != S_IDLE) {
  2663. isGunUsingStatus = true;
  2664. }
  2665. if (pDcChargingInfo->RemoteStartFlag == YES) {
  2666. //_remotestarting = true;
  2667. }
  2668. }
  2669. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  2670. if (isGunUsingStatus && pSysInfo->IsAlternatvieConf) {
  2671. if (dcIndex == 0) {
  2672. threeGunIndex = 0;
  2673. }
  2674. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  2675. return;
  2676. }
  2677. if (dcIndex == 0) {
  2678. threeGunIndex = 0;
  2679. }
  2680. */
  2681. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  2682. if (ShmOCPP16Data->CsMsg.bits[_index].RemoteStartTransactionReq == YES &&
  2683. !isDetectPlugin(_index)) {
  2684. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2685. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  2686. pDcChargingInfo->RemoteStartFlag = YES;
  2687. ShmDcCommonData->is_RemoteStart[_index] = TRUE;
  2688. //pSysInfo->OrderCharging = YES;
  2689. //pDcChargingInfo->SystemStatus = S_AUTHORIZING;
  2690. //pSysInfo->OrderCharging = gunIndex;
  2691. ShmOCPP16Data->CsMsg.bits[_index].RemoteStartTransactionReq = NO;
  2692. DetectPluginStart(_index);
  2693. setSelGunWaitToAuthor(_index);
  2694. log_info("Ocpp Remote Start Gun%d Pass", _index);
  2695. }
  2696. ShmOCPP16Data->CsMsg.bits[_index].RemoteStartTransactionReq = NO;
  2697. }
  2698. }
  2699. }
  2700. }
  2701. void ChkOcppStatus(uint8_t gunIndex)
  2702. {
  2703. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2704. if (pDcChargingInfo->SystemStatus == S_IDLE &&
  2705. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES) {
  2706. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  2707. if (isReservationExpired(gunIndex)) {
  2708. log_error("***************ChkOcppStatus : OcppReservedStatus******************** ");
  2709. pDcChargingInfo->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  2710. pDcChargingInfo->SystemStatus = S_RESERVATION;
  2711. }
  2712. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  2713. }
  2714. if (pDcChargingInfo->SystemStatus == S_RESERVATION &&
  2715. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES) {
  2716. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  2717. if (isReservationExpired(gunIndex)) {
  2718. log_error("***************ChkOcppStatus : Cancel OcppReservedStatus******************** ");
  2719. pDcChargingInfo->ReservationId = 0;
  2720. pDcChargingInfo->SystemStatus = S_IDLE;
  2721. }
  2722. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  2723. }
  2724. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES) {
  2725. log_error("***************ChkOcppStatus : OcppChangeAvailability******************** ");
  2726. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  2727. if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL) {
  2728. if (isDb_ready) {
  2729. DB_Update_Operactive(gunIndex, true);
  2730. }
  2731. pDcChargingInfo->IsAvailable = YES;
  2732. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2733. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2734. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2735. setChargerMode(gunIndex, MODE_IDLE);
  2736. }
  2737. } else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL) {
  2738. if (isDb_ready) {
  2739. DB_Update_Operactive(gunIndex, false);
  2740. }
  2741. pDcChargingInfo->IsAvailable = NO;
  2742. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2743. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2744. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2745. setChargerMode(gunIndex, MODE_MAINTAIN);
  2746. }
  2747. }
  2748. }
  2749. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES) {
  2750. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  2751. if (pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK && // DS60-120 add ||
  2752. pDcChargingInfo->SystemStatus <= S_CHARGING) {
  2753. // 充電中,需停止充電
  2754. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  2755. ChargingTerminalProcess(gunIndex);
  2756. }
  2757. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  2758. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  2759. }
  2760. }
  2761. bool CheckBackendChargingTimeout(uint8_t gunIndex)
  2762. {
  2763. bool result = false;
  2764. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2765. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2766. if (pSysConfig->MaxChargingDuration > 0) {
  2767. if (pDcChargingInfo->PresentChargedDuration > (pSysConfig->MaxChargingDuration * 60)) {
  2768. result = true;
  2769. }
  2770. }
  2771. }
  2772. return result;
  2773. }
  2774. bool CheckBackendChargingEnergy(uint8_t gunIndex)
  2775. {
  2776. bool result = false;
  2777. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2778. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2779. if (pSysConfig->MaxChargingEnergy > 0) {
  2780. if (pDcChargingInfo->PresentChargedEnergy > pSysConfig->MaxChargingEnergy) {
  2781. result = true;
  2782. }
  2783. }
  2784. }
  2785. return result;
  2786. }
  2787. void InformOcppErrOccur(uint8_t codeType)
  2788. {
  2789. char _error[25];
  2790. switch (codeType) {
  2791. case 4:
  2792. strcpy(_error, "InternalError");
  2793. break;
  2794. case 6:
  2795. strcpy(_error, "NoError");
  2796. break;
  2797. case 7:
  2798. strcpy(_error, "OtherError");
  2799. break;
  2800. case 13:
  2801. strcpy(_error, "UnderVoltage");
  2802. break;
  2803. case 14:
  2804. strcpy(_error, "OverVoltage");
  2805. break;
  2806. }
  2807. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2808. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, _error);
  2809. }
  2810. }
  2811. //===============================================
  2812. // Config process
  2813. //===============================================
  2814. /*
  2815. void AddPlugInTimes(uint8_t gunIndex)
  2816. {
  2817. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2818. if (pDcChargingInfo->Type == _Type_Chademo) {
  2819. pSysConfig->ChademoPlugInTimes += 1;
  2820. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  2821. pSysConfig->Ccs2PlugInTimes += 1;
  2822. } else if (pDcChargingInfo->Type == _Type_GB) {
  2823. pSysConfig->GbPlugInTimes += 1;
  2824. }
  2825. }*/
  2826. void ChangeStartOrStopDateTime(uint8_t isStart, uint8_t gunIndex)
  2827. {
  2828. char cmdBuf[32];
  2829. struct timeb csuTime;
  2830. struct tm *tmCSU;
  2831. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2832. ftime(&csuTime);
  2833. tmCSU = localtime(&csuTime.time);
  2834. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  2835. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2836. tmCSU->tm_sec);
  2837. if (isStart) {
  2838. strcpy((char *)pDcChargingInfo->StartDateTime, cmdBuf);
  2839. } else {
  2840. strcpy((char *)pDcChargingInfo->StopDateTime, cmdBuf);
  2841. }
  2842. }
  2843. void ChangeGunSelectByIndex(uint8_t sel)
  2844. {
  2845. pSysInfo->CurGunSelected = sel;
  2846. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  2847. }
  2848. void CheckIsAlternatvieByModelName()
  2849. {
  2850. if (pSysConfig->ModelName[1] == 'W') { //DS60-120
  2851. // 壁掛
  2852. pSysInfo->IsAlternatvieConf = YES;
  2853. } else {
  2854. pSysInfo->IsAlternatvieConf = NO;
  2855. }
  2856. }
  2857. void StopProcessingLoop()
  2858. {
  2859. log_info("Stop Processing....");
  2860. for (;;) {
  2861. CheckFactoryConfigFunction();
  2862. //CheckFwUpdateFunction();
  2863. if (pSysWarning->Level == WARN_LV_ER) {
  2864. ChkPrimaryStatus();
  2865. if (pSysWarning->Level == WARN_LV_NL) {
  2866. log_info("Soft reboot for retry self-tets (Primary). ");
  2867. KillAllTask();
  2868. sleep(3);
  2869. TryCloseWatchdog();
  2870. system("/usr/bin/run_evse_restart.sh");
  2871. return;
  2872. }
  2873. }
  2874. sleep(1);
  2875. TryFeedWatchdog();
  2876. }
  2877. }
  2878. bool IsConnectorWholeIdle()
  2879. {
  2880. bool result = true;
  2881. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2882. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2883. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2884. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  2885. result = false;
  2886. break;
  2887. }
  2888. }
  2889. for (uint8_t count = 0; count < pSysConfig->AcConnectorCount; count++) {
  2890. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  2891. if (pAcChargingInfo->SystemStatus != S_IDLE &&
  2892. pAcChargingInfo->IsErrorOccur == NO) {
  2893. result = false;
  2894. break;
  2895. }
  2896. }
  2897. return result;
  2898. }
  2899. void ClearAlarmCodeWhenAcOff()
  2900. {
  2901. if (!pSysInfo->AcContactorStatus) {
  2902. pAlarmCode->AlarmEvents.bits.PsuNoResource = NO;
  2903. }
  2904. }
  2905. //==========================================
  2906. // Check Smart Charging Profile
  2907. //==========================================
  2908. int GetStartScheduleTime(uint8_t *time)
  2909. {
  2910. int result = -1;
  2911. struct tm tmScheduleStart;
  2912. struct timeb tbScheduleStart;
  2913. if ((sscanf((char *)time, "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6)) {
  2914. tmScheduleStart.tm_year -= 1900;
  2915. tmScheduleStart.tm_mon -= 1;
  2916. tbScheduleStart.time = mktime(&tmScheduleStart);
  2917. tbScheduleStart.millitm = 0;
  2918. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  2919. }
  2920. return result;
  2921. }
  2922. void CheckSmartChargeProfile(uint8_t _index)
  2923. {
  2924. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2925. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == YES) {
  2926. // Get Charging Profile
  2927. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  2928. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL &&
  2929. ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileId == YES //DS60-120 add
  2930. ) {
  2931. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  2932. uint8_t _startCount = NO_DEFINE;
  2933. uint8_t _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  2934. for (uint8_t _count = 0; _count < _maxCount; _count++) {
  2935. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  2936. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod) {
  2937. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  2938. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR) {
  2939. _startCount = _count;
  2940. }
  2941. }
  2942. }
  2943. log_info("_startCount = %d ", _startCount);
  2944. if (_startCount < _maxCount) {
  2945. //DS60-120 add
  2946. log_info("Profile Limit = %f ", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  2947. pDcChargingInfo->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  2948. if (pDcChargingInfo->EvBatterytargetVoltage > 0 && pDcChargingInfo->PresentChargingVoltage > 0) {
  2949. pDcChargingInfo->ChargingProfileCurrent = (pDcChargingInfo->ChargingProfilePower / pDcChargingInfo->PresentChargingVoltage) * 10;
  2950. } else {
  2951. pDcChargingInfo->ChargingProfileCurrent = 0;
  2952. }
  2953. } else {
  2954. pDcChargingInfo->ChargingProfilePower = -1;
  2955. pDcChargingInfo->ChargingProfileCurrent = -1;
  2956. }
  2957. } else {
  2958. pDcChargingInfo->ChargingProfilePower = -1;
  2959. pDcChargingInfo->ChargingProfileCurrent = -1;
  2960. }
  2961. log_info("ChargingProfilePower = %f ", pDcChargingInfo->ChargingProfilePower);
  2962. log_info("ChargingProfileCurrent = %f ", pDcChargingInfo->ChargingProfileCurrent);
  2963. }
  2964. }
  2965. void ChargingProfileFlat(uint8_t _index)
  2966. {
  2967. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO) {
  2968. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO) {
  2969. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  2970. }
  2971. }
  2972. }
  2973. /*
  2974. void CheckReturnToChargingConn()
  2975. {
  2976. if ((pSysConfig->TotalConnectorCount + pSysConfig->AcConnectorCount) > 1 &&
  2977. pSysInfo->PageIndex != _LCM_START_AUTHORIZING &&
  2978. pSysInfo->PageIndex != _LCM_START_AUTHORIZE_FAIL &&
  2979. pSysInfo->PageIndex != _LCM_WAIT_PLUGIN) {
  2980. bool isReturnTimeout = false;
  2981. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2982. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2983. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  2984. if (count == pSysInfo->CurGunSelected) {
  2985. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2986. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2987. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2988. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2989. ) {
  2990. isReturnTimeout = false;
  2991. break;
  2992. }
  2993. } else if (count != pSysInfo->CurGunSelected) {
  2994. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2995. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2996. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2997. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2998. ) {
  2999. isReturnTimeout = true;
  3000. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3001. }
  3002. }
  3003. }
  3004. // AC 槍
  3005. if (!isReturnTimeout && pSysConfig->AcConnectorCount > 0) {
  3006. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  3007. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  3008. // 沒有選中 AC,且 AC 在充電中
  3009. if (pSysInfo->CurGunSelectedByAc == NO_DEFINE &&
  3010. (pAcChargingInfo->SystemStatus >= S_PREPARNING &&
  3011. pAcChargingInfo->SystemStatus <= S_COMPLETE)) {
  3012. // 當前 DC 充電槍在 idle 狀態
  3013. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  3014. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  3015. isReturnTimeout = true;
  3016. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3017. }
  3018. } else if (pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  3019. ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  3020. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  3021. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3022. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))) {
  3023. // 當前 DC 充電槍在 idle 狀態
  3024. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  3025. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  3026. isReturnTimeout = true;
  3027. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3028. }
  3029. }
  3030. }
  3031. if (!isReturnTimeout) {
  3032. StopSystemTimeoutDet();
  3033. }
  3034. }
  3035. }
  3036. bool GetStartChargingByAlterMode(uint8_t _gun)
  3037. {
  3038. bool result = true;
  3039. if (pSysConfig->TotalConnectorCount == 2 &&
  3040. pSysInfo->IsAlternatvieConf == YES) {
  3041. for (uint8_t _select = 0; _select < pSysConfig->TotalConnectorCount; _select++) {
  3042. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_select);
  3043. if (_select != _gun) {
  3044. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  3045. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  3046. result = false;
  3047. break;
  3048. }
  3049. }
  3050. }
  3051. }
  3052. return result;
  3053. }
  3054. */
  3055. void TheEndCharging(uint8_t gunIndex)
  3056. {
  3057. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3058. ftime(&endChargingTime[gunIndex]);
  3059. if (pDcChargingInfo->PresentChargedDuration != 0) {
  3060. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  3061. }
  3062. pDcChargingInfo->isRemoteStart = NO;
  3063. if (pDcChargingInfo->TimeoutFlag != Timeout_FinalCost)
  3064. StopGunInfoTimeoutDet(gunIndex);
  3065. //StartGunInfoTimeoutDet(gunIndex, Timeout_EvseCompleteDet);
  3066. ChangeStartOrStopDateTime(NO, gunIndex);
  3067. DB_Insert_Record(gunIndex);
  3068. }
  3069. void UpdateErrorCodeToOcpp(uint8_t index)
  3070. {
  3071. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  3072. //log_info("%d = ConnectorAlarmCode = %s", index, pDcChargingInfo->ConnectorAlarmCode);
  3073. //log_info("%d = EvConnAlarmCode = %s", index, pDcChargingInfo->EvConnAlarmCode);
  3074. if (strcmp((char *)pDcChargingInfo->ConnectorAlarmCode, "") != EQUAL) {
  3075. //if (strlen((char *)pDcChargingInfo->ConnectorAlarmCode) == EQUAL) {
  3076. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "InternalError");
  3077. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->ConnectorAlarmCode);
  3078. } else if (strcmp((char *)pDcChargingInfo->EvConnAlarmCode, "") != EQUAL) {
  3079. //} else if (strlen((char *)pDcChargingInfo->EvConnAlarmCode) == EQUAL) {
  3080. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "OtherError");
  3081. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->EvConnAlarmCode);
  3082. }
  3083. //log_info("Gun %d = VendorErrorCode = %s", index, (char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode);
  3084. }
  3085. void AdjustChargerCurrent()
  3086. {
  3087. pSysConfig->RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  3088. // 設定的電流~ 如超過可輸出的電流,則 bypass
  3089. if (pSysConfig->RatingCurrent < pSysConfig->MaxChargingCurrent ||
  3090. pSysConfig->RatingCurrent == 0) {
  3091. pSysConfig->MaxChargingCurrent = 0;
  3092. }
  3093. log_info("PSU : MaxChargingPower = %d, MaxChargingCurrent = %d",
  3094. ShmPsuData->SystemAvailablePower / 10,
  3095. ShmPsuData->SystemAvailableCurrent / 10
  3096. );
  3097. log_info("Config : ChargingPower = %d, ChargingCurrent = %d",
  3098. pSysConfig->MaxChargingPower,
  3099. pSysConfig->MaxChargingCurrent
  3100. );
  3101. }
  3102. void ResetDetAlarmStatus(uint8_t gun)
  3103. {
  3104. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gun);
  3105. if (pDcChargingInfo->Type == _Type_Chademo) {
  3106. if (pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP == YES) {
  3107. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = NO;
  3108. }
  3109. } else if (pDcChargingInfo->Type == _Type_GB) {
  3110. if (pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP == YES) {
  3111. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = NO;
  3112. }
  3113. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3114. if (pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP == YES) {
  3115. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = NO;
  3116. }
  3117. }
  3118. }
  3119. static void autoStartCharging(uint8_t gunIndex)
  3120. {
  3121. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3122. if ( (pSysInfo->SystemPage >= _PAGE_AUTHORIZE && pSysInfo->SystemPage <= _PAGE_SENSING) ||
  3123. (pDcChargingInfo->isEVCCIDVerify && strcmp( (char *)pSysConfig->UserId , "" ) != EQUAL)) {
  3124. return;
  3125. }
  3126. if ((pDcChargingInfo->ConnectorPlugIn == YES) &&
  3127. (pDcChargingInfo->SystemStatus == S_IDLE )
  3128. ) {
  3129. if(!pSysConfig->AuthorisationMode) {
  3130. /*
  3131. if (pSysConfig->isAuthrizeByEVCCID && !pDcChargingInfo->isEVCCIDVerify &&
  3132. pDcChargingInfo->Type == _Type_CCS_2) {
  3133. // LCM show linking pic. & timeout 2 min
  3134. if( pSysInfo->CurGunSelected == gunIndex ) {
  3135. systemPageRestoreInit();
  3136. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3137. }
  3138. StartGunInfoTimeoutDet(gunIndex,Timeout_EVCCID_Link);
  3139. // Getting EVCCID
  3140. if( strcmp( (char *)pDcChargingInfo->EVCCID, "" ) != EQUAL ) {
  3141. log_info("Authorizing EVCCID");
  3142. StopSystemTimeoutDet();
  3143. strcpy((char *)pSysConfig->UserId, (char *)pDcChargingInfo->EVCCID);
  3144. ChangeGunSelectByIndex(gunIndex);
  3145. confirmSelGun(gunIndex);
  3146. pDcChargingInfo->isEVCCIDVerify = true;
  3147. pSysInfo->SystemPage = _LCM_START_AUTHORIZING;
  3148. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  3149. setChargerMode(gunIndex, MODE_AUTHORIZING);
  3150. }
  3151. } else
  3152. return;
  3153. */
  3154. } else {
  3155. strcpy((char *)&pSysConfig->UserId, "AutoStartCharging");
  3156. ChangeGunSelectByIndex(gunIndex);
  3157. confirmSelGun(gunIndex);
  3158. ShmDcCommonData->TradeCancel = FALSE;
  3159. ShmDcCommonData->is_AutoStart[gunIndex] = TRUE;
  3160. pSysInfo->SystemPage = _PAGE_SENSING;
  3161. ShmDcCommonData->AuthPass_flag[gunIndex] = TRUE;
  3162. //setChargerMode(gunIndex, MODE_AUTHORIZING);
  3163. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  3164. sleep(1); //讓其他task有時間執行
  3165. }
  3166. }
  3167. }
  3168. static bool PrecheckIsPass(uint8_t gunIndex)
  3169. {
  3170. bool result = true;
  3171. // relay welding or driving 是反向
  3172. result = !ShmDcCommonData->GunRelayWeldingOccur[gunIndex];
  3173. return result;
  3174. }
  3175. static void ReviewCriticalAlarm(void)
  3176. {
  3177. if (
  3178. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES ||
  3179. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES ||
  3180. pAlarmCode->AlarmEvents.bits.DoorOpen == YES ||
  3181. pSysWarning->ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  3182. pAlarmCode->AlarmEvents.bits.PsuFailureAlarm == YES ||
  3183. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES ||
  3184. //Power cabinet alarm status
  3185. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop == YES ||
  3186. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen == YES ||
  3187. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP == YES ||
  3188. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP == YES ||
  3189. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP == YES ||
  3190. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP == YES ||
  3191. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP == YES ||
  3192. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure == YES ) {
  3193. pSysWarning->Level = WARN_LV_ER;
  3194. } else {
  3195. if (ShmSelectGunInfo->EthDevStatus.Backend == 0 ||
  3196. ShmSelectGunInfo->EthDevStatus.Backend == 2 ) {
  3197. pInfoCode->InfoEvents.bits.BackendDisconnectedViaEthernet = TRUE;
  3198. if (ShmDcCommonData->DebugFlag || pSysConfig->AuthorisationMode) {
  3199. pSysWarning->Level = WARN_LV_NL;
  3200. } else {
  3201. pSysWarning->Level = WARN_LV_ER;
  3202. return;
  3203. }
  3204. } else if (ShmSelectGunInfo->EthDevStatus.Backend == 1) {
  3205. pInfoCode->InfoEvents.bits.BackendDisconnectedViaEthernet = FALSE;
  3206. }
  3207. pSysWarning->Level = WARN_LV_NL;
  3208. }
  3209. }
  3210. static void CheckRelayWeldingOrDrivingFault(uint8_t gunIndex)
  3211. {
  3212. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3213. // relay welding fault then stop the charging process.
  3214. uint8_t faultCode = RELAY_STATUS_ERROR_NONE;
  3215. //static uint8_t drivingCount = 0;
  3216. if (gunIndex == 0) {
  3217. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  3218. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  3219. faultCode = RELAY_STATUS_ERROR_WELDING;
  3220. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  3221. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  3222. faultCode = RELAY_STATUS_ERROR_DRIVING;
  3223. }
  3224. } else if (gunIndex == 1) {
  3225. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  3226. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  3227. faultCode = RELAY_STATUS_ERROR_WELDING;
  3228. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  3229. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  3230. faultCode = RELAY_STATUS_ERROR_DRIVING;
  3231. }
  3232. }
  3233. if (pSysConfig->TotalConnectorCount >= 2 &&
  3234. !pSysInfo->IsAlternatvieConf) {
  3235. // 橋接
  3236. if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  3237. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  3238. faultCode = RELAY_STATUS_ERROR_WELDING;
  3239. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  3240. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  3241. faultCode = RELAY_STATUS_ERROR_DRIVING;
  3242. }
  3243. }
  3244. if (faultCode == RELAY_STATUS_ERROR_WELDING) {
  3245. // welding
  3246. if (pDcChargingInfo->Type == _Type_Chademo) {
  3247. RecordAlarmCode(gunIndex, "011011");
  3248. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  3249. } else if (pDcChargingInfo->Type == _Type_GB) {
  3250. RecordAlarmCode(gunIndex, "011015");
  3251. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  3252. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3253. RecordAlarmCode(gunIndex, "011013");
  3254. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  3255. }
  3256. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = YES;
  3257. //EmcOccureByString("");
  3258. } else if (faultCode == RELAY_STATUS_ERROR_DRIVING) {
  3259. //if (drivingCount++ != 2) {
  3260. // return;
  3261. //}
  3262. // driving
  3263. if (pDcChargingInfo->Type == _Type_Chademo) {
  3264. RecordAlarmCode(gunIndex, "011012");
  3265. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  3266. } else if (pDcChargingInfo->Type == _Type_GB) {
  3267. RecordAlarmCode(gunIndex, "011016");
  3268. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  3269. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3270. RecordAlarmCode(gunIndex, "011014");
  3271. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  3272. }
  3273. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = YES;
  3274. //EmcOccureByString("");
  3275. } else {
  3276. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = NO;
  3277. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = NO;
  3278. //drivingCount = 0;
  3279. }
  3280. }
  3281. static void isChargingAverageState(void)
  3282. {
  3283. if (pSysInfo->MainChargingMode != _MAIN_CHARGING_MODE_AVER) {
  3284. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3285. return;
  3286. }
  3287. // 均充 -> 最大充
  3288. if (pSysInfo->BridgeRelayStatus == NO) {
  3289. if (pSysInfo->ReAssignedFlag == _REASSIGNED_NONE) {
  3290. log_info("=============Smart Charging============= Step 11 ");
  3291. pSysInfo->ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  3292. }
  3293. } else if (pSysInfo->ReAssignedFlag != _REASSIGNED_COMP &&
  3294. pSysInfo->ReAssignedFlag != _REASSIGNED_WAITING) {
  3295. log_info("=============Smart Charging============= Step 14 ");
  3296. pSysInfo->ReAssignedFlag = _REASSIGNED_WAITING;
  3297. } else if (pSysInfo->ReAssignedFlag == _REASSIGNED_COMP) {
  3298. pSysInfo->MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3299. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3300. }
  3301. }
  3302. bool Taskconutstring(char *src, char *taskname)
  3303. {
  3304. bool result = false;
  3305. if (src == NULL || strlen(src) == 0)
  3306. return result;
  3307. if (strstr(src, taskname) != NULL &&
  3308. strstr(src, "grep") == NULL &&
  3309. strstr(src, "[") == NULL)
  3310. {
  3311. result = true;
  3312. }
  3313. return result;
  3314. }
  3315. int GetProcessCount(char *procName)
  3316. {
  3317. int result = 0;
  3318. FILE *fp;
  3319. char cmd[256];
  3320. char buf[256];
  3321. sprintf(cmd, "ps -ef |grep %s", procName);
  3322. fp = popen(cmd, "r");
  3323. if(fp != NULL)
  3324. {
  3325. while(fgets(buf, sizeof(buf), fp) != NULL)
  3326. {
  3327. if (Taskconutstring(buf, procName))
  3328. result++;
  3329. }
  3330. }
  3331. pclose(fp);
  3332. return result;
  3333. }
  3334. void CheckTaskAlive()
  3335. {
  3336. pid_t Pid = fork();
  3337. if ( Pid == 0 ) {
  3338. //log_info("CheckTaskAlive fork Child's PID is %d", getpid());
  3339. while(1) {
  3340. sleep(3);
  3341. unsigned char count = GetProcessCount("Module_ChkSysTask");
  3342. if( count != 1 ) {
  3343. system("pkill Module_ChkSysTask");
  3344. sleep(10);
  3345. //log_info("Create Check System Task");
  3346. system("/root/Module_ChkSysTask &");
  3347. }
  3348. }
  3349. return;
  3350. }
  3351. log_info("Create Check Systam fork:%d",Pid);
  3352. return;
  3353. }
  3354. /**
  3355. * [checkPileEndGfdResult 確認充電樁GFD狀態]
  3356. * @Author Jerry
  3357. * @DateTime 2021-07-01
  3358. * @param gunIndex [當前的槍]
  3359. * @param gunType [槍的類型]
  3360. * @param sysStatus [槍的系統狀態]
  3361. */
  3362. static void checkPileEndGfdResult(uint8_t gunIndex, uint8_t gunType, uint8_t sysStatus)
  3363. {
  3364. switch (gunType) {
  3365. case _Type_Chademo:
  3366. // 檢查樁端的 GFD 結果
  3367. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3368. isPrechargeStatus_chademo(gunIndex) >= 6 ) {
  3369. // 當前操作的槍號,進入 Charging
  3370. setChargerMode(gunIndex, MODE_CHARGING);
  3371. }
  3372. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3373. // GFD 錯誤停止
  3374. RecordAlarmCode(gunIndex, "012234");
  3375. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  3376. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3377. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3378. // GFD 警告
  3379. //RecordAlarmCode(gunIndex, "012296");
  3380. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3381. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  3382. }
  3383. else {
  3384. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3385. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3386. }
  3387. break;
  3388. case _Type_GB:
  3389. // 檢查樁端的 GFD 結果
  3390. //if (isPrechargeStatus_gb(gunIndex) > 5 && isPrechargeStatus_gb(gunIndex) < 9) {
  3391. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3392. isPrechargeStatus_gb(gunIndex) >= 6) {
  3393. setChargerMode(gunIndex, MODE_CHARGING);
  3394. }
  3395. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3396. // GFD 錯誤停止
  3397. RecordAlarmCode(gunIndex, "012236");
  3398. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  3399. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3400. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3401. // GFD 警告
  3402. //RecordAlarmCode(gunIndex, "012298");
  3403. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3404. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  3405. }
  3406. else {
  3407. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3408. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3409. }
  3410. break;
  3411. case _Type_CCS_2:
  3412. // 檢查樁端的 GFD 結果
  3413. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3414. (pDcChargingInfo->GroundFaultStatus == GFD_PASS ||
  3415. pDcChargingInfo->GroundFaultStatus == GFD_WARNING)
  3416. ) {
  3417. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP0);
  3418. }
  3419. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3420. // GFD 錯誤停止
  3421. RecordAlarmCode(gunIndex, "012235");
  3422. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  3423. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3424. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3425. // GFD 警告
  3426. //RecordAlarmCode(gunIndex, "012297");
  3427. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3428. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  3429. }
  3430. else {
  3431. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3432. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3433. }
  3434. break;
  3435. }
  3436. }
  3437. static void checkEvBoardReqStop(uint8_t sysStatus, uint8_t gunIndex)
  3438. {
  3439. uint8_t evBoardStopState = 0;
  3440. if ((evBoardStopState = isEvBoardStopChargeFlag(gunIndex)) > 0) {
  3441. // 板端要求停止 (錯誤)
  3442. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3443. sysStatus != S_CCS_PRECHARGE_ST1) {
  3444. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3445. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3446. }
  3447. }
  3448. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  3449. log_info("EV Board Stop Charging with some error");
  3450. ChargingAlarmProcess(gunIndex);
  3451. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  3452. log_info("Power Cabient Stop Charging");
  3453. ChargingTerminalProcess(gunIndex);
  3454. }
  3455. } else if (isEvBoardNormalStopChargeFlag(gunIndex) == YES) {
  3456. // 板端要求停止 (正常)
  3457. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3458. sysStatus != S_CCS_PRECHARGE_ST1) {
  3459. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3460. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3461. }
  3462. }
  3463. log_info("EV Board Normal Stop Charging");
  3464. ChargingTerminalProcess(gunIndex);
  3465. } else if (OcppRemoteStop(gunIndex) == YES ||
  3466. WifiScheduleStop(gunIndex)) {
  3467. // 後臺要求停止
  3468. log_info("OCPP Stop Charging");
  3469. ChargingTerminalProcess(gunIndex);
  3470. }
  3471. }
  3472. static void checkOCPPReqStop(uint8_t gunIndex)
  3473. {
  3474. if (OcppRemoteStop(gunIndex) == YES ||
  3475. WifiScheduleStop(gunIndex) ||
  3476. CheckBackendChargingTimeout(gunIndex) ||
  3477. CheckBackendChargingEnergy(gunIndex) ||
  3478. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL) {
  3479. // 後臺要求停止
  3480. ChargingTerminalProcess(gunIndex);
  3481. }
  3482. }
  3483. void ResetIdleData(uint8_t gunIndex)
  3484. {
  3485. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3486. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3487. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3488. float powerconsumption = ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption;
  3489. pDcChargingInfo->PresentChargedDuration = 0;
  3490. pDcChargingInfo->RemainChargingDuration = 0;
  3491. pDcChargingInfo->PresentChargingVoltage = 0;//DS60-120 add
  3492. pDcChargingInfo->PresentChargingCurrent = 0;//DS60-120 add
  3493. pDcChargingInfo->RemoteStartFlag = NO;
  3494. pDcChargingInfo->Replug_flag = FALSE;
  3495. strcpy((char *)pDcChargingInfo->StartDateTime, "");
  3496. strcpy((char *)pDcChargingInfo->StopDateTime, "");
  3497. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3498. strcpy((char *)pSysConfig->UserId, "");
  3499. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "");
  3500. ClearDetectPluginFlag(gunIndex);
  3501. //Jerry add
  3502. memset(&ShmSelectGunInfo->PricesInfo[gunIndex], 0, sizeof(PricesInfo));
  3503. memset(&ShmDcCommonData->TransactionInfo[gunIndex], 0, sizeof(RecordTransactionInfo));
  3504. memset(&ShmDcCommonData->pGunInfo[gunIndex], 0, sizeof(GunInfo));
  3505. ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption = powerconsumption;
  3506. ShmDcCommonData->finalcost_flag[gunIndex] = FALSE;
  3507. ShmDcCommonData->AuthPass_flag[gunIndex] = FALSE;
  3508. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  3509. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  3510. ShmDcCommonData->is_RemoteStart[gunIndex] = FALSE;
  3511. ShmDcCommonData->is_AutoStart[gunIndex] = FALSE;
  3512. ShmDcCommonData->is_exit[gunIndex] = FALSE;
  3513. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  3514. ShmDcCommonData->is_plugout[gunIndex] = FALSE;
  3515. ShmSelectGunInfo->PricesInfo[gunIndex].Balance = FAIL_BALANCE_PRICES;
  3516. destroySelGun(gunIndex);
  3517. ResetDetAlarmStatus(gunIndex); //recovery OVP status code
  3518. if (ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] == YES) {
  3519. ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] = NO;
  3520. }
  3521. //strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].VendorErrorCode, "");
  3522. ReleaseAlarmCode(gunIndex);
  3523. if (pSysInfo->SystemPage != _PAGE_SELECT_GUN &&
  3524. pSysInfo->SystemPage != _PAGE_SELECT_PAY &&
  3525. pSysInfo->CurGunSelected == gunIndex) {
  3526. systemPageRestoreInit();
  3527. }
  3528. }
  3529. void CheckErrorCode(uint8_t gunIndex)
  3530. {
  3531. /*
  3532. struct ChargingInfoData *pSelectedDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3533. if (gunIndex == pSysInfo->CurGunSelected) {
  3534. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3535. } else if (pSelectedDcChargingInfo->SystemStatus != S_IDLE &&
  3536. pSelectedDcChargingInfo->SystemStatus != S_RESERVATION &&
  3537. pSelectedDcChargingInfo->SystemStatus != S_MAINTAIN &&
  3538. pSelectedDcChargingInfo->SystemStatus != S_FAULT) {
  3539. if (pSelectedDcChargingInfo->SystemStatus == S_CHARGING) {
  3540. if(ShmDcCommonData->PayPass_flag[gunIndex] == FALSE)
  3541. pSysInfo->SystemPage = _PAGE_PAYING;
  3542. else
  3543. pSysInfo->SystemPage = _PAGE_COMPLETE;
  3544. } else {
  3545. systemPageRestoreInit();
  3546. }
  3547. }
  3548. */
  3549. ClearDetectPluginFlag(gunIndex);
  3550. if (pDcChargingInfo->SystemStatus != S_FAULT)
  3551. {
  3552. UpdateErrorCodeToOcpp(gunIndex);
  3553. setChargerMode(gunIndex, MODE_FAULT);
  3554. }
  3555. }
  3556. void AuthorizeStopCharging(uint8_t gunIndex)
  3557. {
  3558. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3559. checkOCPPReqStop(gunIndex);
  3560. //RfidStopCharging();
  3561. }
  3562. void showversion()
  3563. {
  3564. char str[10240];
  3565. int len = 0;
  3566. uint8_t index = 0;
  3567. len += sprintf(&str[len],"\n=========== Dispenser info =========== \n");
  3568. len += sprintf(&str[len],"SW Version = %s\n", fwVersion);
  3569. len += sprintf(&str[len],"Debug Version = %s\n",DebugVersion);
  3570. len += sprintf(&str[len],"ModelName = %s\n", pSysConfig->ModelName);
  3571. len += sprintf(&str[len],"Relay Board FW Rev = %s\n", pSysInfo->RelayModuleFwRev);
  3572. len += sprintf(&str[len],"Fan Board FW Rev = %s\n", pSysInfo->FanModuleFwRev);
  3573. len += sprintf(&str[len],"Primary FW Rev = %s\n", pSysInfo->CsuPrimFwRev);
  3574. len += sprintf(&str[len],"LED FW Rev = %s\n", pSysInfo->LedModuleFwRev);
  3575. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  3576. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  3577. if (pDcChargingInfo->Type == _Type_Chademo) {
  3578. len += sprintf(&str[len],"CHAdeMO[%d] FW Rev = %s\n",
  3579. index,
  3580. ShmCHAdeMOData->evse[pDcChargingInfo->type_index].version);
  3581. } else if (pDcChargingInfo->Type == _Type_GB) {
  3582. len += sprintf(&str[len],"GBT[%d] FW Rev = %s\n",
  3583. index,
  3584. ShmGBTData->evse[pDcChargingInfo->type_index].version);
  3585. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3586. len += sprintf(&str[len],"CCS[%d] FW Rev = %s\n",
  3587. index,
  3588. ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].version);
  3589. }
  3590. }
  3591. len += sprintf(&str[len],"==================================== ");
  3592. log_info("%s",str);
  3593. }
  3594. void ParkingProcess(int gunIndex)
  3595. {
  3596. if (pSysInfo->CurGunSelected != gunIndex)
  3597. return;
  3598. // 取得佔位費資訊過場畫面
  3599. if (ShmDcCommonData->pGunInfo[gunIndex].ParkingStatus == _TCC_PARKING_NONE) {
  3600. return;
  3601. } else if (!ShmDcCommonData->pGunInfo[gunIndex].GetParkingBill &&
  3602. pSysInfo->SystemPage != _PAGE_IDLE &&
  3603. pSysInfo->SystemPage != _PAGE_SELECT_GUN &&
  3604. pSysInfo->SystemPage != _PAGE_COMPLETE &&
  3605. pSysInfo->SystemPage != _PAGE_PAYFAIL &&
  3606. pSysInfo->SystemPage != _PAGE_PLUGOUT &&
  3607. pSysInfo->SystemPage != _PAGE_SENSING &&
  3608. pSysInfo->SystemPage != _PAGE_PLUGIN) {
  3609. pSysInfo->SystemPage = _PAGE_PAYING;
  3610. StartGunInfoTimeoutDet(gunIndex, Timeout_WaitParkingInfo);
  3611. return;
  3612. }
  3613. if (ShmDcCommonData->pGunInfo[gunIndex].GetParkingBill && pSysInfo->SystemPage == _PAGE_PAYING) {
  3614. pSysInfo->SystemPage = _PAGE_BILL;
  3615. }
  3616. switch (pSysInfo->SystemPage) {
  3617. case _PAGE_BILL:
  3618. case _PAGE_DONATE_LEFT:
  3619. case _PAGE_DONATE_RIGHT:
  3620. case _PAGE_SELECT_PAY:
  3621. StartGunInfoTimeoutDet(gunIndex, Timeout_ParkingSelect);
  3622. break;
  3623. case _PAGE_ADD_FRIEND:
  3624. StartGunInfoTimeoutDet(gunIndex, Timeout_ParkingBill);
  3625. if (ShmDcCommonData->ParkingInfo[gunIndex].LineStatus == _LINE_PARKING_INVOICE) {
  3626. ShmDcCommonData->ParkingInfo[gunIndex].IsDonateInvoice = FALSE;
  3627. pSysInfo->SystemPage = _PAGE_SELECT_PAY;
  3628. StopGunInfoTimeoutDet(gunIndex);
  3629. log_info("Change to Select Pay Page");
  3630. } else if (ShmDcCommonData->ParkingInfo[gunIndex].LineStatus == _LINE_PARKING_DONATE) {
  3631. ShmDcCommonData->ParkingInfo[gunIndex].IsDonateInvoice = TRUE;
  3632. pSysInfo->SystemPage = _PAGE_SELECT_PAY;
  3633. StopGunInfoTimeoutDet(gunIndex);
  3634. log_info("Change to Select Pay Page");
  3635. }
  3636. break;
  3637. case _PAGE_AUTHORIZE:
  3638. StopGunInfoTimeoutDet(gunIndex);
  3639. break;
  3640. case _PAGE_PLUGIN:
  3641. StartGunInfoTimeoutDet(gunIndex,Timeout_ParkingLeave);
  3642. /*
  3643. if (pDcChargingInfo->ConnectorPlugIn == NO ) {
  3644. if (pSysInfo->CurGunSelected == gunIndex) {
  3645. StartGunInfoTimeoutDet(gunIndex, Timeout_ExitPage);
  3646. }
  3647. if (pDcChargingInfo->TimeoutFlag != Timeout_ExitPage) {
  3648. StartGunInfoTimeoutDet(gunIndex, Timeout_CompletPlugout);
  3649. }
  3650. }
  3651. */
  3652. break;
  3653. case _PAGE_AUTHORIZE_FAIL:
  3654. StartSystemTimeoutDet(Timeout_VerifyFail);
  3655. break;
  3656. case _PAGE_PLUGOUT:
  3657. StopGunInfoTimeoutDet(gunIndex);
  3658. break;
  3659. }
  3660. }
  3661. int main(void)
  3662. {
  3663. bool isModelNameMatch = true;
  3664. uint8_t _ocppProfileChkFlag;
  3665. uint8_t gunIndex = 0;
  3666. if (CreateAllCsuShareMemory() == FAIL) {
  3667. log_error("create share memory error");
  3668. return FAIL;
  3669. }
  3670. ClearAllShmMemParameter();
  3671. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  3672. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  3673. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  3674. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  3675. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  3676. pInfoCode = (struct InfoCodeData *)GetShmInfoCodeData();
  3677. ShmPsuData = (struct PsuData *)GetShmPsuData();
  3678. ShmCHAdeMOData = (struct CHAdeMOData *)GetShmCHAdeMOData();
  3679. ShmGBTData = (struct GBTData *)GetShmGBTData();
  3680. ShmCcsData = (struct CcsData *)GetShmCcsData();
  3681. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  3682. ShmFanModuleData = (struct FanModuleData *)GetShmFanModuleData();
  3683. ShmRelayModuleData = (struct RelayModuleData *)GetShmRelayModuleData();
  3684. ShmLedModuleData = (struct LedModuleData *)GetShmLedModuleData();
  3685. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3686. ShmOCPP20Data = (struct OCPP20Data*)GetShmOCPP20Data();
  3687. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  3688. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3689. ShmSysConfigAndInfo = (struct SysConfigAndInfo *)GetShmSysConfigAndInfo();
  3690. ShmStatusCodeData = (struct StatusCodeData *)GetShmStatusCodeData();
  3691. log_info(" ********************************************************");
  3692. log_info(" ****************** FileSystem Boot up *****************");
  3693. log_info(" ********************************************************");
  3694. log_info(" ****************** Project:DD360Tcc *****************");
  3695. log_info(" ********************************************************");
  3696. if (!InitialSystemDefaultConfig()) {
  3697. log_error("InitialSystemDefaultConfig NG ");
  3698. //StopProcessingLoop();
  3699. sleep(5);
  3700. system("reboot -f");
  3701. }
  3702. CheckGunTypeFromHw();
  3703. CheckIsAlternatvieByModelName();
  3704. InitialShareMemoryInfo();
  3705. GetFirmwareVersion();
  3706. if (!CheckConnectorTypeStatus()) {
  3707. isModelNameMatch = false;
  3708. }
  3709. Initialization();
  3710. SpawnTask();
  3711. log_info("Spawned all Task");
  3712. if (!isModelNameMatch) {
  3713. pAlarmCode->AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  3714. //ChangeLcmByIndex(_LCM_ERROR);
  3715. ChangeLcmByIndex(_PAGE_MAINTAIN);
  3716. // Module Name 與硬體對應不正確
  3717. log_error("Module Name & HW info none match. ");
  3718. sleep(3);
  3719. KillAllTask();
  3720. StopProcessingLoop();
  3721. }
  3722. CreateTimeoutFork();
  3723. log_info("Start self test... ");
  3724. SelfTestRun();
  3725. StopSystemTimeoutDet();
  3726. log_info("Self test finished : SelfTestSeq = %d, Work_Step = %d ",
  3727. pSysInfo->SelfTestSeq,
  3728. ShmPsuData->Work_Step);
  3729. if (pSysInfo->SelfTestSeq == _STEST_FAIL || ShmPsuData->Work_Step == _NO_WORKING ||
  3730. pInfoCode->InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES ) {
  3731. ChangeLcmByIndex(_PAGE_MAINTAIN);
  3732. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3733. setChargerMode(gunIndex, MODE_MAINTAIN);
  3734. }
  3735. } else {
  3736. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3737. setChargerMode(gunIndex, MODE_IDLE);
  3738. }
  3739. }
  3740. // Local DB
  3741. if (DB_Open() != PASS) {
  3742. log_info("DB_Open fail. ");
  3743. isDb_ready = false;
  3744. } else {
  3745. isDb_ready = true;
  3746. for (int _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  3747. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  3748. pDcChargingInfo->IsAvailable = DB_Get_Operactive(_index);
  3749. }
  3750. DB_Reboot_Record();
  3751. }
  3752. if (PowerDB_Open() != PASS) {
  3753. log_info("Power DB_Open fail. ");
  3754. isDb_ready = false;
  3755. } else {
  3756. int _retry = 0;
  3757. isDb_ready = true;
  3758. for (int _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  3759. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  3760. while (_retry < 3) {
  3761. if (DB_Get_PowerConsumption(_index)) {
  3762. break;
  3763. } else {
  3764. sleep(1);
  3765. _retry++;
  3766. if (_retry == 3) {
  3767. DB_Insert_PowerConsumption(_index, 0);
  3768. }
  3769. }
  3770. } // while
  3771. } // for
  3772. }
  3773. log_info("===== Create DB End ===== ");
  3774. ChangeLcmByIndex(_PAGE_IDLE);
  3775. sleep(1);
  3776. //***** 須新增的偵測 *****//
  3777. // 1. Thernal - 控制風扇轉速
  3778. // 2. ouput fuse - 控制風扇轉速
  3779. CreateRfidFork();
  3780. // Main loop
  3781. CheckFwSlotStatusLog();
  3782. //AdjustChargerCurrent();
  3783. GetClockTime(&_cmdMainPriority_time, NULL);
  3784. // GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
  3785. CheckTaskAlive();
  3786. CreateWatchdog();
  3787. //SetGunTypeOTPValue();
  3788. showversion();
  3789. ShmDcCommonData->ParkingLeaveTime = 15; //結束充電15分鐘後開始計算佔位費
  3790. ShmDcCommonData->SaleLeaveTime = 10; //結算佔位費後開始計算10分鐘
  3791. for (;;) {
  3792. CheckOcppStatus();
  3793. ChkPrimaryStatus();
  3794. if ((IsConnectorWholeIdle() ||
  3795. //(pSysInfo->PageIndex == _LCM_ERROR)) &&
  3796. (pSysInfo->PageIndex == _PAGE_MAINTAIN))
  3797. ) {
  3798. CheckFactoryConfigFunction();
  3799. //CheckFwUpdateFunction();
  3800. }
  3801. // OCPP 邏輯
  3802. OcppRemoteStartChk();
  3803. // 當 AC 沒有搭上時,清除一些錯誤碼
  3804. ClearAlarmCodeWhenAcOff();
  3805. if ((GetClockTimeoutValue(_cmdMainPriority_time) / 1000) > 5000) {
  3806. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3807. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3808. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3809. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3810. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3811. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3812. if (pDcChargingInfo->SystemStatus == S_CHARGING &&
  3813. _ocppProfileChkFlag == 12) {
  3814. ChargingProfileFlat(gunIndex);
  3815. _ocppProfileChkFlag = 0;
  3816. } else if (pDcChargingInfo->SystemStatus != S_CHARGING) {
  3817. ChargingProfileFlat(gunIndex);
  3818. _ocppProfileChkFlag = 0;
  3819. } else {
  3820. _ocppProfileChkFlag++;
  3821. }
  3822. }
  3823. checkGunOTPState(gunIndex); //check gun OTP
  3824. }
  3825. GetClockTime(&_cmdMainPriority_time, NULL);
  3826. }
  3827. gEvBoardErr.GunErrMessage = 0; //清除系統執行中的錯誤訊息
  3828. gChillerTempErr.TempErrMsg = 0;//清除系統執行中的錯誤訊息
  3829. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3830. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3831. // 重新收集各槍的錯誤狀態
  3832. collectError(gunIndex);
  3833. checkEvBoardAlarmState(pDcChargingInfo->Type);
  3834. }
  3835. checkChillerAlarmState();
  3836. // 確認當前錯誤 Level = 2 ?
  3837. ReviewCriticalAlarm();
  3838. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3839. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3840. CheckGpioInStatus();
  3841. CheckErrorOccurStatus(gunIndex);
  3842. // 確認 Relay Welding or Driving Fault
  3843. CheckRelayWeldingOrDrivingFault(gunIndex);
  3844. ChkOcppStatus(gunIndex);
  3845. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3846. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3847. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3848. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3849. CheckSmartChargeProfile(gunIndex);
  3850. }
  3851. switch (pDcChargingInfo->SystemStatus) {
  3852. case S_IDLE:
  3853. if (isModeChange(gunIndex)) {
  3854. log_info("============================= S_IDLE(%x) ============================= ", gunIndex);
  3855. ResetIdleData(gunIndex);
  3856. }
  3857. isChargingAverageState();
  3858. // For RemoteStart Using
  3859. if (pDcChargingInfo->RemoteStartFlag == YES && pDcChargingInfo->IsAvailable) {
  3860. log_info("-------- IDLE Remote Start(%d) --------", gunIndex);
  3861. //ChangeGunSelectByIndex(gunIndex);
  3862. setChargerMode(gunIndex, MODE_AUTHORIZING);
  3863. break;
  3864. }
  3865. autoStartCharging(gunIndex);
  3866. // 讀卡邏輯
  3867. if (!ShmDcCommonData->TradeCancel && pSysInfo->CurGunSelected == gunIndex)
  3868. ScannerCardProcess(gunIndex);
  3869. if (pSysInfo->SystemPage == _PAGE_SELECT_GUN || pSysInfo->SystemPage == _PAGE_IDLE) {
  3870. //ShmDcCommonData->LineStatus[gunIndex] = 0;
  3871. StopGunInfoTimeoutDet(gunIndex);
  3872. }
  3873. if (pSysInfo->CurGunSelected == gunIndex) {
  3874. if (ShmDcCommonData->pGunInfo[gunIndex].isParking) {
  3875. // 佔位費流程
  3876. ParkingProcess(gunIndex);
  3877. } else {
  3878. // 一般充電流程
  3879. if (pSysInfo->SystemPage == _PAGE_BILL) {
  3880. StartSystemTimeoutDet(Timeout_AddLine);
  3881. /*
  3882. if (ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_ADD_FRIEND) {
  3883. pSysInfo->SystemPage = _PAGE_ADD_FRIEND;
  3884. log_info("Change to Add Line Friend Page");
  3885. }
  3886. */
  3887. } else if (pSysInfo->SystemPage == _PAGE_ADD_FRIEND) {
  3888. StopSystemTimeoutDet();
  3889. StartGunInfoTimeoutDet(gunIndex, Timeout_LineReigster);
  3890. if (ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_CREDITCARD_INVOICE) {
  3891. StopGunInfoTimeoutDet(gunIndex);
  3892. pSysInfo->SystemPage = _PAGE_SELECT_PAY;
  3893. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = FALSE;
  3894. log_info("Change to Select Pay Page");
  3895. }
  3896. if (ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_CREDITCARD_DONATE) {
  3897. StopGunInfoTimeoutDet(gunIndex);
  3898. pSysInfo->SystemPage = _PAGE_SELECT_PAY;
  3899. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = TRUE;
  3900. log_info("Change to Select Pay Page");
  3901. }
  3902. }
  3903. }
  3904. }
  3905. // 檢查預約
  3906. if (ShmDcCommonData->pGunInfo[gunIndex].ReservationStatus) {
  3907. setChargerMode(gunIndex, MODE_RESERVATION);
  3908. }
  3909. goto CheckStatus;
  3910. break;
  3911. case S_RESERVATION:
  3912. if (isModeChange(gunIndex)) {
  3913. log_info("============================= S_RESERVATION(%x) ============================= ", gunIndex);
  3914. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  3915. }
  3916. if (pDcChargingInfo->RemoteStartFlag == YES && pDcChargingInfo->IsAvailable) {
  3917. log_info("-------- S_RESERVATION Remote Start(%d) --------", gunIndex);
  3918. //ChangeGunSelectByIndex(gunIndex);
  3919. setChargerMode(gunIndex, MODE_AUTHORIZING);
  3920. break;
  3921. }
  3922. // 讀卡邏輯
  3923. if (!ShmDcCommonData->TradeCancel && pSysInfo->CurGunSelected == gunIndex)
  3924. ScannerCardProcess(gunIndex);
  3925. // 檢查預約
  3926. if (!ShmDcCommonData->pGunInfo[gunIndex].ReservationStatus) {
  3927. setChargerMode(gunIndex, MODE_IDLE);
  3928. }
  3929. goto CheckStatus;
  3930. break;
  3931. case S_FAULT:
  3932. if (isModeChange(gunIndex)) {
  3933. if (pDcChargingInfo->SystemStatus == S_FAULT) {
  3934. log_info("============================= S_FAULT(%x) ============================= ", gunIndex);
  3935. //pSysInfo->SystemPage = _LCM_ERROR;
  3936. //pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3937. }
  3938. }
  3939. CheckStatus:
  3940. if (pDcChargingInfo->IsAvailable == NO) {
  3941. setChargerMode(gunIndex, MODE_MAINTAIN);
  3942. break;
  3943. }
  3944. // Refresh Error Status
  3945. if (pSysWarning->Level == WARN_LV_ER) {
  3946. CheckErrorCode(gunIndex);
  3947. if (pSysInfo->CurGunSelected == gunIndex)
  3948. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3949. continue;
  3950. }
  3951. if (PrecheckIsPass(gunIndex) == false) {
  3952. log_error("Relay welding");
  3953. setChargerMode(gunIndex, MODE_FAULT);
  3954. if (gunIndex == pSysInfo->CurGunSelected) {
  3955. //pSysInfo->SystemPage = _LCM_ERROR;
  3956. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3957. }
  3958. break;
  3959. }
  3960. if (pDcChargingInfo->SystemStatus == S_FAULT) {
  3961. setChargerMode(gunIndex, MODE_IDLE);
  3962. systemPageRestoreInit();
  3963. }
  3964. break;
  3965. case S_AUTHORIZING:
  3966. if (isModeChange(gunIndex)) {
  3967. log_info("============================= S_AUTHORIZING(%x) ============================= ", gunIndex);
  3968. StartGunInfoTimeoutDet(gunIndex, Timeout_AuthorizingForStop);
  3969. }
  3970. if (pSysInfo->CurGunSelected == gunIndex)
  3971. ScannerCardProcess(gunIndex);
  3972. if (pSysInfo->SystemPage == _PAGE_PLUGIN && ShmDcCommonData->TradeCancel == FALSE) {
  3973. pDcChargingInfo->Replug_flag = TRUE;
  3974. StopSystemTimeoutDet();
  3975. }
  3976. if (isDetectPlugin(gunIndex)) {
  3977. StartGunInfoTimeoutDet(gunIndex, Timeout_WaitPlug);
  3978. // 卡號驗證成功後,等待充電槍插入充電車
  3979. if (pDcChargingInfo->RemoteStartFlag == YES) {
  3980. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3981. pDcChargingInfo->IsAvailable ) {
  3982. log_info("-------- S_AUTHORIZING Remote Start(%d) --------", gunIndex);
  3983. pDcChargingInfo->RemoteStartFlag = NO;
  3984. pDcChargingInfo->isRemoteStart = YES; //DS60-120
  3985. //ChangeGunSelectByIndex(gunIndex);
  3986. //AddPlugInTimes(gunIndex);
  3987. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  3988. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3989. ClearDetectPluginFlag(gunIndex);
  3990. continue;
  3991. }
  3992. } else if (pSysInfo->OrderCharging == NO_DEFINE) {
  3993. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3994. pDcChargingInfo->IsAvailable &&
  3995. pDcChargingInfo->SystemStatus == S_AUTHORIZING &&
  3996. ShmDcCommonData->AuthPass_flag[gunIndex] == TRUE &&
  3997. (waitRightGunPlugIt(gunIndex) == PASS ||
  3998. waitLeftGunPlugIt(gunIndex) == PASS)
  3999. ) {
  4000. log_info("-------- RFID/Auto Start(%d) --------", gunIndex);
  4001. //ChangeGunSelectByIndex(gunIndex);
  4002. //AddPlugInTimes(gunIndex);
  4003. pDcChargingInfo->isRemoteStart = NO;
  4004. strcpy((char *)pDcChargingInfo->StartUserId, (char *)pSysConfig->UserId);
  4005. log_info("index = %d, CardNumber = %s ",
  4006. gunIndex,
  4007. pDcChargingInfo->StartUserId);
  4008. strcpy((char *)pSysConfig->UserId, "");
  4009. // 當前操作的槍號,進入 Preparing
  4010. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  4011. ClearDetectPluginFlag(gunIndex);
  4012. continue;
  4013. }
  4014. }
  4015. if (!GetIsCardScan() && pSysInfo->CurGunSelected == gunIndex &&
  4016. pSysInfo->SystemPage != _PAGE_AUTHORIZE_FAIL &&
  4017. pSysInfo->SystemPage != _PAGE_SENSING) {
  4018. pSysInfo->SystemPage = _PAGE_PLUGIN;
  4019. } else if (!GetIsCardScan() && pSysInfo->SystemPage == _PAGE_IDLE &&
  4020. pDcChargingInfo->RemoteStartFlag == YES) {
  4021. pSysInfo->CurGunSelected = gunIndex;
  4022. }
  4023. } else if (ShmDcCommonData->AuthPass_flag[gunIndex] && pSysInfo->CurGunSelected == gunIndex &&
  4024. pSysInfo->SystemPage != _PAGE_AUTHORIZE_FAIL) {
  4025. pSysInfo->SystemPage = _PAGE_SENSING;
  4026. }
  4027. break;
  4028. case S_REASSIGN_CHECK: {
  4029. if (isModeChange(gunIndex)) {
  4030. log_info("============================= S_REASSIGN_CHECK(%x) ============================= ", gunIndex);
  4031. if (pSysInfo->OrderCharging != NO_DEFINE) {
  4032. pSysInfo->OrderCharging = NO_DEFINE;
  4033. pDcChargingInfo->_SaftyDetect = FALSE;
  4034. }
  4035. pDcChargingInfo->Replug_flag = TRUE;
  4036. StopSystemTimeoutDet();
  4037. StopGunInfoTimeoutDet(gunIndex);
  4038. }
  4039. setChargerMode(gunIndex, S_PREPARNING);
  4040. if (pSysInfo->CurGunSelected == gunIndex) {
  4041. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4042. }
  4043. }
  4044. break;
  4045. case S_PREPARNING:
  4046. if (isModeChange(gunIndex)) {
  4047. log_info("============================= S_PREPARNING(%x) ============================= ", gunIndex);
  4048. StopGunInfoTimeoutDet(gunIndex);
  4049. StartGunInfoTimeoutDet(gunIndex, Timeout_Preparing);
  4050. }
  4051. if (ShmPsuData->SystemPresentPsuQuantity > 0 &&
  4052. ShmPsuData->SystemAvailablePower > 10 &&
  4053. GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) >= 1000000) {
  4054. setChargerMode(gunIndex, MODE_PREPARE_FOR_EV);
  4055. }
  4056. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4057. if (pSysInfo->CurGunSelected == gunIndex) {
  4058. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4059. }
  4060. break;
  4061. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  4062. if (isModeChange(gunIndex)) {
  4063. log_info("============================= S_PREPARING_FOR_EV(%x) ============================= ", gunIndex);
  4064. log_info("Waitting for Gun locked.");
  4065. StopGunInfoTimeoutDet(gunIndex);
  4066. StartGunInfoTimeoutDet(gunIndex, Timeout_EvChargingDet);
  4067. }
  4068. if (pDcChargingInfo->Type == _Type_Chademo) {
  4069. if (isEvGunLocked_chademo(gunIndex) == YES) {
  4070. log_info("Gun Locked");
  4071. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  4072. }
  4073. } else if (pDcChargingInfo->Type == _Type_GB) {
  4074. if (isEvGunLocked_gb(gunIndex) == YES) {
  4075. log_info("Gun Locked");
  4076. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  4077. }
  4078. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  4079. if (isEvGunLocked_ccs(gunIndex) == YES) {
  4080. log_info("Gun Locked");
  4081. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  4082. }
  4083. }
  4084. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4085. if (pSysInfo->CurGunSelected == gunIndex) {
  4086. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4087. }
  4088. break;
  4089. case S_PREPARING_FOR_EVSE: // Ground fault test, 等待 Relay Board 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  4090. if (isModeChange(gunIndex)) {
  4091. log_info("============================= S_PREPARING_FOR_EVSE(%x) ============================= ", gunIndex);
  4092. StopGunInfoTimeoutDet(gunIndex);
  4093. StartGunInfoTimeoutDet(gunIndex, Timeout_EvseChargingDet);
  4094. }
  4095. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  4096. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4097. if (pSysInfo->CurGunSelected == gunIndex) {
  4098. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4099. }
  4100. break;
  4101. case S_CCS_PRECHARGE_ST0:
  4102. if (isModeChange(gunIndex)) {
  4103. log_info("============================= CCS Precharge Processing 1(%x) ============================= ", gunIndex);
  4104. StopGunInfoTimeoutDet(gunIndex);
  4105. StartGunInfoTimeoutDet(gunIndex, Timeout_ForCcsPrechargeDet);
  4106. }
  4107. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  4108. // GFD 錯誤停止
  4109. RecordAlarmCode(gunIndex, "012235");
  4110. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  4111. }
  4112. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4113. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  4114. // 切換 D+ Relay to Precharge Relay
  4115. if (isPrechargeStatus_ccs(gunIndex) == 39 ||
  4116. isPrechargeStatus_ccs(gunIndex) == 40) {
  4117. if ((pDcChargingInfo->RelayKPK2Status == YES || pDcChargingInfo->PantographFlag == YES) &&
  4118. pDcChargingInfo->PrechargeStatus != PRECHARGE_READY)
  4119. //if (pDcChargingInfo->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  4120. {
  4121. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  4122. }
  4123. } else if (isPrechargeStatus_ccs(gunIndex) == 45 ||
  4124. isPrechargeStatus_ccs(gunIndex) == 46
  4125. ) {
  4126. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP1);
  4127. }
  4128. if (pSysInfo->CurGunSelected == gunIndex) {
  4129. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4130. }
  4131. break;
  4132. case S_CCS_PRECHARGE_ST1:
  4133. if (isModeChange(gunIndex)) {
  4134. log_info("============================= CCS Precharge Processing 2(%x) ============================= ", gunIndex);
  4135. }
  4136. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  4137. // GFD 錯誤停止
  4138. RecordAlarmCode(gunIndex, "012235");
  4139. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  4140. }
  4141. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4142. // 等待小板通知進入充電
  4143. // 切換 D+ Relay to Precharge Relay
  4144. if (pDcChargingInfo->RelayK1K2Status == YES || pDcChargingInfo->PantographFlag == YES) {
  4145. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  4146. // pSysInfo->CurGunSelected = gunIndex;
  4147. setChargerMode(gunIndex, MODE_CHARGING);
  4148. }
  4149. if (pSysInfo->CurGunSelected == gunIndex) {
  4150. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4151. }
  4152. break;
  4153. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  4154. if (isModeChange(gunIndex)) {
  4155. log_info("============================= S_CHARGING(%x) ============================= ", gunIndex);
  4156. StopGunInfoTimeoutDet(gunIndex);
  4157. ftime(&startChargingTime[gunIndex]);
  4158. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, ""); //DS60-120 add
  4159. ChangeStartOrStopDateTime(YES, gunIndex);
  4160. pDcChargingInfo->Replug_flag = FALSE;
  4161. ShmDcCommonData->finalcost_flag[gunIndex] = FALSE;
  4162. ShmDcCommonData->TransactionInfo[gunIndex].isIntoCharge = TRUE;
  4163. ShmDcCommonData->RecordEnergyTime[gunIndex] = time((time_t*)NULL);
  4164. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  4165. }
  4166. if (ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf) {
  4167. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf = NO;
  4168. }
  4169. ftime(&endChargingTime[gunIndex]);
  4170. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  4171. ShmDcCommonData->TransactionInfo[gunIndex].Energy = pDcChargingInfo->PresentChargedEnergy;
  4172. // 每秒紀錄使用電量到資料庫內
  4173. if ((time((time_t*)NULL) - ShmDcCommonData->RecordEnergyTime[gunIndex]) >= 1) {
  4174. ShmDcCommonData->RecordEnergyTime[gunIndex] = time((time_t*)NULL);
  4175. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  4176. }
  4177. if ((time((time_t*)NULL) - ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime) >= 5) {
  4178. ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime = time((time_t*)NULL);
  4179. if (ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption != 0)
  4180. DB_Update_PowerConsumption(gunIndex, ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption);
  4181. }
  4182. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  4183. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4184. // Check Stop Charging
  4185. AuthorizeStopCharging(gunIndex);
  4186. if (pSysInfo->SystemPage == _PAGE_STOP_CONFIRM_LEFT ||
  4187. pSysInfo->SystemPage == _PAGE_STOP_CONFIRM_RIGHT)
  4188. break;
  4189. // LCM => Charging
  4190. if (pSysInfo->CurGunSelected == gunIndex && pSysInfo->SystemPage != _PAGE_IDLE &&
  4191. pSysInfo->SystemPage != _PAGE_SELECT_GUN) {
  4192. pSysInfo->SystemPage = _PAGE_CHARGING;
  4193. }
  4194. break;
  4195. case S_TERMINATING:
  4196. if (isModeChange(gunIndex)) {
  4197. log_info("============================= S_TERMINATING(%x) ============================= ", gunIndex);
  4198. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  4199. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  4200. }
  4201. ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  4202. if (ShmDcCommonData->is_AutoStart[gunIndex]) {
  4203. ShmDcCommonData->finalcost_flag[gunIndex] = TRUE;
  4204. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  4205. }
  4206. if (ShmDcCommonData->finalcost_flag[gunIndex] == FALSE && pSysInfo->CurGunSelected == gunIndex) {
  4207. pSysInfo->SystemPage = _PAGE_PAYING;
  4208. }
  4209. StartSystemTimeoutDet(Timeout_Terminating);
  4210. StartGunInfoTimeoutDet(gunIndex, Timeout_FinalCost);
  4211. ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime = time((time_t*)NULL);
  4212. if (ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption != 0)
  4213. DB_Update_PowerConsumption(gunIndex, ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption);
  4214. }
  4215. // For Precharging timeout
  4216. if (pDcChargingInfo->Type == _Type_Chademo) {
  4217. if (isEvStopCharging_chademo(gunIndex) == YES ||
  4218. isPrechargeStatus_chademo(gunIndex) <= 0) {
  4219. setChargerMode(gunIndex, MODE_COMPLETE);
  4220. }
  4221. } else if (pDcChargingInfo->Type == _Type_GB) {
  4222. if (isEvStopCharging_gb(gunIndex) == YES ||
  4223. isPrechargeStatus_gb(gunIndex) <= 0) {
  4224. setChargerMode(gunIndex, MODE_COMPLETE);
  4225. }
  4226. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  4227. if (isEvStopCharging_ccs(gunIndex) == YES &&
  4228. (isPrechargeStatus_ccs(gunIndex) >= 53 ||
  4229. isPrechargeStatus_ccs(gunIndex) == 0 ||
  4230. isPrechargeStatus_ccs(gunIndex) == 13 ||
  4231. isPrechargeStatus_ccs(gunIndex) == 14)
  4232. ) {
  4233. setChargerMode(gunIndex, MODE_COMPLETE);
  4234. }
  4235. }
  4236. if (pDcChargingInfo->Replug_flag == TRUE && pSysInfo->CurGunSelected == gunIndex) {
  4237. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  4238. break;
  4239. }
  4240. if (pSysInfo->CurGunSelected == gunIndex &&
  4241. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4242. if (ShmDcCommonData->PayPass_flag[gunIndex] == TRUE)
  4243. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4244. else
  4245. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  4246. }
  4247. break;
  4248. case S_COMPLETE:
  4249. if (isModeChange(gunIndex)) {
  4250. log_info ("============================= S_COMPLETE(%x) ============================= ", gunIndex);
  4251. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  4252. OcppStopTransation(gunIndex);
  4253. }
  4254. TheEndCharging(gunIndex);
  4255. if (pDcChargingInfo->Replug_flag != TRUE && pSysInfo->SystemPage != _PAGE_PLUGOUT)
  4256. StopSystemTimeoutDet();
  4257. if (ShmDcCommonData->is_AutoStart[gunIndex] == TRUE && pSysInfo->CurGunSelected == gunIndex)
  4258. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4259. }
  4260. //if (pSysInfo->SystemPage == _LCM_ERROR) {
  4261. if (pSysInfo->SystemPage == _PAGE_MAINTAIN) {
  4262. break;
  4263. }
  4264. if (pSysInfo->CurGunSelected == gunIndex && ShmDcCommonData->is_exit[gunIndex]) {
  4265. pSysInfo->SystemPage = _PAGE_EXIT;
  4266. break;
  4267. }
  4268. if (pDcChargingInfo->ConnectorPlugIn == NO &&
  4269. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4270. if (pSysInfo->CurGunSelected == gunIndex) {
  4271. StartGunInfoTimeoutDet(gunIndex, Timeout_ExitPage);
  4272. }
  4273. if (pDcChargingInfo->TimeoutFlag != Timeout_ExitPage &&
  4274. pDcChargingInfo->TimeoutFlag != Timeout_FinalCost) {
  4275. StartGunInfoTimeoutDet(gunIndex, Timeout_CompletPlugout);
  4276. }
  4277. }
  4278. if (ShmDcCommonData->pGunInfo[gunIndex].isParking) {
  4279. ParkingProcess(gunIndex);
  4280. break;
  4281. }
  4282. if (pDcChargingInfo->Replug_flag == TRUE && pSysInfo->CurGunSelected == gunIndex) {
  4283. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  4284. break;
  4285. }
  4286. if (pSysInfo->CurGunSelected == gunIndex &&
  4287. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4288. if (ShmDcCommonData->PayPass_flag[gunIndex] == TRUE)
  4289. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4290. else
  4291. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  4292. }
  4293. break;
  4294. case S_ALARM:
  4295. if (isModeChange(gunIndex)) {
  4296. log_info("============================= S_ALARM(%x) ============================= ", gunIndex);
  4297. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  4298. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  4299. }
  4300. UpdateErrorCodeToOcpp(gunIndex);
  4301. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  4302. OcppStopTransation(gunIndex);
  4303. }
  4304. TheEndCharging(gunIndex);
  4305. StopGunInfoTimeoutDet(gunIndex);
  4306. ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  4307. if (ShmDcCommonData->is_AutoStart[gunIndex]) {
  4308. ShmDcCommonData->finalcost_flag[gunIndex] = TRUE;
  4309. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  4310. }
  4311. if (pDcChargingInfo->Replug_flag == TRUE) {
  4312. } else {
  4313. StartGunInfoTimeoutDet(gunIndex, Timeout_FinalCost);
  4314. if(ShmDcCommonData->finalcost_flag[gunIndex] == FALSE && pSysInfo->CurGunSelected == gunIndex) {
  4315. pSysInfo->SystemPage = _PAGE_PAYING;
  4316. }
  4317. }
  4318. ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime = time((time_t*)NULL);
  4319. if (ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption != 0)
  4320. DB_Update_PowerConsumption(gunIndex, ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption);
  4321. }
  4322. if (pSysWarning->Level == WARN_LV_ER) {
  4323. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4324. continue;
  4325. }
  4326. if (pSysInfo->CurGunSelected == gunIndex && ShmDcCommonData->is_exit[gunIndex]) {
  4327. pSysInfo->SystemPage = _PAGE_EXIT;
  4328. break;
  4329. }
  4330. if (pDcChargingInfo->ConnectorPlugIn == NO &&
  4331. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4332. if (pSysInfo->CurGunSelected == gunIndex) {
  4333. StartGunInfoTimeoutDet(gunIndex, Timeout_ExitPage);
  4334. }
  4335. if (pDcChargingInfo->TimeoutFlag != Timeout_ExitPage &&
  4336. pDcChargingInfo->TimeoutFlag != Timeout_FinalCost) {
  4337. StartGunInfoTimeoutDet(gunIndex, Timeout_CompletPlugout);
  4338. }
  4339. }
  4340. if (ShmDcCommonData->pGunInfo[gunIndex].isParking) {
  4341. ParkingProcess(gunIndex);
  4342. break;
  4343. }
  4344. if (pDcChargingInfo->Replug_flag == TRUE && pSysInfo->CurGunSelected == gunIndex) {
  4345. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  4346. break;
  4347. }
  4348. if (pSysInfo->CurGunSelected == gunIndex && pSysInfo->SystemPage != _PAGE_PAYING ) {
  4349. if (ShmDcCommonData->PayPass_flag[gunIndex] == TRUE)
  4350. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4351. else
  4352. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  4353. }
  4354. break;
  4355. case S_MAINTAIN:
  4356. if (isModeChange(gunIndex)) {
  4357. log_info("============================= S_MAINTAIN(%x) ============================= ", gunIndex);
  4358. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4359. if (pSysInfo->FirmwareUpdate == YES) {
  4360. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4361. continue;
  4362. }
  4363. if (pDcChargingInfo->IsAvailable == NO) {
  4364. log_info("Gun%d is not availbale");
  4365. break;
  4366. }
  4367. if (pSysWarning->Level != WARN_LV_ER) {
  4368. if (!DisplaySelfTestFailReason()) { //DS60-120 add
  4369. log_info("Soft reboot for retry self-tets. ");
  4370. sleep(3);
  4371. system("killall OcppBackend &");
  4372. KillAllTask();
  4373. system("/usr/bin/run_evse_restart.sh");
  4374. }
  4375. }
  4376. sleep(3);
  4377. if (pSysWarning->Level == WARN_LV_ER) { //DS60-120 add
  4378. KillTaskExceptPrimary();
  4379. } else {
  4380. KillTask();
  4381. }
  4382. /*
  4383. if (pSysInfo->SelfTestSeq == _STEST_FAIL)
  4384. StopProcessingLoop();
  4385. */
  4386. }
  4387. if (pSysInfo->SelfTestSeq == _STEST_FAIL && pSysInfo->SystemTimeoutFlag != Timeout_SelftestChk) {
  4388. SelfTestRun();
  4389. StopSystemTimeoutDet();
  4390. sleep(3);
  4391. break;
  4392. }
  4393. if (pDcChargingInfo->IsAvailable && pSysInfo->SelfTestSeq == _STEST_COMPLETE) {
  4394. setChargerMode(gunIndex, MODE_IDLE);
  4395. }
  4396. if (pSysInfo->CurGunSelected == gunIndex) {
  4397. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4398. }
  4399. break;
  4400. case S_UPDATE:
  4401. if (isModeChange(gunIndex)) {
  4402. log_info("============================= S_UPDATE ============================= ");
  4403. }
  4404. if (pSysInfo->FirmwareUpdate == YES) {
  4405. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4406. continue;
  4407. } else {
  4408. }
  4409. break;
  4410. }//switch
  4411. }//for
  4412. //if (pSysInfo->SystemPage != _LCM_VIEW)
  4413. //ChangeLcmByIndex(pSysInfo->SystemPage);
  4414. TryFeedWatchdog();
  4415. usleep(WHILE_LOOP_TIME);
  4416. }
  4417. return FAIL;
  4418. }