Skip to content
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

Core compatible constants & setsockopt #157

Merged
merged 3 commits into from
Apr 22, 2024
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
4 changes: 0 additions & 4 deletions adafruit_wiznet5k/adafruit_wiznet5k.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ def _unprettyfy(data: str, seperator: str, correct_length: int) -> bytes:
class WIZNET5K: # pylint: disable=too-many-public-methods, too-many-instance-attributes
"""Interface for WIZNET5K module."""

_TCP_MODE = const(0x21)
_UDP_MODE = const(0x02)
_TLS_MODE = const(0x03) # This is NOT currently implemented

_sockets_reserved = []

# pylint: disable=too-many-arguments
Expand Down
33 changes: 28 additions & 5 deletions adafruit_wiznet5k/adafruit_wiznet5k_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,16 @@ def inet_ntoa(ip_address: Union[bytes, bytearray]) -> str:
return _the_interface.pretty_ip(ip_address)


SOCK_STREAM = const(0x21) # TCP
_TCP_MODE = 80
SOCK_DGRAM = const(0x02) # UDP
# These must match circuitpython "socketpoool" values. However, we cannot
# depend on socketpool being importable, so hard-code them here.
SOCK_STREAM = 1
SOCK_DGRAM = 2

_SOCKET_TYPE_TO_WIZNET = b"\0\x21\2"

SOL_SOCKET = 0xFFF
SO_REUSEADDR = 0x0004

AF_INET = const(3)
_SOCKET_INVALID = const(255)

Expand Down Expand Up @@ -431,7 +438,7 @@ def connect(self, address: Tuple[str, int]) -> None:
self._socknum,
_the_interface.unpretty_ip(gethostbyname(address[0])),
address[1],
self._sock_type,
_SOCKET_TYPE_TO_WIZNET[self._sock_type],
)
_the_interface.src_port = 0
if not result:
Expand Down Expand Up @@ -674,7 +681,23 @@ def _available(self) -> int:

:return int: Number of bytes available.
"""
return _the_interface.socket_available(self._socknum, self._sock_type)
return _the_interface.socket_available(
self._socknum, _SOCKET_TYPE_TO_WIZNET[self._sock_type]
)

@_check_socket_closed
def setsockopt( # pylint: disable=no-self-use
self, level: int, opt: int, value: any
) -> None:
"""
Set a socket option.

Only SOL_SOCKET SO_REUSEADDR is accepted (and the value is ignored).

Other calls result in OSError."""
if level == SOL_SOCKET and opt == SO_REUSEADDR:
return
raise OSError

@_check_socket_closed
def settimeout(self, value: Optional[float]) -> None:
Expand Down
Loading