[FreeRTOS] Porting LWIP 2.1.2 based on STM32 TCP Client application articles

1 Introduction

This article mainly introduces porting on the STM32 platformLwIP 2.1.2Later, how to use API for TCP Client programming.
LwIP mainly provides 3 ways to program:

  1. RAW API : Directly access the core lwIP stack. Advantages : There is no multiple copies of data and small memory usage. Disadvantages : calling RAW API is relatively cumbersome and poor in portability.
  2. Netconn API : Call RAW API to access the core lwIP stack. Advantages : simplify the tedious call of RAW API. Disadvantages : Not common to other platform APIs, poor portability.
  3. Socket API : Access the lwIP stack through a BSD socket style interface. Advantages : Common other platform APIs, convenient for program transplantation. Disadvantages : There are multiple copies of data, which takes up memory.

The relationship between these 3 ways of programming is shown in the figure below:
Insert picture description here
If you want to know how to transplant LwIP 2.1.2, you can refer to this article "[FreeRTOS] Detailed steps for transplanting LWIP 2.1.2 based on STM32" . This article only introduces how to use LwIP TCP API.

2. How to configure LwIP to support Netconn and Socket

Need lwipopts.hto open LWIP_NETCONNand LWIP_SOCKETthese 2 macros in the middle , as follows:

/**
 * LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
 */
#define LWIP_NETCONN                    1

/*
   ------------------------------------
   ---------- Socket options ----------
   ------------------------------------
*/
/**
 * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
 */
#define LWIP_SOCKET                     1

3. Use LwIP Netconn API to implement TCP Client

LwIP Netconn API programming mainly consists of 3 steps:

  1. Initialize netconn and connect to TCP Server: netconn_newnetconn_connect
  2. Create a Task to receive messages:netconn_recv
  3. Call to netconn_writesend a message to TCP Server

The specific implementation code is as follows:

#include "main.h"
#include "tcp_client.h"
#include "lwip/opt.h"
#include "lwip/tcpip.h"
#include "lwip/sys.h"
#include "lwip/api.h"
#include "lwip/sockets.h"

#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#include "string.h"

#define TCP_CLIENT_TASK_PRIO	( tskIDLE_PRIORITY + 4 )
#define TCP_CLIENT_STACK_SIZE	( configMINIMAL_STACK_SIZE * 4 )

static sys_thread_t lwip_thread;

#define RECV_BUF_SIZE			256
static uint8_t recvBuf[RECV_BUF_SIZE];

static struct netconn *tcp_client_handle = NULL;

void tcp_client_disconnect(void)
{
    
    
	struct netconn *conn = tcp_client_handle;

	if(conn != NULL){
    
    
		netconn_close(conn);
		netconn_delete(conn);

		vTaskDelete(lwip_thread.thread_handle);
		lwip_thread.thread_handle = NULL;
	}
}

int8_t tcp_client_send_data(uint8_t *pData, uint16_t len)
{
    
    
	int8_t ret = -1;
	struct netconn *conn = tcp_client_handle;
	
	if(conn == NULL){
    
    
		return ret;
	}

	ret = netconn_write(conn, pData, len, NETCONN_COPY);
	if (ret != ERR_OK) {
    
    
		printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(ret));
	}

	return ret;
}

static void tcp_client_netconn_thread(void *arg)
{
    
    
	struct netconn *conn;
	struct netbuf *buf;
	ip_addr_t DestIPaddr;
	err_t err;
	LWIP_UNUSED_ARG(arg);

	/* Bind to TCP Client port with default IP address */
	conn = netconn_new(NETCONN_TCP);
	if(conn == NULL){
    
    
		printf("[TCP Client]netconn_new: invalid conn\r\n");
		goto EXIT;
	}

	netconn_bind(conn, IP4_ADDR_ANY, LOCAL_PORT);

	IP4_ADDR( &DestIPaddr, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );
	err = netconn_connect(conn, &DestIPaddr, DEST_PORT);
	if(err != ERR_OK){
    
    
		printf("[TCP Client]netconn_connect is fail!!!\r\n");
		goto EXIT;
	}

	tcp_client_handle = conn;
	printf("[TCP Client] Connect server is successful!!!\r\n");

	do {
    
    
		err = netconn_recv(conn, &buf);

		if (err == ERR_OK) {
    
    
			uint8_t len = (buf->p->len < RECV_BUF_SIZE) ? buf->p->len : RECV_BUF_SIZE;
			MEMCPY(recvBuf, buf->p->payload, len);
			recvBuf[len] = '\0';
			printf("[Recv Data]%s\r\n", (char *)recvBuf);
		}

		if (buf != NULL) {
    
    
			netbuf_delete(buf);
		}
	} while (1);

EXIT:
	vTaskDelete(lwip_thread.thread_handle);
	lwip_thread.thread_handle = NULL;
	return;
}

