main.c 156 KB

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