main.c 204 KB

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