Skip to content

Commit

Permalink
Merge pull request #97 from BiffoBear/shorter_socket_exit_timeout
Browse files Browse the repository at this point in the history
Shorter socket exit timeout
  • Loading branch information
FoamyGuy authored Mar 20, 2023
2 parents 6581863 + b7c12e9 commit d2637d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
14 changes: 9 additions & 5 deletions adafruit_wiznet5k/adafruit_wiznet5k.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,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)
Expand Down Expand Up @@ -883,7 +883,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
Expand Down Expand Up @@ -1104,7 +1104,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
Expand Down Expand Up @@ -1170,11 +1170,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)

Expand Down
29 changes: 19 additions & 10 deletions adafruit_wiznet5k/adafruit_wiznet5k_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,25 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
_the_interface.release_socket(self._socknum)
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 > 1000:
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:
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 (
_the_interface.socket_status(self._socknum)[0]
!= wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED
):
pass

# This works around problems with using a class method as a decorator.
def _check_socket_closed(func): # pylint: disable=no-self-argument
Expand Down

0 comments on commit d2637d7

Please sign in to comment.