Module_InternalComm.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <termios.h>
  22. #include <errno.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "define.h"
  30. #include "main.h"
  31. #include "Module_InternalComm.h"
  32. //#define SIMULATION
  33. #define FAIL_SPEC_COMM 100
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL 0
  37. #define ON 1
  38. #define OFF 0
  39. //Print out level
  40. unsigned char PRINT_OUT_LEVEL_STATE = 0;
  41. #define LEVEL_1 4 // 3 Seconds
  42. #define LEVEL_2 8 // 6 Seconds
  43. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  44. struct StatusCodeData *ShmStatusCodeData;
  45. struct PrimaryMcuData *ShmPrimaryMcuData;
  46. struct OCPP16Data *ShmOCPP16Data;
  47. struct Charger *ShmCharger;
  48. uint16_t stepIndex;
  49. long long tsLast, tsNow;
  50. double tmpPowerConsumption;
  51. void trim(char *s);
  52. int mystrcmp(char *p1,char *p2);
  53. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  54. void split(char **arr, char *str, const char *del);
  55. int StoreLogMsg(const char *fmt, ...)
  56. {
  57. char Buf[4096+256];
  58. char buffer[4096];
  59. time_t CurrentTime;
  60. struct tm *tm;
  61. struct timeval tv;
  62. va_list args;
  63. va_start(args, fmt);
  64. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  65. va_end(args);
  66. memset(Buf,0,sizeof(Buf));
  67. CurrentTime = time(NULL);
  68. tm=localtime(&CurrentTime);
  69. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  70. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_InterCommLog",
  71. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  72. buffer,
  73. tm->tm_year+1900,tm->tm_mon+1);
  74. #ifdef SystemLogMessage
  75. system(Buf);
  76. #endif
  77. #ifdef ConsloePrintLog
  78. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  79. #endif
  80. return rc;
  81. }
  82. int DiffTimeb(struct timeb ST, struct timeb ET)
  83. {
  84. //return milli-second
  85. unsigned int StartTime,StopTime;
  86. StartTime=(unsigned int)ST.time;
  87. StopTime=(unsigned int)ET.time;
  88. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  89. }
  90. long long current_timestamp()
  91. {
  92. struct timeval te;
  93. gettimeofday(&te, NULL); // get current time
  94. long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
  95. return milliseconds;
  96. }
  97. //=================================
  98. // Common routine
  99. //=================================
  100. void trim(char *s)
  101. {
  102. int i=0, j, k, l=0;
  103. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  104. i++;
  105. j = strlen(s)-1;
  106. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  107. j--;
  108. if(i==0 && j==strlen(s)-1) { }
  109. else if(i==0) s[j+1] = '\0';
  110. else {
  111. for(k=i; k<=j; k++) s[l++] = s[k];
  112. s[l] = '\0';
  113. }
  114. }
  115. int mystrcmp(char *p1,char *p2)
  116. {
  117. while(*p1==*p2)
  118. {
  119. if(*p1=='\0' || *p2=='\0')
  120. break;
  121. p1++;
  122. p2++;
  123. }
  124. if(*p1=='\0' && *p2=='\0')
  125. return(PASS);
  126. else
  127. return(FAIL);
  128. }
  129. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  130. {
  131. strncpy(dest, src + start, cnt);
  132. dest[cnt] = 0;
  133. }
  134. void split(char **arr, char *str, const char *del)
  135. {
  136. char *s = strtok(str, del);
  137. while(s != NULL)
  138. {
  139. *arr++ = s;
  140. s = strtok(NULL, del);
  141. }
  142. }
  143. //==========================================
  144. // Init all share memory
  145. //==========================================
  146. int InitShareMemory()
  147. {
  148. int result = PASS;
  149. int MeterSMId;
  150. //Initial ShmSysConfigAndInfo
  151. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  152. {
  153. #ifdef SystemLogMessage
  154. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  155. #endif
  156. result = FAIL;
  157. }
  158. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  159. {
  160. #ifdef SystemLogMessage
  161. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  162. #endif
  163. result = FAIL;
  164. }
  165. //Initial ShmStatusCodeData
  166. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  167. {
  168. #ifdef SystemLogMessage
  169. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  170. #endif
  171. result = FAIL;
  172. }
  173. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  174. {
  175. #ifdef SystemLogMessage
  176. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  177. #endif
  178. result = FAIL;
  179. }
  180. //Initial ShmPrimaryMcuKey
  181. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  182. {
  183. #ifdef SystemLogMessage
  184. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  185. #endif
  186. result = FAIL;
  187. }
  188. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  189. {
  190. #ifdef SystemLogMessage
  191. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  192. #endif
  193. result = FAIL;
  194. }
  195. //creat ShmOCPP16Data
  196. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  197. {
  198. #ifdef SystemLogMessage
  199. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  200. #endif
  201. result = FAIL;
  202. }
  203. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  204. {
  205. #ifdef SystemLogMessage
  206. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  207. #endif
  208. result = FAIL;
  209. }
  210. else
  211. {}
  212. //Initial ShmCharger
  213. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
  214. {
  215. #ifdef SystemLogMessage
  216. DEBUG_ERROR("shmget ShmChargerKey NG\n");
  217. #endif
  218. result = FAIL;
  219. }
  220. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  221. {
  222. #ifdef SystemLogMessage
  223. DEBUG_ERROR("shmat ShmChargerKey NG\n");
  224. #endif
  225. result = FAIL;
  226. }
  227. return result;
  228. }
  229. int InitComPort()
  230. {
  231. int fd;
  232. struct termios tios;
  233. fd = open("/dev/ttyS1", O_RDWR);
  234. if(fd<=0)
  235. {
  236. #ifdef SystemLogMessage
  237. DEBUG_ERROR("open /dev/ttyS1 NG\n");
  238. #endif
  239. return -1;
  240. }
  241. ioctl (fd, TCGETS, &tios);
  242. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  243. tios.c_lflag = 0;
  244. tios.c_iflag = 0;
  245. tios.c_oflag = 0;
  246. tios.c_cc[VMIN]=0;
  247. tios.c_cc[VTIME]=(unsigned char)5; // timeout 0.5 secod
  248. tios.c_lflag=0;
  249. tcflush(fd, TCIFLUSH);
  250. ioctl (fd, TCSETS, &tios);
  251. return fd;
  252. }
  253. void ack_delay(unsigned char cmd)
  254. {
  255. switch(cmd)
  256. {
  257. case CMD_UPDATE_START:
  258. case CMD_UPDATE_END:
  259. usleep(300000);
  260. break;
  261. default:
  262. usleep(100000);
  263. break;
  264. }
  265. }
  266. int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
  267. {
  268. int len;
  269. tcflush(fd,TCIOFLUSH);
  270. if(write(fd, cmd, cmd_len) >= cmd_len)
  271. {
  272. ack_delay(cmd[3]);
  273. len = read(fd, rx, 512);
  274. }
  275. else
  276. {
  277. #ifdef SystemLogMessage
  278. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  279. #endif
  280. }
  281. return len;
  282. }
  283. unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  284. {
  285. unsigned char result = FAIL;
  286. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FW_VER, 0x00, 0x00, 0x00};
  287. unsigned char rx[512];
  288. unsigned char chksum = 0x00;
  289. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  290. if(len > 6)
  291. {
  292. if (len < 6+(rx[4] | rx[5]<<8))
  293. return result;
  294. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  295. {
  296. chksum ^= rx[6+idx];
  297. }
  298. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  299. (rx[2] == tx[1]) &&
  300. (rx[1] == tx[2]) &&
  301. (rx[3] == tx[3]))
  302. {
  303. strncpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
  304. result = PASS;
  305. }
  306. }
  307. return result;
  308. }
  309. unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  310. {
  311. unsigned char result = FAIL;
  312. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_HW_VER, 0x00, 0x00, 0x00};
  313. unsigned char rx[512];
  314. unsigned char chksum = 0x00;
  315. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  316. if(len > 6)
  317. {
  318. if (len < 6+(rx[4] | rx[5]<<8))
  319. return result;
  320. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  321. {
  322. chksum ^= rx[6+idx];
  323. }
  324. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  325. (rx[2] == tx[1]) &&
  326. (rx[1] == tx[2]) &&
  327. (rx[3] == tx[3]))
  328. {
  329. strncpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
  330. result = PASS;
  331. }
  332. }
  333. return result;
  334. }
  335. unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
  336. {
  337. unsigned char result = FAIL;
  338. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_INPUTVOLTAGE, 0x00, 0x00, 0x00};
  339. unsigned char rx[512];
  340. unsigned char chksum = 0x00;
  341. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  342. if(len > 6)
  343. {
  344. if (len < 6+(rx[4] | rx[5]<<8))
  345. return result;
  346. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  347. {
  348. chksum ^= rx[6+idx];
  349. }
  350. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  351. (rx[2] == tx[1]) &&
  352. (rx[1] == tx[2]) &&
  353. (rx[3] == tx[3]))
  354. {
  355. Ret_Buf->inputType = rx[6];
  356. Ret_Buf->L1N_L12 =( rx[7] | (rx[8]<<8))/10.0;
  357. Ret_Buf->L2N_L23 =( rx[9] | (rx[10]<<8))/10.0;
  358. Ret_Buf->L3N_L31 =( rx[11] | (rx[12]<<8))/10.0;
  359. result = PASS;
  360. }
  361. }
  362. return result;
  363. }
  364. unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf)
  365. {
  366. unsigned char result = FAIL;
  367. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTVOLTAGE, 0x00, 0x00, 0x00};
  368. unsigned char rx[512];
  369. unsigned char chksum = 0x00;
  370. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  371. if(len > 6)
  372. {
  373. if (len < 6+(rx[4] | rx[5]<<8))
  374. return result;
  375. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  376. {
  377. chksum ^= rx[6+idx];
  378. }
  379. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  380. (rx[2] == tx[1]) &&
  381. (rx[1] == tx[2]) &&
  382. (rx[3] == tx[3]))
  383. {
  384. Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8))/10.0;
  385. Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8))/10.0;
  386. if((rx[4] | rx[5]<<8) > 4)
  387. {
  388. Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8))/10.0;
  389. Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8))/10.0;
  390. }
  391. result = PASS;
  392. }
  393. }
  394. return result;
  395. }
  396. unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
  397. {
  398. unsigned char result = FAIL;
  399. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FAN_SPEED, 0x00, 0x00, 0x00};
  400. unsigned char rx[512];
  401. unsigned char chksum = 0x00;
  402. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  403. if(len > 6)
  404. {
  405. if (len < 6+(rx[4] | rx[5]<<8))
  406. return result;
  407. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  408. {
  409. chksum ^= rx[6+idx];
  410. }
  411. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  412. (rx[2] == tx[1]) &&
  413. (rx[1] == tx[2]) &&
  414. (rx[3] == tx[3]))
  415. {
  416. for(int idx=0;idx<((rx[4] | rx[5]<<8)>>1);idx++)
  417. Ret_Buf->speed[idx] =(rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
  418. result = PASS;
  419. }
  420. }
  421. return result;
  422. }
  423. unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
  424. {
  425. unsigned char result = FAIL;
  426. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_TEMPERATURE, 0x00, 0x00, 0x00};
  427. unsigned char rx[512];
  428. unsigned char chksum = 0x00;
  429. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  430. if(len > 6)
  431. {
  432. if (len < 6+(rx[4] | rx[5]<<8))
  433. return result;
  434. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  435. {
  436. chksum ^= rx[6+idx];
  437. }
  438. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  439. (rx[2] == tx[1]) &&
  440. (rx[1] == tx[2]) &&
  441. (rx[3] == tx[3]))
  442. {
  443. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  444. Ret_Buf->point[idx] = rx[6+idx] - 60;
  445. result = PASS;
  446. }
  447. }
  448. return result;
  449. }
  450. unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
  451. {
  452. unsigned char result = FAIL;
  453. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AUX_POWERVOLTAGE, 0x00, 0x00, 0x00};
  454. unsigned char rx[512];
  455. unsigned char chksum = 0x00;
  456. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  457. if(len > 6)
  458. {
  459. if (len < 6+(rx[4] | rx[5]<<8))
  460. return result;
  461. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  462. {
  463. chksum ^= rx[6+idx];
  464. }
  465. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  466. (rx[2] == tx[1]) &&
  467. (rx[1] == tx[2]) &&
  468. (rx[3] == tx[3]))
  469. {
  470. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  471. Ret_Buf->voltage[idx] = rx[6+idx];
  472. result = PASS;
  473. }
  474. }
  475. return result;
  476. }
  477. unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
  478. {
  479. unsigned char result = FAIL;
  480. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RELAY_OUTPUT, 0x00, 0x00, 0x00};
  481. unsigned char rx[512];
  482. unsigned char chksum = 0x00;
  483. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  484. if(len > 6)
  485. {
  486. if (len < 6+(rx[4] | rx[5]<<8))
  487. return result;
  488. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  489. {
  490. chksum ^= rx[6+idx];
  491. }
  492. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  493. (rx[2] == tx[1]) &&
  494. (rx[1] == tx[2]) &&
  495. (rx[3] == tx[3]))
  496. {
  497. for(int idx_connector=0;idx_connector<(rx[4] | rx[5]<<8);idx_connector++)
  498. {
  499. for(int idx=0;idx<8;idx++)
  500. Ret_Buf->relay_status[idx_connector][idx] = (rx[6+idx_connector]>>idx) & 0x01;
  501. }
  502. result = PASS;
  503. }
  504. }
  505. return result;
  506. }
  507. unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
  508. {
  509. unsigned char result = FAIL;
  510. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GFD_ADC, 0x00, 0x00, 0x00};
  511. unsigned char rx[512];
  512. unsigned char chksum = 0x00;
  513. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  514. if(len > 6)
  515. {
  516. if (len < 6+(rx[4] | rx[5]<<8))
  517. return result;
  518. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  519. {
  520. chksum ^= rx[6+idx];
  521. }
  522. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  523. (rx[2] == tx[1]) &&
  524. (rx[1] == tx[2]) &&
  525. (rx[3] == tx[3]))
  526. {
  527. for(int idx_connector=0;idx_connector<((rx[4] | rx[5]<<8)>>2);idx_connector++)
  528. {
  529. Ret_Buf->adc_value_positive[idx_connector] = (rx[6+(4*idx_connector)] | rx[6+(4*idx_connector)+1]<<8);
  530. Ret_Buf->adc_value_negative[idx_connector] = (rx[6+(4*idx_connector)+2] | rx[6+(4*idx_connector)+3]<<8);;
  531. }
  532. result = PASS;
  533. }
  534. }
  535. return result;
  536. }
  537. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  538. {
  539. unsigned char result = FAIL;
  540. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GPIO_INPUT, 0x00, 0x00, 0x00};
  541. unsigned char rx[512];
  542. unsigned char chksum = 0x00;
  543. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  544. if(len > 6)
  545. {
  546. if (len < 6+(rx[4] | rx[5]<<8))
  547. return result;
  548. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  549. {
  550. chksum ^= rx[6+idx];
  551. }
  552. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  553. (rx[2] == tx[1]) &&
  554. (rx[1] == tx[2]) &&
  555. (rx[3] == tx[3]))
  556. {
  557. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  558. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  559. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  560. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  561. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  562. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  563. Ret_Buf->Button[0] = (rx[6] >> 6) & 0x01;
  564. Ret_Buf->Button[1] = (rx[6] >> 7) & 0x01;
  565. Ret_Buf->Button_Emergency = (rx[7] >> 0) & 0x01;
  566. result = PASS;
  567. }
  568. }
  569. return result;
  570. }
  571. unsigned char Query_Alarm_Log(unsigned char fd, unsigned char targetAddr, Alarm_Log *Ret_Buf)
  572. {
  573. unsigned char result = FAIL;
  574. unsigned char tx[10] = {0xaa, 0x00, targetAddr, CMD_QUERY_ALARM_LOG, 0x03, 0x00, Ret_Buf->logArea, Ret_Buf->alarmIndex&0xff, ((Ret_Buf->alarmIndex>>8)&0xff), 0x00};
  575. unsigned char rx[512];
  576. unsigned char chksum = Ret_Buf->logArea ^ (Ret_Buf->alarmIndex&0xff) ^ ((Ret_Buf->alarmIndex>>8)&0xff);
  577. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  578. if(len > 6)
  579. {
  580. if (len < 6+(rx[4] | rx[5]<<8))
  581. return result;
  582. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  583. {
  584. chksum ^= rx[6+idx];
  585. }
  586. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  587. (rx[2] == tx[1]) &&
  588. (rx[1] == tx[2]) &&
  589. (rx[3] == tx[3]))
  590. {
  591. memcpy(&Ret_Buf->log[0], &rx[8], 8);
  592. result = PASS;
  593. }
  594. }
  595. return result;
  596. }
  597. unsigned char Query_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Ret_Buf)
  598. {
  599. unsigned char result = FAIL;
  600. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RTC, 0x00, 0x00, 0x00};
  601. unsigned char rx[512];
  602. unsigned char chksum = 0x00;
  603. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  604. if(len > 6)
  605. {
  606. if (len < 6+(rx[4] | rx[5]<<8))
  607. return result;
  608. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  609. {
  610. chksum ^= rx[6+idx];
  611. }
  612. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  613. (rx[2] == tx[1]) &&
  614. (rx[1] == tx[2]) &&
  615. (rx[3] == tx[3]))
  616. {
  617. Ret_Buf->year = ((rx[6]-'0')*1000) + ((rx[7]-'0')*100) + ((rx[8]-'0')*10) + ((rx[9]-'0')*1);
  618. Ret_Buf->month = ((rx[10]-'0')*10) + ((rx[11]-'0')*1);
  619. Ret_Buf->day = ((rx[12]-'0')*10) + ((rx[13]-'0')*1);
  620. Ret_Buf->hour = ((rx[14]-'0')*10) + ((rx[15]-'0')*1);
  621. Ret_Buf->min = ((rx[16]-'0')*10) + ((rx[17]-'0')*1);
  622. Ret_Buf->sec = ((rx[18]-'0')*10) + ((rx[19]-'0')*1);
  623. result = PASS;
  624. }
  625. }
  626. return result;
  627. }
  628. unsigned char Query_AC_MCU_Status(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu *Ret_Buf)
  629. {
  630. unsigned char result = FAIL;
  631. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_STATUS, 0x00, 0x00, 0x00};
  632. unsigned char rx[512];
  633. unsigned char chksum = 0x00;
  634. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  635. if(len > 6)
  636. {
  637. if (len < 6+(rx[4] | rx[5]<<8))
  638. return result;
  639. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  640. {
  641. chksum ^= rx[6+idx];
  642. }
  643. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  644. (rx[2] == tx[1]) &&
  645. (rx[1] == tx[2]) &&
  646. (rx[3] == tx[3]))
  647. {
  648. Ret_Buf->cp_state = rx[6];
  649. Ret_Buf->current_limit = rx[7] | (rx[8]<<0x08);
  650. Ret_Buf->cp_voltage_positive = (rx[9] | (rx[10]<<0x08))/100.0;
  651. Ret_Buf->cp_voltage_negtive = (rx[11] | (rx[12]<<0x08))/100.0;
  652. Ret_Buf->locker_state = rx[13];
  653. Ret_Buf->relay_state = rx[14];
  654. Ret_Buf->shutter_state = rx[15];
  655. Ret_Buf->meter_state = rx[16];
  656. Ret_Buf->pp_state = rx[17];
  657. Ret_Buf->rating_current = rx[18];
  658. Ret_Buf->rotatory_switch = (rx[19] & 0x0F);
  659. result = PASS;
  660. }
  661. }
  662. return result;
  663. }
  664. unsigned char Query_AC_MCU_Alarm(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Alarm *Ret_Buf)
  665. {
  666. unsigned char result = FAIL;
  667. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_ALARM, 0x00, 0x00, 0x00};
  668. unsigned char rx[512];
  669. unsigned char chksum = 0x00;
  670. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  671. if(len > 6)
  672. {
  673. if (len < 6+(rx[4] | rx[5]<<8))
  674. return result;
  675. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  676. {
  677. chksum ^= rx[6+idx];
  678. }
  679. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  680. (rx[2] == tx[1]) &&
  681. (rx[1] == tx[2]) &&
  682. (rx[3] == tx[3]))
  683. {
  684. //rx[6]
  685. Ret_Buf->bits.OVP = (((rx[6]>>0)&0x01)?1:0);
  686. Ret_Buf->bits.UVP = (((rx[6]>>1)&0x01)?1:0);
  687. Ret_Buf->bits.OCP = (((rx[6]>>2)&0x01)?1:0);
  688. Ret_Buf->bits.OTP = (((rx[6]>>3)&0x01)?1:0);
  689. Ret_Buf->bits.gmi_fault = (((rx[6]>>4)&0x01)?1:0);
  690. Ret_Buf->bits.cp_fault = (((rx[6]>>5)&0x01)?1:0);
  691. Ret_Buf->bits.ac_leak = (((rx[6]>>6)&0x01)?1:0);
  692. Ret_Buf->bits.dc_leak = (((rx[6]>>7)&0x01)?1:0);
  693. //rx[7]
  694. Ret_Buf->bits.mcu_selftest_fail = (((rx[7]>>0)&0x01)?1:0);
  695. Ret_Buf->bits.handshaking_timeout = (((rx[7]>>1)&0x01)?1:0);
  696. Ret_Buf->bits.emergency_stop = (((rx[7]>>2)&0x01)?1:0);
  697. Ret_Buf->bits.relay_welding = (((rx[7]>>3)&0x01)?1:0);
  698. Ret_Buf->bits.leak_module_fail = (((rx[7]>>4)&0x01)?1:0);
  699. Ret_Buf->bits.shutter_fault = (((rx[7]>>5)&0x01)?1:0);
  700. Ret_Buf->bits.locker_fault = (((rx[7]>>6)&0x01)?1:0);
  701. Ret_Buf->bits.power_drop = (((rx[7]>>7)&0x01)?1:0);
  702. //rx[8] 3~7bits reserved
  703. Ret_Buf->bits.circuit_short = (((rx[8]>>0)&0x01)?1:0);
  704. Ret_Buf->bits.set_circuit = (((rx[8]>>1)&0x01)?1:0);
  705. Ret_Buf->bits.relay_drive_fault = (((rx[8]>>2)&0x01)?1:0);
  706. //rx[9] 0~7bits reserved
  707. result = PASS;
  708. }
  709. }
  710. return result;
  711. }
  712. unsigned char Query_Present_OutputCurrent(unsigned char fd, unsigned char targetAddr, Presentoutputcurrent *Ret_Buf)
  713. {
  714. unsigned char result = FAIL;
  715. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTCURRENT, 0x00, 0x00, 0x00};
  716. unsigned char rx[512];
  717. unsigned char chksum = 0x00;
  718. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  719. if(len > 0)
  720. {
  721. if(len < 6+(rx[4] | rx[5]<<8))
  722. return result;
  723. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  724. {
  725. chksum ^= rx[6+idx];
  726. }
  727. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  728. (rx[2] == tx[1]) &&
  729. (rx[1] == tx[2]) &&
  730. (rx[3] == tx[3]))
  731. {
  732. Ret_Buf->L1N_L12[0] = (rx[6] | (rx[7]<<8))/10.0;
  733. Ret_Buf->L2N_L23[0] = (rx[8] | (rx[9]<<8))/10.0;
  734. Ret_Buf->L3N_L31[0] = (rx[10] | (rx[11]<<8))/10.0;
  735. Ret_Buf->L1N_L12[1] = (rx[12] | (rx[13]<<8))/10.0;
  736. Ret_Buf->L2N_L23[1] = (rx[14] | (rx[15]<<8))/10.0;
  737. Ret_Buf->L3N_L31[1] = (rx[16] | (rx[17]<<8))/10.0;
  738. result = PASS;
  739. }
  740. }
  741. return result;
  742. }
  743. unsigned char Query_Ble_Config(unsigned char fd, unsigned char targetAddr, Ble_Config_Data *Ret_Buf)
  744. {
  745. unsigned char result = FAIL;
  746. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CONFIG_DATA, 0x00, 0x00, 0x00};
  747. unsigned char rx[512];
  748. unsigned char chksum = 0x00;
  749. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  750. if(len > 0)
  751. {
  752. if(len < 6+(rx[4] | rx[5]<<8))
  753. return result;
  754. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  755. {
  756. chksum ^= rx[6+idx];
  757. }
  758. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  759. (rx[2] == tx[1]) &&
  760. (rx[1] == tx[2]) &&
  761. (rx[3] == tx[3]))
  762. {
  763. Ret_Buf->isLogin = (rx[6]?0x01:0x00);
  764. Ret_Buf->isRequestStart = (rx[7]?0x01:0x00);
  765. Ret_Buf->isRequestStop = (rx[8]?0x01:0x00);
  766. result = PASS;
  767. }
  768. }
  769. return result;
  770. }
  771. unsigned char Query_Ble_Central_ID(unsigned char fd, unsigned char targetAddr, Ble_Login_Central_Id *Ret_Buf)
  772. {
  773. unsigned char result = FAIL;
  774. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CENTRAL_ID, 0x00, 0x00, 0x00};
  775. unsigned char rx[512];
  776. unsigned char chksum = 0x00;
  777. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  778. if(len > 0)
  779. {
  780. if(len < 6+(rx[4] | rx[5]<<8))
  781. return result;
  782. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  783. {
  784. chksum ^= rx[6+idx];
  785. }
  786. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  787. (rx[2] == tx[1]) &&
  788. (rx[1] == tx[2]) &&
  789. (rx[3] == tx[3]))
  790. {
  791. memset(Ret_Buf->id, 0x00, ARRAY_SIZE(Ret_Buf->id));
  792. memcpy(&Ret_Buf->id[0], &rx[6], (rx[4] | (rx[5]<<8)));
  793. result = PASS;
  794. }
  795. }
  796. return result;
  797. }
  798. unsigned char Query_Power_Consumption(unsigned char fd, unsigned char targetAddr, Power_Consumption *Ret_Buf)
  799. {
  800. unsigned char result = FAIL;
  801. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_POWER_CONSUMPTION, 0x00, 0x00, 0x00};
  802. unsigned char rx[512];
  803. unsigned char chksum = 0x00;
  804. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  805. if(len > 0)
  806. {
  807. if(len < 6+(rx[4] | rx[5]<<8))
  808. return result;
  809. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  810. {
  811. chksum ^= rx[6+idx];
  812. }
  813. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  814. (rx[2] == tx[1]) &&
  815. (rx[1] == tx[2]) &&
  816. (rx[3] == tx[3]))
  817. {
  818. #ifndef SIMULATION
  819. Ret_Buf-> power_consumption = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
  820. #else
  821. tsNow = current_timestamp();
  822. tmpPowerConsumption += (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrent)*((tsNow-tsLast)/36000000.0);
  823. Ret_Buf-> power_consumption = (uint32_t)tmpPowerConsumption;
  824. tsLast = tsNow;
  825. #endif
  826. result = PASS;
  827. }
  828. }
  829. return result;
  830. }
  831. unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
  832. {
  833. unsigned char result = FAIL;
  834. unsigned char tx[15] = {0xaa, 0x00, targetAddr, CMD_CONFIG_FAN_SPEED, 0x08, 0x00, Set_Buf->speed[0]&0xff, (Set_Buf->speed[0]>>8)&0xff, Set_Buf->speed[1]&0xff, (Set_Buf->speed[1]>>8)&0xff, Set_Buf->speed[2]&0xff, (Set_Buf->speed[2]>>8)&0xff, Set_Buf->speed[3]&0xff, (Set_Buf->speed[3]>>8)&0xff, 0x00};
  835. unsigned char rx[512];
  836. unsigned char chksum = 0x00;
  837. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  838. chksum ^= tx[6+idx];
  839. tx[14] = chksum;
  840. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  841. if(len > 6)
  842. {
  843. if (len < 6+(rx[4] | rx[5]<<8))
  844. return result;
  845. chksum = 0x00;
  846. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  847. {
  848. chksum ^= rx[6+idx];
  849. }
  850. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  851. (rx[2] == tx[1]) &&
  852. (rx[1] == tx[2]) &&
  853. (rx[3] == tx[3]) &&
  854. (rx[6] == 0x01))
  855. {
  856. result = PASS;
  857. }
  858. }
  859. return result;
  860. }
  861. unsigned char Config_Serial_Number(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  862. {
  863. unsigned char result = FAIL;
  864. unsigned char tx[27] = {0xaa, 0x00, targetAddr, CMD_CONFIG_SERIAL_NUMBER, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  865. unsigned char rx[512];
  866. unsigned char chksum = 0x00;
  867. memcpy(&tx[14], &Set_Buf->serial_number[0], ARRAY_SIZE(Set_Buf->serial_number));
  868. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  869. chksum ^= tx[6+idx];
  870. tx[26] = chksum;
  871. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  872. if(len > 6)
  873. {
  874. if (len < 6+(rx[4] | rx[5]<<8))
  875. return result;
  876. chksum = 0x00;
  877. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  878. {
  879. chksum ^= rx[6+idx];
  880. }
  881. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  882. (rx[2] == tx[1]) &&
  883. (rx[1] == tx[2]) &&
  884. (rx[3] == tx[3]) &&
  885. (rx[6] == 0x01))
  886. {
  887. result = PASS;
  888. }
  889. }
  890. return result;
  891. }
  892. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  893. {
  894. unsigned char result = FAIL;
  895. unsigned char tx[21] = {0xaa, 0x00, targetAddr, CMD_CONFIG_MODEL_NAME, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  896. unsigned char rx[512];
  897. unsigned char chksum = 0x00;
  898. memcpy(&tx[6], &Set_Buf->model_name[0], ARRAY_SIZE(Set_Buf->model_name));
  899. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  900. chksum ^= tx[6+idx];
  901. tx[20] = chksum;
  902. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  903. if(len > 6)
  904. {
  905. if (len < 6+(rx[4] | rx[5]<<8))
  906. return result;
  907. chksum = 0x00;
  908. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  909. {
  910. chksum ^= rx[6+idx];
  911. }
  912. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  913. (rx[2] == tx[1]) &&
  914. (rx[1] == tx[2]) &&
  915. (rx[3] == tx[3]) &&
  916. (rx[6] == 0x01))
  917. {
  918. result = PASS;
  919. }
  920. }
  921. return result;
  922. }
  923. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  924. {
  925. unsigned char result = FAIL;
  926. unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_RELAY_OUTPUT, 0x02, 0x00, 0x00, 0x00, 0x00};
  927. unsigned char rx[512];
  928. unsigned char chksum = 0x00;
  929. for(int idx_connector=0;idx_connector<2;idx_connector++)
  930. for(int idx = 0;idx<8;idx++)
  931. tx[6+idx_connector] |= ((Set_Buf->relay_status[idx_connector][idx]?0x01:0x00)<<idx);
  932. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  933. chksum ^= tx[6+idx];
  934. tx[8] = chksum;
  935. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  936. if(len > 6)
  937. {
  938. if (len < 6+(rx[4] | rx[5]<<8))
  939. return result;
  940. chksum = 0x00;
  941. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  942. {
  943. chksum ^= rx[6+idx];
  944. }
  945. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  946. (rx[2] == tx[1]) &&
  947. (rx[1] == tx[2]) &&
  948. (rx[3] == tx[3]) &&
  949. (rx[6] == 0x01))
  950. {
  951. result = PASS;
  952. }
  953. }
  954. return result;
  955. }
  956. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  957. {
  958. unsigned char result = FAIL;
  959. unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GPIO_OUTPUT, 0x01, 0x00, 0x00, 0x00};
  960. unsigned char rx[512];
  961. unsigned char chksum = 0x00;
  962. tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
  963. for(int idx=0;idx<2;idx++)
  964. tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
  965. for(int idx=0;idx<4;idx++)
  966. tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
  967. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  968. chksum ^= tx[6+idx];
  969. tx[14] = chksum;
  970. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  971. if(len > 6)
  972. {
  973. if (len < 6+(rx[4] | rx[5]<<8))
  974. return result;
  975. chksum = 0x00;
  976. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  977. {
  978. chksum ^= rx[6+idx];
  979. }
  980. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  981. (rx[2] == tx[1]) &&
  982. (rx[1] == tx[2]) &&
  983. (rx[3] == tx[3]) &&
  984. (rx[6] == tx[6]))
  985. {
  986. result = PASS;
  987. }
  988. }
  989. return result;
  990. }
  991. unsigned char Config_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  992. {
  993. unsigned char result = FAIL;
  994. unsigned char tx[21];
  995. unsigned char rx[512];
  996. unsigned char chksum = 0x00;
  997. tx[0] = 0xaa;
  998. tx[1] = 0x00;
  999. tx[2] = targetAddr;
  1000. tx[3] = CMD_CONFIG_RTC;
  1001. tx[4] = 0x0e;
  1002. tx[5] = 0x00;
  1003. tx[6] = ((Set_Buf->year)/1000)+'0';
  1004. tx[7] = ((Set_Buf->year)/100%10)+'0';
  1005. tx[8] = ((Set_Buf->year)/10%10)+'0';
  1006. tx[9] = ((Set_Buf->year)%10)+'0';
  1007. tx[10] = ((Set_Buf->month)/10)+'0';
  1008. tx[11] = ((Set_Buf->month)%10)+'0';
  1009. tx[12] = ((Set_Buf->day)/10)+'0';
  1010. tx[13] = ((Set_Buf->day)%10)+'0';
  1011. tx[14] = ((Set_Buf->hour)/10)+'0';
  1012. tx[15] = ((Set_Buf->hour)%10)+'0';
  1013. tx[16] = ((Set_Buf->min)/10)+'0';
  1014. tx[17] = ((Set_Buf->min)%10)+'0';
  1015. tx[18] = ((Set_Buf->sec)/10)+'0';
  1016. tx[19] = ((Set_Buf->sec)%10)+'0';
  1017. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1018. chksum ^= tx[6+idx];
  1019. tx[20] = chksum;
  1020. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1021. if(len > 6)
  1022. {
  1023. if (len < 6+(rx[4] | rx[5]<<8))
  1024. return result;
  1025. chksum = 0x00;
  1026. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1027. {
  1028. chksum ^= rx[6+idx];
  1029. }
  1030. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1031. (rx[2] == tx[1]) &&
  1032. (rx[1] == tx[2]) &&
  1033. (rx[3] == tx[3]) &&
  1034. (rx[6] == 0x01))
  1035. {
  1036. result = PASS;
  1037. }
  1038. }
  1039. return result;
  1040. }
  1041. unsigned char Config_AC_MCU_LED(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Led *Set_Buf)
  1042. {
  1043. unsigned char result = FAIL;
  1044. unsigned char tx[12] ;
  1045. unsigned char rx[512];
  1046. unsigned char chksum = 0x00;
  1047. tx[0] = 0xaa;
  1048. tx[1] = 0x00;
  1049. tx[2] = targetAddr;
  1050. tx[3] = CMD_CONFIG_AC_LED;
  1051. tx[4] = 0x05;
  1052. tx[5] = 0x00;
  1053. tx[6] = Set_Buf->mode;
  1054. tx[7] = ((Set_Buf->alarm_code>>0)&0xff);
  1055. tx[8] = ((Set_Buf->alarm_code>>8)&0xff);
  1056. tx[9] = ((Set_Buf->alarm_code>>16)&0xff);
  1057. tx[10] = ((Set_Buf->alarm_code>>24)&0xff);
  1058. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1059. chksum ^= tx[6+idx];
  1060. tx[11] = chksum;
  1061. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1062. if(len > 6)
  1063. {
  1064. if (len < 6+(rx[4] | rx[5]<<8))
  1065. return result;
  1066. chksum = 0x00;
  1067. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1068. {
  1069. chksum ^= rx[6+idx];
  1070. }
  1071. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1072. (rx[2] == tx[1]) &&
  1073. (rx[1] == tx[2]) &&
  1074. (rx[3] == tx[3]) &&
  1075. (rx[6] == 0x01))
  1076. {
  1077. result = PASS;
  1078. }
  1079. }
  1080. return result;
  1081. }
  1082. unsigned char Config_AC_MCU_LEGACY_REQUEST(unsigned char fd, unsigned char targetAddr, Legacy_Request *Set_Buf)
  1083. {
  1084. unsigned char result = FAIL;
  1085. unsigned char tx[9];
  1086. unsigned char rx[512];
  1087. unsigned char chksum = 0x00;
  1088. tx[0] = 0xaa;
  1089. tx[1] = 0x00;
  1090. tx[2] = targetAddr;
  1091. tx[3] = CMD_CONFIG_LEGACY_REQUEST;
  1092. tx[4] = 0x02;
  1093. tx[5] = 0x00;
  1094. tx[6] = Set_Buf->isLegacyRequest;
  1095. tx[7] = 0x00;
  1096. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1097. chksum ^= tx[6+idx];
  1098. tx[8] = chksum;
  1099. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1100. if(len > 6)
  1101. {
  1102. if (len < 6+(rx[4] | rx[5]<<8))
  1103. return result;
  1104. chksum = 0x00;
  1105. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1106. {
  1107. chksum ^= rx[6+idx];
  1108. }
  1109. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1110. (rx[2] == tx[1]) &&
  1111. (rx[1] == tx[2]) &&
  1112. (rx[3] == tx[3]) &&
  1113. (rx[6] == 0x01))
  1114. {
  1115. result = PASS;
  1116. }
  1117. }
  1118. return result;
  1119. }
  1120. unsigned char Config_AC_MCU_RESET_REQUEST(unsigned char fd, unsigned char targetAddr, Mcu_Reset_Request *Set_Buf)
  1121. {
  1122. unsigned char result = FAIL;
  1123. unsigned char tx[9];
  1124. unsigned char rx[512];
  1125. unsigned char chksum = 0x00;
  1126. tx[0] = 0xaa;
  1127. tx[1] = 0x00;
  1128. tx[2] = targetAddr;
  1129. tx[3] = CMD_CONFIG_MCU_RESET_REQUEST;
  1130. tx[4] = 0x02;
  1131. tx[5] = 0x00;
  1132. tx[6] = Set_Buf->isMcuResetRequest;
  1133. tx[7] = 0x00;
  1134. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1135. chksum ^= tx[6+idx];
  1136. tx[8] = chksum;
  1137. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1138. if(len > 6)
  1139. {
  1140. if (len < 6+(rx[4] | rx[5]<<8))
  1141. return result;
  1142. chksum = 0x00;
  1143. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1144. {
  1145. chksum ^= rx[6+idx];
  1146. }
  1147. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1148. (rx[2] == tx[1]) &&
  1149. (rx[1] == tx[2]) &&
  1150. (rx[3] == tx[3]) &&
  1151. (rx[6] == 0x01))
  1152. {
  1153. result = PASS;
  1154. }
  1155. }
  1156. return result;
  1157. }
  1158. unsigned char Query_AC_GUN_PLUGIN_TIMES(unsigned char fd, unsigned char targetAddr, Gun_Plugin_Times *Ret_Buf)
  1159. {
  1160. unsigned char result = FAIL;
  1161. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GUN_PLUGIN_TIMES, 0x00, 0x00, 0x00};
  1162. unsigned char rx[512];
  1163. unsigned char chksum = 0x00;
  1164. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1165. if(len > 6)
  1166. {
  1167. if(len < 6+(rx[4] | rx[5]<<8))
  1168. return result;
  1169. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1170. {
  1171. chksum ^= rx[6+idx];
  1172. }
  1173. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1174. (rx[2] == tx[1]) &&
  1175. (rx[1] == tx[2]) &&
  1176. (rx[3] == tx[3]))
  1177. {
  1178. Ret_Buf-> GunPluginTimes = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
  1179. result = PASS;
  1180. }
  1181. }
  1182. return result;
  1183. }
  1184. unsigned char Config_AC_MaxCurrent_And_CpPwmDuty(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Cp_Pwm_Duty*Set_Buf)
  1185. {
  1186. unsigned char result = FAIL;
  1187. unsigned char tx[8];
  1188. unsigned char rx[512];
  1189. unsigned char chksum = 0x00;
  1190. tx[0] = 0xaa;
  1191. tx[1] = 0x00;
  1192. tx[2] = targetAddr;
  1193. tx[3] = CMD_CONFIG_CURRENT_LINIT;
  1194. tx[4] = 0x01;
  1195. tx[5] = 0x00;
  1196. tx[6] = Set_Buf->max_current;
  1197. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1198. chksum ^= tx[6+idx];
  1199. tx[7] = chksum;
  1200. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1201. if(len > 6)
  1202. {
  1203. if(len < 6+(rx[4] | rx[5]<<8))
  1204. return result;
  1205. chksum = 0x00;
  1206. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1207. {
  1208. chksum ^= rx[6+idx];
  1209. }
  1210. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1211. (rx[2] == tx[1]) &&
  1212. (rx[1] == tx[2]) &&
  1213. (rx[3] == tx[3]) &&
  1214. (rx[6] == 0x01))
  1215. {
  1216. result = PASS;
  1217. }
  1218. }
  1219. return result;
  1220. }
  1221. unsigned char Config_AC_Set_Breathe_Led_Timing(unsigned char fd, unsigned char targetAddr,Set_Breathe_Led_Timing*Set_Buf)
  1222. {
  1223. unsigned char result = FAIL;
  1224. unsigned char tx[19];
  1225. unsigned char rx[512];
  1226. unsigned char chksum = 0x00;
  1227. tx[0] = 0xaa;
  1228. tx[1] = 0x00;
  1229. tx[2] = targetAddr;
  1230. tx[3] = CMD_CONFIG_MCU_SET_BREATHE_LED_TIMING;
  1231. tx[4] = 0x0C;
  1232. tx[5] = 0x00;
  1233. // Increase LED_ACTION_CONNECTED
  1234. tx[6] = (Set_Buf->set_Led_Action_Connected_Fade_In & 0xff);
  1235. tx[7] = (Set_Buf->set_Led_Action_Connected_Fade_In >> 8);
  1236. // Decrease LED_ACTION_CONNECTED
  1237. tx[8] = (Set_Buf->set_Led_Action_Connected_Fade_Out & 0xff);
  1238. tx[9] = (Set_Buf->set_Led_Action_Connected_Fade_Out >> 8);
  1239. // Increase LED_ACTION_AUTHED
  1240. tx[10] = (Set_Buf->set_Led_Action_Authed_Fade_In & 0xff);
  1241. tx[11] = (Set_Buf->set_Led_Action_Authed_Fade_In >> 8);
  1242. // Decrease LED_ACTION_AUTHED
  1243. tx[12] = (Set_Buf->set_Led_Action_Authed_Fade_Out & 0xff);
  1244. tx[13] = (Set_Buf->set_Led_Action_Authed_Fade_Out >> 8);
  1245. // Increase_LED_ACTION_CHARGING
  1246. tx[14] = (Set_Buf->Set_Led_Action_Chaging_Fade_In & 0xff);
  1247. tx[15] = (Set_Buf->Set_Led_Action_Chaging_Fade_In >> 8);
  1248. // Decrease_LED_ACTION_CHARGING
  1249. tx[16] = (Set_Buf->set_Led_Action_Chaging_Fade_Out & 0xff);
  1250. tx[17] = (Set_Buf->set_Led_Action_Chaging_Fade_Out >> 8);
  1251. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1252. chksum ^= tx[6+idx];
  1253. tx[18] = chksum;
  1254. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1255. if(len > 6)
  1256. {
  1257. if(len < 6+(rx[4] | rx[5]<<8))
  1258. return result;
  1259. chksum = 0x00;
  1260. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1261. {
  1262. chksum ^= rx[6+idx];
  1263. }
  1264. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1265. (rx[2] == tx[1]) &&
  1266. (rx[1] == tx[2]) &&
  1267. (rx[3] == tx[3]) &&
  1268. (rx[6] == 0x01))
  1269. {
  1270. result = PASS;
  1271. }
  1272. }
  1273. return result;
  1274. }
  1275. unsigned char Config_AC_Set_Led_Brightness(unsigned char fd, unsigned char targetAddr,Set_Led_Brightness*Set_Buf)
  1276. {
  1277. unsigned char result = FAIL;
  1278. unsigned char tx[19];
  1279. unsigned char rx[512];
  1280. unsigned char chksum = 0x00;
  1281. tx[0] = 0xaa;
  1282. tx[1] = 0x00;
  1283. tx[2] = targetAddr;
  1284. tx[3] = CMD_CONFIG_MCU_SET_LED_BRIGHTNESS;
  1285. tx[4] = 0x0C;
  1286. tx[5] = 0x00;
  1287. tx[6] = Set_Buf-> sector_1; // 0~1 AM and 1~2 AM
  1288. tx[7] = Set_Buf-> sector_2; // 2~3 AM and 3~4 AM
  1289. tx[8] = Set_Buf-> sector_3; // 4~5 AM and 5~6 AM
  1290. tx[9] = Set_Buf-> sector_4; // 6~7 AM and 7~8 AM
  1291. tx[10] = Set_Buf-> sector_5; // 8~9 AM and 9~10 AM
  1292. tx[11] = Set_Buf-> sector_6; // 10~11 AM and 11~12 AM
  1293. tx[12] = Set_Buf-> sector_7; // 12~13 PM and 13~14 PM
  1294. tx[13] = Set_Buf-> sector_8; // 14~15 PM and 15~16 PM
  1295. tx[14] = Set_Buf-> sector_9; // 16~17 PM and 17~18 PM
  1296. tx[15] = Set_Buf-> sector_10; // 18~19 PM and 19~20 PM
  1297. tx[16] = Set_Buf-> sector_11; // 20~21 PM and 21~22 PM
  1298. tx[17] = Set_Buf-> sector_12; // 22~23 PM and 23~24 PM
  1299. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1300. chksum ^= tx[6+idx];
  1301. tx[18] = chksum;
  1302. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1303. if(len > 6)
  1304. {
  1305. if(len < 6+(rx[4] | rx[5]<<8))
  1306. return result;
  1307. chksum = 0x00;
  1308. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1309. {
  1310. chksum ^= rx[6+idx];
  1311. }
  1312. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1313. (rx[2] == tx[1]) &&
  1314. (rx[1] == tx[2]) &&
  1315. (rx[3] == tx[3]) &&
  1316. (rx[6] == 0x01))
  1317. {
  1318. result = PASS;
  1319. }
  1320. }
  1321. return result;
  1322. }
  1323. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  1324. {
  1325. unsigned char result = FAIL;
  1326. unsigned char tx[11] = {0xaa, 0x00, targetAddr, CMD_UPDATE_START, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
  1327. unsigned char rx[512];
  1328. unsigned char chksum = 0x00;
  1329. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1330. chksum ^= tx[6+idx];
  1331. tx[10] = chksum;
  1332. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1333. if(len > 6)
  1334. {
  1335. if (len < 6+(rx[4] | rx[5]<<8))
  1336. return result;
  1337. chksum = 0x00;
  1338. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1339. {
  1340. chksum ^= rx[6+idx];
  1341. }
  1342. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1343. (rx[2] == tx[1]) &&
  1344. (rx[1] == tx[2]) &&
  1345. (rx[3] == tx[3]) &&
  1346. (rx[6] == 0x00))
  1347. {
  1348. result = PASS;
  1349. }
  1350. }
  1351. return result;
  1352. }
  1353. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  1354. {
  1355. unsigned char result = FAIL;
  1356. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_ABOARD, 0x04, 0x00, 0x00};
  1357. unsigned char rx[512];
  1358. unsigned char chksum = 0x00;
  1359. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1360. if(len > 6)
  1361. {
  1362. if (len < 6+(rx[4] | rx[5]<<8))
  1363. return result;
  1364. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1365. {
  1366. chksum ^= rx[6+idx];
  1367. }
  1368. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1369. (rx[2] == tx[1]) &&
  1370. (rx[1] == tx[2]) &&
  1371. (rx[3] == tx[3]) &&
  1372. (rx[6] == 0x00))
  1373. {
  1374. result = PASS;
  1375. }
  1376. }
  1377. return result;
  1378. }
  1379. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  1380. {
  1381. unsigned char result = FAIL;
  1382. unsigned char tx[11 + length];
  1383. unsigned char rx[512];
  1384. unsigned char chksum = 0x00;
  1385. tx[0] = 0xaa;
  1386. tx[1] = 0x00;
  1387. tx[2] = targetAddr;
  1388. tx[3] = CMD_UPDATE_TRANSFER;
  1389. tx[4] = (4 + length) & 0xff;
  1390. tx[5] = ((4 + length)>>8) & 0xff;
  1391. tx[6] = (startAddr>>0) & 0xff;
  1392. tx[7] = (startAddr>>8) & 0xff;
  1393. tx[8] = (startAddr>>16) & 0xff;
  1394. tx[9] = (startAddr>>24) & 0xff;
  1395. memcpy(tx+10, data, length);
  1396. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1397. chksum ^= tx[6+idx];
  1398. tx[sizeof(tx)-1] = chksum;
  1399. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1400. if(len > 6)
  1401. {
  1402. if (len < 6+(rx[4] | rx[5]<<8))
  1403. return result;
  1404. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1405. {
  1406. chksum ^= rx[6+idx];
  1407. }
  1408. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1409. (rx[2] == tx[1]) &&
  1410. (rx[1] == tx[2]) &&
  1411. (rx[3] == tx[3]) &&
  1412. (rx[6] == 0x00))
  1413. {
  1414. result = PASS;
  1415. }
  1416. }
  1417. return result;
  1418. }
  1419. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  1420. {
  1421. unsigned char result = FAIL;
  1422. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_END, 0x04, 0x00, 0x00};
  1423. unsigned char rx[512];
  1424. unsigned char chksum = 0x00;
  1425. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1426. if(len > 6)
  1427. {
  1428. if (len < 6+(rx[4] | rx[5]<<8))
  1429. return result;
  1430. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1431. {
  1432. chksum ^= rx[6+idx];
  1433. }
  1434. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1435. (rx[2] == tx[1]) &&
  1436. (rx[1] == tx[2]) &&
  1437. (rx[3] == tx[3]) &&
  1438. (rx[6] == 0x01))
  1439. {
  1440. result = PASS;
  1441. }
  1442. }
  1443. return result;
  1444. }
  1445. //================================================
  1446. // Main process
  1447. //================================================
  1448. int main(void)
  1449. {
  1450. int Uart1Fd;
  1451. unsigned short int failCount[2] = {0,0};
  1452. unsigned short int printOutCount[2] = {0,0};
  1453. if(InitShareMemory() == FAIL)
  1454. {
  1455. #ifdef SystemLogMessage
  1456. DEBUG_ERROR("InitShareMemory NG\n");
  1457. #endif
  1458. if(ShmStatusCodeData!=NULL)
  1459. {
  1460. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1461. }
  1462. sleep(5);
  1463. return 0;
  1464. }
  1465. else
  1466. {
  1467. DEBUG_INFO("InitShareMemory OK.\n");
  1468. }
  1469. Uart1Fd=InitComPort();
  1470. if(Uart1Fd<0)
  1471. {
  1472. DEBUG_ERROR("InitComPort NG\n");
  1473. if(ShmStatusCodeData!=NULL)
  1474. {
  1475. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1476. }
  1477. sleep(5);
  1478. return 0;
  1479. }
  1480. else
  1481. {
  1482. DEBUG_INFO("ttyS1 port open success.\n");
  1483. }
  1484. for(;;)
  1485. {
  1486. for(int gun_index=0;gun_index<AC_QUANTITY;gun_index++)
  1487. {
  1488. /*
  1489. * Polling loop
  1490. */
  1491. if((stepIndex%2)==0)
  1492. {
  1493. /*
  1494. * High priority communication
  1495. */
  1496. //===============================
  1497. // Case 1 : Config primary MCU LED
  1498. //===============================
  1499. ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code = ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode;
  1500. if(Config_AC_MCU_LED(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuLed))
  1501. {
  1502. failCount[gun_index] = 0;
  1503. }
  1504. else
  1505. {
  1506. DEBUG_WARN("MCU-%d set led fail...%d\n", gun_index, failCount[gun_index]);
  1507. if(failCount[gun_index]<1000)
  1508. {
  1509. failCount[gun_index]++;
  1510. }
  1511. }
  1512. //===============================
  1513. // Case 2 : Config primary Legacy request
  1514. //===============================
  1515. if(Config_AC_MCU_LEGACY_REQUEST(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1),&ShmCharger->gun_info[gun_index].legacyRequest))
  1516. {
  1517. failCount[gun_index] = 0;
  1518. }
  1519. else
  1520. {
  1521. DEBUG_WARN("MCU-%d set relay request fail...%d\n", gun_index, failCount[gun_index]);
  1522. if(failCount[gun_index]<1000)
  1523. {
  1524. failCount[gun_index]++;
  1525. }
  1526. }
  1527. //===============================
  1528. // Case 3 : Query primary MCU status
  1529. //===============================
  1530. if(Query_AC_MCU_Status(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuState))
  1531. {
  1532. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState = ShmCharger->gun_info[gun_index].primaryMcuState.cp_state;
  1533. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotDuty = (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit>51?(unsigned char)((ShmCharger->gun_info[gun_index].primaryMcuState.current_limit/2.5)+64):(unsigned char)(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit/0.6));
  1534. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotVoltage = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  1535. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  1536. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive;
  1537. failCount[gun_index] = 0;
  1538. }
  1539. else
  1540. {
  1541. DEBUG_WARN("MCU-%d get status fail...%d\n", gun_index, failCount[gun_index]);
  1542. if(failCount[gun_index]<1000)
  1543. {
  1544. failCount[gun_index]++;
  1545. }
  1546. }
  1547. //===============================
  1548. // Case 4 : Query primary MCU Alarm code
  1549. //===============================
  1550. if(Query_AC_MCU_Alarm(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuAlarm))
  1551. {
  1552. //DEBUG_INFO("MCU-%d get alarm code success.\n",gun_index);
  1553. //================================================
  1554. // Byte[6]
  1555. // Byte[7]
  1556. // Byte[8] Alarm code 3~7 bits Reserved
  1557. // Byte[9] Alarm code 0~7 bits Reserved
  1558. //================================================
  1559. //================================================
  1560. // Byte[6]
  1561. //================================================
  1562. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP == 0x01)
  1563. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<0;
  1564. else
  1565. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<0);
  1566. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP == 0x01)
  1567. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<1;
  1568. else
  1569. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<1);
  1570. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP == 0x01)
  1571. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<2;
  1572. else
  1573. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<2);
  1574. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP == 0x01)
  1575. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<3;
  1576. else
  1577. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<3);
  1578. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault == 0x01)
  1579. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<4;
  1580. else
  1581. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<4);
  1582. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault == 0x01)
  1583. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<5;
  1584. else
  1585. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<5);
  1586. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak == 0x01)
  1587. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<6;
  1588. else
  1589. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<6);
  1590. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak == 0x01)
  1591. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<7;
  1592. else
  1593. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<7);
  1594. //================================================
  1595. // Byte[7]
  1596. //================================================
  1597. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail == 0x01)
  1598. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<8;
  1599. else
  1600. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<8);
  1601. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout == 0x01)
  1602. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<9;
  1603. else
  1604. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<9);
  1605. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop == 0x01)
  1606. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<10;
  1607. else
  1608. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<10);
  1609. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding == 0x01)
  1610. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<11;
  1611. else
  1612. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<11);
  1613. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail == 0x01)
  1614. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<12;
  1615. else
  1616. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<12);
  1617. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault == 0x01)
  1618. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<13;
  1619. else
  1620. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<13);
  1621. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault == 0x01)
  1622. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<14;
  1623. else
  1624. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<14);
  1625. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop == 0x01)
  1626. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<15;
  1627. else
  1628. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<15);
  1629. //================================================
  1630. // Byte[8]
  1631. //================================================
  1632. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.circuit_short == 0x01)
  1633. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<16;
  1634. else
  1635. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<16);
  1636. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.set_circuit == 0x01)
  1637. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<17;
  1638. else
  1639. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<17);
  1640. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault == 0x01)
  1641. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<18;
  1642. else
  1643. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<18);
  1644. //================================================
  1645. // Byte[9] Reserved
  1646. //================================================
  1647. failCount[gun_index] = 0;
  1648. }
  1649. else
  1650. {
  1651. DEBUG_WARN("MCU-%d get alarm fail...%d\n", gun_index, failCount[gun_index]);
  1652. if(failCount[gun_index]<1000)
  1653. {
  1654. failCount[gun_index]++;
  1655. }
  1656. }
  1657. //===============================
  1658. // Case 5 : Query primary MCU BLE config
  1659. //===============================
  1660. if(Query_Ble_Config(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleConfigData))
  1661. {
  1662. ShmSysConfigAndInfo->SysConfig.Bluetooth.isLogin = ShmCharger->gun_info[gun_index].bleConfigData.isLogin;
  1663. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStart = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart;
  1664. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStop = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop;
  1665. failCount[gun_index] = 0;
  1666. }
  1667. else
  1668. {
  1669. DEBUG_WARN("MCU-%d get ble config fail...%d\n", gun_index, failCount[gun_index]);
  1670. if(failCount[gun_index]<1000)
  1671. {
  1672. failCount[gun_index]++;
  1673. }
  1674. }
  1675. //===============================
  1676. // Case 6 : Query primary MCU ble login id
  1677. //===============================
  1678. if(Query_Ble_Central_ID(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleLoginCentralId))
  1679. {
  1680. memcpy(ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, sizeof ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID);
  1681. failCount[gun_index] = 0;
  1682. }
  1683. else
  1684. {
  1685. DEBUG_WARN("MCU-%d get ble login central id fail...%d\n", gun_index, failCount[gun_index]);
  1686. if(failCount[gun_index]<1000)
  1687. {
  1688. failCount[gun_index]++;
  1689. }
  1690. }
  1691. //===============================
  1692. // Case 7 : Config primary MCU reset request
  1693. //===============================
  1694. if((access("/sys/class/gpio/gpio116/value", F_OK)) != -1)
  1695. {
  1696. if(ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest == ON)
  1697. {
  1698. if(Config_AC_MCU_RESET_REQUEST(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].mcuResetRequest) == PASS)
  1699. {
  1700. DEBUG_INFO("**************************************************\n");
  1701. DEBUG_INFO("********* High priority polling : Case 7 *********\n");
  1702. DEBUG_INFO("**************************************************\n");
  1703. DEBUG_INFO("MCU-%d set MCU reset Request : %d\n", gun_index, ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest);
  1704. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = OFF;
  1705. failCount[gun_index] = 0;
  1706. }
  1707. else
  1708. {
  1709. DEBUG_WARN("MCU-%d get MCU reset fail...%d\n", gun_index, failCount[gun_index]);
  1710. if(failCount[gun_index]<1000)
  1711. {
  1712. failCount[gun_index]++;
  1713. }
  1714. }
  1715. }
  1716. }
  1717. //===============================
  1718. // Case 8 : Config primary set CP PWN duty
  1719. //===============================
  1720. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == ON)
  1721. {
  1722. if(Config_AC_MaxCurrent_And_CpPwmDuty(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty))
  1723. {
  1724. failCount[gun_index] = 0;
  1725. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = OFF;
  1726. }
  1727. else
  1728. {
  1729. DEBUG_WARN("MCU-%d set cp pwn duty fail...%d\n", gun_index, failCount[gun_index]);
  1730. if(failCount[gun_index]<1000)
  1731. {
  1732. failCount[gun_index]++;
  1733. }
  1734. }
  1735. }
  1736. //==========================================================
  1737. // High priority polling log print out
  1738. //==========================================================
  1739. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  1740. {
  1741. PRINT_OUT_LEVEL_STATE = LEVEL_1;
  1742. if(printOutCount[gun_index]>LEVEL_1)
  1743. printOutCount[gun_index] = 0;
  1744. }
  1745. else
  1746. {
  1747. PRINT_OUT_LEVEL_STATE = LEVEL_2;
  1748. if(printOutCount[gun_index]>LEVEL_2)
  1749. printOutCount[gun_index] = 0;
  1750. }
  1751. if(printOutCount[gun_index] == PRINT_OUT_LEVEL_STATE)
  1752. {
  1753. //===============================
  1754. // Config primary MCU LED
  1755. //===============================
  1756. DEBUG_INFO("**************************************************\n");
  1757. DEBUG_INFO("********* High priority polling : Case 1 *********\n");
  1758. DEBUG_INFO("**************************************************\n");
  1759. DEBUG_INFO("MCU-%d set Led mode : %d\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.mode);
  1760. DEBUG_INFO("MCU-%d set Alarm code : %x\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code);
  1761. //===============================
  1762. // Config primary Legacy request
  1763. //===============================
  1764. DEBUG_INFO("**************************************************\n");
  1765. DEBUG_INFO("********* High priority polling : Case 2 *********\n");
  1766. DEBUG_INFO("**************************************************\n");
  1767. DEBUG_INFO("MCU-%d set relay request : %d\n", gun_index, ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest);
  1768. //===============================
  1769. // Query primary MCU status
  1770. //===============================
  1771. DEBUG_INFO("**************************************************\n");
  1772. DEBUG_INFO("********* High priority polling : Case 3 *********\n");
  1773. DEBUG_INFO("**************************************************\n");
  1774. DEBUG_INFO("MCU-%d get Pilot State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_state);
  1775. DEBUG_INFO("MCU-%d get Pilot Duty : %.2f\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  1776. DEBUG_INFO("MCU-%d get Pilot Voltage Positive : %.2f\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive);
  1777. DEBUG_INFO("MCU-%d get Pilot Voltage Negative : %.2f\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive);
  1778. DEBUG_INFO("MCU-%d get Relay State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relay_state);
  1779. DEBUG_INFO("MCU-%d get Rating Current : %.2f\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.rating_current);
  1780. DEBUG_INFO("MCU-%d get Rotary switch : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch);
  1781. /*
  1782. DEBUG_INFO("MCU-%d get Locker State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.locker_state);
  1783. DEBUG_INFO("MCU-%d get Shutter State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.shutter_state);
  1784. DEBUG_INFO("MCU-%d get Meter State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.meter_state);
  1785. DEBUG_INFO("MCU-%d get PP State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.pp_state);
  1786. */
  1787. //===============================
  1788. // Query primary MCU Alarm code
  1789. //===============================
  1790. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  1791. {
  1792. DEBUG_INFO("**************************************************\n");
  1793. DEBUG_INFO("********* High priority polling : Case 4 *********\n");
  1794. DEBUG_INFO("**************************************************\n");
  1795. DEBUG_INFO("MCU-%d get OVP : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP);
  1796. DEBUG_INFO("MCU-%d get UVP : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP);
  1797. DEBUG_INFO("MCU-%d get OCP : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP);
  1798. DEBUG_INFO("MCU-%d get OTP : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP);
  1799. DEBUG_INFO("MCU-%d get gmi_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault);
  1800. DEBUG_INFO("MCU-%d get cp_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault);
  1801. DEBUG_INFO("MCU-%d get ac_leak : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak);
  1802. DEBUG_INFO("MCU-%d get dc_leak : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak);
  1803. DEBUG_INFO("MCU-%d get mcu_selftest_fail : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail);
  1804. DEBUG_INFO("MCU-%d get handshaking_timeout : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout);
  1805. DEBUG_INFO("MCU-%d get emergency_stop : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop);
  1806. DEBUG_INFO("MCU-%d get relay_welding : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding);
  1807. DEBUG_INFO("MCU-%d get leak_module_fail : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail);
  1808. DEBUG_INFO("MCU-%d get shutter_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault);
  1809. DEBUG_INFO("MCU-%d get locker_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault);
  1810. DEBUG_INFO("MCU-%d get power_drop : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop);
  1811. DEBUG_INFO("MCU-%d get circuit_short : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.circuit_short);
  1812. DEBUG_INFO("MCU-%d get set_circuit : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.set_circuit);
  1813. DEBUG_INFO("MCU-%d get relay_drive_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault);
  1814. DEBUG_INFO("MCU-%d get InputAlarmCode : %x\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode);
  1815. }
  1816. //===============================
  1817. // Query primary MCU BLE config
  1818. //===============================
  1819. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin == ON)
  1820. {
  1821. DEBUG_INFO("**************************************************\n");
  1822. DEBUG_INFO("********* High priority polling : Case 5 *********\n");
  1823. DEBUG_INFO("**************************************************\n");
  1824. DEBUG_INFO("MCU-%d get isUserLogin : %d\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isLogin);
  1825. DEBUG_INFO("MCU-%d get isRequestStartCharger : %d\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart);
  1826. DEBUG_INFO("MCU-%d get isRequestStopCharger : %d\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop);
  1827. }
  1828. //===============================
  1829. // Query primary MCU ble login id
  1830. //===============================
  1831. if(strcmp((char *)&ShmCharger->gun_info[gun_index].bleLoginCentralId.id,"") != 0)
  1832. {
  1833. DEBUG_INFO("**************************************************\n");
  1834. DEBUG_INFO("********* High priority polling : Case 6 *********\n");
  1835. DEBUG_INFO("**************************************************\n");
  1836. DEBUG_INFO("MCU-%d get ble central id : %s\n", gun_index, ShmCharger->gun_info[gun_index].bleLoginCentralId.id);
  1837. }
  1838. usleep(100000);
  1839. }
  1840. printOutCount[gun_index]++;
  1841. }
  1842. else
  1843. {
  1844. /*
  1845. * Normal priority communication
  1846. */
  1847. switch(stepIndex)
  1848. {
  1849. case 1:
  1850. //===============================
  1851. // Query present input voltage
  1852. //===============================
  1853. if(Query_Present_InputVoltage(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].inputVoltage) == PASS)
  1854. {
  1855. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  1856. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  1857. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  1858. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  1859. failCount[gun_index] = 0;
  1860. }
  1861. else
  1862. {
  1863. DEBUG_WARN("MCU-%d get input voltage fail...%d\n", gun_index, failCount[gun_index]);
  1864. if(failCount[gun_index]<1000)
  1865. {
  1866. failCount[gun_index]++;
  1867. }
  1868. }
  1869. break;
  1870. case 3:
  1871. //===============================
  1872. // Query present output current
  1873. //===============================
  1874. if(Query_Present_OutputCurrent(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].outputCurrent) == PASS)
  1875. {
  1876. #ifndef SIMULATION
  1877. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  1878. #else
  1879. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus==SYS_MODE_CHARGING?(((rand()%10)+((ShmCharger->gun_info[gun_index].targetCurrent*10)-5))/10.0):0) ;
  1880. #endif
  1881. failCount[gun_index] = 0;
  1882. }
  1883. else
  1884. {
  1885. DEBUG_WARN("MCU-%d get output current fail...%d\n", gun_index, failCount[gun_index]);
  1886. if(failCount[gun_index]<1000)
  1887. {
  1888. failCount[gun_index]++;
  1889. }
  1890. }
  1891. break;
  1892. case 5:
  1893. //===============================
  1894. // Query gun plug-in times
  1895. //===============================
  1896. if(Query_AC_GUN_PLUGIN_TIMES(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].gunPluginTimes) == PASS)
  1897. {
  1898. ShmSysConfigAndInfo->SysConfig.AcPlugInTimes = ((long)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes & 0xFFFF);
  1899. failCount[gun_index] = 0;
  1900. }
  1901. else
  1902. {
  1903. DEBUG_WARN("MCU-%d get gun plugin times fail...%d\n", gun_index, failCount[gun_index]);
  1904. if(failCount[gun_index]<1000)
  1905. {
  1906. failCount[gun_index]++;
  1907. }
  1908. }
  1909. break;
  1910. case 7:
  1911. //===============================
  1912. // Query temperature
  1913. //===============================
  1914. if(Query_Temperature(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].temperature) == PASS)
  1915. {
  1916. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = ShmCharger->gun_info[gun_index].temperature.point[0];
  1917. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ConnectorTemp = ShmCharger->gun_info[gun_index].temperature.point[0];
  1918. failCount[gun_index] = 0;
  1919. }
  1920. else
  1921. {
  1922. DEBUG_WARN("MCU-%d get temperature fail...%d\n", gun_index, failCount[gun_index]);
  1923. if(failCount[gun_index]<1000)
  1924. {
  1925. failCount[gun_index]++;
  1926. }
  1927. }
  1928. break;
  1929. case 9:
  1930. //===============================
  1931. // Reserve
  1932. //===============================
  1933. break;
  1934. case 11:
  1935. //===============================
  1936. // Query RTC
  1937. //===============================
  1938. if(Query_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc))
  1939. {
  1940. struct timeb csuTime, mcuTime;
  1941. struct tm *tmCSU;
  1942. struct tm tmMcu;
  1943. ftime(&csuTime);
  1944. tmCSU = localtime(&csuTime.time);
  1945. tmMcu.tm_year = ShmCharger->gun_info[gun_index].rtc.year-1900;
  1946. tmMcu.tm_mon = ShmCharger->gun_info[gun_index].rtc.month-1;
  1947. tmMcu.tm_mday = ShmCharger->gun_info[gun_index].rtc.day;
  1948. tmMcu.tm_hour = ShmCharger->gun_info[gun_index].rtc.hour;
  1949. tmMcu.tm_min = ShmCharger->gun_info[gun_index].rtc.min;
  1950. tmMcu.tm_sec = ShmCharger->gun_info[gun_index].rtc.sec;
  1951. mcuTime.time = mktime(&tmMcu);
  1952. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin && !ShmOCPP16Data->OcppConnStatus)
  1953. {
  1954. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  1955. {
  1956. char cmdBuf[128];
  1957. sprintf(cmdBuf, "date -u -s \"%04d-%02d-%02d %02d:%02d:%02d\"", ShmCharger->gun_info[gun_index].rtc.year,
  1958. ShmCharger->gun_info[gun_index].rtc.month,
  1959. ShmCharger->gun_info[gun_index].rtc.day,
  1960. ShmCharger->gun_info[gun_index].rtc.hour,
  1961. ShmCharger->gun_info[gun_index].rtc.min,
  1962. ShmCharger->gun_info[gun_index].rtc.sec);
  1963. system(cmdBuf);
  1964. system("hwclock -w -u");
  1965. system("hwclock -s");
  1966. }
  1967. }
  1968. else
  1969. {
  1970. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  1971. {
  1972. ShmCharger->gun_info[gun_index].rtc.year = tmCSU->tm_year+1900;
  1973. ShmCharger->gun_info[gun_index].rtc.month = tmCSU->tm_mon+1;
  1974. ShmCharger->gun_info[gun_index].rtc.day = tmCSU->tm_mday;
  1975. ShmCharger->gun_info[gun_index].rtc.hour = tmCSU->tm_hour;
  1976. ShmCharger->gun_info[gun_index].rtc.min = tmCSU->tm_min;
  1977. ShmCharger->gun_info[gun_index].rtc.sec = tmCSU->tm_sec;
  1978. Config_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc);
  1979. }
  1980. }
  1981. failCount[gun_index] = 0;
  1982. }
  1983. else
  1984. {
  1985. DEBUG_WARN("MCU-%d get rtc fail...%d\n", gun_index, failCount[gun_index]);
  1986. if(failCount[gun_index]<1000)
  1987. {
  1988. failCount[gun_index]++;
  1989. }
  1990. }
  1991. break;
  1992. case 13:
  1993. //===============================
  1994. // Query primary MCU power consumption
  1995. //===============================
  1996. if(Query_Power_Consumption(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].powerConsumption))
  1997. {
  1998. failCount[gun_index] = 0;
  1999. }
  2000. else
  2001. {
  2002. DEBUG_WARN("MCU-%d get power consumption fail...%d\n", gun_index, failCount[gun_index]);
  2003. if(failCount[gun_index]<1000)
  2004. {
  2005. failCount[gun_index]++;
  2006. }
  2007. }
  2008. break;
  2009. case 15:
  2010. //===============================
  2011. // Upgrade MCU
  2012. //===============================
  2013. if(ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq)
  2014. {
  2015. DEBUG_INFO("==================================================\n");
  2016. DEBUG_INFO("======== Normal priority polling : Case 15 =======\n");
  2017. DEBUG_INFO("==================================================\n");
  2018. unsigned char cmd[512];
  2019. if(Upgrade_UART(Uart1Fd, AC_WALLMOUNT_CONTROLLER, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName))
  2020. {
  2021. DEBUG_INFO("MCU-%d upgrade firmware OK...%s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2022. sleep(20);
  2023. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = OFF;
  2024. failCount[gun_index] = 0;
  2025. }
  2026. else
  2027. {
  2028. DEBUG_WARN("MCU-%d upgrade firmware fail...%d\n", gun_index, failCount[gun_index]);
  2029. if(failCount[gun_index]<1000)
  2030. {
  2031. failCount[gun_index]++;
  2032. }
  2033. }
  2034. sprintf((char*)cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2035. system((char*)cmd);
  2036. }
  2037. break;
  2038. case 17:
  2039. //===============================
  2040. // Config set breathe led timing
  2041. //===============================
  2042. if(ShmCharger->gun_info[gun_index].isSetBreatheLedTiming == ON)
  2043. {
  2044. if(Config_AC_Set_Breathe_Led_Timing(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].setBreatheLedTiming))
  2045. {
  2046. failCount[gun_index] = 0;
  2047. }
  2048. else
  2049. {
  2050. DEBUG_WARN("MCU-%d set breathe led timing fail...%d\n", gun_index, failCount[gun_index]);
  2051. if(failCount[gun_index]<1000)
  2052. {
  2053. failCount[gun_index]++;
  2054. }
  2055. }
  2056. }
  2057. break;
  2058. case 19:
  2059. //===============================
  2060. // Config set led brightness
  2061. //===============================
  2062. if(ShmCharger->gun_info[gun_index].isSetLedBrightness == ON)
  2063. {
  2064. if(Config_AC_Set_Led_Brightness(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].setLedBrightness))
  2065. {
  2066. failCount[gun_index] = 0;
  2067. }
  2068. else
  2069. {
  2070. DEBUG_WARN("MCU-%d set led brightness fail...%d\n", gun_index, failCount[gun_index]);
  2071. if(failCount[gun_index]<1000)
  2072. {
  2073. failCount[gun_index]++;
  2074. }
  2075. }
  2076. }
  2077. break;
  2078. case 21:
  2079. //===============================
  2080. // Query firmware version
  2081. //===============================
  2082. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass != PASS)
  2083. {
  2084. DEBUG_INFO("==================================================\n");
  2085. DEBUG_INFO("======== Normal priority polling : Case 21-1======\n");
  2086. DEBUG_INFO("==================================================\n");
  2087. if(Query_FW_Ver(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].ver) == PASS)
  2088. {
  2089. DEBUG_INFO("MCU-%d get firmware version : %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2090. memcpy(ShmPrimaryMcuData->version, ShmCharger->gun_info[gun_index].ver.Version_FW, sizeof(ShmPrimaryMcuData->version));
  2091. ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass = PASS;
  2092. failCount[gun_index] = 0;
  2093. }
  2094. else
  2095. {
  2096. DEBUG_WARN("MCU-%d get firmware version fail...%d\n", gun_index, failCount[gun_index]);
  2097. if(failCount[gun_index]<1000)
  2098. {
  2099. failCount[gun_index]++;
  2100. }
  2101. }
  2102. }
  2103. //===============================
  2104. // Config primary MCU serial number
  2105. //===============================
  2106. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass != PASS)
  2107. {
  2108. DEBUG_INFO("==================================================\n");
  2109. DEBUG_INFO("======== Normal priority polling : Case 21-2======\n");
  2110. DEBUG_INFO("==================================================\n");
  2111. memcpy(ShmCharger->evseId.serial_number, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmCharger->evseId.serial_number));
  2112. if(Config_Serial_Number(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  2113. {
  2114. DEBUG_INFO("MCU-%d set serial number : %.12s\n",gun_index,ShmCharger->evseId.serial_number);
  2115. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass = PASS;
  2116. failCount[gun_index] = 0;
  2117. }
  2118. else
  2119. {
  2120. DEBUG_WARN("MCU-%d set serial number fail...%d\n", gun_index, failCount[gun_index]);
  2121. if(failCount[gun_index]<1000)
  2122. {
  2123. failCount[gun_index]++;
  2124. }
  2125. }
  2126. }
  2127. //===============================
  2128. // Config primary MCU model name
  2129. //===============================
  2130. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass != PASS)
  2131. {
  2132. DEBUG_INFO("==================================================\n");
  2133. DEBUG_INFO("======== Normal priority polling : Case 21-3======\n");
  2134. DEBUG_INFO("==================================================\n");
  2135. memcpy(ShmCharger->evseId.model_name, ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmCharger->evseId.model_name));
  2136. if(Config_Model_Name(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  2137. {
  2138. DEBUG_INFO("MCU-%d set model name : %.14s\n",gun_index,ShmCharger->evseId.model_name);
  2139. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass = PASS;
  2140. failCount[gun_index] = 0;
  2141. }
  2142. else
  2143. {
  2144. DEBUG_WARN("MCU-%d set model name fail...%d\n", gun_index, failCount[gun_index]);
  2145. if(failCount[gun_index]<1000)
  2146. {
  2147. failCount[gun_index]++;
  2148. }
  2149. }
  2150. }
  2151. break;
  2152. default:
  2153. stepIndex = 0;
  2154. break;
  2155. }
  2156. //==========================================================
  2157. // Low priority polling log print out
  2158. //==========================================================
  2159. if(printOutCount[gun_index] == PRINT_OUT_LEVEL_STATE)
  2160. {
  2161. if(stepIndex == 1)
  2162. {
  2163. DEBUG_INFO("==================================================\n");
  2164. DEBUG_INFO("======== Normal priority polling : Case 1 ========\n");
  2165. DEBUG_INFO("==================================================\n");
  2166. DEBUG_INFO("MCU-%d get input voltage : %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12);
  2167. DEBUG_INFO("MCU-%d get PresentChargingVoltage : %.2f\n", gun_index, (float)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage);
  2168. }
  2169. else if(stepIndex == 3)
  2170. {
  2171. DEBUG_INFO("==================================================\n");
  2172. DEBUG_INFO("======== Normal priority polling : Case 3 ========\n");
  2173. DEBUG_INFO("==================================================\n");
  2174. DEBUG_INFO("MCU-%d get output current : %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0]);
  2175. }
  2176. else if(stepIndex == 5)
  2177. {
  2178. DEBUG_INFO("==================================================\n");
  2179. DEBUG_INFO("======== Normal priority polling : Case 5 ========\n");
  2180. DEBUG_INFO("==================================================\n");
  2181. DEBUG_INFO("MCU-%d get gun plugin times : %ld\n", gun_index, (long)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes);
  2182. }
  2183. else if(stepIndex == 7)
  2184. {
  2185. DEBUG_INFO("==================================================\n");
  2186. DEBUG_INFO("======== Normal priority polling : Case 7 ========\n");
  2187. DEBUG_INFO("==================================================\n");
  2188. DEBUG_INFO("MCU-%d get temperature : %d\n", gun_index, ShmCharger->gun_info[gun_index].temperature.point[0]);
  2189. }
  2190. else if(stepIndex == 9)
  2191. {
  2192. DEBUG_INFO("==================================================\n");
  2193. DEBUG_INFO("======== Normal priority polling : Case 9 ========\n");
  2194. DEBUG_INFO("==================================================\n");
  2195. DEBUG_INFO("MCU-%d set cp pwn duty : %d\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  2196. }
  2197. else if(stepIndex == 11)
  2198. {
  2199. DEBUG_INFO("==================================================\n");
  2200. DEBUG_INFO("======== Normal priority polling : Case 11 =======\n");
  2201. DEBUG_INFO("==================================================\n");
  2202. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin && !ShmOCPP16Data->OcppConnStatus)
  2203. {
  2204. DEBUG_INFO("Sync from MCU-%d rtc OK...%04d-%02d-%02d %02d:%02d:%02d\n", gun_index,
  2205. ShmCharger->gun_info[gun_index].rtc.year,
  2206. ShmCharger->gun_info[gun_index].rtc.month,
  2207. ShmCharger->gun_info[gun_index].rtc.day,
  2208. ShmCharger->gun_info[gun_index].rtc.hour,
  2209. ShmCharger->gun_info[gun_index].rtc.min,
  2210. ShmCharger->gun_info[gun_index].rtc.sec);
  2211. }
  2212. else
  2213. {
  2214. DEBUG_INFO("MCU-%d set rtc OK...%04d-%02d-%02d %02d:%02d:%02d\n", gun_index,
  2215. ShmCharger->gun_info[gun_index].rtc.year,
  2216. ShmCharger->gun_info[gun_index].rtc.month,
  2217. ShmCharger->gun_info[gun_index].rtc.day,
  2218. ShmCharger->gun_info[gun_index].rtc.hour,
  2219. ShmCharger->gun_info[gun_index].rtc.min,
  2220. ShmCharger->gun_info[gun_index].rtc.sec);
  2221. }
  2222. }
  2223. else if(stepIndex == 13)
  2224. {
  2225. DEBUG_INFO("==================================================\n");
  2226. DEBUG_INFO("======== Normal priority polling : Case 13 =======\n");
  2227. DEBUG_INFO("==================================================\n");
  2228. DEBUG_INFO("MCU-%d get power consumption : %f kWh\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100));
  2229. }
  2230. else if(stepIndex == 17)
  2231. {
  2232. if(ShmCharger->gun_info[gun_index].isSetBreatheLedTiming == ON)
  2233. {
  2234. DEBUG_INFO("==================================================\n");
  2235. DEBUG_INFO("======== Normal priority polling : Case 17 =======\n");
  2236. DEBUG_INFO("==================================================\n");
  2237. DEBUG_INFO("MCU-%d set breathe led timing : Authed Fade in [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_In);
  2238. DEBUG_INFO("MCU-%d set breathe led timing : Authed Fade out [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_Out);
  2239. DEBUG_INFO("MCU-%d set breathe led timing : Charging Fade in [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.Set_Led_Action_Chaging_Fade_In);
  2240. DEBUG_INFO("MCU-%d set breathe led timing : Charging Fade out [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Chaging_Fade_Out);
  2241. DEBUG_INFO("MCU-%d set breathe led timing : Connected Fade in [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_In);
  2242. DEBUG_INFO("MCU-%d set breathe led timing : Connected Fade out [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_Out);
  2243. }
  2244. }
  2245. else if(stepIndex == 19)
  2246. {
  2247. if(ShmCharger->gun_info[gun_index].isSetLedBrightness == ON)
  2248. {
  2249. DEBUG_INFO("==================================================\n");
  2250. DEBUG_INFO("======== Normal priority polling : Case 19 =======\n");
  2251. DEBUG_INFO("==================================================\n");
  2252. DEBUG_INFO("MCU-%d set led brightness Sector 01 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_1);
  2253. DEBUG_INFO("MCU-%d set led brightness Sector 02 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_2);
  2254. DEBUG_INFO("MCU-%d set led brightness Sector 03 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_3);
  2255. DEBUG_INFO("MCU-%d set led brightness Sector 04 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_4);
  2256. DEBUG_INFO("MCU-%d set led brightness Sector 05 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_5);
  2257. DEBUG_INFO("MCU-%d set led brightness Sector 06 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_6);
  2258. DEBUG_INFO("MCU-%d set led brightness Sector 07 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_7);
  2259. DEBUG_INFO("MCU-%d set led brightness Sector 08 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_8);
  2260. DEBUG_INFO("MCU-%d set led brightness Sector 09 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_9);
  2261. DEBUG_INFO("MCU-%d set led brightness Sector 10 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_10);
  2262. DEBUG_INFO("MCU-%d set led brightness Sector 11 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_11);
  2263. DEBUG_INFO("MCU-%d set led brightness Sector 12 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_12);
  2264. }
  2265. }
  2266. else
  2267. {}
  2268. }
  2269. }
  2270. stepIndex++;
  2271. //===============================
  2272. // Communication fail check
  2273. //===============================
  2274. if(failCount[gun_index] >= FAIL_SPEC_COMM)
  2275. {
  2276. if((0 <= failCount[gun_index]%FAIL_SPEC_COMM) && (failCount[gun_index]%FAIL_SPEC_COMM < 10))
  2277. {
  2278. system ("pkill Module_InternalComm");
  2279. }
  2280. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == OFF)
  2281. {
  2282. DEBUG_ERROR("Primary MCU-%d communication fault", gun_index);
  2283. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = ON;
  2284. }
  2285. }
  2286. else
  2287. {
  2288. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == ON)
  2289. {
  2290. DEBUG_ERROR("Primary MCU-%d communication recover", gun_index);
  2291. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = OFF;
  2292. }
  2293. }
  2294. usleep(100000);
  2295. }
  2296. }
  2297. return FAIL;
  2298. }