3
3
from typing import Dict , List , Optional , Tuple
4
4
5
5
import can
6
- from ...exceptions import CanInitializationError , CanOperationError
6
+ from ...exceptions import CanInitializationError
7
7
from .boa import *
8
8
9
9
@@ -22,29 +22,17 @@ def __init__(
22
22
23
23
nodeRange = CSI_NodeRange (CSI_NODE_MIN , CSI_NODE_MAX )
24
24
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 ))
32
26
33
27
oci_can_v = BOA_Version (1 , 4 , 0 , 0 )
34
28
35
- # Common
36
-
37
29
self .ctrl = OCI_ControllerHandle ()
38
- ec = OCI_CreateCANControllerNoSearch (
30
+ OCI_CreateCANControllerNoSearch (
39
31
channel .encode (),
40
32
ctypes .byref (oci_can_v ),
41
33
self .tree ,
42
34
ctypes .byref (self .ctrl ),
43
35
)
44
- if ec != 0x0 :
45
- raise CanInitializationError (
46
- f"OCI_CreateCANControllerNoSearch failed with error 0x{ ec :X} "
47
- )
48
36
49
37
ctrlConf = OCI_CANConfiguration ()
50
38
ctrlConf .baudrate = bitrate
@@ -98,13 +86,9 @@ def __init__(
98
86
else :
99
87
rxQConf .selfReceptionMode = OCI_SELF_RECEPTION_OFF
100
88
self .rxQueue = OCI_QueueHandle ()
101
- ec = OCI_CreateCANRxQueue (
89
+ OCI_CreateCANRxQueue (
102
90
self .ctrl , ctypes .byref (rxQConf ), ctypes .byref (self .rxQueue )
103
91
)
104
- if ec != 0x0 :
105
- raise CanInitializationError (
106
- f"OCI_CreateCANRxQueue failed with error 0x{ ec :X} "
107
- )
108
92
109
93
self ._oci_filters = None
110
94
self .filters = can_filters
@@ -114,32 +98,20 @@ def __init__(
114
98
txQConf = OCI_CANTxQueueConfiguration ()
115
99
txQConf .reserved = 0
116
100
self .txQueue = OCI_QueueHandle ()
117
- ec = OCI_CreateCANTxQueue (
101
+ OCI_CreateCANTxQueue (
118
102
self .ctrl , ctypes .byref (txQConf ), ctypes .byref (self .txQueue )
119
103
)
120
- if ec != 0x0 :
121
- raise CanInitializationError (
122
- f"OCI_CreateCANTxQueue failed with error 0x{ ec :X} "
123
- )
124
104
125
105
# Common
126
106
127
107
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 ))
133
109
self .tickFrequency = timerCapabilities .tickFrequency # clock ticks per second
134
110
135
111
# all timestamps are hardware timestamps relative to the CAN device powerup
136
112
# calculate an offset to make them relative to epoch
137
113
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 ))
143
115
self .timeOffset = time .time () - (float (now .value ) / self .tickFrequency )
144
116
145
117
self .channel_info = channel
@@ -156,18 +128,14 @@ def _recv_internal(
156
128
t = OCI_Time (round (timeout * self .tickFrequency ))
157
129
else : # wait indefinitely
158
130
t = OCI_NO_TIME
159
- ec = OCI_ReadCANDataEx (
131
+ OCI_ReadCANDataEx (
160
132
self .rxQueue ,
161
133
t ,
162
134
ociMsgs ,
163
135
1 ,
164
136
ctypes .byref (count ),
165
137
None ,
166
138
)
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} " )
171
139
172
140
msg = None
173
141
@@ -245,17 +213,11 @@ def send(self, msg: can.Message, timeout: Optional[float] = None) -> None:
245
213
if msg .bitrate_switch :
246
214
ociTxMsg .flags |= OCI_CAN_MSG_FLAG_FD_DATA_BIT_RATE
247
215
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 )
251
217
252
218
def _apply_filters (self , filters : Optional [can .typechecking .CanFilters ]) -> None :
253
219
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 )
259
221
260
222
# "accept all" filter
261
223
if filters is None :
@@ -280,68 +242,36 @@ def _apply_filters(self, filters: Optional[can.typechecking.CanFilters]) -> None
280
242
f .flagsMask |= OCI_CAN_MSG_FLAG_EXTENDED
281
243
self ._oci_filters [i ].contents = f
282
244
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 ))
290
246
291
247
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 )
295
249
296
250
def shutdown (self ) -> None :
297
251
# Cleanup TX
298
252
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 )
304
254
self .txQueue = None
305
255
306
256
# Cleanup RX
307
257
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 )
313
259
self .rxQueue = None
314
260
315
261
# Cleanup common
316
262
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 )
327
265
self .ctrl = None
328
266
329
267
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 )
335
269
self .tree = None
336
270
337
271
@property
338
272
def state (self ) -> can .BusState :
339
273
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 ))
345
275
if status .stateCode & OCI_CAN_STATE_ACTIVE :
346
276
return can .BusState .ACTIVE
347
277
elif status .stateCode & OCI_CAN_STATE_PASSIVE :
@@ -362,11 +292,7 @@ def state(self, new_state: can.BusState) -> None:
362
292
def _detect_available_configs () -> List [can .typechecking .AutoDetectedConfig ]:
363
293
nodeRange = CSI_NodeRange (CSI_NODE_MIN , CSI_NODE_MAX )
364
294
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 ))
370
296
371
297
nodes : Dict [str , str ] = []
372
298
@@ -385,10 +311,6 @@ def _findNodes(tree, prefix):
385
311
386
312
_findNodes (tree , "ETAS:/" )
387
313
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 )
393
315
394
316
return nodes
0 commit comments