main.c 201 KB

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