main.c 199 KB

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