PrimaryComm.c 11 KB

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