usbpd

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: 4 Imported by: 0

Documentation

Overview

Package usbpd decodes USB Power Delivery (USB-PD) messages — the protocol spoken over the USB-C CC line to negotiate power (and to tunnel alternate modes and vendor-defined messages). USB-PD is an emerging hardware-attack surface: a malicious charger or a PD-capable cable can advertise bogus power capabilities, drive a sink to request out-of-spec voltage, or carry vendor-defined messages that trigger device-specific behaviour — so a captured PD exchange (from a CC-line analyzer) is real recon. A USB-PD message identifies the **negotiation step** — a Source/Sink Capabilities advertisement (and the offered voltages/currents), a Request, an Accept/Reject/PS_RDY, a role swap, a Vendor-Defined Message — which is the headline for charger / cable analysis. It joins the project's USB analysis stack (internal/usbdesc, internal/hidreport, internal/usbhid).

Wrap-vs-native judgement

Native. A USB-PD message is a 16-bit little-endian header (message type,
roles, spec revision, message id, data-object count) optionally followed
by 32-bit little-endian Data Objects. A bit-field read + a per-message-type
walk; stdlib only, no new go.mod dep.

Verifiable / no confidently-wrong output

The header bit layout, the control- and data-message type tables, and the
Fixed / Variable / Battery Power Data Object layouts follow the USB Power
Delivery specification — deterministic and byte-checkable against spec-
built PDOs (e.g. 5 V @ 3 A). The control-vs-data message-type dispatch is
driven by the header's data-object count (no ambiguity). Only the
standardised, well-defined fields are decoded: the header, and — for
Source/Sink Capabilities — the Fixed (voltage/current + role flags),
Variable and Battery PDOs; an Augmented PDO (PPS / AVS) is surfaced by
type with its 32-bit value raw, and the Request RDO, BIST, Vendor-Defined
and other data messages' objects are surfaced as raw hex (their layouts
are position-dependent and would be confidently-wrong without the prior
Capabilities context). The input is the raw on-wire bytes (little-endian).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataObject

type DataObject struct {
	Raw         string `json:"raw"`
	Kind        string `json:"kind,omitempty"` // PDO type / "RDO" / "VDO" / etc.
	VoltageV    string `json:"voltage_v,omitempty"`
	MaxCurrentA string `json:"max_current_a,omitempty"`
	Flags       string `json:"flags,omitempty"`
}

DataObject is one 32-bit USB-PD data object.

type Result

type Result struct {
	HeaderHex      string       `json:"header_hex"`
	MessageClass   string       `json:"message_class"` // "control" | "data"
	MessageType    int          `json:"message_type"`
	MessageName    string       `json:"message_name"`
	SpecRevision   string       `json:"spec_revision"`
	PortPowerRole  string       `json:"port_power_role"`
	PortDataRole   string       `json:"port_data_role"`
	MessageID      int          `json:"message_id"`
	NumDataObjects int          `json:"num_data_objects"`
	Extended       bool         `json:"extended,omitempty"`
	DataObjects    []DataObject `json:"data_objects,omitempty"`
	Notes          []string     `json:"notes,omitempty"`
}

Result is the decoded view of a USB-PD message.

func Decode

func Decode(input string) (*Result, error)

Decode parses a USB-PD message (raw on-wire bytes, little-endian) from hex (whitespace / ':' / '-' / '_' separators and a '0x' prefix tolerated).

Jump to

Keyboard shortcuts

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