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