main.c 211 KB

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