libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
doip::IConnectionContext Class Referenceabstract

Interface between DoIPServerStateMachine and DoIPConnection. More...

#include <IConnectionContext.h>

Inheritance diagram for doip::IConnectionContext:

Public Member Functions

virtual ~IConnectionContext ()=default
 
virtual ssize_t sendProtocolMessage (const DoIPMessage &msg)=0
 Send a DoIP protocol message to the client. More...
 
virtual void closeConnection (DoIPCloseReason reason)=0
 Close the TCP connection. More...
 
virtual bool isOpen () const =0
 Check if the connection is currently open. More...
 
virtual DoIPCloseReason getCloseReason () const =0
 Get the reason why the connection was closed. More...
 
virtual DoIPAddress getServerAddress () const =0
 Get the server's logical address. More...
 
virtual DoIPAddress getClientAddress () const =0
 Get the currently active source address (routed client) More...
 
virtual void setClientAddress (const DoIPAddress &address)=0
 Set the active source address after routing activation. More...
 
virtual DoIPDiagnosticAck notifyDiagnosticMessage (const DoIPMessage &msg)=0
 Handle an incoming diagnostic message (application callback) More...
 
virtual void notifyConnectionClosed (DoIPCloseReason reason)=0
 Notify application that connection is closing. More...
 
virtual void notifyDiagnosticAckSent (DoIPDiagnosticAck ack)=0
 Notify application that diagnostic ACK/NACK was sent. More...
 
virtual bool hasDownstreamHandler () const =0
 Check if downstream forwarding is available. More...
 
virtual DoIPDownstreamResult notifyDownstreamRequest (const DoIPMessage &msg)=0
 Forward a diagnostic message to downstream (subnet device) More...
 
virtual void receiveDownstreamResponse (const ByteArray &response, DoIPDownstreamResult result)=0
 Receive a response from downstream device. More...
 

Detailed Description

Interface between DoIPServerStateMachine and DoIPConnection.

This interface inverts the dependency between the state machine and connection. Instead of the state machine holding callbacks to the connection, the connection implements this interface and passes itself to the state machine.

Responsibilities:

  • Bridge between protocol layer (state machine) and application layer (user callbacks)
  • Provide connection-level operations (send, close)
  • Provide configuration queries (addresses)
  • Handle downstream (subnet) communication flow

Definition at line 27 of file IConnectionContext.h.

Constructor & Destructor Documentation

◆ ~IConnectionContext()

virtual doip::IConnectionContext::~IConnectionContext ( )
virtualdefault

Member Function Documentation

◆ closeConnection()

virtual void doip::IConnectionContext::closeConnection ( DoIPCloseReason  reason)
pure virtual

Close the TCP connection.

This is called by the state machine when the connection should be closed (timeout, invalid message, etc.)

Parameters
reasonWhy the connection is being closed

Implemented in doip::DoIPDefaultConnection, and doip::DoIPConnection.

◆ getClientAddress()

virtual DoIPAddress doip::IConnectionContext::getClientAddress ( ) const
pure virtual

Get the currently active source address (routed client)

After routing activation, this returns the client's source address

Returns
The active source address, or 0 if no routing is active

Implemented in doip::DoIPDefaultConnection, and doip::DoIPConnection.

◆ getCloseReason()

virtual DoIPCloseReason doip::IConnectionContext::getCloseReason ( ) const
pure virtual

Get the reason why the connection was closed.

Returns
The DoIPCloseReason for the closed connection

Implemented in doip::DoIPDefaultConnection.

◆ getServerAddress()

virtual DoIPAddress doip::IConnectionContext::getServerAddress ( ) const
pure virtual

Get the server's logical address.

Used by state machine for constructing response messages

Returns
The server's DoIP logical address

Implemented in doip::DoIPDefaultConnection, and doip::DoIPConnection.

◆ hasDownstreamHandler()

virtual bool doip::IConnectionContext::hasDownstreamHandler ( ) const
pure virtual

Check if downstream forwarding is available.

This checks if the DoIPServerModel has a downstream handler configured. The state machine uses this to decide whether to forward messages downstream or handle them locally.

Returns
true if downstream forwarding is configured, false otherwise

Implemented in doip::DoIPDefaultConnection, and doip::DoIPConnection.

