# christoph2/pyxcp architecture

> Architecture diagram of the GitHub repository christoph2/pyxcp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/christoph2/pyxcp
- Repository: https://github.com/christoph2/pyxcp

## Overview

##### Project type and purpose
- pyXCP is an open-source, full-featured Python library and CLI toolset for communicating with XCP (ASAM MCD-1)-enabled devices (primarily automotive ECUs).
- It provides APIs to take measurements, adjust parameters, flash ECUs, record/log data, and generate DAQ stimuli.
- Supports multiple transports (CAN, Ethernet, USB, SCI), seed-and-key unlocking, and high-performance data recording via C++ extensions.

##### File structure
- Top-level folders and files:
- container/ – Docker/Tmux/Zsh configuration for development environments
- docs/ – Sphinx documentation and tutorials
- pyxcp/ – core Python package
- pyxcp/cpp_ext/ – C++ extension wrappers for performance-critical operations
- pyxcp/transport/ – transport adapter modules (CAN, Ethernet, SXI, USB)
- pyxcp/daq_stim/ – DAQ stimulus scheduler and optimizer (C++ and Python)
- pyxcp/recorder/ – data recording library (LZ4, file I/O, C++ and Python glue)
- pyxcp/asam/ – ASAM-A2L parsing and data type definitions
- pyxcp/master/ – master state machine and command handling
- pyxcp/scripts/ – helper CLI scripts (auto-discovery, profiling, conversion)
- pyxcp/examples/ – end-user examples illustrating common use cases
- pyxcp/tests/ – unit/integration tests for each component
- Build and CI:
- CMakeLists.txt & build_ext.py for building C++ modules
- setup.py/pyproject.toml for packaging
- .github/workflows, appveyor.yml, code climate etc. for CI, linting, coverage

##### README for additional insights
- Confirms purpose: lightweight Python library to talk to XCP-enabled devices.
- Mentions Python ≥3.7, optional seed-and-key DLLs on Windows requiring a bridge executable.
- Setup via pip, source installation, or Docker (container).

a. Main components to include
1. Client layer
– User applications and CLI scripts (pyxcp/scripts, pyxcp/examples)
2. Core Python API
– pyxcp.master (state machine, command dispatch)
– pyxcp.cmdline (entry points)
3. ASAM and configuration
– pyxcp.asam (A2L parsing, data types)
– pyxcp.config/legacy (configuration management)
4. Transport layer
– Python adapters: can.py, eth.py, sxi.py, usb_transport.py
– C++ transport wrapper (transport_wrapper.cpp)
5. C++ performance extensions
– pyxcp/cpp_ext (helper.hpp, tsqueue, event, blockmem, etc.)
– used by recorder and DAQ stimulus
6. DAQ stimulus engine
– pyxcp.daq_stim (scheduler, bin-packing optimizer, C++ wrappers)
7. Data recorder
– pyxcp.recorder (LZ4 compressor, file I/O, reader/writer, Rekorder C++ backend)
8. External systems
– ECU hardware over CAN/Ethernet/USB/SXI
– Optional seed-and-key DLL bridge (asamkeydll.exe)
9. Storage & logs
– Local file system (recorded data files, configurations)

b. Relationships and data flows
- User invokes CLI ➔ Python API (master, cmdline)
- API loads ASAM A2L definitions ➔ config module
- API issues commands ➔ transport adapter ➔ C++ wrapper (if performance-critical) ➔ physical bus ➔ ECU
- ECU responses flow back through transport ➔ API event queue ➔ user callback or recorder
- Recorder may compress and persist data to disk via C++ Rekorder core
- DAQ stimulus scheduler packages stimuli via C++ optimizer ➔ transport to ECU

c. Architectural patterns and design principles
- Layered architecture: separation of concerns (API, transport, data processing, persistence)
- Plugin-style transport adapters: new bus protocols can be added under pyxcp/transport
- High-performance sections isolated into C++ extensions to minimize Python overhead
- Configuration and ASAM parsing decoupled from command logic (single responsibility)
- CLI/scripts layer thinly wraps core library for discoverability and automation

d. Key technologies, frameworks, and libraries
- Python 3.7+, setuptools, pyproject.toml
- C++11 for extensions, CMake for build glue
- LZ4 for data compression in recorder
- pytest for tests, Sphinx for docs, Docker for dev container
- Black, pylintrc, pre-commit for code style

