main.c 204 KB

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