ostinato/common/payload.cpp
Srivats P. 1357f495ac Major rewrite of the protocol framework - changes not yet complete
Common
------
- Change in OstProto
	- Individual protocols are now extensions of (new) message 'Protocol' instead of 'Stream'
	- Stream now contains a repeated Protocol which also determines the ordered set of currently selected protocols; StreamCore.frame_proto which was doing this earlier has been removed

- Change in AbstractProtocol Interface
	- Parent changed to StreamBase
	- Corresponding change in constructor and factory func - createInstance()
	- new method protocolNumber() - returns unique id for each protocol
	- protoDataCopyInto/From() now copies into OstProto::Protocol instead of OstProto::Stream
- Change in all subclasses of AbstractProtocol to match new interface
- For all protocols, configFrom is no longer static, but each protocol has its own configForm
	- configForm creation is lazy
	- configForm is still a child of the protocol i.e. it will be destroyed alongwith the protocol
	- TODO: convert configWidget() to a pure factory function i.e. the protocol does not own the configForm - this requires us to pass the widget into load/storeConfigWidget() methods

- ProtocolCollection class removed alongwith its .h and .cpp
- ProtocolList class redefined to serve the purpose of ProtocolCollection
- New class ProtocolListIterator defined to iterate ProtocolList
- AbstractProtocol methods now use the ProtocolListIterator
- Factory function createProtocol() added to ProtocolManager
- OstProto::StreamCore accessor functions moved from Stream to StreamBase

Server
------
- MyService uses the newly moved accessors to StreamBase for OstProto::StreamCore members
- StreamInfo now uses the protocols to create the packet

Client
------
- StreamConfigDialog now uses ProtocolListIterator
- So does PacketModel
2009-08-02 14:52:34 +00:00

206 lines
4.4 KiB
C++

#include <qendian.h>
#include <QHostAddress>
//#include "../client/stream.h"
#include "payload.h"
#include "streambase.h"
#define SZ_FCS 4
PayloadConfigForm::PayloadConfigForm(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
}
void PayloadConfigForm::on_cmbPatternMode_currentIndexChanged(int index)
{
switch(index)
{
case OstProto::Payload::e_dp_fixed_word:
lePattern->setEnabled(true);
break;
case OstProto::Payload::e_dp_inc_byte:
case OstProto::Payload::e_dp_dec_byte:
case OstProto::Payload::e_dp_random:
lePattern->setDisabled(true);
break;
default:
qWarning("Unhandled/Unknown PatternMode = %d",index);
}
}
PayloadProtocol::PayloadProtocol(StreamBase *stream)
: AbstractProtocol(stream)
{
configForm = NULL;
}
PayloadProtocol::~PayloadProtocol()
{
delete configForm;
}
AbstractProtocol* PayloadProtocol::createInstance(StreamBase *stream)
{
return new PayloadProtocol(stream);
}
quint32 PayloadProtocol::protocolNumber() const
{
return OstProto::Protocol::kPayloadFieldNumber;
}
void PayloadProtocol::protoDataCopyInto(OstProto::Protocol &protocol) const
{
protocol.MutableExtension(OstProto::payload)->CopyFrom(data);
protocol.mutable_protocol_id()->set_id(protocolNumber());
}
void PayloadProtocol::protoDataCopyFrom(const OstProto::Protocol &protocol)
{
if (protocol.protocol_id().id() == protocolNumber() &&
protocol.HasExtension(OstProto::payload))
data.MergeFrom(protocol.GetExtension(OstProto::payload));
}
QString PayloadProtocol::name() const
{
return QString("Payload Data");
}
QString PayloadProtocol::shortName() const
{
return QString("DATA");
}
int PayloadProtocol::protocolFrameSize() const
{
return (mpStream->frameLen() - protocolFrameOffset() - SZ_FCS);
}
int PayloadProtocol::fieldCount() const
{
return payload_fieldCount;
}
AbstractProtocol::FieldFlags PayloadProtocol::fieldFlags(int index) const
{
AbstractProtocol::FieldFlags flags;
flags = AbstractProtocol::fieldFlags(index);
switch (index)
{
case payload_dataPattern:
break;
// Meta fields
case payload_dataPatternMode:
flags |= FieldIsMeta;
break;
}
return flags;
}
QVariant PayloadProtocol::fieldData(int index, FieldAttrib attrib,
int streamIndex) const
{
switch (index)
{
case payload_dataPattern:
switch(attrib)
{
case FieldName:
return QString("Data");
case FieldValue:
return data.pattern();
case FieldTextValue:
return QString(fieldData(index, FieldFrameValue,
streamIndex).toByteArray().toHex());
case FieldFrameValue:
{
QByteArray fv;
int dataLen;
dataLen = mpStream->frameLen() - protocolFrameOffset();
dataLen -= SZ_FCS;
fv.resize(dataLen+4);
switch(data.pattern_mode())
{
case OstProto::Payload::e_dp_fixed_word:
for (int i = 0; i < (dataLen/4)+1; i++)
qToBigEndian((quint32) data.pattern(),
(uchar*)(fv.data()+(i*4)) );
break;
case OstProto::Payload::e_dp_inc_byte:
for (int i = 0; i < dataLen; i++)
fv[i] = i % (0xFF + 1);
break;
case OstProto::Payload::e_dp_dec_byte:
for (int i = 0; i < dataLen; i++)
fv[i] = 0xFF - (i % (0xFF + 1));
break;
case OstProto::Payload::e_dp_random:
//! \todo cksum will be incorrect for random pattern
for (int i = 0; i < dataLen; i++)
fv[i] = qrand() % (0xFF + 1);
break;
default:
qWarning("Unhandled data pattern %d",
data.pattern_mode());
}
fv.resize(dataLen);
return fv;
}
default:
break;
}
break;
// Meta fields
case payload_dataPatternMode:
default:
break;
}
return AbstractProtocol::fieldData(index, attrib, streamIndex);
}
bool PayloadProtocol::setFieldData(int index, const QVariant &value,
FieldAttrib attrib)
{
// FIXME
return false;
}
QWidget* PayloadProtocol::configWidget()
{
if (configForm == NULL)
configForm = new PayloadConfigForm;
return configForm;
}
void PayloadProtocol::loadConfigWidget()
{
configWidget();
configForm->cmbPatternMode->setCurrentIndex(data.pattern_mode());
configForm->lePattern->setText(uintToHexStr(data.pattern(), 4));
}
void PayloadProtocol::storeConfigWidget()
{
bool isOk;
configWidget();
data.set_pattern_mode((OstProto::Payload::DataPatternMode)
configForm->cmbPatternMode->currentIndex());
data.set_pattern(configForm->lePattern->text().remove(QChar(' ')).toULong(&isOk, 16));
}