main.c 191 KB

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