internalComm.c 18 KB

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