120 lines
4.6 KiB
C++
120 lines
4.6 KiB
C++
#ifndef INCLUDE_CONTROL_PACKET_PROPERTY_HPP_
|
|
#define INCLUDE_CONTROL_PACKET_PROPERTY_HPP_
|
|
|
|
#include <binary_data.hpp>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <ostream>
|
|
#include <queue>
|
|
#include <spdlog/spdlog.h>
|
|
#include <types.hpp>
|
|
#include <utf8_string.hpp>
|
|
#include <utf8_string_pair.hpp>
|
|
#include <variable_byte_int.hpp>
|
|
#include <variant>
|
|
|
|
/**
|
|
* @brief Enumeration of MQTT Control Packet Property Identifiers.
|
|
*/
|
|
enum class PropertyIdentifier : uint32_t {
|
|
PAYLOAD_FORMAT_INDICATOR = 1, /**< Payload Format Indicator. */
|
|
MESSAGE_EXPIRY_INTERVAL = 2, /**< Message Expiry Interval. */
|
|
CONTENT_TYPE = 3, /**< Content Type. */
|
|
RESPONSE_TOPIC = 8, /**< Response Topic. */
|
|
CORRELATION_DATA = 9, /**< Correlation Data. */
|
|
SUBSCRIPTION_IDENTIFIER = 11, /**< Subscription Identifier. */
|
|
SESSION_EXPIRY_INTERVAL = 17, /**< Session Expiry Interval. */
|
|
ASSIGNED_CLIENT_IDENTIFIER = 18, /**< Assigned Client Identifier. */
|
|
SERVER_KEEP_ALIVE = 19, /**< Server Keep Alive. */
|
|
AUTHENTICATION_METHOD = 21, /**< Authentication Method. */
|
|
AUTHENTICATION_DATA = 22, /**< Authentication Data. */
|
|
REQUEST_PROBLEM_INFORMATION = 23, /**< Request Problem Information. */
|
|
WILL_DELAY_INTERVAL = 24, /**< Will Delay Interval. */
|
|
REQUEST_RESPONSE_INFORMATION = 25, /**< Request Response Information. */
|
|
RESPONSE_INFORMATION = 26, /**< Response Information. */
|
|
SERVER_REFERENCE = 28, /**< Server Reference. */
|
|
REASON_STRING = 31, /**< Reason String. */
|
|
RECEIVE_MAXIMUM = 33, /**< Receive Maximum. */
|
|
TOPIC_ALIAS_MAXIMUM = 34, /**< Topic Alias Maximum. */
|
|
TOPIC_ALIAS = 35, /**< Topic Alias. */
|
|
MAXIMUM_QOS = 36, /**< Maximum QoS. */
|
|
RETAIN_AVAILABLE = 37, /**< Retain Available. */
|
|
USER_PROPERTY = 38, /**< User Property. */
|
|
MAXIMUM_PACKET_SIZE = 39, /**< Maximum Packet Size. */
|
|
WILDCARD_SUBSCRIPTION_AVAILABLE = 40, /**< Wildcard Subscription Available. */
|
|
SUBSCRIPTION_IDENTIFIER_AVAILABLE = 41, /**< Subscription Identifier Available. */
|
|
SHARED_SUBSCRIPTION_AVAILABLE = 42 /**< Shared Subscription Available. */
|
|
};
|
|
|
|
using UserProperty = std::queue<UTF8StringPair>; /**< User Property type definition. */
|
|
using PropertyValue = std::variant<std::byte, uint32_t, uint16_t, int_vb, binary_data, utf8_str, UserProperty>; /**< Property Value type definition. */
|
|
using PropertyMap = std::map<PropertyIdentifier, PropertyValue>; /**< Property Map type definition. */
|
|
|
|
std::ostream &operator<<(std::ostream &os, const UserProperty &value);
|
|
std::ostream &operator<<(std::ostream &os, const PropertyIdentifier &value);
|
|
std::ostream &operator<<(std::ostream &os, const PropertyValue &value);
|
|
std::ostream &operator<<(std::ostream &os, const PropertyMap &value);
|
|
|
|
/**
|
|
* @brief Class representing MQTT Control Packet Properties.
|
|
*/
|
|
class MQTTProperties {
|
|
public:
|
|
/**
|
|
* @brief Default constructor.
|
|
*/
|
|
MQTTProperties();
|
|
|
|
/**
|
|
* @brief Constructs MQTTProperties from a byte vector.
|
|
*
|
|
* @param property_start Iterator pointing to the start of the property data in the byte vector.
|
|
*/
|
|
MQTTProperties(const std::vector<std::byte>::const_iterator &property_start);
|
|
|
|
/**
|
|
* @brief Destructor.
|
|
*/
|
|
~MQTTProperties() = default;
|
|
|
|
/**
|
|
* @brief Converts MQTTProperties to a byte vector.
|
|
*
|
|
* @return Byte vector representation of MQTTProperties.
|
|
*/
|
|
std::vector<std::byte> as_bytes() const;
|
|
|
|
/**
|
|
* @brief Gets the size of MQTTProperties.
|
|
*
|
|
* @return The size of MQTTProperties.
|
|
*/
|
|
uint16_t size() const { return static_cast<uint16_t>(this->length); };
|
|
|
|
/**
|
|
* @brief Gets the property value for the specified property identifier.
|
|
*
|
|
* @param prop The property identifier.
|
|
* @return The property value.
|
|
*/
|
|
PropertyValue get_property(const PropertyIdentifier &prop) { return this->properties[prop]; };
|
|
|
|
/**
|
|
* @brief Overloads the subscript operator to access properties by identifier.
|
|
*
|
|
* @param prop The property identifier.
|
|
* @return The property value.
|
|
*/
|
|
PropertyValue &operator[](PropertyIdentifier prop);
|
|
|
|
friend std::ostream &operator<<(std::ostream &os, const std::byte &value);
|
|
friend std::ostream &operator<<(std::ostream &os, const MQTTProperties &value);
|
|
|
|
private:
|
|
int_vb length; /**< Length of the properties. */
|
|
PropertyMap properties; /**< Map containing MQTT properties. */
|
|
};
|
|
|
|
#endif // INCLUDE_CONTROL_PACKET_PROPERTY_HPP_
|