/*
  llrp.cpp

  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.
*/

#include "llrp.h"
#include "rpt/rpt.h"

namespace RDMnet::LLRP {

/**
 * @brief llrp_data::iStream
 * @param stream
 */
void llrp_data::iStream(ACN::PDU::Stream stream)
{
  /// Destination CID - The Destination CID indicates the CID of the intended
  /// recipient of this PDU.
  *stream >> destination;

  /// Transaction Number - The Transaction Number allows an LLRP Manager to
  /// associate reply messages with requests.
  *stream >> transaction;
}


/**
 * @brief llrp_data::oStream
 * @param stream
 */
void llrp_data::oStream(ACN::PDU::Stream stream) const
{
  *stream << destination;
  *stream << transaction;
  if (child)
    *stream << child;
}


/**
 * @brief LLRP::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(4)
{
}


/**
 * @brief LLRP::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);   // flags, length, and vector
  // hase no header               // header
  createData<llrp_data>();        // data

  auto payload = static_cast<llrp_data*>(data_);
  switch (vector_) {
    case VECTOR_LLRP_PROBE_REQUEST:
      {
        auto block = ACN::PDU::Block<ProbeRequest::Pdu>();
        *stream_ >> block;
        if (!block.pdu->empty())
          payload->child = block.pdu->front();
      }
      break;
    case VECTOR_LLRP_PROBE_REPLY:
      {
        auto block = ACN::PDU::Block<ProbeReply::Pdu>();
        *stream_ >> block;
        if (!block.pdu->empty())
          payload->child = block.pdu->front();
      }
      break;
    case VECTOR_LLRP_RDM_CMD:
      {
        auto block = ACN::PDU::Block<RPT::COMMAND::Pdu>();
        *stream_ >> block;
        if (!block.pdu->empty())
          payload->child = block.pdu->front();
      }
      break;
    default:
      stream_->setstate(std::ios_base::failbit);
      return;
    }

  if (!payload->child)
    stream_->setstate(std::ios_base::failbit);
}


namespace ProbeRequest {

/**
 * @brief request_data::iStream
 * @param stream
 */
void request_data::iStream(ACN::PDU::Stream stream)
{
  *stream >> lower;
  *stream >> upper;
  *stream >> filter.value;
  while (stream->good())
    {
      known.emplace_back(RDM::UID());
      *stream >> known.back();
    }
}


/**
 * @brief request_data::oStream
 * @param stream
 */
void request_data::oStream(ACN::PDU::Stream stream) const
{
  *stream << lower;
  *stream << upper;
  *stream << filter.value;
  for (const auto& id : known)
    *stream << id;
}


/**
 * @brief LLRP::ProbeRequest::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(1)
{
}


/**
 * @brief LLRP::ProbeRequest::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);   // flags, length, and vector
  // hase no header               // header
  createData<request_data>();     // data
}

} // namespace ProbeRequest


namespace ProbeReply {

/**
 * @brief reply_data::iStream
 * @param stream
 */
void reply_data::iStream(ACN::PDU::Stream stream)
{
  *stream >> id;
  stream->read(address, sizeof(address));
  if (stream->gcount() != sizeof(address))
    return stream->setstate(std::ios_base::failbit);
  *stream >> type;
}


/**
 * @brief reply_data::oStream
 * @param stream
 */
void reply_data::oStream(ACN::PDU::Stream stream) const
{
  *stream << id;
  stream->write(address, sizeof(address));
  *stream << type;
}


/**
 * @brief LLRP::ProbeReply::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(1)
{
}


/**
 * @brief LLRP::ProbeReply::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);   // flags, length, and vector
  // hase no header               // header
  createData<reply_data>();       // data
}

} // namespace ProbeReply

} // namespace RDMnet::LLRP
