main.c 145 KB

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