main.c 188 KB

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