Skip to content

Commit 29727c3

Browse files
author
BiffoBear
committed
Refactored self._renew to None, "renew" or "rebind" to control content of DHCP request messages.
1 parent 94e7ad3 commit 29727c3

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def __init__(
151151
self._transaction_id = randint(1, 0x7FFFFFFF)
152152
self._start_time = 0.0
153153
self._blocking = False
154-
self._renew = False
154+
self._renew = None
155155

156156
# DHCP binding configuration
157157
self.dhcp_server_ip = _BROADCAST_SERVER_ADDR
@@ -206,7 +206,7 @@ def _dsm_reset(self) -> None:
206206
self.local_ip = _UNASSIGNED_IP_ADDR
207207
self.subnet_mask = _UNASSIGNED_IP_ADDR
208208
self.dns_server_ip = _UNASSIGNED_IP_ADDR
209-
self._renew = False
209+
self._renew = None
210210
self._increment_transaction_id()
211211
self._start_time = time.monotonic()
212212

@@ -351,7 +351,7 @@ def _process_messaging_states(self, *, message_type: int):
351351
self.gateway_ip,
352352
self.dns_server_ip,
353353
)
354-
self._renew = False
354+
self._renew = None
355355
self._dhcp_state = _STATE_BOUND
356356

357357
def _handle_dhcp_message(self) -> int:
@@ -443,14 +443,14 @@ def _dhcp_state_machine(self, *, blocking: bool = False) -> None:
443443

444444
if self._dhcp_state == _STATE_RENEWING:
445445
debug_msg("FSM state is RENEWING.", self._debug)
446-
self._renew = True
446+
self._renew = "renew"
447447
self._dhcp_connection_setup()
448448
self._start_time = time.monotonic()
449449
self._dhcp_state = _STATE_REQUESTING
450450

451451
if self._dhcp_state == _STATE_REBINDING:
452452
debug_msg("FSM state is REBINDING.", self._debug)
453-
self._renew = True
453+
self._renew = "rebind"
454454
self.dhcp_server_ip = _BROADCAST_SERVER_ADDR
455455
self._dhcp_connection_setup()
456456
self._start_time = time.monotonic()
@@ -572,9 +572,10 @@ def option_writer(
572572
offset=pointer, option_code=50, option_data=self.local_ip
573573
)
574574
# Set Server ID to chosen DHCP server IP address.
575-
pointer = option_writer(
576-
offset=pointer, option_code=54, option_data=self.dhcp_server_ip
577-
)
575+
if self._renew != "rebind":
576+
pointer = option_writer(
577+
offset=pointer, option_code=54, option_data=self.dhcp_server_ip
578+
)
578579

579580
_BUFF[pointer] = 0xFF
580581
pointer += 1

tests/test_dhcp_helper_functions.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,14 @@ def test_generate_dhcp_message_discover_with_non_defaults(
184184
assert wiz_dhcp._BUFF == result
185185

186186
@pytest.mark.parametrize(
187-
"mac_address, hostname, msg_type, time_elapsed, renew, \
187+
"mac_address, hostname, msg_type, time_elapsed, \
188188
broadcast_only, local_ip, server_ip, result",
189189
(
190190
(
191191
bytes((255, 97, 36, 101, 42, 99)),
192192
"helicopter.org",
193193
wiz_dhcp._DHCP_REQUEST,
194194
16.3,
195-
False,
196195
True,
197196
bytes((10, 10, 10, 43)),
198197
bytes((145, 66, 45, 22)),
@@ -203,7 +202,6 @@ def test_generate_dhcp_message_discover_with_non_defaults(
203202
None,
204203
wiz_dhcp._DHCP_REQUEST,
205204
72.4,
206-
False,
207205
True,
208206
bytes((100, 101, 102, 4)),
209207
bytes((245, 166, 5, 11)),
@@ -352,7 +350,7 @@ def test_dsm_reset(mocker, mock_wiznet5k):
352350
assert dhcp_client.local_ip == wiz_dhcp._UNASSIGNED_IP_ADDR
353351
assert dhcp_client.subnet_mask == wiz_dhcp._UNASSIGNED_IP_ADDR
354352
assert dhcp_client.dns_server_ip == wiz_dhcp._UNASSIGNED_IP_ADDR
355-
assert dhcp_client._renew is False
353+
assert dhcp_client._renew is None
356354
assert dhcp_client._transaction_id == 4
357355
assert dhcp_client._start_time == time.monotonic()
358356

@@ -569,7 +567,7 @@ def test_timeout_blocking_bad_message(self, mocker, mock_wiznet5k):
569567

570568
@freeze_time("2022-5-5")
571569
@pytest.mark.parametrize(
572-
"renew, blocking", ((True, False), (True, True), (False, False))
570+
"renew, blocking", (("renew", False), ("renew", True), (None, False))
573571
)
574572
def test_no_response_non_blocking_renewing(
575573
self, mocker, mock_wiznet5k, renew, blocking
@@ -608,7 +606,7 @@ def test_no_response_non_blocking_renewing(
608606

609607
@freeze_time("2022-5-5")
610608
@pytest.mark.parametrize(
611-
"renew, blocking", ((True, False), (True, True), (False, False))
609+
"renew, blocking", (("renew", False), ("renew", True), (None, False))
612610
)
613611
def test_bad_message_non_blocking_renewing(
614612
self, mocker, mock_wiznet5k, renew, blocking
@@ -778,8 +776,8 @@ def test_called_from_requesting_with_ack(self, mock_dhcp, lease_time):
778776
mock_dhcp._dhcp_state = wiz_dhcp._STATE_REQUESTING
779777
# Set the lease_time (zero forces a default to be used).
780778
mock_dhcp._lease_time = lease_time
781-
# Set renew to True to confirm that an ACK sets it to False.
782-
mock_dhcp._renew = True
779+
# Set renew to "renew" to confirm that an ACK sets it to None.
780+
mock_dhcp._renew = "renew"
783781
# Set a start time.
784782
mock_dhcp._start_time = time.monotonic()
785783
# Test.
@@ -788,8 +786,8 @@ def test_called_from_requesting_with_ack(self, mock_dhcp, lease_time):
788786
assert mock_dhcp._t1 == time.monotonic() + lease_time // 2
789787
assert mock_dhcp._t2 == time.monotonic() + lease_time - lease_time // 8
790788
assert mock_dhcp._lease_time == time.monotonic() + lease_time
791-
# Check that renew is forced to False
792-
assert mock_dhcp._renew is False
789+
# Check that renew is forced to None
790+
assert mock_dhcp._renew is None
793791
# FSM state should be bound.
794792
assert mock_dhcp._dhcp_state == wiz_dhcp._STATE_BOUND
795793

0 commit comments

Comments
 (0)