Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions can/interfaces/ics_neovi/neovi_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"""

import logging
import os
import tempfile
from collections import deque

from can import Message, CanError, BusABC
Expand All @@ -27,6 +29,35 @@
ics = None


try:
from filelock import FileLock
except ImportError as ie:

logger.warning(
"Using ICS NeoVi can backend without the "
"filelock module installed may cause some issues!: %s",
ie,
)

class FileLock:
"""Dummy file lock that does not actually do anything"""

def __init__(self, lock_file, timeout=-1):
self._lock_file = lock_file
self.timeout = timeout

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
return None


# Use inter-process mutex to prevent concurrent device open.
# When neoVI server is enabled, there is an issue with concurrent device open.
open_lock = FileLock(os.path.join(tempfile.gettempdir(), "neovi.lock"))


class ICSApiError(CanError):
"""
Indicates an error with the ICS API.
Expand Down Expand Up @@ -118,18 +149,28 @@ def __init__(self, channel, can_filters=None, **kwargs):
type_filter = kwargs.get('type_filter')
serial = kwargs.get('serial')
self.dev = self._find_device(type_filter, serial)
ics.open_device(self.dev)

if 'bitrate' in kwargs:
for channel in self.channels:
ics.set_bit_rate(self.dev, kwargs.get('bitrate'), channel)
with open_lock:
ics.open_device(self.dev)

fd = kwargs.get('fd', False)
if fd:
if 'data_bitrate' in kwargs:
try:
if "bitrate" in kwargs:
for channel in self.channels:
ics.set_fd_bit_rate(
self.dev, kwargs.get('data_bitrate'), channel)
ics.set_bit_rate(self.dev, kwargs.get("bitrate"), channel)

if kwargs.get("fd", False):
if "data_bitrate" in kwargs:
for channel in self.channels:
ics.set_fd_bit_rate(
self.dev, kwargs.get("data_bitrate"), channel
)
except ics.RuntimeError as re:
logger.error(re)
err = ICSApiError(*ics.get_last_api_error(self.dev))
try:
self.shutdown()
finally:
raise err

self._use_system_timestamp = bool(
kwargs.get('use_system_timestamp', False)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
'aenum',
'typing;python_version<"3.5"',
'windows-curses;platform_system=="Windows"',
'filelock'
],
setup_requires=pytest_runner,
extras_require=extras_require,
Expand Down