main.c 208 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566
  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. DEBUG_INFO("Load SysConfigData OK\n");
  1408. ShmCharger->isCcsEnable = OFF;
  1409. for(uint8_t idx=0;idx<3;idx++)
  1410. {
  1411. if(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == '7')
  1412. ShmCharger->isCcsEnable = ON;
  1413. }
  1414. RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo);
  1415. return PASS;
  1416. }
  1417. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1418. {
  1419. int result = PASS;
  1420. int fd,wrd;
  1421. unsigned int i,Chk;
  1422. unsigned char *ptr, *BufTmp;
  1423. Chk=0;
  1424. ptr=(unsigned char *)UsrData;
  1425. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  1426. {
  1427. memset(BufTmp,0,MtdBlockSize);
  1428. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  1429. for(i=0;i<MtdBlockSize-4;i++)
  1430. Chk+=*(BufTmp+i);
  1431. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  1432. // Output configuration to file.
  1433. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  1434. if (fd < 0)
  1435. {
  1436. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  1437. free(BufTmp);
  1438. return 0;
  1439. }
  1440. wrd=write(fd, BufTmp, MtdBlockSize);
  1441. close(fd);
  1442. if(wrd<MtdBlockSize)
  1443. {
  1444. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  1445. free(BufTmp);
  1446. return 0;
  1447. }
  1448. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  1449. DEBUG_INFO("Erase /dev/mtd10.\n");
  1450. runShellCmd("flash_erase /dev/mtd10 0 0");
  1451. DEBUG_INFO("Write /dev/mtd10.\n");
  1452. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  1453. DEBUG_INFO("Erase /dev/mtd11.\n");
  1454. runShellCmd("flash_erase /dev/mtd11 0 0");
  1455. DEBUG_INFO("Write /dev/mtd11.\n");
  1456. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  1457. system("rm -f /mnt/EvseConfig.bin");
  1458. DEBUG_INFO("EvseConfig write to flash OK\n");
  1459. }
  1460. else
  1461. {
  1462. DEBUG_ERROR("alloc BlockSize NG\r\n");
  1463. result = FAIL;
  1464. }
  1465. if(BufTmp!=NULL)
  1466. free(BufTmp);
  1467. return result;
  1468. }
  1469. void InitEthernet()
  1470. {
  1471. pid_t pid;
  1472. uint8_t cnt_pingDNS_Fail;
  1473. char tmpbuf[256];
  1474. unsigned int natInterface = 0;
  1475. //Init Eth0 for internet
  1476. memset(tmpbuf,0,256);
  1477. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1478. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1479. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1480. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1481. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1482. system(tmpbuf);
  1483. memset(tmpbuf,0,256);
  1484. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1485. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1486. system(tmpbuf);
  1487. system("ifconfig lo up &");
  1488. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  1489. //system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  1490. //Run DHCP client if enabled
  1491. system("killall udhcpc");
  1492. system("rm -rf /etc/resolv.conf");
  1493. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  1494. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1495. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  1496. {
  1497. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1498. system(tmpbuf);
  1499. }
  1500. // Upgrade system id to /etc/hostname
  1501. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1502. system(tmpbuf);
  1503. // Ethernet MAC address
  1504. getEth0MacAddress();
  1505. //check internet status
  1506. pid = fork();
  1507. if(pid == 0)
  1508. {
  1509. for(;;)
  1510. {
  1511. if(isRouteFail())
  1512. {
  1513. //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  1514. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1515. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1516. {
  1517. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1518. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1519. system(tmpbuf);
  1520. }
  1521. else
  1522. {
  1523. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1524. memset(tmpbuf,0,256);
  1525. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1526. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1527. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1528. system(tmpbuf);
  1529. memset(tmpbuf,0,256);
  1530. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1531. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1532. system(tmpbuf);
  1533. }
  1534. }
  1535. if(isReachableInternet() == PASS)
  1536. {
  1537. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  1538. cnt_pingDNS_Fail = 0;
  1539. }
  1540. else
  1541. {
  1542. if(cnt_pingDNS_Fail >= 3)
  1543. {
  1544. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  1545. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1546. {
  1547. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1548. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1549. system(tmpbuf);
  1550. }
  1551. else
  1552. {
  1553. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1554. memset(tmpbuf,0,256);
  1555. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1556. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1557. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1558. system(tmpbuf);
  1559. memset(tmpbuf,0,256);
  1560. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1561. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1562. system(tmpbuf);
  1563. }
  1564. cnt_pingDNS_Fail = 0;
  1565. }
  1566. else
  1567. {
  1568. cnt_pingDNS_Fail++;
  1569. }
  1570. }
  1571. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  1572. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  1573. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi))
  1574. {
  1575. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  1576. }
  1577. else
  1578. {
  1579. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  1580. }
  1581. //============================================================
  1582. // Priority for internet 0 : First / 1 : Second / 2: Third
  1583. //============================================================
  1584. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  1585. {
  1586. system("/sbin/ifmetric eth0 0");
  1587. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1588. {
  1589. system("/sbin/ifmetric mlan0 1");
  1590. }
  1591. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1592. {
  1593. system("/sbin/ifmetric ppp0 2");
  1594. }
  1595. if(isKernelSupportNAT() == YES)
  1596. {
  1597. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  1598. {
  1599. if(natInterface != 1)
  1600. {
  1601. system("/sbin/iptables -t nat -F");
  1602. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o eth0 -j MASQUERADE");
  1603. natInterface = 1;
  1604. }
  1605. }
  1606. else
  1607. {
  1608. system("/sbin/iptables -t nat -F");
  1609. }
  1610. }
  1611. }
  1612. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  1613. {
  1614. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1615. {
  1616. system("/sbin/ifmetric eth0 1");
  1617. system("/sbin/ifmetric mlan0 0");
  1618. }
  1619. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1620. {
  1621. system("/sbin/ifmetric ppp0 2");
  1622. }
  1623. }
  1624. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  1625. {
  1626. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1627. {
  1628. system("/sbin/ifmetric mlan0 2");
  1629. }
  1630. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1631. {
  1632. system("/sbin/ifmetric eth0 1");
  1633. system("/sbin/ifmetric ppp0 0");
  1634. }
  1635. if(isKernelSupportNAT() == YES)
  1636. {
  1637. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  1638. {
  1639. if(natInterface != 2)
  1640. {
  1641. system("/sbin/iptables -t nat -F");
  1642. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o ppp0 -j MASQUERADE");
  1643. natInterface = 2;
  1644. }
  1645. }
  1646. else
  1647. {
  1648. system("/sbin/iptables -t nat -F");
  1649. }
  1650. }
  1651. }
  1652. else
  1653. {
  1654. if(isKernelSupportNAT() == YES)
  1655. {
  1656. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  1657. {
  1658. system("/sbin/iptables -t nat -F");
  1659. natInterface = 0;
  1660. }
  1661. }
  1662. }
  1663. sleep(5);
  1664. }
  1665. }
  1666. DEBUG_INFO("Initial Ethernet OK\n");
  1667. }
  1668. int SpawnTask(uint8_t gun_index)
  1669. {
  1670. if(gun_index == 0)
  1671. {
  1672. system ("pkill Module_");
  1673. system ("pkill OcppBackend");
  1674. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1675. {
  1676. system("/root/Module_4g &");
  1677. }
  1678. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1679. {
  1680. system("/root/Module_Wifi &");
  1681. }
  1682. system("/root/Module_EventLogging &");
  1683. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  1684. {
  1685. ocpp_process_start();
  1686. }
  1687. else
  1688. {
  1689. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  1690. ocpp_process_start();
  1691. }
  1692. system ("/root/Module_AlarmDetect &");
  1693. system ("/root/Module_InternalComm &");
  1694. system ("/root/Module_Speaker &");
  1695. system ("/root/Module_ProduceUtils &");
  1696. system ("/root/Module_LcmControl &");
  1697. }
  1698. return PASS;
  1699. }
  1700. int InitQca7000()
  1701. {
  1702. int result = PASS;
  1703. if(isKernelSupportNAT() == YES)
  1704. system("/sbin/insmod /lib/qcaspi_nat.ko");
  1705. else
  1706. system("/sbin/insmod /lib/qcaspi.ko");
  1707. sleep(2);
  1708. system("/sbin/ifconfig eth1 192.168.253.11 netmask 255.255.255.0 up");
  1709. sleep(1);
  1710. return result;
  1711. }
  1712. int Initialization(uint8_t gun_index)
  1713. {
  1714. int result = PASS;
  1715. if(gun_index == 0)
  1716. {
  1717. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1718. InitGPIO();
  1719. InitEthernet();
  1720. if(InitQca7000() != PASS)
  1721. {
  1722. DEBUG_ERROR("QCA7000 initial fail.\n");
  1723. result = FAIL;
  1724. }
  1725. if(DB_Open(localDb) != PASS)
  1726. {
  1727. DEBUG_ERROR("Local database initial fail.\n");
  1728. result = FAIL;
  1729. }
  1730. if((rfidFd = InitRfidPort()) == FAIL)
  1731. {
  1732. DEBUG_ERROR("RFID port initial fail.\n");
  1733. result = FAIL;
  1734. }
  1735. if((wtdFd = InitWatchDog()) == FAIL)
  1736. {
  1737. DEBUG_ERROR("Watchdog initial fail.\n");
  1738. result = FAIL;
  1739. }
  1740. if(result == PASS)
  1741. DEBUG_INFO("Initialization OK.\n");
  1742. else
  1743. DEBUG_INFO("Initialization Fail.\n");
  1744. }
  1745. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  1746. return result;
  1747. }
  1748. //=====================================================
  1749. // Common routine
  1750. //=====================================================
  1751. char* getSystemModeName(unsigned char mode)
  1752. {
  1753. char* result;
  1754. switch(mode)
  1755. {
  1756. case SYS_MODE_BOOTING:
  1757. result = "booting";
  1758. break;
  1759. case SYS_MODE_IDLE:
  1760. result = "idle";
  1761. break;
  1762. case SYS_MODE_AUTHORIZING:
  1763. result = "authorizing";
  1764. break;
  1765. case SYS_MODE_PREPARING:
  1766. result = "preparing";
  1767. break;
  1768. case SYS_MODE_CHARGING:
  1769. result = "charging";
  1770. break;
  1771. case SYS_MODE_TERMINATING:
  1772. result = "terminating";
  1773. break;
  1774. case SYS_MODE_COMPLETE:
  1775. result = "complete";
  1776. break;
  1777. case SYS_MODE_ALARM:
  1778. result = "alarm";
  1779. break;
  1780. case SYS_MODE_FAULT:
  1781. result = "fault";
  1782. break;
  1783. case SYS_MODE_MAINTAIN:
  1784. result = "maintain";
  1785. break;
  1786. case SYS_MODE_RESERVATION:
  1787. result = "reservation";
  1788. break;
  1789. case SYS_MODE_BOOKING:
  1790. result = "booking";
  1791. break;
  1792. case SYS_MODE_DEBUG:
  1793. result = "debug";
  1794. break;
  1795. case SYS_MODE_UPDATE:
  1796. result = "upgrade";
  1797. break;
  1798. default:
  1799. result = "unknown";
  1800. break;
  1801. }
  1802. return result;
  1803. }
  1804. void setChargerMode(unsigned char gun_index, unsigned char mode)
  1805. {
  1806. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1807. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  1808. 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));
  1809. }
  1810. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  1811. {
  1812. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  1813. }
  1814. unsigned char isModeChange(unsigned char gun_index)
  1815. {
  1816. unsigned char result = NO;
  1817. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  1818. {
  1819. result = YES;
  1820. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1821. }
  1822. return result;
  1823. }
  1824. void gpio_set_value(unsigned int gpio, unsigned int value)
  1825. {
  1826. int fd;
  1827. char buf[256];
  1828. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  1829. fd = open(buf, O_WRONLY);
  1830. if (fd < 0)
  1831. {
  1832. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  1833. return;
  1834. }
  1835. if (value)
  1836. write(fd, "1", 2);
  1837. else
  1838. write(fd, "0", 2);
  1839. close(fd);
  1840. }
  1841. int gpio_get_value(unsigned int gpio)
  1842. {
  1843. int fd;
  1844. char buf[256];
  1845. char ch;
  1846. int8_t result = FAIL;
  1847. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  1848. fd = open(buf, O_RDONLY);
  1849. if (fd < 0)
  1850. {
  1851. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  1852. return result;
  1853. }
  1854. read(fd, &ch, 1);
  1855. if (ch != '0')
  1856. result = 1;
  1857. else
  1858. result = 0;
  1859. close(fd);
  1860. return result;
  1861. }
  1862. int presentChargedEnergyClear(unsigned char gun_index)
  1863. {
  1864. int result = FAIL;
  1865. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  1866. memset(ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod)*sizeof(float));
  1867. result = PASS;
  1868. return result;
  1869. }
  1870. float presentChargedEnergyTotal(unsigned char gun_index)
  1871. {
  1872. float result = 0.0f;
  1873. for(int idx=0;idx<ARRAY_SIZE(ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod);idx++)
  1874. {
  1875. result += ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod[idx];
  1876. }
  1877. return result;
  1878. }
  1879. int presentChargedEnergyUpdate(unsigned char gun_index)
  1880. {
  1881. int result = FAIL;
  1882. time_t CurrentTime;
  1883. struct tm *tm;
  1884. CurrentTime = time(NULL);
  1885. tm=localtime(&CurrentTime);
  1886. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  1887. {
  1888. // Resolution: 0.01 kwh
  1889. 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))/10.0) - presentChargedEnergyTotal(gun_index);
  1890. }
  1891. else
  1892. {
  1893. // Resolution: 0.01 kwh
  1894. 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))/10.0) +
  1895. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start))/10.0) +
  1896. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start))/10.0))
  1897. - presentChargedEnergyTotal(gun_index);
  1898. }
  1899. // Resolution: 0.1 kwh
  1900. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = presentChargedEnergyTotal(gun_index)/10.0;
  1901. return result;
  1902. }
  1903. //===============================================
  1904. // Get firmware version
  1905. //===============================================
  1906. void get_firmware_version(unsigned char gun_index)
  1907. {
  1908. FILE *fp;
  1909. char cmd[512];
  1910. char buf[512];
  1911. // Get CSU hardware version
  1912. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  1913. // Get CSU boot loader version
  1914. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1915. // Get CSU kernel version
  1916. sprintf(cmd, "/bin/uname -r");
  1917. fp = popen(cmd, "r");
  1918. if(fp == NULL)
  1919. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1920. else
  1921. {
  1922. while(fgets(buf, sizeof(buf), fp) != NULL)
  1923. {
  1924. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  1925. }
  1926. }
  1927. // Get MCU firmware version
  1928. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1929. // Get CSU root file system version
  1930. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.50.00.0000.00");
  1931. // Get AC connector type from model name
  1932. for(uint8_t idx=0;idx<3;idx++)
  1933. {
  1934. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1935. {
  1936. case '1':
  1937. // J1772 Plug
  1938. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  1939. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  1940. break;
  1941. case '2':
  1942. // J1772 Socket
  1943. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  1944. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  1945. break;
  1946. case '3':
  1947. // CE Plug
  1948. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  1949. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  1950. break;
  1951. case '4':
  1952. // CE Socket
  1953. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  1954. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  1955. break;
  1956. case '5':
  1957. // GB Plug
  1958. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  1959. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  1960. break;
  1961. case '6':
  1962. // GB Socket
  1963. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  1964. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  1965. break;
  1966. }
  1967. }
  1968. // Get network option from model name
  1969. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1970. {
  1971. case 'B':
  1972. case 'U':
  1973. //Blue tooth
  1974. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1975. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  1976. break;
  1977. case 'W':
  1978. // WIFI
  1979. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1980. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  1981. break;
  1982. case 'T':
  1983. // 3G/4G
  1984. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1985. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  1986. break;
  1987. default:
  1988. // LAN
  1989. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1990. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  1991. break;
  1992. }
  1993. // Get rating power from model name
  1994. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1995. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1996. // Get vender code from model name
  1997. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1998. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1999. DEBUG_INFO("========================================\n");
  2000. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2001. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  2002. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  2003. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  2004. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2005. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2006. DEBUG_INFO("========================================\n");
  2007. }
  2008. //===============================================
  2009. // Upgrade firmware
  2010. //===============================================
  2011. int upgrade_check()
  2012. {
  2013. int result = PASS;
  2014. int fd;
  2015. DIR *dir;
  2016. struct dirent *file;
  2017. char cmd[512];
  2018. long int MaxLen=48*1024*1024;
  2019. if ((dir = opendir ("/mnt")) != NULL)
  2020. {
  2021. /* print all the files and directories within directory */
  2022. while ((file = readdir (dir)) != NULL)
  2023. {
  2024. if((strlen(file->d_name)>2))
  2025. {
  2026. // Wait for MCU upgrade finish.
  2027. while(ShmCharger->gun_info[0].mcuFlag.isMcuUpgradeReq || (AC_QUANTITY>1?ShmCharger->gun_info[1].mcuFlag.isMcuUpgradeReq:FALSE))sleep(1);
  2028. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  2029. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  2030. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  2031. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  2032. {
  2033. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2034. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2035. read(fd, ptr, MaxLen);
  2036. close(fd);
  2037. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  2038. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  2039. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2040. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  2041. if((ShmCharger->fwUpgradeInfo.modelName[0] == ShmSysConfigAndInfo->SysConfig.ModelName[0]) &&
  2042. (ShmCharger->fwUpgradeInfo.modelName[1] == ShmSysConfigAndInfo->SysConfig.ModelName[1]) &&
  2043. (ShmCharger->fwUpgradeInfo.modelName[7] == ShmSysConfigAndInfo->SysConfig.ModelName[7]) &&
  2044. (ShmCharger->fwUpgradeInfo.modelName[8] == ShmSysConfigAndInfo->SysConfig.ModelName[8]) &&
  2045. (ShmCharger->fwUpgradeInfo.modelName[9] == ShmSysConfigAndInfo->SysConfig.ModelName[9]) &&
  2046. (ShmCharger->fwUpgradeInfo.modelName[11] == ShmSysConfigAndInfo->SysConfig.ModelName[11]) &&
  2047. (ShmCharger->fwUpgradeInfo.modelName[12] == ShmSysConfigAndInfo->SysConfig.ModelName[12]) &&
  2048. (ShmCharger->fwUpgradeInfo.modelName[13] == ShmSysConfigAndInfo->SysConfig.ModelName[13]) &&
  2049. (ShmCharger->fwUpgradeInfo.fwType>0))
  2050. {
  2051. switch(ShmCharger->fwUpgradeInfo.fwType)
  2052. {
  2053. case CSU_BOOTLOADER:
  2054. case CSU_KERNEL_CONFIGURATION:
  2055. case CSU_KERNEL_IMAGE:
  2056. case CSU_ROOT_FILE_SYSTEM:
  2057. case CSU_USER_CONFIGURATION:
  2058. case CSU_PRIMARY_CONTROLLER:
  2059. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  2060. {
  2061. result = FAIL;
  2062. }
  2063. else
  2064. {
  2065. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  2066. {
  2067. DEBUG_INFO("Restore model name & serial number.\n");
  2068. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2069. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2070. {
  2071. DEBUG_INFO("Re-load configuration fail.\n");
  2072. result = FAIL;
  2073. }
  2074. else
  2075. {
  2076. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  2077. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  2078. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  2079. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2080. {
  2081. DEBUG_INFO("Re-write configuration fail.\n");
  2082. result = FAIL;
  2083. }
  2084. else
  2085. DEBUG_INFO("Re-write configuration OK.\n");
  2086. }
  2087. }
  2088. }
  2089. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2090. system(cmd);
  2091. break;
  2092. case AC_WALLMOUNT_CONTROLLER:
  2093. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2094. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  2095. break;
  2096. default:
  2097. result = FAIL;
  2098. DEBUG_WARN("Image file is unknown type.\n");
  2099. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2100. system(cmd);
  2101. break;
  2102. }
  2103. }
  2104. else
  2105. {
  2106. result = FAIL;
  2107. DEBUG_ERROR("Model name and Firmware type error.\n");
  2108. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2109. system(cmd);
  2110. }
  2111. free(ptr);
  2112. }
  2113. else
  2114. {
  2115. result = FAIL;
  2116. DEBUG_ERROR("New firmware open error.\n");
  2117. }
  2118. }
  2119. else
  2120. {
  2121. if(strlen(file->d_name) >= 3)
  2122. {
  2123. result = FAIL;
  2124. DEBUG_ERROR("File name error.\n");
  2125. }
  2126. else
  2127. {
  2128. DEBUG_ERROR("Searching file.\n");
  2129. }
  2130. }
  2131. }
  2132. closedir (dir);
  2133. }
  2134. else
  2135. {
  2136. result = FAIL;
  2137. DEBUG_ERROR("/mnt does not valid.\n");
  2138. }
  2139. return result;
  2140. }
  2141. //===============================================
  2142. // Check RFID is match with start user
  2143. //===============================================
  2144. int isMatchStartUser(unsigned char gun_index)
  2145. {
  2146. uint8_t tmpUser[32];
  2147. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2148. {
  2149. // Big endian
  2150. switch(rfid.snType)
  2151. {
  2152. case RFID_SN_TYPE_6BYTE:
  2153. 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]);
  2154. break;
  2155. case RFID_SN_TYPE_7BYTE:
  2156. 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]);
  2157. break;
  2158. case RFID_SN_TYPE_10BYTE:
  2159. 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]);
  2160. break;
  2161. case RFID_SN_TYPE_4BYTE:
  2162. default:
  2163. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2164. break;
  2165. }
  2166. }
  2167. else
  2168. {
  2169. // Little endian
  2170. switch(rfid.snType)
  2171. {
  2172. case RFID_SN_TYPE_6BYTE:
  2173. 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]);
  2174. break;
  2175. case RFID_SN_TYPE_7BYTE:
  2176. 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]);
  2177. break;
  2178. case RFID_SN_TYPE_10BYTE:
  2179. 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]);
  2180. break;
  2181. case RFID_SN_TYPE_4BYTE:
  2182. default:
  2183. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2184. break;
  2185. }
  2186. }
  2187. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2188. }
  2189. //===============================================
  2190. // Read RFID Serial Number
  2191. //===============================================
  2192. int GetCardSerialNumber()
  2193. {
  2194. int isSuccess = FAIL;
  2195. int module_type = MODULE_EWT;
  2196. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2197. {
  2198. if(rfid.cardType == ISO14443A)
  2199. {
  2200. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2201. {
  2202. isSuccess = PASS;
  2203. }
  2204. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2205. {
  2206. isSuccess = PASS;
  2207. }
  2208. sethaltCard(rfidFd,module_type);
  2209. }
  2210. else if(rfid.cardType == IS014443B)
  2211. {
  2212. isSuccess = PASS;
  2213. }
  2214. else if(rfid.cardType == FELICA)
  2215. {
  2216. isSuccess = PASS;
  2217. }
  2218. else
  2219. {}
  2220. }
  2221. else
  2222. {}
  2223. return isSuccess;
  2224. }
  2225. //===============================================
  2226. // Set led motion
  2227. //===============================================
  2228. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  2229. {
  2230. switch(led_mode)
  2231. {
  2232. case LED_ACTION_INIT:
  2233. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  2234. break;
  2235. case LED_ACTION_IDLE:
  2236. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  2237. break;
  2238. case LED_ACTION_AUTHED:
  2239. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  2240. break;
  2241. case LED_ACTION_CONNECTED:
  2242. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  2243. break;
  2244. case LED_ACTION_CHARGING:
  2245. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  2246. break;
  2247. case LED_ACTION_STOP:
  2248. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  2249. break;
  2250. case LED_ACTION_ALARM:
  2251. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  2252. break;
  2253. case LED_ACTION_MAINTAIN:
  2254. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  2255. break;
  2256. case LED_ACTION_RFID_PASS:
  2257. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  2258. break;
  2259. case LED_ACTION_RFID_FAIL:
  2260. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  2261. break;
  2262. case LED_ACTION_BLE_CONNECT:
  2263. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  2264. break;
  2265. case LED_ACTION_BLE_DISABLE:
  2266. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  2267. break;
  2268. case LED_ACTION_DEBUG:
  2269. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  2270. break;
  2271. case LED_ACTION_ALL_OFF:
  2272. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  2273. break;
  2274. case LED_RELAY_ON:
  2275. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  2276. break;
  2277. case LED_RELAY_OFF:
  2278. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  2279. break;
  2280. case LED_ACTION_HANDSHAKE_FAIL:
  2281. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  2282. break;
  2283. case LED_ACTION_INTERNET_DISCONNECT:
  2284. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  2285. break;
  2286. }
  2287. }
  2288. //===============================================
  2289. // Request on/off set
  2290. //===============================================
  2291. void setRequest(unsigned char gun_index,unsigned char isOn)
  2292. {
  2293. if(isOn == ON)
  2294. {
  2295. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  2296. {
  2297. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  2298. DEBUG_INFO("Gun-%d Output relay sts: ON \n",gun_index);
  2299. }
  2300. }
  2301. else
  2302. {
  2303. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2304. {
  2305. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  2306. DEBUG_INFO("Gun-%d Output relay sts: OFF \n",gun_index);
  2307. }
  2308. }
  2309. }
  2310. //===============================================
  2311. // Request on/off get
  2312. //===============================================
  2313. int getRequest(unsigned char gun_index)
  2314. {
  2315. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  2316. }
  2317. //===============================================
  2318. // Relay on/off set
  2319. //===============================================
  2320. void setRelay(unsigned char gun_index,unsigned char isOn)
  2321. {
  2322. if(isOn == ON)
  2323. {
  2324. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  2325. {
  2326. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  2327. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  2328. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  2329. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  2330. DEBUG_INFO("Gun-%d Output relay sts: ON \n",gun_index);
  2331. }
  2332. }
  2333. else
  2334. {
  2335. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  2336. {
  2337. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  2338. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  2339. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  2340. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  2341. DEBUG_INFO("Gun-%d Output relay sts: OFF \n",gun_index);
  2342. }
  2343. }
  2344. }
  2345. //===============================================
  2346. // Relay on/off get
  2347. //===============================================
  2348. int getRelay(unsigned char gun_index)
  2349. {
  2350. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  2351. }
  2352. //===============================================
  2353. // Set speaker on/off request
  2354. //===============================================
  2355. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  2356. {
  2357. if(isOn == ON)
  2358. {
  2359. ShmCharger->isSpeakerOn = ON;
  2360. ShmCharger->speaker_type = speaker_mode;
  2361. }
  2362. }
  2363. //===============================================
  2364. // Initialization RFID communication port
  2365. //===============================================
  2366. int InitRfidPort()
  2367. {
  2368. int uartO2 = open(rfidPortName, O_RDWR);
  2369. struct termios tios;
  2370. if (uartO2 != FAIL)
  2371. {
  2372. ioctl (uartO2, TCGETS, &tios);
  2373. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  2374. tios.c_lflag = 0;
  2375. tios.c_iflag = 0;
  2376. tios.c_oflag = 0;
  2377. tios.c_cc[VMIN] = 0;
  2378. tios.c_cc[VTIME] = (unsigned char) 1;
  2379. tios.c_lflag = 0;
  2380. tcflush(uartO2, TCIFLUSH);
  2381. ioctl(uartO2, TCSETS, &tios);
  2382. }
  2383. if (uartO2 < 0)
  2384. {
  2385. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  2386. }
  2387. return uartO2;
  2388. }
  2389. //===============================================
  2390. // Check internet access status
  2391. //===============================================
  2392. int isReachableInternet()
  2393. {
  2394. int result = FAIL;
  2395. FILE *fp;
  2396. char cmd[256];
  2397. char buf[512];
  2398. char tmp[512];
  2399. // Get ip address & net mask
  2400. strcpy(cmd, "ifconfig eth0");
  2401. fp = popen(cmd, "r");
  2402. if (fp != NULL)
  2403. {
  2404. while(fgets(buf, sizeof(buf), fp) != NULL)
  2405. {
  2406. if (strstr(buf, "inet addr:") > 0)
  2407. {
  2408. sscanf(buf, "%*s%s", tmp);
  2409. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  2410. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != 0)
  2411. {
  2412. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  2413. }
  2414. sscanf(buf, "%*s%*s%*s%s", tmp);
  2415. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  2416. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  2417. {
  2418. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  2419. }
  2420. }
  2421. }
  2422. }
  2423. pclose(fp);
  2424. memset(buf, 0x00, sizeof(buf));
  2425. // Get gateway
  2426. fp = popen("ip route", "r");
  2427. if(fp == NULL)
  2428. result = FAIL;
  2429. else
  2430. {
  2431. while(fgets(buf, sizeof(buf), fp) != NULL)
  2432. {
  2433. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  2434. break;
  2435. }
  2436. if(strstr(buf, "default") != NULL)
  2437. {
  2438. sscanf(buf, "%*s%*s%s", tmp);
  2439. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  2440. }
  2441. }
  2442. pclose(fp);
  2443. memset(buf, 0x00, sizeof(buf));
  2444. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  2445. {
  2446. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  2447. fp = popen(cmd, "r");
  2448. if(fp != NULL)
  2449. {
  2450. while(fgets(buf, sizeof(buf), fp) != NULL)
  2451. {
  2452. if(strstr(buf, "transmitted") > 0)
  2453. {
  2454. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  2455. if(strstr(buf,"100%") != NULL)
  2456. {
  2457. }
  2458. else
  2459. {
  2460. result = PASS;
  2461. }
  2462. //DEBUG_INFO("%s",buf);
  2463. //DEBUG_INFO("%s\n",tmp);
  2464. }
  2465. }
  2466. }
  2467. pclose(fp);
  2468. }
  2469. return result;
  2470. }
  2471. int isRouteFail()
  2472. {
  2473. int result = YES;
  2474. FILE *fp;
  2475. char buf[512];
  2476. fp = popen("route -n", "r");
  2477. if(fp != NULL)
  2478. {
  2479. while(fgets(buf, sizeof(buf), fp) != NULL)
  2480. {
  2481. if(strstr(buf, "eth0") != NULL)
  2482. result = NO;
  2483. }
  2484. }
  2485. pclose(fp);
  2486. return result;
  2487. }
  2488. //===============================================
  2489. // Check reservation date is expired
  2490. //===============================================
  2491. int isReservationExpired(unsigned char gun_index)
  2492. {
  2493. int result = NO;
  2494. struct tm expiredDate;
  2495. struct timeb expiredTime;
  2496. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2497. {
  2498. 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)
  2499. {
  2500. expiredDate.tm_year -= 1900;
  2501. expiredDate.tm_mon -= 1;
  2502. expiredTime.time = mktime(&expiredDate);
  2503. if(DiffTimebWithNow(expiredTime)>=0)
  2504. {
  2505. result = YES;
  2506. }
  2507. }
  2508. else
  2509. {
  2510. DEBUG_WARN("Expired date parsing error.\n");
  2511. DEBUG_INFO("Date expired.\n");
  2512. result = YES;
  2513. }
  2514. }
  2515. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2516. {
  2517. 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)
  2518. {
  2519. expiredDate.tm_year -= 1900;
  2520. expiredDate.tm_mon -= 1;
  2521. expiredTime.time = mktime(&expiredDate);
  2522. if(DiffTimebWithNow(expiredTime)>=0)
  2523. {
  2524. result = YES;
  2525. }
  2526. }
  2527. else
  2528. {
  2529. DEBUG_WARN("Expired date parsing error.\n");
  2530. DEBUG_INFO("Date expired.\n");
  2531. result = YES;
  2532. }
  2533. }
  2534. return result;
  2535. }
  2536. //===============================================
  2537. // Check charging profile related date routine
  2538. //===============================================
  2539. int isProfileValid(uint8_t gun_index)
  2540. {
  2541. int result = NO;
  2542. struct tm tmFrom, tmTo;
  2543. struct timeb tbFrom, tbTo;
  2544. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2545. {
  2546. 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) &&
  2547. (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))
  2548. {
  2549. tmFrom.tm_year -= 1900;
  2550. tmFrom.tm_mon -= 1;
  2551. tbFrom.time = mktime(&tmFrom);
  2552. tmTo.tm_year -= 1900;
  2553. tmTo.tm_mon -= 1;
  2554. tbTo.time = mktime(&tmTo);
  2555. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2556. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2557. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2558. {
  2559. result = YES;
  2560. }
  2561. }
  2562. else
  2563. {
  2564. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2565. }
  2566. }
  2567. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2568. {
  2569. 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) &&
  2570. (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))
  2571. {
  2572. tmFrom.tm_year -= 1900;
  2573. tmFrom.tm_mon -= 1;
  2574. tbFrom.time = mktime(&tmFrom);
  2575. tmTo.tm_year -= 1900;
  2576. tmTo.tm_mon -= 1;
  2577. tbTo.time = mktime(&tmTo);
  2578. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2579. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2580. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2581. {
  2582. result = YES;
  2583. }
  2584. }
  2585. else
  2586. {
  2587. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2588. }
  2589. }
  2590. return result;
  2591. }
  2592. int getScheduleStart(int gun_index)
  2593. {
  2594. int result = -1;
  2595. struct tm tmScheduleStart;;
  2596. struct timeb tbScheduleStart;
  2597. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2598. {
  2599. 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))
  2600. {
  2601. tmScheduleStart.tm_year -= 1900;
  2602. tmScheduleStart.tm_mon -= 1;
  2603. tbScheduleStart.time = mktime(&tmScheduleStart);
  2604. tbScheduleStart.millitm = 0;
  2605. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2606. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  2607. }
  2608. else
  2609. {
  2610. DEBUG_WARN("Schedule start date parsing error.\n");
  2611. }
  2612. }
  2613. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2614. {
  2615. 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))
  2616. {
  2617. tmScheduleStart.tm_year -= 1900;
  2618. tmScheduleStart.tm_mon -= 1;
  2619. tbScheduleStart.time = mktime(&tmScheduleStart);
  2620. tbScheduleStart.millitm = 0;
  2621. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2622. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  2623. }
  2624. else
  2625. {
  2626. DEBUG_WARN("Schedule start date parsing error.\n");
  2627. }
  2628. }
  2629. return result;
  2630. }
  2631. int getStartSinceToday()
  2632. {
  2633. int result = -1;
  2634. time_t t;
  2635. struct tm *tmStartToday;
  2636. struct timeb tbStartToday;
  2637. t=time(NULL);
  2638. tmStartToday=localtime(&t);
  2639. tmStartToday->tm_hour = 0;
  2640. tmStartToday->tm_min = 0;
  2641. tmStartToday->tm_sec = 0;
  2642. tbStartToday.time = mktime(tmStartToday);
  2643. result = DiffTimebWithNow(tbStartToday)/1000;
  2644. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  2645. return result;
  2646. }
  2647. int getStartSinceWeek()
  2648. {
  2649. int result = -1;
  2650. time_t t;
  2651. struct tm *tmStartWeek;
  2652. struct timeb tbStartWeek;
  2653. t=time(NULL);
  2654. tmStartWeek=localtime(&t);
  2655. t-=86400*tmStartWeek->tm_wday;
  2656. tmStartWeek=localtime(&t);
  2657. tmStartWeek->tm_hour = 0;
  2658. tmStartWeek->tm_min = 0;
  2659. tmStartWeek->tm_sec = 0;
  2660. tbStartWeek.time = mktime(tmStartWeek);
  2661. result = DiffTimebWithNow(tbStartWeek)/1000;
  2662. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  2663. return result;
  2664. }
  2665. //===============================================
  2666. // Valid from local white list
  2667. //===============================================
  2668. int isValidLocalWhiteCard()
  2669. {
  2670. uint8_t result = FAIL;
  2671. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  2672. {
  2673. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  2674. {
  2675. result = PASS;
  2676. }
  2677. }
  2678. return result;
  2679. }
  2680. //==========================================
  2681. // Check routine
  2682. //==========================================
  2683. void checkTask()
  2684. {
  2685. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2686. {
  2687. if(system("pidof -s Module_4g > /dev/null") != 0)
  2688. {
  2689. DEBUG_INFO("Module_4g not running, restart it.\n");
  2690. system("/root/Module_4g &");
  2691. }
  2692. }
  2693. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2694. {
  2695. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  2696. {
  2697. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  2698. system("/root/Module_Wifi &");
  2699. }
  2700. }
  2701. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  2702. {
  2703. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  2704. system("/root/Module_EventLogging &");
  2705. }
  2706. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  2707. {
  2708. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2709. {
  2710. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  2711. {
  2712. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  2713. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  2714. system("pkill OcppBackend");
  2715. }
  2716. if(system("pidof -s OcppBackend > /dev/null") != 0)
  2717. {
  2718. DEBUG_INFO("OcppBackend not running, restart it.\n");
  2719. ocpp_process_start();
  2720. }
  2721. }
  2722. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2723. {
  2724. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  2725. {
  2726. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  2727. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  2728. system("pkill OcppBackend20");
  2729. }
  2730. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  2731. {
  2732. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  2733. ocpp_process_start();
  2734. }
  2735. }
  2736. }
  2737. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  2738. {
  2739. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  2740. system("/root/Module_AlarmDetect &");
  2741. }
  2742. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  2743. {
  2744. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  2745. system("/root/Module_InternalComm &");
  2746. }
  2747. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  2748. {
  2749. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  2750. system("/root/Module_Speaker &");
  2751. }
  2752. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  2753. {
  2754. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  2755. system ("/root/Module_ProduceUtils &");
  2756. }
  2757. if(system("pidof -s Module_LcmControl > /dev/null") != 0)
  2758. {
  2759. DEBUG_INFO("Module_LcmControl not running, restart it.\n");
  2760. system ("/root/Module_LcmControl &");
  2761. }
  2762. if((system("pidof -s Module_PowerSharing > /dev/null") != 0) &&
  2763. ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  2764. {
  2765. DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
  2766. system ("/root/Module_PowerSharing &");
  2767. }
  2768. if((system("pidof -s Module_InitUpgrade > /dev/null") != 0) &&
  2769. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
  2770. {
  2771. DEBUG_INFO("Module_InitUpgrade not running, restart it.\n");
  2772. system ("/root/Module_InitUpgrade &");
  2773. }
  2774. }
  2775. void checkConnectionTimeout()
  2776. {
  2777. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  2778. {
  2779. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2780. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  2781. }
  2782. else
  2783. {
  2784. ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
  2785. if((ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  2786. {
  2787. ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE+10000);
  2788. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  2789. }
  2790. else
  2791. {
  2792. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  2793. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  2794. }
  2795. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2796. }
  2797. }
  2798. void checkReset()
  2799. {
  2800. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2801. {
  2802. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2803. {
  2804. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2805. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2806. {
  2807. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  2808. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2809. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  2810. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  2811. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2812. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2813. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  2814. {
  2815. system("sync");
  2816. sleep(5);
  2817. system("reboot -f");
  2818. sleep(5);
  2819. system("reboot -f");
  2820. }
  2821. else
  2822. {
  2823. sleep(5);
  2824. close(wtdFd);
  2825. system("/usr/bin/run_evse_restart.sh");
  2826. }
  2827. }
  2828. }
  2829. }
  2830. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2831. {
  2832. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  2833. {
  2834. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2835. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2836. {
  2837. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  2838. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  2839. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  2840. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  2841. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2842. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2843. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  2844. {
  2845. system("sync");
  2846. sleep(5);
  2847. system("reboot -f");
  2848. sleep(5);
  2849. system("reboot -f");
  2850. }
  2851. else
  2852. {
  2853. sleep(5);
  2854. close(wtdFd);
  2855. system("/usr/bin/run_evse_restart.sh");
  2856. }
  2857. }
  2858. }
  2859. }
  2860. }
  2861. void checkReservation(uint8_t gun_index)
  2862. {
  2863. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2864. {
  2865. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  2866. {
  2867. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2868. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2869. {
  2870. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  2871. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2872. }
  2873. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2874. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2875. }
  2876. }
  2877. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2878. {
  2879. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  2880. {
  2881. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2882. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2883. {
  2884. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  2885. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2886. }
  2887. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2888. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2889. }
  2890. }
  2891. }
  2892. void checkUnlocker(uint8_t gun_index)
  2893. {
  2894. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2895. {
  2896. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2897. {
  2898. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2899. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  2900. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  2901. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  2902. }
  2903. }
  2904. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2905. {
  2906. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2907. {
  2908. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2909. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "NotSupported");
  2910. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  2911. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  2912. }
  2913. }
  2914. }
  2915. void checkAvailability(uint8_t gun_index)
  2916. {
  2917. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2918. {
  2919. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2920. {
  2921. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  2922. {
  2923. DB_Update_Operactive(localDb, gun_index, true);
  2924. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2925. }
  2926. else
  2927. {
  2928. DB_Update_Operactive(localDb, gun_index, false);
  2929. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2930. }
  2931. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2932. }
  2933. }
  2934. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2935. {
  2936. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2937. {
  2938. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  2939. {
  2940. DB_Update_Operactive(localDb, gun_index, true);
  2941. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2942. }
  2943. else
  2944. {
  2945. DB_Update_Operactive(localDb, gun_index, false);
  2946. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2947. }
  2948. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2949. }
  2950. }
  2951. }
  2952. void checkChargingProfileLimit(uint8_t gun_index)
  2953. {
  2954. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2955. {
  2956. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  2957. (ocpp_get_profile_req(gun_index) != ON) &&
  2958. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2959. {
  2960. // Debug information
  2961. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2962. {
  2963. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2964. DEBUG_INFO("Valid from: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2965. DEBUG_INFO("Valid to: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2966. DEBUG_INFO("Start schedule: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2967. DEBUG_INFO("Profile kind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2968. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2969. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2970. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2971. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2972. DEBUG_INFO("===============================================================\n");
  2973. }
  2974. // Checking profile kind
  2975. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2976. {
  2977. // Absolute profile
  2978. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2979. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2980. {
  2981. // Checking limitation
  2982. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2983. {
  2984. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2985. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  2986. )
  2987. {
  2988. 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);
  2989. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2990. }
  2991. else
  2992. break;
  2993. }
  2994. }
  2995. }
  2996. }
  2997. else if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == 0) && (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  2998. {
  2999. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3000. }
  3001. }
  3002. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3003. {
  3004. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  3005. (ocpp_get_profile_req(gun_index) != ON) &&
  3006. (((strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom)>0) && (strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo)>0)) ? isProfileValid(gun_index) : ON))
  3007. {
  3008. // Debug information
  3009. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  3010. {
  3011. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].id);
  3012. DEBUG_INFO("Valid from: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom);
  3013. DEBUG_INFO("Valid to: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validTo);
  3014. DEBUG_INFO("Start schedule: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  3015. DEBUG_INFO("Profile kind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind);
  3016. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].recurrencyKind);
  3017. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose);
  3018. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId);
  3019. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingRateUnit);
  3020. DEBUG_INFO("===============================================================\n");
  3021. }
  3022. // Checking profile kind
  3023. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  3024. {
  3025. // Absolute profile
  3026. if(((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId == ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId)) ||
  3027. (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == FAIL))
  3028. {
  3029. // Checking limitation
  3030. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3031. {
  3032. if((getScheduleStart(gun_index) > ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3033. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3034. )
  3035. {
  3036. 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);
  3037. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3038. }
  3039. else
  3040. break;
  3041. }
  3042. }
  3043. }
  3044. }
  3045. else if((ShmOCPP20Data->SmartChargingProfile[gun_index].id == 0) && (ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  3046. {
  3047. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3048. }
  3049. }
  3050. }
  3051. void checkStopReason(uint8_t gun_index)
  3052. {
  3053. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3054. {
  3055. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3056. if(!ocpp_get_starttransaction_result(gun_index))
  3057. {
  3058. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  3059. }
  3060. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3061. {
  3062. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  3063. }
  3064. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3065. {
  3066. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3067. }
  3068. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3069. {
  3070. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3071. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3072. else
  3073. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3074. }
  3075. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3076. {
  3077. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3078. if(!isMatchStartUser(gun_index))
  3079. {
  3080. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3081. }
  3082. else
  3083. {
  3084. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3085. }
  3086. DEBUG_INFO("Gun-%d [ IdTag ] : %s \n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  3087. }
  3088. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3089. {
  3090. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3091. }
  3092. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3093. {
  3094. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3095. }
  3096. else
  3097. {
  3098. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3099. }
  3100. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3101. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  3102. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3103. }
  3104. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3105. {
  3106. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3107. if(!ocpp_get_starttransaction_result(gun_index))
  3108. {
  3109. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  3110. }
  3111. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3112. {
  3113. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  3114. }
  3115. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3116. {
  3117. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  3118. }
  3119. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  3120. {
  3121. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  3122. }
  3123. else if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3124. {
  3125. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0)
  3126. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  3127. else
  3128. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OnIdle");
  3129. }
  3130. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3131. {
  3132. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  3133. if(!isMatchStartUser(gun_index))
  3134. {
  3135. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3136. }
  3137. else
  3138. {
  3139. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3140. }
  3141. DEBUG_INFO("Gun-%d [ idToken ] : %s \n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  3142. }
  3143. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L1_OVER_CURRENT) ||
  3144. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L2_OVER_CURRENT) ||
  3145. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L3_OVER_CURRENT))
  3146. {
  3147. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  3148. }
  3149. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  3150. {
  3151. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  3152. }
  3153. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  3154. {
  3155. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  3156. }
  3157. else
  3158. {
  3159. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  3160. }
  3161. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  3162. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  3163. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  3164. }
  3165. }
  3166. void checkRemoteUpgradeStatus()
  3167. {
  3168. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3169. {
  3170. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  3171. {
  3172. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3173. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3174. ShmCharger->isUpdateSuccess = NO;
  3175. }
  3176. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  3177. {
  3178. DEBUG_INFO("Firmware remote upgrading...\n");
  3179. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3180. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3181. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3182. int result = upgrade_check();
  3183. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3184. if(result == PASS)
  3185. {
  3186. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3187. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3188. ShmCharger->isUpdateSuccess = YES;
  3189. DEBUG_INFO("Remote update success...\n");
  3190. }
  3191. else
  3192. {
  3193. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3194. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3195. ShmCharger->isUpdateSuccess = NO;
  3196. DEBUG_INFO("Remote update unsuccess...\n");
  3197. }
  3198. }
  3199. else
  3200. {}
  3201. }
  3202. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3203. {
  3204. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  3205. {
  3206. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3207. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3208. ShmCharger->isUpdateSuccess = NO;
  3209. }
  3210. else if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0)
  3211. {
  3212. DEBUG_INFO("Firmware remote upgrading...\n");
  3213. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3214. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3215. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3216. int result = upgrade_check();
  3217. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3218. if(result == PASS)
  3219. {
  3220. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3221. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3222. ShmCharger->isUpdateSuccess = YES;
  3223. DEBUG_INFO("Remote update success...\n");
  3224. }
  3225. else
  3226. {
  3227. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3228. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3229. ShmCharger->isUpdateSuccess = NO;
  3230. DEBUG_INFO("Remote update unsuccess...\n");
  3231. }
  3232. }
  3233. else
  3234. {}
  3235. }
  3236. }
  3237. //======================================================
  3238. // Main process
  3239. //======================================================
  3240. int main(void)
  3241. {
  3242. //==================================================
  3243. // Create all share memory
  3244. //==================================================
  3245. if(CreatShareMemory()==0)
  3246. {
  3247. DEBUG_ERROR("CreatShareMemory NG\n");
  3248. if(ShmStatusCodeData!=NULL)
  3249. {
  3250. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  3251. }
  3252. sleep(5);
  3253. system("reboot -f");
  3254. sleep(5);
  3255. system("reboot -f");
  3256. }
  3257. else
  3258. {
  3259. DEBUG_INFO("CreatShareMemory OK\n");
  3260. }
  3261. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3262. {
  3263. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  3264. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  3265. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  3266. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  3267. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  3268. }
  3269. //==================================================
  3270. // Main loop
  3271. //==================================================
  3272. for(;;)
  3273. {
  3274. //==============================================
  3275. // Synchronize share memory from OCPP struct
  3276. //==============================================
  3277. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  3278. //==============================================
  3279. // Check task processing
  3280. //==============================================
  3281. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3282. checkTask();
  3283. //==============================================
  3284. // Check connection timeout specification
  3285. //==============================================
  3286. checkConnectionTimeout();
  3287. //==============================================
  3288. // Something need run in Idle mode
  3289. //==============================================
  3290. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  3291. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  3292. {
  3293. // Check restore factory setting request
  3294. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3295. {
  3296. // Set led to initial
  3297. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3298. {
  3299. setLedMotion(gun_index,LED_ACTION_INIT);
  3300. }
  3301. system("cd /root;./Module_FactoryConfig -m");
  3302. system("rm -f /Storage/OCPP/OCPPConfiguration");
  3303. system("sync");
  3304. sleep(5);
  3305. system("reboot -f");
  3306. sleep(5);
  3307. system("reboot -f");
  3308. }
  3309. // Check upgrade firmware request
  3310. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  3311. ocpp_get_update_firmware_req())
  3312. {
  3313. // If available memory too small, free memory cache first
  3314. if(getAvailableMemory() < (200*1024*1024))
  3315. {
  3316. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  3317. system("echo 3 > /proc/sys/vm/drop_caches");
  3318. }
  3319. ShmCharger->isUpdateSuccess = NO;
  3320. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3321. {
  3322. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3323. setChargerMode(gun_index, SYS_MODE_UPDATE);
  3324. }
  3325. }
  3326. }
  3327. //==============================================
  3328. // Check remote reset request
  3329. //==============================================
  3330. checkReset();
  3331. //==============================================
  3332. // Check RFID
  3333. //==============================================
  3334. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  3335. {
  3336. if(GetCardSerialNumber()!= FAIL)
  3337. {
  3338. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  3339. }
  3340. }
  3341. //==============================================
  3342. // Connector loop
  3343. //==============================================
  3344. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3345. {
  3346. //==========================================
  3347. // Power saving logic
  3348. //==========================================
  3349. /*
  3350. * TODO:
  3351. * 1. Power saving logic
  3352. */
  3353. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3354. {
  3355. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  3356. }
  3357. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) ||
  3358. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING))
  3359. {
  3360. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  3361. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  3362. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  3363. }
  3364. else
  3365. {
  3366. if((ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON) ||
  3367. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING)) &&
  3368. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) &&
  3369. (DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]) < 10000)))
  3370. {
  3371. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  3372. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  3373. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  3374. }
  3375. }
  3376. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) > TIMEOUT_SPEC_POWERSAVING_LCD)
  3377. {
  3378. if(ShmCharger->isLcdOn == ON)
  3379. {
  3380. //if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) < (TIMEOUT_SPEC_POWERSAVING_LCD+600))
  3381. DEBUG_INFO("LCD into power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  3382. ShmCharger->isLcdOn = OFF;
  3383. }
  3384. }
  3385. else
  3386. {
  3387. if(ShmCharger->isLcdOn == OFF)
  3388. {
  3389. //if((0 < DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD])) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) < 600))
  3390. DEBUG_INFO("LCD exit power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  3391. ShmCharger->isLcdOn = ON;
  3392. }
  3393. }
  3394. if(DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]) > TIMEOUT_SPEC_POWERSAVING_RFID)
  3395. {
  3396. if(gpio_get_value(GPIO_OUT_RST_RFID) == ON)
  3397. {
  3398. DEBUG_INFO("RFID into power saving...%d\n", DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  3399. gpio_set_value(GPIO_OUT_RST_RFID, OFF);
  3400. }
  3401. }
  3402. else
  3403. {
  3404. if(gpio_get_value(GPIO_OUT_RST_RFID) == OFF)
  3405. {
  3406. DEBUG_INFO("RFID exit power saving...%d\n", DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  3407. gpio_set_value(GPIO_OUT_RST_RFID, ON);
  3408. }
  3409. }
  3410. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]) > TIMEOUT_SPEC_POWERSAVING_METER)
  3411. {
  3412. if(ShmCharger->gun_info[gun_index].isMeterOn )
  3413. {
  3414. DEBUG_INFO("Meter into power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  3415. ShmCharger->gun_info[gun_index].isMeterOn = OFF;
  3416. }
  3417. }
  3418. else
  3419. {
  3420. if(!ShmCharger->gun_info[gun_index].isMeterOn )
  3421. {
  3422. DEBUG_INFO("Meter exit power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  3423. ShmCharger->gun_info[gun_index].isMeterOn = ON;
  3424. }
  3425. }
  3426. //==========================================
  3427. // Synchronize current rating value from MCU
  3428. //==========================================
  3429. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3430. //==========================================
  3431. // Assign connector location index for OCPP
  3432. //==========================================
  3433. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  3434. //==========================================
  3435. // Synchronize present charging power
  3436. //==========================================
  3437. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  3438. {
  3439. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  3440. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  3441. else
  3442. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  3443. }
  3444. else
  3445. {
  3446. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  3447. {
  3448. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000) +
  3449. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2)/1000) +
  3450. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3)/1000));
  3451. }
  3452. else
  3453. {
  3454. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  3455. }
  3456. }
  3457. //==========================================
  3458. // Check initialization "PASS" or "FAIL"
  3459. //==========================================
  3460. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3461. {
  3462. // Alarm event check
  3463. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  3464. {
  3465. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  3466. {
  3467. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  3468. {
  3469. setChargerMode(gun_index, SYS_MODE_ALARM);
  3470. }
  3471. }
  3472. }
  3473. }
  3474. //==========================================
  3475. // Reservation request check
  3476. //==========================================
  3477. checkReservation(gun_index);
  3478. //==========================================
  3479. // Change availability check
  3480. //==========================================
  3481. checkAvailability(gun_index);
  3482. if(ShmCharger->gun_info[gun_index].isOperactive)
  3483. {
  3484. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  3485. {
  3486. setChargerMode(gun_index, SYS_MODE_IDLE);
  3487. }
  3488. }
  3489. else
  3490. {
  3491. if(isMode(gun_index, SYS_MODE_IDLE))
  3492. {
  3493. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  3494. }
  3495. }
  3496. //==========================================
  3497. // Unlock Connector signal check
  3498. //==========================================
  3499. checkUnlocker(gun_index);
  3500. //==========================================
  3501. // Connector process
  3502. //==========================================
  3503. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  3504. {
  3505. case SYS_MODE_BOOTING:
  3506. if(isModeChange(gun_index))
  3507. {
  3508. setLedMotion(gun_index,LED_ACTION_INIT);
  3509. //CSU Initialization & task spawn
  3510. if((Initialization(gun_index) != PASS) ||
  3511. (SpawnTask(gun_index) != PASS))
  3512. {
  3513. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  3514. }
  3515. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  3516. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  3517. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  3518. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  3519. }
  3520. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  3521. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  3522. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  3523. {
  3524. ShmCharger->gun_selectd = 0;
  3525. // Firmware version
  3526. get_firmware_version(gun_index);
  3527. // OCPP BootNotification info set
  3528. DEBUG_INFO("==========================================\n");
  3529. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  3530. DEBUG_INFO("==========================================\n");
  3531. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  3532. ocpp_boot_info_sync();
  3533. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  3534. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  3535. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  3536. DEBUG_INFO("==========================================\n");
  3537. DEBUG_INFO("========== Set Wifi information ==========\n");
  3538. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  3539. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  3540. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  3541. DEBUG_INFO("==========================================\n");
  3542. // Set max current to rating current
  3543. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3544. // Default Ethernet / Wifi / 4G to 1:disconnected
  3545. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  3546. {
  3547. case 'E':
  3548. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3549. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  3550. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  3551. break;
  3552. case 'W':
  3553. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3554. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  3555. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  3556. break;
  3557. case 'T':
  3558. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3559. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  3560. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  3561. break;
  3562. case 'D':
  3563. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3564. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  3565. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  3566. break;
  3567. }
  3568. // Default LCM brightness to 100
  3569. ShmCharger->isLcdOn = ON;
  3570. // If Web Server OPCC URL is empty kill Module_OcppBackend
  3571. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  3572. {
  3573. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  3574. system ("pkill OcppBackend");
  3575. }
  3576. // If rotate switch equal zero, the system needs to change Debug mode
  3577. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  3578. setChargerMode(gun_index, SYS_MODE_DEBUG);
  3579. else
  3580. setChargerMode(gun_index, SYS_MODE_IDLE);
  3581. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = ((ShmSysConfigAndInfo->SysConfig.ModelName[2]=='Y') ||
  3582. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='D') ||
  3583. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='W')
  3584. ?3:1);
  3585. }
  3586. break;
  3587. case SYS_MODE_IDLE:
  3588. if(isModeChange(gun_index))
  3589. {
  3590. setLedMotion(gun_index,LED_ACTION_IDLE);
  3591. setRelay(gun_index, OFF);
  3592. setRequest(gun_index,OFF);
  3593. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3594. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3595. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3596. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3597. ocpp_set_remotestart(gun_index, OFF);
  3598. ocpp_set_remotestop(gun_index, OFF);
  3599. ocpp_set_auth_conf(OFF);
  3600. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  3601. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  3602. presentChargedEnergyClear(gun_index);
  3603. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  3604. ocpp_set_unlocker_req(gun_index, OFF);
  3605. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  3606. // Response StopTransactionConf
  3607. ocpp_set_stoptransaction_conf(gun_index, OFF);
  3608. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_IDLE;
  3609. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3610. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3611. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3612. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
  3613. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  3614. if(ShmCharger->isCcsEnable)system("pkill Module_CCS");
  3615. }
  3616. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  3617. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  3618. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3619. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  3620. (ocpp_get_remotestart(gun_index) == ON) ||
  3621. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  3622. {
  3623. // Clean User id & Card Number
  3624. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3625. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  3626. {
  3627. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  3628. DEBUG_INFO("Start Method : RFID...\n");
  3629. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3630. {
  3631. // Big endian
  3632. switch(rfid.snType)
  3633. {
  3634. case RFID_SN_TYPE_6BYTE:
  3635. 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]);
  3636. break;
  3637. case RFID_SN_TYPE_7BYTE:
  3638. 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]);
  3639. break;
  3640. case RFID_SN_TYPE_10BYTE:
  3641. 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]);
  3642. break;
  3643. case RFID_SN_TYPE_4BYTE:
  3644. default:
  3645. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3646. break;
  3647. }
  3648. }
  3649. else
  3650. {
  3651. // Little endian
  3652. switch(rfid.snType)
  3653. {
  3654. case RFID_SN_TYPE_6BYTE:
  3655. 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]);
  3656. break;
  3657. case RFID_SN_TYPE_7BYTE:
  3658. 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]);
  3659. break;
  3660. case RFID_SN_TYPE_10BYTE:
  3661. 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]);
  3662. break;
  3663. case RFID_SN_TYPE_4BYTE:
  3664. default:
  3665. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3666. break;
  3667. }
  3668. }
  3669. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3670. }
  3671. else if(ocpp_get_remotestart(gun_index))
  3672. {
  3673. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  3674. ocpp_copy_userid_from_remotestart(gun_index);
  3675. DEBUG_INFO("Start Method : BACKEND...\n");
  3676. }
  3677. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  3678. {
  3679. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  3680. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  3681. DEBUG_INFO("Start Method : BLE...\n");
  3682. }
  3683. else
  3684. {
  3685. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  3686. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  3687. DEBUG_INFO("Start Method : FREE...\n");
  3688. }
  3689. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3690. ocpp_set_remotestart(gun_index, OFF);
  3691. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3692. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3693. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  3694. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3695. }
  3696. else
  3697. {}
  3698. break;
  3699. case SYS_MODE_AUTHORIZING:
  3700. if(isModeChange(gun_index))
  3701. {
  3702. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3703. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  3704. {
  3705. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3706. {
  3707. case AUTH_MODE_ENABLE:
  3708. if(ocpp_get_connection_status())
  3709. {
  3710. // On line
  3711. ocpp_set_auth_req(ON, "ISO14443");
  3712. setLedMotion(gun_index,LED_ACTION_AUTHED);
  3713. }
  3714. else
  3715. {
  3716. // Off line
  3717. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3718. {
  3719. case OFF_POLICY_LOCALLIST:
  3720. ocpp_set_auth_req(ON, "ISO14443");
  3721. break;
  3722. case OFF_POLICY_PH_RFID:
  3723. break;
  3724. case OFF_POLICY_FREE:
  3725. break;
  3726. case OFF_POLICY_NOCHARGE:
  3727. default:
  3728. break;
  3729. }
  3730. }
  3731. break;
  3732. case AUTH_MODE_DISABLE:
  3733. default:
  3734. break;
  3735. }
  3736. }
  3737. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3738. if(ShmCharger->isCcsEnable)system("/root/Module_CCS &");
  3739. }
  3740. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  3741. {
  3742. // Authorization timeout process.
  3743. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3744. setChargerMode(gun_index, SYS_MODE_IDLE);
  3745. DEBUG_WARN("Authorize timeout !!!\n");
  3746. }
  3747. else
  3748. {
  3749. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  3750. {
  3751. case START_METHOD_RFID:
  3752. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  3753. sleep(1);
  3754. if((ocpp_get_auth_conf()) ||
  3755. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  3756. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0))
  3757. )
  3758. {
  3759. if(ocpp_get_auth_result(gun_index) ||
  3760. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  3761. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  3762. {
  3763. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3764. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  3765. DEBUG_INFO("Authorize pass.\n");
  3766. setSpeaker(ON,SPEAKER_SHORT);
  3767. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3768. sleep(3);
  3769. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3770. }
  3771. else
  3772. {
  3773. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  3774. DEBUG_INFO("Authorize fail.\n");
  3775. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3776. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3777. sleep(3);
  3778. setChargerMode(gun_index, SYS_MODE_IDLE);
  3779. }
  3780. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3781. ocpp_set_auth_conf(OFF);
  3782. }
  3783. break;
  3784. case START_METHOD_BACKEND:
  3785. case START_METHOD_BLE:
  3786. case START_METHOD_FREE:
  3787. default:
  3788. setSpeaker(ON,SPEAKER_SHORT);
  3789. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3790. break;
  3791. }
  3792. }
  3793. break;
  3794. case SYS_MODE_PREPARING:
  3795. if(isModeChange(gun_index))
  3796. {
  3797. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3798. setLedMotion(gun_index,LED_ACTION_AUTHED);
  3799. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  3800. if(ShmCharger->isCcsEnable)
  3801. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  3802. else
  3803. {
  3804. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  3805. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3806. setRequest(gun_index, ON);
  3807. }
  3808. }
  3809. // If control pilot detect Bx, skip watch dog time out.
  3810. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) ||
  3811. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  3812. {
  3813. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  3814. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  3815. {
  3816. case HANDSHAKE_DUTY_5:
  3817. if(!getRequest(gun_index))
  3818. {
  3819. setRequest(gun_index, ON);
  3820. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  3821. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3822. DEBUG_INFO("Set Request On.\n");
  3823. //Let CCS task start to negotiate
  3824. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = ON;
  3825. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_NONE;
  3826. }
  3827. // Set CCS 5% PWM duty
  3828. if(ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_5)
  3829. {
  3830. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  3831. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  3832. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3833. }
  3834. break;
  3835. case HANDSHAKE_DUTY_5_CHECK:
  3836. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF))
  3837. {
  3838. //2 secs timeout
  3839. ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  3840. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CCS;
  3841. }
  3842. break;
  3843. case HANDSHAKE_CCS:
  3844. //CCS handshake timeout
  3845. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  3846. {
  3847. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  3848. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  3849. DEBUG_INFO("BS/HLC %d secs handshake timeout.\n", (TIMEOUT_SPEC_BS_HLC_HANDSHAKE/1000));
  3850. }
  3851. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  3852. {
  3853. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  3854. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  3855. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3856. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  3857. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3858. }
  3859. //CCS status check
  3860. if((16 < ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 254))
  3861. {
  3862. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  3863. DEBUG_INFO("Enter HLC Mode charging.\n");
  3864. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  3865. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3866. }
  3867. break;
  3868. case HANDSHAKE_CP_STATE_E:
  3869. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3870. {
  3871. if(ShmCharger->isCcsEnable)
  3872. {
  3873. DEBUG_INFO("Change to CP STATE E for 4 secs.\n");
  3874. //CP STATE E for 4 secs
  3875. sleep(4);
  3876. }
  3877. //restore normal CP PWM duty
  3878. // Determine max charging current to MCU
  3879. DEBUG_INFO("Determine max charging current to MCU.\n");
  3880. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3881. {
  3882. 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);
  3883. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3884. {
  3885. 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);
  3886. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3887. }
  3888. }
  3889. else
  3890. {
  3891. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3892. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3893. {
  3894. 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);
  3895. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3896. }
  3897. }
  3898. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3899. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  3900. ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  3901. }
  3902. break;
  3903. case HANDSHAKE_SET_MAX_CURRENT:
  3904. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3905. {
  3906. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
  3907. DEBUG_INFO("Enter BS Mode charging.\n");
  3908. //for EV READY 30 secs didn't start charging to STATE E
  3909. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3910. }
  3911. break;
  3912. case HANDSHAKE_BS_MODE:
  3913. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  3914. setRelay(gun_index,ON);
  3915. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  3916. {
  3917. ocpp_set_unlocker_req(gun_index, OFF);
  3918. presentChargedEnergyClear(gun_index);
  3919. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  3920. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  3921. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  3922. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  3923. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  3924. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  3925. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  3926. else
  3927. {
  3928. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100.0);
  3929. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100.0);
  3930. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100.0);
  3931. }
  3932. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3933. ocpp_copy_userid_to_starttransaction(gun_index);
  3934. ocpp_set_starttransaction_req(gun_index, ON);
  3935. ftime(&startChargingTime[gun_index]);
  3936. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3937. }
  3938. //EV READY CHECK
  3939. /*
  3940. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_EV_READY) && (ShmCharger->gun_info[gun_index].isEvReady2StateE == OFF))
  3941. {
  3942. if(ShmCharger->gun_info[gun_index].isDoEvReadyOnce == OFF)
  3943. {
  3944. DEBUG_INFO("EV READY STATE E 4sec.\n");
  3945. ShmCharger->gun_info[gun_index].isEvReady2StateE = ON;
  3946. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
  3947. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3948. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_E;
  3949. }
  3950. }
  3951. if(ShmCharger->gun_info[gun_index].isEvReady2StateE == ON)
  3952. {
  3953. switch(ShmCharger->gun_info[gun_index].evReadyState)
  3954. {
  3955. case EV_READY_STAT_E:
  3956. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3957. {
  3958. sleep(4);
  3959. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3960. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3961. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_C;
  3962. }
  3963. break;
  3964. case EV_READY_STAT_C:
  3965. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3966. {
  3967. usleep(500000);
  3968. DEBUG_INFO("EV READY Determine max charging current to MCU.\n");
  3969. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3970. {
  3971. 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);
  3972. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3973. {
  3974. 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);
  3975. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3976. }
  3977. }
  3978. else
  3979. {
  3980. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3981. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3982. {
  3983. 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);
  3984. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3985. }
  3986. }
  3987. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3988. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_SET_MAX_CURRENT;
  3989. }
  3990. break;
  3991. case EV_READY_SET_MAX_CURRENT:
  3992. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3993. {
  3994. ShmCharger->gun_info[gun_index].isEvReady2StateE = OFF;
  3995. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = ON;
  3996. }
  3997. break;
  3998. default:
  3999. break;
  4000. }
  4001. }*/
  4002. break;
  4003. case HANDSHAKE_HLC_MODE:
  4004. if(ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)
  4005. {
  4006. setRelay(gun_index,ON);
  4007. ShmCharger->gun_info[gun_index].isCCSStartTransation = ON;
  4008. }
  4009. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) &&
  4010. (ShmCharger->gun_info[gun_index].isCCSStartTransation == ON))
  4011. {
  4012. ocpp_set_unlocker_req(gun_index, OFF);
  4013. presentChargedEnergyClear(gun_index);
  4014. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  4015. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  4016. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  4017. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  4018. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  4019. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  4020. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  4021. else
  4022. {
  4023. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  4024. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  4025. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  4026. }
  4027. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4028. ocpp_copy_userid_to_starttransaction(gun_index);
  4029. ocpp_set_starttransaction_req(gun_index, ON);
  4030. ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
  4031. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4032. ftime(&startChargingTime[gun_index]);
  4033. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  4034. }
  4035. //120 sec timeout
  4036. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  4037. {
  4038. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  4039. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5000)
  4040. {
  4041. DEBUG_INFO("CCS 120 secs handshake timeout.\n");
  4042. //Cancel CCS task negotiating
  4043. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4044. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4045. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4046. }
  4047. }
  4048. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100)
  4049. {
  4050. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  4051. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  4052. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  4053. setChargerMode(gun_index, SYS_MODE_IDLE);
  4054. }
  4055. break;
  4056. default:
  4057. break;
  4058. }
  4059. }
  4060. else if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode)
  4061. {
  4062. DEBUG_INFO("Enter Socket-E Mode charging.\n");
  4063. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_SOCKETE;
  4064. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == CP_STATE_C)
  4065. setRelay(gun_index,ON);
  4066. ocpp_copy_userid_to_starttransaction(gun_index);
  4067. ocpp_set_starttransaction_req(gun_index, ON);
  4068. ftime(&startChargingTime[gun_index]);
  4069. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4070. }
  4071. // Unplug charging gun to Idle mode
  4072. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  4073. {
  4074. DEBUG_INFO("Charging gun is plugged before.\n");
  4075. //setChargerMode(gun_index, SYS_MODE_IDLE);
  4076. //Cancel CCS task negotiating
  4077. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4078. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4079. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4080. }
  4081. // Use RFID card to stop handshaking
  4082. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  4083. {
  4084. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  4085. setSpeaker(ON,SPEAKER_SHORT);
  4086. //setChargerMode(gun_index, SYS_MODE_IDLE);
  4087. //Cancel CCS task negotiating
  4088. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4089. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4090. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4091. }
  4092. else
  4093. {
  4094. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4095. }
  4096. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  4097. {
  4098. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  4099. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  4100. {
  4101. DEBUG_INFO("Handshaking timeout...\n");
  4102. //Cancel CCS task negotiating
  4103. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4104. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4105. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4106. }
  4107. }
  4108. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5)
  4109. {
  4110. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  4111. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  4112. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  4113. setChargerMode(gun_index, SYS_MODE_IDLE);
  4114. }
  4115. break;
  4116. case SYS_MODE_CHARGING:
  4117. if(isModeChange(gun_index))
  4118. {
  4119. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4120. //ftime(&startChargingTime[gun_index]);
  4121. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4122. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4123. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4124. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4125. ocpp_set_auth_req(OFF);
  4126. ocpp_reset_smartcharging_profileId(gun_index);
  4127. ocpp_set_profile_req(gun_index, ON);
  4128. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4129. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4130. }
  4131. //if time up, clear CCS MSG count
  4132. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  4133. (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
  4134. {
  4135. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  4136. if(ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat > 0)
  4137. {
  4138. ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat = 0;
  4139. }
  4140. else
  4141. {
  4142. DEBUG_INFO("CCS could not get MSG from car.\n");
  4143. //setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4144. //setRelay(gun_index, OFF);
  4145. }
  4146. }
  4147. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4148. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4149. ocpp_get_remotestop(gun_index) ||
  4150. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4151. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  4152. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  4153. ocpp_get_reset_req() ||
  4154. ocpp_get_unlocker_req(gun_index) ||
  4155. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  4156. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
  4157. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
  4158. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
  4159. {
  4160. if(ShmCharger->gun_info[gun_index].rfidReq)
  4161. {
  4162. // If RFID SN different with start user, it need to authorize ID
  4163. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4164. {
  4165. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4166. {
  4167. // Request authorization
  4168. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  4169. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4170. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4171. {
  4172. // Big endian
  4173. switch(rfid.snType)
  4174. {
  4175. case RFID_SN_TYPE_6BYTE:
  4176. 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]);
  4177. break;
  4178. case RFID_SN_TYPE_7BYTE:
  4179. 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]);
  4180. break;
  4181. case RFID_SN_TYPE_10BYTE:
  4182. 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]);
  4183. break;
  4184. case RFID_SN_TYPE_4BYTE:
  4185. default:
  4186. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4187. break;
  4188. }
  4189. }
  4190. else
  4191. {
  4192. // Little endian
  4193. switch(rfid.snType)
  4194. {
  4195. case RFID_SN_TYPE_6BYTE:
  4196. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4197. break;
  4198. case RFID_SN_TYPE_7BYTE:
  4199. 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]);
  4200. break;
  4201. case RFID_SN_TYPE_10BYTE:
  4202. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[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]);
  4203. break;
  4204. case RFID_SN_TYPE_4BYTE:
  4205. default:
  4206. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4207. break;
  4208. }
  4209. }
  4210. DEBUG_INFO("End request User Id: %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4211. DEBUG_INFO("Start method: %d...\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  4212. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4213. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4214. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4215. {
  4216. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4217. {
  4218. case AUTH_MODE_ENABLE:
  4219. if(ocpp_get_connection_status())
  4220. {
  4221. // On line
  4222. ocpp_set_auth_req(ON, "ISO14443");
  4223. }
  4224. else
  4225. {
  4226. // Off line
  4227. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4228. {
  4229. case OFF_POLICY_LOCALLIST:
  4230. ocpp_set_auth_req(ON, "ISO14443");
  4231. break;
  4232. case OFF_POLICY_PH_RFID:
  4233. break;
  4234. case OFF_POLICY_FREE:
  4235. break;
  4236. case OFF_POLICY_NOCHARGE:
  4237. default:
  4238. break;
  4239. }
  4240. }
  4241. break;
  4242. case AUTH_MODE_DISABLE:
  4243. default:
  4244. break;
  4245. }
  4246. }
  4247. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4248. {
  4249. DEBUG_INFO("Should be remote stop charger... \n");
  4250. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4251. }
  4252. }
  4253. else
  4254. {
  4255. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4256. sleep(1);
  4257. if(ocpp_get_auth_conf() ||
  4258. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4259. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  4260. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4261. {
  4262. if(ocpp_get_auth_result(gun_index) ||
  4263. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4264. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4265. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4266. {
  4267. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4268. }
  4269. else
  4270. {
  4271. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4272. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4273. ocpp_set_auth_req(OFF);
  4274. DEBUG_INFO("Authorize fail.\n");
  4275. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4276. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4277. sleep(3);
  4278. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4279. }
  4280. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4281. ocpp_set_auth_conf(OFF);
  4282. }
  4283. }
  4284. }
  4285. // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
  4286. if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
  4287. {
  4288. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4289. setRelay(gun_index, OFF);
  4290. }
  4291. }
  4292. else
  4293. {
  4294. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4295. setRelay(gun_index, OFF);
  4296. }
  4297. }
  4298. else
  4299. {
  4300. // Charging session info calculation
  4301. setLedMotion(gun_index,LED_ACTION_CHARGING);
  4302. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  4303. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  4304. else
  4305. {
  4306. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  4307. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  4308. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  4309. }
  4310. ftime(&endChargingTime[gun_index]);
  4311. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  4312. presentChargedEnergyUpdate(gun_index);
  4313. // Response StartTransactionConf
  4314. ocpp_set_starttransaction_conf(gun_index, OFF);
  4315. // Charging profile preparation
  4316. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  4317. {
  4318. if(!ocpp_get_profile_conf(gun_index))
  4319. {
  4320. ocpp_set_profile_req(gun_index, ON);
  4321. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4322. }
  4323. else
  4324. {
  4325. ocpp_set_profile_conf(gun_index, OFF);
  4326. }
  4327. }
  4328. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  4329. checkChargingProfileLimit(gun_index);
  4330. // Charging session target current check if OCPP disconnect and power sharing server connected
  4331. if(!ocpp_get_connection_status() && ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  4332. {
  4333. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent;
  4334. }
  4335. // Determine max charging current to MCU
  4336. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4337. {
  4338. 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);
  4339. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  4340. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4341. {
  4342. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4343. {
  4344. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4345. {
  4346. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4347. {
  4348. 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);
  4349. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4350. }
  4351. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4352. }
  4353. }
  4354. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4355. {
  4356. 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);
  4357. }
  4358. }
  4359. }
  4360. else
  4361. {
  4362. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4363. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  4364. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4365. {
  4366. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4367. {
  4368. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4369. {
  4370. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4371. {
  4372. 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);
  4373. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4374. }
  4375. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4376. }
  4377. }
  4378. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4379. {
  4380. 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);
  4381. }
  4382. }
  4383. }
  4384. // Charging session local limit condition check
  4385. if(ocpp_get_connection_status())
  4386. {
  4387. // On-line max condition check
  4388. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  4389. {
  4390. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4391. {
  4392. setRelay(gun_index, OFF);
  4393. setRequest(gun_index, OFF);
  4394. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4395. }
  4396. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4397. {
  4398. setRelay(gun_index, OFF);
  4399. }
  4400. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4401. 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));
  4402. }
  4403. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  4404. {
  4405. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4406. {
  4407. setRelay(gun_index, OFF);
  4408. setRequest(gun_index, OFF);
  4409. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4410. }
  4411. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4412. {
  4413. setRelay(gun_index, OFF);
  4414. }
  4415. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4416. 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));
  4417. }
  4418. else
  4419. {
  4420. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4421. {
  4422. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4423. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4424. setRelay(gun_index, ON);
  4425. else
  4426. setRelay(gun_index, OFF);
  4427. }
  4428. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4429. {
  4430. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  4431. setRelay(gun_index, ON);
  4432. }
  4433. }
  4434. }
  4435. else
  4436. {
  4437. // Off-line max condition check
  4438. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4439. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  4440. {
  4441. 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))))
  4442. {
  4443. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4444. {
  4445. setRelay(gun_index, OFF);
  4446. setRequest(gun_index, OFF);
  4447. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4448. }
  4449. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4450. {
  4451. setRelay(gun_index, OFF);
  4452. }
  4453. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4454. 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));
  4455. }
  4456. 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))))
  4457. {
  4458. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4459. {
  4460. setRelay(gun_index, OFF);
  4461. setRequest(gun_index, OFF);
  4462. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4463. }
  4464. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4465. {
  4466. setRelay(gun_index, OFF);
  4467. }
  4468. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4469. 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));
  4470. }
  4471. else
  4472. {
  4473. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4474. {
  4475. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4476. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4477. setRelay(gun_index, ON);
  4478. else
  4479. setRelay(gun_index, OFF);
  4480. }
  4481. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4482. {
  4483. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  4484. setRelay(gun_index, ON);
  4485. }
  4486. }
  4487. }
  4488. else
  4489. {
  4490. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4491. {
  4492. setRequest(gun_index, OFF);
  4493. }
  4494. setRelay(gun_index, OFF);
  4495. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  4496. }
  4497. }
  4498. // Debug information
  4499. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  4500. {
  4501. DEBUG_INFO("===============================================================\n");
  4502. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4503. 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);
  4504. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4505. DEBUG_INFO("===============================================================\n");
  4506. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4507. }
  4508. }
  4509. break;
  4510. case SYS_MODE_TERMINATING:
  4511. if(isModeChange(gun_index))
  4512. {
  4513. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4514. {
  4515. setLedMotion(gun_index, LED_ACTION_STOP);
  4516. }
  4517. else
  4518. {}
  4519. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  4520. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4521. }
  4522. ftime(&endChargingTime[gun_index]);
  4523. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  4524. {
  4525. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  4526. }
  4527. // End authorize pass
  4528. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4529. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  4530. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4531. ocpp_get_remotestop(gun_index) ||
  4532. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4533. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  4534. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  4535. ocpp_get_reset_req() ||
  4536. ocpp_get_unlocker_req(gun_index) ||
  4537. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  4538. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  4539. {
  4540. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  4541. {
  4542. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4543. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  4544. {
  4545. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4546. DEBUG_INFO("Authorize pass.\n");
  4547. setSpeaker(ON,SPEAKER_SHORT);
  4548. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4549. sleep(3);
  4550. }
  4551. }
  4552. else
  4553. {}
  4554. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4555. setRequest(gun_index, OFF);
  4556. setRelay(gun_index, OFF);
  4557. setLedMotion(gun_index, LED_ACTION_STOP);
  4558. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4559. {
  4560. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4561. {
  4562. checkStopReason(gun_index);
  4563. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4564. }
  4565. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4566. {
  4567. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4568. //Cancel CCS task negotiating
  4569. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4570. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4571. setRelay(gun_index,OFF);
  4572. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4573. }
  4574. }
  4575. }
  4576. else
  4577. {
  4578. // If the charger stops charging, the led should be stoped blink
  4579. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4580. {
  4581. setLedMotion(gun_index, LED_ACTION_STOP);
  4582. }
  4583. if(ShmCharger->gun_info[gun_index].rfidReq)
  4584. {
  4585. DEBUG_INFO("Certified in terminating mode... \n");
  4586. // If RFID SN different with start user, it need to authorize ID
  4587. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4588. {
  4589. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4590. {
  4591. // Request authorization
  4592. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  4593. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4594. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4595. {
  4596. // Big endian
  4597. switch(rfid.snType)
  4598. {
  4599. case RFID_SN_TYPE_6BYTE:
  4600. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  4601. break;
  4602. case RFID_SN_TYPE_7BYTE:
  4603. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  4604. break;
  4605. case RFID_SN_TYPE_10BYTE:
  4606. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  4607. break;
  4608. case RFID_SN_TYPE_4BYTE:
  4609. default:
  4610. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4611. break;
  4612. }
  4613. }
  4614. else
  4615. {
  4616. // Little endian
  4617. switch(rfid.snType)
  4618. {
  4619. case RFID_SN_TYPE_6BYTE:
  4620. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4621. break;
  4622. case RFID_SN_TYPE_7BYTE:
  4623. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4624. break;
  4625. case RFID_SN_TYPE_10BYTE:
  4626. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4627. break;
  4628. case RFID_SN_TYPE_4BYTE:
  4629. default:
  4630. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4631. break;
  4632. }
  4633. }
  4634. DEBUG_INFO("End request User Id : %s... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4635. DEBUG_INFO("Start method : %d... \n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  4636. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4637. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4638. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4639. {
  4640. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4641. {
  4642. case AUTH_MODE_ENABLE:
  4643. if(ocpp_get_connection_status())
  4644. {
  4645. // On line
  4646. ocpp_set_auth_req(ON, "ISO14443");
  4647. }
  4648. else
  4649. {
  4650. // Off line
  4651. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4652. {
  4653. case OFF_POLICY_LOCALLIST:
  4654. ocpp_set_auth_req(ON, "ISO14443");
  4655. break;
  4656. case OFF_POLICY_PH_RFID:
  4657. break;
  4658. case OFF_POLICY_FREE:
  4659. break;
  4660. case OFF_POLICY_NOCHARGE:
  4661. default:
  4662. break;
  4663. }
  4664. }
  4665. break;
  4666. case AUTH_MODE_DISABLE:
  4667. default:
  4668. break;
  4669. }
  4670. }
  4671. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4672. {
  4673. DEBUG_INFO("Should be remote stop charger... \n");
  4674. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4675. }
  4676. }
  4677. else
  4678. {
  4679. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4680. sleep(1);
  4681. if(ocpp_get_auth_conf() ||
  4682. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4683. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4684. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4685. {
  4686. if(ocpp_get_auth_result(gun_index) ||
  4687. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4688. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  4689. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4690. {
  4691. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4692. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4693. }
  4694. else
  4695. {
  4696. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4697. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4698. ocpp_set_auth_req(OFF);
  4699. DEBUG_INFO("Authorize fail... \n");
  4700. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4701. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4702. sleep(3);
  4703. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4704. }
  4705. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4706. ocpp_set_auth_conf(OFF);
  4707. }
  4708. }
  4709. }
  4710. }
  4711. 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)) &&
  4712. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  4713. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  4714. !ocpp_get_remotestop(gun_index) &&
  4715. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  4716. !ocpp_get_reset_req() &&
  4717. ocpp_get_starttransaction_result(gun_index) &&
  4718. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  4719. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  4720. !(!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)))) &&
  4721. !(!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)))) &&
  4722. (((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))
  4723. )
  4724. {
  4725. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4726. }
  4727. }
  4728. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  4729. (ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) &&
  4730. (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  4731. {
  4732. DEBUG_INFO("Set PWM duty 100% go to SYS_MODE_TERMINATING.\n");
  4733. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  4734. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  4735. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  4736. checkStopReason(gun_index);
  4737. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4738. }
  4739. break;
  4740. case SYS_MODE_COMPLETE:
  4741. if(isModeChange(gun_index))
  4742. {
  4743. setLedMotion(gun_index, LED_ACTION_STOP);
  4744. setRelay(gun_index, OFF);
  4745. setRequest(gun_index, OFF);
  4746. sleep(15);
  4747. }
  4748. if(((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)) ||
  4749. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && (!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)))
  4750. {
  4751. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4752. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  4753. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4754. ocpp_set_remotestop(gun_index, OFF);
  4755. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  4756. DB_Insert_Record(localDb, gun_index);
  4757. setChargerMode(gun_index, SYS_MODE_IDLE);
  4758. }
  4759. break;
  4760. case SYS_MODE_ALARM:
  4761. setLedMotion(gun_index,LED_ACTION_ALARM);
  4762. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4763. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4764. {}
  4765. else
  4766. {
  4767. setRelay(gun_index, OFF);
  4768. setRequest(gun_index, OFF);
  4769. }
  4770. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  4771. {
  4772. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4773. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4774. {
  4775. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  4776. }
  4777. else
  4778. {
  4779. setChargerMode(gun_index, SYS_MODE_IDLE);
  4780. }
  4781. }
  4782. break;
  4783. case SYS_MODE_FAULT:
  4784. if(isModeChange(gun_index))
  4785. {}
  4786. break;
  4787. case SYS_MODE_MAINTAIN:
  4788. if(isModeChange(gun_index))
  4789. {
  4790. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4791. }
  4792. break;
  4793. case SYS_MODE_UPDATE:
  4794. if(isModeChange(gun_index))
  4795. {
  4796. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4797. }
  4798. //======================================
  4799. // Check local upgrade firmware request
  4800. //======================================
  4801. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  4802. {
  4803. DEBUG_INFO("Firmware local upgrading...\n");
  4804. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  4805. int value = upgrade_check();
  4806. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  4807. if(value == PASS)
  4808. {
  4809. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4810. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4811. ShmCharger->isUpdateSuccess = YES;
  4812. DEBUG_INFO("Local update success...\n");
  4813. }
  4814. else
  4815. {
  4816. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4817. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4818. ShmCharger->isUpdateSuccess = NO;
  4819. DEBUG_INFO("Local update unsuccess...\n");
  4820. }
  4821. }
  4822. else if(ocpp_get_update_firmware_req())
  4823. {
  4824. //======================================
  4825. // Check remote upgrade firmware request
  4826. //======================================
  4827. checkRemoteUpgradeStatus();
  4828. }
  4829. else
  4830. {
  4831. //======================================
  4832. // Upgrade complete reboot system
  4833. //======================================
  4834. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  4835. {
  4836. if(ShmCharger->isUpdateSuccess == YES)
  4837. {
  4838. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4839. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4840. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  4841. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4842. DEBUG_WARN("Firmware upgrade success.\n");
  4843. sleep(5);
  4844. DEBUG_INFO("Firmware upgraded, reboot...\n");
  4845. system("reboot -f");
  4846. sleep(5);
  4847. system("reboot -f");
  4848. }
  4849. else
  4850. {
  4851. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  4852. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4853. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  4854. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4855. DEBUG_WARN("Firmware upgrade fail.\n");
  4856. close(wtdFd);
  4857. system("/usr/bin/run_evse_restart.sh");
  4858. }
  4859. }
  4860. }
  4861. break;
  4862. case SYS_MODE_RESERVATION:
  4863. if(isModeChange(gun_index))
  4864. {
  4865. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4866. }
  4867. if(isReservationExpired(gun_index))
  4868. {
  4869. DEBUG_INFO("Reservation: Time's up...\n");
  4870. setChargerMode(gun_index, SYS_MODE_IDLE);
  4871. }
  4872. else if(ocpp_get_cancelreservation_req(gun_index))
  4873. {
  4874. DEBUG_INFO("Reservation: Cancel reservation...\n");
  4875. setChargerMode(gun_index, SYS_MODE_IDLE);
  4876. ocpp_set_cancelreservation_req(gun_index, OFF);
  4877. }
  4878. else
  4879. {
  4880. // Check is there RFID or back end request start
  4881. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4882. ocpp_get_remotestart(gun_index))
  4883. {
  4884. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4885. {
  4886. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4887. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4888. {
  4889. // Big endian
  4890. switch(rfid.snType)
  4891. {
  4892. case RFID_SN_TYPE_6BYTE:
  4893. 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]);
  4894. break;
  4895. case RFID_SN_TYPE_7BYTE:
  4896. 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]);
  4897. break;
  4898. case RFID_SN_TYPE_10BYTE:
  4899. 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]);
  4900. break;
  4901. case RFID_SN_TYPE_4BYTE:
  4902. default:
  4903. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4904. break;
  4905. }
  4906. }
  4907. else
  4908. {
  4909. // Little endian
  4910. switch(rfid.snType)
  4911. {
  4912. case RFID_SN_TYPE_6BYTE:
  4913. 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]);
  4914. break;
  4915. case RFID_SN_TYPE_7BYTE:
  4916. 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]);
  4917. break;
  4918. case RFID_SN_TYPE_10BYTE:
  4919. 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]);
  4920. break;
  4921. case RFID_SN_TYPE_4BYTE:
  4922. default:
  4923. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4924. break;
  4925. }
  4926. }
  4927. if(ocpp_compare_reserve_id_with_user(gun_index))
  4928. {
  4929. DEBUG_INFO("Start Method in reservation : RFID...\n");
  4930. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4931. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4932. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4933. }
  4934. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4935. }
  4936. else if(ocpp_get_remotestart(gun_index))
  4937. {
  4938. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  4939. {
  4940. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  4941. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4942. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4943. }
  4944. ocpp_set_remotestop(gun_index, OFF);
  4945. }
  4946. }
  4947. }
  4948. break;
  4949. case SYS_MODE_BOOKING:
  4950. if(isModeChange(gun_index))
  4951. {}
  4952. break;
  4953. case SYS_MODE_DEBUG:
  4954. if(isModeChange(gun_index))
  4955. {
  4956. setLedMotion(gun_index,LED_ACTION_DEBUG);
  4957. }
  4958. break;
  4959. }
  4960. }
  4961. // System watch dog reset
  4962. write(wtdFd, "a", 1);
  4963. usleep(50000);
  4964. }
  4965. return FAIL;
  4966. }