net.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "net.h"
  2. #include "fly_param.h"
  3. #include "iec104.h"
  4. #include "iec104_queue.h"
  5. #include "os_cpu.h"
  6. #include "stm32f4xx_gpio.h"
  7. #include "ucos_ii.h"
  8. #include <stdio.h>
  9. INT32S g_iec104_socket = -1; /* 创建与104交互的socket */
  10. INT32S g_modbus_tcp_socket = -1; /* 创建与104交互的socket */
  11. INT32S g_modbus_tcp_socket_link_state = 0; /* 与4G模块Socket信息初始化标志:1初始化完成 0:未初始化 */
  12. INT32S g_iec104_socket_link_state = 0; /* 与4G模块Socket信息初始化标志:1初始化完成 0:未初始化 */
  13. u8 g_data_req_flag = 0;
  14. void fly_mosbus_init_flag(void)
  15. {
  16. g_data_req_flag = 0;
  17. return;
  18. }
  19. int modbus_tcp_rec_module(INT8U *rec_buf)
  20. {
  21. INT32S buf_len = 0;
  22. buf_len = recv(g_modbus_tcp_socket, rec_buf, MAX_RECV_SIZE, 0);
  23. if (buf_len <= 0)
  24. {
  25. fly_mosbus_init_flag();
  26. closesocket(g_modbus_tcp_socket);
  27. g_modbus_tcp_socket = -1;
  28. return -1;
  29. }
  30. return buf_len;
  31. }
  32. err_t fly_test_process(INT8U *rec_buf, INT32S buf_len)
  33. {
  34. INT8U response[2] = {0xBB, 0xAA};
  35. if (((buf_len == 2) && ((INT8U *)rec_buf)[0] == 0xAA) && (((INT8U *)rec_buf)[1] == 0xBB))
  36. {
  37. send(g_modbus_tcp_socket, (void *)response, sizeof(response), 0);
  38. }
  39. return 1;
  40. }
  41. void iec104_task(void)
  42. {
  43. /* socket 创建 */
  44. INT8U buf[500];
  45. INT32S ret = 0;
  46. static INT32S iec104_sockfd;
  47. static struct sockaddr_in svr_addr = {0};
  48. struct sockaddr_in clt_addr;
  49. INT32U non_block = 1;
  50. // if (g_iec104_socket_link_state == 1)
  51. // {
  52. // goto SOCKET_LINK;
  53. // }
  54. // iec104_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  55. // if (iec104_sockfd < 0)
  56. // {
  57. // return -1;
  58. // }
  59. // memset(&svr_addr, 0, sizeof(struct sockaddr_in));
  60. // /* 端口号设置为2404 */
  61. // svr_addr.sin_family = AF_INET;
  62. // svr_addr.sin_port = htons(IEC_PORT);
  63. // svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
  64. // /* 绑定 */
  65. // ret = bind(iec104_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  66. // if (ret < 0)
  67. // {
  68. // lwip_close(iec104_sockfd);
  69. // return -1;
  70. // }
  71. // /* 监听1个连接 */
  72. // ret = listen(iec104_sockfd, 1);
  73. // if (ret < 0)
  74. // {
  75. // lwip_close(iec104_sockfd);
  76. // return -1;
  77. // }
  78. // g_iec104_socket_link_state = 1; /* 初始化完成后, socket信息标志置位 */
  79. // SOCKET_LINK:
  80. // /* 建链 */
  81. // len = sizeof(clt_addr);
  82. // g_iec104_socket = accept(iec104_sockfd, (struct sockaddr *)&clt_addr, len);
  83. // if (g_iec104_socket < 0)
  84. // {
  85. // return g_iec104_socket;
  86. // }
  87. // ret = ioctlsocket(g_iec104_socket, FIONBIO, (u_long *)&non_block);
  88. // if (ret <= 0)
  89. // {
  90. // // DEBUG("the other side has been closed (%d).\n", sizeof(buf) - 1);
  91. // lwip_close(g_iec104_socket);
  92. // g_iec104_socket = -1;
  93. // }
  94. // return g_iec104_socket;
  95. svr_addr.sin_family = AF_INET;
  96. svr_addr.sin_port = htons(IEC_PORT);
  97. svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
  98. while (1)
  99. {
  100. iec104_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  101. if (iec104_sockfd < 0)
  102. {
  103. continue;
  104. }
  105. ret = bind(iec104_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  106. if (ret < 0)
  107. {
  108. lwip_close(iec104_sockfd);
  109. iec104_sockfd = -1;
  110. continue;
  111. }
  112. ret = listen(iec104_sockfd, 1);
  113. if (ret < 0)
  114. {
  115. lwip_close(iec104_sockfd);
  116. continue;
  117. }
  118. g_iec104_socket = accept(iec104_sockfd, NULL, NULL);
  119. while (-1 != g_iec104_socket)
  120. {
  121. ret = recv(g_iec104_socket, buf, sizeof(buf) - 1, 0);
  122. if (ret <= 0)
  123. {
  124. lwip_close(g_iec104_socket);
  125. g_iec104_socket = -1;
  126. break;
  127. }
  128. dump_hex(buf, ret);
  129. iec104_receive(buf, ret);
  130. }
  131. lwip_close(iec104_sockfd);
  132. iec104_sockfd = -1;
  133. }
  134. }
  135. INT32S iec104_rec_master(char *rec_buf)
  136. {
  137. INT32S buf_len = 0;
  138. buf_len = recv(g_iec104_socket, rec_buf, MAX_RECV_SIZE, 0);
  139. if (buf_len <= 0)
  140. {
  141. // g_data_req_flag = 0;
  142. closesocket(g_iec104_socket);
  143. g_iec104_socket = -1;
  144. return -1;
  145. }
  146. iec104_receive(rec_buf, buf_len);
  147. return buf_len;
  148. }
  149. int modbus_tcp_connect_module(void)
  150. {
  151. /* socket 创建 */
  152. INT8U buf[500];
  153. INT32S ret = 0;
  154. static INT32S modbus_tcp_sockfd;
  155. static struct sockaddr_in svr_addr = {0};
  156. struct sockaddr_in clt_addr;
  157. INT32U len = 0;
  158. INT32U non_block = 1;
  159. if (g_modbus_tcp_socket_link_state == 1)
  160. {
  161. goto SOCKET_LINK;
  162. }
  163. modbus_tcp_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  164. if (modbus_tcp_sockfd < 0)
  165. {
  166. return -1;
  167. }
  168. memset(&svr_addr, 0, sizeof(struct sockaddr_in));
  169. /* 端口号设置为8080 */
  170. svr_addr.sin_family = AF_INET;
  171. svr_addr.sin_port = htons(TCP_PORT);
  172. svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
  173. /* 绑定 */
  174. ret = bind(modbus_tcp_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  175. if (ret < 0)
  176. {
  177. lwip_close(modbus_tcp_sockfd);
  178. return -1;
  179. }
  180. /* 监听1个连接 */
  181. ret = listen(modbus_tcp_sockfd, 1);
  182. if (ret < 0)
  183. {
  184. lwip_close(modbus_tcp_sockfd);
  185. return -1;
  186. }
  187. g_modbus_tcp_socket_link_state = 1; /* 初始化完成后, socket信息标志置位 */
  188. SOCKET_LINK:
  189. /* 建链 */
  190. g_modbus_tcp_socket = accept(modbus_tcp_sockfd, NULL, NULL);
  191. if (g_modbus_tcp_socket < 0)
  192. {
  193. return g_modbus_tcp_socket;
  194. }
  195. ret = ioctlsocket(g_modbus_tcp_socket, FIONBIO, (u_long *)&non_block);
  196. if (ret < 0)
  197. {
  198. lwip_close(g_modbus_tcp_socket);
  199. g_modbus_tcp_socket = -1;
  200. }
  201. return g_modbus_tcp_socket;
  202. }