Module_InternalComm.c 83 KB

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