From 409d79636426bb3127e79b16c872dec8f8cf0bbf Mon Sep 17 00:00:00 2001 From: Oliver Baltzer Date: Wed, 21 Jul 2021 00:21:01 -0300 Subject: [PATCH 1/3] For UDP servers open socket on chip during bind UDP server support requires the socket on the chip to be opened during bind. For TCP the socket_listen function handles this and now bind calls an equivalent socket_listen_udp if the socket type is SOCK_DGRAM. --- adafruit_wiznet5k/adafruit_wiznet5k.py | 9 ++++++--- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index fdbc508..fa1631b 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -553,7 +553,7 @@ def get_socket(self): print("Allocated socket #{}".format(sock)) return sock - def socket_listen(self, socket_num, port): + def socket_listen(self, socket_num, port, conn_mode=SNMR_TCP): """Start listening on a socket (TCP mode only). :parm int socket_num: socket number :parm int port: port to listen on @@ -567,7 +567,7 @@ def socket_listen(self, socket_num, port): ) # Initialize a socket and set the mode self.src_port = port - res = self.socket_open(socket_num, conn_mode=SNMR_TCP) + res = self.socket_open(socket_num, conn_mode=conn_mode) self.src_port = 0 if res == 1: raise RuntimeError("Failed to initalize the socket.") @@ -575,11 +575,14 @@ def socket_listen(self, socket_num, port): self._send_socket_cmd(socket_num, CMD_SOCK_LISTEN) # Wait until ready status = [SNSR_SOCK_CLOSED] - while status[0] not in (SNSR_SOCK_LISTEN, SNSR_SOCK_ESTABLISHED): + while status[0] not in (SNSR_SOCK_LISTEN, SNSR_SOCK_ESTABLISHED, SNSR_SOCK_UDP): status = self._read_snsr(socket_num) if status[0] == SNSR_SOCK_CLOSED: raise RuntimeError("Listening socket closed.") + def socket_listen_udp(self, socket_num, port): + self.socket_listen(socket_num, port, SNMR_UDP) + def socket_accept(self, socket_num): """Gets the dest IP and port from an incoming connection. Returns the next socket number so listening can continue diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index 3b98267..954058c 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -181,6 +181,11 @@ def bind(self, address): if ip_address != current_ip: _the_interface.ifconfig = (ip_address, subnet_mask, gw_addr, dns) self._listen_port = address[1] + # For UDP servers we need to open the socket here because we won't call + # listen + if self._sock_type == SOCK_DGRAM: + _the_interface.socket_listen_udp(self.socknum, self._listen_port) + self._buffer = b"" def listen(self, backlog=None): """Listen on the port specified by bind. From 8fe0de857103ec66f59d73febdb58200c9fb6096 Mon Sep 17 00:00:00 2001 From: Oliver Baltzer Date: Wed, 21 Jul 2021 12:45:13 -0300 Subject: [PATCH 2/3] Moved call of socket_listen with SNMR_UDP to adafruit_wiznet5k_socket.bind() --- adafruit_wiznet5k/adafruit_wiznet5k.py | 5 +---- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 6 ++++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index fa1631b..730e370 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -554,7 +554,7 @@ def get_socket(self): return sock def socket_listen(self, socket_num, port, conn_mode=SNMR_TCP): - """Start listening on a socket (TCP mode only). + """Start listening on a socket (default TCP mode). :parm int socket_num: socket number :parm int port: port to listen on """ @@ -580,9 +580,6 @@ def socket_listen(self, socket_num, port, conn_mode=SNMR_TCP): if status[0] == SNSR_SOCK_CLOSED: raise RuntimeError("Listening socket closed.") - def socket_listen_udp(self, socket_num, port): - self.socket_listen(socket_num, port, SNMR_UDP) - def socket_accept(self, socket_num): """Gets the dest IP and port from an incoming connection. Returns the next socket number so listening can continue diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index 954058c..aa9bd16 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -181,10 +181,12 @@ def bind(self, address): if ip_address != current_ip: _the_interface.ifconfig = (ip_address, subnet_mask, gw_addr, dns) self._listen_port = address[1] - # For UDP servers we need to open the socket here because we won't call + # For UDP servers we need to open the socket here because we won't call # listen if self._sock_type == SOCK_DGRAM: - _the_interface.socket_listen_udp(self.socknum, self._listen_port) + _the_interface.socket_listen( + self.socknum, self._listen_port, wiznet5k.adafruit_wiznet5k.SNMR_UDP + ) self._buffer = b"" def listen(self, backlog=None): From d0887c356c5c64f6cd798d38974318cb2bd1a3d5 Mon Sep 17 00:00:00 2001 From: Oliver Baltzer Date: Wed, 21 Jul 2021 14:30:42 -0300 Subject: [PATCH 3/3] Update docstring with new conn_mode parameter --- adafruit_wiznet5k/adafruit_wiznet5k.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index 730e370..c713417 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -315,7 +315,7 @@ def remote_ip(self, socket_num): @property def link_status(self): - """"Returns if the PHY is connected.""" + """ "Returns if the PHY is connected.""" if self._chip_type == "w5500": data = self.read(REG_PHYCFGR, 0x00) return data[0] & 0x01 @@ -557,6 +557,7 @@ def socket_listen(self, socket_num, port, conn_mode=SNMR_TCP): """Start listening on a socket (default TCP mode). :parm int socket_num: socket number :parm int port: port to listen on + :parm int conn_mode: connection mode SNMR_TCP (default) or SNMR_UDP """ assert self.link_status, "Ethernet cable disconnected!" if self._debug: