Skip to content

CAN decoder: add bit destuffing for real capture support #14

Description

@vailuc

Problem

The current decodeCAN implementation reads raw bit values without performing CAN bit destuffing. Per the CAN 2.0 spec, after 5 consecutive bits of the same value, a complement (stuff) bit is inserted. The decoder must remove these stuff bits to correctly parse the arbitration, control, data, and CRC fields.

Impact

  • Real CAN captures (e.g. the can_mcp2515_125k_msg_222.sr fixture from sigrok-dumps) cannot be fully decoded — decodeCAN returns 0 frames.
  • autoDetectLaProtocol falls back to UART instead of CAN on real captures.
  • Label-based detection (inferDecoderDefaults with CAN_RX label) still works correctly.

Fix

Add a bit-destuffing pass in decodeCAN that:

  1. Tracks consecutive same-valued bits
  2. After 5 consecutive, skips the next stuff bit
  3. Uses destuffed bit stream for all field parsing (ID, RTR, IDE, DLC, data, CRC)

Test

The existing can_mcp2515_125k_msg_222.sr fixture (4 MS/s, 125 kbits, standard frame ID 222 with 5 data bytes) can be used to verify the fix. The test in sigrokFixtures.test.ts already checks label detection and signal activity — once destuffing is added, it should be extended to verify full frame decode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LALogic Analyzer subsystembugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions