/*
  rpt.h

  Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/
#pragma once

#include "rdmnet/pdu.h"
#include "rdm/message.h"
#include "rdm/uid.h"

namespace RDMnet::RPT {

/**
 * @brief 1.2.3 RDM Packet Transport (RPT)
 *
 *  RPT transports RDM messages over IP and supports a multi-controller
 *  environment. RPT equipment relies on initial configuration by LLRP, or by
 *  alternative means, to operate correctly. The RPT protocol is designed to
 *  scale communication to tens of thousands of Components via use of the Broker
 *  Protocol. The ultimate limit to the size of a system is determined by the
 *  infrastructure and not by the protocol itself.
 */


/**
 * @brief 3.3.2 Dynamic UID Request
 * RPT Components wishing to obtain Dynamic UIDs from the Broker use a special
 * Dynamic UID value known as a Dynamic UID Request.
 */
extern const RDM::UID DynamicUidRequest;

namespace REQUEST {

/**
 * @brief The RPT::REQUEST::Pdu class
 */
class Pdu
  : public RDMnet::Pdu
{
public:
  Pdu();
  void iStream(ACN::PDU::Stream) override;
};

} // namespace REQUEST


namespace STATUS {

/**
 * @brief The RPT::STATUS::Pdu class
 */
class Pdu
  : public RDMnet::Pdu
{
public:
  Pdu();
  void iStream(ACN::PDU::Stream) override;
};


/**
 * @brief 7.5.3.1.3 Status String
 */
struct status_string_data
    : ACN::PDU::pdu_data
{
  std::string status_string;  //!< status message

  size_t streamSize() const override;
  void   iStream(ACN::PDU::Stream) override;
  void   oStream(ACN::PDU::Stream) const override;
};

} // namespace STATUS


namespace NOTIFICATION {

/**
 * @brief The RPT::NOTIFICATION::Pdu class
 */
class Pdu
  : public RDMnet::Pdu
{
public:
  Pdu();
  void iStream(ACN::PDU::Stream) override;
};


} // namespace NOTIFICATION


namespace COMMAND {

/**
 * @brief The RPT::COMMAND::Pdu class
 */
class Pdu
  : public RDMnet::Pdu
{
public:
  Pdu();
  void iStream(ACN::PDU::Stream) override;
};


/**
 * @brief The command_data struct
 */
struct command_data
    : public ACN::PDU::pdu_data
{
  RDM::Message message; //!< RDM message

  size_t streamSize() const override { return 23 + message.length(); }
  void   iStream(ACN::PDU::Stream) override;
  void   oStream(ACN::PDU::Stream) const override;
};

} // namespace COMMAND


/**
 * @brief 7.5.1 RPT PDU
 */
class Pdu
  : public RDMnet::Pdu
{
public:
  Pdu();
  void iStream(ACN::PDU::Stream) override;
};


/**
 * @brief The rpt_header struct
 */
struct rpt_header
    : ACN::PDU::pdu_header
{
  RDM::UID source;                //!< source UID
  uint16_t source_endpoint;       //!< source endpoint number
  RDM::UID destination;           //!< destination UID
  uint16_t destination_endpoint;  //!< destination endpoint number

  size_t streamSize() const override { return 16; }
  void   iStream(ACN::PDU::Stream) override;
  void   oStream(ACN::PDU::Stream) const override;
};


/**
 * @brief The rpt_data struct
 */
struct rpt_data
    : ACN::PDU::pdu_data
{
  uint32_t sequence; //!< sequence number
  uint8_t  reserved; //!< reserved
  std::shared_ptr<RDMnet::Pdu> pdu; //!< RDMnet PDU

  size_t streamSize() const override { return 5 + pdu->streamSize(); }
  void   iStream(ACN::PDU::Stream) override;
  void   oStream(ACN::PDU::Stream) const override;
};

} // namespace RDMnet::RPT
