main.c 191 KB

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