main.c 152 KB

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