You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.7 KiB

  1. #include "stm32f4xx_hal.h"
  2. #include "lwip.h"
  3. #include "udp.h"
  4. #include "string.h"
  5. #include "udpclient.h"
  6. #include "socket.h"
  7. #include "sockets.h"
  8. #include "def.h"
  9. #include "protocol.h"
  10. #include "encoder.h"
  11. static bool udp_client_active_flag;
  12. /* 定义端口号 */
  13. #define UDP_REMOTE_PORT 8881 /* 远端端口 */
  14. #define UDP_LOCAL_PORT 8880 /* 本地端口 */
  15. #define SERVER_FAMILY AF_INET
  16. #define SOCKET_ERROR -1
  17. #define BUFFER_SIZE 256 // 缓冲区大小
  18. #define ACTIVE_ID 0XDD
  19. #define DEVICE_ID 0X01
  20. static char s_sendBuf[BUFFER_SIZE]; // 发送数据的缓冲区
  21. static char s_receBuf[BUFFER_SIZE]; // 接收数据的缓冲区
  22. SOCKET sock_Client = 0; // 客户端用于通信的Socket
  23. static struct sockaddr_in addr_server;
  24. static struct sockaddr_in addr_client;
  25. static struct sockaddr_in sock;
  26. socklen_t sock_len = sizeof(sock);
  27. active_report_data_t active_report_data_structer;
  28. static void active_report_data_structer_init(void)
  29. {
  30. active_report_data_structer.index = 0; /* */
  31. active_report_data_structer.cmd_id = ACTIVE_ID; /* 指令id */
  32. active_report_data_structer.time_stamp_s = 0; /* 时间戳 */
  33. active_report_data_structer.encoder_1_count = 0; /* 编码器1计数 */
  34. active_report_data_structer.encoder_2_count = 0; /* 编码器2计数 */
  35. active_report_data_structer.device_id = DEVICE_ID; /* 设备ID号 */
  36. }
  37. static void active_report_data_structer_update(void)
  38. {
  39. /* 时间戳暂时先不管,后续完善 */
  40. encoder_read_with_encoder(CAMERA_ENCODER, &active_report_data_structer.encoder_1_count);
  41. encoder_read_with_encoder(DRIVEN_ENCODER_GEAR, &active_report_data_structer.encoder_2_count);
  42. }
  43. static void udp_client_recv_data_dump(int recv_datalen)
  44. {
  45. for (size_t i = 0; i < recv_datalen; i++)
  46. {
  47. printf("%d ", s_receBuf[i]);
  48. }
  49. printf("\r\n");
  50. }
  51. static void udp_client_create_basic_response(basic_report_data_t *rxcmd, int recv_datalen)
  52. {
  53. /* 因为发送和接收是一致的,所以不需要二次校验,直接把接收的数据发送即可 */
  54. if (sendto(sock_Client, rxcmd, sizeof(basic_report_data_t), 0, (struct sockaddr *)&addr_server, sizeof(struct sockaddr_in)) == SOCKET_ERROR)
  55. {
  56. printf("send basic_response error\r\n");
  57. }
  58. }
  59. void config_server(struct sockaddr_in *addr_server)
  60. {
  61. addr_server->sin_family = SERVER_FAMILY;
  62. addr_server->sin_port = htons(UDP_REMOTE_PORT);
  63. addr_server->sin_addr.s_addr = htonl(INADDR_BROADCAST);
  64. }
  65. void config_client(struct sockaddr_in *addr_client)
  66. {
  67. addr_client->sin_family = AF_INET;
  68. addr_client->sin_addr.s_addr = inet_addr("0.0.0.0");
  69. addr_client->sin_port = htons(UDP_LOCAL_PORT);
  70. }
  71. void udp_client_send_string(char *pData)
  72. {
  73. memcpy(s_sendBuf, pData, strlen(pData));
  74. if (sendto(sock_Client, s_sendBuf, strlen(pData), 0, (struct sockaddr *)&addr_server, sizeof(struct sockaddr_in)) == SOCKET_ERROR)
  75. {
  76. printf("send string error\n");
  77. }
  78. }
  79. void udp_client_init(void)
  80. {
  81. active_report_data_structer_init();
  82. // bool bOpt = true;
  83. struct timeval tv = {0, 20000};
  84. config_server(&addr_server);
  85. config_client(&addr_client);
  86. // 创建客户端用于通信的Socket
  87. sock_Client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  88. if (sock_Client == SOCKET_ERROR)
  89. {
  90. printf("create socket error...\n");
  91. }
  92. else
  93. {
  94. printf("create socket success!\n");
  95. }
  96. // /* 开启广播 */
  97. // if (lwip_setsockopt(sock_Client, SOL_SOCKET, SO_BROADCAST, (char *)&bOpt, sizeof(bOpt)) == SOCKET_ERROR)
  98. // {
  99. // printf("enable broadcast error...\n");
  100. // }
  101. // else
  102. // {
  103. // printf("enable broadcast success!\n");
  104. // }
  105. /* 设置超时 */
  106. if (lwip_setsockopt(sock_Client, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) == SOCKET_ERROR)
  107. {
  108. printf("enable receive timeout error...\n");
  109. }
  110. else
  111. {
  112. printf("enable receive timeout success!\n");
  113. }
  114. /* 绑定 */
  115. if (bind(sock_Client, (struct sockaddr *)&addr_client, sizeof(addr_client)) == SOCKET_ERROR)
  116. {
  117. printf("Bind failed");
  118. return;
  119. }
  120. }
  121. void udp_client_recv(void)
  122. {
  123. int recv_datalen = recvfrom(sock_Client, s_receBuf, sizeof(s_receBuf), 0, (struct sockaddr *)&sock, &sock_len);
  124. if (recv_datalen > 0)
  125. {
  126. printf("udp recv data len:%d\r\n", recv_datalen);
  127. udp_client_parse(recv_datalen);
  128. }
  129. }
  130. void udp_client_parse(int recv_datalen)
  131. {
  132. udp_client_recv_data_dump(recv_datalen);
  133. if (recv_datalen >= adwin_config_protocol_size)
  134. {
  135. // adwin_config_protocol_t *rxcmd = (adwin_config_protocol_t *)s_receBuf;
  136. }
  137. else if (recv_datalen <= basic_report_data_size)
  138. {
  139. basic_report_data_t *rxcmd = (basic_report_data_t *)s_receBuf;
  140. if (computesum8((char *)rxcmd, recv_datalen - 1) != rxcmd->checksum)
  141. {
  142. /* 校验失败 */
  143. printf("checksum error\r\n");
  144. return;
  145. }
  146. switch (rxcmd->cmd_id)
  147. {
  148. case CMD_SET_AUTOMATIC_REPORTING_FREQUENCY: /* 设置自动上报频率 */
  149. /* code */
  150. break;
  151. case CMD_GET_ENCODER_DATA: /* 获取编码器数据 */
  152. /* code */
  153. break;
  154. case CMD_SET_ACTIVE: /* 设置主动上报 */
  155. if ((rxcmd->data == 0) || (rxcmd->data == 1))
  156. {
  157. udp_client_active_flag = rxcmd->data;
  158. }
  159. udp_client_create_basic_response(rxcmd, recv_datalen);
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. }
  166. void udp_cllient_active(void)
  167. {
  168. if (udp_client_active_flag)
  169. {
  170. active_report_data_structer_update();
  171. printf("encoder1:%d,encoder2:%d\r\n", active_report_data_structer.encoder_1_count, active_report_data_structer.encoder_2_count);
  172. }
  173. }