uds

package
v0.577.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 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 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