1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
/*
Copyright (C) 2010, 2013-2014 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "payload.h"
#include "streambase.h"
PayloadProtocol::PayloadProtocol(StreamBase *stream, AbstractProtocol *parent)
: AbstractProtocol(stream, parent)
{
}
PayloadProtocol::~PayloadProtocol()
{
}
AbstractProtocol* PayloadProtocol::createInstance(StreamBase *stream,
AbstractProtocol *parent)
{
return new PayloadProtocol(stream, parent);
}
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(int streamIndex) const
{
int len;
len = mpStream->frameLen(streamIndex) - protocolFrameOffset(streamIndex)
- protocolFramePayloadSize(streamIndex) - kFcsSize;
if (len < 0)
len = 0;
qDebug("%s: this = %p, streamIndex = %d, len = %d", __FUNCTION__, this,
streamIndex, len);
return len;
}
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 &= ~FrameField;
flags |= MetaField;
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 = protocolFrameSize(streamIndex);
// FIXME: Hack! Bad! Bad! Very Bad!!!
if (dataLen <= 0)
dataLen = 1;
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 (HIGH) cksum is 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:
switch(attrib)
{
case FieldValue: return data.pattern_mode();
default: break;
}
break;
default:
break;
}
return AbstractProtocol::fieldData(index, attrib, streamIndex);
}
bool PayloadProtocol::setFieldData(int index, const QVariant &value,
FieldAttrib attrib)
{
bool isOk = false;
if (attrib != FieldValue)
return false;
switch (index)
{
case payload_dataPattern:
{
uint pattern = value.toUInt(&isOk);
if (isOk)
data.set_pattern(pattern);
break;
}
case payload_dataPatternMode:
{
uint mode = value.toUInt(&isOk);
if (isOk && data.DataPatternMode_IsValid(mode))
data.set_pattern_mode(OstProto::Payload::DataPatternMode(mode));
else
isOk = false;
break;
}
default:
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
index);
break;
}
return isOk;
}
bool PayloadProtocol::isProtocolFrameValueVariable() const
{
return (AbstractProtocol::isProtocolFrameValueVariable()
|| isProtocolFrameSizeVariable());
}
bool PayloadProtocol::isProtocolFrameSizeVariable() const
{
if (mpStream->lenMode() == StreamBase::e_fl_fixed)
return false;
else
return true;
}
int PayloadProtocol::protocolFrameVariableCount() const
{
int count = AbstractProtocol::protocolFrameVariableCount();
if (data.pattern_mode() == OstProto::Payload::e_dp_random)
return mpStream->frameCount();
count = AbstractProtocol::lcm(count, mpStream->frameSizeVariableCount());
return count;
}
|