Skip to content

Manual Bitwise Parsing and Lack of Strong Schemas #35

Description

@RaulSMS

The library depends heavily on manual bit shifts (<<, >>) and bitwise masking (&, |) applied directly inside operational code to parse the 29-bit J1939 identifier (Priority, Data Page, PDU Format, PDU Specific, Source Address) and the 64-bit ECU NAME field.

  • This creates a highly imperative codebase that is prone to off-by-one errors and makes onboarding or debugging incredibly tedious.
  • Data integrity relies entirely on runtime type handling without leveraging static type validation capabilities.

Proposed Architecture Fix

Utilize Python 3.10's modern, optimized @dataclass(slots=True) syntax to create immutable, strongly typed schemas for J1939 data primitives.

  • The use of slots=True minimizes memory overhead and optimizes attribute lookup performance for low-level message routing.
  • Integrate Python 3.10's Structural Pattern Matching (match/case) to cleanly route packets based on PDU format combinations.

Architectural Example (Python 3.10):

from dataclasses import dataclass

@dataclass(frozen=True, slots=True)
class J1939Identifier:
    priority: int
    pgn: int
    source_address: int
    destination_address: int | None = None

# High-performance, declarative dispatching
def dispatch_pdu(pdu_format: int, pdu_specific: int, frame_data: bytes):
    match (pdu_format, pdu_specific):
        case (236, _):
            # Route to Transport Connection Management Handler
            process_tp_cm(frame_data)
        case (235, _):
            # Route to Transport Data Transfer Handler
            process_tp_dt(frame_data)
        case (pdu_f, _) if pdu_f >= 240:
            # Global broadcast PGN pattern
            process_broadcast_pgn(pdu_format, pdu_specific, frame_data)
        case _:
            process_peer_to_peer(pdu_format, pdu_specific, frame_data)

Caution

This issue needs clarification since it may be a large rework and benefits is not that clear yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions