|
| 1 | +import can |
| 2 | +import importlib |
| 3 | + |
| 4 | +from can.broadcastmanager import CyclicSendTaskABC, MultiRateCyclicSendTaskABC |
| 5 | +from can.util import load_config |
| 6 | + |
| 7 | +# interface_name => (module, classname) |
| 8 | +BACKENDS = { |
| 9 | + 'kvaser': ('can.interfaces.kvaser', 'KvaserBus'), |
| 10 | + 'socketcan_ctypes': ('can.interfaces.socketcan', 'SocketcanCtypes_Bus'), |
| 11 | + 'socketcan_native': ('can.interfaces.socketcan', 'SocketcanNative_Bus'), |
| 12 | + 'serial': ('can.interfaces.serial.serial_can', 'SerialBus'), |
| 13 | + 'pcan': ('can.interfaces.pcan', 'PcanBus'), |
| 14 | + 'usb2can': ('can.interfaces.usb2can', 'Usb2canBus'), |
| 15 | + 'ixxat': ('can.interfaces.ixxat', 'IXXATBus'), |
| 16 | + 'nican': ('can.interfaces.nican', 'NicanBus'), |
| 17 | + 'remote': ('can.interfaces.remote', 'RemoteBus'), |
| 18 | + 'virtual': ('can.interfaces.virtual', 'VirtualBus'), |
| 19 | + 'neovi': ('can.interfaces.neovi_api', 'NeoVIBus') |
| 20 | +} |
| 21 | + |
| 22 | +class Bus(object): |
| 23 | + """ |
| 24 | + Instantiates a CAN Bus of the given `bustype`, falls back to reading a |
| 25 | + configuration file from default locations. |
| 26 | + """ |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def __new__(cls, other, channel=None, *args, **kwargs): |
| 30 | + """ |
| 31 | + Takes the same arguments as :class:`can.BusABC` with the addition of: |
| 32 | +
|
| 33 | + :param kwargs: |
| 34 | + Should contain a bustype key with a valid interface name. |
| 35 | +
|
| 36 | + :raises: |
| 37 | + NotImplementedError if the bustype isn't recognized |
| 38 | + :raises: |
| 39 | + ValueError if the bustype or channel isn't either passed as an argument |
| 40 | + or set in the can.rc config. |
| 41 | +
|
| 42 | + """ |
| 43 | + config = load_config(config={ |
| 44 | + 'interface': kwargs.get('bustype'), |
| 45 | + 'channel': channel |
| 46 | + }) |
| 47 | + |
| 48 | + if 'bustype' in kwargs: |
| 49 | + # remove the bustype so it doesn't get passed to the backend |
| 50 | + del kwargs['bustype'] |
| 51 | + interface = config['interface'] |
| 52 | + channel = config['channel'] |
| 53 | + |
| 54 | + # Import the correct Bus backend |
| 55 | + try: |
| 56 | + (module_name, class_name) = BACKENDS[interface] |
| 57 | + except KeyError: |
| 58 | + raise NotImplementedError("CAN interface '{}' not supported".format(interface)) |
| 59 | + |
| 60 | + try: |
| 61 | + module = importlib.import_module(module_name) |
| 62 | + except Exception as e: |
| 63 | + raise ImportError( |
| 64 | + "Cannot import module {} for CAN interface '{}': {}".format(module_name, interface, e) |
| 65 | + ) |
| 66 | + try: |
| 67 | + cls = getattr(module, class_name) |
| 68 | + except Exception as e: |
| 69 | + raise ImportError( |
| 70 | + "Cannot import class {} from module {} for CAN interface '{}': {}".format( |
| 71 | + class_name, module_name, interface, e |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + return cls(channel, **kwargs) |
| 76 | + |
| 77 | + |
| 78 | +class CyclicSendTask(CyclicSendTaskABC): |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def __new__(cls, other, channel, *args, **kwargs): |
| 82 | + |
| 83 | + config = load_config(config={'channel': channel}) |
| 84 | + |
| 85 | + # Import the correct implementation of CyclicSendTask |
| 86 | + if config['interface'] == 'socketcan_ctypes': |
| 87 | + from can.interfaces.socketcan.socketcan_ctypes import CyclicSendTask as _ctypesCyclicSendTask |
| 88 | + cls = _ctypesCyclicSendTask |
| 89 | + elif config['interface'] == 'socketcan_native': |
| 90 | + from can.interfaces.socketcan.socketcan_native import CyclicSendTask as _nativeCyclicSendTask |
| 91 | + cls = _nativeCyclicSendTask |
| 92 | + # CyclicSendTask has not been fully implemented on remote interface yet. |
| 93 | + # Waiting for issue #80 which will change the API to make it easier for |
| 94 | + # interfaces other than socketcan to implement it |
| 95 | + #elif can.rc['interface'] == 'remote': |
| 96 | + # from can.interfaces.remote import CyclicSendTask as _remoteCyclicSendTask |
| 97 | + # cls = _remoteCyclicSendTask |
| 98 | + else: |
| 99 | + raise can.CanError("Current CAN interface doesn't support CyclicSendTask") |
| 100 | + |
| 101 | + return cls(config['channel'], *args, **kwargs) |
| 102 | + |
| 103 | + |
| 104 | +class MultiRateCyclicSendTask(MultiRateCyclicSendTaskABC): |
| 105 | + |
| 106 | + @classmethod |
| 107 | + def __new__(cls, other, channel, *args, **kwargs): |
| 108 | + |
| 109 | + config = load_config(config={'channel': channel}) |
| 110 | + |
| 111 | + # Import the correct implementation of CyclicSendTask |
| 112 | + if config['interface'] == 'socketcan_ctypes': |
| 113 | + from can.interfaces.socketcan.socketcan_ctypes import MultiRateCyclicSendTask as _ctypesMultiRateCyclicSendTask |
| 114 | + cls = _ctypesMultiRateCyclicSendTask |
| 115 | + elif config['interface'] == 'socketcan_native': |
| 116 | + from can.interfaces.socketcan.socketcan_native import MultiRateCyclicSendTask as _nativeMultiRateCyclicSendTask |
| 117 | + cls = _nativeMultiRateCyclicSendTask |
| 118 | + else: |
| 119 | + can.log.info("Current CAN interface doesn't support CyclicSendTask") |
| 120 | + |
| 121 | + return cls(config['channel'], *args, **kwargs) |
0 commit comments