12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837 |
- #include "define.h"
- #include "main.h"
- //==========================
- // System basic sample constant
- //==========================
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- #define YES 1
- #define NO 0
- #define ON 1
- #define OFF 0
- #define BUFFER_SIZE 128
- //==========================
- // Timer interval
- //==========================
- #define TMR_IDX_HANDSHAKING 0
- #define TMR_IDX_AUTH 1
- #define TMR_IDX_LOGPPRINTOUT 2
- #define TMR_IDX_PROFILE_PREPARE 3
- #define TMR_IDX_PWN_CHANGE 4
- #define TMR_IDX_CHECK_TASK 5
- #define TMR_IDX_RESET_4G_WIFI_POWER 6
- #define TMR_IDX_7 7
- #define TMR_IDX_8 8
- #define TMR_IDX_9 9
- #define TIMEOUT_SPEC_HANDSHAKING 180000
- #define TIMEOUT_SPEC_AUTH 15000
- #define TIMEOUT_SPEC_HANDSHAKING_LED 185000
- #define TIMEOUT_SPEC_LOGPPRINTOUT 30000
- #define TIMEOUT_SPEC_PROFILE_PREPARE 60000
- #define TIMEOUT_SPEC_PWN_CHANGE 5000
- #define TIMEOUT_SPEC_RESET_4G_WIFI_POWER 300000
- #define SPEC_TEMPERATURE_WARN 71
- #define SPEC_TEMPERATURE_RECOVER 63
- #define MtdBlockSize 0x300000
- #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
- #define DB_FILE_OCPP_LOCAL "/Storage/OCPP/charger.db"
- //==========================
- // Declare method
- //==========================
- void trim(char *s);
- int mystrcmp(char *p1,char *p2);
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
- void split(char **arr, char *str, const char *del);
- int isReachableInternet();
- int isRouteFail();
- int InitRfidPort(void);
- int GetCardSerialNumber();
- void setLedMotion(unsigned char gun_index,unsigned char led_mode);
- void setRelay(unsigned char gun_index,unsigned char isOn);
- void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
- //==========================
- // Declare RFID module type
- //==========================
- #define MODULE_EWT 0
- int wtdFd = -1;
- int rfidFd = -1;
- char* rfidPortName = "/dev/ttyS2";
- RFID rfid;
- char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
- int isRfidReadable;
- //==========================
- // Declare share memory
- //==========================
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- struct PsuData *ShmPsuData;
- struct CHAdeMOData *ShmCHAdeMOData;
- struct CcsData *ShmCcsData;
- struct PrimaryMcuData *ShmPrimaryMcuData;
- struct FanModuleData *ShmFanModuleData;
- struct RelayModuleData *ShmRelayModuleData;
- struct OCPP16Data *ShmOCPP16Data;
- struct OCPP20Data *ShmOCPP20Data;
- struct OCPP16Data *ShmOCPP16DataPH;
- struct Charger *ShmCharger;
- struct timeb startTime[AC_QUANTITY][10];
- struct timeb startChargingTime[AC_QUANTITY];
- struct timeb endChargingTime[AC_QUANTITY];
- sqlite3 *localDb;
- sqlite3 *ocppDb;
- struct SysConfigData SysConfigOrg;
- ParsingRatedCur modelnameInfo={0};
- struct PreviousData
- {
- uint16_t targetCurrent;
- uint16_t current_limit;
- uint16_t primaryMcuCp_Pwn_Duty;
- }previousData[4];
- //=================================
- // Common routine
- //=================================
- void trim(char *s)
- {
- int i=0, j, k, l=0;
- while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
- i++;
- j = strlen(s)-1;
- while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
- j--;
- if(i==0 && j==strlen(s)-1) { }
- else if(i==0) s[j+1] = '\0';
- else {
- for(k=i; k<=j; k++) s[l++] = s[k];
- s[l] = '\0';
- }
- }
- int mystrcmp(char *p1,char *p2)
- {
- while(*p1==*p2)
- {
- if(*p1=='\0' || *p2=='\0')
- break;
- p1++;
- p2++;
- }
- if(*p1=='\0' && *p2=='\0')
- return(PASS);
- else
- return(FAIL);
- }
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
- {
- strncpy(dest, src + start, cnt);
- dest[cnt] = 0;
- }
- void split(char **arr, char *str, const char *del)
- {
- char *s = strtok(str, del);
- while(s != NULL)
- {
- *arr++ = s;
- s = strtok(NULL, del);
- }
- }
- int StoreLogMsg(const char *fmt, ...)
- {
- char Buf[4096+256];
- char buffer[4096];
- time_t CurrentTime;
- struct tm *tm;
- struct timeval tv;
- va_list args;
- va_start(args, fmt);
- int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
- va_end(args);
- memset(Buf,0,sizeof(Buf));
- CurrentTime = time(NULL);
- tm=localtime(&CurrentTime);
- gettimeofday(&tv, NULL); // get microseconds, 10^-6
- if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
- {
- sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
- tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
- buffer,
- tm->tm_year+1900,tm->tm_mon+1,
- ShmSysConfigAndInfo->SysConfig.ModelName,
- ShmSysConfigAndInfo->SysConfig.SerialNumber);
- }
- else
- {
- sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
- tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
- buffer,
- tm->tm_year+1900,tm->tm_mon+1);
- }
- #ifdef SystemLogMessage
- system(Buf);
- #endif
- #ifdef ConsloePrintLog
- printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
- #endif
- return rc;
- }
- long long DiffTimebWithNow(struct timeb ST)
- {
- //return milli-second
- struct timeb ET;
- long long StartTime,StopTime;
- ftime(&ET);
- StartTime=(long long)ST.time;
- StopTime=(long long)ET.time;
- return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
- }
- long long DiffTimeb(struct timeb ST, struct timeb ET)
- {
- //return milli-second
- long long StartTime,StopTime;
- StartTime=(long long)ST.time;
- StopTime=(long long)ET.time;
- return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
- }
- int DiffTimebWithNowSec(struct timeb ST)
- {
- //return milli-second
- struct timeb ET;
- unsigned int StartTime,StopTime;
- ftime(&ET);
- StartTime=(unsigned int)ST.time;
- StopTime=(unsigned int)ET.time;
- return (StopTime-StartTime);
- }
- int isOvertNow(uint8_t *start)
- {
- int result = YES;
- struct ParsingResult
- {
- int result;
- int scanedElement;
- int year;
- int month;
- int mday;
- int hour;
- int min;
- int sec;
- int tz_hour;
- int tz_min;
- float minSec;
- }parsingResult;
- struct tm tmStart;
- struct timeb tbStart;
- memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
- if(strstr((char*)start, ".") != NULL)
- {
- // Original data with mini second
- if(strstr((char*)start, "Z") != NULL)
- {
- // Original data with Z
- parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
- &parsingResult.year,
- &parsingResult.month,
- &parsingResult.mday,
- &parsingResult.hour,
- &parsingResult.min,
- &parsingResult.sec,
- &parsingResult.minSec);
- }
- else
- {
- // Original data without Z
- parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
- &parsingResult.year,
- &parsingResult.month,
- &parsingResult.mday,
- &parsingResult.hour,
- &parsingResult.min,
- &parsingResult.sec,
- &parsingResult.minSec,
- &parsingResult.tz_hour,
- &parsingResult.tz_min);
- }
- }
- else
- {
- // Original data without mini second
- if(strstr((char*)start, "Z") != NULL)
- {
- // Original data with Z
- parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
- &parsingResult.year,
- &parsingResult.month,
- &parsingResult.mday,
- &parsingResult.hour,
- &parsingResult.min,
- &parsingResult.sec);
- }
- else
- {
- // Original data without Z
- parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
- &parsingResult.year,
- &parsingResult.month,
- &parsingResult.mday,
- &parsingResult.hour,
- &parsingResult.min,
- &parsingResult.sec,
- &parsingResult.tz_hour,
- &parsingResult.tz_min);
- }
- }
- if(parsingResult.scanedElement >= 6)
- {
- tmStart.tm_year = parsingResult.year - 1900;
- tmStart.tm_mon = parsingResult.month - 1;
- tmStart.tm_mday = parsingResult.mday;
- tmStart.tm_hour = parsingResult.hour;
- tmStart.tm_min = parsingResult.min;
- tmStart.tm_sec = parsingResult.sec;
- tmStart.tm_gmtoff = 0;
- tbStart.time = mktime(&tmStart);
- tbStart.timezone = 0;
- tbStart.millitm = 0;
- tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
- if(DiffTimebWithNowSec(tbStart) <= 0)
- result = NO;
- else
- result = YES;
- }
- else
- {
- DEBUG_WARN("Start date parsing error.\n");
- }
- return result;
- }
- void getDateTimeString(char* result)
- {
- time_t CurrentTime;
- struct tm *tm;
- CurrentTime = time(NULL);
- tm=localtime(&CurrentTime);
- sprintf(result, "%04d.%02d.%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
- }
- int getCurrentYear()
- {
- int result = 0;
- time_t CurrentTime;
- struct tm *tm;
- CurrentTime = time(NULL);
- tm=localtime(&CurrentTime);
- result = (tm->tm_year + 1900);
- return result;
- }
- unsigned long long getAvailableMemory()
- {
- long pages = sysconf(_SC_AVPHYS_PAGES);
- long page_size = sysconf(_SC_PAGE_SIZE);
- return pages * page_size;
- }
- unsigned int isKernelSupportNAT()
- {
- unsigned int result = NO;
- unsigned int version = 0;
- FILE *fp;
- char cmd[256];
- char buf[512];
- // Get IP address & net mask
- strcpy(cmd, "uname -v");
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- if(fgets(buf, sizeof(buf), fp) != NULL)
- {
- sscanf(buf, "#%d", &version);
- //DEBUG_INFO("Kernel version: %d\n", version);
- if(version >= 30)
- result = YES;
- }
- }
- pclose(fp);
- return result;
- }
- int getEth0MacAddress()
- {
- int result = PASS;
- FILE *fp;
- char cmd[256];
- char buf[512];
- char tmp[512];
- strcpy(cmd, "ifconfig eth0");
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "eth0") > 0)
- {
- result = PASS;
- }
- if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
- {
- sscanf(buf, "%*s%*s%*s%*s%s", tmp);
- strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
- }
- }
- }
- pclose(fp);
- return result;
- }
- int isUap0up(void)
- {
- int result = FAIL;
- FILE *fp;
- char cmd[256];
- char buf[512];
- strcpy(cmd, "ifconfig uap0");;
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "uap0") > 0)
- {
- result = PASS;
- }
- }
- }
- pclose(fp);
- return result;
- }
- //======================================================
- // OCPP routine
- //======================================================
- void ocpp_process_start()
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- system("/root/OcppBackend &");
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- system("/root/OcppBackend20 &");
- }
- uint8_t ocpp_get_isRemoteStartWait()
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
- }
- return result;
- }
- uint8_t ocpp_get_connection_status()
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->OcppConnStatus;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->OcppConnStatus;
- }
- return result;
- }
- uint16_t ocpp_get_connection_timeout()
- {
- uint16_t result = (TIMEOUT_SPEC_HANDSHAKING/1000);
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
- {
- result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
- {
- result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
- }
- }
- return result;
- }
- uint8_t ocpp_get_update_firmware_req()
- {
- uint8_t result = NO;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq || ShmOCPP16Data->MsMsg.bits.SignedUpdateFirmwareReq;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq;
- }
- return result;
- }
- uint8_t ocpp_get_reset_req()
- {
- uint8_t result = NO;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->MsMsg.bits.ResetReq;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
- {
- result = ShmOCPP20Data->MsMsg.bits.ResetReq;
- }
- else
- {
- if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_CHARGING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_TERMINATING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_COMPLETE)) &&
- (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus != SYS_MODE_CHARGING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus != SYS_MODE_TERMINATING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus != SYS_MODE_COMPLETE)):TRUE))
- {
- result = ShmOCPP20Data->MsMsg.bits.ResetReq;
- }
- }
- }
- return result;
- }
- void ocpp_boot_info_sync()
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
- memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
- sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
- sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
- switch(ShmSysConfigAndInfo->SysConfig.ModelName[3])
- {
- case 'M':
- case 'Z':
- sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "MID");
- break;
- case 'P':
- sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "PTB");
- break;
- case 'I':
- sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "TIC");
- break;
- case 'U':
- sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "UL");
- break;
- default:
- sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "N/A");
- break;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- memcpy((char*)ShmOCPP20Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
- memcpy((char*)ShmOCPP20Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
- sprintf((char*)ShmOCPP20Data->BootNotification.chargingStation.firmwareVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
- }
- }
- void ocpp_set_remotestart(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq != status)
- ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = status;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq != status)
- ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = status;
- }
- }
- void ocpp_set_remotestop(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != status)
- ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = status;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq != status)
- ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = status;
- }
- }
- uint8_t ocpp_get_remotestart(uint8_t gun_index)
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
- }
- return result;
- }
- void ocpp_copy_userid_from_remotestart(uint8_t gun_index)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
- DEBUG_INFO("==========================================\n");
- DEBUG_INFO("Copy IdTag into UserId. \n");
- DEBUG_INFO("IdTag : [%s] ---> UserId : [%s] . \n", ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ShmSysConfigAndInfo->SysConfig.UserId);
- DEBUG_INFO("==========================================\n");
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- DEBUG_INFO("==========================================\n");
- DEBUG_INFO("Copy idToken into UserId. \n");
- DEBUG_INFO("idToken : [%s] ---> UserId : [%s]. \n", ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ShmSysConfigAndInfo->SysConfig.UserId);
- DEBUG_INFO("==========================================\n");
- }
- }
- uint8_t ocpp_get_remotestop(uint8_t gun_index)
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq;
- }
- return result;
- }
- void ocpp_set_auth_conf(uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf != status)
- ShmOCPP16Data->SpMsg.bits.AuthorizeConf = status;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->SpMsg.bits.AuthorizeConf != status)
- ShmOCPP20Data->SpMsg.bits.AuthorizeConf = status;
- }
- }
- void ocpp_set_auth_req(uint8_t status, ...)
- {
- va_list args;
- if(status == ON)
- ocpp_set_auth_conf(OFF);
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->SpMsg.bits.AuthorizeReq != status)
- {
- if(status == ON)
- memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, 0x00, ARRAY_SIZE(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
- ShmOCPP16Data->SpMsg.bits.AuthorizeReq = status;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->SpMsg.bits.AuthorizeReq != status)
- {
- if(status == ON)
- {
- memset(&ShmOCPP20Data->Authorize.Response_idTokenInfo, 0x00, sizeof(struct IdTokenInfoType));
- va_start(args, status);
- sprintf((char*)ShmOCPP20Data->Authorize.idToken.type, "%s", va_arg(args, char*));
- va_end(args);
- }
- ShmOCPP20Data->SpMsg.bits.AuthorizeReq = status;
- }
- }
- }
- uint8_t ocpp_get_auth_req()
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->SpMsg.bits.AuthorizeReq;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->SpMsg.bits.AuthorizeReq;
- }
- return result;
- }
- uint8_t ocpp_get_auth_conf()
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
- }
- return result;
- }
- uint8_t ocpp_get_auth_result(uint8_t gun_index)
- {
- uint8_t result = OFF;
- switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
- {
- case SYS_MODE_AUTHORIZING:
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1) && (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0)) ||
- ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId != -1) && ((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) && (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag)==0))))
- result = PASS;
- DEBUG_INFO("============ [SYS_MODE_AUTHORIZING] ==============\n");
- DEBUG_INFO("Authorize response status: [ %s ].\n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
- if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId != -1))
- {
- DEBUG_INFO("Gun-%d Reservation id: [ %d ].\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId);
- DEBUG_INFO("Authorize response ParentIdTag: [ %s ].\n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
- DEBUG_INFO("Gun-%d Reserve ParentIdTag: [ %s ].\n", gun_index, ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag);
- }
- DEBUG_INFO("==================================================\n");
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1) && ((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) && (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_MasterPassGroupId].variableAttribute[0].value) != 0))) ||
- ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId != -1) && ((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) && (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].groupIdToken.idToken)==0))))
- result = PASS;
- }
- break;
- case SYS_MODE_CHARGING:
- case SYS_MODE_TERMINATING:
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
- (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag)==0) &&
- (strlen((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag)>0))
- result = PASS;
- DEBUG_INFO("==== [SYS_MODE_CHARGING/SYS_MODE_TERMINATING] ====\n");
- DEBUG_INFO("Authorize response status: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
- DEBUG_INFO("Authorize response ParentIdTag: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
- DEBUG_INFO("Gun-%d StartTransaction response ParentIdTag: [ %s ]. \n", gun_index, ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag);
- DEBUG_INFO("==================================================\n");
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) &&
- ((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.groupIdToken.idToken) == 0) ||
- (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_MasterPassGroupId].variableAttribute[0].value) == 0)))
- result = PASS;
- }
- break;
- case SYS_MODE_RESERVATION:
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
- (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag)==0))
- result = PASS;
- DEBUG_INFO("============ [SYS_MODE_RESERVATION] ==============\n");
- DEBUG_INFO("Authorize response status: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
- DEBUG_INFO("Authorize response ParentIdTag: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
- DEBUG_INFO("Gun-%d Reserve ParentIdTag: [ %s ]. \n", gun_index, ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag);
- DEBUG_INFO("==================================================\n");
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) &&
- (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].groupIdToken.idToken)==0))
- result = PASS;
- }
- break;
- default:
- break;
- }
- return result;
- }
- uint8_t ocpp_get_unlocker_req(uint8_t gun_index)
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor;
- }
- return result;
- }
- void ocpp_set_unlocker_req(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor != status)
- ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = status;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor != status)
- ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = status;
- }
- }
- void ocpp_set_starttransaction_req(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq != status)
- ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = status;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
- ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
- }
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
- {
- if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq != status)
- ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = status;
- }
- }
- void ocpp_set_starttransaction_conf(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf != status)
- ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = status;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
- ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
- }
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
- {
- if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf != status)
- ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = status;
- }
- }
- void ocpp_set_stoptransaction_req(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq != status)
- ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = status;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
- ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
- }
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
- {
- if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq != status)
- ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = status;
- }
- }
- void ocpp_set_stoptransaction_conf(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf != status)
- {
- ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = status;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
- {
- ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
- }
- }
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
- {
- if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionConf != status)
- ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionConf = status;
- }
- }
- void ocpp_copy_userid_to_starttransaction(uint8_t gun_index)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
- }
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
- {
- memcpy((char*)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16DataPH->StartTransaction[gun_index].IdTag));
- }
- }
- uint8_t ocpp_get_starttransaction_result(uint8_t gun_index)
- {
- uint8_t result = PASS;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
- {
- if((strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Blocked")==0) ||
- (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Expired")==0) ||
- (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid")==0))
- result = NO;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "true"))
- {
- if((strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Blocked")==0) ||
- (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Expired")==0) ||
- (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Invalid")==0) ||
- (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NoCredit")==0) ||
- (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAllowedTypeEVSE")==0) ||
- (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisLocation")==0) ||
- (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisTime")==0) ||
- (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Unknown")==0))
- result = NO;
- }
- }
- return result;
- }
- uint8_t ocpp_get_maxcharging_profileId()
- {
- uint8_t result = 0;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->MaxChargingProfile.ChargingProfileId;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->MaxChargingProfile.id;
- }
- return result;
- }
- uint8_t ocpp_get_smartcharging_profileId(uint8_t gun_index)
- {
- uint8_t result = 0;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->SmartChargingProfile[gun_index].id;
- }
- return result;
- }
- void ocpp_reset_smartcharging_profileId(uint8_t gun_index)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- ShmOCPP20Data->SmartChargingProfile[gun_index].id = 0;
- }
- }
- uint8_t ocpp_get_profile_req(uint8_t gun_index)
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq;
- }
- return result;
- }
- void ocpp_set_profile_req(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
- ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
- ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
- }
- }
- uint8_t ocpp_get_profile_conf(uint8_t gun_index)
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf;
- }
- return result;
- }
- void ocpp_set_profile_conf(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
- ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
- ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
- }
- }
- uint8_t ocpp_get_StopTransactionOnEVSideDisconnect()
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)
- result = ON;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnEVSideDisconnect].variableAttribute[0].value, "true") == 0)
- result = ON;
- }
- return result;
- }
- uint8_t ocpp_get_cancelreservation_req(uint8_t gun_index)
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- result = ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- result = ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq;
- }
- return result;
- }
- void ocpp_set_cancelreservation_req(uint8_t gun_index, uint8_t status)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq != status)
- ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = status;
- if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
- ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq != status)
- ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = status;
- if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
- ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
- }
- }
- uint8_t ocpp_compare_reserve_id_with_user(uint8_t gun_index)
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
- result = ON;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
- result = ON;
- }
- return result;
- }
- uint8_t ocpp_compare_reserve_id_with_remote_user(uint8_t gun_index)
- {
- uint8_t result = OFF;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
- {
- result = ON;
- memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(strcmp((char*)ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
- {
- result = ON;
- memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- }
- }
- return result;
- }
- uint8_t ocpp_isAuthorizeRemoteStart()
- {
- uint8_t result = NO;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
- {
- result = YES;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "true"))
- {
- result = YES;
- }
- }
- return result;
- }
- uint8_t ocpp_get_freevend_idtag(uint8_t * userId)
- {
- uint8_t result = NO;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- strcpy((char*)userId, (char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
- result = YES;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- strcpy((char*)userId, (char*)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value);
- result = YES;
- }
- return result;
- }
- //======================================================
- // Check interface status
- //======================================================
- int isInterfaceUp(const char *interface)
- {
- int result = FAIL;
- FILE *fp;
- char cmd[256];
- char buf[512];
- strcpy(cmd, "ifconfig");;
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, interface) > 0)
- {
- result = PASS;
- }
- }
- }
- pclose(fp);
- return result;
- }
- //======================================================
- // Create all share memory
- //======================================================
- int CreatShareMemory()
- {
- int result = PASS;
- int MeterSMId;
- //creat ShmSysConfigAndInfo
- if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
- result = FAIL;
- }
- else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
- result = FAIL;
- }
- memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
- //creat ShmStatusCodeData
- if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
- result = FAIL;
- }
- else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
- result = FAIL;
- }
- memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
- //creat ShmPsuData
- if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmPsuData NG\n");
- result = FAIL;
- }
- else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmPsuData NG\n");
- result = FAIL;
- }
- memset(ShmPsuData,0,sizeof(struct PsuData));
- //creat ShmCHAdeMOData
- if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmCHAdeMOData NG1\n");
- result = FAIL;
- }
- else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmCHAdeMOData NG2\n");
- result = FAIL;
- }
- memset(ShmCHAdeMOData,0,sizeof(struct CHAdeMOData));
- //creat ShmCcsData
- if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmCcsData NG\n");
- result = FAIL;
- }
- else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmCcsData NG\n");
- result = FAIL;
- }
- memset(ShmCcsData,0,sizeof(struct CcsData));
- //creat ShmPrimaryMcuData
- if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
- result = FAIL;
- }
- else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
- result = FAIL;
- }
- memset(ShmPrimaryMcuData,0,sizeof(struct PrimaryMcuData));
- /*
- //creat ShmFanModuleData
- if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmFanModuleData NG\n");
- result = FAIL;
- }
- else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmFanModuleData NG\n");
- result = FAIL;
- }
- memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
- //creat ShmRelayModuleData
- if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
- result = FAIL;
- }
- else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
- result = FAIL;
- }
- memset(ShmRelayModuleData,0,sizeof(struct RelayModuleData));*/
- //creat ShmOCPP16Data
- if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
- result = FAIL;
- }
- else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
- result = FAIL;
- }
- memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
- //creat ShmOCPP20Data
- if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget OCPP20Data NG\n");
- result = FAIL;
- }
- else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat OCPP20Data NG\n");
- result = FAIL;
- }
- memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
- //creat ShmCharger
- if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmCharger NG\n");
- result = FAIL;
- }
- else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmCharger NG\n");
- result = FAIL;
- }
- memset(ShmCharger,0,sizeof(struct Charger));
- //creat ShmOCPP16DataPH
- if ((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT|0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmOCPP16DataPH NG\n");
- result = FAIL;
- }
- else if ((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmOCPP16DataPH NG\n");
- result = FAIL;
- }
- else
- {}
- return result;
- }
- //===============================================
- // SQLite3 related routine
- //===============================================
- int DB_Open(sqlite3 *db)
- {
- int result = PASS;
- char* errMsg = NULL;
- char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
- "idx integer primary key AUTOINCREMENT, "
- "reservationId text, "
- "transactionId text, "
- "startMethod text, "
- "userId text, "
- "dateTimeStart text, "
- "dateTimeStop text,"
- "socStart text, "
- "socStop text, "
- "chargeEnergy text, "
- "stopReason text"
- ");";
- char* createRecordBufSql="CREATE TABLE IF NOT EXISTS charging_record_buffer("
- "reservationId text, "
- "transactionId text, "
- "startMethod text, "
- "userId text, "
- "dateTimeStart text, "
- "dateTimeStop text,"
- "socStart text, "
- "socStop text, "
- "chargeEnergy text, "
- "stopReason text"
- ");";
- char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
- "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
- "`item` TEXT NOT NULL, "
- "`connector` INTEGER NOT NULL, "
- "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
- //sqlite3_config(SQLITE_CONFIG_URI, 1);
- if(sqlite3_open(DB_FILE, &db))
- {
- result = FAIL;
- DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
- sqlite3_close(db);
- }
- else
- {
- DEBUG_INFO( "Local charging record database open successfully.\n");
- if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
- {
- result = FAIL;
- DEBUG_ERROR( "Create local charging record table error message: %s\n", errMsg);
- }
- else
- {
- DEBUG_INFO( "Opened local charging record table successfully\n");
- }
- if (sqlite3_exec(db, createRecordBufSql, 0, 0, &errMsg) != SQLITE_OK)
- {
- result = FAIL;
- DEBUG_ERROR( "Create local charging record buffer table error message: %s\n", errMsg);
- }
- else
- {
- DEBUG_INFO( "Opened local charging record buffer table successfully\n");
- }
- if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
- {
- result = FAIL;
- DEBUG_ERROR( "Create local config table error message: %s\n", errMsg);
- }
- else
- {
- DEBUG_INFO( "Opened local config table successfully\n");
- }
- sqlite3_close(db);
- }
- return result;
- }
- int DB_Open_OCPP20(sqlite3 *db)
- {
- int result = PASS;
- char* errMsg = NULL;
- char *sqlOcppVariable = "create table if not exists ocpp20_variable (idx integer primary key,"
- "componentName text NOT NULL ,"
- "componentInstance text, "
- "variableName text NOT NULL, "
- "variableInstance text, "
- "variableCharacteristicsDataType integer, "
- "variableCharacteristicsUnit text, "
- "variableCharacteristicsMaxLimit real,"
- "variableAttributesType text, "
- "variableAttributesTypeMutability text, "
- "variableAttributesTypeValue text, "
- "variableCharacteristicsMinLimit real,"
- "unique(componentName, componentInstance,variableName, variableInstance) on conflict replace);";
- //sqlite3_config(SQLITE_CONFIG_URI, 1);
- if(sqlite3_open(DB_FILE_OCPP_LOCAL, &db))
- {
- result = FAIL;
- DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
- sqlite3_close(db);
- }
- else
- {
- DEBUG_INFO( "OCPP local database open successfully.\n");
- if(sqlite3_exec(db, sqlOcppVariable, 0, 0, &errMsg) != SQLITE_OK)
- {
- result = FAIL;
- DEBUG_ERROR( "Create OCPP 2.0 variable table error message: %s\n", errMsg);
- }
- else
- {
- DEBUG_INFO( "Create OCPP 2.0 variable table successfully\n");
- }
- sqlite3_close(db);
- }
- return result;
- }
- int DB_variableClear(sqlite3 *db)
- {
- int result = PASS;
- //char * sqlcleanLocalList = "delete from ocpp20_variable";
- char * sqlcleanLocalList = "drop table ocpp20_variable";
- char *errMsg = 0;
- if(sqlite3_open(DB_FILE_OCPP_LOCAL, &db))
- {
- result = FAIL;
- DEBUG_ERROR( "Can't open database for ocpp 2.0 variable: %s\n", sqlite3_errmsg(db));
- sqlite3_close(db);
- }
- else
- {
- DEBUG_INFO("Command: &s\n", sqlcleanLocalList);
- if(sqlite3_exec(db, sqlcleanLocalList, 0, 0, &errMsg) != SQLITE_OK)
- {
- DEBUG_INFO("%s\n", errMsg);
- DEBUG_INFO("Drop table failed.\n");
- result = FAIL;
- }
-
- DEBUG_INFO("Drop table succeed.\n");
- }
- return result;
- }
- int DB_Insert_Record(sqlite3 *db, int gun_index)
- {
- int result = PASS;
- char* errMsg = NULL;
- char sqlStr[1024];
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
- "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
- ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
- ShmOCPP16Data->StopTransaction[gun_index].StopReason);
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
- "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
- ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
- ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
- }
- if(sqlite3_open(DB_FILE, &db))
- {
- result = FAIL;
- DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
- sqlite3_close(db);
- }
- else
- {
- DEBUG_INFO( "Local charging record database open successfully.\n");
- // Delete buffer
- if(sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
- {
- result = FAIL;
- DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
- }
- else
- {
- DEBUG_INFO( "Delete local charging record buffer successfully\n");
- }
- // Insert charging record
- if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
- {
- result = FAIL;
- DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
- }
- else
- {
- DEBUG_INFO( "Insert local charging record successfully\n");
- }
- sprintf(sqlStr, "delete from charging_record where (idx < (select idx from charging_record order by idx desc limit 1)-2000) and (dateTimeStop < '%04d.01.01 00:00:00');", (getCurrentYear()-3));
- if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
- {
- result = FAIL;
- DEBUG_INFO( "delete local charging error message: %s\n", errMsg);
- }
- else
- {
- DEBUG_INFO( "delete local charging record successfully\n");
- }
- sqlite3_close(db);
- }
- return result;
- }
- int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
- {
- uint8_t result = false;
- char* errMsg = NULL;
- char sqlStr[1024];
- srand(time(NULL));
- if(sqlite3_open(DB_FILE, &db))
- {
- result = FAIL;
- DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
- sqlite3_close(db);
- }
- else
- {
- DEBUG_INFO( "Local charging record database open successfully.\n");
- sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);", gun_index, isOperactive);
- DEBUG_INFO("sqlStr= %s\n", sqlStr);
- if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
- {
- result = FAIL;
- DEBUG_INFO( "update config error message: %s\n", errMsg);
- }
- else
- {
- DEBUG_INFO("update connector-%d config item isOperactive to %d\n", gun_index, isOperactive);
- }
- sqlite3_close(db);
- }
- return result;
- }
- int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
- {
- uint8_t result = true;
- char* errMsg = NULL;
- char sqlStr[1024];
- char **rs;
- int rows, cols;
- sprintf(sqlStr, "select * from config where item='isOperactive' and connector=%d;", gun_index);
- //DEBUG_INFO("sqlStr= %s\n", sqlStr);
- if(sqlite3_open(DB_FILE, &db))
- {
- result = FAIL;
- DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
- sqlite3_close(db);
- }
- else
- {
- DEBUG_INFO( "Local config query database open successfully.\n");
- sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
- if(rows>0)
- {
- for(int idxRow=1;idxRow<=rows;idxRow++)
- {
- if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
- {
- result = false;
- }
- DEBUG_INFO("Query connector-%d isOperactive: %s\n", gun_index, rs[(idxRow*cols)+3]);
- }
- }
- else
- {
- DEBUG_INFO("Query connector-%d fail, default value as operactive.\n", gun_index);
- }
- sqlite3_free_table(rs);
- sqlite3_close(db);
- }
- return result;
- }
- //======================================================
- // Peripheral initial
- //======================================================
- int InitWatchDog()
- {
- int fd;
- int timeout = 180;
- system("/usr/bin/fuser -k /dev/watchdog");
- sleep(1);
- system("echo V > /dev/watchdog");
- sleep(1);
- fd=open("/dev/watchdog", O_RDWR);
- if(fd<=0)
- {
- DEBUG_ERROR("System watch dog initial fail.\n");
- }
- ioctl(fd, _IOWR('W', 6, int), &timeout);
- return fd;
- }
- void InitGPIO()
- {
- /*****************0~3, 4 bank, bank x 32+ num*********************/
- /***************************************************************/
- /*************** INPUT PIN ***************************************/
- /***************************************************************/
- /***************************************************************/
- /*************** OUTPUT PIN ************************************/
- /***************************************************************/
- /*MCU request:GPIO3_20 => H:ON; L:OFF*/
- system("echo 116 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio116/direction");
- system("echo 0 > /sys/class/gpio/gpio116/value");
- /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
- system("echo 19 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio19/direction");
- system("echo 1 > /sys/class/gpio/gpio19/value");
- /*Speaker:GPIO2_1 => H:ON; L:OFF*/
- system("echo 65 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio65/direction");
- system("echo 0 > /sys/class/gpio/gpio65/value");
- /*(FOR ACCSU-01-RW) 4G POWER RST:GPIO3_14 => H:ON; L:OFF*/
- system("echo 110 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio110/direction");
- system("echo 1 > /sys/class/gpio/gpio110/value");
- sleep(3);
- system("echo 0 > /sys/class/gpio/gpio110/value");
- /*(FOR ACCSU3P5-01-RW) 4G POWER RST:GPIO3_18 => H:ON; L:OFF*/
- system("echo 114 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio114/direction");
- system("echo 1 > /sys/class/gpio/gpio114/value");
- sleep(3);
- system("echo 0 > /sys/class/gpio/gpio114/value");
- DEBUG_INFO("Initial GPIO OK\n");
- }
- int LoadSysConfigAndInfo(struct SysConfigData *ptr)
- {
- int fd,wrd;
- unsigned char *buf;
- unsigned int ChkSum,ChkSumOrg;
- if((buf=malloc(MtdBlockSize))==NULL)
- {
- DEBUG_ERROR("malloc buffer NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, MtdBlockSize);
- //================================================
- // Load configuration from mtdblock10
- //================================================
- system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
- fd = open("/mnt/EvseConfig.bin", O_RDWR);
- if (fd < 0)
- {
- free(buf);
- DEBUG_ERROR("open mtdblock10 NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- wrd=read(fd, buf, MtdBlockSize);
- close(fd);
- if(wrd<MtdBlockSize)
- {
- free(buf);
- DEBUG_ERROR("read SysConfigData data NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- ChkSum=0;
- for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
- {
- ChkSum+=buf[wrd];
- }
- memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
- memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
- memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
- //================================================
- // Load configuration from mtdblock11
- //================================================
- if(ChkSum!=ChkSumOrg)
- {
- DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
- system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
- fd = open("/mnt/EvseConfig.bin", O_RDWR);
- if (fd < 0)
- {
- free(buf);
- DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, MtdBlockSize);
- wrd=read(fd, buf,MtdBlockSize);
- close(fd);
- if(wrd<MtdBlockSize)
- {
- free(buf);
- DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- ChkSum=0;
- for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
- {
- ChkSum+=buf[wrd];
- }
- memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
- //================================================
- // Load configuration from mtdblock12 (Factory default)
- //================================================
- if(ChkSum!=ChkSumOrg)
- {
- DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
- system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
- fd = open("/mnt/EvseConfig.bin", O_RDWR);
- if (fd < 0)
- {
- DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
-
- free(buf);
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, MtdBlockSize);
- wrd=read(fd, buf,MtdBlockSize);
- close(fd);
- if(wrd<MtdBlockSize)
- {
- DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
-
- free(buf);
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- ChkSum=0;
- for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
- {
- ChkSum+=buf[wrd];
- }
- memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
- memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
- memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
- if(ChkSum!=ChkSumOrg)
- {
- DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
- free(buf);
- system("cd /root;./Module_FactoryConfig -m");
- system("rm -f /Storage/OCPP/OCPPConfiguration");
- system("sync");
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- return FAIL;
- }
- }
- }
- //load OK
- memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
- free(buf);
- system("rm -f /mnt/EvseConfig.bin");
- // SysConfig in flash is empty (0xffffffff)
- if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
- (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
- (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
- (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
- {
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
- {
- memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
- {
- memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
- {
- memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
- }
- if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
- {
- DEBUG_INFO("Ethernet dhcp config is null.\n");
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
- {
- DEBUG_INFO("Model name over length.\n");
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
- {
- DEBUG_INFO("Model serial number over length.\n");
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
- {
- DEBUG_INFO("SystemId over length.\n");
- }
- system("cd /root;./Module_FactoryConfig -m");
- sleep(3);
- system("/usr/bin/run_evse_restart.sh");
- }
- DEBUG_INFO("Load SysConfigData OK\n");
-
- RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo);
- return PASS;
- }
- int StoreUsrConfigData(struct SysConfigData *UsrData)
- {
- int result = PASS;
- int fd,wrd;
- unsigned int i,Chk;
- unsigned char *ptr, *BufTmp;
- Chk=0;
- ptr=(unsigned char *)UsrData;
- if((BufTmp=malloc(MtdBlockSize))!=NULL)
- {
- memset(BufTmp,0,MtdBlockSize);
- memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
- for(i=0;i<MtdBlockSize-4;i++)
- Chk+=*(BufTmp+i);
- memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
- // Output configuration to file.
- fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
- if (fd < 0)
- {
- DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
- free(BufTmp);
- return 0;
- }
- wrd=write(fd, BufTmp, MtdBlockSize);
- close(fd);
- if(wrd<MtdBlockSize)
- {
- DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
- free(BufTmp);
- return 0;
- }
- DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
- DEBUG_INFO("Erase /dev/mtd10.\n");
- runShellCmd("flash_erase /dev/mtd10 0 0");
- DEBUG_INFO("Write /dev/mtd10.\n");
- runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
- DEBUG_INFO("Erase /dev/mtd11.\n");
- runShellCmd("flash_erase /dev/mtd11 0 0");
- DEBUG_INFO("Write /dev/mtd11.\n");
- runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
- system("rm -f /mnt/EvseConfig.bin");
- DEBUG_INFO("EvseConfig write to flash OK\n");
- }
- else
- {
- DEBUG_ERROR("alloc BlockSize NG\r\n");
- result = FAIL;
- }
- if(BufTmp!=NULL)
- free(BufTmp);
- return result;
- }
- void InitEthernet()
- {
- pid_t pid;
- uint8_t cnt_pingDNS_Fail;
- char tmpbuf[256];
- unsigned int natInterface = 0;
- // Detele bridge interface
- system("/sbin/ifconfig uap0 down");
- system("/sbin/ifconfig br0 down");
- system("/usr/sbin/brctl delbr br0");
- //Init Eth0 for internet
- memset(tmpbuf,0,256);
- sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
- DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
- system(tmpbuf);
- memset(tmpbuf,0,256);
- sprintf(tmpbuf,"route add default gw %s eth0 &",
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
- system(tmpbuf);
- system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
- system("ifconfig lo up &");
- system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
- if(isInterfaceUp("eth1")==PASS)
- {
- //Init Eth1 for administrator tool
- memset(tmpbuf,0,256);
- sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up &",
- ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
- ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
- system(tmpbuf);
- }
- //Run DHCP client if enabled
- system("killall udhcpc");
- system("rm -rf /etc/resolv.conf");
- system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
- system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
- if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
- {
- sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
- system(tmpbuf);
- }
- // Upgrade system id to /etc/hostname
- sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
- system(tmpbuf);
- // Ethernet MAC address
- getEth0MacAddress();
- //check internet status
- pid = fork();
- if(pid == 0)
- {
- for(;;)
- {
- if(isRouteFail())
- {
- //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
- system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
- if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
- {
- system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
- sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
- system(tmpbuf);
- }
- else
- {
- system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
- memset(tmpbuf,0,256);
- sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
- system(tmpbuf);
- memset(tmpbuf,0,256);
- sprintf(tmpbuf,"route add default gw %s eth0 &",
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
- system(tmpbuf);
- }
- }
- if(isReachableInternet() == PASS)
- {
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
- cnt_pingDNS_Fail = 0;
- }
- else
- {
- if(cnt_pingDNS_Fail >= 3)
- {
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
- if(!ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
- {
- if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
- {
- system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
- sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
- system(tmpbuf);
- }
- else
- {
- system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
- memset(tmpbuf,0,256);
- sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
- system(tmpbuf);
- memset(tmpbuf,0,256);
- sprintf(tmpbuf,"route add default gw %s eth0 &",
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
- system(tmpbuf);
- }
- cnt_pingDNS_Fail = 0;
- }
- }
- else
- {
- cnt_pingDNS_Fail++;
- }
- }
- if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
- ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
- ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) &&
- (ShmOCPP16Data->OcppConnStatus != PASS))
- {
- ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
- }
- else
- {
- ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
- }
- //============================================================
- // Priority for internet 0 : First / 1 : Second / 2: Third
- //============================================================
- if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
- {
- system("/sbin/ifmetric eth0 0");
- if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- system("/sbin/ifmetric mlan0 1");
- }
- if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- system("/sbin/ifmetric ppp0 2");
- }
- if(isKernelSupportNAT() == YES)
- {
- if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
- {
- if(natInterface != 1)
- {
- system("/sbin/iptables -t nat -F");
- system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o eth0 -j MASQUERADE");
- natInterface = 1;
- }
- }
- else
- {
- system("/sbin/iptables -t nat -F");
- }
- }
- }
- else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
- {
- if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- system("/sbin/ifmetric eth0 1");
- system("/sbin/ifmetric mlan0 0");
- }
- if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- system("/sbin/ifmetric ppp0 2");
- }
- }
- else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
- {
- if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- system("/sbin/ifmetric mlan0 2");
- }
- if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- system("/sbin/ifmetric eth0 1");
- system("/sbin/ifmetric ppp0 0");
- }
- if(isKernelSupportNAT() == YES)
- {
- if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
- {
- if(natInterface != 2)
- {
- system("/sbin/iptables -t nat -F");
- system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o ppp0 -j MASQUERADE");
- natInterface = 2;
- }
- }
- else
- {
- system("/sbin/iptables -t nat -F");
- }
- }
- }
- else
- {
- if(isKernelSupportNAT() == YES)
- {
- if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
- {
- system("/sbin/iptables -t nat -F");
- natInterface = 0;
- }
- }
- }
- // Check is need to reset WIFI/4G module power
- if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus) &&
- ((system("pidof -s Module_Wifi > /dev/null") != 0) ||
- (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) ||
- ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 1) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == ON)) ||
- ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi == ON))) &&
- ((system("pidof -s Module_4g > /dev/null") != 0) ||
- (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) ||
- ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 1) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == ON))))
- {
- if(DiffTimebWithNow(startTime[0][TMR_IDX_RESET_4G_WIFI_POWER]) > TIMEOUT_SPEC_RESET_4G_WIFI_POWER)
- {
- if((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != 0) || (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled != 0))
- {
- DEBUG_WARN("WIFI/4G module reset.\n");
- // FOR ACCSU-01-RW RESET 4G
- system("echo 1 > /sys/class/gpio/gpio110/value");
- // FOR ACCSU3P5-01-RW RESET 4G
- system("echo 1 > /sys/class/gpio/gpio114/value");
- sleep(3);
- // FOR ACCSU-01-RW RESET 4G
- system("echo 0 > /sys/class/gpio/gpio110/value");
- // FOR ACCSU3P5-01-RW RESET 4G
- system("echo 0 > /sys/class/gpio/gpio114/value");
- }
- ftime(&startTime[0][TMR_IDX_RESET_4G_WIFI_POWER]);
- }
- }
- else
- {
- ftime(&startTime[0][TMR_IDX_RESET_4G_WIFI_POWER]);
- }
- // Bridge ethernet to uap0 to get dynamic ip address
- if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing &&
- (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) &&
- (isUap0up() == PASS) &&
- (access("/sys/class/net/br0/address", F_OK) == -1))
- {
- sleep(10);
- system("/usr/sbin/brctl addbr br0");
- system("/usr/sbin/brctl addif br0 eth0");
- system("/usr/sbin/brctl addif br0 uap0");
- system("/sbin/ifconfig br0 192.168.10.200 up");
- system("/bin/sed -i '/interface/d' /etc/udhcpd.conf");
- system("/bin/echo 'interface br0' >> /etc/udhcpd.conf");
- system("kill udhcpd");
- system("/usr/sbin/udhcpd /etc/udhcpd.conf");
- DEBUG_INFO("Bridge uap0 & eth0 for local power sharing by ethernet.\n");
- }
- // Change uap0 to low priority
- if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing &&
- (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) &&
- (isUap0up() == PASS))
- {
- system("/sbin/ifmetric uap0 1");
- }
- sleep(5);
- }
- }
- DEBUG_INFO("Initial Ethernet OK\n");
- }
- int SpawnTask()
- {
- system ("pkill Module_");
- system ("pkill OcppBackend");
- if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- system("/root/Module_4g &");
- }
- else if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- system("/root/Module_Wifi &");
- }
- system ("/root/Module_EventLogging &");
- system ("/root/Module_AlarmDetect &");
- system ("/root/Module_InternalComm &");
- system ("/root/Module_Speaker &");
- system ("/root/Module_ProduceUtils &");
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
- {
- ocpp_process_start();
- }
- else
- {
- DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
- ocpp_process_start();
- }
- return PASS;
- }
- int Initialization()
- {
- int result = PASS;
- LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
- InitGPIO();
- InitEthernet();
- if(DB_Open(localDb) != PASS)
- {
- DEBUG_ERROR("Local database initial fail.\n");
- result = FAIL;
- }
- if(DB_Open_OCPP20(ocppDb) != PASS)
- {
- DEBUG_ERROR("OCPP 2.0 configuration variable database initial fail.\n");
- result = FAIL;
- }
- for(int gun_index=0;gun_index< AC_QUANTITY;gun_index++)
- ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
- if((rfidFd = InitRfidPort()) == FAIL)
- {
- DEBUG_ERROR("RFID port initial fail.\n");
- result = FAIL;
- }
- if((wtdFd = InitWatchDog()) == FAIL)
- {
- DEBUG_ERROR("Watchdog initial fail.\n");
- result = FAIL;
- }
- if(result == PASS)
- DEBUG_INFO("Initialization OK.\n");
- else
- DEBUG_INFO("Initialization Fail.\n");
- return result;
- }
- //=====================================================
- // Common routine
- //=====================================================
- char* getSystemModeName(unsigned char mode)
- {
- char* result;
- switch(mode)
- {
- case SYS_MODE_BOOTING:
- result = "booting";
- break;
- case SYS_MODE_IDLE:
- result = "idle";
- break;
- case SYS_MODE_AUTHORIZING:
- result = "authorizing";
- break;
- case SYS_MODE_PREPARING:
- result = "preparing";
- break;
- case SYS_MODE_CHARGING:
- result = "charging";
- break;
- case SYS_MODE_TERMINATING:
- result = "terminating";
- break;
- case SYS_MODE_COMPLETE:
- result = "complete";
- break;
- case SYS_MODE_ALARM:
- result = "alarm";
- break;
- case SYS_MODE_FAULT:
- result = "fault";
- break;
- case SYS_MODE_MAINTAIN:
- result = "maintain";
- break;
- case SYS_MODE_RESERVATION:
- result = "reservation";
- break;
- case SYS_MODE_BOOKING:
- result = "booking";
- break;
- case SYS_MODE_DEBUG:
- result = "debug";
- break;
- case SYS_MODE_UPDATE:
- result = "upgrade";
- break;
- default:
- result = "unknown";
- break;
- }
- return result;
- }
- void setChargerMode(unsigned char gun_index, unsigned char mode)
- {
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
- DEBUG_INFO("Gun-%02d mode switch from %s to %s\n", gun_index, getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus), getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus));
- }
- unsigned char isMode(unsigned char gun_index, unsigned char mode)
- {
- return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
- }
- unsigned char isModeChange(unsigned char gun_index)
- {
- unsigned char result = NO;
- if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
- {
- result = YES;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
- }
- return result;
- }
- void gpio_set_value(unsigned int gpio, unsigned int value)
- {
- int fd;
- char buf[256];
- sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
- fd = open(buf, O_WRONLY);
- if (fd < 0)
- {
- DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
- return;
- }
- if (value)
- write(fd, "1", 2);
- else
- write(fd, "0", 2);
- close(fd);
- }
- int gpio_get_value(unsigned int gpio)
- {
- int fd;
- char buf[256];
- char ch;
- int8_t result = FAIL;
- sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
- fd = open(buf, O_RDONLY);
- if (fd < 0)
- {
- DEBUG_ERROR("GPIO-%d get fail\n", gpio);
- return result;
- }
- read(fd, &ch, 1);
- if (ch != '0')
- result = 1;
- else
- result = 0;
- close(fd);
- return result;
- }
- //===============================================
- // Get firmware version
- //===============================================
- void get_firmware_version(unsigned char gun_index)
- {
- FILE *fp;
- char cmd[512];
- char buf[512];
- // Get CSU hardware version
- sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
- // Get CSU boot loader version
- memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
- // Get CSU kernel version
- sprintf(cmd, "/bin/uname -r");
- fp = popen(cmd, "r");
- if(fp == NULL)
- sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
- else
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
- }
- }
- // Get MCU firmware version
- strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
- // Get CSU root file system version
- sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "V0.72.00.0000.00");
- // Get AC connector type from model name
- for(uint8_t idx=0;idx<3;idx++)
- {
- switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
- {
- case '1':
- // J1772 Plug
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
- break;
- case '2':
- // J1772 Socket
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
- break;
- case '3':
- // CE Plug
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
- break;
- case '4':
- // CE Socket
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
- break;
- case '5':
- // GB Plug
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
- break;
- case '6':
- // GB Socket
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
- break;
- }
- }
- // Get network option from model name
- switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
- {
- case 'B':
- case 'U':
- //Blue tooth
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
- break;
- case 'W':
- // WIFI
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
- break;
- case 'T':
- // 3G/4G
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
- break;
- case 'D':
- // WIFI + 4G
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
- break;
- default:
- // LAN
- ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
- ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
- break;
- }
- // Get rating power from model name
- memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
- memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
- // Get vender code from model name
- memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
- memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
- DEBUG_INFO("========================================\n");
- DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
- DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
- DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
- DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
- DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
- DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
- DEBUG_INFO("========================================\n");
- }
- //===============================================
- // Upgrade firmware
- //===============================================
- int upgrade_check()
- {
- int result = PASS;
- int fd;
- DIR *dir;
- struct dirent *file;
- char cmd[512];
- long int MaxLen=48*1024*1024;
- if ((dir = opendir ("/mnt")) != NULL)
- {
- /* print all the files and directories within directory */
- while ((file = readdir (dir)) != NULL)
- {
- if(strlen(file->d_name)>2)
- {
- memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
- DEBUG_INFO("New firmware file: %s\n", file->d_name);
- sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
- if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
- {
- unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
- memset(ptr, 0xFF, MaxLen); //-48 is take out the header
- read(fd, ptr, MaxLen);
- close(fd);
- ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
- substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
- free(ptr);
- DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
- DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
- if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
- {
- switch(ShmCharger->fwUpgradeInfo.fwType)
- {
- case CSU_BOOTLOADER:
- case CSU_KERNEL_CONFIGURATION:
- case CSU_KERNEL_IMAGE:
- case CSU_ROOT_FILE_SYSTEM:
- case CSU_USER_CONFIGURATION:
- case CSU_PRIMARY_CONTROLLER:
- if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
- {
- result = FAIL;
- }
- else
- {
- if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
- {
- DEBUG_INFO("Restore model name & serial number.\n");
- memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
- if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
- {
- DEBUG_INFO("Re-load configuration fail.\n");
- result = FAIL;
- }
- else
- {
- memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
- memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
- memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
- if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
- {
- DEBUG_INFO("Re-write configuration fail.\n");
- result = FAIL;
- }
- else
- DEBUG_INFO("Re-write configuration OK.\n");
- }
- }
- }
- sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
- system(cmd);
- break;
- case AC_WALLMOUNT_CONTROLLER:
- for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
- ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
- sleep(10);
- break;
- default:
- result = FAIL;
- DEBUG_WARN("Image file is unknown type.\n");
- sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
- system(cmd);
- break;
- }
- }
- else
- {
- result = FAIL;
- DEBUG_ERROR("Model name and Firmware type error.\n");
- }
- }
- else
- {
- result = FAIL;
- DEBUG_ERROR("New firmware open error.\n");
- }
- }
- else
- {
- if(strlen(file->d_name) >= 3)
- {
- result = FAIL;
- DEBUG_ERROR("File name error.\n");
- }
- else
- {
- DEBUG_ERROR("Searching file.\n");
- }
- }
- }
- closedir (dir);
- }
- else
- {
- result = FAIL;
- DEBUG_ERROR("/mnt does not valid.\n");
- }
- return result;
- }
- //===============================================
- // Check RFID is match with start user
- //===============================================
- int isMatchStartUser(unsigned char gun_index)
- {
- uint8_t tmpUser[32];
- if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
- {
- // Big endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
- break;
- }
- }
- else
- {
- // Little endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- }
- }
- return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
- }
- //===============================================
- // Read RFID Serial Number
- //===============================================
- int GetCardSerialNumber()
- {
- int isSuccess = FAIL;
- int module_type = MODULE_EWT;
- if(getRequestCardSN(rfidFd,module_type,&rfid))
- {
- if(rfid.cardType == ISO14443A)
- {
- if(rfid.snType == RFID_SN_TYPE_4BYTE)
- {
- isSuccess = PASS;
- }
- else if (rfid.snType == RFID_SN_TYPE_7BYTE)
- {
- isSuccess = PASS;
- }
- sethaltCard(rfidFd,module_type);
- }
- else if(rfid.cardType == IS014443B)
- {
- isSuccess = PASS;
- }
- else if(rfid.cardType == FELICA)
- {
- isSuccess = PASS;
- }
- else
- {}
- }
- else
- {}
- return isSuccess;
- }
- //===============================================
- // Set led motion
- //===============================================
- void setLedMotion(unsigned char gun_index,unsigned char led_mode)
- {
- switch(led_mode)
- {
- case LED_ACTION_INIT:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
- break;
- case LED_ACTION_IDLE:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
- break;
- case LED_ACTION_AUTHED:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
- break;
- case LED_ACTION_CONNECTED:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
- break;
- case LED_ACTION_CHARGING:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
- break;
- case LED_ACTION_STOP:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
- break;
- case LED_ACTION_ALARM:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
- break;
- case LED_ACTION_MAINTAIN:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
- break;
- case LED_ACTION_RFID_PASS:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
- break;
- case LED_ACTION_RFID_FAIL:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
- break;
- case LED_ACTION_BLE_CONNECT:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
- break;
- case LED_ACTION_BLE_DISABLE:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
- break;
- case LED_ACTION_DEBUG:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
- break;
- case LED_ACTION_ALL_OFF:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
- break;
- case LED_RELAY_ON:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
- break;
- case LED_RELAY_OFF:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
- break;
- case LED_ACTION_HANDSHAKE_FAIL:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
- break;
- case LED_ACTION_INTERNET_DISCONNECT:
- ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
- break;
- }
- }
- //===============================================
- // Request on/off set (PWM)
- //===============================================
- void setRequest(unsigned char gun_index,unsigned char isOn)
- {
- if(isOn == ON)
- {
- if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
- {
- ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
- DEBUG_INFO("Gun-%d permission request: ON \n",gun_index);
- }
- }
- else
- {
- if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
- {
- ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
- DEBUG_INFO("Gun-%d permission request: OFF \n",gun_index);
- }
- }
- }
- //===============================================
- // Request on/off get (PWM)
- //===============================================
- int getRequest(unsigned char gun_index)
- {
- return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
- }
- //===============================================
- // Relay on/off set (Relay)
- //===============================================
- void setRelay(unsigned char gun_index,unsigned char isOn)
- {
- if(isOn == ON)
- {
- if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
- {
- ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
- ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
- ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
- ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
- DEBUG_INFO("Gun-%d Output relay status: ON. \n",gun_index);
- }
- }
- else
- {
- if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
- {
- ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
- ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
- ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
- ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
- DEBUG_INFO("Gun-%d Output relay status: OFF. \n",gun_index);
- }
- }
- }
- //===============================================
- // Relay on/off get (Relay)
- //===============================================
- int getRelay(unsigned char gun_index)
- {
- return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
- }
- //===============================================
- // Set speaker on/off request
- //===============================================
- void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
- {
- if(isOn == ON)
- {
- ShmCharger->isSpeakerOn = ON;
- ShmCharger->speaker_type = speaker_mode;
- }
- }
- //===============================================
- // Initialization RFID communication port
- //===============================================
- int InitRfidPort()
- {
- int uartO2 = open(rfidPortName, O_RDWR);
- struct termios tios;
- if (uartO2 != FAIL)
- {
- ioctl (uartO2, TCGETS, &tios);
- tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
- tios.c_lflag = 0;
- tios.c_iflag = 0;
- tios.c_oflag = 0;
- tios.c_cc[VMIN] = 0;
- tios.c_cc[VTIME] = (unsigned char) 1;
- tios.c_lflag = 0;
- tcflush(uartO2, TCIFLUSH);
- ioctl(uartO2, TCSETS, &tios);
- }
- if (uartO2 < 0)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
- }
- return uartO2;
- }
- //===============================================
- // Check internet access status
- //===============================================
- int isReachableInternet()
- {
- int result = FAIL;
- FILE *fp;
- char cmd[256];
- char buf[512];
- char tmp[512];
-
- // Get ip address & net mask
- strcpy(cmd, "ifconfig eth0");
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if (strstr(buf, "inet addr:") > 0)
- {
- sscanf(buf, "%*s%s", tmp);
- substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
- sscanf(buf, "%*s%*s%*s%s", tmp);
- substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp, strspn(tmp, "Mask:"), strlen(tmp)-strspn(tmp, "Mask:"));
- }
- }
- }
- pclose(fp);
- memset(buf, 0x00, sizeof(buf));
- // Get gateway
- fp = popen("ip route", "r");
- if(fp == NULL)
- result = FAIL;
- else
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
- break;
- }
- if(strstr(buf, "default") != NULL)
- {
- sscanf(buf, "%*s%*s%s", tmp);
- substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
- }
- }
- pclose(fp);
- memset(buf, 0x00, sizeof(buf));
- for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
- {
- sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "transmitted") > 0)
- {
- //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
- if(strstr(buf,"100%") != NULL)
- {
- }
- else
- {
- result = PASS;
- }
- //DEBUG_INFO("%s",buf);
- //DEBUG_INFO("%s\n",tmp);
- }
- }
- }
- pclose(fp);
- }
- return result;
- }
- //===============================================
- // Check route for eth0
- //===============================================
- int isRouteFail()
- {
- int result = YES;
- FILE *fp;
- char buf[512];
- fp = popen("route -n", "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "eth0") != NULL)
- result = NO;
- }
- }
- pclose(fp);
- return result;
- }
- //===============================================
- // Check reservation date is expired
- //===============================================
- int isReservationExpired(unsigned char gun_index)
- {
- int result = NO;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
- result = YES;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(isOvertNow(ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime))
- result = YES;
- }
- return result;
- }
- //===============================================
- // Check charging profile related date routine
- //===============================================
- int isProfileValid(uint8_t gun_index)
- {
- int result = NO;
- struct tm tmFrom, tmTo;
- struct timeb tbFrom, tbTo;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
- (sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
- {
- tmFrom.tm_year -= 1900;
- tmFrom.tm_mon -= 1;
- tbFrom.time = mktime(&tmFrom);
- tmTo.tm_year -= 1900;
- tmTo.tm_mon -= 1;
- tbTo.time = mktime(&tmTo);
- DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
- DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
- if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
- {
- result = YES;
- }
- }
- else
- {
- DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if((sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
- (sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
- {
- tmFrom.tm_year -= 1900;
- tmFrom.tm_mon -= 1;
- tbFrom.time = mktime(&tmFrom);
- tmTo.tm_year -= 1900;
- tmTo.tm_mon -= 1;
- tbTo.time = mktime(&tmTo);
- DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
- DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
- if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
- {
- result = YES;
- }
- }
- else
- {
- DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
- }
- }
- return result;
- }
- int getMaxScheduleStart()
- {
- int result = -1;
- struct tm tmScheduleStart;;
- struct timeb tbScheduleStart;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if((sscanf((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule, "%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))
- {
- tmScheduleStart.tm_year -= 1900;
- tmScheduleStart.tm_mon -= 1;
- tbScheduleStart.time = mktime(&tmScheduleStart);
- tbScheduleStart.millitm = 0;
- result = DiffTimebWithNow(tbScheduleStart)/1000;
- //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
- }
- else
- {
- DEBUG_WARN("Max schedule start date parsing error.\n");
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if((sscanf((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule, "%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))
- {
- tmScheduleStart.tm_year -= 1900;
- tmScheduleStart.tm_mon -= 1;
- tbScheduleStart.time = mktime(&tmScheduleStart);
- tbScheduleStart.millitm = 0;
- result = DiffTimebWithNow(tbScheduleStart)/1000;
- //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
- }
- else
- {
- DEBUG_WARN("Max schedule start date parsing error.\n");
- }
- }
- return result;
- }
- int getScheduleStart(int gun_index)
- {
- int result = -1;
- struct tm tmScheduleStart;;
- struct timeb tbScheduleStart;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule, "%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))
- {
- tmScheduleStart.tm_year -= 1900;
- tmScheduleStart.tm_mon -= 1;
- tbScheduleStart.time = mktime(&tmScheduleStart);
- tbScheduleStart.millitm = 0;
- result = DiffTimebWithNow(tbScheduleStart)/1000;
- //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
- }
- else
- {
- DEBUG_WARN("Schedule start date parsing error.\n");
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if((sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule, "%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))
- {
- tmScheduleStart.tm_year -= 1900;
- tmScheduleStart.tm_mon -= 1;
- tbScheduleStart.time = mktime(&tmScheduleStart);
- tbScheduleStart.millitm = 0;
- result = DiffTimebWithNow(tbScheduleStart)/1000;
- //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
- }
- else
- {
- DEBUG_WARN("Schedule start date parsing error.\n");
- }
- }
- return result;
- }
- int getStartSinceToday()
- {
- int result = -1;
- time_t t;
- struct tm *tmStartToday;
- struct timeb tbStartToday;
- t=time(NULL);
- tmStartToday=localtime(&t);
- tmStartToday->tm_hour = 0;
- tmStartToday->tm_min = 0;
- tmStartToday->tm_sec = 0;
- tbStartToday.time = mktime(tmStartToday);
- result = DiffTimebWithNow(tbStartToday)/1000;
- //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
- return result;
- }
- int getStartSinceWeek()
- {
- int result = -1;
- time_t t;
- struct tm *tmStartWeek;
- struct timeb tbStartWeek;
- t=time(NULL);
- tmStartWeek=localtime(&t);
- t-=86400*tmStartWeek->tm_wday;
- tmStartWeek=localtime(&t);
- tmStartWeek->tm_hour = 0;
- tmStartWeek->tm_min = 0;
- tmStartWeek->tm_sec = 0;
- tbStartWeek.time = mktime(tmStartWeek);
- result = DiffTimebWithNow(tbStartWeek)/1000;
- //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
- return result;
- }
- //===============================================
- // Valid from local white list
- //===============================================
- int isValidLocalWhiteCard()
- {
- uint8_t result = FAIL;
- for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
- {
- if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
- {
- result = PASS;
- }
- }
- return result;
- }
- //==========================================
- // Check routine
- //==========================================
- void checkTask()
- {
- if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- if(system("pidof -s Module_4g > /dev/null") != 0)
- {
- DEBUG_INFO("Module_4g not running, restart it.\n");
- system("/root/Module_4g &");
- }
- }
- if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
- {
- if(system("pidof -s Module_Wifi > /dev/null") != 0)
- {
- DEBUG_INFO("Module_Wifi not running, restart it.\n");
- system("/root/Module_Wifi &");
- }
- }
- if(system("pidof -s Module_EventLogging > /dev/null") != 0)
- {
- DEBUG_INFO("Module_EventLogging not running, restart it.\n");
- system("/root/Module_EventLogging &");
- }
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
- {
- DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
- ShmOCPP16Data->procDogTime = time((time_t*)NULL);
- system("pkill OcppBackend");
- sleep(3);
- ocpp_process_start();
- }
- if(system("pidof -s OcppBackend > /dev/null") != 0)
- {
- DEBUG_INFO("OcppBackend not running, restart it.\n");
- ocpp_process_start();
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
- {
- DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
- ShmOCPP20Data->procDogTime = time((time_t*)NULL);
- system("pkill OcppBackend20");
- sleep(3);
- ocpp_process_start();
- }
- if(system("pidof -s OcppBackend20 > /dev/null") != 0)
- {
- DEBUG_INFO("OcppBackend20 not running, restart it.\n");
- ocpp_process_start();
- }
- }
- }
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
- {
- if(system("pidof -s OcppBackendPH > /dev/null") != 0)
- {
- DEBUG_INFO("OcppBackendPH not running, restart it.\n");
- system("/root/OcppBackendPH &");
- }
- }
- if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
- {
- DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
- system("/root/Module_AlarmDetect &");
- }
- if(system("pidof -s Module_InternalComm > /dev/null") != 0)
- {
- DEBUG_INFO("Module_InternalComm not running, restart it.\n");
- system("/root/Module_InternalComm &");
- }
- if(system("pidof -s Module_Speaker > /dev/null") != 0)
- {
- DEBUG_INFO("Module_Speaker not running, restart it.\n");
- system("/root/Module_Speaker &");
- }
- if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
- {
- DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
- system ("/root/Module_ProduceUtils &");
- }
- if((system("pidof -s Module_PowerSharing > /dev/null") != 0) &&
- ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
- {
- DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
- system ("/root/Module_PowerSharing &");
- }
- }
- void checkConnectionTimeout()
- {
- if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
- {
- ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
- //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
- }
- else
- {
- ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
- if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
- {
- ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
- //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
- }
- else
- {
- ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
- //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
- }
- //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
- }
- }
- void checkReset()
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->MsMsg.bits.ResetReq)
- {
- if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
- (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE))
- {
- ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
- sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
- ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
- DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
- for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
- setChargerMode(gun_index, SYS_MODE_BOOTING);
- if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
- {
- system("sync");
- sleep(10);
- system("reboot -f");
- sleep(10);
- system("reboot -f");
- }
- else
- {
- sleep(10);
- close(wtdFd);
- system("/usr/bin/run_evse_restart.sh");
- }
- }
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->MsMsg.bits.ResetReq)
- {
- if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
- (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE))
- {
- ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
- sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
- ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
- DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
- for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
- setChargerMode(gun_index, SYS_MODE_BOOTING);
- if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
- {
- system("sync");
- sleep(10);
- system("reboot -f");
- sleep(10);
- system("reboot -f");
- }
- else
- {
- sleep(10);
- close(wtdFd);
- system("/usr/bin/run_evse_restart.sh");
- }
- }
- }
- }
- }
- void checkReservation(uint8_t gun_index)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
- {
- ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
- ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
- if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
- {
- sleep(5);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
- setChargerMode(gun_index, SYS_MODE_RESERVATION);
- }
- DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
- {
- ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
- ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
- if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
- {
- sleep(5);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
- setChargerMode(gun_index, SYS_MODE_RESERVATION);
- }
- DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
- }
- }
- }
- void checkUnlocker(uint8_t gun_index)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
- {
- ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
- if(ShmSysConfigAndInfo->SysConfig.ModelName[9-gun_index] == '4')
- {
- sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "Unlocked");
- ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
- }
- else
- {
- sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
- ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
- }
- ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
- {
- ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
- if(ShmSysConfigAndInfo->SysConfig.ModelName[9-gun_index] == '4')
- {
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING)
- {
- sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "OngoingAuthorizedTransaction");
- ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = OFF;
- }
- else
- {
- sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "Unlocked");
- ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
- }
- }
- else
- {
- sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "UnlockFailed");
- ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = OFF;
- }
- ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
- }
- }
- }
- void checkAvailability(uint8_t gun_index)
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
- {
- if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
- {
- DB_Update_Operactive(localDb, gun_index, true);
- ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
- }
- else
- {
- DB_Update_Operactive(localDb, gun_index, false);
- ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
- }
- ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
- {
- if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
- {
- DB_Update_Operactive(localDb, gun_index, true);
- ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
- }
- else
- {
- DB_Update_Operactive(localDb, gun_index, false);
- ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
- }
- ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
- }
- }
- }
- void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
- {
- uint16_t MaxChargingProfileChargingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- //========================================
- // Check TxProfile and TxDefaultProfile
- //========================================
- if((ocpp_get_smartcharging_profileId(gun_index) > 0))
- {
- // Checking profile kind
- if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
- {
- // Checking limitation
- for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
- {
- if((getScheduleStart(gun_index) >= ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
- ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
- )
- {
- ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
- //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
- }
- else
- break;
- }
- }
- }
- else
- {
- ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
- }
- //========================================
- // Check ChargingPointMaxProfile
- //========================================
- if((ocpp_get_maxcharging_profileId() > 0))
- {
- // Absolute profile
- if((mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == PASS))
- {
- // Checking limitation
- for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);idx_period++)
- {
- if((getMaxScheduleStart() >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
- ((idx_period == 0) || (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
- )
- {
- MaxChargingProfileChargingCurrent = (mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
- //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
- }
- else
- break;
- }
- }
- }
- // Compare target current is over max current for EVSE limit
- if(gun_index == 0)
- {
- if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
- {
- ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
- }
- }
- else
- {
- if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
- {
- ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
- }
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- //========================================
- // Check TxProfile and TxDefaultProfile
- //========================================
- if((ocpp_get_smartcharging_profileId(gun_index) > 0))
- {
- // Checking profile kind
- if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
- {
- // Checking limitation
- for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
- {
- if((getScheduleStart(gun_index) >= ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
- ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
- )
- {
- ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingRateUnit,"W")==PASS?ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit/(220*ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases):ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit);
- //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
- }
- else
- break;
- }
- }
- }
- else
- {
- ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
- }
- //========================================
- // Check ChargingPointMaxProfile
- //========================================
- if((ocpp_get_maxcharging_profileId() > 0))
- {
- // Absolute profile
- if((mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, "Absolute") == PASS))
- {
- // Checking limitation
- for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);idx_period++)
- {
- if((getMaxScheduleStart() >= ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
- ((idx_period == 0) || (ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
- )
- {
- MaxChargingProfileChargingCurrent = (mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingRateUnit,"W")==PASS?ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit/(220*ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases):ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit);
- //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
- }
- else
- break;
- }
- }
- }
- // Compare target current is over max current for EVSE limit
- if(gun_index == 0)
- {
- if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
- {
- ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
- }
- }
- else
- {
- if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
- {
- ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
- }
- }
- }
- // Charging session target current check if OCPP disconnect and power sharing server connected
- if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
- {
- if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
- ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>ShmCharger->gun_info[gun_index].primaryMcuState.rating_current?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index];
- else
- {
- if(ShmCharger->gun_info[gun_index].targetCurrent != 0)
- DEBUG_WARN("Disconnect from power sharing server, target current set to 0.\n");
- ShmCharger->gun_info[gun_index].targetCurrent = 0;
- }
- }
- switch(system_mode)
- {
- case SYS_MODE_IDLE:
- case SYS_MODE_PREPARING:
- if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
- {
- ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
- }
- else
- {
- ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
- }
- ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
- break;
- case SYS_MODE_CHARGING:
- case SYS_MODE_TERMINATING:
- if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
- {
- ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
- if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
- (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
- {
- if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
- {
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
- ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
- ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
- }
- else
- {}
- }
- }
- else
- {
- ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
- if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
- (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
- {
- if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
- {
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
- ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
- ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
- }
- else
- {}
- }
- }
- if(previousData[gun_index].primaryMcuCp_Pwn_Duty != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
- {
- DEBUG_INFO("====================================================\n");
- DEBUG_INFO("gun-%02d primaryMcuCp_Pwn_Duty: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
- DEBUG_INFO("====================================================\n");
- previousData[gun_index].primaryMcuCp_Pwn_Duty = ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current;
- }
- if(ShmCharger->gun_info[gun_index].targetCurrent != previousData[gun_index].targetCurrent)
- {
- DEBUG_INFO("====================================================\n");
- DEBUG_INFO("MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
- DEBUG_INFO("gun-%02d targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
- DEBUG_INFO("====================================================\n");
- previousData[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent;
- }
- if(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != previousData[gun_index].current_limit)
- {
- DEBUG_INFO("====================================================\n");
- DEBUG_INFO("gun-%02d mcu current_limit: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
- DEBUG_INFO("====================================================\n");
- previousData[gun_index].current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
- }
- break;
- }
- }
- void checkStopReason(uint8_t gun_index)
- {
- sleep(2);
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- if(!ocpp_get_starttransaction_result(gun_index))
- {
- sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
- }
- else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
- {
- sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
- memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- }
- else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
- {
- sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
- memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- //memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- }
- else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
- {
- if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
- {
- sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
- memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- }
- else
- {
- sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
- memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- }
- }
- else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
- {
- sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
- if(!isMatchStartUser(gun_index))
- {
- memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- }
- else
- {
- memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- }
- DEBUG_INFO("Gun-[%d] : IdTag [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
- }
- else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
- {
- sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
- memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- }
- else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
- {
- sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
- }
- else
- {
- sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
- }
- DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
- DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
- ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
- if(!ocpp_get_starttransaction_result(gun_index))
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
- }
- else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
- memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
- }
- else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
- memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
- }
- else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
- }
- else if((ShmOCPP20Data->MsMsg.bits.ResetReq) && (strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0))
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
- memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
- }
- else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
- if(!isMatchStartUser(gun_index))
- {
- memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
- }
- else
- {
- memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
- }
- DEBUG_INFO("Gun-[%d] : idToken [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
- }
- else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_OVER_CURRENT))
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
- }
- else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
- memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
- }
- else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
- }
- else
- {
- sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
- }
- DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
- DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
- ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
- }
- // Maintain server
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
- {
- memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
- memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].IdTag, ShmOCPP16Data->StopTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
- ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = ON;
- }
- }
- void checkRemoteUpgradeStatus()
- {
- if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
- {
- if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
- {
- DEBUG_INFO("Firmware remote upgraded fail...\n");
- ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
- ShmCharger->isUpdateSuccess = NO;
- }
- else if((strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0))
- {
- DEBUG_INFO("Firmware remote upgrading...\n");
- sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
- sleep(1);
- ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
- ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
- int result = upgrade_check();
- DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
- if(result == PASS)
- {
- if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
- ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
- ShmCharger->isUpdateSuccess = YES;
- DEBUG_INFO("Remote update success...\n");
- }
- else
- {
- if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
- ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
- ShmCharger->isUpdateSuccess = NO;
- DEBUG_INFO("Remote update unsuccess...\n");
- }
- }
- else if((strcmp((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "SignatureVerified")==0))
- {
- DEBUG_INFO("Signature firmware remote upgrading...\n");
- ShmOCPP16Data->SignedFirmwareStatusNotification.requestId = ShmOCPP16Data->SignedUpdateFirmware.requestId;
- sprintf((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "Installing");
- sleep(1);
- ShmOCPP16Data->SpMsg.bits.SignedFirmwareStatusNotificationReq = ON;
- ShmOCPP16Data->MsMsg.bits.SignedUpdateFirmwareReq = OFF;
- if(upgrade_check() == PASS)
- {
- if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
- ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
- ShmCharger->isUpdateSuccess = YES;
- DEBUG_INFO("Remote update success.\n");
- }
- else
- {
- if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
- ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
- ShmCharger->isUpdateSuccess = NO;
- DEBUG_INFO("Remote update fail.\n");
- }
- }
- else
- {}
- }
- else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
- {
- if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
- {
- DEBUG_INFO("Firmware remote upgraded fail...\n");
- ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
- ShmCharger->isUpdateSuccess = NO;
- }
- else if((strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0))
- {
- DEBUG_INFO("Firmware remote upgrading...\n");
- sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
- ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
- ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
- int result = upgrade_check();
- DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
- if(result == PASS)
- {
- if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
- ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
- ShmCharger->isUpdateSuccess = YES;
- DEBUG_INFO("Remote update success...\n");
- }
- else
- {
- if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
- ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
- ShmCharger->isUpdateSuccess = NO;
- DEBUG_INFO("Remote update unsuccess...\n");
- }
- }
- else
- {}
- }
- }
- //===============================================
- // Main process
- //===============================================
- int main(void)
- {
- //==================================================
- // Create all share memory
- //==================================================
- if(CreatShareMemory()==0)
- {
- DEBUG_ERROR("CreatShareMemory NG\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- else
- {
- DEBUG_INFO("CreatShareMemory OK\n");
- }
- for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
- {
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
- ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
- ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
- ShmCharger->gun_info[gun_index].isInitialPass = NO;
- ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
- ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
- }
- //==================================================
- // Main loop
- //==================================================
- for(;;)
- {
- //==============================================
- // Synchronize share memory from OCPP struct
- //==============================================
- ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
- //==============================================
- // Period check for 10 seconds
- //==============================================
- if((DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10000) || (DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
- {
- //==============================================
- // Check task processing
- //==============================================
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
- checkTask();
- //==============================================
- // Check connection timeout specification
- //==============================================
- checkConnectionTimeout();
- ftime(&startTime[0][TMR_IDX_CHECK_TASK]);
- }
- //==============================================
- // Something need run in Idle mode
- //==============================================
- if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_DEBUG)) &&
- (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_DEBUG)):true))
- {
- //======================================
- // Check restore factory setting request
- //======================================
- if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
- {
- // Set led to initial
- for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
- {
- setLedMotion(gun_index,LED_ACTION_INIT);
- }
- sleep(5);
- system("cd /root;./Module_FactoryConfig -m");
- // OCPP 1.6 Configuration key
- system("rm -f /Storage/OCPP/OCPPConfiguration");
- // OCPP 2.O Configuration variable
- DB_variableClear(ocppDb);
- DEBUG_INFO("Default configuration to factory setting. \n");
- system("sync");
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- //======================================
- // Check upgrade firmware request
- //======================================
- if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
- ocpp_get_update_firmware_req())
- {
- // If available memory too small, free memory cache first
- if(getAvailableMemory() < (200*1024*1024))
- {
- DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
- system("echo 3 > /proc/sys/vm/drop_caches");
- }
- ShmCharger->isUpdateSuccess = NO;
- for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
- {
- setLedMotion(gun_index,LED_ACTION_MAINTAIN);
- setChargerMode(gun_index, SYS_MODE_UPDATE);
- }
- }
- }
- //==============================================
- // Check remote reset request
- //==============================================
- checkReset();
- //==============================================
- // Check RFID authorization
- //==============================================
- if((!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq) && (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE))
- {
- if(GetCardSerialNumber()!= FAIL)
- {
- if(isRfidReadable)
- {
- ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
- isRfidReadable = NO;
- }
- }
- else
- {
- isRfidReadable = YES;
- }
- }
- //==============================================
- // Check Local Power Sharing Communication
- //==============================================
- if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
- {
- if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
- {
- if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == ON)
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
- }
- else
- {
- if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == OFF)
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = ON;
- }
- }
- //==============================================
- // Connector loop
- //==============================================
- for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
- {
- // Synchronize current rating value from MCU
- ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
- // Assign connector location index for OCPP
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
- // Synchronize present charging power
- if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
- else
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
- // Check initialization "PASS" or "FAIL"
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
- {
- // Alarm event check
- if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
- {
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
- {
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
- {
- setChargerMode(gun_index, SYS_MODE_ALARM);
- }
- }
- }
- }
- //==========================================
- // Reservation request check
- //==========================================
- checkReservation(gun_index);
- //==========================================
- // Change availability check
- //==========================================
- checkAvailability(gun_index);
- if(ShmCharger->gun_info[gun_index].isOperactive)
- {
- if(isMode(gun_index, SYS_MODE_MAINTAIN))
- {
- setChargerMode(gun_index, SYS_MODE_IDLE);
- }
- }
- else
- {
- if(isMode(gun_index, SYS_MODE_IDLE))
- {
- setChargerMode(gun_index, SYS_MODE_MAINTAIN);
- }
- }
- //==========================================
- // Unlock Connector signal check
- //==========================================
- checkUnlocker(gun_index);
- //==========================================
- // Connector process
- //==========================================
- switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
- {
- case SYS_MODE_BOOTING:
- if(isModeChange(gun_index))
- {
- setLedMotion(gun_index,LED_ACTION_INIT);
- //CSU Initialization & task spawn
- if((Initialization() != PASS) ||
- (SpawnTask() != PASS))
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
- }
- }
- if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
- ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
- ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
- {
- ShmCharger->gun_selectd = 0;
- // Firmware version
- get_firmware_version(gun_index);
- // OCPP BootNotification info set
- DEBUG_INFO("==========================================\n");
- DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
- DEBUG_INFO("==========================================\n");
- DEBUG_INFO("=== OCPP BootNotification information ====\n");
- ocpp_boot_info_sync();
- DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
- DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
- DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
- DEBUG_INFO("==========================================\n");
- DEBUG_INFO("========== Set Wifi information ==========\n");
- DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
- DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
- DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
- DEBUG_INFO("==========================================\n");
- // Set max current to rating current
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
-
- // Default Ethernet / Wifi / 4G to 1:disconnected
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
-
- // If Web Server OPCC URL is empty kill Module_OcppBackend
- if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
- {
- DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
- system("pkill OcppBackend");
- }
- // If rotate switch equal zero, the system needs to change Debug mode
- if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
- setChargerMode(gun_index, SYS_MODE_DEBUG);
- else
- setChargerMode(gun_index, SYS_MODE_IDLE);
- }
- break;
- case SYS_MODE_IDLE:
- if(isModeChange(gun_index))
- {
- setLedMotion(gun_index,LED_ACTION_IDLE);
- setRelay(gun_index,OFF);
- setRequest(gun_index,OFF);
- ShmCharger->gun_info[gun_index].isGunPlugged = NO;
- ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
- ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
- ocpp_set_remotestart(gun_index, OFF);
- ocpp_set_remotestop(gun_index, OFF);
- ocpp_set_auth_conf(OFF);
- ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
- ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
- ocpp_set_unlocker_req(gun_index, OFF);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
- // Response StopTransactionConf
- ocpp_set_stoptransaction_conf(gun_index, OFF);
- memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
- memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- previousData[gun_index].primaryMcuCp_Pwn_Duty = 0;
- previousData[gun_index].targetCurrent = 0;
- previousData[gun_index].current_limit = 0;
- }
- if(ocpp_get_isRemoteStartWait())
- setLedMotion(gun_index,LED_ACTION_CONNECTED);
- else
- setLedMotion(gun_index,LED_ACTION_IDLE);
- if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
- ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
- (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
- (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
- (ocpp_get_remotestart(gun_index) == ON) ||
- (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
- {
- // Clean User id & Card Number
- memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- if((ShmCharger->gun_info[gun_index].rfidReq == ON))
- {
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
- DEBUG_INFO("Start Method: RFID...\n");
- if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
- {
- // Big endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
- break;
- }
- }
- else
- {
- // Little endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- }
- }
- DEBUG_INFO("Start request UserId: %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
- }
- else if(ocpp_get_remotestart(gun_index))
- {
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
- ocpp_copy_userid_from_remotestart(gun_index);
- DEBUG_INFO("Start Method: BACKEND...\n");
- }
- else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
- {
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Local;
- memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
- DEBUG_INFO("Start Method: BLE...\n");
- }
- else
- {
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_NoAuthorization;
- //memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
- ocpp_get_freevend_idtag(ShmSysConfigAndInfo->SysConfig.UserId);
- DEBUG_INFO("Start Method: FREE...\n");
- }
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- ocpp_set_remotestart(gun_index, OFF);
- ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
- ShmCharger->gun_info[gun_index].isGunPlugged = NO;
- ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
- // Get target current
- ocpp_set_profile_req(gun_index, ON);
- sleep(1);
- checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
- setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
- }
- else
- {}
- break;
- case SYS_MODE_AUTHORIZING:
- if(isModeChange(gun_index))
- {
- ftime(&startTime[gun_index][TMR_IDX_AUTH]);
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
- {
- DEBUG_INFO("Rfid start request authorize.\n");
- switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
- {
- case AUTH_MODE_ENABLE:
- if(ocpp_get_connection_status())
- {
- // On line
- ocpp_set_auth_req(ON, "ISO14443");
- }
- else
- {
- // Off line
- switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
- {
- case OFF_POLICY_LOCALLIST:
- ocpp_set_auth_req(ON, "ISO14443");
- break;
- case OFF_POLICY_PH_RFID:
- break;
- case OFF_POLICY_FREE:
- break;
- case OFF_POLICY_NOCHARGE:
- default:
- break;
- }
- }
- break;
- case AUTH_MODE_DISABLE:
- default:
- break;
- }
- }
- else if(ocpp_isAuthorizeRemoteStart() && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND))
- {
- DEBUG_INFO("Remote start request authorize.\n");
- ocpp_set_auth_req(ON, "ISO14443");
- }
- }
- if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
- {
- // Authorization timeout process.
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
- setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
- sleep(3);
- setChargerMode(gun_index, SYS_MODE_IDLE);
- DEBUG_WARN("Authorize timeout !!!\n");
- }
- else
- {
- switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
- {
- case START_METHOD_RFID:
- if((ocpp_get_auth_conf()) ||
- (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
- (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)))
- {
- if(ocpp_get_auth_result(gun_index) ||
- (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
- (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
- {
- memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- DEBUG_INFO("[START_METHOD_RFID] StartUserId: %s \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
- DEBUG_INFO("Authorize pass.\n");
- setSpeaker(ON,SPEAKER_SHORT);
- setLedMotion(gun_index,LED_ACTION_RFID_PASS);
- sleep(3);
- setChargerMode(gun_index, SYS_MODE_PREPARING);
- }
- else
- {
- DEBUG_INFO("Authorize fail.\n");
- setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
- setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
- sleep(3);
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1)
- setChargerMode(gun_index, SYS_MODE_IDLE);
- else
- setChargerMode(gun_index, SYS_MODE_RESERVATION);
- }
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- ocpp_set_auth_conf(OFF);
- }
- break;
- case START_METHOD_BACKEND:
- if(ocpp_isAuthorizeRemoteStart())
- {
- if(ocpp_get_auth_conf())
- {
- if(ocpp_get_auth_result(gun_index))
- {
- memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- DEBUG_INFO("[START_METHOD_BACKEND] StartUserId: %s \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
- DEBUG_INFO("Authorize pass.\n");
- setSpeaker(ON,SPEAKER_SHORT);
- setLedMotion(gun_index,LED_ACTION_RFID_PASS);
- sleep(3);
- setChargerMode(gun_index, SYS_MODE_PREPARING);
- }
- else
- {
- DEBUG_INFO("Authorize fail.\n");
- setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
- setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
- sleep(3);
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1)
- setChargerMode(gun_index, SYS_MODE_IDLE);
- else
- setChargerMode(gun_index, SYS_MODE_RESERVATION);
- }
- ocpp_set_auth_conf(OFF);
- }
- }
- else
- {
- memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- DEBUG_INFO("[START_METHOD_BACKEND] StartUserId: %s \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
- setSpeaker(ON,SPEAKER_SHORT);
- setChargerMode(gun_index, SYS_MODE_PREPARING);
- }
- break;
- case START_METHOD_BLE:
- case START_METHOD_FREE:
- default:
- setSpeaker(ON,SPEAKER_SHORT);
- setChargerMode(gun_index, SYS_MODE_PREPARING);
- break;
- }
- }
- break;
- case SYS_MODE_PREPARING:
- if(isModeChange(gun_index))
- {
- ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
- setLedMotion(gun_index,LED_ACTION_CONNECTED);
- ocpp_set_profile_req(gun_index, ON);
- }
- // Unplug charging gun to Idle mode
- if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
- {
- DEBUG_INFO("Unplug the connector.\n");
- setChargerMode(gun_index, SYS_MODE_IDLE);
- }
- // Use RFID card to stop handshaking
- if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
- {
- DEBUG_INFO("Use RFID card to stop handshaking.\n");
- setSpeaker(ON,SPEAKER_SHORT);
- setChargerMode(gun_index, SYS_MODE_IDLE);
- }
- else
- {
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- }
- // Handshaking timeout and the system back to idle
- if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
- {
- setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
- if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
- {
- DEBUG_INFO("Handshaking timeout...\n");
- setChargerMode(gun_index, SYS_MODE_IDLE);
- }
- }
- else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
- {
- setRequest(gun_index,ON);
- ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
- ShmCharger->gun_info[gun_index].isGunPlugged = YES;
- ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
- checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
- // Set relay on when the system is state C
- if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) && ((ShmCharger->gun_info[gun_index].targetCurrent != 0)))
- setRelay(gun_index, ON);
- if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
- {
- ocpp_set_unlocker_req(gun_index, OFF);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
- getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
- ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
- memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- ocpp_copy_userid_to_starttransaction(gun_index);
- ocpp_set_starttransaction_req(gun_index, ON);
- ftime(&startChargingTime[gun_index]);
- setChargerMode(gun_index, SYS_MODE_CHARGING);
- }
- }
- break;
- case SYS_MODE_CHARGING:
- if(isModeChange(gun_index))
- {
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- //ftime(&startChargingTime[gun_index]);
- ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
- ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
- ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
- startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
- ocpp_set_auth_req(OFF);
- ocpp_reset_smartcharging_profileId(gun_index);
- ocpp_set_profile_req(gun_index, ON);
- ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
- }
- // If unplug the connector during a session, the system must stop charging and stop session.
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
- {
- if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
- {
- ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
- DEBUG_INFO("The connector was unplugged under CHARGING.\n");
- }
- }
- if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES) ||
- (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
- (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
- ocpp_get_remotestop(gun_index) ||
- (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
- (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
- ocpp_get_reset_req() ||
- ocpp_get_unlocker_req(gun_index) ||
- (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
- {
- if(ShmCharger->gun_info[gun_index].rfidReq)
- {
- // If RFID SN different with start user, it need to authorize ID
- if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
- {
- if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
- {
- // Request authorization
- ftime(&startTime[gun_index][TMR_IDX_AUTH]);
- memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
- {
- // Big endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
- break;
- }
- }
- else
- {
- // Little endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- }
- }
- DEBUG_INFO("End request UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
- if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
- //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
- (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
- {
- switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
- {
- case AUTH_MODE_ENABLE:
- if(ocpp_get_connection_status())
- {
- // On line
- ocpp_set_auth_req(ON, "ISO14443");
- }
- else
- {
- // Off line
- switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
- {
- case OFF_POLICY_LOCALLIST:
- ocpp_set_auth_req(ON, "ISO14443");
- break;
- case OFF_POLICY_PH_RFID:
- break;
- case OFF_POLICY_FREE:
- break;
- case OFF_POLICY_NOCHARGE:
- default:
- break;
- }
- }
- break;
- case AUTH_MODE_DISABLE:
- default:
- break;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
- {
- DEBUG_INFO("Should be remote stop charger... \n");
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- }
- }
- else
- {
- if(ocpp_get_auth_conf() ||
- (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
- (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)) ||
- (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
- {
- if(ocpp_get_auth_result(gun_index) ||
- (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
- (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
- (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
- {
- ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
- }
- else
- {
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- ocpp_set_auth_req(OFF);
- DEBUG_INFO("Authorize fail.\n");
- setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
- setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
- sleep(3);
- }
- startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
- ocpp_set_auth_conf(OFF);
- }
- }
- }
- // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
- if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
- setChargerMode(gun_index, SYS_MODE_TERMINATING);
- }
- else
- setChargerMode(gun_index, SYS_MODE_TERMINATING);
- }
- else
- {
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
- ftime(&endChargingTime[gun_index]);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start)/100.0;
- // Response StartTransactionConf
- ocpp_set_starttransaction_conf(gun_index, OFF);
- // Charging profile preparation
- if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
- {
- if(!ocpp_get_profile_conf(gun_index))
- {
- ocpp_set_profile_req(gun_index, ON);
- ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
- }
- else
- {
- ocpp_set_profile_conf(gun_index, OFF);
- }
- }
- // Checking profile id > 0 and current time is between charging profile validFrom & validTo
- checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
- // If temperature warning derating PWM duty 20%
- if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp >= SPEC_TEMPERATURE_WARN)
- {
- ShmCharger->gun_info[gun_index].tempCoefficient = 0.8;
- }
- else if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp <= SPEC_TEMPERATURE_RECOVER)
- {
- ShmCharger->gun_info[gun_index].tempCoefficient = 1.0;
- }
- ShmCharger->gun_info[gun_index].targetCurrent = (ShmCharger->gun_info[gun_index].targetCurrent==0?ShmCharger->gun_info[gun_index].targetCurrent:(((ShmCharger->gun_info[gun_index].targetCurrent*ShmCharger->gun_info[gun_index].tempCoefficient)>=6) ? (ShmCharger->gun_info[gun_index].targetCurrent*ShmCharger->gun_info[gun_index].tempCoefficient):6));
- // Debug information
- if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
- {
- /*
- DEBUG_INFO("=============================================================\n");
- DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
- DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
- DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
- DEBUG_INFO("=============================================================\n");
- */
- ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
- }
- if(ocpp_get_connection_status())
- {
- // On-line max condition check
- if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
- {
- setRelay(gun_index, OFF);
- ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
- setChargerMode(gun_index, SYS_MODE_TERMINATING);
- DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
- }
- else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
- {
- setRelay(gun_index, OFF);
- ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
- setChargerMode(gun_index, SYS_MODE_TERMINATING);
- DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
- }
- else
- {
- ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
- if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
- {
- setLedMotion(gun_index,LED_ACTION_CHARGING);
- setRelay(gun_index, ON);
- }
- else
- {
- setLedMotion(gun_index, LED_ACTION_STOP);
- setRelay(gun_index, OFF);
- }
- }
- }
- else
- {
- // Off-line max condition check
- if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
- ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
- {
- if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))))
- {
- setRelay(gun_index, OFF);
- ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
- setChargerMode(gun_index, SYS_MODE_TERMINATING);
- DEBUG_INFO("Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
- }
- else if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
- {
- setRelay(gun_index, OFF);
- ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
- setChargerMode(gun_index, SYS_MODE_TERMINATING);
- DEBUG_INFO("Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
- }
- else
- {
- ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
- if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
- {
- setLedMotion(gun_index,LED_ACTION_CHARGING);
- setRelay(gun_index, ON);
- }
- else
- {
- setLedMotion(gun_index, LED_ACTION_STOP);
- setRelay(gun_index, OFF);
- }
- }
- }
- else
- {
- setRelay(gun_index, OFF);
- DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
- }
- }
- }
- break;
- case SYS_MODE_TERMINATING:
- if(isModeChange(gun_index))
- {
- setLedMotion(gun_index, LED_ACTION_STOP);
- getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
- startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
-
- if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)
- setRelay(gun_index, OFF);
- sleep(1);
- }
- ftime(&endChargingTime[gun_index]);
- //if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
- //{
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
- //}
- // If unplug the connector during a session, the system must stop charging and stop session.
- if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
- {
- if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
- {
- ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
- DEBUG_INFO("The connector was unplugged under TERMINATING.\n");
- }
- }
- // End authorize pass
- if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
- (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
- (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
- ocpp_get_remotestop(gun_index) ||
- (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
- (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
- ocpp_get_reset_req() ||
- ocpp_get_unlocker_req(gun_index) ||
- (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
- {
- if((ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON) || (ShmCharger->gun_info[gun_index].isChargerStopByCondition == YES))
- {
- if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
- (ShmCharger->gun_info[gun_index].isAuthPassEnd))
- {
- DEBUG_INFO("Authorize pass.\n");
- setSpeaker(ON,SPEAKER_SHORT);
- setLedMotion(gun_index,LED_ACTION_RFID_PASS);
- sleep(3);
- }
- }
- else
- {}
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
- ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
- setLedMotion(gun_index, LED_ACTION_STOP);
- checkStopReason(gun_index);
- setChargerMode(gun_index, SYS_MODE_COMPLETE);
- }
- else
- {
- // If the charger stops charging, the led should be stoped blink
- if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
- {
- setLedMotion(gun_index, LED_ACTION_STOP);
- }
- if(ShmCharger->gun_info[gun_index].rfidReq)
- {
- // If RFID SN different with start user, it need to authorize ID
- if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
- {
- if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
- {
- // Request authorization
- ftime(&startTime[gun_index][TMR_IDX_AUTH]);
- memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
- {
- // Big endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
- break;
- }
- }
- else
- {
- // Little endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- }
- }
- DEBUG_INFO("End request UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
- if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
- //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
- (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
- {
- switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
- {
- case AUTH_MODE_ENABLE:
- if(ocpp_get_connection_status())
- {
- // On line
- ocpp_set_auth_req(ON, "ISO14443");
- }
- else
- {
- // Off line
- switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
- {
- case OFF_POLICY_LOCALLIST:
- ocpp_set_auth_req(ON, "ISO14443");
- break;
- case OFF_POLICY_PH_RFID:
- break;
- case OFF_POLICY_FREE:
- break;
- case OFF_POLICY_NOCHARGE:
- default:
- break;
- }
- }
- break;
- case AUTH_MODE_DISABLE:
- default:
- break;
- }
- }
- else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
- {
- DEBUG_INFO("Should be remote stop charger... \n");
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- }
- }
- else
- {
- if(ocpp_get_auth_conf() ||
- (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
- (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)) ||
- (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
- {
- if(ocpp_get_auth_result(gun_index) ||
- (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
- (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
- (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
- {
- ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
- }
- else
- {
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- ocpp_set_auth_req(OFF);
- DEBUG_INFO("Authorize fail... \n");
- setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
- setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
- sleep(3);
- }
- startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
- ocpp_set_auth_conf(OFF);
- }
- }
- }
- }
- // Charging profile preparation
- if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
- {
- if(!ocpp_get_profile_conf(gun_index))
- {
- ocpp_set_profile_req(gun_index, ON);
- ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
- }
- else
- {
- ocpp_set_profile_conf(gun_index, OFF);
- }
- }
- // Check target current if charging profile limit > 0
- checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
- setRelay(gun_index, OFF);
- // Debug information
- if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
- {
- /*
- DEBUG_INFO("=============================================================\n");
- DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
- DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
- DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
- DEBUG_INFO("=============================================================\n");
- */
- ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
- }
- if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES) &&
- (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
- (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
- (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
- !ocpp_get_remotestop(gun_index) &&
- (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
- !ocpp_get_reset_req() &&
- ocpp_get_starttransaction_result(gun_index) &&
- !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
- !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
- !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))) &&
- !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))))
- {
- sleep(1);
- setChargerMode(gun_index, SYS_MODE_CHARGING);
- }
- }
- break;
- case SYS_MODE_COMPLETE:
- if(isModeChange(gun_index))
- {
- setLedMotion(gun_index, LED_ACTION_STOP);
- sleep(6);
- setRequest(gun_index, OFF);
- setRelay(gun_index, OFF);
- sleep(2);
- }
- if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || ocpp_get_reset_req())
- {
- sleep(2);
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
- ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
- ocpp_set_remotestop(gun_index, OFF);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
- ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
- DB_Insert_Record(localDb, gun_index);
- setChargerMode(gun_index, SYS_MODE_IDLE);
- }
- break;
- case SYS_MODE_ALARM:
- setLedMotion(gun_index,LED_ACTION_ALARM);
- setRelay(gun_index, OFF);
- if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A))
- {
- setRequest(gun_index,OFF);
- if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
- {
- ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
- DEBUG_INFO("The connector was unplugged under ALARM.\n");
- }
- }
- if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
- {
- if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
- (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
- {
- setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
- }
- else
- {
- setChargerMode(gun_index, SYS_MODE_IDLE);
- }
- }
- break;
- case SYS_MODE_FAULT:
- if(isModeChange(gun_index))
- {}
- break;
- case SYS_MODE_MAINTAIN:
- if(isModeChange(gun_index))
- {
- setLedMotion(gun_index,LED_ACTION_MAINTAIN);
- }
- break;
- case SYS_MODE_UPDATE:
- if(isModeChange(gun_index))
- {
- setLedMotion(gun_index,LED_ACTION_MAINTAIN);
- }
- //======================================
- // Check local upgrade firmware request
- //======================================
- if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
- {
- DEBUG_INFO("Firmware local upgrading...\n");
- ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
- int value = upgrade_check();
- DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
- if(value == PASS)
- {
- if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
- ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
- ShmCharger->isUpdateSuccess = YES;
- DEBUG_INFO("Local update success...\n");
- }
- else
- {
- if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
- ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
- ShmCharger->isUpdateSuccess = NO;
- DEBUG_INFO("Local update unsuccess...\n");
- }
- }
- else if(ocpp_get_update_firmware_req())
- {
- //======================================
- // Check remote upgrade firmware request
- //======================================
- checkRemoteUpgradeStatus();
- }
- else
- {
- //======================================
- // Upgrade complete reboot system
- //======================================
- if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
- {
- if(ShmCharger->isUpdateSuccess == YES)
- {
- if((strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing")==0))
- {
- sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
- ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
- }
- else if((strcmp((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "Installing")==0))
- {
- ShmOCPP16Data->SignedFirmwareStatusNotification.requestId = ShmOCPP16Data->SignedUpdateFirmware.requestId;
- sprintf((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "Installed");
- ShmOCPP16Data->SpMsg.bits.SignedFirmwareStatusNotificationReq = ON;
- }
- sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
- ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
- DEBUG_WARN("Firmware upgrade success.\n");
- sleep(5);
- DEBUG_INFO("Firmware upgraded, reboot...\n");
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- else
- {
- sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
- ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
- sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
- ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
- DEBUG_WARN("Firmware upgrade fail.\n");
- sleep(5);
- system("rm -rvf /mnt/* ");
- close(wtdFd);
- system("/usr/bin/run_evse_restart.sh");
- }
- }
- }
- break;
- case SYS_MODE_RESERVATION:
- if(isModeChange(gun_index))
- {
- setLedMotion(gun_index,LED_ACTION_MAINTAIN);
- }
- if(isReservationExpired(gun_index))
- {
- DEBUG_INFO("Reservation: Time's up...\n");
- setChargerMode(gun_index, SYS_MODE_IDLE);
- }
- else if(ocpp_get_cancelreservation_req(gun_index))
- {
- DEBUG_INFO("Reservation: Cancel reservation...\n");
- setChargerMode(gun_index, SYS_MODE_IDLE);
- ocpp_set_cancelreservation_req(gun_index, OFF);
- }
- else
- {
- // Check is there RFID or back end request start
- if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
- ocpp_get_remotestart(gun_index))
- {
- if((ShmCharger->gun_info[gun_index].rfidReq == ON))
- {
- memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
- if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
- {
- // Big endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
- break;
- }
- }
- else
- {
- // Little endian
- switch(rfid.snType)
- {
- case RFID_SN_TYPE_8BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_7BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_10BYTE:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- case RFID_SN_TYPE_4BYTE:
- default:
- sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
- break;
- }
- }
- if(ocpp_compare_reserve_id_with_user(gun_index) ||
- ocpp_get_auth_result(gun_index))
- {
- DEBUG_INFO("Start Method in reservation : RFID...\n");
- DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
- setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
- }
- else
- {
- DEBUG_INFO("It's not reserve user id.\n");
- DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
- //setLedMotion(gun_index,LED_ACTION_MAINTAIN);
- setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
- }
- ShmCharger->gun_info[gun_index].rfidReq = OFF;
- }
- else if(ocpp_get_remotestart(gun_index))
- {
- if(ocpp_compare_reserve_id_with_remote_user(gun_index) || ocpp_isAuthorizeRemoteStart())
- {
- DEBUG_INFO("Start Method in reservation: BACKEND...\n");
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
- ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
- if(ocpp_isAuthorizeRemoteStart())
- ocpp_copy_userid_from_remotestart(gun_index);
- setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
- }
- ocpp_set_remotestart(gun_index, OFF);
- }
- }
- }
- break;
- case SYS_MODE_BOOKING:
- if(isModeChange(gun_index))
- {}
- break;
- case SYS_MODE_DEBUG:
- if(isModeChange(gun_index))
- {
- setLedMotion(gun_index,LED_ACTION_DEBUG);
- }
- break;
- }
- }
- // System watch dog reset
- write(wtdFd, "a", 1);
- usleep(50000);
- }
- return FAIL;
- }
|