0
votes

I am making a TCP client program for a STM32F7 system using the LwIPstack (and FreeRtos), and it works fine connecting to the server, but I can only transmit 8 messages. It seems like it is because the "MEM PBUF_POOL" reaches it's maximum. It seems like the PBUF is never freed after a message is transmitted and the connection is closed.

Since I am not allocating the PBUF myself, but am using the higher level LwIP TCP functions I don't have the pointer the PBUF, so I can't free it.

Do anybody have an idea to what is needed to free the PBUF ?.

When printing the LwIP statistics I can see the all the "MEM PBUF_POOL" are used (as shown below):

MEM PBUF_POOL
avail: 8
used: 8
max: 8
err: 126

For reference my code is shown below:

static uint32_t tcp_send_packet(struct tcp_pcb *pcb)
{
    static auto cnt = 0;
    string string = "Hej Med Dig:" + std::to_string(cnt++);
    const uint32_t len = string.length();

    err_t error = tcp_write(pcb, &string.c_str()[0], len, TCP_WRITE_FLAG_COPY);
    if (error) {
        TRACE("tcp", T_E,  "tcp_write - Code: %d\n", error);
        return error;
    }

    error = tcp_output(pcb);
    if (error) {
        TRACE("tcp", T_E,  "tcp_output - Code:%d\n", error);
    }
    return error;
}

err_t connectCallback(void *arg, struct tcp_pcb *tpcb, err_t err)
{
    TRACE("tcp", T_E, "Connection Established.\n");
    return ERR_OK;
}

err_t tcpRecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
    TRACE("tcp", T_E,"Data recieved.\n");
    if (p == NULL) {
        TRACE("tcp", T_E, "The remote host closed the connection.\n");
        //tcp_close_con();
        return ERR_ABRT;
    } else {
        TRACE("tcp", T_E,"pbufs %d - pbuf %s\n", pbuf_clen(p), (char *)p->payload);
    }
    return ERR_OK;
}

static void tcpErrorHandler(void *arg, const  err_t err) {
    TRACE("tcp", T_E,"Err:%d\n", err);
}

static void client_close(struct tcp_pcb *pcb)
{
   tcp_arg(pcb, NULL);
   tcp_sent(pcb, NULL);
   tcp_close(pcb);
}

err_t tcpSendCallback(void *arg, struct tcp_pcb *tpcb, u16_t len) {
    TRACE("tcp", T_E,"Data sent.\n");
    return ERR_OK;
}

void msgClientTask(void *arg) {
    vTaskDelay(1000);
    struct ip4_addr ip;
    while (1) {
        IP4_ADDR(&ip, 192,168, 10 ,100);    //IP of my server
        tcp_pcb *pcb = tcp_new();
        tcp_err(pcb, tcpErrorHandler);
        tcp_recv(pcb, tcpRecvCallback);
        tcp_sent(pcb, tcpSendCallback);
        tcp_connect(pcb, &ip, 4002, connectCallback);
        tcp_send_packet(pcb);
        vTaskDelay(100);
        client_close(pcb);
        vTaskDelay(100);
    }
}
  
1

1 Answers

0
votes

I think you need to call tcp_recved and free the passed pbuf using pbuf_free in tcpRecvCallback after using the data

err_t tcpRecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
    if (p == NULL) {
        tcp_close_con();
        return ERR_ABRT;
    } else {
        // use data in pbuf p here            

        // Acknowledge received data and free pbuf
        tcp_recved(tpcb, p->len);
        pbuf_free(p);
    }
    return ERR_OK;
}