main.c 179 KB

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