File rtps_types.hpp

File List > astutedds > rtps > rtps_types.hpp

Go to the documentation of this file


#ifndef ASTUTEDDS_RTPS_TYPES_HPP
#define ASTUTEDDS_RTPS_TYPES_HPP

#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>

namespace astutedds::rtps {

// RTPS Protocol Version (9.3.1)
struct ProtocolVersion_t {
    uint8_t major{2};
    uint8_t minor{5};  // RTPS 2.5
};

// RTPS Vendor ID (9.3.2)
struct VendorId_t {
    std::array<uint8_t, 2> vendorId{{0x01, 0x01}};  // Placeholder vendor ID
};

// RTPS GUID (9.3.3)
struct GuidPrefix_t {
    std::array<uint8_t, 12> value{};
};

struct EntityId_t {
    std::array<uint8_t, 3> entityKey{};
    uint8_t entityKind{0};

    // Predefined Entity IDs (9.3.3.1)
    static constexpr EntityId_t ENTITYID_UNKNOWN() {
        return EntityId_t{{0x00, 0x00, 0x00}, 0x00};
    }

    static constexpr EntityId_t ENTITYID_PARTICIPANT() {
        return EntityId_t{{0x00, 0x00, 0x01}, 0xc1};
    }

    static constexpr EntityId_t ENTITYID_SEDP_BUILTIN_TOPICS_ANNOUNCER() {
        return EntityId_t{{0x00, 0x00, 0x02}, 0xc2};
    }

    static constexpr EntityId_t ENTITYID_SEDP_BUILTIN_TOPICS_DETECTOR() {
        return EntityId_t{{0x00, 0x00, 0x02}, 0xc7};
    }

    static constexpr EntityId_t ENTITYID_SEDP_BUILTIN_PUBLICATIONS_ANNOUNCER() {
        return EntityId_t{{0x00, 0x00, 0x03}, 0xc2};
    }

    static constexpr EntityId_t ENTITYID_SEDP_BUILTIN_PUBLICATIONS_DETECTOR() {
        return EntityId_t{{0x00, 0x00, 0x03}, 0xc7};
    }

    static constexpr EntityId_t ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_ANNOUNCER() {
        return EntityId_t{{0x00, 0x00, 0x04}, 0xc2};
    }

    static constexpr EntityId_t ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_DETECTOR() {
        return EntityId_t{{0x00, 0x00, 0x04}, 0xc7};
    }

    static constexpr EntityId_t ENTITYID_SPDP_BUILTIN_PARTICIPANT_ANNOUNCER() {
        return EntityId_t{{0x00, 0x01, 0x00}, 0xc2};
    }

    static constexpr EntityId_t ENTITYID_SPDP_BUILTIN_PARTICIPANT_DETECTOR() {
        return EntityId_t{{0x00, 0x01, 0x00}, 0xc7};
    }
};

struct GUID_t {
    GuidPrefix_t guidPrefix{};
    EntityId_t entityId{};

    // Comparison operator for use in maps
    bool operator<(const GUID_t& other) const {
        int prefix_cmp = std::memcmp(guidPrefix.value.data(), 
                                     other.guidPrefix.value.data(), 12);
        if (prefix_cmp != 0) return prefix_cmp < 0;

        int key_cmp = std::memcmp(entityId.entityKey.data(),
                                 other.entityId.entityKey.data(), 3);
        if (key_cmp != 0) return key_cmp < 0;

        return entityId.entityKind < other.entityId.entityKind;
    }
};

// Sequence Number (9.3.4)
struct SequenceNumber_t {
    int32_t high{0};
    uint32_t low{0};

    static constexpr SequenceNumber_t SEQUENCENUMBER_UNKNOWN() {
        return SequenceNumber_t{-1, 0};
    }
};

// Locator (9.3.5)
enum class LocatorKind_t : int32_t {
    LOCATOR_KIND_INVALID = -1,
    LOCATOR_KIND_RESERVED = 0,
    LOCATOR_KIND_UDPv4 = 1,
    LOCATOR_KIND_UDPv6 = 2
};

struct Locator_t {
    LocatorKind_t kind{LocatorKind_t::LOCATOR_KIND_INVALID};
    uint32_t port{0};
    std::array<uint8_t, 16> address{};
};

using LocatorList_t = std::vector<Locator_t>;

// Timestamp (9.3.6)
struct Time_t {
    constexpr Time_t() = default;
    constexpr Time_t(int32_t s, uint32_t f) : seconds(s), fraction(f) {}
    int32_t seconds{0};
    uint32_t fraction{0};  // Fraction of a second in (1/2^32) units
    uint32_t nanosec{0};   // DDS PSM API: nanosecond representation

    bool operator==(const Time_t&) const = default;
    bool operator!=(const Time_t&) const = default;

    static constexpr Time_t TIME_INVALID() {
        return Time_t{-1, 0xffffffff};
    }

    static constexpr Time_t TIME_ZERO() {
        return Time_t{0, 0};
    }

    static constexpr Time_t TIME_INFINITE() {
        return Time_t{0x7fffffff, 0xffffffff};
    }
};

// Duration (similar to Time_t but used for durations)
using Duration_t = Time_t;

// Count (9.3.7)
using Count_t = int32_t;

// Parameter List (9.4.2.11)
struct ParameterId_t {
    uint16_t value{0};

    // Standard Parameter IDs
    static constexpr uint16_t PID_PAD = 0x0000;
    static constexpr uint16_t PID_SENTINEL = 0x0001;
    static constexpr uint16_t PID_PROTOCOL_VERSION = 0x0015;
    static constexpr uint16_t PID_VENDOR_ID = 0x0016;
    static constexpr uint16_t PID_PARTICIPANT_GUID = 0x0050;
    static constexpr uint16_t PID_ENDPOINT_GUID = 0x005a;
    static constexpr uint16_t PID_TOPIC_NAME = 0x0005;
    static constexpr uint16_t PID_TYPE_NAME = 0x0007;
    static constexpr uint16_t PID_UNICAST_LOCATOR = 0x002f;
    static constexpr uint16_t PID_MULTICAST_LOCATOR = 0x0030;
    static constexpr uint16_t PID_DEFAULT_UNICAST_LOCATOR = 0x0031;
    static constexpr uint16_t PID_DEFAULT_MULTICAST_LOCATOR = 0x0048;
    static constexpr uint16_t PID_METATRAFFIC_UNICAST_LOCATOR = 0x0032;
    static constexpr uint16_t PID_METATRAFFIC_MULTICAST_LOCATOR = 0x0033;
};

struct Parameter {
    ParameterId_t parameterId{};
    uint16_t length{0};
    std::vector<uint8_t> value;
};

using ParameterList = std::vector<Parameter>;

// Submessage Kinds (9.4.5.1.1)
enum class SubmessageKind : uint8_t {
    PAD = 0x01,
    ACKNACK = 0x06,
    HEARTBEAT = 0x07,
    GAP = 0x08,
    INFO_TS = 0x09,
    INFO_SRC = 0x0c,
    INFO_REPLY_IP4 = 0x0d,
    INFO_DST = 0x0e,
    INFO_REPLY = 0x0f,
    NACK_FRAG = 0x12,
    HEARTBEAT_FRAG = 0x13,
    DATA = 0x15,
    DATA_FRAG = 0x16
};

// Submessage Header (9.4.5.1)
struct SubmessageHeader {
    SubmessageKind submessageId{};
    uint8_t flags{0};
    uint16_t submessageLength{0};
};

// Change Kind (for cache changes)
enum class ChangeKind_t : uint32_t {
    ALIVE = 0,
    ALIVE_FILTERED = 1,
    NOT_ALIVE_DISPOSED = 2,
    NOT_ALIVE_UNREGISTERED = 3
};

} // namespace astutedds::rtps

#endif // ASTUTEDDS_RTPS_TYPES_HPP