Skip to content

Commit 66540e3

Browse files
Merge socketcan_native and socketcan_ctypes into one (#326)
Also support broadcast channel
1 parent 614bb04 commit 66540e3

15 files changed

+321
-943
lines changed

can/interface.py

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from .util import load_config
2121
from .interfaces import BACKENDS
2222

23+
from can.interfaces.socketcan.socketcan import CyclicSendTask, MultiRateCyclicSendTask
24+
2325
# Required by "detect_available_configs" for argument interpretation
2426
if sys.version_info.major > 2:
2527
basestring = str
@@ -37,14 +39,6 @@ def _get_class_for_interface(interface):
3739
ImportError if there was a problem while importing the
3840
interface or the bus class within that
3941
"""
40-
41-
# filter out the socketcan special case
42-
if interface == 'socketcan':
43-
try:
44-
interface = can.util.choose_socketcan_implementation()
45-
except Exception as e:
46-
raise ImportError("Cannot choose socketcan implementation: {}".format(e))
47-
4842
# Find the correct backend
4943
try:
5044
module_name, class_name = BACKENDS[interface]
@@ -175,43 +169,3 @@ def detect_available_configs(interfaces=None):
175169
result += available
176170

177171
return result
178-
179-
180-
class CyclicSendTask(CyclicSendTaskABC):
181-
182-
@staticmethod
183-
def __new__(cls, channel, *args, **kwargs):
184-
185-
config = load_config(config={'channel': channel})
186-
187-
# Import the correct implementation of CyclicSendTask
188-
if config['interface'] == 'socketcan_ctypes':
189-
from can.interfaces.socketcan.socketcan_ctypes import CyclicSendTask as _ctypesCyclicSendTask
190-
cls = _ctypesCyclicSendTask
191-
elif config['interface'] == 'socketcan_native':
192-
from can.interfaces.socketcan.socketcan_native import CyclicSendTask as _nativeCyclicSendTask
193-
cls = _nativeCyclicSendTask
194-
else:
195-
raise can.CanError("Current CAN interface doesn't support CyclicSendTask")
196-
197-
return cls(config['channel'], *args, **kwargs)
198-
199-
200-
class MultiRateCyclicSendTask(MultiRateCyclicSendTaskABC):
201-
202-
@staticmethod
203-
def __new__(cls, channel, *args, **kwargs):
204-
205-
config = load_config(config={'channel': channel})
206-
207-
# Import the correct implementation of CyclicSendTask
208-
if config['interface'] == 'socketcan_ctypes':
209-
from can.interfaces.socketcan.socketcan_ctypes import MultiRateCyclicSendTask as _ctypesMultiRateCyclicSendTask
210-
cls = _ctypesMultiRateCyclicSendTask
211-
elif config['interface'] == 'socketcan_native':
212-
from can.interfaces.socketcan.socketcan_native import MultiRateCyclicSendTask as _nativeMultiRateCyclicSendTask
213-
cls = _nativeMultiRateCyclicSendTask
214-
else:
215-
can.log.info("Current CAN interface doesn't support CyclicSendTask")
216-
217-
return cls(config['channel'], *args, **kwargs)

can/interfaces/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
# interface_name => (module, classname)
1111
BACKENDS = {
1212
'kvaser': ('can.interfaces.kvaser', 'KvaserBus'),
13-
'socketcan_ctypes': ('can.interfaces.socketcan', 'SocketcanCtypes_Bus'),
14-
'socketcan_native': ('can.interfaces.socketcan', 'SocketcanNative_Bus'),
13+
'socketcan': ('can.interfaces.socketcan', 'SocketcanBus'),
1514
'serial': ('can.interfaces.serial.serial_can','SerialBus'),
1615
'pcan': ('can.interfaces.pcan', 'PcanBus'),
1716
'usb2can': ('can.interfaces.usb2can', 'Usb2canBus'),
@@ -29,4 +28,4 @@
2928
for interface in iter_entry_points('can.interface')
3029
})
3130

32-
VALID_INTERFACES = frozenset(list(BACKENDS.keys()) + ['socketcan'])
31+
VALID_INTERFACES = frozenset(list(BACKENDS.keys()) + ['socketcan_native', 'socketcan_ctypes'])

can/interfaces/socketcan/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@
55
See: https://www.kernel.org/doc/Documentation/networking/can.txt
66
"""
77

8-
from can.interfaces.socketcan import socketcan_constants as constants
9-
from can.interfaces.socketcan.socketcan_ctypes import SocketcanCtypes_Bus
10-
from can.interfaces.socketcan.socketcan_native import SocketcanNative_Bus
8+
from can.interfaces.socketcan.socketcan import SocketcanBus, CyclicSendTask, MultiRateCyclicSendTask

can/interfaces/socketcan/socketcan_constants.py renamed to can/interfaces/socketcan/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
SOCK_DGRAM = 2
5454
AF_CAN = PF_CAN
5555

56+
SIOCGIFNAME = 0x8910
5657
SIOCGIFINDEX = 0x8933
5758
SIOCGSTAMP = 0x8906
5859
EXTFLG = 0x0004

0 commit comments

Comments
 (0)