Skip to content

fix UDP handling #59

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 3 commits into from
Jun 5, 2022
Merged
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: 13 additions & 1 deletion adafruit_espatcontrol/adafruit_espatcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def __init__(
self._ipdpacket = bytearray(1500)
self._ifconfig = []
self._initialized = False
self._conntype = None

def begin(self) -> None:
"""Initialize the module by syncing, resetting if necessary, setting up
Expand Down Expand Up @@ -187,6 +188,9 @@ def socket_connect(
can be an IP address or DNS (we'll do the lookup for you. Remote port
is integer port on other side. We can't set the local port"""
# lets just do one connection at a time for now
if conntype == self.TYPE_UDP:
# always disconnect for TYPE_UDP
self.socket_disconnect()
while True:
stat = self.status
if stat in (self.STATUS_APCONNECTED, self.STATUS_SOCKETCLOSED):
Expand All @@ -209,7 +213,12 @@ def socket_connect(
)
replies = self.at_response(cmd, timeout=10, retries=retries).split(b"\r\n")
for reply in replies:
if reply == b"CONNECT" and self.status == self.STATUS_SOCKETOPEN:
if reply == b"CONNECT" and (
conntype == self.TYPE_TCP
and self.status == self.STATUS_SOCKETOPEN
or conntype == self.TYPE_UDP
):
self._conntype = conntype
return True
return False

Expand All @@ -232,6 +241,8 @@ def socket_send(self, buffer: bytes, timeout: int = 1) -> bool:
raise RuntimeError("Didn't get data prompt for sending")
self._uart.reset_input_buffer()
self._uart.write(buffer)
if self._conntype == self.TYPE_UDP:
return True
stamp = time.monotonic()
response = b""
while (time.monotonic() - stamp) < timeout:
Expand Down Expand Up @@ -314,6 +325,7 @@ def socket_receive(self, timeout: int = 5) -> bytearray:

def socket_disconnect(self) -> None:
"""Close any open socket, if there is one"""
self._conntype = None
try:
self.at_response("AT+CIPCLOSE", retries=1)
except OKError:
Expand Down