main.c 193 KB

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