signal_generator/User/application/src/tcpserverc.c

284 lines
8.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "tcpserverc.h"
#include "lwip/netif.h"
#include "lwip/ip.h"
#include "lwip/tcp.h"
#include "lwip/init.h"
#include "netif/etharp.h"
#include "lwip/udp.h"
#include "lwip/pbuf.h"
#include <stdio.h>
#include <string.h>
#include "usart.h"
#include "main.h"
#include "ht1200m.h"
struct tcp_pcb *server_pcb1 = NULL;
struct tcp_pcb *server_pcb2 = NULL;
extern uint8_t tcp_echo_flags;
extern uint8_t tcp_echo_flags_hart2;
/*接收回调函数*/
static err_t tcpecho_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{ // 对应接收数据连接的控制块 接收到的数据
if (p != NULL)
{
/* 更新窗口*/
tcp_echo_flags = 1;
tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度
memcpy(&server_pcb1, &tpcb, sizeof(struct tcp_pcb *));
#if 0
memcpy(hart2_uart2.tx_data, (int *)p->payload, p->tot_len);
HART2_RTS_SEND;
HAL_UART_Transmit(&huart2, hart2_uart2.tx_data, p->tot_len, 1000);
HART2_RTS_RECEIVE;
#endif
#if 1
memcpy(hart1_uart5.tx_data, (int *)p->payload, p->tot_len);
HART1_RTS_SEND;
HAL_UART_Transmit(&huart5, hart1_uart5.tx_data, p->tot_len, 100);
HART1_RTS_RECEIVE;
#endif
#if 0
memcpy(ble1_uart6.tx_data, (int *)p->payload, p->tot_len);
dma_usart_send(&huart6, ble1_uart6.tx_data, p->tot_len);
#endif
#if 0
memcpy(ble2_uart3.tx_data, (int *)p->payload, p->tot_len);
dma_usart_send(&huart3, ble2_uart3.tx_data, p->tot_len);
#endif
#if 0
memcpy(lcd_uart4.tx_data, (int *)p->payload, p->tot_len);
dma_usart_send(&huart4, lcd_uart4.tx_data, p->tot_len);
#endif
memset(p->payload, 0, p->tot_len);
pbuf_free(p);
}
else if (err == ERR_OK) // 检测到对方主动关闭连接时也会调用recv函数此时p为空
{
return tcp_close(tpcb);
}
return ERR_OK;
}
static err_t tcpecho_recv_hart2(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{ // 对应接收数据连接的控制块 接收到的数据
if (p != NULL)
{
/* 更新窗口*/
tcp_echo_flags_hart2 = 1;
tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度
memcpy(&server_pcb2, &tpcb, sizeof(struct tcp_pcb *));
#if 1
memcpy(hart2_uart2.tx_data, (int *)p->payload, p->tot_len);
HART2_RTS_SEND;
HAL_UART_Transmit(&huart2, hart2_uart2.tx_data, p->tot_len, 1000);
HART2_RTS_RECEIVE;
#endif
#if 0
memcpy(hart1_uart5.tx_data, (int *)p->payload, p->tot_len);
HART1_RTS_SEND;
HAL_UART_Transmit(&huart5, hart1_uart5.tx_data, p->tot_len, 100);
HART1_RTS_RECEIVE;
#endif
#if 0
memcpy(ble1_uart6.tx_data, (int *)p->payload, p->tot_len);
dma_usart_send(&huart6, ble1_uart6.tx_data, p->tot_len);
#endif
#if 0
memcpy(ble2_uart3.tx_data, (int *)p->payload, p->tot_len);
dma_usart_send(&huart3, ble2_uart3.tx_data, p->tot_len);
#endif
#if 0
memcpy(lcd_uart4.tx_data, (int *)p->payload, p->tot_len);
dma_usart_send(&huart4, lcd_uart4.tx_data, p->tot_len);
#endif
memset(p->payload, 0, p->tot_len);
pbuf_free(p);
}
else if (err == ERR_OK) // 检测到对方主动关闭连接时也会调用recv函数此时p为空
{
return tcp_close(tpcb);
}
return ERR_OK;
}
static err_t tcpecho_accept(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的
// 形参的数量和类型必须一致
{
tcp_recv(newpcb, tcpecho_recv); // 当收到数据时回调用户自己写的tcpecho_recv
return ERR_OK;
}
static err_t tcpecho_accept_hart2(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的
// 形参的数量和类型必须一致
{
tcp_recv(newpcb, tcpecho_recv_hart2); // 当收到数据时回调用户自己写的tcpecho_recv
return ERR_OK;
}
void tcp_echo_init(void)
{
struct tcp_pcb *server_pcb_hart1 = NULL;
struct tcp_pcb *server_pcb_hart2 = NULL;
/* 创建一个TCP控制块 */
server_pcb_hart1 = tcp_new();
/* 绑定TCP控制块 */
tcp_bind(server_pcb_hart1, IP_ADDR_ANY, TCP_PORT_HART1);
/* 进入监听状态 */
server_pcb_hart1 = tcp_listen(server_pcb_hart1);
/* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */
tcp_accept(server_pcb_hart1, tcpecho_accept); // 侦听到连接后回调用户编写的tcpecho_accept
/*************************************************************************/
/* 创建一个TCP控制块 */
server_pcb_hart2 = tcp_new();
/* 绑定TCP控制块 */
tcp_bind(server_pcb_hart2, IP_ADDR_ANY, TCP_PORT_HART2);
/* 进入监听状态 */
server_pcb_hart2 = tcp_listen(server_pcb_hart2);
/* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */
tcp_accept(server_pcb_hart2, tcpecho_accept_hart2); // 侦听到连接后回调用户编写的tcpecho_accept
}
void user_send_data(uint8_t *data, uint16_t len)
{
if (tcp_echo_flags == 1)
{
tcp_write(server_pcb1, data, len, 1);
}
}
void user_send_data_hart2(uint8_t *data, uint16_t len)
{
if (tcp_echo_flags_hart2 == 1)
{
tcp_write(server_pcb2, data, len, 1);
}
}
// #include "lwip/tcp.h"
// #include "lwip/pbuf.h"
// #include "lwip/err.h"
// #include "lwip/sys.h"
// #include "stm32f4xx_hal.h" // 根据具体的 STM32 系列修改头文件
// // 定义多个端口号
// #define PORT1 5000
// #define PORT2 6000
// // 定义 TCP 连接状态
// enum tcp_states {
// TCP_STATE_CLOSED,
// TCP_STATE_LISTEN,
// TCP_STATE_CONNECTED
// };
// // 定义 TCP 连接结构体
// struct tcp_connection {
// struct tcp_pcb *pcb;
// enum tcp_states state;
// };
// // TCP 接收回调函数
// static err_t tcp_recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
// if (err == ERR_OK && p!= NULL) {
// // 打印接收到的数据
// for (u16_t i = 0; i < p->len; i++) {
// printf("Received on TCP: %c", ((char *)p->payload)[i]);
// }
// printf("\n");
// // 释放接收缓冲区
// pbuf_free(p);
// }
// return ERR_OK;
// }
// // TCP 错误回调函数
// static void tcp_err_callback(void *arg, err_t err) {
// struct tcp_connection *conn = (struct tcp_connection *)arg;
// if (conn->pcb!= NULL) {
// tcp_close(conn->pcb);
// mem_free(conn);
// }
// }
// // TCP 连接回调函数
// static err_t tcp_accept_callback(void *arg, struct tcp_pcb *pcb, err_t err) {
// struct tcp_connection *conn = (struct tcp_connection *)mem_malloc(sizeof(struct tcp_connection));
// if (conn == NULL) {
// tcp_close(pcb);
// return ERR_MEM;
// }
// conn->pcb = pcb;
// conn->state = TCP_STATE_CONNECTED;
// tcp_arg(pcb, conn);
// tcp_recv(pcb, tcp_recv_callback);
// tcp_err(pcb, tcp_err_callback);
// return ERR_OK;
// }
// int main(void) {
// struct tcp_pcb *pcb1;
// struct tcp_pcb *pcb2;
// struct tcp_connection *conn1;
// struct tcp_connection *conn2;
// // 初始化 STM32 硬件(例如以太网)
// HAL_Init();
// // 初始化 LwIP 协议栈
// lwip_init();
// // 创建第一个 TCP PCB
// pcb1 = tcp_new();
// if (pcb1 == NULL) {
// printf("Error creating TCP PCB1\n");
// return -1;
// }
// if (tcp_bind(pcb1, IP_ADDR_ANY, PORT1)!= ERR_OK) {
// printf("Error binding TCP PCB1 to port %d\n", PORT1);
// tcp_close(pcb1);
// return -1;
// }
// pcb1 = tcp_listen(pcb1);
// conn1 = mem_malloc(sizeof(struct tcp_connection));
// if (conn1 == NULL) {
// tcp_close(pcb1);
// return -1;
// }
// conn1->pcb = pcb1;
// conn1->state = TCP_STATE_LISTEN;
// tcp_arg(pcb1, conn1);
// tcp_accept(pcb1, tcp_accept_callback);
// // 创建第二个 TCP PCB
// pcb2 = tcp_new();
// if (pcb2 == NULL) {
// printf("Error creating TCP PCB2\n");
// return -1;
// }
// if (tcp_bind(pcb2, IP_ADDR_ANY, PORT2)!= ERR_OK) {
// printf("Error binding TCP PCB2 to port %d\n", PORT2);
// tcp_close(pcb2);
// return -1;
// }
// pcb2 = tcp_listen(pcb2);
// conn2 = mem_malloc(sizeof(struct tcp_connection));
// if (conn2 == NULL) {
// tcp_close(pcb2);
// return -1;
// }
// conn2->pcb = pcb2;
// conn2->state = TCP_STATE_LISTEN;
// tcp_arg(pcb2, conn2);
// tcp_accept(pcb2, tcp_accept_callback);
// while (1) {
// // 主循环中处理系统超时和其他任务
// sys_check_timeouts();
// }
// return 0;
// }