user29081332
user29081332

Reputation: 1

Basic LwIP TCP/IP stack implementation (NO_SYS)

I am struggling with the basic implementation of lwIP (version 2.1.2) raw API (TCP, NO_SYS) on a C2000 MCU (custom board). For the first test, I tried to implement a simple http web server that shows a static page. My code is inspired be an example from microchip ( https://ww1.microchip.com/downloads/en/Appnotes/Embedded_WebServer_Application_Using%20SAME54_DS00003120A.pdf ):

// This is the data for the actual web page.
static char myhttp_data[] =
"HTTP/1.0 200 OK\r\n\
Content-type: text/html\r\n\
\r\n\
<html> \
<head><title>A test page</title></head> \
<body> \
This is a small test page. \
</body> \
</html>";

void myhttp_init(void)
{
    myhttp_s.count_conn = 0;
    myhttp_s.count_data = 0;
    myhttp_s.err_conn = ERR_OK;
    myhttp_s.err_data = ERR_OK;

    // create a new Protocoll Control Block (PCB)
    myhttp_s.pcb = tcp_new();

    // Bind the connection to any local IP and port 80
    tcp_bind(myhttp_s.pcb, IP_ANY_TYPE, 80);

    // Set the state of the connection zu be LISTEN
    myhttp_s.pcb = tcp_listen(myhttp_s.pcb);

    // Specify the callback function that should be called when connecting to another host
    tcp_accept(myhttp_s.pcb, myhttp_accept);
}

// This callback function is called when a NEW CONNECTION has been accepted
static err_t myhttp_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
    myhttp_s.count_conn++;

    if(err != ERR_OK)
    {
        myhttp_s.err_conn = err;
    }

    // Set up the function http_recv() to be called when data arrives.
    tcp_recv(myhttp_s.pcb, myhttp_recv);

    return 0;
}

// This callback function is called when a NEW TCP SEGMENT has arrived in the connection
static err_t myhttp_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
    char *rq;

    myhttp_s.count_data++;

    if(err != ERR_OK)
    {
        myhttp_s.err_data = err;
    }

    // If we got a NULL pbuf in p, the remote end has closed the connection
    if(p != NULL)
    {
        // The payload pointer in the pbuf contains the data in the TCP segment
        rq = p->payload;

        // Check if the request was an HTTP "GET /\r\n"
        if(rq[0] == 'G' && rq[1] == 'E' && rq[2] == 'T' && rq[3] == ' ' && rq[4] == '/' && rq[5] == '\r' && rq[6] == '\n')
        {
            // Send the web page to the remote host. A zero
            // in the last argument means that the data should
            // not be copied into internal buffers.
            tcp_write(pcb, myhttp_data, sizeof(myhttp_data), 0);
        }

        // Free the pbuf
        pbuf_free(p);
    }

    // Close the connection
    tcp_close(pcb);

    return ERR_OK;
}

int main(void)
{
    // other code

    lwIPInit(0, tcp_ip.config.mac.bytes, tcp_ip.config.ip_adress.all, tcp_ip.config.netmask.all, tcp_ip.config.gateway.all, IPADDR_USE_STATIC);
    myhttp_init();
    while(1);
}

But the browser does not show the website.

Pinging the MCU works fine. The systick function is called every 120 ms (verified).

With a few debug variables I found out, when the IP adress is called from the web browser, the function myhttp_accept() is called one time, the function myhttp_recv() never. It seems that there is no incomming data. Here I can't get any further. Can anybody help?

TI provides an example with httpd to create a webserver that shows a simple page - the same functionality a I am trying to implement myself. As the example works, the custom board, configure the PHY ... up to the lwipopts.h and lwIPInit() seem to be correct.

Upvotes: 0

Views: 52

Answers (0)

Related Questions