main.c 158 KB

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