XCP

Rust-backed XCP using a packet transport, independent of its underlying CAN or network connection. Construct XCPClient(transport) and call connect(). XCPOnCAN supports CAN/CAN FD; XCPOnTCP and XCPOnUDP provide XCP Ethernet framing. Clients, transports, configuration and enums live together in automotive.protocols.xcp. XCPTransport exposes maximum TX/RX packet sizes without their transport headers. Closing a transport also closes its client.

XCPOnUDP.connect(host, port, local_addr=(local_host, local_port)) selects a fixed local UDP endpoint. Omitting local_addr selects an ephemeral local port. Both async and synchronous APIs accept this option.

Each method sends one XCP command. upload() and short_upload() read one response; download() sends one packet. get_seed() and unlock() exchange one segment per call, with the mode or remaining length supplied explicitly. Callers handle transfer loops using the CONNECT capabilities. Sizes count bytes; addresses count ECU elements, with negotiated byte order and address granularity.

Commands use a mutex with no background request tasks. Cancelling an active exchange leaves the session unsynchronized; call synchronize() before another command. An interrupted TCP write requires reopening the connection. Closing waits for the active exchange to finish or reach its command deadline.

UDS and XCP share enum conversion machinery, with their own scoped definitions. The API provides standard-mode commands, not an A2L parser, block-mode sequence engine or background DAQ scheduler. The protocol references are ASAM XCP 1.5, sections 7.5.1/7.5.2, and ASAM XCP on Ethernet 1.1, section 1.3.

Rust-backed XCP over raw CAN/CAN FD and Ethernet TCP/UDP.

ASAM XCP 1.5 sections 7.5.1 and 7.5.2 define command encoding. All byte ordering, alignment, framing, serialization, and deadlines are handled by Rust.

class automotive.protocols.xcp.Checksum(checksum_type: int, checksum: int)[source]

Raw XCP checksum type and ECU-calculated value.

checksum_type: int
checksum: int
class automotive.protocols.xcp.Command(*values)

Named Command wire values from Rust.

Members are integers and may be passed wherever a raw code is accepted. Unknown codes remain raw integers in protocol results. Iteration visits only known values; constructing this enum with an unknown value raises ValueError.

CONNECT = 0xFF
DISCONNECT = 0xFE
GET_STATUS = 0xFD
SYNCH = 0xFC
GET_COMM_MODE_INFO = 0xFB
GET_ID = 0xFA
SET_REQUEST = 0xF9
GET_SEED = 0xF8
UNLOCK = 0xF7
SET_MTA = 0xF6
UPLOAD = 0xF5
SHORT_UPLOAD = 0xF4
BUILD_CHECKSUM = 0xF3
TRANSPORT_LAYER_CMD = 0xF2
USER_CMD = 0xF1
DOWNLOAD = 0xF0
DOWNLOAD_NEXT = 0xEF
DOWNLOAD_MAX = 0xEE
SHORT_DOWNLOAD = 0xED
MODIFY_BITS = 0xEC
SET_CAL_PAGE = 0xEB
GET_CAL_PAGE = 0xEA
GET_PAG_PROCESSOR_INFO = 0xE9
GET_SEGMENT_INFO = 0xE8
GET_PAGE_INFO = 0xE7
SET_SEGMENT_MODE = 0xE6
GET_SEGMENT_MODE = 0xE5
COPY_CAL_PAGE = 0xE4
CLEAR_DAQ_LIST = 0xE3
SET_DAQ_PTR = 0xE2
WRITE_DAQ = 0xE1
SET_DAQ_LIST_MODE = 0xE0
GET_DAQ_LIST_MODE = 0xDF
START_STOP_DAQ_LIST = 0xDE
START_STOP_SYNCH = 0xDD
GET_DAQ_CLOCK = 0xDC
READ_DAQ = 0xDB
GET_DAQ_PROCESSOR_INFO = 0xDA
GET_DAQ_RESOLUTION_INFO = 0xD9
GET_DAQ_LIST_INFO = 0xD8
GET_DAQ_EVENT_INFO = 0xD7
FREE_DAQ = 0xD6
ALLOC_DAQ = 0xD5
ALLOC_ODT = 0xD4
ALLOC_ODT_ENTRY = 0xD3
PROGRAM_START = 0xD2
PROGRAM_CLEAR = 0xD1
PROGRAM = 0xD0
PROGRAM_RESET = 0xCF
GET_PGM_PROCESSOR_INFO = 0xCE
GET_SECTOR_INFO = 0xCD
PROGRAM_PREPARE = 0xCC
PROGRAM_FORMAT = 0xCB
PROGRAM_NEXT = 0xCA
PROGRAM_MAX = 0xC9
PROGRAM_VERIFY = 0xC8
WRITE_DAQ_MULTIPLE = 0xC7
TIME_CORRELATION_PROPERTIES = 0xC6
DTO_CTR_PROPERTIES = 0xC5
LEVEL1 = 0xC0
class automotive.protocols.xcp.CommunicationModeInfo(comm_mode_optional: int, max_bs: int, min_st: int, queue_size: int, driver_version: int)[source]

