# langroodi/obd-ii-emulator architecture

> Architecture diagram of the GitHub repository langroodi/obd-ii-emulator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/langroodi/obd-ii-emulator
- Repository: https://github.com/langroodi/obd-ii-emulator

## Overview

OBD-II-Emulator is a Linux library that answers OBD-II scanner queries received from a USB-CAN Analyzer over serial. Its principal workflow is serial byte reception, CAN packet decoding, query/PID parsing, service dispatch, response framing, and serial transmission. It supports synchronous and asynchronous handling, configurable CAN framing, and user-supplied OBD services. The README documents the end-to-end hardware flow; sampled implementation excerpts substantiate the emulator’s routing and serial buffering, but not every internal CAN-driver operation.

Main entry points: [`src/obd_emulator.cpp`](https://github.com/langroodi/obd-ii-emulator/blob/master/src/obd_emulator.cpp) (OBD emulator), [`src/serial_communication.cpp`](https://github.com/langroodi/obd-ii-emulator/blob/master/src/serial_communication.cpp) (Serial communication), [`src/communication_layer.cpp`](https://github.com/langroodi/obd-ii-emulator/blob/master/src/communication_layer.cpp) (Communication interface), [`src/can_driver.cpp`](https://github.com/langroodi/obd-ii-emulator/blob/master/src/can_driver.cpp) (CAN packet driver)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_transport["Serial transport"]
  node_serial["Serial communication"]
  node_communication["Communication interface"]
  node_send_buffer["Packet send buffer<br/>[packet_buffer.cpp]"]
end

subgraph group_can["CAN framing"]
  node_can_driver["CAN packet driver<br/>[can_driver.cpp]"]
  node_can_frame["CAN frame<br/>[can_frame.cpp]"]
end

subgraph group_routing["OBD query handling"]
  node_emulator["OBD emulator<br/>[obd_emulator.cpp]"]
end

subgraph group_services["OBD services"]
  node_service_base["OBD service contract<br/>[obd_service.cpp]"]
  node_service_impl["Current-data service"]
  node_sample_app["Sample application"]
end

node_scanner(("OBD-II scanner"))
node_analyzer["USB-CAN Analyzer"]

node_scanner -->|"sends queries"| node_analyzer
node_analyzer -->|"serial packets"| node_serial
node_serial -->|"implements"| node_communication
node_serial -->|"buffers sends"| node_send_buffer
node_communication -.->|"dispatches received data"| node_emulator
node_emulator -->|"decodes and encodes"| node_can_driver
node_can_driver -->|"converts packets"| node_can_frame
node_emulator -->|"routes by service"| node_service_base
node_service_impl -.->|"implements"| node_service_base
node_sample_app -.->|"configures"| node_service_impl
node_emulator -->|"sends responses"| node_communication

click node_serial "https://github.com/langroodi/obd-ii-emulator/blob/master/src/serial_communication.cpp"
click node_communication "https://github.com/langroodi/obd-ii-emulator/blob/master/src/communication_layer.cpp"
click node_send_buffer "https://github.com/langroodi/obd-ii-emulator/blob/master/src/packet_buffer.cpp"
click node_can_driver "https://github.com/langroodi/obd-ii-emulator/blob/master/src/can_driver.cpp"
click node_can_frame "https://github.com/langroodi/obd-ii-emulator/blob/master/src/can_frame.cpp"
click node_emulator "https://github.com/langroodi/obd-ii-emulator/blob/master/src/obd_emulator.cpp"
click node_service_base "https://github.com/langroodi/obd-ii-emulator/blob/master/src/obd_service.cpp"
click node_service_impl "https://github.com/langroodi/obd-ii-emulator/blob/master/samples/current_data_obd_service.cpp"
click node_sample_app "https://github.com/langroodi/obd-ii-emulator/blob/master/samples/obd_emulator_sample.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_serial,node_communication,node_send_buffer toneBlue
class node_can_driver,node_can_frame toneAmber
class node_emulator toneMint
class node_service_base,node_service_impl,node_sample_app toneRose
class node_scanner,node_analyzer toneIndigo
```

## Components

### Serial transport

- [Serial communication](https://github.com/langroodi/obd-ii-emulator/blob/master/src/serial_communication.cpp): `src/serial_communication.cpp`
- [Communication interface](https://github.com/langroodi/obd-ii-emulator/blob/master/src/communication_layer.cpp): `src/communication_layer.cpp`
- [Packet send buffer](https://github.com/langroodi/obd-ii-emulator/blob/master/src/packet_buffer.cpp): `src/packet_buffer.cpp`

### CAN framing

- [CAN packet driver](https://github.com/langroodi/obd-ii-emulator/blob/master/src/can_driver.cpp): `src/can_driver.cpp`
- [CAN frame](https://github.com/langroodi/obd-ii-emulator/blob/master/src/can_frame.cpp): `src/can_frame.cpp`

### OBD query handling

- [OBD emulator](https://github.com/langroodi/obd-ii-emulator/blob/master/src/obd_emulator.cpp): `src/obd_emulator.cpp`

### OBD services

- [OBD service contract](https://github.com/langroodi/obd-ii-emulator/blob/master/src/obd_service.cpp): `src/obd_service.cpp`
- [Current-data service](https://github.com/langroodi/obd-ii-emulator/blob/master/samples/current_data_obd_service.cpp): `samples/current_data_obd_service.cpp`
- [Sample application](https://github.com/langroodi/obd-ii-emulator/blob/master/samples/obd_emulator_sample.cpp): `samples/obd_emulator_sample.cpp`

### Other components

- **OBD-II scanner**
- **USB-CAN Analyzer**

## Connections

- OBD-II scanner → USB-CAN Analyzer: sends queries (evidence: `README.md`)
- USB-CAN Analyzer → Serial communication: serial packets (evidence: `README.md`)
- Serial communication → Communication interface: implements
- Serial communication → Packet send buffer: buffers sends (evidence: `src/serial_communication.cpp`)
- Communication interface → OBD emulator: dispatches received data
- OBD emulator → CAN packet driver: decodes and encodes (evidence: `src/obd_emulator.cpp`)
- CAN packet driver → CAN frame: converts packets (evidence: `README.md`)
- OBD emulator → OBD service contract: routes by service (evidence: `src/obd_emulator.cpp`)
- Current-data service → OBD service contract: implements
- Sample application → Current-data service: configures
- OBD emulator → Communication interface: sends responses (evidence: `src/obd_emulator.cpp`)
