main.c 188 KB

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