2

I'm currently working on some tests for a CAN node, where I'm using python-can and Linux SocketCAN to send and receive CAN frames from the node.

Regular sending and receiving is working fine, but now I want to inject faults and see how the CAN node behaves.

Does anybody know if it's possible to do this, for example by changing the CRC of the frame. I already have one test where I take down the CAN interface, so the node goes bus-off, but there are so much more CAN errors to test.

Edit: To make thinks clear: I'm working on a test framework, using pytest and python-can, and for regular sending of CAN-frames I have the following code:

import can
bus = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=250000)
msg = can.Message(arbitration_id=can_id,
                  data=data,
                  is_extended_id=False)
bus.send(msg)

And here it stops for me, What I can read for the API, there is no option for fault injections here ( Pyhon-can API ).

A another example of what I have today:

import can
bus = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=250000)
msg = can.Message(arbitration_id=can_id,
                  data=data,
                  is_error_frame=True)
bus.send(msg)

Above codes generates error frames on the bus, that is part of the fault-handling tests I'm designing.

EmbeddedNoob
  • 25
  • 1
  • 6
  • 1
    welcome to SO. Fault injection works on soft and hard interface as well. I suppose you'd like to inject software fault, by the looks of it. Just try to edit the content of your message, for instance have the CRC as a fixed value. It will be wrong. Please, post some of your code it wil make the discussion easier. – Daemon Painter Nov 20 '20 at 08:43
  • Hi and thanks! Well currently I don't have any code, I'm in the investigation and design phase of the test case, (In other words, googling alot :) ). What I can read from the Python bus.send API, there is no way to change the CRC here. I think that the CRC is calculated in the lower layers(Maybe in the HW driver?)... – EmbeddedNoob Nov 20 '20 at 10:02
  • Then I encourage you to try your best first, then come back here with the problems that stop you. You appear to have a little bit of code, though, that's a good place to start. Somewhere you must be setting the content of your CAN message... – Daemon Painter Nov 20 '20 at 12:58
  • Question updated with examples of what I have today! – EmbeddedNoob Nov 23 '20 at 06:52

2 Answers2

0

so, I'm not familiar with the library, however this appears to be the only place in the library where they expose the word CRC.

I fear there might not be a native way to do it, but you may extend the classes of the library and override the CRC field yourself, maybe.

The implementation of can.message doesn't let you touch the CRC. However, this is a "meh" news, in the sense that CRC is not computed when you first create the can.message() object.

A quick search in the socketcan.send() method has no mention of CRC either, so it may be computed somewhere in between. Or, and I fear this one more, it is computed by an external library libc which is referred in the can.interfaces.socketcan class again:

try:
    libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True)
    libc.bind.errcheck = check_status
    libc.connect.errcheck = check_status
    libc.sendto.errcheck = check_status
    libc.recvfrom.errcheck = check_status
except:
    log.warning("libc is unavailable")
    libc = None

and if this is the case, you'll have a very hard time trying to tackle the CRC. I do sincerely hope this is pointing you in the right direction, I can't really do more with my very limited knowledge of this particular library.

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44
0

I have investigated this some more now, and also checked with the HW supplier of my CAN dongle. It seems that what I want to do is not possible from SW side, and I need to purchase a another HW CAN dongle that can support this kind of error injection.

The HW supplier also told me that these kinds of testings is normally done by the CAN controller suppliers, so this is not (according to him) not a normal and easy testcase.

So I'm leaving this topic now, and continues with other testing instead.

EmbeddedNoob
  • 25
  • 1
  • 6