Haberler:

Foruma Resim Yükleme ve Boyut Sınırlaması ( ! )  https://bit.ly/2GMFb8H

Ana Menü

LwIP TCP IP Client

Başlatan ndisci@gmail.com, 26 Temmuz 2021, 13:55:15

ndisci@gmail.com

Merhaba,

STM32F7 ile çalışıyorum. CubeMx kullanarak static ip configuration yapıyorum. Kullanan kişi isterse kullanacağı pcye uygun olarak bu parametreleri değiştirebiliyor. Başta tcp server olarak stm'i kullanıyordum. server stm ve client pcydi. stm clienttan data bekliyordu geldiğinde işlemlerini yapıyordu. Fakat kullanıcının ip adresini bir kez  değiştirdikten sonra unutması halinde sıkıntı oluşmaması için stm'i client ve pc'yi server olarak kullanmaya karar verdim.Yazmaya çalıştığım tcp client source ve header file'ı aşağıda paylaşıyorum.


Stm32f7 ilk açılısta sabit bir ip adresine ping atsın ya da herhangi bir data göndersin istiyorum. ip adresim 192.168.1.1. Fakat bunu nasıl yapacağımı bilmiyorum. Elinizde çalışan sample kod var mıdır ? Aşağıdaki tcp_client_callback_connected() fonksiyonunun içindeki send_data()'yı nasıl doldurabilirim ?

Teşekkür ederim.

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