libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
DoIPPayloadType.h
Go to the documentation of this file.
1 #ifndef DOIPPAYLOADTYPE_H
2 #define DOIPPAYLOADTYPE_H
3 
4 #include <stdint.h>
5 #include <iostream>
6 #include <iomanip>
7 #include <optional>
8 
9 namespace doip {
10 
11 
12 
13 /**
14  * @brief DoIP Payload Type identifiers according to ISO 13400-2
15  *
16  * This enumeration contains all payload types used in the DoIP protocol.
17  * The values correspond to the message types specified in ISO 13400-2.
18  */
19 enum class DoIPPayloadType : uint16_t {
20  /**
21  * @brief Generic Negative Acknowledgement (NACK)
22  * Sent when a message cannot be processed.
23  */
24  NegativeAck = 0x0000,
25 
26  /**
27  * @brief Vehicle Identification Request
28  * Request for vehicle identification.
29  */
31 
32  /**
33  * @brief Vehicle Identification Request with EID
34  * Request with EID (Entity Identifier).
35  */
37 
38  /**
39  * @brief Vehicle Identification Request with VIN
40  * Request with VIN (Vehicle Identification Number).
41  */
43 
44  /**
45  * @brief Vehicle Identification Response (Vehicle Announcement)
46  * Response to a Vehicle Identification Request or periodic announcement.
47  */
49 
50  /**
51  * @brief Routing Activation Request
52  * Request to activate routing for a diagnostic connection.
53  */
54  RoutingActivationRequest = 0x0005,
55 
56  /**
57  * @brief Routing Activation Response
58  * Response to a Routing Activation Request.
59  */
61 
62  /**
63  * @brief Alive Check Request
64  * Request to check if the connection is still alive.
65  */
66  AliveCheckRequest = 0x0007,
67 
68  /**
69  * @brief Alive Check Response
70  * Response to an Alive Check Request.
71  */
72  AliveCheckResponse = 0x0008,
73 
74  // 0x0009 - 0x4000: reserved
75 
76  /**
77  * @brief Entity Status Request
78  * Request for the status of a DoIP entity.
79  */
80  EntityStatusRequest = 0x4001,
81 
82  /**
83  * @brief Entity Status Response
84  * Response to an Entity Status Request.
85  */
86  EntityStatusResponse = 0x4002,
87 
88  /**
89  * @brief Diagnostic Power Mode Request
90  * Request for the current power mode.
91  */
93 
94  /**
95  * @brief Diagnostic Power Mode Response
96  * Response to a Diagnostic Power Mode Request.
97  */
99 
100  // 0x4005 - 0x8000: reserved
101 
102  /**
103  * @brief Diagnostic Message
104  * Payload for diagnostic communication (UDS, KWP, etc.).
105  */
106  DiagnosticMessage = 0x8001,
107 
108  /**
109  * @brief Diagnostic Message Positive Acknowledgement
110  * Positive acknowledgement of a diagnostic message.
111  */
112  DiagnosticMessageAck = 0x8002,
113 
114  /**
115  * @brief Diagnostic Message Negative Acknowledgement
116  * Negative acknowledgement of a diagnostic message.
117  */
119 
120  /**
121  * @brief Periodic Diagnostic Message
122  * Periodically sent diagnostic message.
123  */
125  // 0x8005 - 0x8FFF : reserved
126  // 0x9000 - 0x9FFF : subnet protocol
127  // 0xA000 - 0xEFFF : reserved
128  // 0xF000 - 0xFFFF : reserved for manufacturer
129 };
130 
131 /**
132  * @brief Validates if a uint16_t value represents a valid DoIPPayloadType
133  * @param value The value to validate
134  * @return true if valid, false otherwise
135  */
136 constexpr bool isValidPayloadType(uint16_t value) noexcept {
137  switch (value) {
138  case 0x0000: // NegativeAck
139  case 0x0001: // VehicleIdentificationRequest
140  case 0x0002: // VehicleIdentificationRequestWithEid
141  case 0x0003: // VehicleIdentificationRequestWithVin
142  case 0x0004: // VehicleIdentificationResponse
143  case 0x0005: // RoutingActivationRequest
144  case 0x0006: // RoutingActivationResponse
145  case 0x0007: // AliveCheckRequest
146  case 0x0008: // AliveCheckResponse
147  case 0x4001: // EntityStatusRequest
148  case 0x4002: // EntityStatusResponse
149  case 0x4003: // DiagnosticPowerModeRequest
150  case 0x4004: // DiagnosticPowerModeResponse
151  case 0x8001: // DiagnosticMessage
152  case 0x8002: // DiagnosticMessageAck
153  case 0x8003: // DiagnosticMessageNegativeAck
154  case 0x8004: // PeriodicDiagnosticMessage
155  return true;
156  default:
157  return false;
158  }
159 }
160 
161 /**
162  * @brief Safely converts uint16_t to DoIPPayloadType with validation
163  * @param value The value to convert
164  * @return Optional containing the payload type if valid, nullopt otherwise
165  */
166 constexpr std::optional<DoIPPayloadType> toPayloadType(uint16_t value) noexcept {
167  if (isValidPayloadType(value)) {
168  return static_cast<DoIPPayloadType>(value);
169  }
170  return std::nullopt;
171 }
172 
173 constexpr std::optional<DoIPPayloadType> toPayloadType(uint8_t hsb, uint8_t lsb) noexcept {
174  return toPayloadType(static_cast<uint16_t>((hsb << 8) | lsb));
175 }
176 
177 /**
178  * @brief Stream operator for DoIPPayloadType enum
179  *
180  * Prints the payload type name and its hex value.
181  * Example: "DiagnosticMessage (0x8001)"
182  *
183  * @param os Output stream
184  * @param type Payload type to print
185  * @return std::ostream& Reference to the output stream
186  */
187 inline std::ostream& operator<<(std::ostream& os, DoIPPayloadType type) {
188  const char* name = nullptr;
189 
190  switch (type) {
192  name = "NegativeAck";
193  break;
195  name = "VehicleIdentificationRequest";
196  break;
198  name = "VehicleIdentificationRequestWithEid";
199  break;
201  name = "VehicleIdentificationRequestWithVin";
202  break;
204  name = "VehicleIdentificationResponse";
205  break;
207  name = "RoutingActivationRequest";
208  break;
210  name = "RoutingActivationResponse";
211  break;
213  name = "AliveCheckRequest";
214  break;
216  name = "AliveCheckResponse";
217  break;
219  name = "EntityStatusRequest";
220  break;
222  name = "EntityStatusResponse";
223  break;
225  name = "DiagnosticPowerModeRequest";
226  break;
228  name = "DiagnosticPowerModeResponse";
229  break;
231  name = "DiagnosticMessage";
232  break;
234  name = "DiagnosticMessageAck";
235  break;
237  name = "DiagnosticMessageNegativeAck";
238  break;
240  name = "PeriodicDiagnosticMessage";
241  break;
242  default:
243  name = "Unknown";
244  break;
245  }
246 
247  os << name << " (0x" << std::hex << std::uppercase << std::setw(4) << std::setfill('0')
248  << static_cast<uint16_t>(type) << std::dec << ")";
249 
250  return os;
251 }
252 
253 } // namespace doip
254 
255 #endif /* DOIPPAYLOADTYPE_H */
Definition: AnsiColors.h:3
constexpr bool isValidPayloadType(uint16_t value) noexcept
Validates if a uint16_t value represents a valid DoIPPayloadType.
std::ostream & operator<<(std::ostream &os, const ByteArray &arr)
Stream operator for ByteArray.
Definition: ByteArray.h:303
DoIPPayloadType
DoIP Payload Type identifiers according to ISO 13400-2.
@ DiagnosticMessageAck
Diagnostic Message Positive Acknowledgement Positive acknowledgement of a diagnostic message.
@ DiagnosticMessage
Diagnostic Message Payload for diagnostic communication (UDS, KWP, etc.).
@ PeriodicDiagnosticMessage
Periodic Diagnostic Message Periodically sent diagnostic message.
@ AliveCheckResponse
Alive Check Response Response to an Alive Check Request.
@ RoutingActivationResponse
Routing Activation Response Response to a Routing Activation Request.
@ DiagnosticPowerModeRequest
Diagnostic Power Mode Request Request for the current power mode.
@ VehicleIdentificationRequestWithVin
Vehicle Identification Request with VIN Request with VIN (Vehicle Identification Number).
@ DiagnosticPowerModeResponse
Diagnostic Power Mode Response Response to a Diagnostic Power Mode Request.
@ RoutingActivationRequest
Routing Activation Request Request to activate routing for a diagnostic connection.
@ EntityStatusRequest
Entity Status Request Request for the status of a DoIP entity.
@ VehicleIdentificationRequestWithEid
Vehicle Identification Request with EID Request with EID (Entity Identifier).
@ VehicleIdentificationResponse
Vehicle Identification Response (Vehicle Announcement) Response to a Vehicle Identification Request o...
@ VehicleIdentificationRequest
Vehicle Identification Request Request for vehicle identification.
@ NegativeAck
Generic Negative Acknowledgement (NACK) Sent when a message cannot be processed.
@ DiagnosticMessageNegativeAck
Diagnostic Message Negative Acknowledgement Negative acknowledgement of a diagnostic message.
@ AliveCheckRequest
Alive Check Request Request to check if the connection is still alive.
@ EntityStatusResponse
Entity Status Response Response to an Entity Status Request.
constexpr std::optional< DoIPPayloadType > toPayloadType(uint16_t value) noexcept
Safely converts uint16_t to DoIPPayloadType with validation.