main.c 166 KB

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