From 79413ea5c122fe90c35f6cbcf34c1f414d974feb Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Wed, 15 Feb 2023 03:21:54 +0300 Subject: [PATCH 1/3] Shortened the socket.socket__exit__ timeout from 1000 to 19 secs to match default hardware setting. --- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index 0378d03..c320c0b 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -243,12 +243,12 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None: self._disconnect() stamp = time.monotonic() while self._status == wiznet5k.adafruit_wiznet5k.SNSR_SOCK_FIN_WAIT: - if time.monotonic() - stamp > 1000: + if time.monotonic() - stamp > 19: # Hardware default timeout. raise RuntimeError("Failed to disconnect socket") self.close() stamp = time.monotonic() while self._status != wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED: - if time.monotonic() - stamp > 1000: + if time.monotonic() - stamp > 19: # Hardware default timeout. raise RuntimeError("Failed to close socket") @property From 2f0fa6a7a77da78ce340086f91031a523405e35d Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Wed, 8 Mar 2023 12:24:27 +0700 Subject: [PATCH 2/3] Changed socket.__exit__() to use hardware interrupt register. --- adafruit_wiznet5k/adafruit_wiznet5k.py | 14 +++++---- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 29 ++++++++++++------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index bf6a122..04a5dae 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -105,9 +105,9 @@ # Socket n Interrupt Register _SNIR_SEND_OK = const(0x10) -_SNIR_TIMEOUT = const(0x08) +SNIR_TIMEOUT = const(0x08) _SNIR_RECV = const(0x04) -_SNIR_DISCON = const(0x02) +SNIR_DISCON = const(0x02) _SNIR_CON = const(0x01) _CH_SIZE = const(0x100) @@ -837,7 +837,7 @@ def socket_open(self, socket_num: int, conn_mode: int = _SNMR_TCP) -> int: time.sleep(0.00025) self._write_snmr(socket_num, conn_mode) - self._write_snir(socket_num, 0xFF) + self.write_snir(socket_num, 0xFF) if self.src_port > 0: # write to socket source port @@ -1058,7 +1058,7 @@ def socket_write( return 0 time.sleep(0.01) - self._write_snir(socket_num, _SNIR_SEND_OK) + self.write_snir(socket_num, _SNIR_SEND_OK) return ret # Socket-Register Methods @@ -1124,11 +1124,15 @@ def _read_snsr(self, sock: int) -> Optional[bytearray]: """Read Socket n Status Register.""" return self._read_socket(sock, _REG_SNSR) + def read_snir(self, sock: int) -> Optional[bytearray]: + """Read Socket n Status Register.""" + return self._read_socket(sock, _REG_SNIR) + def _write_snmr(self, sock: int, protocol: int) -> None: """Write to Socket n Mode Register.""" self._write_socket(sock, _REG_SNMR, protocol) - def _write_snir(self, sock: int, data: int) -> None: + def write_snir(self, sock: int, data: int) -> None: """Write to Socket n Interrupt Register.""" self._write_socket(sock, _REG_SNIR, data) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index c320c0b..3132ce9 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -240,16 +240,25 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb) -> None: if self._sock_type == SOCK_STREAM: - self._disconnect() - stamp = time.monotonic() - while self._status == wiznet5k.adafruit_wiznet5k.SNSR_SOCK_FIN_WAIT: - if time.monotonic() - stamp > 19: # Hardware default timeout. - raise RuntimeError("Failed to disconnect socket") - self.close() - stamp = time.monotonic() - while self._status != wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED: - if time.monotonic() - stamp > 19: # Hardware default timeout. - raise RuntimeError("Failed to close socket") + _the_interface.write_snir( + self._socknum, 0xFF + ) # Reset socket interrupt register. + _the_interface.socket_disconnect(self._socknum) + mask = ( + wiznet5k.adafruit_wiznet5k.SNIR_TIMEOUT + | wiznet5k.adafruit_wiznet5k.SNIR_DISCON + ) + while not _the_interface.read_snir(self._socknum)[0] & mask: + pass + _the_interface.write_snir( + self._socknum, 0xFF + ) # Reset socket interrupt register. + _the_interface.socket_close(self._socknum) + while ( + not _the_interface.socket_status(self._socknum)[0] + & wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED + ): + pass @property def _status(self) -> int: From b7c12e9f346b0de82e725c89dbcd9999cd9fd2e0 Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Wed, 8 Mar 2023 12:34:54 +0700 Subject: [PATCH 3/3] Improved logic of closed confirmation in socket.__exit__(). --- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index 3132ce9..8940947 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -255,8 +255,8 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None: ) # Reset socket interrupt register. _the_interface.socket_close(self._socknum) while ( - not _the_interface.socket_status(self._socknum)[0] - & wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED + _the_interface.socket_status(self._socknum)[0] + != wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED ): pass