Merge pull request #1836 from mdlemay/dhcp

galileo: Add process to perform DHCP configuration
This commit is contained in:
Nicolas Tsiftes 2016-09-09 16:55:05 +02:00 committed by GitHub
commit 8bf235ef86
2 changed files with 53 additions and 2 deletions

View file

@ -65,10 +65,10 @@ app_main(void)
process_init(); process_init();
procinit_init(); procinit_init();
ctimer_init(); ctimer_init();
autostart_start(autostart_processes);
eth_init(); eth_init();
autostart_start(autostart_processes);
while(1) { while(1) {
process_run(); process_run();
} }

View file

@ -29,8 +29,10 @@
*/ */
#include "eth-conf.h" #include "eth-conf.h"
#include <stdio.h>
#include "net/eth-proc.h" #include "net/eth-proc.h"
#include "contiki-net.h" #include "contiki-net.h"
#include "net/ip/dhcpc.h"
#include "net/linkaddr.h" #include "net/linkaddr.h"
#if NETSTACK_CONF_WITH_IPV6 #if NETSTACK_CONF_WITH_IPV6
@ -44,6 +46,8 @@ const linkaddr_t linkaddr_null = { { 0, 0, 0, 0, 0, 0 } };
#define NAMESERVER_IP GATEWAY_IP #define NAMESERVER_IP GATEWAY_IP
#endif #endif
PROCESS(dhcp_process, "DHCP");
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
eth_init(void) eth_init(void)
@ -70,5 +74,52 @@ eth_init(void)
#endif #endif
process_start(&eth_process, NULL); process_start(&eth_process, NULL);
/* Comment out the following line to disable DHCP and simply use the static
* IP configuration setup above.
*/
process_start(&dhcp_process, NULL);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(dhcp_process, ev, data)
{
PROCESS_BEGIN();
dhcpc_init(uip_lladdr.addr, sizeof(uip_lladdr.addr));
printf("Requesting DHCP configuration...\n");
dhcpc_request();
while(1) {
PROCESS_WAIT_EVENT();
if(ev == tcpip_event || ev == PROCESS_EVENT_TIMER) {
dhcpc_appcall(ev, data);
} else if(ev == PROCESS_EVENT_EXIT) {
process_exit(&dhcp_process);
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
void
dhcpc_configured(const struct dhcpc_state *s)
{
uip_sethostaddr(&s->ipaddr);
uip_setnetmask(&s->netmask);
uip_setdraddr(&s->default_router);
uip_nameserver_update(&s->dnsaddr, UIP_NAMESERVER_INFINITE_LIFETIME);
printf("DHCP configured:\n");
printf(" - Host IP: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s->ipaddr));
printf(" - Netmask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s->netmask));
printf(" - Default router: %d.%d.%d.%d\n",
uip_ipaddr_to_quad(&s->default_router));
printf(" - DNS server: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s->dnsaddr));
}
/*---------------------------------------------------------------------------*/
void
dhcpc_unconfigured(const struct dhcpc_state *s)
{
printf("DHCP unconfigured.\n");
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/