Optional communication modes and limits returned by the ECU.

comm_mode_optional: int
max_bs: int
min_st: int
queue_size: int
driver_version: int
class automotive.protocols.xcp.ConnectInfo(resource: int, comm_mode_basic: int, byte_order: Literal['little', 'big'], address_granularity: int, max_cto: int, max_dto: int, protocol_version: int, transport_version: int, max_upload_size: int)[source]

Negotiated limits. Version fields contain major versions only.

address_granularity is bytes per ECU address. max_upload_size is the largest byte count accepted by a single SHORT_UPLOAD request.

resource: int
comm_mode_basic: int
byte_order: Literal['little', 'big']
address_granularity: int
max_cto: int
max_dto: int
protocol_version: int
transport_version: int
max_upload_size: int
property slave_block_mode: bool

Whether the ECU advertises slave block transfer support.

class automotive.protocols.xcp.ConnectMode(*values)

Named ConnectMode wire values from Rust.

Members are integers and may be passed wherever a raw code is accepted. Unknown codes remain raw integers in protocol results. Iteration visits only known values; constructing this enum with an unknown value raises ValueError.

NORMAL = 0x00
USER_DEFINED = 0x01
class automotive.protocols.xcp.ErrorCode(*values)

Named ErrorCode wire values from Rust.

Members are integers and may be passed wherever a raw code is accepted. Unknown codes remain raw integers in protocol results. Iteration visits only known values; constructing this enum with an unknown value raises ValueError.

COMMAND_SYNCHRONIZATION = 0x00
COMMAND_BUSY = 0x10
DAQ_ACTIVE = 0x11
PGM_ACTIVE = 0x12
COMMAND_UNKNOWN = 0x20
COMMAND_SYNTAX = 0x21
OUT_OF_RANGE = 0x22
WRITE_PROTECTED = 0x23
ACCESS_DENIED = 0x24
ACCESS_LOCKED = 0x25
PAGE_NOT_VALID = 0x26
MODE_NOT_VALID = 0x27
SEGMENT_NOT_VALID = 0x28
SEQUENCE = 0x29
DAQ_CONFIG = 0x2A
MEMORY_OVERFLOW = 0x30
GENERIC = 0x31
VERIFY = 0x32
RESOURCE_TEMPORARILY_NOT_ACCESSIBLE = 0x33
class automotive.protocols.xcp.Identification(mode: int, length: int, data: bytes)[source]

GET_ID result; mode 0 sets MTA for subsequent UPLOAD commands.

mode: int
length: int
data: bytes
class automotive.protocols.xcp.IdentificationType(*values)

Named IdentificationType wire values from Rust.

Members are integers and may be passed wherever a raw code is accepted. Unknown codes remain raw integers in protocol results. Iteration visits only known values; constructing this enum with an unknown value raises ValueError.

ASCII = 0x00
FILE_NAME = 0x01
PATH = 0x02
URL = 0x03
UPLOAD = 0x04
class automotive.protocols.xcp.Resource(*values)

Named Resource wire values from Rust.

Members are integers and may be passed wherever a raw code is accepted. Flags may be combined; unknown bits are preserved.

CAL_PAG = 1
DAQ = 4
STIM = 8
PGM = 16
DBG = 32
class automotive.protocols.xcp.Seed(length: int, data: bytes)[source]

GET_SEED segment; length includes this and all remaining seed bytes.

