main.c 211 KB

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