PrimaryComm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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, 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. usleep(50000);
  41. len = read(fd, rx, 512);
  42. } else {
  43. #ifdef SystemLogMessage
  44. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  45. #endif
  46. }
  47. return len;
  48. }
  49. unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  50. {
  51. unsigned char result = FAIL;
  52. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_FW_Ver, 0x00, 0x00, 0x00};
  53. unsigned char rx[512];
  54. unsigned char chksum = 0x00;
  55. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  56. if (len > 0) {
  57. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  58. chksum ^= rx[6 + idx];
  59. }
  60. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  61. (rx[2] == tx[1]) &&
  62. (rx[1] == tx[2]) &&
  63. (rx[3] == tx[3])) {
  64. memcpy(Ret_Buf->Version_FW, (char *)rx + 6, (rx[4] | rx[5] << 8));
  65. *(Ret_Buf->Version_FW + 8) = 0x00;
  66. result = PASS;
  67. }
  68. }
  69. return result;
  70. }
  71. unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  72. {
  73. unsigned char result = FAIL;
  74. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_HW_Ver, 0x00, 0x00, 0x00};
  75. unsigned char rx[512];
  76. unsigned char chksum = 0x00;
  77. if (tranceive(fd, tx, sizeof(tx), rx) > 0) {
  78. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  79. chksum ^= rx[6 + idx];
  80. }
  81. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  82. (rx[2] == tx[1]) &&
  83. (rx[1] == tx[2]) &&
  84. (rx[3] == tx[3])) {
  85. memcpy(Ret_Buf->Version_HW, (char *)rx + 6, (rx[4] | rx[5] << 8));
  86. //*(Ret_Buf->Version_HW + 8) = 0x00;
  87. result = PASS;
  88. }
  89. }
  90. return result;
  91. }
  92. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  93. {
  94. unsigned char result = FAIL;
  95. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
  96. unsigned char rx[512];
  97. unsigned char chksum = 0x00;
  98. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  99. if (len > 0) {
  100. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  101. chksum ^= rx[6 + idx];
  102. }
  103. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  104. (rx[2] == tx[1]) &&
  105. (rx[1] == tx[2]) &&
  106. (rx[3] == tx[3])) {
  107. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  108. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  109. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  110. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  111. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  112. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  113. Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  114. Ret_Buf->Emergency_IO = (rx[6] >> 7) & 0x01;
  115. Ret_Buf->Emergency_Btn = (rx[7] >> 0) & 0x01;
  116. Ret_Buf->Button[0] = (rx[7] >> 1) & 0x01;
  117. Ret_Buf->Button[1] = (rx[7] >> 2) & 0x01;
  118. Ret_Buf->Key[0] = (rx[7] >> 3) & 0x01;
  119. Ret_Buf->Key[1] = (rx[7] >> 4) & 0x01;
  120. Ret_Buf->Key[2] = (rx[7] >> 5) & 0x01;
  121. Ret_Buf->Key[3] = (rx[7] >> 6) & 0x01;
  122. result = PASS;
  123. }
  124. }
  125. return result;
  126. }
  127. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  128. {
  129. unsigned char result = FAIL;
  130. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
  131. unsigned char rx[512];
  132. unsigned char chksum = 0x00;
  133. for (int idx = 0; idx < 2; idx++) {
  134. tx[6] |= (Set_Buf->Button_LED[idx] ? 0x01 : 0x00) << (0 + idx);
  135. }
  136. for (int idx = 0; idx < 4; idx++) {
  137. tx[6] |= (Set_Buf->System_LED[idx] ? 0x01 : 0x00) << (2 + idx);
  138. }
  139. tx[6] |= (Set_Buf->AC_Connector ? 0x01 : 0x00) << 6;
  140. tx[6] |= (Set_Buf->AC_Breaker ? 0x01 : 0x00) << 7;
  141. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  142. chksum ^= tx[6 + idx];
  143. }
  144. tx[7] = chksum;
  145. if (tranceive(fd, tx, sizeof(tx), rx) > 0) {
  146. chksum = 0x00;
  147. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  148. chksum ^= rx[6 + idx];
  149. }
  150. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  151. (rx[2] == tx[1]) &&
  152. (rx[1] == tx[2]) &&
  153. (rx[3] == tx[3]) &&
  154. (rx[6] == tx[6])) {
  155. result = PASS;
  156. }
  157. }
  158. return result;
  159. }
  160. unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  161. {
  162. unsigned char result = FAIL;
  163. unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
  164. Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
  165. Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]
  166. };
  167. unsigned char rx[512];
  168. unsigned char chksum = 0x00;
  169. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  170. chksum ^= tx[6 + idx];
  171. }
  172. tx[20] = chksum;
  173. if (tranceive(fd, tx, sizeof(tx), rx) > 0) {
  174. chksum = 0x00;
  175. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  176. chksum ^= rx[6 + idx];
  177. }
  178. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  179. (rx[2] == tx[1]) &&
  180. (rx[1] == tx[2]) &&
  181. (rx[3] == tx[3]) &&
  182. (rx[6] == tx[6])) {
  183. result = PASS;
  184. }
  185. }
  186. return result;
  187. }
  188. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
  189. {
  190. unsigned char result = FAIL;
  191. unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
  192. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  193. };
  194. unsigned char rx[512];
  195. unsigned char chksum = 0x00;
  196. memcpy(tx + 6, modelname, 14);
  197. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  198. chksum ^= tx[6 + idx];
  199. }
  200. tx[20] = chksum;
  201. // for(int i = 0; i < 21; i++)
  202. // printf ("tx = %x \n", tx[i]);
  203. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  204. // for(int i = 0; i < len; i++)
  205. // printf ("rx = %x \n", rx[i]);
  206. if (len > 6) {
  207. if (len < 6 + (rx[4] | rx[5] << 8)) {
  208. return result;
  209. }
  210. chksum = 0x00;
  211. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  212. chksum ^= rx[6 + idx];
  213. }
  214. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  215. (rx[2] == tx[1]) &&
  216. (rx[1] == tx[2]) &&
  217. (rx[3] == tx[3]) &&
  218. rx[6] == PASS) {
  219. result = PASS;
  220. }
  221. }
  222. return result;
  223. }
  224. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  225. {
  226. unsigned char result = FAIL;
  227. 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};
  228. unsigned char rx[512];
  229. unsigned char chksum = 0x00;
  230. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  231. chksum ^= tx[6 + idx];
  232. }
  233. tx[10] = chksum;
  234. if (tranceive(fd, tx, sizeof(tx), rx) > 0) {
  235. chksum = 0x00;
  236. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  237. chksum ^= rx[6 + idx];
  238. }
  239. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  240. (rx[2] == tx[1]) &&
  241. (rx[1] == tx[2]) &&
  242. (rx[3] == tx[3]) &&
  243. (rx[6] == 0x00)) {
  244. result = PASS;
  245. }
  246. }
  247. return result;
  248. }
  249. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  250. {
  251. unsigned char result = FAIL;
  252. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
  253. unsigned char rx[512];
  254. unsigned char chksum = 0x00;
  255. if (tranceive(fd, tx, sizeof(tx), rx) > 0) {
  256. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  257. chksum ^= rx[6 + idx];
  258. }
  259. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  260. (rx[2] == tx[1]) &&
  261. (rx[1] == tx[2]) &&
  262. (rx[3] == tx[3]) &&
  263. (rx[6] == 0x00)) {
  264. result = PASS;
  265. }
  266. }
  267. return result;
  268. }
  269. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  270. {
  271. unsigned char result = FAIL;
  272. unsigned char tx[11 + length];
  273. unsigned char rx[512];
  274. unsigned char chksum = 0x00;
  275. tx[0] = 0xaa;
  276. tx[1] = 0x00;
  277. tx[2] = targetAddr;
  278. tx[3] = Cmd.update_Transfer;
  279. tx[4] = (4 + length) & 0xff;
  280. tx[5] = ((4 + length) >> 8) & 0xff;
  281. tx[6] = (startAddr >> 0) & 0xff;
  282. tx[7] = (startAddr >> 8) & 0xff;
  283. tx[8] = (startAddr >> 16) & 0xff;
  284. tx[9] = (startAddr >> 24) & 0xff;
  285. memcpy(tx + 10, data, length);
  286. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  287. chksum ^= tx[6 + idx];
  288. }
  289. tx[sizeof(tx) - 1] = chksum;
  290. if (tranceive(fd, tx, sizeof(tx), rx) > 0) {
  291. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  292. chksum ^= rx[6 + idx];
  293. }
  294. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  295. (rx[2] == tx[1]) &&
  296. (rx[1] == tx[2]) &&
  297. (rx[3] == tx[3]) &&
  298. (rx[6] == 0x00)) {
  299. result = PASS;
  300. }
  301. }
  302. return result;
  303. }
  304. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  305. {
  306. unsigned char result = FAIL;
  307. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
  308. unsigned char rx[512];
  309. unsigned char chksum = 0x00;
  310. if (tranceive(fd, tx, sizeof(tx), rx) > 0) {
  311. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  312. chksum ^= rx[6 + idx];
  313. }
  314. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  315. (rx[2] == tx[1]) &&
  316. (rx[1] == tx[2]) &&
  317. (rx[3] == tx[3]) &&
  318. (rx[6] == 0x00)) {
  319. result = PASS;
  320. }
  321. }
  322. return result;
  323. }