|
|
|
Re: RAW Encoder and Decoder [message #1804783 is a reply to message #1804707] |
Mon, 01 April 2019 08:30 |
|
Hi le tian,
I see the misunderstanding;
A few clarifications first:
-the decoder will read the stream byte by byte; at each byte it will have to be able to take a decision on how to continue; that is , it will not read the whole message and then parse and evaluate it;
-this means that your structure is not decodable: let's assume that the second field is omitted ; when reading the first byte of the third field ,no decoder can decide whether this is indeed the first byte of the third field or the first byte of the un-omitted second field; another piece of auxiliary information is necessary to do that
-protocols are designed specifically to help the decoder; not every protocol is decodable (although every protocol can be encoded)
So what you are missing in your simple protocol is an additional piece of information that would signal the absence or presence of your second optional field.
For that , the RAW attribute PRESENCE can be used.
One more remark here:
the ...length(2) expression is addressed to the compiler and it is disregarded by the codec; it is being used during compilation to check type restrictions;
to communicate this information to the codec, one will need a
with { variant "FIELDLENGTH(2)" };
encoding variant; this is being used during runtime. while encoding/decoding.
A complete example would look as below:
module RAWTest2
{
external function enc_data(in Test_data pdu) return octetstring
with { extension "prototype(convert)"
extension "encode(RAW)"
};
external function dec_data(in octetstring stream) return Test_data
with { extension "prototype(convert)"
extension "decode(RAW)"
extension "errorbehavior(ALL:WARNING)"
};
type octetstring OCT1 length(1) with { variant "FIELDLENGTH(1)" };
type octetstring OCT2 length(2) with { variant "FIELDLENGTH(2)" };
type record Test_data
{
OCT2 field1,
OCT1 presence_field,
OCT2 field2 optional,
OCT2 field3
} with {variant (field2) "PRESENCE(presence_field = 'FF'O)" };
template Test_data t1:=
{
field1:='AABB'O,
presence_field:='FF'O,
field2:='CCDD'O,
field3:='EEFF'O
}
template Test_data t2:=
{
field1:='AABB'O,
presence_field:='00'O,
field2:= omit,
field3:='EEFF'O
}
control {
log("Encode ", enc_data(valueof(t1)))
log("Decode", dec_data(enc_data(valueof(t1))))
log("----------------------------------------------------------------------------------")
log("Encode ", enc_data(valueof(t2)))
log("Decode", dec_data(enc_data(valueof(t2))))
log("----------------------------------------------------------------------------------")
}
}with { encode "RAW" };
and the execution log :
09:07:37.772726 - TTCN-3 Test Executor started in single mode. Version: CRL 113 200/6 R5B.
09:07:37.772820 - Maximum number of open file descriptors: 8192, FD_SETSIZE = 1024
09:07:37.776253 - TTCN Logger v2.2 options: TimeStampFormat:=Time; LogEntityName:=No; LogEventTypes:=No; SourceInfoFormat:=Single; *.FileMask:=LOG_ALL; *.ConsoleMask:=ACTION | ERROR | TESTCASE | STATISTICS_VERDICT | STATISTICS_UNQUALIFIED | WARNING; LogFileSize:=0; LogFileNumber:=1; DiskFullAction:=Error
09:07:37.776291 - Initializing module PreGenRecordOf.
09:07:37.776310 - Initialization of module PreGenRecordOf finished.
09:07:37.776327 - Initializing module RAWTest2.
09:07:37.776362 - Initialization of module RAWTest2 finished.
09:07:37.776379 - Initializing module TitanLoggerApi.
09:07:37.776396 - Initialization of module TitanLoggerApi finished.
09:07:37.776419 RAWTest2.ttcn:48 Execution of control part in module RAWTest2 started.
09:07:37.776501 RAWTest2.ttcn:51 Encode 'AABBFFCCDDEEFF'O
09:07:37.777876 RAWTest2.ttcn:53 Decode{ field1 := 'AABB'O, presence_field := 'FF'O, field2 := 'CCDD'O, field3 := 'EEFF'O }
09:07:37.777913 RAWTest2.ttcn:55 ----------------------------------------------------------------------------------
09:07:37.777931 RAWTest2.ttcn:58 Encode 'AABB00EEFF'O
09:07:37.777955 RAWTest2.ttcn:60 Decode{ field1 := 'AABB'O, presence_field := '00'O, field2 := omit, field3 := 'EEFF'O }
09:07:37.777983 RAWTest2.ttcn:62 ----------------------------------------------------------------------------------
09:07:37.778002 RAWTest2.ttcn:62 Execution of control part in module RAWTest2 finished.
09:07:37.778117 - Verdict statistics: 0 none, 0 pass, 0 inconc, 0 fail, 0 error.
09:07:37.778176 - Test execution summary: 0 test case was executed. Overall verdict: none
09:07:37.778216 - TTCN-3 Test Executor finished in single mode.
for details on RAW, please see also:
The binary RAW encoder of Titan : part I(Basic principles)
https://www.eclipse.org/forums/index.php/t/1070954/
The binary RAW encoder of Titan : part II(Examples of attributes )
https://www.eclipse.org/forums/index.php/t/1071180/
The binary RAW encoder of Titan : part III(Automatic generation of codec functions - Function prototypes)
https://www.eclipse.org/forums/index.php/t/1071394/
I hope this helps
BR
Elemer
-
Attachment: RAWTest2.ttcn
(Size: 1.25KB, Downloaded 93 times) -
Attachment: Makefile
(Size: 4.97KB, Downloaded 98 times)
[Updated on: Mon, 01 April 2019 13:22] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03194 seconds