void tcp_client_init(void)
{
    
    
	lwip_thread = sys_thread_new("tcp_client_netconn", tcp_client_netconn_thread, NULL, TCP_CLIENT_STACK_SIZE, TCP_CLIENT_TASK_PRIO);
}

4. Use LwIP Socket API to implement TCP Client

LwIP Socket API programming mainly consists of 3 steps:

  1. Create socket and connect to TCP Server: socketconnect
  2. Create a Task to receive messages:recv
  3. Call to sendsend a message to TCP Server

The specific implementation code is as follows:

#include "main.h"
#include "tcp_client.h"
#include "lwip/opt.h"
#include "lwip/tcpip.h"
#include "lwip/sys.h"
#include "lwip/api.h"
#include "lwip/sockets.h"

#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#include "string.h"

#define TCP_CLIENT_TASK_PRIO	( tskIDLE_PRIORITY + 4 )
#define TCP_CLIENT_STACK_SIZE	( configMINIMAL_STACK_SIZE * 4 )

static sys_thread_t lwip_thread;

//客户端只需要一个套接字文件描述符,用于和服务器通信
int serverSocket = -1;

void tcp_client_disconnect(void)
{
    
    
	if(serverSocket != -1){
    
    
		close(serverSocket);
		serverSocket = -1;

		vTaskDelete(lwip_thread.thread_handle);
		lwip_thread.thread_handle = NULL;
	}
}

int8_t tcp_client_send_data(uint8_t *pData, uint16_t len)
{
    
    
	int8_t ret = -1;
	
	if(serverSocket == -1){
    
    
		return ret;
	}

	ret = send(serverSocket, pData, len, 0); //向服务端发送消息
	if (ret < 0) {
    
    
		printf("[TCP Client] Send data is error!!!\r\n");
	}

	return ret;
}

static void tcp_client_socket_thread(void *arg)
{
    
    
	//描述服务器的socket
	struct sockaddr_in serverAddr;

	char recvbuf[100]; //存储 接收到的信息 
	int iDataNum;

	if((serverSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0)
	{
    
    
		printf("[TCP Client] Create socket is fail!!!\r\n");
		goto EXIT;
	}

	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(SERVER_PORT);

	//指定服务器端的ip,地址:192.168.1.100
	//inet_addr()函数,将点分十进制IP转换成网络字节序IP
	serverAddr.sin_addr.s_addr = inet_addr("192.168.1.102");
	 
	if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
	{
    
    
		printf("[TCP Client] Connect is fail!!!\r\n");
		goto EXIT;
	}

	printf("[TCP Client] Connect server is successful!!!\r\n");

	while(1)
	{
    
    
		iDataNum = recv(serverSocket, recvbuf, sizeof(recvbuf), 0); //接收服务端发来的消息
		recvbuf[iDataNum] = '\0';
		printf("[Recv Data]%s\r\n", recvbuf);
	}

EXIT:
	vTaskDelete(lwip_thread.thread_handle);
	lwip_thread.thread_handle = NULL;
	return;
}


void tcp_client_init(void)
{
    
    
	lwip_thread = sys_thread_new("tcp_client_socket", tcp_client_socket_thread, NULL, TCP_CLIENT_STACK_SIZE, TCP_CLIENT_TASK_PRIO);
}

5. Verification test

Finally, the verification test is successful, as follows:
Insert picture description here

6. Data download address

The complete code download address for successful transplantation is as follows:
https://download.csdn.net/download/ZHONGCAI0901/13126159

Guess you like

Origin blog.csdn.net/ZHONGCAI0901/article/details/109813305