main.c 198 KB

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