main.c 133 KB

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