83
83
_GET_HOST_BY_NAME_CMD = const (0x35 )
84
84
_START_SCAN_NETWORKS = const (0x36 )
85
85
_GET_FW_VERSION_CMD = const (0x37 )
86
+ _SEND_UDP_DATA_CMD = const (0x39 )
86
87
_GET_TIME = const (0x3B )
87
88
_GET_IDX_BSSID_CMD = const (0x3C )
88
89
_GET_IDX_CHAN_CMD = const (0x3D )
89
90
_PING_CMD = const (0x3E )
90
91
91
92
_SEND_DATA_TCP_CMD = const (0x44 )
92
93
_GET_DATABUF_TCP_CMD = const (0x45 )
94
+ _INSERT_DATABUF_TCP_CMD = const (0x46 )
93
95
_SET_ENT_IDENT_CMD = const (0x4A )
94
96
_SET_ENT_UNAME_CMD = const (0x4B )
95
97
_SET_ENT_PASSWD_CMD = const (0x4C )
@@ -689,15 +691,19 @@ def socket_connected(self, socket_num):
689
691
"""Test if a socket is connected to the destination, returns boolean true/false"""
690
692
return self .socket_status (socket_num ) == SOCKET_ESTABLISHED
691
693
692
- def socket_write (self , socket_num , buffer ):
694
+ def socket_write (self , socket_num , buffer , conn_mode = TCP_MODE ):
693
695
"""Write the bytearray buffer to a socket"""
694
696
if self ._debug :
695
697
print ("Writing:" , buffer )
696
698
self ._socknum_ll [0 ][0 ] = socket_num
697
699
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 ):
699
705
resp = self ._send_command_get_response (
700
- _SEND_DATA_TCP_CMD ,
706
+ send_command ,
701
707
(
702
708
self ._socknum_ll [0 ],
703
709
memoryview (buffer )[(chunk * 64 ) : ((chunk + 1 ) * 64 )],
@@ -706,6 +712,18 @@ def socket_write(self, socket_num, buffer):
706
712
)
707
713
sent += resp [0 ][0 ]
708
714
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
+
709
727
if sent != len (buffer ):
710
728
raise RuntimeError (
711
729
"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):
749
767
print ("*** Socket connect mode" , conn_mode )
750
768
751
769
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
+
752
776
times = time .monotonic ()
753
777
while (time .monotonic () - times ) < 3 : # wait 3 seconds
754
778
if self .socket_connected (socket_num ):
0 commit comments