main.c 197 KB

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