main.c 210 KB

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