/*
  ept.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 "ept.h"
#include "rdmnet/rdmnet.h"
#include "rpt/rpt.h"

namespace RDMnet::EPT {

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


/**
 * @brief EPT::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);   // flags, length, and vector
  createHeader<ept_header>();     // header
  switch (vector())               // data
    {
    case VECTOR_EPT_DATA:
      createData<DATA::Pdu>();
      break;
    case VECTOR_EPT_STATUS:
      createData<STATUS::Pdu>();
      break;
    default:
      return;
    }
}


namespace DATA {

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


/**
 * @brief EPT::DATA::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);   // flags, length, and vector
  // has no header segment        // header
  createData<data_data>();        // data
}


/**
 * @brief data_data::iStream
 * @param stream
 */
void data_data::iStream(ACN::PDU::Stream stream)
{
  while (stream->good())
    data.push_back(stream->readType<uint8_t>());
}


/**
 * @brief data_data::oStream
 * @param stream
 */
void data_data::oStream(ACN::PDU::Stream stream) const
{
  for (const auto & byte : data)
    *stream << byte;
}

} // namespace DATA


namespace STATUS {

/**
 * @brief EPT::STATUS::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(2)
{
}


/**
 * @brief EPT::STATUS::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);                   // flags, length, and vector
  // has no header segment                        // header
  createData<RPT::STATUS::status_string_data>();  // data
}

} // namespace STATUS


} // namespace RDMnet::EPT
