Skip to content

Commit

Permalink
Merge pull request #97 from dearmash/master
Browse files Browse the repository at this point in the history
Add UDP support
  • Loading branch information
brentru authored May 7, 2020
2 parents 96887ee + 974820a commit 94b0351
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions adafruit_esp32spi/adafruit_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@
_GET_HOST_BY_NAME_CMD = const(0x35)
_START_SCAN_NETWORKS = const(0x36)
_GET_FW_VERSION_CMD = const(0x37)
_SEND_UDP_DATA_CMD = const(0x39)
_GET_TIME = const(0x3B)
_GET_IDX_BSSID_CMD = const(0x3C)
_GET_IDX_CHAN_CMD = const(0x3D)
_PING_CMD = const(0x3E)

_SEND_DATA_TCP_CMD = const(0x44)
_GET_DATABUF_TCP_CMD = const(0x45)
_INSERT_DATABUF_TCP_CMD = const(0x46)
_SET_ENT_IDENT_CMD = const(0x4A)
_SET_ENT_UNAME_CMD = const(0x4B)
_SET_ENT_PASSWD_CMD = const(0x4C)
Expand Down Expand Up @@ -689,15 +691,19 @@ def socket_connected(self, socket_num):
"""Test if a socket is connected to the destination, returns boolean true/false"""
return self.socket_status(socket_num) == SOCKET_ESTABLISHED

def socket_write(self, socket_num, buffer):
def socket_write(self, socket_num, buffer, conn_mode=TCP_MODE):
"""Write the bytearray buffer to a socket"""
if self._debug:
print("Writing:", buffer)
self._socknum_ll[0][0] = socket_num
sent = 0
for chunk in range((len(buffer) // 64) + 1):
total_chunks = (len(buffer) // 64) + 1
send_command = _SEND_DATA_TCP_CMD
if conn_mode == self.UDP_MODE: # UDP requires a different command to write
send_command = _INSERT_DATABUF_TCP_CMD
for chunk in range(total_chunks):
resp = self._send_command_get_response(
_SEND_DATA_TCP_CMD,
send_command,
(
self._socknum_ll[0],
memoryview(buffer)[(chunk * 64) : ((chunk + 1) * 64)],
Expand All @@ -706,6 +712,18 @@ def socket_write(self, socket_num, buffer):
)
sent += resp[0][0]

if conn_mode == self.UDP_MODE:
# UDP verifies chunks on write, not bytes
if sent != total_chunks:
raise RuntimeError(
"Failed to write %d chunks (sent %d)" % (total_chunks, sent)
)
# UDP needs to finalize with this command, does the actual sending
resp = self._send_command_get_response(_SEND_UDP_DATA_CMD, self._socknum_ll)
if resp[0][0] != 1:
raise RuntimeError("Failed to send UDP data")
return

if sent != len(buffer):
raise RuntimeError(
"Failed to send %d bytes (sent %d)" % (len(buffer), sent)
Expand Down Expand Up @@ -749,6 +767,12 @@ def socket_connect(self, socket_num, dest, port, conn_mode=TCP_MODE):
print("*** Socket connect mode", conn_mode)

self.socket_open(socket_num, dest, port, conn_mode=conn_mode)
if conn_mode == self.UDP_MODE:
# UDP doesn't actually establish a connection
# but the socket for writing is created via start_server
self.start_server(port, socket_num, conn_mode)
return True

times = time.monotonic()
while (time.monotonic() - times) < 3: # wait 3 seconds
if self.socket_connected(socket_num):
Expand Down

0 comments on commit 94b0351

Please sign in to comment.