main.c 197 KB

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