main.c 190 KB

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