main.c 198 KB

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