Module_InternalComm.c 89 KB

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