main.c 164 KB

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