@@ -349,7 +349,9 @@ def __init__(
349
349
self .task_id = task_id
350
350
self ._tx_setup (self .messages )
351
351
352
- def _tx_setup (self , messages : Sequence [Message ]) -> None :
352
+ def _tx_setup (
353
+ self , messages : Sequence [Message ], raise_if_task_exists : bool = True
354
+ ) -> None :
353
355
# Create a low level packed frame to pass to the kernel
354
356
body = bytearray ()
355
357
self .flags = CAN_FD_FRAME if messages [0 ].is_fd else 0
@@ -363,7 +365,8 @@ def _tx_setup(self, messages: Sequence[Message]) -> None:
363
365
ival1 = 0.0
364
366
ival2 = self .period
365
367
366
- self ._check_bcm_task ()
368
+ if raise_if_task_exists :
369
+ self ._check_bcm_task ()
367
370
368
371
header = build_bcm_transmit_header (
369
372
self .task_id , count , ival1 , ival2 , self .flags , nframes = len (messages )
@@ -375,7 +378,7 @@ def _tx_setup(self, messages: Sequence[Message]) -> None:
375
378
376
379
def _check_bcm_task (self ) -> None :
377
380
# Do a TX_READ on a task ID, and check if we get EINVAL. If so,
378
- # then we are referring to a CAN message with the existing ID
381
+ # then we are referring to a CAN message with an existing ID
379
382
check_header = build_bcm_header (
380
383
opcode = CAN_BCM_TX_READ ,
381
384
flags = 0 ,
@@ -387,12 +390,19 @@ def _check_bcm_task(self) -> None:
387
390
can_id = self .task_id ,
388
391
nframes = 0 ,
389
392
)
393
+ log .debug (
394
+ f"Reading properties of (cyclic) transmission task id={ self .task_id } " ,
395
+ )
390
396
try :
391
397
self .bcm_socket .send (check_header )
392
398
except OSError as error :
393
399
if error .errno != errno .EINVAL :
394
400
raise can .CanOperationError ("failed to check" , error .errno ) from error
401
+ else :
402
+ log .debug ("Invalid argument - transmission task not known to kernel" )
395
403
else :
404
+ # No exception raised - transmission task with this ID exists in kernel.
405
+ # Existence of an existing transmission task might not be a problem!
396
406
raise can .CanOperationError (
397
407
f"A periodic task for task ID { self .task_id } is already in progress "
398
408
"by the SocketCAN Linux layer"
@@ -438,15 +448,15 @@ def modify_data(self, messages: Union[Sequence[Message], Message]) -> None:
438
448
send_bcm (self .bcm_socket , header + body )
439
449
440
450
def start (self ) -> None :
441
- """Start a periodic task by sending TX_SETUP message to Linux kernel.
451
+ """Restart a periodic task by sending TX_SETUP message to Linux kernel.
442
452
443
453
It verifies presence of the particular BCM task through sending TX_READ
444
454
message to Linux kernel prior to scheduling.
445
455
446
456
:raises ValueError:
447
457
If the task referenced by ``task_id`` is already running.
448
458
"""
449
- self ._tx_setup (self .messages )
459
+ self ._tx_setup (self .messages , raise_if_task_exists = False )
450
460
451
461
452
462
class MultiRateCyclicSendTask (CyclicSendTask ):
0 commit comments