##### Essential details
- Plugin boundaries (transport, C++ ext, recorder)
- Call out performance-critical paths vs. control-flow paths
- That external dependencies (ECU hardware, DLLs, file storage) are represented

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        direction TB
        Scripts["CLI Scripts"]:::python
        Examples["End-User Examples"]:::python
    end

    %% Core Python API
    subgraph "Core Python API"
        direction TB
        Cmdline["cmdline.py"]:::python
        Master["Master State Machine"]:::python
    end

    %% ASAM and Configuration
    subgraph "ASAM & Configuration"
        direction TB
        ASAM["A2L/AML Parsing"]:::python
        Config["Legacy Configuration"]:::python
        KeyBridge["Seed-and-Key Bridge"]:::optional
    end

    %% Transport Layer
    subgraph "Transport Layer"
        direction TB
        PyTransport["Python Adapters"]:::python
        CppTransport["C++ Transport Wrapper"]:::cpp
    end

    %% C++ Performance Extensions
    subgraph "C++ Performance Extensions"
        direction TB
        CppExt["cpp_ext Module"]:::cpp
    end

    %% DAQ Stimulus Engine
    subgraph "DAQ Stimulus Engine"
        direction TB
        DaqPy["Scheduler/Optimizer (Python)"]:::python
        DaqCpp["Scheduler/Stimulus (C++)"]:::cpp
    end

    %% Data Recorder
    subgraph "Data Recorder"
        direction TB
        RecorderPy["Recorder Interface"]:::python
        RecorderCpp["Rekorder Backend"]:::cpp
        LZ4["LZ4 Compression"]:::cpp
    end

    %% Supporting Modules
    subgraph "Supporting Modules & Utilities"
        direction TB
        Checksum["Checksums"]:::python
        ErrorMatrix["Error Matrix"]:::python
        Timing["Timing Helpers"]:::python
        CommonTypes["Constants & Types"]:::python
        Utils["Utility Functions"]:::python
    end

    %% Build & CI
    subgraph "Build & CI"
        direction TB
        CMake["CMake & build_ext"]:::build
        Packaging["setup.py/pyproject.toml"]:::build
        CI["CI Configurations"]:::build
        Container["Dev Container"]:::build
    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        ECU["ECU Hardware"]:::external
        DLL["Seed-and-Key DLL"]:::external
        Storage["File System"]:::external
    end

    %% Connections
    Scripts -->|"invoke"| Cmdline
    Examples -->|"import"| Cmdline
    Cmdline -->|"dispatch"| Master
    Master -->|"load definitions"| ASAM
    Master -->|"use config"| Config
    Master -->|"issue commands"| PyTransport
    PyTransport -->|"wrap calls"| CppTransport
    CppTransport -->|"raw frames"| ECU
    ECU -->|"responses"| CppTransport
    CppTransport -->|"events"| Master
    Master -->|"callback/data"| RecorderPy
    RecorderPy -->|"compress"| LZ4
    RecorderPy -->|"persist"| Storage
    RecorderCpp -->|"backend I/O"| RecorderPy
    DaqPy -->|"optimize"| DaqCpp
    DaqCpp -->|"stimulus frames"| PyTransport
    Master -->|"stim commands"| DaqPy
    Master -->|"utility calls"| Checksum
    Master -->|"error handling"| ErrorMatrix
    Master -->|"timing hooks"| Timing
    Master -->|"common types"| CommonTypes
    Master -->|"utilities"| Utils
    KeyBridge -.->|optional| Master
    DLL -.->|bridge| KeyBridge

    %% Click Events
    click Scripts "https://github.com/christoph2/pyxcp/tree/master/pyxcp/scripts/"
    click Examples "https://github.com/christoph2/pyxcp/tree/master/pyxcp/examples/"
    click Cmdline "https://github.com/christoph2/pyxcp/blob/master/pyxcp/cmdline.py"
    click Master "https://github.com/christoph2/pyxcp/tree/master/pyxcp/master/"
    click ASAM "https://github.com/christoph2/pyxcp/blob/master/pyxcp/asam/types.py"
    click Config "https://github.com/christoph2/pyxcp/blob/master/pyxcp/config/legacy.py"
    click KeyBridge "https://github.com/christoph2/pyxcp/blob/master/pyxcp/asamkeydll.c"
    click PyTransport "https://github.com/christoph2/pyxcp/tree/master/pyxcp/transport/"
    click CppTransport "https://github.com/christoph2/pyxcp/blob/master/pyxcp/transport/transport_wrapper.cpp"
    click CppExt "https://github.com/christoph2/pyxcp/tree/master/pyxcp/cpp_ext/"
    click DaqPy "https://github.com/christoph2/pyxcp/blob/master/pyxcp/daq_stim/optimize/binpacking.py"
    click DaqCpp "https://github.com/christoph2/pyxcp/blob/master/pyxcp/daq_stim/scheduler.cpp"
    click RecorderPy "https://github.com/christoph2/pyxcp/blob/master/pyxcp/recorder/reco.py"
    click RecorderCpp "https://github.com/christoph2/pyxcp/blob/master/pyxcp/recorder/rekorder.cpp"
    click LZ4 "https://github.com/christoph2/pyxcp/blob/master/pyxcp/recorder/lz4.c"
    click Checksum "https://github.com/christoph2/pyxcp/blob/master/pyxcp/checksum.py"
    click ErrorMatrix "https://github.com/christoph2/pyxcp/blob/master/pyxcp/errormatrix.py"
    click Timing "https://github.com/christoph2/pyxcp/blob/master/pyxcp/timing.py"
    click CommonTypes "https://github.com/christoph2/pyxcp/blob/master/pyxcp/constants.py"
    click Utils "https://github.com/christoph2/pyxcp/blob/master/pyxcp/utils.py"
    click CMake "https://github.com/christoph2/pyxcp/blob/master/CMakeLists.txt"
    click Packaging "https://github.com/christoph2/pyxcp/blob/master/setup.py"
    click CI "https://github.com/christoph2/pyxcp/tree/master/.github/workflows/"
    click Container "https://github.com/christoph2/pyxcp/tree/master/container/"

    %% Styles
    classDef python fill:#D0E6FF,stroke:#004080;
    classDef cpp fill:#ECECEC,stroke:#666666;
    classDef external fill:#E8FFD0,stroke:#408000;
    classDef build fill:#FFF5CC,stroke:#806600;
    classDef optional stroke-dasharray: 5 5;
```
