2024-05-27 10:35:06 +02:00
|
|
|
//
|
|
|
|
|
// Copyright (c) 2023-2024 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
|
|
|
|
|
//
|
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
|
// (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
//
|
|
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
#ifndef ASYNC_MQTT5_ERROR_HPP
|
|
|
|
|
#define ASYNC_MQTT5_ERROR_HPP
|
|
|
|
|
|
2024-10-08 09:59:35 +02:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <ostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
#include <boost/asio/error.hpp>
|
|
|
|
|
|
|
|
|
|
namespace async_mqtt5 {
|
2023-10-18 12:16:05 +02:00
|
|
|
|
2023-10-13 09:07:56 +02:00
|
|
|
/**
|
|
|
|
|
* \brief A representation of Disconnect Reason Code.
|
|
|
|
|
*
|
|
|
|
|
* \details Represents all Reason Codes that the Client can send to the Server
|
2023-10-18 12:16:05 +02:00
|
|
|
* in the \__DISCONNECT\__ packet as the reason for the disconnection.
|
2023-10-13 09:07:56 +02:00
|
|
|
*/
|
2024-02-05 15:25:05 +01:00
|
|
|
enum class disconnect_rc_e : uint8_t {
|
2023-10-18 12:16:05 +02:00
|
|
|
/** Close the connection normally. Do not send the Will Message. */
|
2023-10-05 13:59:32 +02:00
|
|
|
normal_disconnection = 0x00,
|
2023-10-13 09:07:56 +02:00
|
|
|
|
2023-10-16 15:57:26 +02:00
|
|
|
/** The Client wishes to disconnect but requires that
|
2023-10-18 12:16:05 +02:00
|
|
|
the Server also publishes its Will Message. */
|
2023-10-27 10:41:15 +02:00
|
|
|
disconnect_with_will_message = 0x04
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
enum class disconnect_rc_e : uint8_t {
|
2023-10-27 10:41:15 +02:00
|
|
|
normal_disconnection = 0x00,
|
2023-10-05 13:59:32 +02:00
|
|
|
disconnect_with_will_message = 0x04,
|
2023-10-13 09:07:56 +02:00
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
unspecified_error = 0x80,
|
|
|
|
|
malformed_packet = 0x81,
|
|
|
|
|
protocol_error = 0x82,
|
|
|
|
|
implementation_specific_error = 0x83,
|
|
|
|
|
topic_name_invalid = 0x90,
|
|
|
|
|
receive_maximum_exceeded = 0x93,
|
|
|
|
|
topic_alias_invalid = 0x94,
|
|
|
|
|
packet_too_large = 0x95,
|
|
|
|
|
message_rate_too_high = 0x96,
|
|
|
|
|
quota_exceeded = 0x97,
|
|
|
|
|
administrative_action = 0x98,
|
|
|
|
|
payload_format_invalid = 0x99
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-27 10:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
namespace client {
|
2023-10-13 09:07:56 +02:00
|
|
|
/**
|
2024-02-05 15:25:05 +01:00
|
|
|
* \brief Defines error codes related to MQTT client.
|
2023-10-13 09:07:56 +02:00
|
|
|
*
|
2024-02-05 15:25:05 +01:00
|
|
|
* \details Encapsulates errors that occur on the client side.
|
2023-10-13 09:07:56 +02:00
|
|
|
*/
|
2023-10-05 13:59:32 +02:00
|
|
|
enum class error : int {
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The packet is malformed */
|
2023-10-27 10:41:15 +02:00
|
|
|
malformed_packet = 100,
|
2023-10-13 09:07:56 +02:00
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The packet has exceeded the Maximum Packet Size the Server is willing to accept */
|
2023-12-22 09:17:45 +01:00
|
|
|
packet_too_large,
|
|
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The Client's session does not exist or it has expired */
|
2023-11-29 11:50:07 +01:00
|
|
|
session_expired,
|
|
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
/** There are no more available Packet Identifiers to use */
|
2023-10-05 13:59:32 +02:00
|
|
|
pid_overrun,
|
2023-10-13 09:07:56 +02:00
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The Topic is invalid and does not conform to the specification */
|
2023-11-29 11:50:07 +01:00
|
|
|
invalid_topic,
|
2023-11-23 15:36:06 +01:00
|
|
|
|
2023-10-13 09:07:56 +02:00
|
|
|
// publish
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The Server does not support the specified \ref qos_e */
|
2023-10-05 13:59:32 +02:00
|
|
|
qos_not_supported,
|
2023-10-13 09:07:56 +02:00
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The Server does not support retained messages */
|
2023-10-05 13:59:32 +02:00
|
|
|
retain_not_available,
|
2023-10-13 09:07:56 +02:00
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The Client attempted to send a Topic Alias that is greater than Topic Alias Maximum */
|
2023-12-13 15:13:07 +01:00
|
|
|
topic_alias_maximum_reached,
|
|
|
|
|
|
|
|
|
|
// subscribe
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The Server does not support Wildcard Subscriptions */
|
2023-12-13 15:13:07 +01:00
|
|
|
wildcard_subscription_not_available,
|
|
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The Server does not support this Subscription Identifier */
|
2023-12-13 15:13:07 +01:00
|
|
|
subscription_identifier_not_available,
|
|
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
/** The Server does not support Shared Subscriptions */
|
2024-02-05 07:53:47 +01:00
|
|
|
shared_subscription_not_available
|
2023-10-05 13:59:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline std::string client_error_to_string(error err) {
|
|
|
|
|
switch (err) {
|
2023-12-06 08:25:12 +01:00
|
|
|
case error::malformed_packet:
|
2024-02-05 15:25:05 +01:00
|
|
|
return "The packet is malformed";
|
2023-12-22 09:17:45 +01:00
|
|
|
case error::packet_too_large:
|
|
|
|
|
return "The packet has exceeded the Maximum Packet Size "
|
2024-02-05 15:25:05 +01:00
|
|
|
"the Server is willing to accept";
|
2023-12-06 08:25:12 +01:00
|
|
|
case error::session_expired:
|
2024-02-05 15:25:05 +01:00
|
|
|
return "The Client's session does not exist or it has expired";
|
2023-12-06 08:25:12 +01:00
|
|
|
case error::pid_overrun:
|
2024-02-05 15:25:05 +01:00
|
|
|
return "There are no more available Packet Identifiers to use";
|
2023-12-06 08:25:12 +01:00
|
|
|
case error::invalid_topic:
|
2023-11-29 11:50:07 +01:00
|
|
|
return "The Topic is invalid and "
|
2024-02-05 15:25:05 +01:00
|
|
|
"does not conform to the specification";
|
2023-12-06 08:25:12 +01:00
|
|
|
case error::qos_not_supported:
|
2024-02-05 15:25:05 +01:00
|
|
|
return "The Server does not support the specified QoS";
|
2023-12-06 08:25:12 +01:00
|
|
|
case error::retain_not_available:
|
2024-02-05 15:25:05 +01:00
|
|
|
return "The Server does not support retained messages";
|
2023-12-06 08:25:12 +01:00
|
|
|
case error::topic_alias_maximum_reached:
|
2023-10-05 13:59:32 +02:00
|
|
|
return "The Client attempted to send a Topic Alias "
|
2024-02-05 15:25:05 +01:00
|
|
|
"that is greater than Topic Alias Maximum";
|
2023-12-13 15:13:07 +01:00
|
|
|
case error::wildcard_subscription_not_available:
|
2024-02-05 15:25:05 +01:00
|
|
|
return "The Server does not support Wildcard Subscriptions";
|
2023-12-13 15:13:07 +01:00
|
|
|
case error::subscription_identifier_not_available:
|
2024-02-05 15:25:05 +01:00
|
|
|
return "The Server does not support this Subscription Identifier";
|
2023-12-13 15:13:07 +01:00
|
|
|
case error::shared_subscription_not_available:
|
2024-02-05 15:25:05 +01:00
|
|
|
return "The Server does not support Shared Subscriptions";
|
2023-10-05 13:59:32 +02:00
|
|
|
default:
|
2024-02-05 15:25:05 +01:00
|
|
|
return "Unknown client error";
|
2023-10-05 13:59:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct client_ec_category : public boost::system::error_category {
|
|
|
|
|
const char* name() const noexcept override { return "mqtt_client_error"; }
|
|
|
|
|
std::string message(int ev) const noexcept override {
|
|
|
|
|
return client_error_to_string(static_cast<error>(ev));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-18 12:16:05 +02:00
|
|
|
/// Returns the error category associated with \ref client::error.
|
2023-10-05 13:59:32 +02:00
|
|
|
inline const client_ec_category& get_error_code_category() {
|
|
|
|
|
static client_ec_category cat;
|
|
|
|
|
return cat;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-18 12:16:05 +02:00
|
|
|
/// Creates an \ref error_code from a \ref client::error.
|
2023-10-05 13:59:32 +02:00
|
|
|
inline boost::system::error_code make_error_code(error r) {
|
|
|
|
|
return { static_cast<int>(r), get_error_code_category() };
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 15:25:05 +01:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, const error& err) {
|
|
|
|
|
os << get_error_code_category().name() << ":" << static_cast<int>(err);
|
|
|
|
|
return os;
|
|
|
|
|
}
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
|
} // end namespace client
|
|
|
|
|
|
|
|
|
|
} // end namespace async_mqtt5
|
|
|
|
|
|
|
|
|
|
namespace boost::system {
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
struct is_error_code_enum <async_mqtt5::client::error> : std::true_type {};
|
|
|
|
|
|
|
|
|
|
} // end namespace boost::system
|
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
#endif // !ASYNC_MQTT5_ERROR_HPP
|