main.c 195 KB

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