Skip to content

Commit 0a2aa84

Browse files
author
Felix Nieuwenhuizen
committed
incorporate review comments by felixdivo
1 parent 23046fa commit 0a2aa84

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

can/interfaces/etas/__init__.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import ctypes
12
import time
2-
from typing import Any, List, Optional, Tuple
3+
from typing import Dict, List, Optional, Tuple
34

45
import can
6+
from ...exceptions import CanInitializationError, CanOperationError
57
from .boa import *
68

79

810
class EtasBus(can.BusABC):
911
def __init__(
1012
self,
11-
channel: Any,
13+
channel: str,
1214
can_filters: Optional[can.typechecking.CanFilters] = None,
1315
receive_own_messages: bool = False,
1416
bitrate: int = 1000000,
@@ -24,7 +26,7 @@ def __init__(
2426
ctypes.c_char_p(b""), nodeRange, ctypes.byref(self.tree)
2527
)
2628
if ec != 0x0:
27-
raise can.exceptions.CanInitializationError(
29+
raise CanInitializationError(
2830
f"CSI_CreateProtocolTree failed with error 0x{ec:X}"
2931
)
3032

@@ -40,7 +42,7 @@ def __init__(
4042
ctypes.byref(self.ctrl),
4143
)
4244
if ec != 0x0:
43-
raise can.exceptions.CanInitializationError(
45+
raise CanInitializationError(
4446
f"OCI_CreateCANControllerNoSearch failed with error 0x{ec:X}"
4547
)
4648

@@ -80,7 +82,7 @@ def __init__(
8082
self.ctrl, ctypes.byref(ctrlConf), ctypes.byref(ctrlProp)
8183
)
8284
if ec != 0x0 and ec != 0x40004000: # accept BOA_WARN_PARAM_ADAPTED
83-
raise can.exceptions.CanInitializationError(
85+
raise CanInitializationError(
8486
f"OCI_OpenCANController failed with error 0x{ec:X}"
8587
)
8688

@@ -100,7 +102,7 @@ def __init__(
100102
self.ctrl, ctypes.byref(rxQConf), ctypes.byref(self.rxQueue)
101103
)
102104
if ec != 0x0:
103-
raise can.exceptions.CanInitializationError(
105+
raise CanInitializationError(
104106
f"OCI_CreateCANRxQueue failed with error 0x{ec:X}"
105107
)
106108

@@ -116,7 +118,7 @@ def __init__(
116118
self.ctrl, ctypes.byref(txQConf), ctypes.byref(self.txQueue)
117119
)
118120
if ec != 0x0:
119-
raise can.exceptions.CanInitializationError(
121+
raise CanInitializationError(
120122
f"OCI_CreateCANTxQueue failed with error 0x{ec:X}"
121123
)
122124

@@ -125,17 +127,17 @@ def __init__(
125127
timerCapabilities = OCI_TimerCapabilities()
126128
ec = OCI_GetTimerCapabilities(self.ctrl, ctypes.byref(timerCapabilities))
127129
if ec != 0x0:
128-
raise can.exceptions.CanInitializationError(
130+
raise CanInitializationError(
129131
f"OCI_GetTimerCapabilities failed with error 0x{ec:X}"
130132
)
131133
self.tickFrequency = timerCapabilities.tickFrequency # clock ticks per second
132134

133-
# all timestamps are hardware timestamps relative to powerup
135+
# all timestamps are hardware timestamps relative to the CAN device powerup
134136
# calculate an offset to make them relative to epoch
135137
now = OCI_Time()
136138
ec = OCI_GetTimerValue(self.ctrl, ctypes.byref(now))
137139
if ec != 0x0:
138-
raise can.exceptions.CanInitializationError(
140+
raise CanInitializationError(
139141
f"OCI_GetTimerValue failed with error 0x{ec:X}"
140142
)
141143
self.timeOffset = time.time() - (float(now.value) / self.tickFrequency)
@@ -153,7 +155,7 @@ def _recv_internal(
153155
count = ctypes.c_uint32()
154156
remaining = ctypes.c_uint32()
155157
if timeout is not None:
156-
t = OCI_Time(int(timeout * self.tickFrequency))
158+
t = OCI_Time(round(timeout * self.tickFrequency))
157159
else:
158160
t = OCI_NO_TIME
159161
ec = OCI_ReadCANDataEx(
@@ -167,9 +169,7 @@ def _recv_internal(
167169
if ec != 0x0:
168170
text = ctypes.create_string_buffer(500)
169171
OCI_GetError(self.ctrl, ec, text, 500)
170-
raise can.exceptions.CanOperationError(
171-
f"OCI_ReadCANDataEx failed with error 0x{ec:X}"
172-
)
172+
raise CanOperationError(f"OCI_ReadCANDataEx failed with error 0x{ec:X}")
173173

174174
msg = None
175175

@@ -260,15 +260,13 @@ def send(self, msg: can.Message, timeout: Optional[float] = None) -> None:
260260

261261
ec = OCI_WriteCANDataEx(self.txQueue, OCI_NO_TIME, canMessages, 1, None)
262262
if ec != 0x0:
263-
raise can.exceptions.CanOperationError(
264-
f"OCI_WriteCANDataEx failed with error 0x{ec:X}"
265-
)
263+
raise CanOperationError(f"OCI_WriteCANDataEx failed with error 0x{ec:X}")
266264

267265
def _apply_filters(self, filters: Optional[can.typechecking.CanFilters]) -> None:
268266
if self._oci_filters:
269267
ec = OCI_RemoveCANFrameFilterEx(self.rxQueue, self._oci_filters, 1)
270268
if ec != 0x0:
271-
raise can.exceptions.CanOperationError(
269+
raise CanOperationError(
272270
f"OCI_RemoveCANFrameFilterEx failed with error 0x{ec:X}"
273271
)
274272

@@ -277,6 +275,7 @@ def _apply_filters(self, filters: Optional[can.typechecking.CanFilters]) -> None
277275
filters = [{"can_id": 0x0, "can_mask": 0x0}]
278276

279277
self._oci_filters = (ctypes.POINTER(OCI_CANRxFilterEx) * len(filters))()
278+
280279
for i, filter in enumerate(filters):
281280
f = OCI_CANRxFilterEx()
282281
f.frameIDValue = filter["can_id"]
@@ -298,23 +297,21 @@ def _apply_filters(self, filters: Optional[can.typechecking.CanFilters]) -> None
298297
self.rxQueue, self._oci_filters, len(self._oci_filters)
299298
)
300299
if ec != 0x0:
301-
raise can.exceptions.CanOperationError(
300+
raise CanOperationError(
302301
f"OCI_AddCANFrameFilterEx failed with error 0x{ec:X}"
303302
)
304303

305304
def flush_tx_buffer(self) -> None:
306305
ec = OCI_ResetQueue(self.txQueue)
307306
if ec != 0x0:
308-
raise can.exceptions.CanOperationError(
309-
f"OCI_ResetQueue failed with error 0x{ec:X}"
310-
)
307+
raise CanOperationError(f"OCI_ResetQueue failed with error 0x{ec:X}")
311308

312309
def shutdown(self) -> None:
313310
# Cleanup TX
314311
if self.txQueue:
315312
ec = OCI_DestroyCANTxQueue(self.txQueue)
316313
if ec != 0x0:
317-
raise can.exceptions.CanOperationError(
314+
raise CanOperationError(
318315
f"OCI_DestroyCANTxQueue failed with error 0x{ec:X}"
319316
)
320317
self.txQueue = None
@@ -323,7 +320,7 @@ def shutdown(self) -> None:
323320
if self.rxQueue:
324321
ec = OCI_DestroyCANRxQueue(self.rxQueue)
325322
if ec != 0x0:
326-
raise can.exceptions.CanOperationError(
323+
raise CanOperationError(
327324
f"OCI_DestroyCANRxQueue failed with error 0x{ec:X}"
328325
)
329326
self.rxQueue = None
@@ -332,20 +329,20 @@ def shutdown(self) -> None:
332329
if self.ctrl:
333330
ec = OCI_CloseCANController(self.ctrl)
334331
if ec != 0x0:
335-
raise can.exceptions.CanOperationError(
332+
raise CanOperationError(
336333
f"OCI_CloseCANController failed with error 0x{ec:X}"
337334
)
338335
ec = OCI_DestroyCANController(self.ctrl)
339336
if ec != 0x0:
340-
raise can.exceptions.CanOperationError(
337+
raise CanOperationError(
341338
f"OCI_DestroyCANController failed with error 0x{ec:X}"
342339
)
343340
self.ctrl = None
344341

345342
if self.tree:
346343
ec = CSI_DestroyProtocolTree(self.tree)
347344
if ec != 0x0:
348-
raise can.exceptions.CanOperationError(
345+
raise CanOperationError(
349346
f"CSI_DestroyProtocolTree failed with error 0x{ec:X}"
350347
)
351348
self.tree = None
@@ -355,7 +352,7 @@ def state(self) -> can.BusState:
355352
status = OCI_CANControllerStatus()
356353
ec = OCI_GetCANControllerStatus(self.ctrl, ctypes.byref(status))
357354
if ec != 0x0:
358-
raise can.exceptions.CanOperationError(
355+
raise CanOperationError(
359356
f"OCI_GetCANControllerStatus failed with error 0x{ec:X}"
360357
)
361358
if status.stateCode & OCI_CAN_STATE_ACTIVE:
@@ -365,25 +362,26 @@ def state(self) -> can.BusState:
365362

366363
@state.setter
367364
def state(self, new_state: can.BusState) -> None:
365+
# disabled, OCI_AdaptCANConfiguration does not allow changing the bus mode
368366
# if new_state == can.BusState.ACTIVE:
369367
# self.ctrlConf.busParticipationMode = OCI_BUSMODE_ACTIVE
370368
# else:
371369
# self.ctrlConf.busParticipationMode = OCI_BUSMODE_PASSIVE
372370
# ec = OCI_AdaptCANConfiguration(self.ctrl, ctypes.byref(self.ctrlConf))
373371
# if ec != 0x0:
374-
# raise can.exceptions.CanOperationError(f"OCI_AdaptCANConfiguration failed with error 0x{ec:X}")
372+
# raise CanOperationError(f"OCI_AdaptCANConfiguration failed with error 0x{ec:X}")
375373
raise NotImplementedError("Setting state is not implemented.")
376374

377375
def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]:
378376
nodeRange = CSI_NodeRange(CSI_NODE_MIN, CSI_NODE_MAX)
379377
tree = ctypes.POINTER(CSI_Tree)()
380378
ec = CSI_CreateProtocolTree(ctypes.c_char_p(b""), nodeRange, ctypes.byref(tree))
381379
if ec != 0x0:
382-
raise can.exceptions.CanOperationError(
380+
raise CanOperationError(
383381
f"CSI_CreateProtocolTree failed with error 0x{ec:X}"
384382
)
385383

386-
nodes = []
384+
nodes: Dict[str, str] = []
387385

388386
def _findNodes(tree, prefix):
389387
uri = f"{prefix}/{tree.contents.item.uriName.decode()}"
@@ -402,7 +400,7 @@ def _findNodes(tree, prefix):
402400

403401
ec = CSI_DestroyProtocolTree(tree)
404402
if ec != 0x0:
405-
raise can.exceptions.CanOperationError(
403+
raise CanOperationError(
406404
f"CSI_DestroyProtocolTree failed with error 0x{ec:X}"
407405
)
408406

0 commit comments

Comments
 (0)