main.c 187 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202
  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("System id 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 12");
  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 12");
  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->SysConfig.Eth0Interface.EthDhcpClient==0))
  1717. {
  1718. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1719. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1720. system(tmpbuf);
  1721. }
  1722. else
  1723. {
  1724. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1725. memset(tmpbuf,0,256);
  1726. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1727. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1728. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1729. system(tmpbuf);
  1730. memset(tmpbuf,0,256);
  1731. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1732. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1733. system(tmpbuf);
  1734. }
  1735. cnt_pingDNS_Fail = 0;
  1736. }
  1737. else
  1738. {
  1739. cnt_pingDNS_Fail++;
  1740. }
  1741. }
  1742. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  1743. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  1744. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) &&
  1745. (ShmOCPP16Data->OcppConnStatus != PASS))
  1746. {
  1747. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  1748. }
  1749. else
  1750. {
  1751. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  1752. }
  1753. //============================================================
  1754. // Priority for internet 0 : First / 1 : Second / 2: Third
  1755. //============================================================
  1756. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  1757. {
  1758. system("/sbin/ifmetric eth0 0");
  1759. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1760. {
  1761. system("/sbin/ifmetric mlan0 1");
  1762. }
  1763. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1764. {
  1765. system("/sbin/ifmetric ppp0 2");
  1766. }
  1767. }
  1768. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  1769. {
  1770. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1771. {
  1772. system("/sbin/ifmetric eth0 1");
  1773. system("/sbin/ifmetric mlan0 0");
  1774. }
  1775. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1776. {
  1777. system("/sbin/ifmetric ppp0 2");
  1778. }
  1779. }
  1780. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  1781. {
  1782. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1783. {
  1784. system("/sbin/ifmetric mlan0 2");
  1785. }
  1786. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1787. {
  1788. system("/sbin/ifmetric eth0 1");
  1789. system("/sbin/ifmetric ppp0 0");
  1790. }
  1791. }
  1792. sleep(5);
  1793. }
  1794. }
  1795. DEBUG_INFO("Initial Ethernet OK\n");
  1796. }
  1797. int SpawnTask()
  1798. {
  1799. system ("pkill Module_");
  1800. system ("pkill OcppBackend");
  1801. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1802. {
  1803. system("/root/Module_4g &");
  1804. }
  1805. else if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1806. {
  1807. system("/root/Module_Wifi &");
  1808. }
  1809. system("/root/Module_EventLogging &");
  1810. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  1811. {
  1812. ocpp_process_start();
  1813. }
  1814. else
  1815. {
  1816. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  1817. ocpp_process_start();
  1818. }
  1819. system ("/root/Module_AlarmDetect &");
  1820. system ("/root/Module_InternalComm &");
  1821. system ("/root/Module_Speaker &");
  1822. system ("/root/Module_ProduceUtils &");
  1823. return PASS;
  1824. }
  1825. int Initialization()
  1826. {
  1827. int result = PASS;
  1828. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1829. InitGPIO();
  1830. InitEthernet();
  1831. if(DB_Open(localDb) != PASS)
  1832. {
  1833. DEBUG_ERROR("Local database initial fail.\n");
  1834. result = FAIL;
  1835. }
  1836. for(int gun_index=0;gun_index< AC_QUANTITY;gun_index++)
  1837. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  1838. if((rfidFd = InitRfidPort()) == FAIL)
  1839. {
  1840. DEBUG_ERROR("RFID port initial fail.\n");
  1841. result = FAIL;
  1842. }
  1843. if((wtdFd = InitWatchDog()) == FAIL)
  1844. {
  1845. DEBUG_ERROR("Watchdog initial fail.\n");
  1846. result = FAIL;
  1847. }
  1848. if(result == PASS)
  1849. DEBUG_INFO("Initialization OK.\n");
  1850. else
  1851. DEBUG_INFO("Initialization Fail.\n");
  1852. return result;
  1853. }
  1854. //=====================================================
  1855. // Common routine
  1856. //=====================================================
  1857. char* getSystemModeName(unsigned char mode)
  1858. {
  1859. char* result;
  1860. switch(mode)
  1861. {
  1862. case SYS_MODE_BOOTING:
  1863. result = "booting";
  1864. break;
  1865. case SYS_MODE_IDLE:
  1866. result = "idle";
  1867. break;
  1868. case SYS_MODE_AUTHORIZING:
  1869. result = "authorizing";
  1870. break;
  1871. case SYS_MODE_PREPARING:
  1872. result = "preparing";
  1873. break;
  1874. case SYS_MODE_CHARGING:
  1875. result = "charging";
  1876. break;
  1877. case SYS_MODE_TERMINATING:
  1878. result = "terminating";
  1879. break;
  1880. case SYS_MODE_COMPLETE:
  1881. result = "complete";
  1882. break;
  1883. case SYS_MODE_ALARM:
  1884. result = "alarm";
  1885. break;
  1886. case SYS_MODE_FAULT:
  1887. result = "fault";
  1888. break;
  1889. case SYS_MODE_MAINTAIN:
  1890. result = "maintain";
  1891. break;
  1892. case SYS_MODE_RESERVATION:
  1893. result = "reservation";
  1894. break;
  1895. case SYS_MODE_BOOKING:
  1896. result = "booking";
  1897. break;
  1898. case SYS_MODE_DEBUG:
  1899. result = "debug";
  1900. break;
  1901. case SYS_MODE_UPDATE:
  1902. result = "upgrade";
  1903. break;
  1904. default:
  1905. result = "unknown";
  1906. break;
  1907. }
  1908. return result;
  1909. }
  1910. void setChargerMode(unsigned char gun_index, unsigned char mode)
  1911. {
  1912. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1913. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  1914. 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));
  1915. }
  1916. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  1917. {
  1918. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  1919. }
  1920. unsigned char isModeChange(unsigned char gun_index)
  1921. {
  1922. unsigned char result = NO;
  1923. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  1924. {
  1925. result = YES;
  1926. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1927. }
  1928. return result;
  1929. }
  1930. void gpio_set_value(unsigned int gpio, unsigned int value)
  1931. {
  1932. int fd;
  1933. char buf[256];
  1934. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  1935. fd = open(buf, O_WRONLY);
  1936. if (fd < 0)
  1937. {
  1938. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  1939. return;
  1940. }
  1941. if (value)
  1942. write(fd, "1", 2);
  1943. else
  1944. write(fd, "0", 2);
  1945. close(fd);
  1946. }
  1947. int gpio_get_value(unsigned int gpio)
  1948. {
  1949. int fd;
  1950. char buf[256];
  1951. char ch;
  1952. int8_t result = FAIL;
  1953. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  1954. fd = open(buf, O_RDONLY);
  1955. if (fd < 0)
  1956. {
  1957. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  1958. return result;
  1959. }
  1960. read(fd, &ch, 1);
  1961. if (ch != '0')
  1962. result = 1;
  1963. else
  1964. result = 0;
  1965. close(fd);
  1966. return result;
  1967. }
  1968. //===============================================
  1969. // Get firmware version
  1970. //===============================================
  1971. void get_firmware_version(unsigned char gun_index)
  1972. {
  1973. FILE *fp;
  1974. char cmd[512];
  1975. char buf[512];
  1976. // Get CSU hardware version
  1977. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  1978. // Get CSU boot loader version
  1979. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1980. // Get CSU kernel version
  1981. sprintf(cmd, "/bin/uname -r");
  1982. fp = popen(cmd, "r");
  1983. if(fp == NULL)
  1984. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1985. else
  1986. {
  1987. while(fgets(buf, sizeof(buf), fp) != NULL)
  1988. {
  1989. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  1990. }
  1991. }
  1992. // Get MCU firmware version
  1993. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1994. // Get CSU root file system version
  1995. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "V0.70.00.0000.00");
  1996. // Get AC connector type from model name
  1997. for(uint8_t idx=0;idx<3;idx++)
  1998. {
  1999. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2000. {
  2001. case '1':
  2002. // J1772 Plug
  2003. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  2004. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  2005. break;
  2006. case '2':
  2007. // J1772 Socket
  2008. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  2009. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  2010. break;
  2011. case '3':
  2012. // CE Plug
  2013. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  2014. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  2015. break;
  2016. case '4':
  2017. // CE Socket
  2018. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  2019. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  2020. break;
  2021. case '5':
  2022. // GB Plug
  2023. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  2024. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  2025. break;
  2026. case '6':
  2027. // GB Socket
  2028. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  2029. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  2030. break;
  2031. }
  2032. }
  2033. // Get network option from model name
  2034. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  2035. {
  2036. case 'B':
  2037. case 'U':
  2038. //Blue tooth
  2039. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  2040. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  2041. break;
  2042. case 'W':
  2043. // WIFI
  2044. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  2045. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  2046. break;
  2047. case 'T':
  2048. // 3G/4G
  2049. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  2050. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  2051. break;
  2052. case 'D':
  2053. // WIFI + 4G
  2054. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  2055. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  2056. break;
  2057. default:
  2058. // LAN
  2059. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  2060. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  2061. break;
  2062. }
  2063. // Get rating power from model name
  2064. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2065. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2066. // Get vender code from model name
  2067. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2068. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2069. DEBUG_INFO("========================================\n");
  2070. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2071. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  2072. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  2073. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  2074. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2075. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2076. DEBUG_INFO("========================================\n");
  2077. }
  2078. //===============================================
  2079. // Upgrade firmware
  2080. //===============================================
  2081. int upgrade_check()
  2082. {
  2083. int result = PASS;
  2084. int fd;
  2085. DIR *dir;
  2086. struct dirent *file;
  2087. char cmd[512];
  2088. long int MaxLen=48*1024*1024;
  2089. if ((dir = opendir ("/mnt")) != NULL)
  2090. {
  2091. /* print all the files and directories within directory */
  2092. while ((file = readdir (dir)) != NULL)
  2093. {
  2094. if(strlen(file->d_name)>2)
  2095. {
  2096. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  2097. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  2098. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  2099. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  2100. {
  2101. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2102. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2103. read(fd, ptr, MaxLen);
  2104. close(fd);
  2105. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  2106. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  2107. free(ptr);
  2108. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2109. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  2110. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  2111. {
  2112. switch(ShmCharger->fwUpgradeInfo.fwType)
  2113. {
  2114. case CSU_BOOTLOADER:
  2115. case CSU_KERNEL_CONFIGURATION:
  2116. case CSU_KERNEL_IMAGE:
  2117. case CSU_ROOT_FILE_SYSTEM:
  2118. case CSU_USER_CONFIGURATION:
  2119. case CSU_PRIMARY_CONTROLLER:
  2120. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  2121. {
  2122. result = FAIL;
  2123. }
  2124. else
  2125. {
  2126. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  2127. {
  2128. DEBUG_INFO("Restore model name & serial number.\n");
  2129. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2130. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2131. {
  2132. DEBUG_INFO("Re-load configuration fail.\n");
  2133. result = FAIL;
  2134. }
  2135. else
  2136. {
  2137. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  2138. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  2139. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  2140. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2141. {
  2142. DEBUG_INFO("Re-write configuration fail.\n");
  2143. result = FAIL;
  2144. }
  2145. else
  2146. DEBUG_INFO("Re-write configuration OK.\n");
  2147. }
  2148. }
  2149. }
  2150. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2151. system(cmd);
  2152. break;
  2153. case AC_WALLMOUNT_CONTROLLER:
  2154. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2155. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  2156. sleep(10);
  2157. break;
  2158. default:
  2159. result = FAIL;
  2160. DEBUG_WARN("Image file is unknown type.\n");
  2161. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2162. system(cmd);
  2163. break;
  2164. }
  2165. }
  2166. else
  2167. {
  2168. result = FAIL;
  2169. DEBUG_ERROR("Model name and Firmware type error.\n");
  2170. }
  2171. }
  2172. else
  2173. {
  2174. result = FAIL;
  2175. DEBUG_ERROR("New firmware open error.\n");
  2176. }
  2177. }
  2178. else
  2179. {
  2180. if(strlen(file->d_name) >= 3)
  2181. {
  2182. result = FAIL;
  2183. DEBUG_ERROR("File name error.\n");
  2184. }
  2185. else
  2186. {
  2187. DEBUG_ERROR("Searching file.\n");
  2188. }
  2189. }
  2190. }
  2191. closedir (dir);
  2192. }
  2193. else
  2194. {
  2195. result = FAIL;
  2196. DEBUG_ERROR("/mnt does not valid.\n");
  2197. }
  2198. return result;
  2199. }
  2200. //===============================================
  2201. // Check RFID is match with start user
  2202. //===============================================
  2203. int isMatchStartUser(unsigned char gun_index)
  2204. {
  2205. uint8_t tmpUser[32];
  2206. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2207. {
  2208. // Big endian
  2209. switch(rfid.snType)
  2210. {
  2211. case RFID_SN_TYPE_6BYTE:
  2212. 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]);
  2213. break;
  2214. case RFID_SN_TYPE_7BYTE:
  2215. 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]);
  2216. break;
  2217. case RFID_SN_TYPE_10BYTE:
  2218. 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]);
  2219. break;
  2220. case RFID_SN_TYPE_4BYTE:
  2221. default:
  2222. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2223. break;
  2224. }
  2225. }
  2226. else
  2227. {
  2228. // Little endian
  2229. switch(rfid.snType)
  2230. {
  2231. case RFID_SN_TYPE_6BYTE:
  2232. 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]);
  2233. break;
  2234. case RFID_SN_TYPE_7BYTE:
  2235. 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]);
  2236. break;
  2237. case RFID_SN_TYPE_10BYTE:
  2238. 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]);
  2239. break;
  2240. case RFID_SN_TYPE_4BYTE:
  2241. default:
  2242. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2243. break;
  2244. }
  2245. }
  2246. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2247. }
  2248. //===============================================
  2249. // Read RFID Serial Number
  2250. //===============================================
  2251. int GetCardSerialNumber()
  2252. {
  2253. int isSuccess = FAIL;
  2254. int module_type = MODULE_EWT;
  2255. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2256. {
  2257. if(rfid.cardType == ISO14443A)
  2258. {
  2259. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2260. {
  2261. isSuccess = PASS;
  2262. }
  2263. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2264. {
  2265. isSuccess = PASS;
  2266. }
  2267. sethaltCard(rfidFd,module_type);
  2268. }
  2269. else if(rfid.cardType == IS014443B)
  2270. {
  2271. isSuccess = PASS;
  2272. }
  2273. else if(rfid.cardType == FELICA)
  2274. {
  2275. isSuccess = PASS;
  2276. }
  2277. else
  2278. {}
  2279. }
  2280. else
  2281. {}
  2282. return isSuccess;
  2283. }
  2284. //===============================================
  2285. // Set led motion
  2286. //===============================================
  2287. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  2288. {
  2289. switch(led_mode)
  2290. {
  2291. case LED_ACTION_INIT:
  2292. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  2293. break;
  2294. case LED_ACTION_IDLE:
  2295. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  2296. break;
  2297. case LED_ACTION_AUTHED:
  2298. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  2299. break;
  2300. case LED_ACTION_CONNECTED:
  2301. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  2302. break;
  2303. case LED_ACTION_CHARGING:
  2304. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  2305. break;
  2306. case LED_ACTION_STOP:
  2307. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  2308. break;
  2309. case LED_ACTION_ALARM:
  2310. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  2311. break;
  2312. case LED_ACTION_MAINTAIN:
  2313. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  2314. break;
  2315. case LED_ACTION_RFID_PASS:
  2316. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  2317. break;
  2318. case LED_ACTION_RFID_FAIL:
  2319. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  2320. break;
  2321. case LED_ACTION_BLE_CONNECT:
  2322. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  2323. break;
  2324. case LED_ACTION_BLE_DISABLE:
  2325. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  2326. break;
  2327. case LED_ACTION_DEBUG:
  2328. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  2329. break;
  2330. case LED_ACTION_ALL_OFF:
  2331. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  2332. break;
  2333. case LED_RELAY_ON:
  2334. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  2335. break;
  2336. case LED_RELAY_OFF:
  2337. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  2338. break;
  2339. case LED_ACTION_HANDSHAKE_FAIL:
  2340. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  2341. break;
  2342. case LED_ACTION_INTERNET_DISCONNECT:
  2343. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  2344. break;
  2345. }
  2346. }
  2347. //===============================================
  2348. // Request on/off set (PWM)
  2349. //===============================================
  2350. void setRequest(unsigned char gun_index,unsigned char isOn)
  2351. {
  2352. if(isOn == ON)
  2353. {
  2354. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  2355. {
  2356. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  2357. DEBUG_INFO("Gun-%d permission request: ON \n",gun_index);
  2358. }
  2359. }
  2360. else
  2361. {
  2362. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2363. {
  2364. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  2365. DEBUG_INFO("Gun-%d permission request: OFF \n",gun_index);
  2366. }
  2367. }
  2368. }
  2369. //===============================================
  2370. // Request on/off get (PWM)
  2371. //===============================================
  2372. int getRequest(unsigned char gun_index)
  2373. {
  2374. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  2375. }
  2376. //===============================================
  2377. // Relay on/off set (Relay)
  2378. //===============================================
  2379. void setRelay(unsigned char gun_index,unsigned char isOn)
  2380. {
  2381. if(isOn == ON)
  2382. {
  2383. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  2384. {
  2385. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  2386. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  2387. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  2388. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  2389. DEBUG_INFO("Gun-%d Output relay status: ON. \n",gun_index);
  2390. }
  2391. }
  2392. else
  2393. {
  2394. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  2395. {
  2396. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  2397. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  2398. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  2399. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  2400. DEBUG_INFO("Gun-%d Output relay status: OFF. \n",gun_index);
  2401. }
  2402. }
  2403. }
  2404. //===============================================
  2405. // Relay on/off get (Relay)
  2406. //===============================================
  2407. int getRelay(unsigned char gun_index)
  2408. {
  2409. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  2410. }
  2411. //===============================================
  2412. // Set speaker on/off request
  2413. //===============================================
  2414. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  2415. {
  2416. if(isOn == ON)
  2417. {
  2418. ShmCharger->isSpeakerOn = ON;
  2419. ShmCharger->speaker_type = speaker_mode;
  2420. }
  2421. }
  2422. //===============================================
  2423. // Initialization RFID communication port
  2424. //===============================================
  2425. int InitRfidPort()
  2426. {
  2427. int uartO2 = open(rfidPortName, O_RDWR);
  2428. struct termios tios;
  2429. if (uartO2 != FAIL)
  2430. {
  2431. ioctl (uartO2, TCGETS, &tios);
  2432. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  2433. tios.c_lflag = 0;
  2434. tios.c_iflag = 0;
  2435. tios.c_oflag = 0;
  2436. tios.c_cc[VMIN] = 0;
  2437. tios.c_cc[VTIME] = (unsigned char) 1;
  2438. tios.c_lflag = 0;
  2439. tcflush(uartO2, TCIFLUSH);
  2440. ioctl(uartO2, TCSETS, &tios);
  2441. }
  2442. if (uartO2 < 0)
  2443. {
  2444. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  2445. }
  2446. return uartO2;
  2447. }
  2448. //===============================================
  2449. // Check internet access status
  2450. //===============================================
  2451. int isReachableInternet()
  2452. {
  2453. int result = FAIL;
  2454. FILE *fp;
  2455. char cmd[256];
  2456. char buf[512];
  2457. char tmp[512];
  2458. strcpy(cmd, "ifconfig eth0");
  2459. fp = popen(cmd, "r");
  2460. if(fp != NULL)
  2461. {
  2462. while(fgets(buf, sizeof(buf), fp) != NULL)
  2463. {
  2464. if (strstr(buf, "inet addr:") > 0)
  2465. {
  2466. sscanf(buf, "%*s%s", tmp);
  2467. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  2468. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != 0)
  2469. {
  2470. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  2471. }
  2472. sscanf(buf, "%*s%*s%*s%s", tmp);
  2473. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  2474. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  2475. {
  2476. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  2477. }
  2478. }
  2479. }
  2480. }
  2481. pclose(fp);
  2482. memset(buf, 0x00, sizeof(buf));
  2483. // Get gateway
  2484. fp = popen("ip route", "r");
  2485. if(fp == NULL)
  2486. result = FAIL;
  2487. else
  2488. {
  2489. while(fgets(buf, sizeof(buf), fp) != NULL)
  2490. {
  2491. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  2492. break;
  2493. }
  2494. if(strstr(buf, "default") != NULL)
  2495. {
  2496. sscanf(buf, "%*s%*s%s", tmp);
  2497. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  2498. }
  2499. }
  2500. pclose(fp);
  2501. memset(buf, 0x00, sizeof(buf));
  2502. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  2503. {
  2504. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  2505. fp = popen(cmd, "r");
  2506. if(fp != NULL)
  2507. {
  2508. while(fgets(buf, sizeof(buf), fp) != NULL)
  2509. {
  2510. if(strstr(buf, "transmitted") > 0)
  2511. {
  2512. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  2513. if(strstr(buf,"100%") != NULL)
  2514. {
  2515. }
  2516. else
  2517. {
  2518. result = PASS;
  2519. }
  2520. //DEBUG_INFO("%s",buf);
  2521. //DEBUG_INFO("%s\n",tmp);
  2522. }
  2523. }
  2524. }
  2525. pclose(fp);
  2526. }
  2527. return result;
  2528. }
  2529. //===============================================
  2530. // Check route for eth0
  2531. //===============================================
  2532. int isRouteFail()
  2533. {
  2534. int result = YES;
  2535. FILE *fp;
  2536. char buf[512];
  2537. fp = popen("route -n", "r");
  2538. if(fp != NULL)
  2539. {
  2540. while(fgets(buf, sizeof(buf), fp) != NULL)
  2541. {
  2542. if(strstr(buf, "eth0") != NULL)
  2543. result = NO;
  2544. }
  2545. }
  2546. pclose(fp);
  2547. return result;
  2548. }
  2549. //===============================================
  2550. // Check reservation date is expired
  2551. //===============================================
  2552. int isReservationExpired(unsigned char gun_index)
  2553. {
  2554. int result = NO;
  2555. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2556. {
  2557. if(isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
  2558. result = YES;
  2559. }
  2560. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2561. {
  2562. if(isOvertNow(ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime))
  2563. result = YES;
  2564. }
  2565. return result;
  2566. }
  2567. //===============================================
  2568. // Check charging profile related date routine
  2569. //===============================================
  2570. int isProfileValid(uint8_t gun_index)
  2571. {
  2572. int result = NO;
  2573. struct tm tmFrom, tmTo;
  2574. struct timeb tbFrom, tbTo;
  2575. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2576. {
  2577. 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) &&
  2578. (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))
  2579. {
  2580. tmFrom.tm_year -= 1900;
  2581. tmFrom.tm_mon -= 1;
  2582. tbFrom.time = mktime(&tmFrom);
  2583. tmTo.tm_year -= 1900;
  2584. tmTo.tm_mon -= 1;
  2585. tbTo.time = mktime(&tmTo);
  2586. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2587. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2588. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2589. {
  2590. result = YES;
  2591. }
  2592. }
  2593. else
  2594. {
  2595. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2596. }
  2597. }
  2598. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2599. {
  2600. 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) &&
  2601. (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))
  2602. {
  2603. tmFrom.tm_year -= 1900;
  2604. tmFrom.tm_mon -= 1;
  2605. tbFrom.time = mktime(&tmFrom);
  2606. tmTo.tm_year -= 1900;
  2607. tmTo.tm_mon -= 1;
  2608. tbTo.time = mktime(&tmTo);
  2609. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2610. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2611. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2612. {
  2613. result = YES;
  2614. }
  2615. }
  2616. else
  2617. {
  2618. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2619. }
  2620. }
  2621. return result;
  2622. }
  2623. int getScheduleStart(int gun_index)
  2624. {
  2625. int result = -1;
  2626. struct tm tmScheduleStart;;
  2627. struct timeb tbScheduleStart;
  2628. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2629. {
  2630. 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))
  2631. {
  2632. tmScheduleStart.tm_year -= 1900;
  2633. tmScheduleStart.tm_mon -= 1;
  2634. tbScheduleStart.time = mktime(&tmScheduleStart);
  2635. tbScheduleStart.millitm = 0;
  2636. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2637. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  2638. }
  2639. else
  2640. {
  2641. DEBUG_WARN("Schedule start date parsing error.\n");
  2642. }
  2643. }
  2644. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2645. {
  2646. 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))
  2647. {
  2648. tmScheduleStart.tm_year -= 1900;
  2649. tmScheduleStart.tm_mon -= 1;
  2650. tbScheduleStart.time = mktime(&tmScheduleStart);
  2651. tbScheduleStart.millitm = 0;
  2652. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2653. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  2654. }
  2655. else
  2656. {
  2657. DEBUG_WARN("Schedule start date parsing error.\n");
  2658. }
  2659. }
  2660. return result;
  2661. }
  2662. int getStartSinceToday()
  2663. {
  2664. int result = -1;
  2665. time_t t;
  2666. struct tm *tmStartToday;
  2667. struct timeb tbStartToday;
  2668. t=time(NULL);
  2669. tmStartToday=localtime(&t);
  2670. tmStartToday->tm_hour = 0;
  2671. tmStartToday->tm_min = 0;
  2672. tmStartToday->tm_sec = 0;
  2673. tbStartToday.time = mktime(tmStartToday);
  2674. result = DiffTimebWithNow(tbStartToday)/1000;
  2675. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  2676. return result;
  2677. }
  2678. int getStartSinceWeek()
  2679. {
  2680. int result = -1;
  2681. time_t t;
  2682. struct tm *tmStartWeek;
  2683. struct timeb tbStartWeek;
  2684. t=time(NULL);
  2685. tmStartWeek=localtime(&t);
  2686. t-=86400*tmStartWeek->tm_wday;
  2687. tmStartWeek=localtime(&t);
  2688. tmStartWeek->tm_hour = 0;
  2689. tmStartWeek->tm_min = 0;
  2690. tmStartWeek->tm_sec = 0;
  2691. tbStartWeek.time = mktime(tmStartWeek);
  2692. result = DiffTimebWithNow(tbStartWeek)/1000;
  2693. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  2694. return result;
  2695. }
  2696. //===============================================
  2697. // Valid from local white list
  2698. //===============================================
  2699. int isValidLocalWhiteCard()
  2700. {
  2701. uint8_t result = FAIL;
  2702. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  2703. {
  2704. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  2705. {
  2706. result = PASS;
  2707. }
  2708. }
  2709. return result;
  2710. }
  2711. //==========================================
  2712. // Check routine
  2713. //==========================================
  2714. void checkTask()
  2715. {
  2716. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2717. {
  2718. if(system("pidof -s Module_4g > /dev/null") != 0)
  2719. {
  2720. DEBUG_INFO("Module_4g not running, restart it.\n");
  2721. system("/root/Module_4g &");
  2722. }
  2723. }
  2724. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2725. {
  2726. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  2727. {
  2728. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  2729. system("/root/Module_Wifi &");
  2730. }
  2731. }
  2732. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  2733. {
  2734. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  2735. system("/root/Module_EventLogging &");
  2736. }
  2737. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  2738. {
  2739. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2740. {
  2741. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  2742. {
  2743. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  2744. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  2745. system("pkill OcppBackend");
  2746. sleep(3);
  2747. ocpp_process_start();
  2748. }
  2749. if(system("pidof -s OcppBackend > /dev/null") != 0)
  2750. {
  2751. DEBUG_INFO("OcppBackend not running, restart it.\n");
  2752. ocpp_process_start();
  2753. }
  2754. }
  2755. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2756. {
  2757. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  2758. {
  2759. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  2760. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  2761. system("pkill OcppBackend20");
  2762. sleep(3);
  2763. ocpp_process_start();
  2764. }
  2765. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  2766. {
  2767. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  2768. ocpp_process_start();
  2769. }
  2770. }
  2771. }
  2772. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  2773. {
  2774. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  2775. system("/root/Module_AlarmDetect &");
  2776. }
  2777. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  2778. {
  2779. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  2780. system("/root/Module_InternalComm &");
  2781. }
  2782. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  2783. {
  2784. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  2785. system("/root/Module_Speaker &");
  2786. }
  2787. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  2788. {
  2789. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  2790. system ("/root/Module_ProduceUtils &");
  2791. }
  2792. /*
  2793. if((system("pidof -s Module_InitUpgrade > /dev/null") != 0) &&
  2794. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
  2795. {
  2796. DEBUG_INFO("Module_InitUpgrade not running, restart it.\n");
  2797. system ("/root/Module_InitUpgrade &");
  2798. }*/
  2799. }
  2800. void checkConnectionTimeout()
  2801. {
  2802. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  2803. {
  2804. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2805. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  2806. }
  2807. else
  2808. {
  2809. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  2810. {
  2811. ShmCharger->timeoutSpec.Setting_Timeout_Spec = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2812. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  2813. {
  2814. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2815. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  2816. }
  2817. else
  2818. {
  2819. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  2820. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  2821. }
  2822. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2823. }
  2824. else
  2825. {
  2826. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2827. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Table is blank...\n.");
  2828. }
  2829. //DEBUG_INFO("Present timeout spec : %d \n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  2830. }
  2831. }
  2832. void checkReset()
  2833. {
  2834. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2835. {
  2836. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2837. {
  2838. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2839. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2840. {
  2841. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  2842. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2843. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  2844. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  2845. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2846. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2847. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  2848. {
  2849. system("sync");
  2850. sleep(5);
  2851. system("reboot -f");
  2852. sleep(5);
  2853. system("reboot -f");
  2854. }
  2855. else
  2856. {
  2857. sleep(5);
  2858. close(wtdFd);
  2859. system("/usr/bin/run_evse_restart.sh");
  2860. }
  2861. }
  2862. }
  2863. }
  2864. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2865. {
  2866. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  2867. {
  2868. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2869. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2870. {
  2871. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  2872. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  2873. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  2874. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  2875. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2876. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2877. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  2878. {
  2879. system("sync");
  2880. sleep(5);
  2881. system("reboot -f");
  2882. sleep(5);
  2883. system("reboot -f");
  2884. }
  2885. else
  2886. {
  2887. sleep(5);
  2888. close(wtdFd);
  2889. system("/usr/bin/run_evse_restart.sh");
  2890. }
  2891. }
  2892. }
  2893. }
  2894. }
  2895. void checkReservation(uint8_t gun_index)
  2896. {
  2897. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2898. {
  2899. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  2900. {
  2901. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2902. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2903. {
  2904. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  2905. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2906. }
  2907. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2908. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2909. }
  2910. }
  2911. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2912. {
  2913. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  2914. {
  2915. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2916. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2917. {
  2918. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  2919. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2920. }
  2921. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2922. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2923. }
  2924. }
  2925. }
  2926. void checkUnlocker(uint8_t gun_index)
  2927. {
  2928. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2929. {
  2930. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2931. {
  2932. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2933. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  2934. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  2935. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  2936. }
  2937. }
  2938. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2939. {
  2940. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2941. {
  2942. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2943. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "NotSupported");
  2944. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  2945. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  2946. }
  2947. }
  2948. }
  2949. void checkAvailability(uint8_t gun_index)
  2950. {
  2951. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2952. {
  2953. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2954. {
  2955. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  2956. {
  2957. DB_Update_Operactive(localDb, gun_index, true);
  2958. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2959. }
  2960. else
  2961. {
  2962. DB_Update_Operactive(localDb, gun_index, false);
  2963. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2964. }
  2965. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2966. }
  2967. }
  2968. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2969. {
  2970. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2971. {
  2972. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  2973. {
  2974. DB_Update_Operactive(localDb, gun_index, true);
  2975. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2976. }
  2977. else
  2978. {
  2979. DB_Update_Operactive(localDb, gun_index, false);
  2980. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2981. }
  2982. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2983. }
  2984. }
  2985. }
  2986. void checkChargingProfileLimit(uint8_t gun_index)
  2987. {
  2988. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2989. {
  2990. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  2991. (ocpp_get_profile_req(gun_index) != ON) &&
  2992. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2993. {
  2994. // Debug information
  2995. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2996. {
  2997. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2998. DEBUG_INFO("Valid from: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2999. DEBUG_INFO("Valid to: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  3000. DEBUG_INFO("Start schedule: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  3001. DEBUG_INFO("Profile kind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  3002. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  3003. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  3004. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  3005. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  3006. DEBUG_INFO("========================================\n");
  3007. }
  3008. // Checking profile kind
  3009. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  3010. {
  3011. // Absolute profile
  3012. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  3013. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  3014. {
  3015. // Checking limitation
  3016. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3017. {
  3018. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3019. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3020. )
  3021. {
  3022. 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);
  3023. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3024. }
  3025. else
  3026. break;
  3027. }
  3028. }
  3029. }
  3030. }
  3031. else if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == 0) && (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  3032. {
  3033. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3034. }
  3035. }
  3036. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3037. {
  3038. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  3039. (ocpp_get_profile_req(gun_index) != ON) &&
  3040. (((strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom)>0) && (strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo)>0)) ? isProfileValid(gun_index) : ON))
  3041. {
  3042. // Debug information
  3043. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  3044. {
  3045. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].id);
  3046. DEBUG_INFO("Valid from: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom);
  3047. DEBUG_INFO("Valid to: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validTo);
  3048. DEBUG_INFO("Start schedule: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  3049. DEBUG_INFO("Profile kind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind);
  3050. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].recurrencyKind);
  3051. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose);
  3052. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId);
  3053. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingRateUnit);
  3054. DEBUG_INFO("=======================================\n");
  3055. }
  3056. // Checking profile kind
  3057. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  3058. {
  3059. // Absolute profile
  3060. if(((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId == ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId)) ||
  3061. (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == FAIL))
  3062. {
  3063. // Checking limitation
  3064. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3065. {
  3066. if((getScheduleStart(gun_index) > ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3067. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3068. )
  3069. {
  3070. 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);
  3071. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3072. }
  3073. else
  3074. break;
  3075. }
  3076. }
  3077. }
  3078. }
  3079. else if((ShmOCPP20Data->SmartChargingProfile[gun_index].id == 0) && (ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  3080. {
  3081. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3082. }
  3083. }
  3084. }
  3085. void checkStopReason(uint8_t gun_index)
  3086. {
  3087. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3088. {
  3089. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3090. if(!ocpp_get_starttransaction_result(gun_index))
  3091. {
  3092. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  3093. }
  3094. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3095. {
  3096. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  3097. }
  3098. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3099. {
  3100. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3101. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3102. }
  3103. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3104. {
  3105. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3106. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3107. else
  3108. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3109. }
  3110. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3111. {
  3112. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3113. if(!isMatchStartUser(gun_index))
  3114. {
  3115. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3116. }
  3117. else
  3118. {
  3119. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3120. }
  3121. DEBUG_INFO("Gun-[%d] : IdTag [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  3122. }
  3123. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3124. {
  3125. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3126. }
  3127. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3128. {
  3129. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3130. }
  3131. else
  3132. {
  3133. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3134. }
  3135. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3136. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3137. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3138. }
  3139. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3140. {
  3141. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3142. if(!ocpp_get_starttransaction_result(gun_index))
  3143. {
  3144. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  3145. }
  3146. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3147. {
  3148. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  3149. }
  3150. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3151. {
  3152. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  3153. }
  3154. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  3155. {
  3156. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  3157. }
  3158. else if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3159. {
  3160. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0)
  3161. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  3162. else
  3163. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OnIdle");
  3164. }
  3165. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3166. {
  3167. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  3168. if(!isMatchStartUser(gun_index))
  3169. {
  3170. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3171. }
  3172. else
  3173. {
  3174. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3175. }
  3176. DEBUG_INFO("Gun-[%d] : idToken [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  3177. }
  3178. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_OVER_CURRENT))
  3179. {
  3180. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  3181. }
  3182. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  3183. {
  3184. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  3185. }
  3186. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  3187. {
  3188. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  3189. }
  3190. else
  3191. {
  3192. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  3193. }
  3194. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  3195. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3196. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  3197. }
  3198. }
  3199. void checkRemoteUpgradeStatus()
  3200. {
  3201. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3202. {
  3203. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  3204. {
  3205. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3206. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3207. ShmCharger->isUpdateSuccess = NO;
  3208. }
  3209. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  3210. {
  3211. DEBUG_INFO("Firmware remote upgrading...\n");
  3212. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3213. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3214. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3215. int result = upgrade_check();
  3216. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3217. if(result == PASS)
  3218. {
  3219. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3220. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3221. ShmCharger->isUpdateSuccess = YES;
  3222. DEBUG_INFO("Remote update success...\n");
  3223. }
  3224. else
  3225. {
  3226. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3227. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3228. ShmCharger->isUpdateSuccess = NO;
  3229. DEBUG_INFO("Remote update unsuccess...\n");
  3230. }
  3231. }
  3232. else
  3233. {}
  3234. }
  3235. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3236. {
  3237. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  3238. {
  3239. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3240. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3241. ShmCharger->isUpdateSuccess = NO;
  3242. }
  3243. else if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0)
  3244. {
  3245. DEBUG_INFO("Firmware remote upgrading...\n");
  3246. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3247. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3248. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3249. int result = upgrade_check();
  3250. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3251. if(result == PASS)
  3252. {
  3253. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3254. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3255. ShmCharger->isUpdateSuccess = YES;
  3256. DEBUG_INFO("Remote update success...\n");
  3257. }
  3258. else
  3259. {
  3260. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3261. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3262. ShmCharger->isUpdateSuccess = NO;
  3263. DEBUG_INFO("Remote update unsuccess...\n");
  3264. }
  3265. }
  3266. else
  3267. {}
  3268. }
  3269. }
  3270. //===============================================
  3271. // Main process
  3272. //===============================================
  3273. int main(void)
  3274. {
  3275. //Create all share memory
  3276. if(CreatShareMemory()==0)
  3277. {
  3278. DEBUG_ERROR("CreatShareMemory NG\n");
  3279. if(ShmStatusCodeData!=NULL)
  3280. {
  3281. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  3282. }
  3283. sleep(5);
  3284. system("reboot -f");
  3285. sleep(5);
  3286. system("reboot -f");
  3287. }
  3288. else
  3289. {
  3290. DEBUG_INFO("CreatShareMemory OK\n");
  3291. }
  3292. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3293. {
  3294. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  3295. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  3296. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  3297. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  3298. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  3299. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  3300. }
  3301. // Main loop
  3302. for(;;)
  3303. {
  3304. //==========================================
  3305. // Synchronize share memory from OCPP struct
  3306. //==========================================
  3307. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  3308. //==============================================
  3309. // Period check for 10 seconds
  3310. //==============================================
  3311. if((DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10000) || (DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
  3312. {
  3313. //==============================================
  3314. // Check task processing
  3315. //==============================================
  3316. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3317. checkTask();
  3318. //==============================================
  3319. // Check connection timeout specification
  3320. //==============================================
  3321. checkConnectionTimeout();
  3322. ftime(&startTime[0][TMR_IDX_CHECK_TASK]);
  3323. }
  3324. //==========================================
  3325. // Something need run in Idle mode
  3326. //==========================================
  3327. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  3328. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  3329. {
  3330. //======================================
  3331. // Check restore factory setting request
  3332. //======================================
  3333. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3334. {
  3335. // Set led to initial
  3336. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3337. {
  3338. setLedMotion(gun_index,LED_ACTION_INIT);
  3339. }
  3340. sleep(5);
  3341. system("cd /root;./Module_FactoryConfig -m");
  3342. system("rm -f /Storage/OCPP/OCPPConfiguration");
  3343. system("sync");
  3344. sleep(5);
  3345. system("reboot -f");
  3346. sleep(5);
  3347. system("reboot -f");
  3348. }
  3349. //======================================
  3350. // Check upgrade firmware request
  3351. //======================================
  3352. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  3353. ocpp_get_update_firmware_req())
  3354. {
  3355. // If available memory too small, free memory cache first
  3356. if(getAvailableMemory() < (200*1024*1024))
  3357. {
  3358. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  3359. system("echo 3 > /proc/sys/vm/drop_caches");
  3360. }
  3361. ShmCharger->isUpdateSuccess = NO;
  3362. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3363. {
  3364. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3365. setChargerMode(gun_index, SYS_MODE_UPDATE);
  3366. }
  3367. }
  3368. }
  3369. //==========================================
  3370. // Check remote reset request
  3371. //==========================================
  3372. checkReset();
  3373. //==========================================
  3374. // Check RFID
  3375. //==========================================
  3376. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  3377. {
  3378. if(GetCardSerialNumber()!= FAIL)
  3379. {
  3380. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  3381. }
  3382. }
  3383. //==========================================
  3384. // Connector loop
  3385. //==========================================
  3386. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3387. {
  3388. // Synchronize current rating value from MCU
  3389. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3390. // Assign connector location index for OCPP
  3391. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  3392. // Synchronize present charging power
  3393. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  3394. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  3395. else
  3396. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  3397. // Check initialization "PASS" or "FAIL"
  3398. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3399. {
  3400. // Alarm event check
  3401. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  3402. {
  3403. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  3404. {
  3405. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  3406. {
  3407. setChargerMode(gun_index, SYS_MODE_ALARM);
  3408. }
  3409. }
  3410. }
  3411. }
  3412. // Reservation request check
  3413. checkReservation(gun_index);
  3414. // Change availability check
  3415. checkAvailability(gun_index);
  3416. if(ShmCharger->gun_info[gun_index].isOperactive)
  3417. {
  3418. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  3419. {
  3420. setChargerMode(gun_index, SYS_MODE_IDLE);
  3421. }
  3422. }
  3423. else
  3424. {
  3425. if(isMode(gun_index, SYS_MODE_IDLE))
  3426. {
  3427. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  3428. }
  3429. }
  3430. // Unlock Connector signal check
  3431. checkUnlocker(gun_index);
  3432. // Connector process
  3433. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  3434. {
  3435. case SYS_MODE_BOOTING:
  3436. if(isModeChange(gun_index))
  3437. {
  3438. setLedMotion(gun_index,LED_ACTION_INIT);
  3439. //CSU Initialization & task spawn
  3440. if((Initialization() != PASS) ||
  3441. (SpawnTask() != PASS))
  3442. {
  3443. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  3444. }
  3445. }
  3446. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  3447. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  3448. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  3449. {
  3450. ShmCharger->gun_selectd = 0;
  3451. // Firmware version
  3452. get_firmware_version(gun_index);
  3453. // OCPP BootNotification info set
  3454. DEBUG_INFO("==========================================\n");
  3455. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  3456. DEBUG_INFO("==========================================\n");
  3457. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  3458. ocpp_boot_info_sync();
  3459. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  3460. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  3461. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  3462. DEBUG_INFO("==========================================\n");
  3463. DEBUG_INFO("========== Set Wifi information ==========\n");
  3464. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  3465. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  3466. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  3467. DEBUG_INFO("==========================================\n");
  3468. // Set max current to rating current
  3469. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3470. // Default Ethernet / Wifi / 4G to 1:disconnected
  3471. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3472. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  3473. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  3474. // If Web Server OPCC URL is empty kill Module_OcppBackend
  3475. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  3476. {
  3477. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  3478. system ("pkill OcppBackend");
  3479. }
  3480. // If rotate switch equal zero, the system needs to change Debug mode
  3481. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  3482. setChargerMode(gun_index, SYS_MODE_DEBUG);
  3483. else
  3484. setChargerMode(gun_index, SYS_MODE_IDLE);
  3485. }
  3486. break;
  3487. case SYS_MODE_IDLE:
  3488. if(isModeChange(gun_index))
  3489. {
  3490. setLedMotion(gun_index,LED_ACTION_IDLE);
  3491. setRelay(gun_index,OFF);
  3492. setRequest(gun_index,OFF);
  3493. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3494. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3495. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3496. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3497. ocpp_set_remotestart(gun_index, OFF);
  3498. ocpp_set_remotestop(gun_index, OFF);
  3499. ocpp_set_auth_conf(OFF);
  3500. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  3501. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  3502. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  3503. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  3504. ocpp_set_unlocker_req(gun_index, OFF);
  3505. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  3506. // Response StopTransactionConf
  3507. ocpp_set_stoptransaction_conf(gun_index, OFF);
  3508. }
  3509. if(ocpp_get_isRemoteStartWait())
  3510. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  3511. else
  3512. setLedMotion(gun_index,LED_ACTION_IDLE);
  3513. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  3514. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  3515. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3516. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  3517. (ocpp_get_remotestart(gun_index) == ON) ||
  3518. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  3519. {
  3520. // Clean User id & Card Number
  3521. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3522. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  3523. {
  3524. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  3525. DEBUG_INFO("Start Method : RFID...\n");
  3526. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3527. {
  3528. // Big endian
  3529. switch(rfid.snType)
  3530. {
  3531. case RFID_SN_TYPE_6BYTE:
  3532. 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]);
  3533. break;
  3534. case RFID_SN_TYPE_7BYTE:
  3535. 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]);
  3536. break;
  3537. case RFID_SN_TYPE_10BYTE:
  3538. 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]);
  3539. break;
  3540. case RFID_SN_TYPE_4BYTE:
  3541. default:
  3542. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3543. break;
  3544. }
  3545. }
  3546. else
  3547. {
  3548. // Little endian
  3549. switch(rfid.snType)
  3550. {
  3551. case RFID_SN_TYPE_6BYTE:
  3552. 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]);
  3553. break;
  3554. case RFID_SN_TYPE_7BYTE:
  3555. 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]);
  3556. break;
  3557. case RFID_SN_TYPE_10BYTE:
  3558. 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]);
  3559. break;
  3560. case RFID_SN_TYPE_4BYTE:
  3561. default:
  3562. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3563. break;
  3564. }
  3565. }
  3566. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3567. }
  3568. else if(ocpp_get_remotestart(gun_index))
  3569. {
  3570. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  3571. ocpp_copy_userid_from_remotestart(gun_index);
  3572. DEBUG_INFO("Start Method : BACKEND...\n");
  3573. }
  3574. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  3575. {
  3576. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  3577. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  3578. DEBUG_INFO("Start Method : BLE...\n");
  3579. }
  3580. else
  3581. {
  3582. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  3583. //memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  3584. ocpp_get_freevend_idtag(ShmSysConfigAndInfo->SysConfig.UserId);
  3585. DEBUG_INFO("Start Method : FREE...\n");
  3586. }
  3587. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3588. ocpp_set_remotestart(gun_index, OFF);
  3589. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3590. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3591. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  3592. // Get target current
  3593. ocpp_set_profile_req(gun_index, ON);
  3594. sleep(1);
  3595. checkChargingProfileLimit(gun_index);
  3596. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3597. {
  3598. 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);
  3599. if((6 <= ShmCharger->gun_info[gun_index].targetCurrent))
  3600. {
  3601. 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);
  3602. }
  3603. else if((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent < 6))
  3604. {
  3605. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 6;
  3606. }
  3607. else
  3608. {
  3609. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
  3610. }
  3611. }
  3612. else
  3613. {
  3614. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3615. if((6 <= ShmCharger->gun_info[gun_index].targetCurrent))
  3616. {
  3617. 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);
  3618. }
  3619. else if((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent < 6))
  3620. {
  3621. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 6;
  3622. }
  3623. else
  3624. {
  3625. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
  3626. }
  3627. }
  3628. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3629. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3630. }
  3631. else
  3632. {}
  3633. break;
  3634. case SYS_MODE_AUTHORIZING:
  3635. if(isModeChange(gun_index))
  3636. {
  3637. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3638. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  3639. {
  3640. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3641. {
  3642. case AUTH_MODE_ENABLE:
  3643. if(ocpp_get_connection_status())
  3644. {
  3645. // On line
  3646. ocpp_set_auth_req(ON, "ISO14443");
  3647. }
  3648. else
  3649. {
  3650. // Off line
  3651. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3652. {
  3653. case OFF_POLICY_LOCALLIST:
  3654. ocpp_set_auth_req(ON, "ISO14443");
  3655. break;
  3656. case OFF_POLICY_PH_RFID:
  3657. break;
  3658. case OFF_POLICY_FREE:
  3659. break;
  3660. case OFF_POLICY_NOCHARGE:
  3661. default:
  3662. break;
  3663. }
  3664. }
  3665. break;
  3666. case AUTH_MODE_DISABLE:
  3667. default:
  3668. break;
  3669. }
  3670. }
  3671. else if(ocpp_isAuthorizeRemoteStart() && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND))
  3672. {
  3673. DEBUG_INFO("Remote start request authorize.\n");
  3674. ocpp_set_auth_req(ON, "ISO14443");
  3675. }
  3676. }
  3677. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  3678. {
  3679. // Authorization timeout process.
  3680. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3681. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3682. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3683. sleep(3);
  3684. setChargerMode(gun_index, SYS_MODE_IDLE);
  3685. DEBUG_WARN("Authorize timeout !!!\n");
  3686. }
  3687. else
  3688. {
  3689. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  3690. {
  3691. case START_METHOD_RFID:
  3692. if((ocpp_get_auth_conf()) ||
  3693. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  3694. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)))
  3695. {
  3696. if(ocpp_get_auth_result(gun_index) ||
  3697. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  3698. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  3699. {
  3700. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3701. DEBUG_INFO("Authorize pass.\n");
  3702. setSpeaker(ON,SPEAKER_SHORT);
  3703. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3704. sleep(3);
  3705. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3706. }
  3707. else
  3708. {
  3709. DEBUG_INFO("Authorize fail.\n");
  3710. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3711. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3712. sleep(3);
  3713. setChargerMode(gun_index, SYS_MODE_IDLE);
  3714. }
  3715. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3716. ocpp_set_auth_conf(OFF);
  3717. }
  3718. break;
  3719. case START_METHOD_BACKEND:
  3720. if(ocpp_isAuthorizeRemoteStart())
  3721. {
  3722. if(ocpp_get_auth_conf())
  3723. {
  3724. if(ocpp_get_auth_result(gun_index))
  3725. {
  3726. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3727. DEBUG_INFO("Authorize pass.\n");
  3728. setSpeaker(ON,SPEAKER_SHORT);
  3729. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3730. sleep(3);
  3731. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3732. }
  3733. else
  3734. {
  3735. DEBUG_INFO("Authorize fail.\n");
  3736. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3737. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3738. sleep(3);
  3739. setChargerMode(gun_index, SYS_MODE_IDLE);
  3740. }
  3741. ocpp_set_auth_conf(OFF);
  3742. }
  3743. }
  3744. else
  3745. {
  3746. setSpeaker(ON,SPEAKER_SHORT);
  3747. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3748. }
  3749. break;
  3750. case START_METHOD_BLE:
  3751. case START_METHOD_FREE:
  3752. default:
  3753. setSpeaker(ON,SPEAKER_SHORT);
  3754. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3755. break;
  3756. }
  3757. }
  3758. break;
  3759. case SYS_MODE_PREPARING:
  3760. if(isModeChange(gun_index))
  3761. {
  3762. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3763. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  3764. }
  3765. // If control pilot detect Bx, skip watch dog time out.
  3766. /*
  3767. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  3768. {
  3769. setRequest(gun_index,ON);
  3770. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3771. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  3772. }
  3773. */
  3774. // Unplug charging gun to Idle mode
  3775. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  3776. {
  3777. DEBUG_INFO("Charging gun is plugged before.\n");
  3778. setChargerMode(gun_index, SYS_MODE_IDLE);
  3779. }
  3780. // Use RFID card to stop handshaking
  3781. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  3782. {
  3783. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  3784. setSpeaker(ON,SPEAKER_SHORT);
  3785. setChargerMode(gun_index, SYS_MODE_IDLE);
  3786. }
  3787. else
  3788. {
  3789. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3790. }
  3791. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  3792. {
  3793. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  3794. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  3795. {
  3796. DEBUG_INFO("Handshaking timeout...\n");
  3797. setChargerMode(gun_index, SYS_MODE_IDLE);
  3798. }
  3799. }
  3800. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  3801. {
  3802. setRequest(gun_index,ON);
  3803. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3804. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  3805. // Set relay on when the system is state C
  3806. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  3807. setRelay(gun_index, ON);
  3808. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
  3809. {
  3810. ocpp_set_unlocker_req(gun_index, OFF);
  3811. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  3812. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  3813. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  3814. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3815. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3816. ocpp_copy_userid_to_starttransaction(gun_index);
  3817. ocpp_set_starttransaction_req(gun_index, ON);
  3818. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3819. }
  3820. }
  3821. break;
  3822. case SYS_MODE_CHARGING:
  3823. if(isModeChange(gun_index))
  3824. {
  3825. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3826. ftime(&startChargingTime[gun_index]);
  3827. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  3828. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  3829. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3830. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3831. ocpp_set_auth_req(OFF);
  3832. ocpp_reset_smartcharging_profileId(gun_index);
  3833. ocpp_set_profile_req(gun_index, ON);
  3834. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  3835. }
  3836. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3837. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  3838. ocpp_get_remotestop(gun_index) ||
  3839. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  3840. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  3841. ocpp_get_reset_req() ||
  3842. ocpp_get_unlocker_req(gun_index) ||
  3843. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  3844. {
  3845. if(ShmCharger->gun_info[gun_index].rfidReq)
  3846. {
  3847. DEBUG_INFO("Certified in charging mode... \n");
  3848. // If RFID SN different with start user, it need to authorize ID
  3849. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  3850. {
  3851. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  3852. {
  3853. // Request authorization
  3854. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3855. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3856. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3857. {
  3858. // Big endian
  3859. switch(rfid.snType)
  3860. {
  3861. case RFID_SN_TYPE_6BYTE:
  3862. 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]);
  3863. break;
  3864. case RFID_SN_TYPE_7BYTE:
  3865. 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]);
  3866. break;
  3867. case RFID_SN_TYPE_10BYTE:
  3868. 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]);
  3869. break;
  3870. case RFID_SN_TYPE_4BYTE:
  3871. default:
  3872. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3873. break;
  3874. }
  3875. }
  3876. else
  3877. {
  3878. // Little endian
  3879. switch(rfid.snType)
  3880. {
  3881. case RFID_SN_TYPE_6BYTE:
  3882. 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]);
  3883. break;
  3884. case RFID_SN_TYPE_7BYTE:
  3885. 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]);
  3886. break;
  3887. case RFID_SN_TYPE_10BYTE:
  3888. 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]);
  3889. break;
  3890. case RFID_SN_TYPE_4BYTE:
  3891. default:
  3892. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3893. break;
  3894. }
  3895. }
  3896. DEBUG_INFO("End request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3897. DEBUG_INFO("Start method : %d\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  3898. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  3899. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  3900. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  3901. {
  3902. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3903. {
  3904. case AUTH_MODE_ENABLE:
  3905. if(ocpp_get_connection_status())
  3906. {
  3907. // On line
  3908. ocpp_set_auth_req(ON, "ISO14443");
  3909. }
  3910. else
  3911. {
  3912. // Off line
  3913. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3914. {
  3915. case OFF_POLICY_LOCALLIST:
  3916. ocpp_set_auth_req(ON, "ISO14443");
  3917. break;
  3918. case OFF_POLICY_PH_RFID:
  3919. break;
  3920. case OFF_POLICY_FREE:
  3921. break;
  3922. case OFF_POLICY_NOCHARGE:
  3923. default:
  3924. break;
  3925. }
  3926. }
  3927. break;
  3928. case AUTH_MODE_DISABLE:
  3929. default:
  3930. break;
  3931. }
  3932. }
  3933. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  3934. {
  3935. DEBUG_INFO("Should be remote stop charger... \n");
  3936. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3937. }
  3938. }
  3939. else
  3940. {
  3941. if(ocpp_get_auth_conf() ||
  3942. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3943. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)) ||
  3944. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3945. {
  3946. if(ocpp_get_auth_result(gun_index) ||
  3947. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3948. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  3949. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3950. {
  3951. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  3952. }
  3953. else
  3954. {
  3955. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3956. ocpp_set_auth_req(OFF);
  3957. DEBUG_INFO("Authorize fail.\n");
  3958. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3959. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3960. sleep(3);
  3961. }
  3962. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3963. ocpp_set_auth_conf(OFF);
  3964. }
  3965. }
  3966. }
  3967. // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
  3968. if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
  3969. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3970. }
  3971. else
  3972. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3973. }
  3974. else
  3975. {
  3976. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3977. ftime(&endChargingTime[gun_index]);
  3978. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  3979. 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;
  3980. // Response StartTransactionConf
  3981. ocpp_set_starttransaction_conf(gun_index, OFF);
  3982. // Charging profile preparation
  3983. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  3984. {
  3985. if(!ocpp_get_profile_conf(gun_index))
  3986. {
  3987. ocpp_set_profile_req(gun_index, ON);
  3988. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  3989. }
  3990. else
  3991. {
  3992. ocpp_set_profile_conf(gun_index, OFF);
  3993. }
  3994. }
  3995. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  3996. checkChargingProfileLimit(gun_index);
  3997. // If temperature warning derating PWM duty 20%
  3998. if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp >= SPEC_TEMPERATURE_WARN)
  3999. {
  4000. ShmCharger->gun_info[gun_index].tempCoefficient = 0.8;
  4001. }
  4002. else if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp <= SPEC_TEMPERATURE_RECOVER)
  4003. {
  4004. ShmCharger->gun_info[gun_index].tempCoefficient = 1.0;
  4005. }
  4006. 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));
  4007. // Determine max charging current to MCU
  4008. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4009. {
  4010. 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);
  4011. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4012. {
  4013. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4014. {
  4015. if((6 <= ShmCharger->gun_info[gun_index].targetCurrent))
  4016. {
  4017. 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);
  4018. }
  4019. else if((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent < 6))
  4020. {
  4021. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 6;
  4022. }
  4023. else
  4024. {
  4025. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
  4026. }
  4027. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4028. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4029. }
  4030. else
  4031. {}
  4032. }
  4033. }
  4034. else
  4035. {
  4036. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4037. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4038. {
  4039. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4040. {
  4041. if((6 <= ShmCharger->gun_info[gun_index].targetCurrent))
  4042. {
  4043. 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);
  4044. }
  4045. else if((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent < 6))
  4046. {
  4047. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 6;
  4048. }
  4049. else
  4050. {
  4051. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
  4052. }
  4053. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4054. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4055. }
  4056. else
  4057. {}
  4058. }
  4059. }
  4060. // Debug information
  4061. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
  4062. {
  4063. DEBUG_INFO("=============================================================\n");
  4064. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4065. 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);
  4066. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4067. DEBUG_INFO("=============================================================\n");
  4068. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4069. }
  4070. if(ocpp_get_connection_status())
  4071. {
  4072. // On-line max condition check
  4073. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  4074. {
  4075. setRelay(gun_index, OFF);
  4076. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4077. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4078. 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));
  4079. }
  4080. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  4081. {
  4082. setRelay(gun_index, OFF);
  4083. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4084. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4085. 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));
  4086. }
  4087. else
  4088. {
  4089. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4090. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  4091. {
  4092. setLedMotion(gun_index,LED_ACTION_CHARGING);
  4093. setRelay(gun_index, ON);
  4094. }
  4095. else
  4096. {
  4097. setLedMotion(gun_index, LED_ACTION_STOP);
  4098. setRelay(gun_index, OFF);
  4099. }
  4100. }
  4101. }
  4102. else
  4103. {
  4104. // Off-line max condition check
  4105. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4106. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  4107. {
  4108. 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))))
  4109. {
  4110. setRelay(gun_index, OFF);
  4111. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4112. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4113. 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));
  4114. }
  4115. 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))))
  4116. {
  4117. setRelay(gun_index, OFF);
  4118. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4119. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4120. 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));
  4121. }
  4122. else
  4123. {
  4124. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4125. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  4126. {
  4127. setLedMotion(gun_index,LED_ACTION_CHARGING);
  4128. setRelay(gun_index, ON);
  4129. }
  4130. else
  4131. {
  4132. setLedMotion(gun_index, LED_ACTION_STOP);
  4133. setRelay(gun_index, OFF);
  4134. }
  4135. }
  4136. }
  4137. else
  4138. {
  4139. setRelay(gun_index, OFF);
  4140. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  4141. }
  4142. }
  4143. }
  4144. break;
  4145. case SYS_MODE_TERMINATING:
  4146. if(isModeChange(gun_index))
  4147. {
  4148. setLedMotion(gun_index, LED_ACTION_STOP);
  4149. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  4150. {
  4151. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  4152. }
  4153. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  4154. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4155. sleep(3);
  4156. }
  4157. // End authorize pass
  4158. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4159. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  4160. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4161. ocpp_get_remotestop(gun_index) ||
  4162. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4163. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  4164. ocpp_get_reset_req() ||
  4165. ocpp_get_unlocker_req(gun_index) ||
  4166. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  4167. {
  4168. if((ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON) || (ShmCharger->gun_info[gun_index].isChargerStopByCondition == YES))
  4169. {
  4170. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4171. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  4172. {
  4173. DEBUG_INFO("Authorize pass.\n");
  4174. setSpeaker(ON,SPEAKER_SHORT);
  4175. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4176. sleep(3);
  4177. }
  4178. }
  4179. else
  4180. {}
  4181. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
  4182. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4183. setLedMotion(gun_index, LED_ACTION_STOP);
  4184. checkStopReason(gun_index);
  4185. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4186. /*
  4187. // If relay is off, the system should change to complete mode
  4188. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4189. {
  4190. checkStopReason(gun_index);
  4191. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4192. }
  4193. */
  4194. }
  4195. else
  4196. {
  4197. // If the charger stops charging, the led should be stoped blink
  4198. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4199. {
  4200. setLedMotion(gun_index, LED_ACTION_STOP);
  4201. }
  4202. if(ShmCharger->gun_info[gun_index].rfidReq)
  4203. {
  4204. DEBUG_INFO("Certified in terminating mode... \n");
  4205. // If RFID SN different with start user, it need to authorize ID
  4206. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4207. {
  4208. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4209. {
  4210. // Request authorization
  4211. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  4212. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4213. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4214. {
  4215. // Big endian
  4216. switch(rfid.snType)
  4217. {
  4218. case RFID_SN_TYPE_6BYTE:
  4219. 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]);
  4220. break;
  4221. case RFID_SN_TYPE_7BYTE:
  4222. 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]);
  4223. break;
  4224. case RFID_SN_TYPE_10BYTE:
  4225. 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]);
  4226. break;
  4227. case RFID_SN_TYPE_4BYTE:
  4228. default:
  4229. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4230. break;
  4231. }
  4232. }
  4233. else
  4234. {
  4235. // Little endian
  4236. switch(rfid.snType)
  4237. {
  4238. case RFID_SN_TYPE_6BYTE:
  4239. 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]);
  4240. break;
  4241. case RFID_SN_TYPE_7BYTE:
  4242. 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]);
  4243. break;
  4244. case RFID_SN_TYPE_10BYTE:
  4245. 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]);
  4246. break;
  4247. case RFID_SN_TYPE_4BYTE:
  4248. default:
  4249. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4250. break;
  4251. }
  4252. }
  4253. DEBUG_INFO("End request User Id : %s... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4254. DEBUG_INFO("Start method : %d... \n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  4255. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4256. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4257. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4258. {
  4259. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4260. {
  4261. case AUTH_MODE_ENABLE:
  4262. if(ocpp_get_connection_status())
  4263. {
  4264. // On line
  4265. ocpp_set_auth_req(ON, "ISO14443");
  4266. }
  4267. else
  4268. {
  4269. // Off line
  4270. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4271. {
  4272. case OFF_POLICY_LOCALLIST:
  4273. ocpp_set_auth_req(ON, "ISO14443");
  4274. break;
  4275. case OFF_POLICY_PH_RFID:
  4276. break;
  4277. case OFF_POLICY_FREE:
  4278. break;
  4279. case OFF_POLICY_NOCHARGE:
  4280. default:
  4281. break;
  4282. }
  4283. }
  4284. break;
  4285. case AUTH_MODE_DISABLE:
  4286. default:
  4287. break;
  4288. }
  4289. }
  4290. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4291. {
  4292. DEBUG_INFO("Should be remote stop charger... \n");
  4293. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4294. }
  4295. }
  4296. else
  4297. {
  4298. if(ocpp_get_auth_conf() ||
  4299. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4300. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)) ||
  4301. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4302. {
  4303. if(ocpp_get_auth_result(gun_index) ||
  4304. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4305. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4306. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4307. {
  4308. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4309. }
  4310. else
  4311. {
  4312. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4313. ocpp_set_auth_req(OFF);
  4314. DEBUG_INFO("Authorize fail... \n");
  4315. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4316. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4317. sleep(3);
  4318. }
  4319. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4320. ocpp_set_auth_conf(OFF);
  4321. }
  4322. }
  4323. }
  4324. }
  4325. // Charging profile preparation
  4326. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  4327. {
  4328. if(!ocpp_get_profile_conf(gun_index))
  4329. {
  4330. ocpp_set_profile_req(gun_index, ON);
  4331. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4332. }
  4333. else
  4334. {
  4335. ocpp_set_profile_conf(gun_index, OFF);
  4336. }
  4337. }
  4338. // Check target current if charging profile limit > 0
  4339. checkChargingProfileLimit(gun_index);
  4340. // Determine max charging current to MCU
  4341. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4342. {
  4343. 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);
  4344. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4345. {
  4346. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4347. {
  4348. if((6 <= ShmCharger->gun_info[gun_index].targetCurrent))
  4349. {
  4350. 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);
  4351. }
  4352. else if((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent < 6))
  4353. {
  4354. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 6;
  4355. }
  4356. else
  4357. {
  4358. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
  4359. }
  4360. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4361. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4362. }
  4363. else
  4364. {}
  4365. }
  4366. }
  4367. else
  4368. {
  4369. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4370. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4371. {
  4372. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4373. {
  4374. if((6 <= ShmCharger->gun_info[gun_index].targetCurrent))
  4375. {
  4376. 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);
  4377. }
  4378. else if((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent < 6))
  4379. {
  4380. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 6;
  4381. }
  4382. else
  4383. {
  4384. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
  4385. }
  4386. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4387. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4388. }
  4389. else
  4390. {}
  4391. }
  4392. }
  4393. // Debug information
  4394. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
  4395. {
  4396. DEBUG_INFO("=============================================================\n");
  4397. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4398. 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);
  4399. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4400. DEBUG_INFO("=============================================================\n");
  4401. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4402. }
  4403. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  4404. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  4405. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  4406. !ocpp_get_remotestop(gun_index) &&
  4407. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  4408. !ocpp_get_reset_req() &&
  4409. ocpp_get_starttransaction_result(gun_index) &&
  4410. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  4411. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  4412. !(!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)))) &&
  4413. !(!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)))))
  4414. {
  4415. sleep(1);
  4416. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4417. }
  4418. }
  4419. break;
  4420. case SYS_MODE_COMPLETE:
  4421. if(isModeChange(gun_index))
  4422. {
  4423. setLedMotion(gun_index, LED_ACTION_STOP);
  4424. sleep(6);
  4425. setRequest(gun_index, OFF);
  4426. setRelay(gun_index, OFF);
  4427. sleep(2);
  4428. }
  4429. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || ocpp_get_reset_req())
  4430. {
  4431. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4432. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  4433. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4434. ocpp_set_remotestop(gun_index, OFF);
  4435. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  4436. DB_Insert_Record(localDb, gun_index);
  4437. setChargerMode(gun_index, SYS_MODE_IDLE);
  4438. }
  4439. break;
  4440. case SYS_MODE_ALARM:
  4441. setLedMotion(gun_index,LED_ACTION_ALARM);
  4442. setRelay(gun_index, OFF);
  4443. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  4444. {
  4445. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4446. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4447. {
  4448. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  4449. }
  4450. else
  4451. {
  4452. setChargerMode(gun_index, SYS_MODE_IDLE);
  4453. }
  4454. }
  4455. break;
  4456. case SYS_MODE_FAULT:
  4457. if(isModeChange(gun_index))
  4458. {}
  4459. break;
  4460. case SYS_MODE_MAINTAIN:
  4461. if(isModeChange(gun_index))
  4462. {
  4463. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4464. }
  4465. break;
  4466. case SYS_MODE_UPDATE:
  4467. if(isModeChange(gun_index))
  4468. {
  4469. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4470. }
  4471. //======================================
  4472. // Check local upgrade firmware request
  4473. //======================================
  4474. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  4475. {
  4476. DEBUG_INFO("Firmware local upgrading...\n");
  4477. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  4478. int value = upgrade_check();
  4479. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  4480. if(value == PASS)
  4481. {
  4482. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4483. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4484. ShmCharger->isUpdateSuccess = YES;
  4485. DEBUG_INFO("Local update success...\n");
  4486. }
  4487. else
  4488. {
  4489. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4490. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4491. ShmCharger->isUpdateSuccess = NO;
  4492. DEBUG_INFO("Local update unsuccess...\n");
  4493. }
  4494. }
  4495. else if(ocpp_get_update_firmware_req())
  4496. {
  4497. //======================================
  4498. // Check remote upgrade firmware request
  4499. //======================================
  4500. checkRemoteUpgradeStatus();
  4501. }
  4502. else
  4503. {
  4504. //======================================
  4505. // Upgrade complete reboot system
  4506. //======================================
  4507. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  4508. {
  4509. if(ShmCharger->isUpdateSuccess == YES)
  4510. {
  4511. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4512. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4513. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  4514. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4515. DEBUG_WARN("Firmware upgrade success.\n");
  4516. sleep(5);
  4517. DEBUG_INFO("Firmware upgraded, reboot...\n");
  4518. system("reboot -f");
  4519. sleep(5);
  4520. system("reboot -f");
  4521. }
  4522. else
  4523. {
  4524. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  4525. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4526. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  4527. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4528. DEBUG_WARN("Firmware upgrade fail.\n");
  4529. sleep(5);
  4530. system("rm -rvf /mnt/* ");
  4531. close(wtdFd);
  4532. system("/usr/bin/run_evse_restart.sh");
  4533. }
  4534. }
  4535. }
  4536. break;
  4537. case SYS_MODE_RESERVATION:
  4538. if(isModeChange(gun_index))
  4539. {
  4540. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4541. }
  4542. if(isReservationExpired(gun_index))
  4543. {
  4544. DEBUG_INFO("Reservation: Time's up...\n");
  4545. setChargerMode(gun_index, SYS_MODE_IDLE);
  4546. }
  4547. else if(ocpp_get_cancelreservation_req(gun_index))
  4548. {
  4549. DEBUG_INFO("Reservation: Cancel reservation...\n");
  4550. setChargerMode(gun_index, SYS_MODE_IDLE);
  4551. ocpp_set_cancelreservation_req(gun_index, OFF);
  4552. }
  4553. else
  4554. {
  4555. // Check is there RFID or back end request start
  4556. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4557. ocpp_get_remotestart(gun_index))
  4558. {
  4559. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4560. {
  4561. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4562. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4563. {
  4564. // Big 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[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  4569. break;
  4570. case RFID_SN_TYPE_7BYTE:
  4571. 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]);
  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[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]);
  4575. break;
  4576. case RFID_SN_TYPE_4BYTE:
  4577. default:
  4578. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4579. break;
  4580. }
  4581. }
  4582. else
  4583. {
  4584. // Little endian
  4585. switch(rfid.snType)
  4586. {
  4587. case RFID_SN_TYPE_6BYTE:
  4588. 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]);
  4589. break;
  4590. case RFID_SN_TYPE_7BYTE:
  4591. 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]);
  4592. break;
  4593. case RFID_SN_TYPE_10BYTE:
  4594. 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]);
  4595. break;
  4596. case RFID_SN_TYPE_4BYTE:
  4597. default:
  4598. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4599. break;
  4600. }
  4601. }
  4602. if(ocpp_compare_reserve_id_with_user(gun_index))
  4603. {
  4604. DEBUG_INFO("Start Method in reservation : RFID...\n");
  4605. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4606. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4607. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4608. }
  4609. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4610. }
  4611. else if(ocpp_get_remotestart(gun_index))
  4612. {
  4613. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  4614. {
  4615. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  4616. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4617. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4618. }
  4619. ocpp_set_remotestop(gun_index, OFF);
  4620. }
  4621. }
  4622. }
  4623. break;
  4624. case SYS_MODE_BOOKING:
  4625. if(isModeChange(gun_index))
  4626. {}
  4627. break;
  4628. case SYS_MODE_DEBUG:
  4629. if(isModeChange(gun_index))
  4630. {
  4631. setLedMotion(gun_index,LED_ACTION_DEBUG);
  4632. }
  4633. break;
  4634. }
  4635. }
  4636. // System watch dog reset
  4637. write(wtdFd, "a", 1);
  4638. usleep(50000);
  4639. }
  4640. return FAIL;
  4641. }