main.c 214 KB

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