libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
DoIPNegativeDiagnosticAck.h
Go to the documentation of this file.
1 #ifndef DOIPNEGATIVEDIAGNOSTICACK_H
2 #define DOIPNEGATIVEDIAGNOSTICACK_H
3 
4 #include <stdint.h>
5 #include <optional>
6 #include <iostream>
7 #include <iomanip>
8 
9 namespace doip {
10 
11 // Table 26
12 enum class DoIPNegativeDiagnosticAck : uint8_t {
13  // 0 and 1: reserved
17  OutOfMemory = 5,
19  UnknownNetwork = 7,
20  TransportProtocolError = 8, // also use if other error codes do not apply
21  TargetBusy = 9,
22 };
23 
24 /**
25  * @brief Alias for diagnostic acknowledgment type.
26  *
27  * This type represents either a successful acknowledgment (std::nullopt) or
28  * a negative acknowledgment (DoIPNegativeDiagnosticAck).
29  * This is to circumvent the reserved value '0' in DoIPNegativeDiagnosticAck.
30  */
31 using DoIPDiagnosticAck = std::optional<DoIPNegativeDiagnosticAck>;
32 
33 /**
34  * @brief Stream output operator for DoIPNegativeDiagnosticAck
35  *
36  * @param os the output stream
37  * @param nack the negative acknowledgment code
38  * @return std::ostream& the output stream
39  */
40 inline std::ostream& operator<<(std::ostream& os, doip::DoIPNegativeDiagnosticAck nack) {
41  const char* name = nullptr;
42  switch (nack) {
44  name = "InvalidSourceAddress";
45  break;
47  name = "UnknownTargetAddress";
48  break;
50  name = "DiagnosticMessageTooLarge";
51  break;
53  name = "OutOfMemory";
54  break;
56  name = "TargetUnreachable";
57  break;
59  name = "UnknownNetwork";
60  break;
62  name = "TransportProtocolError";
63  break;
65  name = "TargetBusy";
66  break;
67  default:
68  name = "Unknown";
69  break;
70  }
71  os << name << " (0x" << std::hex << std::uppercase << std::setw(2) << std::setfill('0')
72  << static_cast<unsigned int>(static_cast<uint8_t>(nack)) << std::dec << ")";
73 
74  return os;
75 }
76 
77 /**
78  * @brief Stream output operator for DoIPNegativeDiagnosticAck
79  *
80  * @param os the output stream
81  * @param ack the negative acknowledgment code
82  * @return std::ostream& the output stream
83  */
84 inline std::ostream& operator<<(std::ostream& os, doip::DoIPDiagnosticAck ack) {
85  if (!ack.has_value()) {
86  os << "PositiveAck (0x00)";
87  return os;
88  }
89 
90  return os << ack.value();
91 }
92 
93 }
94 #endif /* DOIPNEGATIVEDIAGNOSTICACK_H */
Definition: AnsiColors.h:3
std::optional< DoIPNegativeDiagnosticAck > DoIPDiagnosticAck
Alias for diagnostic acknowledgment type.
std::ostream & operator<<(std::ostream &os, const ByteArray &arr)
Stream operator for ByteArray.
Definition: ByteArray.h:303