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: address@hidden
Subject: Re: [lwip-users] about stm32f7 LwIP tcp client source code
Date: Mon, 26 Jul 2021 17:14:11 +0200 (CEST)

I *believe* what you refer to is the "moving target" problem inherent in DHCP: A server that obtains its IP address via DHCP may be assigned different addresses without the client knowing.

 

There is no "good" solution to the issue. What happens normally is that the client addresses the server not via the server's ip address but via the server's DNS address, and there is some kind of interface between DNS and the target node that informs someone via a proprietary protocol of address changes.

 

You may want to look at dyndns for an example of how that works, but it'll never work out of the box.All solutions I'm aware require some software to be installed on the target that communicate IP address changes to the domain server.

 

To my best knowledge, there is no accepted standard yet for this scenario.

 

 

 

-----Original-Nachricht-----

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

Datum: 2021-07-26T16:03:26+0200

Von: "fatmanur disci" <ndisci@gmail.com>

An: "lwip-users@nongnu.org" <lwip-users@nongnu.org>

 

 

 

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 */

reply via email to

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