Coverage Report

Created: 2026-12-01 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/header/DoIpLayer.h
Line
Count
Source
1
#pragma once
2
3
#include <algorithm>
4
#include <cstring>
5
#include <iterator>
6
#include <vector>
7
#include "Layer.h"
8
#include "Logger.h"
9
10
/// @file
11
12
/// @namespace pcpp
13
/// @brief The main namespace for the PcapPlusPlus lib
14
namespace pcpp
15
{
16
/// @struct doiphdr
17
/// Represents an DoIP protocol header
18
#pragma pack(push, 1)
19
  struct doiphdr
20
  {
21
    /// DoIP version (DOIPV)
22
    uint8_t protocolVersion;
23
    /// DoIP invert version (DOIPIV). Inverse of protocol version
24
    uint8_t invertProtocolVersion;
25
    /// DoIP payload type (DOIPT)
26
    uint16_t payloadType;
27
    /// DoIP content payload length (DOIPL)
28
    uint32_t payloadLength;
29
  };
30
#pragma pack(pop)
31
  static_assert(sizeof(doiphdr) == 8, "DoIP header must be exactly 8 bytes.");
32
33
  namespace DoIpConstants
34
  {
35
    /// @brief Length of doip header
36
    static constexpr size_t DOIP_HEADER_LEN = sizeof(doiphdr);
37
38
    /// @brief Length of the Equiepement Identifier (EID) field.
39
    static constexpr size_t DOIP_EID_LEN = 6;
40
41
    /// @brief Length of the Group Identifier (GID) field.
42
    static constexpr size_t DOIP_GID_LEN = 6;
43
44
    /// @brief Length of the Vehicle Identification Number (VIN) field.
45
    static constexpr size_t DOIP_VIN_LEN = 17;
46
47
    /// @brief Length of the Reserved ISO field.
48
    static constexpr size_t DOIP_RESERVED_ISO_LEN = 4;
49
50
    /// @brief Length of the Reserved OEM field.
51
    static constexpr size_t DOIP_RESERVED_OEM_LEN = 4;
52
53
    /// @brief Length of the source address
54
    static constexpr size_t DOIP_SOURCE_ADDRESS_LEN = 2;
55
56
    /// @brief Length of the target address
57
    static constexpr size_t DOIP_TARGET_ADDRESS_LEN = 2;
58
  }  // namespace DoIpConstants
59
60
  /// @brief Enum representing DoIP routing activation types.
61
  /// These values specify the type of routing activation used in DoIP(Diagnostic over IP).
62
  enum class DoIpActivationTypes : uint8_t
63
  {
64
    /// Default routing activation type.
65
    /// Used when no specific type is required.
66
    DEFAULT = 0x00U,
67
68
    /// WWH-OBD (Worldwide Harmonized On-Board Diagnostics) routing activation type.
69
    /// Used for vehicle diagnostics in compliance with WWH-OBD standards.
70
    WWH_OBD = 0x01U,
71
72
    /// Central security routing activation type.
73
    /// Used for secure communications involving a central security system.
74
    CENTRAL_SECURITY = 0xE0U,
75
76
    /// Unknown routing activation type.
77
    /// This value is used when the routing activation type is not specified or recognized.
78
    UNKNOWN
79
  };
80
81
  /// @brief Enum representing DoIP Generic Header NACK codes (ISO 13400).
82
  /// These codes are used to indicate specific errors in the DoIP Generic Header.
83
  enum class DoIpGenericHeaderNackCodes : uint8_t
84
  {
85
    /// Incorrect pattern detected in the header.
86
    /// Indicates that the header does not follow the expected pattern.
87
    INCORRECT_PATTERN = 0x00U,
88
89
    /// Unknown payload type.
90
    /// The payload type in the message is not recognized.
91
    UNKNOWN_PAYLOAD_TYPE = 0x01U,
92
93
    /// Message too large.
94
    /// The message size exceeds the allowed limit.
95
    MESSAGE_TOO_LARGE = 0x02U,
96
97
    /// Out of memory.
98
    /// There is insufficient memory available to process the message.
99
    OUT_OF_MEMORY = 0x03U,
100
101
    /// Invalid payload length.
102
    /// The payload length specified in the header is invalid.
103
    INVALID_PAYLOAD_LENGTH = 0x04U,
104
105
    /// UNKNOWN.
106
    /// Indicates that the NACK code is not recognized or supported.
107
    UNKNOWN
108
  };
109
110
  /// @brief Enum representing DoIP action codes for DoIP announcement messages (ISO 13400).
111
  /// These action codes specify the next steps required after receiving a DoIP announcement message.
112
  /// Some codes are reserved for future use by ISO standards.
113
  enum class DoIpActionCodes : uint8_t
114
  {
115
    /// No further action required.
116
    /// Indicates that no additional steps are needed after the announcement.
117
    NO_FURTHER_ACTION_REQUIRED = 0x00U,
118
119
    /// Reserved for ISO (0x01).
120
    /// Reserved for future use as per ISO standards.
121
    RESERVED_ISO_0x01 = 0x01U,
122
123
    /// Reserved for ISO (0x02).
124
    /// Reserved for future use as per ISO standards.
125
    RESERVED_ISO_0x02 = 0x02U,
126
127
    /// Reserved for ISO (0x03).
128
    /// Reserved for future use as per ISO standards.
129
    RESERVED_ISO_0x03 = 0x03U,
130
131
    /// Reserved for ISO (0x04).
132
    /// Reserved for future use as per ISO standards.
133
    RESERVED_ISO_0x04 = 0x04U,
134
135
    /// Reserved for ISO (0x05).
136
    /// Reserved for future use as per ISO standards.
137
    RESERVED_ISO_0x05 = 0x05U,
138
139
    /// Reserved for ISO (0x06).
140
    /// Reserved for future use as per ISO standards.
141
    RESERVED_ISO_0x06 = 0x06U,
142
143
    /// Reserved for ISO (0x07).
144
    /// Reserved for future use as per ISO standards.
145
    RESERVED_ISO_0x07 = 0x07U,
146
147
    /// Reserved for ISO (0x08).
148
    /// Reserved for future use as per ISO standards.
149
    RESERVED_ISO_0x08 = 0x08U,
150
151
    /// Reserved for ISO (0x09).
152
    /// Reserved for future use as per ISO standards.
153
    RESERVED_ISO_0x09 = 0x09U,
154
155
    /// Reserved for ISO (0x0A).
156
    /// Reserved for future use as per ISO standards.
157
    RESERVED_ISO_0x0A = 0x0AU,
158
159
    /// Reserved for ISO (0x0B).
160
    /// Reserved for future use as per ISO standards.
161
    RESERVED_ISO_0x0B = 0x0BU,
162
163
    /// Reserved for ISO (0x0C).
164
    /// Reserved for future use as per ISO standards.
165
    RESERVED_ISO_0x0C = 0x0CU,
166
167
    /// Reserved for ISO (0x0D).
168
    /// Reserved for future use as per ISO standards.
169
    RESERVED_ISO_0x0D = 0x0DU,
170
171
    /// Reserved for ISO (0x0E).
172
    /// Reserved for future use as per ISO standards.
173
    RESERVED_ISO_0x0E = 0x0EU,
174
175
    /// Reserved for ISO (0x0F).
176
    /// Reserved for future use as per ISO standards.
177
    RESERVED_ISO_0x0F = 0x0FU,
178
179
    /// Routing activation required.
180
    /// Indicates that routing activation is needed after the announcement message.
181
    ROUTING_ACTIVATION_REQUIRED = 0x10U,
182
183
    /// UNKNOWN.
184
    /// Indicates that the action code is not recognized or supported.
185
    UNKNOWN
186
  };
187
188
  /// @brief Enum representing DoIP routing activation response codes (ISO 13400).
189
  /// These codes are used in response to routing activation requests, providing status
190
  /// or error information related to the request.
191
  enum class DoIpRoutingResponseCodes : uint8_t
192
  {
193
    /// Unknown source address.
194
    /// The source address provided in the request is not recognized.
195
    UNKNOWN_SOURCE_ADDRESS = 0x00U,
196
197
    /// No free socket.
198
    /// There are no available sockets to establish the connection.
199
    NO_FREE_SOCKET = 0x01U,
200
201
    /// Wrong source address.
202
    /// The source address provided in the request is invalid.
203
    WRONG_SOURCE_ADDRESS = 0x02U,
204
205
    /// Source address already registered.
206
    /// The provided source address has already been activated.
207
    SOURCE_ADDRESS_ALREADY_REGISTERED = 0x03U,
208
209
    /// Missing authentication.
210
    /// The request is missing required authentication credentials.
211
    MISSING_AUTHENTICATION = 0x04U,
212
213
    /// Rejected confirmation.
214
    /// The confirmation of routing activation was rejected.
215
    REJECTED_CONFIRMATION = 0x05U,
216
217
    /// Unsupported activation type.
218
    /// The requested routing activation type is not supported.
219
220
    UNSUPPORTED_ACTIVATION_TYPE = 0x06U,
221
222
    /// Encrypted connection required(TLS).
223
    /// Indicates that the routing activation requires a secure TLS connection.
224
    ENCRYPTED_CONNECTION_TLS = 0x07U,
225
226
    /// Reserved for ISO (0x08).
227
    /// Reserved for future use as per ISO standards.
228
    RESERVED_ISO_0x08 = 0x08U,
229
230
    /// Reserved for ISO (0x09).
231
    /// Reserved for future use as per ISO standards.
232
    RESERVED_ISO_0x09 = 0x09U,
233
234
    /// Reserved for ISO (0x0A).
235
    /// Reserved for future use as per ISO standards.
236
    RESERVED_ISO_0x0A = 0x0AU,
237
238
    /// Reserved for ISO (0x0B).
239
    /// Reserved for future use as per ISO standards.
240
    RESERVED_ISO_0x0B = 0x0BU,
241
242
    /// Reserved for ISO (0x0C).
243
    /// Reserved for future use as per ISO standards.
244
    RESERVED_ISO_0x0C = 0x0CU,
245
246
    /// Reserved for ISO (0x0D).
247
    /// Reserved for future use as per ISO standards.
248
    RESERVED_ISO_0x0D = 0x0DU,
249
250
    /// Reserved for ISO (0x0E).
251
    /// Reserved for future use as per ISO standards.
252
    RESERVED_ISO_0x0E = 0x0EU,
253
254
    /// Reserved for ISO (0x0F).
255
    /// Reserved for future use as per ISO standards.
256
    RESERVED_ISO_0x0F = 0x0FU,
257
258
    /// Routing successfully activated.
259
    /// The routing activation request was processed successfully.
260
    ROUTING_SUCCESSFULLY_ACTIVATED = 0x10U,
261
262
    /// Confirmation required.
263
    /// Additional confirmation is required to complete the routing activation.
264
    CONFIRMATION_REQUIRED = 0x11U,
265
266
    /// UNKNOWN.
267
    /// Indicates that the routing response code is not recognized or supported.
268
    UNKNOWN
269
  };
270
271
  /// @brief Enum representing DoIP diagnostic message NACK codes (ISO 13400).
272
  /// These codes indicate reasons for rejecting or failing to process a diagnostic message
273
  /// in the DoIP protocol.
274
  enum class DoIpDiagnosticMessageNackCodes : uint8_t
275
  {
276
    /// Reserved for ISO (0x00).
277
    /// Reserved for future use as per ISO standards.
278
    RESERVED_ISO_0x00 = 0x00U,
279
280
    /// Reserved for ISO (0x01).
281
    /// Reserved for future use as per ISO standards.
282
    RESERVED_ISO_0x01 = 0x01U,
283
284
    /// Invalid source address.
285
    /// The source address specified in the message is invalid.
286
    INVALID_SOURCE_ADDRESS = 0x02U,
287
288
    /// Invalid target address.
289
    /// The target address specified in the message is invalid.
290
    INVALID_TARGET_ADDRESS = 0x03U,
291
292
    /// Message too large.
293
    /// The size of the message exceeds the maximum allowed limit.
294
    MESSAGE_TOO_LARGE = 0x04U,
295
296
    /// Out of memory.
297
    /// There is insufficient memory available to process the message.
298
    OUT_OF_MEMORY = 0x05U,
299
300
    /// Target unreachable.
301
    /// The specified target address cannot be reached.
302
    TARGET_UNREACHABLE = 0x06U,
303
304
    /// Unknown network.
305
    /// The message references a network that is not recognized or supported.
306
    UNKNOWN_NETWORK = 0x07U,
307
308
    /// Transport protocol error.
309
    /// An error occurred at the transport protocol level, preventing the message from being processed.
310
    TRANSPORT_PROTOCOL_ERROR = 0x08U,
311
312
    /// UNKNOWN.
313
    /// Indicates that the NACK code is not recognized or supported.
314
    UNKNOWN
315
  };
316
317
  /// @brief Enum representing DoIP diagnostic power mode codes (ISO 13400).
318
  /// These codes indicate the diagnostic power mode status of a DoIP entity,
319
  /// providing information about its readiness for diagnostic operations.
320
  enum class DoIpDiagnosticPowerModeCodes : uint8_t
321
  {
322
    /// Not ready.
323
    /// The DoIP entity is not ready to perform diagnostic operations.
324
    NOT_READY = 0x00U,
325
326
    /// Ready.
327
    /// The DoIP entity is ready to perform diagnostic operations.
328
    READY = 0x01U,
329
330
    /// Not supported.
331
    /// The DoIP entity does not support diagnostic power mode reporting.
332
    NOT_SUPPORTED = 0x02U,
333
334
    /// UNKNOWN.
335
    /// Indicates that the power mode code is not recognized or supported.
336
    UNKNOWN
337
  };
338
339
  /// @brief Enum representing DoIP diagnostic acknowledgment codes (ISO 13400).
340
  /// These codes are used to acknowledge the receipt or processing of diagnostic messages
341
  /// in the DoIP protocol.
342
  enum class DoIpDiagnosticAckCodes : uint8_t
343
  {
344
    /// Acknowledgment.
345
    /// Indicates successful receipt or acknowledgment of a diagnostic message.
346
    ACK = 0x00U,
347
348
    /// UNKNOWN.
349
    /// Indicates that the acknowledgment code is not recognized or supported.
350
    UNKNOWN
351
  };
352
353
  /// @brief Enum representing DoIP entity status response codes (ISO 13400).
354
  /// These codes are used to indicate the role or type of a DoIP entity in the network.
355
  enum class DoIpEntityStatusResponseCode : uint8_t
356
  {
357
    /// Gateway.
358
    /// The entity functions as a gateway,
359
    /// facilitating communication between networks.
360
    GATEWAY = 0x00U,
361
362
    /// Node.
363
    /// The entity functions as an individual node within the DoIP network.
364
    NODE = 0x01U,
365
366
    /// UNKNOWN.
367
    /// Indicates that the entity status response code is not recognized or supported.
368
    UNKNOWN
369
  };
370
371
  /// @brief Enum representing DoIP sync status (ISO 13400).
372
  /// These codes are used to indicate whether GID and VIN are synchronized or not.
373
  enum class DoIpSyncStatus : uint8_t
374
  {
375
    /// VIN and or GID are synchronized.
376
    VIN_AND_OR_GID_ARE_SINCHRONIZED = 0x00,
377
378
    /// Reserved for ISO (0x01).
379
    /// Reserved for future use as per ISO standards.
380
    RESERVED_ISO_0x01 = 0x01U,
381
382
    /// Reserved for ISO (0x02).
383
    /// Reserved for future use as per ISO standards.
384
    RESERVED_ISO_0x02 = 0x02U,
385
386
    /// Reserved for ISO (0x03).
387
    /// Reserved for future use as per ISO standards.
388
    RESERVED_ISO_0x03 = 0x03U,
389
390
    /// Reserved for ISO (0x04).
391
    /// Reserved for future use as per ISO standards.
392
    RESERVED_ISO_0x04 = 0x04U,
393
394
    /// Reserved for ISO (0x05).
395
    /// Reserved for future use as per ISO standards.
396
    RESERVED_ISO_0x05 = 0x05U,
397
398
    /// Reserved for ISO (0x06).
399
    /// Reserved for future use as per ISO standards.
400
    RESERVED_ISO_0x06 = 0x06U,
401
402
    /// Reserved for ISO (0x07).
403
    /// Reserved for future use as per ISO standards.
404
    RESERVED_ISO_0x07 = 0x07U,
405
406
    /// Reserved for ISO (0x08).
407
    /// Reserved for future use as per ISO standards.
408
    RESERVED_ISO_0x08 = 0x08U,
409
410
    /// Reserved for ISO (0x09).
411
    /// Reserved for future use as per ISO standards.
412
    RESERVED_ISO_0x09 = 0x09U,
413
414
    /// Reserved for ISO (0x0A).
415
    /// Reserved for future use as per ISO standards.
416
    RESERVED_ISO_0x0A = 0x0AU,
417
418
    /// Reserved for ISO (0x0B).
419
    /// Reserved for future use as per ISO standards.
420
    RESERVED_ISO_0x0B = 0x0BU,
421
422
    /// Reserved for ISO (0x0C).
423
    /// Reserved for future use as per ISO standards.
424
    RESERVED_ISO_0x0C = 0x0CU,
425
426
    /// Reserved for ISO (0x0D).
427
    /// Reserved for future use as per ISO standards.
428
    RESERVED_ISO_0x0D = 0x0DU,
429
430
    /// Reserved for ISO (0x0E).
431
    /// Reserved for future use as per ISO standards.
432
    RESERVED_ISO_0x0E = 0x0EU,
433
434
    /// Reserved for ISO (0x08).
435
    /// Reserved for future use as per ISO standards.
436
    RESERVED_ISO_0x0F = 0x0FU,
437
438
    /// VIN and or GID are not synchronized.
439
    VIN_AND_OR_GID_ARE_NOT_SINCHRONIZED = 0x10U,
440
441
    /// UNKNOWN.
442
    /// Indicates that the sync status is not recognized or supported.
443
    UNKNOWN
444
  };
445
446
  /// @brief Represents the DoIP (Diagnostics over IP) protocol versions.
447
  enum class DoIpProtocolVersion : uint8_t
448
  {
449
    /// @brief Reserved protocol version.
450
    /// This value is used when the version is not specified.
451
    RESERVED_VER = 0x00U,
452
453
    /// @brief Protocol version 1, based on ISO 2010 specification.
454
    ISO13400_2010 = 0x01U,
455
456
    /// @brief Protocol version 2, based on ISO 2012 specification.
457
    ISO13400_2012 = 0x02U,
458
459
    /// @brief Protocol version 3, based on ISO 2019 specification.
460
    ISO13400_2019 = 0x03U,
461
462
    /// @brief Protocol version 4, based on ISO 2019 AMD1 (Amendment 1) specification.
463
    ISO13400_2019_AMD1 = 0x04U,
464
465
    /// @brief Default protocol version.
466
    /// Used for broadcast Vehicle Identification Request Messages.
467
    DEFAULT_VALUE = 0xFFU,
468
469
    /// Represents an unknown or unsupported protocol version (not specified by ISO).
470
    /// Used to indicate an unsupported or unknown protocol version for internal usage.
471
    UNKNOWN = 0xEF
472
  };
473
474
  /// @brief Enum representing DoIP payload types.
475
  /// These payload types are defined as part of theDoIP(Diagnostic over IP) protocol
476
  /// and specify the type of message being transmitted.
477
  enum class DoIpPayloadTypes : uint16_t
478
  {
479
    /// Generic header negative acknowledgment.
480
    /// Indicates a failure or error in processing the generic header.
481
    GENERIC_HEADER_NACK = 0x0000U,
482
483
    /// Vehicle identification request.
484
    /// Used to request identification details of a vehicle.
485
    VEHICLE_IDENTIFICATION_REQUEST = 0x0001U,
486
487
    /// Vehicle identification request with EID.
488
    /// Requests identification using an external identifier(EID).
489
    VEHICLE_IDENTIFICATION_REQUEST_WITH_EID = 0x0002U,
490
491
    /// Vehicle identification request with VIN.
492
    /// Requests identification using the vehicle's VIN (Vehicle Identification Number).
493
    VEHICLE_IDENTIFICATION_REQUEST_WITH_VIN = 0x0003U,
494
495
    /// Vehicle announcement message.
496
    /// Sent to announce the availability of a DoIP entity.
497
    VEHICLE_ANNOUNCEMENT_MESSAGE = 0x0004U,
498
499
    /// Routing activation request.
500
    /// Initiates a routing activation procedure.
501
    ROUTING_ACTIVATION_REQUEST = 0x0005U,
502
503
    /// Routing activation response.
504
    /// Response to a routing activation request.
505
    ROUTING_ACTIVATION_RESPONSE = 0x0006U,
506
507
    /// Alive check request.
508
    /// Sent to verify that a DoIP entity is still operational.
509
    ALIVE_CHECK_REQUEST = 0x0007U,
510
511
    /// Alive check response.
512
    /// Response to an alive check request.
513
    ALIVE_CHECK_RESPONSE = 0x0008U,
514
515
    /// Entity status request.
516
    /// Used to request the status of a DoIP entity.
517
    ENTITY_STATUS_REQUEST = 0x4001U,
518
519
    /// Entity status response.
520
    /// Response to an entity status request.
521
    ENTITY_STATUS_RESPONSE = 0x4002U,
522
523
    /// Diagnostic power mode request.
524
    /// Requests the current power mode of a DoIP entity.
525
    DIAGNOSTIC_POWER_MODE_REQUEST = 0x4003U,
526
527
    /// Diagnostic power mode response.
528
    /// Response to a diagnostic power mode request.
529
    DIAGNOSTIC_POWER_MODE_RESPONSE = 0x4004U,
530
531
    /// Diagnostic message type.
532
    /// Represents a generic diagnostic message.
533
    DIAGNOSTIC_MESSAGE = 0x8001U,
534
535
    /// Diagnostic message positive acknowledgment.
536
    /// Indicates successful processing of a diagnostic message.
537
    DIAGNOSTIC_MESSAGE_ACK = 0x8002U,
538
539
    /// Diagnostic message negative acknowledgment.
540
    /// Indicates an error in processing a diagnostic message.
541
    DIAGNOSTIC_MESSAGE_NACK = 0x8003U
542
  };
543
544
  /// @brief Enum representing DoIP diagnostic ports (ISO 13400).
545
  /// These ports are used for communication in the DoIP protocol over different transport layers.
546
  enum class DoIpPorts : uint16_t
547
  {
548
    /// TCP and UDP doip Ports.
549
    /// The standard port for DoIP communication.
550
    TCP_UDP_PORT = 13400U,
551
552
    /// TLS Port.
553
    /// The standard port for DoIP communication over a secure TLS connection.
554
    TLS_PORT = 3496U
555
  };
556
557
  using namespace DoIpConstants;
558
559
  /// @class DoIpLayer
560
  /// Represents an DoIP protocol layer. Currently only IPv4 DoIP messages are supported
561
  class DoIpLayer : public Layer
562
  {
563
  public:
564
    /// Get the doip payload type
565
    /// @return DoIpPayloadTypes presenting the message doip payload type
566
    virtual DoIpPayloadTypes getPayloadType() const = 0;
567
568
    /// Get the doip payload type as string
569
    /// @return uint16_t presenting the message doip payload type as string
570
    std::string getPayloadTypeAsStr() const;
571
572
    /// Get the version of DOIP protocol
573
    /// @return DoIpProtocolVersion presenting the used protocol version (DOIPV)
574
    DoIpProtocolVersion getProtocolVersion() const;
575
576
    /// Get the version of DOIP protocol
577
    /// @return string presentation the used protocol version (DOIPV)
578
    std::string getProtocolVersionAsStr() const;
579
580
    /// Set the version of DOIP protocol
581
    /// @param[in] version the version of DOIP protocol to set, restricted to existent doip version
582
    void setProtocolVersion(DoIpProtocolVersion version);
583
584
    /// Additional setter for raw protocol version (for testing/fuzzing/debugging)
585
    /// @param[in] rawVersion the raw version of DOIP protocol to set
586
    void setProtocolVersion(uint8_t rawVersion);
587
588
    /// Get the invert version of DOIP protocol
589
    /// @return A uint8_t presenting the used protocol invert version (DOIPV)
590
    uint8_t getInvertProtocolVersion() const;
591
592
    /// Set the invert protocol version of DOIP protocol
593
    /// @param[in] iVersion the invert version of DOIP protocol to set
594
    void setInvertProtocolVersion(uint8_t iVersion);
595
596
    /// Get the doip payload length
597
    /// @return uint32_t presenting the length of doip paylad not including the header
598
    uint32_t getPayloadLength() const;
599
600
    /// Set the doip payload length
601
    /// @param[in] length the doip payload length to set
602
    void setPayloadLength(uint32_t length);
603
604
    /// A method that creates a DoIP layer from packet raw data
605
    /// @param[in] data A pointer to the raw data
606
    /// @param[in] dataLen Size of the data in bytes
607
    /// @param[in] prevLayer A pointer to the previous layer
608
    /// @param[in] packet A pointer to the Packet instance where layer will be stored
609
    /// @return A newly allocated DoIP layer of one of the declared types (according to the message type)
610
    static DoIpLayer* parseDoIpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
611
612
    /// A static method that checks whether a port is considered as a DOIP port
613
    /// @param[in] port The port number to check
614
    /// @return True if this is a DOIP port number, false otherwise
615
    static inline bool isDoIpPort(uint16_t port);
616
617
    /// A static method that validates the input data
618
    /// @param[in] data The pointer to the beginning of a byte stream of an DOIP layer
619
    /// @param[in] dataLen The length of the byte stream
620
    /// @return True if the data is valid and can represent an DOIP layer
621
    static bool isDataValid(uint8_t* data, size_t dataLen);
622
623
    // implement abstract methods
624
625
    /// parse UDS layer
626
    void parseNextLayer() override;
627
628
    /// @return The size of @ref doiphdr + attached fields length
629
    size_t getHeaderLen() const override
630
0
    {
631
0
      return m_DataLen;
632
0
    }
633
634
    std::string toString() const override;
635
636
    void computeCalculateFields() override
637
0
    {}
638
639
    OsiModelLayer getOsiModelLayer() const override
640
0
    {
641
0
      return OsiModelApplicationLayer;
642
0
    }
643
644
    /// @struct SerializedFields
645
    /// Fields written by DoIpLayer's serializeLayer(), in addition to
646
    /// Layer::SerializedFields. Covers the common 8-byte DoIP header.
647
    struct SerializedFields : Layer::SerializedFields
648
    {
649
      /// @return All field descriptors for DoIpLayer
650
      static std::vector<FieldDescriptor> all()
651
0
      {
652
0
        auto result = Layer::SerializedFields::all();
653
0
        std::initializer_list<FieldDescriptor> extra{ ProtocolVersion, ProtocolVersionName, PayloadType,
654
0
                                                    PayloadTypeName, PayloadLength };
655
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
656
0
        return result;
657
0
      }
658
659
      /// @brief DoIP protocol version as the enum numeric value (see getProtocolVersion())
660
      static const FieldDescriptor ProtocolVersion;
661
662
      /// @brief DoIP protocol version as a readable string (see getProtocolVersionAsStr())
663
      static const FieldDescriptor ProtocolVersionName;
664
665
      /// @brief DoIP payload type as the enum numeric value (see getPayloadType())
666
      static const FieldDescriptor PayloadType;
667
668
      /// @brief DoIP payload type as a readable string (see getPayloadTypeAsStr())
669
      static const FieldDescriptor PayloadTypeName;
670
671
      /// @brief Payload length from the DoIP header, in bytes, not including the 8-byte header,
672
      /// in host byte order
673
      static const FieldDescriptor PayloadLength;
674
675
      // cppcheck-suppress duplInheritedMember
676
      /// Maximum field ID used by the layer.
677
      static constexpr uint16_t MaxID = Layer::SerializedFields::MaxID + 5;
678
    };
679
680
  private:
681
    void setPayloadType(DoIpPayloadTypes payloadType);
682
683
    static inline bool isPayloadTypeValid(uint16_t type);
684
685
    static inline bool isProtocolVersionValid(uint8_t version, uint8_t inVersion, DoIpPayloadTypes type);
686
687
    static inline bool isPayloadLengthValid(uint32_t payloadLength, size_t dataLen);
688
689
  protected:
690
    // protected c'tors, this class cannot be instantiated by users
691
    DoIpLayer(size_t length);
692
693
    DoIpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
694
695
    doiphdr* getDoIpHeader() const
696
0
    {
697
0
      return reinterpret_cast<doiphdr*>(m_Data);
698
0
    }
699
700
    void setHeaderFields(DoIpProtocolVersion version, DoIpPayloadTypes type, uint32_t length);
701
702
    void serializeLayer(ObjectScope& serializer) const override;
703
  };
704
705
  // inline methods definition
706
  inline bool DoIpLayer::isDoIpPort(uint16_t port)
707
0
  {
708
0
    auto portAsEnum = static_cast<DoIpPorts>(port);
709
0
    return (portAsEnum == DoIpPorts::TCP_UDP_PORT || portAsEnum == DoIpPorts::TLS_PORT);
710
0
  }
711
712
  inline bool DoIpLayer::isProtocolVersionValid(uint8_t version, uint8_t inVersion, DoIpPayloadTypes type)
713
0
  {
714
0
    const DoIpProtocolVersion parsedVersion = static_cast<DoIpProtocolVersion>(version);
715
716
0
    switch (parsedVersion)
717
0
    {
718
0
    case DoIpProtocolVersion::RESERVED_VER:
719
0
    {
720
0
      PCPP_LOG_DEBUG("[Malformed doip packet]: Reserved ISO DoIP protocol version detected: 0x"
721
0
                     << std::hex << static_cast<int>(version));
722
0
      return false;
723
0
    }
724
0
    case DoIpProtocolVersion::DEFAULT_VALUE:
725
0
      if (type != DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_VIN &&
726
0
          type != DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_EID &&
727
0
          type != DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST)
728
0
      {
729
0
        PCPP_LOG_DEBUG("[Malformed doip packet]: Invalid/unsupported DoIP version!");
730
0
        return false;
731
0
      }
732
0
    case DoIpProtocolVersion::ISO13400_2010:
733
0
    case DoIpProtocolVersion::ISO13400_2012:
734
0
    case DoIpProtocolVersion::ISO13400_2019:
735
0
    case DoIpProtocolVersion::ISO13400_2019_AMD1:
736
0
    {
737
0
      if (version != static_cast<uint8_t>(~inVersion))
738
0
      {
739
0
        PCPP_LOG_DEBUG("[Malformed doip packet]: Protocol version and inverse version mismatch! Version: 0x"
740
0
                       << std::hex << static_cast<int>(version) << ", Inverted: 0x"
741
0
                       << static_cast<int>(inVersion));
742
0
        return false;
743
0
      }
744
0
      return true;
745
0
    }
746
0
    default:
747
0
      PCPP_LOG_DEBUG("[Malformed doip packet]: Unknown DoIP protocol version: 0x" << std::hex
748
0
                                                                                  << static_cast<int>(version));
749
0
      return false;
750
0
    }
751
0
  }
752
753
  inline bool DoIpLayer::isPayloadTypeValid(uint16_t type)
754
0
  {
755
0
    const DoIpPayloadTypes payloadType = static_cast<DoIpPayloadTypes>(type);
756
757
0
    switch (payloadType)
758
0
    {
759
0
    case DoIpPayloadTypes::GENERIC_HEADER_NACK:
760
0
    case DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST:
761
0
    case DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_EID:
762
0
    case DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_VIN:
763
0
    case DoIpPayloadTypes::VEHICLE_ANNOUNCEMENT_MESSAGE:
764
0
    case DoIpPayloadTypes::ROUTING_ACTIVATION_REQUEST:
765
0
    case DoIpPayloadTypes::ROUTING_ACTIVATION_RESPONSE:
766
0
    case DoIpPayloadTypes::ALIVE_CHECK_REQUEST:
767
0
    case DoIpPayloadTypes::ALIVE_CHECK_RESPONSE:
768
0
    case DoIpPayloadTypes::ENTITY_STATUS_REQUEST:
769
0
    case DoIpPayloadTypes::ENTITY_STATUS_RESPONSE:
770
0
    case DoIpPayloadTypes::DIAGNOSTIC_POWER_MODE_REQUEST:
771
0
    case DoIpPayloadTypes::DIAGNOSTIC_POWER_MODE_RESPONSE:
772
0
    case DoIpPayloadTypes::DIAGNOSTIC_MESSAGE:
773
0
    case DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_ACK:
774
0
    case DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_NACK:
775
0
      return true;
776
777
0
    default:
778
0
      PCPP_LOG_DEBUG("[Malformed doip packet]: Invalid DoIP payload type: 0x" << std::hex << type);
779
0
      return false;
780
0
    }
781
0
  }
782
783
  inline bool DoIpLayer::isPayloadLengthValid(uint32_t payloadLength, size_t dataLen)
784
0
  {
785
0
    const size_t actualPayloadLen = dataLen - DOIP_HEADER_LEN;
786
787
0
    if (payloadLength != actualPayloadLen)
788
0
    {
789
0
      PCPP_LOG_DEBUG("[Malformed doip packet]: Payload length mismatch: expected "
790
0
                     << payloadLength << " bytes, but got " << actualPayloadLen << " bytes.");
791
0
      return false;
792
0
    }
793
794
0
    return true;
795
0
  }
796
797
  //~~~~~~~~~~~~~~~~~~~~~~|
798
  // DoIpGenericHeaderNack|
799
  //~~~~~~~~~~~~~~~~~~~~~~|
800
801
  /// @class DoIpGenericHeaderNack
802
  /// @brief Represents a DoIP Generic Header Negative Acknowledgement message.
803
  ///
804
  /// This message indicates that a received DoIP header was invalid or unsupported.
805
  class DoIpGenericHeaderNack : public DoIpLayer
806
  {
807
  public:
808
    /// @brief Constructs the layer from raw DoIP packet data.
809
    /// @param[in] data Pointer to raw packet data.
810
    /// @param[in] dataLen Length of the raw data.
811
    /// @param[in] prevLayer Pointer to the previous layer.
812
    /// @param[in] packet Pointer to the parent packet instance.
813
    DoIpGenericHeaderNack(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
814
815
    /// @brief Constructs the message with a specific NACK code.
816
    /// @param[in] nackCode The generic header NACK code.
817
    explicit DoIpGenericHeaderNack(DoIpGenericHeaderNackCodes nackCode);
818
819
    /// @brief Gets the NACK code.
820
    /// @return enum DoIpGenericHeaderNackCodes representing the NACK code.
821
    DoIpGenericHeaderNackCodes getNackCode() const;
822
823
    /// @brief Sets the NACK code.
824
    /// @param[in] code enum DoIpGenericHeaderNackCodes representing the NACK code to set.
825
    void setNackCode(DoIpGenericHeaderNackCodes code);
826
827
    /// @brief Returns a human-readable summary of the message.
828
    std::string getSummary() const;
829
830
    /// @brief Returns the DoIP payload type for this message.
831
    DoIpPayloadTypes getPayloadType() const override
832
0
    {
833
0
      return DoIpPayloadTypes::GENERIC_HEADER_NACK;
834
0
    }
835
836
    /// @brief Checks if the NACK data length is valid.
837
    /// @param[in] dataLen Length of the data buffer.
838
    /// @return true if the data length is valid, false otherwise.
839
    static inline bool isDataLenValid(size_t dataLen)
840
0
    {
841
0
      return (dataLen == FIXED_LEN);
842
0
    }
843
844
    /// @struct SerializedFields
845
    /// Fields written by DoIpGenericHeaderNack's serializeLayer(), in addition to
846
    /// DoIpLayer::SerializedFields.
847
    struct SerializedFields : DoIpLayer::SerializedFields
848
    {
849
      /// @return All field descriptors for DoIpGenericHeaderNack
850
      static std::vector<FieldDescriptor> all()
851
0
      {
852
0
        auto result = DoIpLayer::SerializedFields::all();
853
0
        std::initializer_list<FieldDescriptor> extra{ NackCode, NackCodeName };
854
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
855
0
        return result;
856
0
      }
857
858
      /// @brief Generic header NACK code as the enum numeric value
859
      static const FieldDescriptor NackCode;
860
861
      /// @brief Generic header NACK code as a readable string
862
      static const FieldDescriptor NackCodeName;
863
864
      // cppcheck-suppress duplInheritedMember
865
      /// Maximum field ID used by the layer.
866
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 2;
867
    };
868
869
  protected:
870
    void serializeLayer(ObjectScope& serializer) const override;
871
872
  private:
873
#pragma pack(push, 1)
874
    struct generic_header_nack : doiphdr
875
    {
876
      uint8_t nackCode;
877
    };
878
#pragma pack(pop)
879
    generic_header_nack* getGenericHeaderNack() const
880
0
    {
881
0
      return reinterpret_cast<generic_header_nack*>(m_Data);
882
0
    }
883
    static constexpr size_t FIXED_LEN = sizeof(generic_header_nack);
884
  };
885
886
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
887
  // DoIpVehicleIdentificationRequest|
888
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
889
890
  /// @class DoIpVehicleIdentificationRequest
891
  /// @brief Represents a Vehicle Identification Request message in the DoIP protocol.
892
  ///
893
  /// This message is sent by a tester to request vehicle identification information
894
  /// such as VIN, logical addresses, and other metadata. It can be broadcast or directed.
895
  class DoIpVehicleIdentificationRequest : public DoIpLayer
896
  {
897
  public:
898
    /// @brief Constructs a VehicleIdentificationRequest from raw packet data.
899
    /// @param[in] data Pointer to the raw payload data.
900
    /// @param[in] dataLen Length of the data buffer.
901
    /// @param[in] prevLayer Pointer to the previous protocol layer.
902
    /// @param[in] packet Pointer to the parent packet.
903
    DoIpVehicleIdentificationRequest(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
904
0
        : DoIpLayer(data, dataLen, prevLayer, packet)
905
0
    {}
906
907
    /// @brief Default constructor to create an empty VehicleIdentificationRequest.
908
    DoIpVehicleIdentificationRequest();
909
910
    /// @brief Returns the DoIP payload type.
911
    DoIpPayloadTypes getPayloadType() const override
912
0
    {
913
0
      return DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST;
914
0
    }
915
916
    /// @brief Checks if the Vehicle Identification Request data length is valid.
917
    /// @param[in] dataLen Length of the data buffer.
918
    /// @return true if the data length is valid, false otherwise.
919
    static inline bool isDataLenValid(size_t dataLen)
920
0
    {
921
0
      return (dataLen == DOIP_HEADER_LEN);  // No payload
922
0
    }
923
  };
924
925
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
926
  // DoIpVehicleIdentificationRequestWithEID|
927
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
928
929
  /// @class DoIpVehicleIdentificationRequestWithEID
930
  /// @brief Represents a DoIP Vehicle Identification Request with EID.
931
  ///
932
  /// This message is used to identify a vehicle based on its Entity ID (EID).
933
  class DoIpVehicleIdentificationRequestWithEID : public DoIpLayer
934
  {
935
  public:
936
    /// @brief Constructs the layer from raw DoIP packet data.
937
    /// @param[in] data Pointer to raw packet data.
938
    /// @param[in] dataLen Length of the raw data.
939
    /// @param[in] prevLayer Pointer to the previous layer.
940
    /// @param[in] packet Pointer to the parent packet instance.
941
    DoIpVehicleIdentificationRequestWithEID(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
942
943
    /// @brief Constructs the message using the specified EID.
944
    /// @param[in] eid A 6-byte Entity ID used for vehicle identification.
945
    explicit DoIpVehicleIdentificationRequestWithEID(const std::array<uint8_t, DOIP_EID_LEN>& eid = {});
946
947
    /// @brief Gets the Entity ID (EID).
948
    /// @return A 6-byte Entity ID (EID).
949
    std::array<uint8_t, DOIP_EID_LEN> getEID() const;
950
951
    /// @brief Sets the Entity ID (EID).
952
    /// @param[in] eid A 6-byte Entity ID (EID).
953
    void setEID(const std::array<uint8_t, DOIP_EID_LEN>& eid);
954
955
    /// @brief Returns a human-readable summary of the message.
956
    std::string getSummary() const;
957
958
    /// @brief Returns the DoIP payload type for this message.
959
    DoIpPayloadTypes getPayloadType() const override
960
0
    {
961
0
      return DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_EID;
962
0
    }
963
964
    /// @brief Checks if the EID data length is valid.
965
    /// @param[in] dataLen Length of the data buffer.
966
    /// @return true if the data length is valid, false otherwise.
967
    static inline bool isDataLenValid(size_t dataLen)
968
0
    {
969
0
      return (dataLen == FIXED_LEN);
970
0
    }
971
972
    /// @struct SerializedFields
973
    /// Fields written by DoIpVehicleIdentificationRequestWithEID's serializeLayer(), in
974
    /// addition to DoIpLayer::SerializedFields.
975
    struct SerializedFields : DoIpLayer::SerializedFields
976
    {
977
      /// @return All field descriptors for DoIpVehicleIdentificationRequestWithEID
978
      static std::vector<FieldDescriptor> all()
979
0
      {
980
0
        auto result = DoIpLayer::SerializedFields::all();
981
0
        result.push_back(Eid);
982
0
        return result;
983
0
      }
984
985
      /// @brief Entity ID (EID) as a lowercase hexadecimal string without separators
986
      static const FieldDescriptor Eid;
987
988
      // cppcheck-suppress duplInheritedMember
989
      /// Maximum field ID used by the layer.
990
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 1;
991
    };
992
993
  protected:
994
    void serializeLayer(ObjectScope& serializer) const override;
995
996
  private:
997
#pragma pack(push, 1)
998
    struct vehicle_identification_request_with_eid : doiphdr
999
    {
1000
      std::array<uint8_t, DOIP_EID_LEN> eid;
1001
    };
1002
#pragma pack(pop)
1003
    vehicle_identification_request_with_eid* getVehicleIdentificationRequestWEID() const
1004
0
    {
1005
0
      return reinterpret_cast<vehicle_identification_request_with_eid*>(m_Data);
1006
0
    }
1007
    static constexpr size_t FIXED_LEN = sizeof(vehicle_identification_request_with_eid);
1008
  };
1009
1010
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1011
  // DoIpVehicleIdentificationRequestWithVIN|
1012
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1013
1014
  /// @class DoIpVehicleIdentificationRequestWithVIN
1015
  /// @brief Represents a DoIP Vehicle Identification Request with VIN.
1016
  ///
1017
  /// This message is used to identify a vehicle based on its Vehicle Identification Number (VIN).
1018
  class DoIpVehicleIdentificationRequestWithVIN : public DoIpLayer
1019
  {
1020
  public:
1021
    /// @brief Constructs the layer from raw DoIP packet data.
1022
    /// @param[in] data Pointer to raw packet data.
1023
    /// @param[in] dataLen Length of the raw data.
1024
    /// @param[in] prevLayer Pointer to the previous layer.
1025
    /// @param[in] packet Pointer to the parent packet instance.
1026
    DoIpVehicleIdentificationRequestWithVIN(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
1027
1028
    /// @brief Constructs the message using the specified VIN.
1029
    /// @param[in] vin A 17-byte Vehicle Identification Number.
1030
    explicit DoIpVehicleIdentificationRequestWithVIN(const std::array<uint8_t, DOIP_VIN_LEN>& vin = {});
1031
1032
    /// @brief Gets the Vehicle Identification Number (VIN).
1033
    /// @return A 17-byte Vehicle Identification Number (VIN).
1034
    std::array<uint8_t, DOIP_VIN_LEN> getVIN() const;
1035
1036
    /// @brief Sets the Vehicle Identification Number (VIN).
1037
    /// @param[in] vin A 17-byte Vehicle Identification Number (VIN).
1038
    void setVIN(const std::array<uint8_t, DOIP_VIN_LEN>& vin);
1039
1040
    /// @brief Returns a human-readable summary of the message.
1041
    std::string getSummary() const;
1042
1043
    /// @brief Returns the DoIP payload type for this message.
1044
    DoIpPayloadTypes getPayloadType() const override
1045
0
    {
1046
0
      return DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_VIN;
1047
0
    }
1048
1049
    /// @brief Checks if the VIN data length is valid.
1050
    /// @param[in] dataLen Length of the data buffer.
1051
    /// @return true if the data length is valid, false otherwise.
1052
    static inline bool isDataLenValid(size_t dataLen)
1053
0
    {
1054
0
      return (dataLen == FIXED_LEN);
1055
0
    }
1056
1057
    /// @struct SerializedFields
1058
    /// Fields written by DoIpVehicleIdentificationRequestWithVIN's serializeLayer(), in
1059
    /// addition to DoIpLayer::SerializedFields.
1060
    struct SerializedFields : DoIpLayer::SerializedFields
1061
    {
1062
      /// @return All field descriptors for DoIpVehicleIdentificationRequestWithVIN
1063
      static std::vector<FieldDescriptor> all()
1064
0
      {
1065
0
        auto result = DoIpLayer::SerializedFields::all();
1066
0
        result.push_back(Vin);
1067
0
        return result;
1068
0
      }
1069
1070
      /// @brief Vehicle Identification Number (VIN) as a 17-character string
1071
      static const FieldDescriptor Vin;
1072
1073
      // cppcheck-suppress duplInheritedMember
1074
      /// Maximum field ID used by the layer.
1075
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 1;
1076
    };
1077
1078
  protected:
1079
    void serializeLayer(ObjectScope& serializer) const override;
1080
1081
  private:
1082
#pragma pack(push, 1)
1083
    struct vehicle_identification_request_with_vin : doiphdr
1084
    {
1085
      std::array<uint8_t, DOIP_VIN_LEN> vin;
1086
    };
1087
#pragma pack(pop)
1088
    vehicle_identification_request_with_vin* getVehicleIdentificationRequestWVIN() const
1089
0
    {
1090
0
      return reinterpret_cast<vehicle_identification_request_with_vin*>(m_Data);
1091
0
    }
1092
    static constexpr size_t FIXED_LEN = sizeof(vehicle_identification_request_with_vin);
1093
  };
1094
1095
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1096
  // DoIpVehicleAnnouncementMessage|
1097
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1098
1099
  /// @class DoIpVehicleAnnouncementMessage
1100
  /// @brief Represents a DoIP Vehicle Announcement message.
1101
  ///
1102
  /// This message is broadcasted by a vehicle to announce its presence, including VIN,
1103
  /// logical address, EID, GID, and optionally synchronization status.
1104
  class DoIpVehicleAnnouncementMessage : public DoIpLayer
1105
  {
1106
  public:
1107
    /// @brief Constructs the layer from raw DoIP packet data.
1108
    /// @param[in] data Pointer to the raw data buffer.
1109
    /// @param[in] dataLen Size of the data buffer in bytes.
1110
    /// @param[in] prevLayer Pointer to the previous protocol layer.
1111
    /// @param[in] packet Pointer to the parent packet.
1112
    DoIpVehicleAnnouncementMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
1113
1114
    /// @brief Constructs the message using specified field values.
1115
    /// @param[in] vin Vehicle Identification Number (VIN).
1116
    /// @param[in] logicalAddress Logical address of the vehicle.
1117
    /// @param[in] eid Entity Identifier (EID).
1118
    /// @param[in] gid Group Identifier (GID).
1119
    /// @param[in] actionCode Further action code.
1120
    DoIpVehicleAnnouncementMessage(const std::array<uint8_t, DOIP_VIN_LEN>& vin, uint16_t logicalAddress,
1121
                                   const std::array<uint8_t, DOIP_EID_LEN>& eid,
1122
                                   const std::array<uint8_t, DOIP_GID_LEN>& gid, DoIpActionCodes actionCode);
1123
1124
    /// @brief Gets the Vehicle Identification Number (VIN).
1125
    /// @return A 17-byte Vehicle Identification Number (VIN).
1126
    std::array<uint8_t, DOIP_VIN_LEN> getVIN() const;
1127
1128
    /// @brief Sets the Vehicle Identification Number (VIN).
1129
    /// @param[in] vin A 17-byte Vehicle Identification Number (VIN).
1130
    void setVIN(const std::array<uint8_t, DOIP_VIN_LEN>& vin);
1131
1132
    /// @brief Gets the logical address of the vehicle.
1133
    /// @return A 2-byte logical address of the vehicle.
1134
    uint16_t getLogicalAddress() const;
1135
1136
    /// @brief Sets the logical address.
1137
    /// @param[in] address A 2-byte logical address of the vehicle.
1138
    void setLogicalAddress(uint16_t address);
1139
1140
    /// @brief Gets the Entity Identifier (EID).
1141
    /// @return A 6-byte Entity Identifier (EID).
1142
    std::array<uint8_t, DOIP_EID_LEN> getEID() const;
1143
1144
    /// @brief Sets the Entity Identifier (EID).
1145
    /// @param[in] eid A 6-byte Entity Identifier (EID).
1146
    void setEID(const std::array<uint8_t, DOIP_EID_LEN>& eid);
1147
1148
    /// @brief Gets the Group Identifier (GID).
1149
    /// @return A 6-byte Group Identifier (GID).
1150
    std::array<uint8_t, DOIP_GID_LEN> getGID() const;
1151
1152
    /// @brief Sets the Group Identifier (GID).
1153
    /// @param[in] gid A 6-byte Group Identifier (GID).
1154
    void setGID(const std::array<uint8_t, DOIP_GID_LEN>& gid);
1155
1156
    /// @brief Gets the further action required code.
1157
    /// @return enum DoIpActionCodes representing the further action required code.
1158
    DoIpActionCodes getFurtherActionRequired() const;
1159
1160
    /// @brief Sets the further action required code.
1161
    /// @param[in] action enum DoIpActionCodes representing the further action required code to set.
1162
    void setFurtherActionRequired(DoIpActionCodes action);
1163
1164
    /// @brief Gets the optional synchronization status if available.
1165
    /// @throw std::runtime_error if the sync status is not present.
1166
    /// @note To use this method safely, check beforehand if the sync status is present using hasSyncStatus().
1167
    /// @return A 1-byte synchronization status.
1168
    DoIpSyncStatus getSyncStatus() const;
1169
1170
    /// @brief Sets the synchronization status.
1171
    /// @param[in] sync enum DoIpSyncStatus representing the synchronization status to set.
1172
    void setSyncStatus(DoIpSyncStatus sync);
1173
1174
    /// @brief Checks whether the sync status is present.
1175
    /// @return true if the sync status is present, false otherwise.
1176
    bool hasSyncStatus() const;
1177
1178
    /// @brief Clears the optional sync status field.
1179
    void clearSyncStatus();
1180
1181
    /// @brief Returns a human-readable summary of the message.
1182
    std::string getSummary() const;
1183
1184
    /// @brief Returns the DoIP payload type.
1185
    DoIpPayloadTypes getPayloadType() const override
1186
0
    {
1187
0
      return DoIpPayloadTypes::VEHICLE_ANNOUNCEMENT_MESSAGE;
1188
0
    }
1189
1190
    /// @brief checks if the vehicle announcement data length is valid.
1191
    /// @param[in] dataLen Length of the data buffer.
1192
    /// @return true if the data length is valid, false otherwise.
1193
    static inline bool isDataLenValid(size_t dataLen)
1194
0
    {
1195
0
      return (dataLen == FIXED_LEN || dataLen == OPT_LEN);
1196
0
    }
1197
1198
    /// @struct SerializedFields
1199
    /// Fields written by DoIpVehicleAnnouncementMessage's serializeLayer(), in addition to
1200
    /// DoIpLayer::SerializedFields.
1201
    struct SerializedFields : DoIpLayer::SerializedFields
1202
    {
1203
      /// @return All field descriptors for DoIpVehicleAnnouncementMessage
1204
      static std::vector<FieldDescriptor> all()
1205
0
      {
1206
0
        auto result = DoIpLayer::SerializedFields::all();
1207
0
        std::initializer_list<FieldDescriptor> extra{
1208
0
          Vin,        LogicalAddress, Eid, Gid, FurtherActionRequired, FurtherActionRequiredName,
1209
0
          SyncStatus, SyncStatusName
1210
0
        };
1211
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
1212
0
        return result;
1213
0
      }
1214
1215
      /// @brief Vehicle Identification Number (VIN) as a 17-character string
1216
      static const FieldDescriptor Vin;
1217
1218
      /// @brief Logical address of the vehicle, in host byte order
1219
      static const FieldDescriptor LogicalAddress;
1220
1221
      /// @brief Entity ID (EID) as a lowercase hexadecimal string without separators
1222
      static const FieldDescriptor Eid;
1223
1224
      /// @brief Group ID (GID) as a lowercase hexadecimal string without separators
1225
      static const FieldDescriptor Gid;
1226
1227
      /// @brief Further action required as the enum numeric value
1228
      static const FieldDescriptor FurtherActionRequired;
1229
1230
      /// @brief Further action required as a readable string
1231
      static const FieldDescriptor FurtherActionRequiredName;
1232
1233
      /// @brief VIN/GID sync status as the enum numeric value. Omitted when the optional field
1234
      /// is not present
1235
      static const FieldDescriptor SyncStatus;
1236
1237
      /// @brief VIN/GID sync status as a readable string. Omitted when the optional field is
1238
      /// not present
1239
      static const FieldDescriptor SyncStatusName;
1240
1241
      // cppcheck-suppress duplInheritedMember
1242
      /// Maximum field ID used by the layer.
1243
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 8;
1244
    };
1245
1246
  protected:
1247
    void serializeLayer(ObjectScope& serializer) const override;
1248
1249
  private:
1250
#pragma pack(push, 1)
1251
    struct vehicle_announcement_message : doiphdr
1252
    {
1253
      std::array<uint8_t, DOIP_VIN_LEN> vin;
1254
1255
      uint16_t logicalAddress;
1256
1257
      std::array<uint8_t, DOIP_EID_LEN> eid;
1258
1259
      std::array<uint8_t, DOIP_GID_LEN> gid;
1260
1261
      uint8_t actionCode;
1262
    };
1263
#pragma pack(pop)
1264
1265
    vehicle_announcement_message* getVehicleAnnouncementMessage() const
1266
0
    {
1267
0
      return reinterpret_cast<vehicle_announcement_message*>(m_Data);
1268
0
    }
1269
    static constexpr size_t FIXED_LEN = sizeof(vehicle_announcement_message);
1270
    static constexpr size_t SYNC_STATUS_OFFSET = FIXED_LEN;
1271
    static constexpr size_t SYNC_STATUS_LEN = sizeof(uint8_t);
1272
    static constexpr size_t OPT_LEN = FIXED_LEN + SYNC_STATUS_LEN;
1273
  };
1274
1275
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1276
  // DoIpRoutingActivationRequest|
1277
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1278
1279
  /// @class DoIpRoutingActivationRequest
1280
  /// @brief Represents a DoIP Routing Activation Request message.
1281
  ///
1282
  /// Provides parsing and construction for Routing Activation Request messages
1283
  /// as defined by the DoIP protocol.
1284
  class DoIpRoutingActivationRequest : public DoIpLayer
1285
  {
1286
  public:
1287
    /// @brief Constructs the layer from raw DoIP packet data.
1288
    /// @param[in] data Pointer to raw packet data.
1289
    /// @param[in] dataLen Length of the raw data.
1290
    /// @param[in] prevLayer Pointer to the previous layer.
1291
    /// @param[in] packet Pointer to the parent packet instance.
1292
    DoIpRoutingActivationRequest(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
1293
1294
    /// @brief Constructs the message from field values.
1295
    /// @param[in] sourceAddress Source address of the tester.
1296
    /// @param[in] activationType Type of routing activation.
1297
    DoIpRoutingActivationRequest(uint16_t sourceAddress, DoIpActivationTypes activationType);
1298
1299
    /// @brief Returns the source address.
1300
    /// @return The 2-byte source address.
1301
    uint16_t getSourceAddress() const;
1302
1303
    /// @brief Sets the source address.
1304
    /// @param[in] value The source address to set.
1305
    void setSourceAddress(uint16_t value);
1306
1307
    /// @brief Returns the activation type.
1308
    /// @return The activation type as a DoIpActivationTypes enum value.
1309
    DoIpActivationTypes getActivationType() const;
1310
1311
    /// @brief Sets the activation type.
1312
    /// @param[in] activationType The activation type to set.
1313
    void setActivationType(DoIpActivationTypes activationType);
1314
1315
    /// @brief Gets the reserved ISO bytes.
1316
    /// @return 4-byte array representing the reserved ISO bytes.
1317
    std::array<uint8_t, DOIP_RESERVED_ISO_LEN> getReservedIso() const;
1318
1319
    /// @brief Sets the reserved ISO bytes.
1320
    /// @param[in] reservedIso The reserved ISO bytes to set.
1321
    void setReservedIso(const std::array<uint8_t, DOIP_RESERVED_ISO_LEN>& reservedIso);
1322
1323
    /// @brief Gets the to reserved OEM bytes if present.
1324
    /// @throw std::runtime_error if the reserved OEM bytes are not present.
1325
    /// @note To use this method safely, check beforehand if the reserved OEM bytes are present using
1326
    /// hasReservedOem().
1327
    std::array<uint8_t, DOIP_RESERVED_OEM_LEN> getReservedOem() const;
1328
1329
    /// @brief Sets the reserved OEM bytes.
1330
    /// @param[in] reservedOem The reserved OEM bytes to set.
1331
    void setReservedOem(const std::array<uint8_t, DOIP_RESERVED_OEM_LEN>& reservedOem);
1332
1333
    /// @brief Checks if OEM reserved bytes are present.
1334
    /// @return true if OEM reserved bytes are present, false otherwise.
1335
    bool hasReservedOem() const;
1336
1337
    /// @brief Clears the OEM reserved bytes.
1338
    void clearReservedOem();
1339
1340
    /// @brief Returns a human-readable summary of the message.
1341
    std::string getSummary() const;
1342
1343
    /// @brief Returns the DoIP payload type for this message.
1344
    DoIpPayloadTypes getPayloadType() const override
1345
0
    {
1346
0
      return DoIpPayloadTypes::ROUTING_ACTIVATION_REQUEST;
1347
0
    }
1348
1349
    /// @brief Checks if the routing activation request data length is valid.
1350
    /// @param[in] dataLen The length of the data.
1351
    /// @return true if the data is valid, false otherwise.
1352
    static inline bool isDataLenValid(size_t dataLen)
1353
0
    {
1354
0
      return (dataLen == FIXED_LEN || dataLen == OPT_LEN);
1355
0
    }
1356
1357
    /// @struct SerializedFields
1358
    /// Fields written by DoIpRoutingActivationRequest's serializeLayer(), in addition to
1359
    /// DoIpLayer::SerializedFields. Reserved ISO bytes are omitted.
1360
    struct SerializedFields : DoIpLayer::SerializedFields
1361
    {
1362
      /// @return All field descriptors for DoIpRoutingActivationRequest
1363
      static std::vector<FieldDescriptor> all()
1364
0
      {
1365
0
        auto result = DoIpLayer::SerializedFields::all();
1366
0
        std::initializer_list<FieldDescriptor> extra{ SourceAddress, ActivationType, ActivationTypeName,
1367
0
                                                    ReservedOem };
1368
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
1369
0
        return result;
1370
0
      }
1371
1372
      /// @brief Source address of the tester, in host byte order
1373
      static const FieldDescriptor SourceAddress;
1374
1375
      /// @brief Routing activation type as the enum numeric value
1376
      static const FieldDescriptor ActivationType;
1377
1378
      /// @brief Routing activation type as a readable string
1379
      static const FieldDescriptor ActivationTypeName;
1380
1381
      /// @brief Optional reserved OEM bytes as a hex string. Omitted when not present
1382
      static const FieldDescriptor ReservedOem;
1383
1384
      // cppcheck-suppress duplInheritedMember
1385
      /// Maximum field ID used by the layer.
1386
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 4;
1387
    };
1388
1389
  protected:
1390
    void serializeLayer(ObjectScope& serializer) const override;
1391
1392
  private:
1393
#pragma pack(push, 1)
1394
    struct routing_activation_request : doiphdr
1395
    {
1396
      uint16_t sourceAddress;
1397
1398
      uint8_t activationType;
1399
1400
      std::array<uint8_t, DOIP_RESERVED_ISO_LEN> reservedIso;
1401
    };
1402
#pragma pack(pop)
1403
1404
    routing_activation_request* getRoutingActivationRequest() const
1405
0
    {
1406
0
      return reinterpret_cast<routing_activation_request*>(m_Data);
1407
0
    }
1408
    static constexpr size_t FIXED_LEN = sizeof(routing_activation_request);
1409
    static constexpr size_t RESERVED_OEM_OFFSET = FIXED_LEN;
1410
    static constexpr size_t OPT_LEN = FIXED_LEN + DOIP_RESERVED_OEM_LEN;
1411
  };
1412
1413
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1414
  // DoIpRoutingActivationResponse|
1415
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1416
1417
  /// @class DoIpRoutingActivationResponse
1418
  /// @brief Represents a DoIP Routing Activation Response message.
1419
  ///
1420
  /// Provides parsing and construction for Routing Activation Response messages
1421
  /// as defined by the DoIP protocol.
1422
  class DoIpRoutingActivationResponse : public DoIpLayer
1423
  {
1424
  public:
1425
    /// @brief Constructs the layer from raw DoIP packet data.
1426
    /// @param[in] data Pointer to raw packet data.
1427
    /// @param[in] dataLen Length of the raw data.
1428
    /// @param[in] prevLayer Pointer to the previous layer.
1429
    /// @param[in] packet Pointer to the parent packet instance.
1430
    DoIpRoutingActivationResponse(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
1431
1432
    /// @brief Constructs the message from field values.
1433
    /// @param[in] logicalAddressExternalTester Logical address of the external tester.
1434
    /// @param[in] sourceAddress ECU source address.
1435
    /// @param[in] responseCode The routing response code.
1436
    DoIpRoutingActivationResponse(uint16_t logicalAddressExternalTester, uint16_t sourceAddress,
1437
                                  DoIpRoutingResponseCodes responseCode);
1438
1439
    /// @brief Gets the logical address of the external tester.
1440
    /// @return The 2-byte logical address of the external tester.
1441
    uint16_t getLogicalAddressExternalTester() const;
1442
1443
    /// @brief Sets the logical address of the external tester.
1444
    /// @param[in] addr The logical address of the external tester to set.
1445
    void setLogicalAddressExternalTester(uint16_t addr);
1446
1447
    /// @brief Gets the source address.
1448
    /// @return The 2-byte source address.
1449
    uint16_t getSourceAddress() const;
1450
1451
    /// @brief Sets the source address.
1452
    /// @param[in] sourceAddress The source address to set.
1453
    void setSourceAddress(uint16_t sourceAddress);
1454
1455
    /// @brief Gets the routing response code.
1456
    /// @return enum DoIpRoutingResponseCodes representing the routing response code.
1457
    DoIpRoutingResponseCodes getResponseCode() const;
1458
1459
    /// @brief Sets the routing response code.
1460
    /// @param[in] code The routing response code to set.
1461
    void setResponseCode(DoIpRoutingResponseCodes code);
1462
1463
    /// @brief Gets the reserved ISO bytes.
1464
    /// @return 4-byte array representing the reserved ISO bytes.
1465
    std::array<uint8_t, DOIP_RESERVED_ISO_LEN> getReservedIso() const;
1466
1467
    /// @brief Sets the reserved ISO bytes.
1468
    /// @param[in] reservedIso The reserved ISO bytes to set.
1469
    void setReservedIso(const std::array<uint8_t, DOIP_RESERVED_ISO_LEN>& reservedIso);
1470
1471
    /// @brief Gets pointer to reserved OEM bytes if present.
1472
    /// @throw std::runtime_error if the reserved OEM bytes are not present.
1473
    /// @note before using this method, check beforehand if the reserved OEM bytes are present using
1474
    /// hasReservedOem().
1475
    std::array<uint8_t, DOIP_RESERVED_OEM_LEN> getReservedOem() const;
1476
1477
    /// @brief Sets the reserved OEM bytes.
1478
    /// @param[in] reservedOem The reserved OEM bytes to set.
1479
    void setReservedOem(const std::array<uint8_t, DOIP_RESERVED_OEM_LEN>& reservedOem);
1480
1481
    /// @brief Checks if OEM reserved bytes are present.
1482
    /// @return true if OEM reserved bytes are present, false otherwise.
1483
    bool hasReservedOem() const;
1484
1485
    /// @brief Clears the OEM reserved bytes.
1486
    void clearReservedOem();
1487
1488
    /// @brief Returns a human-readable summary of the message.
1489
    std::string getSummary() const;
1490
1491
    /// @brief Returns the DoIP payload type for this message.
1492
    DoIpPayloadTypes getPayloadType() const override
1493
0
    {
1494
0
      return DoIpPayloadTypes::ROUTING_ACTIVATION_RESPONSE;
1495
0
    }
1496
1497
    /// @brief Checks if the routing activation response data length is valid.
1498
    /// @param[in] dataLen The length of the data.
1499
    /// @return true if the data is valid, false otherwise.
1500
    static inline bool isDataLenValid(size_t dataLen)
1501
0
    {
1502
0
      return (dataLen == FIXED_LEN || dataLen == OPT_LEN);
1503
0
    }
1504
1505
    /// @struct SerializedFields
1506
    /// Fields written by DoIpRoutingActivationResponse's serializeLayer(), in addition to
1507
    /// DoIpLayer::SerializedFields. Reserved ISO bytes are omitted.
1508
    struct SerializedFields : DoIpLayer::SerializedFields
1509
    {
1510
      /// @return All field descriptors for DoIpRoutingActivationResponse
1511
      static std::vector<FieldDescriptor> all()
1512
0
      {
1513
0
        auto result = DoIpLayer::SerializedFields::all();
1514
0
        std::initializer_list<FieldDescriptor> extra{ LogicalAddressExternalTester, SourceAddress, ResponseCode,
1515
0
                                                    ReservedOem };
1516
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
1517
0
        return result;
1518
0
      }
1519
1520
      /// @brief Logical address of the external tester, in host byte order
1521
      static const FieldDescriptor LogicalAddressExternalTester;
1522
1523
      /// @brief Source address, in host byte order
1524
      static const FieldDescriptor SourceAddress;
1525
1526
      /// @brief Routing activation response code as the enum numeric value
1527
      static const FieldDescriptor ResponseCode;
1528
1529
      /// @brief Optional reserved OEM bytes as a hex string. Omitted when not present
1530
      static const FieldDescriptor ReservedOem;
1531
1532
      // cppcheck-suppress duplInheritedMember
1533
      /// Maximum field ID used by the layer.
1534
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 4;
1535
    };
1536
1537
  protected:
1538
    void serializeLayer(ObjectScope& serializer) const override;
1539
1540
  private:
1541
#pragma pack(push, 1)
1542
    struct routing_activation_response : doiphdr
1543
    {
1544
      uint16_t logicalAddressExternalTester;
1545
1546
      uint16_t sourceAddress;
1547
1548
      uint8_t responseCode;
1549
1550
      std::array<uint8_t, DOIP_RESERVED_ISO_LEN> reservedIso;
1551
    };
1552
#pragma pack(pop)
1553
    routing_activation_response* getRoutingActivationResponse() const
1554
0
    {
1555
0
      return reinterpret_cast<routing_activation_response*>(m_Data);
1556
0
    }
1557
    static constexpr size_t FIXED_LEN = sizeof(routing_activation_response);
1558
    static constexpr size_t RESERVED_OEM_OFFSET = FIXED_LEN;
1559
    static constexpr size_t OPT_LEN = FIXED_LEN + DOIP_RESERVED_OEM_LEN;
1560
  };
1561
1562
  //~~~~~~~~~~~~~~~~~~~~~~|
1563
  // DoIpAliveCheckRequest|
1564
  //~~~~~~~~~~~~~~~~~~~~~~|
1565
1566
  /// @class DoIpAliveCheckRequest
1567
  /// @brief Represents an Alive Check Request message in the DoIP protocol.
1568
  ///
1569
  /// This message is sent by a tester to verify if a DoIP entity is responsive.
1570
  /// The responding DoIP node should reply with an Alive Check Response.
1571
  class DoIpAliveCheckRequest : public DoIpLayer
1572
  {
1573
  public:
1574
    /// @brief Constructs an AliveCheckRequest from raw packet data.
1575
    /// @param[in] data Pointer to the raw payload data.
1576
    /// @param[in] dataLen Length of the data buffer.
1577
    /// @param[in] prevLayer Pointer to the previous protocol layer.
1578
    /// @param[in] packet Pointer to the parent packet.
1579
    DoIpAliveCheckRequest(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
1580
0
        : DoIpLayer(data, dataLen, prevLayer, packet)
1581
0
    {}
1582
1583
    /// @brief Default constructor to create an empty AliveCheckRequest message.
1584
    DoIpAliveCheckRequest();
1585
1586
    /// @brief Returns the DoIP payload type for this message.
1587
    DoIpPayloadTypes getPayloadType() const override
1588
0
    {
1589
0
      return DoIpPayloadTypes::ALIVE_CHECK_REQUEST;
1590
0
    }
1591
1592
    /// @brief Checks if the Alive Check Request data length is valid.
1593
    /// @param[in] dataLen Length of the data buffer.
1594
    /// @return true if the data length is valid, false otherwise.
1595
    static inline bool isDataLenValid(size_t dataLen)
1596
0
    {
1597
0
      return (dataLen == DOIP_HEADER_LEN);  // No payload
1598
0
    }
1599
  };
1600
1601
  //~~~~~~~~~~~~~~~~~~~~~~~|
1602
  // DoIpAliveCheckResponse|
1603
  //~~~~~~~~~~~~~~~~~~~~~~~|
1604
1605
  /// @class DoIpAliveCheckResponse
1606
  /// @brief Represents a DoIP Alive Check Response message.
1607
  ///
1608
  /// The Alive Check Response is used to confirm that an entity is still active in the network.
1609
  /// It contains the source address of the responder.
1610
  class DoIpAliveCheckResponse : public DoIpLayer
1611
  {
1612
  public:
1613
    /// @brief Constructs the layer from raw DoIP packet data.
1614
    /// @param[in] data Pointer to the raw data buffer.
1615
    /// @param[in] dataLen Size of the data buffer in bytes.
1616
    /// @param[in] prevLayer Pointer to the previous protocol layer.
1617
    /// @param[in] packet Pointer to the parent packet.
1618
    DoIpAliveCheckResponse(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
1619
1620
    /// @brief Constructs the message using the specified source address.
1621
    /// @param[in] sourceAddress The source address of the responder.
1622
    explicit DoIpAliveCheckResponse(uint16_t sourceAddress);
1623
1624
    /// @brief Gets the source address.
1625
    /// @return The 2-byte source address of the responder.
1626
    uint16_t getSourceAddress() const;
1627
1628
    /// @brief Sets the source address.
1629
    /// @param[in] address The new source address.
1630
    void setSourceAddress(uint16_t address);
1631
1632
    /// @brief Returns a human-readable summary of the message.
1633
    std::string getSummary() const;
1634
1635
    /// @brief Returns the DoIP payload type.
1636
    /// @return DoIpPayloadTypes::ALIVE_CHECK_RESPONSE
1637
    DoIpPayloadTypes getPayloadType() const override
1638
0
    {
1639
0
      return DoIpPayloadTypes::ALIVE_CHECK_RESPONSE;
1640
0
    }
1641
    /// @brief Checks if the Alive Check Response data length is valid.
1642
    /// @param[in] dataLen Length of the data buffer.
1643
    /// @return true if the data length is valid, false otherwise.
1644
    static inline bool isDataLenValid(size_t dataLen)
1645
0
    {
1646
0
      return (dataLen == FIXED_LEN);
1647
0
    }
1648
1649
    /// @struct SerializedFields
1650
    /// Fields written by DoIpAliveCheckResponse's serializeLayer(), in addition to
1651
    /// DoIpLayer::SerializedFields.
1652
    struct SerializedFields : DoIpLayer::SerializedFields
1653
    {
1654
      /// @return All field descriptors for DoIpAliveCheckResponse
1655
      static std::vector<FieldDescriptor> all()
1656
0
      {
1657
0
        auto result = DoIpLayer::SerializedFields::all();
1658
0
        result.push_back(SourceAddress);
1659
0
        return result;
1660
0
      }
1661
1662
      /// @brief Source address of the responder, in host byte order
1663
      static const FieldDescriptor SourceAddress;
1664
1665
      // cppcheck-suppress duplInheritedMember
1666
      /// Maximum field ID used by the layer.
1667
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 1;
1668
    };
1669
1670
  protected:
1671
    void serializeLayer(ObjectScope& serializer) const override;
1672
1673
  private:
1674
#pragma pack(push, 1)
1675
    struct alive_check_response : doiphdr
1676
    {
1677
      uint16_t sourceAddress;
1678
    };
1679
#pragma pack(pop)
1680
    alive_check_response* getAliveCheckResponse() const
1681
0
    {
1682
0
      return reinterpret_cast<alive_check_response*>(m_Data);
1683
0
    }
1684
    static constexpr size_t FIXED_LEN = sizeof(alive_check_response);
1685
  };
1686
1687
  //~~~~~~~~~~~~~~~~~~~~~~~~|
1688
  // DoIpEntityStatusRequest|
1689
  //~~~~~~~~~~~~~~~~~~~~~~~~|
1690
1691
  /// @class DoIpEntityStatusRequest
1692
  /// @brief Represents an Entity Status Request message in the DoIP protocol.
1693
  ///
1694
  /// This message is sent by a tester to request the current status of the DoIP entity,
1695
  /// including capabilities such as maximum number of concurrent socket connections
1696
  /// and optionally the maximum data size supported.
1697
  class DoIpEntityStatusRequest : public DoIpLayer
1698
  {
1699
  public:
1700
    /// @brief Constructs an EntityStatusRequest from raw packet data.
1701
    /// @param[in] data Pointer to the raw payload data.
1702
    /// @param[in] dataLen Length of the data buffer.
1703
    /// @param[in] prevLayer Pointer to the previous protocol layer.
1704
    /// @param[in] packet Pointer to the parent packet.
1705
    DoIpEntityStatusRequest(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
1706
0
        : DoIpLayer(data, dataLen, prevLayer, packet)
1707
0
    {}
1708
1709
    /// @brief Default constructor to create an empty EntityStatusRequest message.
1710
    DoIpEntityStatusRequest();
1711
1712
    /// @brief Returns the DoIP payload type for this message.
1713
    DoIpPayloadTypes getPayloadType() const override
1714
0
    {
1715
0
      return DoIpPayloadTypes::ENTITY_STATUS_REQUEST;
1716
0
    }
1717
1718
    /// @brief Checks if the Entity Status Request data length is valid.
1719
    /// @param[in] dataLen Length of the data buffer.
1720
    /// @return true if the data length is valid, false otherwise.
1721
    static inline bool isDataLenValid(size_t dataLen)
1722
0
    {
1723
0
      return (dataLen == DOIP_HEADER_LEN);  // No payload
1724
0
    }
1725
  };
1726
1727
  //~~~~~~~~~~~~~~~~~~~~~~~~~|
1728
  // DoIpEntityStatusResponse|
1729
  //~~~~~~~~~~~~~~~~~~~~~~~~~|
1730
1731
  /// @class DoIpEntityStatusResponse
1732
  /// @brief Represents a DoIP Entity Status Response message.
1733
  ///
1734
  /// This message provides the status of a DoIP entity, such as its type,
1735
  /// the number of concurrent sockets it can support, and optionally the max data size.
1736
  class DoIpEntityStatusResponse : public DoIpLayer
1737
  {
1738
  public:
1739
    /// @brief Constructs the layer from raw DoIP packet data.
1740
    /// @param[in] data Pointer to the raw data buffer.
1741
    /// @param[in] dataLen Size of the data buffer in bytes.
1742
    /// @param[in] prevLayer Pointer to the previous protocol layer.
1743
    /// @param[in] packet Pointer to the parent packet.
1744
    DoIpEntityStatusResponse(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
1745
1746
    /// @brief Constructs the message using provided field values.
1747
    /// @param[in] nodeType Type of the DoIP node (default: GATEWAY).
1748
    /// @param[in] maxConcurrentSockets Maximum supported concurrent sockets.
1749
    /// @param[in] currentlyOpenSockets Currently active sockets.
1750
    DoIpEntityStatusResponse(DoIpEntityStatusResponseCode nodeType, uint8_t maxConcurrentSockets,
1751
                             uint8_t currentlyOpenSockets);
1752
1753
    /// @brief Returns the DoIP payload type.
1754
    /// @return DoIpPayloadTypes::ENTITY_STATUS_RESPONSE
1755
    DoIpPayloadTypes getPayloadType() const override
1756
0
    {
1757
0
      return DoIpPayloadTypes::ENTITY_STATUS_RESPONSE;
1758
0
    }
1759
1760
    /// @brief Gets the type of the DoIP node.
1761
    /// @return enum DoIpEntityStatusResponseCode representing the node type.
1762
    DoIpEntityStatusResponseCode getNodeType() const;
1763
1764
    /// @brief Gets the maximum number of concurrent sockets supported.
1765
    /// @return Max supported concurrent sockets.
1766
    uint8_t getMaxConcurrentSockets() const;
1767
1768
    /// @brief Gets the number of currently open sockets.
1769
    /// @return Number of currently open sockets.
1770
    uint8_t getCurrentlyOpenSockets() const;
1771
1772
    /// @brief Gets the optional maximum data size field.
1773
    /// @throws std::runtime_error if the max data size is not present.
1774
    /// @note To use this method safely, check beforehand if the max data size is present using hasMaxDataSize().
1775
    /// @return The maximum data size in bytes.
1776
    uint32_t getMaxDataSize() const;
1777
1778
    /// @brief Sets the DoIP node type.
1779
    /// @param[in] status enum DoIpEntityStatusResponseCode representing the node type to set.
1780
    void setNodeType(DoIpEntityStatusResponseCode status);
1781
1782
    /// @brief Sets the maximum number of concurrent sockets.
1783
    /// @param[in] sockets New maximum concurrent socket count.
1784
    void setMaxConcurrentSockets(uint8_t sockets);
1785
1786
    /// @brief Sets the number of currently open sockets.
1787
    /// @param[in] sockets New count of currently open sockets.
1788
    void setCurrentlyOpenSockets(uint8_t sockets);
1789
1790
    /// @brief Sets the maximum data size field.
1791
    /// @param[in] data uint32_t representing the max data size.
1792
    void setMaxDataSize(uint32_t data);
1793
1794
    /// @brief Checks if the optional max data size is present.
1795
    /// @return True if max data size is available, false otherwise.
1796
    bool hasMaxDataSize() const;
1797
1798
    /// @brief Clears the optional max data size field.
1799
    void clearMaxDataSize();
1800
1801
    /// @brief Returns a human-readable summary of the message.
1802
    /// @return A string summarizing the Entity Status Response.
1803
    std::string getSummary() const;
1804
1805
    /// @brief  Checks if the Entity Status Response data length is valid.
1806
    /// @param[in] dataLen Length of the data buffer.
1807
    /// @return  true if the data length is valid, false otherwise.
1808
    static inline bool isDataLenValid(size_t dataLen)
1809
0
    {
1810
0
      return (dataLen == FIXED_LEN || dataLen == OPT_LEN);
1811
0
    }
1812
1813
    /// @struct SerializedFields
1814
    /// Fields written by DoIpEntityStatusResponse's serializeLayer(), in addition to
1815
    /// DoIpLayer::SerializedFields.
1816
    struct SerializedFields : DoIpLayer::SerializedFields
1817
    {
1818
      /// @return All field descriptors for DoIpEntityStatusResponse
1819
      static std::vector<FieldDescriptor> all()
1820
0
      {
1821
0
        auto result = DoIpLayer::SerializedFields::all();
1822
0
        std::initializer_list<FieldDescriptor> extra{ NodeType, NodeTypeName, MaxConcurrentSockets,
1823
0
                                                    CurrentlyOpenSockets, MaxDataSize };
1824
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
1825
0
        return result;
1826
0
      }
1827
1828
      /// @brief DoIP node type as the enum numeric value
1829
      static const FieldDescriptor NodeType;
1830
1831
      /// @brief DoIP node type as a readable string
1832
      static const FieldDescriptor NodeTypeName;
1833
1834
      /// @brief Maximum number of concurrent sockets supported
1835
      static const FieldDescriptor MaxConcurrentSockets;
1836
1837
      /// @brief Number of currently open sockets
1838
      static const FieldDescriptor CurrentlyOpenSockets;
1839
1840
      /// @brief Optional maximum data size in bytes, in host byte order. Omitted when not
1841
      /// present
1842
      static const FieldDescriptor MaxDataSize;
1843
1844
      // cppcheck-suppress duplInheritedMember
1845
      /// Maximum field ID used by the layer.
1846
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 5;
1847
    };
1848
1849
  protected:
1850
    void serializeLayer(ObjectScope& serializer) const override;
1851
1852
  private:
1853
#pragma pack(push, 1)
1854
    struct entity_status_response : doiphdr
1855
    {
1856
      uint8_t nodeType;
1857
1858
      uint8_t maxConcurrentSockets;
1859
1860
      uint8_t currentlyOpenSockets;
1861
    };
1862
#pragma pack(pop)
1863
1864
    entity_status_response* getEntityStatusResponse() const
1865
0
    {
1866
0
      return reinterpret_cast<entity_status_response*>(m_Data);
1867
0
    }
1868
    static constexpr size_t FIXED_LEN = sizeof(entity_status_response);
1869
    static constexpr size_t MAX_DATA_SIZE_OFFSET = FIXED_LEN;
1870
    static constexpr size_t MAX_DATA_SIZE_LEN = sizeof(uint32_t);
1871
    static constexpr size_t OPT_LEN = FIXED_LEN + MAX_DATA_SIZE_LEN;
1872
  };
1873
1874
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1875
  // DoIpDiagnosticPowerModeRequest|
1876
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1877
1878
  /// @class DoIpDiagnosticPowerModeRequest
1879
  /// @brief Represents a Diagnostic Power Mode Request message in the DoIP protocol.
1880
  ///
1881
  /// This message is sent to inquire about the current power mode status of the vehicle,
1882
  /// which helps determine if diagnostic communication can be initiated or continued.
1883
  class DoIpDiagnosticPowerModeRequest : public DoIpLayer
1884
  {
1885
  public:
1886
    /// @brief Constructs a DiagnosticPowerModeRequest from raw packet data.
1887
    /// @param[in] data Pointer to the raw payload data.
1888
    /// @param[in] dataLen Length of the data buffer.
1889
    /// @param[in] prevLayer Pointer to the previous protocol layer.
1890
    /// @param[in] packet Pointer to the parent packet.
1891
    DoIpDiagnosticPowerModeRequest(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
1892
0
        : DoIpLayer(data, dataLen, prevLayer, packet)
1893
0
    {}
1894
1895
    /// @brief Default constructor to create an empty DiagnosticPowerModeRequest.
1896
    DoIpDiagnosticPowerModeRequest();
1897
1898
    /// @brief Returns the DoIP payload type.
1899
    DoIpPayloadTypes getPayloadType() const override
1900
0
    {
1901
0
      return DoIpPayloadTypes::DIAGNOSTIC_POWER_MODE_REQUEST;
1902
0
    }
1903
1904
    /// @brief Checks if the Entity Status Request data length is valid.
1905
    /// @param[in] dataLen Length of the data buffer.
1906
    /// @return true if the data length is valid, false otherwise.
1907
    static inline bool isDataLenValid(size_t dataLen)
1908
0
    {
1909
0
      return (dataLen == DOIP_HEADER_LEN);  // No payload
1910
0
    }
1911
  };
1912
1913
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1914
  // DoIpDiagnosticPowerModeResponse|
1915
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1916
1917
  /// @class DoIpDiagnosticPowerModeResponse
1918
  /// @brief Represents a DoIP Diagnostic Power Mode Response message.
1919
  ///
1920
  /// This message is used to communicate the current power mode of the vehicle
1921
  /// or control unit in response to a diagnostic power mode request.
1922
  class DoIpDiagnosticPowerModeResponse : public DoIpLayer
1923
  {
1924
  public:
1925
    /// @brief Constructs the layer from raw DoIP packet data.
1926
    /// @param[in] data Pointer to the raw data buffer.
1927
    /// @param[in] dataLen Size of the data buffer in bytes.
1928
    /// @param[in] prevLayer Pointer to the previous protocol layer.
1929
    /// @param[in] packet Pointer to the parent packet.
1930
    DoIpDiagnosticPowerModeResponse(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
1931
1932
    /// @brief Constructs the message using the specified power mode code.
1933
    /// @param[in] modeCode Diagnostic power mode code to set.
1934
    explicit DoIpDiagnosticPowerModeResponse(DoIpDiagnosticPowerModeCodes modeCode);
1935
1936
    /// @brief Gets the current power mode code.
1937
    /// @return The diagnostic power mode code.
1938
    DoIpDiagnosticPowerModeCodes getPowerModeCode() const;
1939
1940
    /// @brief Sets the power mode code.
1941
    /// @param[in] code enum DoIpDiagnosticPowerModeCodes representing the power mode code to set.
1942
    void setPowerModeCode(DoIpDiagnosticPowerModeCodes code);
1943
1944
    /// @brief Returns a human-readable summary of the message.
1945
    std::string getSummary() const;
1946
1947
    /// @brief Returns the DoIP payload type.
1948
    /// @return DoIpPayloadTypes::DIAGNOSTIC_POWER_MODE_RESPONSE
1949
    DoIpPayloadTypes getPayloadType() const override
1950
0
    {
1951
0
      return DoIpPayloadTypes::DIAGNOSTIC_POWER_MODE_RESPONSE;
1952
0
    }
1953
1954
    /// @brief Checks if the Diagnostic Power Mode Response data length is valid.
1955
    /// @param[in] dataLen Length of the data buffer.
1956
    /// @return true if the data length is valid, false otherwise.
1957
    static inline bool isDataLenValid(size_t dataLen)
1958
0
    {
1959
0
      return (dataLen == FIXED_LEN);
1960
0
    }
1961
1962
    /// @struct SerializedFields
1963
    /// Fields written by DoIpDiagnosticPowerModeResponse's serializeLayer(), in addition to
1964
    /// DoIpLayer::SerializedFields.
1965
    struct SerializedFields : DoIpLayer::SerializedFields
1966
    {
1967
      /// @return All field descriptors for DoIpDiagnosticPowerModeResponse
1968
      static std::vector<FieldDescriptor> all()
1969
0
      {
1970
0
        auto result = DoIpLayer::SerializedFields::all();
1971
0
        std::initializer_list<FieldDescriptor> extra{ PowerModeCode, PowerModeCodeName };
1972
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
1973
0
        return result;
1974
0
      }
1975
1976
      /// @brief Diagnostic power mode as the enum numeric value
1977
      static const FieldDescriptor PowerModeCode;
1978
1979
      /// @brief Diagnostic power mode as a readable string
1980
      static const FieldDescriptor PowerModeCodeName;
1981
1982
      // cppcheck-suppress duplInheritedMember
1983
      /// Maximum field ID used by the layer.
1984
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 2;
1985
    };
1986
1987
  protected:
1988
    void serializeLayer(ObjectScope& serializer) const override;
1989
1990
  private:
1991
#pragma pack(push, 1)
1992
    struct diagnostic_power_mode_response : doiphdr
1993
    {
1994
      uint8_t powerModeCode;
1995
    };
1996
#pragma pack(pop)
1997
    diagnostic_power_mode_response* getDiagnosticPowerModeResponse() const
1998
0
    {
1999
0
      return reinterpret_cast<diagnostic_power_mode_response*>(m_Data);
2000
0
    }
2001
    static constexpr size_t FIXED_LEN = sizeof(diagnostic_power_mode_response);
2002
  };
2003
2004
  //~~~~~~~~~~~~~~~~~~~|
2005
  // DoIpDiagnosticBase|
2006
  //~~~~~~~~~~~~~~~~~~~|
2007
2008
  /// @class DoIpDiagnosticBase
2009
  /// @brief Represents a DoIP Diagnostic Message sent between tester and ECU.
2010
  /// This class includes source and target addresses.
2011
  class DoIpDiagnosticBase : public DoIpLayer
2012
  {
2013
  public:
2014
    /// @brief Gets the source logical address of the message.
2015
    /// @return 16-bit address of the source ECU.
2016
    uint16_t getSourceAddress() const;
2017
2018
    /// @brief Sets the source logical address.
2019
    /// @param[in] sourceAddress 16-bit source address.
2020
    void setSourceAddress(uint16_t sourceAddress);
2021
2022
    /// @brief Gets the target logical address of the message.
2023
    /// @return 16-bit address of the destination ECU.
2024
    uint16_t getTargetAddress() const;
2025
2026
    /// @brief Sets the target logical address.
2027
    /// @param[in] targetAddress 16-bit target address.
2028
    void setTargetAddress(uint16_t targetAddress);
2029
2030
    /// @brief Returns a human-readable summary of the message content.
2031
    /// @return A string summarizing the diagnostic message.
2032
    virtual std::string getSummary() const = 0;
2033
2034
    /// @struct SerializedFields
2035
    /// Fields written by DoIpDiagnosticBase's serializeLayer(), in addition to
2036
    /// DoIpLayer::SerializedFields.
2037
    struct SerializedFields : DoIpLayer::SerializedFields
2038
    {
2039
      /// @return All field descriptors for DoIpDiagnosticBase
2040
      static std::vector<FieldDescriptor> all()
2041
0
      {
2042
0
        auto result = DoIpLayer::SerializedFields::all();
2043
0
        std::initializer_list<FieldDescriptor> extra{ SourceAddress, TargetAddress };
2044
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
2045
0
        return result;
2046
0
      }
2047
2048
      /// @brief Source logical address, in host byte order
2049
      static const FieldDescriptor SourceAddress;
2050
2051
      /// @brief Target logical address, in host byte order
2052
      static const FieldDescriptor TargetAddress;
2053
2054
      // cppcheck-suppress duplInheritedMember
2055
      /// Maximum field ID used by the layer.
2056
      static constexpr uint16_t MaxID = DoIpLayer::SerializedFields::MaxID + 2;
2057
    };
2058
2059
  protected:
2060
    DoIpDiagnosticBase(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
2061
2062
0
    DoIpDiagnosticBase(size_t length) : DoIpLayer(length) {};
2063
2064
    void serializeLayer(ObjectScope& serializer) const override;
2065
2066
#pragma pack(push, 1)
2067
    /// An internal structure representing the common diagnostic header.
2068
    struct common_diagnostic_header : doiphdr
2069
    {
2070
      uint16_t sourceAddress;
2071
2072
      uint16_t targetAddress;
2073
    };
2074
#pragma pack(pop)
2075
    common_diagnostic_header* getCommonDiagnosticHeader() const
2076
0
    {
2077
0
      return reinterpret_cast<common_diagnostic_header*>(m_Data);
2078
0
    }
2079
  };
2080
2081
  //~~~~~~~~~~~~~~~~~~~~~~|
2082
  // DoIpDiagnosticMessage|
2083
  //~~~~~~~~~~~~~~~~~~~~~~|
2084
2085
  /// @class DoIpDiagnosticMessage
2086
  /// @brief Represents a DoIP Diagnostic message sent between tester and ECU.
2087
  ///
2088
  /// This message includes source and target addresses and carries diagnostic service data.
2089
  class DoIpDiagnosticMessage : public DoIpDiagnosticBase
2090
  {
2091
  public:
2092
    /// @brief Constructs the DiagnosticMessage from raw packet data.
2093
    /// @param[in] data Pointer to the raw payload data.
2094
    /// @param[in] dataLen Length of the data buffer.
2095
    /// @param[in] prevLayer Pointer to the previous protocol layer.
2096
    /// @param[in] packet Pointer to the parent packet.
2097
    DoIpDiagnosticMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
2098
2099
    /// @brief Constructs a DiagnosticMessage from specified field values.
2100
    /// @param[in] sourceAddress Address of the sending ECU/tester.
2101
    /// @param[in] targetAddress Address of the target ECU.
2102
    /// @param[in] diagnosticData Vector containing UDS diagnostic service data.
2103
    DoIpDiagnosticMessage(uint16_t sourceAddress, uint16_t targetAddress,
2104
                          const std::vector<uint8_t>& diagnosticData);
2105
2106
    /// @brief Set the diagnostic data payload.
2107
    /// @param[in] data A vector containing the diagnostic data bytes to be stored.
2108
    void setDiagnosticData(const std::vector<uint8_t>& data);
2109
2110
    /// @brief Get the diagnostic data payload.
2111
    /// @return A vector containing the diagnostic data bytes.
2112
    std::vector<uint8_t> getDiagnosticData() const;
2113
2114
    /// @brief Returns a human-readable summary of the message content.
2115
    /// @return A string summarizing the diagnostic message.
2116
    std::string getSummary() const override;
2117
2118
    /// @brief Returns the DoIP payload type.
2119
    /// @return DoIpPayloadTypes::DIAGNOSTIC_MESSAGE
2120
    DoIpPayloadTypes getPayloadType() const override
2121
0
    {
2122
0
      return DoIpPayloadTypes::DIAGNOSTIC_MESSAGE;
2123
0
    }
2124
2125
    // override getHeaderLen()
2126
    size_t getHeaderLen() const override
2127
0
    {
2128
0
      return sizeof(doiphdr) + 2 * sizeof(uint16_t);
2129
0
    }
2130
2131
    /// @brief Checks if the diagnostic data length is valid.
2132
    /// @return true if the data length is valid, false otherwise.
2133
    static inline bool isDataLenValid(size_t dataLen)
2134
0
    {
2135
0
      return (dataLen >= MIN_LEN);
2136
0
    }
2137
2138
  private:
2139
    static constexpr size_t DIAGNOSTIC_DATA_OFFSET = sizeof(common_diagnostic_header);
2140
    static constexpr size_t MIN_LEN = DIAGNOSTIC_DATA_OFFSET + 1; /*Min diagnostic request Len*/
2141
  };
2142
2143
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
2144
  // DoIpDiagnosticResponseMessageBase|
2145
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
2146
  /// @class DoIpDiagnosticResponseMessageBase
2147
  /// @brief Represents a Basic class for Diagnostic message (ACK/NACK) sent back to tester.
2148
  class DoIpDiagnosticResponseMessageBase : public DoIpDiagnosticBase
2149
  {
2150
  public:
2151
    /// @brief Gets the acknowledgment/nack code (1-byte).
2152
    /// @return The acknowledgment/nack code.
2153
    uint8_t getResponseCode() const;
2154
2155
    /// @brief Sets the acknowledgment/nack code (1-byte).
2156
    /// @param[in] code The acknowledgment/nack code to set.
2157
    void setResponseCode(uint8_t code);
2158
2159
    /// @brief Gets the optional previously echoed diagnostic message.
2160
    /// @return A vector containing the previously echoed diagnostic message.
2161
    std::vector<uint8_t> getPreviousMessage() const;
2162
2163
    /// @brief Checks if a previous message is attached.
2164
    /// @return true if a previous message is present, false otherwise.
2165
    bool hasPreviousMessage() const;
2166
2167
    /// @brief Sets the previous echoed diagnostic message.
2168
    /// @param[in] msg A vector containing the previously echoed diagnostic message.
2169
    void setPreviousMessage(const std::vector<uint8_t>& msg);
2170
2171
    /// @brief Clears the previously stored diagnostic message.
2172
    void clearPreviousMessage();
2173
2174
    /// @brief Checks if data length is valid.
2175
    /// @param[in] dataLen Length of the data buffer.
2176
    /// @return true if the data length is valid, false otherwise.
2177
    static inline bool isDataLenValid(size_t dataLen)
2178
0
    {
2179
0
      return (dataLen >= FIXED_LEN);
2180
0
    }
2181
2182
  protected:
2183
    DoIpDiagnosticResponseMessageBase(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
2184
    DoIpDiagnosticResponseMessageBase(uint16_t sourceAddress, uint16_t targetAddress, DoIpPayloadTypes type);
2185
2186
  private:
2187
#pragma pack(push, 1)
2188
    struct diagnostic_response_message_base : common_diagnostic_header
2189
    {
2190
      uint8_t diagnosticCode;
2191
    };
2192
#pragma pack(pop)
2193
    diagnostic_response_message_base* getDiagnosticResponseMessageBase() const
2194
0
    {
2195
0
      return reinterpret_cast<diagnostic_response_message_base*>(m_Data);
2196
0
    }
2197
    static constexpr size_t FIXED_LEN = sizeof(diagnostic_response_message_base);
2198
    static constexpr size_t PREVIOUS_MSG_OFFSET = FIXED_LEN;
2199
  };
2200
2201
  //~~~~~~~~~~~~~~~~~~~~~~~~~|
2202
  // DoIpDiagnosticMessageAck|
2203
  //~~~~~~~~~~~~~~~~~~~~~~~~~|
2204
2205
  /// @class DoIpDiagnosticMessageAck
2206
  /// @brief Represents a positive acknowledgment message in response to a DiagnosticMessage.
2207
  ///
2208
  /// This message is sent by a DoIP node to acknowledge the correct reception and processing
2209
  /// of a diagnostic message. Optionally, the original message (or part of it) may be echoed back.
2210
  class DoIpDiagnosticMessageAck : public DoIpDiagnosticResponseMessageBase
2211
  {
2212
  public:
2213
    /// @brief Constructs a DiagnosticAckMessage from raw packet data.
2214
    /// @param[in] data Pointer to the raw payload data.
2215
    /// @param[in] dataLen Length of the data buffer.
2216
    /// @param[in] prevLayer Pointer to the previous protocol layer.
2217
    /// @param[in] packet Pointer to the parent packet.
2218
    DoIpDiagnosticMessageAck(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
2219
2220
    /// @brief Constructs a DiagnosticAckMessage from specified field values.
2221
    /// @param[in] sourceAddress Address of the sending ECU.
2222
    /// @param[in] targetAddress Address of the receiving ECU.
2223
    /// @param[in] ackCode Acknowledgment code describing the result.
2224
    DoIpDiagnosticMessageAck(uint16_t sourceAddress, uint16_t targetAddress, DoIpDiagnosticAckCodes ackCode);
2225
2226
    /// @brief Gets the diagnostic acknowledgment code.
2227
    /// @return enum DoIpDiagnosticAckCodes describing the acknowledgment code.
2228
    DoIpDiagnosticAckCodes getAckCode() const;
2229
2230
    /// @brief Sets the acknowledgment code.
2231
    /// @param[in] code enum DoIpDiagnosticAckCodes describing the acknowledgment code.
2232
    void setAckCode(DoIpDiagnosticAckCodes code);
2233
2234
    /// @brief Returns a human-readable summary of the message.
2235
    std::string getSummary() const override;
2236
2237
    /// @brief Returns the DoIP payload type.
2238
    DoIpPayloadTypes getPayloadType() const override
2239
0
    {
2240
0
      return DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_ACK;
2241
0
    }
2242
2243
    /// @struct SerializedFields
2244
    /// Fields written by DoIpDiagnosticMessageAck's serializeLayer(), in addition to
2245
    /// DoIpDiagnosticBase::SerializedFields. Optional previous-message bytes are omitted.
2246
    struct SerializedFields : DoIpDiagnosticBase::SerializedFields
2247
    {
2248
      /// @return All field descriptors for DoIpDiagnosticMessageAck
2249
      static std::vector<FieldDescriptor> all()
2250
0
      {
2251
0
        auto result = DoIpDiagnosticBase::SerializedFields::all();
2252
0
        std::initializer_list<FieldDescriptor> extra{ AckCode, AckCodeName };
2253
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
2254
0
        return result;
2255
0
      }
2256
2257
      /// @brief Diagnostic ACK code as the enum numeric value
2258
      static const FieldDescriptor AckCode;
2259
2260
      /// @brief Diagnostic ACK code as a readable string
2261
      static const FieldDescriptor AckCodeName;
2262
2263
      // cppcheck-suppress duplInheritedMember
2264
      /// Maximum field ID used by the layer.
2265
      static constexpr uint16_t MaxID = DoIpDiagnosticBase::SerializedFields::MaxID + 2;
2266
    };
2267
2268
  protected:
2269
    void serializeLayer(ObjectScope& serializer) const override;
2270
  };
2271
2272
  //~~~~~~~~~~~~~~~~~~~~~~~~~~|
2273
  // DoIpDiagnosticMessageNack|
2274
  //~~~~~~~~~~~~~~~~~~~~~~~~~~|
2275
2276
  /// @class DoIpDiagnosticMessageNack
2277
  /// @brief Represents a negative acknowledgment message in response to a DiagnosticMessage.
2278
  ///
2279
  /// This message is sent by a DoIP node when a diagnostic message is received but could not
2280
  /// be processed successfully. It may include the original message for reference.
2281
  class DoIpDiagnosticMessageNack : public DoIpDiagnosticResponseMessageBase
2282
  {
2283
  public:
2284
    /// @brief Constructs a DiagnosticNackMessage from raw packet data.
2285
    /// @param[in] data Pointer to the raw payload data.
2286
    /// @param[in] dataLen Length of the data buffer.
2287
    /// @param[in] prevLayer Pointer to the previous protocol layer.
2288
    /// @param[in] packet Pointer to the parent packet.
2289
    DoIpDiagnosticMessageNack(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
2290
2291
    /// @brief Constructs a DiagnosticNackMessage from specified field values.
2292
    /// @param[in] sourceAddress Address of the sending ECU.
2293
    /// @param[in] targetAddress Address of the receiving ECU.
2294
    /// @param[in] nackCode Negative acknowledgment code describing the failure.
2295
    DoIpDiagnosticMessageNack(uint16_t sourceAddress, uint16_t targetAddress,
2296
                              DoIpDiagnosticMessageNackCodes nackCode);
2297
2298
    /// @brief Gets the negative acknowledgment code.
2299
    /// @return enum DoIpDiagnosticMessageNackCodes describing the negative acknowledgment code.
2300
    DoIpDiagnosticMessageNackCodes getNackCode() const;
2301
2302
    /// @brief Sets the negative acknowledgment code.
2303
    /// @param[in] code enum DoIpDiagnosticMessageNackCodes describing the negative acknowledgment code.
2304
    void setNackCode(DoIpDiagnosticMessageNackCodes code);
2305
2306
    /// @brief Returns a human-readable summary of the message.
2307
    std::string getSummary() const override;
2308
2309
    /// @brief Returns the DoIP payload type.
2310
    DoIpPayloadTypes getPayloadType() const override
2311
0
    {
2312
0
      return DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_NACK;
2313
0
    }
2314
2315
    /// @struct SerializedFields
2316
    /// Fields written by DoIpDiagnosticMessageNack's serializeLayer(), in addition to
2317
    /// DoIpDiagnosticBase::SerializedFields. Optional previous-message bytes are omitted.
2318
    struct SerializedFields : DoIpDiagnosticBase::SerializedFields
2319
    {
2320
      /// @return All field descriptors for DoIpDiagnosticMessageNack
2321
      static std::vector<FieldDescriptor> all()
2322
0
      {
2323
0
        auto result = DoIpDiagnosticBase::SerializedFields::all();
2324
0
        std::initializer_list<FieldDescriptor> extra{ NackCode, NackCodeName };
2325
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
2326
0
        return result;
2327
0
      }
2328
2329
      /// @brief Diagnostic NACK code as the enum numeric value
2330
      static const FieldDescriptor NackCode;
2331
2332
      /// @brief Diagnostic NACK code as a readable string
2333
      static const FieldDescriptor NackCodeName;
2334
2335
      // cppcheck-suppress duplInheritedMember
2336
      /// Maximum field ID used by the layer.
2337
      static constexpr uint16_t MaxID = DoIpDiagnosticBase::SerializedFields::MaxID + 2;
2338
    };
2339
2340
  protected:
2341
    void serializeLayer(ObjectScope& serializer) const override;
2342
  };
2343
2344
}  // namespace pcpp