Accessing lwIP struct netif instance in ChibiOS

Problem

You are using the ChibiOS lwIP binding to access the network your microcontroller application.

You need to access the lwIP struct netif structure, for example to get the current DHCP IP address assigned to the network interface. However, the default ChibiOS implementaton in lwipthread.c does not export the interface structure.

Solution

You can simply use the lwIP default interface declared in netif.h like this:

/** The default network interface. */
extern struct netif *netif_default;

If you use multiple interface, you can also use the netif_find(const char* name) function.

Old solution (deprecated, kept for compatibility)

There are three simple modifications you need to make:

  1. In lwipthread.c, just below the line containing WORKING_AREA (or THD_WORKING_AREA in ChibiOS 3.x), add these lines:
//The ethernet interface in use
struct netif thisif;
  1. In lwipthread.c, remove this line in msg_t lwip_thread():
static struct netif thisif;
  1. In lwipthread.h, add this line just below the line where wa_lwip_thread is declared:
extern struct netif thisif;

By doing this, you will be able to access thisif from any file where you have lwipthread.h included. Note that the exact locations of the declarations described above do not matter but were added as a guidance.

This approach was tested with both ChibiOS 3.x (git) and ChibiOS 2.6.x.