Add SAE J1939 vehicle network protocol support#30
Conversation
…939Socket Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/697f7ddf-27ac-4f96-aa9c-11573e8b46c9 Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/697f7ddf-27ac-4f96-aa9c-11573e8b46c9 Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/fb7d3bc1-c522-4aa2-9a36-fa29330158aa Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
… J1939_PDU1_MAX_PF constant Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/56d18773-0a7e-4250-8257-45470e858a03 Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
| # Custom field: little-endian 3-byte PGN storage (J1939 wire format) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| class J1939LEPGNField(Field): # type: ignore[type-arg] |
There was a problem hiding this comment.
Can the ThreeByteField be used?
There was a problem hiding this comment.
Replaced J1939LEPGNField with the built-in XLE3BytesField (commit 0669a54). The custom class was a duplicate of XLE3BytesField — both encode a 3-byte little-endian value and display it in hex — so the entire class and the Field import are now removed.
Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/55b27afa-f1dc-4ba9-8582-1b9547033bd5 Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
|
|
||
| # Socket-level constants | ||
| SOL_CAN_BASE = 100 | ||
| SOL_CAN_J1939 = SOL_CAN_BASE + socket.CAN_J1939 # 107 |
There was a problem hiding this comment.
in python 3.7 this is unknown.
check at which python version this socket.CAN_J1939 is defined and provide it for older versions.
There was a problem hiding this comment.
Fixed in commit 3c1a7c7. All J1939 socket constants (CAN_J1939, J1939_*, SO_J1939_*, SCM_J1939_*) were added to CPython in 3.9. They are now all defined using getattr(socket, 'NAME', fallback) with the kernel-defined integer fallbacks from linux/can/j1939.h, so the module loads cleanly on Python 3.7/3.8. NativeJ1939Socket was updated to use the module-level constants instead of socket.* attributes.
Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/fa552fbf-b860-4eb2-93bb-d81d2e2711d3 Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
| } | ||
|
|
||
|
|
||
| class J1939_TP_CM_RTS(Packet): |
There was a problem hiding this comment.
create more unit test for this communication commands.
Also create interoperability unit tests, where a J1939 Socket send data do a NativeCanSocket and vice versa, so that the interoperability of long messages is given.
There was a problem hiding this comment.
Done in commit 1c7bf4e.
Extended TP unit tests (13 new non-vcan tests, section "TP Communication Commands – extended unit tests"):
J1939_TP_CM_RTS: default field values (ctrl=16,max_packets=0xFF, 8-byte wire size), PGN little-endian encoding,max_packetsround-tripJ1939_TP_CM_CTS: reserved bytes0xFFFF,next_packetround-tripJ1939_TP_CM_ACK: reserved byte0xFF, 8-byte wire sizeJ1939_TP_CM_ABORT: reserved byte defaults, all 14 defined reason codesJ1939_TP_DT: default0xFFpadding, 3-packet session reassemblyJ1939_TP_CM.dispatch_hook: empty bytes,None, unknown ctrl bytes- Complete BAM exact wire bytes, RTS/CTS/ACK handshake wire bytes
Interop vcan tests (2 new vcan_socket needs_root tests):
NativeJ1939Socketsends a 20-byte payload →NativeCANSocketcollects 4 raw CAN frames, verifiesTP.CM BAMstructure andTP.DTsequence/payloadNativeCANSocketsends manually-craftedTP.CM BAM+ 3×TP.DTCAN frames →NativeJ1939Socketreceives the kernel-reassembled message and verifies payload/PGN/SA
All 84 non-vcan tests pass.
…erop tests Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/de07cfe2-43fe-415d-88a8-1174b542bffb Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/de07cfe2-43fe-415d-88a8-1174b542bffb Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
…zzer utilities Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/f4008f06-5ed9-4abc-adb6-826da5eadd48 Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
…es; remove TruckDevil attribution comment Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/c1664eef-32b6-4bc4-b6d8-c28288699fa6 Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/ab7cd4c1-ef38-4f65-8988-f677041c013a Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Fixed in commit
|
|
@copilot Fix the failing unit tests |
Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/1b9995db-718d-4de4-a10b-abd3f601012e Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Fixed in commit
|
|
@copilot two tests still fail with permission denied errors. Maybe only execute those tests with the tag "needs_root" |
…ix J1939_NO_PGN fallback Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/7202a1c5-a049-4d43-bc34-b34b7a685fe3 Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Fixed in commit Root cause: both sender sockets were created with the default Two additional fixes in the same commit:
|
|
@copilot the unit tests still fail. |
|
@copilot Fix the failing unit tests |
sendto()in CI):promisc=True, which setsSO_J1939_PROMISC=1. The Linux kernel rejectssendto()on promiscuous J1939 sockets with EPERM (promiscuous = receive-only).promisc=Falseto sender sockets in tests 087 and 088; only receiver sockets need promiscuous mode.J1939_NO_PGNfallback value inj1939.pywas0x40000— corrected to0x40000000(matches Linuxlinux/can/j1939.h)._socket.J1939_NO_PGN(unavailable on Python < 3.9) with module-levelJ1939_NO_PGNconstant.