/*
  test_sacn.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 <gtest/gtest.h>

#include "sacn/node.h"

class sACNdataTest
    : public ::testing::Test
{
 protected:
  sACNdataTest()
  {
    // Root Layer (See Section 5)
    // Preamble Size
    e131_data[1] = 0x10;
    // Post-amble Size
    // ACN Packet Identifier
    e131_data[4] = 0x41;
    e131_data[5] = 0x53;
    e131_data[6] = 0x43;
    e131_data[7] = 0x2d;
    e131_data[8] = 0x45;
    e131_data[9] = 0x31;
    e131_data[10] = 0x2e;
    e131_data[11] = 0x31;
    e131_data[12] = 0x37;
    // Flags and Length
    e131_data[16] = 0x72;
    e131_data[17] = 0x6e;
    // Vector
    e131_data[21] = 0x04; //!< VECTOR_ROOT_E131_DATA
    // CID
    // E1.31 Framing Layer (See Section 6)
    // Flags and Length
    e131_data[38] = 0x72;
    e131_data[39] = 0x58;
    // Vector
    e131_data[43] = 0x02; //!< VECTOR_E131_DATA_PACKET
    // Source Name
    // Priority
    e131_data[108] = 0x64;  //!< priority 100
    // Synchronization Address
    // Sequence Number
    // Options
    // Universe
    // DMP Layer (See Section 7)
    // Flags and Length
    e131_data[115] = 0x72;
    e131_data[116] = 0x0b;
    // Vector
    e131_data[117] = 0x02; //!< VECTOR_DMP_SET_PROPERTY
    // Address Type & Data Type
    e131_data[118] = 0xa1;
    // First Property Address
    // Address Increment
    e131_data[122] = 0x01;
    // Property value count
    e131_data[123] = 0x02;
    e131_data[124] = 0x01;
    // Property values
  }

  SACN::Node node;
  uint8_t e131_data[638] = {0}; //!< Table 4-1: E1.31 Data Packet
};


TEST_F(sACNdataTest, stream_in) {
  // random CID
  auto cid = UUID::uuid();
  cid.uuid4();
  for (int i = 0; i < UUID_LENGTH ; i++)
    e131_data[22+i] = cid.bytes()[i];

  // source description
  auto desc = std::string("test suite's really long name that is exactly 64 characters lng");
  for (size_t s = 0; s < (desc.size() > 64 ? 64 : desc.size()); s++)
    e131_data[44+s] = desc.at(s);

  uint8_t priority = 200;
  e131_data[108] = priority;

  // Universe
  uint16_t univ = 64000;
  e131_data[113] = univ >> 8;    // high
  e131_data[114] = univ & 0xff; // low
  node.subscribe(univ);

  // univ low byte on channel 512
  e131_data[637] = univ & 0xff;

  // send enough to get the rate up
  for (int r = 0; r < 3; r++)
    {
      ACN::PDU::Stream stream(new ACN::PDU::pdu_stream(e131_data, sizeof(e131_data)));
      ASSERT_EQ(stream->data(), e131_data) << "failed to create stream";
      node.UdpPayloadReceiver(stream);
    }

  auto universe = node.Receiver::universe(univ);
  ASSERT_TRUE(universe) << "test setup failure";

  EXPECT_DOUBLE_EQ(universe->rxRate(), 1.2) << "rate calucation mismatch";

  auto source = universe->source();
  ASSERT_TRUE(source) << "stream failure";

  EXPECT_EQ(source->CID(), cid) << "UUID mismatch";
  EXPECT_EQ(source->description(), desc) << "Source Description mismatch";
  EXPECT_EQ(source->universe(), univ) << "universe number mismatch";
  EXPECT_EQ(source->priority(), priority) << "priority mismatch";
  EXPECT_EQ(source->syncAddress(), 0) << "sync address mismatch";
  EXPECT_EQ(universe->slot(512), (univ & 0xff)) << "slot 512 data mismatch";

  node.unsubscribe(univ);
}
