internalComm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. if(len > 6)
  181. {
  182. if (len < 6+(rx[4] | rx[5]<<8))
  183. return result;
  184. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  185. {
  186. chksum ^= rx[6+idx];
  187. }
  188. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  189. (rx[2] == tx[1]) &&
  190. (rx[1] == tx[2]) &&
  191. (rx[3] == tx[3]))
  192. {
  193. for(int idx=0;idx < 4;idx++)
  194. Ret_Buf->speed[idx] = (rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
  195. result = PASS;
  196. }
  197. }
  198. return result;
  199. }
  200. unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
  201. {
  202. unsigned char result = FAIL;
  203. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Temperature, 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->temperature[idx] = rx[6+idx] - 60;
  222. result = PASS;
  223. }
  224. }
  225. return result;
  226. }
  227. unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
  228. {
  229. unsigned char result = FAIL;
  230. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Aux_PowerVoltage, 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<(rx[4] | rx[5]<<8);idx++)
  248. Ret_Buf->voltage[idx] = rx[6+idx];
  249. result = PASS;
  250. }
  251. }
  252. return result;
  253. }
  254. unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
  255. {
  256. unsigned char result = FAIL;
  257. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Relay_Output, 0x00, 0x00, 0x00};
  258. unsigned char rx[512];
  259. unsigned char chksum = 0x00;
  260. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  261. // for (int i = 0; i < 7; i++)
  262. // printf("tx = %x \n", tx[i]);
  263. // for (int i = 0; i < len; i++)
  264. // printf("rx = %x \n", rx[i]);
  265. if(len > 6)
  266. {
  267. if (len < 6+(rx[4] | rx[5]<<8))
  268. return result;
  269. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  270. {
  271. chksum ^= rx[6+idx];
  272. }
  273. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  274. (rx[2] == tx[1]) &&
  275. (rx[1] == tx[2]) &&
  276. (rx[3] == tx[3]))
  277. {
  278. Ret_Buf->relay_event.bits.AC_Contactor = (rx[6] >> 0) & 0x01;
  279. Ret_Buf->relay_event.bits.CCS_Precharge = (rx[6] >> 1) & 0x01;
  280. Ret_Buf->relay_event.bits.Gun1_N = (rx[7] >> 0) & 0x01;
  281. Ret_Buf->relay_event.bits.Gun1_P = (rx[7] >> 1) & 0x01;
  282. Ret_Buf->relay_event.bits.Gun1_Parallel_N = (rx[7] >> 2) & 0x01;
  283. Ret_Buf->relay_event.bits.Gun1_Parallel_P = (rx[7] >> 3) & 0x01;
  284. Ret_Buf->relay_event.bits.Gun2_N = (rx[8] >> 0) & 0x01;
  285. Ret_Buf->relay_event.bits.Gun2_P = (rx[8] >> 1) & 0x01;
  286. result = PASS;
  287. }
  288. }
  289. return result;
  290. }
  291. unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
  292. {
  293. unsigned char result = FAIL;
  294. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gfd_Adc, 0x00, 0x00, 0x00};
  295. unsigned char rx[512];
  296. unsigned char chksum = 0x00;
  297. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  298. // for(int i = 0; i < 7; i++)
  299. // printf ("tx = %d \n", tx[i]);
  300. if(len > 6)
  301. {
  302. if (len < 6+(rx[4] | rx[5]<<8))
  303. {
  304. printf("Query_Gfd_Adc fail \n");
  305. return result;
  306. }
  307. // for(int i = 0; i < len; i++)
  308. // printf ("rx = %d \n", rx[i]);
  309. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  310. {
  311. chksum ^= rx[6+idx];
  312. }
  313. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  314. (rx[2] == tx[1]) &&
  315. (rx[1] == tx[2]) &&
  316. (rx[3] == tx[3]))
  317. {
  318. Ret_Buf->Resister_conn1 = (rx[6] | (rx[7] << 8));
  319. Ret_Buf->voltage_conn1 = (rx[8] | (rx[9] << 8));
  320. Ret_Buf->result_conn1 = rx[10];
  321. Ret_Buf->rb_step_1 = rx[11];
  322. Ret_Buf->Resister_conn2 = (rx[12] | (rx[13] << 8));
  323. Ret_Buf->voltage_conn2 = (rx[14] | (rx[15] << 8));
  324. Ret_Buf->result_conn2 = rx[16];
  325. Ret_Buf->rb_step_2 = rx[17];
  326. result = PASS;
  327. }
  328. }
  329. return result;
  330. }
  331. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  332. {
  333. unsigned char result = FAIL;
  334. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
  335. unsigned char rx[512];
  336. unsigned char chksum = 0x00;
  337. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  338. if(len > 6)
  339. {
  340. if (len < 6+(rx[4] | rx[5]<<8))
  341. return result;
  342. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  343. {
  344. chksum ^= rx[6+idx];
  345. }
  346. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  347. (rx[2] == tx[1]) &&
  348. (rx[1] == tx[2]) &&
  349. (rx[3] == tx[3]))
  350. {
  351. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  352. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  353. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  354. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  355. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  356. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  357. Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  358. Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
  359. Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
  360. Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
  361. Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
  362. Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
  363. Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
  364. Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
  365. Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
  366. result = PASS;
  367. }
  368. }
  369. return result;
  370. }
  371. unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
  372. {
  373. unsigned char result = FAIL;
  374. 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};
  375. unsigned char rx[512];
  376. unsigned char chksum = 0x00;
  377. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  378. chksum ^= tx[6+idx];
  379. tx[14] = chksum;
  380. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  381. if(len > 6)
  382. {
  383. if (len < 6+(rx[4] | rx[5]<<8))
  384. return result;
  385. chksum = 0x00;
  386. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  387. {
  388. chksum ^= rx[6+idx];
  389. }
  390. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  391. (rx[2] == tx[1]) &&
  392. (rx[1] == tx[2]) &&
  393. (rx[3] == tx[3]))
  394. {
  395. result = PASS;
  396. }
  397. }
  398. return result;
  399. }
  400. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  401. {
  402. unsigned char result = FAIL;
  403. 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]};
  404. unsigned char rx[512];
  405. unsigned char chksum = 0x00;
  406. for(int idx = 6;idx<8;idx++)
  407. chksum ^= tx[idx];
  408. tx[9] = chksum;
  409. // for (int i = 0; i < 10; i++)
  410. // printf("set relay cmd : tx = %x \n", tx[i]);
  411. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  412. if(len > 6)
  413. {
  414. if (len < 6+(rx[4] | rx[5]<<8))
  415. return result;
  416. // for (int i = 0; i < len; i++)
  417. // printf("set relay cmd : rx = %x \n", rx[i]);
  418. chksum = 0x00;
  419. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  420. {
  421. chksum ^= rx[6+idx];
  422. }
  423. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  424. (rx[2] == tx[1]) &&
  425. (rx[1] == tx[2]) &&
  426. (rx[3] == tx[3]) &&
  427. (rx[6] == 0x01))
  428. {
  429. result = PASS;
  430. }
  431. }
  432. return result;
  433. }
  434. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  435. {
  436. unsigned char result = FAIL;
  437. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
  438. unsigned char rx[512];
  439. unsigned char chksum = 0x00;
  440. tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
  441. for(int idx = 0;idx<2;idx++)
  442. tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
  443. for(int idx = 0;idx<4;idx++)
  444. tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
  445. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  446. chksum ^= tx[6+idx];
  447. tx[14] = chksum;
  448. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  449. if(len > 6)
  450. {
  451. if (len < 6+(rx[4] | rx[5]<<8))
  452. return result;
  453. chksum = 0x00;
  454. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  455. {
  456. chksum ^= rx[6+idx];
  457. }
  458. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  459. (rx[2] == tx[1]) &&
  460. (rx[1] == tx[2]) &&
  461. (rx[3] == tx[3]) &&
  462. (rx[6] == tx[6]))
  463. {
  464. result = PASS;
  465. }
  466. }
  467. return result;
  468. }
  469. unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf)
  470. {
  471. unsigned char result = FAIL;
  472. unsigned char tx[12] = {0xaa, 0x00, targetAddr, Cmd.config_Gfd_Value, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  473. unsigned char rx[512];
  474. unsigned char chksum = 0x00;
  475. tx[6] = Set_Buf->index;
  476. tx[7] = (unsigned char)(Set_Buf->reqVol) & 0xff;
  477. tx[8] = (Set_Buf->reqVol >> 8) & 0xff;
  478. tx[9] = (unsigned char)(Set_Buf->resister) & 0xff;
  479. tx[10] = (Set_Buf->resister >> 8) & 0xff;
  480. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  481. chksum ^= tx[6+idx];
  482. tx[11] = chksum;
  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. chksum = 0x00;
  489. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  490. {
  491. chksum ^= rx[6+idx];
  492. }
  493. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  494. (rx[2] == tx[1]) &&
  495. (rx[1] == tx[2]) &&
  496. (rx[3] == tx[3]) &&
  497. (rx[6] == tx[6]))
  498. {
  499. result = PASS;
  500. }
  501. }
  502. return result;
  503. }
  504. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  505. {
  506. unsigned char result = FAIL;
  507. 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};
  508. unsigned char rx[512];
  509. unsigned char chksum = 0x00;
  510. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  511. chksum ^= tx[6+idx];
  512. tx[10] = chksum;
  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. chksum = 0x00;
  519. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  520. {
  521. chksum ^= rx[6+idx];
  522. }
  523. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  524. (rx[2] == tx[1]) &&
  525. (rx[1] == tx[2]) &&
  526. (rx[3] == tx[3]) &&
  527. (rx[6] == 0x00))
  528. {
  529. result = PASS;
  530. }
  531. }
  532. return result;
  533. }
  534. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  535. {
  536. unsigned char result = FAIL;
  537. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
  538. unsigned char rx[512];
  539. unsigned char chksum = 0x00;
  540. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  541. if(len > 6)
  542. {
  543. if (len < 6+(rx[4] | rx[5]<<8))
  544. return result;
  545. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  546. {
  547. chksum ^= rx[6+idx];
  548. }
  549. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  550. (rx[2] == tx[1]) &&
  551. (rx[1] == tx[2]) &&
  552. (rx[3] == tx[3]) &&
  553. (rx[6] == 0x00))
  554. {
  555. result = PASS;
  556. }
  557. }
  558. return result;
  559. }
  560. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  561. {
  562. unsigned char result = FAIL;
  563. unsigned char tx[11 + length];
  564. unsigned char rx[512];
  565. unsigned char chksum = 0x00;
  566. tx[0] = 0xaa;
  567. tx[1] = 0x00;
  568. tx[2] = targetAddr;
  569. tx[3] = Cmd.update_Transfer;
  570. tx[4] = (4 + length) & 0xff;
  571. tx[5] = ((4 + length)>>8) & 0xff;
  572. tx[6] = (startAddr>>0) & 0xff;
  573. tx[7] = (startAddr>>8) & 0xff;
  574. tx[8] = (startAddr>>16) & 0xff;
  575. tx[9] = (startAddr>>24) & 0xff;
  576. memcpy(tx+10, data, length);
  577. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  578. chksum ^= tx[6+idx];
  579. tx[sizeof(tx)-1] = chksum;
  580. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  581. if(len > 6)
  582. {
  583. if (len < 6+(rx[4] | rx[5]<<8))
  584. return result;
  585. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  586. {
  587. chksum ^= rx[6+idx];
  588. }
  589. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  590. (rx[2] == tx[1]) &&
  591. (rx[1] == tx[2]) &&
  592. (rx[3] == tx[3]) &&
  593. (rx[6] == 0x00))
  594. {
  595. result = PASS;
  596. }
  597. }
  598. return result;
  599. }
  600. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  601. {
  602. unsigned char result = FAIL;
  603. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
  604. unsigned char rx[512];
  605. unsigned char chksum = 0x00;
  606. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  607. if(len > 6)
  608. {
  609. if (len < 6+(rx[4] | rx[5]<<8))
  610. return result;
  611. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  612. {
  613. chksum ^= rx[6+idx];
  614. }
  615. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  616. (rx[2] == tx[1]) &&
  617. (rx[1] == tx[2]) &&
  618. (rx[3] == tx[3]) &&
  619. (rx[6] == 0x00))
  620. {
  621. result = PASS;
  622. }
  623. }
  624. return result;
  625. }