main.c 185 KB

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