main.c 179 KB

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