Skip to content

Commit aa30f2e

Browse files
author
BiffoBear
committed
Updated to latest version of socket.py.
1 parent 5ea607a commit aa30f2e

7 files changed

+377
-284
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@
8989
SNSR_SOCK_ESTABLISHED = const(0x17)
9090
SNSR_SOCK_FIN_WAIT = const(0x18)
9191
_SNSR_SOCK_CLOSING = const(0x1A)
92-
_SNSR_SOCK_TIME_WAIT = const(0x1B)
93-
_SNSR_SOCK_CLOSE_WAIT = const(0x1C)
92+
SNSR_SOCK_TIME_WAIT = const(0x1B)
93+
SNSR_SOCK_CLOSE_WAIT = const(0x1C)
9494
_SNSR_SOCK_LAST_ACK = const(0x1D)
9595
_SNSR_SOCK_UDP = const(0x22)
9696
_SNSR_SOCK_IPRAW = const(0x32)
@@ -260,7 +260,9 @@ def get_host_by_name(self, hostname: str) -> bytes:
260260
if isinstance(hostname, str):
261261
hostname = bytes(hostname, "utf-8")
262262
# Return IP assigned by DHCP
263-
_dns_client = dns.DNS(self, self._dns, debug=self._debug)
263+
_dns_client = dns.DNS(
264+
self, self.pretty_ip(bytearray(self._dns)), debug=self._debug
265+
)
264266
ret = _dns_client.gethostbyname(hostname)
265267
debug_msg("* Resolved IP: {}".format(ret), self._debug)
266268
if ret == -1:
@@ -817,9 +819,9 @@ def socket_open(self, socket_num: int, conn_mode: int = _SNMR_TCP) -> int:
817819
status = self.read_snsr(socket_num)[0]
818820
if status in (
819821
SNSR_SOCK_CLOSED,
820-
_SNSR_SOCK_TIME_WAIT,
822+
SNSR_SOCK_TIME_WAIT,
821823
SNSR_SOCK_FIN_WAIT,
822-
_SNSR_SOCK_CLOSE_WAIT,
824+
SNSR_SOCK_CLOSE_WAIT,
823825
_SNSR_SOCK_CLOSING,
824826
_SNSR_SOCK_UDP,
825827
):
@@ -897,7 +899,7 @@ def socket_read(self, socket_num: int, length: int) -> Tuple[int, bytes]:
897899
if ret == 0:
898900
# no data on socket?
899901
status = self._read_snmr(socket_num)
900-
if status in (SNSR_SOCK_LISTEN, SNSR_SOCK_CLOSED, _SNSR_SOCK_CLOSE_WAIT):
902+
if status in (SNSR_SOCK_LISTEN, SNSR_SOCK_CLOSED, SNSR_SOCK_CLOSE_WAIT):
901903
# remote end closed its side of the connection, EOF state
902904
raise RuntimeError("Lost connection to peer.")
903905
# connection is alive, no data waiting to be read
@@ -993,7 +995,7 @@ def socket_write(
993995
while free_size < ret:
994996
free_size = self._get_tx_free_size(socket_num)
995997
status = self.socket_status(socket_num)[0]
996-
if status not in (SNSR_SOCK_ESTABLISHED, _SNSR_SOCK_CLOSE_WAIT) or (
998+
if status not in (SNSR_SOCK_ESTABLISHED, SNSR_SOCK_CLOSE_WAIT) or (
997999
timeout and time.monotonic() - stamp > timeout
9981000
):
9991001
ret = 0
@@ -1032,18 +1034,18 @@ def socket_write(
10321034
time.sleep(0.001)
10331035

10341036
# check data was transferred correctly
1035-
while not self._read_snir(socket_num)[0] & _SNIR_SEND_OK:
1037+
while not self.read_snir(socket_num)[0] & _SNIR_SEND_OK:
10361038
if self.socket_status(socket_num)[0] in (
10371039
SNSR_SOCK_CLOSED,
1038-
_SNSR_SOCK_TIME_WAIT,
1040+
SNSR_SOCK_TIME_WAIT,
10391041
SNSR_SOCK_FIN_WAIT,
1040-
_SNSR_SOCK_CLOSE_WAIT,
1042+
SNSR_SOCK_CLOSE_WAIT,
10411043
_SNSR_SOCK_CLOSING,
10421044
):
10431045
raise RuntimeError("Socket closed before data was sent.")
10441046
if timeout and time.monotonic() - stamp > timeout:
10451047
raise RuntimeError("Operation timed out. No data sent.")
1046-
if self._read_snir(socket_num)[0] & _SNIR_TIMEOUT:
1048+
if self.read_snir(socket_num)[0] & _SNIR_TIMEOUT:
10471049
raise TimeoutError(
10481050
"Hardware timeout while sending on socket {}.".format(socket_num)
10491051
)
@@ -1127,7 +1129,7 @@ def read_snsr(self, sock: int) -> Optional[bytearray]:
11271129
"""Read Socket n Status Register."""
11281130
return self._read_socket(sock, _REG_SNSR)
11291131

1130-
def _read_snir(self, sock: int) -> Optional[bytearray]:
1132+
def read_snir(self, sock: int) -> Optional[bytearray]:
11311133
"""Read Socket n Interrupt Register."""
11321134
return self._read_socket(sock, _REG_SNIR)
11331135

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
except ImportError:
2525
pass
2626

27+
2728
import gc
2829
import time
2930
from random import randint

adafruit_wiznet5k/adafruit_wiznet5k_dns.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ def gethostbyname(self, hostname: bytes) -> Union[int, bytes]:
251251
self._query_id, self._query_length, buffer = _build_dns_query(hostname)
252252

253253
# Send DNS request packet
254-
self._sock.bind((None, _DNS_PORT))
255-
self._sock.connect((self._iface.pretty_ip(self._dns_server), _DNS_PORT))
254+
self._sock.bind(("", _DNS_PORT))
255+
self._sock.connect((self._dns_server, _DNS_PORT))
256256
_debug_print(debug=self._debug, message="* DNS: Sending request packet...")
257257
self._sock.send(buffer)
258258

@@ -261,9 +261,11 @@ def gethostbyname(self, hostname: bytes) -> Union[int, bytes]:
261261
for _ in range(5):
262262
# wait for a response
263263
socket_timeout = time.monotonic() + 1.0
264-
packet_size = self._sock.available()
264+
packet_size = self._sock._available() # pylint: disable=protected-access
265265
while packet_size == 0:
266-
packet_size = self._sock.available()
266+
packet_size = (
267+
self._sock._available() # pylint: disable=protected-access
268+
)
267269
if time.monotonic() > socket_timeout:
268270
_debug_print(
269271
debug=self._debug,
@@ -273,7 +275,7 @@ def gethostbyname(self, hostname: bytes) -> Union[int, bytes]:
273275
return -1
274276
time.sleep(0.05)
275277
# recv packet into buf
276-
buffer = self._sock.recv(512)
278+
buffer = self._sock.recv(512) # > UDP payload length
277279
_debug_print(
278280
debug=self._debug,
279281
message="DNS Packet Received: {}".format(buffer),

adafruit_wiznet5k/adafruit_wiznet5k_ntp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def get_time(self) -> time.struct_time:
7474
self._sock.sendto(self._pkt_buf_, (self._ntp_server, 123))
7575
end_time = time.monotonic() + 0.2 * 2**retry
7676
while time.monotonic() < end_time:
77-
data = self._sock.recv()
77+
data = self._sock.recv(64) # NTP returns a 48 byte message.
7878
if data:
7979
sec = data[40:44]
8080
int_cal = int.from_bytes(sec, "big")

0 commit comments

Comments
 (0)