Skip to content

Commit 7414037

Browse files
author
Felix Nieuwenhuizen
committed
use ctypes.errcheck for error checking
1 parent 6afeb8b commit 7414037

File tree

2 files changed

+64
-116
lines changed

2 files changed

+64
-116
lines changed

can/interfaces/etas/__init__.py

Lines changed: 20 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Dict, List, Optional, Tuple
44

55
import can
6-
from ...exceptions import CanInitializationError, CanOperationError
6+
from ...exceptions import CanInitializationError
77
from .boa import *
88

99

@@ -22,29 +22,17 @@ def __init__(
2222

2323
nodeRange = CSI_NodeRange(CSI_NODE_MIN, CSI_NODE_MAX)
2424
self.tree = ctypes.POINTER(CSI_Tree)()
25-
ec = CSI_CreateProtocolTree(
26-
ctypes.c_char_p(b""), nodeRange, ctypes.byref(self.tree)
27-
)
28-
if ec != 0x0:
29-
raise CanInitializationError(
30-
f"CSI_CreateProtocolTree failed with error 0x{ec:X}"
31-
)
25+
CSI_CreateProtocolTree(ctypes.c_char_p(b""), nodeRange, ctypes.byref(self.tree))
3226

3327
oci_can_v = BOA_Version(1, 4, 0, 0)
3428

35-
# Common
36-
3729
self.ctrl = OCI_ControllerHandle()
38-
ec = OCI_CreateCANControllerNoSearch(
30+
OCI_CreateCANControllerNoSearch(
3931
channel.encode(),
4032
ctypes.byref(oci_can_v),
4133
self.tree,
4234
ctypes.byref(self.ctrl),
4335
)
44-
if ec != 0x0:
45-
raise CanInitializationError(
46-
f"OCI_CreateCANControllerNoSearch failed with error 0x{ec:X}"
47-
)
4836

4937
ctrlConf = OCI_CANConfiguration()
5038
ctrlConf.baudrate = bitrate
@@ -98,13 +86,9 @@ def __init__(
9886
else:
9987
rxQConf.selfReceptionMode = OCI_SELF_RECEPTION_OFF
10088
self.rxQueue = OCI_QueueHandle()
101-
ec = OCI_CreateCANRxQueue(
89+
OCI_CreateCANRxQueue(
10290
self.ctrl, ctypes.byref(rxQConf), ctypes.byref(self.rxQueue)
10391
)
104-
if ec != 0x0:
105-
raise CanInitializationError(
106-
f"OCI_CreateCANRxQueue failed with error 0x{ec:X}"
107-
)
10892

10993
self._oci_filters = None
11094
self.filters = can_filters
@@ -114,32 +98,20 @@ def __init__(
11498
txQConf = OCI_CANTxQueueConfiguration()
11599
txQConf.reserved = 0
116100
self.txQueue = OCI_QueueHandle()
117-
ec = OCI_CreateCANTxQueue(
101+
OCI_CreateCANTxQueue(
118102
self.ctrl, ctypes.byref(txQConf), ctypes.byref(self.txQueue)
119103
)
120-
if ec != 0x0:
121-
raise CanInitializationError(
122-
f"OCI_CreateCANTxQueue failed with error 0x{ec:X}"
123-
)
124104

125105
# Common
126106

127107
timerCapabilities = OCI_TimerCapabilities()
128-
ec = OCI_GetTimerCapabilities(self.ctrl, ctypes.byref(timerCapabilities))
129-
if ec != 0x0:
130-
raise CanInitializationError(
131-
f"OCI_GetTimerCapabilities failed with error 0x{ec:X}"
132-
)
108+
OCI_GetTimerCapabilities(self.ctrl, ctypes.byref(timerCapabilities))
133109
self.tickFrequency = timerCapabilities.tickFrequency # clock ticks per second
134110

135111
# all timestamps are hardware timestamps relative to the CAN device powerup
136112
# calculate an offset to make them relative to epoch
137113
now = OCI_Time()
138-
ec = OCI_GetTimerValue(self.ctrl, ctypes.byref(now))
139-
if ec != 0x0:
140-
raise CanInitializationError(
141-
f"OCI_GetTimerValue failed with error 0x{ec:X}"
142-
)
114+
OCI_GetTimerValue(self.ctrl, ctypes.byref(now))
143115
self.timeOffset = time.time() - (float(now.value) / self.tickFrequency)
144116

145117
self.channel_info = channel
@@ -156,18 +128,14 @@ def _recv_internal(
156128
t = OCI_Time(round(timeout * self.tickFrequency))
157129
else: # wait indefinitely
158130
t = OCI_NO_TIME
159-
ec = OCI_ReadCANDataEx(
131+
OCI_ReadCANDataEx(
160132
self.rxQueue,
161133
t,
162134
ociMsgs,
163135
1,
164136
ctypes.byref(count),
165137
None,
166138
)
167-
if ec != 0x0:
168-
text = ctypes.create_string_buffer(500)
169-
OCI_GetError(self.ctrl, ec, text, 500)
170-
raise CanOperationError(f"OCI_ReadCANDataEx failed with error 0x{ec:X}")
171139

172140
msg = None
173141

@@ -245,17 +213,11 @@ def send(self, msg: can.Message, timeout: Optional[float] = None) -> None:
245213
if msg.bitrate_switch:
246214
ociTxMsg.flags |= OCI_CAN_MSG_FLAG_FD_DATA_BIT_RATE
247215

248-
ec = OCI_WriteCANDataEx(self.txQueue, OCI_NO_TIME, ociMsgs, 1, None)
249-
if ec != 0x0:
250-
raise CanOperationError(f"OCI_WriteCANDataEx failed with error 0x{ec:X}")
216+
OCI_WriteCANDataEx(self.txQueue, OCI_NO_TIME, ociMsgs, 1, None)
251217

252218
def _apply_filters(self, filters: Optional[can.typechecking.CanFilters]) -> None:
253219
if self._oci_filters:
254-
ec = OCI_RemoveCANFrameFilterEx(self.rxQueue, self._oci_filters, 1)
255-
if ec != 0x0:
256-
raise CanOperationError(
257-
f"OCI_RemoveCANFrameFilterEx failed with error 0x{ec:X}"
258-
)
220+
OCI_RemoveCANFrameFilterEx(self.rxQueue, self._oci_filters, 1)
259221

260222
# "accept all" filter
261223
if filters is None:
@@ -280,68 +242,36 @@ def _apply_filters(self, filters: Optional[can.typechecking.CanFilters]) -> None
280242
f.flagsMask |= OCI_CAN_MSG_FLAG_EXTENDED
281243
self._oci_filters[i].contents = f
282244

283-
ec = OCI_AddCANFrameFilterEx(
284-
self.rxQueue, self._oci_filters, len(self._oci_filters)
285-
)
286-
if ec != 0x0:
287-
raise CanOperationError(
288-
f"OCI_AddCANFrameFilterEx failed with error 0x{ec:X}"
289-
)
245+
OCI_AddCANFrameFilterEx(self.rxQueue, self._oci_filters, len(self._oci_filters))
290246

291247
def flush_tx_buffer(self) -> None:
292-
ec = OCI_ResetQueue(self.txQueue)
293-
if ec != 0x0:
294-
raise CanOperationError(f"OCI_ResetQueue failed with error 0x{ec:X}")
248+
OCI_ResetQueue(self.txQueue)
295249

296250
def shutdown(self) -> None:
297251
# Cleanup TX
298252
if self.txQueue:
299-
ec = OCI_DestroyCANTxQueue(self.txQueue)
300-
if ec != 0x0:
301-
raise CanOperationError(
302-
f"OCI_DestroyCANTxQueue failed with error 0x{ec:X}"
303-
)
253+
OCI_DestroyCANTxQueue(self.txQueue)
304254
self.txQueue = None
305255

306256
# Cleanup RX
307257
if self.rxQueue:
308-
ec = OCI_DestroyCANRxQueue(self.rxQueue)
309-
if ec != 0x0:
310-
raise CanOperationError(
311-
f"OCI_DestroyCANRxQueue failed with error 0x{ec:X}"
312-
)
258+
OCI_DestroyCANRxQueue(self.rxQueue)
313259
self.rxQueue = None
314260

315261
# Cleanup common
316262
if self.ctrl:
317-
ec = OCI_CloseCANController(self.ctrl)
318-
if ec != 0x0:
319-
raise CanOperationError(
320-
f"OCI_CloseCANController failed with error 0x{ec:X}"
321-
)
322-
ec = OCI_DestroyCANController(self.ctrl)
323-
if ec != 0x0:
324-
raise CanOperationError(
325-
f"OCI_DestroyCANController failed with error 0x{ec:X}"
326-
)
263+
OCI_CloseCANController(self.ctrl)
264+
OCI_DestroyCANController(self.ctrl)
327265
self.ctrl = None
328266

329267
if self.tree:
330-
ec = CSI_DestroyProtocolTree(self.tree)
331-
if ec != 0x0:
332-
raise CanOperationError(
333-
f"CSI_DestroyProtocolTree failed with error 0x{ec:X}"
334-
)
268+
CSI_DestroyProtocolTree(self.tree)
335269
self.tree = None
336270

337271
@property
338272
def state(self) -> can.BusState:
339273
status = OCI_CANControllerStatus()
340-
ec = OCI_GetCANControllerStatus(self.ctrl, ctypes.byref(status))
341-
if ec != 0x0:
342-
raise CanOperationError(
343-
f"OCI_GetCANControllerStatus failed with error 0x{ec:X}"
344-
)
274+
OCI_GetCANControllerStatus(self.ctrl, ctypes.byref(status))
345275
if status.stateCode & OCI_CAN_STATE_ACTIVE:
346276
return can.BusState.ACTIVE
347277
elif status.stateCode & OCI_CAN_STATE_PASSIVE:
@@ -362,11 +292,7 @@ def state(self, new_state: can.BusState) -> None:
362292
def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]:
363293
nodeRange = CSI_NodeRange(CSI_NODE_MIN, CSI_NODE_MAX)
364294
tree = ctypes.POINTER(CSI_Tree)()
365-
ec = CSI_CreateProtocolTree(ctypes.c_char_p(b""), nodeRange, ctypes.byref(tree))
366-
if ec != 0x0:
367-
raise CanOperationError(
368-
f"CSI_CreateProtocolTree failed with error 0x{ec:X}"
369-
)
295+
CSI_CreateProtocolTree(ctypes.c_char_p(b""), nodeRange, ctypes.byref(tree))
370296

371297
nodes: Dict[str, str] = []
372298

@@ -385,10 +311,6 @@ def _findNodes(tree, prefix):
385311

386312
_findNodes(tree, "ETAS:/")
387313

388-
ec = CSI_DestroyProtocolTree(tree)
389-
if ec != 0x0:
390-
raise CanOperationError(
391-
f"CSI_DestroyProtocolTree failed with error 0x{ec:X}"
392-
)
314+
CSI_DestroyProtocolTree(tree)
393315

394316
return nodes

0 commit comments

Comments
 (0)