Module_InternalComm.c 95 KB

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