main.c 164 KB

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