main.c 189 KB

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