Skip to content

Commit 9fd20cf

Browse files
authored
Merge pull request #157 from adafruit/core-compatible-socket-type-numbers
Core compatible constants & setsockopt
2 parents 9cfd92f + 72fd989 commit 9fd20cf

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,6 @@ def _unprettyfy(data: str, seperator: str, correct_length: int) -> bytes:
184184
class WIZNET5K: # pylint: disable=too-many-public-methods, too-many-instance-attributes
185185
"""Interface for WIZNET5K module."""
186186

187-
_TCP_MODE = const(0x21)
188-
_UDP_MODE = const(0x02)
189-
_TLS_MODE = const(0x03) # This is NOT currently implemented
190-
191187
_sockets_reserved = []
192188

193189
# pylint: disable=too-many-arguments

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,16 @@ def inet_ntoa(ip_address: Union[bytes, bytearray]) -> str:
147147
return _the_interface.pretty_ip(ip_address)
148148

149149

150-
SOCK_STREAM = const(0x21) # TCP
151-
_TCP_MODE = 80
152-
SOCK_DGRAM = const(0x02) # UDP
150+
# These must match circuitpython "socketpoool" values. However, we cannot
151+
# depend on socketpool being importable, so hard-code them here.
152+
SOCK_STREAM = 1
153+
SOCK_DGRAM = 2
154+
155+
_SOCKET_TYPE_TO_WIZNET = b"\0\x21\2"
156+
157+
SOL_SOCKET = 0xFFF
158+
SO_REUSEADDR = 0x0004
159+
153160
AF_INET = const(3)
154161
_SOCKET_INVALID = const(255)
155162

@@ -440,7 +447,7 @@ def connect(self, address: Tuple[str, int]) -> None:
440447
self._socknum,
441448
_the_interface.unpretty_ip(gethostbyname(address[0])),
442449
address[1],
443-
self._sock_type,
450+
_SOCKET_TYPE_TO_WIZNET[self._sock_type],
444451
)
445452
_the_interface.src_port = 0
446453
if not result:
@@ -693,7 +700,23 @@ def _available(self) -> int:
693700
694701
:return int: Number of bytes available.
695702
"""
696-
return _the_interface.socket_available(self._socknum, self._sock_type)
703+
return _the_interface.socket_available(
704+
self._socknum, _SOCKET_TYPE_TO_WIZNET[self._sock_type]
705+
)
706+
707+
@_check_socket_closed
708+
def setsockopt( # pylint: disable=no-self-use
709+
self, level: int, opt: int, value: any
710+
) -> None:
711+
"""
712+
Set a socket option.
713+
714+
Only SOL_SOCKET SO_REUSEADDR is accepted (and the value is ignored).
715+
716+
Other calls result in OSError."""
717+
if level == SOL_SOCKET and opt == SO_REUSEADDR:
718+
return
719+
raise OSError
697720

698721
@_check_socket_closed
699722
def settimeout(self, value: Optional[float]) -> None:

0 commit comments

Comments
 (0)