Masterctrl/Sources/EthernetNetworkServer.cpp
2016-01-12 06:52:12 -05:00

216 lines
5.7 KiB
C++

#include "EthernetNetworkServer.h"
CEthernetNetworkServer::CEthernetNetworkServer()
{
SetManualPacketReset(true);
mEthernetNetworkServer = new QTcpServer;
connect(mEthernetNetworkServer,SIGNAL(newConnection()),this,SLOT(EthernetNetworkServerConnected()));
mEthernetNetworkServer->listen(QHostAddress::Any,80);
}
int CEthernetNetworkServer::NewFrameReceived(QByteArray Frame)
{
Q_UNUSED(Frame)
return RET_OK;
}
void CEthernetNetworkServer::EthernetNetworkServerConnected()
{
qDebug("Device Connected");
QTcpSocket *EthernetDeviceSocket;
EthernetDeviceSocket = mEthernetNetworkServer->nextPendingConnection();
CEthernetDeviceConnection *NewConnection = new CEthernetDeviceConnection;
NewConnection->mConnectionSocket = EthernetDeviceSocket;
mEthernetDeviceConnections.append(NewConnection);
connect(EthernetDeviceSocket,SIGNAL(readyRead()),this,SLOT(DeviceSocketDataAvail()));
//request device info...
QByteArray Frame = GetTxPacket((unsigned char)ETH_NETWK_DEVICE_INFO_REQUEST,0,0,0,BROADCAST_VALUE,(unsigned char)ID_ETHERNET_VIRTUAL);
EthernetDeviceSocket->write(Frame);
}
CEthernetDeviceConnection * CEthernetNetworkServer::FindConnection(QTcpSocket *Socket)
{
for(int i = 0; i < mEthernetDeviceConnections.size(); i++)
{
if(Socket == mEthernetDeviceConnections.at(i)->mConnectionSocket)
{
return mEthernetDeviceConnections.at(i);
}
}
return 0;
}
int CEthernetNetworkServer::FindConnection(CEthernetDeviceConnection* Connection)
{
for(int i = 0; i < mEthernetDeviceConnections.size(); i++)
{
if(Connection == mEthernetDeviceConnections.at(i))
{
return i;
}
}
return -1;
}
void CEthernetNetworkServer::DeviceSocketDataAvail()
{
QTcpSocket *DeviceSocket = (QTcpSocket*)QObject::sender();
QByteArray Packet = DeviceSocket->readAll();
CEthernetDeviceConnection *DeviceConnection = FindConnection(DeviceSocket);
int ret = AnalyzeRxBuffer(Packet);
bool CloseSocket = false;
// if(DeviceConnection == 0)
// {
// qDebug("Rx from unknown socket!");
// CloseSocket = true;
// }
switch(ret)
{
case PROTOCOL_RET_OK_PACKET_COMPLETE:
{
if(ProtocolGetSenderID() == ID_ETHERNET_VIRTUAL)
{
if(ProtocolGetCmd() == ETH_NETWK_DEVICE_INFO_RESPONSE)
{
QByteArray Data = ProtocolGetData();
char DeviceID,DeviceAddress;
DeviceID = Data.at(0);
DeviceAddress = Data.at(1);
if(IsDeviceRegistered(DeviceID) == true)
{
DeviceConnection->mConnectedDevice->mDeviceID = (int)DeviceID;
DeviceConnection->mConnectedDevice->mDeviceAddress = (int)DeviceAddress;
}
else
{
qDebug("Rx device info from unregistered device");
CloseSocket = true;
}
}
}
else
{
if(DeviceConnection != 0)
{
DeviceConnection->mConnectedDevice->NewDeviceFrameReceived(ProtocolGetSenderID(),ProtocolGetSenderAddress(),ProtocolGetCmd(),ProtocolGetDataSize(),ProtocolGetData());
PrepareForNewPacket();
}
else
{
qDebug("Rx from invalid device connection pointer!");
CloseSocket = true;
}
}
break;
}
case PROTOCOL_RET_OK_PACKET_INCOMPLETE:
{
break;
}
case PROTOCOL_RET_OK_BAD_HEADER:
{
break;
}
case PROTOCOL_RET_ERROR_INVALID_TARGET_DEVICE:
{
qDebug("Bad target device received on Ethernet network");
PrepareForNewPacket();
break;
}
case PROTOCOL_RET_ERROR_INVALID_TARGET_ADDRESS:
{
qDebug("Bad target address received on Ethernet network");
PrepareForNewPacket();
break;
}
case PROTOCOL_RET_ERROR_BAD_CRC:
{
qDebug("Bad target CRC on Ethernet network");
PrepareForNewPacket();
break;
}
case PROTOCOL_RET_ERROR_SM_LOGIC:
{
qDebug("LOGIC ERROR!!! on Ethernet network protocol");
PrepareForNewPacket();
break;
}
}
if(CloseSocket == true)
{
if(CloseConnection(DeviceConnection) != RET_OK)
{
DeviceSocket->close();
delete DeviceSocket;
}
PrepareForNewPacket();
}
}
void CEthernetNetworkServer::DeviceSocketDisconnected()
{
}
int CEthernetNetworkServer::CloseConnection(CEthernetDeviceConnection *Connection)
{
if(Connection != 0)
{
int index = FindConnection(Connection);
if(index != -1)
{
Connection->mConnectionSocket->close();
mEthernetDeviceConnections.removeAt(index);
delete Connection;
return RET_OK;
}
}
return RET_ERROR;
}
int CEthernetNetworkServer::RegisterNewDevice(CNetworkDevice *Device)
{
if(IsDeviceRegistered(Device->GetDeviceID()))
{
qDebug("Device already registered");
return RET_ERROR;
}
mNetworkDevices.append(Device);
return RET_OK;
}
bool CEthernetNetworkServer::IsDeviceRegistered(int DeviceID)
{
for(int i = 0; i < mNetworkDevices.size(); i++)
{
if(mNetworkDevices.at(i)->GetDeviceID() == DeviceID)
{
return true;
}
}
return true;
}