length: int
data: bytes
class automotive.protocols.xcp.Status(session_status: int, resource_protection_status: int, session_configuration_id: int)[source]

Session and resource protection masks returned by GET_STATUS.

session_status: int
resource_protection_status: int
session_configuration_id: int
class automotive.protocols.xcp.XCPCanConfig(*, tx_id: int | Identifier, rx_id: int | Identifier, bus: int = 0, fd: bool = False, brs: bool | None = None, padding: int | None = 0, max_dlen: int | None = None)[source]

CAN addressing and frame configuration.

fd selects CAN FD. brs=None follows the adapter setting. max_dlen=None selects eight bytes for CAN or 64 for FD. padding=None disables optional fill; FD still rounds to a valid DLC. SocketCAN timing is configured by the OS. Integers or explicit standard/extended Identifiers are accepted.

tx_id: int | Identifier
rx_id: int | Identifier
bus: int
fd: bool
brs: bool | None
padding: int | None
max_dlen: int | None
class automotive.protocols.xcp.XCPClient(transport: XCPTransport, config: XCPConfig | None = None)[source]

Persistent XCP client retaining its CAN adapter or Ethernet socket.

Construct with XCPOnCAN, XCPOnTCP or XCPOnUDP, then call connect() before other commands. One client serializes its command exchanges. After cancellation, a timeout, or a malformed response, call synchronize(); an interrupted TCP transmission requires closing and reopening the client. Negative responses raise XCPResponseError with raw command, code, and data fields.

Optional memory commands require ECU support and sufficient resource access. Memory sizes are bytes, aligned to the negotiated address granularity; addresses count ECU elements. No memory write or programming command is sent by connection or read commands. Each method sends one command; callers handle transfer loops and segmented seed/key exchanges. Closing waits for an active exchange to finish or reach its deadline.

connection_info: ConnectInfo | None
async connect(mode: ConnectMode | int = ConnectMode.NORMAL) → ConnectInfo[source]

CONNECT in normal mode (0), or the ECU-defined mode (1).

async disconnect() → None[source]

Send DISCONNECT and clear the negotiated session after acknowledgement.

async synchronize() → None[source]

Recover synchronization by waiting for SYNCH’s ERR_CMD_SYNCH reply.

async get_status() → Status[source]

Read session state and resource protection without changing the ECU.

async get_comm_mode_info() → CommunicationModeInfo[source]

Read optional communication capabilities.

async get_id(id_type: IdentificationType | int = IdentificationType.ASCII) → Identification[source]

GET_ID; zero requests ASCII identification, other values are ECU-dependent.

async get_seed(mode: int, resource: Resource | int) → Seed[source]

GET_SEED: read one segment. Mode 0 starts; mode 1 continues.

async unlock(length: int, key: bytes | bytearray | memoryview) → int[source]

UNLOCK: send one segment; length includes all remaining key bytes.

async set_mta(address: int, address_extension: int = 0) → None[source]

SET_MTA: set the ECU memory transfer address.

async upload(size: int) → bytes[source]

UPLOAD: read one response at MTA; size is bytes aligned to AG.

async short_upload(address: int, size: int, address_extension: int = 0) → bytes[source]

Read one response, at most connection_info.max_upload_size bytes.

Size is in bytes and must be aligned to address_granularity. Rust encodes the element count, address extension, address, and alignment.

async download(data: bytes | bytearray | memoryview) → None[source]

DOWNLOAD: write one packet at MTA within the negotiated MAX_CTO.

async build_checksum(block_size: int) → Checksum[source]

Calculate a checksum at MTA; block_size counts ECU address elements.

class automotive.protocols.xcp.XCPConfig(*, timeout: float = 1.0)[source]

Command deadline in seconds, including transmission and reception.

Timeout must be finite and positive. Commands are never automatically retried. Cancelling a command before it acquires the session sends nothing. Cancelling an active exchange requires an explicit synchronize() afterwards.

timeout: float
class automotive.protocols.xcp.XCPOnCAN(adapter: CanAdapter, config: XCPCanConfig)[source]

Packet transport retaining an adapter and its own receive subscription.

class automotive.protocols.xcp.XCPOnTCP(native: R)[source]

Reassemble complete XCP packets from TCP, retaining partial frames.

async classmethod connect(host: str, port: int = 5555, timeout: float = 1.0) → Self[source]

