main.c 186 KB

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