main.c 162 KB

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