* Support longer lease times.

This commit is contained in:
bg- 2007-03-16 12:16:16 +00:00
parent 109a377a91
commit cb6bda5378
2 changed files with 32 additions and 22 deletions

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* @(#)$Id: dhcpc.c,v 1.6 2006/09/07 15:57:59 bg- Exp $ * @(#)$Id: dhcpc.c,v 1.7 2007/03/16 12:16:16 bg- Exp $
*/ */
#include <stdio.h> #include <stdio.h>
@ -326,18 +326,11 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data))
bound: bound:
#if 0 #if 0
printf("Got IP address %d.%d.%d.%d\n", printf("Got IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.ipaddr));
uip_ipaddr1(s.ipaddr), uip_ipaddr2(s.ipaddr), printf("Got netmask %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.netmask));
uip_ipaddr3(s.ipaddr), uip_ipaddr4(s.ipaddr)); printf("Got DNS server %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.dnsaddr));
printf("Got netmask %d.%d.%d.%d\n",
uip_ipaddr1(s.netmask), uip_ipaddr2(s.netmask),
uip_ipaddr3(s.netmask), uip_ipaddr4(s.netmask));
printf("Got DNS server %d.%d.%d.%d\n",
uip_ipaddr1(s.dnsaddr), uip_ipaddr2(s.dnsaddr),
uip_ipaddr3(s.dnsaddr), uip_ipaddr4(s.dnsaddr));
printf("Got default router %d.%d.%d.%d\n", printf("Got default router %d.%d.%d.%d\n",
uip_ipaddr1(s.default_router), uip_ipaddr2(s.default_router), uip_ipaddr_to_quad(&s.default_router));
uip_ipaddr3(s.default_router), uip_ipaddr4(s.default_router));
printf("Lease expires in %ld seconds\n", printf("Lease expires in %ld seconds\n",
ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1])); ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1]));
#endif #endif
@ -345,28 +338,45 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data))
dhcpc_configured(&s); dhcpc_configured(&s);
#define MAX_TICKS (~((clock_time_t)0) / 2) #define MAX_TICKS (~((clock_time_t)0) / 2)
#define MAX_TICKS32 (~((u32_t)0))
#define IMIN(a, b) ((a) < (b) ? (a) : (b))
if((ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1]))*CLOCK_SECOND/2 if((ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1]))*CLOCK_SECOND/2
<= MAX_TICKS) { <= MAX_TICKS32) {
s.ticks = (clock_time_t)((ntohs(s.lease_time[0])*65536ul s.ticks = (ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1])
+ ntohs(s.lease_time[1]))*CLOCK_SECOND/2); )*CLOCK_SECOND/2;
} else { } else {
s.ticks = MAX_TICKS; s.ticks = MAX_TICKS32;
} }
etimer_set(&s.etimer, s.ticks); while(s.ticks > 0) {
PT_YIELD_UNTIL(&s.pt, etimer_expired(&s.etimer)); clock_time_t ticks;
ticks = IMIN(s.ticks, MAX_TICKS);
s.ticks -= ticks;
etimer_set(&s.etimer, ticks);
PT_YIELD_UNTIL(&s.pt, etimer_expired(&s.etimer));
}
if((ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1]))*CLOCK_SECOND/2
<= MAX_TICKS32) {
s.ticks = (ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1])
)*CLOCK_SECOND/2;
} else {
s.ticks = MAX_TICKS32;
}
/* renewing: */ /* renewing: */
xid++; xid++;
do { do {
clock_time_t ticks;
while(ev != tcpip_event) { while(ev != tcpip_event) {
tcpip_poll_udp(s.conn); tcpip_poll_udp(s.conn);
PT_YIELD(&s.pt); PT_YIELD(&s.pt);
} }
send_request(); send_request();
s.ticks /= 2; ticks = IMIN(s.ticks / 2, MAX_TICKS);
etimer_set(&s.etimer, s.ticks); s.ticks -= ticks;
etimer_set(&s.etimer, ticks);
do { do {
PT_YIELD(&s.pt); PT_YIELD(&s.pt);
if(ev == tcpip_event && uip_newdata() && msg_for_me() == DHCPACK) { if(ev == tcpip_event && uip_newdata() && msg_for_me() == DHCPACK) {

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* @(#)$Id: dhcpc.h,v 1.3 2006/09/07 15:57:59 bg- Exp $ * @(#)$Id: dhcpc.h,v 1.4 2007/03/16 12:16:16 bg- Exp $
*/ */
#ifndef __DHCPC_H__ #ifndef __DHCPC_H__
#define __DHCPC_H__ #define __DHCPC_H__
@ -38,7 +38,7 @@ struct dhcpc_state {
char state; char state;
struct uip_udp_conn *conn; struct uip_udp_conn *conn;
struct etimer etimer; struct etimer etimer;
clock_time_t ticks; u32_t ticks;
const void *mac_addr; const void *mac_addr;
int mac_len; int mac_len;