libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
DoIPServerModel.h
Go to the documentation of this file.
1 #ifndef DOIPSERVERMODEL_H
2 #define DOIPSERVERMODEL_H
3 
4 #include <functional>
5 
6 #include "DoIPAddress.h"
7 #include "DoIPCloseReason.h"
8 #include "DoIPDownstreamResult.h"
9 #include "DoIPMessage.h"
11 #include "DoIPServerEvent.h"
12 #include "DoIPServerState.h"
13 #include "Logger.h"
14 
15 namespace doip {
16 
17 // Forward declaration
18 class IConnectionContext;
19 
20 // Callback type definitions
21 using ServerModelOpenHandler = std::function<void(IConnectionContext &)>;
25 
26 /**
27  * @brief Callback for downstream response notification
28  *
29  * @param response the downstream response (maybe empty)
30  * @param result the downstream result
31  */
32 using ServerModelDownstreamResponseHandler = std::function<void(const ByteArray &response, DoIPDownstreamResult result)>;
33 
34 /**
35  * @brief Callback for downstream (subnet) request handling
36  *
37  * This callback is invoked when a diagnostic message needs to be forwarded
38  * to a downstream device (e.g., via CAN, LIN, or another transport).
39  *
40  * The implementation is responsible for:
41  * 1. Sending the diagnostic message to the appropriate downstream device
42  * 2. When a response is received, calling ctx.receiveDownstreamResponse(response)
43  *
44  * The state machine will handle timeout management internally.
45  *
46  * @param ctx The connection context (use for receiveDownstreamResponse callback)
47  * @param msg The diagnostic message to forward downstream
48  * @param callback the callback method to call when the downstream response arrived
49  * @return DoIPDownstreamResult indicating the result of the request initiation
50  * - Pending: Async request initiated, response will come via receiveDownstreamResponse
51  * - Handled: Message was handled synchronously, no state transition needed
52  * - Error: Failed to initiate request, connection should handle error
53  */
55  const DoIPMessage &msg,
57 
58 /**
59  * @brief DoIP Server Model - Configuration and callbacks for a DoIP server connection
60  *
61  * This struct holds all application-specific configuration and callbacks
62  * that customize the behavior of a DoIP server connection. It separates
63  * the protocol logic (in DoIPServerStateMachine) from the application logic.
64  */
66  /// Called when the connection is being opened
68 
69  /// Called when the connection is being closed
71 
72  /// Called when a diagnostic message is received (for local handling)
74 
75  /// Called after a diagnostic ACK/NACK was sent to the client
77 
78  // === Downstream (subnet) callbacks ===
79 
80  /**
81  * @brief Called when a diagnostic message should be forwarded downstream
82  *
83  * If this callback is set (not nullptr), the state machine will use it
84  * to forward diagnostic messages to downstream devices. If not set,
85  * messages are handled locally via onDiagnosticMessage.
86  *
87  * Typical usage:
88  * - Set this for gateway ECUs that forward to CAN/LIN subnets
89  * - Leave unset for end-node ECUs that handle messages locally
90  */
92 
93  /// The logical address of this DoIP server
95 
96  /**
97  * @brief Check if downstream forwarding is enabled
98  * @return true if onDownstreamRequest callback is set
99  */
100  bool hasDownstreamHandler() const { return onDownstreamRequest != nullptr; }
101 };
102 
103 using UniqueServerModelPtr = std::unique_ptr<DoIPServerModel>;
104 
105 /**
106  * @brief Default DoIP Server Model with no-op callbacks
107  *
108  * This is used as a fallback when no model is configured. All callbacks
109  * log warnings and perform no-op actions. Should only be used for testing
110  * or as a placeholder during initialization.
111  */
114  onOpenConnection = [](IConnectionContext &ctx) noexcept {
115  (void)ctx;
116  };
117 
118  onCloseConnection = [](IConnectionContext &ctx, DoIPCloseReason reason) noexcept {
119  (void)ctx;
120  (void)reason;
121  };
122 
123  onDiagnosticMessage = [](IConnectionContext &ctx, const DoIPMessage &msg) noexcept -> DoIPDiagnosticAck {
124  (void)ctx;
125  (void)msg;
126  LOG_DOIP_DEBUG("Diagnostic message received on DefaultDoIPServerModel");
127  // Default: always ACK
128  return std::nullopt;
129  };
130 
132  (void)ctx;
133  (void)ack;
134  LOG_DOIP_DEBUG("Diagnostic notification on DefaultDoIPServerModel");
135  // Default no-op
136  };
137 
138  // Note: onDownstreamRequest is intentionally left as nullptr
139  // This means downstream forwarding is disabled by default
140  onDownstreamRequest = nullptr;
141  }
142 
144  }
145 };
146 
147 } // namespace doip
148 
149 #endif /* DOIPSERVERMODEL_H */
#define LOG_DOIP_DEBUG(...)
Definition: Logger.h:125
Represents a complete DoIP message with internal ByteArray representation.
Definition: DoIPMessage.h:82
Interface between DoIPServerStateMachine and DoIPConnection.
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::function< void(IConnectionContext &, DoIPDiagnosticAck)> ServerModelDiagnosticNotificationHandler
std::optional< DoIPNegativeDiagnosticAck > DoIPDiagnosticAck
Alias for diagnostic acknowledgment type.
std::function< void(const ByteArray &response, DoIPDownstreamResult result)> ServerModelDownstreamResponseHandler
Callback for downstream response notification.
DoIPDownstreamResult
Result of a downstream request initiation.
std::function< DoIPDownstreamResult(IConnectionContext &ctx, const DoIPMessage &msg, ServerModelDownstreamResponseHandler callback)> ServerModelDownstreamHandler
Callback for downstream (subnet) request handling.
DoIPCloseReason
Reason for connection closure.
std::unique_ptr< DoIPServerModel > UniqueServerModelPtr
std::function< void(IConnectionContext &, DoIPCloseReason)> ServerModelCloseHandler
std::function< void(IConnectionContext &)> ServerModelOpenHandler
std::function< DoIPDiagnosticAck(IConnectionContext &, const DoIPMessage &)> ServerModelDiagnosticHandler
A dynamic array of bytes with utility methods for network protocol handling.
Definition: ByteArray.h:60
Default DoIP Server Model with no-op callbacks.
DoIP Server Model - Configuration and callbacks for a DoIP server connection.
ServerModelDiagnosticHandler onDiagnosticMessage
Called when a diagnostic message is received (for local handling)
bool hasDownstreamHandler() const
Check if downstream forwarding is enabled.
DoIPAddress serverAddress
The logical address of this DoIP server.
ServerModelDownstreamHandler onDownstreamRequest
Called when a diagnostic message should be forwarded downstream.
ServerModelOpenHandler onOpenConnection
Called when the connection is being opened.
ServerModelDiagnosticNotificationHandler onDiagnosticNotification
Called after a diagnostic ACK/NACK was sent to the client.
ServerModelCloseHandler onCloseConnection
Called when the connection is being closed.