internalComm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * internalComm.c
  3. *
  4. * Created on: 2019年5月7日
  5. * Author: foluswen
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/wireless.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <unistd.h>
  22. #include <stdarg.h>
  23. #include <stdio.h> /*標準輸入輸出定義*/
  24. #include <stdlib.h> /*標準函數庫定義*/
  25. #include <unistd.h> /*Unix 標準函數定義*/
  26. #include <fcntl.h> /*檔控制定義*/
  27. #include <termios.h> /*PPSIX 終端控制定義*/
  28. #include <errno.h> /*錯誤號定義*/
  29. #include <string.h>
  30. #include <time.h>
  31. #include <ctype.h>
  32. #include <ifaddrs.h>
  33. #include <math.h>
  34. #include "internalComm.h"
  35. #define PASS 1
  36. #define FAIL -1
  37. struct Address Addr={0x01,0x02,0x03,0x04,0xFF};
  38. struct Command Cmd={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x81,0x85,0x86,0xe0,0xe1,0xe2,0xe3};
  39. int tranceiveRelDelayTime(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx, unsigned short _delay)
  40. {
  41. int len;
  42. //sleep(2); //required to make flush work, for some reason
  43. tcflush(fd,TCIOFLUSH);
  44. if(write(fd, cmd, cmd_len) >= cmd_len)
  45. {
  46. usleep(_delay * 1000);
  47. len = read(fd, rx, 512);
  48. }
  49. else
  50. {
  51. #ifdef SystemLogMessage
  52. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  53. #endif
  54. }
  55. return len;
  56. }
  57. int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
  58. {
  59. int len;
  60. //sleep(2); //required to make flush work, for some reason
  61. tcflush(fd,TCIOFLUSH);
  62. if(write(fd, cmd, cmd_len) >= cmd_len)
  63. {
  64. usleep(15000);
  65. len = read(fd, rx, 512);
  66. }
  67. else
  68. {
  69. #ifdef SystemLogMessage
  70. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  71. #endif
  72. }
  73. return len;
  74. }
  75. unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  76. {
  77. unsigned char result = FAIL;
  78. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_FW_Ver, 0x00, 0x00, 0x00};
  79. unsigned char rx[512];
  80. unsigned char chksum = 0x00;
  81. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  82. if(len > 6)
  83. {
  84. if (len < 6+(rx[4] | rx[5]<<8))
  85. return result;
  86. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  87. {
  88. chksum ^= rx[6+idx];
  89. }
  90. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  91. (rx[2] == tx[1]) &&
  92. (rx[1] == tx[2]) &&
  93. (rx[3] == tx[3]))
  94. {
  95. memcpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
  96. *(Ret_Buf->Version_FW + 8) = 0x00;
  97. result = PASS;
  98. }
  99. }
  100. return result;
  101. }
  102. unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  103. {
  104. unsigned char result = FAIL;
  105. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_HW_Ver, 0x00, 0x00, 0x00};
  106. unsigned char rx[512];
  107. unsigned char chksum = 0x00;
  108. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  109. if(len > 6)
  110. {
  111. if (len < 6+(rx[4] | rx[5]<<8))
  112. return result;
  113. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  114. {
  115. chksum ^= rx[6+idx];
  116. }
  117. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  118. (rx[2] == tx[1]) &&
  119. (rx[1] == tx[2]) &&
  120. (rx[3] == tx[3]))
  121. {
  122. memcpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
  123. *(Ret_Buf->Version_HW + 8) = 0x00;
  124. result = PASS;
  125. }
  126. }
  127. return result;
  128. }
  129. unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
  130. {
  131. unsigned char result = FAIL;
  132. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_InputVoltage, 0x00, 0x00, 0x00};
  133. unsigned char rx[512];
  134. unsigned char chksum = 0x00;
  135. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  136. if(len > 13)
  137. {
  138. if (len < 6+(rx[4] | rx[5]<<8))
  139. return result;
  140. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  141. {
  142. chksum ^= rx[6+idx];
  143. }
  144. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  145. (rx[2] == tx[1]) &&
  146. (rx[1] == tx[2]) &&
  147. (rx[3] == tx[3]) &&
  148. chksum != 0)
  149. {
  150. Ret_Buf->inputType = rx[6];
  151. Ret_Buf->L1N_L12 =(rx[7] | (rx[8]<<8))/10.0;
  152. Ret_Buf->L2N_L23 =(rx[9] | (rx[10]<<8))/10.0;
  153. Ret_Buf->L3N_L31 =(rx[11] | (rx[12]<<8))/10.0;
  154. if(Ret_Buf->L1N_L12 >= 320 ||
  155. Ret_Buf->L2N_L23 >= 320 ||
  156. Ret_Buf->L3N_L31 >= 320)
  157. {
  158. result = FAIL;
  159. }
  160. else
  161. {
  162. result = PASS;
  163. }
  164. }
  165. }
  166. return result;
  167. }
  168. unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf)
  169. {
  170. unsigned char result = FAIL;
  171. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_OutputVoltage, 0x00, 0x00, 0x00};
  172. unsigned char rx[512];
  173. unsigned char chksum = 0x00;
  174. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  175. if(len > 6)
  176. {
  177. if (len < 6+(rx[4] | rx[5]<<8))
  178. return result;
  179. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  180. {
  181. chksum ^= rx[6+idx];
  182. }
  183. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  184. (rx[2] == tx[1]) &&
  185. (rx[1] == tx[2]) &&
  186. (rx[3] == tx[3]))
  187. {
  188. Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8));
  189. Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8));
  190. if((rx[4] | rx[5]<<8) > 4)
  191. {
  192. Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8));
  193. Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8));
  194. }
  195. result = PASS;
  196. }
  197. }
  198. return result;
  199. }
  200. unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
  201. {
  202. unsigned char result = FAIL;
  203. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Fan_Speed, 0x00, 0x00, 0x00};
  204. unsigned char rx[512];
  205. unsigned char chksum = 0x00;
  206. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  207. if(len > 6)
  208. {
  209. if (len < 6+(rx[4] | rx[5]<<8))
  210. return result;
  211. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  212. {
  213. chksum ^= rx[6+idx];
  214. }
  215. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  216. (rx[2] == tx[1]) &&
  217. (rx[1] == tx[2]) &&
  218. (rx[3] == tx[3]))
  219. {
  220. for(int idx=0;idx < 4;idx++)
  221. Ret_Buf->speed[idx] = (rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
  222. result = PASS;
  223. }
  224. }
  225. return result;
  226. }
  227. unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
  228. {
  229. unsigned char result = FAIL;
  230. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Temperature, 0x00, 0x00, 0x00};
  231. unsigned char rx[512];
  232. unsigned char chksum = 0x00;
  233. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  234. if(len > 6)
  235. {
  236. if (len < 6+(rx[4] | rx[5]<<8))
  237. return result;
  238. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  239. {
  240. chksum ^= rx[6+idx];
  241. }
  242. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  243. (rx[2] == tx[1]) &&
  244. (rx[1] == tx[2]) &&
  245. (rx[3] == tx[3]))
  246. {
  247. for(int idx=0;idx < 4;idx++)
  248. Ret_Buf->temperature[idx] = rx[6+idx] - 60;
  249. result = PASS;
  250. }
  251. }
  252. return result;
  253. }
  254. unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
  255. {
  256. unsigned char result = FAIL;
  257. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Aux_PowerVoltage, 0x00, 0x00, 0x00};
  258. unsigned char rx[512];
  259. unsigned char chksum = 0x00;
  260. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  261. if(len > 6)
  262. {
  263. if (len < 6+(rx[4] | rx[5]<<8))
  264. return result;
  265. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  266. {
  267. chksum ^= rx[6+idx];
  268. }
  269. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  270. (rx[2] == tx[1]) &&
  271. (rx[1] == tx[2]) &&
  272. (rx[3] == tx[3]))
  273. {
  274. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  275. Ret_Buf->voltage[idx] = rx[6+idx];
  276. result = PASS;
  277. }
  278. }
  279. return result;
  280. }
  281. unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
  282. {
  283. unsigned char result = FAIL;
  284. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Relay_Output, 0x00, 0x00, 0x00};
  285. unsigned char rx[512];
  286. unsigned char chksum = 0x00;
  287. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  288. // for (int i = 0; i < 7; i++)
  289. // printf("tx = %x \n", tx[i]);
  290. // for (int i = 0; i < len; i++)
  291. // printf("rx = %x \n", rx[i]);
  292. if(len > 6)
  293. {
  294. if (len < 6+(rx[4] | rx[5]<<8))
  295. return result;
  296. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  297. {
  298. chksum ^= rx[6+idx];
  299. }
  300. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  301. (rx[2] == tx[1]) &&
  302. (rx[1] == tx[2]) &&
  303. (rx[3] == tx[3]))
  304. {
  305. Ret_Buf->relay_event.bits.AC_Contactor = (rx[6] >> 0) & 0x01;
  306. Ret_Buf->relay_event.bits.CCS_Precharge = (rx[6] >> 1) & 0x01;
  307. Ret_Buf->relay_event.bits.Gun1_N = (rx[7] >> 0) & 0x01;
  308. Ret_Buf->relay_event.bits.Gun1_P = (rx[7] >> 1) & 0x01;
  309. Ret_Buf->relay_event.bits.Gun1_Parallel_N = (rx[7] >> 2) & 0x01;
  310. Ret_Buf->relay_event.bits.Gun1_Parallel_P = (rx[7] >> 3) & 0x01;
  311. Ret_Buf->relay_event.bits.Gun2_N = (rx[8] >> 0) & 0x01;
  312. Ret_Buf->relay_event.bits.Gun2_P = (rx[8] >> 1) & 0x01;
  313. result = PASS;
  314. }
  315. }
  316. return result;
  317. }
  318. unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
  319. {
  320. unsigned char result = FAIL;
  321. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gfd_Adc, 0x00, 0x00, 0x00};
  322. unsigned char rx[512];
  323. unsigned char chksum = 0x00;
  324. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  325. if(len > 6)
  326. {
  327. if (len < 6+(rx[4] | rx[5]<<8))
  328. return result;
  329. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  330. {
  331. chksum ^= rx[6+idx];
  332. }
  333. printf("chksum = %d \n", chksum);
  334. printf("rx[2] == %d tx[1] = %d \n", rx[2], tx[1]);
  335. printf("rx[1] == %d tx[2] = %d \n", rx[1], tx[2]);
  336. printf("rx[3] == %d tx[3] = %d \n", rx[3], tx[3]);
  337. printf("chksum = %d \n", chksum);
  338. // if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  339. // (rx[2] == tx[1]) &&
  340. // (rx[1] == tx[2]) &&
  341. // (rx[3] == tx[3]))
  342. if(
  343. (rx[2] == tx[1]) &&
  344. (rx[1] == tx[2]) &&
  345. (rx[3] == tx[3]))
  346. {
  347. Ret_Buf->Resister = (rx[6] | (rx[7]<<8) | (rx[8]<<16) | (rx[9]<<24));
  348. Ret_Buf->voltage = rx[10] | (rx[11]<<8);
  349. Ret_Buf->result = rx[12];
  350. result = PASS;
  351. }
  352. }
  353. return result;
  354. }
  355. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  356. {
  357. unsigned char result = FAIL;
  358. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
  359. unsigned char rx[512];
  360. unsigned char chksum = 0x00;
  361. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  362. if(len > 6)
  363. {
  364. if (len < 6+(rx[4] | rx[5]<<8))
  365. return result;
  366. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  367. {
  368. chksum ^= rx[6+idx];
  369. }
  370. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  371. (rx[2] == tx[1]) &&
  372. (rx[1] == tx[2]) &&
  373. (rx[3] == tx[3]))
  374. {
  375. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  376. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  377. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  378. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  379. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  380. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  381. Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  382. Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
  383. Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
  384. Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
  385. Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
  386. Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
  387. Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
  388. Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
  389. Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
  390. result = PASS;
  391. }
  392. }
  393. return result;
  394. }
  395. unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
  396. {
  397. unsigned char result = FAIL;
  398. 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};
  399. unsigned char rx[512];
  400. unsigned char chksum = 0x00;
  401. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  402. chksum ^= tx[6+idx];
  403. tx[14] = chksum;
  404. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  405. if(len > 6)
  406. {
  407. if (len < 6+(rx[4] | rx[5]<<8))
  408. return result;
  409. chksum = 0x00;
  410. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  411. {
  412. chksum ^= rx[6+idx];
  413. }
  414. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  415. (rx[2] == tx[1]) &&
  416. (rx[1] == tx[2]) &&
  417. (rx[3] == tx[3]) &&
  418. rx[6] == PASS)
  419. {
  420. result = PASS;
  421. }
  422. }
  423. return result;
  424. }
  425. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  426. {
  427. unsigned char result = FAIL;
  428. unsigned char tx[10] = {0xaa, 0x00, targetAddr, Cmd.config_Relay_Output, 0x03, 0x00, Set_Buf->relay_event.relay_status[0], Set_Buf->relay_event.relay_status[1], Set_Buf->relay_event.relay_status[2]};
  429. unsigned char rx[512];
  430. unsigned char chksum = 0x00;
  431. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  432. chksum ^= tx[6 + idx];
  433. tx[9] = chksum;
  434. // for (int i = 0; i < 10; i++)
  435. // printf("set relay cmd : tx = %x \n", tx[i]);
  436. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  437. if(len > 6)
  438. {
  439. if (len < 6+(rx[4] | rx[5]<<8))
  440. return result;
  441. // for (int i = 0; i < len; i++)
  442. // printf("set relay cmd : rx = %x \n", rx[i]);
  443. chksum = 0x00;
  444. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  445. {
  446. chksum ^= rx[6+idx];
  447. }
  448. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  449. (rx[2] == tx[1]) &&
  450. (rx[1] == tx[2]) &&
  451. (rx[3] == tx[3]) &&
  452. rx[6] == PASS)
  453. {
  454. result = PASS;
  455. }
  456. }
  457. return result;
  458. }
  459. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  460. {
  461. unsigned char result = FAIL;
  462. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
  463. unsigned char rx[512];
  464. unsigned char chksum = 0x00;
  465. for (int idx = 0; idx < 2; idx++)
  466. tx[6] |= (Set_Buf->Button_LED[idx] ? 0x01:0x00) << (0+idx);
  467. for (int idx = 0; idx < 4; idx++)
  468. tx[6] |= (Set_Buf->System_LED[idx] ? 0x01:0x00) << (2+idx);
  469. tx[6] |= (Set_Buf->AC_Connector ? 0x01:0x00) << 6;
  470. tx[6] |= (Set_Buf->AC_Breaker ? 0x01:0x00) << 7;
  471. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  472. chksum ^= tx[6+idx];
  473. tx[7] = chksum;
  474. if (tranceive(fd, tx, sizeof(tx), rx) > 0)
  475. {
  476. chksum = 0x00;
  477. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  478. {
  479. chksum ^= rx[6+idx];
  480. }
  481. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  482. (rx[2] == tx[1]) &&
  483. (rx[1] == tx[2]) &&
  484. (rx[3] == tx[3]) &&
  485. (rx[6] == tx[6]))
  486. {
  487. result = PASS;
  488. }
  489. }
  490. return result;
  491. }
  492. unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf)
  493. {
  494. unsigned char result = FAIL;
  495. unsigned char tx[11] = {0xaa, 0x00, targetAddr, Cmd.config_Gfd_Value, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00};
  496. unsigned char rx[512];
  497. unsigned char chksum = 0x00;
  498. tx[6] = (unsigned char)(Set_Buf->reqVol) & 0xff;
  499. tx[7] = ((unsigned char)(Set_Buf->reqVol) >> 8) & 0xff;
  500. tx[8] = (unsigned char)(Set_Buf->resister) & 0xff;
  501. tx[9] = ((unsigned char)(Set_Buf->resister) >> 8) & 0xff;
  502. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  503. chksum ^= tx[6+idx];
  504. tx[10] = chksum;
  505. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  506. if(len > 6)
  507. {
  508. if (len < 6+(rx[4] | rx[5]<<8))
  509. return result;
  510. chksum = 0x00;
  511. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  512. {
  513. chksum ^= rx[6+idx];
  514. }
  515. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  516. (rx[2] == tx[1]) &&
  517. (rx[1] == tx[2]) &&
  518. (rx[3] == tx[3]) &&
  519. (rx[6] == tx[6]))
  520. {
  521. result = PASS;
  522. }
  523. }
  524. return result;
  525. }
  526. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  527. {
  528. unsigned char result = FAIL;
  529. 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};
  530. unsigned char rx[512];
  531. unsigned char chksum = 0x00;
  532. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  533. chksum ^= tx[6+idx];
  534. tx[10] = chksum;
  535. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  536. if(len > 6)
  537. {
  538. if (len < 6+(rx[4] | rx[5]<<8))
  539. return result;
  540. chksum = 0x00;
  541. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  542. {
  543. chksum ^= rx[6+idx];
  544. }
  545. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  546. (rx[2] == tx[1]) &&
  547. (rx[1] == tx[2]) &&
  548. (rx[3] == tx[3]) &&
  549. (rx[6] == 0x00))
  550. {
  551. result = PASS;
  552. }
  553. }
  554. return result;
  555. }
  556. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  557. {
  558. unsigned char result = FAIL;
  559. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
  560. unsigned char rx[512];
  561. unsigned char chksum = 0x00;
  562. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  563. if(len > 6)
  564. {
  565. if (len < 6+(rx[4] | rx[5]<<8))
  566. return result;
  567. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  568. {
  569. chksum ^= rx[6+idx];
  570. }
  571. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  572. (rx[2] == tx[1]) &&
  573. (rx[1] == tx[2]) &&
  574. (rx[3] == tx[3]) &&
  575. (rx[6] == 0x00))
  576. {
  577. result = PASS;
  578. }
  579. }
  580. return result;
  581. }
  582. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  583. {
  584. unsigned char result = FAIL;
  585. unsigned char tx[11 + length];
  586. unsigned char rx[512];
  587. unsigned char chksum = 0x00;
  588. tx[0] = 0xaa;
  589. tx[1] = 0x00;
  590. tx[2] = targetAddr;
  591. tx[3] = Cmd.update_Transfer;
  592. tx[4] = (4 + length) & 0xff;
  593. tx[5] = ((4 + length)>>8) & 0xff;
  594. tx[6] = (startAddr>>0) & 0xff;
  595. tx[7] = (startAddr>>8) & 0xff;
  596. tx[8] = (startAddr>>16) & 0xff;
  597. tx[9] = (startAddr>>24) & 0xff;
  598. memcpy(tx+10, data, length);
  599. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  600. chksum ^= tx[6+idx];
  601. tx[sizeof(tx)-1] = chksum;
  602. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  603. if(len > 6)
  604. {
  605. if (len < 6+(rx[4] | rx[5]<<8))
  606. return result;
  607. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  608. {
  609. chksum ^= rx[6+idx];
  610. }
  611. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  612. (rx[2] == tx[1]) &&
  613. (rx[1] == tx[2]) &&
  614. (rx[3] == tx[3]) &&
  615. (rx[6] == 0x00))
  616. {
  617. result = PASS;
  618. }
  619. }
  620. return result;
  621. }
  622. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  623. {
  624. unsigned char result = FAIL;
  625. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
  626. unsigned char rx[512];
  627. unsigned char chksum = 0x00;
  628. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  629. if(len > 6)
  630. {
  631. if (len < 6+(rx[4] | rx[5]<<8))
  632. return result;
  633. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  634. {
  635. chksum ^= rx[6+idx];
  636. }
  637. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  638. (rx[2] == tx[1]) &&
  639. (rx[1] == tx[2]) &&
  640. (rx[3] == tx[3]) &&
  641. (rx[6] == 0x00))
  642. {
  643. result = PASS;
  644. }
  645. }
  646. return result;
  647. }