main.c 209 KB

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