Index: .gitignore =================================================================== diff -u -rd66e924d16e5b5f62570f57a483acd3f10c50dcc -rd34628e03e249d9dfcf8680c5344d6f33e82080b --- .gitignore (.../.gitignore) (revision d66e924d16e5b5f62570f57a483acd3f10c50dcc) +++ .gitignore (.../.gitignore) (revision d34628e03e249d9dfcf8680c5344d6f33e82080b) @@ -22,4 +22,5 @@ docs/source/dialin.utils.rst docs/source/dialin.squish.rst docs/source/modules.rst +docs/source/dialin.common.rst Index: dialin/protocols/CAN.py =================================================================== diff -u -r8d1f61499650e23dac6f857e48daad42180db949 -rd34628e03e249d9dfcf8680c5344d6f33e82080b --- dialin/protocols/CAN.py (.../CAN.py) (revision 8d1f61499650e23dac6f857e48daad42180db949) +++ dialin/protocols/CAN.py (.../CAN.py) (revision d34628e03e249d9dfcf8680c5344d6f33e82080b) @@ -597,3 +597,4 @@ with open(filename, 'a') as f: f.write("{0}\n".format(packet)) + Index: dialin/ui/hd_proxy.py =================================================================== diff -u -rc3cc81dada72714b9675594cc0c6a79975e8991d -rd34628e03e249d9dfcf8680c5344d6f33e82080b --- dialin/ui/hd_proxy.py (.../hd_proxy.py) (revision c3cc81dada72714b9675594cc0c6a79975e8991d) +++ dialin/ui/hd_proxy.py (.../hd_proxy.py) (revision d34628e03e249d9dfcf8680c5344d6f33e82080b) @@ -27,8 +27,6 @@ class HDSimulator(_AbstractSubSystem): - MSG_ID_HD_CREATE_TREATMENT_RESPONSE = 0x0036 - NUM_TREATMENT_PARAMETERS = 18 def __init__(self, can_interface="can0", log_level=None): @@ -63,7 +61,7 @@ payload += integer_to_bytearray(rejection.value) message = DenaliMessage.build_message(channel_id=DenaliChannels.hd_to_ui_ch_id, - message_id=MsgIds.MSG_ID_HD_NEW_TREATMENT_PARAMS_RESPONSE, + message_id=MsgIds.MSG_ID_HD_NEW_TREATMENT_PARAMS_RESPONSE.value, payload=payload) # Send message Index: dialin/utils/base.py =================================================================== diff -u -r8d1f61499650e23dac6f857e48daad42180db949 -rd34628e03e249d9dfcf8680c5344d6f33e82080b --- dialin/utils/base.py (.../base.py) (revision 8d1f61499650e23dac6f857e48daad42180db949) +++ dialin/utils/base.py (.../base.py) (revision d34628e03e249d9dfcf8680c5344d6f33e82080b) @@ -17,8 +17,164 @@ from datetime import datetime import os import logging +import subprocess +def setup_virtual_can_interface(): + """ + Convenience function to setup a virtual can interface using the most common settings + See the setup_can function to setup a can interface with custom settings. + @return: True if successful, false otherwise + """ + bring_interface_down(interface="can0", delete=True) + is_present = is_interface_present("can0") + if not is_present: + setup_can(interface="can0", + is_virtual=True) + result = is_interface_up("can0") + if result: + print("Success") + return True + + print("Failure") + return False + + +def setup_real_can_interface(): + """ + Convenience function to setup a real can interface using the most common settings + See the setup_can function to setup a can interface with custom settings. + + @return: True if successful, False otherwise + """ + delete_virtual = input("Delete virtual can interface? (y,n)") + if delete_virtual.upper() in ["Y", "YES"]: + bring_interface_down(interface="can0", delete=True) + if is_interface_present("can0"): + print("Failure") + return False + else: + bring_interface_down(interface="can0", delete=False) + if not is_interface_present("can0") or is_interface_up("can0"): + print("Failure") + return False + input("Press 'Enter' after plugging in the PCAN-USB.") + setup_can(interface="can0", + is_virtual=False, + bitrate=250000, + restart_ms=100, + txqueue=10000) + + if is_interface_present("can0") and is_interface_up("can0"): + print("Success") + return True + else: + print("Failure") + return False + +def bring_interface_down(interface: str = "can0", delete=False): + """ + Brings the specified can interface down + + @param interface: (str) the interface name + @param delete: (bool) whether to delete the interface after bringing it down + @return: None + """ + cmd_iface_down = "sudo ifconfig {0} down > /dev/null 2>&1".format(interface) + cmd_iface_delete = "sudo ip link delete dev {0} > /dev/null 2>&1".format(interface) + + success, info = run_cmd(cmd_iface_down) + if not success: + print("Warning: Could not bring interface down: {0}".format(info)) + if delete: + run_cmd(cmd_iface_delete) + if not success: + print("Warning: Could not delete the interface: {0}".format(info)) + + +def run_cmd(cmd: str): + """ + Runs the provided command, returns if it was successful and output or error information + + @param cmd: (str) the command to run + @return: tuple(bool, info) Whether the command was successful. If true info is the output, otherwise + info contains the error information + """ + try: + result = subprocess \ + .check_output(cmd, + shell=True).decode("utf-8").strip() + return True, result + except subprocess.CalledProcessError as e: + return False, str(e) + + +def is_interface_up(interface: str = "can0"): + """ + Checks if the specified interface is listed and up + + @param interface: + @return: (bool) True if up, False if not or if we can't tell + """ + cmd_check_interface = "ifconfig {0}".format(interface) + + success, info = run_cmd(cmd_check_interface) + if success: + if interface in info and "UP,RUNNING" in info: + return True + return False + + +def is_interface_present(interface: str = "can0"): + """ + Checks if the specified interface is listed + + @param interface: + @return: (bool) True if present, False if not or if we can't tell + """ + cmd_check_interface = "ip a" + + success, info = run_cmd(cmd_check_interface) + if success: + if interface in info: + return True + return False + +def setup_can(interface: str = "can0", is_virtual=False, bitrate=250000, restart_ms=100, txqueue=10000): + """ + Convenience function to setup a can interface + + @param interface: (str) The interface name + @param is_virtual: (bool) If the interface is virtual or not + @param bitrate: (int) The desired bitrate (applies to non-virtual interfaces only) + @param restart_ms: (int) The desired restart_ms (applies to non-virtual interfaces only) + @param txqueue: (int) The desired txqueue length + @return: + """ + cmd_iface_up = "sudo ip link set {0} up type can bitrate {1} restart-ms {2}"\ + .format(interface, bitrate, restart_ms) + cmd_txqueue = "sudo ifconfig {0} txqueuelen {1}".format(interface, txqueue) + + cmd_iface_type_virtual = "sudo ip link add dev {0} type vcan".format(interface) + cmd_iface_up_virtual = "sudo ip link set {0} up".format(interface) + + if is_virtual: + success, info = run_cmd(cmd_iface_type_virtual) + if not success: + print("Warning: Could not set iface type to virtual: {0}".format(info)) + success, info = run_cmd(cmd_iface_up_virtual) + if not success: + print("Warning: Could not bring up virtual interface: {0}".format(info)) + else: + success, info = run_cmd(cmd_iface_up) + if not success: + print("Warning: Could not bring interface up: {0}".format(info)) + + success, info = run_cmd(cmd_txqueue) + if not success: + print("Warning: Could not set txtqueue length: {0}".format(cmd_txqueue)) + + def create_logger(log_path: str = "/tmp/DialinScript.log", level: str = "ERROR"): """ Convenience function to create a logger for external Dialin scripts Index: tests/test_can.py =================================================================== diff -u --- tests/test_can.py (revision 0) +++ tests/test_can.py (revision d34628e03e249d9dfcf8680c5344d6f33e82080b) @@ -0,0 +1,22 @@ +########################################################################### +# +# Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. +# +# THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN +# WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. +# +# @file test_can.py +# +# @author (last) Peter Lucia +# @date (last) 20-Jul-2020 +# @author (original) Peter Lucia +# @date (original) 15-May-2020 +# +############################################################################ +import sys +sys.path.append("..") +from dialin.utils.base import setup_virtual_can_interface, setup_real_can_interface + + +setup_virtual_can_interface() +