main.c 166 KB

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