//------------------------------------------------------------------------------
// <copyright file="PayloadProtocol.cpp" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// 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.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// </summary>
//
// <owner></owner>
//------------------------------------------------------------------------------


#include "PayloadProtocol.h"
#include "LqrSMACByteArray.h"
#include "LqrSMACByteStream.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace PayloadInterface {

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::ACK_PYLD_POWER_NOTIFICATION_RECORD& data)
{
	bs >> data.address
	   >> data.boardID
	   >> data.state
	   >> data.delayOff
	   >> data.delayOn;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::ACK_PYLD_POWER_NOTIFICATION_RECORD& data)
{
	bs << data.address
	   << data.boardID
	   << data.state
	   << data.delayOff
	   << data.delayOn;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::PYLD_POWER_NOTIFICATION& data)
{
	bs >> data.notificationType
	   >> data.boardID
	   >> data.newState;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::PYLD_POWER_NOTIFICATION& data)
{
	bs << data.notificationType
	   << data.boardID
	   << data.newState;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRI_REQUEST_ID& data)
{
	bs >> data.communicationProtocols
	   >> data.commandFormats;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRI_REQUEST_ID& data)
{
	bs << data.communicationProtocols
	   << data.commandFormats;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRI_GENERIC_ACK& data)
{
	bs >> data.originalCmd;
	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRI_GENERIC_ACK& data)
{
	bs << data.originalCmd;
	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRI_ACK_COUNT_WRAPPER& data)
{
	bs >> data.totalNumber
	   >> data.numberOfReplies;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRI_ACK_COUNT_WRAPPER& data)
{
	bs << data.totalNumber
	   << data.numberOfReplies;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::RESPONSE_HEADER_WITH_FORMAT& data)
{
	bs >> data.cmdFormat;
	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::RESPONSE_HEADER_WITH_FORMAT& data)
{
	bs << data.cmdFormat;
	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRIGenericAck& data)
{
	bs >> (PayloadProtocol::LRI_GENERIC_ACK&) data;
	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRIGenericAckWithFormat& data)
{
	bs >> (PayloadProtocol::LRIGenericAck&) data
	   >> (PayloadProtocol::RESPONSE_HEADER_WITH_FORMAT&) data;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRIGenericAckWithFormat& data)
{
	bs << (const PayloadProtocol::LRIGenericAck&) data
	   << (const PayloadProtocol::RESPONSE_HEADER_WITH_FORMAT&) data;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRIGenericAck& data)
{
	bs << (const PayloadProtocol::LRI_GENERIC_ACK&) data;
	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRIAckCountWrapper& data)
{
	bs >> (PayloadProtocol::LRIGenericAck&) data
	   >> (PayloadProtocol::LRI_ACK_COUNT_WRAPPER&) data;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRIAckCountWrapper& data)
{
	bs << (const PayloadProtocol::LRIGenericAck&) data
	   << (const PayloadProtocol::LRI_ACK_COUNT_WRAPPER&) data;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRIGenericNak& data)
{
	bs >> (PayloadProtocol::LRIGenericAck&) data
	   >> data.nakSummary;

	const size_t countRemaining = bs.CountRemaining();
	ContainerIn<ByteArray> container(data.additionalData, countRemaining);
	bs >> container;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRIGenericNak& data)
{
	bs << (const PayloadProtocol::LRIGenericAck&) data
	   << data.nakSummary
	   << ContainerOut<ByteArray>(data.additionalData);

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRI_REQUEST_PIB_STATUS& data)
{
	bs >> data.timestamp
	   >> data.latitude
	   >> data.longitude
	   >> data.fixValid;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRI_REQUEST_PIB_STATUS& data)
{
	bs << data.timestamp
	   << data.latitude
	   << data.longitude
	   << data.fixValid;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::TIMESTAMP& data)
{
	bs >> data.second
	   >> data.minute
	   >> data.hour
	   >> data.day
	   >> data.month
	   >> data.year;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::TIMESTAMP& data)
{
	bs << data.second
	   << data.minute
	   << data.hour
	   << data.day
	   << data.month
	   << data.year;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::SEND_FWD_ICD_MESSAGE_HEADER& data)
{
	bs >> data.flagSendFwd
	   >> data.cmdFormat;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::SEND_FWD_ICD_MESSAGE_HEADER& data)
{
	bs << data.flagSendFwd
	   << data.cmdFormat;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRI_PIB_STATUS& data)
{
	bs >> data.version
	   >> data.address
	   >> data.alarms
	   >> data.leak1
	   >> data.leak2
	   >> data.humidTemp
	   >> data.humidity
	   >> data.presTemp
	   >> data.pressure;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRI_PIB_STATUS& data)
{
	bs << data.version
	   << data.address
	   << data.alarms
	   << data.leak1
	   << data.leak2
	   << data.humidTemp
	   << data.humidity
	   << data.presTemp
	   << data.pressure;

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator>>(ByteStream& bs, PayloadProtocol::LRI_ID_PACKET& data)
{
	bs >> data.version
	   >> data.deviceType
	   >> data.address;
	bs.GetRaw(data.serialNum);
	bs >> data.port
	   >> data.pollingRate
	   >> data.extraInfo
	   >> data.firmwareMajor
	   >> data.firmwareMinor
	   >> data.firmwareRev;
	bs.GetRaw(data.description);

	return bs;
}

SHARED_LIB_PUBLIC ByteStream& operator<<(ByteStream& bs, const PayloadProtocol::LRI_ID_PACKET& data)
{
	bs << data.version
	   << data.deviceType
	   << data.address;
	bs.PutRaw(data.serialNum);
	bs << data.port
	   << data.pollingRate
	   << data.extraInfo
	   << data.firmwareMajor
	   << data.firmwareMinor
	   << data.firmwareRev;
	bs.PutRaw(data.description);

	return bs;
}

SHARED_LIB_PUBLIC LiquidRobotics::SMACFramework::ByteStream& operator>>(LiquidRobotics::SMACFramework::ByteStream& bs, PayloadProtocol::LRI_FLOAT_COMMAND_ACK_WRAPPER& data)
{
	bs >> data.commandFormat
	   >> data.acknowledgementCode;

	return bs;
}

SHARED_LIB_PUBLIC LiquidRobotics::SMACFramework::ByteStream& operator<<(LiquidRobotics::SMACFramework::ByteStream& bs, const PayloadProtocol::LRI_FLOAT_COMMAND_ACK_WRAPPER& data)
{
	bs << data.commandFormat
	   << data.acknowledgementCode;

	return bs;
}

}
}
