main.c 179 KB

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