PrimaryComm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <string.h>
  23. #include <time.h>
  24. #include <ctype.h>
  25. #include <ifaddrs.h>
  26. #include <math.h>
  27. #include "PrimaryComm.h"
  28. #define PASS 1
  29. #define FAIL -1
  30. struct Address Addr={0x01,0x02,0x03,0x04,0xFF};
  31. struct Command Cmd={0x01,0x02,0x0a,0x2C,0x83,0x86,0x87,0xe0,0xe1,0xe2,0xe3};
  32. int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
  33. {
  34. int len;
  35. //sleep(2); //required to make flush work, for some reason
  36. tcflush(fd,TCIOFLUSH);
  37. if(write(fd, cmd, cmd_len) >= cmd_len)
  38. {
  39. usleep(50000);
  40. len = read(fd, rx, 512);
  41. }
  42. else
  43. {
  44. #ifdef SystemLogMessage
  45. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  46. #endif
  47. }
  48. return len;
  49. }
  50. unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  51. {
  52. unsigned char result = FAIL;
  53. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_FW_Ver, 0x00, 0x00, 0x00};
  54. unsigned char rx[512];
  55. unsigned char chksum = 0x00;
  56. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  57. if(len > 0)
  58. {
  59. for(int idx = 0; idx < (rx[4] | rx[5]<<8); idx++)
  60. {
  61. chksum ^= rx[6+idx];
  62. }
  63. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  64. (rx[2] == tx[1]) &&
  65. (rx[1] == tx[2]) &&
  66. (rx[3] == tx[3]))
  67. {
  68. memcpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
  69. *(Ret_Buf->Version_FW + 8) = 0x00;
  70. result = PASS;
  71. }
  72. }
  73. return result;
  74. }
  75. unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  76. {
  77. unsigned char result = FAIL;
  78. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_HW_Ver, 0x00, 0x00, 0x00};
  79. unsigned char rx[512];
  80. unsigned char chksum = 0x00;
  81. if(tranceive(fd, tx, sizeof(tx), rx) >0)
  82. {
  83. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  84. {
  85. chksum ^= rx[6+idx];
  86. }
  87. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  88. (rx[2] == tx[1]) &&
  89. (rx[1] == tx[2]) &&
  90. (rx[3] == tx[3]))
  91. {
  92. memcpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
  93. //*(Ret_Buf->Version_HW + 8) = 0x00;
  94. result = PASS;
  95. }
  96. }
  97. return result;
  98. }
  99. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  100. {
  101. unsigned char result = FAIL;
  102. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
  103. unsigned char rx[512];
  104. unsigned char chksum = 0x00;
  105. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  106. if(len > 0)
  107. {
  108. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  109. {
  110. chksum ^= rx[6+idx];
  111. }
  112. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  113. (rx[2] == tx[1]) &&
  114. (rx[1] == tx[2]) &&
  115. (rx[3] == tx[3]))
  116. {
  117. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  118. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  119. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  120. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  121. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  122. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  123. Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  124. Ret_Buf->Emergency_IO = (rx[6] >> 7) & 0x01;
  125. Ret_Buf->Emergency_Btn = (rx[7] >> 0) & 0x01;
  126. Ret_Buf->Button[0] = (rx[7] >> 1) & 0x01;
  127. Ret_Buf->Button[1] = (rx[7] >> 2) & 0x01;
  128. Ret_Buf->Key[0] = (rx[7] >> 3) & 0x01;
  129. Ret_Buf->Key[1] = (rx[7] >> 4) & 0x01;
  130. Ret_Buf->Key[2] = (rx[7] >> 5) & 0x01;
  131. Ret_Buf->Key[3] = (rx[7] >> 6) & 0x01;
  132. result = PASS;
  133. }
  134. }
  135. return result;
  136. }
  137. unsigned char Query_Meter_value(unsigned char fd, unsigned char targetAddr, struct StructMeter *Ret_Buf, byte *isWork, unsigned char index)
  138. {
  139. unsigned char result = FAIL;
  140. unsigned char tx[8] = {0xaa, 0x00, targetAddr, Cmd.query_charging_power, 0x01, 0x00, index, 0x00};
  141. unsigned char rx[512];
  142. unsigned char chksum = 0x00;
  143. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  144. chksum ^= tx[6 + idx];
  145. tx[7] = chksum;
  146. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  147. if(len > 0)
  148. {
  149. chksum = 0x00;
  150. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  151. {
  152. chksum ^= rx[6+idx];
  153. }
  154. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  155. (rx[2] == tx[1]) &&
  156. (rx[1] == tx[2]) &&
  157. (rx[3] == tx[3]))
  158. {
  159. Ret_Buf->newMeterValue = (rx[6] | (rx[7]<<8) | (rx[8]<<8) | (rx[9]<<8));
  160. *isWork = rx[23];
  161. result = PASS;
  162. }
  163. }
  164. return result;
  165. }
  166. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  167. {
  168. unsigned char result = FAIL;
  169. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
  170. unsigned char rx[512];
  171. unsigned char chksum = 0x00;
  172. for (int idx = 0; idx < 2; idx++)
  173. tx[6] |= (Set_Buf->Button_LED[idx] ? 0x01:0x00) << (0+idx);
  174. for (int idx = 0; idx < 4; idx++)
  175. tx[6] |= (Set_Buf->System_LED[idx] ? 0x01:0x00) << (2+idx);
  176. tx[6] |= (Set_Buf->AC_Connector ? 0x01:0x00) << 6;
  177. tx[6] |= (Set_Buf->AC_Breaker ? 0x01:0x00) << 7;
  178. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  179. chksum ^= tx[6+idx];
  180. tx[7] = chksum;
  181. if (tranceive(fd, tx, sizeof(tx), rx) > 0)
  182. {
  183. chksum = 0x00;
  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. (rx[6] == tx[6]))
  193. {
  194. result = PASS;
  195. }
  196. }
  197. return result;
  198. }
  199. unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  200. {
  201. unsigned char result = FAIL;
  202. unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
  203. Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
  204. Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]};
  205. unsigned char rx[512];
  206. unsigned char chksum = 0x00;
  207. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  208. chksum ^= tx[6 + idx];
  209. tx[20] = chksum;
  210. if (tranceive(fd, tx, sizeof(tx), rx) > 0)
  211. {
  212. chksum = 0x00;
  213. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  214. {
  215. chksum ^= rx[6 + idx];
  216. }
  217. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  218. (rx[2] == tx[1]) &&
  219. (rx[1] == tx[2]) &&
  220. (rx[3] == tx[3]) &&
  221. (rx[6] == tx[6]))
  222. {
  223. result = PASS;
  224. }
  225. }
  226. return result;
  227. }
  228. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
  229. {
  230. unsigned char result = FAIL;
  231. unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
  232. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  233. unsigned char rx[512];
  234. unsigned char chksum = 0x00;
  235. memcpy(tx + 6, modelname, 14);
  236. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  237. chksum ^= tx[6+idx];
  238. tx[20] = chksum;
  239. // for(int i = 0; i < 21; i++)
  240. // printf ("tx = %x \n", tx[i]);
  241. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  242. // for(int i = 0; i < len; i++)
  243. // printf ("rx = %x \n", rx[i]);
  244. if(len > 6)
  245. {
  246. if (len < 6+(rx[4] | rx[5]<<8))
  247. return result;
  248. chksum = 0x00;
  249. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  250. {
  251. chksum ^= rx[6+idx];
  252. }
  253. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  254. (rx[2] == tx[1]) &&
  255. (rx[1] == tx[2]) &&
  256. (rx[3] == tx[3]) &&
  257. rx[6] == PASS)
  258. {
  259. result = PASS;
  260. }
  261. }
  262. return result;
  263. }
  264. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  265. {
  266. unsigned char result = FAIL;
  267. 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};
  268. unsigned char rx[512];
  269. unsigned char chksum = 0x00;
  270. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  271. chksum ^= tx[6+idx];
  272. tx[10] = chksum;
  273. if(tranceive(fd, tx, sizeof(tx), rx) > 0)
  274. {
  275. chksum = 0x00;
  276. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  277. {
  278. chksum ^= rx[6+idx];
  279. }
  280. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  281. (rx[2] == tx[1]) &&
  282. (rx[1] == tx[2]) &&
  283. (rx[3] == tx[3]) &&
  284. (rx[6] == 0x00))
  285. {
  286. result = PASS;
  287. }
  288. }
  289. return result;
  290. }
  291. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  292. {
  293. unsigned char result = FAIL;
  294. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
  295. unsigned char rx[512];
  296. unsigned char chksum = 0x00;
  297. if(tranceive(fd, tx, sizeof(tx), rx) >0)
  298. {
  299. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  300. {
  301. chksum ^= rx[6+idx];
  302. }
  303. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  304. (rx[2] == tx[1]) &&
  305. (rx[1] == tx[2]) &&
  306. (rx[3] == tx[3]) &&
  307. (rx[6] == 0x00))
  308. {
  309. result = PASS;
  310. }
  311. }
  312. return result;
  313. }
  314. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  315. {
  316. unsigned char result = FAIL;
  317. unsigned char tx[11 + length];
  318. unsigned char rx[512];
  319. unsigned char chksum = 0x00;
  320. tx[0] = 0xaa;
  321. tx[1] = 0x00;
  322. tx[2] = targetAddr;
  323. tx[3] = Cmd.update_Transfer;
  324. tx[4] = (4 + length) & 0xff;
  325. tx[5] = ((4 + length)>>8) & 0xff;
  326. tx[6] = (startAddr>>0) & 0xff;
  327. tx[7] = (startAddr>>8) & 0xff;
  328. tx[8] = (startAddr>>16) & 0xff;
  329. tx[9] = (startAddr>>24) & 0xff;
  330. memcpy(tx+10, data, length);
  331. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  332. chksum ^= tx[6+idx];
  333. tx[sizeof(tx)-1] = chksum;
  334. if(tranceive(fd, tx, sizeof(tx), rx) >0)
  335. {
  336. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  337. {
  338. chksum ^= rx[6+idx];
  339. }
  340. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  341. (rx[2] == tx[1]) &&
  342. (rx[1] == tx[2]) &&
  343. (rx[3] == tx[3]) &&
  344. (rx[6] == 0x00))
  345. {
  346. result = PASS;
  347. }
  348. }
  349. return result;
  350. }
  351. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  352. {
  353. unsigned char result = FAIL;
  354. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
  355. unsigned char rx[512];
  356. unsigned char chksum = 0x00;
  357. if(tranceive(fd, tx, sizeof(tx), rx) >0)
  358. {
  359. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  360. {
  361. chksum ^= rx[6+idx];
  362. }
  363. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  364. (rx[2] == tx[1]) &&
  365. (rx[1] == tx[2]) &&
  366. (rx[3] == tx[3]) &&
  367. (rx[6] == 0x00))
  368. {
  369. result = PASS;
  370. }
  371. }
  372. return result;
  373. }