lwip-users
[Top][All Lists]
Advanced

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

[lwip-users] Sending data from outside lwip callbacks.


From: Tritek Main
Subject: [lwip-users] Sending data from outside lwip callbacks.
Date: Tue, 4 Apr 2023 16:15:45 +0530

Hello,

1. I am trying to implement Modbus TCP Client, on a custom LPC1778 board,
with NO_SYS=1, raw_api, and 'lpc177x_40xx_emac.c' EMAC driver
(contained in lpclwip implementation, that is part of LPCOPEN libraries)

2. The example client program sends the data in connected callback.
 
3. Obviously, I can't implement it this way as the data needs to be sent
and received multiple times during the life of the connection.

4. Hence, I plan to send data from outside the lwip callbacks.

5. Following is the 'Connected' callback implementation in the example

// *******************************************************************
/* structure to be passed as argument to the tcp callbacks */
struct echoclient
{
  enum echoclient_states state; /* connection status */
  struct tcp_pcb *pcb;          /* pointer on the current tcp_pcb */
  struct pbuf *p_tx;            /* pointer on pbuf to be transmitted */
};

static err_t tcp_echoclient_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
  struct echoclient *es = NULL;
 
  if (err == ERR_OK)  
  {
    /* allocate structure es to maintain tcp connection informations */
    es = (struct echoclient *)mem_malloc(sizeof(struct echoclient));
 
    if (es != NULL)
    {
      es->state = ES_CONNECTED;
      es->pcb = tpcb;
     
      sprintf((char*)data, "sending tcp client message %d", (int)message_count++);
       
      /* allocate pbuf */
      es->p_tx = pbuf_alloc(PBUF_TRANSPORT, strlen((char*)data) , PBUF_POOL);
         
      if (es->p_tx)
      {      
        /* copy data to pbuf */
        pbuf_take(es->p_tx, (char*)data, strlen((char*)data));
       
        /* pass newly allocated es structure as argument to tpcb */
        tcp_arg(tpcb, es);
 
        /* initialize LwIP tcp_recv callback function */
        tcp_recv(tpcb, tcp_echoclient_recv);
 
        /* initialize LwIP tcp_sent callback function */
        tcp_sent(tpcb, tcp_echoclient_sent);
 
        /* initialize LwIP tcp_poll callback function */
        tcp_poll(tpcb, tcp_echoclient_poll, 1);
   
        /* send data */
        tcp_echoclient_send(tpcb,es);
       
        return ERR_OK;
      }
    }
    else
    {
      /* close connection */
      tcp_echoclient_connection_close(tpcb, es);
     
      /* return memory allocation error */
      return ERR_MEM;  
    }
  }
  else
  {
    /* close connection */
    tcp_echoclient_connection_close(tpcb, es);
  }
  return err;
}
// *******************************************************************

6. My doubts are as follows,

6.1 Is 'es' the pointer to echoclient structure (initialized thro' malloc) and passed to lwip
    as 'arg' in it's callbacks AND the member 'tpcb' (struct tcp_pcb *tpcb)
    passed by lwip, ARE INVARIANT during the life of the connection

6.2 If that is so (which should be), I can save this in a global variable
    in the connected callback and use it from other segments of the program.

6.3 This should serve my purpose,

Thank you very much.

- Vijay Vaidya

reply via email to

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