main.c 193 KB

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