main.c 196 KB

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