Dynamic IP

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
chelox
Posts: 17
Joined: Tue Jan 22, 2013 8:24 pm

Dynamic IP

Post by chelox »

Hello!

I have a http server with static IP in my LAN.
Now I want that server has a dynamic IP in my LAN.

How I can implement that with Chibios?

Enable LWIP_DHCP in lwipports.h and then? :shock:

Thanks!
tajson
Posts: 2
Joined: Tue Aug 13, 2013 4:46 pm

Re: Dynamic IP

Post by tajson »

I have it kind of working.
Haven't looked into how to correctly handle renews, other proper error handling, sending hostname etc but it does recieve a proper dhcp ip/netmask/gateway/dns.

Code: Select all

diff --git a/os/various/lwip_bindings/lwipthread.c b/os/various/lwip_bindings/lwipthread.c
index 38ef8cf..3af455f 100644
--- a/os/various/lwip_bindings/lwipthread.c
+++ b/os/various/lwip_bindings/lwipthread.c
@@ -72,6 +72,14 @@
 #include <lwip/tcpip.h>
 #include "netif/etharp.h"
 #include "netif/ppp_oe.h"
+#include "lwip/dhcp.h"
+#include "lwip/autoip.h"
+#include <lwip/netifapi.h>
+#include <lwip/netif.h>
+
+#if SEMIHOSTING
+  #include <stdio.h>
+#endif
 
 #define PERIODIC_TIMER_ID       1
 #define FRAME_RECEIVED_ID       2
@@ -81,6 +89,19 @@
  */
 WORKING_AREA(wa_lwip_thread, LWIP_THREAD_STACK_SIZE);
 
+static void netif_status_callback(struct netif *netif) {
+  if(netif_is_up(netif)) {
+#if SEMIHOSTING
+      printf("Interface %c%c up, IP: %s\r\n", netif->name[0], netif->name[1], ipaddr_ntoa(&(netif->ip_addr)));
+#endif
+  }
+  else {
+#if SEMIHOSTING
+    printf("Interface %c%c down.\r\n", netif->name[0], netif->name[1]);
+#endif
+  }
+}
+
 /*
  * Initialization.
  */
@@ -96,6 +117,7 @@ static void low_level_init(struct netif *netif) {
   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
 
   /* Do whatever else is needed to initialize interface. */
+  netif->status_callback = netif_status_callback;
 }
 
 /*
@@ -216,6 +238,12 @@ msg_t lwip_thread(void *p) {
   EventListener el0, el1;
   struct ip_addr ip, gateway, netmask;
   static struct netif thisif;
+#if LWIP_DHCP
+  static struct dhcp netif_dhcp;
+#endif /* LWIP_DHCP */
+#if LWIP_AUTOIP
+  static struct autoip netif_autoip;
+#endif /* LWIP_AUTOIP */
   static const MACConfig mac_config = {thisif.hwaddr};
 
   chRegSetThreadName("lwipthread");
@@ -249,7 +277,21 @@ msg_t lwip_thread(void *p) {
   netif_add(&thisif, &ip, &netmask, &gateway, NULL, ethernetif_init, tcpip_input);
 
   netif_set_default(&thisif);
+
+#if LWIP_AUTOIP
+  autoip_set_struct(&thisif, &netif_autoip);
+#endif /* LWIP_AUTOIP */
+#if LWIP_DHCP
+  dhcp_set_struct(&thisif, &netif_dhcp);
+#endif /* LWIP_DHCP */
+
+#if LWIP_DHCP
+  netifapi_dhcp_start(&thisif);
+#elif LWIP_AUTOIP
+  autoip_start(&thisif);
+#else
   netif_set_up(&thisif);
+#endif
 
   /* Setup event sources.*/
   evtInit(&evt, LWIP_LINK_POLL_INTERVAL);
@@ -266,12 +308,25 @@ msg_t lwip_thread(void *p) {
     if (mask & PERIODIC_TIMER_ID) {
       bool_t current_link_status = macPollLinkStatus(&ETHD1);
       if (current_link_status != netif_is_link_up(&thisif)) {
-        if (current_link_status)
+        if (current_link_status) {
           tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up,
                                      &thisif, 0);
-        else
+#if LWIP_DHCP
+           if (thisif.dhcp) {
+             dhcp_network_changed(&thisif); //dhcp_renew(&thisif);
+           }
+#endif /*LWIP_DHCP */
+        }
+        else {
           tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down,
                                      &thisif, 0);
+#if LWIP_DHCP
+           if (thisif.dhcp) {
+             dhcp_network_changed(&thisif); //dhcp_renew(&thisif);
+           }
+#endif /*LWIP_DHCP */
+
+        }
       }
     }
     if (mask & FRAME_RECEIVED_ID) {


Ignore or remove the SEMIHOSTING stuff.

Would be awesome to have this included in Chibios-RT, DHCP is used in 99% of network setups these days. The only thing I miss from the lwip implementation in Chibios-RT so far.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Dynamic IP

Post by Giovanni »

Hi,

I am not very expert of lwIP, in next weeks the lwIP support will be moved into the community repository in order to make more easily updated, it would be nice to have a dedicated maintainer for everything lwIP-related and its integration with ChibiOS.

Giovanni
Post Reply