Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Shorter socket exit timeout #97

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 8 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 9 additions & 5 deletions adafruit_wiznet5k/adafruit_wiznet5k.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

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 @@ -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 > 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

@property
def _status(self) -> int:
Expand Down