/*
  extended.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 "rlp/rlp.h"
#include "extended.h"
#include "config.h"

namespace SACN {
namespace EXTENDED {

/**
 * @brief frame_sync_header::iStream
 * @param stream
 */
void sync_header::iStream(ACN::PDU::Stream stream)
{
  *stream >> sequence_number;
  *stream >> sync_address;
  *stream >> reserved;
}


/**
 * @brief sync_header::oStream
 * @param stream
 */
void sync_header::oStream(ACN::PDU::Stream stream) const
{
  *stream << sequence_number;
  *stream << sync_address;
  *stream << reserved;
}


/**
 * @brief discovery_header::iStream
 * @param stream
 */
void discovery_header::iStream(ACN::PDU::Stream stream)
{
  stream->readString(source_name, 64);
  *stream >> reserved;
}


/**
 * @brief discovery_header::oStream
 * @param stream
 */
void discovery_header::oStream(ACN::PDU::Stream stream) const
{
  stream->writeString(source_name, 64);
  *stream << reserved;
}


/**
 * @brief Pdu::Pdu
 * @param stream
 */
Pdu::Pdu()
  : ACN::PDU::Pdu(4) // vectors are 4 octets
{
}

void Pdu::iStream(ACN::PDU::Stream stream)
{
  ACN::PDU::Pdu::iStream(stream);         // flags, length, and vector
  switch(vector_)
    {
    case VECTOR_E131_EXTENDED_SYNCHRONIZATION:
      createHeader<sync_header>();        // header
      break;  // has no data              // data
    case VECTOR_E131_EXTENDED_DISCOVERY:
      createHeader<discovery_header>();   // header
      createDataBlock<DISCOVERY::Pdu>();  // data
      break;
    default:
      break;
    }
}

namespace DISCOVERY {

/**
 * @brief discovery_list_header::iStream
 * @param stream
 */
void discovery_list_header::iStream(ACN::PDU::Stream stream)
{
  *stream >> page;
  *stream >> last_page;
}


/**
 * @brief discovery_list_header::oStream
 * @param stream
 */
void discovery_list_header::oStream(ACN::PDU::Stream stream) const
{
  *stream << page;
  *stream << last_page;
}


/**
 * @brief discovery_list_data::iStream
 * @param stream
 */
void discovery_list_data::iStream(ACN::PDU::Stream stream)
{
  while (stream->good())
    {
      found.emplace_back(discoveredUniverse());
      *stream >> found.back().universe;
    }
}


/**
 * @brief discovery_list_data::oStream
 * @param stream
 */
void discovery_list_data::oStream(ACN::PDU::Stream stream) const
{
  for (auto & d : found)
    *stream << d.universe;
}


/**
 * @brief Pdu::Pdu
 * @param stream
 */
Pdu::Pdu()
  : ACN::PDU::Pdu(4) // vectors are 4 octets
{
}


/**
 * @brief Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  ACN::PDU::Pdu::iStream(stream);         // flags, length, and vector
  createHeader<discovery_list_header>();  // header
  createData<discovery_list_data>();      // data

  if (data_)
    {
      auto data = static_cast<discovery_list_data*>(data_);
      auto root_header = static_cast<ACN::RLP::rlp_header*>(parent_->parent()->header());
      auto frame_header = static_cast<EXTENDED::discovery_header*>(parent_->header());
      for ( auto & f : data->found )
        {
          f.source = root_header->cid;
          f.description = frame_header->source_name;
        }
    }
}

} // DISCOVERY
} // EXTENDED
} // SACN
