main.c 179 KB

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