uds

package
v0.783.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package uds decodes UDS (Unified Diagnostic Services, ISO 14229-1) application-layer messages — the protocol behind modern ECU diagnostics and attacks (session control, security access, routine control, memory read/write, firmware transfer). It names the service, classifies the message as a request / positive response / negative response, decodes the negative-response code, and surfaces the sub-function and data identifier where they apply.

Wrap-vs-native judgement

Native. The UDS service-ID assignments, the positive-response convention (response SID = request SID + 0x40), the 0x7F negative-response framing, and the negative-response-code (NRC) table are a public ISO standard (ISO 14229-1), reproduced identically by python-udsoncan, CaringCaribou, and Wireshark's UDS dissector. Decoding is a static lookup over the reassembled application PDU — no ISO-TP reassembly, no bus, no hardware at analysis time (the caller brings the assembled message, e.g. from a canbus capture). The j1850 decoder explicitly covers only legacy OBD-II modes 1-9 and notes UDS as out of scope; this fills that gap.

No confidently-wrong output

Only ISO-14229-assigned service IDs, NRCs, and the common sub-function enums are named; an unknown service / NRC / sub-function value, and every manufacturer-specific data identifier, is surfaced with its raw hex and numeric value, never guessed. ISO-TP framing, the security-access seed/key crypto, and full per-service payload dissection are deliberately left to the raw payload bytes.

Index

Constants

View Source
const NegativeResponseSID = 0x7F

NegativeResponseSID is the service-ID byte that introduces a negative response: 0x7F <originalSID> <NRC>.

Variables

This section is empty.

Functions

func Encode added in v0.411.0

func Encode(r EncodeRequest) ([]byte, error)

Encode builds the bytes of a UDS application PDU — the inverse of DecodeBytes. The byte order matches what the decoder expects (SID, optional sub-function, optional 16-bit DID, then payload), so it round-trips through DecodeBytes. This is the application-layer top of the inject pipeline: build the request here, segment it with isotp_encode, wrap each frame with canbus_fd_encode, and send via canbus_inject.

Wrap-vs-native judgement

Native, and the inverse of the decoder: pure byte assembly over the public ISO 14229 framing (+0x40 positive response, 0x7F negative response). Generation only — it produces a PDU and transmits nothing. Correctness is verifiable by round-trip against DecodeBytes plus hand-computed request bytes (e.g. ReadDataByIdentifier(VIN) = 22 F1 90).

func EncodeHex added in v0.411.0

func EncodeHex(r EncodeRequest) (string, error)

EncodeHex is a convenience wrapper returning the PDU as an uppercase hex string.

Types

type DTCStatus added in v0.727.0

type DTCStatus struct {
	Raw string `json:"raw"` // 0xNN

	TestFailed                         bool `json:"test_failed"`                             // bit 0 (0x01)
	TestFailedThisOperationCycle       bool `json:"test_failed_this_operation_cycle"`        // bit 1 (0x02)
	PendingDTC                         bool `json:"pending_dtc"`                             // bit 2 (0x04)
	ConfirmedDTC                       bool `json:"confirmed_dtc"`                           // bit 3 (0x08)
	TestNotCompletedSinceLastClear     bool `json:"test_not_completed_since_last_clear"`     // bit 4 (0x10)
	TestFailedSinceLastClear           bool `json:"test_failed_since_last_clear"`            // bit 5 (0x20)
	TestNotCompletedThisOperationCycle bool `json:"test_not_completed_this_operation_cycle"` // bit 6 (0x40)
	WarningIndicatorRequested          bool `json:"warning_indicator_requested"`             // bit 7 (0x80)

	// SetFlags lists the names of the set bits, low to high — a one-glance read.
	SetFlags []string `json:"set_flags"`

	// Summary is the single most operator-relevant takeaway: is the fault
	// confirmed (stored), pending, currently failing, or clean.
	Summary string `json:"summary"`
}

DTCStatus is a decoded UDS DTC status byte — the 8-bit DTCStatusMask that service 0x19 (ReadDTCInformation) returns alongside every DTC. The eight bits are defined in ISO 14229-1 Annex D.2 (statusOfDTC); together they say whether a fault is currently failing, pending, confirmed/stored, and whether the MIL (warning indicator) is requested.

func DecodeDTCStatus added in v0.727.0

func DecodeDTCStatus(b byte) *DTCStatus

DecodeDTCStatus decodes a single UDS DTC status byte per ISO 14229-1 Annex D.2. Every value 0x00..0xFF is structurally valid (all eight bits are defined), so there is no failure mode beyond a wrong byte count at the tool layer.

func DecodeDTCStatusHex added in v0.727.0

func DecodeDTCStatusHex(s string) (*DTCStatus, error)

DecodeDTCStatusHex decodes a single status byte supplied as hex (':' / '-' / '_' / whitespace and a '0x' prefix tolerated).

type EncodeRequest added in v0.411.0

type EncodeRequest struct {
	// Direction selects the message shape: "request" (default),
	// "positive_response" (SID + 0x40), or "negative_response"
	// (0x7F <SID> <NRC>).
	Direction string
	// Service is the request service ID (e.g. 0x10, 0x22, 0x27). For a
	// positive response the +0x40 is applied automatically.
	Service int
	// SubFunction, when non-nil, is emitted as the byte after the SID
	// (with the SuppressPositiveResponse bit OR-ed in for a request).
	SubFunction *int
	// SuppressPositiveResponse sets bit 7 of the sub-function byte
	// (request only; ignored without a sub-function).
	SuppressPositiveResponse bool
	// DataIdentifier, when non-nil, is emitted as a 2-byte big-endian DID
	// after the sub-function (Read/Write DataByIdentifier services).
	DataIdentifier *int
	// NRC is the negative-response code (required for negative_response).
	NRC *int
	// Payload is trailing data appended after the structured fields.
	Payload []byte
}

EncodeRequest describes a UDS message to build.

type UDS

type UDS struct {
	Direction                string   `json:"direction"` // request | positive_response | negative_response
	ServiceID                int      `json:"service_id"`
	ServiceIDHex             string   `json:"service_id_hex"`
	Service                  string   `json:"service"`
	SubFunction              *int     `json:"sub_function,omitempty"`
	SubFunctionName          string   `json:"sub_function_name,omitempty"`
	SuppressPositiveResponse bool     `json:"suppress_positive_response,omitempty"`
	DataIdentifier           *int     `json:"data_identifier,omitempty"`
	DataIdentifierName       string   `json:"data_identifier_name,omitempty"`
	NRC                      *int     `json:"nrc,omitempty"`
	NRCName                  string   `json:"nrc_name,omitempty"`
	PayloadHex               string   `json:"payload_hex,omitempty"`
	Notes                    []string `json:"notes,omitempty"`
}

UDS is the decoded view of a UDS message.

func Decode

func Decode(hexStr string) (*UDS, error)

Decode parses a hex-encoded UDS application PDU (the reassembled message, without ISO-TP framing). Separators and a 0x prefix are tolerated.

func DecodeBytes

func DecodeBytes(b []byte) (*UDS, error)

DecodeBytes parses a UDS application PDU from raw bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL