main.c 189 KB

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