|
libdoip
0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
|
This page describes how to build and run the example DoIP server included with the libdoip project, and how to customize its UDS behavior.
The example server application demonstrates a minimal DoIP server using the library's DoIPServer, DoIPConnection and DoIPServerModel types. The relevant example source files are located in the examples/ directory:
examples/exampleDoIPServer.cpp — program entry point and socket setup.examples/ExampleDoIPServerModel.h — example DoIPServerModel with ready-made UDS handlers and a small worker thread that simulates a downstream transport (e.g. CAN).The example shows how to:
uds::UdsMock test helper for local request handling.inc/DoIPServerModel.h — contains the DoIPServerModel callbacks that the application implements for connection-open, close and diagnostic message handling. Customize these callbacks to integrate your ECU logic.examples/ExampleDoIPServerModel.h — shows a concrete DoIPServerModel implementation used by the example server. UDS handlers are registered via uds::UdsMock and typed helpers such as registerReadDataByIdentifierHandler.examples/exampleDoIPServer.cpp — creates and configures a DoIPServer, sets logging level and starts listener threads that accept TCP connections.The DoIPServerModel struct (see inc/DoIPServerModel.h) exposes a few key callbacks your application implements. A concise summary:
onOpenConnection(IConnectionContext &ctx) — called when a new connection is established.onCloseConnection(IConnectionContext &ctx, DoIPCloseReason) — called during graceful/abrupt close.onDiagnosticMessage(IConnectionContext &ctx, const DoIPMessage &msg) — called for locally-handled diagnostic messages.onDownstreamRequest(IConnectionContext &ctx, const DoIPMessage &msg, ServerModelDownstreamResponseHandler cb) — called when the state machine wants to forward a diagnostic request to a downstream transport (e.g. CAN). The implementation should return DoIPDownstreamResult::Pending if it will respond asynchronously and call ctx.receiveDownstreamResponse() when the response arrives.Below is a minimal example implementation that forwards messages to a hypothetical CAN backend and forwards the response to the connection context.
The project uses CMake. From the repository root, run the following commands to build the library and the examples (recommended with a clean build directory):
On success the example binaries are available under build/examples/.
The example executable is exampleDoIPServer. Run it from the build directory, optionally enabling loopback mode (binds announcements to 127.0.0.1 instead of broadcast):
The program will:
DoIPServer (VIN, logical address, announce interval).The example registers default UDS services and a few typed handlers in ExampleDoIPServerModel:
0x10) — registered via m_uds.registerDiagnosticSessionControlHandler.0x11) — registered via m_uds.registerECUResetHandler.0x22) — registered via m_uds.registerReadDataByIdentifierHandler.0x3E) — registered via m_uds.registerTesterPresentHandler.These typed helpers convert a raw diagnostic ByteArray request into typed parameters (for example a DID or a session type) and forward the request to the provided callback. The callback returns a pair of uds::UdsResponseCode and a ByteArray payload. Implement your own handlers to perform ECU-specific logic and return appropriate responses.
Example: register a handler for ReadDataByIdentifier to respond with a VIN value for DID 0xF190:
The example uses uds::UdsMock to simulate downstream behavior. For a real ECU you should implement ServerModelDownstreamHandler in DoIPServerModel::onDownstreamRequest to forward diagnostic messages to the physical bus (e.g., CAN) and call ctx.receiveDownstreamResponse() when a response arrives. The state machine will handle timeouts and transitions for you.
Simple downstream handler sketch:
Below is a PlantUML diagram illustrating DoIPServer, DoIPConnection, DoIPServerModel and a downstream UDS/CAN backend interaction.
spdlog. Set the log level early in main:--loopback while testing locally to avoid network broadcast.exampleDoIPServer exits immediately with critical log about sockets:onDownstreamRequest is set and that the downstream transport calls back to IConnectionContext::receiveDownstreamResponse.If you generate documentation with Doxygen, the file doc/ExampleDoIPServer.md will be picked up automatically if DOC_DIR or the INPUT setting in Doxyfile includes the doc directory. Run:
After generation, look for the "Example DoIP Server Tutorial" page in the generated HTML documentation.
This repository and the example server follow the DoIP payload conventions used in the examples and tests. Important details to understand when implementing or testing downstream forwarding:
ISO 13400 describes DoIP message and payload types. The immediate ack behaviour is consistent with a transport-level acknowledgement: the server confirms reception on the DoIP link while the actual diagnostic response may be pending. If you require strict timing or synchronous behaviour, do not rely on the ack as an application-level confirmation — always await the downstream Diagnostic Message for the functional UDS response.
uds::UdsMock with a real UDS stack or backend for production.