main.c 197 KB

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