main.c 216 KB

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