main.c 195 KB

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