main.c 163 KB

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