main.c 201 KB

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