main.c 153 KB

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