From 33496d1e2016bfda5b77ffc3bcefbd0289ce2793 Mon Sep 17 00:00:00 2001 From: Bongjun Hur Date: Sun, 11 Apr 2021 17:15:20 +0900 Subject: [PATCH 1/9] check recv size --- examples/wiznet5k_simpleserver.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/wiznet5k_simpleserver.py b/examples/wiznet5k_simpleserver.py index deb42fc..fad834a 100644 --- a/examples/wiznet5k_simpleserver.py +++ b/examples/wiznet5k_simpleserver.py @@ -31,6 +31,8 @@ while True: conn, addr = server.accept() # Wait for a connection from a client. with conn: - data = conn.recv() - print(data) - conn.send(data) # Echo message back to client + while True: + data = conn.recv() + if data: # Wait for receiving data + print(data) + conn.send(data) # Echo message back to client From ec91affa98b2a03a87a7f692e29e775a7fc1090a Mon Sep 17 00:00:00 2001 From: Bongjun Hur Date: Sun, 11 Apr 2021 17:30:06 +0900 Subject: [PATCH 2/9] before DHCP, check link status first --- adafruit_wiznet5k/adafruit_wiznet5k.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index cc3063f..659faec 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -175,6 +175,14 @@ def __init__( self.mac_address = mac self._src_port = 0 self._dns = 0 + + # First wait link status is on + # to avoid the code during DHCP - assert self.link_status, "Ethernet cable disconnected!" + while True: + time.sleep(1) + print("My Link is:", self.link_status) + if self.link_status: break + # Set DHCP if is_dhcp: ret = self.set_dhcp(hostname, dhcp_timeout) From 8179adcd492014ce72959fb2eea4e38292ad954b Mon Sep 17 00:00:00 2001 From: Bongjun Hur Date: Mon, 12 Apr 2021 13:15:15 +0900 Subject: [PATCH 3/9] Update adafruit_wiznet5k/adafruit_wiznet5k.py Co-authored-by: Mark <56205165+gamblor21@users.noreply.github.com> --- adafruit_wiznet5k/adafruit_wiznet5k.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index 659faec..821fda3 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -180,7 +180,8 @@ def __init__( # to avoid the code during DHCP - assert self.link_status, "Ethernet cable disconnected!" while True: time.sleep(1) - print("My Link is:", self.link_status) + if self._debug: + print("My Link is:", self.link_status) if self.link_status: break # Set DHCP From 6473abe8a3dd1a986514d54e4d6afb7aefdb1f52 Mon Sep 17 00:00:00 2001 From: Bongjun Hur Date: Mon, 12 Apr 2021 13:29:38 +0900 Subject: [PATCH 4/9] add 5seconds timeout during link check --- adafruit_wiznet5k/adafruit_wiznet5k.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index 821fda3..00e3b92 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -178,11 +178,13 @@ def __init__( # First wait link status is on # to avoid the code during DHCP - assert self.link_status, "Ethernet cable disconnected!" + start_time = time.monotonic() while True: + if self.link_status or ((time.monotonic() - start_time) > 5) : + break time.sleep(1) if self._debug: - print("My Link is:", self.link_status) - if self.link_status: break + print("My Link is:", self.link_status) # Set DHCP if is_dhcp: From c91daed53154b4dde0cf2fb8bf3e4d526ac20537 Mon Sep 17 00:00:00 2001 From: Bongjun Hur Date: Mon, 12 Apr 2021 17:52:32 +0900 Subject: [PATCH 5/9] before sending, _BUFF buf clear in DHCP --- adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py index 7faef3d..3aff6f5 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py @@ -127,6 +127,10 @@ def send_dhcp_message(self, state, time_elapsed): :param float time_elapsed: Number of seconds elapsed since renewal. """ + # before making send packet, shoule init _BUFF. if not, DHCP sometimes fails, wrong padding, garbage bytes, ... + for i in range(len(_BUFF)): + _BUFF[i] = 0 + # OP _BUFF[0] = DHCP_BOOT_REQUEST # HTYPE @@ -361,9 +365,9 @@ def request_dhcp_lease( print("* DHCP: Parsing OFFER") msg_type, xid = self.parse_dhcp_response(self._response_timeout) if msg_type == DHCP_OFFER: - # use the _transaction_id the offer returned, - # rather than the current one - self._transaction_id = self._transaction_id.from_bytes(xid, "l") + # # use the _transaction_id the offer returned, + # # rather than the current one + # self._transaction_id = self._transaction_id.from_bytes(xid, "l") if self._debug: print("* DHCP: Request") self.send_dhcp_message( @@ -372,7 +376,7 @@ def request_dhcp_lease( self._dhcp_state = STATE_DHCP_REQUEST else: print("* Received DHCP Message is not OFFER") - elif STATE_DHCP_REQUEST: + elif self._dhcp_state == STATE_DHCP_REQUEST: if self._debug: print("* DHCP: Parsing ACK") msg_type, xid = self.parse_dhcp_response(self._response_timeout) From cde9d66fe2ce1bc3831b71b4e036d5184776b8a8 Mon Sep 17 00:00:00 2001 From: Bongjun Hur Date: Wed, 14 Apr 2021 15:34:14 +0900 Subject: [PATCH 6/9] init _BUFF - one line code --- adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py index 3aff6f5..92ff0d1 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py @@ -127,9 +127,9 @@ def send_dhcp_message(self, state, time_elapsed): :param float time_elapsed: Number of seconds elapsed since renewal. """ - # before making send packet, shoule init _BUFF. if not, DHCP sometimes fails, wrong padding, garbage bytes, ... - for i in range(len(_BUFF)): - _BUFF[i] = 0 + # before making send packet, shoule init _BUFF. + # if not, DHCP sometimes fails, wrong padding, garbage bytes, ... + _BUFF[:] = b'\x00' * len(_BUFF) # OP _BUFF[0] = DHCP_BOOT_REQUEST From 456fc8a07d0af0a8bede4286a6a110818b4a179b Mon Sep 17 00:00:00 2001 From: Bongjun Hur Date: Thu, 15 Apr 2021 15:34:38 +0900 Subject: [PATCH 7/9] link check in set_dhcp --- adafruit_wiznet5k/adafruit_wiznet5k.py | 21 +++++++++++---------- adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py | 5 +---- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index 00e3b92..1369b5c 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -176,16 +176,6 @@ def __init__( self._src_port = 0 self._dns = 0 - # First wait link status is on - # to avoid the code during DHCP - assert self.link_status, "Ethernet cable disconnected!" - start_time = time.monotonic() - while True: - if self.link_status or ((time.monotonic() - start_time) > 5) : - break - time.sleep(1) - if self._debug: - print("My Link is:", self.link_status) - # Set DHCP if is_dhcp: ret = self.set_dhcp(hostname, dhcp_timeout) @@ -201,6 +191,17 @@ def set_dhcp(self, hostname=None, response_timeout=3): """ if self._debug: print("* Initializing DHCP") + + # First, wait link status is on + # to avoid the code during DHCP - assert self.link_status, "Ethernet cable disconnected!" + start_time = time.monotonic() + while True: + if self.link_status or ((time.monotonic() - start_time) > 5) : + break + time.sleep(1) + if self._debug: + print("My Link is:", self.link_status) + self._src_port = 68 # Return IP assigned by DHCP _dhcp_client = dhcp.DHCP( diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py index 92ff0d1..311e55f 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py @@ -365,11 +365,8 @@ def request_dhcp_lease( print("* DHCP: Parsing OFFER") msg_type, xid = self.parse_dhcp_response(self._response_timeout) if msg_type == DHCP_OFFER: - # # use the _transaction_id the offer returned, - # # rather than the current one - # self._transaction_id = self._transaction_id.from_bytes(xid, "l") if self._debug: - print("* DHCP: Request") + print("* DHCP: Request", xid) self.send_dhcp_message( DHCP_REQUEST, ((time.monotonic() - start_time) / 1000) ) From 93399f694c6c2a2e5173c004ad499842b69b3adf Mon Sep 17 00:00:00 2001 From: Bongjun Hur Date: Sat, 17 Apr 2021 14:56:59 +0900 Subject: [PATCH 8/9] move out accept() from while, specify 1024 byte --- examples/wiznet5k_simpleserver.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/wiznet5k_simpleserver.py b/examples/wiznet5k_simpleserver.py index fad834a..5d98daa 100644 --- a/examples/wiznet5k_simpleserver.py +++ b/examples/wiznet5k_simpleserver.py @@ -28,11 +28,10 @@ server.bind((server_ip, server_port)) # Bind to IP and Port server.listen() # Begin listening for incoming clients +conn, addr = server.accept() # Wait for a connection from a client. while True: - conn, addr = server.accept() # Wait for a connection from a client. with conn: - while True: - data = conn.recv() - if data: # Wait for receiving data - print(data) - conn.send(data) # Echo message back to client + data = conn.recv(1024) + if data: # Wait for receiving data + print(data) + conn.send(data) # Echo message back to client From baca531d3b1c105cf354aeb67e880b64e445c8fa Mon Sep 17 00:00:00 2001 From: Bongjun Hur Date: Thu, 22 Apr 2021 08:43:06 +0900 Subject: [PATCH 9/9] crlf -> lf --- adafruit_wiznet5k/adafruit_wiznet5k.py | 2 +- adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py | 4 ++-- examples/wiznet5k_simpleserver.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index 1369b5c..54fb012 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -196,7 +196,7 @@ def set_dhcp(self, hostname=None, response_timeout=3): # to avoid the code during DHCP - assert self.link_status, "Ethernet cable disconnected!" start_time = time.monotonic() while True: - if self.link_status or ((time.monotonic() - start_time) > 5) : + if self.link_status or ((time.monotonic() - start_time) > 5): break time.sleep(1) if self._debug: diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py index 311e55f..1a42926 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py @@ -127,9 +127,9 @@ def send_dhcp_message(self, state, time_elapsed): :param float time_elapsed: Number of seconds elapsed since renewal. """ - # before making send packet, shoule init _BUFF. + # before making send packet, shoule init _BUFF. # if not, DHCP sometimes fails, wrong padding, garbage bytes, ... - _BUFF[:] = b'\x00' * len(_BUFF) + _BUFF[:] = b"\x00" * len(_BUFF) # OP _BUFF[0] = DHCP_BOOT_REQUEST diff --git a/examples/wiznet5k_simpleserver.py b/examples/wiznet5k_simpleserver.py index 5d98daa..548af79 100644 --- a/examples/wiznet5k_simpleserver.py +++ b/examples/wiznet5k_simpleserver.py @@ -31,7 +31,7 @@ conn, addr = server.accept() # Wait for a connection from a client. while True: with conn: - data = conn.recv(1024) - if data: # Wait for receiving data + data = conn.recv(1024) + if data: # Wait for receiving data print(data) conn.send(data) # Echo message back to client