8000 New dhcp state machine by BiffoBear · Pull Request #95 · adafruit/Adafruit_CircuitPython_Wiznet5k · GitHub
[go: up one dir, main page]

Skip to content

New dhcp state machine #95

< 10000 div class="gh-header-actions mt-0 mb-3 mb-md-2 ml-1 flex-md-order-1 flex-shrink-0 d-flex flex-items-center gap-1">
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 88 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
077c330
Refactored message parser and message generator. All tests pass. Bega…
Dec 17, 2022
868cb51
Updated tests for the broadcast flag.
Dec 17, 2022
5a767f1
Added DHCP options for REQUEST to _generate_dhcp_message.
Dec 17, 2022
e0f17a2
Added IP collision detection via ARP request.
Dec 17, 2022
b740d14
Added all state machine states.
Dec 17, 2022
16def1d
Removed IP collision detection, AF_PACKET not support in CP sockets. …
Dec 18, 2022
861c30a
Refactored dummy data, added freezegun to opt_reqs, added some tests.
Dec 19, 2022
5435203
Added some tests. Changed _BUFF reset in _generate_dhcp_message to a …
Dec 19, 2022
b6afd52
Removed unused releasing state. Added doc string to DHCP.
Dec 19, 2022
9bfe09d
Added more tests.
Dec 21, 2022
f5ab202
Merge branch 'adafruit:main' into new_dhcp_state_machine
BiffoBear Dec 21, 2022
08ed3d7
Added tests for helper methods.
Dec 21, 2022
fa07b95
Added tests for _handle_dhcp_message
Dec 24, 2022
e8f7fe8
Added tests for _handle_dhcp_message
Dec 24, 2022
6cb5a50
Added more tests for _handle_dhcp_message function.
Dec 26, 2022
152a2f8
Refactored fixtures to make tests more readable.
Dec 26, 2022
2888d04
Finished tests for _handle_dhcp_message
Dec 28, 2022
f28030e
Finished tests for _dhcp_state_machine
Dec 28, 2022
eb351df
Refactored _handle_dhcp_message for readability.
Dec 28, 2022
1723d68
Added some comments to explain tests.
Jan 2, 2023
c0cc539
Implemented a DHCP packet receiver that only relies on standard socke…
Jan 3, 2023
3f85df9
Refactored the FSM to use _receive_dhcp_response instead of socket.so…
Jan 3, 2023
688854e
Added ip_in_use() to WIZNET5K to allow DHCP to send an ARP and confir…
Jan 3, 2023
ab72630
Added debugging messages to wiznet5k_dhcp.
Jan 3, 2023
ebe63b0
Moved _ip_in_use to DHCP and refactored to use a UDP call as W5500 ha…
Jan 4, 2023
8d00f52
Moved back to WIZNET5K (final location) and renamed to _ip_address_in…
Jan 7, 2023
37b5e03
Refactored DHCP._socket_set to _dhcp_connection_setup and removed soc…
Jan 8, 2023
29fc1c8
Refactored DHCP to remove all socket.socket calls.
Jan 8, 2023
08345f3
Fixed _dhcp_connection_setup.
Jan 9, 2023
0a8eb0f
Fixed sending date.
Jan 9, 2023
53176d8
Moved sending message outside receiving loop.
Jan 9, 2023
fce4a56
Fixed DHCP resend timing to match DHCP standard.
Jan 9, 2023
8bd5bbd
Refactored to use a for loop in _handle_dhcp_message and local variab…
Jan 9, 2023
1e03545
Refactored to remove _prepare_and_set_next_state
Jan 10, 2023
eb536c0
Refactored to processing message states into a single function - _pro…
Jan 10, 2023
9bec64e
Wrote tests for _handle_dhcp_message
Jan 10, 2023
29aae43
Completed tests for DHCP helper methods.
Jan 10, 2023
24f1d4f
Refactored _handle_DHCP to return 0, a message type or raise a ValueE…
Jan 10, 2023
dd1548e
Added tests for _process_messaging_states.
Jan 11, 2023
052f037
Deleted obsolete tests for FSM
Jan 11, 2023
43b35e3
Added a hexdump to _debugging_message
Jan 12, 2023
bbc9887
Added debugging messages
Jan 12, 2023
85f8f76
Fine tuned debugging messages and fixed a bug the prevented a command…
Jan 12, 2023
9aa49ea
Fixed a bug where the server port was not set. Improved message gener…
Jan 12, 2023
4829ec7
Really fixed the server port bug.
Jan 12, 2023
b983145
Pause to integrate changes from other pull requests.
Jan 17, 2023
db86eb8
Resolved conflicts from recent merges to main.
Jan 17, 2023
836bb29
Updated tests to match changes merged in from main.
Jan 21, 2023
e6d3ea3
Updated tests to match changes merged in from main.
Jan 21, 2023
1792b81
Fixed a glitch in the docstrings that caused Sphinx to fail.
Jan 21, 2023
9121bbb
Moved debug_msg() to __init__.py so that all modules can access it.
Jan 22, 2023
9a27612
Refactored wiznet5k.py to use debug_msg.
Jan 22, 2023
fe302da
DHCP client makes a new connection.
Jan 23, 2023
56d2ad0
Restored stashed changes.
Jan 24, 2023
c68c293
Merge in changes from main.
Jan 24, 2023
05ba882
Refactored DHCP to store IP addresses as byte objects instead of tuples.
Jan 24, 2023
9a68fd4
Refactored DHCP to only accept a MAC address as a bytes object.
Jan 25, 2023
fa52aab
Removed _DEFAULT_LEASE_TIME.
Jan 25, 2023
8000
9ec6b16
Updated tests to remove _DEFAULT_LEASE_TIME.
Jan 25, 2023
53d2cfa
Removed unused response_timeout arg from DHCP and tests.
Jan 25, 2023
7ad6ed6
Removed DHCP timeout from WIZNET5K call.
Jan 25, 2023
a3c71f9
Refactored mac_address to return a bytes object and added error check…
Jan 25, 2023
468d802
Removed spurious print statements.
Jan 25, 2023
1f286bf
Added a gc.collect to end of FSM.
Jan 25, 2023
d2a808c
Improved hexdump for debug_msg function.
Jan 28, 2023
08baf1c
Replaced %S with .format in hexdump.
Feb 2, 2023
8bc470e
Tidied some doc-strings.
Feb 3, 2023
a89bd7f
Added code to assign the ifconfig data to WIZNET5K and to use self._r…
Feb 6, 2023
94e7ad3
Added a return to FSM for when renewing fails.
Feb 6, 2023
29727c3
Refactored self._renew to None, "renew" or "rebind" to control conten…
Feb 9, 2023
895a0e0
Refactored self._lease_time to self._lease.
Feb 9, 2023
0fd0fa2
Changed self._sock.recv() back to self._sock.recv(512) in wiznet5k_dn…
Feb 10, 2023
793e93f
Moved debug_msg() from init.py to adafruit_wiznet5k_debug.py.
Feb 10, 2023
5ea607a
Convert DNS server IP address to dot-quad string before calling socke…
Feb 14, 2023
aa30f2e
Updated to latest version of socket.py.
Feb 22, 2023
63bf8f7
Merge branch 'main' into new_dhcp_state_machine
FoamyGuy Mar 20, 2023
06c2ea1
fix constant name
FoamyGuy Mar 20, 2023
9bbbc80
Merged in updates from main.
Mar 22, 2023
549ca5b
Merge remote-tracking branch 'origin/new_dhcp_state_machine' into new…
Mar 22, 2023
82277ca
Increased buffer to 512 bytes due to long DHCP response. Fixed timeou…
Mar 22, 2023
fafe6e8
Fixed bug where timeout was calculated as 2 x time.monotonic + n sec…
Mar 28, 2023
da2a88a
Added debug text that confirms DHCP releases socket.
Mar 29, 2023
df4efb2
Fixed potential infinite loop in WIZNET5K.socket_close.
Mar 30, 2023
4002d57
Converted read_sncr and read_snsr results to int so that they can act…
Apr 1, 2023
1e373e4
Added a reset to w5500 chip detection.
Apr 9, 2023
cc48f9b
Updated with latest changes to origin/main
Apr 20, 2023
ffb5e32
Fixed explicit soft reset for w5500.
Apr 26, 2023
9231943
Future proofed w5500 initialisation.
Apr 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
391 changes: 253 additions & 138 deletions adafruit_wiznet5k/adafruit_wiznet5k.py

