5
5
# SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries
6
6
# SPDX-FileCopyrightText: 2021 Patrick Van Oosterwijck
7
7
# SPDX-FileCopyrightText: 2021 Adam Cummick
8
+ # SPDX-FileCopyrightText: 2023 Martin Stephens
8
9
#
9
10
# SPDX-License-Identifier: MIT
10
11
"""
155
156
_MR_RST = const (0x80 ) # Mode Register RST
156
157
# Socket mode register
157
158
_SNMR_CLOSE = const (0x00 )
158
- _SNMR_TCP = const (0x21 )
159
+ SNMR_TCP = const (0x21 )
159
160
SNMR_UDP = const (0x02 )
160
161
_SNMR_IPRAW = const (0x03 )
161
162
_SNMR_MACRAW = const (0x04 )
@@ -377,7 +378,7 @@ def mac_address(self, address: Union[MacAddressRaw, str]) -> None:
377
378
"""
378
379
Set the WIZnet hardware MAC address.
379
380
380
- :param Union[MacAddressRaw, str] address: A 6 byte hardware MAC address.
381
+ :param Union[MacAddressRaw, str] address: A hardware MAC address.
381
382
382
383
:raises ValueError: If the MAC address is invalid
383
384
"""
@@ -397,7 +398,7 @@ def mac_address(self, address: Union[MacAddressRaw, str]) -> None:
397
398
@staticmethod
398
399
def pretty_mac (mac : bytes ) -> str :
399
400
"""
400
- Convert a byte MAC address to a ':' seperated string for display.
401
+ Convert a bytes MAC address to a ':' seperated string for display.
401
402
402
403
:param bytes mac: The MAC address.
403
404
@@ -406,7 +407,7 @@ def pretty_mac(mac: bytes) -> str:
406
407
:raises ValueError: If MAC address is not 6 bytes.
407
408
"""
408
409
if len (mac ) != 6 :
409
- raise ValueError ("Wrong length for MAC address." )
410
+ raise ValueError ("Incorrect length for MAC address." )
410
411
return ":" .join (f"{ byte :02x} " for byte in mac )
411
412
412
413
def remote_ip (self , socket_num : int ) -> str :
@@ -491,7 +492,7 @@ def ifconfig(
491
492
492
493
# *** Public Socket Methods ***
493
494
494
- def socket_available (self , socket_num : int , sock_type : int = _SNMR_TCP ) -> int :
495
+ def socket_available (self , socket_num : int , sock_type : int = SNMR_TCP ) -> int :
495
496
"""
496
497
Number of bytes available to be read from the socket.
497
498
@@ -502,6 +503,7 @@ def socket_available(self, socket_num: int, sock_type: int = _SNMR_TCP) -> int:
502
503
:return int: Number of bytes available to read.
503
504
504
505
:raises ValueError: If the socket number is out of range.
506
+ :raises ValueError: If the number of bytes on a UDP socket is negative.
505
507
"""
506
508
debug_msg (
507
509
"socket_available called on socket {}, protocol {}" .format (
@@ -518,22 +520,6 @@ def socket_available(self, socket_num: int, sock_type: int = _SNMR_TCP) -> int:
518
520
raise ValueError ("Negative number of bytes found on socket." )
519
521
return number_of_bytes
520
522
521
- # if sock_type == _SNMR_TCP:
522
- # return number_of_bytes
523
- # if number_of_bytes > 0:
524
- # if self.udp_datasize[socket_num]:
525
- # return self.udp_datasize[socket_num]
526
- # # parse the udp rx packet
527
- # # read the first 8 header bytes
528
- # udp_bytes, self._pbuff[:8] = self.socket_read(socket_num, 8)
529
- # if udp_bytes > 0:
530
- # self.udp_from_ip[socket_num] = self._pbuff[:4]
531
- # self.udp_from_port[socket_num] = (self._pbuff[4] << 8) + self._pbuff[5]
532
- # self.udp_datasize[socket_num] = (self._pbuff[6] << 8) + self._pbuff[7]
533
- # udp_bytes = self.udp_datasize[socket_num]
534
- # return udp_bytes
535
- # return 0
536
-
537
523
def socket_status (self , socket_num : int ) -> int :
538
524
"""
539
525
Socket connection status.
@@ -552,17 +538,17 @@ def socket_status(self, socket_num: int) -> int:
552
538
def socket_connect (
553
539
self ,
554
540
socket_num : int ,
555
- dest : Union [ bytes , IpAddress4Raw ] ,
541
+ dest : IpAddress4Raw ,
556
542
port : int ,
557
- conn_mode : int = _SNMR_TCP ,
543
+ conn_mode : int = SNMR_TCP ,
558
544
) -> int :
559
545
"""
560
546
Open and verify a connection from a socket to a destination IPv4 address
561
547
or hostname. A TCP connection is made by default. A UDP connection can also
562
548
be made.
563
549
564
550
:param int socket_num: ID of the socket to be connected.
565
- :param Union[bytes, Address4Bytes] dest: The destination as a host name or IP address.
551
+ :param IpAddress4Raw dest: The destination as a host name or IP address.
566
552
:param int port: Port to connect to (0 - 65,535).
567
553
:param int conn_mode: The connection mode. Use SNMR_TCP for TCP or SNMR_UDP for UDP,
568
554
defaults to SNMR_TCP.
@@ -585,7 +571,7 @@ def socket_connect(
585
571
self .write_sndport (socket_num , port )
586
572
self .write_sncr (socket_num , _CMD_SOCK_CONNECT )
587
573
588
- if conn_mode == _SNMR_TCP :
574
+ if conn_mode == SNMR_TCP :
589
575
# wait for tcp connection establishment
590
576
while self .socket_status (socket_num ) != SNSR_SOCK_ESTABLISHED :
591
577
time .sleep (0.001 )
@@ -652,7 +638,7 @@ def release_socket(self, socket_number):
652
638
WIZNET5K ._sockets_reserved [socket_number - 1 ] = False
653
639
654
640
def socket_listen (
655
- self , socket_num : int , port : int , conn_mode : int = _SNMR_TCP
641
+ self , socket_num : int , port : int , conn_mode : int = SNMR_TCP
656
642
) -> None :
657
643
"""
658
644
Listen on a socket's port.
@@ -717,7 +703,7 @@ def socket_accept(self, socket_num: int) -> Tuple[int, Tuple[str, int]]:
717
703
)
718
704
return next_socknum , (dest_ip , dest_port )
719
705
720
- def socket_open (self , socket_num : int , conn_mode : int = _SNMR_TCP ) -> None :
706
+ def socket_open (self , socket_num : int , conn_mode : int = SNMR_TCP ) -> None :
721
707
"""
722
708
Open an IP socket.
723
709
0 commit comments