main.c 179 KB

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