main.c 193 KB

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