Skip to content

Commit 94b0351

Browse files
authored
Merge pull request #97 from dearmash/master
Add UDP support
2 parents 96887ee + 974820a commit 94b0351

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@
8383
_GET_HOST_BY_NAME_CMD = const(0x35)
8484
_START_SCAN_NETWORKS = const(0x36)
8585
_GET_FW_VERSION_CMD = const(0x37)
86+
_SEND_UDP_DATA_CMD = const(0x39)
8687
_GET_TIME = const(0x3B)
8788
_GET_IDX_BSSID_CMD = const(0x3C)
8889
_GET_IDX_CHAN_CMD = const(0x3D)
8990
_PING_CMD = const(0x3E)
9091

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

692-
def socket_write(self, socket_num, buffer):
694+
def socket_write(self, socket_num, buffer, conn_mode=TCP_MODE):
693695
"""Write the bytearray buffer to a socket"""
694696
if self._debug:
695697
print("Writing:", buffer)
696698
self._socknum_ll[0][0] = socket_num
697699
sent = 0
698-
for chunk in range((len(buffer) // 64) + 1):
700+
total_chunks = (len(buffer) // 64) + 1
701+
send_command = _SEND_DATA_TCP_CMD
702+
if conn_mode == self.UDP_MODE: # UDP requires a different command to write
703+
send_command = _INSERT_DATABUF_TCP_CMD
704+
for chunk in range(total_chunks):
699705
resp = self._send_command_get_response(
700-
_SEND_DATA_TCP_CMD,
706+
send_command,
701707
(
702708
self._socknum_ll[0],
703709
memoryview(buffer)[(chunk * 64) : ((chunk + 1) * 64)],
@@ -706,6 +712,18 @@ def socket_write(self, socket_num, buffer):
706712
)
707713
sent += resp[0][0]
708714

715+
if conn_mode == self.UDP_MODE:
716+
# UDP verifies chunks on write, not bytes
717+
if sent != total_chunks:
718+
raise RuntimeError(
719+
"Failed to write %d chunks (sent %d)" % (total_chunks, sent)
720+
)
721+
# UDP needs to finalize with this command, does the actual sending
722+
resp = self._send_command_get_response(_SEND_UDP_DATA_CMD, self._socknum_ll)
723+
if resp[0][0] != 1:
724+
raise RuntimeError("Failed to send UDP data")
725+
return
726+
709727
if sent != len(buffer):
710728
raise RuntimeError(
711729
"Failed to send %d bytes (sent %d)" % (len(buffer), sent)
@@ -749,6 +767,12 @@ def socket_connect(self, socket_num, dest, port, conn_mode=TCP_MODE):
749767
print("*** Socket connect mode", conn_mode)
750768

751769
self.socket_open(socket_num, dest, port, conn_mode=conn_mode)
770+
if conn_mode == self.UDP_MODE:
771+
# UDP doesn't actually establish a connection
772+
# but the socket for writing is created via start_server
773+
self.start_server(port, socket_num, conn_mode)
774+
return True
775+
752776
times = time.monotonic()
753777
while (time.monotonic() - times) < 3: # wait 3 seconds
754778
if self.socket_connected(socket_num):

0 commit comments

Comments
 (0)