main.c 195 KB

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