Large diffs are not rendered by default.

979 changes: 563 additions & 416 deletions adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion adafruit_wiznet5k/adafruit_wiznet5k_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ def gethostbyname(self, hostname: bytes) -> Union[int, bytes]:

:return Union[int, bytes] The IPv4 address if successful, -1 otherwise.
"""

if self._dns_server is None:
return _INVALID_SERVER
# build DNS request packet
Expand Down
1 change: 1 addition & 0 deletions adafruit_wiznet5k/adafruit_wiznet5k_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import time
from sys import byteorder
from micropython import const

import adafruit_wiznet5k as wiznet5k

# pylint: disable=invalid-name
Expand Down
124 changes: 124 additions & 0 deletions tests/dhcp_dummy_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# SPDX-FileCopyrightText: 2022 Martin Stephens
#
# SPDX-License-Identifier: MIT
"""Data for use in test_dhcp_helper_files.py"""


def _pad_message(message_section: bytearray, target_length: int) -> bytearray:
"""Pad the message with 0x00."""
return message_section + bytearray(b"\00" * (target_length - len(message_section)))


def _build_message(message_body: bytearray, message_options: bytearray) -> bytearray:
"""Assemble the padded message and body to make a 318 byte packet. The 'header'
section must be 236 bytes and the entire message must be 318 bytes."""
dhcp_message = _pad_message(message_body, 236) + _pad_message(message_options, 276)
assert len(dhcp_message) == 512
return dhcp_message


# Data for testing send data.
# DHCP DISCOVER messages.
# Default settings (DISCOVER, broadcast=False, default hostname, renew=False)
message = bytearray(
b"\x01\x01\x06\x00o\xff\xff\xff\x00\x17\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x05\x06\x07"
b"\x08\t\x00\x00\x00\x00\x00\x00\x00\x00"
)
options = bytearray(
b"c\x82Sc5\x01\x01\x0c\x12WIZnet040506070809=\x07\x01"
b"\x04\x05\x06\x07\x08\t7\x03\x01\x03\x063\x04\x00v\xa7\x00\xff"
)
DHCP_SEND_01 = _build_message(message, options)

message = bytearray(
b"\x01\x01\x06\x00o\xff\xff\xff\x00\x17\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x05\x06\x07"
b"\x08\t"
)
options = bytearray(
b"c\x82Sc5\x01\x01\x0c\x12WIZnet040506070809=\x07\x01"
b"\x04\x05\x06\x07\x08\t7\x03\x01\x03\x063\x04\x00v\xa7\x00\xff"
)
DHCP_SEND_02 = _build_message(message, options)

message = bytearray(
b"\x01\x01\x06\x00o\xff\xff\xff\x00#\x80\x00\xc0\xa8\x03\x04"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18#.9DO"
)
options = bytearray(
b"c\x82Sc5\x01\x01\x0c\x04bert=\x07\x01\x18#.9DO7"
b"\x03\x01\x03\x063\x04\x00v\xa7\x00\xff"
)
DHCP_SEND_03 = _build_message(message, options)

message = bytearray(
b"\x01\x01\x06\x00o\xff\xff\xff\x00#\x80\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xffa$e*c"
)
options = bytearray(
b"c\x82Sc5\x01\x01\x0c\x05clash=\x07\x01\xffa$e*c7"
b"\x03\x01\x03\x063\x04\x00v\xa7\x00\xff"
)
DHCP_SEND_04 = _build_message(message, options)

# DHCP REQUEST messages.
message = bytearray(
b"\x01\x01\x06\x00o\xff\xff\xff\x00\x10\x80\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xffa$e*c"
)

options = bytearray(
b"c\x82Sc5\x01\x03\x0c\nhelicopter=\x07\x01\xffa$e*c7"
b"\x03\x01\x03\x063\x04\x00v\xa7\x002\x04\n\n\n+6\x04\x91B-\x16\xff"
)
DHCP_SEND_05 = _build_message(message, options)

message = bytearray(
b"\x01\x01\x06\x00o\xff\xff\xff\x00H\x80\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00K?\xa6\x04"
b"\xc8e"
)

options = bytearray(
b"c\x82Sc5\x01\x03\x0c\x12WIZnet4B3FA604C865=\x07\x01K?\xa6"
b"\x04\xc8e7\x03\x01\x03\x063\x04\x00v\xa7\x002\x04def\x046"
b"\x04\xf5\xa6\x05\x0b\xff"
)
DHCP_SEND_06 = _build_message(message, options)

# Data to test response parser.
# Basic case, no extra fields, one each of router and DNS.
message = bytearray(
b"\x02\x00\x00\x00\x7f\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xc0"
b"\xa8\x05\x16\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x05\x07\t\x0b"
)

options = bytearray(
b"c\x82Sc5\x01\x02\x01\x04\xc0\xa8\x06\x026\x04\xeao\xde"
b"{3\x04\x00\x01\x01\x00\x03\x04yy\x04\x05\x06\x04\x05\x06"
b'\x07\x08:\x04\x00""\x00;\x04\x0033\x00\xff'
)
GOOD_DATA_01 = _build_message(message, options)

# Complex case, extra field, 2 routers and 2 DNS servers.
message = bytearray(
b"\x02\x00\x00\x004Vx\x9a\x00\x00\x00\x00\x00\x00\x00\x00\x12$@\n\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x01"
)
options = bytearray(
b"c\x82Sc5\x01\x05<\x05\x01\x02\x03\x04\x05\x01\x04\n\x0b"
b"\x07\xde6\x04zN\x91\x03\x03\x08\n\x0b\x0e\x0f\xff\x00"
b"\xff\x00\x06\x08\x13\x11\x0b\x07****3\x04\x00\x00=;:\x04"
b"\x00\x0e\x17@;\x04\x02\x92]\xde\xff"
)
GOOD_DATA_02 = _build_message(message, options)


#
message = bytearray(
b"\x02\x00\x00\x00\xff\xff\xff\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x12$@\n\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x01"
)
options = bytearray(b"c\x82Sc")
BAD_DATA = _build_message(message, options)
Loading
0