main.c 190 KB

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