main.c 210 KB

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