From 2b869b227fcd05c18720467c261ed751c0c96801 Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Thu, 30 Nov 2023 07:45:58 -0600 Subject: [PATCH 01/17] Add feature to detect socketcand beacon --- can/interfaces/socketcand/socketcand.py | 88 +++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 8b55eef3a..30ea10b1a 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -19,6 +19,87 @@ log = logging.getLogger(__name__) +DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS = "" +DEFAULT_SOCKETCAND_DISCOVERY_PORT = 42000 + + +def detect_beacon(): + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.bind((DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, DEFAULT_SOCKETCAND_DISCOVERY_PORT)) + log.info( + f"Listening on for socketcand UDP advertisement on {DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS}:{DEFAULT_SOCKETCAND_DISCOVERY_PORT}" + ) + + # Time between beacons no more than 3 seconds. Allow for at least 3 + timeout_ms = 12000 + now = time.time() * 1000 + end_time = now + timeout_ms + while (time.time() * 1000) < end_time: + try: + # get all sockets that are ready (can be a list with a single value + # being self.socket or an empty list if self.socket is not ready) + ready_receive_sockets, _, _ = select.select([sock], [], [], 1) + + if not ready_receive_sockets: + log.debug("No advertisement received") + continue + + msg = sock.recv(1024).decode("utf-8") + root = ET.fromstring(msg) + if root.tag != "CANBeacon": + log.debug("Unexpected message received over UDP") + continue + + det_devs = [] + det_host = None + det_port = None + for child in root: + if child.tag == "Bus": + bus_name = child.attrib["name"] + det_devs.append(bus_name) + elif child.tag == "URL": + url = urlparselib.urlparse(child.text) + det_host = url.hostname + det_port = url.port + + if not det_devs: + log.debug( + "Got advertisement, but no SocketCAN devices advertised by socketcand" + ) + continue + + if (det_host is None) or (det_port is None): + det_host = None + det_port = None + log.debug( + "Got advertisement, but no SocketCAN URL advertised by socketcand" + ) + continue + + log.info(f"Found SocketCAN devices: {det_devs}") + return [ + { + "interface": "socketcand", + "host": det_host, + "port": det_port, + "channel": channel, + } + for channel in det_devs + ] + + except ET.ParseError: + log.debug("Unexpected message received over UDP") + continue + + except Exception as exc: + # something bad happened (e.g. the interface went down) + log.error(f"Failed to detect beacon: {exc} {traceback.format_exc()}") + raise OSError(f"Failed to detect beacon: {exc} {traceback.format_exc()}") + + raise TimeoutError( + f"detect_beacon: Failed to detect udp beacon for {timeout_ms} ms" + ) + def convert_ascii_message_to_can_message(ascii_msg: str) -> can.Message: if not ascii_msg.startswith("< frame ") or not ascii_msg.endswith(" >"): @@ -79,6 +160,9 @@ class SocketCanDaemonBus(can.BusABC): def __init__(self, channel, host, port, tcp_tune=False, can_filters=None, **kwargs): """Connects to a CAN bus served by socketcand. + It implements :meth:`can.BusABC._detect_available_configs` to search for + available interfaces. + It will attempt to connect to the server for up to 10s, after which a TimeoutError exception will be thrown. @@ -231,3 +315,7 @@ def shutdown(self): """Stops all active periodic tasks and closes the socket.""" super().shutdown() self.__socket.close() + + @staticmethod + def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]: + return detect_beacon() From 3ca43a61b89507ba5ceb36fc3746a60f06063a5c Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Thu, 30 Nov 2023 08:03:23 -0600 Subject: [PATCH 02/17] Fix imports and formatting --- can/interfaces/socketcand/socketcand.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 30ea10b1a..0f4fe7262 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -13,7 +13,10 @@ import socket import time import traceback +import urllib.parse as urlparselib +import xml.etree.ElementTree as ET from collections import deque +from typing import List import can From 86f8807db1d873a79a6e4a302dfbe921540c4233 Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Thu, 30 Nov 2023 10:33:01 -0600 Subject: [PATCH 03/17] Return empty list if no beacon detected --- can/interfaces/socketcand/socketcand.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 0f4fe7262..3372824ce 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -321,4 +321,8 @@ def shutdown(self): @staticmethod def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]: - return detect_beacon() + try: + return detect_beacon() + except Exception as e: + log.warning(f"Could not detect socketcand beacon: {e}") + return [] From ddd0a32ba1b77db096b05e39c440eb2605101699 Mon Sep 17 00:00:00 2001 From: Faisal Shah <37458679+faisal-shah@users.noreply.github.com> Date: Fri, 1 Dec 2023 12:02:26 -0600 Subject: [PATCH 04/17] Use %-format Co-authored-by: zariiii9003 <52598363+zariiii9003@users.noreply.github.com> --- can/interfaces/socketcand/socketcand.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 3372824ce..f04aafeb6 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -30,7 +30,9 @@ def detect_beacon(): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, DEFAULT_SOCKETCAND_DISCOVERY_PORT)) log.info( - f"Listening on for socketcand UDP advertisement on {DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS}:{DEFAULT_SOCKETCAND_DISCOVERY_PORT}" + "Listening on for socketcand UDP advertisement on %s:%s", + DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, + DEFAULT_SOCKETCAND_DISCOVERY_PORT ) # Time between beacons no more than 3 seconds. Allow for at least 3 From eadb8b8170f5f51dd18d8e552adf91d1f941f8ef Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Fri, 1 Dec 2023 12:12:06 -0600 Subject: [PATCH 05/17] black format --- can/interfaces/socketcand/socketcand.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index f04aafeb6..93a68ee2e 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -32,7 +32,7 @@ def detect_beacon(): log.info( "Listening on for socketcand UDP advertisement on %s:%s", DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, - DEFAULT_SOCKETCAND_DISCOVERY_PORT + DEFAULT_SOCKETCAND_DISCOVERY_PORT, ) # Time between beacons no more than 3 seconds. Allow for at least 3 From 8e4a6db5115e738c75f24c221f0eccecccac5807 Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Fri, 1 Dec 2023 12:13:11 -0600 Subject: [PATCH 06/17] Use context manager for detect_beacon() udp sock --- can/interfaces/socketcand/socketcand.py | 144 ++++++++++++------------ 1 file changed, 74 insertions(+), 70 deletions(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 93a68ee2e..1eec0ac86 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -27,83 +27,87 @@ def detect_beacon(): - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sock.bind((DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, DEFAULT_SOCKETCAND_DISCOVERY_PORT)) - log.info( - "Listening on for socketcand UDP advertisement on %s:%s", - DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, - DEFAULT_SOCKETCAND_DISCOVERY_PORT, - ) - - # Time between beacons no more than 3 seconds. Allow for at least 3 - timeout_ms = 12000 - now = time.time() * 1000 - end_time = now + timeout_ms - while (time.time() * 1000) < end_time: - try: - # get all sockets that are ready (can be a list with a single value - # being self.socket or an empty list if self.socket is not ready) - ready_receive_sockets, _, _ = select.select([sock], [], [], 1) + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: + sock.bind( + (DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, DEFAULT_SOCKETCAND_DISCOVERY_PORT) + ) + log.info( + "Listening on for socketcand UDP advertisement on %s:%s", + DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, + DEFAULT_SOCKETCAND_DISCOVERY_PORT, + ) - if not ready_receive_sockets: - log.debug("No advertisement received") - continue + # Time between beacons no more than 3 seconds. Allow for at least 3 + timeout_ms = 12000 + now = time.time() * 1000 + end_time = now + timeout_ms + while (time.time() * 1000) < end_time: + try: + # get all sockets that are ready (can be a list with a single value + # being self.socket or an empty list if self.socket is not ready) + ready_receive_sockets, _, _ = select.select([sock], [], [], 1) + + if not ready_receive_sockets: + log.debug("No advertisement received") + continue + + msg = sock.recv(1024).decode("utf-8") + root = ET.fromstring(msg) + if root.tag != "CANBeacon": + log.debug("Unexpected message received over UDP") + continue + + det_devs = [] + det_host = None + det_port = None + for child in root: + if child.tag == "Bus": + bus_name = child.attrib["name"] + det_devs.append(bus_name) + elif child.tag == "URL": + url = urlparselib.urlparse(child.text) + det_host = url.hostname + det_port = url.port + + if not det_devs: + log.debug( + "Got advertisement, but no SocketCAN devices advertised by socketcand" + ) + continue - msg = sock.recv(1024).decode("utf-8") - root = ET.fromstring(msg) - if root.tag != "CANBeacon": + if (det_host is None) or (det_port is None): + det_host = None + det_port = None + log.debug( + "Got advertisement, but no SocketCAN URL advertised by socketcand" + ) + continue + + log.info(f"Found SocketCAN devices: {det_devs}") + return [ + { + "interface": "socketcand", + "host": det_host, + "port": det_port, + "channel": channel, + } + for channel in det_devs + ] + + except ET.ParseError: log.debug("Unexpected message received over UDP") continue - det_devs = [] - det_host = None - det_port = None - for child in root: - if child.tag == "Bus": - bus_name = child.attrib["name"] - det_devs.append(bus_name) - elif child.tag == "URL": - url = urlparselib.urlparse(child.text) - det_host = url.hostname - det_port = url.port - - if not det_devs: - log.debug( - "Got advertisement, but no SocketCAN devices advertised by socketcand" + except Exception as exc: + # something bad happened (e.g. the interface went down) + log.error(f"Failed to detect beacon: {exc} {traceback.format_exc()}") + raise OSError( + f"Failed to detect beacon: {exc} {traceback.format_exc()}" ) - continue - - if (det_host is None) or (det_port is None): - det_host = None - det_port = None - log.debug( - "Got advertisement, but no SocketCAN URL advertised by socketcand" - ) - continue - - log.info(f"Found SocketCAN devices: {det_devs}") - return [ - { - "interface": "socketcand", - "host": det_host, - "port": det_port, - "channel": channel, - } - for channel in det_devs - ] - except ET.ParseError: - log.debug("Unexpected message received over UDP") - continue - - except Exception as exc: - # something bad happened (e.g. the interface went down) - log.error(f"Failed to detect beacon: {exc} {traceback.format_exc()}") - raise OSError(f"Failed to detect beacon: {exc} {traceback.format_exc()}") - - raise TimeoutError( - f"detect_beacon: Failed to detect udp beacon for {timeout_ms} ms" - ) + raise TimeoutError( + f"detect_beacon: Failed to detect udp beacon for {timeout_ms} ms" + ) def convert_ascii_message_to_can_message(ascii_msg: str) -> can.Message: From 99038a3c0b2ade2affe6a9b0c230822986bb63e1 Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Tue, 5 Dec 2023 10:51:29 -0600 Subject: [PATCH 07/17] Add timeout as parameter, and set to 3.1s --- can/interfaces/socketcand/socketcand.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 1eec0ac86..ec83c5366 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -26,7 +26,7 @@ DEFAULT_SOCKETCAND_DISCOVERY_PORT = 42000 -def detect_beacon(): +def detect_beacon(timeout_ms): with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: sock.bind( (DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, DEFAULT_SOCKETCAND_DISCOVERY_PORT) @@ -37,8 +37,6 @@ def detect_beacon(): DEFAULT_SOCKETCAND_DISCOVERY_PORT, ) - # Time between beacons no more than 3 seconds. Allow for at least 3 - timeout_ms = 12000 now = time.time() * 1000 end_time = now + timeout_ms while (time.time() * 1000) < end_time: @@ -328,7 +326,9 @@ def shutdown(self): @staticmethod def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]: try: - return detect_beacon() + # Time between beacons no more than 3 seconds. Allow for a little + # more than 3s + return detect_beacon(3.1) except Exception as e: log.warning(f"Could not detect socketcand beacon: {e}") return [] From a15928eab9f942e25b86934b1c4f9d62bb209c1a Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Tue, 5 Dec 2023 12:53:15 -0600 Subject: [PATCH 08/17] Document detect_beacon --- can/interfaces/socketcand/socketcand.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index ec83c5366..ac1ae8992 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -26,7 +26,24 @@ DEFAULT_SOCKETCAND_DISCOVERY_PORT = 42000 -def detect_beacon(timeout_ms): +def detect_beacon(timeout_ms) -> List[can.typechecking.AutoDetectedConfig]: + """ + Detects socketcand servers + + This is what :meth:`can.SocketCanDaemonBus._detect_available_configs` + uses to search for available socketcand servers with fixed timeout of + 3.1 seconds (socketcand sends out a beacon packet every 3 seconds). + + Using this method directly allows for adjusting the timeout. Extending + the timeout beyond the default time period could be useful if UDP + packet loss is a concern. + + :param timeout_ms: + Timeout in milliseconds to wait for socketcand beacon packets + + :return: + See :meth:`can.SocketCanDaemonBus._detect_available_configs` + """ with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: sock.bind( (DEFAULT_SOCKETCAND_DISCOVERY_ADDRESS, DEFAULT_SOCKETCAND_DISCOVERY_PORT) From e82bb9d0a79461630674962f46bed686773e0824 Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Tue, 5 Dec 2023 13:36:42 -0600 Subject: [PATCH 09/17] detect_beacon return empty if timed out --- can/interfaces/socketcand/socketcand.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index ac1ae8992..9b6d0f7e2 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -120,9 +120,7 @@ def detect_beacon(timeout_ms) -> List[can.typechecking.AutoDetectedConfig]: f"Failed to detect beacon: {exc} {traceback.format_exc()}" ) - raise TimeoutError( - f"detect_beacon: Failed to detect udp beacon for {timeout_ms} ms" - ) + return [] def convert_ascii_message_to_can_message(ascii_msg: str) -> can.Message: From ab67db099cb4d2c35a66732ce8147f05de33d86f Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Tue, 5 Dec 2023 13:36:59 -0600 Subject: [PATCH 10/17] export detect_beacon --- can/interfaces/socketcand/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/can/interfaces/socketcand/__init__.py b/can/interfaces/socketcand/__init__.py index ce18441bc..64950f7f4 100644 --- a/can/interfaces/socketcand/__init__.py +++ b/can/interfaces/socketcand/__init__.py @@ -8,7 +8,8 @@ __all__ = [ "SocketCanDaemonBus", + "detect_beacon", "socketcand", ] -from .socketcand import SocketCanDaemonBus +from .socketcand import SocketCanDaemonBus, detect_beacon From 56f9fd75db218d0135d19658c96b3f972c4cdf88 Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Tue, 5 Dec 2023 13:37:11 -0600 Subject: [PATCH 11/17] Update documentation for auto config --- can/interfaces/socketcand/socketcand.py | 2 +- doc/interfaces/socketcand.rst | 33 ++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 9b6d0f7e2..1007c7c03 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -42,7 +42,7 @@ def detect_beacon(timeout_ms) -> List[can.typechecking.AutoDetectedConfig]: Timeout in milliseconds to wait for socketcand beacon packets :return: - See :meth:`can.SocketCanDaemonBus._detect_available_configs` + See :meth:`can.BusABC._detect_available_configs` """ with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: sock.bind( diff --git a/doc/interfaces/socketcand.rst b/doc/interfaces/socketcand.rst index a8a314521..6ce1fc249 100644 --- a/doc/interfaces/socketcand.rst +++ b/doc/interfaces/socketcand.rst @@ -2,8 +2,8 @@ socketcand Interface ==================== -`Socketcand `__ is part of the -`Linux-CAN `__ project, providing a +`Socketcand `__ is part of the +`Linux-CAN `__ project, providing a Network-to-CAN bridge as a Linux damon. It implements a specific `TCP/IP based communication protocol `__ to transfer CAN frames and control commands. @@ -11,7 +11,7 @@ to transfer CAN frames and control commands. The main advantage compared to UDP-based protocols (e.g. virtual interface) is, that TCP guarantees delivery and that the message order is kept. -Here is a small example dumping all can messages received by a socketcand +Here is a small example dumping all can messages received by a socketcand daemon running on a remote Raspberry Pi: .. code-block:: python @@ -37,6 +37,33 @@ The output may look like this:: Timestamp: 1637791111.609763 ID: 0000031d X Rx DLC: 8 16 27 d8 3d fe d8 31 24 Timestamp: 1637791111.634630 ID: 00000587 X Rx DLC: 8 4e 06 85 23 6f 81 2b 65 + +This interface also supports :meth:`can.BusABC._detect_available_configs`. + +.. code-block:: python + + import can + import can.interfaces.socketcand + + cfg = can.interfaces.socketcand._detect_available_configs() + if cfg: + bus = can.Bus(**cfg[0]) + +The socketcand daemon broadcasts UDP beacons every 3 seconds. The default +detection method waits for slightly more than 3 seconds to receive the beacon +packet. If you want to increase the timeout, you can use +:meth:`can.interfaces.socketcand.detect_beacon` directly. Below is an example +which detects the beacon and uses the configuration to create a socketcand bus. + +.. code-block:: python + + import can + import can.interfaces.socketcand + + cfg = can.interfaces.socketcand.detect_beacon(6000) + if cfg: + bus = can.Bus(**cfg[0]) + Bus --- From 7f7224b7c5b22d6d6fe8ff63cd486a69aa8f6605 Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Tue, 2 Jan 2024 13:11:23 -0600 Subject: [PATCH 12/17] More documentation fixes --- can/interfaces/socketcand/socketcand.py | 8 ++++---- doc/interfaces/socketcand.rst | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 1007c7c03..7a8fe3a76 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -30,9 +30,9 @@ def detect_beacon(timeout_ms) -> List[can.typechecking.AutoDetectedConfig]: """ Detects socketcand servers - This is what :meth:`can.SocketCanDaemonBus._detect_available_configs` - uses to search for available socketcand servers with fixed timeout of - 3.1 seconds (socketcand sends out a beacon packet every 3 seconds). + This is what :meth:`can.detect_available_configs` ends up calling + search for available socketcand servers with fixed timeout of 3.1 + seconds (socketcand sends out a beacon packet every 3 seconds). Using this method directly allows for adjusting the timeout. Extending the timeout beyond the default time period could be useful if UDP @@ -42,7 +42,7 @@ def detect_beacon(timeout_ms) -> List[can.typechecking.AutoDetectedConfig]: Timeout in milliseconds to wait for socketcand beacon packets :return: - See :meth:`can.BusABC._detect_available_configs` + See :meth:`~can.detect_available_configs` """ with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: sock.bind( diff --git a/doc/interfaces/socketcand.rst b/doc/interfaces/socketcand.rst index 6ce1fc249..f861c81b9 100644 --- a/doc/interfaces/socketcand.rst +++ b/doc/interfaces/socketcand.rst @@ -38,7 +38,7 @@ The output may look like this:: Timestamp: 1637791111.634630 ID: 00000587 X Rx DLC: 8 4e 06 85 23 6f 81 2b 65 -This interface also supports :meth:`can.BusABC._detect_available_configs`. +This interface also supports :meth:`~can.detect_available_configs`. .. code-block:: python @@ -72,6 +72,8 @@ Bus :member-order: bysource :members: +.. autofunction:: can.interfaces.socketcand.detect_beacon + Socketcand Quickstart --------------------- From 651b0f90ed0fac06ffb4586f71088e6d34536d5f Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Tue, 2 Jan 2024 14:08:20 -0600 Subject: [PATCH 13/17] Trigger tests From fb1f2ad4b6f435b332a62b4659515dcaecaf1200 Mon Sep 17 00:00:00 2001 From: Faisal Shah <37458679+faisal-shah@users.noreply.github.com> Date: Thu, 4 Jan 2024 09:57:00 -0600 Subject: [PATCH 14/17] Set default timeout Co-authored-by: zariiii9003 <52598363+zariiii9003@users.noreply.github.com> --- can/interfaces/socketcand/socketcand.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 7a8fe3a76..5c310f396 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -26,7 +26,7 @@ DEFAULT_SOCKETCAND_DISCOVERY_PORT = 42000 -def detect_beacon(timeout_ms) -> List[can.typechecking.AutoDetectedConfig]: +def detect_beacon(timeout_ms: int = 3100) -> List[can.typechecking.AutoDetectedConfig]: """ Detects socketcand servers From c81259123c11403b7ef6f718f1ee3ae6d03e0a54 Mon Sep 17 00:00:00 2001 From: Faisal Shah <37458679+faisal-shah@users.noreply.github.com> Date: Thu, 4 Jan 2024 09:58:01 -0600 Subject: [PATCH 15/17] Use default timeout Co-authored-by: zariiii9003 <52598363+zariiii9003@users.noreply.github.com> --- can/interfaces/socketcand/socketcand.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 5c310f396..74406b381 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -343,7 +343,7 @@ def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]: try: # Time between beacons no more than 3 seconds. Allow for a little # more than 3s - return detect_beacon(3.1) + return detect_beacon() except Exception as e: log.warning(f"Could not detect socketcand beacon: {e}") return [] From b90362498fef9b3a5ebb16695b7b7d04b660a197 Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Thu, 4 Jan 2024 09:59:11 -0600 Subject: [PATCH 16/17] Fix docstring indentation --- can/interfaces/socketcand/socketcand.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index 74406b381..d5f9fc5cd 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -30,19 +30,19 @@ def detect_beacon(timeout_ms: int = 3100) -> List[can.typechecking.AutoDetectedC """ Detects socketcand servers - This is what :meth:`can.detect_available_configs` ends up calling - search for available socketcand servers with fixed timeout of 3.1 - seconds (socketcand sends out a beacon packet every 3 seconds). + This is what :meth:`can.detect_available_configs` ends up calling + search for available socketcand servers with fixed timeout of 3.1 + seconds (socketcand sends out a beacon packet every 3 seconds). - Using this method directly allows for adjusting the timeout. Extending - the timeout beyond the default time period could be useful if UDP - packet loss is a concern. + Using this method directly allows for adjusting the timeout. Extending + the timeout beyond the default time period could be useful if UDP + packet loss is a concern. - :param timeout_ms: - Timeout in milliseconds to wait for socketcand beacon packets + :param timeout_ms: + Timeout in milliseconds to wait for socketcand beacon packets - :return: - See :meth:`~can.detect_available_configs` + :return: + See :meth:`~can.detect_available_configs` """ with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: sock.bind( From 3a5b962e4b63a2afb14de7c3748da75d22d4f1ad Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Thu, 4 Jan 2024 10:05:34 -0600 Subject: [PATCH 17/17] Fix grammar, and make time units consistent in comments --- can/interfaces/socketcand/socketcand.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/can/interfaces/socketcand/socketcand.py b/can/interfaces/socketcand/socketcand.py index d5f9fc5cd..84d5b7a7b 100644 --- a/can/interfaces/socketcand/socketcand.py +++ b/can/interfaces/socketcand/socketcand.py @@ -30,9 +30,9 @@ def detect_beacon(timeout_ms: int = 3100) -> List[can.typechecking.AutoDetectedC """ Detects socketcand servers - This is what :meth:`can.detect_available_configs` ends up calling - search for available socketcand servers with fixed timeout of 3.1 - seconds (socketcand sends out a beacon packet every 3 seconds). + This is what :meth:`can.detect_available_configs` ends up calling to search + for available socketcand servers with a default timeout of 3100ms + (socketcand sends a beacon packet every 3000ms). Using this method directly allows for adjusting the timeout. Extending the timeout beyond the default time period could be useful if UDP @@ -341,8 +341,6 @@ def shutdown(self): @staticmethod def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]: try: - # Time between beacons no more than 3 seconds. Allow for a little - # more than 3s return detect_beacon() except Exception as e: log.warning(f"Could not detect socketcand beacon: {e}")