lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] about stm32f7 LwIP tcp client source code


From: Andrei Chichak
Subject: Re: [lwip-users] about stm32f7 LwIP tcp client source code
Date: Mon, 26 Jul 2021 16:32:27 -0600

What you’re talking about is mDNS.

With mDNS, your server offers a service at a particular address and port. The 
client can send a multicast request for that service and all of the devices on 
the local network offering that service will respond (vaguely), providing the 
IP address (potentially set by DHCP), and the port that the service is being 
offered on.

This system was promoted by Apple, and became known as Bonjour. Microsoft, 
being Microsoft, decided to do their own thing and has recently relented and 
mDNS will be a part of W10 at some point. Linux adopted mDNS as Avahi a long 
time ago.

Printers and file servers have used this protocol for years.

There is a LWIP sample project that implements mDNS if you are using a big-loop 
project, but it does not work with RTOS’.  FreeRTOS TCP supports Microsoft’s 
LLMNR protocol that nobody else support (for large values of nobody). So, as 
long as you can keep your project from going up the complexity slope of large 
big-loop projects, mDNS should do what you want.


Andrei (from The Great White North)



> On 2021-July-26, at 07:21, fatmanur disci <ndisci@gmail.com> wrote:
> 
> Hello everyone,
> I kindly need your help. I am working on an STM32F7 chip and using CubeMx 
> LWIP stack RMII interface . If the user wants , network parameters of stm can 
> be changed. First I tried the tcp server application and it worked. Server 
> was stm and the client was a pc. But what if the client forgot the stm's ip 
> address after several ip address changes ? Now I am trying to write a tcp 
> client source and header file for stm. I want to send any data or ping to 
> 192.168.1.1 server ip address.But I don't know how to do it. Is there any 
> sample code ? I will post my source and header code. How to write send_data() 
> function so that stm as a client starts communication via send ping or data 
> to pc. 
> Thanks.
> 
> /* Define to prevent recursive inclusion 
> -------------------------------------*/ 
> #ifndef __TCP_CLIENT_H__
> #define __TCP_CLIENT_H__
> 
> /* Includes 
> ------------------------------------------------------------------*/
> #include "lwip/tcp.h"
> 
> #define SERVER_IP1 192
> #define SERVER_IP2 168
> #define SERVER_IP3 1
> #define SERVER_IP4 1
> #define SERVER_PORT 7
> /* Exported types 
> ------------------------------------------------------------*/
> typedef struct
> {
> char rxBuffer[100];
> char txBuffer[100];
> uint8_t txBuffer2[100];
> uint8_t rxDataLen;
> uint8_t txDataLen;
> uint8_t u8fRxBuffReadyFlag;
> struct tcp_pcb *tpcb;
> }tsTCPDataDef
> 
> void tcp_client_callback_connect(void);
> 
> #endif /* __TCP_CLIENT_H__ */
> 
> 
> /* Includes 
> ------------------------------------------------------------------*/
> #include "main.h"
> #include "lwip/debug.h"
> #include "lwip/stats.h"
> #include "lwip/tcp.h"
> #include "lwip/memp.h"
> #include <stdio.h>
> #include <string.h>
> 
> #include "tcp_client.h"
> #if LWIP_TCP
> 
> /* Private variables 
> ---------------------------------------------------------*/
> tsTCPDataDef tsTCPData;
> static struct tcp_pcb *tcpPcb;
> 
> /* Private function prototypes 
> -----------------------------------------------*/
> 
> static err_t tcp_client_callback_recv(void *arg, struct tcp_pcb *tpcb, struct 
> pbuf *p, err_t err);
> static void tcp_client_callback_connection_close(struct tcp_pcb *tpcb);
> static err_t tcp_client_callback_poll(void *arg, struct tcp_pcb *tpcb);
> static err_t tcp_client_callback_sent(void *arg, struct tcp_pcb *tpcb, u16_t 
> len);
> static err_t tcp_client_callback_connected(void *arg, struct tcp_pcb *tpcb, 
> err_t err);
> static void send_data(void);
> /**
>   * @brief  Connects to the TCP echo server
>   * @param  None
>   * @retval None
>   */
> void tcp_client_callback_connect(void)
> {
>   ip_addr_t DestIPaddr;
>  
>   /* create new tcp pcb */
>   tcpPcb = tcp_new();
>  
>   if (tcpPcb != NULL)
>   {
>     IP4_ADDR( &DestIPaddr, SERVER_IP1, SERVER_IP2, SERVER_IP3, SERVER_IP4 );
>    
>     /* connect to destination address/port */
>     tcp_connect(tcpPcb,&DestIPaddr,SERVER_PORT,tcp_client_callback_connected);
>   }
>   else
>   {
>     /* deallocate the pcb */
>     memp_free(MEMP_TCP_PCB, tcpPcb);
>   }
> }
> 
> /**
>   * @brief Function called when TCP connection established
>   * @param tpcb: pointer on the connection contol block
>   * @param err: when connection correctly established err should be ERR_OK
>   * @retval err_t: returned error
>   */
> static err_t tcp_client_callback_connected(void *arg, struct tcp_pcb *tpcb, 
> err_t err)
> {
> err_t ret_err;
>         tcp_setprio(tpcb, TCP_PRIO_NORMAL);
>         /* initialize LwIP tcp_recv callback function */
>         tcp_recv(tpcb, tcp_client_callback_recv);
>  
>         /* initialize LwIP tcp_sent callback function */
>         tcp_sent(tpcb, tcp_client_callback_sent);
>  
>         /* initialize LwIP tcp_poll callback function */
>         tcp_poll(tpcb, tcp_client_callback_poll, 1);
> 
> // send_data(); // ?
>    
>       ret_err = ERR_OK;
> 
> return ret_err; 
> }
> 
> 
> /**
>   * @brief tcp_receiv callback
>   * @param arg: argument to be passed to receive callback
>   * @param tpcb: tcp connection control block
>   * @param err: receive error code
>   * @retval err_t: retuned error 
>   */
> static err_t tcp_client_callback_recv(void *arg, struct tcp_pcb *tpcb, struct 
> pbuf *p, err_t err)
> {
>   err_t ret_err;
> 
>  
>   /* if we receive an empty tcp frame from server => close connection */
>   if (p == NULL)
>   {
>     /* remote host closed connection */
>    
>       /* we're done sending, close connection */
>       tcp_client_callback_connection_close(tpcb);
> ret_err = ERR_OK;
> }
> 
>   /* else : a non empty frame was received from echo server but for some 
> reason err != ERR_OK */
>   else if(err != ERR_OK)
>   {
>     /* free received pbuf*/
>     if (p != NULL)
>     {
>       pbuf_free(p);
>     }
>     ret_err = err;
>   }
> 
> 
>   /* data received when connection already closed */
>   else
>   {
>     /* Acknowledge data reception */
>     tcp_recved(tpcb, p->tot_len);
>     pbuf_copy_partial(p, tsTCPData.rxBuffer, p->tot_len, 0);
>     tsTCPData.rxDataLen = p->tot_len;
>     tsTCPData.tpcb = tpcb;
>     tsTCPData.u8fRxBuffReadyFlag = 1;
>     /* free pbuf and do nothing */
>     pbuf_free(p);
>     ret_err = ERR_OK;
>   }
>   return ret_err;
> }
> 
> ///**
> //  * @brief function used to send data
> //  * @param  tpcb: tcp control block
> //  * @param  es: pointer on structure of type echoclient containing info on 
> data
> //  *            to be sent
> //  * @retval None
> //  */
> //static void tcp_client_send(struct tcp_pcb *tpcb, char* str)
> //{
> //  struct pbuf *p;
> // 
> //  /* allocate pbuf from pool*/
> //  p = pbuf_alloc(PBUF_TRANSPORT,strlen((char*)str), PBUF_POOL);
> // 
> //  if (p != NULL)
> //  {
> //    /* copy data to pbuf */
> //    pbuf_take(p, (char*)str, strlen((char*)str));
> //   
> //    /* send tcp data */
> // tcp_sent(tpcb, tcp_client_callback_sent);
> //    tcp_write(tpcb, p->payload, p->len, 1);
> //   
> //    /* free pbuf */
> //    pbuf_free(p);
> //  }
> //}
> 
> static void send_data(void)
> {
> 
> 
> }
> 
> /**
>   * @brief  This function implements the tcp_poll callback function
>   * @param  arg: pointer on argument passed to callback
>   * @param  tpcb: tcp connection control block
>   * @retval err_t: error code
>   */
> static err_t tcp_client_callback_poll(void *arg, struct tcp_pcb *tpcb)
> {
>   err_t ret_err;
>   ret_err = ERR_OK;
>   return ret_err;
> }
> 
> /**
>   * @brief  This function implements the tcp_sent LwIP callback (called when 
> ACK
>   *        is received from remote host for sent data)
>   * @param  arg: pointer on argument passed to callback
>   * @param  tcp_pcb: tcp connection control block
>   * @param  len: length of data sent
>   * @retval err_t: returned error code
>   */
> static err_t tcp_client_callback_sent(void *arg, struct tcp_pcb *tpcb, u16_t 
> len)
> {
> err_t ret_err;
>   ret_err = ERR_OK;
>   return ret_err;
> }
> /**
>   * @brief This function is used to close the tcp connection with server
>   * @param tpcb: tcp connection control block
>   * @param es: pointer on echoclient structure
>   * @retval None
>   */
> static void tcp_client_callback_connection_close(struct tcp_pcb *tpcb)
> {
>   /* remove callbacks */
> tcp_arg(tpcb, NULL);
>   tcp_recv(tpcb, NULL);
>   tcp_sent(tpcb, NULL);
>   tcp_poll(tpcb, NULL,0);
> 
>   /* close tcp connection */
>   tcp_close(tpcb); 
> }
> 
> #endif /* LWIP_TCP */
> _______________________________________________
> lwip-users mailing list
> lwip-users@nongnu.org
> https://lists.nongnu.org/mailman/listinfo/lwip-users





reply via email to

[Prev in Thread] Current Thread [Next in Thread]