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.

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