Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _check_write_feedback(self):
try:
item = self._write_feedback_queue.get_nowait()
except queue.Empty:
return
if isinstance(item, Exception):
raise item
if isinstance(item, CANFrame):
self._tx_hook(item)
else:
raise DriverError("Unexpected item in write feedback queue: %r" % item)
def run(self):
while True:
try:
command = self._tx_queue.get(True, self.QUEUE_BLOCK_TIMEOUT)
if isinstance(command, CANFrame):
self._send_frame(command)
elif isinstance(command, IPCCommandLineExecutionRequest):
self._execute_command(command)
elif command == IPC_COMMAND_STOP:
break
else:
raise DriverError('IO process received unknown IPC command: %r' % command)
except queue.Empty:
# Checking in this handler in order to avoid interference with traffic
if self._termination_condition():
break
except Exception as ex:
logger.error('TX thread exception', exc_info=True)
# Propagating the exception to the parent process
# noinspection PyBroadException
try:
self._rx_queue.put_nowait(ex)
except Exception:
pass
def wait_for_ack():
logger.info('Init: Waiting for ACK...')
conn.timeout = ACK_TIMEOUT
while True:
b = conn.read(1)
if not b:
raise DriverError('SLCAN ACK timeout')
if b == NACK:
raise DriverError('SLCAN NACK in response')
if b == ACK:
break
logger.info('Init: Ignoring byte %r while waiting for ACK', b)
self._logging_thread.start()
deadline = time.monotonic() + IO_PROCESS_INIT_TIMEOUT
while True:
try:
sig = self._rx_queue.get(timeout=IO_PROCESS_INIT_TIMEOUT)
if sig == IPC_SIGNAL_INIT_OK:
break
if isinstance(sig, Exception):
self._tx_queue.put(IPC_COMMAND_STOP, timeout=IO_PROCESS_INIT_TIMEOUT)
raise sig
except queue.Empty:
pass
if time.monotonic() > deadline:
self._tx_queue.put(IPC_COMMAND_STOP, timeout=IO_PROCESS_INIT_TIMEOUT)
raise DriverError('IO process did not confirm initialization')
self._check_alive()
if line[0] == b'T'[0]:
id_len = 8
elif line[0] == b't'[0]:
id_len = 3
else:
return
# Parsing ID and DLC
packet_id = int(line[1:1 + id_len], 16)
if self.PY2_COMPAT:
packet_len = int(line[1 + id_len]) # This version is horribly slow
else:
packet_len = line[1 + id_len] - 48 # Py3 version is faster
if packet_len > 8 or packet_len < 0:
raise DriverError('Invalid packet length')
# Parsing the payload, detecting timestamp
# <data> [timestamp]
# 1 3|8 1 packet_len * 2 [4]
with_timestamp = line_len > (2 + id_len + packet_len * 2)
packet_data = binascii.a2b_hex(line[2 + id_len:2 + id_len + packet_len * 2])
# Handling the timestamp, if present
if with_timestamp:
ts_hardware = int(line[-4:], 16) * 1e-3
ts_mono = self._ts_estimator_mono.update(ts_hardware, local_ts_mono)
ts_real = self._ts_estimator_real.update(ts_hardware, local_ts_real)
else:
ts_mono = local_ts_mono
ts_real = local_ts_real</data>
def get_socket(ifname):
socket_fd = libc.socket(AF_CAN, socket.SOCK_RAW, CAN_RAW)
if socket_fd < 0:
raise DriverError('Could not open socket')
libc.fcntl(socket_fd, fcntl.F_SETFL, os.O_NONBLOCK)
ifidx = libc.if_nametoindex(ifname)
if ctypes.get_errno() != 0:
raise DriverError('Could not determine iface index [errno %s]' % ctypes.get_errno())
addr = sockaddr_can(AF_CAN, ifidx)
error = libc.bind(socket_fd, ctypes.byref(addr), ctypes.sizeof(addr))
if error != 0:
raise DriverError('Could not bind socket [errno %s]' % ctypes.get_errno())
return CANSocket(socket_fd)
def wait_for_ack():
logger.info('Init: Waiting for ACK...')
conn.timeout = ACK_TIMEOUT
while True:
b = conn.read(1)
if not b:
raise DriverError('SLCAN ACK timeout')
if b == NACK:
raise DriverError('SLCAN NACK in response')
if b == ACK:
break
logger.info('Init: Ignoring byte %r while waiting for ACK', b)
def _check_alive(self):
if not self._proc.is_alive():
raise DriverError('IO process is dead :(')