Skip to content

Commit 828e9af

Browse files
committed
Clean up DHCP timeout, make it settable and longer by default
- DHCP timeouts were a bit of a mess, the code was cleaned up. - The default was made 3 seconds vs 1 second which was too short for some DHCP servers. - The DHCP timeout is now settable with a new dhcp_timeout parameter for the WIZNET5K class.
1 parent 07a39d4 commit 828e9af

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ class WIZNET5K: # pylint: disable=too-many-public-methods
154154

155155
# pylint: disable=too-many-arguments
156156
def __init__(
157-
self, spi_bus, cs, reset=None, is_dhcp=True, mac=DEFAULT_MAC, debug=False
157+
self, spi_bus, cs, reset=None, is_dhcp=True, mac=DEFAULT_MAC, debug=False,
158+
dhcp_timeout=3
158159
):
159160
self._debug = debug
160161
self._chip_type = None
@@ -182,10 +183,10 @@ def __init__(
182183
self._dns = 0
183184
# Set DHCP
184185
if is_dhcp:
185-
ret = self.set_dhcp()
186+
ret = self.set_dhcp(dhcp_timeout)
186187
assert ret == 0, "Failed to configure DHCP Server!"
187188

188-
def set_dhcp(self, response_timeout=1):
189+
def set_dhcp(self, response_timeout=3):
189190
"""Initializes the DHCP client and attempts to retrieve
190191
and set network configuration from the DHCP server.
191192
Returns True if DHCP configured, False otherwise.

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,15 @@ class DHCP:
102102
"""
103103

104104
# pylint: disable=too-many-arguments, too-many-instance-attributes, invalid-name
105-
def __init__(self, eth, mac_address, timeout=1, timeout_response=1, debug=False):
105+
def __init__(self, eth, mac_address, response_timeout=3, debug=False):
106106
self._debug = debug
107-
self._timeout = timeout
108-
self._response_timeout = timeout_response
107+
self._response_timeout = response_timeout
109108
self._mac_address = mac_address
110109

111110
# Initalize a new UDP socket for DHCP
112111
socket.set_interface(eth)
113112
self._sock = socket.socket(type=socket.SOCK_DGRAM)
114-
self._sock.settimeout(timeout)
113+
self._sock.settimeout(response_timeout)
115114

116115
# DHCP state machine
117116
self._dhcp_state = STATE_DHCP_START
@@ -373,7 +372,7 @@ def request_dhcp_lease(
373372
elif self._dhcp_state == STATE_DHCP_DISCOVER:
374373
if self._debug:
375374
print("* DHCP: Parsing OFFER")
376-
msg_type, xid = self.parse_dhcp_response(self._timeout)
375+
msg_type, xid = self.parse_dhcp_response(self._response_timeout)
377376
if msg_type == DHCP_OFFER:
378377
# use the _transaction_id the offer returned,
379378
# rather than the current one
@@ -389,7 +388,7 @@ def request_dhcp_lease(
389388
elif STATE_DHCP_REQUEST:
390389
if self._debug:
391390
print("* DHCP: Parsing ACK")
392-
msg_type, xid = self.parse_dhcp_response(self._timeout)
391+
msg_type, xid = self.parse_dhcp_response(self._response_timeout)
393392
if msg_type == DHCP_ACK:
394393
self._dhcp_state = STATE_DHCP_LEASED
395394
result = 1
@@ -412,7 +411,7 @@ def request_dhcp_lease(
412411
msg_type = 0
413412
self._dhcp_state = STATE_DHCP_START
414413

415-
if result != 1 and ((time.monotonic() - start_time > self._timeout)):
414+
if result != 1 and ((time.monotonic() - start_time > self._response_timeout)):
416415
break
417416

418417
self._transaction_id += 1

0 commit comments

Comments
 (0)