#include "EthernetNetworkCommIF.h"
#include "NetworkDevice.h"



CEthernetNetworkCommIF::CEthernetNetworkCommIF(QTcpSocket *Socket)
{
    mTCPSocket = Socket;

    connect(mTCPSocket,SIGNAL(disconnected()),this,SLOT(SocketDisconnected()));
    connect(mTCPSocket,SIGNAL(readyRead()),this,SLOT(SocketDataAvailable()));
}

CEthernetNetworkCommIF::~CEthernetNetworkCommIF()
{

}


int CEthernetNetworkCommIF::SendNetworkMessage(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray *Data)
{
//    Q_UNUSED(DeviceID)
//    Q_UNUSED(DeviceAddress)
//    Q_UNUSED(MessageID)
//    Q_UNUSED(DataSize)
//    Q_UNUSED(Data)
    if(mTCPSocket == 0)
        return RET_ERROR;

    QByteArray Packet;
    if(Data != 0)
    {
        Packet = GetTxPacket(MessageID,0,Data->data(),DataSize,DeviceAddress,DeviceID);
    }
    else
    {
        Packet = GetTxPacket(MessageID,0,0,0,DeviceAddress,DeviceID);
    }

    mTCPSocket->write(Packet);

    return RET_OK;
}

int CEthernetNetworkCommIF::NewFrameReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data)
{
//    Q_UNUSED(DeviceID)
//    Q_UNUSED(DeviceAddress)
//    Q_UNUSED(MessageID)
//    Q_UNUSED(DataSize)
//    Q_UNUSED(Data)

    mDevicePtr->NewDeviceFrameReceived(DeviceID,DeviceAddress,MessageID,DataSize,Data);


    return RET_OK;
}

void CEthernetNetworkCommIF::SocketDataAvailable()
{
    AnalyzeRxBuffer(mTCPSocket->readAll());
}

void CEthernetNetworkCommIF::SocketDisconnected()
{
   // delete mTCPSocket;
    emit EthernetInterfaceSocketDisconnected(mDevicePtr);
}
