main.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092
  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_3 3
  21. #define TMR_IDX_4 4
  22. #define TMR_IDX_5 5
  23. #define TMR_IDX_6 6
  24. #define TMR_IDX_7 7
  25. #define TMR_IDX_8 8
  26. #define TMR_IDX_9 9
  27. #define TIMEOUT_SPEC_HANDSHAKING 180000
  28. #define TIMEOUT_SPEC_AUTH 15000
  29. #define TIMEOUT_SPEC_HANDSHAKING_LED 185000
  30. #define TIMEOUT_SPEC_LOGPPRINTOUT 30000
  31. #define MtdBlockSize 0x600000
  32. //==========================
  33. // Declare method
  34. //==========================
  35. void trim(char *s);
  36. int mystrcmp(char *p1,char *p2);
  37. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  38. void split(char **arr, char *str, const char *del);
  39. int isReachableInternet();
  40. int InitRfidPort(void);
  41. int GetCardSerialNumber();
  42. void setLedMotion(unsigned char gun_index,unsigned char led_mode);
  43. void setRelay(unsigned char gun_index,unsigned char isOn);
  44. void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
  45. //==========================
  46. // Declare RFID module type
  47. //==========================
  48. #define MODULE_EWT 0
  49. int wtdFd = -1;
  50. int rfidFd = -1;
  51. char* rfidPortName = "/dev/ttyS2";
  52. RFID rfid;
  53. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  54. //==========================
  55. // Declare share memory
  56. //==========================
  57. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  58. struct StatusCodeData *ShmStatusCodeData;
  59. struct PsuData *ShmPsuData;
  60. struct CHAdeMOData *ShmCHAdeMOData;
  61. struct CcsData *ShmCcsData;
  62. struct PrimaryMcuData *ShmPrimaryMcuData;
  63. struct FanModuleData *ShmFanModuleData;
  64. struct RelayModuleData *ShmRelayModuleData;
  65. struct OCPP16Data *ShmOCPP16Data;
  66. struct Charger *ShmCharger;
  67. struct timeb startTime[AC_QUANTITY][10];
  68. struct timeb startChargingTime[AC_QUANTITY];
  69. struct timeb endChargingTime[AC_QUANTITY];
  70. sqlite3 *localDb;
  71. struct SysConfigData SysConfigOrg;
  72. //=================================
  73. // Common routine
  74. //=================================
  75. void trim(char *s)
  76. {
  77. int i=0, j, k, l=0;
  78. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  79. i++;
  80. j = strlen(s)-1;
  81. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  82. j--;
  83. if(i==0 && j==strlen(s)-1) { }
  84. else if(i==0) s[j+1] = '\0';
  85. else {
  86. for(k=i; k<=j; k++) s[l++] = s[k];
  87. s[l] = '\0';
  88. }
  89. }
  90. int mystrcmp(char *p1,char *p2)
  91. {
  92. while(*p1==*p2)
  93. {
  94. if(*p1=='\0' || *p2=='\0')
  95. break;
  96. p1++;
  97. p2++;
  98. }
  99. if(*p1=='\0' && *p2=='\0')
  100. return(PASS);
  101. else
  102. return(FAIL);
  103. }
  104. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  105. {
  106. strncpy(dest, src + start, cnt);
  107. dest[cnt] = 0;
  108. }
  109. void split(char **arr, char *str, const char *del)
  110. {
  111. char *s = strtok(str, del);
  112. while(s != NULL)
  113. {
  114. *arr++ = s;
  115. s = strtok(NULL, del);
  116. }
  117. }
  118. int StoreLogMsg(const char *fmt, ...)
  119. {
  120. char Buf[4096+256];
  121. char buffer[4096];
  122. time_t CurrentTime;
  123. struct tm *tm;
  124. va_list args;
  125. va_start(args, fmt);
  126. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  127. va_end(args);
  128. memset(Buf,0,sizeof(Buf));
  129. CurrentTime = time(NULL);
  130. tm=localtime(&CurrentTime);
  131. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  132. {
  133. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  134. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  135. buffer,
  136. tm->tm_year+1900,tm->tm_mon+1,
  137. ShmSysConfigAndInfo->SysConfig.ModelName,
  138. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  139. }
  140. else
  141. {
  142. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  143. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  144. buffer,
  145. tm->tm_year+1900,tm->tm_mon+1);
  146. }
  147. #ifdef SystemLogMessage
  148. system(Buf);
  149. #endif
  150. #ifdef ConsloePrintLog
  151. 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);
  152. #endif
  153. return rc;
  154. }
  155. int DiffTimebWithNow(struct timeb ST)
  156. {
  157. //return milli-second
  158. struct timeb ET;
  159. unsigned int StartTime,StopTime;
  160. ftime(&ET);
  161. StartTime=(unsigned int)ST.time;
  162. StopTime=(unsigned int)ET.time;
  163. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  164. }
  165. int DiffTimeb(struct timeb ST, struct timeb ET)
  166. {
  167. //return milli-second
  168. unsigned int StartTime,StopTime;
  169. StartTime=(unsigned int)ST.time;
  170. StopTime=(unsigned int)ET.time;
  171. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  172. }
  173. void getDateTimeString(char* result)
  174. {
  175. time_t CurrentTime;
  176. struct tm *tm;
  177. CurrentTime = time(NULL);
  178. tm=localtime(&CurrentTime);
  179. 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);
  180. }
  181. //======================================================
  182. // Check interface status
  183. //======================================================
  184. int isInterfaceUp(const char *interface)
  185. {
  186. int result = FAIL;
  187. FILE *fp;
  188. char cmd[256];
  189. char buf[512];
  190. strcpy(cmd, "ifconfig");;
  191. fp = popen(cmd, "r");
  192. if(fp != NULL)
  193. {
  194. while(fgets(buf, sizeof(buf), fp) != NULL)
  195. {
  196. if(strstr(buf, interface) > 0)
  197. {
  198. result = PASS;
  199. }
  200. }
  201. }
  202. pclose(fp);
  203. return result;
  204. }
  205. //======================================================
  206. // Create all share memory
  207. //======================================================
  208. int CreatShareMemory()
  209. {
  210. int result = PASS;
  211. int MeterSMId;
  212. //creat ShmSysConfigAndInfo
  213. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  214. {
  215. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\r\n");
  216. result = FAIL;
  217. }
  218. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  219. {
  220. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\r\n");
  221. result = FAIL;
  222. }
  223. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  224. //creat ShmStatusCodeData
  225. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  226. {
  227. DEBUG_ERROR("shmget ShmStatusCodeData NG\r\n");
  228. result = FAIL;
  229. }
  230. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  231. {
  232. DEBUG_ERROR("shmat ShmStatusCodeData NG\r\n");
  233. result = FAIL;
  234. }
  235. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  236. //creat ShmPsuData
  237. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  238. {
  239. DEBUG_ERROR("shmget ShmPsuData NG\r\n");
  240. result = FAIL;
  241. }
  242. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  243. {
  244. DEBUG_ERROR("shmat ShmPsuData NG\r\n");
  245. result = FAIL;
  246. }
  247. memset(ShmPsuData,0,sizeof(struct PsuData));
  248. //creat ShmCHAdeMOData
  249. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  250. {
  251. DEBUG_ERROR("shmget ShmCHAdeMOData NG1\r\n");
  252. result = FAIL;
  253. }
  254. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  255. {
  256. DEBUG_ERROR("shmat ShmCHAdeMOData NG2\r\n");
  257. result = FAIL;
  258. }
  259. memset(ShmCHAdeMOData,0,sizeof(struct CHAdeMOData));
  260. //creat ShmCcsData
  261. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  262. {
  263. DEBUG_ERROR("shmget ShmCcsData NG\r\n");
  264. result = FAIL;
  265. }
  266. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  267. {
  268. DEBUG_ERROR("shmat ShmCcsData NG\r\n");
  269. result = FAIL;
  270. }
  271. memset(ShmCcsData,0,sizeof(struct CcsData));
  272. //creat ShmPrimaryMcuData
  273. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  274. {
  275. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\r\n");
  276. result = FAIL;
  277. }
  278. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  279. {
  280. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\r\n");
  281. result = FAIL;
  282. }
  283. memset(ShmPrimaryMcuData,0,sizeof(struct PrimaryMcuData));
  284. /*
  285. //creat ShmFanModuleData
  286. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  287. {
  288. DEBUG_ERROR("shmget ShmFanModuleData NG\r\n");
  289. result = FAIL;
  290. }
  291. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  292. {
  293. DEBUG_ERROR("shmat ShmFanModuleData NG\r\n");
  294. result = FAIL;
  295. }
  296. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  297. //creat ShmRelayModuleData
  298. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  299. {
  300. DEBUG_ERROR("shmget ShmRelayModuleData NG\r\n");
  301. result = FAIL;
  302. }
  303. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  304. {
  305. DEBUG_ERROR("shmat ShmRelayModuleData NG\r\n");
  306. result = FAIL;
  307. }
  308. memset(ShmRelayModuleData,0,sizeof(struct RelayModuleData));*/
  309. //creat ShmOCPP16Data
  310. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  311. {
  312. DEBUG_ERROR("shmget ShmOCPP16Data NG\r\n");
  313. result = FAIL;
  314. }
  315. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  316. {
  317. DEBUG_ERROR("shmat ShmOCPP16Data NG\r\n");
  318. result = FAIL;
  319. }
  320. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  321. //creat ShmCharger
  322. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  323. {
  324. DEBUG_ERROR("shmget ShmCharger NG\r\n");
  325. result = FAIL;
  326. }
  327. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  328. {
  329. DEBUG_ERROR("shmat ShmCharger NG\r\n");
  330. result = FAIL;
  331. }
  332. memset(ShmCharger,0,sizeof(struct Charger));
  333. return result;
  334. }
  335. //===============================================
  336. // SQLite3 related routine
  337. //===============================================
  338. int DB_Open(sqlite3 *db)
  339. {
  340. int result = PASS;
  341. char* errMsg = NULL;
  342. char* createSql="CREATE TABLE IF NOT EXISTS charging_record("
  343. "idx integer primary key AUTOINCREMENT, "
  344. "reservationId text, "
  345. "transactionId text, "
  346. "startMethod text, "
  347. "userId text, "
  348. "dateTimeStart text, "
  349. "dateTimeStop text,"
  350. "socStart text, "
  351. "socStop text, "
  352. "chargeEnergy text, "
  353. "stopReason text"
  354. ");";
  355. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  356. if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
  357. {
  358. result = FAIL;
  359. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  360. sqlite3_close(db);
  361. }
  362. else
  363. {
  364. DEBUG_INFO( "Local charging record database open successfully.\r\n");
  365. if (sqlite3_exec(db, createSql, 0, 0, &errMsg) != SQLITE_OK)
  366. {
  367. result = FAIL;
  368. DEBUG_INFO( "Create local charging record table error message: %s\n", errMsg);
  369. }
  370. else
  371. {
  372. DEBUG_INFO( "Opened local charging record table successfully\n");
  373. }
  374. sqlite3_close(db);
  375. }
  376. return result;
  377. }
  378. int DB_Insert_Record(sqlite3 *db, int gun_index)
  379. {
  380. int result = PASS;
  381. char* errMsg = NULL;
  382. char insertSql[1024];
  383. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  384. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  385. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  386. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  387. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  388. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  389. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  390. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  391. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  392. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  393. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  394. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  395. if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
  396. {
  397. result = FAIL;
  398. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  399. sqlite3_close(db);
  400. }
  401. else
  402. {
  403. DEBUG_INFO( "Local charging record database open successfully.\r\n");
  404. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  405. {
  406. result = FAIL;
  407. DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
  408. }
  409. else
  410. {
  411. DEBUG_INFO( "Insert local charging record successfully\n");
  412. }
  413. sqlite3_close(db);
  414. }
  415. return result;
  416. }
  417. //======================================================
  418. // Peripheral initial
  419. //======================================================
  420. int InitWatchDog()
  421. {
  422. int fd;
  423. int timeout = 180;
  424. system("/usr/bin/fuser -k /dev/watchdog");
  425. sleep(1);
  426. system("echo V > /dev/watchdog");
  427. sleep(1);
  428. fd=open("/dev/watchdog", O_RDWR);
  429. if(fd<=0)
  430. {
  431. DEBUG_ERROR("System watch dog initial fail.\r\n");
  432. }
  433. ioctl(fd, _IOWR('W', 6, int), &timeout);
  434. return fd;
  435. }
  436. void InitGPIO()
  437. {
  438. /*****************0~3, 4 bank, bank x 32+ num*********************/
  439. /***************************************************************/
  440. /*************** INPUT PIN ***************************************/
  441. /***************************************************************/
  442. /***************************************************************/
  443. /*************** OUTPUT PIN ************************************/
  444. /***************************************************************/
  445. /*MCU request:GPIO3_20 => H:ON; L:OFF*/
  446. system("echo 116 > /sys/class/gpio/export");
  447. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  448. system("echo 0 > /sys/class/gpio/gpio116/value");
  449. /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
  450. system("echo 19 > /sys/class/gpio/export");
  451. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  452. system("echo 1 > /sys/class/gpio/gpio19/value");
  453. /*Speaker:GPIO2_1 => H:ON; L:OFF*/
  454. system("echo 65 > /sys/class/gpio/export");
  455. system("echo \"out\" > /sys/class/gpio/gpio65/direction");
  456. system("echo 0 > /sys/class/gpio/gpio65/value");
  457. /*4G/Wifi RST:GPIO3_14 => H:ON; L:OFF*/
  458. system("echo 110 > /sys/class/gpio/export");
  459. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  460. system("echo 1 > /sys/class/gpio/gpio110/value");
  461. sleep(3);
  462. system("echo 0 > /sys/class/gpio/gpio110/value");
  463. DEBUG_INFO("Initial GPIO OK\r\n");
  464. }
  465. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  466. {
  467. int fd,wrd;
  468. unsigned char *buf;
  469. unsigned int ChkSum,ChkSumOrg;
  470. if((buf=malloc(MtdBlockSize))==NULL)
  471. {
  472. DEBUG_ERROR("malloc buffer NG,rebooting..\r\n");
  473. if(ShmStatusCodeData!=NULL)
  474. {
  475. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  476. }
  477. sleep(5);
  478. system("reboot -f");
  479. sleep(5);
  480. system("reboot -f");
  481. }
  482. memset(buf, 0, MtdBlockSize);
  483. //================================================
  484. // Load configuration from mtdblock10
  485. //================================================
  486. fd = open("/dev/mtdblock10", O_RDWR);
  487. if (fd < 0)
  488. {
  489. free(buf);
  490. DEBUG_ERROR("open mtdblock10 NG,rebooting..\r\n");
  491. if(ShmStatusCodeData!=NULL)
  492. {
  493. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  494. }
  495. sleep(5);
  496. system("reboot -f");
  497. sleep(5);
  498. system("reboot -f");
  499. }
  500. wrd=read(fd, buf, MtdBlockSize);
  501. close(fd);
  502. if(wrd<MtdBlockSize)
  503. {
  504. free(buf);
  505. DEBUG_ERROR("read SysConfigData data NG,rebooting..\r\n");
  506. if(ShmStatusCodeData!=NULL)
  507. {
  508. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  509. }
  510. sleep(5);
  511. system("reboot -f");
  512. sleep(5);
  513. system("reboot -f");
  514. }
  515. ChkSum=0;
  516. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  517. {
  518. ChkSum+=buf[wrd];
  519. }
  520. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  521. //================================================
  522. // Load configuration from mtdblock11
  523. //================================================
  524. if(ChkSum!=ChkSumOrg)
  525. {
  526. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\r\n");
  527. fd = open("/dev/mtdblock11", O_RDWR);
  528. if (fd < 0)
  529. {
  530. free(buf);
  531. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\r\n");
  532. if(ShmStatusCodeData!=NULL)
  533. {
  534. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  535. }
  536. sleep(5);
  537. system("reboot -f");
  538. sleep(5);
  539. system("reboot -f");
  540. }
  541. memset(buf, 0, MtdBlockSize);
  542. wrd=read(fd, buf,MtdBlockSize);
  543. close(fd);
  544. if(wrd<MtdBlockSize)
  545. {
  546. free(buf);
  547. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\r\n");
  548. if(ShmStatusCodeData!=NULL)
  549. {
  550. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  551. }
  552. sleep(5);
  553. system("reboot -f");
  554. sleep(5);
  555. system("reboot -f");
  556. }
  557. ChkSum=0;
  558. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  559. {
  560. ChkSum+=buf[wrd];
  561. }
  562. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  563. //================================================
  564. // Load configuration from mtdblock12 (Factory default)
  565. //================================================
  566. if(ChkSum!=ChkSumOrg)
  567. {
  568. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\r\n");
  569. fd = open("/dev/mtdblock12", O_RDWR);
  570. if (fd < 0)
  571. {
  572. free(buf);
  573. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  574. if(ShmStatusCodeData!=NULL)
  575. {
  576. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  577. }
  578. sleep(5);
  579. system("reboot -f");
  580. sleep(5);
  581. system("reboot -f");
  582. }
  583. memset(buf, 0, MtdBlockSize);
  584. wrd=read(fd, buf,MtdBlockSize);
  585. close(fd);
  586. if(wrd<MtdBlockSize)
  587. {
  588. free(buf);
  589. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\r\n");
  590. if(ShmStatusCodeData!=NULL)
  591. {
  592. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  593. }
  594. sleep(5);
  595. system("reboot -f");
  596. sleep(5);
  597. system("reboot -f");
  598. }
  599. ChkSum=0;
  600. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  601. {
  602. ChkSum+=buf[wrd];
  603. }
  604. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  605. if(ChkSum!=ChkSumOrg)
  606. {
  607. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\r\n");
  608. free(buf);
  609. system("cd /root;./Module_FactoryConfig -m");
  610. system("sync");
  611. sleep(5);
  612. system("reboot -f");
  613. sleep(5);
  614. system("reboot -f");
  615. return FAIL;
  616. }
  617. }
  618. }
  619. //load OK
  620. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  621. free(buf);
  622. DEBUG_INFO("Load SysConfigData OK\r\n");
  623. return PASS;
  624. }
  625. int StoreUsrConfigData(struct SysConfigData *UsrData)
  626. {
  627. int result = PASS;
  628. int fd,wrd;
  629. unsigned int i,Chk;
  630. unsigned char *ptr, *BufTmp;
  631. Chk=0;
  632. ptr=(unsigned char *)UsrData;
  633. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  634. {
  635. memset(BufTmp,0,MtdBlockSize);
  636. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  637. for(i=0;i<MtdBlockSize-4;i++)
  638. Chk+=*(BufTmp+i);
  639. memcpy( BufTmp+MtdBlockSize-4,&Chk,4);
  640. fd = open("/dev/mtdblock10", O_RDWR);
  641. if (fd>0)
  642. {
  643. wrd=write(fd, BufTmp, MtdBlockSize);
  644. close(fd);
  645. if(wrd>=MtdBlockSize)
  646. {
  647. fd = open("/dev/mtdblock11", O_RDWR);
  648. if (fd>0)
  649. {
  650. wrd=write(fd, BufTmp, MtdBlockSize);
  651. close(fd);
  652. if(wrd<MtdBlockSize)
  653. {
  654. DEBUG_ERROR("write /dev/mtdblock11(backup) NG\r\n");
  655. result = FAIL;
  656. }
  657. }
  658. else
  659. {
  660. DEBUG_ERROR("open /dev/mtdblock11(backup) NG\r\n");
  661. result = FAIL;
  662. }
  663. }
  664. else
  665. {
  666. DEBUG_ERROR("write /dev/mtdblock10 NG\r\n");
  667. result = FAIL;
  668. }
  669. }
  670. else
  671. {
  672. DEBUG_ERROR("open /dev/mtdblock10 NG\r\n");
  673. result = FAIL;
  674. }
  675. }
  676. else
  677. {
  678. DEBUG_ERROR("alloc BlockSize NG\r\n");
  679. result = FAIL;
  680. }
  681. if(BufTmp!=NULL)
  682. free(BufTmp);
  683. return result;
  684. }
  685. void InitEthernet()
  686. {
  687. pid_t pid;
  688. uint8_t cnt_pingDNS_Fail;
  689. char tmpbuf[256];
  690. //unsigned int address;
  691. //Init Eth0 for internet
  692. if(isInterfaceUp("eth0")==PASS)
  693. {
  694. memset(tmpbuf,0,256);
  695. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  696. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  697. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  698. system(tmpbuf);
  699. memset(tmpbuf,0,256);
  700. sprintf(tmpbuf,"route add default gw %s eth0 ",
  701. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  702. system(tmpbuf);
  703. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up");
  704. system("ifconfig lo up");
  705. }
  706. if(isInterfaceUp("eth1")==PASS)
  707. {
  708. //Init Eth1 for administrator tool
  709. memset(tmpbuf,0,256);
  710. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  711. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  712. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  713. system(tmpbuf);
  714. }
  715. //Run DHCP client if enabled
  716. system("killall udhcpc");
  717. system("rm -rf /etc/resolv.conf");
  718. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  719. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  720. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  721. system("/sbin/udhcpc -i eth0 -s /root/dhcp_script/eth0.script > /dev/null &");
  722. // Upgrade system id to /etc/hostname
  723. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  724. system(tmpbuf);
  725. //check internet status
  726. pid = fork();
  727. if(pid == 0)
  728. {
  729. for(;;)
  730. {
  731. if(isReachableInternet() == PASS)
  732. {
  733. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  734. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  735. cnt_pingDNS_Fail = 0;
  736. }
  737. else
  738. {
  739. if(++cnt_pingDNS_Fail > 3)
  740. {
  741. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  742. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  743. }
  744. }
  745. sleep(5);
  746. }
  747. }
  748. DEBUG_INFO("Initial Ethernet OK\r\n");
  749. }
  750. int SpawnTask()
  751. {
  752. system ("pkill Module_4g");
  753. system ("pkill Module_Wifi");
  754. system ("pkill Module_EventLogging");
  755. system ("pkill OcppBackend");
  756. system ("pkill Module_AlarmDetect");
  757. system ("pkill Module_InternalComm");
  758. system ("pkill Module_Speaker");
  759. system ("pkill Module_ProduceUtils");
  760. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  761. {
  762. system("/root/Module_4g &");
  763. }
  764. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  765. {
  766. system("/root/Module_Wifi &");
  767. }
  768. system("/root/Module_EventLogging &");
  769. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  770. {
  771. system("/root/OcppBackend &");
  772. }
  773. else
  774. {
  775. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\r\n");
  776. system("/root/OcppBackend &");
  777. }
  778. system ("/root/Module_AlarmDetect &");
  779. system ("/root/Module_InternalComm &");
  780. system ("/root/Module_Speaker &");
  781. system ("/root/Module_ProduceUtils &");
  782. return PASS;
  783. }
  784. int Initialization()
  785. {
  786. int result = PASS;
  787. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  788. InitGPIO();
  789. InitEthernet();
  790. if(DB_Open(localDb) != PASS)
  791. result = FAIL;
  792. rfidFd = InitRfidPort();
  793. wtdFd = InitWatchDog();
  794. if((wtdFd < 0) || (rfidFd < 0))
  795. result = FAIL;
  796. if(result == PASS)
  797. DEBUG_INFO("Initialization OK.\r\n");
  798. else
  799. DEBUG_INFO("Initialization Fail.\r\n");
  800. return result;
  801. }
  802. //=====================================================
  803. // Common routine
  804. //=====================================================
  805. char* getSystemModeName(unsigned char mode)
  806. {
  807. char* result;
  808. switch(mode)
  809. {
  810. case SYS_MODE_BOOTING:
  811. result = "booting";
  812. break;
  813. case SYS_MODE_IDLE:
  814. result = "idle";
  815. break;
  816. case SYS_MODE_AUTHORIZING:
  817. result = "authorizing";
  818. break;
  819. case SYS_MODE_PREPARING:
  820. result = "preparing";
  821. break;
  822. case SYS_MODE_CHARGING:
  823. result = "charging";
  824. break;
  825. case SYS_MODE_TERMINATING:
  826. result = "terminating";
  827. break;
  828. case SYS_MODE_COMPLETE:
  829. result = "complete";
  830. break;
  831. case SYS_MODE_ALARM:
  832. result = "alarm";
  833. break;
  834. case SYS_MODE_FAULT:
  835. result = "fault";
  836. break;
  837. case SYS_MODE_MAINTAIN:
  838. result = "maintain";
  839. break;
  840. case SYS_MODE_RESERVATION:
  841. result = "reservation";
  842. break;
  843. case SYS_MODE_BOOKING:
  844. result = "booking";
  845. break;
  846. case SYS_MODE_DEBUG:
  847. result = "debug";
  848. break;
  849. case SYS_MODE_UPDATE:
  850. result = "upgrade";
  851. break;
  852. default:
  853. result = "unknown";
  854. break;
  855. }
  856. return result;
  857. }
  858. void setChargerMode(unsigned char gun_index, unsigned char mode)
  859. {
  860. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  861. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  862. 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));
  863. }
  864. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  865. {
  866. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  867. }
  868. unsigned char isModeChange(unsigned char gun_index)
  869. {
  870. unsigned char result = NO;
  871. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  872. {
  873. result = YES;
  874. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  875. }
  876. return result;
  877. }
  878. //===============================================
  879. // Get firmware version
  880. //===============================================
  881. void get_firmware_version(unsigned char gun_index)
  882. {
  883. FILE *fp;
  884. char cmd[512];
  885. char buf[512];
  886. // Get CSU hardware version
  887. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  888. // Get CSU boot loader version
  889. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  890. // Get CSU kernel version
  891. sprintf(cmd, "/bin/uname -r");
  892. fp = popen(cmd, "r");
  893. if(fp == NULL)
  894. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  895. else
  896. {
  897. while(fgets(buf, sizeof(buf), fp) != NULL)
  898. {
  899. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  900. }
  901. }
  902. // Get MCU firmware version
  903. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  904. // Get CSU root file system version
  905. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "B0.29.00.0000.00");
  906. // Get AC connector type from model name
  907. for(uint8_t idx=0;idx<3;idx++)
  908. {
  909. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  910. {
  911. case '1':
  912. // J1772 Plug
  913. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  914. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  915. break;
  916. case '2':
  917. // J1772 Socket
  918. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  919. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  920. break;
  921. case '3':
  922. // CE Plug
  923. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  924. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  925. break;
  926. case '4':
  927. // CE Socket
  928. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  929. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  930. break;
  931. case '5':
  932. // GB Plug
  933. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  934. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  935. break;
  936. case '6':
  937. // GB Socket
  938. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  939. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  940. break;
  941. }
  942. }
  943. // Get network option from model name
  944. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  945. {
  946. case 'B':
  947. case 'U':
  948. //Blue tooth
  949. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  950. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  951. break;
  952. case 'W':
  953. // WIFI
  954. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  955. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  956. break;
  957. case 'T':
  958. // 3G/4G
  959. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  960. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  961. break;
  962. default:
  963. // LAN
  964. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  965. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  966. break;
  967. }
  968. // Get rating power from model name
  969. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  970. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  971. // Get vender code from model name
  972. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  973. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  974. DEBUG_INFO("====================================================================\r\n");
  975. DEBUG_INFO("Model: %s\r\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  976. DEBUG_INFO("CSU hardware version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  977. DEBUG_INFO("CSU boot loader version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  978. DEBUG_INFO("CSU kernel version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  979. DEBUG_INFO("CSU root file system version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  980. DEBUG_INFO("CSU MCU-%2d firmware version: %s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  981. DEBUG_INFO("====================================================================\r\n");
  982. }
  983. //===============================================
  984. // Upgrade firmware
  985. //===============================================
  986. int upgrade_check()
  987. {
  988. int result = PASS;
  989. int fd;
  990. DIR *dir;
  991. struct dirent *file;
  992. char cmd[512];
  993. long int MaxLen=48*1024*1024;
  994. if ((dir = opendir ("/mnt")) != NULL)
  995. {
  996. /* print all the files and directories within directory */
  997. while ((file = readdir (dir)) != NULL)
  998. {
  999. if(strlen(file->d_name)>2)
  1000. {
  1001. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  1002. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  1003. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  1004. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  1005. {
  1006. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  1007. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  1008. read(fd, ptr, MaxLen);
  1009. close(fd);
  1010. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  1011. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  1012. DEBUG_INFO("New firmware type: %X\r\n", ShmCharger->fwUpgradeInfo.fwType);
  1013. DEBUG_INFO("New firmware model name: %s, %s\r\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  1014. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  1015. {
  1016. switch(ShmCharger->fwUpgradeInfo.fwType)
  1017. {
  1018. case CSU_BOOTLOADER:
  1019. case CSU_KERNEL_CONFIGURATION:
  1020. case CSU_KERNEL_IMAGE:
  1021. case CSU_ROOT_FILE_SYSTEM:
  1022. case CSU_USER_CONFIGURATION:
  1023. case CSU_PRIMARY_CONTROLLER:
  1024. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  1025. {
  1026. result = FAIL;
  1027. }
  1028. else
  1029. {
  1030. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  1031. {
  1032. DEBUG_INFO("Restore model name & serial number.\r\n");
  1033. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  1034. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1035. {
  1036. DEBUG_INFO("Re-load configuration fail.\r\n");
  1037. result = FAIL;
  1038. }
  1039. else
  1040. {
  1041. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  1042. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  1043. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  1044. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1045. {
  1046. DEBUG_INFO("Re-write configuration fail.\r\n");
  1047. result = FAIL;
  1048. }
  1049. else
  1050. DEBUG_INFO("Re-write configuration OK.\r\n");
  1051. }
  1052. }
  1053. }
  1054. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1055. system(cmd);
  1056. break;
  1057. case AC_WALLMOUNT_CONTROLLER:
  1058. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1059. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  1060. break;
  1061. default:
  1062. result = FAIL;
  1063. DEBUG_WARN("Image file is unknown type.\r\n");
  1064. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1065. system(cmd);
  1066. break;
  1067. }
  1068. }
  1069. else
  1070. {
  1071. result = FAIL;
  1072. DEBUG_ERROR("Model name and Firmware type error.\r\n");
  1073. }
  1074. free(ptr);
  1075. }
  1076. else
  1077. {
  1078. result = FAIL;
  1079. DEBUG_ERROR("New firmware open error.\r\n");
  1080. }
  1081. }
  1082. else
  1083. {
  1084. if(strlen(file->d_name) >= 3)
  1085. {
  1086. result = FAIL;
  1087. DEBUG_ERROR("File name error.\r\n");
  1088. }
  1089. else
  1090. {
  1091. DEBUG_ERROR("Searching file.\r\n");
  1092. }
  1093. }
  1094. }
  1095. closedir (dir);
  1096. }
  1097. else
  1098. {
  1099. result = FAIL;
  1100. DEBUG_ERROR("/mnt does not valid.\r\n");
  1101. }
  1102. return result;
  1103. }
  1104. //===============================================
  1105. // Check RFID is match with start user
  1106. //===============================================
  1107. int isMatchStartUser(unsigned char gun_index)
  1108. {
  1109. uint8_t tmpUser[32];
  1110. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  1111. {
  1112. // Big endian
  1113. switch(rfid.snType)
  1114. {
  1115. case RFID_SN_TYPE_6BYTE:
  1116. 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]);
  1117. break;
  1118. case RFID_SN_TYPE_7BYTE:
  1119. 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]);
  1120. break;
  1121. case RFID_SN_TYPE_10BYTE:
  1122. 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]);
  1123. break;
  1124. case RFID_SN_TYPE_4BYTE:
  1125. default:
  1126. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  1127. break;
  1128. }
  1129. }
  1130. else
  1131. {
  1132. // Little endian
  1133. switch(rfid.snType)
  1134. {
  1135. case RFID_SN_TYPE_6BYTE:
  1136. 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]);
  1137. break;
  1138. case RFID_SN_TYPE_7BYTE:
  1139. 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]);
  1140. break;
  1141. case RFID_SN_TYPE_10BYTE:
  1142. 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]);
  1143. break;
  1144. case RFID_SN_TYPE_4BYTE:
  1145. default:
  1146. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1147. break;
  1148. }
  1149. }
  1150. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  1151. }
  1152. //===============================================
  1153. // Read RFID Serial Number
  1154. //===============================================
  1155. int GetCardSerialNumber()
  1156. {
  1157. int isSuccess = FAIL;
  1158. int module_type = MODULE_EWT;
  1159. if(getRequestCardSN(rfidFd,module_type,&rfid))
  1160. {
  1161. if(rfid.cardType == ISO14443A)
  1162. {
  1163. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  1164. {
  1165. isSuccess = PASS;
  1166. }
  1167. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  1168. {
  1169. isSuccess = PASS;
  1170. }
  1171. sethaltCard(rfidFd,module_type);
  1172. }
  1173. else if(rfid.cardType == IS014443B)
  1174. {
  1175. isSuccess = PASS;
  1176. }
  1177. else if(rfid.cardType == FELICA)
  1178. {
  1179. isSuccess = PASS;
  1180. }
  1181. else
  1182. {}
  1183. }
  1184. else
  1185. {}
  1186. return isSuccess;
  1187. }
  1188. //===============================================
  1189. // Set led motion
  1190. //===============================================
  1191. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  1192. {
  1193. switch(led_mode)
  1194. {
  1195. case LED_ACTION_INIT:
  1196. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  1197. break;
  1198. case LED_ACTION_IDLE:
  1199. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  1200. break;
  1201. case LED_ACTION_AUTHED:
  1202. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  1203. break;
  1204. case LED_ACTION_CONNECTED:
  1205. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  1206. break;
  1207. case LED_ACTION_CHARGING:
  1208. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  1209. break;
  1210. case LED_ACTION_STOP:
  1211. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  1212. break;
  1213. case LED_ACTION_ALARM:
  1214. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  1215. break;
  1216. case LED_ACTION_MAINTAIN:
  1217. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  1218. break;
  1219. case LED_ACTION_RFID_PASS:
  1220. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  1221. break;
  1222. case LED_ACTION_RFID_FAIL:
  1223. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  1224. break;
  1225. case LED_ACTION_BLE_CONNECT:
  1226. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  1227. break;
  1228. case LED_ACTION_BLE_DISABLE:
  1229. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  1230. break;
  1231. case LED_ACTION_DEBUG:
  1232. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  1233. break;
  1234. case LED_ACTION_ALL_OFF:
  1235. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  1236. break;
  1237. case LED_RELAY_ON:
  1238. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  1239. break;
  1240. case LED_RELAY_OFF:
  1241. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  1242. break;
  1243. case LED_ACTION_HANDSHAKE_FAIL:
  1244. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  1245. break;
  1246. case LED_ACTION_INTERNET_DISCONNECT:
  1247. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  1248. break;
  1249. }
  1250. }
  1251. //===============================================
  1252. // Relay on/off request set
  1253. //===============================================
  1254. void setRelay(unsigned char gun_index,unsigned char isOn)
  1255. {
  1256. if(isOn == ON)
  1257. {
  1258. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  1259. {
  1260. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  1261. DEBUG_INFO("Gun-%d Output relay sts: ON \r\n",gun_index);
  1262. }
  1263. }
  1264. else
  1265. {
  1266. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  1267. {
  1268. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  1269. DEBUG_INFO("Gun-%d Output relay sts: OFF \r\n",gun_index);
  1270. }
  1271. }
  1272. }
  1273. //===============================================
  1274. // Relay on/off request get
  1275. //===============================================
  1276. int getRelay(unsigned char gun_index)
  1277. {
  1278. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  1279. }
  1280. //===============================================
  1281. // Set speaker on/off request
  1282. //===============================================
  1283. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  1284. {
  1285. if(isOn == ON)
  1286. {
  1287. ShmCharger->isSpeakerOn = ON;
  1288. ShmCharger->speaker_type = speaker_mode;
  1289. }
  1290. }
  1291. //===============================================
  1292. // Initialization RFID communication port
  1293. //===============================================
  1294. int InitRfidPort()
  1295. {
  1296. int uartO2 = open(rfidPortName, O_RDWR);
  1297. struct termios tios;
  1298. if (uartO2 != FAIL)
  1299. {
  1300. ioctl (uartO2, TCGETS, &tios);
  1301. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1302. tios.c_lflag = 0;
  1303. tios.c_iflag = 0;
  1304. tios.c_oflag = 0;
  1305. tios.c_cc[VMIN] = 0;
  1306. tios.c_cc[VTIME] = (unsigned char) 1;
  1307. tios.c_lflag = 0;
  1308. tcflush(uartO2, TCIFLUSH);
  1309. ioctl(uartO2, TCSETS, &tios);
  1310. }
  1311. if (uartO2 < 0)
  1312. {
  1313. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1314. }
  1315. return uartO2;
  1316. }
  1317. //===============================================
  1318. // Check internet access status
  1319. //===============================================
  1320. int isReachableInternet()
  1321. {
  1322. int result = FAIL;
  1323. FILE *fp;
  1324. char cmd[256];
  1325. char buf[512];
  1326. //char tmp[512];
  1327. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1328. {
  1329. strcpy(cmd, "ping -c 1 -w 3 ");
  1330. strcat(cmd, valid_Internet[idx]);
  1331. fp = popen(cmd, "r");
  1332. if(fp != NULL)
  1333. {
  1334. while(fgets(buf, sizeof(buf), fp) != NULL)
  1335. {
  1336. if(strstr(buf, "transmitted") > 0)
  1337. {
  1338. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1339. if(strstr(buf,"100%") != NULL)
  1340. {
  1341. }
  1342. else
  1343. {
  1344. result = PASS;
  1345. }
  1346. //DEBUG_INFO("%s",buf);
  1347. //DEBUG_INFO("%s\n",tmp);
  1348. }
  1349. }
  1350. }
  1351. pclose(fp);
  1352. }
  1353. return result;
  1354. }
  1355. //===============================================
  1356. // Check reservation date is expired
  1357. //===============================================
  1358. int isReservationExpired(unsigned char gun_index)
  1359. {
  1360. int result = NO;
  1361. struct tm expiredDate;
  1362. struct timeb expiredTime;
  1363. 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)
  1364. {
  1365. expiredDate.tm_year -= 1900;
  1366. expiredDate.tm_mon -= 1;
  1367. expiredTime.time = mktime(&expiredDate);
  1368. if(DiffTimebWithNow(expiredTime)>=0)
  1369. {
  1370. result = YES;
  1371. }
  1372. }
  1373. else
  1374. {
  1375. DEBUG_WARN("Expired date parsing error.\r\n");
  1376. DEBUG_INFO("Date expired.\r\n");
  1377. result = YES;
  1378. }
  1379. return result;
  1380. }
  1381. //===============================================
  1382. // Check charging profile related date routine
  1383. //===============================================
  1384. int isProfileValid(uint8_t gun_index)
  1385. {
  1386. int result = NO;
  1387. struct tm tmFrom, tmTo;
  1388. struct timeb tbFrom, tbTo;
  1389. 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) &&
  1390. (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))
  1391. {
  1392. tmFrom.tm_year -= 1900;
  1393. tmFrom.tm_mon -= 1;
  1394. tbFrom.time = mktime(&tmFrom);
  1395. tmTo.tm_year -= 1900;
  1396. tmTo.tm_mon -= 1;
  1397. tbTo.time = mktime(&tmTo);
  1398. DEBUG_INFO("Valid from compare Now: %d\r\n", DiffTimebWithNow(tbFrom));
  1399. DEBUG_INFO("Valid to compare Now: %d\r\n", DiffTimebWithNow(tbTo));
  1400. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  1401. {
  1402. result = YES;
  1403. }
  1404. }
  1405. else
  1406. {
  1407. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\r\n");
  1408. }
  1409. return result;
  1410. }
  1411. int getScheduleStart(int gun_index)
  1412. {
  1413. int result = -1;
  1414. struct tm tmScheduleStart;;
  1415. struct timeb tbScheduleStart;
  1416. 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))
  1417. {
  1418. tmScheduleStart.tm_year -= 1900;
  1419. tmScheduleStart.tm_mon -= 1;
  1420. tbScheduleStart.time = mktime(&tmScheduleStart);
  1421. result = DiffTimebWithNow(tbScheduleStart)/1000;
  1422. DEBUG_INFO("Schedule start compare Now(seconds): %d\r\n", result);
  1423. }
  1424. else
  1425. {
  1426. DEBUG_WARN("Schedule start date parsing error.\r\n");
  1427. }
  1428. return result;
  1429. }
  1430. int getStartSinceToday()
  1431. {
  1432. int result = -1;
  1433. time_t t;
  1434. struct tm *tmStartToday;
  1435. struct timeb tbStartToday;
  1436. t=time(NULL);
  1437. tmStartToday=localtime(&t);
  1438. tmStartToday->tm_hour = 0;
  1439. tmStartToday->tm_min = 0;
  1440. tmStartToday->tm_sec = 0;
  1441. tbStartToday.time = mktime(tmStartToday);
  1442. result = DiffTimebWithNow(tbStartToday)/1000;
  1443. DEBUG_INFO("Start today compare Now(seconds): %d\r\n", result);
  1444. return result;
  1445. }
  1446. int getStartSinceWeek()
  1447. {
  1448. int result = -1;
  1449. time_t t;
  1450. struct tm *tmStartWeek;
  1451. struct timeb tbStartWeek;
  1452. t=time(NULL);
  1453. tmStartWeek=localtime(&t);
  1454. t-=86400*tmStartWeek->tm_wday;
  1455. tmStartWeek=localtime(&t);
  1456. tmStartWeek->tm_hour = 0;
  1457. tmStartWeek->tm_min = 0;
  1458. tmStartWeek->tm_sec = 0;
  1459. tbStartWeek.time = mktime(tmStartWeek);
  1460. result = DiffTimebWithNow(tbStartWeek)/1000;
  1461. DEBUG_INFO("Start week compare Now(seconds): %d\r\n", result);
  1462. return result;
  1463. }
  1464. //===============================================
  1465. // Valid from local white list
  1466. //===============================================
  1467. int isValidLocalWhiteCard()
  1468. {
  1469. uint8_t result = FAIL;
  1470. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  1471. {
  1472. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  1473. {
  1474. result = PASS;
  1475. }
  1476. }
  1477. return result;
  1478. }
  1479. //==========================================
  1480. // Get Ethernet MAC address
  1481. //==========================================
  1482. int getEth0MacAddress()
  1483. {
  1484. int result = PASS;
  1485. FILE *fp;
  1486. char cmd[256];
  1487. char buf[512];
  1488. char tmp[512];
  1489. strcpy(cmd, "ifconfig");;
  1490. fp = popen(cmd, "r");
  1491. if(fp != NULL)
  1492. {
  1493. while(fgets(buf, sizeof(buf), fp) != NULL)
  1494. {
  1495. if(strstr(buf, "eth0") > 0)
  1496. {
  1497. result = PASS;
  1498. }
  1499. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  1500. {
  1501. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  1502. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  1503. }
  1504. }
  1505. }
  1506. pclose(fp);
  1507. return result;
  1508. }
  1509. //==========================================
  1510. // Check task processing
  1511. //==========================================
  1512. void checkTask()
  1513. {
  1514. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1515. {
  1516. if(system("pidof -s Module_4g > /dev/null") != 0)
  1517. {
  1518. DEBUG_INFO("Module_4g not running, restart it.\r\n");
  1519. system("/root/Module_4g &");
  1520. }
  1521. }
  1522. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1523. {
  1524. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  1525. {
  1526. DEBUG_INFO("Module_Wifi not running, restart it.\r\n");
  1527. system("/root/Module_Wifi &");
  1528. }
  1529. }
  1530. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  1531. {
  1532. DEBUG_INFO("Module_EventLogging not running, restart it.\r\n");
  1533. system("/root/Module_EventLogging &");
  1534. }
  1535. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  1536. {
  1537. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1538. {
  1539. DEBUG_INFO("OcppBackend not running, restart it.\r\n");
  1540. system("/root/OcppBackend &");
  1541. }
  1542. }
  1543. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  1544. {
  1545. DEBUG_INFO("Module_AlarmDetect not running, restart it.\r\n");
  1546. system("/root/Module_AlarmDetect &");
  1547. }
  1548. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  1549. {
  1550. DEBUG_INFO("Module_InternalComm not running, restart it.\r\n");
  1551. system("/root/Module_InternalComm &");
  1552. }
  1553. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  1554. {
  1555. DEBUG_INFO("Module_Speaker not running, restart it.\r\n");
  1556. system("/root/Module_Speaker &");
  1557. }
  1558. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  1559. {
  1560. DEBUG_INFO("Module_ProduceUtils not running, restart it.\r\n");
  1561. system ("/root/Module_ProduceUtils &");
  1562. }
  1563. }
  1564. //===============================================
  1565. // Check Connection Timeout
  1566. //===============================================
  1567. void checkConnectionTimeout()
  1568. {
  1569. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1570. {
  1571. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1572. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \r\n", TIMEOUT_SPEC_HANDSHAKING/1000);
  1573. }
  1574. else
  1575. {
  1576. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  1577. {
  1578. ShmCharger->timeoutSpec.Setting_Timeout_Spec = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1579. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  1580. {
  1581. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1582. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\r\n.");
  1583. }
  1584. else
  1585. {
  1586. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  1587. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\r\n.");
  1588. }
  1589. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \r\n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1590. }
  1591. else
  1592. {
  1593. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1594. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Table is blank...\r\n.");
  1595. }
  1596. //DEBUG_INFO("Present timeout spec : %d \r\n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  1597. }
  1598. }
  1599. //===============================================
  1600. // Main process
  1601. //===============================================
  1602. int main(void)
  1603. {
  1604. //Create all share memory
  1605. if(CreatShareMemory()==0)
  1606. {
  1607. DEBUG_ERROR("CreatShareMemory NG\r\n");
  1608. if(ShmStatusCodeData!=NULL)
  1609. {
  1610. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1611. }
  1612. sleep(5);
  1613. system("reboot -f");
  1614. sleep(5);
  1615. system("reboot -f");
  1616. }
  1617. else
  1618. {
  1619. DEBUG_INFO("CreatShareMemory OK\r\n");
  1620. }
  1621. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1622. {
  1623. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  1624. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  1625. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  1626. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  1627. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  1628. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  1629. }
  1630. // Main loop
  1631. for(;;)
  1632. {
  1633. //==========================================
  1634. // Synchronize share memory from OCPP struct
  1635. //==========================================
  1636. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ShmOCPP16Data->OcppConnStatus;
  1637. //==========================================
  1638. // Ethernet MAC address
  1639. //==========================================
  1640. getEth0MacAddress();
  1641. //==========================================
  1642. // Check task processing
  1643. //==========================================
  1644. checkTask();
  1645. //==========================================
  1646. // Check connection timeout specification
  1647. //==========================================
  1648. checkConnectionTimeout();
  1649. //==========================================
  1650. // Something need run in Idle mode
  1651. //==========================================
  1652. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  1653. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  1654. {
  1655. //======================================
  1656. // Check restore factory setting request
  1657. //======================================
  1658. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  1659. {
  1660. // Set led to initial
  1661. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1662. {
  1663. setLedMotion(gun_index,LED_ACTION_INIT);
  1664. }
  1665. system("cd /root;./Module_FactoryConfig -m");
  1666. system("sync");
  1667. sleep(5);
  1668. system("reboot -f");
  1669. sleep(5);
  1670. system("reboot -f");
  1671. }
  1672. //======================================
  1673. // Check upgrade firmware request
  1674. //======================================
  1675. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  1676. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  1677. {
  1678. ShmCharger->isUpdateSuccess = NO;
  1679. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1680. {
  1681. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  1682. setChargerMode(gun_index, SYS_MODE_UPDATE);
  1683. }
  1684. }
  1685. }
  1686. //==========================================
  1687. // Check remote reset request
  1688. //==========================================
  1689. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  1690. {
  1691. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  1692. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):true))
  1693. {
  1694. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  1695. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  1696. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  1697. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  1698. {
  1699. system("sync");
  1700. sleep(5);
  1701. system("reboot -f");
  1702. sleep(5);
  1703. system("reboot -f");
  1704. }
  1705. else
  1706. {
  1707. close(wtdFd);
  1708. system("/usr/bin/run_evse_restart.sh");
  1709. }
  1710. }
  1711. }
  1712. //==========================================
  1713. // Check RFID
  1714. //==========================================
  1715. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  1716. {
  1717. if(GetCardSerialNumber()!= FAIL)
  1718. {
  1719. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  1720. }
  1721. }
  1722. //==========================================
  1723. // Connector loop
  1724. //==========================================
  1725. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1726. {
  1727. // Synchronize current rating value from MCU
  1728. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1729. // Assign connector location index for OCPP
  1730. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  1731. // Synchronize present charging power
  1732. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  1733. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  1734. else
  1735. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  1736. // The system switch to Booting mode
  1737. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == 0XFF)
  1738. {
  1739. setChargerMode(gun_index, SYS_MODE_BOOTING);
  1740. }
  1741. // Check initialization "PASS" or "FAIL"
  1742. if(ShmCharger->gun_info[gun_index].isInitialPass == YES)
  1743. {
  1744. // Alarm event check
  1745. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  1746. {
  1747. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  1748. {
  1749. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE)
  1750. {
  1751. }
  1752. else
  1753. {
  1754. setChargerMode(gun_index, SYS_MODE_ALARM);
  1755. }
  1756. }
  1757. }
  1758. else
  1759. {
  1760. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM)
  1761. {
  1762. }
  1763. }
  1764. }
  1765. // Reservation request check
  1766. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  1767. {
  1768. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  1769. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  1770. {
  1771. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  1772. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  1773. }
  1774. DEBUG_INFO("Reservation request on connector-%d.\r\n", gun_index);
  1775. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  1776. }
  1777. // Change availability check
  1778. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  1779. {
  1780. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  1781. {
  1782. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  1783. {
  1784. setChargerMode(gun_index, SYS_MODE_IDLE);
  1785. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  1786. }
  1787. }
  1788. else
  1789. {
  1790. if(isMode(gun_index, SYS_MODE_IDLE))
  1791. {
  1792. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  1793. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  1794. }
  1795. }
  1796. }
  1797. // Unlock Connector signal check
  1798. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  1799. {
  1800. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  1801. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  1802. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  1803. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  1804. }
  1805. // Connector process
  1806. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  1807. {
  1808. case SYS_MODE_BOOTING:
  1809. if(isModeChange(gun_index))
  1810. {
  1811. setLedMotion(gun_index,LED_ACTION_INIT);
  1812. //CSU Initialization & task spawn
  1813. if((Initialization() != PASS) ||
  1814. (SpawnTask() != PASS))
  1815. {
  1816. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  1817. }
  1818. }
  1819. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  1820. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  1821. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  1822. {
  1823. ShmCharger->gun_selectd = 0;
  1824. // Firmware version
  1825. get_firmware_version(gun_index);
  1826. // OCPP BootNotification info set
  1827. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  1828. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1829. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1830. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  1831. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "AC");
  1832. DEBUG_INFO("==========================================\r\n");
  1833. DEBUG_INFO("System ID: %s\r\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  1834. DEBUG_INFO("==========================================\r\n");
  1835. DEBUG_INFO("========== OCPP BootNotification information ==========\r\n");
  1836. DEBUG_INFO("OCPP OcppServerURL: %s\r\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  1837. DEBUG_INFO("OCPP ChargeBoxId: %s\r\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1838. DEBUG_INFO("OCPP ChargePointVendor: %s\r\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1839. DEBUG_INFO("OCPP CpFwVersion: %s\r\n",ShmOCPP16Data->BootNotification.CpFwVersion);
  1840. DEBUG_INFO("OCPP CpMeterSerialNumber: %s\r\n",ShmOCPP16Data->BootNotification.CpMeterSerialNumber);
  1841. DEBUG_INFO("OCPP CpMeterType: %s\r\n",ShmOCPP16Data->BootNotification.CpMeterType);
  1842. DEBUG_INFO("=======================================================\r\n");
  1843. DEBUG_INFO("========== Set Wifi information ==========\r\n");
  1844. DEBUG_INFO("Wifi mode: %d\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  1845. DEBUG_INFO("Wifi SSID: %s\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  1846. DEBUG_INFO("Wifi password: %s\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  1847. DEBUG_INFO("==========================================\r\n");
  1848. // Flag for initialization system success
  1849. ShmCharger->gun_info[gun_index].isInitialPass = YES;
  1850. // Set max current to rating current
  1851. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1852. // If rotate switch equal zero, the system needs to change Debug mode
  1853. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  1854. setChargerMode(gun_index, SYS_MODE_DEBUG);
  1855. else
  1856. setChargerMode(gun_index, SYS_MODE_IDLE);
  1857. }
  1858. break;
  1859. case SYS_MODE_IDLE:
  1860. if(isModeChange(gun_index))
  1861. {
  1862. setLedMotion(gun_index,LED_ACTION_IDLE);
  1863. setRelay(gun_index,OFF);
  1864. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  1865. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  1866. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  1867. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  1868. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  1869. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  1870. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  1871. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  1872. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  1873. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  1874. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1875. memset(ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, 0x00, ARRAY_SIZE(ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus));
  1876. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  1877. // Response StopTransactionConf
  1878. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf == ON)
  1879. {
  1880. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = OFF;
  1881. }
  1882. }
  1883. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  1884. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  1885. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  1886. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  1887. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == ON))
  1888. {
  1889. // Clean User id & Card Number
  1890. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  1891. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  1892. {
  1893. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  1894. DEBUG_INFO("Start Method : RFID...\r\n");
  1895. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  1896. {
  1897. // Big endian
  1898. switch(rfid.snType)
  1899. {
  1900. case RFID_SN_TYPE_6BYTE:
  1901. 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]);
  1902. break;
  1903. case RFID_SN_TYPE_7BYTE:
  1904. 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]);
  1905. break;
  1906. case RFID_SN_TYPE_10BYTE:
  1907. 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]);
  1908. break;
  1909. case RFID_SN_TYPE_4BYTE:
  1910. default:
  1911. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  1912. break;
  1913. }
  1914. }
  1915. else
  1916. {
  1917. // Little endian
  1918. switch(rfid.snType)
  1919. {
  1920. case RFID_SN_TYPE_6BYTE:
  1921. 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]);
  1922. break;
  1923. case RFID_SN_TYPE_7BYTE:
  1924. 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]);
  1925. break;
  1926. case RFID_SN_TYPE_10BYTE:
  1927. 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]);
  1928. break;
  1929. case RFID_SN_TYPE_4BYTE:
  1930. default:
  1931. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1932. break;
  1933. }
  1934. }
  1935. DEBUG_INFO("Start request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  1936. }
  1937. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq)
  1938. {
  1939. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  1940. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  1941. DEBUG_INFO("Start Method : BACKEND...\r\n");
  1942. }
  1943. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  1944. {
  1945. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  1946. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  1947. DEBUG_INFO("Start Method : BLE...\r\n");
  1948. }
  1949. else
  1950. {
  1951. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  1952. DEBUG_INFO("Start Method : FREE..\r\n");
  1953. }
  1954. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  1955. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  1956. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  1957. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  1958. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  1959. }
  1960. else
  1961. {}
  1962. break;
  1963. case SYS_MODE_AUTHORIZING:
  1964. if(isModeChange(gun_index))
  1965. {
  1966. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  1967. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  1968. {
  1969. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  1970. {
  1971. case AUTH_MODE_ENABLE:
  1972. if(ShmOCPP16Data->OcppConnStatus)
  1973. {
  1974. // On line
  1975. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  1976. }
  1977. else
  1978. {
  1979. // Off line
  1980. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  1981. {
  1982. case OFF_POLICY_LOCALLIST:
  1983. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  1984. break;
  1985. case OFF_POLICY_PH_RFID:
  1986. break;
  1987. case OFF_POLICY_FREE:
  1988. break;
  1989. case OFF_POLICY_NOCHARGE:
  1990. default:
  1991. break;
  1992. }
  1993. }
  1994. break;
  1995. case AUTH_MODE_DISABLE:
  1996. default:
  1997. break;
  1998. }
  1999. }
  2000. }
  2001. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  2002. {
  2003. // Authorization timeout process.
  2004. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2005. setChargerMode(gun_index, SYS_MODE_IDLE);
  2006. DEBUG_WARN("Authorize timeout !!!\r\n");
  2007. }
  2008. else
  2009. {
  2010. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  2011. {
  2012. case START_METHOD_RFID:
  2013. if((ShmOCPP16Data->SpMsg.bits.AuthorizeConf) ||
  2014. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  2015. (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  2016. (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))
  2017. {
  2018. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  2019. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  2020. (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  2021. {
  2022. DEBUG_INFO("Authorize pass.\r\n");
  2023. setSpeaker(ON,SPEAKER_SHORT);
  2024. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  2025. sleep(3);
  2026. setChargerMode(gun_index, SYS_MODE_PREPARING);
  2027. }
  2028. else
  2029. {
  2030. DEBUG_INFO("Authorize fail.\r\n");
  2031. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2032. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2033. sleep(3);
  2034. setChargerMode(gun_index, SYS_MODE_IDLE);
  2035. }
  2036. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2037. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2038. }
  2039. break;
  2040. case START_METHOD_BACKEND:
  2041. case START_METHOD_BLE:
  2042. case START_METHOD_FREE:
  2043. default:
  2044. setSpeaker(ON,SPEAKER_SHORT);
  2045. setChargerMode(gun_index, SYS_MODE_PREPARING);
  2046. break;
  2047. }
  2048. }
  2049. break;
  2050. case SYS_MODE_PREPARING:
  2051. if(isModeChange(gun_index))
  2052. {
  2053. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2054. setRelay(gun_index, ON);
  2055. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  2056. }
  2057. // If control pilot detect Bx, skip watch dog time out.
  2058. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)
  2059. {
  2060. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2061. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  2062. }
  2063. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  2064. {
  2065. DEBUG_INFO("Charging gun is plugged before.\r\n");
  2066. setChargerMode(gun_index, SYS_MODE_IDLE);
  2067. }
  2068. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  2069. {
  2070. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  2071. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  2072. {
  2073. DEBUG_INFO("Handshaking timeout...\r\n");
  2074. setChargerMode(gun_index, SYS_MODE_IDLE);
  2075. }
  2076. }
  2077. else if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  2078. {
  2079. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  2080. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].OutputEnergy = 0;
  2081. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2082. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  2083. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  2084. ShmOCPP16Data->StartTransaction[gun_index].MeterStart = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  2085. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2086. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  2087. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = ON;
  2088. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2089. }
  2090. break;
  2091. case SYS_MODE_CHARGING:
  2092. if(isModeChange(gun_index))
  2093. {
  2094. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2095. ftime(&startChargingTime[gun_index]);
  2096. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2097. }
  2098. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2099. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2100. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)||
  2101. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  2102. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2103. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON))
  2104. {
  2105. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2106. }
  2107. else
  2108. {
  2109. setLedMotion(gun_index,LED_ACTION_CHARGING);
  2110. ShmOCPP16Data->StopTransaction[gun_index].MeterStop = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start)/100;
  2111. ftime(&endChargingTime[gun_index]);
  2112. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2113. 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;
  2114. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].OutputEnergy = ((float)(ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start))/100;
  2115. // Response StartTransactionConf
  2116. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == ON)
  2117. {
  2118. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = OFF;
  2119. }
  2120. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  2121. if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId>0) &&
  2122. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2123. {
  2124. // Debug information
  2125. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2126. {
  2127. DEBUG_INFO("Profile ID found: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2128. DEBUG_INFO("Valid from: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2129. DEBUG_INFO("Valid to: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2130. DEBUG_INFO("Start schedule: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2131. DEBUG_INFO("Profile kind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2132. DEBUG_INFO("RecurrencyKind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2133. DEBUG_INFO("Profile purpose: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2134. DEBUG_INFO("Transaction ID: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2135. DEBUG_INFO("ChargingRateUnit: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2136. }
  2137. // Checking profile kind
  2138. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2139. {
  2140. // Absolute profile
  2141. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2142. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2143. {
  2144. // Checking limitation
  2145. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2146. {
  2147. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2148. (((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)))
  2149. {
  2150. 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);
  2151. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2152. }
  2153. }
  2154. }
  2155. }
  2156. else if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Relative") == PASS))
  2157. {
  2158. // Relative profile
  2159. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2160. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2161. {
  2162. // Checking limitation
  2163. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2164. {
  2165. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2166. (((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)))
  2167. {
  2168. 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);
  2169. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2170. }
  2171. }
  2172. }
  2173. }
  2174. else
  2175. {
  2176. // Recurring profile
  2177. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2178. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2179. {
  2180. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind, "Daily") == PASS))
  2181. {
  2182. // Checking limitation
  2183. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2184. {
  2185. if((getStartSinceToday() > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2186. (((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)))
  2187. {
  2188. 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);
  2189. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2190. }
  2191. }
  2192. }
  2193. else
  2194. {
  2195. // Checking limitation
  2196. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2197. {
  2198. if((getStartSinceWeek() > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2199. (((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)))
  2200. {
  2201. 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);
  2202. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2203. }
  2204. }
  2205. }
  2206. }
  2207. }
  2208. }
  2209. // Determine max charging current to MCU
  2210. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  2211. {
  2212. 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);
  2213. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2214. {
  2215. 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);
  2216. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2217. }
  2218. }
  2219. else
  2220. {
  2221. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2222. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2223. {
  2224. 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);
  2225. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2226. }
  2227. }
  2228. // Debug information
  2229. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2230. {
  2231. // Charging profile preparation
  2232. if(!ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf)
  2233. {
  2234. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = ON;
  2235. }
  2236. else
  2237. {
  2238. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = OFF;
  2239. }
  2240. DEBUG_INFO("=======================================================================\r\n");
  2241. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \r\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  2242. 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);
  2243. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  2244. DEBUG_INFO("=======================================================================\r\n");
  2245. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2246. }
  2247. if(ShmOCPP16Data->OcppConnStatus)
  2248. {
  2249. // On-line max condition check
  2250. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  2251. {
  2252. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2253. 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));
  2254. }
  2255. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  2256. {
  2257. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2258. 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));
  2259. }
  2260. else
  2261. {
  2262. setRelay(gun_index, ON);
  2263. }
  2264. }
  2265. else
  2266. {
  2267. // Off-line max condition check
  2268. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2269. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  2270. {
  2271. 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))))
  2272. {
  2273. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2274. 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));
  2275. }
  2276. 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))))
  2277. {
  2278. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2279. 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));
  2280. }
  2281. else
  2282. {
  2283. setRelay(gun_index, ON);
  2284. }
  2285. }
  2286. else
  2287. {
  2288. setRelay(gun_index, OFF);
  2289. DEBUG_INFO("Connector-%d can not charging in off line\r\n", gun_index);
  2290. }
  2291. }
  2292. }
  2293. break;
  2294. case SYS_MODE_TERMINATING:
  2295. if(isModeChange(gun_index))
  2296. {
  2297. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  2298. {
  2299. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2300. }
  2301. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  2302. {
  2303. setLedMotion(gun_index, LED_ACTION_STOP);
  2304. }
  2305. else
  2306. {}
  2307. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  2308. }
  2309. // If RFID SN different with start user, it need to authorize ID
  2310. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  2311. {
  2312. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ShmOCPP16Data->SpMsg.bits.AuthorizeReq)
  2313. {
  2314. // Request authorization
  2315. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  2316. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2317. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2318. {
  2319. // Big endian
  2320. switch(rfid.snType)
  2321. {
  2322. case RFID_SN_TYPE_6BYTE:
  2323. 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]);
  2324. break;
  2325. case RFID_SN_TYPE_7BYTE:
  2326. 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]);
  2327. break;
  2328. case RFID_SN_TYPE_10BYTE:
  2329. 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]);
  2330. break;
  2331. case RFID_SN_TYPE_4BYTE:
  2332. default:
  2333. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2334. break;
  2335. }
  2336. }
  2337. else
  2338. {
  2339. // Little endian
  2340. switch(rfid.snType)
  2341. {
  2342. case RFID_SN_TYPE_6BYTE:
  2343. 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]);
  2344. break;
  2345. case RFID_SN_TYPE_7BYTE:
  2346. 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]);
  2347. break;
  2348. case RFID_SN_TYPE_10BYTE:
  2349. 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]);
  2350. break;
  2351. case RFID_SN_TYPE_4BYTE:
  2352. default:
  2353. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2354. break;
  2355. }
  2356. }
  2357. DEBUG_INFO("End request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2358. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  2359. {
  2360. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  2361. {
  2362. case AUTH_MODE_ENABLE:
  2363. if(ShmOCPP16Data->OcppConnStatus)
  2364. {
  2365. // On line
  2366. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2367. }
  2368. else
  2369. {
  2370. // Off line
  2371. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  2372. {
  2373. case OFF_POLICY_LOCALLIST:
  2374. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2375. break;
  2376. case OFF_POLICY_PH_RFID:
  2377. break;
  2378. case OFF_POLICY_FREE:
  2379. break;
  2380. case OFF_POLICY_NOCHARGE:
  2381. default:
  2382. break;
  2383. }
  2384. }
  2385. break;
  2386. case AUTH_MODE_DISABLE:
  2387. default:
  2388. break;
  2389. }
  2390. }
  2391. }
  2392. else
  2393. {
  2394. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
  2395. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2396. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  2397. {
  2398. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  2399. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2400. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  2401. {
  2402. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  2403. }
  2404. else
  2405. {
  2406. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2407. DEBUG_INFO("Authorize fail.\r\n");
  2408. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2409. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2410. sleep(3);
  2411. }
  2412. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2413. }
  2414. }
  2415. }
  2416. // End authorize pass
  2417. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  2418. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  2419. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2420. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON) ||
  2421. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)) ||
  2422. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2423. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON))
  2424. {
  2425. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2426. {
  2427. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  2428. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  2429. {
  2430. DEBUG_INFO("Authorize pass.\r\n");
  2431. setSpeaker(ON,SPEAKER_SHORT);
  2432. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  2433. sleep(3);
  2434. }
  2435. }
  2436. else
  2437. {}
  2438. setRelay(gun_index, OFF);
  2439. setLedMotion(gun_index, LED_ACTION_STOP);
  2440. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  2441. {
  2442. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  2443. }
  2444. }
  2445. else
  2446. {
  2447. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  2448. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  2449. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  2450. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != ON) &&
  2451. !ShmOCPP16Data->MsMsg.bits.ResetReq &&
  2452. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  2453. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  2454. !(!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)))) &&
  2455. !(!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)))))
  2456. {
  2457. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2458. }
  2459. }
  2460. break;
  2461. case SYS_MODE_COMPLETE:
  2462. if(isModeChange(gun_index))
  2463. {
  2464. setLedMotion(gun_index, LED_ACTION_STOP);
  2465. }
  2466. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2467. {
  2468. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  2469. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  2470. else
  2471. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  2472. }
  2473. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  2474. {
  2475. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  2476. }
  2477. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  2478. {
  2479. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  2480. }
  2481. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  2482. {
  2483. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  2484. }
  2485. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  2486. {
  2487. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  2488. }
  2489. else
  2490. {
  2491. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  2492. }
  2493. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\r\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  2494. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  2495. ShmOCPP16Data->StopTransaction[gun_index].MeterStop = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  2496. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  2497. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2498. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  2499. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  2500. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  2501. DB_Insert_Record(localDb, gun_index);
  2502. setChargerMode(gun_index, SYS_MODE_IDLE);
  2503. break;
  2504. case SYS_MODE_ALARM:
  2505. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  2506. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  2507. {
  2508. }
  2509. else
  2510. {
  2511. setRelay(gun_index, OFF);
  2512. }
  2513. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  2514. {
  2515. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  2516. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  2517. {
  2518. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  2519. }
  2520. else
  2521. {
  2522. setChargerMode(gun_index, SYS_MODE_IDLE);
  2523. }
  2524. }
  2525. setLedMotion(gun_index,LED_ACTION_ALARM);
  2526. break;
  2527. case SYS_MODE_FAULT:
  2528. if(isModeChange(gun_index))
  2529. {}
  2530. break;
  2531. case SYS_MODE_MAINTAIN:
  2532. if(isModeChange(gun_index))
  2533. {
  2534. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  2535. }
  2536. break;
  2537. case SYS_MODE_UPDATE:
  2538. if(isModeChange(gun_index))
  2539. {
  2540. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  2541. }
  2542. //======================================
  2543. // Check local upgrade firmware request
  2544. //======================================
  2545. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  2546. {
  2547. DEBUG_INFO("Firmware local upgrading...\r\n");
  2548. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  2549. int value = upgrade_check();
  2550. DEBUG_INFO("Local update Value: %d...\r\n",value);
  2551. if(value == PASS)
  2552. {
  2553. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  2554. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  2555. ShmCharger->isUpdateSuccess = YES;
  2556. DEBUG_INFO("Local update success...\r\n");
  2557. }
  2558. else
  2559. {
  2560. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  2561. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  2562. ShmCharger->isUpdateSuccess = NO;
  2563. DEBUG_INFO("Local update unsuccess...\r\n");
  2564. }
  2565. }
  2566. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  2567. {
  2568. //======================================
  2569. // Check remote upgrade firmware request
  2570. //======================================
  2571. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  2572. {
  2573. DEBUG_INFO("Firmware remote upgraded fail...\r\n");
  2574. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  2575. ShmCharger->isUpdateSuccess = NO;
  2576. }
  2577. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  2578. {
  2579. DEBUG_INFO("Firmware remote upgrading...\r\n");
  2580. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  2581. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  2582. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  2583. int result = upgrade_check();
  2584. DEBUG_INFO("Remote update Result: %d...\r\n",result);
  2585. if(result == PASS)
  2586. {
  2587. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  2588. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  2589. ShmCharger->isUpdateSuccess = YES;
  2590. DEBUG_INFO("Remote update success...\r\n");
  2591. }
  2592. else
  2593. {
  2594. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  2595. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  2596. ShmCharger->isUpdateSuccess = NO;
  2597. DEBUG_INFO("Remote update unsuccess...\r\n");
  2598. }
  2599. }
  2600. else
  2601. {}
  2602. }
  2603. else
  2604. {
  2605. //======================================
  2606. // Upgrade complete reboot system
  2607. //======================================
  2608. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  2609. {
  2610. if(ShmCharger->isUpdateSuccess == YES)
  2611. {
  2612. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  2613. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  2614. DEBUG_WARN("Firmware upgrade success.\r\n");
  2615. sleep(5);
  2616. DEBUG_INFO("Firmware upgraded, reboot...\r\n");
  2617. system("reboot -f");
  2618. sleep(5);
  2619. system("reboot -f");
  2620. }
  2621. else
  2622. {
  2623. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  2624. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  2625. DEBUG_WARN("Firmware upgrade fail.\r\n");
  2626. close(wtdFd);
  2627. system("/usr/bin/run_evse_restart.sh");
  2628. }
  2629. }
  2630. }
  2631. break;
  2632. case SYS_MODE_RESERVATION:
  2633. if(isModeChange(gun_index))
  2634. {
  2635. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  2636. }
  2637. if(isReservationExpired(gun_index))
  2638. {
  2639. DEBUG_INFO("Reservation: Time's up...\r\n");
  2640. setChargerMode(gun_index, SYS_MODE_IDLE);
  2641. }
  2642. else if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq)
  2643. {
  2644. DEBUG_INFO("Reservation: Cancel reservation...\r\n");
  2645. setChargerMode(gun_index, SYS_MODE_IDLE);
  2646. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = OFF;
  2647. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  2648. }
  2649. else
  2650. {
  2651. // Check is there RFID or back end request start
  2652. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2653. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == ON))
  2654. {
  2655. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  2656. {
  2657. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2658. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2659. {
  2660. // Big endian
  2661. switch(rfid.snType)
  2662. {
  2663. case RFID_SN_TYPE_6BYTE:
  2664. 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]);
  2665. break;
  2666. case RFID_SN_TYPE_7BYTE:
  2667. 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]);
  2668. break;
  2669. case RFID_SN_TYPE_10BYTE:
  2670. 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]);
  2671. break;
  2672. case RFID_SN_TYPE_4BYTE:
  2673. default:
  2674. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2675. break;
  2676. }
  2677. }
  2678. else
  2679. {
  2680. // Little endian
  2681. switch(rfid.snType)
  2682. {
  2683. case RFID_SN_TYPE_6BYTE:
  2684. 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]);
  2685. break;
  2686. case RFID_SN_TYPE_7BYTE:
  2687. 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]);
  2688. break;
  2689. case RFID_SN_TYPE_10BYTE:
  2690. 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]);
  2691. break;
  2692. case RFID_SN_TYPE_4BYTE:
  2693. default:
  2694. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2695. break;
  2696. }
  2697. }
  2698. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  2699. {
  2700. DEBUG_INFO("Start Method in reservation : RFID...\r\n");
  2701. DEBUG_INFO("Start request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2702. DEBUG_INFO("Reservation User Id : %s\r\n", ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  2703. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  2704. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  2705. }
  2706. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2707. }
  2708. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq)
  2709. {
  2710. if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  2711. {
  2712. DEBUG_INFO("Start Method in reservation: BACKEND...\r\n");
  2713. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  2714. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  2715. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  2716. }
  2717. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  2718. }
  2719. }
  2720. }
  2721. break;
  2722. case SYS_MODE_BOOKING:
  2723. if(isModeChange(gun_index))
  2724. {}
  2725. break;
  2726. case SYS_MODE_DEBUG:
  2727. if(isModeChange(gun_index))
  2728. {
  2729. setLedMotion(gun_index,LED_ACTION_DEBUG);
  2730. }
  2731. break;
  2732. }
  2733. }
  2734. // System watch dog reset
  2735. write(wtdFd, "a", 1);
  2736. usleep(50000);
  2737. }
  2738. return FAIL;
  2739. }