2009-05-24 09:54:11 -05:00
|
|
|
#include <qendian.h>
|
|
|
|
|
|
|
|
#include "sample.h"
|
|
|
|
|
|
|
|
SampleConfigForm::SampleConfigForm(QWidget *parent)
|
2009-12-28 02:31:28 -06:00
|
|
|
: QWidget(parent)
|
2009-05-24 09:54:11 -05:00
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
setupUi(this);
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
2009-11-16 07:09:20 -06:00
|
|
|
SampleProtocol::SampleProtocol(StreamBase *stream, AbstractProtocol *parent)
|
2009-12-28 02:31:28 -06:00
|
|
|
: AbstractProtocol(stream, parent)
|
2009-05-24 09:54:11 -05:00
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
configForm = NULL;
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
SampleProtocol::~SampleProtocol()
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
delete configForm;
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
2009-10-14 10:16:56 -05:00
|
|
|
AbstractProtocol* SampleProtocol::createInstance(StreamBase *stream,
|
2009-12-28 02:31:28 -06:00
|
|
|
AbstractProtocol *parent)
|
2009-05-24 09:54:11 -05:00
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
return new SampleProtocol(stream, parent);
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
2009-08-02 09:52:34 -05:00
|
|
|
quint32 SampleProtocol::protocolNumber() const
|
2009-05-24 09:54:11 -05:00
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
return OstProto::Protocol::kSampleFieldNumber;
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
2009-08-02 09:52:34 -05:00
|
|
|
void SampleProtocol::protoDataCopyInto(OstProto::Protocol &protocol) const
|
2009-05-24 09:54:11 -05:00
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
protocol.MutableExtension(OstProto::sample)->CopyFrom(data);
|
|
|
|
protocol.mutable_protocol_id()->set_id(protocolNumber());
|
2009-08-02 09:52:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleProtocol::protoDataCopyFrom(const OstProto::Protocol &protocol)
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
if (protocol.protocol_id().id() == protocolNumber() &&
|
|
|
|
protocol.HasExtension(OstProto::sample))
|
|
|
|
data.MergeFrom(protocol.GetExtension(OstProto::sample));
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SampleProtocol::name() const
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
return QString("Sample Protocol");
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SampleProtocol::shortName() const
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
return QString("SAMPLE");
|
2009-11-16 07:09:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Return the ProtocolIdType for your protocol \n
|
|
|
|
|
|
|
|
If your protocol doesn't have a protocolId field, you don't need to
|
|
|
|
reimplement this method - the base class implementation will do the
|
|
|
|
right thing
|
|
|
|
*/
|
|
|
|
AbstractProtocol::ProtocolIdType SampleProtocol::protocolIdType() const
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
return ProtocolIdIp;
|
2009-11-16 07:09:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Return the protocolId for your protoocol based on the 'type' requested \n
|
|
|
|
|
|
|
|
If not all types are valid for your protocol, handle the valid type(s)
|
|
|
|
and for the remaining fallback to the base class implementation; if your
|
|
|
|
protocol doesn't have a protocolId at all, you don't need to reimplement
|
|
|
|
this method - the base class will do the right thing
|
|
|
|
*/
|
|
|
|
quint32 SampleProtocol::protocolId(ProtocolIdType type) const
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case ProtocolIdIp: return 1234;
|
|
|
|
default:break;
|
|
|
|
}
|
2009-11-16 07:09:20 -06:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
return AbstractProtocol::protocolId(type);
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
int SampleProtocol::fieldCount() const
|
2009-05-24 09:54:11 -05:00
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
return sample_fieldCount;
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
AbstractProtocol::FieldFlags SampleProtocol::fieldFlags(int index) const
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
AbstractProtocol::FieldFlags flags;
|
2009-05-24 09:54:11 -05:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
flags = AbstractProtocol::fieldFlags(index);
|
2009-05-24 09:54:11 -05:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case sample_a:
|
|
|
|
case sample_b:
|
|
|
|
case sample_payloadLength:
|
|
|
|
break;
|
2009-05-24 09:54:11 -05:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
case sample_checksum:
|
|
|
|
flags |= FieldIsCksum;
|
|
|
|
break;
|
2009-05-24 09:54:11 -05:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
case sample_x:
|
|
|
|
case sample_y:
|
|
|
|
break;
|
2009-11-16 07:09:20 -06:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
case sample_is_override_checksum:
|
|
|
|
flags |= FieldIsMeta;
|
|
|
|
break;
|
2009-05-24 09:54:11 -05:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
default:
|
|
|
|
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
|
|
|
|
index);
|
|
|
|
break;
|
|
|
|
}
|
2009-05-24 09:54:11 -05:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
return flags;
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant SampleProtocol::fieldData(int index, FieldAttrib attrib,
|
2009-12-28 02:31:28 -06:00
|
|
|
int streamIndex) const
|
2009-05-24 09:54:11 -05:00
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case sample_a:
|
|
|
|
{
|
|
|
|
int a = data.ab() >> 13;
|
|
|
|
|
|
|
|
switch(attrib)
|
|
|
|
{
|
|
|
|
case FieldName:
|
|
|
|
return QString("A");
|
|
|
|
case FieldValue:
|
|
|
|
return a;
|
|
|
|
case FieldTextValue:
|
|
|
|
return QString("%1").arg(a);
|
|
|
|
case FieldFrameValue:
|
|
|
|
return QByteArray(1, (char) a);
|
|
|
|
case FieldBitSize:
|
|
|
|
return 3;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
case sample_b:
|
|
|
|
{
|
|
|
|
int b = data.ab() & 0x1FFF;
|
|
|
|
|
|
|
|
switch(attrib)
|
|
|
|
{
|
|
|
|
case FieldName:
|
|
|
|
return QString("B");
|
|
|
|
case FieldValue:
|
|
|
|
return b;
|
|
|
|
case FieldTextValue:
|
|
|
|
return QString("%1").arg(b, 4, BASE_HEX, QChar('0'));
|
|
|
|
case FieldFrameValue:
|
|
|
|
{
|
|
|
|
QByteArray fv;
|
|
|
|
fv.resize(2);
|
|
|
|
qToBigEndian((quint16) b, (uchar*) fv.data());
|
|
|
|
return fv;
|
|
|
|
}
|
|
|
|
case FieldBitSize:
|
|
|
|
return 13;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case sample_payloadLength:
|
|
|
|
{
|
|
|
|
switch(attrib)
|
|
|
|
{
|
|
|
|
case FieldName:
|
|
|
|
return QString("Payload Length");
|
|
|
|
case FieldValue:
|
|
|
|
return protocolFramePayloadSize(streamIndex);
|
|
|
|
case FieldFrameValue:
|
|
|
|
{
|
|
|
|
QByteArray fv;
|
|
|
|
int totlen;
|
|
|
|
totlen = protocolFramePayloadSize(streamIndex);
|
|
|
|
fv.resize(2);
|
|
|
|
qToBigEndian((quint16) totlen, (uchar*) fv.data());
|
|
|
|
return fv;
|
|
|
|
}
|
|
|
|
case FieldTextValue:
|
|
|
|
return QString("%1").arg(
|
|
|
|
protocolFramePayloadSize(streamIndex));
|
|
|
|
case FieldBitSize:
|
|
|
|
return 16;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sample_checksum:
|
|
|
|
{
|
|
|
|
quint16 cksum;
|
|
|
|
|
|
|
|
switch(attrib)
|
|
|
|
{
|
|
|
|
case FieldValue:
|
|
|
|
case FieldFrameValue:
|
|
|
|
case FieldTextValue:
|
|
|
|
{
|
|
|
|
if (data.is_override_checksum())
|
|
|
|
cksum = data.checksum();
|
|
|
|
else
|
|
|
|
cksum = protocolFrameCksum(streamIndex, CksumIp);
|
|
|
|
}
|
|
|
|
default:
|
2010-01-03 02:56:16 -06:00
|
|
|
cksum = 0; // avoid the 'maybe used unitialized' warning
|
2009-12-28 02:31:28 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(attrib)
|
|
|
|
{
|
|
|
|
case FieldName:
|
|
|
|
return QString("Checksum");
|
|
|
|
case FieldValue:
|
|
|
|
return cksum;
|
|
|
|
case FieldFrameValue:
|
|
|
|
{
|
|
|
|
QByteArray fv;
|
|
|
|
|
|
|
|
fv.resize(2);
|
|
|
|
qToBigEndian(cksum, (uchar*) fv.data());
|
|
|
|
return fv;
|
|
|
|
}
|
|
|
|
case FieldTextValue:
|
|
|
|
return QString("0x%1").arg(
|
|
|
|
cksum, 4, BASE_HEX, QChar('0'));;
|
|
|
|
case FieldBitSize:
|
|
|
|
return 16;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sample_x:
|
|
|
|
{
|
|
|
|
switch(attrib)
|
|
|
|
{
|
|
|
|
case FieldName:
|
|
|
|
return QString("X");
|
|
|
|
case FieldValue:
|
|
|
|
return data.x();
|
|
|
|
case FieldTextValue:
|
|
|
|
return QString("%1").arg(data.x());
|
|
|
|
//return QString("%1").arg(data.x(), 8, BASE_HEX, QChar('0'));
|
|
|
|
case FieldFrameValue:
|
|
|
|
{
|
|
|
|
QByteArray fv;
|
|
|
|
fv.resize(4);
|
|
|
|
qToBigEndian((quint32) data.x(), (uchar*) fv.data());
|
|
|
|
return fv;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sample_y:
|
|
|
|
{
|
|
|
|
switch(attrib)
|
|
|
|
{
|
|
|
|
case FieldName:
|
|
|
|
return QString("Y");
|
|
|
|
case FieldValue:
|
|
|
|
return data.y();
|
|
|
|
case FieldTextValue:
|
|
|
|
//return QString("%1").arg(data.y());
|
|
|
|
return QString("%1").arg(data.y(), 8, BASE_HEX, QChar('0'));
|
|
|
|
case FieldFrameValue:
|
|
|
|
{
|
|
|
|
QByteArray fv;
|
|
|
|
fv.resize(4);
|
|
|
|
qToBigEndian((quint32) data.y(), (uchar*) fv.data());
|
|
|
|
return fv;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Meta fields
|
|
|
|
case sample_is_override_checksum:
|
|
|
|
{
|
|
|
|
switch(attrib)
|
|
|
|
{
|
|
|
|
case FieldValue:
|
|
|
|
return data.is_override_checksum();
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
|
|
|
|
index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AbstractProtocol::fieldData(index, attrib, streamIndex);
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SampleProtocol::setFieldData(int index, const QVariant &value,
|
2009-12-28 02:31:28 -06:00
|
|
|
FieldAttrib attrib)
|
2009-05-24 09:54:11 -05:00
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
bool isOk = false;
|
|
|
|
|
|
|
|
if (attrib != FieldValue)
|
|
|
|
goto _exit;
|
|
|
|
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case sample_a:
|
|
|
|
{
|
|
|
|
uint a = value.toUInt(&isOk);
|
|
|
|
if (isOk)
|
|
|
|
data.set_ab((data.ab() & 0xe000) | (a << 13));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sample_b:
|
|
|
|
{
|
|
|
|
uint b = value.toUInt(&isOk);
|
|
|
|
if (isOk)
|
|
|
|
data.set_ab((data.ab() & 0x1FFF) | b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sample_payloadLength:
|
|
|
|
{
|
|
|
|
uint len = value.toUInt(&isOk);
|
|
|
|
if (isOk)
|
|
|
|
data.set_payload_length(len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sample_checksum:
|
|
|
|
{
|
|
|
|
uint csum = value.toUInt(&isOk);
|
|
|
|
if (isOk)
|
|
|
|
data.set_checksum(csum);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sample_x:
|
|
|
|
{
|
|
|
|
uint x = value.toUInt(&isOk);
|
|
|
|
if (isOk)
|
|
|
|
data.set_x(x);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sample_y:
|
|
|
|
{
|
|
|
|
uint y = value.toUInt(&isOk);
|
|
|
|
if (isOk)
|
|
|
|
data.set_y(y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sample_is_override_checksum:
|
|
|
|
{
|
|
|
|
bool ovr = value.toBool();
|
|
|
|
data.set_is_override_checksum(ovr);
|
|
|
|
isOk = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
|
|
|
|
index);
|
|
|
|
break;
|
|
|
|
}
|
2009-11-16 07:09:20 -06:00
|
|
|
|
|
|
|
_exit:
|
2009-12-28 02:31:28 -06:00
|
|
|
return isOk;
|
2009-11-16 07:09:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Return the protocol frame size in bytes\n
|
|
|
|
|
|
|
|
If your protocol has a fixed size - you don't need to reimplement this; the
|
|
|
|
base class implementation is good enough
|
|
|
|
*/
|
|
|
|
int SampleProtocol::protocolFrameSize(int streamIndex) const
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
return AbstractProtocol::protocolFrameSize(streamIndex);
|
2009-11-16 07:09:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
If your protocol has any variable fields, return true \n
|
|
|
|
|
|
|
|
Otherwise you don't need to reimplement this method - the base class always
|
|
|
|
returns false
|
|
|
|
*/
|
|
|
|
bool SampleProtocol::isProtocolFrameValueVariable() const
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
return false;
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
2009-11-16 07:09:20 -06:00
|
|
|
/*!
|
|
|
|
If your protocol frame size can vary across pkts of the same stream,
|
|
|
|
return true \n
|
|
|
|
|
|
|
|
Otherwise you don't need to reimplement this method - the base class always
|
|
|
|
returns false
|
|
|
|
*/
|
|
|
|
bool SampleProtocol::isProtocolFrameSizeVariable() const
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
return false;
|
2009-11-16 07:09:20 -06:00
|
|
|
}
|
2009-05-24 09:54:11 -05:00
|
|
|
|
|
|
|
QWidget* SampleProtocol::configWidget()
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
if (configForm == NULL)
|
|
|
|
{
|
|
|
|
configForm = new SampleConfigForm;
|
|
|
|
loadConfigWidget();
|
|
|
|
}
|
2009-08-02 09:52:34 -05:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
return configForm;
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleProtocol::loadConfigWidget()
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
configWidget();
|
2009-11-16 07:09:20 -06:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
configForm->sampleA->setText(fieldData(sample_a, FieldValue).toString());
|
|
|
|
configForm->sampleB->setText(fieldData(sample_b, FieldValue).toString());
|
2009-11-16 07:09:20 -06:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
configForm->samplePayloadLength->setText(
|
|
|
|
fieldData(sample_payloadLength, FieldValue).toString());
|
2009-11-16 07:09:20 -06:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
configForm->isChecksumOverride->setChecked(
|
|
|
|
fieldData(sample_is_override_checksum, FieldValue).toBool());
|
|
|
|
configForm->sampleChecksum->setText(uintToHexStr(
|
|
|
|
fieldData(sample_checksum, FieldValue).toUInt(), 2));
|
2009-11-16 07:09:20 -06:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
configForm->sampleX->setText(fieldData(sample_x, FieldValue).toString());
|
|
|
|
configForm->sampleY->setText(fieldData(sample_y, FieldValue).toString());
|
2009-11-16 07:09:20 -06:00
|
|
|
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleProtocol::storeConfigWidget()
|
|
|
|
{
|
2009-12-28 02:31:28 -06:00
|
|
|
bool isOk;
|
2009-08-02 09:52:34 -05:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
configWidget();
|
|
|
|
setFieldData(sample_a, configForm->sampleA->text());
|
|
|
|
setFieldData(sample_b, configForm->sampleB->text());
|
2009-11-16 07:09:20 -06:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
setFieldData(sample_payloadLength, configForm->samplePayloadLength->text());
|
|
|
|
setFieldData(sample_is_override_checksum,
|
|
|
|
configForm->isChecksumOverride->isChecked());
|
|
|
|
setFieldData(sample_checksum, configForm->sampleChecksum->text().toUInt(&isOk, BASE_HEX));
|
2009-11-16 07:09:20 -06:00
|
|
|
|
2009-12-28 02:31:28 -06:00
|
|
|
setFieldData(sample_x, configForm->sampleX->text());
|
|
|
|
setFieldData(sample_y, configForm->sampleY->text());
|
2009-05-24 09:54:11 -05:00
|
|
|
}
|
|
|
|
|