main.c 186 KB

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