libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
IConnectionContext.h
Go to the documentation of this file.
1 #ifndef ICONNECTIONCONTEXT_H
2 #define ICONNECTIONCONTEXT_H
3 
4 #include "DoIPAddress.h"
5 #include "DoIPCloseReason.h"
6 #include "DoIPDownstreamResult.h"
7 #include "DoIPMessage.h"
9 
10 #include <cstdint>
11 
12 namespace doip {
13 
14 /**
15  * @brief Interface between DoIPServerStateMachine and DoIPConnection
16  *
17  * This interface inverts the dependency between the state machine and connection.
18  * Instead of the state machine holding callbacks to the connection, the connection
19  * implements this interface and passes itself to the state machine.
20  *
21  * Responsibilities:
22  * - Bridge between protocol layer (state machine) and application layer (user callbacks)
23  * - Provide connection-level operations (send, close)
24  * - Provide configuration queries (addresses)
25  * - Handle downstream (subnet) communication flow
26  */
28  public:
29  virtual ~IConnectionContext() = default;
30 
31  /**
32  * @brief Send a DoIP protocol message to the client
33  *
34  * This is called by the state machine when it needs to send a protocol
35  * message (ACK, NACK, alive check, routing activation response, etc.)
36  *
37  * @param msg The DoIP message to send
38  * @return Number of bytes sent, or negative value on error
39  */
40  [[nodiscard]] virtual ssize_t sendProtocolMessage(const DoIPMessage &msg) = 0;
41 
42  /**
43  * @brief Close the TCP connection
44  *
45  * This is called by the state machine when the connection should be closed
46  * (timeout, invalid message, etc.)
47  *
48  * @param reason Why the connection is being closed
49  */
50  virtual void closeConnection(DoIPCloseReason reason) = 0;
51 
52  /**
53  * @brief Check if the connection is currently open
54  *
55  * @return true if the connection is open, false otherwise
56  */
57  virtual bool isOpen() const = 0;
58 
59  /**
60  * @brief Get the reason why the connection was closed
61  *
62  * @return The DoIPCloseReason for the closed connection
63  */
64  virtual DoIPCloseReason getCloseReason() const = 0;
65 
66  /**
67  * @brief Get the server's logical address
68  *
69  * Used by state machine for constructing response messages
70  *
71  * @return The server's DoIP logical address
72  */
73  virtual DoIPAddress getServerAddress() const = 0;
74 
75  /**
76  * @brief Get the currently active source address (routed client)
77  *
78  * After routing activation, this returns the client's source address
79  *
80  * @return The active source address, or 0 if no routing is active
81  */
82  virtual DoIPAddress getClientAddress() const = 0;
83 
84  /**
85  * @brief Set the active source address after routing activation
86  *
87  * Called by state machine when routing activation is successful
88  *
89  * @param address The client's source address
90  */
91  virtual void setClientAddress(const DoIPAddress &address) = 0;
92 
93  /**
94  * @brief Handle an incoming diagnostic message (application callback)
95  *
96  * This forwards the diagnostic message to the application layer for processing.
97  * The application returns either:
98  * - std::nullopt: Send positive ACK
99  * - DoIPNegativeDiagnosticAck value: Send negative ACK with this code
100  *
101  * @param msg The diagnostic message received
102  * @return std::nullopt for ACK, or NACK code
103  */
105 
106  /**
107  * @brief Notify application that connection is closing
108  *
109  * This allows the application to perform cleanup when the connection
110  * is closed (by timeout, error, or graceful shutdown)
111  *
112  * @param reason Why the connection is closing
113  */
114  virtual void notifyConnectionClosed(DoIPCloseReason reason) = 0;
115 
116  /**
117  * @brief Notify application that diagnostic ACK/NACK was sent
118  *
119  * Optional notification after sending diagnostic message acknowledgement
120  *
121  * @param ack The ACK/NACK that was sent (nullopt = positive ACK)
122  */
124 
125  /**
126  * @brief Check if downstream forwarding is available
127  *
128  * This checks if the DoIPServerModel has a downstream handler configured.
129  * The state machine uses this to decide whether to forward messages
130  * downstream or handle them locally.
131  *
132  * @return true if downstream forwarding is configured, false otherwise
133  */
134  virtual bool hasDownstreamHandler() const = 0;
135 
136  /**
137  * @brief Forward a diagnostic message to downstream (subnet device)
138  *
139  * Called by the state machine when a diagnostic message should be routed
140  * to a downstream device (e.g., ECU on CAN bus). The implementation
141  * delegates to DoIPServerModel::onDownstreamRequest.
142  *
143  * The downstream handler is responsible for:
144  * 1. Sending the message via the appropriate transport (CAN, LIN, etc.)
145  * 2. Storing the connection context for async response delivery
146  * 3. Calling receiveDownstreamResponse() when the response arrives
147  *
148  * The state machine handles:
149  * - Transition to WaitDownstreamResponse state
150  * - Starting the downstream response timeout timer
151  * - Processing the response when it arrives
152  *
153  * @param msg The diagnostic message to forward
154  * @return Result indicating if the request was initiated successfully
155  */
157 
158  /**
159  * @brief Receive a response from downstream device
160  *
161  * Called by the application layer when a response is received from
162  * a downstream device. This injects the response back into the
163  * state machine for processing.
164  *
165  * Thread-safety: This method may be called from a different thread
166  * than the one running the state machine. Implementations should
167  * ensure thread-safe access to the state machine.
168  *
169  * Typical call sequence:
170  * 1. State machine calls notifyDownstreamRequest(request)
171  * 2. Application sends request via CAN/LIN/etc.
172  * 3. Application receives response from downstream
173  * 4. Application calls receiveDownstreamResponse(response)
174  * 5. State machine processes response and sends to client
175  *
176  * @param response The diagnostic response from downstream
177  */
178  virtual void receiveDownstreamResponse(const ByteArray &response, DoIPDownstreamResult result) = 0;
179 };
180 
181 } // namespace doip
182 
183 #endif /* ICONNECTIONCONTEXT_H */
Represents a complete DoIP message with internal ByteArray representation.
Definition: DoIPMessage.h:82
Interface between DoIPServerStateMachine and DoIPConnection.
virtual DoIPCloseReason getCloseReason() const =0
Get the reason why the connection was closed.
virtual DoIPDownstreamResult notifyDownstreamRequest(const DoIPMessage &msg)=0
Forward a diagnostic message to downstream (subnet device)
virtual void notifyConnectionClosed(DoIPCloseReason reason)=0
Notify application that connection is closing.
virtual void notifyDiagnosticAckSent(DoIPDiagnosticAck ack)=0
Notify application that diagnostic ACK/NACK was sent.
virtual DoIPDiagnosticAck notifyDiagnosticMessage(const DoIPMessage &msg)=0
Handle an incoming diagnostic message (application callback)
virtual void setClientAddress(const DoIPAddress &address)=0
Set the active source address after routing activation.
virtual ssize_t sendProtocolMessage(const DoIPMessage &msg)=0
Send a DoIP protocol message to the client.
virtual bool isOpen() const =0
Check if the connection is currently open.
virtual ~IConnectionContext()=default
virtual bool hasDownstreamHandler() const =0
Check if downstream forwarding is available.
virtual void receiveDownstreamResponse(const ByteArray &response, DoIPDownstreamResult result)=0
Receive a response from downstream device.
virtual DoIPAddress getClientAddress() const =0
Get the currently active source address (routed client)
virtual DoIPAddress getServerAddress() const =0
Get the server's logical address.
virtual void closeConnection(DoIPCloseReason reason)=0
Close the TCP connection.
Definition: AnsiColors.h:3
uint16_t DoIPAddress
Represents a 16-bit DoIP address consisting of high and low significant bytes.
Definition: DoIPAddress.h:26
std::optional< DoIPNegativeDiagnosticAck > DoIPDiagnosticAck
Alias for diagnostic acknowledgment type.
DoIPDownstreamResult
Result of a downstream request initiation.
DoIPCloseReason
Reason for connection closure.
A dynamic array of bytes with utility methods for network protocol handling.
Definition: ByteArray.h:60