main.c 210 KB

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