main.c 210 KB

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