main.c 177 KB

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