Open TCP; an XCPClient separately negotiates the XCP session.

class automotive.protocols.xcp.XCPOnUDP(native: R)[source]

XCP UDP packets from a fixed ECU peer, with datagram boundary checks.

async classmethod connect(host: str, port: int = 5555, timeout: float = 1.0, *, local_addr: tuple[str, int] | None = None) → Self[source]

Open UDP, optionally binding a local (host, port) before connecting.

None selects an ephemeral local port. XCPClient establishes the session.

class automotive.protocols.xcp.XCPTransport(native: R)[source]

Own complete-packet I/O and framing; attach one XCP client at a time.

Closing stops new commands and waits for the active exchange or its timeout. Rust implementations provide limits, send, receive and optional recovery.

property max_tx_packet_size: int

Largest outgoing packet, excluding transport headers.

property max_rx_packet_size: int

Largest incoming packet, excluding transport headers.

Synchronous API

Use the same configuration, result types and exceptions with ordinary method calls under automotive.sync.protocols.xcp. TCP and UDP constructors block until their transport is open. Call connect() separately to negotiate XCP.

Blocking XCP API forwarding to the same Rust-backed asynchronous client.

class automotive.sync.protocols.xcp.XCPClient(transport: XCPTransport, config: XCPConfig | None = None)[source]

Synchronous XCP over CAN, CAN FD, TCP or UDP.

Configuration, values, exceptions and cancellation behavior match the async client. Call connect() before commands. close() releases the local transport; disconnect() gracefully ends the ECU session first.

property config: XCPConfig

Shared command timing settings.

property connection_info: ConnectInfo | None

Capabilities returned by the last successful CONNECT.

connect(mode: ConnectMode | int = ConnectMode.NORMAL) → ConnectInfo[source]

CONNECT with normal (0) or user-defined (1) mode.

disconnect() → None[source]

End the XCP session and await its acknowledgement.

synchronize() → None[source]

Recover after an uncertain command by waiting for ERR_CMD_SYNCH.

get_status() → Status[source]

Read session and resource protection status.

get_comm_mode_info() → CommunicationModeInfo[source]

Read optional communication capabilities.

get_id(id_type: IdentificationType | int = IdentificationType.ASCII) → Identification[source]

GET_ID; mode zero sets MTA for subsequent UPLOAD commands.

get_seed(mode: int, resource: Resource | int) → Seed[source]

GET_SEED: read one segment, with mode 0 to start or 1 to continue.

unlock(length: int, key: bytes | bytearray | memoryview) → int[source]

UNLOCK: send one segment; length includes all remaining key bytes.

set_mta(address: int, address_extension: int = 0) → None[source]

Set MTA; addresses count the ECU’s negotiated address elements.

upload(size: int) → bytes[source]

UPLOAD: read one response at MTA; size is bytes aligned to AG.

short_upload(address: int, size: int, address_extension: int = 0) → bytes[source]

Read one SHORT_UPLOAD response; size is bytes aligned to AG.

download(data: bytes | bytearray | memoryview) → None[source]

DOWNLOAD: write one packet at MTA within the negotiated MAX_CTO.

build_checksum(block_size: int) → Checksum[source]

Calculate checksum at MTA; block_size counts ECU elements.

class automotive.sync.protocols.xcp.XCPOnCAN(adapter: CanAdapter, config: XCPCanConfig)[source]

Retain the adapter and subscribe to the configured receive ID.

class automotive.sync.protocols.xcp.XCPOnTCP(resource: R)[source]

XCP packet framing over a TCP byte stream.

classmethod connect(host: str, port: int = 5555, timeout: float = 1.0) → Self[source]

Open TCP before constructing an XCPClient.

class automotive.sync.protocols.xcp.XCPOnUDP(resource: R)[source]

XCP packet framing over UDP datagrams.

classmethod connect(host: str, port: int = 5555, timeout: float = 1.0, *, local_addr: tuple[str, int] | None = None) → Self[source]

Open UDP with an optional local (host, port), matching the async API.

class automotive.sync.protocols.xcp.XCPTransport(resource: R)[source]

Packet I/O retained by one protocol client, with shared lifetime.

property max_tx_packet_size: int

Largest outgoing packet, excluding headers.

property max_rx_packet_size: int

Largest incoming packet, excluding headers.