internalComm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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,0x83,0x85,0x86, 0x87,0x8B,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(30000);
  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 = 0;idx<(tx[4] | tx[5]<<8);idx++)
  407. chksum ^= tx[6 + 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[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gfd_Value, 0x02, 0x00, 0x00, 0x00, 0x00};
  473. unsigned char rx[512];
  474. unsigned char chksum = 0x00;
  475. tx[6] = Set_Buf->index;
  476. tx[7] = Set_Buf->state;
  477. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  478. chksum ^= tx[6+idx];
  479. tx[8] = chksum;
  480. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  481. if(len > 6)
  482. {
  483. if (len < 6+(rx[4] | rx[5]<<8))
  484. return result;
  485. chksum = 0x00;
  486. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  487. {
  488. chksum ^= rx[6+idx];
  489. }
  490. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  491. (rx[2] == tx[1]) &&
  492. (rx[1] == tx[2]) &&
  493. (rx[3] == tx[3]) &&
  494. (rx[6] == tx[6]))
  495. {
  496. result = PASS;
  497. }
  498. }
  499. return result;
  500. }
  501. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
  502. {
  503. unsigned char result = FAIL;
  504. unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
  505. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  506. unsigned char rx[512];
  507. unsigned char chksum = 0x00;
  508. memcpy(tx + 6, modelname, 14);
  509. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  510. chksum ^= tx[6+idx];
  511. tx[20] = chksum;
  512. // for(int i = 0; i < 21; i++)
  513. // printf ("tx = %x \n", tx[i]);
  514. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  515. // for(int i = 0; i < len; i++)
  516. // printf ("rx = %x \n", rx[i]);
  517. if(len > 6)
  518. {
  519. if (len < 6+(rx[4] | rx[5]<<8))
  520. return result;
  521. chksum = 0x00;
  522. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  523. {
  524. chksum ^= rx[6+idx];
  525. }
  526. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  527. (rx[2] == tx[1]) &&
  528. (rx[1] == tx[2]) &&
  529. (rx[3] == tx[3]) &&
  530. (rx[6] == tx[6]))
  531. {
  532. result = PASS;
  533. }
  534. }
  535. return result;
  536. }
  537. unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  538. {
  539. unsigned char result = FAIL;
  540. unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
  541. Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
  542. Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]};
  543. unsigned char rx[512];
  544. unsigned char chksum = 0x00;
  545. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  546. chksum ^= tx[6 + idx];
  547. tx[20] = chksum;
  548. if (tranceive(fd, tx, sizeof(tx), rx) > 0)
  549. {
  550. chksum = 0x00;
  551. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  552. {
  553. chksum ^= rx[6 + idx];
  554. }
  555. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  556. (rx[2] == tx[1]) &&
  557. (rx[1] == tx[2]) &&
  558. (rx[3] == tx[3]) &&
  559. (rx[6] == tx[6]))
  560. {
  561. result = PASS;
  562. }
  563. }
  564. return result;
  565. }
  566. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  567. {
  568. unsigned char result = FAIL;
  569. 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};
  570. unsigned char rx[512];
  571. unsigned char chksum = 0x00;
  572. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  573. chksum ^= tx[6+idx];
  574. tx[10] = chksum;
  575. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  576. if(len > 6)
  577. {
  578. if (len < 6+(rx[4] | rx[5]<<8))
  579. return result;
  580. chksum = 0x00;
  581. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  582. {
  583. chksum ^= rx[6+idx];
  584. }
  585. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  586. (rx[2] == tx[1]) &&
  587. (rx[1] == tx[2]) &&
  588. (rx[3] == tx[3]) &&
  589. (rx[6] == 0x00))
  590. {
  591. result = PASS;
  592. }
  593. }
  594. return result;
  595. }
  596. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  597. {
  598. unsigned char result = FAIL;
  599. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
  600. unsigned char rx[512];
  601. unsigned char chksum = 0x00;
  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_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  623. {
  624. unsigned char result = FAIL;
  625. unsigned char tx[11 + length];
  626. unsigned char rx[512];
  627. unsigned char chksum = 0x00;
  628. tx[0] = 0xaa;
  629. tx[1] = 0x00;
  630. tx[2] = targetAddr;
  631. tx[3] = Cmd.update_Transfer;
  632. tx[4] = (4 + length) & 0xff;
  633. tx[5] = ((4 + length)>>8) & 0xff;
  634. tx[6] = (startAddr>>0) & 0xff;
  635. tx[7] = (startAddr>>8) & 0xff;
  636. tx[8] = (startAddr>>16) & 0xff;
  637. tx[9] = (startAddr>>24) & 0xff;
  638. memcpy(tx+10, data, length);
  639. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  640. chksum ^= tx[6+idx];
  641. tx[sizeof(tx)-1] = chksum;
  642. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  643. if(len > 6)
  644. {
  645. if (len < 6+(rx[4] | rx[5]<<8))
  646. return result;
  647. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  648. {
  649. chksum ^= rx[6+idx];
  650. }
  651. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  652. (rx[2] == tx[1]) &&
  653. (rx[1] == tx[2]) &&
  654. (rx[3] == tx[3]) &&
  655. (rx[6] == 0x00))
  656. {
  657. result = PASS;
  658. }
  659. }
  660. return result;
  661. }
  662. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  663. {
  664. unsigned char result = FAIL;
  665. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
  666. unsigned char rx[512];
  667. unsigned char chksum = 0x00;
  668. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  669. if(len > 6)
  670. {
  671. if (len < 6+(rx[4] | rx[5]<<8))
  672. return result;
  673. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  674. {
  675. chksum ^= rx[6+idx];
  676. }
  677. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  678. (rx[2] == tx[1]) &&
  679. (rx[1] == tx[2]) &&
  680. (rx[3] == tx[3]) &&
  681. (rx[6] == 0x00))
  682. {
  683. result = PASS;
  684. }
  685. }
  686. return result;
  687. }