main.c 193 KB

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