main.c 195 KB

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