internalComm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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(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. // for(int i = 0; i < 15; i++)
  385. // printf ("tx = %x \n", tx[i]);
  386. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  387. // for(int i = 0; i < len; i++)
  388. // printf ("rx = %x \n", rx[i]);
  389. if(len > 6)
  390. {
  391. if (len < 6+(rx[4] | rx[5]<<8))
  392. return result;
  393. chksum = 0x00;
  394. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  395. {
  396. chksum ^= rx[6+idx];
  397. }
  398. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  399. (rx[2] == tx[1]) &&
  400. (rx[1] == tx[2]) &&
  401. (rx[3] == tx[3]))
  402. {
  403. result = PASS;
  404. }
  405. }
  406. return result;
  407. }
  408. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  409. {
  410. unsigned char result = FAIL;
  411. 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]};
  412. unsigned char rx[512];
  413. unsigned char chksum = 0x00;
  414. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  415. chksum ^= tx[6 + idx];
  416. tx[9] = chksum;
  417. // for (int i = 0; i < 10; i++)
  418. // printf("set relay cmd : tx = %x \n", tx[i]);
  419. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  420. if(len > 6)
  421. {
  422. if (len < 6+(rx[4] | rx[5]<<8))
  423. return result;
  424. // for (int i = 0; i < len; i++)
  425. // printf("set relay cmd : rx = %x \n", rx[i]);
  426. chksum = 0x00;
  427. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  428. {
  429. chksum ^= rx[6+idx];
  430. }
  431. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  432. (rx[2] == tx[1]) &&
  433. (rx[1] == tx[2]) &&
  434. (rx[3] == tx[3]) &&
  435. (rx[6] == 0x01))
  436. {
  437. result = PASS;
  438. }
  439. }
  440. return result;
  441. }
  442. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  443. {
  444. unsigned char result = FAIL;
  445. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
  446. unsigned char rx[512];
  447. unsigned char chksum = 0x00;
  448. tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
  449. for(int idx = 0;idx<2;idx++)
  450. tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
  451. for(int idx = 0;idx<4;idx++)
  452. tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
  453. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  454. chksum ^= tx[6+idx];
  455. tx[14] = chksum;
  456. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  457. if(len > 6)
  458. {
  459. if (len < 6+(rx[4] | rx[5]<<8))
  460. return result;
  461. chksum = 0x00;
  462. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  463. {
  464. chksum ^= rx[6+idx];
  465. }
  466. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  467. (rx[2] == tx[1]) &&
  468. (rx[1] == tx[2]) &&
  469. (rx[3] == tx[3]) &&
  470. (rx[6] == tx[6]))
  471. {
  472. result = PASS;
  473. }
  474. }
  475. return result;
  476. }
  477. unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf)
  478. {
  479. unsigned char result = FAIL;
  480. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gfd_Value, 0x02, 0x00, 0x00, 0x00, 0x00};
  481. unsigned char rx[512];
  482. unsigned char chksum = 0x00;
  483. tx[6] = Set_Buf->index;
  484. tx[7] = Set_Buf->state;
  485. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  486. chksum ^= tx[6+idx];
  487. tx[8] = chksum;
  488. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  489. if(len > 6)
  490. {
  491. if (len < 6+(rx[4] | rx[5]<<8))
  492. return result;
  493. chksum = 0x00;
  494. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  495. {
  496. chksum ^= rx[6+idx];
  497. }
  498. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  499. (rx[2] == tx[1]) &&
  500. (rx[1] == tx[2]) &&
  501. (rx[3] == tx[3]) &&
  502. (rx[6] == tx[6]))
  503. {
  504. result = PASS;
  505. }
  506. }
  507. return result;
  508. }
  509. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
  510. {
  511. unsigned char result = FAIL;
  512. unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
  513. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  514. unsigned char rx[512];
  515. unsigned char chksum = 0x00;
  516. memcpy(tx + 6, modelname, 14);
  517. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  518. chksum ^= tx[6+idx];
  519. tx[20] = chksum;
  520. // for(int i = 0; i < 21; i++)
  521. // printf ("tx = %x \n", tx[i]);
  522. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  523. // for(int i = 0; i < len; i++)
  524. // printf ("rx = %x \n", rx[i]);
  525. if(len > 6)
  526. {
  527. if (len < 6+(rx[4] | rx[5]<<8))
  528. return result;
  529. chksum = 0x00;
  530. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  531. {
  532. chksum ^= rx[6+idx];
  533. }
  534. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  535. (rx[2] == tx[1]) &&
  536. (rx[1] == tx[2]) &&
  537. (rx[3] == tx[3]) &&
  538. (rx[6] == tx[6]))
  539. {
  540. result = PASS;
  541. }
  542. }
  543. return result;
  544. }
  545. unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  546. {
  547. unsigned char result = FAIL;
  548. unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
  549. Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
  550. Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]};
  551. unsigned char rx[512];
  552. unsigned char chksum = 0x00;
  553. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  554. chksum ^= tx[6 + idx];
  555. tx[20] = chksum;
  556. if (tranceive(fd, tx, sizeof(tx), rx) > 0)
  557. {
  558. chksum = 0x00;
  559. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  560. {
  561. chksum ^= rx[6 + idx];
  562. }
  563. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  564. (rx[2] == tx[1]) &&
  565. (rx[1] == tx[2]) &&
  566. (rx[3] == tx[3]) &&
  567. (rx[6] == tx[6]))
  568. {
  569. result = PASS;
  570. }
  571. }
  572. return result;
  573. }
  574. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  575. {
  576. unsigned char result = FAIL;
  577. 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};
  578. unsigned char rx[512];
  579. unsigned char chksum = 0x00;
  580. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  581. chksum ^= tx[6+idx];
  582. tx[10] = chksum;
  583. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  584. if(len > 6)
  585. {
  586. if (len < 6+(rx[4] | rx[5]<<8))
  587. return result;
  588. chksum = 0x00;
  589. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  590. {
  591. chksum ^= rx[6+idx];
  592. }
  593. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  594. (rx[2] == tx[1]) &&
  595. (rx[1] == tx[2]) &&
  596. (rx[3] == tx[3]) &&
  597. (rx[6] == 0x00))
  598. {
  599. result = PASS;
  600. }
  601. }
  602. return result;
  603. }
  604. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  605. {
  606. unsigned char result = FAIL;
  607. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
  608. unsigned char rx[512];
  609. unsigned char chksum = 0x00;
  610. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  611. if(len > 6)
  612. {
  613. if (len < 6+(rx[4] | rx[5]<<8))
  614. return result;
  615. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  616. {
  617. chksum ^= rx[6+idx];
  618. }
  619. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  620. (rx[2] == tx[1]) &&
  621. (rx[1] == tx[2]) &&
  622. (rx[3] == tx[3]) &&
  623. (rx[6] == 0x00))
  624. {
  625. result = PASS;
  626. }
  627. }
  628. return result;
  629. }
  630. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  631. {
  632. unsigned char result = FAIL;
  633. unsigned char tx[11 + length];
  634. unsigned char rx[512];
  635. unsigned char chksum = 0x00;
  636. tx[0] = 0xaa;
  637. tx[1] = 0x00;
  638. tx[2] = targetAddr;
  639. tx[3] = Cmd.update_Transfer;
  640. tx[4] = (4 + length) & 0xff;
  641. tx[5] = ((4 + length)>>8) & 0xff;
  642. tx[6] = (startAddr>>0) & 0xff;
  643. tx[7] = (startAddr>>8) & 0xff;
  644. tx[8] = (startAddr>>16) & 0xff;
  645. tx[9] = (startAddr>>24) & 0xff;
  646. memcpy(tx+10, data, length);
  647. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  648. chksum ^= tx[6+idx];
  649. tx[sizeof(tx)-1] = chksum;
  650. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  651. if(len > 6)
  652. {
  653. if (len < 6+(rx[4] | rx[5]<<8))
  654. return result;
  655. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  656. {
  657. chksum ^= rx[6+idx];
  658. }
  659. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  660. (rx[2] == tx[1]) &&
  661. (rx[1] == tx[2]) &&
  662. (rx[3] == tx[3]) &&
  663. (rx[6] == 0x00))
  664. {
  665. result = PASS;
  666. }
  667. }
  668. return result;
  669. }
  670. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  671. {
  672. unsigned char result = FAIL;
  673. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
  674. unsigned char rx[512];
  675. unsigned char chksum = 0x00;
  676. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  677. if(len > 6)
  678. {
  679. if (len < 6+(rx[4] | rx[5]<<8))
  680. return result;
  681. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  682. {
  683. chksum ^= rx[6+idx];
  684. }
  685. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  686. (rx[2] == tx[1]) &&
  687. (rx[1] == tx[2]) &&
  688. (rx[3] == tx[3]) &&
  689. (rx[6] == 0x00))
  690. {
  691. result = PASS;
  692. }
  693. }
  694. return result;
  695. }