main.c 215 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837
  1. #include "define.h"
  2. #include "main.h"
  3. //==========================
  4. // System basic sample constant
  5. //==========================
  6. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  7. #define PASS 1
  8. #define FAIL -1
  9. #define YES 1
  10. #define NO 0
  11. #define ON 1
  12. #define OFF 0
  13. #define BUFFER_SIZE 128
  14. //==========================
  15. // Timer interval
  16. //==========================
  17. #define TMR_IDX_HANDSHAKING 0
  18. #define TMR_IDX_AUTH 1
  19. #define TMR_IDX_LOGPPRINTOUT 2
  20. #define TMR_IDX_PROFILE_PREPARE 3
  21. #define TMR_IDX_PWN_CHANGE 4
  22. #define TMR_IDX_CHECK_TASK 5
  23. #define TMR_IDX_RESET_4G_WIFI_POWER 6
  24. #define TMR_IDX_7 7
  25. #define TMR_IDX_8 8
  26. #define TMR_IDX_9 9
  27. #define TIMEOUT_SPEC_HANDSHAKING 180000
  28. #define TIMEOUT_SPEC_AUTH 15000
  29. #define TIMEOUT_SPEC_HANDSHAKING_LED 185000
  30. #define TIMEOUT_SPEC_LOGPPRINTOUT 30000
  31. #define TIMEOUT_SPEC_PROFILE_PREPARE 60000
  32. #define TIMEOUT_SPEC_PWN_CHANGE 5000
  33. #define TIMEOUT_SPEC_RESET_4G_WIFI_POWER 300000
  34. #define SPEC_TEMPERATURE_WARN 71
  35. #define SPEC_TEMPERATURE_RECOVER 63
  36. #define MtdBlockSize 0x300000
  37. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  38. #define DB_FILE_OCPP_LOCAL "/Storage/OCPP/charger.db"
  39. //==========================
  40. // Declare method
  41. //==========================
  42. void trim(char *s);
  43. int mystrcmp(char *p1,char *p2);
  44. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  45. void split(char **arr, char *str, const char *del);
  46. int isReachableInternet();
  47. int isRouteFail();
  48. int InitRfidPort(void);
  49. int GetCardSerialNumber();
  50. void setLedMotion(unsigned char gun_index,unsigned char led_mode);
  51. void setRelay(unsigned char gun_index,unsigned char isOn);
  52. void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
  53. //==========================
  54. // Declare RFID module type
  55. //==========================
  56. #define MODULE_EWT 0
  57. int wtdFd = -1;
  58. int rfidFd = -1;
  59. char* rfidPortName = "/dev/ttyS2";
  60. RFID rfid;
  61. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  62. int isRfidReadable;
  63. //==========================
  64. // Declare share memory
  65. //==========================
  66. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  67. struct StatusCodeData *ShmStatusCodeData;
  68. struct PsuData *ShmPsuData;
  69. struct CHAdeMOData *ShmCHAdeMOData;
  70. struct CcsData *ShmCcsData;
  71. struct PrimaryMcuData *ShmPrimaryMcuData;
  72. struct FanModuleData *ShmFanModuleData;
  73. struct RelayModuleData *ShmRelayModuleData;
  74. struct OCPP16Data *ShmOCPP16Data;
  75. struct OCPP20Data *ShmOCPP20Data;
  76. struct OCPP16Data *ShmOCPP16DataPH;
  77. struct Charger *ShmCharger;
  78. struct timeb startTime[AC_QUANTITY][10];
  79. struct timeb startChargingTime[AC_QUANTITY];
  80. struct timeb endChargingTime[AC_QUANTITY];
  81. sqlite3 *localDb;
  82. sqlite3 *ocppDb;
  83. struct SysConfigData SysConfigOrg;
  84. ParsingRatedCur modelnameInfo={0};
  85. struct PreviousData
  86. {
  87. uint16_t targetCurrent;
  88. uint16_t current_limit;
  89. uint16_t primaryMcuCp_Pwn_Duty;
  90. }previousData[4];
  91. //=================================
  92. // Common routine
  93. //=================================
  94. void trim(char *s)
  95. {
  96. int i=0, j, k, l=0;
  97. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  98. i++;
  99. j = strlen(s)-1;
  100. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  101. j--;
  102. if(i==0 && j==strlen(s)-1) { }
  103. else if(i==0) s[j+1] = '\0';
  104. else {
  105. for(k=i; k<=j; k++) s[l++] = s[k];
  106. s[l] = '\0';
  107. }
  108. }
  109. int mystrcmp(char *p1,char *p2)
  110. {
  111. while(*p1==*p2)
  112. {
  113. if(*p1=='\0' || *p2=='\0')
  114. break;
  115. p1++;
  116. p2++;
  117. }
  118. if(*p1=='\0' && *p2=='\0')
  119. return(PASS);
  120. else
  121. return(FAIL);
  122. }
  123. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  124. {
  125. strncpy(dest, src + start, cnt);
  126. dest[cnt] = 0;
  127. }
  128. void split(char **arr, char *str, const char *del)
  129. {
  130. char *s = strtok(str, del);
  131. while(s != NULL)
  132. {
  133. *arr++ = s;
  134. s = strtok(NULL, del);
  135. }
  136. }
  137. int StoreLogMsg(const char *fmt, ...)
  138. {
  139. char Buf[4096+256];
  140. char buffer[4096];
  141. time_t CurrentTime;
  142. struct tm *tm;
  143. struct timeval tv;
  144. va_list args;
  145. va_start(args, fmt);
  146. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  147. va_end(args);
  148. memset(Buf,0,sizeof(Buf));
  149. CurrentTime = time(NULL);
  150. tm=localtime(&CurrentTime);
  151. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  152. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  153. {
  154. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  155. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  156. buffer,
  157. tm->tm_year+1900,tm->tm_mon+1,
  158. ShmSysConfigAndInfo->SysConfig.ModelName,
  159. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  160. }
  161. else
  162. {
  163. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  164. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  165. buffer,
  166. tm->tm_year+1900,tm->tm_mon+1);
  167. }
  168. #ifdef SystemLogMessage
  169. system(Buf);
  170. #endif
  171. #ifdef ConsloePrintLog
  172. 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);
  173. #endif
  174. return rc;
  175. }
  176. long long DiffTimebWithNow(struct timeb ST)
  177. {
  178. //return milli-second
  179. struct timeb ET;
  180. long long StartTime,StopTime;
  181. ftime(&ET);
  182. StartTime=(long long)ST.time;
  183. StopTime=(long long)ET.time;
  184. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  185. }
  186. long long DiffTimeb(struct timeb ST, struct timeb ET)
  187. {
  188. //return milli-second
  189. long long StartTime,StopTime;
  190. StartTime=(long long)ST.time;
  191. StopTime=(long long)ET.time;
  192. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  193. }
  194. int DiffTimebWithNowSec(struct timeb ST)
  195. {
  196. //return milli-second
  197. struct timeb ET;
  198. unsigned int StartTime,StopTime;
  199. ftime(&ET);
  200. StartTime=(unsigned int)ST.time;
  201. StopTime=(unsigned int)ET.time;
  202. return (StopTime-StartTime);
  203. }
  204. int isOvertNow(uint8_t *start)
  205. {
  206. int result = YES;
  207. struct ParsingResult
  208. {
  209. int result;
  210. int scanedElement;
  211. int year;
  212. int month;
  213. int mday;
  214. int hour;
  215. int min;
  216. int sec;
  217. int tz_hour;
  218. int tz_min;
  219. float minSec;
  220. }parsingResult;
  221. struct tm tmStart;
  222. struct timeb tbStart;
  223. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  224. if(strstr((char*)start, ".") != NULL)
  225. {
  226. // Original data with mini second
  227. if(strstr((char*)start, "Z") != NULL)
  228. {
  229. // Original data with Z
  230. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  231. &parsingResult.year,
  232. &parsingResult.month,
  233. &parsingResult.mday,
  234. &parsingResult.hour,
  235. &parsingResult.min,
  236. &parsingResult.sec,
  237. &parsingResult.minSec);
  238. }
  239. else
  240. {
  241. // Original data without Z
  242. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  243. &parsingResult.year,
  244. &parsingResult.month,
  245. &parsingResult.mday,
  246. &parsingResult.hour,
  247. &parsingResult.min,
  248. &parsingResult.sec,
  249. &parsingResult.minSec,
  250. &parsingResult.tz_hour,
  251. &parsingResult.tz_min);
  252. }
  253. }
  254. else
  255. {
  256. // Original data without mini second
  257. if(strstr((char*)start, "Z") != NULL)
  258. {
  259. // Original data with Z
  260. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  261. &parsingResult.year,
  262. &parsingResult.month,
  263. &parsingResult.mday,
  264. &parsingResult.hour,
  265. &parsingResult.min,
  266. &parsingResult.sec);
  267. }
  268. else
  269. {
  270. // Original data without Z
  271. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  272. &parsingResult.year,
  273. &parsingResult.month,
  274. &parsingResult.mday,
  275. &parsingResult.hour,
  276. &parsingResult.min,
  277. &parsingResult.sec,
  278. &parsingResult.tz_hour,
  279. &parsingResult.tz_min);
  280. }
  281. }
  282. if(parsingResult.scanedElement >= 6)
  283. {
  284. tmStart.tm_year = parsingResult.year - 1900;
  285. tmStart.tm_mon = parsingResult.month - 1;
  286. tmStart.tm_mday = parsingResult.mday;
  287. tmStart.tm_hour = parsingResult.hour;
  288. tmStart.tm_min = parsingResult.min;
  289. tmStart.tm_sec = parsingResult.sec;
  290. tmStart.tm_gmtoff = 0;
  291. tbStart.time = mktime(&tmStart);
  292. tbStart.timezone = 0;
  293. tbStart.millitm = 0;
  294. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  295. if(DiffTimebWithNowSec(tbStart) <= 0)
  296. result = NO;
  297. else
  298. result = YES;
  299. }
  300. else
  301. {
  302. DEBUG_WARN("Start date parsing error.\n");
  303. }
  304. return result;
  305. }
  306. void getDateTimeString(char* result)
  307. {
  308. time_t CurrentTime;
  309. struct tm *tm;
  310. CurrentTime = time(NULL);
  311. tm=localtime(&CurrentTime);
  312. 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);
  313. }
  314. int getCurrentYear()
  315. {
  316. int result = 0;
  317. time_t CurrentTime;
  318. struct tm *tm;
  319. CurrentTime = time(NULL);
  320. tm=localtime(&CurrentTime);
  321. result = (tm->tm_year + 1900);
  322. return result;
  323. }
  324. unsigned long long getAvailableMemory()
  325. {
  326. long pages = sysconf(_SC_AVPHYS_PAGES);
  327. long page_size = sysconf(_SC_PAGE_SIZE);
  328. return pages * page_size;
  329. }
  330. unsigned int isKernelSupportNAT()
  331. {
  332. unsigned int result = NO;
  333. unsigned int version = 0;
  334. FILE *fp;
  335. char cmd[256];
  336. char buf[512];
  337. // Get IP address & net mask
  338. strcpy(cmd, "uname -v");
  339. fp = popen(cmd, "r");
  340. if(fp != NULL)
  341. {
  342. if(fgets(buf, sizeof(buf), fp) != NULL)
  343. {
  344. sscanf(buf, "#%d", &version);
  345. //DEBUG_INFO("Kernel version: %d\n", version);
  346. if(version >= 30)
  347. result = YES;
  348. }
  349. }
  350. pclose(fp);
  351. return result;
  352. }
  353. int getEth0MacAddress()
  354. {
  355. int result = PASS;
  356. FILE *fp;
  357. char cmd[256];
  358. char buf[512];
  359. char tmp[512];
  360. strcpy(cmd, "ifconfig eth0");
  361. fp = popen(cmd, "r");
  362. if(fp != NULL)
  363. {
  364. while(fgets(buf, sizeof(buf), fp) != NULL)
  365. {
  366. if(strstr(buf, "eth0") > 0)
  367. {
  368. result = PASS;
  369. }
  370. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  371. {
  372. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  373. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  374. }
  375. }
  376. }
  377. pclose(fp);
  378. return result;
  379. }
  380. int isUap0up(void)
  381. {
  382. int result = FAIL;
  383. FILE *fp;
  384. char cmd[256];
  385. char buf[512];
  386. strcpy(cmd, "ifconfig uap0");;
  387. fp = popen(cmd, "r");
  388. if(fp != NULL)
  389. {
  390. while(fgets(buf, sizeof(buf), fp) != NULL)
  391. {
  392. if(strstr(buf, "uap0") > 0)
  393. {
  394. result = PASS;
  395. }
  396. }
  397. }
  398. pclose(fp);
  399. return result;
  400. }
  401. //======================================================
  402. // OCPP routine
  403. //======================================================
  404. void ocpp_process_start()
  405. {
  406. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  407. system("/root/OcppBackend &");
  408. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  409. system("/root/OcppBackend20 &");
  410. }
  411. uint8_t ocpp_get_isRemoteStartWait()
  412. {
  413. uint8_t result = OFF;
  414. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  415. {
  416. result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  417. }
  418. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  419. {
  420. result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  421. }
  422. return result;
  423. }
  424. uint8_t ocpp_get_connection_status()
  425. {
  426. uint8_t result = OFF;
  427. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  428. {
  429. result = ShmOCPP16Data->OcppConnStatus;
  430. }
  431. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  432. {
  433. result = ShmOCPP20Data->OcppConnStatus;
  434. }
  435. return result;
  436. }
  437. uint16_t ocpp_get_connection_timeout()
  438. {
  439. uint16_t result = (TIMEOUT_SPEC_HANDSHAKING/1000);
  440. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  441. {
  442. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  443. {
  444. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  445. }
  446. }
  447. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  448. {
  449. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  450. {
  451. result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  452. }
  453. }
  454. return result;
  455. }
  456. uint8_t ocpp_get_update_firmware_req()
  457. {
  458. uint8_t result = NO;
  459. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  460. {
  461. result = ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq || ShmOCPP16Data->MsMsg.bits.SignedUpdateFirmwareReq;
  462. }
  463. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  464. {
  465. result = ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq;
  466. }
  467. return result;
  468. }
  469. uint8_t ocpp_get_reset_req()
  470. {
  471. uint8_t result = NO;
  472. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  473. {
  474. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  475. }
  476. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  477. {
  478. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  479. {
  480. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  481. }
  482. else
  483. {
  484. 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)) &&
  485. (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))
  486. {
  487. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  488. }
  489. }
  490. }
  491. return result;
  492. }
  493. void ocpp_boot_info_sync()
  494. {
  495. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  496. {
  497. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  498. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  499. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  500. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  501. switch(ShmSysConfigAndInfo->SysConfig.ModelName[3])
  502. {
  503. case 'M':
  504. case 'Z':
  505. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "MID");
  506. break;
  507. case 'P':
  508. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "PTB");
  509. break;
  510. case 'I':
  511. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "TIC");
  512. break;
  513. case 'U':
  514. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "UL");
  515. break;
  516. default:
  517. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "N/A");
  518. break;
  519. }
  520. }
  521. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  522. {
  523. memcpy((char*)ShmOCPP20Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  524. memcpy((char*)ShmOCPP20Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  525. sprintf((char*)ShmOCPP20Data->BootNotification.chargingStation.firmwareVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  526. }
  527. }
  528. void ocpp_set_remotestart(uint8_t gun_index, uint8_t status)
  529. {
  530. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  531. {
  532. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq != status)
  533. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = status;
  534. }
  535. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  536. {
  537. if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq != status)
  538. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = status;
  539. }
  540. }
  541. void ocpp_set_remotestop(uint8_t gun_index, uint8_t status)
  542. {
  543. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  544. {
  545. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != status)
  546. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = status;
  547. }
  548. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  549. {
  550. if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq != status)
  551. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = status;
  552. }
  553. }
  554. uint8_t ocpp_get_remotestart(uint8_t gun_index)
  555. {
  556. uint8_t result = OFF;
  557. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  558. {
  559. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  560. }
  561. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  562. {
  563. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  564. }
  565. return result;
  566. }
  567. void ocpp_copy_userid_from_remotestart(uint8_t gun_index)
  568. {
  569. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  570. {
  571. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  572. DEBUG_INFO("==========================================\n");
  573. DEBUG_INFO("Copy IdTag into UserId. \n");
  574. DEBUG_INFO("IdTag : [%s] ---> UserId : [%s] . \n", ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ShmSysConfigAndInfo->SysConfig.UserId);
  575. DEBUG_INFO("==========================================\n");
  576. }
  577. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  578. {
  579. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  580. DEBUG_INFO("==========================================\n");
  581. DEBUG_INFO("Copy idToken into UserId. \n");
  582. DEBUG_INFO("idToken : [%s] ---> UserId : [%s]. \n", ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ShmSysConfigAndInfo->SysConfig.UserId);
  583. DEBUG_INFO("==========================================\n");
  584. }
  585. }
  586. uint8_t ocpp_get_remotestop(uint8_t gun_index)
  587. {
  588. uint8_t result = OFF;
  589. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  590. {
  591. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq;
  592. }
  593. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  594. {
  595. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq;
  596. }
  597. return result;
  598. }
  599. void ocpp_set_auth_conf(uint8_t status)
  600. {
  601. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  602. {
  603. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf != status)
  604. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = status;
  605. }
  606. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  607. {
  608. if(ShmOCPP20Data->SpMsg.bits.AuthorizeConf != status)
  609. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = status;
  610. }
  611. }
  612. void ocpp_set_auth_req(uint8_t status, ...)
  613. {
  614. va_list args;
  615. if(status == ON)
  616. ocpp_set_auth_conf(OFF);
  617. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  618. {
  619. if(ShmOCPP16Data->SpMsg.bits.AuthorizeReq != status)
  620. {
  621. if(status == ON)
  622. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, 0x00, ARRAY_SIZE(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  623. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = status;
  624. }
  625. }
  626. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  627. {
  628. if(ShmOCPP20Data->SpMsg.bits.AuthorizeReq != status)
  629. {
  630. if(status == ON)
  631. {
  632. memset(&ShmOCPP20Data->Authorize.Response_idTokenInfo, 0x00, sizeof(struct IdTokenInfoType));
  633. va_start(args, status);
  634. sprintf((char*)ShmOCPP20Data->Authorize.idToken.type, "%s", va_arg(args, char*));
  635. va_end(args);
  636. }
  637. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = status;
  638. }
  639. }
  640. }
  641. uint8_t ocpp_get_auth_req()
  642. {
  643. uint8_t result = OFF;
  644. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  645. {
  646. result = ShmOCPP16Data->SpMsg.bits.AuthorizeReq;
  647. }
  648. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  649. {
  650. result = ShmOCPP20Data->SpMsg.bits.AuthorizeReq;
  651. }
  652. return result;
  653. }
  654. uint8_t ocpp_get_auth_conf()
  655. {
  656. uint8_t result = OFF;
  657. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  658. {
  659. result = ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  660. }
  661. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  662. {
  663. result = ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  664. }
  665. return result;
  666. }
  667. uint8_t ocpp_get_auth_result(uint8_t gun_index)
  668. {
  669. uint8_t result = OFF;
  670. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  671. {
  672. case SYS_MODE_AUTHORIZING:
  673. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  674. {
  675. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1) && (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0)) ||
  676. ((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))))
  677. result = PASS;
  678. DEBUG_INFO("============ [SYS_MODE_AUTHORIZING] ==============\n");
  679. DEBUG_INFO("Authorize response status: [ %s ].\n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  680. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId != -1))
  681. {
  682. DEBUG_INFO("Gun-%d Reservation id: [ %d ].\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId);
  683. DEBUG_INFO("Authorize response ParentIdTag: [ %s ].\n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  684. DEBUG_INFO("Gun-%d Reserve ParentIdTag: [ %s ].\n", gun_index, ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag);
  685. }
  686. DEBUG_INFO("==================================================\n");
  687. }
  688. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  689. {
  690. 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))) ||
  691. ((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))))
  692. result = PASS;
  693. }
  694. break;
  695. case SYS_MODE_CHARGING:
  696. case SYS_MODE_TERMINATING:
  697. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  698. {
  699. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
  700. (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag)==0) &&
  701. (strlen((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag)>0))
  702. result = PASS;
  703. DEBUG_INFO("==== [SYS_MODE_CHARGING/SYS_MODE_TERMINATING] ====\n");
  704. DEBUG_INFO("Authorize response status: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  705. DEBUG_INFO("Authorize response ParentIdTag: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  706. DEBUG_INFO("Gun-%d StartTransaction response ParentIdTag: [ %s ]. \n", gun_index, ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag);
  707. DEBUG_INFO("==================================================\n");
  708. }
  709. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  710. {
  711. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) &&
  712. ((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.groupIdToken.idToken) == 0) ||
  713. (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_MasterPassGroupId].variableAttribute[0].value) == 0)))
  714. result = PASS;
  715. }
  716. break;
  717. case SYS_MODE_RESERVATION:
  718. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  719. {
  720. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
  721. (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag)==0))
  722. result = PASS;
  723. DEBUG_INFO("============ [SYS_MODE_RESERVATION] ==============\n");
  724. DEBUG_INFO("Authorize response status: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  725. DEBUG_INFO("Authorize response ParentIdTag: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  726. DEBUG_INFO("Gun-%d Reserve ParentIdTag: [ %s ]. \n", gun_index, ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag);
  727. DEBUG_INFO("==================================================\n");
  728. }
  729. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  730. {
  731. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) &&
  732. (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].groupIdToken.idToken)==0))
  733. result = PASS;
  734. }
  735. break;
  736. default:
  737. break;
  738. }
  739. return result;
  740. }
  741. uint8_t ocpp_get_unlocker_req(uint8_t gun_index)
  742. {
  743. uint8_t result = OFF;
  744. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  745. {
  746. result = ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor;
  747. }
  748. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  749. {
  750. result = ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor;
  751. }
  752. return result;
  753. }
  754. void ocpp_set_unlocker_req(uint8_t gun_index, uint8_t status)
  755. {
  756. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  757. {
  758. if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor != status)
  759. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = status;
  760. }
  761. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  762. {
  763. if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor != status)
  764. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = status;
  765. }
  766. }
  767. void ocpp_set_starttransaction_req(uint8_t gun_index, uint8_t status)
  768. {
  769. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  770. {
  771. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq != status)
  772. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = status;
  773. }
  774. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  775. {
  776. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  777. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  778. }
  779. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  780. {
  781. if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq != status)
  782. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = status;
  783. }
  784. }
  785. void ocpp_set_starttransaction_conf(uint8_t gun_index, uint8_t status)
  786. {
  787. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  788. {
  789. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf != status)
  790. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = status;
  791. }
  792. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  793. {
  794. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  795. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  796. }
  797. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  798. {
  799. if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf != status)
  800. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = status;
  801. }
  802. }
  803. void ocpp_set_stoptransaction_req(uint8_t gun_index, uint8_t status)
  804. {
  805. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  806. {
  807. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq != status)
  808. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = status;
  809. }
  810. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  811. {
  812. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  813. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  814. }
  815. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  816. {
  817. if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq != status)
  818. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = status;
  819. }
  820. }
  821. void ocpp_set_stoptransaction_conf(uint8_t gun_index, uint8_t status)
  822. {
  823. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  824. {
  825. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf != status)
  826. {
  827. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = status;
  828. }
  829. }
  830. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  831. {
  832. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  833. {
  834. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  835. }
  836. }
  837. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  838. {
  839. if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionConf != status)
  840. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionConf = status;
  841. }
  842. }
  843. void ocpp_copy_userid_to_starttransaction(uint8_t gun_index)
  844. {
  845. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  846. {
  847. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  848. }
  849. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  850. {
  851. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  852. }
  853. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  854. {
  855. memcpy((char*)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16DataPH->StartTransaction[gun_index].IdTag));
  856. }
  857. }
  858. uint8_t ocpp_get_starttransaction_result(uint8_t gun_index)
  859. {
  860. uint8_t result = PASS;
  861. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  862. {
  863. if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  864. {
  865. if((strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Blocked")==0) ||
  866. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Expired")==0) ||
  867. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid")==0))
  868. result = NO;
  869. }
  870. }
  871. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  872. {
  873. if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "true"))
  874. {
  875. if((strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Blocked")==0) ||
  876. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Expired")==0) ||
  877. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Invalid")==0) ||
  878. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NoCredit")==0) ||
  879. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAllowedTypeEVSE")==0) ||
  880. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisLocation")==0) ||
  881. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisTime")==0) ||
  882. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Unknown")==0))
  883. result = NO;
  884. }
  885. }
  886. return result;
  887. }
  888. uint8_t ocpp_get_maxcharging_profileId()
  889. {
  890. uint8_t result = 0;
  891. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  892. {
  893. result = ShmOCPP16Data->MaxChargingProfile.ChargingProfileId;
  894. }
  895. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  896. {
  897. result = ShmOCPP20Data->MaxChargingProfile.id;
  898. }
  899. return result;
  900. }
  901. uint8_t ocpp_get_smartcharging_profileId(uint8_t gun_index)
  902. {
  903. uint8_t result = 0;
  904. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  905. {
  906. result = ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId;
  907. }
  908. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  909. {
  910. result = ShmOCPP20Data->SmartChargingProfile[gun_index].id;
  911. }
  912. return result;
  913. }
  914. void ocpp_reset_smartcharging_profileId(uint8_t gun_index)
  915. {
  916. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  917. {
  918. ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
  919. }
  920. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  921. {
  922. ShmOCPP20Data->SmartChargingProfile[gun_index].id = 0;
  923. }
  924. }
  925. uint8_t ocpp_get_profile_req(uint8_t gun_index)
  926. {
  927. uint8_t result = OFF;
  928. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  929. {
  930. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  931. }
  932. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  933. {
  934. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  935. }
  936. return result;
  937. }
  938. void ocpp_set_profile_req(uint8_t gun_index, uint8_t status)
  939. {
  940. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  941. {
  942. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  943. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  944. }
  945. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  946. {
  947. if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  948. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  949. }
  950. }
  951. uint8_t ocpp_get_profile_conf(uint8_t gun_index)
  952. {
  953. uint8_t result = OFF;
  954. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  955. {
  956. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  957. }
  958. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  959. {
  960. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  961. }
  962. return result;
  963. }
  964. void ocpp_set_profile_conf(uint8_t gun_index, uint8_t status)
  965. {
  966. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  967. {
  968. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  969. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  970. }
  971. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  972. {
  973. if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  974. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  975. }
  976. }
  977. uint8_t ocpp_get_StopTransactionOnEVSideDisconnect()
  978. {
  979. uint8_t result = OFF;
  980. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  981. {
  982. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)
  983. result = ON;
  984. }
  985. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  986. {
  987. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnEVSideDisconnect].variableAttribute[0].value, "true") == 0)
  988. result = ON;
  989. }
  990. return result;
  991. }
  992. uint8_t ocpp_get_cancelreservation_req(uint8_t gun_index)
  993. {
  994. uint8_t result = OFF;
  995. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  996. {
  997. result = ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq;
  998. }
  999. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1000. {
  1001. result = ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq;
  1002. }
  1003. return result;
  1004. }
  1005. void ocpp_set_cancelreservation_req(uint8_t gun_index, uint8_t status)
  1006. {
  1007. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1008. {
  1009. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  1010. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  1011. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  1012. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  1013. }
  1014. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1015. {
  1016. if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  1017. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  1018. if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  1019. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  1020. }
  1021. }
  1022. uint8_t ocpp_compare_reserve_id_with_user(uint8_t gun_index)
  1023. {
  1024. uint8_t result = OFF;
  1025. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1026. {
  1027. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  1028. result = ON;
  1029. }
  1030. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1031. {
  1032. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  1033. result = ON;
  1034. }
  1035. return result;
  1036. }
  1037. uint8_t ocpp_compare_reserve_id_with_remote_user(uint8_t gun_index)
  1038. {
  1039. uint8_t result = OFF;
  1040. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1041. {
  1042. if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  1043. {
  1044. result = ON;
  1045. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  1046. }
  1047. }
  1048. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1049. {
  1050. if(strcmp((char*)ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  1051. {
  1052. result = ON;
  1053. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  1054. }
  1055. }
  1056. return result;
  1057. }
  1058. uint8_t ocpp_isAuthorizeRemoteStart()
  1059. {
  1060. uint8_t result = NO;
  1061. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1062. {
  1063. if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  1064. {
  1065. result = YES;
  1066. }
  1067. }
  1068. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1069. {
  1070. if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "true"))
  1071. {
  1072. result = YES;
  1073. }
  1074. }
  1075. return result;
  1076. }
  1077. uint8_t ocpp_get_freevend_idtag(uint8_t * userId)
  1078. {
  1079. uint8_t result = NO;
  1080. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1081. {
  1082. strcpy((char*)userId, (char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
  1083. result = YES;
  1084. }
  1085. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1086. {
  1087. strcpy((char*)userId, (char*)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value);
  1088. result = YES;
  1089. }
  1090. return result;
  1091. }
  1092. //======================================================
  1093. // Check interface status
  1094. //======================================================
  1095. int isInterfaceUp(const char *interface)
  1096. {
  1097. int result = FAIL;
  1098. FILE *fp;
  1099. char cmd[256];
  1100. char buf[512];
  1101. strcpy(cmd, "ifconfig");;
  1102. fp = popen(cmd, "r");
  1103. if(fp != NULL)
  1104. {
  1105. while(fgets(buf, sizeof(buf), fp) != NULL)
  1106. {
  1107. if(strstr(buf, interface) > 0)
  1108. {
  1109. result = PASS;
  1110. }
  1111. }
  1112. }
  1113. pclose(fp);
  1114. return result;
  1115. }
  1116. //======================================================
  1117. // Create all share memory
  1118. //======================================================
  1119. int CreatShareMemory()
  1120. {
  1121. int result = PASS;
  1122. int MeterSMId;
  1123. //creat ShmSysConfigAndInfo
  1124. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  1125. {
  1126. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1127. result = FAIL;
  1128. }
  1129. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1130. {
  1131. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  1132. result = FAIL;
  1133. }
  1134. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  1135. //creat ShmStatusCodeData
  1136. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  1137. {
  1138. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1139. result = FAIL;
  1140. }
  1141. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1142. {
  1143. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1144. result = FAIL;
  1145. }
  1146. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  1147. //creat ShmPsuData
  1148. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  1149. {
  1150. DEBUG_ERROR("shmget ShmPsuData NG\n");
  1151. result = FAIL;
  1152. }
  1153. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1154. {
  1155. DEBUG_ERROR("shmat ShmPsuData NG\n");
  1156. result = FAIL;
  1157. }
  1158. memset(ShmPsuData,0,sizeof(struct PsuData));
  1159. //creat ShmCHAdeMOData
  1160. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  1161. {
  1162. DEBUG_ERROR("shmget ShmCHAdeMOData NG1\n");
  1163. result = FAIL;
  1164. }
  1165. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1166. {
  1167. DEBUG_ERROR("shmat ShmCHAdeMOData NG2\n");
  1168. result = FAIL;
  1169. }
  1170. memset(ShmCHAdeMOData,0,sizeof(struct CHAdeMOData));
  1171. //creat ShmCcsData
  1172. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  1173. {
  1174. DEBUG_ERROR("shmget ShmCcsData NG\n");
  1175. result = FAIL;
  1176. }
  1177. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1178. {
  1179. DEBUG_ERROR("shmat ShmCcsData NG\n");
  1180. result = FAIL;
  1181. }
  1182. memset(ShmCcsData,0,sizeof(struct CcsData));
  1183. //creat ShmPrimaryMcuData
  1184. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  1185. {
  1186. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1187. result = FAIL;
  1188. }
  1189. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1190. {
  1191. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1192. result = FAIL;
  1193. }
  1194. memset(ShmPrimaryMcuData,0,sizeof(struct PrimaryMcuData));
  1195. /*
  1196. //creat ShmFanModuleData
  1197. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  1198. {
  1199. DEBUG_ERROR("shmget ShmFanModuleData NG\n");
  1200. result = FAIL;
  1201. }
  1202. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1203. {
  1204. DEBUG_ERROR("shmat ShmFanModuleData NG\n");
  1205. result = FAIL;
  1206. }
  1207. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  1208. //creat ShmRelayModuleData
  1209. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  1210. {
  1211. DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
  1212. result = FAIL;
  1213. }
  1214. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1215. {
  1216. DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
  1217. result = FAIL;
  1218. }
  1219. memset(ShmRelayModuleData,0,sizeof(struct RelayModuleData));*/
  1220. //creat ShmOCPP16Data
  1221. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  1222. {
  1223. DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
  1224. result = FAIL;
  1225. }
  1226. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1227. {
  1228. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  1229. result = FAIL;
  1230. }
  1231. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  1232. //creat ShmOCPP20Data
  1233. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  1234. {
  1235. DEBUG_ERROR("shmget OCPP20Data NG\n");
  1236. result = FAIL;
  1237. }
  1238. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1239. {
  1240. DEBUG_ERROR("shmat OCPP20Data NG\n");
  1241. result = FAIL;
  1242. }
  1243. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  1244. //creat ShmCharger
  1245. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  1246. {
  1247. DEBUG_ERROR("shmget ShmCharger NG\n");
  1248. result = FAIL;
  1249. }
  1250. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1251. {
  1252. DEBUG_ERROR("shmat ShmCharger NG\n");
  1253. result = FAIL;
  1254. }
  1255. memset(ShmCharger,0,sizeof(struct Charger));
  1256. //creat ShmOCPP16DataPH
  1257. if ((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT|0777)) < 0)
  1258. {
  1259. DEBUG_ERROR("shmget ShmOCPP16DataPH NG\n");
  1260. result = FAIL;
  1261. }
  1262. else if ((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1263. {
  1264. DEBUG_ERROR("shmat ShmOCPP16DataPH NG\n");
  1265. result = FAIL;
  1266. }
  1267. else
  1268. {}
  1269. return result;
  1270. }
  1271. //===============================================
  1272. // SQLite3 related routine
  1273. //===============================================
  1274. int DB_Open(sqlite3 *db)
  1275. {
  1276. int result = PASS;
  1277. char* errMsg = NULL;
  1278. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  1279. "idx integer primary key AUTOINCREMENT, "
  1280. "reservationId text, "
  1281. "transactionId text, "
  1282. "startMethod text, "
  1283. "userId text, "
  1284. "dateTimeStart text, "
  1285. "dateTimeStop text,"
  1286. "socStart text, "
  1287. "socStop text, "
  1288. "chargeEnergy text, "
  1289. "stopReason text"
  1290. ");";
  1291. char* createRecordBufSql="CREATE TABLE IF NOT EXISTS charging_record_buffer("
  1292. "reservationId text, "
  1293. "transactionId text, "
  1294. "startMethod text, "
  1295. "userId text, "
  1296. "dateTimeStart text, "
  1297. "dateTimeStop text,"
  1298. "socStart text, "
  1299. "socStop text, "
  1300. "chargeEnergy text, "
  1301. "stopReason text"
  1302. ");";
  1303. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  1304. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  1305. "`item` TEXT NOT NULL, "
  1306. "`connector` INTEGER NOT NULL, "
  1307. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  1308. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  1309. if(sqlite3_open(DB_FILE, &db))
  1310. {
  1311. result = FAIL;
  1312. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  1313. sqlite3_close(db);
  1314. }
  1315. else
  1316. {
  1317. DEBUG_INFO( "Local charging record database open successfully.\n");
  1318. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  1319. {
  1320. result = FAIL;
  1321. DEBUG_ERROR( "Create local charging record table error message: %s\n", errMsg);
  1322. }
  1323. else
  1324. {
  1325. DEBUG_INFO( "Opened local charging record table successfully\n");
  1326. }
  1327. if (sqlite3_exec(db, createRecordBufSql, 0, 0, &errMsg) != SQLITE_OK)
  1328. {
  1329. result = FAIL;
  1330. DEBUG_ERROR( "Create local charging record buffer table error message: %s\n", errMsg);
  1331. }
  1332. else
  1333. {
  1334. DEBUG_INFO( "Opened local charging record buffer table successfully\n");
  1335. }
  1336. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  1337. {
  1338. result = FAIL;
  1339. DEBUG_ERROR( "Create local config table error message: %s\n", errMsg);
  1340. }
  1341. else
  1342. {
  1343. DEBUG_INFO( "Opened local config table successfully\n");
  1344. }
  1345. sqlite3_close(db);
  1346. }
  1347. return result;
  1348. }
  1349. int DB_Open_OCPP20(sqlite3 *db)
  1350. {
  1351. int result = PASS;
  1352. char* errMsg = NULL;
  1353. char *sqlOcppVariable = "create table if not exists ocpp20_variable (idx integer primary key,"
  1354. "componentName text NOT NULL ,"
  1355. "componentInstance text, "
  1356. "variableName text NOT NULL, "
  1357. "variableInstance text, "
  1358. "variableCharacteristicsDataType integer, "
  1359. "variableCharacteristicsUnit text, "
  1360. "variableCharacteristicsMaxLimit real,"
  1361. "variableAttributesType text, "
  1362. "variableAttributesTypeMutability text, "
  1363. "variableAttributesTypeValue text, "
  1364. "variableCharacteristicsMinLimit real,"
  1365. "unique(componentName, componentInstance,variableName, variableInstance) on conflict replace);";
  1366. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  1367. if(sqlite3_open(DB_FILE_OCPP_LOCAL, &db))
  1368. {
  1369. result = FAIL;
  1370. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  1371. sqlite3_close(db);
  1372. }
  1373. else
  1374. {
  1375. DEBUG_INFO( "OCPP local database open successfully.\n");
  1376. if(sqlite3_exec(db, sqlOcppVariable, 0, 0, &errMsg) != SQLITE_OK)
  1377. {
  1378. result = FAIL;
  1379. DEBUG_ERROR( "Create OCPP 2.0 variable table error message: %s\n", errMsg);
  1380. }
  1381. else
  1382. {
  1383. DEBUG_INFO( "Create OCPP 2.0 variable table successfully\n");
  1384. }
  1385. sqlite3_close(db);
  1386. }
  1387. return result;
  1388. }
  1389. int DB_variableClear(sqlite3 *db)
  1390. {
  1391. int result = PASS;
  1392. //char * sqlcleanLocalList = "delete from ocpp20_variable";
  1393. char * sqlcleanLocalList = "drop table ocpp20_variable";
  1394. char *errMsg = 0;
  1395. if(sqlite3_open(DB_FILE_OCPP_LOCAL, &db))
  1396. {
  1397. result = FAIL;
  1398. DEBUG_ERROR( "Can't open database for ocpp 2.0 variable: %s\n", sqlite3_errmsg(db));
  1399. sqlite3_close(db);
  1400. }
  1401. else
  1402. {
  1403. DEBUG_INFO("Command: &s\n", sqlcleanLocalList);
  1404. if(sqlite3_exec(db, sqlcleanLocalList, 0, 0, &errMsg) != SQLITE_OK)
  1405. {
  1406. DEBUG_INFO("%s\n", errMsg);
  1407. DEBUG_INFO("Drop table failed.\n");
  1408. result = FAIL;
  1409. }
  1410. DEBUG_INFO("Drop table succeed.\n");
  1411. }
  1412. return result;
  1413. }
  1414. int DB_Insert_Record(sqlite3 *db, int gun_index)
  1415. {
  1416. int result = PASS;
  1417. char* errMsg = NULL;
  1418. char sqlStr[1024];
  1419. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1420. {
  1421. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  1422. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  1423. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1424. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  1425. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1426. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1427. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1428. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1429. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1430. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1431. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1432. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  1433. }
  1434. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1435. {
  1436. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  1437. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  1438. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1439. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  1440. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1441. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1442. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1443. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1444. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1445. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1446. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1447. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  1448. }
  1449. if(sqlite3_open(DB_FILE, &db))
  1450. {
  1451. result = FAIL;
  1452. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1453. sqlite3_close(db);
  1454. }
  1455. else
  1456. {
  1457. DEBUG_INFO( "Local charging record database open successfully.\n");
  1458. // Delete buffer
  1459. if(sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1460. {
  1461. result = FAIL;
  1462. DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
  1463. }
  1464. else
  1465. {
  1466. DEBUG_INFO( "Delete local charging record buffer successfully\n");
  1467. }
  1468. // Insert charging record
  1469. if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1470. {
  1471. result = FAIL;
  1472. DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
  1473. }
  1474. else
  1475. {
  1476. DEBUG_INFO( "Insert local charging record successfully\n");
  1477. }
  1478. 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));
  1479. if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1480. {
  1481. result = FAIL;
  1482. DEBUG_INFO( "delete local charging error message: %s\n", errMsg);
  1483. }
  1484. else
  1485. {
  1486. DEBUG_INFO( "delete local charging record successfully\n");
  1487. }
  1488. sqlite3_close(db);
  1489. }
  1490. return result;
  1491. }
  1492. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
  1493. {
  1494. uint8_t result = false;
  1495. char* errMsg = NULL;
  1496. char sqlStr[1024];
  1497. srand(time(NULL));
  1498. if(sqlite3_open(DB_FILE, &db))
  1499. {
  1500. result = FAIL;
  1501. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1502. sqlite3_close(db);
  1503. }
  1504. else
  1505. {
  1506. DEBUG_INFO( "Local charging record database open successfully.\n");
  1507. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);", gun_index, isOperactive);
  1508. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1509. if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1510. {
  1511. result = FAIL;
  1512. DEBUG_INFO( "update config error message: %s\n", errMsg);
  1513. }
  1514. else
  1515. {
  1516. DEBUG_INFO("update connector-%d config item isOperactive to %d\n", gun_index, isOperactive);
  1517. }
  1518. sqlite3_close(db);
  1519. }
  1520. return result;
  1521. }
  1522. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  1523. {
  1524. uint8_t result = true;
  1525. char* errMsg = NULL;
  1526. char sqlStr[1024];
  1527. char **rs;
  1528. int rows, cols;
  1529. sprintf(sqlStr, "select * from config where item='isOperactive' and connector=%d;", gun_index);
  1530. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1531. if(sqlite3_open(DB_FILE, &db))
  1532. {
  1533. result = FAIL;
  1534. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1535. sqlite3_close(db);
  1536. }
  1537. else
  1538. {
  1539. DEBUG_INFO( "Local config query database open successfully.\n");
  1540. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1541. if(rows>0)
  1542. {
  1543. for(int idxRow=1;idxRow<=rows;idxRow++)
  1544. {
  1545. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  1546. {
  1547. result = false;
  1548. }
  1549. DEBUG_INFO("Query connector-%d isOperactive: %s\n", gun_index, rs[(idxRow*cols)+3]);
  1550. }
  1551. }
  1552. else
  1553. {
  1554. DEBUG_INFO("Query connector-%d fail, default value as operactive.\n", gun_index);
  1555. }
  1556. sqlite3_free_table(rs);
  1557. sqlite3_close(db);
  1558. }
  1559. return result;
  1560. }
  1561. //======================================================
  1562. // Peripheral initial
  1563. //======================================================
  1564. int InitWatchDog()
  1565. {
  1566. int fd;
  1567. int timeout = 180;
  1568. system("/usr/bin/fuser -k /dev/watchdog");
  1569. sleep(1);
  1570. system("echo V > /dev/watchdog");
  1571. sleep(1);
  1572. fd=open("/dev/watchdog", O_RDWR);
  1573. if(fd<=0)
  1574. {
  1575. DEBUG_ERROR("System watch dog initial fail.\n");
  1576. }
  1577. ioctl(fd, _IOWR('W', 6, int), &timeout);
  1578. return fd;
  1579. }
  1580. void InitGPIO()
  1581. {
  1582. /*****************0~3, 4 bank, bank x 32+ num*********************/
  1583. /***************************************************************/
  1584. /*************** INPUT PIN ***************************************/
  1585. /***************************************************************/
  1586. /***************************************************************/
  1587. /*************** OUTPUT PIN ************************************/
  1588. /***************************************************************/
  1589. /*MCU request:GPIO3_20 => H:ON; L:OFF*/
  1590. system("echo 116 > /sys/class/gpio/export");
  1591. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1592. system("echo 0 > /sys/class/gpio/gpio116/value");
  1593. /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
  1594. system("echo 19 > /sys/class/gpio/export");
  1595. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  1596. system("echo 1 > /sys/class/gpio/gpio19/value");
  1597. /*Speaker:GPIO2_1 => H:ON; L:OFF*/
  1598. system("echo 65 > /sys/class/gpio/export");
  1599. system("echo \"out\" > /sys/class/gpio/gpio65/direction");
  1600. system("echo 0 > /sys/class/gpio/gpio65/value");
  1601. /*(FOR ACCSU-01-RW) 4G POWER RST:GPIO3_14 => H:ON; L:OFF*/
  1602. system("echo 110 > /sys/class/gpio/export");
  1603. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  1604. system("echo 1 > /sys/class/gpio/gpio110/value");
  1605. sleep(3);
  1606. system("echo 0 > /sys/class/gpio/gpio110/value");
  1607. /*(FOR ACCSU3P5-01-RW) 4G POWER RST:GPIO3_18 => H:ON; L:OFF*/
  1608. system("echo 114 > /sys/class/gpio/export");
  1609. system("echo \"out\" > /sys/class/gpio/gpio114/direction");
  1610. system("echo 1 > /sys/class/gpio/gpio114/value");
  1611. sleep(3);
  1612. system("echo 0 > /sys/class/gpio/gpio114/value");
  1613. DEBUG_INFO("Initial GPIO OK\n");
  1614. }
  1615. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1616. {
  1617. int fd,wrd;
  1618. unsigned char *buf;
  1619. unsigned int ChkSum,ChkSumOrg;
  1620. if((buf=malloc(MtdBlockSize))==NULL)
  1621. {
  1622. DEBUG_ERROR("malloc buffer NG,rebooting..\n");
  1623. if(ShmStatusCodeData!=NULL)
  1624. {
  1625. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1626. }
  1627. sleep(5);
  1628. system("reboot -f");
  1629. sleep(5);
  1630. system("reboot -f");
  1631. }
  1632. memset(buf, 0, MtdBlockSize);
  1633. //================================================
  1634. // Load configuration from mtdblock10
  1635. //================================================
  1636. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1637. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1638. if (fd < 0)
  1639. {
  1640. free(buf);
  1641. DEBUG_ERROR("open mtdblock10 NG,rebooting..\n");
  1642. if(ShmStatusCodeData!=NULL)
  1643. {
  1644. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1645. }
  1646. sleep(5);
  1647. system("reboot -f");
  1648. sleep(5);
  1649. system("reboot -f");
  1650. }
  1651. wrd=read(fd, buf, MtdBlockSize);
  1652. close(fd);
  1653. if(wrd<MtdBlockSize)
  1654. {
  1655. free(buf);
  1656. DEBUG_ERROR("read SysConfigData data NG,rebooting..\n");
  1657. if(ShmStatusCodeData!=NULL)
  1658. {
  1659. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1660. }
  1661. sleep(5);
  1662. system("reboot -f");
  1663. sleep(5);
  1664. system("reboot -f");
  1665. }
  1666. ChkSum=0;
  1667. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1668. {
  1669. ChkSum+=buf[wrd];
  1670. }
  1671. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1672. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1673. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1674. //================================================
  1675. // Load configuration from mtdblock11
  1676. //================================================
  1677. if(ChkSum!=ChkSumOrg)
  1678. {
  1679. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  1680. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1681. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1682. if (fd < 0)
  1683. {
  1684. free(buf);
  1685. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
  1686. if(ShmStatusCodeData!=NULL)
  1687. {
  1688. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1689. }
  1690. sleep(5);
  1691. system("reboot -f");
  1692. sleep(5);
  1693. system("reboot -f");
  1694. }
  1695. memset(buf, 0, MtdBlockSize);
  1696. wrd=read(fd, buf,MtdBlockSize);
  1697. close(fd);
  1698. if(wrd<MtdBlockSize)
  1699. {
  1700. free(buf);
  1701. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
  1702. if(ShmStatusCodeData!=NULL)
  1703. {
  1704. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1705. }
  1706. sleep(5);
  1707. system("reboot -f");
  1708. sleep(5);
  1709. system("reboot -f");
  1710. }
  1711. ChkSum=0;
  1712. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1713. {
  1714. ChkSum+=buf[wrd];
  1715. }
  1716. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1717. //================================================
  1718. // Load configuration from mtdblock12 (Factory default)
  1719. //================================================
  1720. if(ChkSum!=ChkSumOrg)
  1721. {
  1722. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
  1723. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1724. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1725. if (fd < 0)
  1726. {
  1727. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
  1728. free(buf);
  1729. if(ShmStatusCodeData!=NULL)
  1730. {
  1731. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1732. }
  1733. sleep(5);
  1734. system("reboot -f");
  1735. sleep(5);
  1736. system("reboot -f");
  1737. }
  1738. memset(buf, 0, MtdBlockSize);
  1739. wrd=read(fd, buf,MtdBlockSize);
  1740. close(fd);
  1741. if(wrd<MtdBlockSize)
  1742. {
  1743. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
  1744. free(buf);
  1745. if(ShmStatusCodeData!=NULL)
  1746. {
  1747. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1748. }
  1749. sleep(5);
  1750. system("reboot -f");
  1751. sleep(5);
  1752. system("reboot -f");
  1753. }
  1754. ChkSum=0;
  1755. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1756. {
  1757. ChkSum+=buf[wrd];
  1758. }
  1759. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1760. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1761. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1762. if(ChkSum!=ChkSumOrg)
  1763. {
  1764. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
  1765. free(buf);
  1766. system("cd /root;./Module_FactoryConfig -m");
  1767. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1768. system("sync");
  1769. sleep(5);
  1770. system("reboot -f");
  1771. sleep(5);
  1772. system("reboot -f");
  1773. return FAIL;
  1774. }
  1775. }
  1776. }
  1777. //load OK
  1778. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1779. free(buf);
  1780. system("rm -f /mnt/EvseConfig.bin");
  1781. // SysConfig in flash is empty (0xffffffff)
  1782. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1783. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1784. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1785. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1786. {
  1787. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1788. {
  1789. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1790. }
  1791. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1792. {
  1793. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1794. }
  1795. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1796. {
  1797. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1798. }
  1799. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1800. {
  1801. DEBUG_INFO("Ethernet dhcp config is null.\n");
  1802. }
  1803. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
  1804. {
  1805. DEBUG_INFO("Model name over length.\n");
  1806. }
  1807. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
  1808. {
  1809. DEBUG_INFO("Model serial number over length.\n");
  1810. }
  1811. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
  1812. {
  1813. DEBUG_INFO("SystemId over length.\n");
  1814. }
  1815. system("cd /root;./Module_FactoryConfig -m");
  1816. sleep(3);
  1817. system("/usr/bin/run_evse_restart.sh");
  1818. }
  1819. DEBUG_INFO("Load SysConfigData OK\n");
  1820. RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo);
  1821. return PASS;
  1822. }
  1823. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1824. {
  1825. int result = PASS;
  1826. int fd,wrd;
  1827. unsigned int i,Chk;
  1828. unsigned char *ptr, *BufTmp;
  1829. Chk=0;
  1830. ptr=(unsigned char *)UsrData;
  1831. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  1832. {
  1833. memset(BufTmp,0,MtdBlockSize);
  1834. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  1835. for(i=0;i<MtdBlockSize-4;i++)
  1836. Chk+=*(BufTmp+i);
  1837. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  1838. // Output configuration to file.
  1839. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  1840. if (fd < 0)
  1841. {
  1842. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  1843. free(BufTmp);
  1844. return 0;
  1845. }
  1846. wrd=write(fd, BufTmp, MtdBlockSize);
  1847. close(fd);
  1848. if(wrd<MtdBlockSize)
  1849. {
  1850. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  1851. free(BufTmp);
  1852. return 0;
  1853. }
  1854. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  1855. DEBUG_INFO("Erase /dev/mtd10.\n");
  1856. runShellCmd("flash_erase /dev/mtd10 0 0");
  1857. DEBUG_INFO("Write /dev/mtd10.\n");
  1858. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  1859. DEBUG_INFO("Erase /dev/mtd11.\n");
  1860. runShellCmd("flash_erase /dev/mtd11 0 0");
  1861. DEBUG_INFO("Write /dev/mtd11.\n");
  1862. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  1863. system("rm -f /mnt/EvseConfig.bin");
  1864. DEBUG_INFO("EvseConfig write to flash OK\n");
  1865. }
  1866. else
  1867. {
  1868. DEBUG_ERROR("alloc BlockSize NG\r\n");
  1869. result = FAIL;
  1870. }
  1871. if(BufTmp!=NULL)
  1872. free(BufTmp);
  1873. return result;
  1874. }
  1875. void InitEthernet()
  1876. {
  1877. pid_t pid;
  1878. uint8_t cnt_pingDNS_Fail;
  1879. char tmpbuf[256];
  1880. unsigned int natInterface = 0;
  1881. // Detele bridge interface
  1882. system("/sbin/ifconfig uap0 down");
  1883. system("/sbin/ifconfig br0 down");
  1884. system("/usr/sbin/brctl delbr br0");
  1885. //Init Eth0 for internet
  1886. memset(tmpbuf,0,256);
  1887. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1888. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1889. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1890. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1891. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1892. system(tmpbuf);
  1893. memset(tmpbuf,0,256);
  1894. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1895. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1896. system(tmpbuf);
  1897. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  1898. system("ifconfig lo up &");
  1899. system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  1900. if(isInterfaceUp("eth1")==PASS)
  1901. {
  1902. //Init Eth1 for administrator tool
  1903. memset(tmpbuf,0,256);
  1904. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up &",
  1905. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1906. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  1907. system(tmpbuf);
  1908. }
  1909. //Run DHCP client if enabled
  1910. system("killall udhcpc");
  1911. system("rm -rf /etc/resolv.conf");
  1912. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  1913. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1914. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  1915. {
  1916. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1917. system(tmpbuf);
  1918. }
  1919. // Upgrade system id to /etc/hostname
  1920. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1921. system(tmpbuf);
  1922. // Ethernet MAC address
  1923. getEth0MacAddress();
  1924. //check internet status
  1925. pid = fork();
  1926. if(pid == 0)
  1927. {
  1928. for(;;)
  1929. {
  1930. if(isRouteFail())
  1931. {
  1932. //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  1933. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1934. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1935. {
  1936. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1937. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1938. system(tmpbuf);
  1939. }
  1940. else
  1941. {
  1942. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1943. memset(tmpbuf,0,256);
  1944. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1945. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1946. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1947. system(tmpbuf);
  1948. memset(tmpbuf,0,256);
  1949. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1950. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1951. system(tmpbuf);
  1952. }
  1953. }
  1954. if(isReachableInternet() == PASS)
  1955. {
  1956. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  1957. cnt_pingDNS_Fail = 0;
  1958. }
  1959. else
  1960. {
  1961. if(cnt_pingDNS_Fail >= 3)
  1962. {
  1963. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  1964. if(!ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  1965. {
  1966. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1967. {
  1968. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1969. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1970. system(tmpbuf);
  1971. }
  1972. else
  1973. {
  1974. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1975. memset(tmpbuf,0,256);
  1976. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1977. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1978. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1979. system(tmpbuf);
  1980. memset(tmpbuf,0,256);
  1981. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1982. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1983. system(tmpbuf);
  1984. }
  1985. cnt_pingDNS_Fail = 0;
  1986. }
  1987. }
  1988. else
  1989. {
  1990. cnt_pingDNS_Fail++;
  1991. }
  1992. }
  1993. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  1994. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  1995. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) &&
  1996. (ShmOCPP16Data->OcppConnStatus != PASS))
  1997. {
  1998. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  1999. }
  2000. else
  2001. {
  2002. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  2003. }
  2004. //============================================================
  2005. // Priority for internet 0 : First / 1 : Second / 2: Third
  2006. //============================================================
  2007. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  2008. {
  2009. system("/sbin/ifmetric eth0 0");
  2010. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2011. {
  2012. system("/sbin/ifmetric mlan0 1");
  2013. }
  2014. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2015. {
  2016. system("/sbin/ifmetric ppp0 2");
  2017. }
  2018. if(isKernelSupportNAT() == YES)
  2019. {
  2020. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2021. {
  2022. if(natInterface != 1)
  2023. {
  2024. system("/sbin/iptables -t nat -F");
  2025. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o eth0 -j MASQUERADE");
  2026. natInterface = 1;
  2027. }
  2028. }
  2029. else
  2030. {
  2031. system("/sbin/iptables -t nat -F");
  2032. }
  2033. }
  2034. }
  2035. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  2036. {
  2037. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2038. {
  2039. system("/sbin/ifmetric eth0 1");
  2040. system("/sbin/ifmetric mlan0 0");
  2041. }
  2042. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2043. {
  2044. system("/sbin/ifmetric ppp0 2");
  2045. }
  2046. }
  2047. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  2048. {
  2049. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2050. {
  2051. system("/sbin/ifmetric mlan0 2");
  2052. }
  2053. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2054. {
  2055. system("/sbin/ifmetric eth0 1");
  2056. system("/sbin/ifmetric ppp0 0");
  2057. }
  2058. if(isKernelSupportNAT() == YES)
  2059. {
  2060. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2061. {
  2062. if(natInterface != 2)
  2063. {
  2064. system("/sbin/iptables -t nat -F");
  2065. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o ppp0 -j MASQUERADE");
  2066. natInterface = 2;
  2067. }
  2068. }
  2069. else
  2070. {
  2071. system("/sbin/iptables -t nat -F");
  2072. }
  2073. }
  2074. }
  2075. else
  2076. {
  2077. if(isKernelSupportNAT() == YES)
  2078. {
  2079. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2080. {
  2081. system("/sbin/iptables -t nat -F");
  2082. natInterface = 0;
  2083. }
  2084. }
  2085. }
  2086. // Check is need to reset WIFI/4G module power
  2087. if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus) &&
  2088. ((system("pidof -s Module_Wifi > /dev/null") != 0) ||
  2089. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) ||
  2090. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 1) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == ON)) ||
  2091. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi == ON))) &&
  2092. ((system("pidof -s Module_4g > /dev/null") != 0) ||
  2093. (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) ||
  2094. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 1) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == ON))))
  2095. {
  2096. if(DiffTimebWithNow(startTime[0][TMR_IDX_RESET_4G_WIFI_POWER]) > TIMEOUT_SPEC_RESET_4G_WIFI_POWER)
  2097. {
  2098. if((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != 0) || (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled != 0))
  2099. {
  2100. DEBUG_WARN("WIFI/4G module reset.\n");
  2101. // FOR ACCSU-01-RW RESET 4G
  2102. system("echo 1 > /sys/class/gpio/gpio110/value");
  2103. // FOR ACCSU3P5-01-RW RESET 4G
  2104. system("echo 1 > /sys/class/gpio/gpio114/value");
  2105. sleep(3);
  2106. // FOR ACCSU-01-RW RESET 4G
  2107. system("echo 0 > /sys/class/gpio/gpio110/value");
  2108. // FOR ACCSU3P5-01-RW RESET 4G
  2109. system("echo 0 > /sys/class/gpio/gpio114/value");
  2110. }
  2111. ftime(&startTime[0][TMR_IDX_RESET_4G_WIFI_POWER]);
  2112. }
  2113. }
  2114. else
  2115. {
  2116. ftime(&startTime[0][TMR_IDX_RESET_4G_WIFI_POWER]);
  2117. }
  2118. // Bridge ethernet to uap0 to get dynamic ip address
  2119. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing &&
  2120. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) &&
  2121. (isUap0up() == PASS) &&
  2122. (access("/sys/class/net/br0/address", F_OK) == -1))
  2123. {
  2124. sleep(10);
  2125. system("/usr/sbin/brctl addbr br0");
  2126. system("/usr/sbin/brctl addif br0 eth0");
  2127. system("/usr/sbin/brctl addif br0 uap0");
  2128. system("/sbin/ifconfig br0 192.168.10.200 up");
  2129. system("/bin/sed -i '/interface/d' /etc/udhcpd.conf");
  2130. system("/bin/echo 'interface br0' >> /etc/udhcpd.conf");
  2131. system("kill udhcpd");
  2132. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  2133. DEBUG_INFO("Bridge uap0 & eth0 for local power sharing by ethernet.\n");
  2134. }
  2135. // Change uap0 to low priority
  2136. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing &&
  2137. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) &&
  2138. (isUap0up() == PASS))
  2139. {
  2140. system("/sbin/ifmetric uap0 1");
  2141. }
  2142. sleep(5);
  2143. }
  2144. }
  2145. DEBUG_INFO("Initial Ethernet OK\n");
  2146. }
  2147. int SpawnTask()
  2148. {
  2149. system ("pkill Module_");
  2150. system ("pkill OcppBackend");
  2151. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2152. {
  2153. system("/root/Module_4g &");
  2154. }
  2155. else if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2156. {
  2157. system("/root/Module_Wifi &");
  2158. }
  2159. system ("/root/Module_EventLogging &");
  2160. system ("/root/Module_AlarmDetect &");
  2161. system ("/root/Module_InternalComm &");
  2162. system ("/root/Module_Speaker &");
  2163. system ("/root/Module_ProduceUtils &");
  2164. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  2165. {
  2166. ocpp_process_start();
  2167. }
  2168. else
  2169. {
  2170. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  2171. ocpp_process_start();
  2172. }
  2173. return PASS;
  2174. }
  2175. int Initialization()
  2176. {
  2177. int result = PASS;
  2178. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2179. InitGPIO();
  2180. InitEthernet();
  2181. if(DB_Open(localDb) != PASS)
  2182. {
  2183. DEBUG_ERROR("Local database initial fail.\n");
  2184. result = FAIL;
  2185. }
  2186. if(DB_Open_OCPP20(ocppDb) != PASS)
  2187. {
  2188. DEBUG_ERROR("OCPP 2.0 configuration variable database initial fail.\n");
  2189. result = FAIL;
  2190. }
  2191. for(int gun_index=0;gun_index< AC_QUANTITY;gun_index++)
  2192. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2193. if((rfidFd = InitRfidPort()) == FAIL)
  2194. {
  2195. DEBUG_ERROR("RFID port initial fail.\n");
  2196. result = FAIL;
  2197. }
  2198. if((wtdFd = InitWatchDog()) == FAIL)
  2199. {
  2200. DEBUG_ERROR("Watchdog initial fail.\n");
  2201. result = FAIL;
  2202. }
  2203. if(result == PASS)
  2204. DEBUG_INFO("Initialization OK.\n");
  2205. else
  2206. DEBUG_INFO("Initialization Fail.\n");
  2207. return result;
  2208. }
  2209. //=====================================================
  2210. // Common routine
  2211. //=====================================================
  2212. char* getSystemModeName(unsigned char mode)
  2213. {
  2214. char* result;
  2215. switch(mode)
  2216. {
  2217. case SYS_MODE_BOOTING:
  2218. result = "booting";
  2219. break;
  2220. case SYS_MODE_IDLE:
  2221. result = "idle";
  2222. break;
  2223. case SYS_MODE_AUTHORIZING:
  2224. result = "authorizing";
  2225. break;
  2226. case SYS_MODE_PREPARING:
  2227. result = "preparing";
  2228. break;
  2229. case SYS_MODE_CHARGING:
  2230. result = "charging";
  2231. break;
  2232. case SYS_MODE_TERMINATING:
  2233. result = "terminating";
  2234. break;
  2235. case SYS_MODE_COMPLETE:
  2236. result = "complete";
  2237. break;
  2238. case SYS_MODE_ALARM:
  2239. result = "alarm";
  2240. break;
  2241. case SYS_MODE_FAULT:
  2242. result = "fault";
  2243. break;
  2244. case SYS_MODE_MAINTAIN:
  2245. result = "maintain";
  2246. break;
  2247. case SYS_MODE_RESERVATION:
  2248. result = "reservation";
  2249. break;
  2250. case SYS_MODE_BOOKING:
  2251. result = "booking";
  2252. break;
  2253. case SYS_MODE_DEBUG:
  2254. result = "debug";
  2255. break;
  2256. case SYS_MODE_UPDATE:
  2257. result = "upgrade";
  2258. break;
  2259. default:
  2260. result = "unknown";
  2261. break;
  2262. }
  2263. return result;
  2264. }
  2265. void setChargerMode(unsigned char gun_index, unsigned char mode)
  2266. {
  2267. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2268. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  2269. 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));
  2270. }
  2271. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  2272. {
  2273. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  2274. }
  2275. unsigned char isModeChange(unsigned char gun_index)
  2276. {
  2277. unsigned char result = NO;
  2278. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  2279. {
  2280. result = YES;
  2281. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2282. }
  2283. return result;
  2284. }
  2285. void gpio_set_value(unsigned int gpio, unsigned int value)
  2286. {
  2287. int fd;
  2288. char buf[256];
  2289. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2290. fd = open(buf, O_WRONLY);
  2291. if (fd < 0)
  2292. {
  2293. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  2294. return;
  2295. }
  2296. if (value)
  2297. write(fd, "1", 2);
  2298. else
  2299. write(fd, "0", 2);
  2300. close(fd);
  2301. }
  2302. int gpio_get_value(unsigned int gpio)
  2303. {
  2304. int fd;
  2305. char buf[256];
  2306. char ch;
  2307. int8_t result = FAIL;
  2308. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2309. fd = open(buf, O_RDONLY);
  2310. if (fd < 0)
  2311. {
  2312. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  2313. return result;
  2314. }
  2315. read(fd, &ch, 1);
  2316. if (ch != '0')
  2317. result = 1;
  2318. else
  2319. result = 0;
  2320. close(fd);
  2321. return result;
  2322. }
  2323. //===============================================
  2324. // Get firmware version
  2325. //===============================================
  2326. void get_firmware_version(unsigned char gun_index)
  2327. {
  2328. FILE *fp;
  2329. char cmd[512];
  2330. char buf[512];
  2331. // Get CSU hardware version
  2332. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  2333. // Get CSU boot loader version
  2334. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2335. // Get CSU kernel version
  2336. sprintf(cmd, "/bin/uname -r");
  2337. fp = popen(cmd, "r");
  2338. if(fp == NULL)
  2339. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  2340. else
  2341. {
  2342. while(fgets(buf, sizeof(buf), fp) != NULL)
  2343. {
  2344. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  2345. }
  2346. }
  2347. // Get MCU firmware version
  2348. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2349. // Get CSU root file system version
  2350. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "V0.72.00.0000.00");
  2351. // Get AC connector type from model name
  2352. for(uint8_t idx=0;idx<3;idx++)
  2353. {
  2354. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2355. {
  2356. case '1':
  2357. // J1772 Plug
  2358. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  2359. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  2360. break;
  2361. case '2':
  2362. // J1772 Socket
  2363. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  2364. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  2365. break;
  2366. case '3':
  2367. // CE Plug
  2368. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  2369. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  2370. break;
  2371. case '4':
  2372. // CE Socket
  2373. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  2374. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  2375. break;
  2376. case '5':
  2377. // GB Plug
  2378. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  2379. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  2380. break;
  2381. case '6':
  2382. // GB Socket
  2383. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  2384. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  2385. break;
  2386. }
  2387. }
  2388. // Get network option from model name
  2389. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  2390. {
  2391. case 'B':
  2392. case 'U':
  2393. //Blue tooth
  2394. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  2395. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  2396. break;
  2397. case 'W':
  2398. // WIFI
  2399. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  2400. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  2401. break;
  2402. case 'T':
  2403. // 3G/4G
  2404. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  2405. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  2406. break;
  2407. case 'D':
  2408. // WIFI + 4G
  2409. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  2410. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  2411. break;
  2412. default:
  2413. // LAN
  2414. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  2415. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  2416. break;
  2417. }
  2418. // Get rating power from model name
  2419. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2420. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2421. // Get vender code from model name
  2422. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2423. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2424. DEBUG_INFO("========================================\n");
  2425. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2426. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  2427. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  2428. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  2429. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2430. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2431. DEBUG_INFO("========================================\n");
  2432. }
  2433. //===============================================
  2434. // Upgrade firmware
  2435. //===============================================
  2436. int upgrade_check()
  2437. {
  2438. int result = PASS;
  2439. int fd;
  2440. DIR *dir;
  2441. struct dirent *file;
  2442. char cmd[512];
  2443. long int MaxLen=48*1024*1024;
  2444. if ((dir = opendir ("/mnt")) != NULL)
  2445. {
  2446. /* print all the files and directories within directory */
  2447. while ((file = readdir (dir)) != NULL)
  2448. {
  2449. if(strlen(file->d_name)>2)
  2450. {
  2451. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  2452. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  2453. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  2454. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  2455. {
  2456. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2457. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2458. read(fd, ptr, MaxLen);
  2459. close(fd);
  2460. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  2461. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  2462. free(ptr);
  2463. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2464. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  2465. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  2466. {
  2467. switch(ShmCharger->fwUpgradeInfo.fwType)
  2468. {
  2469. case CSU_BOOTLOADER:
  2470. case CSU_KERNEL_CONFIGURATION:
  2471. case CSU_KERNEL_IMAGE:
  2472. case CSU_ROOT_FILE_SYSTEM:
  2473. case CSU_USER_CONFIGURATION:
  2474. case CSU_PRIMARY_CONTROLLER:
  2475. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  2476. {
  2477. result = FAIL;
  2478. }
  2479. else
  2480. {
  2481. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  2482. {
  2483. DEBUG_INFO("Restore model name & serial number.\n");
  2484. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2485. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2486. {
  2487. DEBUG_INFO("Re-load configuration fail.\n");
  2488. result = FAIL;
  2489. }
  2490. else
  2491. {
  2492. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  2493. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  2494. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  2495. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2496. {
  2497. DEBUG_INFO("Re-write configuration fail.\n");
  2498. result = FAIL;
  2499. }
  2500. else
  2501. DEBUG_INFO("Re-write configuration OK.\n");
  2502. }
  2503. }
  2504. }
  2505. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2506. system(cmd);
  2507. break;
  2508. case AC_WALLMOUNT_CONTROLLER:
  2509. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2510. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  2511. sleep(10);
  2512. break;
  2513. default:
  2514. result = FAIL;
  2515. DEBUG_WARN("Image file is unknown type.\n");
  2516. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2517. system(cmd);
  2518. break;
  2519. }
  2520. }
  2521. else
  2522. {
  2523. result = FAIL;
  2524. DEBUG_ERROR("Model name and Firmware type error.\n");
  2525. }
  2526. }
  2527. else
  2528. {
  2529. result = FAIL;
  2530. DEBUG_ERROR("New firmware open error.\n");
  2531. }
  2532. }
  2533. else
  2534. {
  2535. if(strlen(file->d_name) >= 3)
  2536. {
  2537. result = FAIL;
  2538. DEBUG_ERROR("File name error.\n");
  2539. }
  2540. else
  2541. {
  2542. DEBUG_ERROR("Searching file.\n");
  2543. }
  2544. }
  2545. }
  2546. closedir (dir);
  2547. }
  2548. else
  2549. {
  2550. result = FAIL;
  2551. DEBUG_ERROR("/mnt does not valid.\n");
  2552. }
  2553. return result;
  2554. }
  2555. //===============================================
  2556. // Check RFID is match with start user
  2557. //===============================================
  2558. int isMatchStartUser(unsigned char gun_index)
  2559. {
  2560. uint8_t tmpUser[32];
  2561. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2562. {
  2563. // Big endian
  2564. switch(rfid.snType)
  2565. {
  2566. case RFID_SN_TYPE_8BYTE:
  2567. 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]);
  2568. break;
  2569. case RFID_SN_TYPE_7BYTE:
  2570. 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]);
  2571. break;
  2572. case RFID_SN_TYPE_10BYTE:
  2573. 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]);
  2574. break;
  2575. case RFID_SN_TYPE_4BYTE:
  2576. default:
  2577. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2578. break;
  2579. }
  2580. }
  2581. else
  2582. {
  2583. // Little endian
  2584. switch(rfid.snType)
  2585. {
  2586. case RFID_SN_TYPE_8BYTE:
  2587. 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]);
  2588. break;
  2589. case RFID_SN_TYPE_7BYTE:
  2590. 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]);
  2591. break;
  2592. case RFID_SN_TYPE_10BYTE:
  2593. 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]);
  2594. break;
  2595. case RFID_SN_TYPE_4BYTE:
  2596. default:
  2597. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2598. break;
  2599. }
  2600. }
  2601. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2602. }
  2603. //===============================================
  2604. // Read RFID Serial Number
  2605. //===============================================
  2606. int GetCardSerialNumber()
  2607. {
  2608. int isSuccess = FAIL;
  2609. int module_type = MODULE_EWT;
  2610. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2611. {
  2612. if(rfid.cardType == ISO14443A)
  2613. {
  2614. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2615. {
  2616. isSuccess = PASS;
  2617. }
  2618. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2619. {
  2620. isSuccess = PASS;
  2621. }
  2622. sethaltCard(rfidFd,module_type);
  2623. }
  2624. else if(rfid.cardType == IS014443B)
  2625. {
  2626. isSuccess = PASS;
  2627. }
  2628. else if(rfid.cardType == FELICA)
  2629. {
  2630. isSuccess = PASS;
  2631. }
  2632. else
  2633. {}
  2634. }
  2635. else
  2636. {}
  2637. return isSuccess;
  2638. }
  2639. //===============================================
  2640. // Set led motion
  2641. //===============================================
  2642. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  2643. {
  2644. switch(led_mode)
  2645. {
  2646. case LED_ACTION_INIT:
  2647. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  2648. break;
  2649. case LED_ACTION_IDLE:
  2650. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  2651. break;
  2652. case LED_ACTION_AUTHED:
  2653. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  2654. break;
  2655. case LED_ACTION_CONNECTED:
  2656. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  2657. break;
  2658. case LED_ACTION_CHARGING:
  2659. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  2660. break;
  2661. case LED_ACTION_STOP:
  2662. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  2663. break;
  2664. case LED_ACTION_ALARM:
  2665. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  2666. break;
  2667. case LED_ACTION_MAINTAIN:
  2668. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  2669. break;
  2670. case LED_ACTION_RFID_PASS:
  2671. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  2672. break;
  2673. case LED_ACTION_RFID_FAIL:
  2674. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  2675. break;
  2676. case LED_ACTION_BLE_CONNECT:
  2677. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  2678. break;
  2679. case LED_ACTION_BLE_DISABLE:
  2680. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  2681. break;
  2682. case LED_ACTION_DEBUG:
  2683. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  2684. break;
  2685. case LED_ACTION_ALL_OFF:
  2686. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  2687. break;
  2688. case LED_RELAY_ON:
  2689. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  2690. break;
  2691. case LED_RELAY_OFF:
  2692. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  2693. break;
  2694. case LED_ACTION_HANDSHAKE_FAIL:
  2695. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  2696. break;
  2697. case LED_ACTION_INTERNET_DISCONNECT:
  2698. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  2699. break;
  2700. }
  2701. }
  2702. //===============================================
  2703. // Request on/off set (PWM)
  2704. //===============================================
  2705. void setRequest(unsigned char gun_index,unsigned char isOn)
  2706. {
  2707. if(isOn == ON)
  2708. {
  2709. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  2710. {
  2711. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  2712. DEBUG_INFO("Gun-%d permission request: ON \n",gun_index);
  2713. }
  2714. }
  2715. else
  2716. {
  2717. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2718. {
  2719. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  2720. DEBUG_INFO("Gun-%d permission request: OFF \n",gun_index);
  2721. }
  2722. }
  2723. }
  2724. //===============================================
  2725. // Request on/off get (PWM)
  2726. //===============================================
  2727. int getRequest(unsigned char gun_index)
  2728. {
  2729. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  2730. }
  2731. //===============================================
  2732. // Relay on/off set (Relay)
  2733. //===============================================
  2734. void setRelay(unsigned char gun_index,unsigned char isOn)
  2735. {
  2736. if(isOn == ON)
  2737. {
  2738. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  2739. {
  2740. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  2741. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  2742. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  2743. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  2744. DEBUG_INFO("Gun-%d Output relay status: ON. \n",gun_index);
  2745. }
  2746. }
  2747. else
  2748. {
  2749. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  2750. {
  2751. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  2752. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  2753. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  2754. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  2755. DEBUG_INFO("Gun-%d Output relay status: OFF. \n",gun_index);
  2756. }
  2757. }
  2758. }
  2759. //===============================================
  2760. // Relay on/off get (Relay)
  2761. //===============================================
  2762. int getRelay(unsigned char gun_index)
  2763. {
  2764. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  2765. }
  2766. //===============================================
  2767. // Set speaker on/off request
  2768. //===============================================
  2769. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  2770. {
  2771. if(isOn == ON)
  2772. {
  2773. ShmCharger->isSpeakerOn = ON;
  2774. ShmCharger->speaker_type = speaker_mode;
  2775. }
  2776. }
  2777. //===============================================
  2778. // Initialization RFID communication port
  2779. //===============================================
  2780. int InitRfidPort()
  2781. {
  2782. int uartO2 = open(rfidPortName, O_RDWR);
  2783. struct termios tios;
  2784. if (uartO2 != FAIL)
  2785. {
  2786. ioctl (uartO2, TCGETS, &tios);
  2787. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  2788. tios.c_lflag = 0;
  2789. tios.c_iflag = 0;
  2790. tios.c_oflag = 0;
  2791. tios.c_cc[VMIN] = 0;
  2792. tios.c_cc[VTIME] = (unsigned char) 1;
  2793. tios.c_lflag = 0;
  2794. tcflush(uartO2, TCIFLUSH);
  2795. ioctl(uartO2, TCSETS, &tios);
  2796. }
  2797. if (uartO2 < 0)
  2798. {
  2799. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  2800. }
  2801. return uartO2;
  2802. }
  2803. //===============================================
  2804. // Check internet access status
  2805. //===============================================
  2806. int isReachableInternet()
  2807. {
  2808. int result = FAIL;
  2809. FILE *fp;
  2810. char cmd[256];
  2811. char buf[512];
  2812. char tmp[512];
  2813. // Get ip address & net mask
  2814. strcpy(cmd, "ifconfig eth0");
  2815. fp = popen(cmd, "r");
  2816. if(fp != NULL)
  2817. {
  2818. while(fgets(buf, sizeof(buf), fp) != NULL)
  2819. {
  2820. if (strstr(buf, "inet addr:") > 0)
  2821. {
  2822. sscanf(buf, "%*s%s", tmp);
  2823. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  2824. sscanf(buf, "%*s%*s%*s%s", tmp);
  2825. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp, strspn(tmp, "Mask:"), strlen(tmp)-strspn(tmp, "Mask:"));
  2826. }
  2827. }
  2828. }
  2829. pclose(fp);
  2830. memset(buf, 0x00, sizeof(buf));
  2831. // Get gateway
  2832. fp = popen("ip route", "r");
  2833. if(fp == NULL)
  2834. result = FAIL;
  2835. else
  2836. {
  2837. while(fgets(buf, sizeof(buf), fp) != NULL)
  2838. {
  2839. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  2840. break;
  2841. }
  2842. if(strstr(buf, "default") != NULL)
  2843. {
  2844. sscanf(buf, "%*s%*s%s", tmp);
  2845. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  2846. }
  2847. }
  2848. pclose(fp);
  2849. memset(buf, 0x00, sizeof(buf));
  2850. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  2851. {
  2852. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  2853. fp = popen(cmd, "r");
  2854. if(fp != NULL)
  2855. {
  2856. while(fgets(buf, sizeof(buf), fp) != NULL)
  2857. {
  2858. if(strstr(buf, "transmitted") > 0)
  2859. {
  2860. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  2861. if(strstr(buf,"100%") != NULL)
  2862. {
  2863. }
  2864. else
  2865. {
  2866. result = PASS;
  2867. }
  2868. //DEBUG_INFO("%s",buf);
  2869. //DEBUG_INFO("%s\n",tmp);
  2870. }
  2871. }
  2872. }
  2873. pclose(fp);
  2874. }
  2875. return result;
  2876. }
  2877. //===============================================
  2878. // Check route for eth0
  2879. //===============================================
  2880. int isRouteFail()
  2881. {
  2882. int result = YES;
  2883. FILE *fp;
  2884. char buf[512];
  2885. fp = popen("route -n", "r");
  2886. if(fp != NULL)
  2887. {
  2888. while(fgets(buf, sizeof(buf), fp) != NULL)
  2889. {
  2890. if(strstr(buf, "eth0") != NULL)
  2891. result = NO;
  2892. }
  2893. }
  2894. pclose(fp);
  2895. return result;
  2896. }
  2897. //===============================================
  2898. // Check reservation date is expired
  2899. //===============================================
  2900. int isReservationExpired(unsigned char gun_index)
  2901. {
  2902. int result = NO;
  2903. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2904. {
  2905. if(isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
  2906. result = YES;
  2907. }
  2908. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2909. {
  2910. if(isOvertNow(ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime))
  2911. result = YES;
  2912. }
  2913. return result;
  2914. }
  2915. //===============================================
  2916. // Check charging profile related date routine
  2917. //===============================================
  2918. int isProfileValid(uint8_t gun_index)
  2919. {
  2920. int result = NO;
  2921. struct tm tmFrom, tmTo;
  2922. struct timeb tbFrom, tbTo;
  2923. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2924. {
  2925. 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) &&
  2926. (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))
  2927. {
  2928. tmFrom.tm_year -= 1900;
  2929. tmFrom.tm_mon -= 1;
  2930. tbFrom.time = mktime(&tmFrom);
  2931. tmTo.tm_year -= 1900;
  2932. tmTo.tm_mon -= 1;
  2933. tbTo.time = mktime(&tmTo);
  2934. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2935. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2936. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2937. {
  2938. result = YES;
  2939. }
  2940. }
  2941. else
  2942. {
  2943. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2944. }
  2945. }
  2946. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2947. {
  2948. 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) &&
  2949. (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))
  2950. {
  2951. tmFrom.tm_year -= 1900;
  2952. tmFrom.tm_mon -= 1;
  2953. tbFrom.time = mktime(&tmFrom);
  2954. tmTo.tm_year -= 1900;
  2955. tmTo.tm_mon -= 1;
  2956. tbTo.time = mktime(&tmTo);
  2957. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2958. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2959. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2960. {
  2961. result = YES;
  2962. }
  2963. }
  2964. else
  2965. {
  2966. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2967. }
  2968. }
  2969. return result;
  2970. }
  2971. int getMaxScheduleStart()
  2972. {
  2973. int result = -1;
  2974. struct tm tmScheduleStart;;
  2975. struct timeb tbScheduleStart;
  2976. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2977. {
  2978. 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))
  2979. {
  2980. tmScheduleStart.tm_year -= 1900;
  2981. tmScheduleStart.tm_mon -= 1;
  2982. tbScheduleStart.time = mktime(&tmScheduleStart);
  2983. tbScheduleStart.millitm = 0;
  2984. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2985. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  2986. }
  2987. else
  2988. {
  2989. DEBUG_WARN("Max schedule start date parsing error.\n");
  2990. }
  2991. }
  2992. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2993. {
  2994. 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))
  2995. {
  2996. tmScheduleStart.tm_year -= 1900;
  2997. tmScheduleStart.tm_mon -= 1;
  2998. tbScheduleStart.time = mktime(&tmScheduleStart);
  2999. tbScheduleStart.millitm = 0;
  3000. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3001. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3002. }
  3003. else
  3004. {
  3005. DEBUG_WARN("Max schedule start date parsing error.\n");
  3006. }
  3007. }
  3008. return result;
  3009. }
  3010. int getScheduleStart(int gun_index)
  3011. {
  3012. int result = -1;
  3013. struct tm tmScheduleStart;;
  3014. struct timeb tbScheduleStart;
  3015. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3016. {
  3017. 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))
  3018. {
  3019. tmScheduleStart.tm_year -= 1900;
  3020. tmScheduleStart.tm_mon -= 1;
  3021. tbScheduleStart.time = mktime(&tmScheduleStart);
  3022. tbScheduleStart.millitm = 0;
  3023. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3024. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3025. }
  3026. else
  3027. {
  3028. DEBUG_WARN("Schedule start date parsing error.\n");
  3029. }
  3030. }
  3031. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3032. {
  3033. 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))
  3034. {
  3035. tmScheduleStart.tm_year -= 1900;
  3036. tmScheduleStart.tm_mon -= 1;
  3037. tbScheduleStart.time = mktime(&tmScheduleStart);
  3038. tbScheduleStart.millitm = 0;
  3039. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3040. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3041. }
  3042. else
  3043. {
  3044. DEBUG_WARN("Schedule start date parsing error.\n");
  3045. }
  3046. }
  3047. return result;
  3048. }
  3049. int getStartSinceToday()
  3050. {
  3051. int result = -1;
  3052. time_t t;
  3053. struct tm *tmStartToday;
  3054. struct timeb tbStartToday;
  3055. t=time(NULL);
  3056. tmStartToday=localtime(&t);
  3057. tmStartToday->tm_hour = 0;
  3058. tmStartToday->tm_min = 0;
  3059. tmStartToday->tm_sec = 0;
  3060. tbStartToday.time = mktime(tmStartToday);
  3061. result = DiffTimebWithNow(tbStartToday)/1000;
  3062. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  3063. return result;
  3064. }
  3065. int getStartSinceWeek()
  3066. {
  3067. int result = -1;
  3068. time_t t;
  3069. struct tm *tmStartWeek;
  3070. struct timeb tbStartWeek;
  3071. t=time(NULL);
  3072. tmStartWeek=localtime(&t);
  3073. t-=86400*tmStartWeek->tm_wday;
  3074. tmStartWeek=localtime(&t);
  3075. tmStartWeek->tm_hour = 0;
  3076. tmStartWeek->tm_min = 0;
  3077. tmStartWeek->tm_sec = 0;
  3078. tbStartWeek.time = mktime(tmStartWeek);
  3079. result = DiffTimebWithNow(tbStartWeek)/1000;
  3080. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  3081. return result;
  3082. }
  3083. //===============================================
  3084. // Valid from local white list
  3085. //===============================================
  3086. int isValidLocalWhiteCard()
  3087. {
  3088. uint8_t result = FAIL;
  3089. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  3090. {
  3091. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  3092. {
  3093. result = PASS;
  3094. }
  3095. }
  3096. return result;
  3097. }
  3098. //==========================================
  3099. // Check routine
  3100. //==========================================
  3101. void checkTask()
  3102. {
  3103. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3104. {
  3105. if(system("pidof -s Module_4g > /dev/null") != 0)
  3106. {
  3107. DEBUG_INFO("Module_4g not running, restart it.\n");
  3108. system("/root/Module_4g &");
  3109. }
  3110. }
  3111. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3112. {
  3113. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  3114. {
  3115. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  3116. system("/root/Module_Wifi &");
  3117. }
  3118. }
  3119. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  3120. {
  3121. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  3122. system("/root/Module_EventLogging &");
  3123. }
  3124. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  3125. {
  3126. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3127. {
  3128. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  3129. {
  3130. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  3131. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3132. system("pkill OcppBackend");
  3133. sleep(3);
  3134. ocpp_process_start();
  3135. }
  3136. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3137. {
  3138. DEBUG_INFO("OcppBackend not running, restart it.\n");
  3139. ocpp_process_start();
  3140. }
  3141. }
  3142. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3143. {
  3144. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  3145. {
  3146. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  3147. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3148. system("pkill OcppBackend20");
  3149. sleep(3);
  3150. ocpp_process_start();
  3151. }
  3152. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3153. {
  3154. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  3155. ocpp_process_start();
  3156. }
  3157. }
  3158. }
  3159. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  3160. {
  3161. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3162. {
  3163. DEBUG_INFO("OcppBackendPH not running, restart it.\n");
  3164. system("/root/OcppBackendPH &");
  3165. }
  3166. }
  3167. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  3168. {
  3169. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  3170. system("/root/Module_AlarmDetect &");
  3171. }
  3172. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  3173. {
  3174. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  3175. system("/root/Module_InternalComm &");
  3176. }
  3177. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  3178. {
  3179. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  3180. system("/root/Module_Speaker &");
  3181. }
  3182. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  3183. {
  3184. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  3185. system ("/root/Module_ProduceUtils &");
  3186. }
  3187. if((system("pidof -s Module_PowerSharing > /dev/null") != 0) &&
  3188. ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
  3189. {
  3190. DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
  3191. system ("/root/Module_PowerSharing &");
  3192. }
  3193. }
  3194. void checkConnectionTimeout()
  3195. {
  3196. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  3197. {
  3198. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3199. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  3200. }
  3201. else
  3202. {
  3203. ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
  3204. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  3205. {
  3206. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3207. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  3208. }
  3209. else
  3210. {
  3211. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  3212. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  3213. }
  3214. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3215. }
  3216. }
  3217. void checkReset()
  3218. {
  3219. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3220. {
  3221. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3222. {
  3223. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3224. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE))
  3225. {
  3226. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  3227. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3228. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  3229. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  3230. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3231. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3232. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  3233. {
  3234. system("sync");
  3235. sleep(10);
  3236. system("reboot -f");
  3237. sleep(10);
  3238. system("reboot -f");
  3239. }
  3240. else
  3241. {
  3242. sleep(10);
  3243. close(wtdFd);
  3244. system("/usr/bin/run_evse_restart.sh");
  3245. }
  3246. }
  3247. }
  3248. }
  3249. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3250. {
  3251. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3252. {
  3253. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3254. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE))
  3255. {
  3256. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  3257. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  3258. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  3259. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  3260. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3261. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3262. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  3263. {
  3264. system("sync");
  3265. sleep(10);
  3266. system("reboot -f");
  3267. sleep(10);
  3268. system("reboot -f");
  3269. }
  3270. else
  3271. {
  3272. sleep(10);
  3273. close(wtdFd);
  3274. system("/usr/bin/run_evse_restart.sh");
  3275. }
  3276. }
  3277. }
  3278. }
  3279. }
  3280. void checkReservation(uint8_t gun_index)
  3281. {
  3282. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3283. {
  3284. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  3285. {
  3286. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3287. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3288. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3289. {
  3290. sleep(5);
  3291. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3292. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3293. }
  3294. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  3295. }
  3296. }
  3297. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3298. {
  3299. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  3300. {
  3301. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3302. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3303. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3304. {
  3305. sleep(5);
  3306. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  3307. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3308. }
  3309. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  3310. }
  3311. }
  3312. }
  3313. void checkUnlocker(uint8_t gun_index)
  3314. {
  3315. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3316. {
  3317. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3318. {
  3319. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3320. if(ShmSysConfigAndInfo->SysConfig.ModelName[9-gun_index] == '4')
  3321. {
  3322. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "Unlocked");
  3323. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  3324. }
  3325. else
  3326. {
  3327. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  3328. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  3329. }
  3330. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  3331. }
  3332. }
  3333. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3334. {
  3335. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3336. {
  3337. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3338. if(ShmSysConfigAndInfo->SysConfig.ModelName[9-gun_index] == '4')
  3339. {
  3340. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING)
  3341. {
  3342. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "OngoingAuthorizedTransaction");
  3343. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = OFF;
  3344. }
  3345. else
  3346. {
  3347. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "Unlocked");
  3348. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  3349. }
  3350. }
  3351. else
  3352. {
  3353. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "UnlockFailed");
  3354. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = OFF;
  3355. }
  3356. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  3357. }
  3358. }
  3359. }
  3360. void checkAvailability(uint8_t gun_index)
  3361. {
  3362. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3363. {
  3364. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3365. {
  3366. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  3367. {
  3368. DB_Update_Operactive(localDb, gun_index, true);
  3369. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3370. }
  3371. else
  3372. {
  3373. DB_Update_Operactive(localDb, gun_index, false);
  3374. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3375. }
  3376. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3377. }
  3378. }
  3379. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3380. {
  3381. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3382. {
  3383. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  3384. {
  3385. DB_Update_Operactive(localDb, gun_index, true);
  3386. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3387. }
  3388. else
  3389. {
  3390. DB_Update_Operactive(localDb, gun_index, false);
  3391. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3392. }
  3393. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3394. }
  3395. }
  3396. }
  3397. void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
  3398. {
  3399. uint16_t MaxChargingProfileChargingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3400. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3401. {
  3402. //========================================
  3403. // Check TxProfile and TxDefaultProfile
  3404. //========================================
  3405. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3406. {
  3407. // Checking profile kind
  3408. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  3409. {
  3410. // Checking limitation
  3411. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3412. {
  3413. if((getScheduleStart(gun_index) >= ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3414. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3415. )
  3416. {
  3417. 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);
  3418. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3419. }
  3420. else
  3421. break;
  3422. }
  3423. }
  3424. }
  3425. else
  3426. {
  3427. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3428. }
  3429. //========================================
  3430. // Check ChargingPointMaxProfile
  3431. //========================================
  3432. if((ocpp_get_maxcharging_profileId() > 0))
  3433. {
  3434. // Absolute profile
  3435. if((mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == PASS))
  3436. {
  3437. // Checking limitation
  3438. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3439. {
  3440. if((getMaxScheduleStart() >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3441. ((idx_period == 0) || (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3442. )
  3443. {
  3444. 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);
  3445. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3446. }
  3447. else
  3448. break;
  3449. }
  3450. }
  3451. }
  3452. // Compare target current is over max current for EVSE limit
  3453. if(gun_index == 0)
  3454. {
  3455. if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  3456. {
  3457. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  3458. }
  3459. }
  3460. else
  3461. {
  3462. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
  3463. {
  3464. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3465. }
  3466. }
  3467. }
  3468. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3469. {
  3470. //========================================
  3471. // Check TxProfile and TxDefaultProfile
  3472. //========================================
  3473. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3474. {
  3475. // Checking profile kind
  3476. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  3477. {
  3478. // Checking limitation
  3479. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3480. {
  3481. if((getScheduleStart(gun_index) >= ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3482. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3483. )
  3484. {
  3485. 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);
  3486. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3487. }
  3488. else
  3489. break;
  3490. }
  3491. }
  3492. }
  3493. else
  3494. {
  3495. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3496. }
  3497. //========================================
  3498. // Check ChargingPointMaxProfile
  3499. //========================================
  3500. if((ocpp_get_maxcharging_profileId() > 0))
  3501. {
  3502. // Absolute profile
  3503. if((mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, "Absolute") == PASS))
  3504. {
  3505. // Checking limitation
  3506. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3507. {
  3508. if((getMaxScheduleStart() >= ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3509. ((idx_period == 0) || (ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3510. )
  3511. {
  3512. 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);
  3513. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3514. }
  3515. else
  3516. break;
  3517. }
  3518. }
  3519. }
  3520. // Compare target current is over max current for EVSE limit
  3521. if(gun_index == 0)
  3522. {
  3523. if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  3524. {
  3525. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  3526. }
  3527. }
  3528. else
  3529. {
  3530. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
  3531. {
  3532. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3533. }
  3534. }
  3535. }
  3536. // Charging session target current check if OCPP disconnect and power sharing server connected
  3537. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
  3538. {
  3539. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  3540. 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];
  3541. else
  3542. {
  3543. if(ShmCharger->gun_info[gun_index].targetCurrent != 0)
  3544. DEBUG_WARN("Disconnect from power sharing server, target current set to 0.\n");
  3545. ShmCharger->gun_info[gun_index].targetCurrent = 0;
  3546. }
  3547. }
  3548. switch(system_mode)
  3549. {
  3550. case SYS_MODE_IDLE:
  3551. case SYS_MODE_PREPARING:
  3552. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3553. {
  3554. 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);
  3555. 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);
  3556. 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));
  3557. }
  3558. else
  3559. {
  3560. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3561. 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);
  3562. 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));
  3563. }
  3564. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3565. break;
  3566. case SYS_MODE_CHARGING:
  3567. case SYS_MODE_TERMINATING:
  3568. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3569. {
  3570. 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);
  3571. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  3572. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  3573. {
  3574. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3575. {
  3576. 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);
  3577. 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));
  3578. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3579. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3580. }
  3581. else
  3582. {}
  3583. }
  3584. }
  3585. else
  3586. {
  3587. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3588. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  3589. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  3590. {
  3591. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3592. {
  3593. 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);
  3594. 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));
  3595. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3596. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3597. }
  3598. else
  3599. {}
  3600. }
  3601. }
  3602. if(previousData[gun_index].primaryMcuCp_Pwn_Duty != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3603. {
  3604. DEBUG_INFO("====================================================\n");
  3605. DEBUG_INFO("gun-%02d primaryMcuCp_Pwn_Duty: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  3606. DEBUG_INFO("====================================================\n");
  3607. previousData[gun_index].primaryMcuCp_Pwn_Duty = ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current;
  3608. }
  3609. if(ShmCharger->gun_info[gun_index].targetCurrent != previousData[gun_index].targetCurrent)
  3610. {
  3611. DEBUG_INFO("====================================================\n");
  3612. DEBUG_INFO("MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  3613. DEBUG_INFO("gun-%02d targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  3614. DEBUG_INFO("====================================================\n");
  3615. previousData[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent;
  3616. }
  3617. if(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != previousData[gun_index].current_limit)
  3618. {
  3619. DEBUG_INFO("====================================================\n");
  3620. DEBUG_INFO("gun-%02d mcu current_limit: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  3621. DEBUG_INFO("====================================================\n");
  3622. previousData[gun_index].current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  3623. }
  3624. break;
  3625. }
  3626. }
  3627. void checkStopReason(uint8_t gun_index)
  3628. {
  3629. sleep(2);
  3630. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3631. {
  3632. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3633. if(!ocpp_get_starttransaction_result(gun_index))
  3634. {
  3635. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  3636. }
  3637. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3638. {
  3639. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  3640. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3641. }
  3642. else if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)) && (strstr(ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") != NULL))
  3643. {
  3644. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3645. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3646. //memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3647. }
  3648. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3649. {
  3650. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3651. {
  3652. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3653. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3654. }
  3655. else
  3656. {
  3657. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3658. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3659. }
  3660. }
  3661. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3662. {
  3663. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3664. if(!isMatchStartUser(gun_index))
  3665. {
  3666. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3667. }
  3668. else
  3669. {
  3670. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3671. }
  3672. DEBUG_INFO("Gun-[%d] : IdTag [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  3673. }
  3674. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3675. {
  3676. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3677. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3678. }
  3679. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3680. {
  3681. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3682. }
  3683. else
  3684. {
  3685. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3686. }
  3687. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3688. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3689. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  3690. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3691. }
  3692. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3693. {
  3694. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3695. if(!ocpp_get_starttransaction_result(gun_index))
  3696. {
  3697. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  3698. }
  3699. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3700. {
  3701. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  3702. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3703. }
  3704. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  3705. {
  3706. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  3707. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3708. }
  3709. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  3710. {
  3711. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  3712. }
  3713. else if((ShmOCPP20Data->MsMsg.bits.ResetReq) && (strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0))
  3714. {
  3715. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  3716. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3717. }
  3718. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3719. {
  3720. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  3721. if(!isMatchStartUser(gun_index))
  3722. {
  3723. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3724. }
  3725. else
  3726. {
  3727. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3728. }
  3729. DEBUG_INFO("Gun-[%d] : idToken [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  3730. }
  3731. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_OVER_CURRENT))
  3732. {
  3733. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  3734. }
  3735. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  3736. {
  3737. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  3738. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3739. }
  3740. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  3741. {
  3742. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  3743. }
  3744. else
  3745. {
  3746. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  3747. }
  3748. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  3749. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3750. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  3751. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  3752. }
  3753. // Maintain server
  3754. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  3755. {
  3756. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  3757. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].IdTag, ShmOCPP16Data->StopTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3758. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3759. }
  3760. }
  3761. void checkRemoteUpgradeStatus()
  3762. {
  3763. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3764. {
  3765. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  3766. {
  3767. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3768. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3769. ShmCharger->isUpdateSuccess = NO;
  3770. }
  3771. else if((strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0))
  3772. {
  3773. DEBUG_INFO("Firmware remote upgrading...\n");
  3774. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3775. sleep(1);
  3776. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3777. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3778. int result = upgrade_check();
  3779. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3780. if(result == PASS)
  3781. {
  3782. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3783. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3784. ShmCharger->isUpdateSuccess = YES;
  3785. DEBUG_INFO("Remote update success...\n");
  3786. }
  3787. else
  3788. {
  3789. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3790. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3791. ShmCharger->isUpdateSuccess = NO;
  3792. DEBUG_INFO("Remote update unsuccess...\n");
  3793. }
  3794. }
  3795. else if((strcmp((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "SignatureVerified")==0))
  3796. {
  3797. DEBUG_INFO("Signature firmware remote upgrading...\n");
  3798. ShmOCPP16Data->SignedFirmwareStatusNotification.requestId = ShmOCPP16Data->SignedUpdateFirmware.requestId;
  3799. sprintf((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "Installing");
  3800. sleep(1);
  3801. ShmOCPP16Data->SpMsg.bits.SignedFirmwareStatusNotificationReq = ON;
  3802. ShmOCPP16Data->MsMsg.bits.SignedUpdateFirmwareReq = OFF;
  3803. if(upgrade_check() == PASS)
  3804. {
  3805. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3806. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3807. ShmCharger->isUpdateSuccess = YES;
  3808. DEBUG_INFO("Remote update success.\n");
  3809. }
  3810. else
  3811. {
  3812. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3813. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3814. ShmCharger->isUpdateSuccess = NO;
  3815. DEBUG_INFO("Remote update fail.\n");
  3816. }
  3817. }
  3818. else
  3819. {}
  3820. }
  3821. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3822. {
  3823. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  3824. {
  3825. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3826. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3827. ShmCharger->isUpdateSuccess = NO;
  3828. }
  3829. else if((strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0))
  3830. {
  3831. DEBUG_INFO("Firmware remote upgrading...\n");
  3832. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3833. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3834. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3835. int result = upgrade_check();
  3836. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3837. if(result == PASS)
  3838. {
  3839. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3840. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3841. ShmCharger->isUpdateSuccess = YES;
  3842. DEBUG_INFO("Remote update success...\n");
  3843. }
  3844. else
  3845. {
  3846. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3847. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3848. ShmCharger->isUpdateSuccess = NO;
  3849. DEBUG_INFO("Remote update unsuccess...\n");
  3850. }
  3851. }
  3852. else
  3853. {}
  3854. }
  3855. }
  3856. //===============================================
  3857. // Main process
  3858. //===============================================
  3859. int main(void)
  3860. {
  3861. //==================================================
  3862. // Create all share memory
  3863. //==================================================
  3864. if(CreatShareMemory()==0)
  3865. {
  3866. DEBUG_ERROR("CreatShareMemory NG\n");
  3867. if(ShmStatusCodeData!=NULL)
  3868. {
  3869. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  3870. }
  3871. sleep(5);
  3872. system("reboot -f");
  3873. sleep(5);
  3874. system("reboot -f");
  3875. }
  3876. else
  3877. {
  3878. DEBUG_INFO("CreatShareMemory OK\n");
  3879. }
  3880. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3881. {
  3882. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  3883. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  3884. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  3885. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  3886. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  3887. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  3888. }
  3889. //==================================================
  3890. // Main loop
  3891. //==================================================
  3892. for(;;)
  3893. {
  3894. //==============================================
  3895. // Synchronize share memory from OCPP struct
  3896. //==============================================
  3897. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  3898. //==============================================
  3899. // Period check for 10 seconds
  3900. //==============================================
  3901. if((DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10000) || (DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
  3902. {
  3903. //==============================================
  3904. // Check task processing
  3905. //==============================================
  3906. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3907. checkTask();
  3908. //==============================================
  3909. // Check connection timeout specification
  3910. //==============================================
  3911. checkConnectionTimeout();
  3912. ftime(&startTime[0][TMR_IDX_CHECK_TASK]);
  3913. }
  3914. //==============================================
  3915. // Something need run in Idle mode
  3916. //==============================================
  3917. 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)) &&
  3918. (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))
  3919. {
  3920. //======================================
  3921. // Check restore factory setting request
  3922. //======================================
  3923. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3924. {
  3925. // Set led to initial
  3926. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3927. {
  3928. setLedMotion(gun_index,LED_ACTION_INIT);
  3929. }
  3930. sleep(5);
  3931. system("cd /root;./Module_FactoryConfig -m");
  3932. // OCPP 1.6 Configuration key
  3933. system("rm -f /Storage/OCPP/OCPPConfiguration");
  3934. // OCPP 2.O Configuration variable
  3935. DB_variableClear(ocppDb);
  3936. DEBUG_INFO("Default configuration to factory setting. \n");
  3937. system("sync");
  3938. sleep(5);
  3939. system("reboot -f");
  3940. sleep(5);
  3941. system("reboot -f");
  3942. }
  3943. //======================================
  3944. // Check upgrade firmware request
  3945. //======================================
  3946. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  3947. ocpp_get_update_firmware_req())
  3948. {
  3949. // If available memory too small, free memory cache first
  3950. if(getAvailableMemory() < (200*1024*1024))
  3951. {
  3952. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  3953. system("echo 3 > /proc/sys/vm/drop_caches");
  3954. }
  3955. ShmCharger->isUpdateSuccess = NO;
  3956. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3957. {
  3958. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3959. setChargerMode(gun_index, SYS_MODE_UPDATE);
  3960. }
  3961. }
  3962. }
  3963. //==============================================
  3964. // Check remote reset request
  3965. //==============================================
  3966. checkReset();
  3967. //==============================================
  3968. // Check RFID authorization
  3969. //==============================================
  3970. if((!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq) && (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE))
  3971. {
  3972. if(GetCardSerialNumber()!= FAIL)
  3973. {
  3974. if(isRfidReadable)
  3975. {
  3976. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  3977. isRfidReadable = NO;
  3978. }
  3979. }
  3980. else
  3981. {
  3982. isRfidReadable = YES;
  3983. }
  3984. }
  3985. //==============================================
  3986. // Check Local Power Sharing Communication
  3987. //==============================================
  3988. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
  3989. {
  3990. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  3991. {
  3992. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == ON)
  3993. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  3994. }
  3995. else
  3996. {
  3997. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == OFF)
  3998. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = ON;
  3999. }
  4000. }
  4001. //==============================================
  4002. // Connector loop
  4003. //==============================================
  4004. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4005. {
  4006. // Synchronize current rating value from MCU
  4007. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  4008. // Assign connector location index for OCPP
  4009. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  4010. // Synchronize present charging power
  4011. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4012. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  4013. else
  4014. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4015. // Check initialization "PASS" or "FAIL"
  4016. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4017. {
  4018. // Alarm event check
  4019. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  4020. {
  4021. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  4022. {
  4023. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  4024. {
  4025. setChargerMode(gun_index, SYS_MODE_ALARM);
  4026. }
  4027. }
  4028. }
  4029. }
  4030. //==========================================
  4031. // Reservation request check
  4032. //==========================================
  4033. checkReservation(gun_index);
  4034. //==========================================
  4035. // Change availability check
  4036. //==========================================
  4037. checkAvailability(gun_index);
  4038. if(ShmCharger->gun_info[gun_index].isOperactive)
  4039. {
  4040. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  4041. {
  4042. setChargerMode(gun_index, SYS_MODE_IDLE);
  4043. }
  4044. }
  4045. else
  4046. {
  4047. if(isMode(gun_index, SYS_MODE_IDLE))
  4048. {
  4049. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  4050. }
  4051. }
  4052. //==========================================
  4053. // Unlock Connector signal check
  4054. //==========================================
  4055. checkUnlocker(gun_index);
  4056. //==========================================
  4057. // Connector process
  4058. //==========================================
  4059. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  4060. {
  4061. case SYS_MODE_BOOTING:
  4062. if(isModeChange(gun_index))
  4063. {
  4064. setLedMotion(gun_index,LED_ACTION_INIT);
  4065. //CSU Initialization & task spawn
  4066. if((Initialization() != PASS) ||
  4067. (SpawnTask() != PASS))
  4068. {
  4069. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  4070. }
  4071. }
  4072. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  4073. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  4074. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  4075. {
  4076. ShmCharger->gun_selectd = 0;
  4077. // Firmware version
  4078. get_firmware_version(gun_index);
  4079. // OCPP BootNotification info set
  4080. DEBUG_INFO("==========================================\n");
  4081. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  4082. DEBUG_INFO("==========================================\n");
  4083. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  4084. ocpp_boot_info_sync();
  4085. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  4086. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  4087. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  4088. DEBUG_INFO("==========================================\n");
  4089. DEBUG_INFO("========== Set Wifi information ==========\n");
  4090. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  4091. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  4092. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  4093. DEBUG_INFO("==========================================\n");
  4094. // Set max current to rating current
  4095. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  4096. // Default Ethernet / Wifi / 4G to 1:disconnected
  4097. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4098. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  4099. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  4100. // If Web Server OPCC URL is empty kill Module_OcppBackend
  4101. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  4102. {
  4103. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  4104. system("pkill OcppBackend");
  4105. }
  4106. // If rotate switch equal zero, the system needs to change Debug mode
  4107. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  4108. setChargerMode(gun_index, SYS_MODE_DEBUG);
  4109. else
  4110. setChargerMode(gun_index, SYS_MODE_IDLE);
  4111. }
  4112. break;
  4113. case SYS_MODE_IDLE:
  4114. if(isModeChange(gun_index))
  4115. {
  4116. setLedMotion(gun_index,LED_ACTION_IDLE);
  4117. setRelay(gun_index,OFF);
  4118. setRequest(gun_index,OFF);
  4119. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  4120. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  4121. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4122. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4123. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4124. ocpp_set_remotestart(gun_index, OFF);
  4125. ocpp_set_remotestop(gun_index, OFF);
  4126. ocpp_set_auth_conf(OFF);
  4127. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  4128. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  4129. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  4130. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  4131. ocpp_set_unlocker_req(gun_index, OFF);
  4132. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  4133. // Response StopTransactionConf
  4134. ocpp_set_stoptransaction_conf(gun_index, OFF);
  4135. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  4136. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4137. previousData[gun_index].primaryMcuCp_Pwn_Duty = 0;
  4138. previousData[gun_index].targetCurrent = 0;
  4139. previousData[gun_index].current_limit = 0;
  4140. }
  4141. if(ocpp_get_isRemoteStartWait())
  4142. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  4143. else
  4144. setLedMotion(gun_index,LED_ACTION_IDLE);
  4145. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  4146. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  4147. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4148. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  4149. (ocpp_get_remotestart(gun_index) == ON) ||
  4150. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  4151. {
  4152. // Clean User id & Card Number
  4153. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4154. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4155. {
  4156. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4157. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  4158. DEBUG_INFO("Start Method: RFID...\n");
  4159. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4160. {
  4161. // Big endian
  4162. switch(rfid.snType)
  4163. {
  4164. case RFID_SN_TYPE_8BYTE:
  4165. 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]);
  4166. break;
  4167. case RFID_SN_TYPE_7BYTE:
  4168. 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]);
  4169. break;
  4170. case RFID_SN_TYPE_10BYTE:
  4171. 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]);
  4172. break;
  4173. case RFID_SN_TYPE_4BYTE:
  4174. default:
  4175. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4176. break;
  4177. }
  4178. }
  4179. else
  4180. {
  4181. // Little endian
  4182. switch(rfid.snType)
  4183. {
  4184. case RFID_SN_TYPE_8BYTE:
  4185. 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]);
  4186. break;
  4187. case RFID_SN_TYPE_7BYTE:
  4188. 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]);
  4189. break;
  4190. case RFID_SN_TYPE_10BYTE:
  4191. 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]);
  4192. break;
  4193. case RFID_SN_TYPE_4BYTE:
  4194. default:
  4195. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4196. break;
  4197. }
  4198. }
  4199. DEBUG_INFO("Start request UserId: %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4200. }
  4201. else if(ocpp_get_remotestart(gun_index))
  4202. {
  4203. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4204. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  4205. ocpp_copy_userid_from_remotestart(gun_index);
  4206. DEBUG_INFO("Start Method: BACKEND...\n");
  4207. }
  4208. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  4209. {
  4210. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  4211. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Local;
  4212. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  4213. DEBUG_INFO("Start Method: BLE...\n");
  4214. }
  4215. else
  4216. {
  4217. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  4218. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_NoAuthorization;
  4219. //memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  4220. ocpp_get_freevend_idtag(ShmSysConfigAndInfo->SysConfig.UserId);
  4221. DEBUG_INFO("Start Method: FREE...\n");
  4222. }
  4223. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4224. ocpp_set_remotestart(gun_index, OFF);
  4225. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4226. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  4227. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  4228. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  4229. // Get target current
  4230. ocpp_set_profile_req(gun_index, ON);
  4231. sleep(1);
  4232. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  4233. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4234. }
  4235. else
  4236. {}
  4237. break;
  4238. case SYS_MODE_AUTHORIZING:
  4239. if(isModeChange(gun_index))
  4240. {
  4241. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  4242. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  4243. {
  4244. DEBUG_INFO("Rfid start request authorize.\n");
  4245. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4246. {
  4247. case AUTH_MODE_ENABLE:
  4248. if(ocpp_get_connection_status())
  4249. {
  4250. // On line
  4251. ocpp_set_auth_req(ON, "ISO14443");
  4252. }
  4253. else
  4254. {
  4255. // Off line
  4256. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4257. {
  4258. case OFF_POLICY_LOCALLIST:
  4259. ocpp_set_auth_req(ON, "ISO14443");
  4260. break;
  4261. case OFF_POLICY_PH_RFID:
  4262. break;
  4263. case OFF_POLICY_FREE:
  4264. break;
  4265. case OFF_POLICY_NOCHARGE:
  4266. default:
  4267. break;
  4268. }
  4269. }
  4270. break;
  4271. case AUTH_MODE_DISABLE:
  4272. default:
  4273. break;
  4274. }
  4275. }
  4276. else if(ocpp_isAuthorizeRemoteStart() && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND))
  4277. {
  4278. DEBUG_INFO("Remote start request authorize.\n");
  4279. ocpp_set_auth_req(ON, "ISO14443");
  4280. }
  4281. }
  4282. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  4283. {
  4284. // Authorization timeout process.
  4285. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4286. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4287. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4288. sleep(3);
  4289. setChargerMode(gun_index, SYS_MODE_IDLE);
  4290. DEBUG_WARN("Authorize timeout !!!\n");
  4291. }
  4292. else
  4293. {
  4294. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  4295. {
  4296. case START_METHOD_RFID:
  4297. if((ocpp_get_auth_conf()) ||
  4298. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  4299. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)))
  4300. {
  4301. if(ocpp_get_auth_result(gun_index) ||
  4302. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  4303. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  4304. {
  4305. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4306. DEBUG_INFO("[START_METHOD_RFID] StartUserId: %s \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  4307. DEBUG_INFO("Authorize pass.\n");
  4308. setSpeaker(ON,SPEAKER_SHORT);
  4309. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4310. sleep(3);
  4311. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4312. }
  4313. else
  4314. {
  4315. DEBUG_INFO("Authorize fail.\n");
  4316. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4317. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4318. sleep(3);
  4319. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1)
  4320. setChargerMode(gun_index, SYS_MODE_IDLE);
  4321. else
  4322. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  4323. }
  4324. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4325. ocpp_set_auth_conf(OFF);
  4326. }
  4327. break;
  4328. case START_METHOD_BACKEND:
  4329. if(ocpp_isAuthorizeRemoteStart())
  4330. {
  4331. if(ocpp_get_auth_conf())
  4332. {
  4333. if(ocpp_get_auth_result(gun_index))
  4334. {
  4335. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4336. DEBUG_INFO("[START_METHOD_BACKEND] StartUserId: %s \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  4337. DEBUG_INFO("Authorize pass.\n");
  4338. setSpeaker(ON,SPEAKER_SHORT);
  4339. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4340. sleep(3);
  4341. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4342. }
  4343. else
  4344. {
  4345. DEBUG_INFO("Authorize fail.\n");
  4346. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4347. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4348. sleep(3);
  4349. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1)
  4350. setChargerMode(gun_index, SYS_MODE_IDLE);
  4351. else
  4352. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  4353. }
  4354. ocpp_set_auth_conf(OFF);
  4355. }
  4356. }
  4357. else
  4358. {
  4359. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4360. DEBUG_INFO("[START_METHOD_BACKEND] StartUserId: %s \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  4361. setSpeaker(ON,SPEAKER_SHORT);
  4362. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4363. }
  4364. break;
  4365. case START_METHOD_BLE:
  4366. case START_METHOD_FREE:
  4367. default:
  4368. setSpeaker(ON,SPEAKER_SHORT);
  4369. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4370. break;
  4371. }
  4372. }
  4373. break;
  4374. case SYS_MODE_PREPARING:
  4375. if(isModeChange(gun_index))
  4376. {
  4377. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  4378. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  4379. ocpp_set_profile_req(gun_index, ON);
  4380. }
  4381. // Unplug charging gun to Idle mode
  4382. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  4383. {
  4384. DEBUG_INFO("Unplug the connector.\n");
  4385. setChargerMode(gun_index, SYS_MODE_IDLE);
  4386. }
  4387. // Use RFID card to stop handshaking
  4388. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  4389. {
  4390. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  4391. setSpeaker(ON,SPEAKER_SHORT);
  4392. setChargerMode(gun_index, SYS_MODE_IDLE);
  4393. }
  4394. else
  4395. {
  4396. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4397. }
  4398. // Handshaking timeout and the system back to idle
  4399. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  4400. {
  4401. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  4402. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  4403. {
  4404. DEBUG_INFO("Handshaking timeout...\n");
  4405. setChargerMode(gun_index, SYS_MODE_IDLE);
  4406. }
  4407. }
  4408. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  4409. {
  4410. setRequest(gun_index,ON);
  4411. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  4412. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  4413. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  4414. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  4415. // Set relay on when the system is state C
  4416. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) && ((ShmCharger->gun_info[gun_index].targetCurrent != 0)))
  4417. setRelay(gun_index, ON);
  4418. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
  4419. {
  4420. ocpp_set_unlocker_req(gun_index, OFF);
  4421. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  4422. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  4423. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  4424. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  4425. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4426. ocpp_copy_userid_to_starttransaction(gun_index);
  4427. ocpp_set_starttransaction_req(gun_index, ON);
  4428. ftime(&startChargingTime[gun_index]);
  4429. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4430. }
  4431. }
  4432. break;
  4433. case SYS_MODE_CHARGING:
  4434. if(isModeChange(gun_index))
  4435. {
  4436. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4437. //ftime(&startChargingTime[gun_index]);
  4438. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4439. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4440. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4441. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4442. ocpp_set_auth_req(OFF);
  4443. ocpp_reset_smartcharging_profileId(gun_index);
  4444. ocpp_set_profile_req(gun_index, ON);
  4445. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4446. }
  4447. // If unplug the connector during a session, the system must stop charging and stop session.
  4448. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  4449. {
  4450. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
  4451. {
  4452. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  4453. DEBUG_INFO("The connector was unplugged under CHARGING.\n");
  4454. }
  4455. }
  4456. if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES) ||
  4457. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4458. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4459. ocpp_get_remotestop(gun_index) ||
  4460. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4461. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  4462. ocpp_get_reset_req() ||
  4463. ocpp_get_unlocker_req(gun_index) ||
  4464. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  4465. {
  4466. if(ShmCharger->gun_info[gun_index].rfidReq)
  4467. {
  4468. // If RFID SN different with start user, it need to authorize ID
  4469. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4470. {
  4471. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4472. {
  4473. // Request authorization
  4474. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  4475. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4476. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4477. {
  4478. // Big endian
  4479. switch(rfid.snType)
  4480. {
  4481. case RFID_SN_TYPE_8BYTE:
  4482. 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]);
  4483. break;
  4484. case RFID_SN_TYPE_7BYTE:
  4485. 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]);
  4486. break;
  4487. case RFID_SN_TYPE_10BYTE:
  4488. 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]);
  4489. break;
  4490. case RFID_SN_TYPE_4BYTE:
  4491. default:
  4492. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4493. break;
  4494. }
  4495. }
  4496. else
  4497. {
  4498. // Little endian
  4499. switch(rfid.snType)
  4500. {
  4501. case RFID_SN_TYPE_8BYTE:
  4502. 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]);
  4503. break;
  4504. case RFID_SN_TYPE_7BYTE:
  4505. 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]);
  4506. break;
  4507. case RFID_SN_TYPE_10BYTE:
  4508. 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]);
  4509. break;
  4510. case RFID_SN_TYPE_4BYTE:
  4511. default:
  4512. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4513. break;
  4514. }
  4515. }
  4516. DEBUG_INFO("End request UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4517. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4518. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4519. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4520. {
  4521. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4522. {
  4523. case AUTH_MODE_ENABLE:
  4524. if(ocpp_get_connection_status())
  4525. {
  4526. // On line
  4527. ocpp_set_auth_req(ON, "ISO14443");
  4528. }
  4529. else
  4530. {
  4531. // Off line
  4532. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4533. {
  4534. case OFF_POLICY_LOCALLIST:
  4535. ocpp_set_auth_req(ON, "ISO14443");
  4536. break;
  4537. case OFF_POLICY_PH_RFID:
  4538. break;
  4539. case OFF_POLICY_FREE:
  4540. break;
  4541. case OFF_POLICY_NOCHARGE:
  4542. default:
  4543. break;
  4544. }
  4545. }
  4546. break;
  4547. case AUTH_MODE_DISABLE:
  4548. default:
  4549. break;
  4550. }
  4551. }
  4552. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4553. {
  4554. DEBUG_INFO("Should be remote stop charger... \n");
  4555. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4556. }
  4557. }
  4558. else
  4559. {
  4560. if(ocpp_get_auth_conf() ||
  4561. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4562. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)) ||
  4563. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4564. {
  4565. if(ocpp_get_auth_result(gun_index) ||
  4566. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4567. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4568. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4569. {
  4570. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4571. }
  4572. else
  4573. {
  4574. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4575. ocpp_set_auth_req(OFF);
  4576. DEBUG_INFO("Authorize fail.\n");
  4577. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4578. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4579. sleep(3);
  4580. }
  4581. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4582. ocpp_set_auth_conf(OFF);
  4583. }
  4584. }
  4585. }
  4586. // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
  4587. if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
  4588. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4589. }
  4590. else
  4591. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4592. }
  4593. else
  4594. {
  4595. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  4596. ftime(&endChargingTime[gun_index]);
  4597. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  4598. 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;
  4599. // Response StartTransactionConf
  4600. ocpp_set_starttransaction_conf(gun_index, OFF);
  4601. // Charging profile preparation
  4602. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  4603. {
  4604. if(!ocpp_get_profile_conf(gun_index))
  4605. {
  4606. ocpp_set_profile_req(gun_index, ON);
  4607. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4608. }
  4609. else
  4610. {
  4611. ocpp_set_profile_conf(gun_index, OFF);
  4612. }
  4613. }
  4614. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  4615. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  4616. // If temperature warning derating PWM duty 20%
  4617. if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp >= SPEC_TEMPERATURE_WARN)
  4618. {
  4619. ShmCharger->gun_info[gun_index].tempCoefficient = 0.8;
  4620. }
  4621. else if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp <= SPEC_TEMPERATURE_RECOVER)
  4622. {
  4623. ShmCharger->gun_info[gun_index].tempCoefficient = 1.0;
  4624. }
  4625. 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));
  4626. // Debug information
  4627. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
  4628. {
  4629. /*
  4630. DEBUG_INFO("=============================================================\n");
  4631. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4632. 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);
  4633. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4634. DEBUG_INFO("=============================================================\n");
  4635. */
  4636. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4637. }
  4638. if(ocpp_get_connection_status())
  4639. {
  4640. // On-line max condition check
  4641. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  4642. {
  4643. setRelay(gun_index, OFF);
  4644. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4645. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4646. 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));
  4647. }
  4648. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  4649. {
  4650. setRelay(gun_index, OFF);
  4651. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4652. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4653. 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));
  4654. }
  4655. else
  4656. {
  4657. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4658. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  4659. {
  4660. setLedMotion(gun_index,LED_ACTION_CHARGING);
  4661. setRelay(gun_index, ON);
  4662. }
  4663. else
  4664. {
  4665. setLedMotion(gun_index, LED_ACTION_STOP);
  4666. setRelay(gun_index, OFF);
  4667. }
  4668. }
  4669. }
  4670. else
  4671. {
  4672. // Off-line max condition check
  4673. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4674. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  4675. {
  4676. 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))))
  4677. {
  4678. setRelay(gun_index, OFF);
  4679. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4680. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4681. 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));
  4682. }
  4683. 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))))
  4684. {
  4685. setRelay(gun_index, OFF);
  4686. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4687. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4688. 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));
  4689. }
  4690. else
  4691. {
  4692. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4693. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  4694. {
  4695. setLedMotion(gun_index,LED_ACTION_CHARGING);
  4696. setRelay(gun_index, ON);
  4697. }
  4698. else
  4699. {
  4700. setLedMotion(gun_index, LED_ACTION_STOP);
  4701. setRelay(gun_index, OFF);
  4702. }
  4703. }
  4704. }
  4705. else
  4706. {
  4707. setRelay(gun_index, OFF);
  4708. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  4709. }
  4710. }
  4711. }
  4712. break;
  4713. case SYS_MODE_TERMINATING:
  4714. if(isModeChange(gun_index))
  4715. {
  4716. setLedMotion(gun_index, LED_ACTION_STOP);
  4717. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  4718. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4719. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)
  4720. setRelay(gun_index, OFF);
  4721. sleep(1);
  4722. }
  4723. ftime(&endChargingTime[gun_index]);
  4724. //if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  4725. //{
  4726. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  4727. //}
  4728. // If unplug the connector during a session, the system must stop charging and stop session.
  4729. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  4730. {
  4731. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
  4732. {
  4733. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  4734. DEBUG_INFO("The connector was unplugged under TERMINATING.\n");
  4735. }
  4736. }
  4737. // End authorize pass
  4738. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4739. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  4740. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4741. ocpp_get_remotestop(gun_index) ||
  4742. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4743. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  4744. ocpp_get_reset_req() ||
  4745. ocpp_get_unlocker_req(gun_index) ||
  4746. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  4747. {
  4748. if((ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON) || (ShmCharger->gun_info[gun_index].isChargerStopByCondition == YES))
  4749. {
  4750. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4751. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  4752. {
  4753. DEBUG_INFO("Authorize pass.\n");
  4754. setSpeaker(ON,SPEAKER_SHORT);
  4755. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4756. sleep(3);
  4757. }
  4758. }
  4759. else
  4760. {}
  4761. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
  4762. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4763. setLedMotion(gun_index, LED_ACTION_STOP);
  4764. checkStopReason(gun_index);
  4765. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4766. }
  4767. else
  4768. {
  4769. // If the charger stops charging, the led should be stoped blink
  4770. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4771. {
  4772. setLedMotion(gun_index, LED_ACTION_STOP);
  4773. }
  4774. if(ShmCharger->gun_info[gun_index].rfidReq)
  4775. {
  4776. // If RFID SN different with start user, it need to authorize ID
  4777. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4778. {
  4779. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4780. {
  4781. // Request authorization
  4782. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  4783. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4784. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4785. {
  4786. // Big endian
  4787. switch(rfid.snType)
  4788. {
  4789. case RFID_SN_TYPE_8BYTE:
  4790. 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]);
  4791. break;
  4792. case RFID_SN_TYPE_7BYTE:
  4793. 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]);
  4794. break;
  4795. case RFID_SN_TYPE_10BYTE:
  4796. 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]);
  4797. break;
  4798. case RFID_SN_TYPE_4BYTE:
  4799. default:
  4800. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4801. break;
  4802. }
  4803. }
  4804. else
  4805. {
  4806. // Little endian
  4807. switch(rfid.snType)
  4808. {
  4809. case RFID_SN_TYPE_8BYTE:
  4810. 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]);
  4811. break;
  4812. case RFID_SN_TYPE_7BYTE:
  4813. 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]);
  4814. break;
  4815. case RFID_SN_TYPE_10BYTE:
  4816. 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]);
  4817. break;
  4818. case RFID_SN_TYPE_4BYTE:
  4819. default:
  4820. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4821. break;
  4822. }
  4823. }
  4824. DEBUG_INFO("End request UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4825. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4826. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4827. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4828. {
  4829. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4830. {
  4831. case AUTH_MODE_ENABLE:
  4832. if(ocpp_get_connection_status())
  4833. {
  4834. // On line
  4835. ocpp_set_auth_req(ON, "ISO14443");
  4836. }
  4837. else
  4838. {
  4839. // Off line
  4840. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4841. {
  4842. case OFF_POLICY_LOCALLIST:
  4843. ocpp_set_auth_req(ON, "ISO14443");
  4844. break;
  4845. case OFF_POLICY_PH_RFID:
  4846. break;
  4847. case OFF_POLICY_FREE:
  4848. break;
  4849. case OFF_POLICY_NOCHARGE:
  4850. default:
  4851. break;
  4852. }
  4853. }
  4854. break;
  4855. case AUTH_MODE_DISABLE:
  4856. default:
  4857. break;
  4858. }
  4859. }
  4860. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4861. {
  4862. DEBUG_INFO("Should be remote stop charger... \n");
  4863. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4864. }
  4865. }
  4866. else
  4867. {
  4868. if(ocpp_get_auth_conf() ||
  4869. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4870. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)) ||
  4871. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4872. {
  4873. if(ocpp_get_auth_result(gun_index) ||
  4874. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4875. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4876. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4877. {
  4878. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4879. }
  4880. else
  4881. {
  4882. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4883. ocpp_set_auth_req(OFF);
  4884. DEBUG_INFO("Authorize fail... \n");
  4885. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4886. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4887. sleep(3);
  4888. }
  4889. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4890. ocpp_set_auth_conf(OFF);
  4891. }
  4892. }
  4893. }
  4894. }
  4895. // Charging profile preparation
  4896. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  4897. {
  4898. if(!ocpp_get_profile_conf(gun_index))
  4899. {
  4900. ocpp_set_profile_req(gun_index, ON);
  4901. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4902. }
  4903. else
  4904. {
  4905. ocpp_set_profile_conf(gun_index, OFF);
  4906. }
  4907. }
  4908. // Check target current if charging profile limit > 0
  4909. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  4910. setRelay(gun_index, OFF);
  4911. // Debug information
  4912. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
  4913. {
  4914. /*
  4915. DEBUG_INFO("=============================================================\n");
  4916. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4917. 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);
  4918. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4919. DEBUG_INFO("=============================================================\n");
  4920. */
  4921. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4922. }
  4923. if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES) &&
  4924. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  4925. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  4926. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  4927. !ocpp_get_remotestop(gun_index) &&
  4928. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  4929. !ocpp_get_reset_req() &&
  4930. ocpp_get_starttransaction_result(gun_index) &&
  4931. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  4932. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  4933. !(!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)))) &&
  4934. !(!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)))))
  4935. {
  4936. sleep(1);
  4937. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4938. }
  4939. }
  4940. break;
  4941. case SYS_MODE_COMPLETE:
  4942. if(isModeChange(gun_index))
  4943. {
  4944. setLedMotion(gun_index, LED_ACTION_STOP);
  4945. sleep(6);
  4946. setRequest(gun_index, OFF);
  4947. setRelay(gun_index, OFF);
  4948. sleep(2);
  4949. }
  4950. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || ocpp_get_reset_req())
  4951. {
  4952. sleep(2);
  4953. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4954. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  4955. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4956. ocpp_set_remotestop(gun_index, OFF);
  4957. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  4958. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  4959. DB_Insert_Record(localDb, gun_index);
  4960. setChargerMode(gun_index, SYS_MODE_IDLE);
  4961. }
  4962. break;
  4963. case SYS_MODE_ALARM:
  4964. setLedMotion(gun_index,LED_ACTION_ALARM);
  4965. setRelay(gun_index, OFF);
  4966. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A))
  4967. {
  4968. setRequest(gun_index,OFF);
  4969. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
  4970. {
  4971. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  4972. DEBUG_INFO("The connector was unplugged under ALARM.\n");
  4973. }
  4974. }
  4975. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  4976. {
  4977. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4978. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4979. {
  4980. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  4981. }
  4982. else
  4983. {
  4984. setChargerMode(gun_index, SYS_MODE_IDLE);
  4985. }
  4986. }
  4987. break;
  4988. case SYS_MODE_FAULT:
  4989. if(isModeChange(gun_index))
  4990. {}
  4991. break;
  4992. case SYS_MODE_MAINTAIN:
  4993. if(isModeChange(gun_index))
  4994. {
  4995. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4996. }
  4997. break;
  4998. case SYS_MODE_UPDATE:
  4999. if(isModeChange(gun_index))
  5000. {
  5001. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5002. }
  5003. //======================================
  5004. // Check local upgrade firmware request
  5005. //======================================
  5006. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  5007. {
  5008. DEBUG_INFO("Firmware local upgrading...\n");
  5009. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  5010. int value = upgrade_check();
  5011. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  5012. if(value == PASS)
  5013. {
  5014. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  5015. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  5016. ShmCharger->isUpdateSuccess = YES;
  5017. DEBUG_INFO("Local update success...\n");
  5018. }
  5019. else
  5020. {
  5021. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  5022. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  5023. ShmCharger->isUpdateSuccess = NO;
  5024. DEBUG_INFO("Local update unsuccess...\n");
  5025. }
  5026. }
  5027. else if(ocpp_get_update_firmware_req())
  5028. {
  5029. //======================================
  5030. // Check remote upgrade firmware request
  5031. //======================================
  5032. checkRemoteUpgradeStatus();
  5033. }
  5034. else
  5035. {
  5036. //======================================
  5037. // Upgrade complete reboot system
  5038. //======================================
  5039. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  5040. {
  5041. if(ShmCharger->isUpdateSuccess == YES)
  5042. {
  5043. if((strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing")==0))
  5044. {
  5045. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5046. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5047. }
  5048. else if((strcmp((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "Installing")==0))
  5049. {
  5050. ShmOCPP16Data->SignedFirmwareStatusNotification.requestId = ShmOCPP16Data->SignedUpdateFirmware.requestId;
  5051. sprintf((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "Installed");
  5052. ShmOCPP16Data->SpMsg.bits.SignedFirmwareStatusNotificationReq = ON;
  5053. }
  5054. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5055. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5056. DEBUG_WARN("Firmware upgrade success.\n");
  5057. sleep(5);
  5058. DEBUG_INFO("Firmware upgraded, reboot...\n");
  5059. system("reboot -f");
  5060. sleep(5);
  5061. system("reboot -f");
  5062. }
  5063. else
  5064. {
  5065. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5066. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5067. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5068. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5069. DEBUG_WARN("Firmware upgrade fail.\n");
  5070. sleep(5);
  5071. system("rm -rvf /mnt/* ");
  5072. close(wtdFd);
  5073. system("/usr/bin/run_evse_restart.sh");
  5074. }
  5075. }
  5076. }
  5077. break;
  5078. case SYS_MODE_RESERVATION:
  5079. if(isModeChange(gun_index))
  5080. {
  5081. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5082. }
  5083. if(isReservationExpired(gun_index))
  5084. {
  5085. DEBUG_INFO("Reservation: Time's up...\n");
  5086. setChargerMode(gun_index, SYS_MODE_IDLE);
  5087. }
  5088. else if(ocpp_get_cancelreservation_req(gun_index))
  5089. {
  5090. DEBUG_INFO("Reservation: Cancel reservation...\n");
  5091. setChargerMode(gun_index, SYS_MODE_IDLE);
  5092. ocpp_set_cancelreservation_req(gun_index, OFF);
  5093. }
  5094. else
  5095. {
  5096. // Check is there RFID or back end request start
  5097. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  5098. ocpp_get_remotestart(gun_index))
  5099. {
  5100. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  5101. {
  5102. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5103. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  5104. {
  5105. // Big endian
  5106. switch(rfid.snType)
  5107. {
  5108. case RFID_SN_TYPE_8BYTE:
  5109. 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]);
  5110. break;
  5111. case RFID_SN_TYPE_7BYTE:
  5112. 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]);
  5113. break;
  5114. case RFID_SN_TYPE_10BYTE:
  5115. 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]);
  5116. break;
  5117. case RFID_SN_TYPE_4BYTE:
  5118. default:
  5119. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  5120. break;
  5121. }
  5122. }
  5123. else
  5124. {
  5125. // Little endian
  5126. switch(rfid.snType)
  5127. {
  5128. case RFID_SN_TYPE_8BYTE:
  5129. 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]);
  5130. break;
  5131. case RFID_SN_TYPE_7BYTE:
  5132. 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]);
  5133. break;
  5134. case RFID_SN_TYPE_10BYTE:
  5135. 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]);
  5136. break;
  5137. case RFID_SN_TYPE_4BYTE:
  5138. default:
  5139. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  5140. break;
  5141. }
  5142. }
  5143. if(ocpp_compare_reserve_id_with_user(gun_index) ||
  5144. ocpp_get_auth_result(gun_index))
  5145. {
  5146. DEBUG_INFO("Start Method in reservation : RFID...\n");
  5147. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  5148. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  5149. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  5150. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5151. }
  5152. else
  5153. {
  5154. DEBUG_INFO("It's not reserve user id.\n");
  5155. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  5156. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  5157. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  5158. //setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5159. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5160. }
  5161. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5162. }
  5163. else if(ocpp_get_remotestart(gun_index))
  5164. {
  5165. if(ocpp_compare_reserve_id_with_remote_user(gun_index) || ocpp_isAuthorizeRemoteStart())
  5166. {
  5167. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  5168. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  5169. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  5170. if(ocpp_isAuthorizeRemoteStart())
  5171. ocpp_copy_userid_from_remotestart(gun_index);
  5172. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5173. }
  5174. ocpp_set_remotestart(gun_index, OFF);
  5175. }
  5176. }
  5177. }
  5178. break;
  5179. case SYS_MODE_BOOKING:
  5180. if(isModeChange(gun_index))
  5181. {}
  5182. break;
  5183. case SYS_MODE_DEBUG:
  5184. if(isModeChange(gun_index))
  5185. {
  5186. setLedMotion(gun_index,LED_ACTION_DEBUG);
  5187. }
  5188. break;
  5189. }
  5190. }
  5191. // System watch dog reset
  5192. write(wtdFd, "a", 1);
  5193. usleep(50000);
  5194. }
  5195. return FAIL;
  5196. }