kwp

package
v0.596.0 Latest Latest
Warning

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

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

Documentation

Overview

Package kwp decodes KWP2000 (Keyword Protocol 2000, ISO 14230-3) diagnostic messages — the predecessor to UDS still spoken by many pre-CAN / early-CAN ECUs and ELM327 adapters. It names the service, classifies the message as a request / positive response / negative response, and decodes the negative-response code.

Wrap-vs-native judgement

Native. KWP2000 shares UDS's application framing (positive response = request SID + 0x40; negative response = 0x7F <SID> <NRC>) but has a DISTINCT service-ID table — the local-identifier services (0x21 ReadDataByLocalIdentifier, 0x30 InputOutputControlByLocalIdentifier, 0x31 StartRoutineByLocalIdentifier, 0x3B WriteDataByLocalIdentifier, …) and the communication-control services (0x81 StartCommunication, 0x82 StopCommunication) do not exist in UDS, and some shared SID numbers carry different meanings. Decoding KWP traffic with uds_decode would therefore mislabel it; this is the dedicated, correct table. The service-ID and NRC assignments are a public ISO standard (ISO 14230-3), reproduced identically by ELM327 tooling, CaringCaribou and Wireshark. It is a static lookup over the application PDU — no bus, no ISO-TP reassembly, no hardware (the caller brings the assembled message, e.g. from a canbus / isotp capture). The j1850 decoder explicitly lists KWP2000 as out of scope; this fills that gap.

No confidently-wrong output

Only ISO-14230-assigned service IDs and NRCs are named; an unknown service or NRC is surfaced with its raw hex and numeric value, never guessed. The byte after the SID is surfaced with a per-service label (diagnostic session, local identifier, access mode, …) but its value enum is NOT guessed — KWP sub-function/identifier values are largely manufacturer-defined, so the raw byte plus the remaining payload are surfaced for the operator to interpret.

Index

Constants

View Source
const NegativeResponseSID = 0x7F

NegativeResponseSID introduces a negative response: 0x7F <SID> <NRC>.

Variables

This section is empty.

Functions

func Encode added in v0.412.0

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

Encode builds the bytes of a KWP2000 (ISO 14230) application PDU — the inverse of DecodeBytes. The byte order matches the decoder (SID, optional param byte, then payload), so it round-trips through DecodeBytes. KWP shares UDS's +0x40 / 0x7F framing but its own service-ID semantics (a 1-byte local-identifier/param byte, not a 16-bit DID or a suppress bit), so this is distinct from uds.Encode. Generation only — produces a PDU, transmits nothing.

Wrap-vs-native judgement

Native, and the inverse of the decoder: pure byte assembly over the public ISO 14230 framing. Correctness is verifiable by round-trip against DecodeBytes plus hand-computed request bytes (e.g. ReadDataByLocalIdentifier 0x21 + local id 0x01 = 21 01).

func EncodeHex added in v0.412.0

func EncodeHex(r EncodeRequest) (string, error)

EncodeHex returns the PDU as an uppercase hex string.

Types

type EncodeRequest added in v0.412.0

type EncodeRequest struct {
	// Direction: "request" (default), "positive_response" (SID + 0x40), or
	// "negative_response" (0x7F <SID> <NRC>).
	Direction string
	// Service is the request service ID (e.g. 0x10, 0x21, 0x81). For a
	// positive response the +0x40 is applied automatically.
	Service int
	// Param, when non-nil, is the byte after the SID — a local identifier,
	// session/access/reset type, etc. (KWP does not use UDS's
	// suppress-positive-response sub-function bit).
	Param *int
	// NRC is the negative-response code (required for negative_response).
	NRC *int
	// Payload is trailing data appended after the SID/param.
	Payload []byte
}

EncodeRequest describes a KWP2000 message to build.

type KWP

type KWP 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"`
	ParamByte    *int     `json:"param_byte,omitempty"`
	ParamLabel   string   `json:"param_label,omitempty"`
	NRC          *int     `json:"nrc,omitempty"`
	NRCName      string   `json:"nrc_name,omitempty"`
	PayloadHex   string   `json:"payload_hex,omitempty"`
	Notes        []string `json:"notes,omitempty"`
}

KWP is the decoded view of a KWP2000 message.

func Decode

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

Decode parses a hex-encoded KWP2000 application PDU. Separators and a 0x prefix are tolerated.

func DecodeBytes

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

DecodeBytes parses a KWP2000 application PDU from raw bytes.

Jump to

Keyboard shortcuts

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