main.c 195 KB

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