main.c 153 KB

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