main.c 194 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248
  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.47.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. {
  2627. DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
  2628. system ("/root/Module_PowerSharing &");
  2629. }
  2630. if((system("pidof -s Module_InitUpgrade > /dev/null") != 0) &&
  2631. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
  2632. {
  2633. DEBUG_INFO("Module_InitUpgrade not running, restart it.\n");
  2634. system ("/root/Module_InitUpgrade &");
  2635. }
  2636. }
  2637. void checkConnectionTimeout()
  2638. {
  2639. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  2640. {
  2641. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2642. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  2643. }
  2644. else
  2645. {
  2646. ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
  2647. if((ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  2648. {
  2649. ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE+10000);
  2650. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  2651. }
  2652. else
  2653. {
  2654. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  2655. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  2656. }
  2657. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2658. }
  2659. }
  2660. void checkReset()
  2661. {
  2662. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2663. {
  2664. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2665. {
  2666. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2667. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2668. {
  2669. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  2670. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2671. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  2672. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  2673. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  2674. {
  2675. system("sync");
  2676. sleep(5);
  2677. system("reboot -f");
  2678. sleep(5);
  2679. system("reboot -f");
  2680. }
  2681. else
  2682. {
  2683. sleep(5);
  2684. close(wtdFd);
  2685. system("/usr/bin/run_evse_restart.sh");
  2686. }
  2687. }
  2688. }
  2689. }
  2690. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2691. {
  2692. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  2693. {
  2694. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2695. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2696. {
  2697. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  2698. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  2699. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  2700. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  2701. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  2702. {
  2703. system("sync");
  2704. sleep(5);
  2705. system("reboot -f");
  2706. sleep(5);
  2707. system("reboot -f");
  2708. }
  2709. else
  2710. {
  2711. sleep(5);
  2712. close(wtdFd);
  2713. system("/usr/bin/run_evse_restart.sh");
  2714. }
  2715. }
  2716. }
  2717. }
  2718. }
  2719. void checkReservation(uint8_t gun_index)
  2720. {
  2721. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2722. {
  2723. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  2724. {
  2725. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2726. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2727. {
  2728. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  2729. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2730. }
  2731. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2732. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2733. }
  2734. }
  2735. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2736. {
  2737. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  2738. {
  2739. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2740. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2741. {
  2742. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  2743. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2744. }
  2745. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2746. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2747. }
  2748. }
  2749. }
  2750. void checkUnlocker(uint8_t gun_index)
  2751. {
  2752. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2753. {
  2754. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2755. {
  2756. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2757. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  2758. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  2759. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  2760. }
  2761. }
  2762. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2763. {
  2764. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2765. {
  2766. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2767. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "NotSupported");
  2768. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  2769. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  2770. }
  2771. }
  2772. }
  2773. void checkAvailability(uint8_t gun_index)
  2774. {
  2775. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2776. {
  2777. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2778. {
  2779. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  2780. {
  2781. DB_Update_Operactive(localDb, gun_index, true);
  2782. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2783. }
  2784. else
  2785. {
  2786. DB_Update_Operactive(localDb, gun_index, false);
  2787. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2788. }
  2789. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2790. }
  2791. }
  2792. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2793. {
  2794. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2795. {
  2796. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  2797. {
  2798. DB_Update_Operactive(localDb, gun_index, true);
  2799. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2800. }
  2801. else
  2802. {
  2803. DB_Update_Operactive(localDb, gun_index, false);
  2804. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2805. }
  2806. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2807. }
  2808. }
  2809. }
  2810. void checkChargingProfileLimit(uint8_t gun_index)
  2811. {
  2812. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2813. {
  2814. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  2815. (ocpp_get_profile_req(gun_index) != ON) &&
  2816. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2817. {
  2818. // Debug information
  2819. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2820. {
  2821. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2822. DEBUG_INFO("Valid from: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2823. DEBUG_INFO("Valid to: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2824. DEBUG_INFO("Start schedule: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2825. DEBUG_INFO("Profile kind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2826. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2827. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2828. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2829. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2830. DEBUG_INFO("===============================================================\n");
  2831. }
  2832. // Checking profile kind
  2833. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2834. {
  2835. // Absolute profile
  2836. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2837. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2838. {
  2839. // Checking limitation
  2840. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2841. {
  2842. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2843. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  2844. )
  2845. {
  2846. 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].Limit);
  2847. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2848. }
  2849. else
  2850. break;
  2851. }
  2852. }
  2853. }
  2854. }
  2855. else if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == 0) && (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  2856. {
  2857. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2858. }
  2859. }
  2860. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2861. {
  2862. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  2863. (ocpp_get_profile_req(gun_index) != ON) &&
  2864. (((strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom)>0) && (strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo)>0)) ? isProfileValid(gun_index) : ON))
  2865. {
  2866. // Debug information
  2867. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2868. {
  2869. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].id);
  2870. DEBUG_INFO("Valid from: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom);
  2871. DEBUG_INFO("Valid to: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validTo);
  2872. DEBUG_INFO("Start schedule: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  2873. DEBUG_INFO("Profile kind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind);
  2874. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].recurrencyKind);
  2875. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose);
  2876. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId);
  2877. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingRateUnit);
  2878. DEBUG_INFO("===============================================================\n");
  2879. }
  2880. // Checking profile kind
  2881. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  2882. {
  2883. // Absolute profile
  2884. if(((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId == ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId)) ||
  2885. (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == FAIL))
  2886. {
  2887. // Checking limitation
  2888. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2889. {
  2890. if((getScheduleStart(gun_index) > ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  2891. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  2892. )
  2893. {
  2894. 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].limit);
  2895. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2896. }
  2897. else
  2898. break;
  2899. }
  2900. }
  2901. }
  2902. }
  2903. else if((ShmOCPP20Data->SmartChargingProfile[gun_index].id == 0) && (ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  2904. {
  2905. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2906. }
  2907. }
  2908. }
  2909. void checkStopReason(uint8_t gun_index)
  2910. {
  2911. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2912. {
  2913. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2914. {
  2915. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  2916. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  2917. else
  2918. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  2919. }
  2920. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  2921. {
  2922. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  2923. }
  2924. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  2925. {
  2926. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  2927. }
  2928. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  2929. {
  2930. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  2931. }
  2932. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  2933. {
  2934. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  2935. }
  2936. else
  2937. {
  2938. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  2939. }
  2940. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  2941. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  2942. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  2943. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  2944. }
  2945. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2946. {
  2947. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  2948. {
  2949. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Hard")==0)
  2950. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "HardReset");
  2951. else
  2952. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "SoftReset");
  2953. }
  2954. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  2955. {
  2956. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  2957. }
  2958. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  2959. {
  2960. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  2961. }
  2962. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  2963. {
  2964. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  2965. }
  2966. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  2967. {
  2968. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  2969. }
  2970. else
  2971. {
  2972. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  2973. }
  2974. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  2975. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  2976. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  2977. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  2978. }
  2979. }
  2980. void checkRemoteUpgradeStatus()
  2981. {
  2982. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2983. {
  2984. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  2985. {
  2986. DEBUG_INFO("Firmware remote upgraded fail...\n");
  2987. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  2988. ShmCharger->isUpdateSuccess = NO;
  2989. }
  2990. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  2991. {
  2992. DEBUG_INFO("Firmware remote upgrading...\n");
  2993. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  2994. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  2995. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  2996. int result = upgrade_check();
  2997. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  2998. if(result == PASS)
  2999. {
  3000. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3001. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3002. ShmCharger->isUpdateSuccess = YES;
  3003. DEBUG_INFO("Remote update success...\n");
  3004. }
  3005. else
  3006. {
  3007. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3008. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3009. ShmCharger->isUpdateSuccess = NO;
  3010. DEBUG_INFO("Remote update unsuccess...\n");
  3011. }
  3012. }
  3013. else
  3014. {}
  3015. }
  3016. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3017. {
  3018. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  3019. {
  3020. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3021. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3022. ShmCharger->isUpdateSuccess = NO;
  3023. }
  3024. else if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0)
  3025. {
  3026. DEBUG_INFO("Firmware remote upgrading...\n");
  3027. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3028. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3029. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3030. int result = upgrade_check();
  3031. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3032. if(result == PASS)
  3033. {
  3034. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3035. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3036. ShmCharger->isUpdateSuccess = YES;
  3037. DEBUG_INFO("Remote update success...\n");
  3038. }
  3039. else
  3040. {
  3041. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3042. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3043. ShmCharger->isUpdateSuccess = NO;
  3044. DEBUG_INFO("Remote update unsuccess...\n");
  3045. }
  3046. }
  3047. else
  3048. {}
  3049. }
  3050. }
  3051. //======================================================
  3052. // Main process
  3053. //======================================================
  3054. int main(void)
  3055. {
  3056. //==================================================
  3057. // Create all share memory
  3058. //==================================================
  3059. if(CreatShareMemory()==0)
  3060. {
  3061. DEBUG_ERROR("CreatShareMemory NG\n");
  3062. if(ShmStatusCodeData!=NULL)
  3063. {
  3064. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  3065. }
  3066. sleep(5);
  3067. system("reboot -f");
  3068. sleep(5);
  3069. system("reboot -f");
  3070. }
  3071. else
  3072. {
  3073. DEBUG_INFO("CreatShareMemory OK\n");
  3074. }
  3075. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3076. {
  3077. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  3078. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  3079. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  3080. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  3081. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  3082. }
  3083. //==================================================
  3084. // Main loop
  3085. //==================================================
  3086. for(;;)
  3087. {
  3088. //==============================================
  3089. // Synchronize share memory from OCPP struct
  3090. //==============================================
  3091. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  3092. //==============================================
  3093. // Check task processing
  3094. //==============================================
  3095. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3096. checkTask();
  3097. //==============================================
  3098. // Check connection timeout specification
  3099. //==============================================
  3100. checkConnectionTimeout();
  3101. //==============================================
  3102. // Something need run in Idle mode
  3103. //==============================================
  3104. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  3105. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  3106. {
  3107. // Check restore factory setting request
  3108. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3109. {
  3110. // Set led to initial
  3111. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3112. {
  3113. setLedMotion(gun_index,LED_ACTION_INIT);
  3114. }
  3115. system("cd /root;./Module_FactoryConfig -m");
  3116. system("rm -f /Storage/OCPP/OCPPConfiguration");
  3117. system("sync");
  3118. sleep(5);
  3119. system("reboot -f");
  3120. sleep(5);
  3121. system("reboot -f");
  3122. }
  3123. // Check upgrade firmware request
  3124. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  3125. ocpp_get_update_firmware_req())
  3126. {
  3127. // If available memory too small, free memory cache first
  3128. if(getAvailableMemory() < (200*1024*1024))
  3129. {
  3130. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  3131. system("echo 3 > /proc/sys/vm/drop_caches");
  3132. }
  3133. ShmCharger->isUpdateSuccess = NO;
  3134. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3135. {
  3136. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3137. setChargerMode(gun_index, SYS_MODE_UPDATE);
  3138. }
  3139. }
  3140. }
  3141. //==============================================
  3142. // Check remote reset request
  3143. //==============================================
  3144. checkReset();
  3145. //==============================================
  3146. // Check RFID
  3147. //==============================================
  3148. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  3149. {
  3150. if(GetCardSerialNumber()!= FAIL)
  3151. {
  3152. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  3153. }
  3154. }
  3155. //==============================================
  3156. // Connector loop
  3157. //==============================================
  3158. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3159. {
  3160. //==========================================
  3161. // Power saving logic
  3162. //==========================================
  3163. /*
  3164. * TODO:
  3165. * 1. Power saving logic
  3166. */
  3167. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) > TIMEOUT_SPEC_POWERSAVING_LCD)
  3168. {
  3169. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) < (TIMEOUT_SPEC_POWERSAVING_LCD+600))
  3170. DEBUG_INFO("LCD into power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  3171. }
  3172. else
  3173. {
  3174. if((0 < DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD])) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) < 600))
  3175. DEBUG_INFO("LCD exit power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  3176. }
  3177. if(DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]) > TIMEOUT_SPEC_POWERSAVING_RFID)
  3178. {
  3179. if(gpio_get_value(GPIO_OUT_RST_RFID) == ON)
  3180. {
  3181. DEBUG_INFO("RFID into power saving...%d\n", DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  3182. gpio_set_value(GPIO_OUT_RST_RFID, OFF);
  3183. }
  3184. }
  3185. else
  3186. {
  3187. if(gpio_get_value(GPIO_OUT_RST_RFID) == OFF)
  3188. {
  3189. DEBUG_INFO("RFID exit power saving...%d\n", DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  3190. gpio_set_value(GPIO_OUT_RST_RFID, ON);
  3191. }
  3192. }
  3193. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]) > TIMEOUT_SPEC_POWERSAVING_METER)
  3194. {
  3195. if(ShmCharger->gun_info[gun_index].isMeterOn )
  3196. {
  3197. DEBUG_INFO("Meter into power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  3198. ShmCharger->gun_info[gun_index].isMeterOn = OFF;
  3199. }
  3200. }
  3201. else
  3202. {
  3203. if(!ShmCharger->gun_info[gun_index].isMeterOn )
  3204. {
  3205. DEBUG_INFO("Meter exit power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  3206. ShmCharger->gun_info[gun_index].isMeterOn = ON;
  3207. }
  3208. }
  3209. //==========================================
  3210. // Synchronize current rating value from MCU
  3211. //==========================================
  3212. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3213. //==========================================
  3214. // Assign connector location index for OCPP
  3215. //==========================================
  3216. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  3217. //==========================================
  3218. // Synchronize present charging power
  3219. //==========================================
  3220. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  3221. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  3222. else
  3223. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  3224. //==========================================
  3225. // Check initialization "PASS" or "FAIL"
  3226. //==========================================
  3227. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3228. {
  3229. // Alarm event check
  3230. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  3231. {
  3232. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  3233. {
  3234. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  3235. {
  3236. setChargerMode(gun_index, SYS_MODE_ALARM);
  3237. }
  3238. }
  3239. }
  3240. }
  3241. //==========================================
  3242. // Reservation request check
  3243. //==========================================
  3244. checkReservation(gun_index);
  3245. //==========================================
  3246. // Change availability check
  3247. //==========================================
  3248. checkAvailability(gun_index);
  3249. if(ShmCharger->gun_info[gun_index].isOperactive)
  3250. {
  3251. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  3252. {
  3253. setChargerMode(gun_index, SYS_MODE_IDLE);
  3254. }
  3255. }
  3256. else
  3257. {
  3258. if(isMode(gun_index, SYS_MODE_IDLE))
  3259. {
  3260. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  3261. }
  3262. }
  3263. //==========================================
  3264. // Unlock Connector signal check
  3265. //==========================================
  3266. checkUnlocker(gun_index);
  3267. //==========================================
  3268. // Connector process
  3269. //==========================================
  3270. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  3271. {
  3272. case SYS_MODE_BOOTING:
  3273. if(isModeChange(gun_index))
  3274. {
  3275. setLedMotion(gun_index,LED_ACTION_INIT);
  3276. //CSU Initialization & task spawn
  3277. if((Initialization() != PASS) ||
  3278. (SpawnTask() != PASS))
  3279. {
  3280. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  3281. }
  3282. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  3283. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  3284. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  3285. }
  3286. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  3287. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  3288. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  3289. {
  3290. ShmCharger->gun_selectd = 0;
  3291. // Firmware version
  3292. get_firmware_version(gun_index);
  3293. // OCPP BootNotification info set
  3294. DEBUG_INFO("==========================================\n");
  3295. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  3296. DEBUG_INFO("==========================================\n");
  3297. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  3298. ocpp_boot_info_sync();
  3299. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  3300. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  3301. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  3302. DEBUG_INFO("==========================================\n");
  3303. DEBUG_INFO("========== Set Wifi information ==========\n");
  3304. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  3305. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  3306. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  3307. DEBUG_INFO("==========================================\n");
  3308. // Set max current to rating current
  3309. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3310. // Default Ethernet / Wifi / 4G to 1:disconnected
  3311. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3312. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  3313. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  3314. // If Web Server OPCC URL is empty kill Module_OcppBackend
  3315. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  3316. {
  3317. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  3318. system ("pkill OcppBackend");
  3319. }
  3320. // If rotate switch equal zero, the system needs to change Debug mode
  3321. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  3322. setChargerMode(gun_index, SYS_MODE_DEBUG);
  3323. else
  3324. setChargerMode(gun_index, SYS_MODE_IDLE);
  3325. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = ((ShmSysConfigAndInfo->SysConfig.ModelName[2]=='Y') ||
  3326. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='D') ||
  3327. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='W')
  3328. ?3:1);
  3329. }
  3330. break;
  3331. case SYS_MODE_IDLE:
  3332. if(isModeChange(gun_index))
  3333. {
  3334. setLedMotion(gun_index,LED_ACTION_IDLE);
  3335. setRelay(gun_index, OFF);
  3336. setRequest(gun_index,OFF);
  3337. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3338. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3339. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3340. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3341. ocpp_set_remotestart(gun_index, OFF);
  3342. ocpp_set_remotestop(gun_index, OFF);
  3343. ocpp_set_auth_conf(OFF);
  3344. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  3345. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  3346. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  3347. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  3348. ocpp_set_unlocker_req(gun_index, OFF);
  3349. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  3350. // Response StopTransactionConf
  3351. ocpp_set_stoptransaction_conf(gun_index, OFF);
  3352. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_IDLE;
  3353. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3354. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3355. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3356. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
  3357. if(ShmCharger->isCcsEnable)system("pkill Module_CCS");
  3358. }
  3359. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  3360. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  3361. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3362. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  3363. (ocpp_get_remotestart(gun_index) == ON) ||
  3364. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  3365. {
  3366. // Clean User id & Card Number
  3367. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3368. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  3369. {
  3370. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  3371. DEBUG_INFO("Start Method : RFID...\n");
  3372. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3373. {
  3374. // Big endian
  3375. switch(rfid.snType)
  3376. {
  3377. case RFID_SN_TYPE_6BYTE:
  3378. 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]);
  3379. break;
  3380. case RFID_SN_TYPE_7BYTE:
  3381. 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]);
  3382. break;
  3383. case RFID_SN_TYPE_10BYTE:
  3384. 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]);
  3385. break;
  3386. case RFID_SN_TYPE_4BYTE:
  3387. default:
  3388. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3389. break;
  3390. }
  3391. }
  3392. else
  3393. {
  3394. // Little endian
  3395. switch(rfid.snType)
  3396. {
  3397. case RFID_SN_TYPE_6BYTE:
  3398. 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]);
  3399. break;
  3400. case RFID_SN_TYPE_7BYTE:
  3401. 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]);
  3402. break;
  3403. case RFID_SN_TYPE_10BYTE:
  3404. 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]);
  3405. break;
  3406. case RFID_SN_TYPE_4BYTE:
  3407. default:
  3408. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3409. break;
  3410. }
  3411. }
  3412. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3413. }
  3414. else if(ocpp_get_remotestart(gun_index))
  3415. {
  3416. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  3417. ocpp_copy_userid_from_remotestart(gun_index);
  3418. DEBUG_INFO("Start Method : BACKEND...\n");
  3419. }
  3420. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  3421. {
  3422. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  3423. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  3424. DEBUG_INFO("Start Method : BLE...\n");
  3425. }
  3426. else
  3427. {
  3428. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  3429. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  3430. DEBUG_INFO("Start Method : FREE...\n");
  3431. }
  3432. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3433. ocpp_set_remotestart(gun_index, OFF);
  3434. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3435. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3436. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  3437. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3438. }
  3439. else
  3440. {}
  3441. break;
  3442. case SYS_MODE_AUTHORIZING:
  3443. if(isModeChange(gun_index))
  3444. {
  3445. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3446. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  3447. {
  3448. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3449. {
  3450. case AUTH_MODE_ENABLE:
  3451. if(ocpp_get_connection_status())
  3452. {
  3453. // On line
  3454. ocpp_set_auth_req(ON, "ISO14443");
  3455. setLedMotion(gun_index,LED_ACTION_AUTHED);
  3456. }
  3457. else
  3458. {
  3459. // Off line
  3460. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3461. {
  3462. case OFF_POLICY_LOCALLIST:
  3463. ocpp_set_auth_req(ON, "ISO14443");
  3464. break;
  3465. case OFF_POLICY_PH_RFID:
  3466. break;
  3467. case OFF_POLICY_FREE:
  3468. break;
  3469. case OFF_POLICY_NOCHARGE:
  3470. default:
  3471. break;
  3472. }
  3473. }
  3474. break;
  3475. case AUTH_MODE_DISABLE:
  3476. default:
  3477. break;
  3478. }
  3479. }
  3480. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3481. if(ShmCharger->isCcsEnable)system("/root/Module_CCS &");
  3482. }
  3483. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  3484. {
  3485. // Authorization timeout process.
  3486. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3487. setChargerMode(gun_index, SYS_MODE_IDLE);
  3488. DEBUG_WARN("Authorize timeout !!!\n");
  3489. }
  3490. else
  3491. {
  3492. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  3493. {
  3494. case START_METHOD_RFID:
  3495. if((ocpp_get_auth_conf()) ||
  3496. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  3497. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0))
  3498. )
  3499. {
  3500. if(ocpp_get_auth_result() ||
  3501. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  3502. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  3503. {
  3504. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3505. DEBUG_INFO("Authorize pass.\n");
  3506. setSpeaker(ON,SPEAKER_SHORT);
  3507. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3508. sleep(3);
  3509. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3510. }
  3511. else
  3512. {
  3513. DEBUG_INFO("Authorize fail.\n");
  3514. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3515. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3516. sleep(3);
  3517. setChargerMode(gun_index, SYS_MODE_IDLE);
  3518. }
  3519. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3520. ocpp_set_auth_conf(OFF);
  3521. }
  3522. break;
  3523. case START_METHOD_BACKEND:
  3524. case START_METHOD_BLE:
  3525. case START_METHOD_FREE:
  3526. default:
  3527. setSpeaker(ON,SPEAKER_SHORT);
  3528. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3529. break;
  3530. }
  3531. }
  3532. break;
  3533. case SYS_MODE_PREPARING:
  3534. if(isModeChange(gun_index))
  3535. {
  3536. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3537. setLedMotion(gun_index,LED_ACTION_AUTHED);
  3538. if(ShmCharger->isCcsEnable)
  3539. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  3540. else
  3541. {
  3542. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  3543. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3544. setRequest(gun_index, ON);
  3545. }
  3546. }
  3547. // If control pilot detect Bx, skip watch dog time out.
  3548. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) ||
  3549. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  3550. {
  3551. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  3552. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  3553. {
  3554. case HANDSHAKE_DUTY_5:
  3555. if(!getRequest(gun_index))
  3556. {
  3557. setRequest(gun_index, ON);
  3558. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  3559. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3560. DEBUG_INFO("Set Request On.\n");
  3561. //Let CCS task start to negotiate
  3562. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = ON;
  3563. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_NONE;
  3564. }
  3565. // Set CCS 5% PWM duty
  3566. if(ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_5)
  3567. {
  3568. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  3569. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  3570. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3571. }
  3572. break;
  3573. case HANDSHAKE_DUTY_5_CHECK:
  3574. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF))
  3575. {
  3576. //2 secs timeout
  3577. ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  3578. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CCS;
  3579. }
  3580. break;
  3581. case HANDSHAKE_CCS:
  3582. //CCS handshake timeout
  3583. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  3584. {
  3585. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  3586. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  3587. DEBUG_INFO("BS/HLC %d secs handshake timeout.\n", (TIMEOUT_SPEC_BS_HLC_HANDSHAKE/1000));
  3588. }
  3589. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  3590. {
  3591. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  3592. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  3593. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3594. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  3595. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3596. }
  3597. //CCS status check
  3598. if((16 < ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 254))
  3599. {
  3600. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  3601. DEBUG_INFO("Enter HLC Mode charging.\n");
  3602. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  3603. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3604. }
  3605. break;
  3606. case HANDSHAKE_CP_STATE_E:
  3607. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3608. {
  3609. if(ShmCharger->isCcsEnable)
  3610. {
  3611. DEBUG_INFO("Change to CP STATE E for 4 secs.\n");
  3612. //CP STATE E for 4 secs
  3613. sleep(4);
  3614. }
  3615. //restore normal CP PWM duty
  3616. // Determine max charging current to MCU
  3617. DEBUG_INFO("Determine max charging current to MCU.\n");
  3618. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3619. {
  3620. 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);
  3621. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3622. {
  3623. 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);
  3624. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3625. }
  3626. }
  3627. else
  3628. {
  3629. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3630. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3631. {
  3632. 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);
  3633. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3634. }
  3635. }
  3636. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3637. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  3638. ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  3639. }
  3640. break;
  3641. case HANDSHAKE_SET_MAX_CURRENT:
  3642. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3643. {
  3644. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
  3645. DEBUG_INFO("Enter BS Mode charging.\n");
  3646. //for EV READY 30 secs didn't start charging to STATE E
  3647. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3648. }
  3649. break;
  3650. case HANDSHAKE_BS_MODE:
  3651. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  3652. setRelay(gun_index,ON);
  3653. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  3654. {
  3655. ocpp_set_unlocker_req(gun_index, OFF);
  3656. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  3657. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  3658. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  3659. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  3660. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  3661. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  3662. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  3663. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  3664. else
  3665. {
  3666. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100.0);
  3667. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100.0);
  3668. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100.0);
  3669. }
  3670. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3671. ocpp_copy_userid_to_starttransaction(gun_index);
  3672. ocpp_set_starttransaction_req(gun_index, ON);
  3673. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3674. }
  3675. //EV READY CHECK
  3676. /*
  3677. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_EV_READY) && (ShmCharger->gun_info[gun_index].isEvReady2StateE == OFF))
  3678. {
  3679. if(ShmCharger->gun_info[gun_index].isDoEvReadyOnce == OFF)
  3680. {
  3681. DEBUG_INFO("EV READY STATE E 4sec.\n");
  3682. ShmCharger->gun_info[gun_index].isEvReady2StateE = ON;
  3683. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
  3684. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3685. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_E;
  3686. }
  3687. }
  3688. if(ShmCharger->gun_info[gun_index].isEvReady2StateE == ON)
  3689. {
  3690. switch(ShmCharger->gun_info[gun_index].evReadyState)
  3691. {
  3692. case EV_READY_STAT_E:
  3693. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3694. {
  3695. sleep(4);
  3696. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3697. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3698. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_C;
  3699. }
  3700. break;
  3701. case EV_READY_STAT_C:
  3702. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3703. {
  3704. usleep(500000);
  3705. DEBUG_INFO("EV READY Determine max charging current to MCU.\n");
  3706. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3707. {
  3708. 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);
  3709. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3710. {
  3711. 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);
  3712. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3713. }
  3714. }
  3715. else
  3716. {
  3717. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3718. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3719. {
  3720. 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);
  3721. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3722. }
  3723. }
  3724. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3725. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_SET_MAX_CURRENT;
  3726. }
  3727. break;
  3728. case EV_READY_SET_MAX_CURRENT:
  3729. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3730. {
  3731. ShmCharger->gun_info[gun_index].isEvReady2StateE = OFF;
  3732. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = ON;
  3733. }
  3734. break;
  3735. default:
  3736. break;
  3737. }
  3738. }*/
  3739. break;
  3740. case HANDSHAKE_HLC_MODE:
  3741. if(ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)
  3742. {
  3743. setRelay(gun_index,ON);
  3744. ShmCharger->gun_info[gun_index].isCCSStartTransation = ON;
  3745. }
  3746. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) &&
  3747. (ShmCharger->gun_info[gun_index].isCCSStartTransation == ON))
  3748. {
  3749. ocpp_set_unlocker_req(gun_index, OFF);
  3750. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  3751. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  3752. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  3753. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  3754. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  3755. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  3756. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  3757. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  3758. else
  3759. {
  3760. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  3761. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  3762. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  3763. }
  3764. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3765. ocpp_copy_userid_to_starttransaction(gun_index);
  3766. ocpp_set_starttransaction_req(gun_index, ON);
  3767. ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
  3768. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3769. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  3770. }
  3771. //120 sec timeout
  3772. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  3773. {
  3774. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  3775. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5000)
  3776. {
  3777. DEBUG_INFO("CCS 120 secs handshake timeout.\n");
  3778. //Cancel CCS task negotiating
  3779. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  3780. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  3781. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  3782. }
  3783. }
  3784. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100)
  3785. {
  3786. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3787. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3788. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  3789. setChargerMode(gun_index, SYS_MODE_IDLE);
  3790. }
  3791. break;
  3792. default:
  3793. break;
  3794. }
  3795. }
  3796. else if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode)
  3797. {
  3798. DEBUG_INFO("Enter Socket-E Mode charging.\n");
  3799. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_SOCKETE;
  3800. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == CP_STATE_C)
  3801. setRelay(gun_index,ON);
  3802. ocpp_copy_userid_to_starttransaction(gun_index);
  3803. ocpp_set_starttransaction_req(gun_index, ON);
  3804. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3805. }
  3806. // Unplug charging gun to Idle mode
  3807. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  3808. {
  3809. DEBUG_INFO("Charging gun is plugged before.\n");
  3810. //setChargerMode(gun_index, SYS_MODE_IDLE);
  3811. //Cancel CCS task negotiating
  3812. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  3813. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  3814. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  3815. }
  3816. // Use RFID card to stop handshaking
  3817. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  3818. {
  3819. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  3820. setSpeaker(ON,SPEAKER_SHORT);
  3821. //setChargerMode(gun_index, SYS_MODE_IDLE);
  3822. //Cancel CCS task negotiating
  3823. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  3824. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  3825. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  3826. }
  3827. else
  3828. {
  3829. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3830. }
  3831. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  3832. {
  3833. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  3834. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  3835. {
  3836. DEBUG_INFO("Handshaking timeout...\n");
  3837. //Cancel CCS task negotiating
  3838. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  3839. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  3840. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  3841. }
  3842. }
  3843. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5)
  3844. {
  3845. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3846. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3847. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  3848. setChargerMode(gun_index, SYS_MODE_IDLE);
  3849. }
  3850. break;
  3851. case SYS_MODE_CHARGING:
  3852. if(isModeChange(gun_index))
  3853. {
  3854. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3855. ftime(&startChargingTime[gun_index]);
  3856. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  3857. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  3858. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3859. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3860. ocpp_set_auth_req(OFF);
  3861. ocpp_reset_smartcharging_profileId(gun_index);
  3862. ocpp_set_profile_req(gun_index, ON);
  3863. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  3864. }
  3865. //if time up, clear CCS MSG count
  3866. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  3867. (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
  3868. {
  3869. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  3870. if(ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat > 0)
  3871. {
  3872. ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat = 0;
  3873. }
  3874. else
  3875. {
  3876. DEBUG_INFO("CCS could not get MSG from car.\n");
  3877. //setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3878. //setRelay(gun_index, OFF);
  3879. }
  3880. }
  3881. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3882. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  3883. ocpp_get_remotestop(gun_index) ||
  3884. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  3885. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  3886. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  3887. ocpp_get_reset_req() ||
  3888. ocpp_get_unlocker_req(gun_index) ||
  3889. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  3890. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
  3891. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
  3892. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
  3893. {
  3894. if(ShmCharger->gun_info[gun_index].rfidReq)
  3895. {
  3896. // If RFID SN different with start user, it need to authorize ID
  3897. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  3898. {
  3899. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  3900. {
  3901. // Request authorization
  3902. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3903. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3904. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3905. {
  3906. // Big endian
  3907. switch(rfid.snType)
  3908. {
  3909. case RFID_SN_TYPE_6BYTE:
  3910. 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]);
  3911. break;
  3912. case RFID_SN_TYPE_7BYTE:
  3913. 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]);
  3914. break;
  3915. case RFID_SN_TYPE_10BYTE:
  3916. 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]);
  3917. break;
  3918. case RFID_SN_TYPE_4BYTE:
  3919. default:
  3920. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3921. break;
  3922. }
  3923. }
  3924. else
  3925. {
  3926. // Little endian
  3927. switch(rfid.snType)
  3928. {
  3929. case RFID_SN_TYPE_6BYTE:
  3930. 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]);
  3931. break;
  3932. case RFID_SN_TYPE_7BYTE:
  3933. 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]);
  3934. break;
  3935. case RFID_SN_TYPE_10BYTE:
  3936. 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]);
  3937. break;
  3938. case RFID_SN_TYPE_4BYTE:
  3939. default:
  3940. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3941. break;
  3942. }
  3943. }
  3944. DEBUG_INFO("End request User Id: %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3945. DEBUG_INFO("Start method: %d...\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  3946. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  3947. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  3948. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  3949. {
  3950. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3951. {
  3952. case AUTH_MODE_ENABLE:
  3953. if(ocpp_get_connection_status())
  3954. {
  3955. // On line
  3956. ocpp_set_auth_req(ON, "ISO14443");
  3957. }
  3958. else
  3959. {
  3960. // Off line
  3961. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3962. {
  3963. case OFF_POLICY_LOCALLIST:
  3964. ocpp_set_auth_req(ON, "ISO14443");
  3965. break;
  3966. case OFF_POLICY_PH_RFID:
  3967. break;
  3968. case OFF_POLICY_FREE:
  3969. break;
  3970. case OFF_POLICY_NOCHARGE:
  3971. default:
  3972. break;
  3973. }
  3974. }
  3975. break;
  3976. case AUTH_MODE_DISABLE:
  3977. default:
  3978. break;
  3979. }
  3980. }
  3981. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  3982. {
  3983. DEBUG_INFO("Should be remote stop charger... \n");
  3984. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3985. }
  3986. }
  3987. else
  3988. {
  3989. if(ocpp_get_auth_conf() ||
  3990. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3991. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  3992. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3993. {
  3994. if(ocpp_get_auth_result() ||
  3995. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3996. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  3997. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3998. {
  3999. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4000. }
  4001. else
  4002. {
  4003. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4004. ocpp_set_auth_req(OFF);
  4005. DEBUG_INFO("Authorize fail.\n");
  4006. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4007. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4008. sleep(3);
  4009. }
  4010. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4011. ocpp_set_auth_conf(OFF);
  4012. }
  4013. }
  4014. }
  4015. // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
  4016. if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
  4017. {
  4018. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4019. setRelay(gun_index, OFF);
  4020. }
  4021. }
  4022. else
  4023. {
  4024. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4025. setRelay(gun_index, OFF);
  4026. }
  4027. }
  4028. else
  4029. {
  4030. setLedMotion(gun_index,LED_ACTION_CHARGING);
  4031. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  4032. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  4033. else
  4034. {
  4035. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  4036. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  4037. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  4038. }
  4039. ftime(&endChargingTime[gun_index]);
  4040. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  4041. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  4042. 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;
  4043. else
  4044. {
  4045. //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;
  4046. //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;
  4047. //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;
  4048. }
  4049. // Response StartTransactionConf
  4050. ocpp_set_starttransaction_conf(gun_index, OFF);
  4051. // Charging profile preparation
  4052. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  4053. {
  4054. if(!ocpp_get_profile_conf(gun_index))
  4055. {
  4056. ocpp_set_profile_req(gun_index, ON);
  4057. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4058. }
  4059. else
  4060. {
  4061. ocpp_set_profile_conf(gun_index, OFF);
  4062. }
  4063. }
  4064. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  4065. checkChargingProfileLimit(gun_index);
  4066. // Determine max charging current to MCU
  4067. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4068. {
  4069. 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);
  4070. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  4071. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4072. {
  4073. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4074. {
  4075. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4076. {
  4077. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4078. {
  4079. 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);
  4080. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4081. }
  4082. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4083. }
  4084. }
  4085. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4086. {
  4087. 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);
  4088. }
  4089. }
  4090. }
  4091. else
  4092. {
  4093. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4094. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  4095. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4096. {
  4097. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4098. {
  4099. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4100. {
  4101. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4102. {
  4103. 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);
  4104. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4105. }
  4106. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4107. }
  4108. }
  4109. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4110. {
  4111. 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);
  4112. }
  4113. }
  4114. }
  4115. // Debug information
  4116. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  4117. {
  4118. DEBUG_INFO("===============================================================\n");
  4119. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4120. 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);
  4121. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4122. DEBUG_INFO("===============================================================\n");
  4123. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4124. }
  4125. if(ocpp_get_connection_status())
  4126. {
  4127. // On-line max condition check
  4128. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  4129. {
  4130. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4131. {
  4132. setRelay(gun_index, OFF);
  4133. setRequest(gun_index, OFF);
  4134. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4135. }
  4136. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4137. {
  4138. setRelay(gun_index, OFF);
  4139. }
  4140. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4141. 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));
  4142. }
  4143. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  4144. {
  4145. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4146. {
  4147. setRelay(gun_index, OFF);
  4148. setRequest(gun_index, OFF);
  4149. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4150. }
  4151. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4152. {
  4153. setRelay(gun_index, OFF);
  4154. }
  4155. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4156. 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));
  4157. }
  4158. else
  4159. {
  4160. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4161. {
  4162. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4163. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4164. setRelay(gun_index, ON);
  4165. else
  4166. setRelay(gun_index, OFF);
  4167. }
  4168. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4169. {
  4170. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  4171. setRelay(gun_index, ON);
  4172. }
  4173. }
  4174. }
  4175. else
  4176. {
  4177. // Off-line max condition check
  4178. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4179. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  4180. {
  4181. 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))))
  4182. {
  4183. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4184. {
  4185. setRelay(gun_index, OFF);
  4186. setRequest(gun_index, OFF);
  4187. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4188. }
  4189. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4190. {
  4191. setRelay(gun_index, OFF);
  4192. }
  4193. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4194. 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));
  4195. }
  4196. 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))))
  4197. {
  4198. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4199. {
  4200. setRelay(gun_index, OFF);
  4201. setRequest(gun_index, OFF);
  4202. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4203. }
  4204. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4205. {
  4206. setRelay(gun_index, OFF);
  4207. }
  4208. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4209. 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));
  4210. }
  4211. else
  4212. {
  4213. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4214. {
  4215. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4216. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4217. setRelay(gun_index, ON);
  4218. else
  4219. setRelay(gun_index, OFF);
  4220. }
  4221. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4222. {
  4223. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  4224. setRelay(gun_index, ON);
  4225. }
  4226. }
  4227. }
  4228. else
  4229. {
  4230. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4231. {
  4232. setRequest(gun_index, OFF);
  4233. }
  4234. setRelay(gun_index, OFF);
  4235. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  4236. }
  4237. }
  4238. }
  4239. break;
  4240. case SYS_MODE_TERMINATING:
  4241. if(isModeChange(gun_index))
  4242. {
  4243. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  4244. {
  4245. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  4246. }
  4247. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4248. {
  4249. setLedMotion(gun_index, LED_ACTION_STOP);
  4250. }
  4251. else
  4252. {}
  4253. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  4254. }
  4255. // End authorize pass
  4256. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4257. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  4258. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4259. ocpp_get_remotestop(gun_index) ||
  4260. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4261. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  4262. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  4263. ocpp_get_reset_req() ||
  4264. ocpp_get_unlocker_req(gun_index) ||
  4265. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  4266. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  4267. {
  4268. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  4269. {
  4270. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4271. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  4272. {
  4273. DEBUG_INFO("Authorize pass.\n");
  4274. setSpeaker(ON,SPEAKER_SHORT);
  4275. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4276. sleep(3);
  4277. }
  4278. }
  4279. else
  4280. {}
  4281. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4282. setRequest(gun_index, OFF);
  4283. setRelay(gun_index, OFF);
  4284. setLedMotion(gun_index, LED_ACTION_STOP);
  4285. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4286. {
  4287. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4288. {
  4289. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4290. }
  4291. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4292. {
  4293. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4294. //Cancel CCS task negotiating
  4295. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4296. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4297. setRelay(gun_index,OFF);
  4298. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4299. }
  4300. }
  4301. }
  4302. else
  4303. {
  4304. // If the charger stops charging, the led should be stoped blink
  4305. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4306. {
  4307. setLedMotion(gun_index, LED_ACTION_STOP);
  4308. }
  4309. if(ShmCharger->gun_info[gun_index].rfidReq)
  4310. {
  4311. DEBUG_INFO("Certified in terminating mode... \n");
  4312. // If RFID SN different with start user, it need to authorize ID
  4313. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4314. {
  4315. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4316. {
  4317. // Request authorization
  4318. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  4319. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4320. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4321. {
  4322. // Big endian
  4323. switch(rfid.snType)
  4324. {
  4325. case RFID_SN_TYPE_6BYTE:
  4326. 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]);
  4327. break;
  4328. case RFID_SN_TYPE_7BYTE:
  4329. 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]);
  4330. break;
  4331. case RFID_SN_TYPE_10BYTE:
  4332. 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]);
  4333. break;
  4334. case RFID_SN_TYPE_4BYTE:
  4335. default:
  4336. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4337. break;
  4338. }
  4339. }
  4340. else
  4341. {
  4342. // Little endian
  4343. switch(rfid.snType)
  4344. {
  4345. case RFID_SN_TYPE_6BYTE:
  4346. 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]);
  4347. break;
  4348. case RFID_SN_TYPE_7BYTE:
  4349. 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]);
  4350. break;
  4351. case RFID_SN_TYPE_10BYTE:
  4352. 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]);
  4353. break;
  4354. case RFID_SN_TYPE_4BYTE:
  4355. default:
  4356. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4357. break;
  4358. }
  4359. }
  4360. DEBUG_INFO("End request User Id : %s... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4361. DEBUG_INFO("Start method : %d... \n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  4362. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4363. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4364. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4365. {
  4366. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4367. {
  4368. case AUTH_MODE_ENABLE:
  4369. if(ocpp_get_connection_status())
  4370. {
  4371. // On line
  4372. ocpp_set_auth_req(ON, "ISO14443");
  4373. }
  4374. else
  4375. {
  4376. // Off line
  4377. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4378. {
  4379. case OFF_POLICY_LOCALLIST:
  4380. ocpp_set_auth_req(ON, "ISO14443");
  4381. break;
  4382. case OFF_POLICY_PH_RFID:
  4383. break;
  4384. case OFF_POLICY_FREE:
  4385. break;
  4386. case OFF_POLICY_NOCHARGE:
  4387. default:
  4388. break;
  4389. }
  4390. }
  4391. break;
  4392. case AUTH_MODE_DISABLE:
  4393. default:
  4394. break;
  4395. }
  4396. }
  4397. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4398. {
  4399. DEBUG_INFO("Should be remote stop charger... \n");
  4400. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4401. }
  4402. }
  4403. else
  4404. {
  4405. if(ocpp_get_auth_conf() ||
  4406. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4407. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4408. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4409. {
  4410. if(ocpp_get_auth_result() ||
  4411. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4412. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  4413. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4414. {
  4415. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4416. }
  4417. else
  4418. {
  4419. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4420. ocpp_set_auth_req(OFF);
  4421. DEBUG_INFO("Authorize fail... \n");
  4422. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4423. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4424. sleep(3);
  4425. }
  4426. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4427. ocpp_set_auth_conf(OFF);
  4428. }
  4429. }
  4430. }
  4431. }
  4432. 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)) &&
  4433. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  4434. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  4435. !ocpp_get_remotestop(gun_index) &&
  4436. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  4437. !ocpp_get_reset_req() &&
  4438. ocpp_get_starttransaction_result(gun_index) &&
  4439. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  4440. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  4441. !(!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)))) &&
  4442. !(!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)))) &&
  4443. (((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))
  4444. )
  4445. {
  4446. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4447. }
  4448. }
  4449. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  4450. (ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) &&
  4451. (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  4452. {
  4453. DEBUG_INFO("Set PWM duty 100% go to SYS_MODE_TERMINATING.\n");
  4454. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  4455. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  4456. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  4457. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4458. }
  4459. break;
  4460. case SYS_MODE_COMPLETE:
  4461. if(isModeChange(gun_index))
  4462. {
  4463. setLedMotion(gun_index, LED_ACTION_STOP);
  4464. setRelay(gun_index, OFF);
  4465. setRequest(gun_index, OFF);
  4466. }
  4467. checkStopReason(gun_index);
  4468. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4469. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  4470. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4471. ocpp_set_remotestop(gun_index, OFF);
  4472. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  4473. DB_Insert_Record(localDb, gun_index);
  4474. setChargerMode(gun_index, SYS_MODE_IDLE);
  4475. break;
  4476. case SYS_MODE_ALARM:
  4477. setLedMotion(gun_index,LED_ACTION_ALARM);
  4478. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4479. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4480. {}
  4481. else
  4482. {
  4483. setRelay(gun_index, OFF);
  4484. setRequest(gun_index, OFF);
  4485. }
  4486. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  4487. {
  4488. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4489. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4490. {
  4491. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  4492. }
  4493. else
  4494. {
  4495. setChargerMode(gun_index, SYS_MODE_IDLE);
  4496. }
  4497. }
  4498. break;
  4499. case SYS_MODE_FAULT:
  4500. if(isModeChange(gun_index))
  4501. {}
  4502. break;
  4503. case SYS_MODE_MAINTAIN:
  4504. if(isModeChange(gun_index))
  4505. {
  4506. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4507. }
  4508. break;
  4509. case SYS_MODE_UPDATE:
  4510. if(isModeChange(gun_index))
  4511. {
  4512. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4513. }
  4514. //======================================
  4515. // Check local upgrade firmware request
  4516. //======================================
  4517. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  4518. {
  4519. DEBUG_INFO("Firmware local upgrading...\n");
  4520. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  4521. int value = upgrade_check();
  4522. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  4523. if(value == PASS)
  4524. {
  4525. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4526. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4527. ShmCharger->isUpdateSuccess = YES;
  4528. DEBUG_INFO("Local update success...\n");
  4529. }
  4530. else
  4531. {
  4532. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4533. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4534. ShmCharger->isUpdateSuccess = NO;
  4535. DEBUG_INFO("Local update unsuccess...\n");
  4536. }
  4537. }
  4538. else if(ocpp_get_update_firmware_req())
  4539. {
  4540. //======================================
  4541. // Check remote upgrade firmware request
  4542. //======================================
  4543. checkRemoteUpgradeStatus();
  4544. }
  4545. else
  4546. {
  4547. //======================================
  4548. // Upgrade complete reboot system
  4549. //======================================
  4550. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  4551. {
  4552. if(ShmCharger->isUpdateSuccess == YES)
  4553. {
  4554. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4555. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4556. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  4557. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4558. DEBUG_WARN("Firmware upgrade success.\n");
  4559. sleep(5);
  4560. DEBUG_INFO("Firmware upgraded, reboot...\n");
  4561. system("reboot -f");
  4562. sleep(5);
  4563. system("reboot -f");
  4564. }
  4565. else
  4566. {
  4567. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  4568. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4569. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  4570. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4571. DEBUG_WARN("Firmware upgrade fail.\n");
  4572. close(wtdFd);
  4573. system("/usr/bin/run_evse_restart.sh");
  4574. }
  4575. }
  4576. }
  4577. break;
  4578. case SYS_MODE_RESERVATION:
  4579. if(isModeChange(gun_index))
  4580. {
  4581. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4582. }
  4583. if(isReservationExpired(gun_index))
  4584. {
  4585. DEBUG_INFO("Reservation: Time's up...\n");
  4586. setChargerMode(gun_index, SYS_MODE_IDLE);
  4587. }
  4588. else if(ocpp_get_cancelreservation_req(gun_index))
  4589. {
  4590. DEBUG_INFO("Reservation: Cancel reservation...\n");
  4591. setChargerMode(gun_index, SYS_MODE_IDLE);
  4592. ocpp_set_cancelreservation_req(gun_index, OFF);
  4593. }
  4594. else
  4595. {
  4596. // Check is there RFID or back end request start
  4597. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4598. ocpp_get_remotestart(gun_index))
  4599. {
  4600. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4601. {
  4602. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4603. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4604. {
  4605. // Big endian
  4606. switch(rfid.snType)
  4607. {
  4608. case RFID_SN_TYPE_6BYTE:
  4609. 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]);
  4610. break;
  4611. case RFID_SN_TYPE_7BYTE:
  4612. 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]);
  4613. break;
  4614. case RFID_SN_TYPE_10BYTE:
  4615. 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]);
  4616. break;
  4617. case RFID_SN_TYPE_4BYTE:
  4618. default:
  4619. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4620. break;
  4621. }
  4622. }
  4623. else
  4624. {
  4625. // Little endian
  4626. switch(rfid.snType)
  4627. {
  4628. case RFID_SN_TYPE_6BYTE:
  4629. 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]);
  4630. break;
  4631. case RFID_SN_TYPE_7BYTE:
  4632. 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]);
  4633. break;
  4634. case RFID_SN_TYPE_10BYTE:
  4635. 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]);
  4636. break;
  4637. case RFID_SN_TYPE_4BYTE:
  4638. default:
  4639. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4640. break;
  4641. }
  4642. }
  4643. if(ocpp_compare_reserve_id_with_user(gun_index))
  4644. {
  4645. DEBUG_INFO("Start Method in reservation : RFID...\n");
  4646. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4647. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4648. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4649. }
  4650. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4651. }
  4652. else if(ocpp_get_remotestart(gun_index))
  4653. {
  4654. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  4655. {
  4656. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  4657. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4658. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4659. }
  4660. ocpp_set_remotestop(gun_index, OFF);
  4661. }
  4662. }
  4663. }
  4664. break;
  4665. case SYS_MODE_BOOKING:
  4666. if(isModeChange(gun_index))
  4667. {}
  4668. break;
  4669. case SYS_MODE_DEBUG:
  4670. if(isModeChange(gun_index))
  4671. {
  4672. setLedMotion(gun_index,LED_ACTION_DEBUG);
  4673. }
  4674. break;
  4675. }
  4676. }
  4677. // System watch dog reset
  4678. write(wtdFd, "a", 1);
  4679. usleep(50000);
  4680. }
  4681. return FAIL;
  4682. }