◆ isOpen()

virtual bool doip::IConnectionContext::isOpen ( ) const
pure virtual

Check if the connection is currently open.

Returns
true if the connection is open, false otherwise

Implemented in doip::DoIPDefaultConnection.

◆ notifyConnectionClosed()

virtual void doip::IConnectionContext::notifyConnectionClosed ( DoIPCloseReason  reason)
pure virtual

Notify application that connection is closing.

This allows the application to perform cleanup when the connection is closed (by timeout, error, or graceful shutdown)

Parameters
reasonWhy the connection is closing

Implemented in doip::DoIPDefaultConnection, and doip::DoIPConnection.

◆ notifyDiagnosticAckSent()

virtual void doip::IConnectionContext::notifyDiagnosticAckSent ( DoIPDiagnosticAck  ack)
pure virtual

Notify application that diagnostic ACK/NACK was sent.

Optional notification after sending diagnostic message acknowledgement

Parameters
ackThe ACK/NACK that was sent (nullopt = positive ACK)

Implemented in doip::DoIPDefaultConnection, and doip::DoIPConnection.

◆ notifyDiagnosticMessage()

virtual DoIPDiagnosticAck doip::IConnectionContext::notifyDiagnosticMessage ( const DoIPMessage msg)
pure virtual

Handle an incoming diagnostic message (application callback)

This forwards the diagnostic message to the application layer for processing. The application returns either:

  • std::nullopt: Send positive ACK
  • DoIPNegativeDiagnosticAck value: Send negative ACK with this code
Parameters
msgThe diagnostic message received
Returns
std::nullopt for ACK, or NACK code

Implemented in doip::DoIPDefaultConnection, and doip::DoIPConnection.

◆ notifyDownstreamRequest()

virtual DoIPDownstreamResult doip::IConnectionContext::notifyDownstreamRequest ( const DoIPMessage msg)
pure virtual

Forward a diagnostic message to downstream (subnet device)

Called by the state machine when a diagnostic message should be routed to a downstream device (e.g., ECU on CAN bus). The implementation delegates to DoIPServerModel::onDownstreamRequest.

The downstream handler is responsible for:

  1. Sending the message via the appropriate transport (CAN, LIN, etc.)
  2. Storing the connection context for async response delivery
  3. Calling receiveDownstreamResponse() when the response arrives

The state machine handles:

  • Transition to WaitDownstreamResponse state
  • Starting the downstream response timeout timer
  • Processing the response when it arrives
Parameters
msgThe diagnostic message to forward
Returns
Result indicating if the request was initiated successfully

Implemented in doip::DoIPDefaultConnection.

◆ receiveDownstreamResponse()

virtual void doip::IConnectionContext::receiveDownstreamResponse ( const ByteArray response,
DoIPDownstreamResult  result 
)
pure virtual

Receive a response from downstream device.

Called by the application layer when a response is received from a downstream device. This injects the response back into the state machine for processing.

Thread-safety: This method may be called from a different thread than the one running the state machine. Implementations should ensure thread-safe access to the state machine.

Typical call sequence:

  1. State machine calls notifyDownstreamRequest(request)
  2. Application sends request via CAN/LIN/etc.
  3. Application receives response from downstream
  4. Application calls receiveDownstreamResponse(response)
  5. State machine processes response and sends to client
Parameters
responseThe diagnostic response from downstream

Implemented in doip::DoIPDefaultConnection.

◆ sendProtocolMessage()

virtual ssize_t doip::IConnectionContext::sendProtocolMessage ( const DoIPMessage msg)
pure virtual

Send a DoIP protocol message to the client.

This is called by the state machine when it needs to send a protocol message (ACK, NACK, alive check, routing activation response, etc.)

Parameters
msgThe DoIP message to send
Returns
Number of bytes sent, or negative value on error

Implemented in doip::DoIPDefaultConnection, and doip::DoIPConnection.

◆ setClientAddress()

virtual void doip::IConnectionContext::setClientAddress ( const DoIPAddress address)
pure virtual

Set the active source address after routing activation.

Called by state machine when routing activation is successful

Parameters
addressThe client's source address

Implemented in doip::DoIPConnection.


The documentation for this class was generated from the following file: