PrimaryComm.c 11 KB

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