Module_InternalComm.c 105 KB

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